Merge branch 'jk/cat-file-batch-optim'
[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 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}++;
27                         }
28                 }
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";
37                         }
38                         else {
39                                 $ok = "good";
40                         }
41                         print STDERR "Author $author (expected $value, attributed $count) $ok\n";
42                 }
43                 exit($bad);
44         ' "$@" <actual
45 }
46
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."
53 '
54
55 test_expect_success 'blame 1 author' '
56         check_count A 2
57 '
58
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."
64 '
65
66 test_expect_success 'blame 2 authors' '
67         check_count A 2 B 2
68 '
69
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"
76 '
77
78 test_expect_success 'blame 2 authors + 1 branch1 author' '
79         check_count A 2 B 2 B1 2
80 '
81
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"
88 '
89
90 test_expect_success 'blame 2 authors + 1 branch2 author' '
91         check_count A 2 B 1 B2 1
92 '
93
94 test_expect_success 'merge branch1 & branch2' '
95         git pull . branch1
96 '
97
98 test_expect_success 'blame 2 authors + 2 merged-in authors' '
99         check_count A 2 B 1 B1 2 B2 1
100 '
101
102 test_expect_success 'blame ancestor' '
103         check_count -h master A 2 B 2
104 '
105
106 test_expect_success 'blame great-ancestor' '
107         check_count -h master^ A 2
108 '
109
110 test_expect_success 'setup evil merge' '
111         echo "evil merge." >>file &&
112         git commit -a --amend
113 '
114
115 test_expect_success 'blame evil merge' '
116         check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1
117 '
118
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"
123 '
124
125 test_expect_success 'blame incomplete line' '
126         check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
127 '
128
129 test_expect_success 'setup edits' '
130         mv file file.orig &&
131         {
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"
138 '
139
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
142 '
143
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"
150 '
151
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
154 '
155
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
158 '
159
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
162 '
163
164 test_expect_success 'blame -L X (X to end)' '
165         check_count -L5 B1 1 C 1 D 1 "A U Thor" 1
166 '
167
168 test_expect_success 'blame -L X, (X to end)' '
169         check_count -L5, B1 1 C 1 D 1 "A U Thor" 1
170 '
171
172 test_expect_success 'blame -L ,Y (up to Y)' '
173         check_count -L,3 A 1 B2 1 E 1
174 '
175
176 test_expect_success 'blame -L X,X' '
177         check_count -L3,3 B2 1
178 '
179
180 test_expect_success 'blame -L X,Y' '
181         check_count -L3,6 B 1 B1 1 B2 1 D 1
182 '
183
184 test_expect_success 'blame -L Y,X (undocumented)' '
185         check_count -L6,3 B 1 B1 1 B2 1 D 1
186 '
187
188 test_expect_success 'blame -L ,+0' '
189         test_must_fail $PROG -L,+0 file
190 '
191
192 test_expect_success 'blame -L X,+0' '
193         test_must_fail $PROG -L1,+0 file
194 '
195
196 test_expect_success 'blame -L X,+1' '
197         check_count -L3,+1 B2 1
198 '
199
200 test_expect_success 'blame -L X,+N' '
201         check_count -L3,+4 B 1 B1 1 B2 1 D 1
202 '
203
204 test_expect_success 'blame -L ,-0' '
205         test_must_fail $PROG -L,-0 file
206 '
207
208 test_expect_success 'blame -L X,-0' '
209         test_must_fail $PROG -L1,-0 file
210 '
211
212 test_expect_success 'blame -L X,-1' '
213         check_count -L3,-1 B2 1
214 '
215
216 test_expect_success 'blame -L X,-N' '
217         check_count -L6,-4 B 1 B1 1 B2 1 D 1
218 '
219
220 test_expect_success 'blame -L /RE/ (RE to end)' '
221         check_count -L/evil/ C 1 "A U Thor" 1
222 '
223
224 test_expect_success 'blame -L /RE/,/RE2/' '
225         check_count -L/robot/,/green/ A 1 B 1 B2 1 D 1 E 1
226 '
227
228 test_expect_success 'blame -L X,/RE/' '
229         check_count -L5,/evil/ B1 1 D 1 "A U Thor" 1
230 '
231
232 test_expect_success 'blame -L /RE/,Y' '
233         check_count -L/99/,7 B1 1 D 1 "A U Thor" 1
234 '
235
236 test_expect_success 'blame -L /RE/,+N' '
237         check_count -L/99/,+3 B1 1 D 1 "A U Thor" 1
238 '
239
240 test_expect_success 'blame -L /RE/,-N' '
241         check_count -L/99/,-3 B 1 B2 1 D 1
242 '
243
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
249 '
250
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
254 '
255
256 test_expect_success 'blame -L X (X > nlines)' '
257         test_must_fail $PROG -L12345 file
258 '
259
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
263 '
264
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
268 '
269
270 test_expect_success 'blame -L ,Y (Y > nlines)' '
271         test_must_fail $PROG -L,12345 file
272 '
273
274 test_expect_success 'setup -L :regex' '
275         tr Q "\\t" >hello.c <<-\EOF &&
276         int main(int argc, const char *argv[])
277         {
278         Qputs("hello");
279         }
280         EOF
281         git add hello.c &&
282         GIT_AUTHOR_NAME="F" GIT_AUTHOR_EMAIL="F@test.git" \
283         git commit -m "hello" &&
284
285         mv hello.c hello.orig &&
286         sed -e "/}/ {x; s/$/Qputs(\"goodbye\");/; G;}" <hello.orig |
287         tr Q "\\t" >hello.c &&
288         GIT_AUTHOR_NAME="G" GIT_AUTHOR_EMAIL="G@test.git" \
289         git commit -a -m "goodbye" &&
290
291         mv hello.c hello.orig &&
292         echo "#include <stdio.h>" >hello.c &&
293         cat hello.orig >>hello.c &&
294         tr Q "\\t" >>hello.c <<-\EOF
295         void mail()
296         {
297         Qputs("mail");
298         }
299         EOF
300         GIT_AUTHOR_NAME="H" GIT_AUTHOR_EMAIL="H@test.git" \
301         git commit -a -m "mail"
302 '
303
304 test_expect_success 'blame -L :literal' '
305         check_count -f hello.c -L:main F 4 G 1
306 '
307
308 test_expect_success 'blame -L :regex' '
309         check_count -f hello.c "-L:m[a-z][a-z]l" H 4
310 '
311
312 test_expect_success 'blame -L :nomatch' '
313         test_must_fail $PROG -L:nomatch hello.c
314 '
315
316 test_expect_success 'setup incremental' '
317         (
318         GIT_AUTHOR_NAME=I &&
319         export GIT_AUTHOR_NAME &&
320         GIT_AUTHOR_EMAIL=I@test.git &&
321         export GIT_AUTHOR_EMAIL &&
322         >incremental &&
323         git add incremental &&
324         git commit -m "step 0" &&
325         printf "partial" >>incremental &&
326         git commit -a -m "step 0.5" &&
327         echo >>incremental &&
328         git commit -a -m "step 1"
329         )
330 '
331
332 test_expect_success 'blame empty' '
333         check_count -h HEAD^^ -f incremental
334 '
335
336 test_expect_success 'blame -L 0 empty (undocumented)' '
337         check_count -h HEAD^^ -f incremental -L0
338 '
339
340 test_expect_success 'blame -L 1 empty' '
341         test_must_fail $PROG -L1 incremental HEAD^^
342 '
343
344 test_expect_success 'blame -L 2 empty' '
345         test_must_fail $PROG -L2 incremental HEAD^^
346 '
347
348 test_expect_success 'blame half' '
349         check_count -h HEAD^ -f incremental I 1
350 '
351
352 test_expect_success 'blame -L 0 half (undocumented)' '
353         check_count -h HEAD^ -f incremental -L0 I 1
354 '
355
356 test_expect_success 'blame -L 1 half' '
357         check_count -h HEAD^ -f incremental -L1 I 1
358 '
359
360 test_expect_success 'blame -L 2 half' '
361         test_must_fail $PROG -L2 incremental HEAD^
362 '
363
364 test_expect_success 'blame -L 3 half' '
365         test_must_fail $PROG -L3 incremental HEAD^
366 '
367
368 test_expect_success 'blame full' '
369         check_count -f incremental I 1
370 '
371
372 test_expect_success 'blame -L 0 full (undocumented)' '
373         check_count -f incremental -L0 I 1
374 '
375
376 test_expect_success 'blame -L 1 full' '
377         check_count -f incremental -L1 I 1
378 '
379
380 test_expect_success 'blame -L 2 full' '
381         test_must_fail $PROG -L2 incremental
382 '
383
384 test_expect_success 'blame -L 3 full' '
385         test_must_fail $PROG -L3 incremental
386 '
387
388 test_expect_success 'blame -L' '
389         test_must_fail $PROG -L file
390 '
391
392 test_expect_success 'blame -L X,+' '
393         test_must_fail $PROG -L1,+ file
394 '
395
396 test_expect_success 'blame -L X,-' '
397         test_must_fail $PROG -L1,- file
398 '
399
400 test_expect_success 'blame -L X (non-numeric X)' '
401         test_must_fail $PROG -LX file
402 '
403
404 test_expect_success 'blame -L X,Y (non-numeric Y)' '
405         test_must_fail $PROG -L1,Y file
406 '
407
408 test_expect_success 'blame -L X,+N (non-numeric N)' '
409         test_must_fail $PROG -L1,+N file
410 '
411
412 test_expect_success 'blame -L X,-N (non-numeric N)' '
413         test_must_fail $PROG -L1,-N file
414 '