Skip to content

Commit

Permalink
Merge branch 'jc/checkout-no-op-switch-errors' into next
Browse files Browse the repository at this point in the history
"git checkout --ours" (no other arguments) complained that the
option is incompatible with branch switching, which is technically
correct, but found confusing by some users.  It now says that the
user needs to give pathspec to specify what paths to checkout.

* jc/checkout-no-op-switch-errors:
  checkout: special case error messages during noop switching
  • Loading branch information
gitster committed Jul 22, 2024
2 parents 5437b7d + d1e6c61 commit 9573259
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
21 changes: 14 additions & 7 deletions builtin/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,10 @@ static void die_if_switching_to_a_branch_in_use(struct checkout_opts *opts,
static int checkout_branch(struct checkout_opts *opts,
struct branch_info *new_branch_info)
{
int noop_switch = (!new_branch_info->name &&
!opts->new_branch &&
!opts->force_detach);

if (opts->pathspec.nr)
die(_("paths cannot be used with switching branches"));

Expand All @@ -1583,9 +1587,14 @@ static int checkout_branch(struct checkout_opts *opts,
die(_("'%s' cannot be used with switching branches"),
"--[no]-overlay");

if (opts->writeout_stage)
die(_("'%s' cannot be used with switching branches"),
"--ours/--theirs");
if (opts->writeout_stage) {
const char *msg;
if (noop_switch)
msg = _("'%s' needs the paths to check out");
else
msg = _("'%s' cannot be used with switching branches");
die(msg, "--ours/--theirs");
}

if (opts->force && opts->merge)
die(_("'%s' cannot be used with '%s'"), "-f", "-m");
Expand All @@ -1612,10 +1621,8 @@ static int checkout_branch(struct checkout_opts *opts,
die(_("Cannot switch branch to a non-commit '%s'"),
new_branch_info->name);

if (!opts->switch_branch_doing_nothing_is_ok &&
!new_branch_info->name &&
!opts->new_branch &&
!opts->force_detach)
if (noop_switch &&
!opts->switch_branch_doing_nothing_is_ok)
die(_("missing branch or commit argument"));

if (!opts->implicit_detach &&
Expand Down
13 changes: 13 additions & 0 deletions t/t7201-co.sh
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,19 @@ test_expect_success 'checkout unmerged stage' '
test ztheirside = "z$(cat file)"
'

test_expect_success 'checkout --ours is incompatible with switching' '
test_must_fail git checkout --ours 2>error &&
test_grep "needs the paths to check out" error &&
test_must_fail git checkout --ours HEAD 2>error &&
test_grep "cannot be used with switching" error &&
test_must_fail git checkout --ours main 2>error &&
test_grep "cannot be used with switching" error &&
git checkout --ours file
'

test_expect_success 'checkout path with --merge from tree-ish is a no-no' '
setup_conflicting_index &&
test_must_fail git checkout -m HEAD -- file
Expand Down

0 comments on commit 9573259

Please sign in to comment.