Skip to contentSkip to conversation

Fix branch.autosetup(merge|rebase) completion

PATCH Superseded Ramkumar Ramachandra 3 participants 8 messages Jun 15, 2016

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fix branch.autosetup(merge|rebase) completion

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(-)
Signature · 1 line
1.8.5.2.227.g53f3478
#1 lore raw

PATCH v2 1/4 completion: prioritize ./git-completion.bash

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

---
 contrib/completion/git-completion.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/completion/git-completion.zsh +1 -1
index fac5e71..6fca145 100644
@@ -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
Signature · 1 line
1.8.5.2.227.g53f3478
#2 lore raw

Re: [PATCH v2 1/4] completion: prioritize ./git-completion.bash

Quoted 14 lines · Ramkumar Ramachandra

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

---
 contrib/completion/git-completion.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/completion/git-completion.zsh +0 -0
index fac5e71..6fca145 100644
@@ -30,10 +30,10 @@ if [ -z "$script" ]; then
 	local -a locations

contrib/completion/git-completion.zsh if [ -z "$script" ]; then · line 39

Quoted 10 lines
contrib/completion/git-completion.zsh +1 -1
 	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.

Signature · 3 lines
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
#3 lore raw

Re: [PATCH v2 1/4] completion: prioritize ./git-completion.bash

Quoted 21 lines · brian m. carlson
Quoted 20 lines · Ramkumar Ramachandra

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

---
 contrib/completion/git-completion.zsh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/completion/git-completion.zsh +1 -0
index fac5e71..6fca145 100644
@@ -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
Quoted 8 lines

contrib/completion/git-completion.zsh if [ -z "$script" ]; then · line 39

Quoted 4 lines
contrib/completion/git-completion.zsh +0 -1
-		$(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.

Quoted 2 lines

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.

#4 lore raw

Re: [PATCH v2 1/4] completion: prioritize ./git-completion.bash

In reply to brian m. carlson
Quoted 3 lines · brian m. carlson

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.

#5 lore raw

PATCH v2 2/4 completion: introduce __gitcomp_2 ()

In reply to Ramkumar Ramachandra

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.

---
 contrib/completion/git-completion.bash | 30 ++++++++++++++++++++++++++++++
 contrib/completion/git-completion.zsh  | 10 ++++++++++
 2 files changed, 40 insertions(+)
contrib/completion/git-completion.bash +30 -0
index 51c2dd4..64b20b8 100644
@@ -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:
contrib/completion/git-completion.zsh +10 -0
index 6fca145..261a7f5 100644
@@ -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
Signature · 1 line
1.8.5.2.227.g53f3478
#6 lore raw

PATCH v2 3/4 completion: fix branch.autosetup(merge|rebase)

In reply to Ramkumar Ramachandra

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 ().

---
 contrib/completion/git-completion.bash | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
contrib/completion/git-completion.bash +3 -1
index 64b20b8..cbb4eca 100644
@@ -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.*.*)
Signature · 1 line
1.8.5.2.227.g53f3478
#7 lore raw

PATCH v2 4/4 completion: fix remote.pushdefault

In reply to Ramkumar Ramachandra

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 ().

---
 contrib/completion/git-completion.bash | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
contrib/completion/git-completion.bash +1 -1
index cbb4eca..04d72a1 100644
@@ -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.*.*)
Signature · 1 line
1.8.5.2.227.g53f3478