Skip to content

Commit 67076b8

Browse files
committed
Merge branch 'ak/restore-both-incompatible-with-conflicts'
"git restore" supports options like "--ours" that are only meaningful during a conflicted merge, but these options are only meaningful when updating the working tree files. These options are marked to be incompatible when both "--staged" and "--worktree" are in effect. * ak/restore-both-incompatible-with-conflicts: restore: fault --staged --worktree with merge opts
2 parents b0d2440 + ee8a888 commit 67076b8

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

builtin/checkout.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,15 +490,28 @@ static int checkout_paths(const struct checkout_opts *opts,
490490
die(_("'%s' must be used when '%s' is not specified"),
491491
"--worktree", "--source");
492492

493-
if (opts->checkout_index && !opts->checkout_worktree &&
494-
opts->writeout_stage)
495-
die(_("'%s' or '%s' cannot be used with %s"),
496-
"--ours", "--theirs", "--staged");
497-
498-
if (opts->checkout_index && !opts->checkout_worktree &&
499-
opts->merge)
500-
die(_("'%s' or '%s' cannot be used with %s"),
501-
"--merge", "--conflict", "--staged");
493+
/*
494+
* Reject --staged option to the restore command when combined with
495+
* merge-related options. Use the accept_ref flag to distinguish it
496+
* from the checkout command, which does not accept --staged anyway.
497+
*
498+
* `restore --ours|--theirs --worktree --staged` could mean resolving
499+
* conflicted paths to one side in both the worktree and the index,
500+
* but does not currently.
501+
*
502+
* `restore --merge|--conflict=<style>` already recreates conflicts
503+
* in both the worktree and the index, so adding --staged would be
504+
* meaningless.
505+
*/
506+
if (!opts->accept_ref && opts->checkout_index) {
507+
if (opts->writeout_stage)
508+
die(_("'%s' or '%s' cannot be used with %s"),
509+
"--ours", "--theirs", "--staged");
510+
511+
if (opts->merge)
512+
die(_("'%s' or '%s' cannot be used with %s"),
513+
"--merge", "--conflict", "--staged");
514+
}
502515

503516
if (opts->patch_mode) {
504517
enum add_p_mode patch_mode;

t/t2070-restore.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,20 @@ test_expect_success 'restore --staged invalidates cache tree for deletions' '
137137
test_must_fail git rev-parse HEAD:new1
138138
'
139139

140+
test_expect_success 'restore with merge options rejects --staged' '
141+
for opts in \
142+
"--staged --ours" \
143+
"--staged --theirs" \
144+
"--staged --merge" \
145+
"--staged --conflict=diff3" \
146+
"--staged --worktree --ours" \
147+
"--staged --worktree --theirs" \
148+
"--staged --worktree --merge" \
149+
"--staged --worktree --conflict=zdiff3"
150+
do
151+
test_must_fail git restore $opts . 2>err &&
152+
grep "cannot be used with --staged" err || return
153+
done
154+
'
155+
140156
test_done

0 commit comments

Comments
 (0)