blame: allow --contents to work with non-HEAD commit
[git.git] / t / annotate-tests.sh
blobb35be20cf327249874a48d91681f381f873ad176
1 # This file isn't used as a test script directly, instead it is
2 # sourced from t8001-annotate.sh and t8002-blame.sh.
4 if test_have_prereq MINGW
5 then
6 sanitize_L () {
7 echo "$1" | sed 'sX\(^-L\|,\)\^\?/X&\\;*Xg'
9 else
10 sanitize_L () {
11 echo "$1"
15 check_count () {
16 head= &&
17 file='file' &&
18 options= &&
19 while :
21 case "$1" in
22 -h) head="$2"; shift; shift ;;
23 -f) file="$2"; shift; shift ;;
24 -L*) options="$options $(sanitize_L "$1")"; shift ;;
25 -*) options="$options $1"; shift ;;
26 *) break ;;
27 esac
28 done &&
29 echo "$PROG $options $file $head" >&4 &&
30 $PROG $options $file $head >actual &&
31 perl -e '
32 my %expect = (@ARGV);
33 my %count = map { $_ => 0 } keys %expect;
34 while (<STDIN>) {
35 if (/^[0-9a-f]+\t\(([^\t]+)\t/) {
36 my $author = $1;
37 for ($author) { s/^\s*//; s/\s*$//; }
38 $count{$author}++;
41 my $bad = 0;
42 while (my ($author, $count) = each %count) {
43 my $ok;
44 my $value = 0;
45 $value = $expect{$author} if defined $expect{$author};
46 if ($value != $count) {
47 $bad = 1;
48 $ok = "bad";
50 else {
51 $ok = "good";
53 print STDERR "Author $author (expected $value, attributed $count) $ok\n";
55 exit($bad);
56 ' "$@" <actual
59 get_progress_result () {
60 tr '\015' '\012' | tail -n 1
63 test_expect_success 'setup A lines' '
64 echo "1A quick brown fox jumps over the" >file &&
65 echo "lazy dog" >>file &&
66 git add file &&
67 GIT_AUTHOR_NAME="A" GIT_AUTHOR_EMAIL="A@test.git" \
68 git commit -a -m "Initial."
71 test_expect_success 'blame 1 author' '
72 check_count A 2
75 test_expect_success 'blame with --contents' '
76 check_count --contents=file A 2
79 test_expect_success 'blame with --contents changed' '
80 echo "1A quick brown fox jumps over the" >contents &&
81 echo "another lazy dog" >>contents &&
82 check_count --contents=contents A 1 "Not Committed Yet" 1
85 test_expect_success 'blame in a bare repo without starting commit' '
86 git clone --bare . bare.git &&
88 cd bare.git &&
89 check_count A 2
93 test_expect_success 'blame by tag objects' '
94 git tag -m "test tag" testTag &&
95 git tag -m "test tag #2" testTag2 testTag &&
96 check_count -h testTag A 2 &&
97 check_count -h testTag2 A 2
100 test_expect_success 'setup B lines' '
101 echo "2A quick brown fox jumps over the" >>file &&
102 echo "lazy dog" >>file &&
103 GIT_AUTHOR_NAME="B" GIT_AUTHOR_EMAIL="B@test.git" \
104 git commit -a -m "Second."
107 test_expect_success 'blame 2 authors' '
108 check_count A 2 B 2
111 test_expect_success 'blame with --contents and revision' '
112 check_count -h testTag --contents=file A 2 "Not Committed Yet" 2
115 test_expect_success 'setup B1 lines (branch1)' '
116 git checkout -b branch1 main &&
117 echo "3A slow green fox jumps into the" >>file &&
118 echo "well." >>file &&
119 GIT_AUTHOR_NAME="B1" GIT_AUTHOR_EMAIL="B1@test.git" \
120 git commit -a -m "Branch1-1"
123 test_expect_success 'blame 2 authors + 1 branch1 author' '
124 check_count A 2 B 2 B1 2
127 test_expect_success 'setup B2 lines (branch2)' '
128 git checkout -b branch2 main &&
129 sed -e "s/2A quick brown/4A quick brown lazy dog/" <file >file.new &&
130 mv file.new file &&
131 GIT_AUTHOR_NAME="B2" GIT_AUTHOR_EMAIL="B2@test.git" \
132 git commit -a -m "Branch2-1"
135 test_expect_success 'blame 2 authors + 1 branch2 author' '
136 check_count A 2 B 1 B2 1
139 test_expect_success 'merge branch1 & branch2' '
140 git merge branch1
143 test_expect_success 'blame 2 authors + 2 merged-in authors' '
144 check_count A 2 B 1 B1 2 B2 1
147 test_expect_success 'blame --first-parent blames merge for branch1' '
148 check_count --first-parent A 2 B 1 "A U Thor" 2 B2 1
151 test_expect_success 'blame ancestor' '
152 check_count -h main A 2 B 2
155 test_expect_success 'blame great-ancestor' '
156 check_count -h main^ A 2
159 test_expect_success 'setup evil merge' '
160 echo "evil merge." >>file &&
161 git commit -a --amend
164 test_expect_success 'blame evil merge' '
165 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1
168 test_expect_success 'blame huge graft' '
169 test_when_finished "git checkout branch2" &&
170 test_when_finished "rm -rf .git/info" &&
171 graft= &&
172 for i in 0 1 2
174 for j in 0 1 2 3 4 5 6 7 8 9
176 git checkout --orphan "$i$j" &&
177 printf "%s\n" "$i" "$j" >file &&
178 test_tick &&
179 GIT_AUTHOR_NAME=$i$j GIT_AUTHOR_EMAIL=$i$j@test.git \
180 git commit -a -m "$i$j" &&
181 commit=$(git rev-parse --verify HEAD) &&
182 graft="$graft$commit " || return 1
183 done
184 done &&
185 mkdir .git/info &&
186 printf "%s " $graft >.git/info/grafts &&
187 check_count -h 00 01 1 10 1
190 test_expect_success 'setup incomplete line' '
191 echo "incomplete" | tr -d "\\012" >>file &&
192 GIT_AUTHOR_NAME="C" GIT_AUTHOR_EMAIL="C@test.git" \
193 git commit -a -m "Incomplete"
196 test_expect_success 'blame incomplete line' '
197 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
200 test_expect_success 'setup edits' '
201 mv file file.orig &&
203 cat file.orig &&
204 echo
205 } | sed -e "s/^3A/99/" -e "/^1A/d" -e "/^incomplete/d" >file &&
206 echo "incomplete" | tr -d "\\012" >>file &&
207 GIT_AUTHOR_NAME="D" GIT_AUTHOR_EMAIL="D@test.git" \
208 git commit -a -m "edit"
211 test_expect_success 'blame edits' '
212 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1
215 test_expect_success 'setup obfuscated email' '
216 echo "No robots allowed" >file.new &&
217 cat file >>file.new &&
218 mv file.new file &&
219 GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" \
220 git commit -a -m "norobots"
223 test_expect_success 'blame obfuscated email' '
224 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
227 test_expect_success 'blame -L 1 (all)' '
228 check_count -L1 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
231 test_expect_success 'blame -L , (all)' '
232 check_count -L, A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
235 test_expect_success 'blame -L X (X to end)' '
236 check_count -L5 B1 1 C 1 D 1 "A U Thor" 1
239 test_expect_success 'blame -L X, (X to end)' '
240 check_count -L5, B1 1 C 1 D 1 "A U Thor" 1
243 test_expect_success 'blame -L ,Y (up to Y)' '
244 check_count -L,3 A 1 B2 1 E 1
247 test_expect_success 'blame -L X,X' '
248 check_count -L3,3 B2 1
251 test_expect_success 'blame -L X,Y' '
252 check_count -L3,6 B 1 B1 1 B2 1 D 1
255 test_expect_success 'blame -L Y,X (undocumented)' '
256 check_count -L6,3 B 1 B1 1 B2 1 D 1
259 test_expect_success 'blame -L -X' '
260 test_must_fail $PROG -L-1 file
263 test_expect_success 'blame -L 0' '
264 test_must_fail $PROG -L0 file
267 test_expect_success 'blame -L ,0' '
268 test_must_fail $PROG -L,0 file
271 test_expect_success 'blame -L ,+0' '
272 test_must_fail $PROG -L,+0 file
275 test_expect_success 'blame -L X,+0' '
276 test_must_fail $PROG -L1,+0 file
279 test_expect_success 'blame -L X,+1' '
280 check_count -L3,+1 B2 1
283 test_expect_success 'blame -L X,+N' '
284 check_count -L3,+4 B 1 B1 1 B2 1 D 1
287 test_expect_success 'blame -L ,-0' '
288 test_must_fail $PROG -L,-0 file
291 test_expect_success 'blame -L X,-0' '
292 test_must_fail $PROG -L1,-0 file
295 test_expect_success 'blame -L X,-1' '
296 check_count -L3,-1 B2 1
299 test_expect_success 'blame -L X,-N' '
300 check_count -L6,-4 B 1 B1 1 B2 1 D 1
303 test_expect_success 'blame -L /RE/ (RE to end)' '
304 check_count -L/evil/ C 1 "A U Thor" 1
307 test_expect_success 'blame -L /RE/,/RE2/' '
308 check_count -L/robot/,/green/ A 1 B 1 B2 1 D 1 E 1
311 test_expect_success 'blame -L X,/RE/' '
312 check_count -L5,/evil/ B1 1 D 1 "A U Thor" 1
315 test_expect_success 'blame -L /RE/,Y' '
316 check_count -L/99/,7 B1 1 D 1 "A U Thor" 1
319 test_expect_success 'blame -L /RE/,+N' '
320 check_count -L/99/,+3 B1 1 D 1 "A U Thor" 1
323 test_expect_success 'blame -L /RE/,-N' '
324 check_count -L/99/,-3 B 1 B2 1 D 1
327 # 'file' ends with an incomplete line, so 'wc' reports one fewer lines than
328 # git-blame sees, hence the last line is actually $(wc...)+1.
329 test_expect_success 'blame -L X (X == nlines)' '
330 n=$(expr $(wc -l <file) + 1) &&
331 check_count -L$n C 1
334 test_expect_success 'blame -L X (X == nlines + 1)' '
335 n=$(expr $(wc -l <file) + 2) &&
336 test_must_fail $PROG -L$n file
339 test_expect_success 'blame -L X (X > nlines)' '
340 test_must_fail $PROG -L12345 file
343 test_expect_success 'blame -L ,Y (Y == nlines)' '
344 n=$(expr $(wc -l <file) + 1) &&
345 check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
348 test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
349 n=$(expr $(wc -l <file) + 2) &&
350 check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
353 test_expect_success 'blame -L ,Y (Y > nlines)' '
354 check_count -L,12345 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
357 test_expect_success 'blame -L multiple (disjoint)' '
358 check_count -L2,3 -L6,7 A 1 B1 1 B2 1 "A U Thor" 1
361 test_expect_success 'blame -L multiple (disjoint: unordered)' '
362 check_count -L6,7 -L2,3 A 1 B1 1 B2 1 "A U Thor" 1
365 test_expect_success 'blame -L multiple (adjacent)' '
366 check_count -L2,3 -L4,5 A 1 B 1 B2 1 D 1
369 test_expect_success 'blame -L multiple (adjacent: unordered)' '
370 check_count -L4,5 -L2,3 A 1 B 1 B2 1 D 1
373 test_expect_success 'blame -L multiple (overlapping)' '
374 check_count -L2,4 -L3,5 A 1 B 1 B2 1 D 1
377 test_expect_success 'blame -L multiple (overlapping: unordered)' '
378 check_count -L3,5 -L2,4 A 1 B 1 B2 1 D 1
381 test_expect_success 'blame -L multiple (superset/subset)' '
382 check_count -L2,8 -L3,5 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
385 test_expect_success 'blame -L multiple (superset/subset: unordered)' '
386 check_count -L3,5 -L2,8 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
389 test_expect_success 'blame -L /RE/ (relative)' '
390 check_count -L3,3 -L/fox/ B1 1 B2 1 C 1 D 1 "A U Thor" 1
393 test_expect_success 'blame -L /RE/ (relative: no preceding range)' '
394 check_count -L/dog/ A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
397 test_expect_success 'blame -L /RE/ (relative: adjacent)' '
398 check_count -L1,1 -L/dog/,+1 A 1 E 1
401 test_expect_success 'blame -L /RE/ (relative: not found)' '
402 test_must_fail $PROG -L4,4 -L/dog/ file
405 test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
406 test_must_fail $PROG -L, -L/$/ file
409 test_expect_success 'blame -L ^/RE/ (absolute)' '
410 check_count -L3,3 -L^/dog/,+2 A 1 B2 1
413 test_expect_success 'blame -L ^/RE/ (absolute: no preceding range)' '
414 check_count -L^/dog/,+2 A 1 B2 1
417 test_expect_success 'blame -L ^/RE/ (absolute: not found)' '
418 test_must_fail $PROG -L4,4 -L^/tambourine/ file
421 test_expect_success 'blame -L ^/RE/ (absolute: end-of-file)' '
422 n=$(expr $(wc -l <file) + 1) &&
423 check_count -L$n -L^/$/,+2 A 1 C 1 E 1
426 test_expect_success 'setup -L :regex' '
427 tr Q "\\t" >hello.c <<-\EOF &&
428 int main(int argc, const char *argv[])
430 Qputs("hello");
433 git add hello.c &&
434 GIT_AUTHOR_NAME="F" GIT_AUTHOR_EMAIL="F@test.git" \
435 git commit -m "hello" &&
437 mv hello.c hello.orig &&
438 sed -e "/}/ {x; s/$/Qputs(\"goodbye\");/; G;}" <hello.orig |
439 tr Q "\\t" >hello.c &&
440 GIT_AUTHOR_NAME="G" GIT_AUTHOR_EMAIL="G@test.git" \
441 git commit -a -m "goodbye" &&
443 mv hello.c hello.orig &&
444 echo "#include <stdio.h>" >hello.c &&
445 cat hello.orig >>hello.c &&
446 tr Q "\\t" >>hello.c <<-\EOF &&
447 void mail()
449 Qputs("mail");
452 GIT_AUTHOR_NAME="H" GIT_AUTHOR_EMAIL="H@test.git" \
453 git commit -a -m "mail"
456 test_expect_success 'blame -L :literal' '
457 check_count -f hello.c -L:main F 4 G 1
460 test_expect_success 'blame -L :regex' '
461 check_count -f hello.c "-L:m[a-z][a-z]l" H 4
464 test_expect_success 'blame -L :nomatch' '
465 test_must_fail $PROG -L:nomatch hello.c
468 test_expect_success 'blame -L :RE (relative)' '
469 check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
472 test_expect_success 'blame -L :RE (relative: no preceding range)' '
473 check_count -f hello.c -L:ma.. F 4 G 1
476 test_expect_success 'blame -L :RE (relative: not found)' '
477 test_must_fail $PROG -L3,3 -L:tambourine hello.c
480 test_expect_success 'blame -L :RE (relative: end-of-file)' '
481 test_must_fail $PROG -L, -L:main hello.c
484 test_expect_success 'blame -L ^:RE (absolute)' '
485 check_count -f hello.c -L3,3 -L^:ma.. F 4 G 1
488 test_expect_success 'blame -L ^:RE (absolute: no preceding range)' '
489 check_count -f hello.c -L^:ma.. F 4 G 1
492 test_expect_success 'blame -L ^:RE (absolute: not found)' '
493 test_must_fail $PROG -L4,4 -L^:tambourine hello.c
496 test_expect_success 'blame -L ^:RE (absolute: end-of-file)' '
497 n=$(printf "%d" $(wc -l <hello.c)) &&
498 check_count -f hello.c -L$n -L^:ma.. F 4 G 1 H 1
501 test_expect_success 'blame -L :funcname with userdiff driver' '
502 cat >file.template <<-\EOF &&
503 DO NOT MATCH THIS LINE
504 function RIGHT(a, b) result(c)
505 AS THE DEFAULT DRIVER WOULD
507 integer, intent(in) :: ChangeMe
510 fortran_file=file.f03 &&
511 test_when_finished "rm .gitattributes" &&
512 echo "$fortran_file diff=fortran" >.gitattributes &&
514 test_commit --author "A <A@test.git>" \
515 "add" "$fortran_file" \
516 "$(cat file.template)" &&
517 test_commit --author "B <B@test.git>" \
518 "change" "$fortran_file" \
519 "$(cat file.template | sed -e s/ChangeMe/IWasChanged/)" &&
520 check_count -f "$fortran_file" -L:RIGHT A 3 B 1
523 test_expect_success 'setup incremental' '
525 GIT_AUTHOR_NAME=I &&
526 export GIT_AUTHOR_NAME &&
527 GIT_AUTHOR_EMAIL=I@test.git &&
528 export GIT_AUTHOR_EMAIL &&
529 >incremental &&
530 git add incremental &&
531 git commit -m "step 0" &&
532 printf "partial" >>incremental &&
533 git commit -a -m "step 0.5" &&
534 echo >>incremental &&
535 git commit -a -m "step 1"
539 test_expect_success 'blame empty' '
540 check_count -h HEAD^^ -f incremental
543 test_expect_success 'blame -L 0 empty' '
544 test_must_fail $PROG -L0 incremental HEAD^^
547 test_expect_success 'blame -L 1 empty' '
548 test_must_fail $PROG -L1 incremental HEAD^^
551 test_expect_success 'blame -L 2 empty' '
552 test_must_fail $PROG -L2 incremental HEAD^^
555 test_expect_success 'blame half' '
556 check_count -h HEAD^ -f incremental I 1
559 test_expect_success 'blame -L 0 half' '
560 test_must_fail $PROG -L0 incremental HEAD^
563 test_expect_success 'blame -L 1 half' '
564 check_count -h HEAD^ -f incremental -L1 I 1
567 test_expect_success 'blame -L 2 half' '
568 test_must_fail $PROG -L2 incremental HEAD^
571 test_expect_success 'blame -L 3 half' '
572 test_must_fail $PROG -L3 incremental HEAD^
575 test_expect_success 'blame full' '
576 check_count -f incremental I 1
579 test_expect_success 'blame -L 0 full' '
580 test_must_fail $PROG -L0 incremental
583 test_expect_success 'blame -L 1 full' '
584 check_count -f incremental -L1 I 1
587 test_expect_success 'blame -L 2 full' '
588 test_must_fail $PROG -L2 incremental
591 test_expect_success 'blame -L 3 full' '
592 test_must_fail $PROG -L3 incremental
595 test_expect_success 'blame -L' '
596 test_must_fail $PROG -L file
599 test_expect_success 'blame -L X,+' '
600 test_must_fail $PROG -L1,+ file
603 test_expect_success 'blame -L X,-' '
604 test_must_fail $PROG -L1,- file
607 test_expect_success 'blame -L X (non-numeric X)' '
608 test_must_fail $PROG -LX file
611 test_expect_success 'blame -L X,Y (non-numeric Y)' '
612 test_must_fail $PROG -L1,Y file
615 test_expect_success 'blame -L X,+N (non-numeric N)' '
616 test_must_fail $PROG -L1,+N file
619 test_expect_success 'blame -L X,-N (non-numeric N)' '
620 test_must_fail $PROG -L1,-N file
623 test_expect_success 'blame -L ,^/RE/' '
624 test_must_fail $PROG -L1,^/99/ file
627 test_expect_success 'blame progress on a full file' '
628 cat >expect <<-\EOF &&
629 Blaming lines: 100% (10/10), done.
632 GIT_PROGRESS_DELAY=0 \
633 git blame --progress hello.c 2>stderr &&
635 get_progress_result <stderr >actual &&
636 test_cmp expect actual
639 test_expect_success 'blame progress on a single range' '
640 cat >expect <<-\EOF &&
641 Blaming lines: 100% (4/4), done.
644 GIT_PROGRESS_DELAY=0 \
645 git blame --progress -L 3,6 hello.c 2>stderr &&
647 get_progress_result <stderr >actual &&
648 test_cmp expect actual
651 test_expect_success 'blame progress on multiple ranges' '
652 cat >expect <<-\EOF &&
653 Blaming lines: 100% (7/7), done.
656 GIT_PROGRESS_DELAY=0 \
657 git blame --progress -L 3,6 -L 8,10 hello.c 2>stderr &&
659 get_progress_result <stderr >actual &&
660 test_cmp expect actual