Skip to content

Commit 34a6bbb

Browse files
lilyballgitster
authored andcommitted
completion: Support the DWIM mode for git checkout
Ever since commit 70c9ac2 (DWIM: "git checkout frotz" to "git checkout -b frotz origin/frotz"), git checkout has supported a DWIM mode where it creates a local tracking branch for a remote branch if just the name of the remote branch is specified on the command-line and only one remote has a branch with that name. Teach the bash completion script to understand this DWIM mode and provide such remote-tracking branch names as possible completions. Signed-off-by: Kevin Ballard <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c752e7f commit 34a6bbb

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

contrib/completion/git-completion.bash

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,16 +380,19 @@ __git_tags ()
380380
done
381381
}
382382

383-
# __git_refs accepts 0 or 1 arguments (to pass to __gitdir)
383+
# __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments
384+
# presence of 2nd argument means use the guess heuristic employed
385+
# by checkout for tracking branches
384386
__git_refs ()
385387
{
386-
local i is_hash=y dir="$(__gitdir "${1-}")"
388+
local i is_hash=y dir="$(__gitdir "${1-}")" track="${2-}"
387389
local cur="${COMP_WORDS[COMP_CWORD]}" format refs
388390
if [ -d "$dir" ]; then
389391
case "$cur" in
390392
refs|refs/*)
391393
format="refname"
392394
refs="${cur%/*}"
395+
track=""
393396
;;
394397
*)
395398
for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
@@ -401,6 +404,21 @@ __git_refs ()
401404
esac
402405
git --git-dir="$dir" for-each-ref --format="%($format)" \
403406
$refs
407+
if [ -n "$track" ]; then
408+
# employ the heuristic used by git checkout
409+
# Try to find a remote branch that matches the completion word
410+
# but only output if the branch name is unique
411+
local ref entry
412+
git --git-dir="$dir" for-each-ref --shell --format="ref=%(refname:short)" \
413+
"refs/remotes/" | \
414+
while read entry; do
415+
eval "$entry"
416+
ref="${ref#*/}"
417+
if [[ "$ref" == "$cur"* ]]; then
418+
echo "$ref"
419+
fi
420+
done | uniq -u
421+
fi
404422
return
405423
fi
406424
for i in $(git ls-remote "$dir" 2>/dev/null); do
@@ -988,7 +1006,13 @@ _git_checkout ()
9881006
"
9891007
;;
9901008
*)
991-
__gitcomp "$(__git_refs)"
1009+
# check if --track, --no-track, or --no-guess was specified
1010+
# if so, disable DWIM mode
1011+
local flags="--track --no-track --no-guess" track=1
1012+
if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1013+
track=''
1014+
fi
1015+
__gitcomp "$(__git_refs '' $track)"
9921016
;;
9931017
esac
9941018
}

0 commit comments

Comments
 (0)