# [PATCH] contrib/completion: avoid empty arithemetic expressions

3 messages from 2021-05-30 to 2021-05-30. Participants: David Aguilar, Felipe Contreras.
Thread: https://gitlist.dev/t/55799

## David Aguilar, 2021-05-30 02:15

Subject: [PATCH] contrib/completion: avoid empty arithemetic expressions
Message-ID: <20210530021528.21287-1-davvid@gmail.com>
URL: https://gitlist.dev/e/20210530021528.21287-1-davvid%40gmail.com

```
$__git_cmd_idx can be empty in some situations, which leads to
errors when using "git add <tab>", "git mv <tab>",
"git tag <tab>" and "git branch <tab>".

"git mv <tab>" prints this error:

	__git_count_arguments:5:
	bad math expression: operand expected at `""'

	_git_mv:[:9: unknown condition: -gt

"git branch <tab>" prints this error:

	_git_branch:[:4: unknown condition: -lt

"git tag <tab>" prints this error:

	_git_tag:[:3: unknown condition: -lt

"git add <tab>" prints this error:

	__git_find_on_cmdline:[:13: unknown condition: -lt

Fix _git_branch, __git_find_on_cmdline and _git_tag by
initializing the local "c" variable to 1 when empty.

Fix __git_count_arguments by initializing "__git_cmd_idx" to 1.

Adjust the for loop in __git_count_arguments to avoid quoting
the numeric argument to avoid the following error:

	__git_count_arguments:8:
	bad math expression: operand expected at `"1"'

This was tested on zsh 5.7.1 (x86_64-apple-darwin19.0).

Signed-off-by: David Aguilar <davvid@gmail.com>
---
 contrib/completion/git-completion.bash | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 3c5739b905..d51ff5302d 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1177,6 +1177,9 @@ __git_aliased_command ()
 __git_find_on_cmdline ()
 {
 	local word c="$__git_cmd_idx" show_idx
+	if [ -z "$c" ]; then
+		c=1
+	fi
 
 	while test $# -gt 1; do
 		case "$1" in
@@ -1304,9 +1307,12 @@ __git_has_doubledash ()
 __git_count_arguments ()
 {
 	local word i c=0
+	if [ -z "$__git_cmd_idx" ]; then
+		__git_cmd_idx=1
+	fi
 
 	# 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
@@ -1448,6 +1454,9 @@ __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD
 _git_branch ()
 {
 	local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
+	if [ -z "$c" ]; then
+		c=1
+	fi
 
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
@@ -3213,6 +3222,10 @@ _git_svn ()
 _git_tag ()
 {
 	local i c="$__git_cmd_idx" f=0
+	if [ -z "$c" ]; then
+		c=1
+	fi
+
 	while [ $c -lt $cword ]; do
 		i="${words[c]}"
 		case "$i" in
-- 
2.32.0.rc2.1.g6e92745b1d


```

## Felipe Contreras, 2021-05-30 03:48

Subject: RE: [PATCH] contrib/completion: avoid empty arithemetic expressions
Message-ID: <60b30b159bda0_50fd208ee@natae.notmuch>
URL: https://gitlist.dev/e/60b30b159bda0_50fd208ee%40natae.notmuch
In-Reply-To: <20210530021528.21287-1-davvid@gmail.com>

```
David Aguilar wrote:
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 3c5739b905..d51ff5302d 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -1177,6 +1177,9 @@ __git_aliased_command ()
>  __git_find_on_cmdline ()
>  {
>  	local word c="$__git_cmd_idx" show_idx
> +	if [ -z "$c" ]; then
> +		c=1
> +	fi

This is not the correct location to fix this, it's here:

diff --git a/git-completion.zsh b/git-completion.zsh
index 0ef15ff..df98e68 100644
--- a/git-completion.zsh
+++ b/git-completion.zsh
@@ -233,7 +233,7 @@ __git_zsh_main ()
                emulate ksh -c __git_complete_config_variable_name_and_value
                ;;
        (arg)
-               local command="${words[1]}" __git_dir
+               local command="${words[1]}" __git_dir __git_cmd_idx=1
 
                if (( $+opt_args[--bare] )); then
                        __git_dir='.'

Commit 59d85a2a05 (git-completion.bash: use $__git_cmd_idx in more
places, 2021-04-22) broke zsh because it modified __git_main, but not
__git_zsh_main.

Cheers.

-- 
Felipe Contreras

```

## David Aguilar, 2021-05-30 06:18

Subject: Re: [PATCH] contrib/completion: avoid empty arithemetic expressions
Message-ID: <CAJDDKr7rVpnCgMEbw4aeLXrUWMxV=sOYNCUn-2o69FgpbU3XwA@mail.gmail.com>
URL: https://gitlist.dev/e/CAJDDKr7rVpnCgMEbw4aeLXrUWMxV%3DsOYNCUn-2o69FgpbU3XwA%40mail.gmail.com
In-Reply-To: <60b30b159bda0_50fd208ee@natae.notmuch>

```
On Sat, May 29, 2021 at 8:48 PM Felipe Contreras
<felipe.contreras@gmail.com> wrote:
>
> David Aguilar wrote:
> > diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> > index 3c5739b905..d51ff5302d 100644
> > --- a/contrib/completion/git-completion.bash
> > +++ b/contrib/completion/git-completion.bash
> > @@ -1177,6 +1177,9 @@ __git_aliased_command ()
> >  __git_find_on_cmdline ()
> >  {
> >       local word c="$__git_cmd_idx" show_idx
> > +     if [ -z "$c" ]; then
> > +             c=1
> > +     fi
>
> This is not the correct location to fix this, it's here:
>
> diff --git a/git-completion.zsh b/git-completion.zsh
> index 0ef15ff..df98e68 100644
> --- a/git-completion.zsh
> +++ b/git-completion.zsh
> @@ -233,7 +233,7 @@ __git_zsh_main ()
>                 emulate ksh -c __git_complete_config_variable_name_and_value
>                 ;;
>         (arg)
> -               local command="${words[1]}" __git_dir
> +               local command="${words[1]}" __git_dir __git_cmd_idx=1
>
>                 if (( $+opt_args[--bare] )); then
>                         __git_dir='.'
>
> Commit 59d85a2a05 (git-completion.bash: use $__git_cmd_idx in more
> places, 2021-04-22) broke zsh because it modified __git_main, but not
> __git_zsh_main.
>
> Cheers.
>
> --
> Felipe Contreras

Sweet, thanks Felipe.

The resulting patch is much simpler now too. Please disregard the
previous patches, v2 is on the way.
-- 
David

```
