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 <andy.koppe@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Andy Koppe committed Feb 26, 2023 at 18:43 UTC ee8a88826af1137f0b192caa39b016032ac96af2
2 files changed +38 -9
builtin/checkout.c
+22 -9
@@ -487,15 +487,28 @@ static int checkout_paths(const struct checkout_opts *opts,
487 die(_("'%s' must be used when '%s' is not specified"),
488 "--worktree", "--source");
489
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 + }
512
513 if (opts->patch_mode) {
514 const char *patch_mode;
t/t2070-restore.sh
+16
@@ -137,4 +137,20 @@ test_expect_success 'restore --staged invalidates cache tree for deletions' '
137 test_must_fail git rev-parse HEAD:new1
138 '
139
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 +
156 test_done