Skip to content

Commit 15980de

Browse files
dschogitster
authored andcommitted
merge-file: ensure that conflict sections match eol style
In the previous patch, we made sure that the conflict markers themselves match the end-of-line style of the input files. However, this still left out the conflicting text itself: if it lacks a trailing newline, we add one, and should add a carriage return when appropriate, too. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 86efa21 commit 15980de

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

t/t6023-merge-file.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,14 @@ test_expect_success 'conflict at EOF without LF resolved by --union' \
346346
printf "line1\nline2\nline3x\nline3y" >expect.txt &&
347347
test_cmp expect.txt output.txt'
348348

349-
test_expect_success 'conflict markers match existing line endings' '
349+
test_expect_success 'conflict sections match existing line endings' '
350350
printf "1\\r\\n2\\r\\n3" >crlf-orig.txt &&
351351
printf "1\\r\\n2\\r\\n4" >crlf-diff1.txt &&
352352
printf "1\\r\\n2\\r\\n5" >crlf-diff2.txt &&
353353
test_must_fail git -c core.eol=crlf merge-file -p \
354354
crlf-diff1.txt crlf-orig.txt crlf-diff2.txt >crlf.txt &&
355355
test $(tr "\015" Q <crlf.txt | grep "^[<=>].*Q$" | wc -l) = 3 &&
356+
test $(tr "\015" Q <crlf.txt | grep "[345]Q$" | wc -l) = 3 &&
356357
test_must_fail git -c core.eol=crlf merge-file -p \
357358
nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >nolf.txt &&
358359
test $(tr "\015" Q <nolf.txt | grep "^[<=>].*Q$" | wc -l) = 0

xdiff/xmerge.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2,
109109
return 0;
110110
}
111111

112-
static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int add_nl, char *dest)
112+
static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
113113
{
114114
xrecord_t **recs;
115115
int size = 0;
@@ -125,6 +125,12 @@ static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int add
125125
if (add_nl) {
126126
i = recs[count - 1]->size;
127127
if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') {
128+
if (needs_cr) {
129+
if (dest)
130+
dest[size] = '\r';
131+
size++;
132+
}
133+
128134
if (dest)
129135
dest[size] = '\n';
130136
size++;
@@ -133,14 +139,14 @@ static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int add
133139
return size;
134140
}
135141

136-
static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
142+
static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
137143
{
138-
return xdl_recs_copy_0(0, xe, i, count, add_nl, dest);
144+
return xdl_recs_copy_0(0, xe, i, count, needs_cr, add_nl, dest);
139145
}
140146

141-
static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int add_nl, char *dest)
147+
static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest)
142148
{
143-
return xdl_recs_copy_0(1, xe, i, count, add_nl, dest);
149+
return xdl_recs_copy_0(1, xe, i, count, needs_cr, add_nl, dest);
144150
}
145151

146152
/*
@@ -202,7 +208,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
202208
marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
203209

204210
/* Before conflicting part */
205-
size += xdl_recs_copy(xe1, i, m->i1 - i, 0,
211+
size += xdl_recs_copy(xe1, i, m->i1 - i, 0, 0,
206212
dest ? dest + size : NULL);
207213

208214
if (!dest) {
@@ -221,7 +227,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
221227
}
222228

223229
/* Postimage from side #1 */
224-
size += xdl_recs_copy(xe1, m->i1, m->chg1, 1,
230+
size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, 1,
225231
dest ? dest + size : NULL);
226232

227233
if (style == XDL_MERGE_DIFF3) {
@@ -240,7 +246,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
240246
dest[size++] = '\r';
241247
dest[size++] = '\n';
242248
}
243-
size += xdl_orig_copy(xe1, m->i0, m->chg0, 1,
249+
size += xdl_orig_copy(xe1, m->i0, m->chg0, needs_cr, 1,
244250
dest ? dest + size : NULL);
245251
}
246252

@@ -255,7 +261,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
255261
}
256262

257263
/* Postimage from side #2 */
258-
size += xdl_recs_copy(xe2, m->i2, m->chg2, 1,
264+
size += xdl_recs_copy(xe2, m->i2, m->chg2, needs_cr, 1,
259265
dest ? dest + size : NULL);
260266
if (!dest) {
261267
size += marker_size + 1 + needs_cr + marker2_size;
@@ -294,21 +300,24 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
294300
marker_size);
295301
else if (m->mode & 3) {
296302
/* Before conflicting part */
297-
size += xdl_recs_copy(xe1, i, m->i1 - i, 0,
303+
size += xdl_recs_copy(xe1, i, m->i1 - i, 0, 0,
298304
dest ? dest + size : NULL);
299305
/* Postimage from side #1 */
300-
if (m->mode & 1)
301-
size += xdl_recs_copy(xe1, m->i1, m->chg1, (m->mode & 2),
306+
if (m->mode & 1) {
307+
int needs_cr = is_cr_needed(xe1, xe2, m);
308+
309+
size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, (m->mode & 2),
302310
dest ? dest + size : NULL);
311+
}
303312
/* Postimage from side #2 */
304313
if (m->mode & 2)
305-
size += xdl_recs_copy(xe2, m->i2, m->chg2, 0,
314+
size += xdl_recs_copy(xe2, m->i2, m->chg2, 0, 0,
306315
dest ? dest + size : NULL);
307316
} else
308317
continue;
309318
i = m->i1 + m->chg1;
310319
}
311-
size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0,
320+
size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, 0,
312321
dest ? dest + size : NULL);
313322
return size;
314323
}

0 commit comments

Comments
 (0)