Skip to content

Commit 5a99c1a

Browse files
phillipwoodgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dbeaf8e commit 5a99c1a

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

builtin/checkout.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct checkout_opts {
100100
struct tree *source_tree;
101101
};
102102

103-
#define CHECKOUT_OPTS_INIT { .conflict_style = -1 }
103+
#define CHECKOUT_OPTS_INIT { .conflict_style = -1, .merge = -1 }
104104

105105
struct branch_info {
106106
char *name; /* The short name used */
@@ -1633,6 +1633,9 @@ static int parse_opt_conflict(const struct option *o, const char *arg, int unset
16331633
opts->conflict_style = parse_conflict_style_name(arg);
16341634
if (opts->conflict_style < 0)
16351635
return error(_("unknown conflict style '%s'"), arg);
1636+
/* --conflict overrides a previous --no-merge */
1637+
if (!opts->merge)
1638+
opts->merge = -1;
16361639

16371640
return 0;
16381641
}
@@ -1740,8 +1743,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
17401743
opts->show_progress = isatty(2);
17411744
}
17421745

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;
17451749

17461750
if (opts->force) {
17471751
opts->discard_changes = 1;

t/t7201-co.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,66 @@ test_expect_success 'checkout --conflict=diff3' '
631631
test_cmp merged file
632632
'
633633

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+
634694
test_expect_success 'checkout with invalid conflict style' '
635695
test_must_fail git checkout --conflict=bad 2>actual -- file &&
636696
echo "error: unknown conflict style ${SQ}bad${SQ}" >expect &&

0 commit comments

Comments
 (0)