]> git.ipfire.org Git - thirdparty/git.git/blob - t/annotate-tests.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / annotate-tests.sh
1 # This file isn't used as a test script directly, instead it is
2 # sourced from t8001-annotate.sh and t8002-blame.sh.
3
4 if test_have_prereq MINGW
5 then
6 sanitize_L () {
7 echo "$1" | sed 'sX\(^-L\|,\)\^\?/X&\\;*Xg'
8 }
9 else
10 sanitize_L () {
11 echo "$1"
12 }
13 fi
14
15 check_count () {
16 head= &&
17 file='file' &&
18 options= &&
19 while :
20 do
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}++;
39 }
40 }
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";
49 }
50 else {
51 $ok = "good";
52 }
53 print STDERR "Author $author (expected $value, attributed $count) $ok\n";
54 }
55 exit($bad);
56 ' "$@" <actual
57 }
58
59 get_progress_result () {
60 tr '\015' '\012' | tail -n 1
61 }
62
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."
69 '
70
71 test_expect_success 'blame 1 author' '
72 check_count A 2
73 '
74
75 test_expect_success 'blame in a bare repo without starting commit' '
76 git clone --bare . bare.git &&
77 (
78 cd bare.git &&
79 check_count A 2
80 )
81 '
82
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
88 '
89
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."
95 '
96
97 test_expect_success 'blame 2 authors' '
98 check_count A 2 B 2
99 '
100
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"
107 '
108
109 test_expect_success 'blame 2 authors + 1 branch1 author' '
110 check_count A 2 B 2 B1 2
111 '
112
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"
119 '
120
121 test_expect_success 'blame 2 authors + 1 branch2 author' '
122 check_count A 2 B 1 B2 1
123 '
124
125 test_expect_success 'merge branch1 & branch2' '
126 git merge branch1
127 '
128
129 test_expect_success 'blame 2 authors + 2 merged-in authors' '
130 check_count A 2 B 1 B1 2 B2 1
131 '
132
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
135 '
136
137 test_expect_success 'blame ancestor' '
138 check_count -h main A 2 B 2
139 '
140
141 test_expect_success 'blame great-ancestor' '
142 check_count -h main^ A 2
143 '
144
145 test_expect_success 'setup evil merge' '
146 echo "evil merge." >>file &&
147 git commit -a --amend
148 '
149
150 test_expect_success 'blame evil merge' '
151 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1
152 '
153
154 test_expect_success 'blame huge graft' '
155 test_when_finished "git checkout branch2" &&
156 test_when_finished "rm -rf .git/info" &&
157 graft= &&
158 for i in 0 1 2
159 do
160 for j in 0 1 2 3 4 5 6 7 8 9
161 do
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 mkdir .git/info &&
172 printf "%s " $graft >.git/info/grafts &&
173 check_count -h 00 01 1 10 1
174 '
175
176 test_expect_success 'setup incomplete line' '
177 echo "incomplete" | tr -d "\\012" >>file &&
178 GIT_AUTHOR_NAME="C" GIT_AUTHOR_EMAIL="C@test.git" \
179 git commit -a -m "Incomplete"
180 '
181
182 test_expect_success 'blame incomplete line' '
183 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
184 '
185
186 test_expect_success 'setup edits' '
187 mv file file.orig &&
188 {
189 cat file.orig &&
190 echo
191 } | sed -e "s/^3A/99/" -e "/^1A/d" -e "/^incomplete/d" >file &&
192 echo "incomplete" | tr -d "\\012" >>file &&
193 GIT_AUTHOR_NAME="D" GIT_AUTHOR_EMAIL="D@test.git" \
194 git commit -a -m "edit"
195 '
196
197 test_expect_success 'blame edits' '
198 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1
199 '
200
201 test_expect_success 'setup obfuscated email' '
202 echo "No robots allowed" >file.new &&
203 cat file >>file.new &&
204 mv file.new file &&
205 GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" \
206 git commit -a -m "norobots"
207 '
208
209 test_expect_success 'blame obfuscated email' '
210 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
211 '
212
213 test_expect_success 'blame -L 1 (all)' '
214 check_count -L1 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
215 '
216
217 test_expect_success 'blame -L , (all)' '
218 check_count -L, A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
219 '
220
221 test_expect_success 'blame -L X (X to end)' '
222 check_count -L5 B1 1 C 1 D 1 "A U Thor" 1
223 '
224
225 test_expect_success 'blame -L X, (X to end)' '
226 check_count -L5, B1 1 C 1 D 1 "A U Thor" 1
227 '
228
229 test_expect_success 'blame -L ,Y (up to Y)' '
230 check_count -L,3 A 1 B2 1 E 1
231 '
232
233 test_expect_success 'blame -L X,X' '
234 check_count -L3,3 B2 1
235 '
236
237 test_expect_success 'blame -L X,Y' '
238 check_count -L3,6 B 1 B1 1 B2 1 D 1
239 '
240
241 test_expect_success 'blame -L Y,X (undocumented)' '
242 check_count -L6,3 B 1 B1 1 B2 1 D 1
243 '
244
245 test_expect_success 'blame -L -X' '
246 test_must_fail $PROG -L-1 file
247 '
248
249 test_expect_success 'blame -L 0' '
250 test_must_fail $PROG -L0 file
251 '
252
253 test_expect_success 'blame -L ,0' '
254 test_must_fail $PROG -L,0 file
255 '
256
257 test_expect_success 'blame -L ,+0' '
258 test_must_fail $PROG -L,+0 file
259 '
260
261 test_expect_success 'blame -L X,+0' '
262 test_must_fail $PROG -L1,+0 file
263 '
264
265 test_expect_success 'blame -L X,+1' '
266 check_count -L3,+1 B2 1
267 '
268
269 test_expect_success 'blame -L X,+N' '
270 check_count -L3,+4 B 1 B1 1 B2 1 D 1
271 '
272
273 test_expect_success 'blame -L ,-0' '
274 test_must_fail $PROG -L,-0 file
275 '
276
277 test_expect_success 'blame -L X,-0' '
278 test_must_fail $PROG -L1,-0 file
279 '
280
281 test_expect_success 'blame -L X,-1' '
282 check_count -L3,-1 B2 1
283 '
284
285 test_expect_success 'blame -L X,-N' '
286 check_count -L6,-4 B 1 B1 1 B2 1 D 1
287 '
288
289 test_expect_success 'blame -L /RE/ (RE to end)' '
290 check_count -L/evil/ C 1 "A U Thor" 1
291 '
292
293 test_expect_success 'blame -L /RE/,/RE2/' '
294 check_count -L/robot/,/green/ A 1 B 1 B2 1 D 1 E 1
295 '
296
297 test_expect_success 'blame -L X,/RE/' '
298 check_count -L5,/evil/ B1 1 D 1 "A U Thor" 1
299 '
300
301 test_expect_success 'blame -L /RE/,Y' '
302 check_count -L/99/,7 B1 1 D 1 "A U Thor" 1
303 '
304
305 test_expect_success 'blame -L /RE/,+N' '
306 check_count -L/99/,+3 B1 1 D 1 "A U Thor" 1
307 '
308
309 test_expect_success 'blame -L /RE/,-N' '
310 check_count -L/99/,-3 B 1 B2 1 D 1
311 '
312
313 # 'file' ends with an incomplete line, so 'wc' reports one fewer lines than
314 # git-blame sees, hence the last line is actually $(wc...)+1.
315 test_expect_success 'blame -L X (X == nlines)' '
316 n=$(expr $(wc -l <file) + 1) &&
317 check_count -L$n C 1
318 '
319
320 test_expect_success 'blame -L X (X == nlines + 1)' '
321 n=$(expr $(wc -l <file) + 2) &&
322 test_must_fail $PROG -L$n file
323 '
324
325 test_expect_success 'blame -L X (X > nlines)' '
326 test_must_fail $PROG -L12345 file
327 '
328
329 test_expect_success 'blame -L ,Y (Y == nlines)' '
330 n=$(expr $(wc -l <file) + 1) &&
331 check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
332 '
333
334 test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
335 n=$(expr $(wc -l <file) + 2) &&
336 check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
337 '
338
339 test_expect_success 'blame -L ,Y (Y > nlines)' '
340 check_count -L,12345 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
341 '
342
343 test_expect_success 'blame -L multiple (disjoint)' '
344 check_count -L2,3 -L6,7 A 1 B1 1 B2 1 "A U Thor" 1
345 '
346
347 test_expect_success 'blame -L multiple (disjoint: unordered)' '
348 check_count -L6,7 -L2,3 A 1 B1 1 B2 1 "A U Thor" 1
349 '
350
351 test_expect_success 'blame -L multiple (adjacent)' '
352 check_count -L2,3 -L4,5 A 1 B 1 B2 1 D 1
353 '
354
355 test_expect_success 'blame -L multiple (adjacent: unordered)' '
356 check_count -L4,5 -L2,3 A 1 B 1 B2 1 D 1
357 '
358
359 test_expect_success 'blame -L multiple (overlapping)' '
360 check_count -L2,4 -L3,5 A 1 B 1 B2 1 D 1
361 '
362
363 test_expect_success 'blame -L multiple (overlapping: unordered)' '
364 check_count -L3,5 -L2,4 A 1 B 1 B2 1 D 1
365 '
366
367 test_expect_success 'blame -L multiple (superset/subset)' '
368 check_count -L2,8 -L3,5 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
369 '
370
371 test_expect_success 'blame -L multiple (superset/subset: unordered)' '
372 check_count -L3,5 -L2,8 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
373 '
374
375 test_expect_success 'blame -L /RE/ (relative)' '
376 check_count -L3,3 -L/fox/ B1 1 B2 1 C 1 D 1 "A U Thor" 1
377 '
378
379 test_expect_success 'blame -L /RE/ (relative: no preceding range)' '
380 check_count -L/dog/ A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
381 '
382
383 test_expect_success 'blame -L /RE/ (relative: adjacent)' '
384 check_count -L1,1 -L/dog/,+1 A 1 E 1
385 '
386
387 test_expect_success 'blame -L /RE/ (relative: not found)' '
388 test_must_fail $PROG -L4,4 -L/dog/ file
389 '
390
391 test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
392 test_must_fail $PROG -L, -L/$/ file
393 '
394
395 test_expect_success 'blame -L ^/RE/ (absolute)' '
396 check_count -L3,3 -L^/dog/,+2 A 1 B2 1
397 '
398
399 test_expect_success 'blame -L ^/RE/ (absolute: no preceding range)' '
400 check_count -L^/dog/,+2 A 1 B2 1
401 '
402
403 test_expect_success 'blame -L ^/RE/ (absolute: not found)' '
404 test_must_fail $PROG -L4,4 -L^/tambourine/ file
405 '
406
407 test_expect_success 'blame -L ^/RE/ (absolute: end-of-file)' '
408 n=$(expr $(wc -l <file) + 1) &&
409 check_count -L$n -L^/$/,+2 A 1 C 1 E 1
410 '
411
412 test_expect_success 'setup -L :regex' '
413 tr Q "\\t" >hello.c <<-\EOF &&
414 int main(int argc, const char *argv[])
415 {
416 Qputs("hello");
417 }
418 EOF
419 git add hello.c &&
420 GIT_AUTHOR_NAME="F" GIT_AUTHOR_EMAIL="F@test.git" \
421 git commit -m "hello" &&
422
423 mv hello.c hello.orig &&
424 sed -e "/}/ {x; s/$/Qputs(\"goodbye\");/; G;}" <hello.orig |
425 tr Q "\\t" >hello.c &&
426 GIT_AUTHOR_NAME="G" GIT_AUTHOR_EMAIL="G@test.git" \
427 git commit -a -m "goodbye" &&
428
429 mv hello.c hello.orig &&
430 echo "#include <stdio.h>" >hello.c &&
431 cat hello.orig >>hello.c &&
432 tr Q "\\t" >>hello.c <<-\EOF &&
433 void mail()
434 {
435 Qputs("mail");
436 }
437 EOF
438 GIT_AUTHOR_NAME="H" GIT_AUTHOR_EMAIL="H@test.git" \
439 git commit -a -m "mail"
440 '
441
442 test_expect_success 'blame -L :literal' '
443 check_count -f hello.c -L:main F 4 G 1
444 '
445
446 test_expect_success 'blame -L :regex' '
447 check_count -f hello.c "-L:m[a-z][a-z]l" H 4
448 '
449
450 test_expect_success 'blame -L :nomatch' '
451 test_must_fail $PROG -L:nomatch hello.c
452 '
453
454 test_expect_success 'blame -L :RE (relative)' '
455 check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
456 '
457
458 test_expect_success 'blame -L :RE (relative: no preceding range)' '
459 check_count -f hello.c -L:ma.. F 4 G 1
460 '
461
462 test_expect_success 'blame -L :RE (relative: not found)' '
463 test_must_fail $PROG -L3,3 -L:tambourine hello.c
464 '
465
466 test_expect_success 'blame -L :RE (relative: end-of-file)' '
467 test_must_fail $PROG -L, -L:main hello.c
468 '
469
470 test_expect_success 'blame -L ^:RE (absolute)' '
471 check_count -f hello.c -L3,3 -L^:ma.. F 4 G 1
472 '
473
474 test_expect_success 'blame -L ^:RE (absolute: no preceding range)' '
475 check_count -f hello.c -L^:ma.. F 4 G 1
476 '
477
478 test_expect_success 'blame -L ^:RE (absolute: not found)' '
479 test_must_fail $PROG -L4,4 -L^:tambourine hello.c
480 '
481
482 test_expect_success 'blame -L ^:RE (absolute: end-of-file)' '
483 n=$(printf "%d" $(wc -l <hello.c)) &&
484 check_count -f hello.c -L$n -L^:ma.. F 4 G 1 H 1
485 '
486
487 test_expect_success 'blame -L :funcname with userdiff driver' '
488 cat >file.template <<-\EOF &&
489 DO NOT MATCH THIS LINE
490 function RIGHT(a, b) result(c)
491 AS THE DEFAULT DRIVER WOULD
492
493 integer, intent(in) :: ChangeMe
494 EOF
495
496 fortran_file=file.f03 &&
497 test_when_finished "rm .gitattributes" &&
498 echo "$fortran_file diff=fortran" >.gitattributes &&
499
500 test_commit --author "A <A@test.git>" \
501 "add" "$fortran_file" \
502 "$(cat file.template)" &&
503 test_commit --author "B <B@test.git>" \
504 "change" "$fortran_file" \
505 "$(cat file.template | sed -e s/ChangeMe/IWasChanged/)" &&
506 check_count -f "$fortran_file" -L:RIGHT A 3 B 1
507 '
508
509 test_expect_success 'setup incremental' '
510 (
511 GIT_AUTHOR_NAME=I &&
512 export GIT_AUTHOR_NAME &&
513 GIT_AUTHOR_EMAIL=I@test.git &&
514 export GIT_AUTHOR_EMAIL &&
515 >incremental &&
516 git add incremental &&
517 git commit -m "step 0" &&
518 printf "partial" >>incremental &&
519 git commit -a -m "step 0.5" &&
520 echo >>incremental &&
521 git commit -a -m "step 1"
522 )
523 '
524
525 test_expect_success 'blame empty' '
526 check_count -h HEAD^^ -f incremental
527 '
528
529 test_expect_success 'blame -L 0 empty' '
530 test_must_fail $PROG -L0 incremental HEAD^^
531 '
532
533 test_expect_success 'blame -L 1 empty' '
534 test_must_fail $PROG -L1 incremental HEAD^^
535 '
536
537 test_expect_success 'blame -L 2 empty' '
538 test_must_fail $PROG -L2 incremental HEAD^^
539 '
540
541 test_expect_success 'blame half' '
542 check_count -h HEAD^ -f incremental I 1
543 '
544
545 test_expect_success 'blame -L 0 half' '
546 test_must_fail $PROG -L0 incremental HEAD^
547 '
548
549 test_expect_success 'blame -L 1 half' '
550 check_count -h HEAD^ -f incremental -L1 I 1
551 '
552
553 test_expect_success 'blame -L 2 half' '
554 test_must_fail $PROG -L2 incremental HEAD^
555 '
556
557 test_expect_success 'blame -L 3 half' '
558 test_must_fail $PROG -L3 incremental HEAD^
559 '
560
561 test_expect_success 'blame full' '
562 check_count -f incremental I 1
563 '
564
565 test_expect_success 'blame -L 0 full' '
566 test_must_fail $PROG -L0 incremental
567 '
568
569 test_expect_success 'blame -L 1 full' '
570 check_count -f incremental -L1 I 1
571 '
572
573 test_expect_success 'blame -L 2 full' '
574 test_must_fail $PROG -L2 incremental
575 '
576
577 test_expect_success 'blame -L 3 full' '
578 test_must_fail $PROG -L3 incremental
579 '
580
581 test_expect_success 'blame -L' '
582 test_must_fail $PROG -L file
583 '
584
585 test_expect_success 'blame -L X,+' '
586 test_must_fail $PROG -L1,+ file
587 '
588
589 test_expect_success 'blame -L X,-' '
590 test_must_fail $PROG -L1,- file
591 '
592
593 test_expect_success 'blame -L X (non-numeric X)' '
594 test_must_fail $PROG -LX file
595 '
596
597 test_expect_success 'blame -L X,Y (non-numeric Y)' '
598 test_must_fail $PROG -L1,Y file
599 '
600
601 test_expect_success 'blame -L X,+N (non-numeric N)' '
602 test_must_fail $PROG -L1,+N file
603 '
604
605 test_expect_success 'blame -L X,-N (non-numeric N)' '
606 test_must_fail $PROG -L1,-N file
607 '
608
609 test_expect_success 'blame -L ,^/RE/' '
610 test_must_fail $PROG -L1,^/99/ file
611 '
612
613 test_expect_success 'blame progress on a full file' '
614 cat >expect <<-\EOF &&
615 Blaming lines: 100% (10/10), done.
616 EOF
617
618 GIT_PROGRESS_DELAY=0 \
619 git blame --progress hello.c 2>stderr &&
620
621 get_progress_result <stderr >actual &&
622 test_cmp expect actual
623 '
624
625 test_expect_success 'blame progress on a single range' '
626 cat >expect <<-\EOF &&
627 Blaming lines: 100% (4/4), done.
628 EOF
629
630 GIT_PROGRESS_DELAY=0 \
631 git blame --progress -L 3,6 hello.c 2>stderr &&
632
633 get_progress_result <stderr >actual &&
634 test_cmp expect actual
635 '
636
637 test_expect_success 'blame progress on multiple ranges' '
638 cat >expect <<-\EOF &&
639 Blaming lines: 100% (7/7), done.
640 EOF
641
642 GIT_PROGRESS_DELAY=0 \
643 git blame --progress -L 3,6 -L 8,10 hello.c 2>stderr &&
644
645 get_progress_result <stderr >actual &&
646 test_cmp expect actual
647 '