@matheusbsbs / matheus-bagliano / commits / 00e7bd2b00
Fork 0

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 <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Keller committed May 28, 2020 at 11:10 UTC 00e7bd2b0057a20d78193405af73b0acebdd86d2
2 files changed +24 -6
contrib/completion/git-completion.bash
+22 -2
@@ -624,6 +624,19 @@ __git_heads ()
624 "refs/heads/$cur_*" "refs/heads/$cur_*/**"
625 }
626
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 +
640 # Lists tags from the local repository.
641 # Accepts the same positional parameters as __git_heads() above.
642 __git_tags ()
@@ -783,8 +796,9 @@ __git_refs ()
796 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
797 # space.
798 # --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.
802 __git_complete_refs ()
803 {
804 local remote dwim pfx cur_="$cur" sfx=" " mode="refs"
@@ -810,6 +824,8 @@ __git_complete_refs ()
824 __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
825 heads)
826 __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
827 + remote-heads)
828 + __gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
829 *)
830 return 1 ;;
831 esac
@@ -1492,6 +1508,8 @@ _git_checkout ()
1508
1509 if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
1510 __git_complete_refs --mode="refs"
1511 + elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
1512 + __git_complete_refs --mode="remote-heads"
1513 else
1514 __git_complete_refs $dwim_opt --mode="refs"
1515 fi
@@ -2346,6 +2364,8 @@ _git_switch ()
2364
2365 if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
2366 __git_complete_refs --mode="refs"
2367 + elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
2368 + __git_complete_refs --mode="remote-heads"
2369 else
2370 __git_complete_refs $dwim_opt --mode="heads"
2371 fi
t/t9902-completion.sh
+2 -4
@@ -1404,16 +1404,14 @@ test_expect_success 'git checkout - with -d, complete only references' '
1404 EOF
1405 '
1406
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' '
1408 test_completion "git switch --track " <<-\EOF
1409 other/branch-in-other Z
1410 other/master-in-other Z
1411 EOF
1412 '
1413
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' '
1415 test_completion "git checkout --track " <<-\EOF
1416 other/branch-in-other Z
1417 other/master-in-other Z