Refactor: Quote files passed to preview functions

In some cases we need to pass multiple files to preview functions. These
files are treated as a single string that is evaluated by fzf and passed
on to our preview functions as individual arguments.
This commit introduces a _forgit_quote_files function that ensures the
resulting arguments are quoted properly.
This commit is contained in:
sandroid 2024-03-15 23:44:46 +01:00
parent 71e691989e
commit 5119c45a5a
No known key found for this signature in database
GPG key ID: 91418C9982B8B76E

View file

@ -104,6 +104,29 @@ _forgit_parse_array() {
${old_IFS+"false"} && unset IFS || IFS="$old_IFS"
}
# parse the input arguments and print only those after the "--"
# separator as a single line of quoted arguments to stdout
_forgit_quote_files() {
local files add
files=()
add=false
while (( "$#" )); do
case "$1" in
--)
add=true
shift
;;
*)
if [ $add == true ]; then
files+=("'$1'")
fi
shift
;;
esac
done
echo "${files[*]}"
}
_forgit_pager=${FORGIT_PAGER:-$(git config core.pager || echo 'cat')}
_forgit_show_pager=${FORGIT_SHOW_PAGER:-$(git config pager.show || echo "$_forgit_pager")}
_forgit_diff_pager=${FORGIT_DIFF_PAGER:-$(git config pager.diff || echo "$_forgit_pager")}
@ -138,14 +161,14 @@ _forgit_log_enter() {
# git commit viewer
_forgit_log() {
_forgit_inside_work_tree || return 1
local opts graph files log_format
files=$(sed -nE 's/.*-- (.*)/\1/p' <<< "$*") # extract files parameters for `git show` command
local opts graph quoted_files log_format
quoted_files=$(_forgit_quote_files "$@")
opts="
$FORGIT_FZF_DEFAULT_OPTS
+s +m --tiebreak=index
--bind=\"enter:execute($FORGIT log_enter {} $files)\"
--bind=\"enter:execute($FORGIT log_enter {} $quoted_files)\"
--bind=\"ctrl-y:execute-silent($FORGIT yank_sha {})\"
--preview=\"$FORGIT log_preview {} $files\"
--preview=\"$FORGIT log_preview {} $quoted_files\"
$FORGIT_LOG_FZF_OPTS
"
graph=()
@ -532,17 +555,17 @@ _forgit_cherry_pick_from_branch() {
_forgit_rebase() {
_forgit_inside_work_tree || return 1
local opts graph files target_commit prev_commit
local opts graph quoted_files target_commit prev_commit
graph=()
[[ $_forgit_log_graph_enable == true ]] && graph=(--graph)
_forgit_rebase_git_opts=()
_forgit_parse_array _forgit_rebase_git_opts "$FORGIT_REBASE_GIT_OPTS"
files=$(sed -nE 's/.* -- (.*)/\1/p' <<< "$*") # extract files parameters for `git show` command
quoted_files=$(_forgit_quote_files "$@")
opts="
$FORGIT_FZF_DEFAULT_OPTS
+s +m --tiebreak=index
--bind=\"ctrl-y:execute-silent($FORGIT yank_sha {})\"
--preview=\"$FORGIT file_preview {} $files\"
--preview=\"$FORGIT file_preview {} $quoted_files\"
$FORGIT_REBASE_FZF_OPTS
"
target_commit=$(
@ -566,17 +589,17 @@ _forgit_file_preview() {
_forgit_fixup() {
_forgit_inside_work_tree || return 1
git diff --cached --quiet && echo 'Nothing to fixup: there are no staged changes.' && return 1
local opts graph files target_commit prev_commit
local opts graph quoted_files target_commit prev_commit
graph=()
[[ $_forgit_log_graph_enable == true ]] && graph=(--graph)
_forgit_fixup_git_opts=()
_forgit_parse_array _forgit_fixup_git_opts "$FORGIT_FIXUP_GIT_OPTS"
files=$(sed -nE 's/.* -- (.*)/\1/p' <<< "$*")
quoted_files=$(_forgit_quote_files "$@")
opts="
$FORGIT_FZF_DEFAULT_OPTS
+s +m --tiebreak=index
--bind=\"ctrl-y:execute-silent($FORGIT yank_sha {})\"
--preview=\"$FORGIT file_preview {} $files\"
--preview=\"$FORGIT file_preview {} $quoted_files\"
$FORGIT_FIXUP_FZF_OPTS
"
target_commit=$(