grep: support newline separated pattern list
[git] / t / t7810-grep.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='git grep various.
7 '
8
9 . ./test-lib.sh
10
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;
17         /* char ?? */
18 }
19 EOF
20
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 &&
29         {
30                 echo Hello world
31                 echo HeLLo world
32                 echo Hello_world
33                 echo HeLLo_world
34         } >hello_world &&
35         {
36                 echo "a+b*c"
37                 echo "a+bc"
38                 echo "abc"
39         } >ab &&
40         echo vvv >v &&
41         echo ww w >w &&
42         echo x x xx x >x &&
43         echo y yy >y &&
44         echo zzz > z &&
45         mkdir t &&
46         echo test >t/t &&
47         echo vvv >t/v &&
48         mkdir t/a &&
49         echo vvv >t/a/v &&
50         git add . &&
51         test_tick &&
52         git commit -m initial
53 '
54
55 test_expect_success 'grep should not segfault with a bad input' '
56         test_must_fail git grep "("
57 '
58
59 for H in HEAD ''
60 do
61         case "$H" in
62         HEAD)   HC='HEAD:' L='HEAD' ;;
63         '')     HC= L='in working tree' ;;
64         esac
65
66         test_expect_success "grep -w $L" '
67                 {
68                         echo ${HC}file:1:foo mmap bar
69                         echo ${HC}file:3:foo_mmap bar mmap
70                         echo ${HC}file:4:foo mmap bar_mmap
71                         echo ${HC}file:5:foo_mmap bar mmap baz
72                 } >expected &&
73                 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
74                 test_cmp expected actual
75         '
76
77         test_expect_success "grep -w $L" '
78                 {
79                         echo ${HC}file:1:foo mmap bar
80                         echo ${HC}file:3:foo_mmap bar mmap
81                         echo ${HC}file:4:foo mmap bar_mmap
82                         echo ${HC}file:5:foo_mmap bar mmap baz
83                 } >expected &&
84                 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
85                 test_cmp expected actual
86         '
87
88         test_expect_success "grep -w $L" '
89                 {
90                         echo ${HC}file:foo mmap bar
91                         echo ${HC}file:foo_mmap bar mmap
92                         echo ${HC}file:foo mmap bar_mmap
93                         echo ${HC}file:foo_mmap bar mmap baz
94                 } >expected &&
95                 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
96                 test_cmp expected actual
97         '
98
99         test_expect_success "grep -w $L (w)" '
100                 : >expected &&
101                 test_must_fail git grep -n -w -e "^w" >actual &&
102                 test_cmp expected actual
103         '
104
105         test_expect_success "grep -w $L (x)" '
106                 {
107                         echo ${HC}x:1:x x xx x
108                 } >expected &&
109                 git grep -n -w -e "x xx* x" $H >actual &&
110                 test_cmp expected actual
111         '
112
113         test_expect_success "grep -w $L (y-1)" '
114                 {
115                         echo ${HC}y:1:y yy
116                 } >expected &&
117                 git grep -n -w -e "^y" $H >actual &&
118                 test_cmp expected actual
119         '
120
121         test_expect_success "grep -w $L (y-2)" '
122                 : >expected &&
123                 if git grep -n -w -e "^y y" $H >actual
124                 then
125                         echo should not have matched
126                         cat actual
127                         false
128                 else
129                         test_cmp expected actual
130                 fi
131         '
132
133         test_expect_success "grep -w $L (z)" '
134                 : >expected &&
135                 if git grep -n -w -e "^z" $H >actual
136                 then
137                         echo should not have matched
138                         cat actual
139                         false
140                 else
141                         test_cmp expected actual
142                 fi
143         '
144
145         test_expect_success "grep $L (t-1)" '
146                 echo "${HC}t/t:1:test" >expected &&
147                 git grep -n -e test $H >actual &&
148                 test_cmp expected actual
149         '
150
151         test_expect_success "grep $L (t-2)" '
152                 echo "${HC}t:1:test" >expected &&
153                 (
154                         cd t &&
155                         git grep -n -e test $H
156                 ) >actual &&
157                 test_cmp expected actual
158         '
159
160         test_expect_success "grep $L (t-3)" '
161                 echo "${HC}t/t:1:test" >expected &&
162                 (
163                         cd t &&
164                         git grep --full-name -n -e test $H
165                 ) >actual &&
166                 test_cmp expected actual
167         '
168
169         test_expect_success "grep -c $L (no /dev/null)" '
170                 ! git grep -c test $H | grep /dev/null
171         '
172
173         test_expect_success "grep --max-depth -1 $L" '
174                 {
175                         echo ${HC}t/a/v:1:vvv
176                         echo ${HC}t/v:1:vvv
177                         echo ${HC}v:1:vvv
178                 } >expected &&
179                 git grep --max-depth -1 -n -e vvv $H >actual &&
180                 test_cmp expected actual
181         '
182
183         test_expect_success "grep --max-depth 0 $L" '
184                 {
185                         echo ${HC}v:1:vvv
186                 } >expected &&
187                 git grep --max-depth 0 -n -e vvv $H >actual &&
188                 test_cmp expected actual
189         '
190
191         test_expect_success "grep --max-depth 0 -- '*' $L" '
192                 {
193                         echo ${HC}t/a/v:1:vvv
194                         echo ${HC}t/v:1:vvv
195                         echo ${HC}v:1:vvv
196                 } >expected &&
197                 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
198                 test_cmp expected actual
199         '
200
201         test_expect_success "grep --max-depth 1 $L" '
202                 {
203                         echo ${HC}t/v:1:vvv
204                         echo ${HC}v:1:vvv
205                 } >expected &&
206                 git grep --max-depth 1 -n -e vvv $H >actual &&
207                 test_cmp expected actual
208         '
209
210         test_expect_success "grep --max-depth 0 -- t $L" '
211                 {
212                         echo ${HC}t/v:1:vvv
213                 } >expected &&
214                 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
215                 test_cmp expected actual
216         '
217
218         test_expect_success "grep --max-depth 0 -- . t $L" '
219                 {
220                         echo ${HC}t/v:1:vvv
221                         echo ${HC}v:1:vvv
222                 } >expected &&
223                 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
224                 test_cmp expected actual
225         '
226
227         test_expect_success "grep --max-depth 0 -- t . $L" '
228                 {
229                         echo ${HC}t/v:1:vvv
230                         echo ${HC}v:1:vvv
231                 } >expected &&
232                 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
233                 test_cmp expected actual
234         '
235         test_expect_success "grep $L with grep.extendedRegexp=false" '
236                 echo "ab:a+bc" >expected &&
237                 git -c grep.extendedRegexp=false grep "a+b*c" ab >actual &&
238                 test_cmp expected actual
239         '
240
241         test_expect_success "grep $L with grep.extendedRegexp=true" '
242                 echo "ab:abc" >expected &&
243                 git -c grep.extendedRegexp=true grep "a+b*c" ab >actual &&
244                 test_cmp expected actual
245         '
246 done
247
248 cat >expected <<EOF
249 file:foo mmap bar_mmap
250 EOF
251
252 test_expect_success 'grep -e A --and -e B' '
253         git grep -e "foo mmap" --and -e bar_mmap >actual &&
254         test_cmp expected actual
255 '
256
257 cat >expected <<EOF
258 file:foo_mmap bar mmap
259 file:foo_mmap bar mmap baz
260 EOF
261
262
263 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
264         git grep \( -e foo_ --or -e baz \) \
265                 --and -e " mmap" >actual &&
266         test_cmp expected actual
267 '
268
269 cat >expected <<EOF
270 file:foo mmap bar
271 EOF
272
273 test_expect_success 'grep -e A --and --not -e B' '
274         git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
275         test_cmp expected actual
276 '
277
278 test_expect_success 'grep should ignore GREP_OPTIONS' '
279         GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
280         test_cmp expected actual
281 '
282
283 test_expect_success 'grep -f, non-existent file' '
284         test_must_fail git grep -f patterns
285 '
286
287 cat >expected <<EOF
288 file:foo mmap bar
289 file:foo_mmap bar
290 file:foo_mmap bar mmap
291 file:foo mmap bar_mmap
292 file:foo_mmap bar mmap baz
293 EOF
294
295 cat >pattern <<EOF
296 mmap
297 EOF
298
299 test_expect_success 'grep -f, one pattern' '
300         git grep -f pattern >actual &&
301         test_cmp expected actual
302 '
303
304 cat >expected <<EOF
305 file:foo mmap bar
306 file:foo_mmap bar
307 file:foo_mmap bar mmap
308 file:foo mmap bar_mmap
309 file:foo_mmap bar mmap baz
310 t/a/v:vvv
311 t/v:vvv
312 v:vvv
313 EOF
314
315 cat >patterns <<EOF
316 mmap
317 vvv
318 EOF
319
320 test_expect_success 'grep -f, multiple patterns' '
321         git grep -f patterns >actual &&
322         test_cmp expected actual
323 '
324
325 test_expect_success 'grep, multiple patterns' '
326         git grep "$(cat patterns)" >actual &&
327         test_cmp expected actual
328 '
329
330 cat >expected <<EOF
331 file:foo mmap bar
332 file:foo_mmap bar
333 file:foo_mmap bar mmap
334 file:foo mmap bar_mmap
335 file:foo_mmap bar mmap baz
336 t/a/v:vvv
337 t/v:vvv
338 v:vvv
339 EOF
340
341 cat >patterns <<EOF
342
343 mmap
344
345 vvv
346
347 EOF
348
349 test_expect_success 'grep -f, ignore empty lines' '
350         git grep -f patterns >actual &&
351         test_cmp expected actual
352 '
353
354 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
355         git grep -f - <patterns >actual &&
356         test_cmp expected actual
357 '
358
359 cat >expected <<EOF
360 y:y yy
361 --
362 z:zzz
363 EOF
364
365 test_expect_success 'grep -q, silently report matches' '
366         >empty &&
367         git grep -q mmap >actual &&
368         test_cmp empty actual &&
369         test_must_fail git grep -q qfwfq >actual &&
370         test_cmp empty actual
371 '
372
373 # Create 1024 file names that sort between "y" and "z" to make sure
374 # the two files are handled by different calls to an external grep.
375 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
376 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"
377 test_expect_success 'grep -C1, hunk mark between files' '
378         for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
379         git add y-?? &&
380         git grep -C1 "^[yz]" >actual &&
381         test_cmp expected actual
382 '
383
384 test_expect_success 'grep -C1 hunk mark between files' '
385         git grep -C1 "^[yz]" >actual &&
386         test_cmp expected actual
387 '
388
389 test_expect_success 'log grep setup' '
390         echo a >>file &&
391         test_tick &&
392         GIT_AUTHOR_NAME="With * Asterisk" \
393         GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
394         git commit -a -m "second" &&
395
396         echo a >>file &&
397         test_tick &&
398         git commit -a -m "third" &&
399
400         echo a >>file &&
401         test_tick &&
402         GIT_AUTHOR_NAME="Night Fall" \
403         GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
404         git commit -a -m "fourth"
405 '
406
407 test_expect_success 'log grep (1)' '
408         git log --author=author --pretty=tformat:%s >actual &&
409         ( echo third ; echo initial ) >expect &&
410         test_cmp expect actual
411 '
412
413 test_expect_success 'log grep (2)' '
414         git log --author=" * " -F --pretty=tformat:%s >actual &&
415         ( echo second ) >expect &&
416         test_cmp expect actual
417 '
418
419 test_expect_success 'log grep (3)' '
420         git log --author="^A U" --pretty=tformat:%s >actual &&
421         ( echo third ; echo initial ) >expect &&
422         test_cmp expect actual
423 '
424
425 test_expect_success 'log grep (4)' '
426         git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
427         ( echo second ) >expect &&
428         test_cmp expect actual
429 '
430
431 test_expect_success 'log grep (5)' '
432         git log --author=Thor -F --pretty=tformat:%s >actual &&
433         ( echo third ; echo initial ) >expect &&
434         test_cmp expect actual
435 '
436
437 test_expect_success 'log grep (6)' '
438         git log --author=-0700  --pretty=tformat:%s >actual &&
439         >expect &&
440         test_cmp expect actual
441 '
442
443 test_expect_success 'log --grep --author implicitly uses all-match' '
444         # grep matches initial and second but not third
445         # author matches only initial and third
446         git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
447         echo initial >expect &&
448         test_cmp expect actual
449 '
450
451 test_expect_success 'log with multiple --author uses union' '
452         git log --author="Thor" --author="Aster" --format=%s >actual &&
453         {
454             echo third && echo second && echo initial
455         } >expect &&
456         test_cmp expect actual
457 '
458
459 test_expect_success 'log with --grep and multiple --author uses all-match' '
460         git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
461         {
462             echo third && echo initial
463         } >expect &&
464         test_cmp expect actual
465 '
466
467 test_expect_success 'log with --grep and multiple --author uses all-match' '
468         git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
469         >expect &&
470         test_cmp expect actual
471 '
472
473 test_expect_success 'grep with CE_VALID file' '
474         git update-index --assume-unchanged t/t &&
475         rm t/t &&
476         test "$(git grep test)" = "t/t:test" &&
477         git update-index --no-assume-unchanged t/t &&
478         git checkout t/t
479 '
480
481 cat >expected <<EOF
482 hello.c=#include <stdio.h>
483 hello.c:        return 0;
484 EOF
485
486 test_expect_success 'grep -p with userdiff' '
487         git config diff.custom.funcname "^#" &&
488         echo "hello.c diff=custom" >.gitattributes &&
489         git grep -p return >actual &&
490         test_cmp expected actual
491 '
492
493 cat >expected <<EOF
494 hello.c=int main(int argc, const char **argv)
495 hello.c:        return 0;
496 EOF
497
498 test_expect_success 'grep -p' '
499         rm -f .gitattributes &&
500         git grep -p return >actual &&
501         test_cmp expected actual
502 '
503
504 cat >expected <<EOF
505 hello.c-#include <stdio.h>
506 hello.c=int main(int argc, const char **argv)
507 hello.c-{
508 hello.c-        printf("Hello world.\n");
509 hello.c:        return 0;
510 EOF
511
512 test_expect_success 'grep -p -B5' '
513         git grep -p -B5 return >actual &&
514         test_cmp expected actual
515 '
516
517 test_expect_success 'grep from a subdirectory to search wider area (1)' '
518         mkdir -p s &&
519         (
520                 cd s && git grep "x x x" ..
521         )
522 '
523
524 test_expect_success 'grep from a subdirectory to search wider area (2)' '
525         mkdir -p s &&
526         (
527                 cd s || exit 1
528                 ( git grep xxyyzz .. >out ; echo $? >status )
529                 ! test -s out &&
530                 test 1 = $(cat status)
531         )
532 '
533
534 cat >expected <<EOF
535 hello.c:int main(int argc, const char **argv)
536 EOF
537
538 test_expect_success 'grep -Fi' '
539         git grep -Fi "CHAR *" >actual &&
540         test_cmp expected actual
541 '
542
543 test_expect_success 'outside of git repository' '
544         rm -fr non &&
545         mkdir -p non/git/sub &&
546         echo hello >non/git/file1 &&
547         echo world >non/git/sub/file2 &&
548         echo ".*o*" >non/git/.gitignore &&
549         {
550                 echo file1:hello &&
551                 echo sub/file2:world
552         } >non/expect.full &&
553         echo file2:world >non/expect.sub &&
554         (
555                 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
556                 export GIT_CEILING_DIRECTORIES &&
557                 cd non/git &&
558                 test_must_fail git grep o &&
559                 git grep --no-index o >../actual.full &&
560                 test_cmp ../expect.full ../actual.full
561                 cd sub &&
562                 test_must_fail git grep o &&
563                 git grep --no-index o >../../actual.sub &&
564                 test_cmp ../../expect.sub ../../actual.sub
565         )
566 '
567
568 test_expect_success 'inside git repository but with --no-index' '
569         rm -fr is &&
570         mkdir -p is/git/sub &&
571         echo hello >is/git/file1 &&
572         echo world >is/git/sub/file2 &&
573         echo ".*o*" >is/git/.gitignore &&
574         {
575                 echo file1:hello &&
576                 echo sub/file2:world
577         } >is/expect.full &&
578         : >is/expect.empty &&
579         echo file2:world >is/expect.sub &&
580         (
581                 cd is/git &&
582                 git init &&
583                 test_must_fail git grep o >../actual.full &&
584                 test_cmp ../expect.empty ../actual.full &&
585                 git grep --no-index o >../actual.full &&
586                 test_cmp ../expect.full ../actual.full &&
587                 cd sub &&
588                 test_must_fail git grep o >../../actual.sub &&
589                 test_cmp ../../expect.empty ../../actual.sub &&
590                 git grep --no-index o >../../actual.sub &&
591                 test_cmp ../../expect.sub ../../actual.sub
592         )
593 '
594
595 test_expect_success 'setup double-dash tests' '
596 cat >double-dash <<EOF &&
597 --
598 ->
599 other
600 EOF
601 git add double-dash
602 '
603
604 cat >expected <<EOF
605 double-dash:->
606 EOF
607 test_expect_success 'grep -- pattern' '
608         git grep -- "->" >actual &&
609         test_cmp expected actual
610 '
611 test_expect_success 'grep -- pattern -- pathspec' '
612         git grep -- "->" -- double-dash >actual &&
613         test_cmp expected actual
614 '
615 test_expect_success 'grep -e pattern -- path' '
616         git grep -e "->" -- double-dash >actual &&
617         test_cmp expected actual
618 '
619
620 cat >expected <<EOF
621 double-dash:--
622 EOF
623 test_expect_success 'grep -e -- -- path' '
624         git grep -e -- -- double-dash >actual &&
625         test_cmp expected actual
626 '
627
628 cat >expected <<EOF
629 hello.c:int main(int argc, const char **argv)
630 hello.c:        printf("Hello world.\n");
631 EOF
632
633 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
634         git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
635         test_cmp expected actual
636 '
637
638 test_expect_success LIBPCRE 'grep -P pattern' '
639         git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
640         test_cmp expected actual
641 '
642
643 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
644         >empty &&
645         test_must_fail git -c grep.extendedregexp=true \
646                 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
647         test_cmp empty actual
648 '
649
650 test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
651         git -c grep.extendedregexp=true \
652                 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
653         test_cmp expected actual
654 '
655
656 test_expect_success LIBPCRE 'grep -P -v pattern' '
657         {
658                 echo "ab:a+b*c"
659                 echo "ab:a+bc"
660         } >expected &&
661         git grep -P -v "abc" ab >actual &&
662         test_cmp expected actual
663 '
664
665 test_expect_success LIBPCRE 'grep -P -i pattern' '
666         cat >expected <<-EOF &&
667         hello.c:        printf("Hello world.\n");
668         EOF
669         git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
670         test_cmp expected actual
671 '
672
673 test_expect_success LIBPCRE 'grep -P -w pattern' '
674         {
675                 echo "hello_world:Hello world"
676                 echo "hello_world:HeLLo world"
677         } >expected &&
678         git grep -P -w "He((?i)ll)o" hello_world >actual &&
679         test_cmp expected actual
680 '
681
682 test_expect_success 'grep -G invalidpattern properly dies ' '
683         test_must_fail git grep -G "a["
684 '
685
686 test_expect_success 'grep -E invalidpattern properly dies ' '
687         test_must_fail git grep -E "a["
688 '
689
690 test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
691         test_must_fail git grep -P "a["
692 '
693
694 test_expect_success 'grep -G -E -F pattern' '
695         echo "ab:a+b*c" >expected &&
696         git grep -G -E -F "a+b*c" ab >actual &&
697         test_cmp expected actual
698 '
699
700 test_expect_success 'grep -E -F -G pattern' '
701         echo "ab:a+bc" >expected &&
702         git grep -E -F -G "a+b*c" ab >actual &&
703         test_cmp expected actual
704 '
705
706 test_expect_success 'grep -F -G -E pattern' '
707         echo "ab:abc" >expected &&
708         git grep -F -G -E "a+b*c" ab >actual &&
709         test_cmp expected actual
710 '
711
712 test_expect_success 'grep -G -F -P -E pattern' '
713         >empty &&
714         test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
715         test_cmp empty actual
716 '
717
718 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
719         echo "ab:a+b*c" >expected &&
720         git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
721         test_cmp expected actual
722 '
723
724 test_done