threads / patch / 55801

v2contrib/completion: fix zsh completion regression from 59d85a2a05

Subject: [PATCH v2] contrib/completion: fix zsh completion regression from 59d85a2a05

## tl;dr

2 messages between May 30, 2021 and May 30, 2021. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

David Aguilar· May 30, 2021, 06:24 UTC · lore

A recent change to make git-completion.bash use $__git_cmd_idx in more places broke a number of completions on zsh because it modified __git_main but did not update __git_zsh_main.

Notably, completions for "add", "branch", "mv" and "push" were broken as a result of this change.

In addition to the undefined variable usage, "git mv <tab>" also prints the following error:

	__git_count_arguments:7: bad math expression:
	operand expected at `"1"'
	_git_mv:[:7: unknown condition: -gt

Remove the quotes around $__git_cmd_idx in __git_count_arguments and set __git_cmd_idx=1 early in __git_zsh_main to fix the regressions from 59d85a2a05.

This was tested on zsh 5.7.1 (x86_64-apple-darwin19.0).
Helped-by: Felipe Contreras <felipe.contreras@gmail.com>
Signed-off-by: David Aguilar <davvid@gmail.com>
---
 contrib/completion/git-completion.bash | 2 +-
 contrib/completion/git-completion.zsh  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Show changes to 2 files +2 −2

contrib/completion/git-completion.bash, contrib/completion/git-completion.zsh

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 3c5739b905..b50c5d0ea3 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1306,7 +1306,7 @@ __git_count_arguments ()
 	local word i c=0
 
 	# Skip "git" (first argument)
-	for ((i="$__git_cmd_idx"; i < ${#words[@]}; i++)); do
+	for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
 		word="${words[i]}"
 
 		case "$word" in
diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index 6c56296997..cac6f61881 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -251,7 +251,7 @@ __git_zsh_main ()
 		done
 		;;
 	(arg)
-		local command="${words[1]}" __git_dir
+		local command="${words[1]}" __git_dir __git_cmd_idx=1
 
 		if (( $+opt_args[--bare] )); then
 			__git_dir='.'
-- 
2.32.0.rc2.1.gffeee53afa
Felipe Contreras· May 30, 2021, 16:52 UTC · re: David Aguilar · lore

RE: [PATCH v2] contrib/completion: fix zsh completion regression from 59d85a2a05

David Aguilar wrote:
Show 11 quoted lines
> --- a/contrib/completion/git-completion.zsh
> +++ b/contrib/completion/git-completion.zsh
> @@ -251,7 +251,7 @@ __git_zsh_main ()
>  		done
>  		;;
>  	(arg)
> -		local command="${words[1]}" __git_dir
> +		local command="${words[1]}" __git_dir __git_cmd_idx=1
>  
>  		if (( $+opt_args[--bare] )); then
>  			__git_dir='.'

Note that this is not the correct fix, since the index of the command will not always be 1.

We would probably need to at least do something like:
Show changes to git-completion.zsh +1 −1
--- a/git-completion.zsh
+++ b/git-completion.zsh
@@ -260,7 +260,7 @@ __git_zsh_main ()
 
                (( $+opt_args[--help] )) && command='help'
 
-               words=( ${orig_words[@]} )
+               words=( git ${words[@]} )
 
                __git_zsh_bash_func $command
                ;;

And then it will almost always do the correct thing (at least in
git-completion[1] which does have all the git main arguments).

For now doing __git_cmd_idx=1 should fix the regression.

Cheers.

[1] https://github.com/felipec/git-completion
-- 
Felipe Contreras

← back to recent threads