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
44 test_expect_success 'grep should not segfault with a bad input' '
45 test_must_fail git grep "("
51 HEAD) HC='HEAD:' L='HEAD' ;;
52 '') HC= L='in working tree' ;;
55 test_expect_success "grep -w $L" '
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
62 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
63 test_cmp expected actual
66 test_expect_success "grep -w $L" '
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
73 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
74 test_cmp expected actual
77 test_expect_success "grep -w $L" '
79 echo ${HC}file:foo mmap bar
80 echo ${HC}file:foo_mmap bar mmap
81 echo ${HC}file:foo mmap bar_mmap
82 echo ${HC}file:foo_mmap bar mmap baz
84 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
85 test_cmp expected actual
88 test_expect_success "grep -w $L (w)" '
90 test_must_fail git grep -n -w -e "^w" >actual &&
91 test_cmp expected actual
94 test_expect_success "grep -w $L (x)" '
96 echo ${HC}x:1:x x xx x
98 git grep -n -w -e "x xx* x" $H >actual &&
99 test_cmp expected actual
102 test_expect_success "grep -w $L (y-1)" '
106 git grep -n -w -e "^y" $H >actual &&
107 test_cmp expected actual
110 test_expect_success "grep -w $L (y-2)" '
112 if git grep -n -w -e "^y y" $H >actual
114 echo should not have matched
118 test_cmp expected actual
122 test_expect_success "grep -w $L (z)" '
124 if git grep -n -w -e "^z" $H >actual
126 echo should not have matched
130 test_cmp expected actual
134 test_expect_success "grep $L (t-1)" '
135 echo "${HC}t/t:1:test" >expected &&
136 git grep -n -e test $H >actual &&
137 test_cmp expected actual
140 test_expect_success "grep $L (t-2)" '
141 echo "${HC}t:1:test" >expected &&
144 git grep -n -e test $H
146 test_cmp expected actual
149 test_expect_success "grep $L (t-3)" '
150 echo "${HC}t/t:1:test" >expected &&
153 git grep --full-name -n -e test $H
155 test_cmp expected actual
158 test_expect_success "grep -c $L (no /dev/null)" '
159 ! git grep -c test $H | grep /dev/null
162 test_expect_success "grep --max-depth -1 $L" '
164 echo ${HC}t/a/v:1:vvv
168 git grep --max-depth -1 -n -e vvv $H >actual &&
169 test_cmp expected actual
172 test_expect_success "grep --max-depth 0 $L" '
176 git grep --max-depth 0 -n -e vvv $H >actual &&
177 test_cmp expected actual
180 test_expect_success "grep --max-depth 0 -- '*' $L" '
182 echo ${HC}t/a/v:1:vvv
186 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
187 test_cmp expected actual
190 test_expect_success "grep --max-depth 1 $L" '
195 git grep --max-depth 1 -n -e vvv $H >actual &&
196 test_cmp expected actual
199 test_expect_success "grep --max-depth 0 -- t $L" '
203 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
204 test_cmp expected actual
207 test_expect_success "grep --max-depth 0 -- . t $L" '
212 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
213 test_cmp expected actual
216 test_expect_success "grep --max-depth 0 -- t . $L" '
221 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
222 test_cmp expected actual
228 file:foo mmap bar_mmap
231 test_expect_success 'grep -e A --and -e B' '
232 git grep -e "foo mmap" --and -e bar_mmap >actual &&
233 test_cmp expected actual
237 file:foo_mmap bar mmap
238 file:foo_mmap bar mmap baz
242 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
243 git grep \( -e foo_ --or -e baz \) \
244 --and -e " mmap" >actual &&
245 test_cmp expected actual
252 test_expect_success 'grep -e A --and --not -e B' '
253 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
254 test_cmp expected actual
257 test_expect_success 'grep should ignore GREP_OPTIONS' '
258 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
259 test_cmp expected actual
262 test_expect_success 'grep -f, non-existent file' '
263 test_must_fail git grep -f patterns
269 file:foo_mmap bar mmap
270 file:foo mmap bar_mmap
271 file:foo_mmap bar mmap baz
278 test_expect_success 'grep -f, one pattern' '
279 git grep -f pattern >actual &&
280 test_cmp expected actual
286 file:foo_mmap bar mmap
287 file:foo mmap bar_mmap
288 file:foo_mmap bar mmap baz
299 test_expect_success 'grep -f, multiple patterns' '
300 git grep -f patterns >actual &&
301 test_cmp expected actual
307 file:foo_mmap bar mmap
308 file:foo mmap bar_mmap
309 file:foo_mmap bar mmap baz
323 test_expect_success 'grep -f, ignore empty lines' '
324 git grep -f patterns >actual &&
325 test_cmp expected actual
328 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
329 git grep -f - <patterns >actual &&
330 test_cmp expected actual
339 test_expect_success 'grep -q, silently report matches' '
341 git grep -q mmap >actual &&
342 test_cmp empty actual &&
343 test_must_fail git grep -q qfwfq >actual &&
344 test_cmp empty actual
347 # Create 1024 file names that sort between "y" and "z" to make sure
348 # the two files are handled by different calls to an external grep.
349 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
350 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"
351 test_expect_success 'grep -C1, hunk mark between files' '
352 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
354 git grep -C1 "^[yz]" >actual &&
355 test_cmp expected actual
358 test_expect_success 'grep -C1 hunk mark between files' '
359 git grep -C1 "^[yz]" >actual &&
360 test_cmp expected actual
363 test_expect_success 'log grep setup' '
366 GIT_AUTHOR_NAME="With * Asterisk" \
367 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
368 git commit -a -m "second" &&
372 git commit -a -m "third" &&
376 GIT_AUTHOR_NAME="Night Fall" \
377 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
378 git commit -a -m "fourth"
381 test_expect_success 'log grep (1)' '
382 git log --author=author --pretty=tformat:%s >actual &&
383 ( echo third ; echo initial ) >expect &&
384 test_cmp expect actual
387 test_expect_success 'log grep (2)' '
388 git log --author=" * " -F --pretty=tformat:%s >actual &&
389 ( echo second ) >expect &&
390 test_cmp expect actual
393 test_expect_success 'log grep (3)' '
394 git log --author="^A U" --pretty=tformat:%s >actual &&
395 ( echo third ; echo initial ) >expect &&
396 test_cmp expect actual
399 test_expect_success 'log grep (4)' '
400 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
401 ( echo second ) >expect &&
402 test_cmp expect actual
405 test_expect_success 'log grep (5)' '
406 git log --author=Thor -F --pretty=tformat:%s >actual &&
407 ( echo third ; echo initial ) >expect &&
408 test_cmp expect actual
411 test_expect_success 'log grep (6)' '
412 git log --author=-0700 --pretty=tformat:%s >actual &&
414 test_cmp expect actual
417 test_expect_success 'log --grep --author implicitly uses all-match' '
418 # grep matches initial and second but not third
419 # author matches only initial and third
420 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
421 echo initial >expect &&
422 test_cmp expect actual
425 test_expect_success 'log with multiple --author uses union' '
426 git log --author="Thor" --author="Aster" --format=%s >actual &&
428 echo third && echo second && echo initial
430 test_cmp expect actual
433 test_expect_success 'log with --grep and multiple --author uses all-match' '
434 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
436 echo third && echo initial
438 test_cmp expect actual
441 test_expect_success 'log with --grep and multiple --author uses all-match' '
442 git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
444 test_cmp expect actual
447 test_expect_success 'grep with CE_VALID file' '
448 git update-index --assume-unchanged t/t &&
450 test "$(git grep test)" = "t/t:test" &&
451 git update-index --no-assume-unchanged t/t &&
456 hello.c=#include <stdio.h>
460 test_expect_success 'grep -p with userdiff' '
461 git config diff.custom.funcname "^#" &&
462 echo "hello.c diff=custom" >.gitattributes &&
463 git grep -p return >actual &&
464 test_cmp expected actual
468 hello.c=int main(int argc, const char **argv)
472 test_expect_success 'grep -p' '
473 rm -f .gitattributes &&
474 git grep -p return >actual &&
475 test_cmp expected actual
479 hello.c-#include <stdio.h>
480 hello.c=int main(int argc, const char **argv)
482 hello.c- printf("Hello world.\n");
486 test_expect_success 'grep -p -B5' '
487 git grep -p -B5 return >actual &&
488 test_cmp expected actual
491 test_expect_success 'grep from a subdirectory to search wider area (1)' '
494 cd s && git grep "x x x" ..
498 test_expect_success 'grep from a subdirectory to search wider area (2)' '
502 ( git grep xxyyzz .. >out ; echo $? >status )
504 test 1 = $(cat status)
509 hello.c:int main(int argc, const char **argv)
512 test_expect_success 'grep -Fi' '
513 git grep -Fi "CHAR *" >actual &&
514 test_cmp expected actual
517 test_expect_success 'outside of git repository' '
519 mkdir -p non/git/sub &&
520 echo hello >non/git/file1 &&
521 echo world >non/git/sub/file2 &&
522 echo ".*o*" >non/git/.gitignore &&
526 } >non/expect.full &&
527 echo file2:world >non/expect.sub &&
529 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
530 export GIT_CEILING_DIRECTORIES &&
532 test_must_fail git grep o &&
533 git grep --no-index o >../actual.full &&
534 test_cmp ../expect.full ../actual.full
536 test_must_fail git grep o &&
537 git grep --no-index o >../../actual.sub &&
538 test_cmp ../../expect.sub ../../actual.sub
542 test_expect_success 'inside git repository but with --no-index' '
544 mkdir -p is/git/sub &&
545 echo hello >is/git/file1 &&
546 echo world >is/git/sub/file2 &&
547 echo ".*o*" >is/git/.gitignore &&
552 : >is/expect.empty &&
553 echo file2:world >is/expect.sub &&
557 test_must_fail git grep o >../actual.full &&
558 test_cmp ../expect.empty ../actual.full &&
559 git grep --no-index o >../actual.full &&
560 test_cmp ../expect.full ../actual.full &&
562 test_must_fail git grep o >../../actual.sub &&
563 test_cmp ../../expect.empty ../../actual.sub &&
564 git grep --no-index o >../../actual.sub &&
565 test_cmp ../../expect.sub ../../actual.sub
569 test_expect_success 'setup double-dash tests' '
570 cat >double-dash <<EOF &&
581 test_expect_success 'grep -- pattern' '
582 git grep -- "->" >actual &&
583 test_cmp expected actual
585 test_expect_success 'grep -- pattern -- pathspec' '
586 git grep -- "->" -- double-dash >actual &&
587 test_cmp expected actual
589 test_expect_success 'grep -e pattern -- path' '
590 git grep -e "->" -- double-dash >actual &&
591 test_cmp expected actual
597 test_expect_success 'grep -e -- -- path' '
598 git grep -e -- -- double-dash >actual &&
599 test_cmp expected actual