git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] completion: suppress zsh's special 'words' variable

From
Jonathan Nieder <jrnieder@gmail.com>
Date
May 10, 2011, 02:59 UTC
Message-ID
<20110510025916.GB26619@elie>
In-Reply-To
<20110510025529.GA26619@elie>
From: Felipe Contreras <felipe.contreras@gmail.com>

After git's tab completion script gained zsh support in v1.7.4-rc0~169^2 (completion: make compatible with zsh, 2010-09-06) it was broken moments later. More precisely, the completion does not notice when it has seen a subcommand name, so all words complete as options to the git wrapper or subcommand names. For example, typing "git log origi<TAB>" gives no completions because there are no "git origi..." commands.

The cause: it turns out 'words' is one of the special parameters used by the zsh completion system, used to hold the words from the command it is completing. As a result (in the words of zshcompwid(1)):

	[...] the parameters are reset on each function exit
	(including nested function calls from within the completion
	widget) to the values they had when the function was entered.
Each function in git's completion script using the 'words' array
 - declares "local words", causing the array to be cleared (but not
   resetting the special attribute);
 - calls "_get_comp_words_by_ref -n := words" to fill it with a
   modified version of COMP_WORDS with ':' and '=' no longer treated
   as word separators (see v1.7.4-rc0~11^2~2, 2010-12-02).  Within
   _get_comp_words_by_ref all is well, and when the function returns,
   words is reset to its former value;
 - examines $words and finds it empty.

Fix it by suppressing the special 'words' variable with typeset -h so it can be used as an ordinary array. The only risk is that the completion script might call a function that wants to inspect the 'words' variable, expecting the zsh-specific meaning; luckily the next version of zsh's bashcompinit (e880604f, 29140: hide the "words" special variable so that it may be used as an ordinary variable by bash completions, 2011-05-04) will also use 'typeset -h words' when calling completion functions so

 - soon this fix will be redundant :)
 - anyone else using the bashcompinit library is risking the same
   problem, so presumably other functions from that library are
   carefully written to only look at $COMP_WORDS and not $words.
This fixes a regression introduced by v1.7.4-rc0~11^2~2 (2010-12-02).
Reported-by: Stefan Haller <lists@haller-berlin.de>
Improved-by: SZEDER Gábor <szeder@ira.uka.de>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 contrib/completion/git-completion.bash |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b81f444..da586e5 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2608,6 +2608,7 @@ _git ()
 	if [[ -n ${ZSH_VERSION-} ]]; then
 		emulate -L bash
 		setopt KSH_TYPESET
+		typeset -h words
 	fi
 
 	local cur words cword prev
@@ -2659,6 +2660,7 @@ _gitk ()
 	if [[ -n ${ZSH_VERSION-} ]]; then
 		emulate -L bash
 		setopt KSH_TYPESET
+		typeset -h words
 	fi
 
 	local cur words cword prev
-- 
1.7.5.1
Previous: Jonathan NiederNext: Jonathan Nieder
Message 12 of 19 in “git-completion: fix zsh support”
  1. git-completion: fix zsh supportFelipe Contreras, May 9, 2011
  2. Jonathan NiederMay 9, 2011
  3. Felipe ContrerasMay 9, 2011
  4. git-completion: fix zsh supportFelipe Contreras, May 9, 2011
  5. Jonathan NiederMay 9, 2011
  6. Felipe ContrerasMay 9, 2011
  7. Jonathan NiederMay 9, 2011
  8. Felipe ContrerasMay 9, 2011
  9. Junio C HamanoMay 9, 2011
  10. Jonathan NiederMay 9, 2011
  11. 0/2 git-completion: fix zsh supportJonathan Nieder, May 10, 2011
  12. 1/2 completion: suppress zsh's special 'words' variableJonathan Nieder, May 10, 2011
  13. Jonathan NiederMay 10, 2011
  14. Felipe ContrerasMay 10, 2011
  15. Felipe ContrerasMay 10, 2011
  16. 2/2 completion: move private shopt shim for zsh to __git_ namespaceJonathan Nieder, May 10, 2011
  17. Felipe ContrerasMay 10, 2011
  18. Jonathan NiederMay 10, 2011
  19. Felipe ContrerasMay 10, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.