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");
32 test_expect_success setup '
36 echo foo_mmap bar mmap
37 echo foo mmap bar_mmap
38 echo foo_mmap bar mmap baz
55 test_expect_success 'grep should not segfault with a bad input' '
56 test_must_fail git grep "("
62 HEAD) HC='HEAD:' L='HEAD' ;;
63 '') HC= L='in working tree' ;;
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 grep -n -w -e mmap $H >actual &&
77 test_expect_success "grep -w $L (w)" '
79 ! git grep -n -w -e "^w" >actual &&
80 test_cmp expected actual
83 test_expect_success "grep -w $L (x)" '
85 echo ${HC}x:1:x x xx x
87 git grep -n -w -e "x xx* x" $H >actual &&
91 test_expect_success "grep -w $L (y-1)" '
95 git grep -n -w -e "^y" $H >actual &&
99 test_expect_success "grep -w $L (y-2)" '
101 if git grep -n -w -e "^y y" $H >actual
103 echo should not have matched
111 test_expect_success "grep -w $L (z)" '
113 if git grep -n -w -e "^z" $H >actual
115 echo should not have matched
123 test_expect_success "grep $L (t-1)" '
124 echo "${HC}t/t:1:test" >expected &&
125 git grep -n -e test $H >actual &&
129 test_expect_success "grep $L (t-2)" '
130 echo "${HC}t:1:test" >expected &&
133 git grep -n -e test $H
138 test_expect_success "grep $L (t-3)" '
139 echo "${HC}t/t:1:test" >expected &&
142 git grep --full-name -n -e test $H
147 test_expect_success "grep -c $L (no /dev/null)" '
148 ! git grep -c test $H | grep /dev/null
151 test_expect_success "grep --max-depth -1 $L" '
153 echo ${HC}t/a/v:1:vvv
157 git grep --max-depth -1 -n -e vvv $H >actual &&
158 test_cmp expected actual
161 test_expect_success "grep --max-depth 0 $L" '
165 git grep --max-depth 0 -n -e vvv $H >actual &&
166 test_cmp expected actual
169 test_expect_success "grep --max-depth 0 -- '*' $L" '
171 echo ${HC}t/a/v:1:vvv
175 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
176 test_cmp expected actual
179 test_expect_success "grep --max-depth 1 $L" '
184 git grep --max-depth 1 -n -e vvv $H >actual &&
185 test_cmp expected actual
188 test_expect_success "grep --max-depth 0 -- t $L" '
192 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
193 test_cmp expected actual
199 file:foo mmap bar_mmap
202 test_expect_success 'grep -e A --and -e B' '
203 git grep -e "foo mmap" --and -e bar_mmap >actual &&
204 test_cmp expected actual
208 file:foo_mmap bar mmap
209 file:foo_mmap bar mmap baz
213 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
214 git grep \( -e foo_ --or -e baz \) \
215 --and -e " mmap" >actual &&
216 test_cmp expected actual
223 test_expect_success 'grep -e A --and --not -e B' '
224 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
225 test_cmp expected actual
234 # Create 1024 file names that sort between "y" and "z" to make sure
235 # the two files are handled by different calls to an external grep.
236 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
237 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"
238 test_expect_success 'grep -C1, hunk mark between files' '
239 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
241 git grep -C1 "^[yz]" >actual &&
242 test_cmp expected actual
245 test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
246 git grep -C1 --no-ext-grep "^[yz]" >actual &&
247 test_cmp expected actual
250 test_expect_success 'log grep setup' '
253 GIT_AUTHOR_NAME="With * Asterisk" \
254 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
255 git commit -a -m "second" &&
259 git commit -a -m "third"
263 test_expect_success 'log grep (1)' '
264 git log --author=author --pretty=tformat:%s >actual &&
265 ( echo third ; echo initial ) >expect &&
266 test_cmp expect actual
269 test_expect_success 'log grep (2)' '
270 git log --author=" * " -F --pretty=tformat:%s >actual &&
271 ( echo second ) >expect &&
272 test_cmp expect actual
275 test_expect_success 'log grep (3)' '
276 git log --author="^A U" --pretty=tformat:%s >actual &&
277 ( echo third ; echo initial ) >expect &&
278 test_cmp expect actual
281 test_expect_success 'log grep (4)' '
282 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
283 ( echo second ) >expect &&
284 test_cmp expect actual
287 test_expect_success 'log grep (5)' '
288 git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
289 ( echo third ; echo initial ) >expect &&
290 test_cmp expect actual
293 test_expect_success 'log grep (6)' '
294 git log --author=-0700 --pretty=tformat:%s >actual &&
296 test_cmp expect actual
299 test_expect_success 'grep with CE_VALID file' '
300 git update-index --assume-unchanged t/t &&
302 test "$(git grep --no-ext-grep test)" = "t/t:test" &&
303 git update-index --no-assume-unchanged t/t &&
308 hello.c=#include <stdio.h>
312 test_expect_success 'grep -p with userdiff' '
313 git config diff.custom.funcname "^#" &&
314 echo "hello.c diff=custom" >.gitattributes &&
315 git grep -p return >actual &&
316 test_cmp expected actual
320 hello.c=int main(int argc, const char **argv)
324 test_expect_success 'grep -p' '
325 rm -f .gitattributes &&
326 git grep -p return >actual &&
327 test_cmp expected actual
331 hello.c-#include <stdio.h>
332 hello.c=int main(int argc, const char **argv)
334 hello.c- printf("Hello world.\n");
338 test_expect_success 'grep -p -B5' '
339 git grep -p -B5 return >actual &&
340 test_cmp expected actual