Skip to content

Commit

Permalink
Refactor: Quote files passed to preview functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sandr01d committed Mar 24, 2024
1 parent 0c0668f commit 0a8a70b
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions bin/git-forgit
Original file line number Diff line number Diff line change
Expand Up @@ -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")}
Expand Down Expand Up @@ -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=()
Expand Down Expand Up @@ -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=$(
Expand All @@ -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=$(
Expand Down

0 comments on commit 0a8a70b

Please sign in to comment.