Skip to content

Commit 4496526

Browse files
phillipwoodnewren
authored andcommitted
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>
1 parent abe6bb3 commit 4496526

File tree

6 files changed

+155
-9
lines changed

6 files changed

+155
-9
lines changed

builtin/merge-file.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ int cmd_merge_file(int argc, const char **argv, const char *prefix)
3434
struct option options[] = {
3535
OPT_BOOL('p', "stdout", &to_stdout, N_("send results to standard output")),
3636
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),
3739
OPT_SET_INT(0, "ours", &xmp.favor, N_("for conflicts, use our version"),
3840
XDL_MERGE_FAVOR_OURS),
3941
OPT_SET_INT(0, "theirs", &xmp.favor, N_("for conflicts, use their version"),

contrib/completion/git-completion.bash

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ _git_checkout ()
15661566

15671567
case "$cur" in
15681568
--conflict=*)
1569-
__gitcomp "diff3 merge" "" "${cur##--conflict=}"
1569+
__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
15701570
;;
15711571
--*)
15721572
__gitcomp_builtin checkout
@@ -2437,7 +2437,7 @@ _git_switch ()
24372437

24382438
case "$cur" in
24392439
--conflict=*)
2440-
__gitcomp "diff3 merge" "" "${cur##--conflict=}"
2440+
__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
24412441
;;
24422442
--*)
24432443
__gitcomp_builtin switch
@@ -2877,7 +2877,7 @@ _git_restore ()
28772877

28782878
case "$cur" in
28792879
--conflict=*)
2880-
__gitcomp "diff3 merge" "" "${cur##--conflict=}"
2880+
__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
28812881
;;
28822882
--source=*)
28832883
__git_complete_refs --cur="${cur##--source=}"

t/t6427-diff3-conflict-markers.sh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,94 @@ test_expect_success 'rebase --apply describes fake ancestor base' '
211211
)
212212
'
213213

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+
214304
test_done

xdiff-interface.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ int git_xmerge_config(const char *var, const char *value, void *cb)
313313
die("'%s' is not a boolean", var);
314314
if (!strcmp(value, "diff3"))
315315
git_xmerge_style = XDL_MERGE_DIFF3;
316+
else if (!strcmp(value, "zdiff3"))
317+
git_xmerge_style = XDL_MERGE_ZEALOUS_DIFF3;
316318
else if (!strcmp(value, "merge"))
317319
git_xmerge_style = 0;
318320
/*

xdiff/xdiff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ extern "C" {
6666

6767
/* merge output styles */
6868
#define XDL_MERGE_DIFF3 1
69+
#define XDL_MERGE_ZEALOUS_DIFF3 2
6970

7071
typedef struct s_mmfile {
7172
char *ptr;

xdiff/xmerge.c

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
230230
size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, 1,
231231
dest ? dest + size : NULL);
232232

233-
if (style == XDL_MERGE_DIFF3) {
233+
if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) {
234234
/* Shared preimage */
235235
if (!dest) {
236236
size += marker_size + 1 + needs_cr + marker3_size;
@@ -322,6 +322,40 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
322322
return size;
323323
}
324324

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+
325359
/*
326360
* Sometimes, changes are not quite identical, but differ in only a few
327361
* 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,
482516
int style = xmp->style;
483517
int favor = xmp->favor;
484518

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) {
486535
/*
487536
* "diff3 -m" output does not make sense for anything
488537
* more aggressive than XDL_MERGE_EAGER.
@@ -603,10 +652,12 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1,
603652
if (!changes)
604653
changes = c;
605654
/* 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)) {
610661
xdl_cleanup_merge(changes);
611662
return -1;
612663
}

0 commit comments

Comments
 (0)