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
142 file:foo mmap bar_mmap
145 test_expect_success
'grep -e A --and -e B' '
146 git grep -e "foo mmap" --and -e bar_mmap >actual &&
147 test_cmp expected actual
151 file:foo_mmap bar mmap
152 file:foo_mmap bar mmap baz
156 test_expect_success
'grep ( -e A --or -e B ) --and -e B' '
157 git grep \( -e foo_ --or -e baz \) \
158 --and -e " mmap" >actual &&
159 test_cmp expected actual
166 test_expect_success
'grep -e A --and --not -e B' '
167 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
168 test_cmp expected actual
171 test_expect_success
'grep -f, non-existent file' '
172 test_must_fail git grep -f patterns
178 file:foo_mmap bar mmap
179 file:foo mmap bar_mmap
180 file:foo_mmap bar mmap baz
187 test_expect_success
'grep -f, one pattern' '
188 git grep -f pattern >actual &&
189 test_cmp expected actual
195 file:foo_mmap bar mmap
196 file:foo mmap bar_mmap
197 file:foo_mmap bar mmap baz
208 test_expect_success
'grep -f, multiple patterns' '
209 git grep -f patterns >actual &&
210 test_cmp expected actual
216 file:foo_mmap bar mmap
217 file:foo mmap bar_mmap
218 file:foo_mmap bar mmap baz
232 test_expect_success
'grep -f, ignore empty lines' '
233 git grep -f patterns >actual &&
234 test_cmp expected actual
243 # Create 1024 file names that sort between "y" and "z" to make sure
244 # the two files are handled by different calls to an external grep.
245 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
246 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"
247 test_expect_success
'grep -C1, hunk mark between files' '
248 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
250 git grep -C1 "^[yz]" >actual &&
251 test_cmp expected actual
254 test_expect_success
'grep -C1 --no-ext-grep, hunk mark between files' '
255 git grep -C1 --no-ext-grep "^[yz]" >actual &&
256 test_cmp expected actual
259 test_expect_success
'log grep setup' '
262 GIT_AUTHOR_NAME="With * Asterisk" \
263 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
264 git commit -a -m "second" &&
268 git commit -a -m "third"
272 test_expect_success
'log grep (1)' '
273 git log --author=author --pretty=tformat:%s >actual &&
274 ( echo third ; echo initial ) >expect &&
275 test_cmp expect actual
278 test_expect_success
'log grep (2)' '
279 git log --author=" * " -F --pretty=tformat:%s >actual &&
280 ( echo second ) >expect &&
281 test_cmp expect actual
284 test_expect_success
'log grep (3)' '
285 git log --author="^A U" --pretty=tformat:%s >actual &&
286 ( echo third ; echo initial ) >expect &&
287 test_cmp expect actual
290 test_expect_success
'log grep (4)' '
291 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
292 ( echo second ) >expect &&
293 test_cmp expect actual
296 test_expect_success
'log grep (5)' '
297 git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
298 ( echo third ; echo initial ) >expect &&
299 test_cmp expect actual
302 test_expect_success
'log grep (6)' '
303 git log --author=-0700 --pretty=tformat:%s >actual &&
305 test_cmp expect actual
308 test_expect_success
'grep with CE_VALID file' '
309 git update-index --assume-unchanged t/t &&
311 test "$(git grep --no-ext-grep test)" = "t/t:test" &&
312 git update-index --no-assume-unchanged t/t &&
317 hello.c=#include <stdio.h>
321 test_expect_success
'grep -p with userdiff' '
322 git config diff.custom.funcname "^#" &&
323 echo "hello.c diff=custom" >.gitattributes &&
324 git grep -p return >actual &&
325 test_cmp expected actual
329 hello.c=int main(int argc, const char **argv)
333 test_expect_success
'grep -p' '
334 rm -f .gitattributes &&
335 git grep -p return >actual &&
336 test_cmp expected actual
340 hello.c-#include <stdio.h>
341 hello.c=int main(int argc, const char **argv)
343 hello.c- printf("Hello world.\n");
347 test_expect_success
'grep -p -B5' '
348 git grep -p -B5 return >actual &&
349 test_cmp expected actual
352 test_expect_success
'grep from a subdirectory to search wider area (1)' '
355 cd s && git grep "x x x" ..
359 test_expect_success
'grep from a subdirectory to search wider area (2)' '
363 ( git grep xxyyzz .. >out ; echo $? >status )
365 test 1 = $(cat status)