From c1679180981d146b80d0af9d1c991952b3f6cce5 Mon Sep 17 00:00:00 2001 From: carlfriedrich Date: Wed, 3 Aug 2022 09:15:13 +0200 Subject: [PATCH] Support stashes in forgit::diff Git stashes are named "stash@{x}", which contains the fzf placeholder "{x}". In order to support passing stashes as arguments to forgit::diff, we have to prevent fzf from interpreting this substring by escaping the opening bracket. --- conf.d/forgit.plugin.fish | 10 ++++++++-- forgit.plugin.zsh | 9 +++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/conf.d/forgit.plugin.fish b/conf.d/forgit.plugin.fish index c8b9203..6c05d1f 100644 --- a/conf.d/forgit.plugin.fish +++ b/conf.d/forgit.plugin.fish @@ -117,8 +117,14 @@ function forgit::diff -d "git diff viewer" --argument-names arg1 arg2 end end - set preview_cmd "forgit::extract_file {} | xargs -I% git diff --color=always -U$forgit_preview_context $commits -- % | $forgit_diff_pager" - set enter_cmd "forgit::extract_file {} | xargs -I% git diff --color=always -U$forgit_fullscreen_context $commits -- % | $forgit_diff_pager" + # Git stashes are named "stash@{x}", which contains the fzf placeholder "{x}". + # In order to support passing stashes as arguments to forgit::diff, we have to + # prevent fzf from interpreting this substring by escaping the opening bracket. + # The string is evaluated a few subsequent times, so we need multiple escapes. + set escaped_commits (echo $commits | sed 's/{/\\\\\\\\{/g') + + set preview_cmd "forgit::extract_file {} | xargs -I% git diff --color=always -U$forgit_preview_context $escaped_commits -- % | $forgit_diff_pager" + set enter_cmd "forgit::extract_file {} | xargs -I% git diff --color=always -U$forgit_fullscreen_context $escaped_commits -- % | $forgit_diff_pager" set opts " $FORGIT_FZF_DEFAULT_OPTS diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index 43a0392..7eb583a 100755 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -70,8 +70,13 @@ forgit::diff() { } repo="$(git rev-parse --show-toplevel)" get_files="cd '$repo' && echo {} | sed 's/.*] *//' | sed 's/ -> / /'" - preview_cmd="$get_files | xargs -I% git diff --color=always -U$forgit_preview_context $commits -- % | $forgit_diff_pager" - enter_cmd="$get_files | xargs -I% git diff --color=always -U$forgit_fullscreen_context $commits -- % | $forgit_diff_pager" + # Git stashes are named "stash@{x}", which contains the fzf placeholder "{x}". + # In order to support passing stashes as arguments to forgit::diff, we have to + # prevent fzf from interpreting this substring by escaping the opening bracket. + # The string is evaluated a few subsequent times, so we need multiple escapes. + escaped_commits=${commits//\{/\\\\\{} + preview_cmd="$get_files | xargs -I% git diff --color=always -U$forgit_preview_context $escaped_commits -- % | $forgit_diff_pager" + enter_cmd="$get_files | xargs -I% git diff --color=always -U$forgit_fullscreen_context $escaped_commits -- % | $forgit_diff_pager" opts=" $FORGIT_FZF_DEFAULT_OPTS +m -0 --bind=\"enter:execute($enter_cmd | LESS='-r' less)\"