line-range: teach -L^:RE to search from start of file
[git.git] / t / annotate-tests.sh
blobdabe89d91a2198f8c8f0c3d42eb5d2cc480b5a14
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 check_count () {
5 head= &&
6 file='file' &&
7 options= &&
8 while :
9 do
10 case "$1" in
11 -h) head="$2"; shift; shift ;;
12 -f) file="$2"; shift; shift ;;
13 -*) options="$options $1"; shift ;;
14 *) break ;;
15 esac
16 done &&
17 echo "$PROG $options $file $head" >&4 &&
18 $PROG $options $file $head >actual &&
19 perl -e '
20 my %expect = (@ARGV);
21 my %count = map { $_ => 0 } keys %expect;
22 while (<STDIN>) {
23 if (/^[0-9a-f]+\t\(([^\t]+)\t/) {
24 my $author = $1;
25 for ($author) { s/^\s*//; s/\s*$//; }
26 $count{$author}++;
29 my $bad = 0;
30 while (my ($author, $count) = each %count) {
31 my $ok;
32 my $value = 0;
33 $value = $expect{$author} if defined $expect{$author};
34 if ($value != $count) {
35 $bad = 1;
36 $ok = "bad";
38 else {
39 $ok = "good";
41 print STDERR "Author $author (expected $value, attributed $count) $ok\n";
43 exit($bad);
44 ' "$@" <actual
47 test_expect_success 'setup A lines' '
48 echo "1A quick brown fox jumps over the" >file &&
49 echo "lazy dog" >>file &&
50 git add file &&
51 GIT_AUTHOR_NAME="A" GIT_AUTHOR_EMAIL="A@test.git" \
52 git commit -a -m "Initial."
55 test_expect_success 'blame 1 author' '
56 check_count A 2
59 test_expect_success 'setup B lines' '
60 echo "2A quick brown fox jumps over the" >>file &&
61 echo "lazy dog" >>file &&
62 GIT_AUTHOR_NAME="B" GIT_AUTHOR_EMAIL="B@test.git" \
63 git commit -a -m "Second."
66 test_expect_success 'blame 2 authors' '
67 check_count A 2 B 2
70 test_expect_success 'setup B1 lines (branch1)' '
71 git checkout -b branch1 master &&
72 echo "3A slow green fox jumps into the" >>file &&
73 echo "well." >>file &&
74 GIT_AUTHOR_NAME="B1" GIT_AUTHOR_EMAIL="B1@test.git" \
75 git commit -a -m "Branch1-1"
78 test_expect_success 'blame 2 authors + 1 branch1 author' '
79 check_count A 2 B 2 B1 2
82 test_expect_success 'setup B2 lines (branch2)' '
83 git checkout -b branch2 master &&
84 sed -e "s/2A quick brown/4A quick brown lazy dog/" <file >file.new &&
85 mv file.new file &&
86 GIT_AUTHOR_NAME="B2" GIT_AUTHOR_EMAIL="B2@test.git" \
87 git commit -a -m "Branch2-1"
90 test_expect_success 'blame 2 authors + 1 branch2 author' '
91 check_count A 2 B 1 B2 1
94 test_expect_success 'merge branch1 & branch2' '
95 git pull . branch1
98 test_expect_success 'blame 2 authors + 2 merged-in authors' '
99 check_count A 2 B 1 B1 2 B2 1
102 test_expect_success 'blame ancestor' '
103 check_count -h master A 2 B 2
106 test_expect_success 'blame great-ancestor' '
107 check_count -h master^ A 2
110 test_expect_success 'setup evil merge' '
111 echo "evil merge." >>file &&
112 git commit -a --amend
115 test_expect_success 'blame evil merge' '
116 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1
119 test_expect_success 'setup incomplete line' '
120 echo "incomplete" | tr -d "\\012" >>file &&
121 GIT_AUTHOR_NAME="C" GIT_AUTHOR_EMAIL="C@test.git" \
122 git commit -a -m "Incomplete"
125 test_expect_success 'blame incomplete line' '
126 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
129 test_expect_success 'setup edits' '
130 mv file file.orig &&
132 cat file.orig &&
133 echo
134 } | sed -e "s/^3A/99/" -e "/^1A/d" -e "/^incomplete/d" >file &&
135 echo "incomplete" | tr -d "\\012" >>file &&
136 GIT_AUTHOR_NAME="D" GIT_AUTHOR_EMAIL="D@test.git" \
137 git commit -a -m "edit"
140 test_expect_success 'blame edits' '
141 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1
144 test_expect_success 'setup obfuscated email' '
145 echo "No robots allowed" >file.new &&
146 cat file >>file.new &&
147 mv file.new file &&
148 GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" \
149 git commit -a -m "norobots"
152 test_expect_success 'blame obfuscated email' '
153 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
156 test_expect_success 'blame -L 1 (all)' '
157 check_count -L1 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
160 test_expect_success 'blame -L , (all)' '
161 check_count -L, A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
164 test_expect_success 'blame -L X (X to end)' '
165 check_count -L5 B1 1 C 1 D 1 "A U Thor" 1
168 test_expect_success 'blame -L X, (X to end)' '
169 check_count -L5, B1 1 C 1 D 1 "A U Thor" 1
172 test_expect_success 'blame -L ,Y (up to Y)' '
173 check_count -L,3 A 1 B2 1 E 1
176 test_expect_success 'blame -L X,X' '
177 check_count -L3,3 B2 1
180 test_expect_success 'blame -L X,Y' '
181 check_count -L3,6 B 1 B1 1 B2 1 D 1
184 test_expect_success 'blame -L Y,X (undocumented)' '
185 check_count -L6,3 B 1 B1 1 B2 1 D 1
188 test_expect_success 'blame -L ,+0' '
189 test_must_fail $PROG -L,+0 file
192 test_expect_success 'blame -L X,+0' '
193 test_must_fail $PROG -L1,+0 file
196 test_expect_success 'blame -L X,+1' '
197 check_count -L3,+1 B2 1
200 test_expect_success 'blame -L X,+N' '
201 check_count -L3,+4 B 1 B1 1 B2 1 D 1
204 test_expect_success 'blame -L ,-0' '
205 test_must_fail $PROG -L,-0 file
208 test_expect_success 'blame -L X,-0' '
209 test_must_fail $PROG -L1,-0 file
212 test_expect_success 'blame -L X,-1' '
213 check_count -L3,-1 B2 1
216 test_expect_success 'blame -L X,-N' '
217 check_count -L6,-4 B 1 B1 1 B2 1 D 1
220 test_expect_success 'blame -L /RE/ (RE to end)' '
221 check_count -L/evil/ C 1 "A U Thor" 1
224 test_expect_success 'blame -L /RE/,/RE2/' '
225 check_count -L/robot/,/green/ A 1 B 1 B2 1 D 1 E 1
228 test_expect_success 'blame -L X,/RE/' '
229 check_count -L5,/evil/ B1 1 D 1 "A U Thor" 1
232 test_expect_success 'blame -L /RE/,Y' '
233 check_count -L/99/,7 B1 1 D 1 "A U Thor" 1
236 test_expect_success 'blame -L /RE/,+N' '
237 check_count -L/99/,+3 B1 1 D 1 "A U Thor" 1
240 test_expect_success 'blame -L /RE/,-N' '
241 check_count -L/99/,-3 B 1 B2 1 D 1
244 # 'file' ends with an incomplete line, so 'wc' reports one fewer lines than
245 # git-blame sees, hence the last line is actually $(wc...)+1.
246 test_expect_success 'blame -L X (X == nlines)' '
247 n=$(expr $(wc -l <file) + 1) &&
248 check_count -L$n C 1
251 test_expect_success 'blame -L X (X == nlines + 1)' '
252 n=$(expr $(wc -l <file) + 2) &&
253 test_must_fail $PROG -L$n file
256 test_expect_success 'blame -L X (X > nlines)' '
257 test_must_fail $PROG -L12345 file
260 test_expect_success 'blame -L ,Y (Y == nlines)' '
261 n=$(expr $(wc -l <file) + 1) &&
262 check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
265 test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
266 n=$(expr $(wc -l <file) + 2) &&
267 test_must_fail $PROG -L,$n file
270 test_expect_success 'blame -L ,Y (Y > nlines)' '
271 test_must_fail $PROG -L,12345 file
274 test_expect_success 'blame -L multiple (disjoint)' '
275 check_count -L2,3 -L6,7 A 1 B1 1 B2 1 "A U Thor" 1
278 test_expect_success 'blame -L multiple (disjoint: unordered)' '
279 check_count -L6,7 -L2,3 A 1 B1 1 B2 1 "A U Thor" 1
282 test_expect_success 'blame -L multiple (adjacent)' '
283 check_count -L2,3 -L4,5 A 1 B 1 B2 1 D 1
286 test_expect_success 'blame -L multiple (adjacent: unordered)' '
287 check_count -L4,5 -L2,3 A 1 B 1 B2 1 D 1
290 test_expect_success 'blame -L multiple (overlapping)' '
291 check_count -L2,4 -L3,5 A 1 B 1 B2 1 D 1
294 test_expect_success 'blame -L multiple (overlapping: unordered)' '
295 check_count -L3,5 -L2,4 A 1 B 1 B2 1 D 1
298 test_expect_success 'blame -L multiple (superset/subset)' '
299 check_count -L2,8 -L3,5 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
302 test_expect_success 'blame -L multiple (superset/subset: unordered)' '
303 check_count -L3,5 -L2,8 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
306 test_expect_success 'blame -L /RE/ (relative)' '
307 check_count -L3,3 -L/fox/ B1 1 B2 1 C 1 D 1 "A U Thor" 1
310 test_expect_success 'blame -L /RE/ (relative: no preceding range)' '
311 check_count -L/dog/ A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
314 test_expect_success 'blame -L /RE/ (relative: adjacent)' '
315 check_count -L1,1 -L/dog/,+1 A 1 E 1
318 test_expect_success 'blame -L /RE/ (relative: not found)' '
319 test_must_fail $PROG -L4,4 -L/dog/ file
322 test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
323 test_must_fail $PROG -L, -L/$/ file
326 test_expect_success 'blame -L ^/RE/ (absolute)' '
327 check_count -L3,3 -L^/dog/,+2 A 1 B2 1
330 test_expect_success 'blame -L ^/RE/ (absolute: no preceding range)' '
331 check_count -L^/dog/,+2 A 1 B2 1
334 test_expect_success 'blame -L ^/RE/ (absolute: not found)' '
335 test_must_fail $PROG -L4,4 -L^/tambourine/ file
338 test_expect_success 'blame -L ^/RE/ (absolute: end-of-file)' '
339 n=$(expr $(wc -l <file) + 1) &&
340 check_count -L$n -L^/$/,+2 A 1 C 1 E 1
343 test_expect_success 'setup -L :regex' '
344 tr Q "\\t" >hello.c <<-\EOF &&
345 int main(int argc, const char *argv[])
347 Qputs("hello");
350 git add hello.c &&
351 GIT_AUTHOR_NAME="F" GIT_AUTHOR_EMAIL="F@test.git" \
352 git commit -m "hello" &&
354 mv hello.c hello.orig &&
355 sed -e "/}/ {x; s/$/Qputs(\"goodbye\");/; G;}" <hello.orig |
356 tr Q "\\t" >hello.c &&
357 GIT_AUTHOR_NAME="G" GIT_AUTHOR_EMAIL="G@test.git" \
358 git commit -a -m "goodbye" &&
360 mv hello.c hello.orig &&
361 echo "#include <stdio.h>" >hello.c &&
362 cat hello.orig >>hello.c &&
363 tr Q "\\t" >>hello.c <<-\EOF
364 void mail()
366 Qputs("mail");
369 GIT_AUTHOR_NAME="H" GIT_AUTHOR_EMAIL="H@test.git" \
370 git commit -a -m "mail"
373 test_expect_success 'blame -L :literal' '
374 check_count -f hello.c -L:main F 4 G 1
377 test_expect_success 'blame -L :regex' '
378 check_count -f hello.c "-L:m[a-z][a-z]l" H 4
381 test_expect_success 'blame -L :nomatch' '
382 test_must_fail $PROG -L:nomatch hello.c
385 test_expect_success 'blame -L :RE (relative)' '
386 check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
389 test_expect_success 'blame -L :RE (relative: no preceding range)' '
390 check_count -f hello.c -L:ma.. F 4 G 1
393 test_expect_success 'blame -L :RE (relative: not found)' '
394 test_must_fail $PROG -L3,3 -L:tambourine hello.c
397 test_expect_success 'blame -L :RE (relative: end-of-file)' '
398 test_must_fail $PROG -L, -L:main hello.c
401 test_expect_success 'blame -L ^:RE (absolute)' '
402 check_count -f hello.c -L3,3 -L^:ma.. F 4 G 1
405 test_expect_success 'blame -L ^:RE (absolute: no preceding range)' '
406 check_count -f hello.c -L^:ma.. F 4 G 1
409 test_expect_success 'blame -L ^:RE (absolute: not found)' '
410 test_must_fail $PROG -L4,4 -L^:tambourine hello.c
413 test_expect_success 'blame -L ^:RE (absolute: end-of-file)' '
414 n=$(printf "%d" $(wc -l <hello.c)) &&
415 check_count -f hello.c -L$n -L^:ma.. F 4 G 1 H 1
418 test_expect_success 'setup incremental' '
420 GIT_AUTHOR_NAME=I &&
421 export GIT_AUTHOR_NAME &&
422 GIT_AUTHOR_EMAIL=I@test.git &&
423 export GIT_AUTHOR_EMAIL &&
424 >incremental &&
425 git add incremental &&
426 git commit -m "step 0" &&
427 printf "partial" >>incremental &&
428 git commit -a -m "step 0.5" &&
429 echo >>incremental &&
430 git commit -a -m "step 1"
434 test_expect_success 'blame empty' '
435 check_count -h HEAD^^ -f incremental
438 test_expect_success 'blame -L 0 empty (undocumented)' '
439 check_count -h HEAD^^ -f incremental -L0
442 test_expect_success 'blame -L 1 empty' '
443 test_must_fail $PROG -L1 incremental HEAD^^
446 test_expect_success 'blame -L 2 empty' '
447 test_must_fail $PROG -L2 incremental HEAD^^
450 test_expect_success 'blame half' '
451 check_count -h HEAD^ -f incremental I 1
454 test_expect_success 'blame -L 0 half (undocumented)' '
455 check_count -h HEAD^ -f incremental -L0 I 1
458 test_expect_success 'blame -L 1 half' '
459 check_count -h HEAD^ -f incremental -L1 I 1
462 test_expect_success 'blame -L 2 half' '
463 test_must_fail $PROG -L2 incremental HEAD^
466 test_expect_success 'blame -L 3 half' '
467 test_must_fail $PROG -L3 incremental HEAD^
470 test_expect_success 'blame full' '
471 check_count -f incremental I 1
474 test_expect_success 'blame -L 0 full (undocumented)' '
475 check_count -f incremental -L0 I 1
478 test_expect_success 'blame -L 1 full' '
479 check_count -f incremental -L1 I 1
482 test_expect_success 'blame -L 2 full' '
483 test_must_fail $PROG -L2 incremental
486 test_expect_success 'blame -L 3 full' '
487 test_must_fail $PROG -L3 incremental
490 test_expect_success 'blame -L' '
491 test_must_fail $PROG -L file
494 test_expect_success 'blame -L X,+' '
495 test_must_fail $PROG -L1,+ file
498 test_expect_success 'blame -L X,-' '
499 test_must_fail $PROG -L1,- file
502 test_expect_success 'blame -L X (non-numeric X)' '
503 test_must_fail $PROG -LX file
506 test_expect_success 'blame -L X,Y (non-numeric Y)' '
507 test_must_fail $PROG -L1,Y file
510 test_expect_success 'blame -L X,+N (non-numeric N)' '
511 test_must_fail $PROG -L1,+N file
514 test_expect_success 'blame -L X,-N (non-numeric N)' '
515 test_must_fail $PROG -L1,-N file
518 test_expect_success 'blame -L ,^/RE/' '
519 test_must_fail $PROG -L1,^/99/ file