Skip to content

Commit 90139ae

Browse files
committed
Merge branch 'jc/checkout-no-op-switch-errors'
"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
2 parents d71121c + d1e6c61 commit 90139ae

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

builtin/checkout.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,10 @@ static void die_if_switching_to_a_branch_in_use(struct checkout_opts *opts,
15721572
static int checkout_branch(struct checkout_opts *opts,
15731573
struct branch_info *new_branch_info)
15741574
{
1575+
int noop_switch = (!new_branch_info->name &&
1576+
!opts->new_branch &&
1577+
!opts->force_detach);
1578+
15751579
if (opts->pathspec.nr)
15761580
die(_("paths cannot be used with switching branches"));
15771581

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

1586-
if (opts->writeout_stage)
1587-
die(_("'%s' cannot be used with switching branches"),
1588-
"--ours/--theirs");
1590+
if (opts->writeout_stage) {
1591+
const char *msg;
1592+
if (noop_switch)
1593+
msg = _("'%s' needs the paths to check out");
1594+
else
1595+
msg = _("'%s' cannot be used with switching branches");
1596+
die(msg, "--ours/--theirs");
1597+
}
15891598

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

1615-
if (!opts->switch_branch_doing_nothing_is_ok &&
1616-
!new_branch_info->name &&
1617-
!opts->new_branch &&
1618-
!opts->force_detach)
1624+
if (noop_switch &&
1625+
!opts->switch_branch_doing_nothing_is_ok)
16191626
die(_("missing branch or commit argument"));
16201627

16211628
if (!opts->implicit_detach &&

t/t7201-co.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,19 @@ test_expect_success 'checkout unmerged stage' '
498498
test ztheirside = "z$(cat file)"
499499
'
500500

501+
test_expect_success 'checkout --ours is incompatible with switching' '
502+
test_must_fail git checkout --ours 2>error &&
503+
test_grep "needs the paths to check out" error &&
504+
505+
test_must_fail git checkout --ours HEAD 2>error &&
506+
test_grep "cannot be used with switching" error &&
507+
508+
test_must_fail git checkout --ours main 2>error &&
509+
test_grep "cannot be used with switching" error &&
510+
511+
git checkout --ours file
512+
'
513+
501514
test_expect_success 'checkout path with --merge from tree-ish is a no-no' '
502515
setup_conflicting_index &&
503516
test_must_fail git checkout -m HEAD -- file

0 commit comments

Comments
 (0)