3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git grep various.
13 int main(int argc, const char **argv)
15 printf("Hello world.\n");
21 test_expect_success setup '
25 echo foo_mmap bar mmap
26 echo foo mmap bar_mmap
27 echo foo_mmap bar mmap baz
55 test_expect_success 'grep should not segfault with a bad input' '
56 test_must_fail git grep "("
62 HEAD) HC='HEAD:' L='HEAD' ;;
63 '') HC= L='in working tree' ;;
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
73 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
74 test_cmp expected actual
77 test_expect_success "grep -w $L" '
79 echo ${HC}file:1:foo mmap bar
80 echo ${HC}file:3:foo_mmap bar mmap
81 echo ${HC}file:4:foo mmap bar_mmap
82 echo ${HC}file:5:foo_mmap bar mmap baz
84 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
85 test_cmp expected actual
88 test_expect_success "grep -w $L" '
90 echo ${HC}file:foo mmap bar
91 echo ${HC}file:foo_mmap bar mmap
92 echo ${HC}file:foo mmap bar_mmap
93 echo ${HC}file:foo_mmap bar mmap baz
95 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
96 test_cmp expected actual
99 test_expect_success "grep -w $L (w)" '
101 test_must_fail git grep -n -w -e "^w" >actual &&
102 test_cmp expected actual
105 test_expect_success "grep -w $L (x)" '
107 echo ${HC}x:1:x x xx x
109 git grep -n -w -e "x xx* x" $H >actual &&
110 test_cmp expected actual
113 test_expect_success "grep -w $L (y-1)" '
117 git grep -n -w -e "^y" $H >actual &&
118 test_cmp expected actual
121 test_expect_success "grep -w $L (y-2)" '
123 if git grep -n -w -e "^y y" $H >actual
125 echo should not have matched
129 test_cmp expected actual
133 test_expect_success "grep -w $L (z)" '
135 if git grep -n -w -e "^z" $H >actual
137 echo should not have matched
141 test_cmp expected actual
145 test_expect_success "grep $L (t-1)" '
146 echo "${HC}t/t:1:test" >expected &&
147 git grep -n -e test $H >actual &&
148 test_cmp expected actual
151 test_expect_success "grep $L (t-2)" '
152 echo "${HC}t:1:test" >expected &&
155 git grep -n -e test $H
157 test_cmp expected actual
160 test_expect_success "grep $L (t-3)" '
161 echo "${HC}t/t:1:test" >expected &&
164 git grep --full-name -n -e test $H
166 test_cmp expected actual
169 test_expect_success "grep -c $L (no /dev/null)" '
170 ! git grep -c test $H | grep /dev/null
173 test_expect_success "grep --max-depth -1 $L" '
175 echo ${HC}t/a/v:1:vvv
179 git grep --max-depth -1 -n -e vvv $H >actual &&
180 test_cmp expected actual
183 test_expect_success "grep --max-depth 0 $L" '
187 git grep --max-depth 0 -n -e vvv $H >actual &&
188 test_cmp expected actual
191 test_expect_success "grep --max-depth 0 -- '*' $L" '
193 echo ${HC}t/a/v:1:vvv
197 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
198 test_cmp expected actual
201 test_expect_success "grep --max-depth 1 $L" '
206 git grep --max-depth 1 -n -e vvv $H >actual &&
207 test_cmp expected actual
210 test_expect_success "grep --max-depth 0 -- t $L" '
214 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
215 test_cmp expected actual
218 test_expect_success "grep --max-depth 0 -- . t $L" '
223 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
224 test_cmp expected actual
227 test_expect_success "grep --max-depth 0 -- t . $L" '
232 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
233 test_cmp expected actual
235 test_expect_success "grep $L with grep.extendedRegexp=false" '
236 echo "ab:a+bc" >expected &&
237 git -c grep.extendedRegexp=false grep "a+b*c" ab >actual &&
238 test_cmp expected actual
241 test_expect_success "grep $L with grep.extendedRegexp=true" '
242 echo "ab:abc" >expected &&
243 git -c grep.extendedRegexp=true grep "a+b*c" ab >actual &&
244 test_cmp expected actual
249 file:foo mmap bar_mmap
252 test_expect_success 'grep -e A --and -e B' '
253 git grep -e "foo mmap" --and -e bar_mmap >actual &&
254 test_cmp expected actual
258 file:foo_mmap bar mmap
259 file:foo_mmap bar mmap baz
263 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
264 git grep \( -e foo_ --or -e baz \) \
265 --and -e " mmap" >actual &&
266 test_cmp expected actual
273 test_expect_success 'grep -e A --and --not -e B' '
274 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
275 test_cmp expected actual
278 test_expect_success 'grep should ignore GREP_OPTIONS' '
279 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
280 test_cmp expected actual
283 test_expect_success 'grep -f, non-existent file' '
284 test_must_fail git grep -f patterns
290 file:foo_mmap bar mmap
291 file:foo mmap bar_mmap
292 file:foo_mmap bar mmap baz
299 test_expect_success 'grep -f, one pattern' '
300 git grep -f pattern >actual &&
301 test_cmp expected actual
307 file:foo_mmap bar mmap
308 file:foo mmap bar_mmap
309 file:foo_mmap bar mmap baz
320 test_expect_success 'grep -f, multiple patterns' '
321 git grep -f patterns >actual &&
322 test_cmp expected actual
328 file:foo_mmap bar mmap
329 file:foo mmap bar_mmap
330 file:foo_mmap bar mmap baz
344 test_expect_success 'grep -f, ignore empty lines' '
345 git grep -f patterns >actual &&
346 test_cmp expected actual
349 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
350 git grep -f - <patterns >actual &&
351 test_cmp expected actual
360 test_expect_success 'grep -q, silently report matches' '
362 git grep -q mmap >actual &&
363 test_cmp empty actual &&
364 test_must_fail git grep -q qfwfq >actual &&
365 test_cmp empty actual
368 # Create 1024 file names that sort between "y" and "z" to make sure
369 # the two files are handled by different calls to an external grep.
370 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
371 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"
372 test_expect_success 'grep -C1, hunk mark between files' '
373 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
375 git grep -C1 "^[yz]" >actual &&
376 test_cmp expected actual
379 test_expect_success 'grep -C1 hunk mark between files' '
380 git grep -C1 "^[yz]" >actual &&
381 test_cmp expected actual
384 test_expect_success 'log grep setup' '
387 GIT_AUTHOR_NAME="With * Asterisk" \
388 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
389 git commit -a -m "second" &&
393 git commit -a -m "third" &&
397 GIT_AUTHOR_NAME="Night Fall" \
398 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
399 git commit -a -m "fourth"
402 test_expect_success 'log grep (1)' '
403 git log --author=author --pretty=tformat:%s >actual &&
404 ( echo third ; echo initial ) >expect &&
405 test_cmp expect actual
408 test_expect_success 'log grep (2)' '
409 git log --author=" * " -F --pretty=tformat:%s >actual &&
410 ( echo second ) >expect &&
411 test_cmp expect actual
414 test_expect_success 'log grep (3)' '
415 git log --author="^A U" --pretty=tformat:%s >actual &&
416 ( echo third ; echo initial ) >expect &&
417 test_cmp expect actual
420 test_expect_success 'log grep (4)' '
421 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
422 ( echo second ) >expect &&
423 test_cmp expect actual
426 test_expect_success 'log grep (5)' '
427 git log --author=Thor -F --pretty=tformat:%s >actual &&
428 ( echo third ; echo initial ) >expect &&
429 test_cmp expect actual
432 test_expect_success 'log grep (6)' '
433 git log --author=-0700 --pretty=tformat:%s >actual &&
435 test_cmp expect actual
438 test_expect_success 'log --grep --author implicitly uses all-match' '
439 # grep matches initial and second but not third
440 # author matches only initial and third
441 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
442 echo initial >expect &&
443 test_cmp expect actual
446 test_expect_success 'log with multiple --author uses union' '
447 git log --author="Thor" --author="Aster" --format=%s >actual &&
449 echo third && echo second && echo initial
451 test_cmp expect actual
454 test_expect_success 'log with --grep and multiple --author uses all-match' '
455 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
457 echo third && echo initial
459 test_cmp expect actual
462 test_expect_success 'log with --grep and multiple --author uses all-match' '
463 git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
465 test_cmp expect actual
468 test_expect_success 'grep with CE_VALID file' '
469 git update-index --assume-unchanged t/t &&
471 test "$(git grep test)" = "t/t:test" &&
472 git update-index --no-assume-unchanged t/t &&
477 hello.c=#include <stdio.h>
481 test_expect_success 'grep -p with userdiff' '
482 git config diff.custom.funcname "^#" &&
483 echo "hello.c diff=custom" >.gitattributes &&
484 git grep -p return >actual &&
485 test_cmp expected actual
489 hello.c=int main(int argc, const char **argv)
493 test_expect_success 'grep -p' '
494 rm -f .gitattributes &&
495 git grep -p return >actual &&
496 test_cmp expected actual
500 hello.c-#include <stdio.h>
501 hello.c=int main(int argc, const char **argv)
503 hello.c- printf("Hello world.\n");
507 test_expect_success 'grep -p -B5' '
508 git grep -p -B5 return >actual &&
509 test_cmp expected actual
513 hello.c=int main(int argc, const char **argv)
515 hello.c- printf("Hello world.\n");
517 hello.c- /* char ?? */
521 test_expect_success 'grep -W' '
522 git grep -W return >actual &&
523 test_cmp expected actual
526 test_expect_success 'grep from a subdirectory to search wider area (1)' '
529 cd s && git grep "x x x" ..
533 test_expect_success 'grep from a subdirectory to search wider area (2)' '
537 ( git grep xxyyzz .. >out ; echo $? >status )
539 test 1 = $(cat status)
544 hello.c:int main(int argc, const char **argv)
547 test_expect_success 'grep -Fi' '
548 git grep -Fi "CHAR *" >actual &&
549 test_cmp expected actual
552 test_expect_success 'outside of git repository' '
554 mkdir -p non/git/sub &&
555 echo hello >non/git/file1 &&
556 echo world >non/git/sub/file2 &&
557 echo ".*o*" >non/git/.gitignore &&
561 } >non/expect.full &&
562 echo file2:world >non/expect.sub &&
564 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
565 export GIT_CEILING_DIRECTORIES &&
567 test_must_fail git grep o &&
568 git grep --no-index o >../actual.full &&
569 test_cmp ../expect.full ../actual.full
571 test_must_fail git grep o &&
572 git grep --no-index o >../../actual.sub &&
573 test_cmp ../../expect.sub ../../actual.sub
577 test_expect_success 'inside git repository but with --no-index' '
579 mkdir -p is/git/sub &&
580 echo hello >is/git/file1 &&
581 echo world >is/git/sub/file2 &&
582 echo ".*o*" >is/git/.gitignore &&
587 : >is/expect.empty &&
588 echo file2:world >is/expect.sub &&
592 test_must_fail git grep o >../actual.full &&
593 test_cmp ../expect.empty ../actual.full &&
594 git grep --no-index o >../actual.full &&
595 test_cmp ../expect.full ../actual.full &&
597 test_must_fail git grep o >../../actual.sub &&
598 test_cmp ../../expect.empty ../../actual.sub &&
599 git grep --no-index o >../../actual.sub &&
600 test_cmp ../../expect.sub ../../actual.sub
604 test_expect_success 'setup double-dash tests' '
605 cat >double-dash <<EOF &&
616 test_expect_success 'grep -- pattern' '
617 git grep -- "->" >actual &&
618 test_cmp expected actual
620 test_expect_success 'grep -- pattern -- pathspec' '
621 git grep -- "->" -- double-dash >actual &&
622 test_cmp expected actual
624 test_expect_success 'grep -e pattern -- path' '
625 git grep -e "->" -- double-dash >actual &&
626 test_cmp expected actual
632 test_expect_success 'grep -e -- -- path' '
633 git grep -e -- -- double-dash >actual &&
634 test_cmp expected actual
638 hello.c:int main(int argc, const char **argv)
639 hello.c: printf("Hello world.\n");
642 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
643 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
644 test_cmp expected actual
647 test_expect_success LIBPCRE 'grep -P pattern' '
648 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
649 test_cmp expected actual
652 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
654 test_must_fail git -c grep.extendedregexp=true \
655 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
656 test_cmp empty actual
659 test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
660 git -c grep.extendedregexp=true \
661 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
662 test_cmp expected actual
665 test_expect_success LIBPCRE 'grep -P -v pattern' '
670 git grep -P -v "abc" ab >actual &&
671 test_cmp expected actual
674 test_expect_success LIBPCRE 'grep -P -i pattern' '
675 cat >expected <<-EOF &&
676 hello.c: printf("Hello world.\n");
678 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
679 test_cmp expected actual
682 test_expect_success LIBPCRE 'grep -P -w pattern' '
684 echo "hello_world:Hello world"
685 echo "hello_world:HeLLo world"
687 git grep -P -w "He((?i)ll)o" hello_world >actual &&
688 test_cmp expected actual
691 test_expect_success 'grep -G invalidpattern properly dies ' '
692 test_must_fail git grep -G "a["
695 test_expect_success 'grep -E invalidpattern properly dies ' '
696 test_must_fail git grep -E "a["
699 test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
700 test_must_fail git grep -P "a["
703 test_expect_success 'grep -G -E -F pattern' '
704 echo "ab:a+b*c" >expected &&
705 git grep -G -E -F "a+b*c" ab >actual &&
706 test_cmp expected actual
709 test_expect_success 'grep -E -F -G pattern' '
710 echo "ab:a+bc" >expected &&
711 git grep -E -F -G "a+b*c" ab >actual &&
712 test_cmp expected actual
715 test_expect_success 'grep -F -G -E pattern' '
716 echo "ab:abc" >expected &&
717 git grep -F -G -E "a+b*c" ab >actual &&
718 test_cmp expected actual
721 test_expect_success 'grep -G -F -P -E pattern' '
723 test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
724 test_cmp empty actual
727 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
728 echo "ab:a+b*c" >expected &&
729 git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
730 test_cmp expected actual
734 git config "$1" "$2" &&
735 test_when_finished "git config --unset $1"
739 hello.c<RED>:<RESET>int main(int argc, const char **argv)
740 hello.c<RED>-<RESET>{
742 hello.c<RED>:<RESET> /* char ?? */
743 hello.c<RED>-<RESET>}
745 hello_world<RED>:<RESET>Hello_world
746 hello_world<RED>-<RESET>HeLLo_world
749 test_expect_success 'grep --color, separator' '
750 test_config color.grep.context normal &&
751 test_config color.grep.filename normal &&
752 test_config color.grep.function normal &&
753 test_config color.grep.linenumber normal &&
754 test_config color.grep.match normal &&
755 test_config color.grep.selected normal &&
756 test_config color.grep.separator red &&
758 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
759 test_decode_color >actual &&
760 test_cmp expected actual
764 hello.c:int main(int argc, const char **argv)
765 hello.c: /* char ?? */
767 hello_world:Hello_world
770 test_expect_success 'grep --break' '
771 git grep --break -e char -e lo_w hello.c hello_world >actual &&
772 test_cmp expected actual
776 hello.c:int main(int argc, const char **argv)
779 hello.c: /* char ?? */
782 hello_world:Hello_world
783 hello_world-HeLLo_world
786 test_expect_success 'grep --break with context' '
787 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
788 test_cmp expected actual
793 int main(int argc, const char **argv)
799 test_expect_success 'grep --heading' '
800 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
801 test_cmp expected actual
805 <BOLD;GREEN>hello.c<RESET>
806 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
807 6: /* <BLACK;BYELLOW>char<RESET> ?? */
809 <BOLD;GREEN>hello_world<RESET>
810 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
813 test_expect_success 'mimic ack-grep --group' '
814 test_config color.grep.context normal &&
815 test_config color.grep.filename "bold green" &&
816 test_config color.grep.function normal &&
817 test_config color.grep.linenumber normal &&
818 test_config color.grep.match "black yellow" &&
819 test_config color.grep.selected normal &&
820 test_config color.grep.separator normal &&
822 git grep --break --heading -n --color \
823 -e char -e lo_w hello.c hello_world |
824 test_decode_color >actual &&
825 test_cmp expected actual