3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git grep various.
11 test_expect_success 'Check for external grep support' '
12 case "$(git grep -h 2>&1|grep ext-grep)" in
14 test_set_prereq EXTGREP
16 *"(ignored by this build)"*)
25 int main(int argc, const char **argv)
27 printf("Hello world.\n");
33 test_expect_success setup '
37 echo foo_mmap bar mmap
38 echo foo mmap bar_mmap
39 echo foo_mmap bar mmap baz
56 test_expect_success 'grep should not segfault with a bad input' '
57 test_must_fail git grep "("
63 HEAD) HC='HEAD:' L='HEAD' ;;
64 '') HC= L='in working tree' ;;
67 test_expect_success "grep -w $L" '
69 echo ${HC}file:1:foo mmap bar
70 echo ${HC}file:3:foo_mmap bar mmap
71 echo ${HC}file:4:foo mmap bar_mmap
72 echo ${HC}file:5:foo_mmap bar mmap baz
74 git grep -n -w -e mmap $H >actual &&
78 test_expect_success "grep -w $L (w)" '
80 ! git grep -n -w -e "^w" >actual &&
81 test_cmp expected actual
84 test_expect_success "grep -w $L (x)" '
86 echo ${HC}x:1:x x xx x
88 git grep -n -w -e "x xx* x" $H >actual &&
92 test_expect_success "grep -w $L (y-1)" '
96 git grep -n -w -e "^y" $H >actual &&
100 test_expect_success "grep -w $L (y-2)" '
102 if git grep -n -w -e "^y y" $H >actual
104 echo should not have matched
112 test_expect_success "grep -w $L (z)" '
114 if git grep -n -w -e "^z" $H >actual
116 echo should not have matched
124 test_expect_success "grep $L (t-1)" '
125 echo "${HC}t/t:1:test" >expected &&
126 git grep -n -e test $H >actual &&
130 test_expect_success "grep $L (t-2)" '
131 echo "${HC}t:1:test" >expected &&
134 git grep -n -e test $H
139 test_expect_success "grep $L (t-3)" '
140 echo "${HC}t/t:1:test" >expected &&
143 git grep --full-name -n -e test $H
148 test_expect_success "grep -c $L (no /dev/null)" '
149 ! git grep -c test $H | grep /dev/null
152 test_expect_success "grep --max-depth -1 $L" '
154 echo ${HC}t/a/v:1:vvv
158 git grep --max-depth -1 -n -e vvv $H >actual &&
159 test_cmp expected actual
162 test_expect_success "grep --max-depth 0 $L" '
166 git grep --max-depth 0 -n -e vvv $H >actual &&
167 test_cmp expected actual
170 test_expect_success "grep --max-depth 0 -- '*' $L" '
172 echo ${HC}t/a/v:1:vvv
176 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
177 test_cmp expected actual
180 test_expect_success "grep --max-depth 1 $L" '
185 git grep --max-depth 1 -n -e vvv $H >actual &&
186 test_cmp expected actual
189 test_expect_success "grep --max-depth 0 -- t $L" '
193 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
194 test_cmp expected actual
200 file:foo mmap bar_mmap
203 test_expect_success 'grep -e A --and -e B' '
204 git grep -e "foo mmap" --and -e bar_mmap >actual &&
205 test_cmp expected actual
209 file:foo_mmap bar mmap
210 file:foo_mmap bar mmap baz
214 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
215 git grep \( -e foo_ --or -e baz \) \
216 --and -e " mmap" >actual &&
217 test_cmp expected actual
224 test_expect_success 'grep -e A --and --not -e B' '
225 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
226 test_cmp expected actual
229 test_expect_success 'grep should ignore GREP_OPTIONS' '
230 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
231 test_cmp expected actual
234 test_expect_success 'grep -f, non-existent file' '
235 test_must_fail git grep -f patterns
241 file:foo_mmap bar mmap
242 file:foo mmap bar_mmap
243 file:foo_mmap bar mmap baz
250 test_expect_success 'grep -f, one pattern' '
251 git grep -f pattern >actual &&
252 test_cmp expected actual
258 file:foo_mmap bar mmap
259 file:foo mmap bar_mmap
260 file:foo_mmap bar mmap baz
271 test_expect_success 'grep -f, multiple patterns' '
272 git grep -f patterns >actual &&
273 test_cmp expected actual
279 file:foo_mmap bar mmap
280 file:foo mmap bar_mmap
281 file:foo_mmap bar mmap baz
295 test_expect_success 'grep -f, ignore empty lines' '
296 git grep -f patterns >actual &&
297 test_cmp expected actual
306 # Create 1024 file names that sort between "y" and "z" to make sure
307 # the two files are handled by different calls to an external grep.
308 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
309 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"
310 test_expect_success 'grep -C1, hunk mark between files' '
311 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
313 git grep -C1 "^[yz]" >actual &&
314 test_cmp expected actual
317 test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
318 git grep -C1 --no-ext-grep "^[yz]" >actual &&
319 test_cmp expected actual
322 test_expect_success 'log grep setup' '
325 GIT_AUTHOR_NAME="With * Asterisk" \
326 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
327 git commit -a -m "second" &&
331 git commit -a -m "third"
335 test_expect_success 'log grep (1)' '
336 git log --author=author --pretty=tformat:%s >actual &&
337 ( echo third ; echo initial ) >expect &&
338 test_cmp expect actual
341 test_expect_success 'log grep (2)' '
342 git log --author=" * " -F --pretty=tformat:%s >actual &&
343 ( echo second ) >expect &&
344 test_cmp expect actual
347 test_expect_success 'log grep (3)' '
348 git log --author="^A U" --pretty=tformat:%s >actual &&
349 ( echo third ; echo initial ) >expect &&
350 test_cmp expect actual
353 test_expect_success 'log grep (4)' '
354 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
355 ( echo second ) >expect &&
356 test_cmp expect actual
359 test_expect_success 'log grep (5)' '
360 git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
361 ( echo third ; echo initial ) >expect &&
362 test_cmp expect actual
365 test_expect_success 'log grep (6)' '
366 git log --author=-0700 --pretty=tformat:%s >actual &&
368 test_cmp expect actual
371 test_expect_success 'grep with CE_VALID file' '
372 git update-index --assume-unchanged t/t &&
374 test "$(git grep --no-ext-grep test)" = "t/t:test" &&
375 git update-index --no-assume-unchanged t/t &&
380 hello.c=#include <stdio.h>
384 test_expect_success 'grep -p with userdiff' '
385 git config diff.custom.funcname "^#" &&
386 echo "hello.c diff=custom" >.gitattributes &&
387 git grep -p return >actual &&
388 test_cmp expected actual
392 hello.c=int main(int argc, const char **argv)
396 test_expect_success 'grep -p' '
397 rm -f .gitattributes &&
398 git grep -p return >actual &&
399 test_cmp expected actual
403 hello.c-#include <stdio.h>
404 hello.c=int main(int argc, const char **argv)
406 hello.c- printf("Hello world.\n");
410 test_expect_success 'grep -p -B5' '
411 git grep -p -B5 return >actual &&
412 test_cmp expected actual
415 test_expect_success 'grep from a subdirectory to search wider area (1)' '
418 cd s && git grep "x x x" ..
422 test_expect_success 'grep from a subdirectory to search wider area (2)' '
426 ( git grep xxyyzz .. >out ; echo $? >status )
428 test 1 = $(cat status)
433 hello.c:int main(int argc, const char **argv)
436 test_expect_success 'grep -Fi' '
437 git grep -Fi "CHAR *" >actual &&
438 test_cmp expected actual
441 test_expect_success EXTGREP 'external grep is called' '
442 GIT_TRACE=2 git grep foo >/dev/null 2>actual &&
443 grep "trace: grep:.*foo" actual >/dev/null
446 test_expect_success EXTGREP 'no external grep when skip-worktree entries exist' '
447 git update-index --skip-worktree file &&
448 GIT_TRACE=2 git grep foo >/dev/null 2>actual &&
449 ! grep "trace: grep:" actual >/dev/null &&
450 git update-index --no-skip-worktree file