diff options
| author | Mattias Andrée <m@maandree.se> | 2026-03-25 21:57:54 +0100 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2026-03-25 21:57:54 +0100 |
| commit | 4b0d34cd2235d7f0ae5e7be0f1821c6301ba35e4 (patch) | |
| tree | 72cbb27ef49a6d4e6d90ddd60c67b1a28c6e1440 /git/git-cdiff | |
| parent | add git-cdiff (diff) | |
| download | dotfiles-4b0d34cd2235d7f0ae5e7be0f1821c6301ba35e4.tar.gz dotfiles-4b0d34cd2235d7f0ae5e7be0f1821c6301ba35e4.tar.bz2 dotfiles-4b0d34cd2235d7f0ae5e7be0f1821c6301ba35e4.tar.xz | |
Signed-off-by: Mattias Andrée <m@maandree.se>
Diffstat (limited to '')
| -rwxr-xr-x | git/git-cdiff | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/git/git-cdiff b/git/git-cdiff index bd4ae31..15d91e9 100755 --- a/git/git-cdiff +++ b/git/git-cdiff @@ -1,19 +1,43 @@ #!/bin/dash if test -z "${GIT_CDIFF_CONTEXT}"; then git difftool -y -x "diff -c --color=always" "$@" -elif echo "${GIT_CDIFF_CONTEXT}" | grep -q '^-'; then +elif printf '%s' "${GIT_CDIFF_CONTEXT}" | grep -q '^-'; then git difftool -y -x "diff ${GIT_CDIFF_CONTEXT} --color=always" "$@" else git difftool -y -x "diff -C${GIT_CDIFF_CONTEXT} --color=always" "$@" fi | { + # this part of the pipeline is optional, it just makes GNU diff colouring bit nicer if test "$TERM" = linux; then sed '/^\(\x1b\[[0-9;]*m\)* /s/\x1b\[[0-9;]*m//g' else sed '/^\(\x1b\[[0-9;]*m\)* /s/^/\x1b\[2m/g' fi } | { + # this part of the pipeline is optional (and breaks diff format), it adds line number to the margin + if printf '%s' "${GIT_CDIFF_CONTEXT}" | grep -q '^-'; then + exec cat + fi + if test "$TERM" = linux; then + linecolour="35" + else + linecolour="2;35" + fi + lineno=x + while IFS= read -r line; do + if printf '%s\n' "$line" | sed 's/\x1b\[[0-9;]*m//g' | grep -q '^\(\*\*\* \|--- \)[0-9]*,[0-9]* \(\*\*\*\*\|----\)$'; then + lineno="$(printf '%s' "$line" | sed 's/\x1b\[[0-9;]*m//g' | sed 's/^\(\*\*\* \|--- \)\([0-9]*\),.*$/\2/')" + printf '%s\n' "$line" + elif test ! "$lineno" = x && printf '%s\n' "$line" | sed 's/\x1b\[[0-9;]*m//g' | grep -q '^\(!\|+\|-\| \) '; then + printf '\e[%sm%5i \e[m%s\n' "$linecolour" "$lineno" "$line" + lineno=$(( $lineno + 1 )) + else + lineno=x + printf '%s\n' "$line" + fi + done +} | { if test -t 1; then - less -R + less -FRX else cat fi |
