checkout: fix interaction between --conflict and --merge

When using "git checkout" to recreate merge conflicts or merge uncommitted changes when switching branch "--conflict" sensibly implies "--merge". Unfortunately the way this is implemented means that "git checkout --conflict=diff3 --no-merge" implies "--merge" violating the usual last-one-wins rule. Fix this by only overriding the value of opts->merge if "--conflicts" comes after "--no-merge" or "-[-no]-merge" is not given on the command line. The behavior of "git checkout --merge --no-conflict" is unchanged and will still merge on the basis that the "-[-no]-conflict" options are primarily intended to affect the conflict style and so "--no-conflict" should cancel a previous "--conflict" but not override "--merge". Of the four new tests the second one tests the behavior change introduced by this commit, the other three check that this commit does not regress the existing behavior. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Mar 14, 2024 at 17:05 UTC 5a99c1ac1a9640f7ee0374e9b90523f500bdbb5a
2 files changed +67 -3
builtin/checkout.c
+7 -3
@@ -100,7 +100,7 @@ struct checkout_opts {
100 struct tree *source_tree;
101 };
102
103 -#define CHECKOUT_OPTS_INIT { .conflict_style = -1 }
103 +#define CHECKOUT_OPTS_INIT { .conflict_style = -1, .merge = -1 }
104
105 struct branch_info {
106 char *name; /* The short name used */
@@ -1633,6 +1633,9 @@ static int parse_opt_conflict(const struct option *o, const char *arg, int unset
1633 opts->conflict_style = parse_conflict_style_name(arg);
1634 if (opts->conflict_style < 0)
1635 return error(_("unknown conflict style '%s'"), arg);
1636 + /* --conflict overrides a previous --no-merge */
1637 + if (!opts->merge)
1638 + opts->merge = -1;
1639
1640 return 0;
1641 }
@@ -1740,8 +1743,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1743 opts->show_progress = isatty(2);
1744 }
1745
1743 - if (opts->conflict_style >= 0)
1744 - opts->merge = 1; /* implied */
1746 + /* --conflicts implies --merge */
1747 + if (opts->merge == -1)
1748 + opts->merge = opts->conflict_style >= 0;
1749
1750 if (opts->force) {
1751 opts->discard_changes = 1;
t/t7201-co.sh
+60
@@ -631,6 +631,66 @@ test_expect_success 'checkout --conflict=diff3' '
631 test_cmp merged file
632 '
633
634 +test_expect_success 'checkout --conflict=diff3 --no-conflict does not merge' '
635 + setup_conflicting_index &&
636 + echo "none of the above" >expect &&
637 + cat expect >fild &&
638 + cat expect >file &&
639 + test_must_fail git checkout --conflict=diff3 --no-conflict -- fild file 2>err &&
640 + test_cmp expect file &&
641 + test_cmp expect fild &&
642 + echo "error: path ${SQ}file${SQ} is unmerged" >expect &&
643 + test_cmp expect err
644 +'
645 +
646 +test_expect_success 'checkout --conflict=diff3 --no-merge does not merge' '
647 + setup_conflicting_index &&
648 + echo "none of the above" >expect &&
649 + cat expect >fild &&
650 + cat expect >file &&
651 + test_must_fail git checkout --conflict=diff3 --no-merge -- fild file 2>err &&
652 + test_cmp expect file &&
653 + test_cmp expect fild &&
654 + echo "error: path ${SQ}file${SQ} is unmerged" >expect &&
655 + test_cmp expect err
656 +'
657 +
658 +test_expect_success 'checkout --no-merge --conflict=diff3 does merge' '
659 + setup_conflicting_index &&
660 + echo "none of the above" >fild &&
661 + echo "none of the above" >file &&
662 + git checkout --no-merge --conflict=diff3 -- fild file &&
663 + echo "ourside" >expect &&
664 + test_cmp expect fild &&
665 + cat >expect <<-\EOF &&
666 + <<<<<<< ours
667 + ourside
668 + ||||||| base
669 + original
670 + =======
671 + theirside
672 + >>>>>>> theirs
673 + EOF
674 + test_cmp expect file
675 +'
676 +
677 +test_expect_success 'checkout --merge --conflict=diff3 --no-conflict does merge' '
678 + setup_conflicting_index &&
679 + echo "none of the above" >fild &&
680 + echo "none of the above" >file &&
681 + git checkout --merge --conflict=diff3 --no-conflict -- fild file &&
682 + echo "ourside" >expect &&
683 + test_cmp expect fild &&
684 + cat >expect <<-\EOF &&
685 + <<<<<<< ours
686 + ourside
687 + =======
688 + theirside
689 + >>>>>>> theirs
690 + EOF
691 + test_cmp expect file
692 +'
693 +
694 test_expect_success 'checkout with invalid conflict style' '
695 test_must_fail git checkout --conflict=bad 2>actual -- file &&
696 echo "error: unknown conflict style ${SQ}bad${SQ}" >expect &&