Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / t / annotate-tests.sh
blobcc01d891504eb117eb9c367ebc4e89b516157012
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 in a bare repo without starting commit' '
76 git clone --bare . bare.git &&
78 cd bare.git &&
79 check_count A 2
83 test_expect_success 'blame by tag objects' '
84 git tag -m "test tag" testTag &&
85 git tag -m "test tag #2" testTag2 testTag &&
86 check_count -h testTag A 2 &&
87 check_count -h testTag2 A 2
90 test_expect_success 'setup B lines' '
91 echo "2A quick brown fox jumps over the" >>file &&
92 echo "lazy dog" >>file &&
93 GIT_AUTHOR_NAME="B" GIT_AUTHOR_EMAIL="B@test.git" \
94 git commit -a -m "Second."
97 test_expect_success 'blame 2 authors' '
98 check_count A 2 B 2
101 test_expect_success 'setup B1 lines (branch1)' '
102 git checkout -b branch1 main &&
103 echo "3A slow green fox jumps into the" >>file &&
104 echo "well." >>file &&
105 GIT_AUTHOR_NAME="B1" GIT_AUTHOR_EMAIL="B1@test.git" \
106 git commit -a -m "Branch1-1"
109 test_expect_success 'blame 2 authors + 1 branch1 author' '
110 check_count A 2 B 2 B1 2
113 test_expect_success 'setup B2 lines (branch2)' '
114 git checkout -b branch2 main &&
115 sed -e "s/2A quick brown/4A quick brown lazy dog/" <file >file.new &&
116 mv file.new file &&
117 GIT_AUTHOR_NAME="B2" GIT_AUTHOR_EMAIL="B2@test.git" \
118 git commit -a -m "Branch2-1"
121 test_expect_success 'blame 2 authors + 1 branch2 author' '
122 check_count A 2 B 1 B2 1
125 test_expect_success 'merge branch1 & branch2' '
126 git merge branch1
129 test_expect_success 'blame 2 authors + 2 merged-in authors' '
130 check_count A 2 B 1 B1 2 B2 1
133 test_expect_success 'blame --first-parent blames merge for branch1' '
134 check_count --first-parent A 2 B 1 "A U Thor" 2 B2 1
137 test_expect_success 'blame ancestor' '
138 check_count -h main A 2 B 2
141 test_expect_success 'blame great-ancestor' '
142 check_count -h main^ A 2
145 test_expect_success 'setup evil merge' '
146 echo "evil merge." >>file &&
147 git commit -a --amend
150 test_expect_success 'blame evil merge' '
151 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1
154 test_expect_success 'blame huge graft' '
155 test_when_finished "git checkout branch2" &&
156 test_when_finished "rm -f .git/info/grafts" &&
157 graft= &&
158 for i in 0 1 2
160 for j in 0 1 2 3 4 5 6 7 8 9
162 git checkout --orphan "$i$j" &&
163 printf "%s\n" "$i" "$j" >file &&
164 test_tick &&
165 GIT_AUTHOR_NAME=$i$j GIT_AUTHOR_EMAIL=$i$j@test.git \
166 git commit -a -m "$i$j" &&
167 commit=$(git rev-parse --verify HEAD) &&
168 graft="$graft$commit " || return 1
169 done
170 done &&
171 printf "%s " $graft >.git/info/grafts &&
172 check_count -h 00 01 1 10 1
175 test_expect_success 'setup incomplete line' '
176 echo "incomplete" | tr -d "\\012" >>file &&
177 GIT_AUTHOR_NAME="C" GIT_AUTHOR_EMAIL="C@test.git" \
178 git commit -a -m "Incomplete"
181 test_expect_success 'blame incomplete line' '
182 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
185 test_expect_success 'setup edits' '
186 mv file file.orig &&
188 cat file.orig &&
189 echo
190 } | sed -e "s/^3A/99/" -e "/^1A/d" -e "/^incomplete/d" >file &&
191 echo "incomplete" | tr -d "\\012" >>file &&
192 GIT_AUTHOR_NAME="D" GIT_AUTHOR_EMAIL="D@test.git" \
193 git commit -a -m "edit"
196 test_expect_success 'blame edits' '
197 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1
200 test_expect_success 'setup obfuscated email' '
201 echo "No robots allowed" >file.new &&
202 cat file >>file.new &&
203 mv file.new file &&
204 GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" \
205 git commit -a -m "norobots"
208 test_expect_success 'blame obfuscated email' '
209 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
212 test_expect_success 'blame -L 1 (all)' '
213 check_count -L1 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
216 test_expect_success 'blame -L , (all)' '
217 check_count -L, A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
220 test_expect_success 'blame -L X (X to end)' '
221 check_count -L5 B1 1 C 1 D 1 "A U Thor" 1
224 test_expect_success 'blame -L X, (X to end)' '
225 check_count -L5, B1 1 C 1 D 1 "A U Thor" 1
228 test_expect_success 'blame -L ,Y (up to Y)' '
229 check_count -L,3 A 1 B2 1 E 1
232 test_expect_success 'blame -L X,X' '
233 check_count -L3,3 B2 1
236 test_expect_success 'blame -L X,Y' '
237 check_count -L3,6 B 1 B1 1 B2 1 D 1
240 test_expect_success 'blame -L Y,X (undocumented)' '
241 check_count -L6,3 B 1 B1 1 B2 1 D 1
244 test_expect_success 'blame -L -X' '
245 test_must_fail $PROG -L-1 file
248 test_expect_success 'blame -L 0' '
249 test_must_fail $PROG -L0 file
252 test_expect_success 'blame -L ,0' '
253 test_must_fail $PROG -L,0 file
256 test_expect_success 'blame -L ,+0' '
257 test_must_fail $PROG -L,+0 file
260 test_expect_success 'blame -L X,+0' '
261 test_must_fail $PROG -L1,+0 file
264 test_expect_success 'blame -L X,+1' '
265 check_count -L3,+1 B2 1
268 test_expect_success 'blame -L X,+N' '
269 check_count -L3,+4 B 1 B1 1 B2 1 D 1
272 test_expect_success 'blame -L ,-0' '
273 test_must_fail $PROG -L,-0 file
276 test_expect_success 'blame -L X,-0' '
277 test_must_fail $PROG -L1,-0 file
280 test_expect_success 'blame -L X,-1' '
281 check_count -L3,-1 B2 1
284 test_expect_success 'blame -L X,-N' '
285 check_count -L6,-4 B 1 B1 1 B2 1 D 1
288 test_expect_success 'blame -L /RE/ (RE to end)' '
289 check_count -L/evil/ C 1 "A U Thor" 1
292 test_expect_success 'blame -L /RE/,/RE2/' '
293 check_count -L/robot/,/green/ A 1 B 1 B2 1 D 1 E 1
296 test_expect_success 'blame -L X,/RE/' '
297 check_count -L5,/evil/ B1 1 D 1 "A U Thor" 1
300 test_expect_success 'blame -L /RE/,Y' '
301 check_count -L/99/,7 B1 1 D 1 "A U Thor" 1
304 test_expect_success 'blame -L /RE/,+N' '
305 check_count -L/99/,+3 B1 1 D 1 "A U Thor" 1
308 test_expect_success 'blame -L /RE/,-N' '
309 check_count -L/99/,-3 B 1 B2 1 D 1
312 # 'file' ends with an incomplete line, so 'wc' reports one fewer lines than
313 # git-blame sees, hence the last line is actually $(wc...)+1.
314 test_expect_success 'blame -L X (X == nlines)' '
315 n=$(expr $(wc -l <file) + 1) &&
316 check_count -L$n C 1
319 test_expect_success 'blame -L X (X == nlines + 1)' '
320 n=$(expr $(wc -l <file) + 2) &&
321 test_must_fail $PROG -L$n file
324 test_expect_success 'blame -L X (X > nlines)' '
325 test_must_fail $PROG -L12345 file
328 test_expect_success 'blame -L ,Y (Y == nlines)' '
329 n=$(expr $(wc -l <file) + 1) &&
330 check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
333 test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
334 n=$(expr $(wc -l <file) + 2) &&
335 check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
338 test_expect_success 'blame -L ,Y (Y > nlines)' '
339 check_count -L,12345 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
342 test_expect_success 'blame -L multiple (disjoint)' '
343 check_count -L2,3 -L6,7 A 1 B1 1 B2 1 "A U Thor" 1
346 test_expect_success 'blame -L multiple (disjoint: unordered)' '
347 check_count -L6,7 -L2,3 A 1 B1 1 B2 1 "A U Thor" 1
350 test_expect_success 'blame -L multiple (adjacent)' '
351 check_count -L2,3 -L4,5 A 1 B 1 B2 1 D 1
354 test_expect_success 'blame -L multiple (adjacent: unordered)' '
355 check_count -L4,5 -L2,3 A 1 B 1 B2 1 D 1
358 test_expect_success 'blame -L multiple (overlapping)' '
359 check_count -L2,4 -L3,5 A 1 B 1 B2 1 D 1
362 test_expect_success 'blame -L multiple (overlapping: unordered)' '
363 check_count -L3,5 -L2,4 A 1 B 1 B2 1 D 1
366 test_expect_success 'blame -L multiple (superset/subset)' '
367 check_count -L2,8 -L3,5 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
370 test_expect_success 'blame -L multiple (superset/subset: unordered)' '
371 check_count -L3,5 -L2,8 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
374 test_expect_success 'blame -L /RE/ (relative)' '
375 check_count -L3,3 -L/fox/ B1 1 B2 1 C 1 D 1 "A U Thor" 1
378 test_expect_success 'blame -L /RE/ (relative: no preceding range)' '
379 check_count -L/dog/ A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
382 test_expect_success 'blame -L /RE/ (relative: adjacent)' '
383 check_count -L1,1 -L/dog/,+1 A 1 E 1
386 test_expect_success 'blame -L /RE/ (relative: not found)' '
387 test_must_fail $PROG -L4,4 -L/dog/ file
390 test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
391 test_must_fail $PROG -L, -L/$/ file
394 test_expect_success 'blame -L ^/RE/ (absolute)' '
395 check_count -L3,3 -L^/dog/,+2 A 1 B2 1
398 test_expect_success 'blame -L ^/RE/ (absolute: no preceding range)' '
399 check_count -L^/dog/,+2 A 1 B2 1
402 test_expect_success 'blame -L ^/RE/ (absolute: not found)' '
403 test_must_fail $PROG -L4,4 -L^/tambourine/ file
406 test_expect_success 'blame -L ^/RE/ (absolute: end-of-file)' '
407 n=$(expr $(wc -l <file) + 1) &&
408 check_count -L$n -L^/$/,+2 A 1 C 1 E 1
411 test_expect_success 'setup -L :regex' '
412 tr Q "\\t" >hello.c <<-\EOF &&
413 int main(int argc, const char *argv[])
415 Qputs("hello");
418 git add hello.c &&
419 GIT_AUTHOR_NAME="F" GIT_AUTHOR_EMAIL="F@test.git" \
420 git commit -m "hello" &&
422 mv hello.c hello.orig &&
423 sed -e "/}/ {x; s/$/Qputs(\"goodbye\");/; G;}" <hello.orig |
424 tr Q "\\t" >hello.c &&
425 GIT_AUTHOR_NAME="G" GIT_AUTHOR_EMAIL="G@test.git" \
426 git commit -a -m "goodbye" &&
428 mv hello.c hello.orig &&
429 echo "#include <stdio.h>" >hello.c &&
430 cat hello.orig >>hello.c &&
431 tr Q "\\t" >>hello.c <<-\EOF &&
432 void mail()
434 Qputs("mail");
437 GIT_AUTHOR_NAME="H" GIT_AUTHOR_EMAIL="H@test.git" \
438 git commit -a -m "mail"
441 test_expect_success 'blame -L :literal' '
442 check_count -f hello.c -L:main F 4 G 1
445 test_expect_success 'blame -L :regex' '
446 check_count -f hello.c "-L:m[a-z][a-z]l" H 4
449 test_expect_success 'blame -L :nomatch' '
450 test_must_fail $PROG -L:nomatch hello.c
453 test_expect_success 'blame -L :RE (relative)' '
454 check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
457 test_expect_success 'blame -L :RE (relative: no preceding range)' '
458 check_count -f hello.c -L:ma.. F 4 G 1
461 test_expect_success 'blame -L :RE (relative: not found)' '
462 test_must_fail $PROG -L3,3 -L:tambourine hello.c
465 test_expect_success 'blame -L :RE (relative: end-of-file)' '
466 test_must_fail $PROG -L, -L:main hello.c
469 test_expect_success 'blame -L ^:RE (absolute)' '
470 check_count -f hello.c -L3,3 -L^:ma.. F 4 G 1
473 test_expect_success 'blame -L ^:RE (absolute: no preceding range)' '
474 check_count -f hello.c -L^:ma.. F 4 G 1
477 test_expect_success 'blame -L ^:RE (absolute: not found)' '
478 test_must_fail $PROG -L4,4 -L^:tambourine hello.c
481 test_expect_success 'blame -L ^:RE (absolute: end-of-file)' '
482 n=$(printf "%d" $(wc -l <hello.c)) &&
483 check_count -f hello.c -L$n -L^:ma.. F 4 G 1 H 1
486 test_expect_success 'blame -L :funcname with userdiff driver' '
487 cat >file.template <<-\EOF &&
488 DO NOT MATCH THIS LINE
489 function RIGHT(a, b) result(c)
490 AS THE DEFAULT DRIVER WOULD
492 integer, intent(in) :: ChangeMe
495 fortran_file=file.f03 &&
496 test_when_finished "rm .gitattributes" &&
497 echo "$fortran_file diff=fortran" >.gitattributes &&
499 test_commit --author "A <A@test.git>" \
500 "add" "$fortran_file" \
501 "$(cat file.template)" &&
502 test_commit --author "B <B@test.git>" \
503 "change" "$fortran_file" \
504 "$(cat file.template | sed -e s/ChangeMe/IWasChanged/)" &&
505 check_count -f "$fortran_file" -L:RIGHT A 3 B 1
508 test_expect_success 'setup incremental' '
510 GIT_AUTHOR_NAME=I &&
511 export GIT_AUTHOR_NAME &&
512 GIT_AUTHOR_EMAIL=I@test.git &&
513 export GIT_AUTHOR_EMAIL &&
514 >incremental &&
515 git add incremental &&
516 git commit -m "step 0" &&
517 printf "partial" >>incremental &&
518 git commit -a -m "step 0.5" &&
519 echo >>incremental &&
520 git commit -a -m "step 1"
524 test_expect_success 'blame empty' '
525 check_count -h HEAD^^ -f incremental
528 test_expect_success 'blame -L 0 empty' '
529 test_must_fail $PROG -L0 incremental HEAD^^
532 test_expect_success 'blame -L 1 empty' '
533 test_must_fail $PROG -L1 incremental HEAD^^
536 test_expect_success 'blame -L 2 empty' '
537 test_must_fail $PROG -L2 incremental HEAD^^
540 test_expect_success 'blame half' '
541 check_count -h HEAD^ -f incremental I 1
544 test_expect_success 'blame -L 0 half' '
545 test_must_fail $PROG -L0 incremental HEAD^
548 test_expect_success 'blame -L 1 half' '
549 check_count -h HEAD^ -f incremental -L1 I 1
552 test_expect_success 'blame -L 2 half' '
553 test_must_fail $PROG -L2 incremental HEAD^
556 test_expect_success 'blame -L 3 half' '
557 test_must_fail $PROG -L3 incremental HEAD^
560 test_expect_success 'blame full' '
561 check_count -f incremental I 1
564 test_expect_success 'blame -L 0 full' '
565 test_must_fail $PROG -L0 incremental
568 test_expect_success 'blame -L 1 full' '
569 check_count -f incremental -L1 I 1
572 test_expect_success 'blame -L 2 full' '
573 test_must_fail $PROG -L2 incremental
576 test_expect_success 'blame -L 3 full' '
577 test_must_fail $PROG -L3 incremental
580 test_expect_success 'blame -L' '
581 test_must_fail $PROG -L file
584 test_expect_success 'blame -L X,+' '
585 test_must_fail $PROG -L1,+ file
588 test_expect_success 'blame -L X,-' '
589 test_must_fail $PROG -L1,- file
592 test_expect_success 'blame -L X (non-numeric X)' '
593 test_must_fail $PROG -LX file
596 test_expect_success 'blame -L X,Y (non-numeric Y)' '
597 test_must_fail $PROG -L1,Y file
600 test_expect_success 'blame -L X,+N (non-numeric N)' '
601 test_must_fail $PROG -L1,+N file
604 test_expect_success 'blame -L X,-N (non-numeric N)' '
605 test_must_fail $PROG -L1,-N file
608 test_expect_success 'blame -L ,^/RE/' '
609 test_must_fail $PROG -L1,^/99/ file
612 test_expect_success 'blame progress on a full file' '
613 cat >expect <<-\EOF &&
614 Blaming lines: 100% (10/10), done.
617 GIT_PROGRESS_DELAY=0 \
618 git blame --progress hello.c 2>stderr &&
620 get_progress_result <stderr >actual &&
621 test_cmp expect actual
624 test_expect_success 'blame progress on a single range' '
625 cat >expect <<-\EOF &&
626 Blaming lines: 100% (4/4), done.
629 GIT_PROGRESS_DELAY=0 \
630 git blame --progress -L 3,6 hello.c 2>stderr &&
632 get_progress_result <stderr >actual &&
633 test_cmp expect actual
636 test_expect_success 'blame progress on multiple ranges' '
637 cat >expect <<-\EOF &&
638 Blaming lines: 100% (7/7), done.
641 GIT_PROGRESS_DELAY=0 \
642 git blame --progress -L 3,6 -L 8,10 hello.c 2>stderr &&
644 get_progress_result <stderr >actual &&
645 test_cmp expect actual