@matheusbsbs / matheus-bagliano / commits / 91439928ec
Fork 0

completion: improve handling of --orphan option of switch/checkout

The --orphan option is used to create a local branch which is detached from the current history. In git switch, it always resets to the empty tree, and thus the only completion we can provide is a branch name. Follow the same rules for -c/-C (and -b/-B) when completing the argument to --orphan. In the case of git switch, after we complete the argument, there is nothing more we can complete for git switch, so do not even try. Nothing else would be valid. In the case of git checkout, --orphan takes a start point which it uses to determine the checked out tree, even though it created orphaned history. Update the previously added test cases as they are now passing. 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 91439928ecfcb402856dc5dffacdc36ca070d84e
2 files changed +17 -13
contrib/completion/git-completion.bash
+14 -7
@@ -1508,7 +1508,7 @@ _git_checkout ()
1508 local prevword prevword="${words[cword-1]}"
1509
1510 case "$prevword" in
1511 - -b|-B)
1511 + -b|-B|--orphan)
1512 # Complete local branches (and DWIM branch
1513 # remote branch names) for an option argument
1514 # specifying a new branch name. This is for
@@ -1522,14 +1522,14 @@ _git_checkout ()
1522 esac
1523
1524 # At this point, we've already handled special completion for
1525 - # the arguments to -b/-B. There are 3 main things left we can
1526 - # possibly complete:
1527 - # 1) a start-point for -b/-B or -d/--detach
1525 + # the arguments to -b/-B, and --orphan. There are 3 main
1526 + # things left we can possibly complete:
1527 + # 1) a start-point for -b/-B, -d/--detach, or --orphan
1528 # 2) a remote head, for --track
1529 # 3) an arbitrary reference, possibly including DWIM names
1530 #
1531
1532 - if [ -n "$(__git_find_on_cmdline "-b -B -d --detach")" ]; then
1532 + if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
1533 __git_complete_refs --mode="refs"
1534 elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
1535 __git_complete_refs --mode="remote-heads"
@@ -2387,7 +2387,7 @@ _git_switch ()
2387 local prevword prevword="${words[cword-1]}"
2388
2389 case "$prevword" in
2390 - -c|-C)
2390 + -c|-C|--orphan)
2391 # Complete local branches (and DWIM branch
2392 # remote branch names) for an option argument
2393 # specifying a new branch name. This is for
@@ -2400,8 +2400,15 @@ _git_switch ()
2400 ;;
2401 esac
2402
2403 + # Unlike in git checkout, git switch --orphan does not take
2404 + # a start point. Thus we really have nothing to complete after
2405 + # the branch name.
2406 + if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
2407 + return
2408 + fi
2409 +
2410 # At this point, we've already handled special completion for
2404 - # -c/-C. There are 3 main things left to
2411 + # -c/-C, and --orphan. There are 3 main things left to
2412 # complete:
2413 # 1) a start-point for -c/-C or -d/--detach
2414 # 2) a remote head, for --track
t/t9902-completion.sh
+3 -6
@@ -1669,15 +1669,13 @@ test_expect_success 'git switch - with --orphan completes local branch names and
1669 EOF
1670 '
1671
1672 -#TODO: switch --orphan does not take a start-point and thus has nothing to complete
1673 -test_expect_failure 'git switch - --orphan with branch already provided completes nothing else' '
1672 +test_expect_success 'git switch - --orphan with branch already provided completes nothing else' '
1673 test_completion "git switch --orphan master " <<-\EOF
1674
1675 EOF
1676 '
1677
1679 -#TODO: --orphan argument completion should not include all references
1680 -test_expect_failure 'git checkout - with --orphan completes local branch names and unique remote branch names' '
1678 +test_expect_success 'git checkout - with --orphan completes local branch names and unique remote branch names' '
1679 test_completion "git checkout --orphan " <<-\EOF
1680 branch-in-other Z
1681 master Z
@@ -1686,8 +1684,7 @@ test_expect_failure 'git checkout - with --orphan completes local branch names a
1684 EOF
1685 '
1686
1689 -#TODO: checkout --orphan start-point completion should not included DWIM remote unique branch names
1690 -test_expect_failure 'git checkout - --orphan with branch already provided completes local refs for a start-point' '
1687 +test_expect_success 'git checkout - --orphan with branch already provided completes local refs for a start-point' '
1688 test_completion "git checkout --orphan master " <<-\EOF
1689 HEAD Z
1690 master Z