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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 27, 2016 at 17:37 UTC 15980deab9f41324aba5cc9536e463d204bafe63
2 files changed +25 -15
t/t6023-merge-file.sh
+2 -1
@@ -346,13 +346,14 @@ test_expect_success 'conflict at EOF without LF resolved by --union' \
346 printf "line1\nline2\nline3x\nline3y" >expect.txt &&
347 test_cmp expect.txt output.txt'
348
349 -test_expect_success 'conflict markers match existing line endings' '
349 +test_expect_success 'conflict sections match existing line endings' '
350 printf "1\\r\\n2\\r\\n3" >crlf-orig.txt &&
351 printf "1\\r\\n2\\r\\n4" >crlf-diff1.txt &&
352 printf "1\\r\\n2\\r\\n5" >crlf-diff2.txt &&
353 test_must_fail git -c core.eol=crlf merge-file -p \
354 crlf-diff1.txt crlf-orig.txt crlf-diff2.txt >crlf.txt &&
355 test $(tr "\015" Q <crlf.txt | grep "^[<=>].*Q$" | wc -l) = 3 &&
356 + test $(tr "\015" Q <crlf.txt | grep "[345]Q$" | wc -l) = 3 &&
357 test_must_fail git -c core.eol=crlf merge-file -p \
358 nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >nolf.txt &&
359 test $(tr "\015" Q <nolf.txt | grep "^[<=>].*Q$" | wc -l) = 0
xdiff/xmerge.c
+23 -14
@@ -109,7 +109,7 @@ static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2,
109 return 0;
110 }
111
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)
113 {
114 xrecord_t **recs;
115 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
125 if (add_nl) {
126 i = recs[count - 1]->size;
127 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 +
134 if (dest)
135 dest[size] = '\n';
136 size++;
@@ -133,14 +139,14 @@ static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int add
139 return size;
140 }
141
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)
143 {
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);
145 }
146
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)
148 {
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);
150 }
151
152 /*
@@ -202,7 +208,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
208 marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
209
210 /* 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,
212 dest ? dest + size : NULL);
213
214 if (!dest) {
@@ -221,7 +227,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
227 }
228
229 /* 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,
231 dest ? dest + size : NULL);
232
233 if (style == XDL_MERGE_DIFF3) {
@@ -240,7 +246,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
246 dest[size++] = '\r';
247 dest[size++] = '\n';
248 }
243 - size += xdl_orig_copy(xe1, m->i0, m->chg0, 1,
249 + size += xdl_orig_copy(xe1, m->i0, m->chg0, needs_cr, 1,
250 dest ? dest + size : NULL);
251 }
252
@@ -255,7 +261,7 @@ static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1,
261 }
262
263 /* 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,
265 dest ? dest + size : NULL);
266 if (!dest) {
267 size += marker_size + 1 + needs_cr + marker2_size;
@@ -294,21 +300,24 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
300 marker_size);
301 else if (m->mode & 3) {
302 /* 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,
304 dest ? dest + size : NULL);
305 /* 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),
310 dest ? dest + size : NULL);
311 + }
312 /* Postimage from side #2 */
313 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,
315 dest ? dest + size : NULL);
316 } else
317 continue;
318 i = m->i1 + m->chg1;
319 }
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,
321 dest ? dest + size : NULL);
322 return size;
323 }