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");
20 test_expect_success setup '
24 echo foo_mmap bar mmap
25 echo foo mmap bar_mmap
26 echo foo_mmap bar mmap baz
43 test_expect_success 'grep should not segfault with a bad input' '
44 test_must_fail git grep "("
50 HEAD) HC='HEAD:' L='HEAD' ;;
51 '') HC= L='in working tree' ;;
54 test_expect_success "grep -w $L" '
56 echo ${HC}file:1:foo mmap bar
57 echo ${HC}file:3:foo_mmap bar mmap
58 echo ${HC}file:4:foo mmap bar_mmap
59 echo ${HC}file:5:foo_mmap bar mmap baz
61 git grep -n -w -e mmap $H >actual &&
65 test_expect_success "grep -w $L (w)" '
67 ! git grep -n -w -e "^w" >actual &&
68 test_cmp expected actual
71 test_expect_success "grep -w $L (x)" '
73 echo ${HC}x:1:x x xx x
75 git grep -n -w -e "x xx* x" $H >actual &&
79 test_expect_success "grep -w $L (y-1)" '
83 git grep -n -w -e "^y" $H >actual &&
87 test_expect_success "grep -w $L (y-2)" '
89 if git grep -n -w -e "^y y" $H >actual
91 echo should not have matched
99 test_expect_success "grep -w $L (z)" '
101 if git grep -n -w -e "^z" $H >actual
103 echo should not have matched
111 test_expect_success "grep $L (t-1)" '
112 echo "${HC}t/t:1:test" >expected &&
113 git grep -n -e test $H >actual &&
117 test_expect_success "grep $L (t-2)" '
118 echo "${HC}t:1:test" >expected &&
121 git grep -n -e test $H
126 test_expect_success "grep $L (t-3)" '
127 echo "${HC}t/t:1:test" >expected &&
130 git grep --full-name -n -e test $H
135 test_expect_success "grep -c $L (no /dev/null)" '
136 ! git grep -c test $H | grep /dev/null
139 test_expect_success "grep --max-depth -1 $L" '
141 echo ${HC}t/a/v:1:vvv
145 git grep --max-depth -1 -n -e vvv $H >actual &&
146 test_cmp expected actual
149 test_expect_success "grep --max-depth 0 $L" '
153 git grep --max-depth 0 -n -e vvv $H >actual &&
154 test_cmp expected actual
157 test_expect_success "grep --max-depth 0 -- '*' $L" '
159 echo ${HC}t/a/v:1:vvv
163 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
164 test_cmp expected actual
167 test_expect_success "grep --max-depth 1 $L" '
172 git grep --max-depth 1 -n -e vvv $H >actual &&
173 test_cmp expected actual
176 test_expect_success "grep --max-depth 0 -- t $L" '
180 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
181 test_cmp expected actual
187 file:foo mmap bar_mmap
190 test_expect_success 'grep -e A --and -e B' '
191 git grep -e "foo mmap" --and -e bar_mmap >actual &&
192 test_cmp expected actual
196 file:foo_mmap bar mmap
197 file:foo_mmap bar mmap baz
201 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
202 git grep \( -e foo_ --or -e baz \) \
203 --and -e " mmap" >actual &&
204 test_cmp expected actual
211 test_expect_success 'grep -e A --and --not -e B' '
212 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
213 test_cmp expected actual
216 test_expect_success 'grep should ignore GREP_OPTIONS' '
217 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
218 test_cmp expected actual
221 test_expect_success 'grep -f, non-existent file' '
222 test_must_fail git grep -f patterns
228 file:foo_mmap bar mmap
229 file:foo mmap bar_mmap
230 file:foo_mmap bar mmap baz
237 test_expect_success 'grep -f, one pattern' '
238 git grep -f pattern >actual &&
239 test_cmp expected actual
245 file:foo_mmap bar mmap
246 file:foo mmap bar_mmap
247 file:foo_mmap bar mmap baz
258 test_expect_success 'grep -f, multiple patterns' '
259 git grep -f patterns >actual &&
260 test_cmp expected actual
266 file:foo_mmap bar mmap
267 file:foo mmap bar_mmap
268 file:foo_mmap bar mmap baz
282 test_expect_success 'grep -f, ignore empty lines' '
283 git grep -f patterns >actual &&
284 test_cmp expected actual
293 # Create 1024 file names that sort between "y" and "z" to make sure
294 # the two files are handled by different calls to an external grep.
295 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
296 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"
297 test_expect_success 'grep -C1, hunk mark between files' '
298 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
300 git grep -C1 "^[yz]" >actual &&
301 test_cmp expected actual
304 test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
305 git grep -C1 --no-ext-grep "^[yz]" >actual &&
306 test_cmp expected actual
309 test_expect_success 'log grep setup' '
312 GIT_AUTHOR_NAME="With * Asterisk" \
313 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
314 git commit -a -m "second" &&
318 git commit -a -m "third"
322 test_expect_success 'log grep (1)' '
323 git log --author=author --pretty=tformat:%s >actual &&
324 ( echo third ; echo initial ) >expect &&
325 test_cmp expect actual
328 test_expect_success 'log grep (2)' '
329 git log --author=" * " -F --pretty=tformat:%s >actual &&
330 ( echo second ) >expect &&
331 test_cmp expect actual
334 test_expect_success 'log grep (3)' '
335 git log --author="^A U" --pretty=tformat:%s >actual &&
336 ( echo third ; echo initial ) >expect &&
337 test_cmp expect actual
340 test_expect_success 'log grep (4)' '
341 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
342 ( echo second ) >expect &&
343 test_cmp expect actual
346 test_expect_success 'log grep (5)' '
347 git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
348 ( echo third ; echo initial ) >expect &&
349 test_cmp expect actual
352 test_expect_success 'log grep (6)' '
353 git log --author=-0700 --pretty=tformat:%s >actual &&
355 test_cmp expect actual
358 test_expect_success 'grep with CE_VALID file' '
359 git update-index --assume-unchanged t/t &&
361 test "$(git grep --no-ext-grep test)" = "t/t:test" &&
362 git update-index --no-assume-unchanged t/t &&
367 hello.c=#include <stdio.h>
371 test_expect_success 'grep -p with userdiff' '
372 git config diff.custom.funcname "^#" &&
373 echo "hello.c diff=custom" >.gitattributes &&
374 git grep -p return >actual &&
375 test_cmp expected actual
379 hello.c=int main(int argc, const char **argv)
383 test_expect_success 'grep -p' '
384 rm -f .gitattributes &&
385 git grep -p return >actual &&
386 test_cmp expected actual
390 hello.c-#include <stdio.h>
391 hello.c=int main(int argc, const char **argv)
393 hello.c- printf("Hello world.\n");
397 test_expect_success 'grep -p -B5' '
398 git grep -p -B5 return >actual &&
399 test_cmp expected actual
402 test_expect_success 'grep from a subdirectory to search wider area (1)' '
405 cd s && git grep "x x x" ..
409 test_expect_success 'grep from a subdirectory to search wider area (2)' '
413 ( git grep xxyyzz .. >out ; echo $? >status )
415 test 1 = $(cat status)