]> git.ipfire.org Git - thirdparty/git.git/blob - t/t4013-diff-various.sh
submodule-config.c: strengthen URL fsck check
[thirdparty/git.git] / t / t4013-diff-various.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='Various diff formatting options'
7
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 . ./test-lib.sh
12 . "$TEST_DIRECTORY"/lib-diff.sh
13
14 test_expect_success setup '
15
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 &&
19
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 &&
27
28 git branch initial &&
29 git branch side &&
30
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 &&
34
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." &&
40
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 &&
44
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 &&
50
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 &&
54
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 &&
62
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 &&
66
67 git checkout master &&
68 git pull -s ours --no-rebase . side &&
69
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 &&
73
74 test_write_lines A B C >>file0 &&
75 test_write_lines 1 2 >>dir/sub &&
76 git update-index file0 dir/sub &&
77
78 mkdir dir3 &&
79 cp dir/sub dir3/sub &&
80 test-tool chmtime +1 dir3/sub &&
81
82 git config log.showroot false &&
83 git commit --amend &&
84
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 &&
93
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 &&
101
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 &&
110
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 &&
116
117 git config diff.renames false &&
118
119 git show-branch
120 '
121
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
134 EOF
135
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;
142
143 sub munge_oid {
144 my ($oid) = @_;
145 my $x;
146
147 return "" unless length $oid;
148
149 if ($oid =~ /^(100644|100755|120000)$/) {
150 return $oid;
151 }
152
153 if ($oid =~ /^0*$/) {
154 $x = "0";
155 } else {
156 $x = "f";
157 }
158
159 if (length($oid) == 40) {
160 return $x x $oid_length;
161 } else {
162 return $x x length($oid);
163 }
164 }
165
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;
174 }
175 ' "$ZERO_OID" <"$1"
176 }
177
178 V=$(git version | sed -e 's/^git version //' -e 's/\./\\./g')
179 while read magic cmd
180 do
181 status=success
182 case "$magic" in
183 '' | '#'*)
184 continue ;;
185 :*)
186 magic=${magic#:}
187 label="$magic-$cmd"
188 case "$magic" in
189 noellipses) ;;
190 failure)
191 status=failure
192 magic=
193 label="$cmd" ;;
194 *)
195 BUG "unknown magic $magic" ;;
196 esac ;;
197 *)
198 cmd="$magic $cmd" magic=
199 label="$cmd" ;;
200 esac
201 test=$(echo "$label" | sed -e 's|[/ ][/ ]*|_|g')
202 pfx=$(printf "%04d" $test_count)
203 expect="$TEST_DIRECTORY/t4013/diff.$test"
204 actual="$pfx-diff.$test"
205
206 test_expect_$status "git $cmd # magic is ${magic:-(not used)}" '
207 {
208 echo "$ git $cmd"
209 case "$magic" in
210 "")
211 GIT_PRINT_SHA1_ELLIPSIS=yes git $cmd ;;
212 noellipses)
213 git $cmd ;;
214 esac |
215 sed -e "s/^\\(-*\\)$V\\(-*\\)\$/\\1g-i-t--v-e-r-s-i-o-n\2/" \
216 -e "s/^\\(.*mixed; boundary=\"-*\\)$V\\(-*\\)\"\$/\\1g-i-t--v-e-r-s-i-o-n\2\"/"
217 echo "\$"
218 } >"$actual" &&
219 if test -f "$expect"
220 then
221 process_diffs "$actual" >actual &&
222 process_diffs "$expect" >expect &&
223 case $cmd in
224 *format-patch* | *-stat*)
225 test_cmp expect actual;;
226 *)
227 test_cmp expect actual;;
228 esac &&
229 rm -f "$actual" actual expect
230 else
231 # this is to help developing new tests.
232 cp "$actual" "$expect"
233 false
234 fi
235 '
236 done <<\EOF
237 diff-tree initial
238 diff-tree -r initial
239 diff-tree -r --abbrev initial
240 diff-tree -r --abbrev=4 initial
241 diff-tree --root initial
242 diff-tree --root --abbrev initial
243 :noellipses diff-tree --root --abbrev initial
244 diff-tree --root -r initial
245 diff-tree --root -r --abbrev initial
246 :noellipses diff-tree --root -r --abbrev initial
247 diff-tree --root -r --abbrev=4 initial
248 :noellipses diff-tree --root -r --abbrev=4 initial
249 diff-tree -p initial
250 diff-tree --root -p initial
251 diff-tree --root -p --abbrev=10 initial
252 diff-tree --root -p --full-index initial
253 diff-tree --root -p --full-index --abbrev=10 initial
254 diff-tree --patch-with-stat initial
255 diff-tree --root --patch-with-stat initial
256 diff-tree --patch-with-raw initial
257 diff-tree --root --patch-with-raw initial
258
259 diff-tree --pretty initial
260 diff-tree --pretty --root initial
261 diff-tree --pretty -p initial
262 diff-tree --pretty --stat initial
263 diff-tree --pretty --summary initial
264 diff-tree --pretty --stat --summary initial
265 diff-tree --pretty --root -p initial
266 diff-tree --pretty --root --stat initial
267 # improved by Timo's patch
268 diff-tree --pretty --root --summary initial
269 # improved by Timo's patch
270 diff-tree --pretty --root --summary -r initial
271 diff-tree --pretty --root --stat --summary initial
272 diff-tree --pretty --patch-with-stat initial
273 diff-tree --pretty --root --patch-with-stat initial
274 diff-tree --pretty --patch-with-raw initial
275 diff-tree --pretty --root --patch-with-raw initial
276
277 diff-tree --pretty=oneline initial
278 diff-tree --pretty=oneline --root initial
279 diff-tree --pretty=oneline -p initial
280 diff-tree --pretty=oneline --root -p initial
281 diff-tree --pretty=oneline --patch-with-stat initial
282 # improved by Timo's patch
283 diff-tree --pretty=oneline --root --patch-with-stat initial
284 diff-tree --pretty=oneline --patch-with-raw initial
285 diff-tree --pretty=oneline --root --patch-with-raw initial
286
287 diff-tree --pretty side
288 diff-tree --pretty -p side
289 diff-tree --pretty --patch-with-stat side
290
291 diff-tree initial mode
292 diff-tree --stat initial mode
293 diff-tree --summary initial mode
294
295 diff-tree master
296 diff-tree -m master
297 diff-tree -p master
298 diff-tree -p -m master
299 diff-tree -c master
300 diff-tree -c --abbrev master
301 :noellipses diff-tree -c --abbrev master
302 diff-tree --cc master
303 # stat only should show the diffstat with the first parent
304 diff-tree -c --stat master
305 diff-tree --cc --stat master
306 diff-tree -c --stat --summary master
307 diff-tree --cc --stat --summary master
308 # stat summary should show the diffstat and summary with the first parent
309 diff-tree -c --stat --summary side
310 diff-tree --cc --stat --summary side
311 diff-tree --cc --shortstat master
312 diff-tree --cc --summary REVERSE
313 # improved by Timo's patch
314 diff-tree --cc --patch-with-stat master
315 # improved by Timo's patch
316 diff-tree --cc --patch-with-stat --summary master
317 # this is correct
318 diff-tree --cc --patch-with-stat --summary side
319
320 log master
321 log -p master
322 log --root master
323 log --root -p master
324 log --patch-with-stat master
325 log --root --patch-with-stat master
326 log --root --patch-with-stat --summary master
327 # improved by Timo's patch
328 log --root -c --patch-with-stat --summary master
329 # improved by Timo's patch
330 log --root --cc --patch-with-stat --summary master
331 log --no-diff-merges -p --first-parent master
332 log --diff-merges=off -p --first-parent master
333 log --first-parent --diff-merges=off -p master
334 log -p --first-parent master
335 log -p --diff-merges=first-parent master
336 log --diff-merges=first-parent master
337 log -m -p --first-parent master
338 log -m -p master
339 log --cc -m -p master
340 log -c -m -p master
341 log -m --raw master
342 log -m --stat master
343 log -SF master
344 log -S F master
345 log -SF -p master
346 log -SF master --max-count=0
347 log -SF master --max-count=1
348 log -SF master --max-count=2
349 log -GF master
350 log -GF -p master
351 log -GF -p --pickaxe-all master
352 log -IA -IB -I1 -I2 -p master
353 log --decorate --all
354 log --decorate=full --all
355 log --decorate --clear-decorations --all
356 log --decorate=full --clear-decorations --all
357
358 rev-list --parents HEAD
359 rev-list --children HEAD
360
361 whatchanged master
362 :noellipses whatchanged master
363 whatchanged -p master
364 whatchanged --root master
365 :noellipses whatchanged --root master
366 whatchanged --root -p master
367 whatchanged --patch-with-stat master
368 whatchanged --root --patch-with-stat master
369 whatchanged --root --patch-with-stat --summary master
370 # improved by Timo's patch
371 whatchanged --root -c --patch-with-stat --summary master
372 # improved by Timo's patch
373 whatchanged --root --cc --patch-with-stat --summary master
374 whatchanged -SF master
375 :noellipses whatchanged -SF master
376 whatchanged -SF -p master
377
378 log --patch-with-stat master -- dir/
379 whatchanged --patch-with-stat master -- dir/
380 log --patch-with-stat --summary master -- dir/
381 whatchanged --patch-with-stat --summary master -- dir/
382
383 show initial
384 show --root initial
385 show side
386 show master
387 show -c master
388 show -m master
389 show --first-parent master
390 show --stat side
391 show --stat --summary side
392 show --patch-with-stat side
393 show --patch-with-raw side
394 :noellipses show --patch-with-raw side
395 show --patch-with-stat --summary side
396
397 format-patch --stdout initial..side
398 format-patch --stdout initial..master^
399 format-patch --stdout initial..master
400 format-patch --stdout --no-numbered initial..master
401 format-patch --stdout --numbered initial..master
402 format-patch --attach --stdout initial..side
403 format-patch --attach --stdout --suffix=.diff initial..side
404 format-patch --attach --stdout initial..master^
405 format-patch --attach --stdout initial..master
406 format-patch --inline --stdout initial..side
407 format-patch --inline --stdout initial..master^
408 format-patch --inline --stdout --numbered-files initial..master
409 format-patch --inline --stdout initial..master
410 format-patch --inline --stdout --subject-prefix=TESTCASE initial..master
411 config format.subjectprefix DIFFERENT_PREFIX
412 format-patch --inline --stdout initial..master^^
413 format-patch --stdout --cover-letter -n initial..master^
414
415 diff --abbrev initial..side
416 diff -U initial..side
417 diff -U1 initial..side
418 diff -r initial..side
419 diff --stat initial..side
420 diff -r --stat initial..side
421 diff initial..side
422 diff --patch-with-stat initial..side
423 diff --patch-with-raw initial..side
424 :noellipses diff --patch-with-raw initial..side
425 diff --patch-with-stat -r initial..side
426 diff --patch-with-raw -r initial..side
427 :noellipses diff --patch-with-raw -r initial..side
428 diff --name-status dir2 dir
429 diff --no-index --name-status dir2 dir
430 diff --no-index --name-status -- dir2 dir
431 diff --no-index dir dir3
432 diff master master^ side
433 # Can't use spaces...
434 diff --line-prefix=abc master master^ side
435 diff --dirstat master~1 master~2
436 diff --dirstat initial rearrange
437 diff --dirstat-by-file initial rearrange
438 diff --dirstat --cc master~1 master
439 # No-index --abbrev and --no-abbrev
440 diff --raw initial
441 :noellipses diff --raw initial
442 diff --raw --abbrev=4 initial
443 :noellipses diff --raw --abbrev=4 initial
444 diff --raw --no-abbrev initial
445 diff --no-index --raw dir2 dir
446 :noellipses diff --no-index --raw dir2 dir
447 diff --no-index --raw --abbrev=4 dir2 dir
448 :noellipses diff --no-index --raw --abbrev=4 dir2 dir
449 diff --no-index --raw --no-abbrev dir2 dir
450
451 diff-tree --pretty --root --stat --compact-summary initial
452 diff-tree --pretty -R --root --stat --compact-summary initial
453 diff-tree --pretty note
454 diff-tree --pretty --notes note
455 diff-tree --format=%N note
456 diff-tree --stat --compact-summary initial mode
457 diff-tree -R --stat --compact-summary initial mode
458 EOF
459
460 test_expect_success 'log -m matches pure log' '
461 git log master >result &&
462 process_diffs result >expected &&
463 git log -m >result &&
464 process_diffs result >actual &&
465 test_cmp expected actual
466 '
467
468 test_expect_success 'log --diff-merges=on matches --diff-merges=separate' '
469 git log -p --diff-merges=separate master >result &&
470 process_diffs result >expected &&
471 git log -p --diff-merges=on master >result &&
472 process_diffs result >actual &&
473 test_cmp expected actual
474 '
475
476 test_expect_success 'log --dd matches --diff-merges=1 -p' '
477 git log --diff-merges=1 -p master >result &&
478 process_diffs result >expected &&
479 git log --dd master >result &&
480 process_diffs result >actual &&
481 test_cmp expected actual
482 '
483
484 test_expect_success 'deny wrong log.diffMerges config' '
485 test_config log.diffMerges wrong-value &&
486 test_expect_code 128 git log
487 '
488
489 test_expect_success 'git config log.diffMerges first-parent' '
490 git log -p --diff-merges=first-parent master >result &&
491 process_diffs result >expected &&
492 test_config log.diffMerges first-parent &&
493 git log -p --diff-merges=on master >result &&
494 process_diffs result >actual &&
495 test_cmp expected actual
496 '
497
498 test_expect_success 'git config log.diffMerges first-parent vs -m' '
499 git log -p --diff-merges=first-parent master >result &&
500 process_diffs result >expected &&
501 test_config log.diffMerges first-parent &&
502 git log -p -m master >result &&
503 process_diffs result >actual &&
504 test_cmp expected actual
505 '
506
507 # -m in "git diff-index" means "match missing", that differs
508 # from its meaning in "git diff". Let's check it in diff-index.
509 # The line in the output for removed file should disappear when
510 # we provide -m in diff-index.
511 test_expect_success 'git diff-index -m' '
512 rm -f file1 &&
513 git diff-index HEAD >without-m &&
514 lines_count=$(wc -l <without-m) &&
515 git diff-index -m HEAD >with-m &&
516 git restore file1 &&
517 test_line_count = $((lines_count - 1)) with-m
518 '
519
520 test_expect_success 'log -S requires an argument' '
521 test_must_fail git log -S
522 '
523
524 test_expect_success 'diff --cached on unborn branch' '
525 echo ref: refs/heads/unborn >.git/HEAD &&
526 git diff --cached >result &&
527 process_diffs result >actual &&
528 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached" >expected &&
529 test_cmp expected actual
530 '
531
532 test_expect_success 'diff --cached -- file on unborn branch' '
533 git diff --cached -- file0 >result &&
534 process_diffs result >actual &&
535 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached_--_file0" >expected &&
536 test_cmp expected actual
537 '
538 test_expect_success 'diff --line-prefix with spaces' '
539 git diff --line-prefix="| | | " --cached -- file0 >result &&
540 process_diffs result >actual &&
541 process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--line-prefix_--cached_--_file0" >expected &&
542 test_cmp expected actual
543 '
544
545 test_expect_success 'diff-tree --stdin with log formatting' '
546 cat >expect <<-\EOF &&
547 Side
548 Third
549 Second
550 EOF
551 git rev-list master | git diff-tree --stdin --format=%s -s >actual &&
552 test_cmp expect actual
553 '
554
555 test_expect_success 'diff-tree --stdin with pathspec' '
556 cat >expect <<-EOF &&
557 Third
558
559 dir/sub
560 Second
561
562 dir/sub
563 EOF
564 git rev-list master^ |
565 git diff-tree -r --stdin --name-only --format=%s dir >actual &&
566 test_cmp expect actual
567 '
568
569 test_expect_success 'show A B ... -- <pathspec>' '
570 # side touches dir/sub, file0, and file3
571 # master^ touches dir/sub, and file1
572 # master^^ touches dir/sub, file0, and file2
573 git show --name-only --format="<%s>" side master^ master^^ -- dir >actual &&
574 cat >expect <<-\EOF &&
575 <Side>
576
577 dir/sub
578 <Third>
579
580 dir/sub
581 <Second>
582
583 dir/sub
584 EOF
585 test_cmp expect actual
586 '
587
588 test_expect_success 'diff -I<regex>: setup' '
589 git checkout master &&
590 test_seq 50 >file0 &&
591 git commit -m "Set up -I<regex> test file" file0 &&
592 test_seq 50 | sed -e "s/13/ten and three/" -e "/7\$/d" >file0 &&
593 echo >>file0
594 '
595 test_expect_success 'diff -I<regex>' '
596 git diff --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
597 cat >expect <<-\EOF &&
598 diff --git a/file0 b/file0
599 --- a/file0
600 +++ b/file0
601 @@ -34,7 +31,6 @@
602 34
603 35
604 36
605 -37
606 38
607 39
608 40
609 EOF
610 compare_diff_patch expect actual
611 '
612
613 test_expect_success 'diff -I<regex> --stat' '
614 git diff --stat --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
615 cat >expect <<-\EOF &&
616 file0 | 1 -
617 1 file changed, 1 deletion(-)
618 EOF
619 test_cmp expect actual
620 '
621
622 test_expect_success 'diff -I<regex>: detect malformed regex' '
623 test_expect_code 129 git diff --ignore-matching-lines="^[124-9" 2>error &&
624 test_grep "invalid regex given to -I: " error
625 '
626
627 # check_prefix <patch> <src> <dst>
628 # check only lines with paths to avoid dependency on exact oid/contents
629 check_prefix () {
630 grep -E '^(diff|---|\+\+\+) ' "$1" >actual.paths &&
631 cat >expect <<-EOF &&
632 diff --git $2 $3
633 --- $2
634 +++ $3
635 EOF
636 test_cmp expect actual.paths
637 }
638
639 test_expect_success 'diff-files does not respect diff.noprefix' '
640 git -c diff.noprefix diff-files -p >actual &&
641 check_prefix actual a/file0 b/file0
642 '
643
644 test_expect_success 'diff-files respects --no-prefix' '
645 git diff-files -p --no-prefix >actual &&
646 check_prefix actual file0 file0
647 '
648
649 test_expect_success 'diff respects diff.noprefix' '
650 git -c diff.noprefix diff >actual &&
651 check_prefix actual file0 file0
652 '
653
654 test_expect_success 'diff --default-prefix overrides diff.noprefix' '
655 git -c diff.noprefix diff --default-prefix >actual &&
656 check_prefix actual a/file0 b/file0
657 '
658
659 test_expect_success 'diff respects diff.mnemonicprefix' '
660 git -c diff.mnemonicprefix diff >actual &&
661 check_prefix actual i/file0 w/file0
662 '
663
664 test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
665 git -c diff.mnemonicprefix diff --default-prefix >actual &&
666 check_prefix actual a/file0 b/file0
667 '
668
669 test_done