I wrote a script to display
git diff. It will
- display the difference in
stdoutif the output fits in the terminal screen, or
- display the difference in pager otherwise.
#!/bin/sh if ! git diff "$@" > /dev/null; then # not in a Git repository exit 1 fi if [ `git diff "$@" | wc --lines` -gt `tput lines` ]; then git diff "$@" --color=always | less else git diff "$@" --color=always | cat fi
The current limitations of the script:
- If I need to do the same with
git log, I need to write another two similar scripts (which is not DRY).
- Auto-completion does not work. I need to write my own one (taking too much time).
Now I wish to write a script that does the same but works as a pager so that I can set Git configuration
core.pager as the script and solve the problem mentioned.
Note: I am using Zsh.
less --no-init --quit-if-one-screen (
less -FX), and it does almost what I need, except that it will not clear the screen in the more-than-one-page case. So actually I need the
--no-init option to be conditional: disable initialization and quit if it fits one screen; no special action otherwise. Maybe
less --no-init-if-one-screen --quit-if-one-screen?