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

Re: [PATCH 3/3] bash: don't declare 'local words' to make zsh happy

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
May 3, 2011, 17:53 UTC
Message-ID
<BANLkTimbLhkMRKLUBWhjg+oBtRwAEWGSqQ@mail.gmail.com>
In-Reply-To
<1304006513-19392-3-git-send-email-szeder@ira.uka.de>
On Thu, Apr 28, 2011 at 7:01 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
Show 68 quoted lines
> 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 <lists@haller-berlin.de>
> Suggested-by: Felipe Contreras <felipe.contreras@gmail.com>
> Explained-by: Jonathan Nieder <jrnieder@gmail.com>
> Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
> ---
>  contrib/completion/git-completion.bash |    8 ++++++--
>  1 files changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 862b840..6869765 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -2608,9 +2608,11 @@ _git ()
>        if [[ -n ${ZSH_VERSION-} ]]; then
>                emulate -L bash
>                setopt KSH_TYPESET
> +       else
> +               local words
>        fi
>
> -       local cur words cword prev
> +       local cur cword prev
>        _get_comp_words_by_ref -n =: cur words cword prev
>        while [ $c -lt $cword ]; do
>                i="${words[c]}"
> @@ -2659,9 +2661,11 @@ _gitk ()
>        if [[ -n ${ZSH_VERSION-} ]]; then
>                emulate -L bash
>                setopt KSH_TYPESET
> +       else
> +               local words
>        fi
>
> -       local cur words cword prev
> +       local cur cword prev
>        _get_comp_words_by_ref -n =: cur words cword prev
>
>        __git_has_doubledash && return
> --
> 1.7.5.86.g799a6
Here's another option:
From 603e4db259283a4eb6bac2315a630480e3238f50 Mon Sep 17 00:00:00 2001
From: Felipe Contreras <felipe.contreras@gmail.com>
Date: Tue, 3 May 2011 20:45:26 +0300
Subject: [PATCH] git-completion: fix zsh support
It turns out 'words' is a special variable used by zsh completion.

There's probably a bug in zsh's bashcompinit: http://article.gmane.org/gmane.comp.shells.zsh.devel/22546

But in the meantime we can workaround it this way.
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/contrib/completion/git-completion.bash
b/contrib/completion/git-completion.bash
index 00691fc..d32b1b8 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2608,6 +2608,9 @@ _git ()
 	if [[ -n ${ZSH_VERSION-} ]]; then
 		emulate -L bash
 		setopt KSH_TYPESET
+
+		# 'words' has special meaning in zsh; override that
+		typeset -h words
 	fi

 	local cur words cword prev
-- 
1.7.5

-- 
Felipe Contreras
Previous: SZEDER GáborNext: Felipe Contreras
Message 21 of 28 in “git-completion: fix zsh support”
  1. git-completion: fix zsh supportFelipe Contreras, Apr 27, 2011
  2. Jonathan NiederApr 27, 2011
  3. Felipe ContrerasApr 27, 2011
  4. Junio C HamanoApr 27, 2011
  5. completion: avoid "words" as variable name for zsh portabilityJonathan Nieder, Apr 27, 2011
  6. Felipe ContrerasApr 27, 2011
  7. Jonathan NiederApr 27, 2011
  8. Felipe ContrerasApr 27, 2011
  9. John SzakmeisterApr 27, 2011
  10. Felipe ContrerasApr 27, 2011
  11. completion: move private shopt shim for zsh to __git_ namespaceJonathan Nieder, Apr 27, 2011
  12. Felipe ContrerasApr 27, 2011
  13. Jonathan NiederApr 27, 2011
  14. Jonathan NiederMay 6, 2011
  15. Felipe ContrerasMay 6, 2011
  16. SZEDER GáborMay 8, 2011
  17. SZEDER GáborApr 28, 2011
  18. 1/3 bash: don't modify the $cur variable in completion functionsSZEDER Gábor, Apr 28, 2011
  19. 2/3 bash: remove unnecessary _get_comp_words_by_ref() invocationsSZEDER Gábor, Apr 28, 2011
  20. 3/3 bash: don't declare 'local words' to make zsh happySZEDER Gábor, Apr 28, 2011
  21. Felipe ContrerasMay 3, 2011
  22. Felipe ContrerasApr 28, 2011
  23. Junio C HamanoApr 28, 2011
  24. Felipe ContrerasApr 28, 2011
  25. Felipe ContrerasApr 27, 2011
  26. Junio C HamanoApr 27, 2011
  27. Felipe ContrerasApr 27, 2011
  28. Jonathan NiederApr 27, 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.