Skip to content

Commit

Permalink
Use correct IFS store/restore mechanism (#306)
Browse files Browse the repository at this point in the history
The previous implementation had the problem that if IFS was not set
before, it was set to an empty string after restoring, which is not the
same as being unset. This broke grc / git forgit revert_commit, since
"git revert" was being interpreted as the command name instead of
command and argument.

The correct way is to check whether IFS is unset and, if so, unset it
again afterwards.

See for reference:
https://unix.stackexchange.com/a/264947/317320
  • Loading branch information
carlfriedrich committed May 8, 2023
1 parent f74aa93 commit 5642a1a
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions bin/git-forgit
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,11 @@ _forgit_cherry_pick() {
[[ $fzf_exitval != 0 ]] && return $fzf_exitval
[[ -z "$fzf_selection" ]] && return $fzf_exitval

old_IFS=$IFS IFS=$'\n'
${IFS+"false"} && unset old_IFS || old_IFS="$IFS"
IFS=$'\n'
# shellcheck disable=2207
commits=($(echo "$fzf_selection" | sort --numeric-sort --key=1 | cut -f2 | cut -d' ' -f1 | _forgit_reverse_lines))
IFS=${old_IFS}
${old_IFS+"false"} && unset IFS || IFS="$old_IFS"
[ ${#commits[@]} -eq 0 ] && return 1

$git_cherry_pick "${commits[@]}"
Expand Down Expand Up @@ -553,15 +554,16 @@ _forgit_revert_commit() {
files=$(sed -nE 's/.* -- (.*)/\1/p' <<< "$*") # extract files parameters for `git show` command
preview="echo {} | cut -f2- | $_forgit_extract_sha | xargs -I% git show --color=always % -- $files | $_forgit_show_pager"

old_IFS=$IFS IFS=$'\n'
${IFS+"false"} && unset old_IFS || old_IFS="$IFS"
IFS=$'\n'
# shellcheck disable=2207
commits=($(eval "$cmd" |
nl |
FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" -m |
sort --numeric-sort --key=1 |
cut -f2- |
sed 's/^[^a-f^0-9]*\([a-f0-9]*\).*/\1/'))
IFS=${old_IFS}
${old_IFS+"false"} && unset IFS || IFS="$old_IFS"

[ ${#commits[@]} -eq 0 ] && return 1

Expand Down Expand Up @@ -607,11 +609,12 @@ _forgit_ignore() {
--preview=\"eval $cmd\"
$FORGIT_IGNORE_FZF_OPTS
"
old_IFS=$IFS IFS=$'\n'
${IFS+"false"} && unset old_IFS || old_IFS="$IFS"
IFS=$'\n'
# shellcheck disable=SC2206,2207
args=($@) && [[ $# -eq 0 ]] && args=($(_forgit_ignore_list | nl -nrn -w4 -s' ' |
FZF_DEFAULT_OPTS="$opts" fzf | awk '{print $2}'))
IFS=${old_IFS}
${old_IFS+"false"} && unset IFS || IFS="$old_IFS"
[ ${#args[@]} -eq 0 ] && return 1
# shellcheck disable=SC2068
_forgit_ignore_get ${args[@]}
Expand Down

0 comments on commit 5642a1a

Please sign in to comment.