Skip to content

Commit 3bee6a4

Browse files
szedergitster
authored andcommitted
completion: don't declare 'local words' to make zsh happy
The "_get_comp_words_by_ref -n := words" command from the bash_completion library reassembles a modified version of COMP_WORDS with ':' and '=' no longer treated as word separators and stores it in the ${words[@]} array. Git's programmable tab completion script uses this to abstract away the difference between bash v3's and bash v4's definitions of COMP_WORDS (bash v3 used shell words, while bash v4 breaks at separator characters); see v1.7.4-rc0~11^2~2 (bash: get --pretty=m<tab> completion to work with bash v4, 2010-12-02). zsh has (or rather its completion functions have) another idea about what ${words[@]} should contain: the array is prepopulated with the words from the command it is completing. For reasons that are not well understood, when git-completion.bash reserves its own "words" variable with "local words", the variable becomes empty and cannot be changed from then on. So the completion script neglects the arguments it has seen, and words complete like git subcommand names. For example, typing "git log origi<TAB>" gives no completions because there are no "git origi..." commands. However, when this words variable is not declared as local but is just populated by _get_comp_words_by_ref() and then read in various completion functions, then zsh seems to be happy about it and our completion script works as expected. So, to get our completion script working again under zsh and to prevent the words variable from leaking into the shell environment under bash, we will only declare words as local when using bash. Reported-by: Stefan Haller <[email protected]> Suggested-by: Felipe Contreras <[email protected]> Explained-by: Jonathan Nieder <[email protected]> Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent da4902a commit 3bee6a4

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

contrib/completion/git-completion.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,9 +2608,11 @@ _git ()
26082608
if [[ -n ${ZSH_VERSION-} ]]; then
26092609
emulate -L bash
26102610
setopt KSH_TYPESET
2611+
else
2612+
local words
26112613
fi
26122614

2613-
local cur words cword prev
2615+
local cur cword prev
26142616
_get_comp_words_by_ref -n =: cur words cword prev
26152617
while [ $c -lt $cword ]; do
26162618
i="${words[c]}"
@@ -2659,9 +2661,11 @@ _gitk ()
26592661
if [[ -n ${ZSH_VERSION-} ]]; then
26602662
emulate -L bash
26612663
setopt KSH_TYPESET
2664+
else
2665+
local words
26622666
fi
26632667

2664-
local cur words cword prev
2668+
local cur cword prev
26652669
_get_comp_words_by_ref -n =: cur words cword prev
26662670

26672671
__git_has_doubledash && return

0 commit comments

Comments
 (0)