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

Re: [RFC/PATCH] git-completion.bash: remove bashism to fix ZSH compatibility

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 11, 2013, 17:23 UTC
Message-ID
<7v8v5talzu.fsf@alter.siamese.dyndns.org>
In-Reply-To
<513E0FB4.40607@gmail.com>
Manlio Perillo <manlio.perillo@gmail.com> writes:
> Yes, I was plainning to send another patch to fix this (and your other
> suggestion regarding the CDPATH environment variable, if I remember
> correctly),...

Ahh, thanks for reminding me of this. You are right; these two functions are broken when the user has CDPATH set, I think.

Here is a reroll.
-- >8 --
From: Matthieu Moy <Matthieu.Moy@imag.fr>
Date: Mon, 11 Mar 2013 13:21:27 +0100
Subject: [PATCH] git-completion.bash: zsh does not implement function
 redirection correctly

A recent change added functions whose entire standard error stream is redirected to /dev/null using a construct that is valid POSIX.1 but is not widely used:

	funcname () {
		cd "$1" && run some command "$2"
	} 2>/dev/null

Even though this file is "git-completion.bash", zsh completion support dot-sources it (instead of asking bash to grok it like tcsh completion does), and zsh does not implement this redirection correctly.

With zsh, trying to complete an inexistant directory gave this:
  git add no-such-dir/__git_ls_files_helper:cd:2: no such file or directory: no-such-dir/

Also these functions use "cd" to first go somewhere else before running a command, but the location the caller wants them to go that is given as an argument to them should not be affected by CDPATH variable the users may have set for their interactive session.

To fix both of these, wrap the body of the function in a subshell, unset CDPATH at the beginning of the subshell, and redirect the standard error stream of the subshell to /dev/null.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 contrib/completion/git-completion.bash | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 51b8b3b..430566d 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -299,9 +299,12 @@ __git_index_file_list_filter ()
 # the second argument.
 __git_ls_files_helper ()
 {
-	# NOTE: $2 is not quoted in order to support multiple options
-	cd "$1" && git ls-files --exclude-standard $2
-} 2>/dev/null
+	(
+		test -n "${CDPATH+set}" && unset CDPATH
+		# NOTE: $2 is not quoted in order to support multiple options
+		cd "$1" && git ls-files --exclude-standard $2
+	) 2>/dev/null
+}
 
 
 # Execute git diff-index, returning paths relative to the directory
@@ -309,8 +312,11 @@ __git_ls_files_helper ()
 # specified in the second argument.
 __git_diff_index_helper ()
 {
-	cd "$1" && git diff-index --name-only --relative "$2"
-} 2>/dev/null
+	(
+		test -n "${CDPATH+set}" && unset CDPATH
+		cd "$1" && git diff-index --name-only --relative "$2"
+	) 2>/dev/null
+}
 
 # __git_index_files accepts 1 or 2 arguments:
 # 1: Options to pass to ls-files (required).
-- 
1.8.2-rc3-219-ge56455f
Previous: Manlio PerilloNext: Matthieu Moy
Message 8 of 12 in “git-completion.bash: remove bashism to fix ZSH compatibility”
  1. git-completion.bash: remove bashism to fix ZSH compatibilityMatthieu Moy, Mar 11, 2013
  2. Junio C HamanoMar 11, 2013
  3. Junio C HamanoMar 11, 2013
  4. Matthieu MoyMar 11, 2013
  5. Junio C HamanoMar 11, 2013
  6. Manlio PerilloMar 11, 2013
  7. Manlio PerilloMar 11, 2013
  8. Junio C HamanoMar 11, 2013
  9. Matthieu MoyMar 11, 2013
  10. Junio C HamanoMar 11, 2013
  11. Paul SmithMar 11, 2013
  12. Manlio PerilloMar 11, 2013

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.