Merge branch 'nd/sparse'
[alt-git.git] / t / t7002-grep.sh
blob76c5e091b7d45bde38f34dc9726c51034ee7f9eb
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git grep various.
9 . ./test-lib.sh
11 test_expect_success 'Check for external grep support' '
12 case "$(git grep -h 2>&1|grep ext-grep)" in
13 *"(default)"*)
14 test_set_prereq EXTGREP
15 true;;
16 *"(ignored by this build)"*)
17 true;;
19 false;;
20 esac
23 cat >hello.c <<EOF
24 #include <stdio.h>
25 int main(int argc, const char **argv)
27 printf("Hello world.\n");
28 return 0;
29 /* char ?? */
31 EOF
33 test_expect_success setup '
35 echo foo mmap bar
36 echo foo_mmap bar
37 echo foo_mmap bar mmap
38 echo foo mmap bar_mmap
39 echo foo_mmap bar mmap baz
40 } >file &&
41 echo vvv >v &&
42 echo ww w >w &&
43 echo x x xx x >x &&
44 echo y yy >y &&
45 echo zzz > z &&
46 mkdir t &&
47 echo test >t/t &&
48 echo vvv >t/v &&
49 mkdir t/a &&
50 echo vvv >t/a/v &&
51 git add . &&
52 test_tick &&
53 git commit -m initial
56 test_expect_success 'grep should not segfault with a bad input' '
57 test_must_fail git grep "("
60 for H in HEAD ''
62 case "$H" in
63 HEAD) HC='HEAD:' L='HEAD' ;;
64 '') HC= L='in working tree' ;;
65 esac
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
73 } >expected &&
74 git grep -n -w -e mmap $H >actual &&
75 diff expected actual
78 test_expect_success "grep -w $L (w)" '
79 : >expected &&
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
87 } >expected &&
88 git grep -n -w -e "x xx* x" $H >actual &&
89 diff expected actual
92 test_expect_success "grep -w $L (y-1)" '
94 echo ${HC}y:1:y yy
95 } >expected &&
96 git grep -n -w -e "^y" $H >actual &&
97 diff expected actual
100 test_expect_success "grep -w $L (y-2)" '
101 : >expected &&
102 if git grep -n -w -e "^y y" $H >actual
103 then
104 echo should not have matched
105 cat actual
106 false
107 else
108 diff expected actual
112 test_expect_success "grep -w $L (z)" '
113 : >expected &&
114 if git grep -n -w -e "^z" $H >actual
115 then
116 echo should not have matched
117 cat actual
118 false
119 else
120 diff expected actual
124 test_expect_success "grep $L (t-1)" '
125 echo "${HC}t/t:1:test" >expected &&
126 git grep -n -e test $H >actual &&
127 diff expected actual
130 test_expect_success "grep $L (t-2)" '
131 echo "${HC}t:1:test" >expected &&
133 cd t &&
134 git grep -n -e test $H
135 ) >actual &&
136 diff expected actual
139 test_expect_success "grep $L (t-3)" '
140 echo "${HC}t/t:1:test" >expected &&
142 cd t &&
143 git grep --full-name -n -e test $H
144 ) >actual &&
145 diff expected actual
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
155 echo ${HC}t/v:1:vvv
156 echo ${HC}v:1:vvv
157 } >expected &&
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" '
164 echo ${HC}v:1:vvv
165 } >expected &&
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
173 echo ${HC}t/v:1:vvv
174 echo ${HC}v:1:vvv
175 } >expected &&
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" '
182 echo ${HC}t/v:1:vvv
183 echo ${HC}v:1:vvv
184 } >expected &&
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" '
191 echo ${HC}t/v:1:vvv
192 } >expected &&
193 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
194 test_cmp expected actual
197 done
199 cat >expected <<EOF
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
208 cat >expected <<EOF
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
220 cat >expected <<EOF
221 file:foo mmap bar
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
238 cat >expected <<EOF
239 file:foo mmap bar
240 file:foo_mmap bar
241 file:foo_mmap bar mmap
242 file:foo mmap bar_mmap
243 file:foo_mmap bar mmap baz
246 cat >pattern <<EOF
247 mmap
250 test_expect_success 'grep -f, one pattern' '
251 git grep -f pattern >actual &&
252 test_cmp expected actual
255 cat >expected <<EOF
256 file:foo mmap bar
257 file:foo_mmap bar
258 file:foo_mmap bar mmap
259 file:foo mmap bar_mmap
260 file:foo_mmap bar mmap baz
261 t/a/v:vvv
262 t/v:vvv
263 v:vvv
266 cat >patterns <<EOF
267 mmap
271 test_expect_success 'grep -f, multiple patterns' '
272 git grep -f patterns >actual &&
273 test_cmp expected actual
276 cat >expected <<EOF
277 file:foo mmap bar
278 file:foo_mmap bar
279 file:foo_mmap bar mmap
280 file:foo mmap bar_mmap
281 file:foo_mmap bar mmap baz
282 t/a/v:vvv
283 t/v:vvv
284 v:vvv
287 cat >patterns <<EOF
289 mmap
295 test_expect_success 'grep -f, ignore empty lines' '
296 git grep -f patterns >actual &&
297 test_cmp expected actual
300 cat >expected <<EOF
301 y:y yy
303 z:zzz
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 &&
312 git add y-?? &&
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' '
323 echo a >>file &&
324 test_tick &&
325 GIT_AUTHOR_NAME="With * Asterisk" \
326 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
327 git commit -a -m "second" &&
329 echo a >>file &&
330 test_tick &&
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 &&
367 >expect &&
368 test_cmp expect actual
371 test_expect_success 'grep with CE_VALID file' '
372 git update-index --assume-unchanged t/t &&
373 rm t/t &&
374 test "$(git grep --no-ext-grep test)" = "t/t:test" &&
375 git update-index --no-assume-unchanged t/t &&
376 git checkout t/t
379 cat >expected <<EOF
380 hello.c=#include <stdio.h>
381 hello.c: return 0;
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
391 cat >expected <<EOF
392 hello.c=int main(int argc, const char **argv)
393 hello.c: return 0;
396 test_expect_success 'grep -p' '
397 rm -f .gitattributes &&
398 git grep -p return >actual &&
399 test_cmp expected actual
402 cat >expected <<EOF
403 hello.c-#include <stdio.h>
404 hello.c=int main(int argc, const char **argv)
405 hello.c-{
406 hello.c- printf("Hello world.\n");
407 hello.c: return 0;
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)' '
416 mkdir -p s &&
418 cd s && git grep "x x x" ..
422 test_expect_success 'grep from a subdirectory to search wider area (2)' '
423 mkdir -p s &&
425 cd s || exit 1
426 ( git grep xxyyzz .. >out ; echo $? >status )
427 ! test -s out &&
428 test 1 = $(cat status)
432 cat >expected <<EOF
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
453 test_done