]> git.ipfire.org Git - thirdparty/git.git/blob - t/annotate-tests.sh
clone: allow "--bare" with "-o"
[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 -f .git/info/grafts" &&
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 printf "%s " $graft >.git/info/grafts &&
172 check_count -h 00 01 1 10 1
173 '
174
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"
179 '
180
181 test_expect_success 'blame incomplete line' '
182 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
183 '
184
185 test_expect_success 'setup edits' '
186 mv file file.orig &&
187 {
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"
194 '
195
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
198 '
199
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"
206 '
207
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
210 '
211
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
214 '
215
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
218 '
219
220 test_expect_success 'blame -L X (X to end)' '
221 check_count -L5 B1 1 C 1 D 1 "A U Thor" 1
222 '
223
224 test_expect_success 'blame -L X, (X to end)' '
225 check_count -L5, B1 1 C 1 D 1 "A U Thor" 1
226 '
227
228 test_expect_success 'blame -L ,Y (up to Y)' '
229 check_count -L,3 A 1 B2 1 E 1
230 '
231
232 test_expect_success 'blame -L X,X' '
233 check_count -L3,3 B2 1
234 '
235
236 test_expect_success 'blame -L X,Y' '
237 check_count -L3,6 B 1 B1 1 B2 1 D 1
238 '
239
240 test_expect_success 'blame -L Y,X (undocumented)' '
241 check_count -L6,3 B 1 B1 1 B2 1 D 1
242 '
243
244 test_expect_success 'blame -L -X' '
245 test_must_fail $PROG -L-1 file
246 '
247
248 test_expect_success 'blame -L 0' '
249 test_must_fail $PROG -L0 file
250 '
251
252 test_expect_success 'blame -L ,0' '
253 test_must_fail $PROG -L,0 file
254 '
255
256 test_expect_success 'blame -L ,+0' '
257 test_must_fail $PROG -L,+0 file
258 '
259
260 test_expect_success 'blame -L X,+0' '
261 test_must_fail $PROG -L1,+0 file
262 '
263
264 test_expect_success 'blame -L X,+1' '
265 check_count -L3,+1 B2 1
266 '
267
268 test_expect_success 'blame -L X,+N' '
269 check_count -L3,+4 B 1 B1 1 B2 1 D 1
270 '
271
272 test_expect_success 'blame -L ,-0' '
273 test_must_fail $PROG -L,-0 file
274 '
275
276 test_expect_success 'blame -L X,-0' '
277 test_must_fail $PROG -L1,-0 file
278 '
279
280 test_expect_success 'blame -L X,-1' '
281 check_count -L3,-1 B2 1
282 '
283
284 test_expect_success 'blame -L X,-N' '
285 check_count -L6,-4 B 1 B1 1 B2 1 D 1
286 '
287
288 test_expect_success 'blame -L /RE/ (RE to end)' '
289 check_count -L/evil/ C 1 "A U Thor" 1
290 '
291
292 test_expect_success 'blame -L /RE/,/RE2/' '
293 check_count -L/robot/,/green/ A 1 B 1 B2 1 D 1 E 1
294 '
295
296 test_expect_success 'blame -L X,/RE/' '
297 check_count -L5,/evil/ B1 1 D 1 "A U Thor" 1
298 '
299
300 test_expect_success 'blame -L /RE/,Y' '
301 check_count -L/99/,7 B1 1 D 1 "A U Thor" 1
302 '
303
304 test_expect_success 'blame -L /RE/,+N' '
305 check_count -L/99/,+3 B1 1 D 1 "A U Thor" 1
306 '
307
308 test_expect_success 'blame -L /RE/,-N' '
309 check_count -L/99/,-3 B 1 B2 1 D 1
310 '
311
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
317 '
318
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
322 '
323
324 test_expect_success 'blame -L X (X > nlines)' '
325 test_must_fail $PROG -L12345 file
326 '
327
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
331 '
332
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
336 '
337
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
340 '
341
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
344 '
345
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
348 '
349
350 test_expect_success 'blame -L multiple (adjacent)' '
351 check_count -L2,3 -L4,5 A 1 B 1 B2 1 D 1
352 '
353
354 test_expect_success 'blame -L multiple (adjacent: unordered)' '
355 check_count -L4,5 -L2,3 A 1 B 1 B2 1 D 1
356 '
357
358 test_expect_success 'blame -L multiple (overlapping)' '
359 check_count -L2,4 -L3,5 A 1 B 1 B2 1 D 1
360 '
361
362 test_expect_success 'blame -L multiple (overlapping: unordered)' '
363 check_count -L3,5 -L2,4 A 1 B 1 B2 1 D 1
364 '
365
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
368 '
369
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
372 '
373
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
376 '
377
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
380 '
381
382 test_expect_success 'blame -L /RE/ (relative: adjacent)' '
383 check_count -L1,1 -L/dog/,+1 A 1 E 1
384 '
385
386 test_expect_success 'blame -L /RE/ (relative: not found)' '
387 test_must_fail $PROG -L4,4 -L/dog/ file
388 '
389
390 test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
391 test_must_fail $PROG -L, -L/$/ file
392 '
393
394 test_expect_success 'blame -L ^/RE/ (absolute)' '
395 check_count -L3,3 -L^/dog/,+2 A 1 B2 1
396 '
397
398 test_expect_success 'blame -L ^/RE/ (absolute: no preceding range)' '
399 check_count -L^/dog/,+2 A 1 B2 1
400 '
401
402 test_expect_success 'blame -L ^/RE/ (absolute: not found)' '
403 test_must_fail $PROG -L4,4 -L^/tambourine/ file
404 '
405
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
409 '
410
411 test_expect_success 'setup -L :regex' '
412 tr Q "\\t" >hello.c <<-\EOF &&
413 int main(int argc, const char *argv[])
414 {
415 Qputs("hello");
416 }
417 EOF
418 git add hello.c &&
419 GIT_AUTHOR_NAME="F" GIT_AUTHOR_EMAIL="F@test.git" \
420 git commit -m "hello" &&
421
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" &&
427
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()
433 {
434 Qputs("mail");
435 }
436 EOF
437 GIT_AUTHOR_NAME="H" GIT_AUTHOR_EMAIL="H@test.git" \
438 git commit -a -m "mail"
439 '
440
441 test_expect_success 'blame -L :literal' '
442 check_count -f hello.c -L:main F 4 G 1
443 '
444
445 test_expect_success 'blame -L :regex' '
446 check_count -f hello.c "-L:m[a-z][a-z]l" H 4
447 '
448
449 test_expect_success 'blame -L :nomatch' '
450 test_must_fail $PROG -L:nomatch hello.c
451 '
452
453 test_expect_success 'blame -L :RE (relative)' '
454 check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
455 '
456
457 test_expect_success 'blame -L :RE (relative: no preceding range)' '
458 check_count -f hello.c -L:ma.. F 4 G 1
459 '
460
461 test_expect_success 'blame -L :RE (relative: not found)' '
462 test_must_fail $PROG -L3,3 -L:tambourine hello.c
463 '
464
465 test_expect_success 'blame -L :RE (relative: end-of-file)' '
466 test_must_fail $PROG -L, -L:main hello.c
467 '
468
469 test_expect_success 'blame -L ^:RE (absolute)' '
470 check_count -f hello.c -L3,3 -L^:ma.. F 4 G 1
471 '
472
473 test_expect_success 'blame -L ^:RE (absolute: no preceding range)' '
474 check_count -f hello.c -L^:ma.. F 4 G 1
475 '
476
477 test_expect_success 'blame -L ^:RE (absolute: not found)' '
478 test_must_fail $PROG -L4,4 -L^:tambourine hello.c
479 '
480
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
484 '
485
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
491
492 integer, intent(in) :: ChangeMe
493 EOF
494
495 fortran_file=file.f03 &&
496 test_when_finished "rm .gitattributes" &&
497 echo "$fortran_file diff=fortran" >.gitattributes &&
498
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
506 '
507
508 test_expect_success 'setup incremental' '
509 (
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"
521 )
522 '
523
524 test_expect_success 'blame empty' '
525 check_count -h HEAD^^ -f incremental
526 '
527
528 test_expect_success 'blame -L 0 empty' '
529 test_must_fail $PROG -L0 incremental HEAD^^
530 '
531
532 test_expect_success 'blame -L 1 empty' '
533 test_must_fail $PROG -L1 incremental HEAD^^
534 '
535
536 test_expect_success 'blame -L 2 empty' '
537 test_must_fail $PROG -L2 incremental HEAD^^
538 '
539
540 test_expect_success 'blame half' '
541 check_count -h HEAD^ -f incremental I 1
542 '
543
544 test_expect_success 'blame -L 0 half' '
545 test_must_fail $PROG -L0 incremental HEAD^
546 '
547
548 test_expect_success 'blame -L 1 half' '
549 check_count -h HEAD^ -f incremental -L1 I 1
550 '
551
552 test_expect_success 'blame -L 2 half' '
553 test_must_fail $PROG -L2 incremental HEAD^
554 '
555
556 test_expect_success 'blame -L 3 half' '
557 test_must_fail $PROG -L3 incremental HEAD^
558 '
559
560 test_expect_success 'blame full' '
561 check_count -f incremental I 1
562 '
563
564 test_expect_success 'blame -L 0 full' '
565 test_must_fail $PROG -L0 incremental
566 '
567
568 test_expect_success 'blame -L 1 full' '
569 check_count -f incremental -L1 I 1
570 '
571
572 test_expect_success 'blame -L 2 full' '
573 test_must_fail $PROG -L2 incremental
574 '
575
576 test_expect_success 'blame -L 3 full' '
577 test_must_fail $PROG -L3 incremental
578 '
579
580 test_expect_success 'blame -L' '
581 test_must_fail $PROG -L file
582 '
583
584 test_expect_success 'blame -L X,+' '
585 test_must_fail $PROG -L1,+ file
586 '
587
588 test_expect_success 'blame -L X,-' '
589 test_must_fail $PROG -L1,- file
590 '
591
592 test_expect_success 'blame -L X (non-numeric X)' '
593 test_must_fail $PROG -LX file
594 '
595
596 test_expect_success 'blame -L X,Y (non-numeric Y)' '
597 test_must_fail $PROG -L1,Y file
598 '
599
600 test_expect_success 'blame -L X,+N (non-numeric N)' '
601 test_must_fail $PROG -L1,+N file
602 '
603
604 test_expect_success 'blame -L X,-N (non-numeric N)' '
605 test_must_fail $PROG -L1,-N file
606 '
607
608 test_expect_success 'blame -L ,^/RE/' '
609 test_must_fail $PROG -L1,^/99/ file
610 '
611
612 test_expect_success 'blame progress on a full file' '
613 cat >expect <<-\EOF &&
614 Blaming lines: 100% (10/10), done.
615 EOF
616
617 GIT_PROGRESS_DELAY=0 \
618 git blame --progress hello.c 2>stderr &&
619
620 get_progress_result <stderr >actual &&
621 test_cmp expect actual
622 '
623
624 test_expect_success 'blame progress on a single range' '
625 cat >expect <<-\EOF &&
626 Blaming lines: 100% (4/4), done.
627 EOF
628
629 GIT_PROGRESS_DELAY=0 \
630 git blame --progress -L 3,6 hello.c 2>stderr &&
631
632 get_progress_result <stderr >actual &&
633 test_cmp expect actual
634 '
635
636 test_expect_success 'blame progress on multiple ranges' '
637 cat >expect <<-\EOF &&
638 Blaming lines: 100% (7/7), done.
639 EOF
640
641 GIT_PROGRESS_DELAY=0 \
642 git blame --progress -L 3,6 -L 8,10 hello.c 2>stderr &&
643
644 get_progress_result <stderr >actual &&
645 test_cmp expect actual
646 '