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_PASSES_SANITIZE_LEAK
=true
13 .
"$TEST_DIRECTORY"/lib-diff.sh
15 test_expect_success setup
'
17 GIT_AUTHOR_DATE="2006-06-26 00:00:00 +0000" &&
18 GIT_COMMITTER_DATE="2006-06-26 00:00:00 +0000" &&
19 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
23 test_write_lines 1 2 3 >file0 &&
24 test_write_lines A B >dir/sub &&
26 git add file0 file2 dir/sub &&
27 git commit -m Initial &&
32 GIT_AUTHOR_DATE="2006-06-26 00:01:00 +0000" &&
33 GIT_COMMITTER_DATE="2006-06-26 00:01:00 +0000" &&
34 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
36 test_write_lines 4 5 6 >>file0 &&
37 test_write_lines C D >>dir/sub &&
39 git update-index --remove file0 file2 dir/sub &&
40 git commit -m "Second${LF}${LF}This is the second commit." &&
42 GIT_AUTHOR_DATE="2006-06-26 00:02:00 +0000" &&
43 GIT_COMMITTER_DATE="2006-06-26 00:02:00 +0000" &&
44 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
46 test_write_lines A B C >file1 &&
48 test_write_lines E F >>dir/sub &&
49 git update-index dir/sub &&
50 git commit -m Third &&
52 GIT_AUTHOR_DATE="2006-06-26 00:03:00 +0000" &&
53 GIT_COMMITTER_DATE="2006-06-26 00:03:00 +0000" &&
54 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
57 test_write_lines A B C >>file0 &&
58 test_write_lines 1 2 >>dir/sub &&
61 git update-index file0 dir/sub &&
64 GIT_AUTHOR_DATE="2006-06-26 00:04:00 +0000" &&
65 GIT_COMMITTER_DATE="2006-06-26 00:04:00 +0000" &&
66 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
68 git checkout master &&
69 git pull -s ours --no-rebase . side &&
71 GIT_AUTHOR_DATE="2006-06-26 00:05:00 +0000" &&
72 GIT_COMMITTER_DATE="2006-06-26 00:05:00 +0000" &&
73 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
75 test_write_lines A B C >>file0 &&
76 test_write_lines 1 2 >>dir/sub &&
77 git update-index file0 dir/sub &&
80 cp dir/sub dir3/sub &&
81 test-tool chmtime +1 dir3/sub &&
83 git config log.showroot false &&
86 GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
87 GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
88 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
89 git checkout -b rearrange initial &&
90 test_write_lines B A >dir/sub &&
92 git commit -m "Rearranged lines in dir/sub" &&
93 git checkout master &&
95 GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
96 GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
97 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
98 git checkout -b mode initial &&
99 git update-index --chmod=+x file0 &&
100 git commit -m "update mode" &&
101 git checkout -f master &&
103 GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
104 GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
105 export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
106 git checkout -b note initial &&
107 git update-index --chmod=+x file2 &&
108 git commit -m "update mode (file2)" &&
109 git notes add -m "note" &&
110 git checkout -f master &&
112 # Same merge as master, but with parents reversed. Hide it in a
113 # pseudo-ref to avoid impacting tests with --all.
114 commit=$(echo reverse |
115 git commit-tree -p master^2 -p master^1 master^{tree}) &&
116 git update-ref REVERSE $commit &&
118 git config diff.renames false &&
125 * [master
] Merge branch
'side'
126 ! [rearrange
] Rearranged lines
in dir
/sub
129 + [rearrange
] Rearranged lines
in dir
/sub
130 - [master
] Merge branch
'side'
134 +*++ [initial
] Initial
139 my $oid_length = length($ARGV[0]);
140 my $x40 = "[0-9a-f]{40}";
141 my $xab = "[0-9a-f]{4,16}";
142 my $orx = "[0-9a-f]" x $oid_length;
148 return "" unless length $oid;
150 if ($oid =~ /^(100644|100755|120000)$/) {
154 if ($oid =~ /^0*$/) {
160 if (length($oid) == 40) {
161 return $x x $oid_length;
163 return $x x length($oid);
168 s/($orx)/munge_oid($1)/ge;
169 s/From ($x40)( |\))/"From " . munge_oid($1) . $2/ge;
170 s/commit ($x40)($| \(from )($x40?)/"commit " . munge_oid($1) . $2 . munge_oid($3)/ge;
171 s/\b($x40)( |\.\.|$)/munge_oid($1) . $2/ge;
172 s/^($x40)($| )/munge_oid($1) . $2/e;
173 s/($xab)(\.\.|,| |\.\.\.|$)/munge_oid($1) . $2/ge;
179 V
=$
(git version |
sed -e 's/^git version //' -e 's/\./\\./g')
190 BUG
"unknown magic $magic"
199 test=$
(echo "$label" |
sed -e 's|[/ ][/ ]*|_|g')
200 pfx
=$
(printf "%04d" $test_count)
201 expect
="$TEST_DIRECTORY/t4013/diff.$test"
202 actual
="$pfx-diff.$test"
204 test_expect_success
"git $cmd # magic is ${magic:-(not used)}" '
209 GIT_PRINT_SHA1_ELLIPSIS=yes git $cmd ;;
213 sed -e "s/^\\(-*\\)$V\\(-*\\)\$/\\1g-i-t--v-e-r-s-i-o-n\2/" \
214 -e "s/^\\(.*mixed; boundary=\"-*\\)$V\\(-*\\)\"\$/\\1g-i-t--v-e-r-s-i-o-n\2\"/"
219 process_diffs "$actual" >actual &&
220 process_diffs "$expect" >expect &&
222 *format-patch* | *-stat*)
223 test_cmp expect actual;;
225 test_cmp expect actual;;
227 rm -f "$actual" actual expect
229 # this is to help developing new tests.
230 cp "$actual" "$expect"
237 diff-tree
-r --abbrev initial
238 diff-tree
-r --abbrev=4 initial
239 diff-tree
--root initial
240 diff-tree
--root --abbrev initial
241 :noellipses diff-tree
--root --abbrev initial
242 diff-tree
--root -r initial
243 diff-tree
--root -r --abbrev initial
244 :noellipses diff-tree
--root -r --abbrev initial
245 diff-tree
--root -r --abbrev=4 initial
246 :noellipses diff-tree
--root -r --abbrev=4 initial
248 diff-tree
--root -p initial
249 diff-tree
--root -p --abbrev=10 initial
250 diff-tree
--root -p --full-index initial
251 diff-tree
--root -p --full-index --abbrev=10 initial
252 diff-tree
--patch-with-stat initial
253 diff-tree
--root --patch-with-stat initial
254 diff-tree
--patch-with-raw initial
255 diff-tree
--root --patch-with-raw initial
257 diff-tree
--pretty initial
258 diff-tree
--pretty --root initial
259 diff-tree
--pretty -p initial
260 diff-tree
--pretty --stat initial
261 diff-tree
--pretty --summary initial
262 diff-tree
--pretty --stat --summary initial
263 diff-tree
--pretty --root -p initial
264 diff-tree
--pretty --root --stat initial
265 # improved by Timo's patch
266 diff-tree
--pretty --root --summary initial
267 # improved by Timo's patch
268 diff-tree
--pretty --root --summary -r initial
269 diff-tree
--pretty --root --stat --summary initial
270 diff-tree
--pretty --patch-with-stat initial
271 diff-tree
--pretty --root --patch-with-stat initial
272 diff-tree
--pretty --patch-with-raw initial
273 diff-tree
--pretty --root --patch-with-raw initial
275 diff-tree
--pretty=oneline initial
276 diff-tree
--pretty=oneline
--root initial
277 diff-tree
--pretty=oneline
-p initial
278 diff-tree
--pretty=oneline
--root -p initial
279 diff-tree
--pretty=oneline
--patch-with-stat initial
280 # improved by Timo's patch
281 diff-tree
--pretty=oneline
--root --patch-with-stat initial
282 diff-tree
--pretty=oneline
--patch-with-raw initial
283 diff-tree
--pretty=oneline
--root --patch-with-raw initial
285 diff-tree
--pretty side
286 diff-tree
--pretty -p side
287 diff-tree
--pretty --patch-with-stat side
289 diff-tree initial mode
290 diff-tree
--stat initial mode
291 diff-tree
--summary initial mode
296 diff-tree
-p -m master
298 diff-tree
-c --abbrev master
299 :noellipses diff-tree
-c --abbrev master
300 diff-tree
--cc master
301 # stat only should show the diffstat with the first parent
302 diff-tree
-c --stat master
303 diff-tree
--cc --stat master
304 diff-tree
-c --stat --summary master
305 diff-tree
--cc --stat --summary master
306 # stat summary should show the diffstat and summary with the first parent
307 diff-tree
-c --stat --summary side
308 diff-tree
--cc --stat --summary side
309 diff-tree
--cc --shortstat master
310 diff-tree
--cc --summary REVERSE
311 # improved by Timo's patch
312 diff-tree
--cc --patch-with-stat master
313 # improved by Timo's patch
314 diff-tree
--cc --patch-with-stat --summary master
316 diff-tree
--cc --patch-with-stat --summary side
322 log
--patch-with-stat master
323 log
--root --patch-with-stat master
324 log
--root --patch-with-stat --summary master
325 # improved by Timo's patch
326 log
--root -c --patch-with-stat --summary master
327 # improved by Timo's patch
328 log
--root --cc --patch-with-stat --summary master
329 log
--no-diff-merges -p --first-parent master
330 log
--diff-merges=off
-p --first-parent master
331 log
--first-parent --diff-merges=off
-p master
332 log
-p --first-parent master
333 log
-p --diff-merges=first-parent master
334 log
--diff-merges=first-parent master
335 log
-m -p --first-parent master
337 log
--cc -m -p master
344 log
-SF master
--max-count=0
345 log
-SF master
--max-count=1
346 log
-SF master
--max-count=2
349 log
-GF -p --pickaxe-all master
350 log
-IA -IB -I1 -I2 -p master
352 log
--decorate=full
--all
353 log
--decorate --clear-decorations --all
354 log
--decorate=full
--clear-decorations --all
356 rev-list
--parents HEAD
357 rev-list
--children HEAD
360 :noellipses whatchanged master
361 whatchanged
-p master
362 whatchanged
--root master
363 :noellipses whatchanged
--root master
364 whatchanged
--root -p master
365 whatchanged
--patch-with-stat master
366 whatchanged
--root --patch-with-stat master
367 whatchanged
--root --patch-with-stat --summary master
368 # improved by Timo's patch
369 whatchanged
--root -c --patch-with-stat --summary master
370 # improved by Timo's patch
371 whatchanged
--root --cc --patch-with-stat --summary master
372 whatchanged
-SF master
373 :noellipses whatchanged
-SF master
374 whatchanged
-SF -p master
376 log
--patch-with-stat master
-- dir
/
377 whatchanged
--patch-with-stat master
-- dir
/
378 log
--patch-with-stat --summary master
-- dir
/
379 whatchanged
--patch-with-stat --summary master
-- dir
/
387 show
--first-parent master
389 show
--stat --summary side
390 show
--patch-with-stat side
391 show
--patch-with-raw side
392 :noellipses show
--patch-with-raw side
393 show
--patch-with-stat --summary side
395 format-patch
--stdout initial..side
396 format-patch
--stdout initial..master^
397 format-patch
--stdout initial..master
398 format-patch
--stdout --no-numbered initial..master
399 format-patch
--stdout --numbered initial..master
400 format-patch
--attach --stdout initial..side
401 format-patch
--attach --stdout --suffix=.
diff initial..side
402 format-patch
--attach --stdout initial..master^
403 format-patch
--attach --stdout initial..master
404 format-patch
--inline --stdout initial..side
405 format-patch
--inline --stdout initial..master^
406 format-patch
--inline --stdout --numbered-files initial..master
407 format-patch
--inline --stdout initial..master
408 format-patch
--inline --stdout --subject-prefix=TESTCASE initial..master
409 config format.subjectprefix DIFFERENT_PREFIX
410 format-patch
--inline --stdout initial..master^^
411 format-patch
--stdout --cover-letter -n initial..master^
413 diff --abbrev initial..side
414 diff -U initial..side
415 diff -U1 initial..side
416 diff -r initial..side
417 diff --stat initial..side
418 diff -r --stat initial..side
420 diff --patch-with-stat initial..side
421 diff --patch-with-raw initial..side
422 :noellipses
diff --patch-with-raw initial..side
423 diff --patch-with-stat -r initial..side
424 diff --patch-with-raw -r initial..side
425 :noellipses
diff --patch-with-raw -r initial..side
426 diff --name-status dir2 dir
427 diff --no-index --name-status dir2 dir
428 diff --no-index --name-status -- dir2 dir
429 diff --no-index dir dir3
430 diff master master^ side
431 # Can't use spaces...
432 diff --line-prefix=abc master master^ side
433 diff --dirstat master~
1 master~
2
434 diff --dirstat initial rearrange
435 diff --dirstat-by-file initial rearrange
436 diff --dirstat --cc master~
1 master
437 # No-index --abbrev and --no-abbrev
439 :noellipses
diff --raw initial
440 diff --raw --abbrev=4 initial
441 :noellipses
diff --raw --abbrev=4 initial
442 diff --raw --no-abbrev initial
443 diff --no-index --raw dir2 dir
444 :noellipses
diff --no-index --raw dir2 dir
445 diff --no-index --raw --abbrev=4 dir2 dir
446 :noellipses
diff --no-index --raw --abbrev=4 dir2 dir
447 diff --no-index --raw --no-abbrev dir2 dir
449 diff-tree
--pretty --root --stat --compact-summary initial
450 diff-tree
--pretty -R --root --stat --compact-summary initial
451 diff-tree
--pretty note
452 diff-tree
--pretty --notes note
453 diff-tree
--format=%N note
454 diff-tree
--stat --compact-summary initial mode
455 diff-tree
-R --stat --compact-summary initial mode
458 test_expect_success
'log -m matches pure log' '
459 git log master >result &&
460 process_diffs result >expected &&
461 git log -m >result &&
462 process_diffs result >actual &&
463 test_cmp expected actual
466 test_expect_success
'log --diff-merges=on matches --diff-merges=separate' '
467 git log -p --diff-merges=separate master >result &&
468 process_diffs result >expected &&
469 git log -p --diff-merges=on master >result &&
470 process_diffs result >actual &&
471 test_cmp expected actual
474 test_expect_success
'log --dd matches --diff-merges=1 -p' '
475 git log --diff-merges=1 -p master >result &&
476 process_diffs result >expected &&
477 git log --dd master >result &&
478 process_diffs result >actual &&
479 test_cmp expected actual
482 test_expect_success
'deny wrong log.diffMerges config' '
483 test_config log.diffMerges wrong-value &&
484 test_expect_code 128 git log
487 test_expect_success
'git config log.diffMerges first-parent' '
488 git log -p --diff-merges=first-parent master >result &&
489 process_diffs result >expected &&
490 test_config log.diffMerges first-parent &&
491 git log -p --diff-merges=on master >result &&
492 process_diffs result >actual &&
493 test_cmp expected actual
496 test_expect_success
'git config log.diffMerges first-parent vs -m' '
497 git log -p --diff-merges=first-parent master >result &&
498 process_diffs result >expected &&
499 test_config log.diffMerges first-parent &&
500 git log -p -m master >result &&
501 process_diffs result >actual &&
502 test_cmp expected actual
505 # -m in "git diff-index" means "match missing", that differs
506 # from its meaning in "git diff". Let's check it in diff-index.
507 # The line in the output for removed file should disappear when
508 # we provide -m in diff-index.
509 test_expect_success
'git diff-index -m' '
511 git diff-index HEAD >without-m &&
512 lines_count=$(wc -l <without-m) &&
513 git diff-index -m HEAD >with-m &&
515 test_line_count = $((lines_count - 1)) with-m
518 test_expect_success
'log -S requires an argument' '
519 test_must_fail git log -S
522 test_expect_success
'diff --cached on unborn branch' '
523 git symbolic-ref HEAD refs/heads/unborn &&
524 git diff --cached >result &&
525 process_diffs result >actual &&
526 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached" >expected &&
527 test_cmp expected actual
530 test_expect_success
'diff --cached -- file on unborn branch' '
531 git diff --cached -- file0 >result &&
532 process_diffs result >actual &&
533 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached_--_file0" >expected &&
534 test_cmp expected actual
536 test_expect_success
'diff --line-prefix with spaces' '
537 git diff --line-prefix="| | | " --cached -- file0 >result &&
538 process_diffs result >actual &&
539 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--line-prefix_--cached_--_file0" >expected &&
540 test_cmp expected actual
543 test_expect_success
'diff-tree --stdin with log formatting' '
544 cat >expect <<-\EOF &&
549 git rev-list master | git diff-tree --stdin --format=%s -s >actual &&
550 test_cmp expect actual
553 test_expect_success
'diff-tree --stdin with pathspec' '
554 cat >expect <<-EOF &&
562 git rev-list master^ |
563 git diff-tree -r --stdin --name-only --format=%s dir >actual &&
564 test_cmp expect actual
567 test_expect_success
'show A B ... -- <pathspec>' '
568 # side touches dir/sub, file0, and file3
569 # master^ touches dir/sub, and file1
570 # master^^ touches dir/sub, file0, and file2
571 git show --name-only --format="<%s>" side master^ master^^ -- dir >actual &&
572 cat >expect <<-\EOF &&
583 test_cmp expect actual
586 test_expect_success
'diff -I<regex>: setup' '
587 git checkout master &&
588 test_seq 50 >file0 &&
589 git commit -m "Set up -I<regex> test file" file0 &&
590 test_seq 50 | sed -e "s/13/ten and three/" -e "/7\$/d" >file0 &&
593 test_expect_success
'diff -I<regex>' '
594 git diff --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
595 cat >expect <<-\EOF &&
596 diff --git a/file0 b/file0
608 compare_diff_patch expect actual
611 test_expect_success
'diff -I<regex> --stat' '
612 git diff --stat --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
613 cat >expect <<-\EOF &&
615 1 file changed, 1 deletion(-)
617 test_cmp expect actual
620 test_expect_success
'diff -I<regex>: detect malformed regex' '
621 test_expect_code 129 git diff --ignore-matching-lines="^[124-9" 2>error &&
622 test_grep "invalid regex given to -I: " error
625 # check_prefix <patch> <src> <dst>
626 # check only lines with paths to avoid dependency on exact oid/contents
628 grep -E '^(diff|---|\+\+\+) ' "$1" >actual.paths
&&
629 cat >expect
<<-EOF &&
634 test_cmp expect actual.paths
637 test_expect_success
'diff-files does not respect diff.noPrefix' '
638 git -c diff.noPrefix diff-files -p >actual &&
639 check_prefix actual a/file0 b/file0
642 test_expect_success
'diff-files respects --no-prefix' '
643 git diff-files -p --no-prefix >actual &&
644 check_prefix actual file0 file0
647 test_expect_success
'diff respects diff.noPrefix' '
648 git -c diff.noPrefix diff >actual &&
649 check_prefix actual file0 file0
652 test_expect_success
'diff --default-prefix overrides diff.noPrefix' '
653 git -c diff.noPrefix diff --default-prefix >actual &&
654 check_prefix actual a/file0 b/file0
657 test_expect_success
'diff respects diff.mnemonicPrefix' '
658 git -c diff.mnemonicPrefix diff >actual &&
659 check_prefix actual i/file0 w/file0
662 test_expect_success
'diff --default-prefix overrides diff.mnemonicPrefix' '
663 git -c diff.mnemonicPrefix diff --default-prefix >actual &&
664 check_prefix actual a/file0 b/file0
667 test_expect_success
'diff respects diff.srcPrefix' '
668 git -c diff.srcPrefix=x/ diff >actual &&
669 check_prefix actual x/file0 b/file0
672 test_expect_success
'diff respects diff.dstPrefix' '
673 git -c diff.dstPrefix=y/ diff >actual &&
674 check_prefix actual a/file0 y/file0
677 test_expect_success
'diff --src-prefix overrides diff.srcPrefix' '
678 git -c diff.srcPrefix=y/ diff --src-prefix=z/ >actual &&
679 check_prefix actual z/file0 b/file0
682 test_expect_success
'diff --dst-prefix overrides diff.dstPrefix' '
683 git -c diff.dstPrefix=y/ diff --dst-prefix=z/ >actual &&
684 check_prefix actual a/file0 z/file0
687 test_expect_success
'diff.{src,dst}Prefix ignored with diff.noPrefix' '
688 git -c diff.dstPrefix=y/ -c diff.srcPrefix=x/ -c diff.noPrefix diff >actual &&
689 check_prefix actual file0 file0
692 test_expect_success
'diff.{src,dst}Prefix ignored with diff.mnemonicPrefix' '
693 git -c diff.dstPrefix=x/ -c diff.srcPrefix=y/ -c diff.mnemonicPrefix diff >actual &&
694 check_prefix actual i/file0 w/file0
697 test_expect_success
'diff.{src,dst}Prefix ignored with --default-prefix' '
698 git -c diff.dstPrefix=x/ -c diff.srcPrefix=y/ diff --default-prefix >actual &&
699 check_prefix actual a/file0 b/file0
702 test_expect_success
'diff --no-renames cannot be abbreviated' '
703 test_expect_code 129 git diff --no-rename >actual 2>error &&
704 test_must_be_empty actual &&
705 grep "invalid option: --no-rename" error