# [PATCH v2 0/4] Fix branch.autosetup(merge|rebase) completion

8 messages from 2014-01-03 to 2014-01-05. Participants: Ramkumar Ramachandra, brian m. carlson, Junio C Hamano.
Thread: https://gitlist.dev/t/35600

## Ramkumar Ramachandra, 2014-01-03 08:00

Subject: [PATCH v2 0/4] Fix branch.autosetup(merge|rebase) completion
Message-ID: <1388736031-6068-1-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1388736031-6068-1-git-send-email-artagnon%40gmail.com

```
Hi,

In this iteration, I've removed hunks to prevent completing:

  $ git config remote.pushdefault.<TAB>
  $ git config branch.autosetupmerge.<TAB>
  $ git config branch.autosetuprebase.<TAB>

Since they're perfectly valid remote/ branch names.

Thanks.

Ramkumar Ramachandra (4):
  completion: prioritize ./git-completion.bash
  completion: introduce __gitcomp_2 ()
  completion: fix branch.autosetup(merge|rebase)
  completion: fix remote.pushdefault

 contrib/completion/git-completion.bash | 36 ++++++++++++++++++++++++++++++++--
 contrib/completion/git-completion.zsh  | 12 +++++++++++-
 2 files changed, 45 insertions(+), 3 deletions(-)

-- 
1.8.5.2.227.g53f3478

```

## Ramkumar Ramachandra, 2014-01-03 08:00

Subject: [PATCH v2 1/4] completion: prioritize ./git-completion.bash
Message-ID: <1388736031-6068-2-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1388736031-6068-2-git-send-email-artagnon%40gmail.com
In-Reply-To: <1388736031-6068-1-git-send-email-artagnon@gmail.com>

```
To ease development, prioritize ./git-completion.bash over other
standard system paths.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 contrib/completion/git-completion.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index fac5e71..6fca145 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -30,10 +30,10 @@ if [ -z "$script" ]; then
 	local -a locations
 	local e
 	locations=(
+		$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
 		'/etc/bash_completion.d/git' # fedora, old debian
 		'/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
 		'/usr/share/bash-completion/git' # gentoo
-		$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
 		)
 	for e in $locations; do
 		test -f $e && script="$e" && break
-- 
1.8.5.2.227.g53f3478

```

## Ramkumar Ramachandra, 2014-01-03 08:00

Subject: [PATCH v2 2/4] completion: introduce __gitcomp_2 ()
Message-ID: <1388736031-6068-3-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1388736031-6068-3-git-send-email-artagnon%40gmail.com
In-Reply-To: <1388736031-6068-1-git-send-email-artagnon@gmail.com>

```
There are situations where two classes of completions possible. For
example

  branch.<TAB>

should try to complete

  branch.master.
  branch.autosetupmerge
  branch.autosetuprebase

The first candidate has the suffix ".", and the second/ third candidates
have the suffix " ". To facilitate completions of this kind, create a
variation of __gitcomp_nl () that accepts two sets of arguments and two
independent suffixes.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 contrib/completion/git-completion.bash | 30 ++++++++++++++++++++++++++++++
 contrib/completion/git-completion.zsh  | 10 ++++++++++
 2 files changed, 40 insertions(+)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 51c2dd4..64b20b8 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -233,6 +233,36 @@ __gitcomp_nl ()
 	__gitcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
 }
 
+# Generates completion reply from two sets of completion words, with
+# configurable suffixes for each.
+#
+# It accepts 2 to 6 arguments:
+# 1: First set of possible completion words.
+# 2: Second set of possible completion words.
+# 3: A prefix to be added to each completion word (both $1 and $2)
+#    (optional).
+# 4: Generate possible completion matches for this word (optional).
+# 5: A suffix to be appended to each completion word in the first set
+#    ($1) instead of the default space (optional).
+# 6: A suffix to be appended to each completion word in the second set
+#    ($2) instead of the default space (optional).
+__gitcomp_2 ()
+{
+	local pfx="${3-}" cur_="${4-$cur}" sfx="${5- }" sfx2="${6- }" i=0
+	local IFS=$' \t\n'
+
+	for x in $1; do
+		if [[ "$x" == "$cur_"* ]]; then
+			COMPREPLY[i++]="$pfx$x$sfx"
+		fi
+	done
+	for x in $2; do
+		if [[ "$x" == "$cur_"* ]]; then
+			COMPREPLY[i++]="$pfx$x$sfx2"
+		fi
+	done
+}
+
 # Generates completion reply with compgen from newline-separated possible
 # completion filenames.
 # It accepts 1 to 3 arguments:
diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
index 6fca145..261a7f5 100644
--- a/contrib/completion/git-completion.zsh
+++ b/contrib/completion/git-completion.zsh
@@ -76,6 +76,16 @@ __gitcomp_nl ()
 	compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
 }
 
+__gitcomp_2 ()
+{
+	emulate -L zsh
+
+	local IFS=$' \t\n'
+	compset -P '*[=:]'
+	compadd -Q -S "${5- }" -p "${3-}" -- ${=1} && _ret=0
+	compadd -Q -S "${6- }" -p "${3-}" -- ${=2} && _ret=0
+}
+
 __gitcomp_file ()
 {
 	emulate -L zsh
-- 
1.8.5.2.227.g53f3478

```

## Ramkumar Ramachandra, 2014-01-03 08:00

Subject: [PATCH v2 3/4] completion: fix branch.autosetup(merge|rebase)
Message-ID: <1388736031-6068-4-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1388736031-6068-4-git-send-email-artagnon%40gmail.com
In-Reply-To: <1388736031-6068-1-git-send-email-artagnon@gmail.com>

```
When attempting to complete

  $ git config branch.auto<TAB>

'autosetupmerge' and 'autosetuprebase' don't come up. This is because
"$cur" is matched with "branch.*" and a list of branches are
completed. Add 'autosetup(merge|rebase)' to the list of branches using
__gitcomp_2 ().

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 contrib/completion/git-completion.bash | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 64b20b8..cbb4eca 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1856,7 +1856,9 @@ _git_config ()
 		;;
 	branch.*)
 		local pfx="${cur%.*}." cur_="${cur#*.}"
-		__gitcomp_nl "$(__git_heads)" "$pfx" "$cur_" "."
+		__gitcomp_2 "$(__git_heads)" "
+			autosetupmerge autosetuprebase
+			" "$pfx" "$cur_" "."
 		return
 		;;
 	guitool.*.*)
-- 
1.8.5.2.227.g53f3478

```

## Ramkumar Ramachandra, 2014-01-03 08:00

Subject: [PATCH v2 4/4] completion: fix remote.pushdefault
Message-ID: <1388736031-6068-5-git-send-email-artagnon@gmail.com>
URL: https://gitlist.dev/e/1388736031-6068-5-git-send-email-artagnon%40gmail.com
In-Reply-To: <1388736031-6068-1-git-send-email-artagnon@gmail.com>

```
When attempting to complete

  $ git config remote.push<TAB>

'pushdefault' doesn't come up. This is because "$cur" is matched with
"remote.*" and a list of remotes are completed. Add 'pushdefault' to the
list of remotes using __gitcomp_2 ().

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 contrib/completion/git-completion.bash | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index cbb4eca..04d72a1 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1900,7 +1900,7 @@ _git_config ()
 		;;
 	remote.*)
 		local pfx="${cur%.*}." cur_="${cur#*.}"
-		__gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
+		__gitcomp_2 "$(__git_remotes)" "pushdefault" "$pfx" "$cur_" "."
 		return
 		;;
 	url.*.*)
-- 
1.8.5.2.227.g53f3478

```

## brian m. carlson, 2014-01-03 22:55

Subject: Re: [PATCH v2 1/4] completion: prioritize ./git-completion.bash
Message-ID: <20140103225541.GG3338@vauxhall.crustytoothpaste.net>
URL: https://gitlist.dev/e/20140103225541.GG3338%40vauxhall.crustytoothpaste.net
In-Reply-To: <1388736031-6068-2-git-send-email-artagnon@gmail.com>

```
On Fri, Jan 03, 2014 at 01:30:28PM +0530, Ramkumar Ramachandra wrote:
> To ease development, prioritize ./git-completion.bash over other
> standard system paths.
> 
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
> ---
>  contrib/completion/git-completion.zsh | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
> index fac5e71..6fca145 100644
> --- a/contrib/completion/git-completion.zsh
> +++ b/contrib/completion/git-completion.zsh
> @@ -30,10 +30,10 @@ if [ -z "$script" ]; then
>  	local -a locations
>  	local e
>  	locations=(
> +		$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
>  		'/etc/bash_completion.d/git' # fedora, old debian
>  		'/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
>  		'/usr/share/bash-completion/git' # gentoo
> -		$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
>  		)
>  	for e in $locations; do
>  		test -f $e && script="$e" && break

I'm not clear on this change.  It looks like this loads
git-completion.bash from the same directory as git-completion.zsh.  Is
this correct?  Your commit message says "./", and if that's the case, it
has the same security problems as putting "." first in your PATH.

-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187

```

## Junio C Hamano, 2014-01-03 23:03

Subject: Re: [PATCH v2 1/4] completion: prioritize ./git-completion.bash
Message-ID: <xmqqtxdkllcr.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqtxdkllcr.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20140103225541.GG3338@vauxhall.crustytoothpaste.net>

```
"brian m. carlson" <sandals@crustytoothpaste.net> writes:

> On Fri, Jan 03, 2014 at 01:30:28PM +0530, Ramkumar Ramachandra wrote:
>> To ease development, prioritize ./git-completion.bash over other
>> standard system paths.
>> 
>> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
>> ---
>>  contrib/completion/git-completion.zsh | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>> 
>> diff --git a/contrib/completion/git-completion.zsh b/contrib/completion/git-completion.zsh
>> index fac5e71..6fca145 100644
>> --- a/contrib/completion/git-completion.zsh
>> +++ b/contrib/completion/git-completion.zsh
>> @@ -30,10 +30,10 @@ if [ -z "$script" ]; then
>>  	local -a locations
>>  	local e
>>  	locations=(
>> +		$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
>>  		'/etc/bash_completion.d/git' # fedora, old debian
>>  		'/usr/share/bash-completion/completions/git' # arch, ubuntu, new debian
>>  		'/usr/share/bash-completion/git' # gentoo
>> -		$(dirname ${funcsourcetrace[1]%:*})/git-completion.bash
>>  		)
>>  	for e in $locations; do
>>  		test -f $e && script="$e" && break
>
> I'm not clear on this change.  It looks like this loads
> git-completion.bash from the same directory as git-completion.zsh.  Is
> this correct?

I think the idea is to help those who have installed a closer to
bleeding-edge version of completion scripts --- they will not be
reading their zsh completions from the system default locations,
and the place next to it would be a place more likely to have the
matching bleeding-edge version of bash completion than the system
default place.

> Your commit message says "./", and if that's the case, it
> has the same security problems as putting "." first in your PATH.

A correct observation. I think

	Subject: zsh completion: find matching custom bash completion

	If zsh completion is being read from a location that is
	different from system-wide default, it is likely that the
	user is trying to use a custom version, perhaps closer to
	the bleeding edge, installed in her own directory. We will
	more likely to find the matching bash completion script in
	the same directory than in those system default places.

would probably a lot closer to what the patch really does.

```

## Ramkumar Ramachandra, 2014-01-05 10:08

Subject: Re: [PATCH v2 1/4] completion: prioritize ./git-completion.bash
Message-ID: <CALkWK0nt_EJ_Ajy1Tp9P_Z-Vj_03ZTETBcDTJBiCptKfa2qyLw@mail.gmail.com>
URL: https://gitlist.dev/e/CALkWK0nt_EJ_Ajy1Tp9P_Z-Vj_03ZTETBcDTJBiCptKfa2qyLw%40mail.gmail.com
In-Reply-To: <20140103225541.GG3338@vauxhall.crustytoothpaste.net>

```
brian m. carlson wrote:
> I'm not clear on this change.  It looks like this loads
> git-completion.bash from the same directory as git-completion.zsh.  Is
> this correct?

Yes, and that's what I meant to convey with the "./". Junio's message
is clearer though, so I'll use that.

Thanks.

```
