Skip to content

Commit 8a16143

Browse files
jrngitster
authored andcommitted
xdl_merge(): add optional ancestor label to diff3-style output
The ‘git checkout --conflict=diff3’ command can be used to present conflicts hunks including text from the common ancestor: <<<<<<< ours ourside ||||||| original ======= theirside >>>>>>> theirs The added information is helpful for resolving merges by hand, and merge tools can usually grok it because it is very similar to the output from diff3 -m. A subtle change can help more tools to understand the output. ‘diff3’ includes the name of the merge base on the ||||||| line of the output, and some tools misparse the conflict hunks without it. Add a new xmp->ancestor parameter to xdl_merge() for use with conflict style XDL_MERGE_DIFF3 as a label on the ||||||| line for any conflict hunks. If xmp->ancestor is NULL, the output format is unchanged. Thus, this change only provides unexposed plumbing for the new feature; it does not affect the outward behavior of git. Requested-by: Stefan Monnier <monnier@iro.umontreal.ca> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Acked-by: Bert Wesarg <Bert.Wesarg@googlemail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6a84334 commit 8a16143

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

xdiff/xdiff.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ typedef struct s_xmparam {
117117
int level;
118118
int favor;
119119
int style;
120+
const char *ancestor; /* label for orig */
120121
} xmparam_t;
121122

122123
#define DEFAULT_CONFLICT_MARKER_SIZE 7

xdiff/xmerge.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
145145

146146
static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
147147
xdfenv_t *xe2, const char *name2,
148+
const char *name3,
148149
int size, int i, int style,
149150
xdmerge_t *m, char *dest, int marker_size)
150151
{
151152
int marker1_size = (name1 ? strlen(name1) + 1 : 0);
152153
int marker2_size = (name2 ? strlen(name2) + 1 : 0);
154+
int marker3_size = (name3 ? strlen(name3) + 1 : 0);
153155
int j;
154156

155157
if (marker_size <= 0)
@@ -179,10 +181,15 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
179181
if (style == XDL_MERGE_DIFF3) {
180182
/* Shared preimage */
181183
if (!dest) {
182-
size += marker_size + 1;
184+
size += marker_size + 1 + marker3_size;
183185
} else {
184186
for (j = 0; j < marker_size; j++)
185187
dest[size++] = '|';
188+
if (marker3_size) {
189+
dest[size] = ' ';
190+
memcpy(dest + size + 1, name3, marker3_size - 1);
191+
size += marker3_size;
192+
}
186193
dest[size++] = '\n';
187194
}
188195
size += xdl_orig_copy(xe1, m->i0, m->chg0, 1,
@@ -217,6 +224,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
217224

218225
static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
219226
xdfenv_t *xe2, const char *name2,
227+
const char *ancestor_name,
220228
int favor,
221229
xdmerge_t *m, char *dest, int style,
222230
int marker_size)
@@ -229,6 +237,7 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
229237

230238
if (m->mode == 0)
231239
size = fill_conflict_hunk(xe1, name1, xe2, name2,
240+
ancestor_name,
232241
size, i, style, m, dest,
233242
marker_size);
234243
else if (m->mode & 3) {
@@ -403,6 +412,7 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
403412
xmparam_t const *xmp, mmbuffer_t *result) {
404413
xdmerge_t *changes, *c;
405414
xpparam_t const *xpp = &xmp->xpp;
415+
const char *const ancestor_name = xmp->ancestor;
406416
int i0, i1, i2, chg0, chg1, chg2;
407417
int level = xmp->level;
408418
int style = xmp->style;
@@ -540,6 +550,7 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
540550
if (result) {
541551
int marker_size = xmp->marker_size;
542552
int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2,
553+
ancestor_name,
543554
favor, changes, NULL, style,
544555
marker_size);
545556
result->ptr = xdl_malloc(size);
@@ -548,7 +559,8 @@ static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, const char *name1,
548559
return -1;
549560
}
550561
result->size = size;
551-
xdl_fill_merge_buffer(xe1, name1, xe2, name2, favor, changes,
562+
xdl_fill_merge_buffer(xe1, name1, xe2, name2,
563+
ancestor_name, favor, changes,
552564
result->ptr, style, marker_size);
553565
}
554566
return xdl_cleanup_merge(changes);

0 commit comments

Comments
 (0)