threads / rfc / 33154

RFC patchgit-completion.bash: remove bashism to fix ZSH compatibility

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

## tl;dr

12 messages between Mar 11, 2013 and Mar 11, 2013. Diffs are folded; open one to read it.

replies: 11people: 5as markdown or json

Matthieu Moy· Mar 11, 2013, 12:21 UTC · lore

The function-wide redirection used for __git_ls_files_helper and __git_diff_index_helper work only with bash. Using 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/
Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
These two instances seem to be the only ones in the file.

I'm not sure whether the 2>/dev/null would be needed for the command on the RHS of the && too (git ls-files and git diff-index).

 contrib/completion/git-completion.bash | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Show changes to contrib/completion/git-completion.bash +4 −4
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index b62bec0..0640274 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -300,8 +300,8 @@ __git_index_file_list_filter ()
 __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
+	cd "$1" 2>/dev/null && git ls-files --exclude-standard $2
+}
 
 
 # Execute git diff-index, returning paths relative to the directory
@@ -309,8 +309,8 @@ __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
+	cd "$1" 2>/dev/null && git diff-index --name-only --relative "$2"
+}
 
 # __git_index_files accepts 1 or 2 arguments:
 # 1: Options to pass to ls-files (required).
-- 
1.8.2.rc3.16.g0a33571.dirty
Junio C Hamano· Mar 11, 2013, 16:17 UTC · re: Matthieu Moy · lore

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

Matthieu Moy <Matthieu.Moy@imag.fr> writes:
Show 8 quoted lines
> The function-wide redirection used for __git_ls_files_helper and
> __git_diff_index_helper work only with bash. Using 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/
>
> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
> ---

This is not bash-ism but POSIX.1, even though it is not very well known. I recall commenting on this exact pattern during the review.

  http://thread.gmane.org/gmane.comp.version-control.git/213232/focus=213286

After all, I was right when I said that some implementations may get it wrong and we shouldn't use the construct X-<.

> These two instances seem to be the only ones in the file.
>
> I'm not sure whether the 2>/dev/null would be needed for the command
> on the RHS of the && too (git ls-files and git diff-index).
It would not hurt to discard their standard error.
Show 29 quoted lines
>  contrib/completion/git-completion.bash | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index b62bec0..0640274 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -300,8 +300,8 @@ __git_index_file_list_filter ()
>  __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
> +	cd "$1" 2>/dev/null && git ls-files --exclude-standard $2
> +}
>  
>  
>  # Execute git diff-index, returning paths relative to the directory
> @@ -309,8 +309,8 @@ __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
> +	cd "$1" 2>/dev/null && git diff-index --name-only --relative "$2"
> +}
>  
>  # __git_index_files accepts 1 or 2 arguments:
>  # 1: Options to pass to ls-files (required).
Junio C Hamano· Mar 11, 2013, 16:41 UTC · re: Junio C Hamano · lore

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

Junio C Hamano <gitster@pobox.com> writes:
Show 9 quoted lines
> After all, I was right when I said that some implementations may get
> it wrong and we shouldn't use the construct X-<.
>
>> These two instances seem to be the only ones in the file.
>>
>> I'm not sure whether the 2>/dev/null would be needed for the command
>> on the RHS of the && too (git ls-files and git diff-index).
>
> It would not hurt to discard their standard error.
So here is an updated based on your patch.
-- >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 () {
		funcbody
	} 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/

It is easy to work around by refraining from using this construct. The correct thing to do in the longer term may be to stop dot-sourcing the source meant for bash into zsh, but this patch should suffice as a band-aid in the meantime.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 contrib/completion/git-completion.bash | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Show changes to contrib/completion/git-completion.bash +4 −4
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 51b8b3b..3d4cc7c 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -300,8 +300,8 @@ __git_index_file_list_filter ()
 __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
+	cd "$1" 2>/dev/null && git ls-files --exclude-standard $2 2>/dev/null
+}
 
 
 # Execute git diff-index, returning paths relative to the directory
@@ -309,8 +309,8 @@ __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
+	cd "$1" 2>/dev/null && 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-271-g00e868e
Matthieu Moy· Mar 11, 2013, 16:47 UTC · re: Junio C Hamano · lore

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

Junio C Hamano <gitster@pobox.com> writes:
> So here is an updated based on your patch.
Perfect, thanks.
> The correct thing to do in the longer term may be to stop dot-sourcing
> the source meant for bash into zsh, but this patch should suffice as
> a band-aid in the meantime.

I disagree with this particular part though. I think using the same code for bash and zsh makes sense, and it implies restricting to the common subset. I don't consider it "band-aid", but "nice code factoring" ;-).

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Junio C Hamano· Mar 11, 2013, 17:01 UTC · re: Matthieu Moy · lore

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

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 13 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>> So here is an updated based on your patch.
>
> Perfect, thanks.
>
>> The correct thing to do in the longer term may be to stop dot-sourcing
>> the source meant for bash into zsh, but this patch should suffice as
>> a band-aid in the meantime.
>
> I disagree with this particular part though. I think using the same code
> for bash and zsh makes sense, and it implies restricting to the common
> subset.

Having to restrict to the common subset means that whenever bash adds new and useful features that this script could take advantage of to improve the user experience, they cannot be employed until zsh catches up (and worse yet, it is outside the control of this script if zsh may ever catch up in the specific feature).

Manlio Perillo· Mar 11, 2013, 17:12 UTC · re: Junio C Hamano · lore

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Il 11/03/2013 18:01, Junio C Hamano ha scritto:
Show 7 quoted lines
> [...]
> Having to restrict to the common subset means that whenever bash
> adds new and useful features that this script could take advantage
> of to improve the user experience, they cannot be employed until zsh
> catches up (and worse yet, it is outside the control of this script
> if zsh may ever catch up in the specific feature).
> 

Maybe, to avoid this problem and code duplication (the main reason bash script is sourced, as far as I can tell), it may be useful to add additional reusable git commands, for use in shell completion?

E.g:
	git suggest <cmd> *args
returns a line separed list of filenames affected by cmd.
Regards  Manlio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlE+EJkACgkQscQJ24LbaURjNwCfdW73fET/n4FRGftKcSJPsK7M nu4An1CC0dspGxLe5zqR9BdXBBDHWl/Y =11j7 -----END PGP SIGNATURE-----

Manlio Perillo· Mar 11, 2013, 17:09 UTC · re: Junio C Hamano · lore

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Il 11/03/2013 17:17, Junio C Hamano ha scritto:
Show 14 quoted lines
> Matthieu Moy <Matthieu.Moy@imag.fr> writes:
> 
>> The function-wide redirection used for __git_ls_files_helper and
>> __git_diff_index_helper work only with bash. Using 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/
>>
>> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
>> ---
> 
> This is not bash-ism but POSIX.1, even though it is not very well
> known.  I recall commenting on this exact pattern during the review.
> 

Yes, I was plainning to send another patch to fix this (and your other suggestion regarding the CDPATH environment variable, if I remember correctly), but I was busy with other things; sorry.

> [...]
Regards  Manlio
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlE+D7QACgkQscQJ24LbaURBTgCffpMCPjmcsP53/WE/VIQ2FIIc fiIAn3obBJ1yrHVUEmslz32ezvESCZ4G =7nia -----END PGP SIGNATURE-----

Junio C Hamano· Mar 11, 2013, 17:23 UTC · re: Manlio Perillo · lore

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

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(-)
Show changes to contrib/completion/git-completion.bash +11 −5
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
Matthieu Moy· Mar 11, 2013, 17:48 UTC · re: Junio C Hamano · lore

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

Junio C Hamano <gitster@pobox.com> writes:
> 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.

Thanks. Even nicer that the previous since the CDPATH implied the subshell anyway.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Junio C Hamano· Mar 11, 2013, 18:09 UTC · re: Matthieu Moy · lore

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

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
Show 9 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>> 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.
>
> Thanks. Even nicer that the previous since the CDPATH implied the
> subshell anyway.

Actually, "cd", not CDPATH, is what implies that the caller must be calling us in a subshell, e.g.

	result=$(__git_ls_files_helper dir/ args...)

Otherwise the user's shell would have been taken to an unexpected place, with or without CDPATH.

So strictly speaking there is no reason for an extra subshell here, but writing this in the way the patch does makes our intention crystal clear, I think.

In any case, let's queue this fix for the 1.8.2 final. The CDPATH thing will affect not just zsh but bash users.

Paul Smith· Mar 11, 2013, 18:19 UTC · re: Junio C Hamano · lore

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

On Mon, 2013-03-11 at 11:09 -0700, Junio C Hamano wrote:
> So strictly speaking there is no reason for an extra subshell here,
> but writing this in the way the patch does makes our intention
> crystal clear, I think.

If you're concerned about the extra processing of the new shell you can use {} instead of ():

        {
            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

Zsh does support this properly in my testing. It's only redirection of an entire function body, as the original, that is working differently in zsh and bash.

Manlio Perillo· Mar 11, 2013, 19:09 UTC · re: Junio C Hamano · lore

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Il 11/03/2013 19:09, Junio C Hamano ha scritto:
Show 20 quoted lines
> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
> 
>> Junio C Hamano <gitster@pobox.com> writes:
>>
>>> 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.
>>
>> Thanks. Even nicer that the previous since the CDPATH implied the
>> subshell anyway.
> 
> Actually, "cd", not CDPATH, is what implies that the caller must be
> calling us in a subshell, e.g.
> 
> 	result=$(__git_ls_files_helper dir/ args...)
> 
> Otherwise the user's shell would have been taken to an unexpected
> place, with or without CDPATH.
> 
Right; this is the reason I used the `{` grouping, instead of `(`.

However, since the `{` is already specified when the function is defined, I did not add another `{}` grouping.

> [...]
Regards  Manlio Perillo
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlE+K/4ACgkQscQJ24LbaUQqvwCgmReHb4VtMJDT+tv+XF9RPmXE DlEAnjhsgXszSBVG1iW0WCLM6212+fdA =SYzh -----END PGP SIGNATURE-----

← back to recent threads