Skip to content

Commit 71a30ee

Browse files
tgummerergitster
authored andcommitted
rerere: teach rerere to handle nested conflicts
Currently rerere can't handle nested conflicts and will error out when it encounters such conflicts. Do that by recursively calling the 'handle_conflict' function to normalize the conflict. The conflict ID calculation here deserves some explanation: As we are using the same handle_conflict function, the nested conflict is normalized the same way as for non-nested conflicts, which means the ancestor in the diff3 case is stripped out, and the parts of the conflict are ordered alphabetically. The conflict ID is however is only calculated in the top level handle_conflict call, so it will include the markers that 'rerere' adds to the output. e.g. say there's the following conflict: <<<<<<< HEAD 1 ======= <<<<<<< HEAD 3 ======= 2 >>>>>>> branch-2 >>>>>>> branch-3~ it would be recorde as follows in the preimage: <<<<<<< 1 ======= <<<<<<< 2 ======= 3 >>>>>>> >>>>>>> and the conflict ID would be calculated as sha1(1<NUL><<<<<<< 2 ======= 3 >>>>>>><NUL>) Stripping out vs. leaving the conflict markers in place in the inner conflict should have no practical impact, but it simplifies the implementation. Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 515154e commit 71a30ee

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

Documentation/technical/rerere.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,45 @@ SHA1('B<NUL>C<NUL>').
138138
If there are multiple conflicts in one file, the sha1 is calculated
139139
the same way with all hunks appended to each other, in the order in
140140
which they appear in the file, separated by a <NUL> character.
141+
142+
Nested conflicts
143+
~~~~~~~~~~~~~~~~
144+
145+
Nested conflicts are handled very similarly to "simple" conflicts.
146+
Similar to simple conflicts, the conflict is first normalized by
147+
stripping the labels from conflict markers, stripping the diff3
148+
output, and the sorting the conflict hunks, both for the outer and the
149+
inner conflict. This is done recursively, so any number of nested
150+
conflicts can be handled.
151+
152+
The only difference is in how the conflict ID is calculated. For the
153+
inner conflict, the conflict markers themselves are not stripped out
154+
before calculating the sha1.
155+
156+
Say we have the following conflict for example:
157+
158+
<<<<<<< HEAD
159+
1
160+
=======
161+
<<<<<<< HEAD
162+
3
163+
=======
164+
2
165+
>>>>>>> branch-2
166+
>>>>>>> branch-3~
167+
168+
After stripping out the labels of the conflict markers, and sorting
169+
the hunks, the conflict would look as follows:
170+
171+
<<<<<<<
172+
1
173+
=======
174+
<<<<<<<
175+
2
176+
=======
177+
3
178+
>>>>>>>
179+
>>>>>>>
180+
181+
and finally the conflict ID would be calculated as:
182+
`sha1('1<NUL><<<<<<<\n3\n=======\n2\n>>>>>>><NUL>')`

rerere.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,18 @@ static int handle_conflict(struct strbuf *out, struct rerere_io *io,
365365
RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
366366
} hunk = RR_SIDE_1;
367367
struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
368-
struct strbuf buf = STRBUF_INIT;
368+
struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT;
369369
int has_conflicts = -1;
370370

371371
while (!io->getline(&buf, io)) {
372372
if (is_cmarker(buf.buf, '<', marker_size)) {
373-
break;
373+
if (handle_conflict(&conflict, io, marker_size, NULL) < 0)
374+
break;
375+
if (hunk == RR_SIDE_1)
376+
strbuf_addbuf(&one, &conflict);
377+
else
378+
strbuf_addbuf(&two, &conflict);
379+
strbuf_release(&conflict);
374380
} else if (is_cmarker(buf.buf, '|', marker_size)) {
375381
if (hunk != RR_SIDE_1)
376382
break;

t/t4200-rerere.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,41 @@ test_expect_success 'rerere with unexpected conflict markers does not crash' '
602602
git rerere clear
603603
'
604604

605+
test_expect_success 'rerere with inner conflict markers' '
606+
git reset --hard &&
607+
608+
git checkout -b A master &&
609+
echo "bar" >test &&
610+
git add test &&
611+
git commit -q -m two &&
612+
echo "baz" >test &&
613+
git add test &&
614+
git commit -q -m three &&
615+
616+
git reset --hard &&
617+
git checkout -b B master &&
618+
echo "foo" >test &&
619+
git add test &&
620+
git commit -q -a -m one &&
621+
622+
test_must_fail git merge A~ &&
623+
git add test &&
624+
git commit -q -m "will solve conflicts later" &&
625+
test_must_fail git merge A &&
626+
627+
echo "resolved" >test &&
628+
git add test &&
629+
git commit -q -m "solved conflict" &&
630+
631+
echo "resolved" >expect &&
632+
633+
git reset --hard HEAD~~ &&
634+
test_must_fail git merge A~ &&
635+
git add test &&
636+
git commit -q -m "will solve conflicts later" &&
637+
test_must_fail git merge A &&
638+
cat test >actual &&
639+
test_cmp expect actual
640+
'
641+
605642
test_done

0 commit comments

Comments
 (0)