@matheusbsbs / matheus-bagliano / commits / 68d97c7fdd
Fork 0

completion: improve completion for git switch with no options

Add a new --mode option to __git_complete_refs, which allows changing the behavior to call __git_heads instead of __git_refs. By passing --mode=heads, __git_complete_refs will only output local branches. This enables using "--mode=heads --dwim" to enable listing local branches and the remote unique branch names for DWIM. Refactor completion support to use the new mode option, rather than calling __git_heads directly. This has the advantage that we can now correctly allow local branches along with suitable DWIM refs, rather than only allowing DWIM when we complete all references. Choose what mode it uses when calling __git_complete_refs. If -d or --detach have been provided, then simply complete all refs, but *without* the DWIM option as these DWIM names won't work properly in --detach mode. Otherwise, call __git_complete_refs with the default dwim_opt value and use the new "heads" mode. In this way, the basic support for completing just "git switch <TAB>" will result in only local branches and remote unique names for DWIM. The basic no-options tests for git switch, as well as several of the -c/-C tests now pass, so remove the known breakage tags. 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 68d97c7fdd5bbfd87b2e0b14ddfcd2b1825b2059
2 files changed +26 -25
contrib/completion/git-completion.bash
+20 -13
@@ -782,9 +782,12 @@ __git_refs ()
782 # word to be completed.
783 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
784 # space.
785 +# --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.
788 __git_complete_refs ()
789 {
787 - local remote dwim pfx cur_="$cur" sfx=" "
790 + local remote dwim pfx cur_="$cur" sfx=" " mode="refs"
791
792 while test $# != 0; do
793 case "$1" in
@@ -795,13 +798,23 @@ __git_complete_refs ()
798 --pfx=*) pfx="${1##--pfx=}" ;;
799 --cur=*) cur_="${1##--cur=}" ;;
800 --sfx=*) sfx="${1##--sfx=}" ;;
801 + --mode=*) mode="${1##--mode=}" ;;
802 *) return 1 ;;
803 esac
804 shift
805 done
806
803 - __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")"
807 + # complete references based on the specified mode
808 + case "$mode" in
809 + refs)
810 + __gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
811 + heads)
812 + __gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
813 + *)
814 + return 1 ;;
815 + esac
816
817 + # Append DWIM remote branch names if requested
818 if [ "$dwim" = "yes" ]; then
819 __gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
820 fi
@@ -2324,18 +2337,12 @@ _git_switch ()
2337 __gitcomp_builtin switch
2338 ;;
2339 *)
2327 - local dwim_opt="$(__git_checkout_default_dwim_mode)" only_local_ref=n
2328 - if [ -z "$(__git_find_on_cmdline "-d --detach")" ]; then
2329 - only_local_ref=y
2330 - else
2331 - # --guess --detach is invalid combination, no
2332 - # dwim will be done when --detach is specified
2333 - dwim_opt=
2334 - fi
2335 - if [ $only_local_ref = y -a -z "$dwim_opt" ]; then
2336 - __gitcomp_direct "$(__git_heads "" "$cur" " ")"
2340 + local dwim_opt="$(__git_checkout_default_dwim_mode)"
2341 +
2342 + if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
2343 + __git_complete_refs --mode="refs"
2344 else
2338 - __git_complete_refs $dwim_opt
2345 + __git_complete_refs $dwim_opt --mode="heads"
2346 fi
2347 ;;
2348 esac
t/t9902-completion.sh
+6 -12
@@ -1240,8 +1240,7 @@ test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1240 test_cmp expected out
1241 '
1242
1243 -#TODO: git switch completion includes unexpected references
1244 -test_expect_failure 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
1243 +test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
1244 test_completion "git switch " <<-\EOF
1245 branch-in-other Z
1246 master Z
@@ -1277,8 +1276,7 @@ test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS=1, compl
1276 EOF
1277 '
1278
1280 -#TODO: git switch completion includes unexpected references
1281 -test_expect_failure 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete local branches and unique remote names for DWIM logic' '
1279 +test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete local branches and unique remote names for DWIM logic' '
1280 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch --guess " <<-\EOF
1281 branch-in-other Z
1282 master Z
@@ -1287,8 +1285,7 @@ test_expect_failure 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_G
1285 EOF
1286 '
1287
1290 -#TODO: git switch completion includes unexpected references
1291 -test_expect_failure 'git switch - a later --guess overrides previous --no-guess, complete local and remote unique branches for DWIM' '
1288 +test_expect_success 'git switch - a later --guess overrides previous --no-guess, complete local and remote unique branches for DWIM' '
1289 test_completion "git switch --no-guess --guess " <<-\EOF
1290 branch-in-other Z
1291 master Z
@@ -1585,8 +1582,7 @@ test_expect_success 'git checkout - with -B and --no-track, complete all referen
1582 EOF
1583 '
1584
1588 -#TODO: -c/-C argument completion should not include all references
1589 -test_expect_failure 'git switch - for -c, complete local branches and unique remote branches' '
1585 +test_expect_success 'git switch - for -c, complete local branches and unique remote branches' '
1586 test_completion "git switch -c " <<-\EOF
1587 branch-in-other Z
1588 master Z
@@ -1595,8 +1591,7 @@ test_expect_failure 'git switch - for -c, complete local branches and unique rem
1591 EOF
1592 '
1593
1598 -#TODO: -c/-C argument completion should not include all references
1599 -test_expect_failure 'git switch - for -C, complete local branches and unique remote branches' '
1594 +test_expect_success 'git switch - for -C, complete local branches and unique remote branches' '
1595 test_completion "git switch -C " <<-\EOF
1596 branch-in-other Z
1597 master Z
@@ -1685,8 +1680,7 @@ test_expect_failure 'git checkout - for -B with --no-track, complete local branc
1680 EOF
1681 '
1682
1688 -#TODO: --orphan argument completion should not include all references
1689 -test_expect_failure 'git switch - with --orphan completes local branch names and unique remote branch names' '
1683 +test_expect_success 'git switch - with --orphan completes local branch names and unique remote branch names' '
1684 test_completion "git switch --orphan " <<-\EOF
1685 branch-in-other Z
1686 master Z