@matheusbsbs / matheus-bagliano / commits / db8a9ff038
Fork 0

bash completion: Resolve git show ref:path<tab> losing ref: portion

Linus reported that the bash completion for git show often dropped the ref portion of the argument (stuff before the :) when trying to complete a file name of a file in another branch or tag. Björn Steinbrink tracked it down to the gvfs completion script which comes standard on many Fedora Core based systems. That is removing : from COMP_WORDBREAKS, making readline treat the entire argument (including the ref) as the name that must be completed. When the git completion routines supplied a completion of just the filename, readline replaced everything. Since Git users often need to use "ref:path" or "ref:ref" sort of arguments, and expect completion support on both sides of the : we really want the : in COMP_WORDBREAKS to provide a good user experience. This is also the default that ships with bash as it can be useful in other contexts, such as rcp/scp. We now try to add : back to COMP_WORDBREAKS if it has been removed by a script that loaded before us. However if this doesn't work (as the : is stripped after we load) we fallback in the completion routines to include "ref:" as part of the prefix for completions, allowing readine to fully insert the argument the user wanted. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Jul 15, 2008 at 05:52 UTC db8a9ff03831a26aa8bfad8bb026b90739d684ec
1 file changed +25 -2
contrib/completion/git-completion.bash
+25 -2
@@ -45,6 +45,11 @@
45 # git@vger.kernel.org
46 #
47
48 +case "$COMP_WORDBREAKS" in
49 +*:*) : great ;;
50 +*) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
51 +esac
52 +
53 __gitdir ()
54 {
55 if [ -z "$1" ]; then
@@ -294,6 +299,12 @@ __git_complete_file ()
299 ls="$ref"
300 ;;
301 esac
302 +
303 + case "$COMP_WORDBREAKS" in
304 + *:*) : great ;;
305 + *) pfx="$ref:$pfx" ;;
306 + esac
307 +
308 local IFS=$'\n'
309 COMPREPLY=($(compgen -P "$pfx" \
310 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
@@ -700,7 +711,12 @@ _git_fetch ()
711 *)
712 case "$cur" in
713 *:*)
703 - __gitcomp "$(__git_refs)" "" "${cur#*:}"
714 + local pfx=""
715 + case "$COMP_WORDBREAKS" in
716 + *:*) : great ;;
717 + *) pfx="${cur%%:*}:" ;;
718 + esac
719 + __gitcomp "$(__git_refs)" "$pfx" "${cur#*:}"
720 ;;
721 *)
722 local remote
@@ -873,7 +889,14 @@ _git_push ()
889 git-push) remote="${COMP_WORDS[1]}" ;;
890 git) remote="${COMP_WORDS[2]}" ;;
891 esac
876 - __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
892 +
893 + local pfx=""
894 + case "$COMP_WORDBREAKS" in
895 + *:*) : great ;;
896 + *) pfx="${cur%%:*}:" ;;
897 + esac
898 +
899 + __gitcomp "$(__git_refs "$remote")" "$pfx" "${cur#*:}"
900 ;;
901 +*)
902 __gitcomp "$(__git_refs)" + "${cur#+}"