Do not exit git diff automatically if the changes are on the same page

If I started git diff, and my changes were less than one page, the command will automatically exit. This is undesirable because it is in a script, and I call immediately git commit. This skips the change lists on one page.

My first thought would be to convey the differences in less, but this does not mean that change changes do not display a blank screen (which requires a click q). Here is the command I use: git diff --color=always | less.

Is there a better way to do this?

+4
source share
3 answers

bash script :

#!/bin/bash

num_lines=$(git diff | wc -l)

if [ $num_lines -eq 0 ]
then
  echo "No changes"
else
  git diff --color=always | less --raw-control-chars
fi

, @Phillip comment, @tripleee:

git diff --color=always | (IFS=$'\n' read -r A; if [ -n "$A" ]; then (printf '%s\n' "$A"; cat) | less --raw-control-chars; else echo "No changes"; fi)

git diff .

@tripiple:

read -r, . . IFS=$'\n' read -r. echo , ( echo ) - printf '%s\n' "$A", .

--raw-control-chars ( -r), less, .

+5

, , less -F git. less , less. , git FRX less. ( less, git less -FRX.)

- .gitconfig :

[core]
    pager = less -+F

less , less

LESS=FRX less -+F

, -F , less, .

( , less . , , LESS=X , -F .)

+2

less ,

export LESS=-E

. , -E ,

LESS=-E"${LESS#-}"

git, , -

alias git='LESS=-E"${LESS#-}" git'

( sh, Bash, ksh, zsh ..)

, . , .

+1

Source: https://habr.com/ru/post/1654489/


All Articles