threads / patch / 16499

patchbash: complete full refs

Subject: [PATCH] bash: complete full refs

## tl;dr

4 messages between Nov 27, 2008 and Nov 28, 2008. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

SZEDER Gábor· Nov 27, 2008, 13:35 UTC · lore

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'.
Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
Show changes to contrib/completion/git-completion.bash +15 −4
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 0ee071b..39bf18b 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -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
-- 
1.6.0.4.814.gfe502
Shawn O. Pearce· Nov 27, 2008, 21:29 UTC · re: SZEDER Gábor · lore

Re: [PATCH] bash: complete full refs

SZEDER GGGbor <szeder@ira.uka.de> wrote:
> Sometimes it's handy to complete full refs, [...]
> 
> To do that, we check whether the ref to be completed starts with
> 'refs'.
...
Show 17 quoted lines
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 0ee071b..39bf18b 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -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*)
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,
Acked-by: Shawn O. Pearce <spearce@spearce.org>
-- 
Shawn.
SZEDER Gábor· Nov 28, 2008, 00:46 UTC · re: Shawn O. Pearce · lore

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'.
Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
On Thu, Nov 27, 2008 at 01:29:38PM -0800, Shawn O. Pearce wrote:
Show 6 quoted 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.
 contrib/completion/git-completion.bash |   19 +++++++++++++++----
 1 files changed, 15 insertions(+), 4 deletions(-)
Show changes to contrib/completion/git-completion.bash +15 −4
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 0ee071b..244ed41 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -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
-- 
1.6.0.4.814.gfe502
Shawn O. Pearce· Nov 28, 2008, 00:51 UTC · re: SZEDER Gábor · lore

Re: [PATCH] bash: complete full refs

SZEDER GGGbor <szeder@ira.uka.de> wrote:
> 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').
...
Show 8 quoted lines
> On Thu, Nov 27, 2008 at 01:29:38PM -0800, Shawn O. Pearce wrote:
> > 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.
Acked-by: Shawn O. Pearce <spearce@spearce.org>
 
Show 37 quoted lines
>  contrib/completion/git-completion.bash |   19 +++++++++++++++----
>  1 files changed, 15 insertions(+), 4 deletions(-)
> 
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 0ee071b..244ed41 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -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
> -- 
> 1.6.0.4.814.gfe502
> 
-- 
Shawn.

← back to recent threads