Merge branch 'nd/sparse'
[git/git.git] / t / t7002-grep.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='git grep various.
7 '
8
9 . ./test-lib.sh
10
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;;
18 *)
19 false;;
20 esac
21 '
22
23 cat >hello.c <<EOF
24 #include <stdio.h>
25 int main(int argc, const char **argv)
26 {
27 printf("Hello world.\n");
28 return 0;
29 /* char ?? */
30 }
31 EOF
32
33 test_expect_success setup '
34 {
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
54 '
55
56 test_expect_success 'grep should not segfault with a bad input' '
57 test_must_fail git grep "("
58 '
59
60 for H in HEAD ''
61 do
62 case "$H" in
63 HEAD) HC='HEAD:' L='HEAD' ;;
64 '') HC= L='in working tree' ;;
65 esac
66
67 test_expect_success "grep -w $L" '
68 {
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
76 '
77
78 test_expect_success "grep -w $L (w)" '
79 : >expected &&
80 ! git grep -n -w -e "^w" >actual &&
81 test_cmp expected actual
82 '
83
84 test_expect_success "grep -w $L (x)" '
85 {
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
90 '
91
92 test_expect_success "grep -w $L (y-1)" '
93 {
94 echo ${HC}y:1:y yy
95 } >expected &&
96 git grep -n -w -e "^y" $H >actual &&
97 diff expected actual
98 '
99
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
109 fi
110 '
111
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
121 fi
122 '
123
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
128 '
129
130 test_expect_success "grep $L (t-2)" '
131 echo "${HC}t:1:test" >expected &&
132 (
133 cd t &&
134 git grep -n -e test $H
135 ) >actual &&
136 diff expected actual
137 '
138
139 test_expect_success "grep $L (t-3)" '
140 echo "${HC}t/t:1:test" >expected &&
141 (
142 cd t &&
143 git grep --full-name -n -e test $H
144 ) >actual &&
145 diff expected actual
146 '
147
148 test_expect_success "grep -c $L (no /dev/null)" '
149 ! git grep -c test $H | grep /dev/null
150 '
151
152 test_expect_success "grep --max-depth -1 $L" '
153 {
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
160 '
161
162 test_expect_success "grep --max-depth 0 $L" '
163 {
164 echo ${HC}v:1:vvv
165 } >expected &&
166 git grep --max-depth 0 -n -e vvv $H >actual &&
167 test_cmp expected actual
168 '
169
170 test_expect_success "grep --max-depth 0 -- '*' $L" '
171 {
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
178 '
179
180 test_expect_success "grep --max-depth 1 $L" '
181 {
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
187 '
188
189 test_expect_success "grep --max-depth 0 -- t $L" '
190 {
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
195 '
196
197 done
198
199 cat >expected <<EOF
200 file:foo mmap bar_mmap
201 EOF
202
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
206 '
207
208 cat >expected <<EOF
209 file:foo_mmap bar mmap
210 file:foo_mmap bar mmap baz
211 EOF
212
213
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
218 '
219
220 cat >expected <<EOF
221 file:foo mmap bar
222 EOF
223
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
227 '
228
229 test_expect_success 'grep should ignore GREP_OPTIONS' '
230 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
231 test_cmp expected actual
232 '
233
234 test_expect_success 'grep -f, non-existent file' '
235 test_must_fail git grep -f patterns
236 '
237
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
244 EOF
245
246 cat >pattern <<EOF
247 mmap
248 EOF
249
250 test_expect_success 'grep -f, one pattern' '
251 git grep -f pattern >actual &&
252 test_cmp expected actual
253 '
254
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
264 EOF
265
266 cat >patterns <<EOF
267 mmap
268 vvv
269 EOF
270
271 test_expect_success 'grep -f, multiple patterns' '
272 git grep -f patterns >actual &&
273 test_cmp expected actual
274 '
275
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
285 EOF
286
287 cat >patterns <<EOF
288
289 mmap
290
291 vvv
292
293 EOF
294
295 test_expect_success 'grep -f, ignore empty lines' '
296 git grep -f patterns >actual &&
297 test_cmp expected actual
298 '
299
300 cat >expected <<EOF
301 y:y yy
302 --
303 z:zzz
304 EOF
305
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
315 '
316
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
320 '
321
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" &&
328
329 echo a >>file &&
330 test_tick &&
331 git commit -a -m "third"
332
333 '
334
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
339 '
340
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
345 '
346
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
351 '
352
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
357 '
358
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
363 '
364
365 test_expect_success 'log grep (6)' '
366 git log --author=-0700 --pretty=tformat:%s >actual &&
367 >expect &&
368 test_cmp expect actual
369 '
370
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
377 '
378
379 cat >expected <<EOF
380 hello.c=#include <stdio.h>
381 hello.c: return 0;
382 EOF
383
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
389 '
390
391 cat >expected <<EOF
392 hello.c=int main(int argc, const char **argv)
393 hello.c: return 0;
394 EOF
395
396 test_expect_success 'grep -p' '
397 rm -f .gitattributes &&
398 git grep -p return >actual &&
399 test_cmp expected actual
400 '
401
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;
408 EOF
409
410 test_expect_success 'grep -p -B5' '
411 git grep -p -B5 return >actual &&
412 test_cmp expected actual
413 '
414
415 test_expect_success 'grep from a subdirectory to search wider area (1)' '
416 mkdir -p s &&
417 (
418 cd s && git grep "x x x" ..
419 )
420 '
421
422 test_expect_success 'grep from a subdirectory to search wider area (2)' '
423 mkdir -p s &&
424 (
425 cd s || exit 1
426 ( git grep xxyyzz .. >out ; echo $? >status )
427 ! test -s out &&
428 test 1 = $(cat status)
429 )
430 '
431
432 cat >expected <<EOF
433 hello.c:int main(int argc, const char **argv)
434 EOF
435
436 test_expect_success 'grep -Fi' '
437 git grep -Fi "CHAR *" >actual &&
438 test_cmp expected actual
439 '
440
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
444 '
445
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
451 '
452
453 test_done