Code

bash: complete full refs
authorSZEDER Gábor <szeder@ira.uka.de>
Fri, 28 Nov 2008 00:46:38 +0000 (01:46 +0100)
committerJunio C Hamano <gitster@pobox.com>
Fri, 28 Nov 2008 02:34:30 +0000 (18:34 -0800)
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>
Acked-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/completion/git-completion.bash

index de193ba7c1caf69367410d763f3541555a64746f..5fb34c49dc44b962dfeea447d58b11957d875bc5 100755 (executable)
@@ -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