Merge branch 'js/info-man-path'
[git/jnareb-git.git] / t / t7810-grep.sh
blob8184c264cf942bcd8e6c2f75b5526ddfdc5a11e9
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 -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
63 test_cmp expected actual
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
72 } >expected &&
73 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
74 test_cmp expected actual
77 test_expect_success "grep -w $L" '
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
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
94 test_expect_success "grep -w $L (x)" '
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
102 test_expect_success "grep -w $L (y-1)" '
104 echo ${HC}y:1:y yy
105 } >expected &&
106 git grep -n -w -e "^y" $H >actual &&
107 test_cmp expected actual
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
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
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
140 test_expect_success "grep $L (t-2)" '
141 echo "${HC}t:1:test" >expected &&
143 cd t &&
144 git grep -n -e test $H
145 ) >actual &&
146 test_cmp expected actual
149 test_expect_success "grep $L (t-3)" '
150 echo "${HC}t/t:1:test" >expected &&
152 cd t &&
153 git grep --full-name -n -e test $H
154 ) >actual &&
155 test_cmp expected actual
158 test_expect_success "grep -c $L (no /dev/null)" '
159 ! git grep -c test $H | grep /dev/null
162 test_expect_success "grep --max-depth -1 $L" '
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
172 test_expect_success "grep --max-depth 0 $L" '
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 0 -- '*' $L" '
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
190 test_expect_success "grep --max-depth 1 $L" '
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
199 test_expect_success "grep --max-depth 0 -- t $L" '
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
207 test_expect_success "grep --max-depth 0 -- . t $L" '
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
216 test_expect_success "grep --max-depth 0 -- t . $L" '
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
225 done
227 cat >expected <<EOF
228 file:foo mmap bar_mmap
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
236 cat >expected <<EOF
237 file:foo_mmap bar mmap
238 file:foo_mmap bar mmap baz
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
248 cat >expected <<EOF
249 file:foo mmap bar
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
257 test_expect_success 'grep should ignore GREP_OPTIONS' '
258 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
259 test_cmp expected actual
262 test_expect_success 'grep -f, non-existent file' '
263 test_must_fail git grep -f patterns
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
274 cat >pattern <<EOF
275 mmap
278 test_expect_success 'grep -f, one pattern' '
279 git grep -f pattern >actual &&
280 test_cmp expected actual
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
294 cat >patterns <<EOF
295 mmap
299 test_expect_success 'grep -f, multiple patterns' '
300 git grep -f patterns >actual &&
301 test_cmp expected actual
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
315 cat >patterns <<EOF
317 mmap
323 test_expect_success 'grep -f, ignore empty lines' '
324 git grep -f patterns >actual &&
325 test_cmp expected actual
328 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
329 git grep -f - <patterns >actual &&
330 test_cmp expected actual
333 cat >expected <<EOF
334 y:y yy
336 z:zzz
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
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
358 test_expect_success 'grep -C1 hunk mark between files' '
359 git grep -C1 "^[yz]" >actual &&
360 test_cmp expected actual
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"
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
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
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
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
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
411 test_expect_success 'log grep (6)' '
412 git log --author=-0700 --pretty=tformat:%s >actual &&
413 >expect &&
414 test_cmp expect actual
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
425 test_expect_success 'log with multiple --author uses union' '
426 git log --author="Thor" --author="Aster" --format=%s >actual &&
428 echo third && echo second && echo initial
429 } >expect &&
430 test_cmp expect actual
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 &&
436 echo third && echo initial
437 } >expect &&
438 test_cmp expect actual
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
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
455 cat >expected <<EOF
456 hello.c=#include <stdio.h>
457 hello.c: return 0;
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
467 cat >expected <<EOF
468 hello.c=int main(int argc, const char **argv)
469 hello.c: return 0;
472 test_expect_success 'grep -p' '
473 rm -f .gitattributes &&
474 git grep -p return >actual &&
475 test_cmp expected actual
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;
486 test_expect_success 'grep -p -B5' '
487 git grep -p -B5 return >actual &&
488 test_cmp expected actual
491 test_expect_success 'grep from a subdirectory to search wider area (1)' '
492 mkdir -p s &&
494 cd s && git grep "x x x" ..
498 test_expect_success 'grep from a subdirectory to search wider area (2)' '
499 mkdir -p s &&
501 cd s || exit 1
502 ( git grep xxyyzz .. >out ; echo $? >status )
503 ! test -s out &&
504 test 1 = $(cat status)
508 cat >expected <<EOF
509 hello.c:int main(int argc, const char **argv)
512 test_expect_success 'grep -Fi' '
513 git grep -Fi "CHAR *" >actual &&
514 test_cmp expected actual
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 &&
524 echo file1:hello &&
525 echo sub/file2:world
526 } >non/expect.full &&
527 echo file2:world >non/expect.sub &&
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
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 &&
549 echo file1:hello &&
550 echo sub/file2:world
551 } >is/expect.full &&
552 : >is/expect.empty &&
553 echo file2:world >is/expect.sub &&
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
569 test_expect_success 'setup double-dash tests' '
570 cat >double-dash <<EOF &&
573 other
575 git add double-dash
578 cat >expected <<EOF
579 double-dash:->
581 test_expect_success 'grep -- pattern' '
582 git grep -- "->" >actual &&
583 test_cmp expected actual
585 test_expect_success 'grep -- pattern -- pathspec' '
586 git grep -- "->" -- double-dash >actual &&
587 test_cmp expected actual
589 test_expect_success 'grep -e pattern -- path' '
590 git grep -e "->" -- double-dash >actual &&
591 test_cmp expected actual
594 cat >expected <<EOF
595 double-dash:--
597 test_expect_success 'grep -e -- -- path' '
598 git grep -e -- -- double-dash >actual &&
599 test_cmp expected actual
602 test_done