]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4038-diff-combined.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / t4038-diff-combined.sh
CommitLineData
b810cbbd
JH
1#!/bin/sh
2
3test_description='combined diff'
4
8f37854b 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
b810cbbd 8. ./test-lib.sh
ebd73f50 9. "$TEST_DIRECTORY"/lib-diff.sh
b810cbbd
JH
10
11setup_helper () {
12 one=$1 branch=$2 side=$3 &&
13
14 git branch $side $branch &&
15 for l in $one two three fyra
16 do
17 echo $l
18 done >file &&
19 git add file &&
20 test_tick &&
21 git commit -m $branch &&
22 git checkout $side &&
23 for l in $one two three quatro
24 do
25 echo $l
26 done >file &&
27 git add file &&
28 test_tick &&
29 git commit -m $side &&
30 test_must_fail git merge $branch &&
31 for l in $one three four
32 do
33 echo $l
34 done >file &&
35 git add file &&
36 test_tick &&
37 git commit -m "merge $branch into $side"
38}
39
40verify_helper () {
41 it=$1 &&
42
43 # Ignore lines that were removed only from the other parent
44 sed -e '
45 1,/^@@@/d
46 /^ -/d
47 s/^\(.\)./\1/
48 ' "$it" >"$it.actual.1" &&
49 sed -e '
50 1,/^@@@/d
51 /^- /d
52 s/^.\(.\)/\1/
53 ' "$it" >"$it.actual.2" &&
54
55 git diff "$it^" "$it" -- | sed -e '1,/^@@/d' >"$it.expect.1" &&
56 test_cmp "$it.expect.1" "$it.actual.1" &&
57
58 git diff "$it^2" "$it" -- | sed -e '1,/^@@/d' >"$it.expect.2" &&
59 test_cmp "$it.expect.2" "$it.actual.2"
60}
61
62test_expect_success setup '
63 >file &&
64 git add file &&
65 test_tick &&
66 git commit -m initial &&
67
68 git branch withone &&
69 git branch sansone &&
70
71 git checkout withone &&
72 setup_helper one withone sidewithone &&
73
74 git checkout sansone &&
75 setup_helper "" sansone sidesansone
76'
77
78test_expect_success 'check combined output (1)' '
79 git show sidewithone -- >sidewithone &&
80 verify_helper sidewithone
81'
82
a79c6b60
RS
83test_expect_success 'check combined output (1) with git diff <rev>^!' '
84 git diff sidewithone^! -- >sidewithone &&
85 verify_helper sidewithone
86'
87
df3dac37 88test_expect_success 'check combined output (2)' '
b810cbbd
JH
89 git show sidesansone -- >sidesansone &&
90 verify_helper sidesansone
91'
92
a79c6b60
RS
93test_expect_success 'check combined output (2) with git diff <rev>^!' '
94 git diff sidesansone^! -- >sidesansone &&
95 verify_helper sidesansone
96'
97
21798708
TR
98test_expect_success 'diagnose truncated file' '
99 >file &&
100 git add file &&
101 git commit --amend -C HEAD &&
102 git show >out &&
103 grep "diff --cc file" out
104'
105
edbc00e7
JK
106test_expect_success 'setup for --cc --raw' '
107 blob=$(echo file | git hash-object --stdin -w) &&
108 base_tree=$(echo "100644 blob $blob file" | git mktree) &&
109 trees= &&
7c0c51ba 110 for i in $(test_seq 1 40)
edbc00e7
JK
111 do
112 blob=$(echo file$i | git hash-object --stdin -w) &&
cbe1d9d6 113 trees="$trees$(echo "100644 blob $blob file" | git mktree)$LF" || return 1
edbc00e7
JK
114 done
115'
116
117test_expect_success 'check --cc --raw with four trees' '
118 four_trees=$(echo "$trees" | sed -e 4q) &&
119 git diff --cc --raw $four_trees $base_tree >out &&
120 # Check for four leading colons in the output:
121 grep "^::::[^:]" out
122'
123
124test_expect_success 'check --cc --raw with forty trees' '
125 git diff --cc --raw $trees $base_tree >out &&
126 # Check for forty leading colons in the output:
127 grep "^::::::::::::::::::::::::::::::::::::::::[^:]" out
128'
129
fa04ae0b 130test_expect_success 'setup combined ignore spaces' '
8f37854b 131 git checkout main &&
fa04ae0b
AP
132 >test &&
133 git add test &&
134 git commit -m initial &&
135
136 tr -d Q <<-\EOF >test &&
137 always coalesce
138 eol space coalesce Q
139 space change coalesce
140 all spa ces coalesce
141 eol spaces Q
142 space change
143 all spa ces
144 EOF
145 git commit -m "test space change" -a &&
146
147 git checkout -b side HEAD^ &&
148 tr -d Q <<-\EOF >test &&
149 always coalesce
150 eol space coalesce
151 space change coalesce
152 all spaces coalesce
153 eol spaces
154 space change
155 all spaces
156 EOF
157 git commit -m "test other space changes" -a &&
158
8f37854b 159 test_must_fail git merge main &&
fa04ae0b
AP
160 tr -d Q <<-\EOF >test &&
161 eol spaces Q
162 space change
163 all spa ces
164 EOF
165 git commit -m merged -a
166'
167
168test_expect_success 'check combined output (no ignore space)' '
169 git show >actual.tmp &&
170 sed -e "1,/^@@@/d" < actual.tmp >actual &&
171 tr -d Q <<-\EOF >expected &&
172 --always coalesce
173 - eol space coalesce
174 - space change coalesce
175 - all spaces coalesce
176 - eol spaces
177 - space change
178 - all spaces
179 -eol space coalesce Q
180 -space change coalesce
181 -all spa ces coalesce
182 + eol spaces Q
183 + space change
184 + all spa ces
185 EOF
186 compare_diff_patch expected actual
187'
188
189test_expect_success 'check combined output (ignore space at eol)' '
190 git show --ignore-space-at-eol >actual.tmp &&
191 sed -e "1,/^@@@/d" < actual.tmp >actual &&
192 tr -d Q <<-\EOF >expected &&
193 --always coalesce
194 --eol space coalesce
195 - space change coalesce
196 - all spaces coalesce
197 -space change coalesce
198 -all spa ces coalesce
199 eol spaces Q
200 - space change
201 - all spaces
202 + space change
203 + all spa ces
204 EOF
205 compare_diff_patch expected actual
206'
207
208test_expect_success 'check combined output (ignore space change)' '
209 git show -b >actual.tmp &&
210 sed -e "1,/^@@@/d" < actual.tmp >actual &&
211 tr -d Q <<-\EOF >expected &&
212 --always coalesce
213 --eol space coalesce
214 --space change coalesce
215 - all spaces coalesce
216 -all spa ces coalesce
217 eol spaces Q
218 space change
219 - all spaces
220 + all spa ces
221 EOF
222 compare_diff_patch expected actual
223'
224
225test_expect_success 'check combined output (ignore all spaces)' '
226 git show -w >actual.tmp &&
227 sed -e "1,/^@@@/d" < actual.tmp >actual &&
228 tr -d Q <<-\EOF >expected &&
229 --always coalesce
230 --eol space coalesce
231 --space change coalesce
232 --all spaces coalesce
233 eol spaces Q
234 space change
235 all spa ces
236 EOF
237 compare_diff_patch expected actual
238'
239
99d32060
AP
240test_expect_success 'combine diff coalesce simple' '
241 >test &&
242 git add test &&
243 git commit -m initial &&
244 test_seq 4 >test &&
245 git commit -a -m empty1 &&
246 git branch side1 &&
247 git checkout HEAD^ &&
248 test_seq 5 >test &&
249 git commit -a -m empty2 &&
250 test_must_fail git merge side1 &&
251 >test &&
252 git commit -a -m merge &&
253 git show >actual.tmp &&
254 sed -e "1,/^@@@/d" < actual.tmp >actual &&
255 tr -d Q <<-\EOF >expected &&
256 --1
257 --2
258 --3
259 --4
260 - 5
261 EOF
262 compare_diff_patch expected actual
263'
264
265test_expect_success 'combine diff coalesce tricky' '
266 >test &&
267 git add test &&
268 git commit -m initial --allow-empty &&
269 cat <<-\EOF >test &&
270 3
271 1
272 2
273 3
274 4
275 EOF
276 git commit -a -m empty1 &&
277 git branch -f side1 &&
278 git checkout HEAD^ &&
279 cat <<-\EOF >test &&
280 1
281 3
282 5
283 4
284 EOF
285 git commit -a -m empty2 &&
286 git branch -f side2 &&
287 test_must_fail git merge side1 &&
288 >test &&
289 git commit -a -m merge &&
290 git show >actual.tmp &&
291 sed -e "1,/^@@@/d" < actual.tmp >actual &&
292 tr -d Q <<-\EOF >expected &&
293 -3
294 --1
295 -2
296 --3
297 - 5
298 --4
299 EOF
300 compare_diff_patch expected actual &&
301 git checkout -f side1 &&
302 test_must_fail git merge side2 &&
303 >test &&
304 git commit -a -m merge &&
305 git show >actual.tmp &&
306 sed -e "1,/^@@@/d" < actual.tmp >actual &&
307 tr -d Q <<-\EOF >expected &&
308 - 3
309 --1
310 - 2
311 --3
312 -5
313 --4
314 EOF
315 compare_diff_patch expected actual
316'
317
318test_expect_failure 'combine diff coalesce three parents' '
319 >test &&
320 git add test &&
321 git commit -m initial --allow-empty &&
322 cat <<-\EOF >test &&
323 3
324 1
325 2
326 3
327 4
328 EOF
329 git commit -a -m empty1 &&
330 git checkout -B side1 &&
331 git checkout HEAD^ &&
332 cat <<-\EOF >test &&
333 1
334 3
335 7
336 5
337 4
338 EOF
339 git commit -a -m empty2 &&
340 git branch -f side2 &&
341 git checkout HEAD^ &&
342 cat <<-\EOF >test &&
343 3
344 1
345 6
346 5
347 4
348 EOF
349 git commit -a -m empty3 &&
350 >test &&
351 git add test &&
352 TREE=$(git write-tree) &&
353 COMMIT=$(git commit-tree -p HEAD -p side1 -p side2 -m merge $TREE) &&
354 git show $COMMIT >actual.tmp &&
355 sed -e "1,/^@@@/d" < actual.tmp >actual &&
356 tr -d Q <<-\EOF >expected &&
357 -- 3
358 ---1
359 - 6
360 - 2
361 --3
362 -7
363 - -5
364 ---4
365 EOF
366 compare_diff_patch expected actual
367'
368
aac38571 369# Test for a bug reported at
3eae30e4 370# https://lore.kernel.org/git/20130515143508.GO25742@login.drsnuggles.stderr.nl/
aac38571
MK
371# where a delete lines were missing from combined diff output when they
372# occurred exactly before the context lines of a later change.
373test_expect_success 'combine diff missing delete bug' '
374 git commit -m initial --allow-empty &&
375 cat <<-\EOF >test &&
376 1
377 2
378 3
379 4
380 EOF
381 git add test &&
382 git commit -a -m side1 &&
383 git checkout -B side1 &&
384 git checkout HEAD^ &&
385 cat <<-\EOF >test &&
386 0
387 1
388 2
389 3
390 4modified
391 EOF
392 git add test &&
393 git commit -m side2 &&
394 git branch -f side2 &&
395 test_must_fail git merge --no-commit side1 &&
396 cat <<-\EOF >test &&
397 1
398 2
399 3
400 4modified
401 EOF
402 git add test &&
403 git commit -a -m merge &&
404 git diff-tree -c -p HEAD >actual.tmp &&
405 sed -e "1,/^@@@/d" < actual.tmp >actual &&
406 tr -d Q <<-\EOF >expected &&
407 - 0
408 1
409 2
410 3
411 -4
412 +4modified
413 EOF
edc7f0ab 414 compare_diff_patch expected actual
aac38571
MK
415'
416
e09867f0
JK
417test_expect_success 'combine diff gets tree sorting right' '
418 # create a directory and a file that sort differently in trees
419 # versus byte-wise (implied "/" sorts after ".")
8f37854b 420 git checkout -f main &&
e09867f0
JK
421 mkdir foo &&
422 echo base >foo/one &&
423 echo base >foo/two &&
424 echo base >foo.ext &&
425 git add foo foo.ext &&
426 git commit -m base &&
427
428 # one side modifies a file in the directory, along with the root
429 # file...
8f37854b
JS
430 echo main >foo/one &&
431 echo main >foo.ext &&
432 git commit -a -m main &&
e09867f0
JK
433
434 # the other side modifies the other file in the directory
435 git checkout -b other HEAD^ &&
436 echo other >foo/two &&
437 git commit -a -m other &&
438
439 # And now we merge. The files in the subdirectory will resolve cleanly,
440 # meaning that a combined diff will not find them interesting. But it
441 # will find the tree itself interesting, because it had to be merged.
8f37854b 442 git checkout main &&
e09867f0
JK
443 git merge other &&
444
445 printf "MM\tfoo\n" >expect &&
446 git diff-tree -c --name-status -t HEAD >actual.tmp &&
447 sed 1d <actual.tmp >actual &&
448 test_cmp expect actual
449'
450
d76ce4f7
EN
451test_expect_success 'setup for --combined-all-paths' '
452 git branch side1c &&
453 git branch side2c &&
454 git checkout side1c &&
455 test_seq 1 10 >filename-side1c &&
0370b354 456 side1cf=$(git hash-object filename-side1c) &&
d76ce4f7
EN
457 git add filename-side1c &&
458 git commit -m with &&
459 git checkout side2c &&
460 test_seq 1 9 >filename-side2c &&
461 echo ten >>filename-side2c &&
0370b354 462 side2cf=$(git hash-object filename-side2c) &&
d76ce4f7
EN
463 git add filename-side2c &&
464 git commit -m iam &&
465 git checkout -b mergery side1c &&
466 git merge --no-commit side2c &&
467 git rm filename-side1c &&
468 echo eleven >>filename-side2c &&
469 git mv filename-side2c filename-merged &&
0370b354 470 mergedf=$(git hash-object filename-merged) &&
d76ce4f7
EN
471 git add filename-merged &&
472 git commit
473'
474
475test_expect_success '--combined-all-paths and --raw' '
0370b354 476 cat <<-EOF >expect &&
477 ::100644 100644 100644 $side1cf $side2cf $mergedf RR filename-side1c filename-side2c filename-merged
d76ce4f7
EN
478 EOF
479 git diff-tree -c -M --raw --combined-all-paths HEAD >actual.tmp &&
480 sed 1d <actual.tmp >actual &&
481 test_cmp expect actual
482'
483
484test_expect_success '--combined-all-paths and --cc' '
485 cat <<-\EOF >expect &&
486 --- a/filename-side1c
487 --- a/filename-side2c
488 +++ b/filename-merged
489 EOF
490 git diff-tree --cc -M --combined-all-paths HEAD >actual.tmp &&
491 grep ^[-+][-+][-+] <actual.tmp >actual &&
492 test_cmp expect actual
493'
494
495test_expect_success FUNNYNAMES 'setup for --combined-all-paths with funny names' '
496 git branch side1d &&
497 git branch side2d &&
498 git checkout side1d &&
7951a016 499 test_seq 1 10 >"$(printf "file\twith\ttabs")" &&
d76ce4f7 500 git add file* &&
0370b354 501 side1df=$(git hash-object *tabs) &&
d76ce4f7
EN
502 git commit -m with &&
503 git checkout side2d &&
7951a016
TZ
504 test_seq 1 9 >"$(printf "i\tam\ttabbed")" &&
505 echo ten >>"$(printf "i\tam\ttabbed")" &&
d76ce4f7 506 git add *tabbed &&
0370b354 507 side2df=$(git hash-object *tabbed) &&
d76ce4f7
EN
508 git commit -m iam &&
509 git checkout -b funny-names-mergery side1d &&
510 git merge --no-commit side2d &&
511 git rm *tabs &&
7951a016 512 echo eleven >>"$(printf "i\tam\ttabbed")" &&
d76ce4f7
EN
513 git mv "$(printf "i\tam\ttabbed")" "$(printf "fickle\tnaming")" &&
514 git add fickle* &&
0370b354 515 headf=$(git hash-object fickle*) &&
516 git commit &&
517 head=$(git rev-parse HEAD)
d76ce4f7
EN
518'
519
520test_expect_success FUNNYNAMES '--combined-all-paths and --raw and funny names' '
0370b354 521 cat <<-EOF >expect &&
522 ::100644 100644 100644 $side1df $side2df $headf RR "file\twith\ttabs" "i\tam\ttabbed" "fickle\tnaming"
d76ce4f7
EN
523 EOF
524 git diff-tree -c -M --raw --combined-all-paths HEAD >actual.tmp &&
525 sed 1d <actual.tmp >actual &&
526 test_cmp expect actual
527'
528
529test_expect_success FUNNYNAMES '--combined-all-paths and --raw -and -z and funny names' '
0370b354 530 printf "$head\0::100644 100644 100644 $side1df $side2df $headf RR\0file\twith\ttabs\0i\tam\ttabbed\0fickle\tnaming\0" >expect &&
d76ce4f7 531 git diff-tree -c -M --raw --combined-all-paths -z HEAD >actual &&
75b2c154 532 test_cmp expect actual
d76ce4f7
EN
533'
534
535test_expect_success FUNNYNAMES '--combined-all-paths and --cc and funny names' '
536 cat <<-\EOF >expect &&
537 --- "a/file\twith\ttabs"
538 --- "a/i\tam\ttabbed"
539 +++ "b/fickle\tnaming"
540 EOF
541 git diff-tree --cc -M --combined-all-paths HEAD >actual.tmp &&
542 grep ^[-+][-+][-+] <actual.tmp >actual &&
543 test_cmp expect actual
544'
545
b810cbbd 546test_done