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. Note that a conflict like this would only be produced if a user commits a file with conflict markers, and gets a conflict including that in a susbsequent operation. 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 <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Aug 5, 2018 at 18:20 UTC 4af32207bc106412a456791037fc4a402101fbb4
3 files changed +87 -2
Documentation/technical/rerere.txt
+42
@@ -138,3 +138,45 @@ SHA1('B<NUL>C<NUL>').
138 If there are multiple conflicts in one file, the sha1 is calculated
139 the same way with all hunks appended to each other, in the order in
140 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 common ancestor
148 +version, 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
+8 -2
@@ -365,12 +365,18 @@ static int handle_conflict(struct strbuf *out, struct rerere_io *io,
365 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
366 } hunk = RR_SIDE_1;
367 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
368 - struct strbuf buf = STRBUF_INIT;
368 + struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT;
369 int has_conflicts = -1;
370
371 while (!io->getline(&buf, io)) {
372 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);
380 } else if (is_cmarker(buf.buf, '|', marker_size)) {
381 if (hunk != RR_SIDE_1)
382 break;
t/t4200-rerere.sh
+37
@@ -601,4 +601,41 @@ test_expect_success 'rerere with unexpected conflict markers does not crash' '
601 git rerere clear
602 '
603
604 +test_expect_success 'rerere with inner conflict markers' '
605 + git reset --hard &&
606 +
607 + git checkout -b A master &&
608 + echo "bar" >test &&
609 + git add test &&
610 + git commit -q -m two &&
611 + echo "baz" >test &&
612 + git add test &&
613 + git commit -q -m three &&
614 +
615 + git reset --hard &&
616 + git checkout -b B master &&
617 + echo "foo" >test &&
618 + git add test &&
619 + git commit -q -a -m one &&
620 +
621 + test_must_fail git merge A~ &&
622 + git add test &&
623 + git commit -q -m "will solve conflicts later" &&
624 + test_must_fail git merge A &&
625 +
626 + echo "resolved" >test &&
627 + git add test &&
628 + git commit -q -m "solved conflict" &&
629 +
630 + echo "resolved" >expect &&
631 +
632 + git reset --hard HEAD~~ &&
633 + test_must_fail git merge A~ &&
634 + git add test &&
635 + git commit -q -m "will solve conflicts later" &&
636 + test_must_fail git merge A &&
637 + cat test >actual &&
638 + test_cmp expect actual
639 +'
640 +
641 test_done