contrib/coverage-diff: avoid redundant pipelines
[git/gitster.git] / t / t4013-diff-various.sh
blob1e3b2dbea48488ecb3a68a8dba82d57b6fbca2a7
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='Various diff formatting options'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 . ./test-lib.sh
12 . "$TEST_DIRECTORY"/lib-diff.sh
14 test_expect_success setup '
16 GIT_AUTHOR_DATE="2006-06-26 00:00:00 +0000" &&
17 GIT_COMMITTER_DATE="2006-06-26 00:00:00 +0000" &&
18 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
20 mkdir dir &&
21 mkdir dir2 &&
22 test_write_lines 1 2 3 >file0 &&
23 test_write_lines A B >dir/sub &&
24 cat file0 >file2 &&
25 git add file0 file2 dir/sub &&
26 git commit -m Initial &&
28 git branch initial &&
29 git branch side &&
31 GIT_AUTHOR_DATE="2006-06-26 00:01:00 +0000" &&
32 GIT_COMMITTER_DATE="2006-06-26 00:01:00 +0000" &&
33 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
35 test_write_lines 4 5 6 >>file0 &&
36 test_write_lines C D >>dir/sub &&
37 rm -f file2 &&
38 git update-index --remove file0 file2 dir/sub &&
39 git commit -m "Second${LF}${LF}This is the second commit." &&
41 GIT_AUTHOR_DATE="2006-06-26 00:02:00 +0000" &&
42 GIT_COMMITTER_DATE="2006-06-26 00:02:00 +0000" &&
43 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
45 test_write_lines A B C >file1 &&
46 git add file1 &&
47 test_write_lines E F >>dir/sub &&
48 git update-index dir/sub &&
49 git commit -m Third &&
51 GIT_AUTHOR_DATE="2006-06-26 00:03:00 +0000" &&
52 GIT_COMMITTER_DATE="2006-06-26 00:03:00 +0000" &&
53 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
55 git checkout side &&
56 test_write_lines A B C >>file0 &&
57 test_write_lines 1 2 >>dir/sub &&
58 cat dir/sub >file3 &&
59 git add file3 &&
60 git update-index file0 dir/sub &&
61 git commit -m Side &&
63 GIT_AUTHOR_DATE="2006-06-26 00:04:00 +0000" &&
64 GIT_COMMITTER_DATE="2006-06-26 00:04:00 +0000" &&
65 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
67 git checkout master &&
68 git pull -s ours --no-rebase . side &&
70 GIT_AUTHOR_DATE="2006-06-26 00:05:00 +0000" &&
71 GIT_COMMITTER_DATE="2006-06-26 00:05:00 +0000" &&
72 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
74 test_write_lines A B C >>file0 &&
75 test_write_lines 1 2 >>dir/sub &&
76 git update-index file0 dir/sub &&
78 mkdir dir3 &&
79 cp dir/sub dir3/sub &&
80 test-tool chmtime +1 dir3/sub &&
82 git config log.showroot false &&
83 git commit --amend &&
85 GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
86 GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
87 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
88 git checkout -b rearrange initial &&
89 test_write_lines B A >dir/sub &&
90 git add dir/sub &&
91 git commit -m "Rearranged lines in dir/sub" &&
92 git checkout master &&
94 GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
95 GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
96 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
97 git checkout -b mode initial &&
98 git update-index --chmod=+x file0 &&
99 git commit -m "update mode" &&
100 git checkout -f master &&
102 GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
103 GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
104 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
105 git checkout -b note initial &&
106 git update-index --chmod=+x file2 &&
107 git commit -m "update mode (file2)" &&
108 git notes add -m "note" &&
109 git checkout -f master &&
111 # Same merge as master, but with parents reversed. Hide it in a
112 # pseudo-ref to avoid impacting tests with --all.
113 commit=$(echo reverse |
114 git commit-tree -p master^2 -p master^1 master^{tree}) &&
115 git update-ref REVERSE $commit &&
117 git config diff.renames false &&
119 git show-branch
122 : <<\EOF
123 ! [initial] Initial
124 * [master] Merge branch 'side'
125 ! [rearrange] Rearranged lines in dir/sub
126 ! [side] Side
127 ----
128 + [rearrange] Rearranged lines in dir/sub
129 - [master] Merge branch 'side'
130 * + [side] Side
131 * [master^] Third
132 * [master~2] Second
133 +*++ [initial] Initial
136 process_diffs () {
137 perl -e '
138 my $oid_length = length($ARGV[0]);
139 my $x40 = "[0-9a-f]{40}";
140 my $xab = "[0-9a-f]{4,16}";
141 my $orx = "[0-9a-f]" x $oid_length;
143 sub munge_oid {
144 my ($oid) = @_;
145 my $x;
147 return "" unless length $oid;
149 if ($oid =~ /^(100644|100755|120000)$/) {
150 return $oid;
153 if ($oid =~ /^0*$/) {
154 $x = "0";
155 } else {
156 $x = "f";
159 if (length($oid) == 40) {
160 return $x x $oid_length;
161 } else {
162 return $x x length($oid);
166 while (<STDIN>) {
167 s/($orx)/munge_oid($1)/ge;
168 s/From ($x40)( |\))/"From " . munge_oid($1) . $2/ge;
169 s/commit ($x40)($| \(from )($x40?)/"commit " . munge_oid($1) . $2 . munge_oid($3)/ge;
170 s/\b($x40)( |\.\.|$)/munge_oid($1) . $2/ge;
171 s/^($x40)($| )/munge_oid($1) . $2/e;
172 s/($xab)(\.\.|,| |\.\.\.|$)/munge_oid($1) . $2/ge;
173 print;
175 ' "$ZERO_OID" <"$1"
178 V=$(git version | sed -e 's/^git version //' -e 's/\./\\./g')
179 while read magic cmd
181 case "$magic" in
182 '' | '#'*)
183 continue ;;
184 :noellipses)
185 magic=noellipses
186 label="$magic-$cmd"
189 BUG "unknown magic $magic"
192 cmd="$magic $cmd"
193 magic=
194 label="$cmd"
196 esac
198 test=$(echo "$label" | sed -e 's|[/ ][/ ]*|_|g')
199 pfx=$(printf "%04d" $test_count)
200 expect="$TEST_DIRECTORY/t4013/diff.$test"
201 actual="$pfx-diff.$test"
203 test_expect_success "git $cmd # magic is ${magic:-(not used)}" '
205 echo "$ git $cmd"
206 case "$magic" in
208 GIT_PRINT_SHA1_ELLIPSIS=yes git $cmd ;;
209 noellipses)
210 git $cmd ;;
211 esac |
212 sed -e "s/^\\(-*\\)$V\\(-*\\)\$/\\1g-i-t--v-e-r-s-i-o-n\2/" \
213 -e "s/^\\(.*mixed; boundary=\"-*\\)$V\\(-*\\)\"\$/\\1g-i-t--v-e-r-s-i-o-n\2\"/"
214 echo "\$"
215 } >"$actual" &&
216 if test -f "$expect"
217 then
218 process_diffs "$actual" >actual &&
219 process_diffs "$expect" >expect &&
220 case $cmd in
221 *format-patch* | *-stat*)
222 test_cmp expect actual;;
224 test_cmp expect actual;;
225 esac &&
226 rm -f "$actual" actual expect
227 else
228 # this is to help developing new tests.
229 cp "$actual" "$expect"
230 false
233 done <<\EOF
234 diff-tree initial
235 diff-tree -r initial
236 diff-tree -r --abbrev initial
237 diff-tree -r --abbrev=4 initial
238 diff-tree --root initial
239 diff-tree --root --abbrev initial
240 :noellipses diff-tree --root --abbrev initial
241 diff-tree --root -r initial
242 diff-tree --root -r --abbrev initial
243 :noellipses diff-tree --root -r --abbrev initial
244 diff-tree --root -r --abbrev=4 initial
245 :noellipses diff-tree --root -r --abbrev=4 initial
246 diff-tree -p initial
247 diff-tree --root -p initial
248 diff-tree --root -p --abbrev=10 initial
249 diff-tree --root -p --full-index initial
250 diff-tree --root -p --full-index --abbrev=10 initial
251 diff-tree --patch-with-stat initial
252 diff-tree --root --patch-with-stat initial
253 diff-tree --patch-with-raw initial
254 diff-tree --root --patch-with-raw initial
256 diff-tree --pretty initial
257 diff-tree --pretty --root initial
258 diff-tree --pretty -p initial
259 diff-tree --pretty --stat initial
260 diff-tree --pretty --summary initial
261 diff-tree --pretty --stat --summary initial
262 diff-tree --pretty --root -p initial
263 diff-tree --pretty --root --stat initial
264 # improved by Timo's patch
265 diff-tree --pretty --root --summary initial
266 # improved by Timo's patch
267 diff-tree --pretty --root --summary -r initial
268 diff-tree --pretty --root --stat --summary initial
269 diff-tree --pretty --patch-with-stat initial
270 diff-tree --pretty --root --patch-with-stat initial
271 diff-tree --pretty --patch-with-raw initial
272 diff-tree --pretty --root --patch-with-raw initial
274 diff-tree --pretty=oneline initial
275 diff-tree --pretty=oneline --root initial
276 diff-tree --pretty=oneline -p initial
277 diff-tree --pretty=oneline --root -p initial
278 diff-tree --pretty=oneline --patch-with-stat initial
279 # improved by Timo's patch
280 diff-tree --pretty=oneline --root --patch-with-stat initial
281 diff-tree --pretty=oneline --patch-with-raw initial
282 diff-tree --pretty=oneline --root --patch-with-raw initial
284 diff-tree --pretty side
285 diff-tree --pretty -p side
286 diff-tree --pretty --patch-with-stat side
288 diff-tree initial mode
289 diff-tree --stat initial mode
290 diff-tree --summary initial mode
292 diff-tree master
293 diff-tree -m master
294 diff-tree -p master
295 diff-tree -p -m master
296 diff-tree -c master
297 diff-tree -c --abbrev master
298 :noellipses diff-tree -c --abbrev master
299 diff-tree --cc master
300 # stat only should show the diffstat with the first parent
301 diff-tree -c --stat master
302 diff-tree --cc --stat master
303 diff-tree -c --stat --summary master
304 diff-tree --cc --stat --summary master
305 # stat summary should show the diffstat and summary with the first parent
306 diff-tree -c --stat --summary side
307 diff-tree --cc --stat --summary side
308 diff-tree --cc --shortstat master
309 diff-tree --cc --summary REVERSE
310 # improved by Timo's patch
311 diff-tree --cc --patch-with-stat master
312 # improved by Timo's patch
313 diff-tree --cc --patch-with-stat --summary master
314 # this is correct
315 diff-tree --cc --patch-with-stat --summary side
317 log master
318 log -p master
319 log --root master
320 log --root -p master
321 log --patch-with-stat master
322 log --root --patch-with-stat master
323 log --root --patch-with-stat --summary master
324 # improved by Timo's patch
325 log --root -c --patch-with-stat --summary master
326 # improved by Timo's patch
327 log --root --cc --patch-with-stat --summary master
328 log --no-diff-merges -p --first-parent master
329 log --diff-merges=off -p --first-parent master
330 log --first-parent --diff-merges=off -p master
331 log -p --first-parent master
332 log -p --diff-merges=first-parent master
333 log --diff-merges=first-parent master
334 log -m -p --first-parent master
335 log -m -p master
336 log --cc -m -p master
337 log -c -m -p master
338 log -m --raw master
339 log -m --stat master
340 log -SF master
341 log -S F master
342 log -SF -p master
343 log -SF master --max-count=0
344 log -SF master --max-count=1
345 log -SF master --max-count=2
346 log -GF master
347 log -GF -p master
348 log -GF -p --pickaxe-all master
349 log -IA -IB -I1 -I2 -p master
350 log --decorate --all
351 log --decorate=full --all
352 log --decorate --clear-decorations --all
353 log --decorate=full --clear-decorations --all
355 rev-list --parents HEAD
356 rev-list --children HEAD
358 whatchanged master
359 :noellipses whatchanged master
360 whatchanged -p master
361 whatchanged --root master
362 :noellipses whatchanged --root master
363 whatchanged --root -p master
364 whatchanged --patch-with-stat master
365 whatchanged --root --patch-with-stat master
366 whatchanged --root --patch-with-stat --summary master
367 # improved by Timo's patch
368 whatchanged --root -c --patch-with-stat --summary master
369 # improved by Timo's patch
370 whatchanged --root --cc --patch-with-stat --summary master
371 whatchanged -SF master
372 :noellipses whatchanged -SF master
373 whatchanged -SF -p master
375 log --patch-with-stat master -- dir/
376 whatchanged --patch-with-stat master -- dir/
377 log --patch-with-stat --summary master -- dir/
378 whatchanged --patch-with-stat --summary master -- dir/
380 show initial
381 show --root initial
382 show side
383 show master
384 show -c master
385 show -m master
386 show --first-parent master
387 show --stat side
388 show --stat --summary side
389 show --patch-with-stat side
390 show --patch-with-raw side
391 :noellipses show --patch-with-raw side
392 show --patch-with-stat --summary side
394 format-patch --stdout initial..side
395 format-patch --stdout initial..master^
396 format-patch --stdout initial..master
397 format-patch --stdout --no-numbered initial..master
398 format-patch --stdout --numbered initial..master
399 format-patch --attach --stdout initial..side
400 format-patch --attach --stdout --suffix=.diff initial..side
401 format-patch --attach --stdout initial..master^
402 format-patch --attach --stdout initial..master
403 format-patch --inline --stdout initial..side
404 format-patch --inline --stdout initial..master^
405 format-patch --inline --stdout --numbered-files initial..master
406 format-patch --inline --stdout initial..master
407 format-patch --inline --stdout --subject-prefix=TESTCASE initial..master
408 config format.subjectprefix DIFFERENT_PREFIX
409 format-patch --inline --stdout initial..master^^
410 format-patch --stdout --cover-letter -n initial..master^
412 diff --abbrev initial..side
413 diff -U initial..side
414 diff -U1 initial..side
415 diff -r initial..side
416 diff --stat initial..side
417 diff -r --stat initial..side
418 diff initial..side
419 diff --patch-with-stat initial..side
420 diff --patch-with-raw initial..side
421 :noellipses diff --patch-with-raw initial..side
422 diff --patch-with-stat -r initial..side
423 diff --patch-with-raw -r initial..side
424 :noellipses diff --patch-with-raw -r initial..side
425 diff --name-status dir2 dir
426 diff --no-index --name-status dir2 dir
427 diff --no-index --name-status -- dir2 dir
428 diff --no-index dir dir3
429 diff master master^ side
430 # Can't use spaces...
431 diff --line-prefix=abc master master^ side
432 diff --dirstat master~1 master~2
433 diff --dirstat initial rearrange
434 diff --dirstat-by-file initial rearrange
435 diff --dirstat --cc master~1 master
436 # No-index --abbrev and --no-abbrev
437 diff --raw initial
438 :noellipses diff --raw initial
439 diff --raw --abbrev=4 initial
440 :noellipses diff --raw --abbrev=4 initial
441 diff --raw --no-abbrev initial
442 diff --no-index --raw dir2 dir
443 :noellipses diff --no-index --raw dir2 dir
444 diff --no-index --raw --abbrev=4 dir2 dir
445 :noellipses diff --no-index --raw --abbrev=4 dir2 dir
446 diff --no-index --raw --no-abbrev dir2 dir
448 diff-tree --pretty --root --stat --compact-summary initial
449 diff-tree --pretty -R --root --stat --compact-summary initial
450 diff-tree --pretty note
451 diff-tree --pretty --notes note
452 diff-tree --format=%N note
453 diff-tree --stat --compact-summary initial mode
454 diff-tree -R --stat --compact-summary initial mode
457 test_expect_success 'log -m matches pure log' '
458 git log master >result &&
459 process_diffs result >expected &&
460 git log -m >result &&
461 process_diffs result >actual &&
462 test_cmp expected actual
465 test_expect_success 'log --diff-merges=on matches --diff-merges=separate' '
466 git log -p --diff-merges=separate master >result &&
467 process_diffs result >expected &&
468 git log -p --diff-merges=on master >result &&
469 process_diffs result >actual &&
470 test_cmp expected actual
473 test_expect_success 'log --dd matches --diff-merges=1 -p' '
474 git log --diff-merges=1 -p master >result &&
475 process_diffs result >expected &&
476 git log --dd master >result &&
477 process_diffs result >actual &&
478 test_cmp expected actual
481 test_expect_success 'deny wrong log.diffMerges config' '
482 test_config log.diffMerges wrong-value &&
483 test_expect_code 128 git log
486 test_expect_success 'git config log.diffMerges first-parent' '
487 git log -p --diff-merges=first-parent master >result &&
488 process_diffs result >expected &&
489 test_config log.diffMerges first-parent &&
490 git log -p --diff-merges=on master >result &&
491 process_diffs result >actual &&
492 test_cmp expected actual
495 test_expect_success 'git config log.diffMerges first-parent vs -m' '
496 git log -p --diff-merges=first-parent master >result &&
497 process_diffs result >expected &&
498 test_config log.diffMerges first-parent &&
499 git log -p -m master >result &&
500 process_diffs result >actual &&
501 test_cmp expected actual
504 # -m in "git diff-index" means "match missing", that differs
505 # from its meaning in "git diff". Let's check it in diff-index.
506 # The line in the output for removed file should disappear when
507 # we provide -m in diff-index.
508 test_expect_success 'git diff-index -m' '
509 rm -f file1 &&
510 git diff-index HEAD >without-m &&
511 lines_count=$(wc -l <without-m) &&
512 git diff-index -m HEAD >with-m &&
513 git restore file1 &&
514 test_line_count = $((lines_count - 1)) with-m
517 test_expect_success 'log -S requires an argument' '
518 test_must_fail git log -S
521 test_expect_success 'diff --cached on unborn branch' '
522 git symbolic-ref HEAD refs/heads/unborn &&
523 git diff --cached >result &&
524 process_diffs result >actual &&
525 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached" >expected &&
526 test_cmp expected actual
529 test_expect_success 'diff --cached -- file on unborn branch' '
530 git diff --cached -- file0 >result &&
531 process_diffs result >actual &&
532 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached_--_file0" >expected &&
533 test_cmp expected actual
535 test_expect_success 'diff --line-prefix with spaces' '
536 git diff --line-prefix="| | | " --cached -- file0 >result &&
537 process_diffs result >actual &&
538 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--line-prefix_--cached_--_file0" >expected &&
539 test_cmp expected actual
542 test_expect_success 'diff-tree --stdin with log formatting' '
543 cat >expect <<-\EOF &&
544 Side
545 Third
546 Second
548 git rev-list master | git diff-tree --stdin --format=%s -s >actual &&
549 test_cmp expect actual
552 test_expect_success 'diff-tree --stdin with pathspec' '
553 cat >expect <<-EOF &&
554 Third
556 dir/sub
557 Second
559 dir/sub
561 git rev-list master^ |
562 git diff-tree -r --stdin --name-only --format=%s dir >actual &&
563 test_cmp expect actual
566 test_expect_success 'show A B ... -- <pathspec>' '
567 # side touches dir/sub, file0, and file3
568 # master^ touches dir/sub, and file1
569 # master^^ touches dir/sub, file0, and file2
570 git show --name-only --format="<%s>" side master^ master^^ -- dir >actual &&
571 cat >expect <<-\EOF &&
572 <Side>
574 dir/sub
575 <Third>
577 dir/sub
578 <Second>
580 dir/sub
582 test_cmp expect actual
585 test_expect_success 'diff -I<regex>: setup' '
586 git checkout master &&
587 test_seq 50 >file0 &&
588 git commit -m "Set up -I<regex> test file" file0 &&
589 test_seq 50 | sed -e "s/13/ten and three/" -e "/7\$/d" >file0 &&
590 echo >>file0
592 test_expect_success 'diff -I<regex>' '
593 git diff --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
594 cat >expect <<-\EOF &&
595 diff --git a/file0 b/file0
596 --- a/file0
597 +++ b/file0
598 @@ -34,7 +31,6 @@
607 compare_diff_patch expect actual
610 test_expect_success 'diff -I<regex> --stat' '
611 git diff --stat --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
612 cat >expect <<-\EOF &&
613 file0 | 1 -
614 1 file changed, 1 deletion(-)
616 test_cmp expect actual
619 test_expect_success 'diff -I<regex>: detect malformed regex' '
620 test_expect_code 129 git diff --ignore-matching-lines="^[124-9" 2>error &&
621 test_grep "invalid regex given to -I: " error
624 # check_prefix <patch> <src> <dst>
625 # check only lines with paths to avoid dependency on exact oid/contents
626 check_prefix () {
627 grep -E '^(diff|---|\+\+\+) ' "$1" >actual.paths &&
628 cat >expect <<-EOF &&
629 diff --git $2 $3
630 --- $2
631 +++ $3
633 test_cmp expect actual.paths
636 test_expect_success 'diff-files does not respect diff.noprefix' '
637 git -c diff.noprefix diff-files -p >actual &&
638 check_prefix actual a/file0 b/file0
641 test_expect_success 'diff-files respects --no-prefix' '
642 git diff-files -p --no-prefix >actual &&
643 check_prefix actual file0 file0
646 test_expect_success 'diff respects diff.noprefix' '
647 git -c diff.noprefix diff >actual &&
648 check_prefix actual file0 file0
651 test_expect_success 'diff --default-prefix overrides diff.noprefix' '
652 git -c diff.noprefix diff --default-prefix >actual &&
653 check_prefix actual a/file0 b/file0
656 test_expect_success 'diff respects diff.mnemonicprefix' '
657 git -c diff.mnemonicprefix diff >actual &&
658 check_prefix actual i/file0 w/file0
661 test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
662 git -c diff.mnemonicprefix diff --default-prefix >actual &&
663 check_prefix actual a/file0 b/file0
666 test_expect_success 'diff --no-renames cannot be abbreviated' '
667 test_expect_code 129 git diff --no-rename >actual 2>error &&
668 test_must_be_empty actual &&
669 grep "invalid option: --no-rename" error
672 test_done