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 grep -n -w -e mmap $H >actual &&
63 test_cmp expected actual
66 test_expect_success
"grep -w $L (w)" '
68 test_must_fail git grep -n -w -e "^w" >actual &&
69 test_cmp expected actual
72 test_expect_success
"grep -w $L (x)" '
74 echo ${HC}x:1:x x xx x
76 git grep -n -w -e "x xx* x" $H >actual &&
77 test_cmp expected actual
80 test_expect_success
"grep -w $L (y-1)" '
84 git grep -n -w -e "^y" $H >actual &&
85 test_cmp expected actual
88 test_expect_success
"grep -w $L (y-2)" '
90 if git grep -n -w -e "^y y" $H >actual
92 echo should not have matched
96 test_cmp expected actual
100 test_expect_success
"grep -w $L (z)" '
102 if git grep -n -w -e "^z" $H >actual
104 echo should not have matched
108 test_cmp expected actual
112 test_expect_success
"grep $L (t-1)" '
113 echo "${HC}t/t:1:test" >expected &&
114 git grep -n -e test $H >actual &&
115 test_cmp expected actual
118 test_expect_success
"grep $L (t-2)" '
119 echo "${HC}t:1:test" >expected &&
122 git grep -n -e test $H
124 test_cmp expected actual
127 test_expect_success
"grep $L (t-3)" '
128 echo "${HC}t/t:1:test" >expected &&
131 git grep --full-name -n -e test $H
133 test_cmp expected actual
136 test_expect_success
"grep -c $L (no /dev/null)" '
137 ! git grep -c test $H | grep /dev/null
140 test_expect_success
"grep --max-depth -1 $L" '
142 echo ${HC}t/a/v:1:vvv
146 git grep --max-depth -1 -n -e vvv $H >actual &&
147 test_cmp expected actual
150 test_expect_success
"grep --max-depth 0 $L" '
154 git grep --max-depth 0 -n -e vvv $H >actual &&
155 test_cmp expected actual
158 test_expect_success
"grep --max-depth 0 -- '*' $L" '
160 echo ${HC}t/a/v:1:vvv
164 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
165 test_cmp expected actual
168 test_expect_success
"grep --max-depth 1 $L" '
173 git grep --max-depth 1 -n -e vvv $H >actual &&
174 test_cmp expected actual
177 test_expect_success
"grep --max-depth 0 -- t $L" '
181 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
182 test_cmp expected actual
188 file:foo mmap bar_mmap
191 test_expect_success
'grep -e A --and -e B' '
192 git grep -e "foo mmap" --and -e bar_mmap >actual &&
193 test_cmp expected actual
197 file:foo_mmap bar mmap
198 file:foo_mmap bar mmap baz
202 test_expect_success
'grep ( -e A --or -e B ) --and -e B' '
203 git grep \( -e foo_ --or -e baz \) \
204 --and -e " mmap" >actual &&
205 test_cmp expected actual
212 test_expect_success
'grep -e A --and --not -e B' '
213 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
214 test_cmp expected actual
217 test_expect_success
'grep should ignore GREP_OPTIONS' '
218 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
219 test_cmp expected actual
222 test_expect_success
'grep -f, non-existent file' '
223 test_must_fail git grep -f patterns
229 file:foo_mmap bar mmap
230 file:foo mmap bar_mmap
231 file:foo_mmap bar mmap baz
238 test_expect_success
'grep -f, one pattern' '
239 git grep -f pattern >actual &&
240 test_cmp expected actual
246 file:foo_mmap bar mmap
247 file:foo mmap bar_mmap
248 file:foo_mmap bar mmap baz
259 test_expect_success
'grep -f, multiple patterns' '
260 git grep -f patterns >actual &&
261 test_cmp expected actual
267 file:foo_mmap bar mmap
268 file:foo mmap bar_mmap
269 file:foo_mmap bar mmap baz
283 test_expect_success
'grep -f, ignore empty lines' '
284 git grep -f patterns >actual &&
285 test_cmp expected actual
294 test_expect_success
'grep -q, silently report matches' '
296 git grep -q mmap >actual &&
297 test_cmp empty actual &&
298 test_must_fail git grep -q qfwfq >actual &&
299 test_cmp empty actual
302 # Create 1024 file names that sort between "y" and "z" to make sure
303 # the two files are handled by different calls to an external grep.
304 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
305 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"
306 test_expect_success
'grep -C1, hunk mark between files' '
307 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
309 git grep -C1 "^[yz]" >actual &&
310 test_cmp expected actual
313 test_expect_success
'grep -C1 hunk mark between files' '
314 git grep -C1 "^[yz]" >actual &&
315 test_cmp expected actual
318 test_expect_success
'log grep setup' '
321 GIT_AUTHOR_NAME="With * Asterisk" \
322 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
323 git commit -a -m "second" &&
327 git commit -a -m "third"
331 test_expect_success
'log grep (1)' '
332 git log --author=author --pretty=tformat:%s >actual &&
333 ( echo third ; echo initial ) >expect &&
334 test_cmp expect actual
337 test_expect_success
'log grep (2)' '
338 git log --author=" * " -F --pretty=tformat:%s >actual &&
339 ( echo second ) >expect &&
340 test_cmp expect actual
343 test_expect_success
'log grep (3)' '
344 git log --author="^A U" --pretty=tformat:%s >actual &&
345 ( echo third ; echo initial ) >expect &&
346 test_cmp expect actual
349 test_expect_success
'log grep (4)' '
350 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
351 ( echo second ) >expect &&
352 test_cmp expect actual
355 test_expect_success
'log grep (5)' '
356 git log --author=Thor -F --pretty=tformat:%s >actual &&
357 ( echo third ; echo initial ) >expect &&
358 test_cmp expect actual
361 test_expect_success
'log grep (6)' '
362 git log --author=-0700 --pretty=tformat:%s >actual &&
364 test_cmp expect actual
367 test_expect_success
'log --grep --author implicitly uses all-match' '
368 # grep matches initial and second but not third
369 # author matches only initial and third
370 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
371 echo initial >expect &&
372 test_cmp expect actual
375 test_expect_success
'grep with CE_VALID file' '
376 git update-index --assume-unchanged t/t &&
378 test "$(git grep test)" = "t/t:test" &&
379 git update-index --no-assume-unchanged t/t &&
384 hello.c=#include <stdio.h>
388 test_expect_success
'grep -p with userdiff' '
389 git config diff.custom.funcname "^#" &&
390 echo "hello.c diff=custom" >.gitattributes &&
391 git grep -p return >actual &&
392 test_cmp expected actual
396 hello.c=int main(int argc, const char **argv)
400 test_expect_success
'grep -p' '
401 rm -f .gitattributes &&
402 git grep -p return >actual &&
403 test_cmp expected actual
407 hello.c-#include <stdio.h>
408 hello.c=int main(int argc, const char **argv)
410 hello.c- printf("Hello world.\n");
414 test_expect_success
'grep -p -B5' '
415 git grep -p -B5 return >actual &&
416 test_cmp expected actual
419 test_expect_success
'grep from a subdirectory to search wider area (1)' '
422 cd s && git grep "x x x" ..
426 test_expect_success
'grep from a subdirectory to search wider area (2)' '
430 ( git grep xxyyzz .. >out ; echo $? >status )
432 test 1 = $(cat status)
437 hello.c:int main(int argc, const char **argv)
440 test_expect_success
'grep -Fi' '
441 git grep -Fi "CHAR *" >actual &&
442 test_cmp expected actual
445 test_expect_success
'outside of git repository' '
447 mkdir -p non/git/sub &&
448 echo hello >non/git/file1 &&
449 echo world >non/git/sub/file2 &&
450 echo ".*o*" >non/git/.gitignore &&
454 } >non/expect.full &&
455 echo file2:world >non/expect.sub
457 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
458 export GIT_CEILING_DIRECTORIES &&
460 test_must_fail git grep o &&
461 git grep --no-index o >../actual.full &&
462 test_cmp ../expect.full ../actual.full
464 test_must_fail git grep o &&
465 git grep --no-index o >../../actual.sub &&
466 test_cmp ../../expect.sub ../../actual.sub
470 test_expect_success
'inside git repository but with --no-index' '
472 mkdir -p is/git/sub &&
473 echo hello >is/git/file1 &&
474 echo world >is/git/sub/file2 &&
475 echo ".*o*" >is/git/.gitignore &&
480 : >is/expect.empty &&
481 echo file2:world >is/expect.sub
485 test_must_fail git grep o >../actual.full &&
486 test_cmp ../expect.empty ../actual.full &&
487 git grep --no-index o >../actual.full &&
488 test_cmp ../expect.full ../actual.full &&
490 test_must_fail git grep o >../../actual.sub &&
491 test_cmp ../../expect.empty ../../actual.sub &&
492 git grep --no-index o >../../actual.sub &&
493 test_cmp ../../expect.sub ../../actual.sub
497 test_expect_success
'setup double-dash tests' '
498 cat >double-dash <<EOF &&
509 test_expect_success
'grep -- pattern' '
510 git grep -- "->" >actual &&
511 test_cmp expected actual
513 test_expect_success
'grep -- pattern -- pathspec' '
514 git grep -- "->" -- double-dash >actual &&
515 test_cmp expected actual
517 test_expect_success
'grep -e pattern -- path' '
518 git grep -e "->" -- double-dash >actual &&
519 test_cmp expected actual
525 test_expect_success
'grep -e -- -- path' '
526 git grep -e -- -- double-dash >actual &&
527 test_cmp expected actual