]> git.ipfire.org Git - thirdparty/git.git/blob - t/annotate-tests.sh
Merge branch 'maint-2.5' into maint-2.6
[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 test_expect_success 'setup A lines' '
60 echo "1A quick brown fox jumps over the" >file &&
61 echo "lazy dog" >>file &&
62 git add file &&
63 GIT_AUTHOR_NAME="A" GIT_AUTHOR_EMAIL="A@test.git" \
64 git commit -a -m "Initial."
65 '
66
67 test_expect_success 'blame 1 author' '
68 check_count A 2
69 '
70
71 test_expect_success 'setup B lines' '
72 echo "2A quick brown fox jumps over the" >>file &&
73 echo "lazy dog" >>file &&
74 GIT_AUTHOR_NAME="B" GIT_AUTHOR_EMAIL="B@test.git" \
75 git commit -a -m "Second."
76 '
77
78 test_expect_success 'blame 2 authors' '
79 check_count A 2 B 2
80 '
81
82 test_expect_success 'setup B1 lines (branch1)' '
83 git checkout -b branch1 master &&
84 echo "3A slow green fox jumps into the" >>file &&
85 echo "well." >>file &&
86 GIT_AUTHOR_NAME="B1" GIT_AUTHOR_EMAIL="B1@test.git" \
87 git commit -a -m "Branch1-1"
88 '
89
90 test_expect_success 'blame 2 authors + 1 branch1 author' '
91 check_count A 2 B 2 B1 2
92 '
93
94 test_expect_success 'setup B2 lines (branch2)' '
95 git checkout -b branch2 master &&
96 sed -e "s/2A quick brown/4A quick brown lazy dog/" <file >file.new &&
97 mv file.new file &&
98 GIT_AUTHOR_NAME="B2" GIT_AUTHOR_EMAIL="B2@test.git" \
99 git commit -a -m "Branch2-1"
100 '
101
102 test_expect_success 'blame 2 authors + 1 branch2 author' '
103 check_count A 2 B 1 B2 1
104 '
105
106 test_expect_success 'merge branch1 & branch2' '
107 git merge branch1
108 '
109
110 test_expect_success 'blame 2 authors + 2 merged-in authors' '
111 check_count A 2 B 1 B1 2 B2 1
112 '
113
114 test_expect_success 'blame --first-parent blames merge for branch1' '
115 check_count --first-parent A 2 B 1 "A U Thor" 2 B2 1
116 '
117
118 test_expect_success 'blame ancestor' '
119 check_count -h master A 2 B 2
120 '
121
122 test_expect_success 'blame great-ancestor' '
123 check_count -h master^ A 2
124 '
125
126 test_expect_success 'setup evil merge' '
127 echo "evil merge." >>file &&
128 git commit -a --amend
129 '
130
131 test_expect_success 'blame evil merge' '
132 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1
133 '
134
135 test_expect_success 'blame huge graft' '
136 test_when_finished "git checkout branch2" &&
137 test_when_finished "rm -f .git/info/grafts" &&
138 graft= &&
139 for i in 0 1 2
140 do
141 for j in 0 1 2 3 4 5 6 7 8 9
142 do
143 git checkout --orphan "$i$j" &&
144 printf "%s\n" "$i" "$j" >file &&
145 test_tick &&
146 GIT_AUTHOR_NAME=$i$j GIT_AUTHOR_EMAIL=$i$j@test.git \
147 git commit -a -m "$i$j" &&
148 commit=$(git rev-parse --verify HEAD) &&
149 graft="$graft$commit "
150 done
151 done &&
152 printf "%s " $graft >.git/info/grafts &&
153 check_count -h 00 01 1 10 1
154 '
155
156 test_expect_success 'setup incomplete line' '
157 echo "incomplete" | tr -d "\\012" >>file &&
158 GIT_AUTHOR_NAME="C" GIT_AUTHOR_EMAIL="C@test.git" \
159 git commit -a -m "Incomplete"
160 '
161
162 test_expect_success 'blame incomplete line' '
163 check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
164 '
165
166 test_expect_success 'setup edits' '
167 mv file file.orig &&
168 {
169 cat file.orig &&
170 echo
171 } | sed -e "s/^3A/99/" -e "/^1A/d" -e "/^incomplete/d" >file &&
172 echo "incomplete" | tr -d "\\012" >>file &&
173 GIT_AUTHOR_NAME="D" GIT_AUTHOR_EMAIL="D@test.git" \
174 git commit -a -m "edit"
175 '
176
177 test_expect_success 'blame edits' '
178 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1
179 '
180
181 test_expect_success 'setup obfuscated email' '
182 echo "No robots allowed" >file.new &&
183 cat file >>file.new &&
184 mv file.new file &&
185 GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" \
186 git commit -a -m "norobots"
187 '
188
189 test_expect_success 'blame obfuscated email' '
190 check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
191 '
192
193 test_expect_success 'blame -L 1 (all)' '
194 check_count -L1 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
195 '
196
197 test_expect_success 'blame -L , (all)' '
198 check_count -L, A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
199 '
200
201 test_expect_success 'blame -L X (X to end)' '
202 check_count -L5 B1 1 C 1 D 1 "A U Thor" 1
203 '
204
205 test_expect_success 'blame -L X, (X to end)' '
206 check_count -L5, B1 1 C 1 D 1 "A U Thor" 1
207 '
208
209 test_expect_success 'blame -L ,Y (up to Y)' '
210 check_count -L,3 A 1 B2 1 E 1
211 '
212
213 test_expect_success 'blame -L X,X' '
214 check_count -L3,3 B2 1
215 '
216
217 test_expect_success 'blame -L X,Y' '
218 check_count -L3,6 B 1 B1 1 B2 1 D 1
219 '
220
221 test_expect_success 'blame -L Y,X (undocumented)' '
222 check_count -L6,3 B 1 B1 1 B2 1 D 1
223 '
224
225 test_expect_success 'blame -L -X' '
226 test_must_fail $PROG -L-1 file
227 '
228
229 test_expect_success 'blame -L 0' '
230 test_must_fail $PROG -L0 file
231 '
232
233 test_expect_success 'blame -L ,0' '
234 test_must_fail $PROG -L,0 file
235 '
236
237 test_expect_success 'blame -L ,+0' '
238 test_must_fail $PROG -L,+0 file
239 '
240
241 test_expect_success 'blame -L X,+0' '
242 test_must_fail $PROG -L1,+0 file
243 '
244
245 test_expect_success 'blame -L X,+1' '
246 check_count -L3,+1 B2 1
247 '
248
249 test_expect_success 'blame -L X,+N' '
250 check_count -L3,+4 B 1 B1 1 B2 1 D 1
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 X,-0' '
258 test_must_fail $PROG -L1,-0 file
259 '
260
261 test_expect_success 'blame -L X,-1' '
262 check_count -L3,-1 B2 1
263 '
264
265 test_expect_success 'blame -L X,-N' '
266 check_count -L6,-4 B 1 B1 1 B2 1 D 1
267 '
268
269 test_expect_success 'blame -L /RE/ (RE to end)' '
270 check_count -L/evil/ C 1 "A U Thor" 1
271 '
272
273 test_expect_success 'blame -L /RE/,/RE2/' '
274 check_count -L/robot/,/green/ A 1 B 1 B2 1 D 1 E 1
275 '
276
277 test_expect_success 'blame -L X,/RE/' '
278 check_count -L5,/evil/ B1 1 D 1 "A U Thor" 1
279 '
280
281 test_expect_success 'blame -L /RE/,Y' '
282 check_count -L/99/,7 B1 1 D 1 "A U Thor" 1
283 '
284
285 test_expect_success 'blame -L /RE/,+N' '
286 check_count -L/99/,+3 B1 1 D 1 "A U Thor" 1
287 '
288
289 test_expect_success 'blame -L /RE/,-N' '
290 check_count -L/99/,-3 B 1 B2 1 D 1
291 '
292
293 # 'file' ends with an incomplete line, so 'wc' reports one fewer lines than
294 # git-blame sees, hence the last line is actually $(wc...)+1.
295 test_expect_success 'blame -L X (X == nlines)' '
296 n=$(expr $(wc -l <file) + 1) &&
297 check_count -L$n C 1
298 '
299
300 test_expect_success 'blame -L X (X == nlines + 1)' '
301 n=$(expr $(wc -l <file) + 2) &&
302 test_must_fail $PROG -L$n file
303 '
304
305 test_expect_success 'blame -L X (X > nlines)' '
306 test_must_fail $PROG -L12345 file
307 '
308
309 test_expect_success 'blame -L ,Y (Y == nlines)' '
310 n=$(expr $(wc -l <file) + 1) &&
311 check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
312 '
313
314 test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
315 n=$(expr $(wc -l <file) + 2) &&
316 test_must_fail $PROG -L,$n file
317 '
318
319 test_expect_success 'blame -L ,Y (Y > nlines)' '
320 test_must_fail $PROG -L,12345 file
321 '
322
323 test_expect_success 'blame -L multiple (disjoint)' '
324 check_count -L2,3 -L6,7 A 1 B1 1 B2 1 "A U Thor" 1
325 '
326
327 test_expect_success 'blame -L multiple (disjoint: unordered)' '
328 check_count -L6,7 -L2,3 A 1 B1 1 B2 1 "A U Thor" 1
329 '
330
331 test_expect_success 'blame -L multiple (adjacent)' '
332 check_count -L2,3 -L4,5 A 1 B 1 B2 1 D 1
333 '
334
335 test_expect_success 'blame -L multiple (adjacent: unordered)' '
336 check_count -L4,5 -L2,3 A 1 B 1 B2 1 D 1
337 '
338
339 test_expect_success 'blame -L multiple (overlapping)' '
340 check_count -L2,4 -L3,5 A 1 B 1 B2 1 D 1
341 '
342
343 test_expect_success 'blame -L multiple (overlapping: unordered)' '
344 check_count -L3,5 -L2,4 A 1 B 1 B2 1 D 1
345 '
346
347 test_expect_success 'blame -L multiple (superset/subset)' '
348 check_count -L2,8 -L3,5 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
349 '
350
351 test_expect_success 'blame -L multiple (superset/subset: unordered)' '
352 check_count -L3,5 -L2,8 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
353 '
354
355 test_expect_success 'blame -L /RE/ (relative)' '
356 check_count -L3,3 -L/fox/ B1 1 B2 1 C 1 D 1 "A U Thor" 1
357 '
358
359 test_expect_success 'blame -L /RE/ (relative: no preceding range)' '
360 check_count -L/dog/ A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
361 '
362
363 test_expect_success 'blame -L /RE/ (relative: adjacent)' '
364 check_count -L1,1 -L/dog/,+1 A 1 E 1
365 '
366
367 test_expect_success 'blame -L /RE/ (relative: not found)' '
368 test_must_fail $PROG -L4,4 -L/dog/ file
369 '
370
371 test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
372 test_must_fail $PROG -L, -L/$/ file
373 '
374
375 test_expect_success 'blame -L ^/RE/ (absolute)' '
376 check_count -L3,3 -L^/dog/,+2 A 1 B2 1
377 '
378
379 test_expect_success 'blame -L ^/RE/ (absolute: no preceding range)' '
380 check_count -L^/dog/,+2 A 1 B2 1
381 '
382
383 test_expect_success 'blame -L ^/RE/ (absolute: not found)' '
384 test_must_fail $PROG -L4,4 -L^/tambourine/ file
385 '
386
387 test_expect_success 'blame -L ^/RE/ (absolute: end-of-file)' '
388 n=$(expr $(wc -l <file) + 1) &&
389 check_count -L$n -L^/$/,+2 A 1 C 1 E 1
390 '
391
392 test_expect_success 'setup -L :regex' '
393 tr Q "\\t" >hello.c <<-\EOF &&
394 int main(int argc, const char *argv[])
395 {
396 Qputs("hello");
397 }
398 EOF
399 git add hello.c &&
400 GIT_AUTHOR_NAME="F" GIT_AUTHOR_EMAIL="F@test.git" \
401 git commit -m "hello" &&
402
403 mv hello.c hello.orig &&
404 sed -e "/}/ {x; s/$/Qputs(\"goodbye\");/; G;}" <hello.orig |
405 tr Q "\\t" >hello.c &&
406 GIT_AUTHOR_NAME="G" GIT_AUTHOR_EMAIL="G@test.git" \
407 git commit -a -m "goodbye" &&
408
409 mv hello.c hello.orig &&
410 echo "#include <stdio.h>" >hello.c &&
411 cat hello.orig >>hello.c &&
412 tr Q "\\t" >>hello.c <<-\EOF &&
413 void mail()
414 {
415 Qputs("mail");
416 }
417 EOF
418 GIT_AUTHOR_NAME="H" GIT_AUTHOR_EMAIL="H@test.git" \
419 git commit -a -m "mail"
420 '
421
422 test_expect_success 'blame -L :literal' '
423 check_count -f hello.c -L:main F 4 G 1
424 '
425
426 test_expect_success 'blame -L :regex' '
427 check_count -f hello.c "-L:m[a-z][a-z]l" H 4
428 '
429
430 test_expect_success 'blame -L :nomatch' '
431 test_must_fail $PROG -L:nomatch hello.c
432 '
433
434 test_expect_success 'blame -L :RE (relative)' '
435 check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
436 '
437
438 test_expect_success 'blame -L :RE (relative: no preceding range)' '
439 check_count -f hello.c -L:ma.. F 4 G 1
440 '
441
442 test_expect_success 'blame -L :RE (relative: not found)' '
443 test_must_fail $PROG -L3,3 -L:tambourine hello.c
444 '
445
446 test_expect_success 'blame -L :RE (relative: end-of-file)' '
447 test_must_fail $PROG -L, -L:main hello.c
448 '
449
450 test_expect_success 'blame -L ^:RE (absolute)' '
451 check_count -f hello.c -L3,3 -L^:ma.. F 4 G 1
452 '
453
454 test_expect_success 'blame -L ^:RE (absolute: no preceding range)' '
455 check_count -f hello.c -L^:ma.. F 4 G 1
456 '
457
458 test_expect_success 'blame -L ^:RE (absolute: not found)' '
459 test_must_fail $PROG -L4,4 -L^:tambourine hello.c
460 '
461
462 test_expect_success 'blame -L ^:RE (absolute: end-of-file)' '
463 n=$(printf "%d" $(wc -l <hello.c)) &&
464 check_count -f hello.c -L$n -L^:ma.. F 4 G 1 H 1
465 '
466
467 test_expect_success 'setup incremental' '
468 (
469 GIT_AUTHOR_NAME=I &&
470 export GIT_AUTHOR_NAME &&
471 GIT_AUTHOR_EMAIL=I@test.git &&
472 export GIT_AUTHOR_EMAIL &&
473 >incremental &&
474 git add incremental &&
475 git commit -m "step 0" &&
476 printf "partial" >>incremental &&
477 git commit -a -m "step 0.5" &&
478 echo >>incremental &&
479 git commit -a -m "step 1"
480 )
481 '
482
483 test_expect_success 'blame empty' '
484 check_count -h HEAD^^ -f incremental
485 '
486
487 test_expect_success 'blame -L 0 empty' '
488 test_must_fail $PROG -L0 incremental HEAD^^
489 '
490
491 test_expect_success 'blame -L 1 empty' '
492 test_must_fail $PROG -L1 incremental HEAD^^
493 '
494
495 test_expect_success 'blame -L 2 empty' '
496 test_must_fail $PROG -L2 incremental HEAD^^
497 '
498
499 test_expect_success 'blame half' '
500 check_count -h HEAD^ -f incremental I 1
501 '
502
503 test_expect_success 'blame -L 0 half' '
504 test_must_fail $PROG -L0 incremental HEAD^
505 '
506
507 test_expect_success 'blame -L 1 half' '
508 check_count -h HEAD^ -f incremental -L1 I 1
509 '
510
511 test_expect_success 'blame -L 2 half' '
512 test_must_fail $PROG -L2 incremental HEAD^
513 '
514
515 test_expect_success 'blame -L 3 half' '
516 test_must_fail $PROG -L3 incremental HEAD^
517 '
518
519 test_expect_success 'blame full' '
520 check_count -f incremental I 1
521 '
522
523 test_expect_success 'blame -L 0 full' '
524 test_must_fail $PROG -L0 incremental
525 '
526
527 test_expect_success 'blame -L 1 full' '
528 check_count -f incremental -L1 I 1
529 '
530
531 test_expect_success 'blame -L 2 full' '
532 test_must_fail $PROG -L2 incremental
533 '
534
535 test_expect_success 'blame -L 3 full' '
536 test_must_fail $PROG -L3 incremental
537 '
538
539 test_expect_success 'blame -L' '
540 test_must_fail $PROG -L file
541 '
542
543 test_expect_success 'blame -L X,+' '
544 test_must_fail $PROG -L1,+ file
545 '
546
547 test_expect_success 'blame -L X,-' '
548 test_must_fail $PROG -L1,- file
549 '
550
551 test_expect_success 'blame -L X (non-numeric X)' '
552 test_must_fail $PROG -LX file
553 '
554
555 test_expect_success 'blame -L X,Y (non-numeric Y)' '
556 test_must_fail $PROG -L1,Y file
557 '
558
559 test_expect_success 'blame -L X,+N (non-numeric N)' '
560 test_must_fail $PROG -L1,+N file
561 '
562
563 test_expect_success 'blame -L X,-N (non-numeric N)' '
564 test_must_fail $PROG -L1,-N file
565 '
566
567 test_expect_success 'blame -L ,^/RE/' '
568 test_must_fail $PROG -L1,^/99/ file
569 '