;;
*)
local dwim_opt="$(__git_checkout_default_dwim_mode)"
+ local prevword prevword="${words[cword-1]}"
+
+ case "$prevword" in
+ -b|-B)
+ # Complete local branches (and DWIM branch
+ # remote branch names) for an option argument
+ # specifying a new branch name. This is for
+ # convenience, assuming new branches are
+ # possibly based on pre-existing branch names.
+ __git_complete_refs $dwim_opt --mode="heads"
+ return
+ ;;
+ *)
+ ;;
+ esac
+
+ # At this point, we've already handled special completion for
+ # the arguments to -b/-B. There are 3 main things left we can
+ # possibly complete:
+ # 1) a start-point for -b/-B or -d/--detach
+ # 2) a remote head, for --track
+ # 3) an arbitrary reference, possibly including DWIM names
+ #
- if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
+ if [ -n "$(__git_find_on_cmdline "-b -B -d --detach")" ]; then
__git_complete_refs --mode="refs"
elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
__git_complete_refs --mode="remote-heads"
;;
*)
local dwim_opt="$(__git_checkout_default_dwim_mode)"
+ local prevword prevword="${words[cword-1]}"
+
+ case "$prevword" in
+ -c|-C)
+ # Complete local branches (and DWIM branch
+ # remote branch names) for an option argument
+ # specifying a new branch name. This is for
+ # convenience, assuming new branches are
+ # possibly based on pre-existing branch names.
+ __git_complete_refs $dwim_opt --mode="heads"
+ return
+ ;;
+ *)
+ ;;
+ esac
+
+ # At this point, we've already handled special completion for
+ # -c/-C. There are 3 main things left to
+ # complete:
+ # 1) a start-point for -c/-C or -d/--detach
+ # 2) a remote head, for --track
+ # 3) a branch name, possibly including DWIM remote branches
- if [ -n "$(__git_find_on_cmdline "-d --detach")" ]; then
+ if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
__git_complete_refs --mode="refs"
elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
__git_complete_refs --mode="remote-heads"
EOF
'
-#TODO: completing the start point of -c/-C should not include DWIM references
-test_expect_failure 'git switch - with -c, complete all references' '
+test_expect_success 'git switch - with -c, complete all references' '
test_completion "git switch -c new-branch " <<-\EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -c/-C should not include DWIM references
-test_expect_failure 'git switch - with -C, complete all references' '
+test_expect_success 'git switch - with -C, complete all references' '
test_completion "git switch -C new-branch " <<-\EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -c/-C should include all references, not just local branches
-test_expect_failure 'git switch - with -c and --track, complete all references' '
+test_expect_success 'git switch - with -c and --track, complete all references' '
test_completion "git switch -c new-branch --track " <<-EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -c/-C should include all references, not just local branches
-test_expect_failure 'git switch - with -C and --track, complete all references' '
+test_expect_success 'git switch - with -C and --track, complete all references' '
test_completion "git switch -C new-branch --track " <<-EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -c/-C should include all references, not just local branches
-test_expect_failure 'git switch - with -c and --no-track, complete all references' '
+test_expect_success 'git switch - with -c and --no-track, complete all references' '
test_completion "git switch -c new-branch --no-track " <<-\EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -c/-C should include all references, not just local branches
-test_expect_failure 'git switch - with -C and --no-track, complete all references' '
+test_expect_success 'git switch - with -C and --no-track, complete all references' '
test_completion "git switch -C new-branch --no-track " <<-\EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -b/-B should not include DWIM references
-test_expect_failure 'git checkout - with -b, complete all references' '
+test_expect_success 'git checkout - with -b, complete all references' '
test_completion "git checkout -b new-branch " <<-\EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -b/-B should not include DWIM references
-test_expect_failure 'git checkout - with -B, complete all references' '
+test_expect_success 'git checkout - with -B, complete all references' '
test_completion "git checkout -B new-branch " <<-\EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -b/-B should not include DWIM references
-test_expect_failure 'git checkout - with -b and --track, complete all references' '
+test_expect_success 'git checkout - with -b and --track, complete all references' '
test_completion "git checkout -b new-branch --track " <<-EOF
HEAD Z
master Z
EOF
'
-#TODO: completing the start point of -b/-B should not include DWIM references
-test_expect_failure 'git checkout - with -B and --track, complete all references' '
+test_expect_success 'git checkout - with -B and --track, complete all references' '
test_completion "git checkout -B new-branch --track " <<-EOF
HEAD Z
master Z
EOF
'
-#TODO: -b/-B argument completion should not include all references
-test_expect_failure 'git checkout - for -b, complete local branches and unique remote branches' '
+test_expect_success 'git checkout - for -b, complete local branches and unique remote branches' '
test_completion "git checkout -b " <<-\EOF
branch-in-other Z
master Z
EOF
'
-#TODO: -b/-B argument completion should not include all references
-test_expect_failure 'git checkout - for -B, complete local branches and unique remote branches' '
+test_expect_success 'git checkout - for -B, complete local branches and unique remote branches' '
test_completion "git checkout -B " <<-\EOF
branch-in-other Z
master Z
EOF
'
-#TODO: -b/-B argument completion should not include all references
-test_expect_failure 'git checkout - for -b with --no-guess, complete local branches only' '
+test_expect_success 'git checkout - for -b with --no-guess, complete local branches only' '
test_completion "git checkout --no-guess -b " <<-\EOF
master Z
matching-branch Z
EOF
'
-#TODO: -b/-B argument completion should not include all references
-test_expect_failure 'git checkout - for -B with --no-guess, complete local branches only' '
+test_expect_success 'git checkout - for -B with --no-guess, complete local branches only' '
test_completion "git checkout --no-guess -B " <<-\EOF
master Z
matching-branch Z
EOF
'
-#TODO: -b/-B argument completion should not include all references
-test_expect_failure 'git checkout - for -b with --no-track, complete local branches only' '
+test_expect_success 'git checkout - for -b with --no-track, complete local branches only' '
test_completion "git checkout --no-track -b " <<-\EOF
master Z
matching-branch Z
EOF
'
-#TODO: -b/-B argument completion should not include all references
-test_expect_failure 'git checkout - for -B with --no-track, complete local branches only' '
+test_expect_success 'git checkout - for -B with --no-track, complete local branches only' '
test_completion "git checkout --no-track -B " <<-\EOF
master Z
matching-branch Z