Support remote archive from all smart transports
[git/dkf.git] / t / t7002-grep.sh
blobabd14bf819f5c60fc1b9dc758c04974bd24b10a4
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git grep various.
9 . ./test-lib.sh
11 cat >hello.c <<EOF
12 #include <stdio.h>
13 int main(int argc, const char **argv)
15 printf("Hello world.\n");
16 return 0;
17 /* char ?? */
19 EOF
21 test_expect_success setup '
23 echo foo mmap bar
24 echo foo_mmap bar
25 echo foo_mmap bar mmap
26 echo foo mmap bar_mmap
27 echo foo_mmap bar mmap baz
28 } >file &&
29 echo vvv >v &&
30 echo ww w >w &&
31 echo x x xx x >x &&
32 echo y yy >y &&
33 echo zzz > z &&
34 mkdir t &&
35 echo test >t/t &&
36 echo vvv >t/v &&
37 mkdir t/a &&
38 echo vvv >t/a/v &&
39 git add . &&
40 test_tick &&
41 git commit -m initial
44 test_expect_success 'grep should not segfault with a bad input' '
45 test_must_fail git grep "("
48 for H in HEAD ''
50 case "$H" in
51 HEAD) HC='HEAD:' L='HEAD' ;;
52 '') HC= L='in working tree' ;;
53 esac
55 test_expect_success "grep -w $L" '
57 echo ${HC}file:1:foo mmap bar
58 echo ${HC}file:3:foo_mmap bar mmap
59 echo ${HC}file:4:foo mmap bar_mmap
60 echo ${HC}file:5:foo_mmap bar mmap baz
61 } >expected &&
62 git grep -n -w -e mmap $H >actual &&
63 diff expected actual
66 test_expect_success "grep -w $L (w)" '
67 : >expected &&
68 ! git grep -n -w -e "^w" >actual &&
69 test_cmp expected actual
72 test_expect_success "grep -w $L (x)" '
74 echo ${HC}x:1:x x xx x
75 } >expected &&
76 git grep -n -w -e "x xx* x" $H >actual &&
77 diff expected actual
80 test_expect_success "grep -w $L (y-1)" '
82 echo ${HC}y:1:y yy
83 } >expected &&
84 git grep -n -w -e "^y" $H >actual &&
85 diff expected actual
88 test_expect_success "grep -w $L (y-2)" '
89 : >expected &&
90 if git grep -n -w -e "^y y" $H >actual
91 then
92 echo should not have matched
93 cat actual
94 false
95 else
96 diff expected actual
100 test_expect_success "grep -w $L (z)" '
101 : >expected &&
102 if git grep -n -w -e "^z" $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 $L (t-1)" '
113 echo "${HC}t/t:1:test" >expected &&
114 git grep -n -e test $H >actual &&
115 diff expected actual
118 test_expect_success "grep $L (t-2)" '
119 echo "${HC}t:1:test" >expected &&
121 cd t &&
122 git grep -n -e test $H
123 ) >actual &&
124 diff expected actual
127 test_expect_success "grep $L (t-3)" '
128 echo "${HC}t/t:1:test" >expected &&
130 cd t &&
131 git grep --full-name -n -e test $H
132 ) >actual &&
133 diff expected actual
136 test_expect_success "grep -c $L (no /dev/null)" '
137 ! git grep -c test $H | grep /dev/null
140 test_expect_success "grep --max-depth -1 $L" '
142 echo ${HC}t/a/v:1:vvv
143 echo ${HC}t/v:1:vvv
144 echo ${HC}v:1:vvv
145 } >expected &&
146 git grep --max-depth -1 -n -e vvv $H >actual &&
147 test_cmp expected actual
150 test_expect_success "grep --max-depth 0 $L" '
152 echo ${HC}v:1:vvv
153 } >expected &&
154 git grep --max-depth 0 -n -e vvv $H >actual &&
155 test_cmp expected actual
158 test_expect_success "grep --max-depth 0 -- '*' $L" '
160 echo ${HC}t/a/v:1:vvv
161 echo ${HC}t/v:1:vvv
162 echo ${HC}v:1:vvv
163 } >expected &&
164 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
165 test_cmp expected actual
168 test_expect_success "grep --max-depth 1 $L" '
170 echo ${HC}t/v:1:vvv
171 echo ${HC}v:1:vvv
172 } >expected &&
173 git grep --max-depth 1 -n -e vvv $H >actual &&
174 test_cmp expected actual
177 test_expect_success "grep --max-depth 0 -- t $L" '
179 echo ${HC}t/v:1:vvv
180 } >expected &&
181 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
182 test_cmp expected actual
185 done
187 cat >expected <<EOF
188 file:foo mmap bar_mmap
191 test_expect_success 'grep -e A --and -e B' '
192 git grep -e "foo mmap" --and -e bar_mmap >actual &&
193 test_cmp expected actual
196 cat >expected <<EOF
197 file:foo_mmap bar mmap
198 file:foo_mmap bar mmap baz
202 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
203 git grep \( -e foo_ --or -e baz \) \
204 --and -e " mmap" >actual &&
205 test_cmp expected actual
208 cat >expected <<EOF
209 file:foo mmap bar
212 test_expect_success 'grep -e A --and --not -e B' '
213 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
214 test_cmp expected actual
217 test_expect_success 'grep should ignore GREP_OPTIONS' '
218 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
219 test_cmp expected actual
222 test_expect_success 'grep -f, non-existent file' '
223 test_must_fail git grep -f patterns
226 cat >expected <<EOF
227 file:foo mmap bar
228 file:foo_mmap bar
229 file:foo_mmap bar mmap
230 file:foo mmap bar_mmap
231 file:foo_mmap bar mmap baz
234 cat >pattern <<EOF
235 mmap
238 test_expect_success 'grep -f, one pattern' '
239 git grep -f pattern >actual &&
240 test_cmp expected actual
243 cat >expected <<EOF
244 file:foo mmap bar
245 file:foo_mmap bar
246 file:foo_mmap bar mmap
247 file:foo mmap bar_mmap
248 file:foo_mmap bar mmap baz
249 t/a/v:vvv
250 t/v:vvv
251 v:vvv
254 cat >patterns <<EOF
255 mmap
259 test_expect_success 'grep -f, multiple patterns' '
260 git grep -f patterns >actual &&
261 test_cmp expected actual
264 cat >expected <<EOF
265 file:foo mmap bar
266 file:foo_mmap bar
267 file:foo_mmap bar mmap
268 file:foo mmap bar_mmap
269 file:foo_mmap bar mmap baz
270 t/a/v:vvv
271 t/v:vvv
272 v:vvv
275 cat >patterns <<EOF
277 mmap
283 test_expect_success 'grep -f, ignore empty lines' '
284 git grep -f patterns >actual &&
285 test_cmp expected actual
288 cat >expected <<EOF
289 y:y yy
291 z:zzz
294 # Create 1024 file names that sort between "y" and "z" to make sure
295 # the two files are handled by different calls to an external grep.
296 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
297 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"
298 test_expect_success 'grep -C1, hunk mark between files' '
299 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
300 git add y-?? &&
301 git grep -C1 "^[yz]" >actual &&
302 test_cmp expected actual
305 test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
306 git grep -C1 --no-ext-grep "^[yz]" >actual &&
307 test_cmp expected actual
310 test_expect_success 'log grep setup' '
311 echo a >>file &&
312 test_tick &&
313 GIT_AUTHOR_NAME="With * Asterisk" \
314 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
315 git commit -a -m "second" &&
317 echo a >>file &&
318 test_tick &&
319 git commit -a -m "third"
323 test_expect_success 'log grep (1)' '
324 git log --author=author --pretty=tformat:%s >actual &&
325 ( echo third ; echo initial ) >expect &&
326 test_cmp expect actual
329 test_expect_success 'log grep (2)' '
330 git log --author=" * " -F --pretty=tformat:%s >actual &&
331 ( echo second ) >expect &&
332 test_cmp expect actual
335 test_expect_success 'log grep (3)' '
336 git log --author="^A U" --pretty=tformat:%s >actual &&
337 ( echo third ; echo initial ) >expect &&
338 test_cmp expect actual
341 test_expect_success 'log grep (4)' '
342 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
343 ( echo second ) >expect &&
344 test_cmp expect actual
347 test_expect_success 'log grep (5)' '
348 git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
349 ( echo third ; echo initial ) >expect &&
350 test_cmp expect actual
353 test_expect_success 'log grep (6)' '
354 git log --author=-0700 --pretty=tformat:%s >actual &&
355 >expect &&
356 test_cmp expect actual
359 test_expect_success 'grep with CE_VALID file' '
360 git update-index --assume-unchanged t/t &&
361 rm t/t &&
362 test "$(git grep --no-ext-grep test)" = "t/t:test" &&
363 git update-index --no-assume-unchanged t/t &&
364 git checkout t/t
367 cat >expected <<EOF
368 hello.c=#include <stdio.h>
369 hello.c: return 0;
372 test_expect_success 'grep -p with userdiff' '
373 git config diff.custom.funcname "^#" &&
374 echo "hello.c diff=custom" >.gitattributes &&
375 git grep -p return >actual &&
376 test_cmp expected actual
379 cat >expected <<EOF
380 hello.c=int main(int argc, const char **argv)
381 hello.c: return 0;
384 test_expect_success 'grep -p' '
385 rm -f .gitattributes &&
386 git grep -p return >actual &&
387 test_cmp expected actual
390 cat >expected <<EOF
391 hello.c-#include <stdio.h>
392 hello.c=int main(int argc, const char **argv)
393 hello.c-{
394 hello.c- printf("Hello world.\n");
395 hello.c: return 0;
398 test_expect_success 'grep -p -B5' '
399 git grep -p -B5 return >actual &&
400 test_cmp expected actual
403 test_expect_success 'grep from a subdirectory to search wider area (1)' '
404 mkdir -p s &&
406 cd s && git grep "x x x" ..
410 test_expect_success 'grep from a subdirectory to search wider area (2)' '
411 mkdir -p s &&
413 cd s || exit 1
414 ( git grep xxyyzz .. >out ; echo $? >status )
415 ! test -s out &&
416 test 1 = $(cat status)
420 cat >expected <<EOF
421 hello.c:int main(int argc, const char **argv)
424 test_expect_success 'grep -Fi' '
425 git grep -Fi "CHAR *" >actual &&
426 test_cmp expected actual
429 test_done