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
251 test_expect_success 'grep -l -C' '
252 git grep -l -C1 foo >actual &&
253 test_cmp expected actual
259 test_expect_success 'grep -l -C' '
260 git grep -c -C1 foo >actual &&
261 test_cmp expected actual
264 test_expect_success 'grep -L -C' '
265 git ls-files >expected &&
266 git grep -L -C1 nonexistent_string >actual &&
267 test_cmp expected actual
271 file:foo mmap bar_mmap
274 test_expect_success 'grep -e A --and -e B' '
275 git grep -e "foo mmap" --and -e bar_mmap >actual &&
276 test_cmp expected actual
280 file:foo_mmap bar mmap
281 file:foo_mmap bar mmap baz
285 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
286 git grep \( -e foo_ --or -e baz \) \
287 --and -e " mmap" >actual &&
288 test_cmp expected actual
295 test_expect_success 'grep -e A --and --not -e B' '
296 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
297 test_cmp expected actual
300 test_expect_success 'grep should ignore GREP_OPTIONS' '
301 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
302 test_cmp expected actual
305 test_expect_success 'grep -f, non-existent file' '
306 test_must_fail git grep -f patterns
312 file:foo_mmap bar mmap
313 file:foo mmap bar_mmap
314 file:foo_mmap bar mmap baz
321 test_expect_success 'grep -f, one pattern' '
322 git grep -f pattern >actual &&
323 test_cmp expected actual
329 file:foo_mmap bar mmap
330 file:foo mmap bar_mmap
331 file:foo_mmap bar mmap baz
342 test_expect_success 'grep -f, multiple patterns' '
343 git grep -f patterns >actual &&
344 test_cmp expected actual
350 file:foo_mmap bar mmap
351 file:foo mmap bar_mmap
352 file:foo_mmap bar mmap baz
366 test_expect_success 'grep -f, ignore empty lines' '
367 git grep -f patterns >actual &&
368 test_cmp expected actual
371 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
372 git grep -f - <patterns >actual &&
373 test_cmp expected actual
382 test_expect_success 'grep -q, silently report matches' '
384 git grep -q mmap >actual &&
385 test_cmp empty actual &&
386 test_must_fail git grep -q qfwfq >actual &&
387 test_cmp empty actual
390 # Create 1024 file names that sort between "y" and "z" to make sure
391 # the two files are handled by different calls to an external grep.
392 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
393 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"
394 test_expect_success 'grep -C1, hunk mark between files' '
395 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
397 git grep -C1 "^[yz]" >actual &&
398 test_cmp expected actual
401 test_expect_success 'grep -C1 hunk mark between files' '
402 git grep -C1 "^[yz]" >actual &&
403 test_cmp expected actual
406 test_expect_success 'log grep setup' '
409 GIT_AUTHOR_NAME="With * Asterisk" \
410 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
411 git commit -a -m "second" &&
415 git commit -a -m "third" &&
419 GIT_AUTHOR_NAME="Night Fall" \
420 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
421 git commit -a -m "fourth"
424 test_expect_success 'log grep (1)' '
425 git log --author=author --pretty=tformat:%s >actual &&
426 ( echo third ; echo initial ) >expect &&
427 test_cmp expect actual
430 test_expect_success 'log grep (2)' '
431 git log --author=" * " -F --pretty=tformat:%s >actual &&
432 ( echo second ) >expect &&
433 test_cmp expect actual
436 test_expect_success 'log grep (3)' '
437 git log --author="^A U" --pretty=tformat:%s >actual &&
438 ( echo third ; echo initial ) >expect &&
439 test_cmp expect actual
442 test_expect_success 'log grep (4)' '
443 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
444 ( echo second ) >expect &&
445 test_cmp expect actual
448 test_expect_success 'log grep (5)' '
449 git log --author=Thor -F --pretty=tformat:%s >actual &&
450 ( echo third ; echo initial ) >expect &&
451 test_cmp expect actual
454 test_expect_success 'log grep (6)' '
455 git log --author=-0700 --pretty=tformat:%s >actual &&
457 test_cmp expect actual
460 test_expect_success 'log --grep --author implicitly uses all-match' '
461 # grep matches initial and second but not third
462 # author matches only initial and third
463 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
464 echo initial >expect &&
465 test_cmp expect actual
468 test_expect_success 'log with multiple --author uses union' '
469 git log --author="Thor" --author="Aster" --format=%s >actual &&
471 echo third && echo second && echo initial
473 test_cmp expect actual
476 test_expect_success 'log with --grep and multiple --author uses all-match' '
477 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
479 echo third && echo initial
481 test_cmp expect actual
484 test_expect_success 'log with --grep and multiple --author uses all-match' '
485 git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
487 test_cmp expect actual
490 test_expect_success 'grep with CE_VALID file' '
491 git update-index --assume-unchanged t/t &&
493 test "$(git grep test)" = "t/t:test" &&
494 git update-index --no-assume-unchanged t/t &&
499 hello.c=#include <stdio.h>
503 test_expect_success 'grep -p with userdiff' '
504 git config diff.custom.funcname "^#" &&
505 echo "hello.c diff=custom" >.gitattributes &&
506 git grep -p return >actual &&
507 test_cmp expected actual
511 hello.c=int main(int argc, const char **argv)
515 test_expect_success 'grep -p' '
516 rm -f .gitattributes &&
517 git grep -p return >actual &&
518 test_cmp expected actual
522 hello.c-#include <stdio.h>
523 hello.c=int main(int argc, const char **argv)
525 hello.c- printf("Hello world.\n");
529 test_expect_success 'grep -p -B5' '
530 git grep -p -B5 return >actual &&
531 test_cmp expected actual
535 hello.c=int main(int argc, const char **argv)
537 hello.c- printf("Hello world.\n");
539 hello.c- /* char ?? */
543 test_expect_success 'grep -W' '
544 git grep -W return >actual &&
545 test_cmp expected actual
549 hello.c= printf("Hello world.\n");
551 hello.c- /* char ?? */
554 test_expect_success 'grep -W with userdiff' '
555 test_when_finished "rm -f .gitattributes" &&
556 git config diff.custom.xfuncname "(printf.*|})$" &&
557 echo "hello.c diff=custom" >.gitattributes &&
558 git grep -W return >actual &&
559 test_cmp expected actual
562 test_expect_success 'grep from a subdirectory to search wider area (1)' '
565 cd s && git grep "x x x" ..
569 test_expect_success 'grep from a subdirectory to search wider area (2)' '
573 ( git grep xxyyzz .. >out ; echo $? >status )
575 test 1 = $(cat status)
580 hello.c:int main(int argc, const char **argv)
583 test_expect_success 'grep -Fi' '
584 git grep -Fi "CHAR *" >actual &&
585 test_cmp expected actual
588 test_expect_success 'outside of git repository' '
590 mkdir -p non/git/sub &&
591 echo hello >non/git/file1 &&
592 echo world >non/git/sub/file2 &&
596 } >non/expect.full &&
597 echo file2:world >non/expect.sub &&
599 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
600 export GIT_CEILING_DIRECTORIES &&
602 test_must_fail git grep o &&
603 git grep --no-index o >../actual.full &&
604 test_cmp ../expect.full ../actual.full
606 test_must_fail git grep o &&
607 git grep --no-index o >../../actual.sub &&
608 test_cmp ../../expect.sub ../../actual.sub
611 echo ".*o*" >non/git/.gitignore &&
613 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
614 export GIT_CEILING_DIRECTORIES &&
616 test_must_fail git grep o &&
617 git grep --no-index --exclude-standard o >../actual.full &&
618 test_cmp ../expect.full ../actual.full &&
621 echo ".gitignore:.*o*"
623 } >../expect.with.ignored &&
624 git grep --no-index --no-exclude o >../actual.full &&
625 test_cmp ../expect.with.ignored ../actual.full
629 test_expect_success 'inside git repository but with --no-index' '
631 mkdir -p is/git/sub &&
632 echo hello >is/git/file1 &&
633 echo world >is/git/sub/file2 &&
634 echo ".*o*" >is/git/.gitignore &&
638 } >is/expect.unignored &&
640 echo ".gitignore:.*o*" &&
641 cat is/expect.unignored
643 : >is/expect.empty &&
644 echo file2:world >is/expect.sub &&
648 test_must_fail git grep o >../actual.full &&
649 test_cmp ../expect.empty ../actual.full &&
651 git grep --untracked o >../actual.unignored &&
652 test_cmp ../expect.unignored ../actual.unignored &&
654 git grep --no-index o >../actual.full &&
655 test_cmp ../expect.full ../actual.full &&
657 git grep --no-index --exclude-standard o >../actual.unignored &&
658 test_cmp ../expect.unignored ../actual.unignored &&
661 test_must_fail git grep o >../../actual.sub &&
662 test_cmp ../../expect.empty ../../actual.sub &&
664 git grep --no-index o >../../actual.sub &&
665 test_cmp ../../expect.sub ../../actual.sub &&
667 git grep --untracked o >../../actual.sub &&
668 test_cmp ../../expect.sub ../../actual.sub
672 test_expect_success 'setup double-dash tests' '
673 cat >double-dash <<EOF &&
684 test_expect_success 'grep -- pattern' '
685 git grep -- "->" >actual &&
686 test_cmp expected actual
688 test_expect_success 'grep -- pattern -- pathspec' '
689 git grep -- "->" -- double-dash >actual &&
690 test_cmp expected actual
692 test_expect_success 'grep -e pattern -- path' '
693 git grep -e "->" -- double-dash >actual &&
694 test_cmp expected actual
700 test_expect_success 'grep -e -- -- path' '
701 git grep -e -- -- double-dash >actual &&
702 test_cmp expected actual
706 hello.c:int main(int argc, const char **argv)
707 hello.c: printf("Hello world.\n");
710 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
711 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
712 test_cmp expected actual
715 test_expect_success LIBPCRE 'grep -P pattern' '
716 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
717 test_cmp expected actual
720 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
722 test_must_fail git -c grep.extendedregexp=true \
723 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
724 test_cmp empty actual
727 test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
728 git -c grep.extendedregexp=true \
729 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
730 test_cmp expected actual
733 test_expect_success LIBPCRE 'grep -P -v pattern' '
738 git grep -P -v "abc" ab >actual &&
739 test_cmp expected actual
742 test_expect_success LIBPCRE 'grep -P -i pattern' '
743 cat >expected <<-EOF &&
744 hello.c: printf("Hello world.\n");
746 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
747 test_cmp expected actual
750 test_expect_success LIBPCRE 'grep -P -w pattern' '
752 echo "hello_world:Hello world"
753 echo "hello_world:HeLLo world"
755 git grep -P -w "He((?i)ll)o" hello_world >actual &&
756 test_cmp expected actual
759 test_expect_success 'grep -G invalidpattern properly dies ' '
760 test_must_fail git grep -G "a["
763 test_expect_success 'grep -E invalidpattern properly dies ' '
764 test_must_fail git grep -E "a["
767 test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
768 test_must_fail git grep -P "a["
771 test_expect_success 'grep -G -E -F pattern' '
772 echo "ab:a+b*c" >expected &&
773 git grep -G -E -F "a+b*c" ab >actual &&
774 test_cmp expected actual
777 test_expect_success 'grep -E -F -G pattern' '
778 echo "ab:a+bc" >expected &&
779 git grep -E -F -G "a+b*c" ab >actual &&
780 test_cmp expected actual
783 test_expect_success 'grep -F -G -E pattern' '
784 echo "ab:abc" >expected &&
785 git grep -F -G -E "a+b*c" ab >actual &&
786 test_cmp expected actual
789 test_expect_success 'grep -G -F -P -E pattern' '
791 test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
792 test_cmp empty actual
795 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
796 echo "ab:a+b*c" >expected &&
797 git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
798 test_cmp expected actual
802 git config "$1" "$2" &&
803 test_when_finished "git config --unset $1"
807 hello.c<RED>:<RESET>int main(int argc, const char **argv)
808 hello.c<RED>-<RESET>{
810 hello.c<RED>:<RESET> /* char ?? */
811 hello.c<RED>-<RESET>}
813 hello_world<RED>:<RESET>Hello_world
814 hello_world<RED>-<RESET>HeLLo_world
817 test_expect_success 'grep --color, separator' '
818 test_config color.grep.context normal &&
819 test_config color.grep.filename normal &&
820 test_config color.grep.function normal &&
821 test_config color.grep.linenumber normal &&
822 test_config color.grep.match normal &&
823 test_config color.grep.selected normal &&
824 test_config color.grep.separator red &&
826 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
827 test_decode_color >actual &&
828 test_cmp expected actual
832 hello.c:int main(int argc, const char **argv)
833 hello.c: /* char ?? */
835 hello_world:Hello_world
838 test_expect_success 'grep --break' '
839 git grep --break -e char -e lo_w hello.c hello_world >actual &&
840 test_cmp expected actual
844 hello.c:int main(int argc, const char **argv)
847 hello.c: /* char ?? */
850 hello_world:Hello_world
851 hello_world-HeLLo_world
854 test_expect_success 'grep --break with context' '
855 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
856 test_cmp expected actual
861 int main(int argc, const char **argv)
867 test_expect_success 'grep --heading' '
868 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
869 test_cmp expected actual
873 <BOLD;GREEN>hello.c<RESET>
874 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
875 6: /* <BLACK;BYELLOW>char<RESET> ?? */
877 <BOLD;GREEN>hello_world<RESET>
878 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
881 test_expect_success 'mimic ack-grep --group' '
882 test_config color.grep.context normal &&
883 test_config color.grep.filename "bold green" &&
884 test_config color.grep.function normal &&
885 test_config color.grep.linenumber normal &&
886 test_config color.grep.match "black yellow" &&
887 test_config color.grep.selected normal &&
888 test_config color.grep.separator normal &&
890 git grep --break --heading -n --color \
891 -e char -e lo_w hello.c hello_world |
892 test_decode_color >actual &&
893 test_cmp expected actual