Skip to contentSkip to conversation

bash: complete full refs

PATCH In review SZEDER Gábor 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bash: complete full refs

Sometimes it's handy to complete full refs, e.g. the user has some refs outside of refs/{heads,remotes,tags} or the user wants to complete some git command's special refs (like 'git show refs/bisect/bad').

To do that, we check whether the ref to be completed starts with 'refs'. If it does, then we offer full refs for completion; otherwise everything works as usual.

This way the impact on the common case is fairly small (hopefully not many users have branches or tags starting with 'refs'), and in the special case the cost of typing out 'refs' is bearable.

While at it, also remove the unused 'cmd' variable from '__git_refs'.

---
 contrib/completion/git-completion.bash |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
contrib/completion/git-completion.bash +15 -4
index 0ee071b..39bf18b 100755
@@ -188,11 +188,22 @@ __git_tags ()
 
 __git_refs ()
 {
-	local cmd i is_hash=y dir="$(__gitdir "$1")"
+	local i is_hash=y dir="$(__gitdir "$1")"
+	local cur="${COMP_WORDS[COMP_CWORD]}" format refs
 	if [ -d "$dir" ]; then
-		if [ -e "$dir/HEAD" ]; then echo HEAD; fi
-		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
-			refs/tags refs/heads refs/remotes
+		case "$cur" in
+		refs*)
+			format="refname"
+			refs="${cur%/*}"
+			;;
+		*)
+			if [ -e "$dir/HEAD" ]; then echo HEAD; fi
+			format="refname:short"
+			refs="refs/tags refs/heads refs/remotes"
+			;;
+		esac
+		git --git-dir="$dir" for-each-ref --format="%($format)" \
+			$refs
 		return
 	fi
 	for i in $(git ls-remote "$dir" 2>/dev/null); do
Signature · 1 line
1.6.0.4.814.gfe502
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · SZEDER GGGbor

Sometimes it's handy to complete full refs, [...]

To do that, we check whether the ref to be completed starts with 'refs'.

...

Quoted 7 lines
contrib/completion/git-completion.bash +0 -0
index 0ee071b..39bf18b 100755
@@ -188,11 +188,22 @@ __git_tags ()
 
 __git_refs ()

contrib/completion/git-completion.bash __git_tags () · line 195

Quoted 10 lines
contrib/completion/git-completion.bash +4 -4
 {
-	local cmd i is_hash=y dir="$(__gitdir "$1")"
+	local i is_hash=y dir="$(__gitdir "$1")"
+	local cur="${COMP_WORDS[COMP_CWORD]}" format refs
 	if [ -d "$dir" ]; then
-		if [ -e "$dir/HEAD" ]; then echo HEAD; fi
-		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
-			refs/tags refs/heads refs/remotes
+		case "$cur" in
+		refs*)

I wonder if the pattern shouldn't be:

	refs|refs/*)

to reduce the risk of matching "refs-" and trying to do a full ref match instead of a short ref match.

Otherwise,

Signature · 1 line
Shawn.
#2 lore raw

PATCH 1/1 bash: complete full refs

Sometimes it's handy to complete full refs, e.g. the user has some refs outside of refs/{heads,remotes,tags} or the user wants to complete some git command's special refs (like 'git show refs/bisect/bad').

To do that, we check whether the ref to be completed starts with 'refs/' or is 'refs' (to reduce the risk of matching 'refs-'). If it does, then we offer full refs for completion; otherwise everything works as usual.

This way the impact on the common case is fairly small (hopefully not many users have branches or tags starting with 'refs'), and in the special case the cost of typing out 'refs' is bearable.

While at it, also remove the unused 'cmd' variable from '__git_refs'.

---

Quoted 6 lines · Shawn O. Pearce

I wonder if the pattern shouldn't be:

	refs|refs/*)

to reduce the risk of matching "refs-" and trying to do a full ref match instead of a short ref match.

Good point. Adjusted the code and commit message accordingly.

 contrib/completion/git-completion.bash |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
contrib/completion/git-completion.bash +15 -4
index 0ee071b..244ed41 100755
@@ -188,11 +188,22 @@ __git_tags ()
 
 __git_refs ()
 {
-	local cmd i is_hash=y dir="$(__gitdir "$1")"
+	local i is_hash=y dir="$(__gitdir "$1")"
+	local cur="${COMP_WORDS[COMP_CWORD]}" format refs
 	if [ -d "$dir" ]; then
-		if [ -e "$dir/HEAD" ]; then echo HEAD; fi
-		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
-			refs/tags refs/heads refs/remotes
+		case "$cur" in
+		refs|refs/*)
+			format="refname"
+			refs="${cur%/*}"
+			;;
+		*)
+			if [ -e "$dir/HEAD" ]; then echo HEAD; fi
+			format="refname:short"
+			refs="refs/tags refs/heads refs/remotes"
+			;;
+		esac
+		git --git-dir="$dir" for-each-ref --format="%($format)" \
+			$refs
 		return
 	fi
 	for i in $(git ls-remote "$dir" 2>/dev/null); do
Signature · 1 line
1.6.0.4.814.gfe502
Acked-by on v1 1/1
#3 lore raw
Quoted 4 lines · SZEDER GGGbor

Sometimes it's handy to complete full refs, e.g. the user has some refs outside of refs/{heads,remotes,tags} or the user wants to complete some git command's special refs (like 'git show refs/bisect/bad').

...

Quoted 8 lines · Shawn O. Pearce
Quoted 6 lines

I wonder if the pattern shouldn't be:

	refs|refs/*)

to reduce the risk of matching "refs-" and trying to do a full ref match instead of a short ref match.

Good point. Adjusted the code and commit message accordingly.

Thanks.

Quoted 37 lines
 contrib/completion/git-completion.bash |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
contrib/completion/git-completion.bash +15 -4
index 0ee071b..244ed41 100755
@@ -188,11 +188,22 @@ __git_tags ()
 
 __git_refs ()
 {
-	local cmd i is_hash=y dir="$(__gitdir "$1")"
+	local i is_hash=y dir="$(__gitdir "$1")"
+	local cur="${COMP_WORDS[COMP_CWORD]}" format refs
 	if [ -d "$dir" ]; then
-		if [ -e "$dir/HEAD" ]; then echo HEAD; fi
-		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
-			refs/tags refs/heads refs/remotes
+		case "$cur" in
+		refs|refs/*)
+			format="refname"
+			refs="${cur%/*}"
+			;;
+		*)
+			if [ -e "$dir/HEAD" ]; then echo HEAD; fi
+			format="refname:short"
+			refs="refs/tags refs/heads refs/remotes"
+			;;
+		esac
+		git --git-dir="$dir" for-each-ref --format="%($format)" \
+			$refs
 		return
 	fi
 	for i in $(git ls-remote "$dir" 2>/dev/null); do
Signature · 1 line
1.6.0.4.814.gfe502
Signature · 1 line
Shawn.