1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
6 test_description='git grep various.
7 '
9 . ./test-lib.sh
11 cat >hello.c <<EOF
12 #include <stdio.h>
13 int main(int argc, const char **argv)
14 {
15 printf("Hello world.\n");
16 return 0;
17 /* char ?? */
18 }
19 EOF
21 test_expect_success setup '
22 {
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
42 '
44 test_expect_success 'grep should not segfault with a bad input' '
45 test_must_fail git grep "("
46 '
48 for H in HEAD ''
49 do
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" '
56 {
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 -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
63 test_cmp expected actual
64 '
66 test_expect_success "grep -w $L" '
67 {
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
72 } >expected &&
73 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
74 test_cmp expected actual
75 '
77 test_expect_success "grep -w $L" '
78 {
79 echo ${HC}file:foo mmap bar
80 echo ${HC}file:foo_mmap bar mmap
81 echo ${HC}file:foo mmap bar_mmap
82 echo ${HC}file:foo_mmap bar mmap baz
83 } >expected &&
84 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
85 test_cmp expected actual
86 '
88 test_expect_success "grep -w $L (w)" '
89 : >expected &&
90 test_must_fail git grep -n -w -e "^w" >actual &&
91 test_cmp expected actual
92 '
94 test_expect_success "grep -w $L (x)" '
95 {
96 echo ${HC}x:1:x x xx x
97 } >expected &&
98 git grep -n -w -e "x xx* x" $H >actual &&
99 test_cmp expected actual
100 '
102 test_expect_success "grep -w $L (y-1)" '
103 {
104 echo ${HC}y:1:y yy
105 } >expected &&
106 git grep -n -w -e "^y" $H >actual &&
107 test_cmp expected actual
108 '
110 test_expect_success "grep -w $L (y-2)" '
111 : >expected &&
112 if git grep -n -w -e "^y y" $H >actual
113 then
114 echo should not have matched
115 cat actual
116 false
117 else
118 test_cmp expected actual
119 fi
120 '
122 test_expect_success "grep -w $L (z)" '
123 : >expected &&
124 if git grep -n -w -e "^z" $H >actual
125 then
126 echo should not have matched
127 cat actual
128 false
129 else
130 test_cmp expected actual
131 fi
132 '
134 test_expect_success "grep $L (t-1)" '
135 echo "${HC}t/t:1:test" >expected &&
136 git grep -n -e test $H >actual &&
137 test_cmp expected actual
138 '
140 test_expect_success "grep $L (t-2)" '
141 echo "${HC}t:1:test" >expected &&
142 (
143 cd t &&
144 git grep -n -e test $H
145 ) >actual &&
146 test_cmp expected actual
147 '
149 test_expect_success "grep $L (t-3)" '
150 echo "${HC}t/t:1:test" >expected &&
151 (
152 cd t &&
153 git grep --full-name -n -e test $H
154 ) >actual &&
155 test_cmp expected actual
156 '
158 test_expect_success "grep -c $L (no /dev/null)" '
159 ! git grep -c test $H | grep /dev/null
160 '
162 test_expect_success "grep --max-depth -1 $L" '
163 {
164 echo ${HC}t/a/v:1:vvv
165 echo ${HC}t/v:1:vvv
166 echo ${HC}v:1:vvv
167 } >expected &&
168 git grep --max-depth -1 -n -e vvv $H >actual &&
169 test_cmp expected actual
170 '
172 test_expect_success "grep --max-depth 0 $L" '
173 {
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 '
180 test_expect_success "grep --max-depth 0 -- '*' $L" '
181 {
182 echo ${HC}t/a/v:1:vvv
183 echo ${HC}t/v:1:vvv
184 echo ${HC}v:1:vvv
185 } >expected &&
186 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
187 test_cmp expected actual
188 '
190 test_expect_success "grep --max-depth 1 $L" '
191 {
192 echo ${HC}t/v:1:vvv
193 echo ${HC}v:1:vvv
194 } >expected &&
195 git grep --max-depth 1 -n -e vvv $H >actual &&
196 test_cmp expected actual
197 '
199 test_expect_success "grep --max-depth 0 -- t $L" '
200 {
201 echo ${HC}t/v:1:vvv
202 } >expected &&
203 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
204 test_cmp expected actual
205 '
207 test_expect_success "grep --max-depth 0 -- . t $L" '
208 {
209 echo ${HC}t/v:1:vvv
210 echo ${HC}v:1:vvv
211 } >expected &&
212 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
213 test_cmp expected actual
214 '
216 test_expect_success "grep --max-depth 0 -- t . $L" '
217 {
218 echo ${HC}t/v:1:vvv
219 echo ${HC}v:1:vvv
220 } >expected &&
221 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
222 test_cmp expected actual
223 '
225 done
227 cat >expected <<EOF
228 file:foo mmap bar_mmap
229 EOF
231 test_expect_success 'grep -e A --and -e B' '
232 git grep -e "foo mmap" --and -e bar_mmap >actual &&
233 test_cmp expected actual
234 '
236 cat >expected <<EOF
237 file:foo_mmap bar mmap
238 file:foo_mmap bar mmap baz
239 EOF
242 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
243 git grep \( -e foo_ --or -e baz \) \
244 --and -e " mmap" >actual &&
245 test_cmp expected actual
246 '
248 cat >expected <<EOF
249 file:foo mmap bar
250 EOF
252 test_expect_success 'grep -e A --and --not -e B' '
253 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
254 test_cmp expected actual
255 '
257 test_expect_success 'grep should ignore GREP_OPTIONS' '
258 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
259 test_cmp expected actual
260 '
262 test_expect_success 'grep -f, non-existent file' '
263 test_must_fail git grep -f patterns
264 '
266 cat >expected <<EOF
267 file:foo mmap bar
268 file:foo_mmap bar
269 file:foo_mmap bar mmap
270 file:foo mmap bar_mmap
271 file:foo_mmap bar mmap baz
272 EOF
274 cat >pattern <<EOF
275 mmap
276 EOF
278 test_expect_success 'grep -f, one pattern' '
279 git grep -f pattern >actual &&
280 test_cmp expected actual
281 '
283 cat >expected <<EOF
284 file:foo mmap bar
285 file:foo_mmap bar
286 file:foo_mmap bar mmap
287 file:foo mmap bar_mmap
288 file:foo_mmap bar mmap baz
289 t/a/v:vvv
290 t/v:vvv
291 v:vvv
292 EOF
294 cat >patterns <<EOF
295 mmap
296 vvv
297 EOF
299 test_expect_success 'grep -f, multiple patterns' '
300 git grep -f patterns >actual &&
301 test_cmp expected actual
302 '
304 cat >expected <<EOF
305 file:foo mmap bar
306 file:foo_mmap bar
307 file:foo_mmap bar mmap
308 file:foo mmap bar_mmap
309 file:foo_mmap bar mmap baz
310 t/a/v:vvv
311 t/v:vvv
312 v:vvv
313 EOF
315 cat >patterns <<EOF
317 mmap
319 vvv
321 EOF
323 test_expect_success 'grep -f, ignore empty lines' '
324 git grep -f patterns >actual &&
325 test_cmp expected actual
326 '
328 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
329 git grep -f - <patterns >actual &&
330 test_cmp expected actual
331 '
333 cat >expected <<EOF
334 y:y yy
335 --
336 z:zzz
337 EOF
339 test_expect_success 'grep -q, silently report matches' '
340 >empty &&
341 git grep -q mmap >actual &&
342 test_cmp empty actual &&
343 test_must_fail git grep -q qfwfq >actual &&
344 test_cmp empty actual
345 '
347 # Create 1024 file names that sort between "y" and "z" to make sure
348 # the two files are handled by different calls to an external grep.
349 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
350 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"
351 test_expect_success 'grep -C1, hunk mark between files' '
352 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
353 git add y-?? &&
354 git grep -C1 "^[yz]" >actual &&
355 test_cmp expected actual
356 '
358 test_expect_success 'grep -C1 hunk mark between files' '
359 git grep -C1 "^[yz]" >actual &&
360 test_cmp expected actual
361 '
363 test_expect_success 'log grep setup' '
364 echo a >>file &&
365 test_tick &&
366 GIT_AUTHOR_NAME="With * Asterisk" \
367 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
368 git commit -a -m "second" &&
370 echo a >>file &&
371 test_tick &&
372 git commit -a -m "third" &&
374 echo a >>file &&
375 test_tick &&
376 GIT_AUTHOR_NAME="Night Fall" \
377 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
378 git commit -a -m "fourth"
379 '
381 test_expect_success 'log grep (1)' '
382 git log --author=author --pretty=tformat:%s >actual &&
383 ( echo third ; echo initial ) >expect &&
384 test_cmp expect actual
385 '
387 test_expect_success 'log grep (2)' '
388 git log --author=" * " -F --pretty=tformat:%s >actual &&
389 ( echo second ) >expect &&
390 test_cmp expect actual
391 '
393 test_expect_success 'log grep (3)' '
394 git log --author="^A U" --pretty=tformat:%s >actual &&
395 ( echo third ; echo initial ) >expect &&
396 test_cmp expect actual
397 '
399 test_expect_success 'log grep (4)' '
400 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
401 ( echo second ) >expect &&
402 test_cmp expect actual
403 '
405 test_expect_success 'log grep (5)' '
406 git log --author=Thor -F --pretty=tformat:%s >actual &&
407 ( echo third ; echo initial ) >expect &&
408 test_cmp expect actual
409 '
411 test_expect_success 'log grep (6)' '
412 git log --author=-0700 --pretty=tformat:%s >actual &&
413 >expect &&
414 test_cmp expect actual
415 '
417 test_expect_success 'log --grep --author implicitly uses all-match' '
418 # grep matches initial and second but not third
419 # author matches only initial and third
420 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
421 echo initial >expect &&
422 test_cmp expect actual
423 '
425 test_expect_success 'log with multiple --author uses union' '
426 git log --author="Thor" --author="Aster" --format=%s >actual &&
427 {
428 echo third && echo second && echo initial
429 } >expect &&
430 test_cmp expect actual
431 '
433 test_expect_success 'log with --grep and multiple --author uses all-match' '
434 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
435 {
436 echo third && echo initial
437 } >expect &&
438 test_cmp expect actual
439 '
441 test_expect_success 'log with --grep and multiple --author uses all-match' '
442 git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
443 >expect &&
444 test_cmp expect actual
445 '
447 test_expect_success 'grep with CE_VALID file' '
448 git update-index --assume-unchanged t/t &&
449 rm t/t &&
450 test "$(git grep test)" = "t/t:test" &&
451 git update-index --no-assume-unchanged t/t &&
452 git checkout t/t
453 '
455 cat >expected <<EOF
456 hello.c=#include <stdio.h>
457 hello.c: return 0;
458 EOF
460 test_expect_success 'grep -p with userdiff' '
461 git config diff.custom.funcname "^#" &&
462 echo "hello.c diff=custom" >.gitattributes &&
463 git grep -p return >actual &&
464 test_cmp expected actual
465 '
467 cat >expected <<EOF
468 hello.c=int main(int argc, const char **argv)
469 hello.c: return 0;
470 EOF
472 test_expect_success 'grep -p' '
473 rm -f .gitattributes &&
474 git grep -p return >actual &&
475 test_cmp expected actual
476 '
478 cat >expected <<EOF
479 hello.c-#include <stdio.h>
480 hello.c=int main(int argc, const char **argv)
481 hello.c-{
482 hello.c- printf("Hello world.\n");
483 hello.c: return 0;
484 EOF
486 test_expect_success 'grep -p -B5' '
487 git grep -p -B5 return >actual &&
488 test_cmp expected actual
489 '
491 test_expect_success 'grep from a subdirectory to search wider area (1)' '
492 mkdir -p s &&
493 (
494 cd s && git grep "x x x" ..
495 )
496 '
498 test_expect_success 'grep from a subdirectory to search wider area (2)' '
499 mkdir -p s &&
500 (
501 cd s || exit 1
502 ( git grep xxyyzz .. >out ; echo $? >status )
503 ! test -s out &&
504 test 1 = $(cat status)
505 )
506 '
508 cat >expected <<EOF
509 hello.c:int main(int argc, const char **argv)
510 EOF
512 test_expect_success 'grep -Fi' '
513 git grep -Fi "CHAR *" >actual &&
514 test_cmp expected actual
515 '
517 test_expect_success 'outside of git repository' '
518 rm -fr non &&
519 mkdir -p non/git/sub &&
520 echo hello >non/git/file1 &&
521 echo world >non/git/sub/file2 &&
522 echo ".*o*" >non/git/.gitignore &&
523 {
524 echo file1:hello &&
525 echo sub/file2:world
526 } >non/expect.full &&
527 echo file2:world >non/expect.sub &&
528 (
529 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
530 export GIT_CEILING_DIRECTORIES &&
531 cd non/git &&
532 test_must_fail git grep o &&
533 git grep --no-index o >../actual.full &&
534 test_cmp ../expect.full ../actual.full
535 cd sub &&
536 test_must_fail git grep o &&
537 git grep --no-index o >../../actual.sub &&
538 test_cmp ../../expect.sub ../../actual.sub
539 )
540 '
542 test_expect_success 'inside git repository but with --no-index' '
543 rm -fr is &&
544 mkdir -p is/git/sub &&
545 echo hello >is/git/file1 &&
546 echo world >is/git/sub/file2 &&
547 echo ".*o*" >is/git/.gitignore &&
548 {
549 echo file1:hello &&
550 echo sub/file2:world
551 } >is/expect.full &&
552 : >is/expect.empty &&
553 echo file2:world >is/expect.sub &&
554 (
555 cd is/git &&
556 git init &&
557 test_must_fail git grep o >../actual.full &&
558 test_cmp ../expect.empty ../actual.full &&
559 git grep --no-index o >../actual.full &&
560 test_cmp ../expect.full ../actual.full &&
561 cd sub &&
562 test_must_fail git grep o >../../actual.sub &&
563 test_cmp ../../expect.empty ../../actual.sub &&
564 git grep --no-index o >../../actual.sub &&
565 test_cmp ../../expect.sub ../../actual.sub
566 )
567 '
569 test_expect_success 'setup double-dash tests' '
570 cat >double-dash <<EOF &&
571 --
572 ->
573 other
574 EOF
575 git add double-dash
576 '
578 cat >expected <<EOF
579 double-dash:->
580 EOF
581 test_expect_success 'grep -- pattern' '
582 git grep -- "->" >actual &&
583 test_cmp expected actual
584 '
585 test_expect_success 'grep -- pattern -- pathspec' '
586 git grep -- "->" -- double-dash >actual &&
587 test_cmp expected actual
588 '
589 test_expect_success 'grep -e pattern -- path' '
590 git grep -e "->" -- double-dash >actual &&
591 test_cmp expected actual
592 '
594 cat >expected <<EOF
595 double-dash:--
596 EOF
597 test_expect_success 'grep -e -- -- path' '
598 git grep -e -- -- double-dash >actual &&
599 test_cmp expected actual
600 '
602 test_done