Skip to content

Commit ee8a888

Browse files
ak2gitster
authored andcommitted
restore: fault --staged --worktree with merge opts
The 'restore' command already rejects the --merge, --conflict, --ours and --theirs options when combined with --staged, but accepts them when --worktree is added as well. Unfortunately that doesn't appear to do anything useful. The --ours and --theirs options seem to be ignored when both --staged and --worktree are given, whereas with --merge or --conflict, the command has the same effect as if the --staged option wasn't present. So reject those options with '--staged --worktree' as well, using opts->accept_ref to distinguish restore from checkout. Add test for both '--staged' and '--staged --worktree'. Signed-off-by: Andy Koppe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7556e5d commit ee8a888

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
@@ -487,15 +487,28 @@ static int checkout_paths(const struct checkout_opts *opts,
487487
die(_("'%s' must be used when '%s' is not specified"),
488488
"--worktree", "--source");
489489

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

500513
if (opts->patch_mode) {
501514
const char *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)