Skip to content

Commit 8012396

Browse files
Fix empty diff preview on renames (#282)
This had been fixed before in #189, but obviously the patch broke support for whitespaces in file names. That was fixed in #204, which in turn broke support for renames again. Prepare the list of file names with the null-character \0 as a delimiter, so that we can use "xargs -0" to read it. This makes renames as well as filenames with spaces work correctly.
1 parent 065f784 commit 8012396

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

bin/git-forgit

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
# This gives users the choice to set aliases inside of their git config instead
1313
# of their shell config if they prefer.
1414

15+
# Set shell for fzf preview commands
1516
# Disable shellcheck for "which", because it suggests "command -v xxx" instead,
1617
# which is not a working replacement.
1718
# See https://github.com/koalaman/shellcheck/issues/1162
1819
# shellcheck disable=2230
19-
SHELL="$(which bash)" # Set shell for fzf preview commands
20+
SHELL="$(which bash)"
21+
export SHELL
2022

2123
# Get absolute forgit path
2224
FORGIT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)/$(basename -- "${BASH_SOURCE[0]}")
@@ -112,14 +114,26 @@ _forgit_diff() {
112114
fi
113115
}
114116
repo="$(git rev-parse --show-toplevel)"
115-
get_files="cd '$repo' && echo {} | sed 's/.*] *//' | sed 's/ -> / /'"
117+
# Construct a null-terminated list of the filenames
118+
# The input looks like one of these lines:
119+
# [R100] file -> another file
120+
# [A] file with spaces
121+
# [D] oldfile
122+
# And we transform it to this representation for further usage with "xargs -0":
123+
# file\0another file\0
124+
# file with spaces\0
125+
# oldfile\0
126+
# We have to do a two-step sed -> tr pipe because OSX's sed implementation does
127+
# not support the null-character directly.
128+
get_files="echo {} | sed 's/.*] *//' | sed 's/ -> /\\\n/' | tr '\\\n' '\\\0'"
116129
# Git stashes are named "stash@{x}", which contains the fzf placeholder "{x}".
117130
# In order to support passing stashes as arguments to _forgit_diff, we have to
118131
# prevent fzf from interpreting this substring by escaping the opening bracket.
119132
# The string is evaluated a few subsequent times, so we need multiple escapes.
120133
escaped_commits=${commits//\{/\\\\\{}
121-
preview_cmd="$get_files | xargs -I% git diff --color=always -U$_forgit_preview_context $escaped_commits -- % | $_forgit_diff_pager"
122-
enter_cmd="$get_files | xargs -I% git diff --color=always -U$_forgit_fullscreen_context $escaped_commits -- % | $_forgit_diff_pager"
134+
git_diff="git diff --color=always $escaped_commits"
135+
preview_cmd="cd '$repo' && $get_files | xargs -0 $git_diff -U$_forgit_preview_context -- | $_forgit_diff_pager"
136+
enter_cmd="cd '$repo' && $get_files | xargs -0 $git_diff -U$_forgit_fullscreen_context -- | $_forgit_diff_pager"
123137
opts="
124138
$FORGIT_FZF_DEFAULT_OPTS
125139
+m -0 --bind=\"enter:execute($enter_cmd | $_forgit_enter_pager)\"

0 commit comments

Comments
 (0)