3 # Copyright (c) 2006 Junio C Hamano
6 test_description
='git grep various.
13 int main(int argc, const char **argv)
15 printf("Hello world.\n");
21 test_expect_success setup
'
25 echo foo_mmap bar mmap
26 echo foo mmap bar_mmap
27 echo foo_mmap bar mmap baz
50 test_expect_success
'grep should not segfault with a bad input' '
51 test_must_fail git grep "("
57 HEAD
) HC
='HEAD:' L
='HEAD' ;;
58 '') HC
= L
='in working tree' ;;
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
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
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
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)" '
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
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)" '
112 git grep -n -w -e "^y" $H >actual &&
113 test_cmp expected actual
116 test_expect_success
"grep -w $L (y-2)" '
118 if git grep -n -w -e "^y y" $H >actual
120 echo should not have matched
124 test_cmp expected actual
128 test_expect_success
"grep -w $L (z)" '
130 if git grep -n -w -e "^z" $H >actual
132 echo should not have matched
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 &&
150 git grep -n -e test $H
152 test_cmp expected actual
155 test_expect_success
"grep $L (t-3)" '
156 echo "${HC}t/t:1:test" >expected &&
159 git grep --full-name -n -e test $H
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
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" '
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
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" '
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" '
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" '
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" '
227 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
228 test_cmp expected actual
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
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
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
275 file:foo_mmap bar mmap
276 file:foo mmap bar_mmap
277 file:foo_mmap bar mmap baz
284 test_expect_success
'grep -f, one pattern' '
285 git grep -f pattern >actual &&
286 test_cmp expected actual
292 file:foo_mmap bar mmap
293 file:foo mmap bar_mmap
294 file:foo_mmap bar mmap baz
305 test_expect_success
'grep -f, multiple patterns' '
306 git grep -f patterns >actual &&
307 test_cmp expected actual
313 file:foo_mmap bar mmap
314 file:foo mmap bar_mmap
315 file:foo_mmap bar mmap baz
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
345 test_expect_success
'grep -q, silently report matches' '
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 &&
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' '
372 GIT_AUTHOR_NAME="With * Asterisk" \
373 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
374 git commit -a -m "second" &&
378 git commit -a -m "third" &&
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 &&
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
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
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 &&
450 test_cmp expect actual
453 test_expect_success
'grep with CE_VALID file' '
454 git update-index --assume-unchanged t/t &&
456 test "$(git grep test)" = "t/t:test" &&
457 git update-index --no-assume-unchanged t/t &&
462 hello.c=#include <stdio.h>
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
474 hello.c=int main(int argc, const char **argv)
478 test_expect_success
'grep -p' '
479 rm -f .gitattributes &&
480 git grep -p return >actual &&
481 test_cmp expected actual
485 hello.c-#include <stdio.h>
486 hello.c=int main(int argc, const char **argv)
488 hello.c- printf("Hello world.\n");
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)' '
500 cd s && git grep "x x x" ..
504 test_expect_success
'grep from a subdirectory to search wider area (2)' '
508 ( git grep xxyyzz .. >out ; echo $? >status )
510 test 1 = $(cat status)
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' '
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 &&
532 } >non/expect.full &&
533 echo file2:world >non/expect.sub &&
535 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
536 export GIT_CEILING_DIRECTORIES &&
538 test_must_fail git grep o &&
539 git grep --no-index o >../actual.full &&
540 test_cmp ../expect.full ../actual.full
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' '
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 &&
558 : >is/expect.empty &&
559 echo file2:world >is/expect.sub &&
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 &&
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 &&
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
603 test_expect_success
'grep -e -- -- path' '
604 git grep -e -- -- double-dash >actual &&
605 test_cmp expected actual
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\");"
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"
636 git grep -P -w "He((?i)ll)o" hello_world >actual &&
637 test_cmp expected actual