git-merge-file: do not add LF at EOF while applying unrelated change

If 'current-file' does not contain LF at EOF, and change between 'base-file' and 'other-file' does not change any line close to EOF, the 3-way merge should not add LF to EOF. This is what 'diff3 -m' does, and seems to be a reasonable expectation. The change which introduced the behavior is cd1d61c44f. It always calls function xdl_recs_copy() for sides with add_nl == 1. In fact, it looks like the only case when this is needed is when 2 files are being union-merged, and they do not have LF at EOF (strictly speaking, the first of them). Add tests: * "merge without conflict (missing LF at EOF, away from change in the other file)" and "merge does not add LF away of change", to demonstrate the changed behavior. * "conflict at EOF without LF resolved by --union", to verify that the union-merge at the end inerts newline between versions. * some more tests which I felt like not covering the functionality well Signed-off-by: Max Kirillov <max@max630.net> Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Max Kirillov committed Jun 29, 2014 at 01:04 UTC ba311807f8408761a48d4cd596259ca30a4375f2
2 files changed +87 -2
t/t6023-merge-file.sh
+85
@@ -83,6 +83,23 @@ test_expect_failure "merge without conflict (missing LF at EOF)" \
83 test_expect_failure "merge result added missing LF" \
84 "test_cmp test.txt test2.txt"
85
86 +cp new4.txt test3.txt
87 +test_expect_success "merge without conflict (missing LF at EOF, away from change in the other file)" \
88 + "git merge-file --quiet test3.txt new2.txt new3.txt"
89 +
90 +cat > expect.txt << EOF
91 +DOMINUS regit me,
92 +et nihil mihi deerit.
93 +In loco pascuae ibi me collocavit,
94 +super aquam refectionis educavit me;
95 +animam meam convertit,
96 +deduxit me super semitas jusitiae,
97 +EOF
98 +printf "propter nomen suum." >> expect.txt
99 +
100 +test_expect_success "merge does not add LF away of change" \
101 + "test_cmp test3.txt expect.txt"
102 +
103 cp test.txt backup.txt
104 test_expect_success "merge with conflicts" \
105 "test_must_fail git merge-file test.txt orig.txt new3.txt"
@@ -107,6 +124,55 @@ EOF
124 test_expect_success "expected conflict markers" "test_cmp test.txt expect.txt"
125
126 cp backup.txt test.txt
127 +
128 +cat > expect.txt << EOF
129 +Dominus regit me, et nihil mihi deerit.
130 +In loco pascuae ibi me collocavit,
131 +super aquam refectionis educavit me;
132 +animam meam convertit,
133 +deduxit me super semitas jusitiae,
134 +propter nomen suum.
135 +Nam et si ambulavero in medio umbrae mortis,
136 +non timebo mala, quoniam tu mecum es:
137 +virga tua et baculus tuus ipsa me consolata sunt.
138 +EOF
139 +test_expect_success "merge conflicting with --ours" \
140 + "git merge-file --ours test.txt orig.txt new3.txt && test_cmp test.txt expect.txt"
141 +cp backup.txt test.txt
142 +
143 +cat > expect.txt << EOF
144 +DOMINUS regit me,
145 +et nihil mihi deerit.
146 +In loco pascuae ibi me collocavit,
147 +super aquam refectionis educavit me;
148 +animam meam convertit,
149 +deduxit me super semitas jusitiae,
150 +propter nomen suum.
151 +Nam et si ambulavero in medio umbrae mortis,
152 +non timebo mala, quoniam tu mecum es:
153 +virga tua et baculus tuus ipsa me consolata sunt.
154 +EOF
155 +test_expect_success "merge conflicting with --theirs" \
156 + "git merge-file --theirs test.txt orig.txt new3.txt && test_cmp test.txt expect.txt"
157 +cp backup.txt test.txt
158 +
159 +cat > expect.txt << EOF
160 +Dominus regit me, et nihil mihi deerit.
161 +DOMINUS regit me,
162 +et nihil mihi deerit.
163 +In loco pascuae ibi me collocavit,
164 +super aquam refectionis educavit me;
165 +animam meam convertit,
166 +deduxit me super semitas jusitiae,
167 +propter nomen suum.
168 +Nam et si ambulavero in medio umbrae mortis,
169 +non timebo mala, quoniam tu mecum es:
170 +virga tua et baculus tuus ipsa me consolata sunt.
171 +EOF
172 +test_expect_success "merge conflicting with --union" \
173 + "git merge-file --union test.txt orig.txt new3.txt && test_cmp test.txt expect.txt"
174 +cp backup.txt test.txt
175 +
176 test_expect_success "merge with conflicts, using -L" \
177 "test_must_fail git merge-file -L 1 -L 2 test.txt orig.txt new3.txt"
178
@@ -260,4 +326,23 @@ test_expect_success 'marker size' '
326 test_cmp expect actual
327 '
328
329 +printf "line1\nline2\nline3" >nolf-orig.txt
330 +printf "line1\nline2\nline3x" >nolf-diff1.txt
331 +printf "line1\nline2\nline3y" >nolf-diff2.txt
332 +
333 +test_expect_success 'conflict at EOF without LF resolved by --ours' \
334 + 'git merge-file -p --ours nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >output.txt &&
335 + printf "line1\nline2\nline3x" >expect.txt &&
336 + test_cmp expect.txt output.txt'
337 +
338 +test_expect_success 'conflict at EOF without LF resolved by --theirs' \
339 + 'git merge-file -p --theirs nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >output.txt &&
340 + printf "line1\nline2\nline3y" >expect.txt &&
341 + test_cmp expect.txt output.txt'
342 +
343 +test_expect_success 'conflict at EOF without LF resolved by --union' \
344 + 'git merge-file -p --union nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >output.txt &&
345 + printf "line1\nline2\nline3x\nline3y" >expect.txt &&
346 + test_cmp expect.txt output.txt'
347 +
348 test_done
xdiff/xmerge.c
+2 -2
@@ -245,11 +245,11 @@ static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1,
245 dest ? dest + size : NULL);
246 /* Postimage from side #1 */
247 if (m->mode & 1)
248 - size += xdl_recs_copy(xe1, m->i1, m->chg1, 1,
248 + size += xdl_recs_copy(xe1, m->i1, m->chg1, (m->mode & 2),
249 dest ? dest + size : NULL);
250 /* Postimage from side #2 */
251 if (m->mode & 2)
252 - size += xdl_recs_copy(xe2, m->i2, m->chg2, 1,
252 + size += xdl_recs_copy(xe2, m->i2, m->chg2, 0,
253 dest ? dest + size : NULL);
254 } else
255 continue;