Skip to content

Commit fca416a

Browse files
committed
completion: use "git -C $there" instead of (cd $there && git ...)
We have had "git -C $there" to first go to a different directory and run a Git command without changing the arguments for quite some time. Use it instead of (cd $there && git ...) in the completion script. This allows us to lose the work-around for misfeatures of modern interactive-minded shells that make "cd" unusable in scripts (e.g. end users' $CDPATH taking us to unexpected places in any POSIX shell, and chpwd functions spewing unwanted output in zsh). Based on Øystein Walle's idea, which was raised during the discussion on the solution by Brandon Turner for a problem zsh users had with RVM which mucks with chpwd_functions in users' environments (rvm/rvm#3076). As $root variable, which is used to direct where to chdir to, is set to "." based on if $2 to __git_index_files is set (not if it is empty), the only caller of the function is fixed not to pass the optional $2 when it does not want us to switch to a different directory. Otherwise we would end up doing "git -C '' command...", which would not work. Maybe we would want "git -C '' command..." to mean "do not chdir anywhere", but that is a spearate topic. Signed-off-by: Junio C Hamano <[email protected]>
1 parent eeff891 commit fca416a

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

contrib/completion/git-completion.bash

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,16 +263,12 @@ __gitcomp_file ()
263263
# argument, and using the options specified in the second argument.
264264
__git_ls_files_helper ()
265265
{
266-
(
267-
test -n "${CDPATH+set}" && unset CDPATH
268-
cd "$1"
269-
if [ "$2" == "--committable" ]; then
270-
git diff-index --name-only --relative HEAD
271-
else
272-
# NOTE: $2 is not quoted in order to support multiple options
273-
git ls-files --exclude-standard $2
274-
fi
275-
) 2>/dev/null
266+
if [ "$2" == "--committable" ]; then
267+
git -C "$1" diff-index --name-only --relative HEAD
268+
else
269+
# NOTE: $2 is not quoted in order to support multiple options
270+
git -C "$1" ls-files --exclude-standard $2
271+
fi 2>/dev/null
276272
}
277273

278274

@@ -504,7 +500,7 @@ __git_complete_index_file ()
504500
;;
505501
esac
506502

507-
__gitcomp_file "$(__git_index_files "$1" "$pfx")" "$pfx" "$cur_"
503+
__gitcomp_file "$(__git_index_files "$1" ${pfx:+"$pfx"})" "$pfx" "$cur_"
508504
}
509505

510506
__git_complete_file ()

0 commit comments

Comments
 (0)