Skip to content

Commit 00e7bd2

Browse files
jacob-kellergitster
authored andcommitted
completion: improve handling of --track in switch/checkout
Current completion for the --track option of git switch and git checkout is sub par. In addition to the DWIM logic of a bare branch name, --track has DWIM logic to convert specified remote/branch names into a local branch tracking that remote. For example $git switch --track origin/master This will create a local branch name master, that tracks the master branch of the origin remote. In fact, git switch --track on its own will not accept other forms of references. These must instead be specified manually via the -c/-C/-b/-B options. Introduce __git_remote_heads() and the "remote-heads" mode for __git_complete_refs. Use this when the --track option is provided while completing in _git_switch and _git_checkout. Just as in the --detach case, we never enable DWIM mode for --track, because it doesn't make sense. It should be noted that completion support is still a bit sub par when it comes to handling -c/-C and --orphan. This will be resolved in a future change. Signed-off-by: Jacob Keller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d76a5c commit 00e7bd2

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

contrib/completion/git-completion.bash

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,19 @@ __git_heads ()
624624
"refs/heads/$cur_*" "refs/heads/$cur_*/**"
625625
}
626626

627+
# Lists branches from remote repositories.
628+
# 1: A prefix to be added to each listed branch (optional).
629+
# 2: List only branches matching this word (optional; list all branches if
630+
# unset or empty).
631+
# 3: A suffix to be appended to each listed branch (optional).
632+
__git_remote_heads ()
633+
{
634+
local pfx="${1-}" cur_="${2-}" sfx="${3-}"
635+
636+
__git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
637+
"refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
638+
}
639+
627640
# Lists tags from the local repository.
628641
# Accepts the same positional parameters as __git_heads() above.
629642
__git_tags ()
@@ -783,8 +796,9 @@ __git_refs ()
783796
# --sfx=<suffix>: A suffix to be appended to each ref instead of the default
784797
# space.
785798
# --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
786-
# complete all refs, 'heads' to complete only branches. Note
787-
# that --remote is only compatible with --mode=refs.
799+
# complete all refs, 'heads' to complete only branches, or
800+
# 'remote-heads' to complete only remote branches. Note that
801+
# --remote is only compatible with --mode=refs.
788802
__git_complete_refs ()
789803
{
790804
local remote dwim pfx cur_="$cur" sfx=" " mode="refs"
@@ -810,6 +824,8 @@ __git_complete_refs ()
810824
__gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
811825
heads)
812826
__gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
827+
remote-heads)
828+
__gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
813829
*)
814830
return 1 ;;
815831
esac
@@ -1492,6 +1508,8 @@ _git_checkout ()
14921508

14931509
if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
14941510
__git_complete_refs --mode="refs"
1511+
elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
1512+
__git_complete_refs --mode="remote-heads"
14951513
else
14961514
__git_complete_refs $dwim_opt --mode="refs"
14971515
fi
@@ -2346,6 +2364,8 @@ _git_switch ()
23462364

23472365
if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
23482366
__git_complete_refs --mode="refs"
2367+
elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
2368+
__git_complete_refs --mode="remote-heads"
23492369
else
23502370
__git_complete_refs $dwim_opt --mode="heads"
23512371
fi

t/t9902-completion.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,16 +1404,14 @@ test_expect_success 'git checkout - with -d, complete only references' '
14041404
EOF
14051405
'
14061406

1407-
#TODO: --track should only complete fully specified remote branches
1408-
test_expect_failure 'git switch - with --track, complete only remote branches' '
1407+
test_expect_success 'git switch - with --track, complete only remote branches' '
14091408
test_completion "git switch --track " <<-\EOF
14101409
other/branch-in-other Z
14111410
other/master-in-other Z
14121411
EOF
14131412
'
14141413

1415-
#TODO: --track should only complete fully specified remote branches
1416-
test_expect_failure 'git checkout - with --track, complete only remote branches' '
1414+
test_expect_success 'git checkout - with --track, complete only remote branches' '
14171415
test_completion "git checkout --track " <<-\EOF
14181416
other/branch-in-other Z
14191417
other/master-in-other Z

0 commit comments

Comments
 (0)