From 7d43115881d57b54789259553033d886ed12e20a Mon Sep 17 00:00:00 2001 From: carlfriedrich Date: Thu, 10 Mar 2022 11:11:08 +0100 Subject: [PATCH 1/3] gd: support passing two revisions Fixes wfxr/forgit#187 --- forgit.plugin.zsh | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index 6160f05..c8c7878 100755 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -36,22 +36,26 @@ forgit::log() { # git diff viewer forgit::diff() { forgit::inside_work_tree || return 1 - local cmd files opts commit repo + local cmd files opts commits repo [[ $# -ne 0 ]] && { if git rev-parse "$1" -- &>/dev/null ; then - commit="$1" && files=("${@:2}") + if [[ $# -gt 1 ]] && git rev-parse "$2" -- &>/dev/null; then + commits="$1 $2" && files=("${@:3}") + else + commits="$1" && files=("${@:2}") + fi else files=("$@") fi } repo="$(git rev-parse --show-toplevel)" - cmd="echo {} |sed 's/.*] //' |xargs -I% git diff --color=always $commit -- '$repo/%' | $forgit_diff_pager" + cmd="echo {} |sed 's/.*] //' |xargs -I% git diff --color=always $commits -- '$repo/%' | $forgit_diff_pager" opts=" $FORGIT_FZF_DEFAULT_OPTS +m -0 --bind=\"enter:execute($cmd |LESS='-r' less)\" $FORGIT_DIFF_FZF_OPTS " - eval "git diff --name-status $commit -- ${files[*]} | sed -E 's/^(.)[[:space:]]+(.*)$/[\1] \2/'" | + eval "git diff --name-status $commits -- ${files[*]} | sed -E 's/^(.)[[:space:]]+(.*)$/[\1] \2/'" | FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" } From 5093c185329488448aa6e631d3e751a402729a15 Mon Sep 17 00:00:00 2001 From: carlfriedrich Date: Fri, 11 Mar 2022 18:37:30 +0100 Subject: [PATCH 2/3] gd: support git move/rename For "git diff" to display a move/rename action correctly, it has to be passed both the old and the new file name. Change the gd preview command accordingly: instead of constructing a path out of the repo directory and the file name (which would not work with multiple file names), cd to the repo directory and use the file names as direct arguments to "git diff" instead. Change the regex used to parse the "git diff --name-status" output so that it supports the "Rxxx" syntax for moved/renamed files. Use tabs instead of hardcoded number of spaces along with "expand" to have file names horizontally aligned. Fixes wfxr/forgit#188 --- forgit.plugin.zsh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index c8c7878..f4bb709 100755 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -49,13 +49,13 @@ forgit::diff() { fi } repo="$(git rev-parse --show-toplevel)" - cmd="echo {} |sed 's/.*] //' |xargs -I% git diff --color=always $commits -- '$repo/%' | $forgit_diff_pager" + cmd="cd $repo && echo {} |sed 's/.*] *//' |xargs git diff --color=always $commits -- | $forgit_diff_pager" opts=" $FORGIT_FZF_DEFAULT_OPTS +m -0 --bind=\"enter:execute($cmd |LESS='-r' less)\" $FORGIT_DIFF_FZF_OPTS " - eval "git diff --name-status $commits -- ${files[*]} | sed -E 's/^(.)[[:space:]]+(.*)$/[\1] \2/'" | + eval "git diff --name-status $commits -- ${files[*]} | sed -E 's/^([[:alnum:]]+)[[:space:]]+(.*)$/[\1]\t\2/'" | expand -t 8 | FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" } From 2e5f06e7892f1a3b1609068400258a909aa80538 Mon Sep 17 00:00:00 2001 From: carlfriedrich Date: Fri, 11 Mar 2022 19:18:13 +0100 Subject: [PATCH 3/3] gd: improve visualization for move/rename Show an arrow instead of just blank spaces when a file has been moved or renamed: [R100] foo.txt -> bar.txt The arrow is added as a replacement for the second tabspace of the "git diff --name-status" command. For the preview command it is replaced by a single space again. --- forgit.plugin.zsh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index f4bb709..c4e3034 100755 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -49,13 +49,14 @@ forgit::diff() { fi } repo="$(git rev-parse --show-toplevel)" - cmd="cd $repo && echo {} |sed 's/.*] *//' |xargs git diff --color=always $commits -- | $forgit_diff_pager" + cmd="cd $repo && echo {} |sed 's/.*] *//' | sed 's/ -> / /' |xargs git diff --color=always $commits -- | $forgit_diff_pager" opts=" $FORGIT_FZF_DEFAULT_OPTS +m -0 --bind=\"enter:execute($cmd |LESS='-r' less)\" $FORGIT_DIFF_FZF_OPTS " - eval "git diff --name-status $commits -- ${files[*]} | sed -E 's/^([[:alnum:]]+)[[:space:]]+(.*)$/[\1]\t\2/'" | expand -t 8 | + eval "git diff --name-status $commits -- ${files[*]} | sed -E 's/^([[:alnum:]]+)[[:space:]]+(.*)$/[\1]\t\2/'" | + sed 's/\t/ -> /2' | expand -t 8 | FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" }