git-grep: Bail out when -P is used with -F or -E
[git/git-svn.git] / t / t7810-grep.sh
blob2a31eca5f257e674390bee1a93eb5a867262eff5
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git grep various.
9 . ./test-lib.sh
11 cat >hello.c <<EOF
12 #include <stdio.h>
13 int main(int argc, const char **argv)
15 printf("Hello world.\n");
16 return 0;
17 /* char ?? */
19 EOF
21 test_expect_success setup '
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 &&
30 echo Hello world
31 echo HeLLo world
32 echo Hello_world
33 echo HeLLo_world
34 } >hello_world &&
35 echo vvv >v &&
36 echo ww w >w &&
37 echo x x xx x >x &&
38 echo y yy >y &&
39 echo zzz > z &&
40 mkdir t &&
41 echo test >t/t &&
42 echo vvv >t/v &&
43 mkdir t/a &&
44 echo vvv >t/a/v &&
45 git add . &&
46 test_tick &&
47 git commit -m initial
50 test_expect_success 'grep should not segfault with a bad input' '
51 test_must_fail git grep "("
54 for H in HEAD ''
56 case "$H" in
57 HEAD) HC='HEAD:' L='HEAD' ;;
58 '') HC= L='in working tree' ;;
59 esac
61 test_expect_success "grep -w $L" '
63 echo ${HC}file:1:foo mmap bar
64 echo ${HC}file:3:foo_mmap bar mmap
65 echo ${HC}file:4:foo mmap bar_mmap
66 echo ${HC}file:5:foo_mmap bar mmap baz
67 } >expected &&
68 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
69 test_cmp expected actual
72 test_expect_success "grep -w $L" '
74 echo ${HC}file:1:foo mmap bar
75 echo ${HC}file:3:foo_mmap bar mmap
76 echo ${HC}file:4:foo mmap bar_mmap
77 echo ${HC}file:5:foo_mmap bar mmap baz
78 } >expected &&
79 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
80 test_cmp expected actual
83 test_expect_success "grep -w $L" '
85 echo ${HC}file:foo mmap bar
86 echo ${HC}file:foo_mmap bar mmap
87 echo ${HC}file:foo mmap bar_mmap
88 echo ${HC}file:foo_mmap bar mmap baz
89 } >expected &&
90 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
91 test_cmp expected actual
94 test_expect_success "grep -w $L (w)" '
95 : >expected &&
96 test_must_fail git grep -n -w -e "^w" >actual &&
97 test_cmp expected actual
100 test_expect_success "grep -w $L (x)" '
102 echo ${HC}x:1:x x xx x
103 } >expected &&
104 git grep -n -w -e "x xx* x" $H >actual &&
105 test_cmp expected actual
108 test_expect_success "grep -w $L (y-1)" '
110 echo ${HC}y:1:y yy
111 } >expected &&
112 git grep -n -w -e "^y" $H >actual &&
113 test_cmp expected actual
116 test_expect_success "grep -w $L (y-2)" '
117 : >expected &&
118 if git grep -n -w -e "^y y" $H >actual
119 then
120 echo should not have matched
121 cat actual
122 false
123 else
124 test_cmp expected actual
128 test_expect_success "grep -w $L (z)" '
129 : >expected &&
130 if git grep -n -w -e "^z" $H >actual
131 then
132 echo should not have matched
133 cat actual
134 false
135 else
136 test_cmp expected actual
140 test_expect_success "grep $L (t-1)" '
141 echo "${HC}t/t:1:test" >expected &&
142 git grep -n -e test $H >actual &&
143 test_cmp expected actual
146 test_expect_success "grep $L (t-2)" '
147 echo "${HC}t:1:test" >expected &&
149 cd t &&
150 git grep -n -e test $H
151 ) >actual &&
152 test_cmp expected actual
155 test_expect_success "grep $L (t-3)" '
156 echo "${HC}t/t:1:test" >expected &&
158 cd t &&
159 git grep --full-name -n -e test $H
160 ) >actual &&
161 test_cmp expected actual
164 test_expect_success "grep -c $L (no /dev/null)" '
165 ! git grep -c test $H | grep /dev/null
168 test_expect_success "grep --max-depth -1 $L" '
170 echo ${HC}t/a/v:1:vvv
171 echo ${HC}t/v:1:vvv
172 echo ${HC}v:1:vvv
173 } >expected &&
174 git grep --max-depth -1 -n -e vvv $H >actual &&
175 test_cmp expected actual
178 test_expect_success "grep --max-depth 0 $L" '
180 echo ${HC}v:1:vvv
181 } >expected &&
182 git grep --max-depth 0 -n -e vvv $H >actual &&
183 test_cmp expected actual
186 test_expect_success "grep --max-depth 0 -- '*' $L" '
188 echo ${HC}t/a/v:1:vvv
189 echo ${HC}t/v:1:vvv
190 echo ${HC}v:1:vvv
191 } >expected &&
192 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
193 test_cmp expected actual
196 test_expect_success "grep --max-depth 1 $L" '
198 echo ${HC}t/v:1:vvv
199 echo ${HC}v:1:vvv
200 } >expected &&
201 git grep --max-depth 1 -n -e vvv $H >actual &&
202 test_cmp expected actual
205 test_expect_success "grep --max-depth 0 -- t $L" '
207 echo ${HC}t/v:1:vvv
208 } >expected &&
209 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
210 test_cmp expected actual
213 test_expect_success "grep --max-depth 0 -- . t $L" '
215 echo ${HC}t/v:1:vvv
216 echo ${HC}v:1:vvv
217 } >expected &&
218 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
219 test_cmp expected actual
222 test_expect_success "grep --max-depth 0 -- t . $L" '
224 echo ${HC}t/v:1:vvv
225 echo ${HC}v:1:vvv
226 } >expected &&
227 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
228 test_cmp expected actual
231 done
233 cat >expected <<EOF
234 file:foo mmap bar_mmap
237 test_expect_success 'grep -e A --and -e B' '
238 git grep -e "foo mmap" --and -e bar_mmap >actual &&
239 test_cmp expected actual
242 cat >expected <<EOF
243 file:foo_mmap bar mmap
244 file:foo_mmap bar mmap baz
248 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
249 git grep \( -e foo_ --or -e baz \) \
250 --and -e " mmap" >actual &&
251 test_cmp expected actual
254 cat >expected <<EOF
255 file:foo mmap bar
258 test_expect_success 'grep -e A --and --not -e B' '
259 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
260 test_cmp expected actual
263 test_expect_success 'grep should ignore GREP_OPTIONS' '
264 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
265 test_cmp expected actual
268 test_expect_success 'grep -f, non-existent file' '
269 test_must_fail git grep -f patterns
272 cat >expected <<EOF
273 file:foo mmap bar
274 file:foo_mmap bar
275 file:foo_mmap bar mmap
276 file:foo mmap bar_mmap
277 file:foo_mmap bar mmap baz
280 cat >pattern <<EOF
281 mmap
284 test_expect_success 'grep -f, one pattern' '
285 git grep -f pattern >actual &&
286 test_cmp expected actual
289 cat >expected <<EOF
290 file:foo mmap bar
291 file:foo_mmap bar
292 file:foo_mmap bar mmap
293 file:foo mmap bar_mmap
294 file:foo_mmap bar mmap baz
295 t/a/v:vvv
296 t/v:vvv
297 v:vvv
300 cat >patterns <<EOF
301 mmap
305 test_expect_success 'grep -f, multiple patterns' '
306 git grep -f patterns >actual &&
307 test_cmp expected actual
310 cat >expected <<EOF
311 file:foo mmap bar
312 file:foo_mmap bar
313 file:foo_mmap bar mmap
314 file:foo mmap bar_mmap
315 file:foo_mmap bar mmap baz
316 t/a/v:vvv
317 t/v:vvv
318 v:vvv
321 cat >patterns <<EOF
323 mmap
329 test_expect_success 'grep -f, ignore empty lines' '
330 git grep -f patterns >actual &&
331 test_cmp expected actual
334 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
335 git grep -f - <patterns >actual &&
336 test_cmp expected actual
339 cat >expected <<EOF
340 y:y yy
342 z:zzz
345 test_expect_success 'grep -q, silently report matches' '
346 >empty &&
347 git grep -q mmap >actual &&
348 test_cmp empty actual &&
349 test_must_fail git grep -q qfwfq >actual &&
350 test_cmp empty actual
353 # Create 1024 file names that sort between "y" and "z" to make sure
354 # the two files are handled by different calls to an external grep.
355 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
356 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"
357 test_expect_success 'grep -C1, hunk mark between files' '
358 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
359 git add y-?? &&
360 git grep -C1 "^[yz]" >actual &&
361 test_cmp expected actual
364 test_expect_success 'grep -C1 hunk mark between files' '
365 git grep -C1 "^[yz]" >actual &&
366 test_cmp expected actual
369 test_expect_success 'log grep setup' '
370 echo a >>file &&
371 test_tick &&
372 GIT_AUTHOR_NAME="With * Asterisk" \
373 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
374 git commit -a -m "second" &&
376 echo a >>file &&
377 test_tick &&
378 git commit -a -m "third" &&
380 echo a >>file &&
381 test_tick &&
382 GIT_AUTHOR_NAME="Night Fall" \
383 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
384 git commit -a -m "fourth"
387 test_expect_success 'log grep (1)' '
388 git log --author=author --pretty=tformat:%s >actual &&
389 ( echo third ; echo initial ) >expect &&
390 test_cmp expect actual
393 test_expect_success 'log grep (2)' '
394 git log --author=" * " -F --pretty=tformat:%s >actual &&
395 ( echo second ) >expect &&
396 test_cmp expect actual
399 test_expect_success 'log grep (3)' '
400 git log --author="^A U" --pretty=tformat:%s >actual &&
401 ( echo third ; echo initial ) >expect &&
402 test_cmp expect actual
405 test_expect_success 'log grep (4)' '
406 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
407 ( echo second ) >expect &&
408 test_cmp expect actual
411 test_expect_success 'log grep (5)' '
412 git log --author=Thor -F --pretty=tformat:%s >actual &&
413 ( echo third ; echo initial ) >expect &&
414 test_cmp expect actual
417 test_expect_success 'log grep (6)' '
418 git log --author=-0700 --pretty=tformat:%s >actual &&
419 >expect &&
420 test_cmp expect actual
423 test_expect_success 'log --grep --author implicitly uses all-match' '
424 # grep matches initial and second but not third
425 # author matches only initial and third
426 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
427 echo initial >expect &&
428 test_cmp expect actual
431 test_expect_success 'log with multiple --author uses union' '
432 git log --author="Thor" --author="Aster" --format=%s >actual &&
434 echo third && echo second && echo initial
435 } >expect &&
436 test_cmp expect actual
439 test_expect_success 'log with --grep and multiple --author uses all-match' '
440 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
442 echo third && echo initial
443 } >expect &&
444 test_cmp expect actual
447 test_expect_success 'log with --grep and multiple --author uses all-match' '
448 git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
449 >expect &&
450 test_cmp expect actual
453 test_expect_success 'grep with CE_VALID file' '
454 git update-index --assume-unchanged t/t &&
455 rm t/t &&
456 test "$(git grep test)" = "t/t:test" &&
457 git update-index --no-assume-unchanged t/t &&
458 git checkout t/t
461 cat >expected <<EOF
462 hello.c=#include <stdio.h>
463 hello.c: return 0;
466 test_expect_success 'grep -p with userdiff' '
467 git config diff.custom.funcname "^#" &&
468 echo "hello.c diff=custom" >.gitattributes &&
469 git grep -p return >actual &&
470 test_cmp expected actual
473 cat >expected <<EOF
474 hello.c=int main(int argc, const char **argv)
475 hello.c: return 0;
478 test_expect_success 'grep -p' '
479 rm -f .gitattributes &&
480 git grep -p return >actual &&
481 test_cmp expected actual
484 cat >expected <<EOF
485 hello.c-#include <stdio.h>
486 hello.c=int main(int argc, const char **argv)
487 hello.c-{
488 hello.c- printf("Hello world.\n");
489 hello.c: return 0;
492 test_expect_success 'grep -p -B5' '
493 git grep -p -B5 return >actual &&
494 test_cmp expected actual
497 test_expect_success 'grep from a subdirectory to search wider area (1)' '
498 mkdir -p s &&
500 cd s && git grep "x x x" ..
504 test_expect_success 'grep from a subdirectory to search wider area (2)' '
505 mkdir -p s &&
507 cd s || exit 1
508 ( git grep xxyyzz .. >out ; echo $? >status )
509 ! test -s out &&
510 test 1 = $(cat status)
514 cat >expected <<EOF
515 hello.c:int main(int argc, const char **argv)
518 test_expect_success 'grep -Fi' '
519 git grep -Fi "CHAR *" >actual &&
520 test_cmp expected actual
523 test_expect_success 'outside of git repository' '
524 rm -fr non &&
525 mkdir -p non/git/sub &&
526 echo hello >non/git/file1 &&
527 echo world >non/git/sub/file2 &&
528 echo ".*o*" >non/git/.gitignore &&
530 echo file1:hello &&
531 echo sub/file2:world
532 } >non/expect.full &&
533 echo file2:world >non/expect.sub &&
535 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
536 export GIT_CEILING_DIRECTORIES &&
537 cd non/git &&
538 test_must_fail git grep o &&
539 git grep --no-index o >../actual.full &&
540 test_cmp ../expect.full ../actual.full
541 cd sub &&
542 test_must_fail git grep o &&
543 git grep --no-index o >../../actual.sub &&
544 test_cmp ../../expect.sub ../../actual.sub
548 test_expect_success 'inside git repository but with --no-index' '
549 rm -fr is &&
550 mkdir -p is/git/sub &&
551 echo hello >is/git/file1 &&
552 echo world >is/git/sub/file2 &&
553 echo ".*o*" >is/git/.gitignore &&
555 echo file1:hello &&
556 echo sub/file2:world
557 } >is/expect.full &&
558 : >is/expect.empty &&
559 echo file2:world >is/expect.sub &&
561 cd is/git &&
562 git init &&
563 test_must_fail git grep o >../actual.full &&
564 test_cmp ../expect.empty ../actual.full &&
565 git grep --no-index o >../actual.full &&
566 test_cmp ../expect.full ../actual.full &&
567 cd sub &&
568 test_must_fail git grep o >../../actual.sub &&
569 test_cmp ../../expect.empty ../../actual.sub &&
570 git grep --no-index o >../../actual.sub &&
571 test_cmp ../../expect.sub ../../actual.sub
575 test_expect_success 'setup double-dash tests' '
576 cat >double-dash <<EOF &&
579 other
581 git add double-dash
584 cat >expected <<EOF
585 double-dash:->
587 test_expect_success 'grep -- pattern' '
588 git grep -- "->" >actual &&
589 test_cmp expected actual
591 test_expect_success 'grep -- pattern -- pathspec' '
592 git grep -- "->" -- double-dash >actual &&
593 test_cmp expected actual
595 test_expect_success 'grep -e pattern -- path' '
596 git grep -e "->" -- double-dash >actual &&
597 test_cmp expected actual
600 cat >expected <<EOF
601 double-dash:--
603 test_expect_success 'grep -e -- -- path' '
604 git grep -e -- -- double-dash >actual &&
605 test_cmp expected actual
608 cat >expected <<EOF
609 hello.c:int main(int argc, const char **argv)
610 hello.c: printf("Hello world.\n");
613 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
614 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
615 test_cmp expected actual
618 test_expect_success LIBPCRE 'grep -P pattern' '
619 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
620 test_cmp expected actual
623 test_expect_success LIBPCRE 'grep -P -i pattern' '
625 echo "hello.c: printf(\"Hello world.\n\");"
626 } >expected &&
627 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
628 test_cmp expected actual
631 test_expect_success LIBPCRE 'grep -P -w pattern' '
633 echo "hello_world:Hello world"
634 echo "hello_world:HeLLo world"
635 } >expected &&
636 git grep -P -w "He((?i)ll)o" hello_world >actual &&
637 test_cmp expected actual
640 test_expect_success LIBPCRE 'grep -P -F returns error' '
641 test_expect_code 128 git grep -P -F main
644 test_expect_success LIBPCRE 'grep -P -E returns error' '
645 test_expect_code 128 git grep -P -E main
648 test_expect_failure LIBPCRE 'grep -P -G returns error' '
649 test_expect_code 128 git grep -P -G main
652 test_expect_failure LIBPCRE 'grep -P -E -G returns error' '
653 test_expect_code 128 git grep -P -E -G main
656 test_done