@@ -386,16 +386,19 @@ __git_tags ()
386
386
done
387
387
}
388
388
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
390
392
__git_refs ()
391
393
{
392
- local i is_hash=y dir=" $( __gitdir " ${1-} " ) "
394
+ local i is_hash=y dir=" $( __gitdir " ${1-} " ) " track= " ${2-} "
393
395
local cur=" ${COMP_WORDS[COMP_CWORD]} " format refs
394
396
if [ -d " $dir " ]; then
395
397
case " $cur " in
396
398
refs|refs/* )
397
399
format=" refname"
398
400
refs=" ${cur%/* } "
401
+ track=" "
399
402
;;
400
403
* )
401
404
for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
@@ -407,6 +410,21 @@ __git_refs ()
407
410
esac
408
411
git --git-dir=" $dir " for-each-ref --format=" %($format )" \
409
412
$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
410
428
return
411
429
fi
412
430
for i in $( git ls-remote " $dir " 2> /dev/null) ; do
@@ -1011,7 +1029,13 @@ _git_checkout ()
1011
1029
"
1012
1030
;;
1013
1031
* )
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 ) "
1015
1039
;;
1016
1040
esac
1017
1041
}
0 commit comments