Skip to content

Commit 9143992

Browse files
jacob-kellergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent acb658f commit 9143992

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

contrib/completion/git-completion.bash

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ _git_checkout ()
15081508
local prevword prevword="${words[cword-1]}"
15091509

15101510
case "$prevword" in
1511-
-b|-B)
1511+
-b|-B|--orphan)
15121512
# Complete local branches (and DWIM branch
15131513
# remote branch names) for an option argument
15141514
# specifying a new branch name. This is for
@@ -1522,14 +1522,14 @@ _git_checkout ()
15221522
esac
15231523

15241524
# 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
15281528
# 2) a remote head, for --track
15291529
# 3) an arbitrary reference, possibly including DWIM names
15301530
#
15311531

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
15331533
__git_complete_refs --mode="refs"
15341534
elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
15351535
__git_complete_refs --mode="remote-heads"
@@ -2387,7 +2387,7 @@ _git_switch ()
23872387
local prevword prevword="${words[cword-1]}"
23882388

23892389
case "$prevword" in
2390-
-c|-C)
2390+
-c|-C|--orphan)
23912391
# Complete local branches (and DWIM branch
23922392
# remote branch names) for an option argument
23932393
# specifying a new branch name. This is for
@@ -2400,8 +2400,15 @@ _git_switch ()
24002400
;;
24012401
esac
24022402

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+
24032410
# 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
24052412
# complete:
24062413
# 1) a start-point for -c/-C or -d/--detach
24072414
# 2) a remote head, for --track

t/t9902-completion.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,15 +1669,13 @@ test_expect_success 'git switch - with --orphan completes local branch names and
16691669
EOF
16701670
'
16711671

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' '
16741673
test_completion "git switch --orphan master " <<-\EOF
16751674
16761675
EOF
16771676
'
16781677

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' '
16811679
test_completion "git checkout --orphan " <<-\EOF
16821680
branch-in-other Z
16831681
master Z
@@ -1686,8 +1684,7 @@ test_expect_failure 'git checkout - with --orphan completes local branch names a
16861684
EOF
16871685
'
16881686

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' '
16911688
test_completion "git checkout --orphan master " <<-\EOF
16921689
HEAD Z
16931690
master Z

0 commit comments

Comments
 (0)