]>
Commit | Line | Data |
---|---|---|
f25b7939 JH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Junio C Hamano | |
4 | # | |
5 | ||
0d042fec | 6 | test_description='git grep various. |
f25b7939 JH |
7 | ' |
8 | ||
9 | . ./test-lib.sh | |
10 | ||
2944e4e6 RS |
11 | cat >hello.c <<EOF |
12 | #include <stdio.h> | |
13 | int main(int argc, const char **argv) | |
14 | { | |
15 | printf("Hello world.\n"); | |
16 | return 0; | |
5183bf67 | 17 | /* char ?? */ |
2944e4e6 RS |
18 | } |
19 | EOF | |
20 | ||
f25b7939 JH |
21 | test_expect_success setup ' |
22 | { | |
23 | echo foo mmap bar | |
24 | echo foo_mmap bar | |
25 | echo foo_mmap bar mmap | |
26 | echo foo mmap bar_mmap | |
27 | echo foo_mmap bar mmap baz | |
28 | } >file && | |
a91f453f | 29 | echo vvv >v && |
dbb6a4ad | 30 | echo ww w >w && |
f25b7939 JH |
31 | echo x x xx x >x && |
32 | echo y yy >y && | |
33 | echo zzz > z && | |
0d042fec JH |
34 | mkdir t && |
35 | echo test >t/t && | |
a91f453f MK |
36 | echo vvv >t/v && |
37 | mkdir t/a && | |
38 | echo vvv >t/a/v && | |
39 | git add . && | |
a4d7d2c6 | 40 | test_tick && |
f25b7939 JH |
41 | git commit -m initial |
42 | ' | |
43 | ||
c922b01f LT |
44 | test_expect_success 'grep should not segfault with a bad input' ' |
45 | test_must_fail git grep "(" | |
46 | ' | |
47 | ||
f25b7939 JH |
48 | for H in HEAD '' |
49 | do | |
50 | case "$H" in | |
51 | HEAD) HC='HEAD:' L='HEAD' ;; | |
52 | '') HC= L='in working tree' ;; | |
53 | esac | |
54 | ||
55 | test_expect_success "grep -w $L" ' | |
56 | { | |
57 | echo ${HC}file:1:foo mmap bar | |
58 | echo ${HC}file:3:foo_mmap bar mmap | |
59 | echo ${HC}file:4:foo mmap bar_mmap | |
60 | echo ${HC}file:5:foo_mmap bar mmap baz | |
61 | } >expected && | |
62 | git grep -n -w -e mmap $H >actual && | |
4fdf71be | 63 | test_cmp expected actual |
f25b7939 JH |
64 | ' |
65 | ||
dbb6a4ad RS |
66 | test_expect_success "grep -w $L (w)" ' |
67 | : >expected && | |
ce14e0b2 | 68 | test_must_fail git grep -n -w -e "^w" >actual && |
dbb6a4ad RS |
69 | test_cmp expected actual |
70 | ' | |
71 | ||
f25b7939 JH |
72 | test_expect_success "grep -w $L (x)" ' |
73 | { | |
74 | echo ${HC}x:1:x x xx x | |
75 | } >expected && | |
76 | git grep -n -w -e "x xx* x" $H >actual && | |
4fdf71be | 77 | test_cmp expected actual |
f25b7939 JH |
78 | ' |
79 | ||
80 | test_expect_success "grep -w $L (y-1)" ' | |
81 | { | |
82 | echo ${HC}y:1:y yy | |
83 | } >expected && | |
84 | git grep -n -w -e "^y" $H >actual && | |
4fdf71be | 85 | test_cmp expected actual |
f25b7939 JH |
86 | ' |
87 | ||
88 | test_expect_success "grep -w $L (y-2)" ' | |
89 | : >expected && | |
90 | if git grep -n -w -e "^y y" $H >actual | |
91 | then | |
92 | echo should not have matched | |
93 | cat actual | |
94 | false | |
95 | else | |
4fdf71be | 96 | test_cmp expected actual |
f25b7939 JH |
97 | fi |
98 | ' | |
99 | ||
100 | test_expect_success "grep -w $L (z)" ' | |
101 | : >expected && | |
102 | if git grep -n -w -e "^z" $H >actual | |
103 | then | |
104 | echo should not have matched | |
105 | cat actual | |
106 | false | |
107 | else | |
4fdf71be | 108 | test_cmp expected actual |
f25b7939 JH |
109 | fi |
110 | ' | |
0d042fec JH |
111 | |
112 | test_expect_success "grep $L (t-1)" ' | |
113 | echo "${HC}t/t:1:test" >expected && | |
114 | git grep -n -e test $H >actual && | |
4fdf71be | 115 | test_cmp expected actual |
0d042fec JH |
116 | ' |
117 | ||
118 | test_expect_success "grep $L (t-2)" ' | |
119 | echo "${HC}t:1:test" >expected && | |
120 | ( | |
121 | cd t && | |
122 | git grep -n -e test $H | |
123 | ) >actual && | |
4fdf71be | 124 | test_cmp expected actual |
0d042fec JH |
125 | ' |
126 | ||
127 | test_expect_success "grep $L (t-3)" ' | |
128 | echo "${HC}t/t:1:test" >expected && | |
129 | ( | |
130 | cd t && | |
131 | git grep --full-name -n -e test $H | |
132 | ) >actual && | |
4fdf71be | 133 | test_cmp expected actual |
0d042fec JH |
134 | ' |
135 | ||
41ac414e | 136 | test_expect_success "grep -c $L (no /dev/null)" ' |
87539416 | 137 | ! git grep -c test $H | grep /dev/null |
d99ebf08 JH |
138 | ' |
139 | ||
a91f453f MK |
140 | test_expect_success "grep --max-depth -1 $L" ' |
141 | { | |
142 | echo ${HC}t/a/v:1:vvv | |
143 | echo ${HC}t/v:1:vvv | |
144 | echo ${HC}v:1:vvv | |
145 | } >expected && | |
146 | git grep --max-depth -1 -n -e vvv $H >actual && | |
147 | test_cmp expected actual | |
148 | ' | |
149 | ||
150 | test_expect_success "grep --max-depth 0 $L" ' | |
151 | { | |
152 | echo ${HC}v:1:vvv | |
153 | } >expected && | |
154 | git grep --max-depth 0 -n -e vvv $H >actual && | |
155 | test_cmp expected actual | |
156 | ' | |
157 | ||
158 | test_expect_success "grep --max-depth 0 -- '*' $L" ' | |
159 | { | |
160 | echo ${HC}t/a/v:1:vvv | |
161 | echo ${HC}t/v:1:vvv | |
162 | echo ${HC}v:1:vvv | |
163 | } >expected && | |
164 | git grep --max-depth 0 -n -e vvv $H -- "*" >actual && | |
165 | test_cmp expected actual | |
166 | ' | |
167 | ||
168 | test_expect_success "grep --max-depth 1 $L" ' | |
169 | { | |
170 | echo ${HC}t/v:1:vvv | |
171 | echo ${HC}v:1:vvv | |
172 | } >expected && | |
173 | git grep --max-depth 1 -n -e vvv $H >actual && | |
174 | test_cmp expected actual | |
175 | ' | |
176 | ||
177 | test_expect_success "grep --max-depth 0 -- t $L" ' | |
178 | { | |
179 | echo ${HC}t/v:1:vvv | |
180 | } >expected && | |
181 | git grep --max-depth 0 -n -e vvv $H -- t >actual && | |
182 | test_cmp expected actual | |
183 | ' | |
184 | ||
b31f6880 NTND |
185 | test_expect_success "grep --max-depth 0 -- . t $L" ' |
186 | { | |
187 | echo ${HC}t/v:1:vvv | |
188 | echo ${HC}v:1:vvv | |
189 | } >expected && | |
190 | git grep --max-depth 0 -n -e vvv $H -- . t >actual && | |
191 | test_cmp expected actual | |
192 | ' | |
193 | ||
194 | test_expect_success "grep --max-depth 0 -- t . $L" ' | |
195 | { | |
196 | echo ${HC}t/v:1:vvv | |
197 | echo ${HC}v:1:vvv | |
198 | } >expected && | |
199 | git grep --max-depth 0 -n -e vvv $H -- t . >actual && | |
200 | test_cmp expected actual | |
201 | ' | |
202 | ||
f25b7939 JH |
203 | done |
204 | ||
0f705046 TR |
205 | cat >expected <<EOF |
206 | file:foo mmap bar_mmap | |
207 | EOF | |
208 | ||
209 | test_expect_success 'grep -e A --and -e B' ' | |
210 | git grep -e "foo mmap" --and -e bar_mmap >actual && | |
211 | test_cmp expected actual | |
212 | ' | |
213 | ||
214 | cat >expected <<EOF | |
215 | file:foo_mmap bar mmap | |
216 | file:foo_mmap bar mmap baz | |
217 | EOF | |
218 | ||
219 | ||
220 | test_expect_success 'grep ( -e A --or -e B ) --and -e B' ' | |
221 | git grep \( -e foo_ --or -e baz \) \ | |
222 | --and -e " mmap" >actual && | |
223 | test_cmp expected actual | |
224 | ' | |
225 | ||
226 | cat >expected <<EOF | |
227 | file:foo mmap bar | |
228 | EOF | |
229 | ||
230 | test_expect_success 'grep -e A --and --not -e B' ' | |
231 | git grep -e "foo mmap" --and --not -e bar_mmap >actual && | |
232 | test_cmp expected actual | |
233 | ' | |
234 | ||
b4827599 RS |
235 | test_expect_success 'grep should ignore GREP_OPTIONS' ' |
236 | GREP_OPTIONS=-v git grep " mmap bar\$" >actual && | |
237 | test_cmp expected actual | |
238 | ' | |
239 | ||
cfe370c6 MK |
240 | test_expect_success 'grep -f, non-existent file' ' |
241 | test_must_fail git grep -f patterns | |
242 | ' | |
243 | ||
244 | cat >expected <<EOF | |
245 | file:foo mmap bar | |
246 | file:foo_mmap bar | |
247 | file:foo_mmap bar mmap | |
248 | file:foo mmap bar_mmap | |
249 | file:foo_mmap bar mmap baz | |
250 | EOF | |
251 | ||
252 | cat >pattern <<EOF | |
253 | mmap | |
254 | EOF | |
255 | ||
256 | test_expect_success 'grep -f, one pattern' ' | |
257 | git grep -f pattern >actual && | |
258 | test_cmp expected actual | |
259 | ' | |
260 | ||
261 | cat >expected <<EOF | |
262 | file:foo mmap bar | |
263 | file:foo_mmap bar | |
264 | file:foo_mmap bar mmap | |
265 | file:foo mmap bar_mmap | |
266 | file:foo_mmap bar mmap baz | |
267 | t/a/v:vvv | |
268 | t/v:vvv | |
269 | v:vvv | |
270 | EOF | |
271 | ||
272 | cat >patterns <<EOF | |
273 | mmap | |
274 | vvv | |
275 | EOF | |
276 | ||
277 | test_expect_success 'grep -f, multiple patterns' ' | |
278 | git grep -f patterns >actual && | |
279 | test_cmp expected actual | |
280 | ' | |
281 | ||
282 | cat >expected <<EOF | |
283 | file:foo mmap bar | |
284 | file:foo_mmap bar | |
285 | file:foo_mmap bar mmap | |
286 | file:foo mmap bar_mmap | |
287 | file:foo_mmap bar mmap baz | |
288 | t/a/v:vvv | |
289 | t/v:vvv | |
290 | v:vvv | |
291 | EOF | |
292 | ||
293 | cat >patterns <<EOF | |
294 | ||
295 | mmap | |
296 | ||
297 | vvv | |
298 | ||
299 | EOF | |
300 | ||
301 | test_expect_success 'grep -f, ignore empty lines' ' | |
302 | git grep -f patterns >actual && | |
303 | test_cmp expected actual | |
304 | ' | |
305 | ||
c41dd2fd RS |
306 | test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' ' |
307 | git grep -f - <patterns >actual && | |
308 | test_cmp expected actual | |
309 | ' | |
310 | ||
046802d0 RS |
311 | cat >expected <<EOF |
312 | y:y yy | |
313 | -- | |
314 | z:zzz | |
315 | EOF | |
316 | ||
4ff61c21 JH |
317 | test_expect_success 'grep -q, silently report matches' ' |
318 | >empty && | |
319 | git grep -q mmap >actual && | |
320 | test_cmp empty actual && | |
321 | test_must_fail git grep -q qfwfq >actual && | |
322 | test_cmp empty actual | |
323 | ' | |
324 | ||
046802d0 RS |
325 | # Create 1024 file names that sort between "y" and "z" to make sure |
326 | # the two files are handled by different calls to an external grep. | |
327 | # This depends on MAXARGS in builtin-grep.c being 1024 or less. | |
328 | c32="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v" | |
329 | test_expect_success 'grep -C1, hunk mark between files' ' | |
330 | for a in $c32; do for b in $c32; do : >y-$a$b; done; done && | |
331 | git add y-?? && | |
332 | git grep -C1 "^[yz]" >actual && | |
333 | test_cmp expected actual | |
334 | ' | |
335 | ||
bbc09c22 JH |
336 | test_expect_success 'grep -C1 hunk mark between files' ' |
337 | git grep -C1 "^[yz]" >actual && | |
046802d0 RS |
338 | test_cmp expected actual |
339 | ' | |
340 | ||
a4d7d2c6 JH |
341 | test_expect_success 'log grep setup' ' |
342 | echo a >>file && | |
343 | test_tick && | |
344 | GIT_AUTHOR_NAME="With * Asterisk" \ | |
345 | GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \ | |
346 | git commit -a -m "second" && | |
347 | ||
348 | echo a >>file && | |
349 | test_tick && | |
5aaeb733 | 350 | git commit -a -m "third" && |
a4d7d2c6 | 351 | |
5aaeb733 JH |
352 | echo a >>file && |
353 | test_tick && | |
354 | GIT_AUTHOR_NAME="Night Fall" \ | |
355 | GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \ | |
356 | git commit -a -m "fourth" | |
a4d7d2c6 JH |
357 | ' |
358 | ||
359 | test_expect_success 'log grep (1)' ' | |
360 | git log --author=author --pretty=tformat:%s >actual && | |
361 | ( echo third ; echo initial ) >expect && | |
362 | test_cmp expect actual | |
363 | ' | |
364 | ||
365 | test_expect_success 'log grep (2)' ' | |
366 | git log --author=" * " -F --pretty=tformat:%s >actual && | |
367 | ( echo second ) >expect && | |
368 | test_cmp expect actual | |
369 | ' | |
370 | ||
371 | test_expect_success 'log grep (3)' ' | |
372 | git log --author="^A U" --pretty=tformat:%s >actual && | |
373 | ( echo third ; echo initial ) >expect && | |
374 | test_cmp expect actual | |
375 | ' | |
376 | ||
377 | test_expect_success 'log grep (4)' ' | |
378 | git log --author="frotz\.com>$" --pretty=tformat:%s >actual && | |
379 | ( echo second ) >expect && | |
380 | test_cmp expect actual | |
381 | ' | |
382 | ||
383 | test_expect_success 'log grep (5)' ' | |
80235ba7 | 384 | git log --author=Thor -F --pretty=tformat:%s >actual && |
a4d7d2c6 JH |
385 | ( echo third ; echo initial ) >expect && |
386 | test_cmp expect actual | |
387 | ' | |
388 | ||
389 | test_expect_success 'log grep (6)' ' | |
390 | git log --author=-0700 --pretty=tformat:%s >actual && | |
391 | >expect && | |
392 | test_cmp expect actual | |
57d43466 | 393 | ' |
a4d7d2c6 | 394 | |
80235ba7 JH |
395 | test_expect_success 'log --grep --author implicitly uses all-match' ' |
396 | # grep matches initial and second but not third | |
397 | # author matches only initial and third | |
398 | git log --author="A U Thor" --grep=s --grep=l --format=%s >actual && | |
399 | echo initial >expect && | |
400 | test_cmp expect actual | |
401 | ' | |
402 | ||
5aaeb733 JH |
403 | test_expect_success 'log with multiple --author uses union' ' |
404 | git log --author="Thor" --author="Aster" --format=%s >actual && | |
405 | { | |
406 | echo third && echo second && echo initial | |
407 | } >expect && | |
408 | test_cmp expect actual | |
409 | ' | |
410 | ||
411 | test_expect_success 'log with --grep and multiple --author uses all-match' ' | |
412 | git log --author="Thor" --author="Night" --grep=i --format=%s >actual && | |
413 | { | |
414 | echo third && echo initial | |
415 | } >expect && | |
416 | test_cmp expect actual | |
417 | ' | |
418 | ||
419 | test_expect_success 'log with --grep and multiple --author uses all-match' ' | |
420 | git log --author="Thor" --author="Night" --grep=q --format=%s >actual && | |
421 | >expect && | |
422 | test_cmp expect actual | |
423 | ' | |
424 | ||
57d43466 NTND |
425 | test_expect_success 'grep with CE_VALID file' ' |
426 | git update-index --assume-unchanged t/t && | |
427 | rm t/t && | |
bbc09c22 | 428 | test "$(git grep test)" = "t/t:test" && |
57d43466 NTND |
429 | git update-index --no-assume-unchanged t/t && |
430 | git checkout t/t | |
a4d7d2c6 JH |
431 | ' |
432 | ||
60ecac98 RS |
433 | cat >expected <<EOF |
434 | hello.c=#include <stdio.h> | |
435 | hello.c: return 0; | |
436 | EOF | |
437 | ||
438 | test_expect_success 'grep -p with userdiff' ' | |
439 | git config diff.custom.funcname "^#" && | |
440 | echo "hello.c diff=custom" >.gitattributes && | |
441 | git grep -p return >actual && | |
442 | test_cmp expected actual | |
443 | ' | |
444 | ||
2944e4e6 RS |
445 | cat >expected <<EOF |
446 | hello.c=int main(int argc, const char **argv) | |
447 | hello.c: return 0; | |
448 | EOF | |
449 | ||
450 | test_expect_success 'grep -p' ' | |
60ecac98 | 451 | rm -f .gitattributes && |
2944e4e6 RS |
452 | git grep -p return >actual && |
453 | test_cmp expected actual | |
454 | ' | |
455 | ||
456 | cat >expected <<EOF | |
457 | hello.c-#include <stdio.h> | |
458 | hello.c=int main(int argc, const char **argv) | |
459 | hello.c-{ | |
460 | hello.c- printf("Hello world.\n"); | |
461 | hello.c: return 0; | |
462 | EOF | |
463 | ||
464 | test_expect_success 'grep -p -B5' ' | |
465 | git grep -p -B5 return >actual && | |
466 | test_cmp expected actual | |
467 | ' | |
468 | ||
493b7a08 CB |
469 | test_expect_success 'grep from a subdirectory to search wider area (1)' ' |
470 | mkdir -p s && | |
471 | ( | |
472 | cd s && git grep "x x x" .. | |
473 | ) | |
474 | ' | |
475 | ||
476 | test_expect_success 'grep from a subdirectory to search wider area (2)' ' | |
477 | mkdir -p s && | |
478 | ( | |
479 | cd s || exit 1 | |
480 | ( git grep xxyyzz .. >out ; echo $? >status ) | |
481 | ! test -s out && | |
482 | test 1 = $(cat status) | |
483 | ) | |
484 | ' | |
485 | ||
5183bf67 BC |
486 | cat >expected <<EOF |
487 | hello.c:int main(int argc, const char **argv) | |
488 | EOF | |
489 | ||
490 | test_expect_success 'grep -Fi' ' | |
491 | git grep -Fi "CHAR *" >actual && | |
492 | test_cmp expected actual | |
493 | ' | |
494 | ||
59332d13 JH |
495 | test_expect_success 'outside of git repository' ' |
496 | rm -fr non && | |
497 | mkdir -p non/git/sub && | |
498 | echo hello >non/git/file1 && | |
499 | echo world >non/git/sub/file2 && | |
500 | echo ".*o*" >non/git/.gitignore && | |
501 | { | |
502 | echo file1:hello && | |
503 | echo sub/file2:world | |
504 | } >non/expect.full && | |
a48fcd83 | 505 | echo file2:world >non/expect.sub && |
59332d13 JH |
506 | ( |
507 | GIT_CEILING_DIRECTORIES="$(pwd)/non/git" && | |
508 | export GIT_CEILING_DIRECTORIES && | |
509 | cd non/git && | |
510 | test_must_fail git grep o && | |
511 | git grep --no-index o >../actual.full && | |
512 | test_cmp ../expect.full ../actual.full | |
513 | cd sub && | |
514 | test_must_fail git grep o && | |
515 | git grep --no-index o >../../actual.sub && | |
516 | test_cmp ../../expect.sub ../../actual.sub | |
517 | ) | |
518 | ' | |
519 | ||
520 | test_expect_success 'inside git repository but with --no-index' ' | |
521 | rm -fr is && | |
522 | mkdir -p is/git/sub && | |
523 | echo hello >is/git/file1 && | |
524 | echo world >is/git/sub/file2 && | |
525 | echo ".*o*" >is/git/.gitignore && | |
526 | { | |
527 | echo file1:hello && | |
528 | echo sub/file2:world | |
529 | } >is/expect.full && | |
530 | : >is/expect.empty && | |
a48fcd83 | 531 | echo file2:world >is/expect.sub && |
59332d13 JH |
532 | ( |
533 | cd is/git && | |
534 | git init && | |
535 | test_must_fail git grep o >../actual.full && | |
536 | test_cmp ../expect.empty ../actual.full && | |
537 | git grep --no-index o >../actual.full && | |
538 | test_cmp ../expect.full ../actual.full && | |
539 | cd sub && | |
540 | test_must_fail git grep o >../../actual.sub && | |
541 | test_cmp ../../expect.empty ../../actual.sub && | |
542 | git grep --no-index o >../../actual.sub && | |
543 | test_cmp ../../expect.sub ../../actual.sub | |
544 | ) | |
545 | ' | |
546 | ||
1123c67c JK |
547 | test_expect_success 'setup double-dash tests' ' |
548 | cat >double-dash <<EOF && | |
549 | -- | |
550 | -> | |
551 | other | |
552 | EOF | |
553 | git add double-dash | |
554 | ' | |
555 | ||
556 | cat >expected <<EOF | |
557 | double-dash:-> | |
558 | EOF | |
559 | test_expect_success 'grep -- pattern' ' | |
560 | git grep -- "->" >actual && | |
561 | test_cmp expected actual | |
562 | ' | |
563 | test_expect_success 'grep -- pattern -- pathspec' ' | |
564 | git grep -- "->" -- double-dash >actual && | |
565 | test_cmp expected actual | |
566 | ' | |
567 | test_expect_success 'grep -e pattern -- path' ' | |
568 | git grep -e "->" -- double-dash >actual && | |
569 | test_cmp expected actual | |
570 | ' | |
571 | ||
572 | cat >expected <<EOF | |
573 | double-dash:-- | |
574 | EOF | |
575 | test_expect_success 'grep -e -- -- path' ' | |
576 | git grep -e -- -- double-dash >actual && | |
577 | test_cmp expected actual | |
578 | ' | |
579 | ||
f25b7939 | 580 | test_done |