@@ -386,16 +386,19 @@ __git_tags ()
386386 done
387387}
388388
389- # __git_refs accepts 0 or 1 arguments (to pass to __gitdir)
389+ # __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments
390+ # presence of 2nd argument means use the guess heuristic employed
391+ # by checkout for tracking branches
390392__git_refs ()
391393{
392- local i is_hash=y dir=" $( __gitdir " ${1-} " ) "
394+ local i is_hash=y dir=" $( __gitdir " ${1-} " ) " track= " ${2-} "
393395 local cur=" ${COMP_WORDS[COMP_CWORD]} " format refs
394396 if [ -d " $dir " ]; then
395397 case " $cur " in
396398 refs|refs/* )
397399 format=" refname"
398400 refs=" ${cur%/* } "
401+ track=" "
399402 ;;
400403 * )
401404 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
@@ -407,6 +410,21 @@ __git_refs ()
407410 esac
408411 git --git-dir=" $dir " for-each-ref --format=" %($format )" \
409412 $refs
413+ if [ -n " $track " ]; then
414+ # employ the heuristic used by git checkout
415+ # Try to find a remote branch that matches the completion word
416+ # but only output if the branch name is unique
417+ local ref entry
418+ git --git-dir=" $dir " for-each-ref --shell --format=" ref=%(refname:short)" \
419+ " refs/remotes/" | \
420+ while read entry; do
421+ eval " $entry "
422+ ref=" ${ref#*/ } "
423+ if [[ " $ref " == " $cur " * ]]; then
424+ echo " $ref "
425+ fi
426+ done | uniq -u
427+ fi
410428 return
411429 fi
412430 for i in $( git ls-remote " $dir " 2> /dev/null) ; do
@@ -1011,7 +1029,13 @@ _git_checkout ()
10111029 "
10121030 ;;
10131031 * )
1014- __gitcomp " $( __git_refs) "
1032+ # check if --track, --no-track, or --no-guess was specified
1033+ # if so, disable DWIM mode
1034+ local flags=" --track --no-track --no-guess" track=1
1035+ if [ -n " $( __git_find_on_cmdline " $flags " ) " ]; then
1036+ track=' '
1037+ fi
1038+ __gitcomp " $( __git_refs ' ' $track ) "
10151039 ;;
10161040 esac
10171041}
0 commit comments