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

[PATCH] completion: move private shopt shim for zsh to __git_ namespace

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Apr 27, 2011, 21:27 UTC
Message-ID
<20110427212704.GB18596@elie>
In-Reply-To
<BANLkTikN7iMa_z7wRN8pUS07SMatpyoDPQ@mail.gmail.com>

Most zsh users probably probably do not expect a custom shopt function to enter their environment just because they ran "source ~/.git-completion.sh".

Such namespace pollution makes development of other scripts confusing (because it makes the bash-specific shopt utility seem to be available in zsh) and makes git's tab completion script brittle (since any other shell snippet implementing some other subset of shopt will break it). Rename the shopt shim to the more innocuous __git_shopt to be a good citizen (with two underscores to avoid confusion with completion rules for a hypothetical "git shopt" command).

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
Hi,
Felipe Contreras wrote:
Show 12 quoted lines
> +++ b/contrib/completion/git-completion.bash
> @@ -75,6 +75,10 @@
> 
>  if [[ -n ${ZSH_VERSION-} ]]; then
>         autoload -U +X bashcompinit && bashcompinit
> +
> +       # 'words' has special meaning in zsh, and only typeset -h seems to
> +       # override that
> +       alias local="typeset -h"
>  fi
> 
>  case "$COMP_WORDBREAKS" in

The above would change the meaning of "local" in the user's environment and in all shell snippets she sources later. Are you sure that's intended?

Actually the completion script already has a problem along the same lines. Thanks for a reminder.

 contrib/completion/git-completion.bash |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 9150ea6..ab95690 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -629,12 +629,12 @@ __git_refs_remotes ()
 __git_remotes ()
 {
 	local i ngoff IFS=$'\n' d="$(__gitdir)"
-	shopt -q nullglob || ngoff=1
-	shopt -s nullglob
+	__git_shopt -q nullglob || ngoff=1
+	__git_shopt -s nullglob
 	for i in "$d/remotes"/*; do
 		echo ${i#$d/remotes/}
 	done
-	[ "$ngoff" ] && shopt -u nullglob
+	[ "$ngoff" ] && __git_shopt -u nullglob
 	for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
 		i="${i#remote.}"
 		echo "${i/.url*/}"
@@ -2800,7 +2800,7 @@ complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
 fi
 
 if [[ -n ${ZSH_VERSION-} ]]; then
-	shopt () {
+	__git_shopt () {
 		local option
 		if [ $# -ne 2 ]; then
 			echo "USAGE: $0 (-q|-s|-u) <option>" >&2
@@ -2823,4 +2823,8 @@ if [[ -n ${ZSH_VERSION-} ]]; then
 			return 1
 		esac
 	}
+else
+	__git_shopt () {
+		shopt "$@"
+	}
 fi
-- 
1.7.5
Previous: Felipe ContrerasNext: Felipe Contreras
Message 11 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.