xdiff: implement a zealous diff3, or "zdiff3"

"zdiff3" is identical to ordinary diff3 except that it allows compaction of common lines on the two sides of history at the beginning or end of the conflict hunk. For example, the following diff3 conflict: 1 2 3 4 <<<<<< A B C D E |||||| 5 6 ====== A X C Y E >>>>>> 7 8 9 has common lines 'A', 'C', and 'E' on the two sides. With zdiff3, one would instead get the following conflict: 1 2 3 4 A <<<<<< B C D |||||| 5 6 ====== X C Y >>>>>> E 7 8 9 Note that the common lines, 'A', and 'E' were moved outside the conflict. Unlike with the two-way conflicts from the 'merge' conflictStyle, the zdiff3 conflict is NOT split into multiple conflict regions to allow the common 'C' lines to be shown outside a conflict, because zdiff3 shows the base version too and the base version cannot be reasonably split. Note also that the removing of lines common to the two sides might make the remaining text inside the conflict region match the base text inside the conflict region (for example, if the diff3 conflict had '5 6 E' on the right side of the conflict, then the common line 'E' would be moved outside and both the base and right side's remaining conflict text would be the lines '5' and '6'). This has the potential to surprise users and make them think there should not have been a conflict, but there definitely was a conflict and it should remain. Based-on-patch-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de> Co-authored-by: Elijah Newren <newren@gmail.com> Signed-off-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Dec 1, 2021 at 00:05 UTC 4496526f80b3e4952036550b279eff8d1babd60a
6 files changed +155 -9
builtin/merge-file.c
+2
@@ -34,6 +34,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
34 struct option options[] = {
35 OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
36 OPT_SET_INT(0, "diff3", &xmp.style, N_("use a diff3 based merge"), XDL_MERGE_DIFF3),
37 + OPT_SET_INT(0, "zdiff3", &xmp.style, N_("use a zealous diff3 based merge"),
38 + XDL_MERGE_ZEALOUS_DIFF3),
39 OPT_SET_INT(0, "ours", &xmp.favor, N_("for conflicts, use our version"),
40 XDL_MERGE_FAVOR_OURS),
41 OPT_SET_INT(0, "theirs", &xmp.favor, N_("for conflicts, use their version"),
contrib/completion/git-completion.bash
+3 -3
@@ -1566,7 +1566,7 @@ _git_checkout ()
1566
1567 case "$cur" in
1568 --conflict=*)
1569 - __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1569 + __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
1570 ;;
1571 --*)
1572 __gitcomp_builtin checkout
@@ -2437,7 +2437,7 @@ _git_switch ()
2437
2438 case "$cur" in
2439 --conflict=*)
2440 - __gitcomp "diff3 merge" "" "${cur##--conflict=}"
2440 + __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2441 ;;
2442 --*)
2443 __gitcomp_builtin switch
@@ -2877,7 +2877,7 @@ _git_restore ()
2877
2878 case "$cur" in
2879 --conflict=*)
2880 - __gitcomp "diff3 merge" "" "${cur##--conflict=}"
2880 + __gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
2881 ;;
2882 --source=*)
2883 __git_complete_refs --cur="${cur##--source=}"
t/t6427-diff3-conflict-markers.sh
+90
@@ -211,4 +211,94 @@ test_expect_success 'rebase --apply describes fake ancestor base' '
211 )
212 '
213
214 +test_setup_zdiff3 () {
215 + test_create_repo zdiff3 &&
216 + (
217 + cd zdiff3 &&
218 +
219 + test_write_lines 1 2 3 4 5 6 7 8 9 >basic &&
220 + test_write_lines 1 2 3 AA 4 5 BB 6 7 8 >middle-common &&
221 + test_write_lines 1 2 3 4 5 6 7 8 9 >interesting &&
222 + test_write_lines 1 2 3 4 5 6 7 8 9 >evil &&
223 +
224 + git add basic middle-common interesting evil &&
225 + git commit -m base &&
226 +
227 + git branch left &&
228 + git branch right &&
229 +
230 + git checkout left &&
231 + test_write_lines 1 2 3 4 A B C D E 7 8 9 >basic &&
232 + test_write_lines 1 2 3 CC 4 5 DD 6 7 8 >middle-common &&
233 + test_write_lines 1 2 3 4 A B C D E F G H I J 7 8 9 >interesting &&
234 + test_write_lines 1 2 3 4 X A B C 7 8 9 >evil &&
235 + git add -u &&
236 + git commit -m letters &&
237 +
238 + git checkout right &&
239 + test_write_lines 1 2 3 4 A X C Y E 7 8 9 >basic &&
240 + test_write_lines 1 2 3 EE 4 5 FF 6 7 8 >middle-common &&
241 + test_write_lines 1 2 3 4 A B C 5 6 G H I J 7 8 9 >interesting &&
242 + test_write_lines 1 2 3 4 Y A B C B C 7 8 9 >evil &&
243 + git add -u &&
244 + git commit -m permuted
245 + )
246 +}
247 +
248 +test_expect_success 'check zdiff3 markers' '
249 + test_setup_zdiff3 &&
250 + (
251 + cd zdiff3 &&
252 +
253 + git checkout left^0 &&
254 +
255 + base=$(git rev-parse --short HEAD^1) &&
256 + test_must_fail git -c merge.conflictstyle=zdiff3 merge -s recursive right^0 &&
257 +
258 + test_write_lines 1 2 3 4 A \
259 + "<<<<<<< HEAD" B C D \
260 + "||||||| $base" 5 6 \
261 + ======= X C Y \
262 + ">>>>>>> right^0" \
263 + E 7 8 9 \
264 + >expect &&
265 + test_cmp expect basic &&
266 +
267 + test_write_lines 1 2 3 \
268 + "<<<<<<< HEAD" CC \
269 + "||||||| $base" AA \
270 + ======= EE \
271 + ">>>>>>> right^0" \
272 + 4 5 \
273 + "<<<<<<< HEAD" DD \
274 + "||||||| $base" BB \
275 + ======= FF \
276 + ">>>>>>> right^0" \
277 + 6 7 8 \
278 + >expect &&
279 + test_cmp expect middle-common &&
280 +
281 + test_write_lines 1 2 3 4 A B C \
282 + "<<<<<<< HEAD" D E F \
283 + "||||||| $base" 5 6 \
284 + ======= 5 6 \
285 + ">>>>>>> right^0" \
286 + G H I J 7 8 9 \
287 + >expect &&
288 + test_cmp expect interesting &&
289 +
290 + # Not passing this one yet; the common "B C" lines is still
291 + # being left in the conflict blocks on the left and right
292 + # sides.
293 + test_write_lines 1 2 3 4 \
294 + "<<<<<<< HEAD" X A \
295 + "||||||| $base" 5 6 \
296 + ======= Y A B C \
297 + ">>>>>>> right^0" \
298 + B C 7 8 9 \
299 + >expect &&
300 + test_cmp expect evil
301 + )
302 +'
303 +
304 test_done
xdiff-interface.c
+2
@@ -313,6 +313,8 @@ int git_xmerge_config(const char *var, const char *value, void *cb)
313 die("'%s' is not a boolean", var);
314 if (!strcmp(value, "diff3"))
315 git_xmerge_style = XDL_MERGE_DIFF3;
316 + else if (!strcmp(value, "zdiff3"))
317 + git_xmerge_style = XDL_MERGE_ZEALOUS_DIFF3;
318 else if (!strcmp(value, "merge"))
319 git_xmerge_style = 0;
320 /*
xdiff/xdiff.h
+1
@@ -66,6 +66,7 @@ extern "C" {
66
67 /* merge output styles */
68 #define XDL_MERGE_DIFF3 1
69 +#define XDL_MERGE_ZEALOUS_DIFF3 2
70
71 typedef struct s_mmfile {
72 char *ptr;
xdiff/xmerge.c
+57 -6
@@ -230,7 +230,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
230 size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, 1,
231 dest ? dest + size : NULL);
232
233 - if (style == XDL_MERGE_DIFF3) {
233 + if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) {
234 /* Shared preimage */
235 if (!dest) {
236 size += marker_size + 1 + needs_cr + marker3_size;
@@ -322,6 +322,40 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
322 return size;
323 }
324
325 +static int recmatch(xrecord_t *rec1, xrecord_t *rec2, unsigned long flags)
326 +{
327 + return xdl_recmatch(rec1->ptr, rec1->size,
328 + rec2->ptr, rec2->size, flags);
329 +}
330 +
331 +/*
332 + * Remove any common lines from the beginning and end of the conflicted region.
333 + */
334 +static void xdl_refine_zdiff3_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m,
335 + xpparam_t const *xpp)
336 +{
337 + xrecord_t **rec1 = xe1->xdf2.recs, **rec2 = xe2->xdf2.recs;
338 + for (; m; m = m->next) {
339 + /* let's handle just the conflicts */
340 + if (m->mode)
341 + continue;
342 +
343 + while(m->chg1 && m->chg2 &&
344 + recmatch(rec1[m->i1], rec2[m->i2], xpp->flags)) {
345 + m->chg1--;
346 + m->chg2--;
347 + m->i1++;
348 + m->i2++;
349 + }
350 + while (m->chg1 && m->chg2 &&
351 + recmatch(rec1[m->i1 + m->chg1 - 1],
352 + rec2[m->i2 + m->chg2 - 1], xpp->flags)) {
353 + m->chg1--;
354 + m->chg2--;
355 + }
356 + }
357 +}
358 +
359 /*
360 * Sometimes, changes are not quite identical, but differ in only a few
361 * lines. Try hard to show only these few lines as conflicting.
@@ -482,7 +516,22 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1,
516 int style = xmp->style;
517 int favor = xmp->favor;
518
485 - if (style == XDL_MERGE_DIFF3) {
519 + /*
520 + * XDL_MERGE_DIFF3 does not attempt to refine conflicts by looking
521 + * at common areas of sides 1 & 2, because the base (side 0) does
522 + * not match and is being shown. Similarly, simplification of
523 + * non-conflicts is also skipped due to the skipping of conflict
524 + * refinement.
525 + *
526 + * XDL_MERGE_ZEALOUS_DIFF3, on the other hand, will attempt to
527 + * refine conflicts looking for common areas of sides 1 & 2.
528 + * However, since the base is being shown and does not match,
529 + * it will only look for common areas at the beginning or end
530 + * of the conflict block. Since XDL_MERGE_ZEALOUS_DIFF3's
531 + * conflict refinement is much more limited in this fashion, the
532 + * conflict simplification will be skipped.
533 + */
534 + if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) {
535 /*
536 * "diff3 -m" output does not make sense for anything
537 * more aggressive than XDL_MERGE_EAGER.
@@ -603,10 +652,12 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1,
652 if (!changes)
653 changes = c;
654 /* refine conflicts */
606 - if (XDL_MERGE_ZEALOUS <= level &&
607 - (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 ||
608 - xdl_simplify_non_conflicts(xe1, changes,
609 - XDL_MERGE_ZEALOUS < level) < 0)) {
655 + if (style == XDL_MERGE_ZEALOUS_DIFF3) {
656 + xdl_refine_zdiff3_conflicts(xe1, xe2, changes, xpp);
657 + } else if (XDL_MERGE_ZEALOUS <= level &&
658 + (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 ||
659 + xdl_simplify_non_conflicts(xe1, changes,
660 + XDL_MERGE_ZEALOUS < level) < 0)) {
661 xdl_cleanup_merge(changes);
662 return -1;
663 }