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
51 echo "line without leading space1"
52 echo " line with leading space1"
53 echo " line with leading space2"
54 echo " line with leading space3"
55 echo "line without leading space2"
62 test_expect_success
'grep should not segfault with a bad input' '
63 test_must_fail git grep "("
69 HEAD
) HC
='HEAD:' L
='HEAD' ;;
70 '') HC
= L
='in working tree' ;;
73 test_expect_success
"grep -w $L" '
75 echo ${HC}file:1:foo mmap bar
76 echo ${HC}file:3:foo_mmap bar mmap
77 echo ${HC}file:4:foo mmap bar_mmap
78 echo ${HC}file:5:foo_mmap bar mmap baz
80 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
81 test_cmp expected actual
84 test_expect_success
"grep -w $L" '
86 echo ${HC}file:1:foo mmap bar
87 echo ${HC}file:3:foo_mmap bar mmap
88 echo ${HC}file:4:foo mmap bar_mmap
89 echo ${HC}file:5:foo_mmap bar mmap baz
91 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
92 test_cmp expected actual
95 test_expect_success
"grep -w $L" '
97 echo ${HC}file:foo mmap bar
98 echo ${HC}file:foo_mmap bar mmap
99 echo ${HC}file:foo mmap bar_mmap
100 echo ${HC}file:foo_mmap bar mmap baz
102 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
103 test_cmp expected actual
106 test_expect_success
"grep -w $L (w)" '
108 test_must_fail git grep -n -w -e "^w" >actual &&
109 test_cmp expected actual
112 test_expect_success
"grep -w $L (x)" '
114 echo ${HC}x:1:x x xx x
116 git grep -n -w -e "x xx* x" $H >actual &&
117 test_cmp expected actual
120 test_expect_success
"grep -w $L (y-1)" '
124 git grep -n -w -e "^y" $H >actual &&
125 test_cmp expected actual
128 test_expect_success
"grep -w $L (y-2)" '
130 if git grep -n -w -e "^y y" $H >actual
132 echo should not have matched
136 test_cmp expected actual
140 test_expect_success
"grep -w $L (z)" '
142 if git grep -n -w -e "^z" $H >actual
144 echo should not have matched
148 test_cmp expected actual
152 test_expect_success
"grep $L (t-1)" '
153 echo "${HC}t/t:1:test" >expected &&
154 git grep -n -e test $H >actual &&
155 test_cmp expected actual
158 test_expect_success
"grep $L (t-2)" '
159 echo "${HC}t:1:test" >expected &&
162 git grep -n -e test $H
164 test_cmp expected actual
167 test_expect_success
"grep $L (t-3)" '
168 echo "${HC}t/t:1:test" >expected &&
171 git grep --full-name -n -e test $H
173 test_cmp expected actual
176 test_expect_success
"grep -c $L (no /dev/null)" '
177 ! git grep -c test $H | grep /dev/null
180 test_expect_success
"grep --max-depth -1 $L" '
182 echo ${HC}t/a/v:1:vvv
186 git grep --max-depth -1 -n -e vvv $H >actual &&
187 test_cmp expected actual
190 test_expect_success
"grep --max-depth 0 $L" '
194 git grep --max-depth 0 -n -e vvv $H >actual &&
195 test_cmp expected actual
198 test_expect_success
"grep --max-depth 0 -- '*' $L" '
200 echo ${HC}t/a/v:1:vvv
204 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
205 test_cmp expected actual
208 test_expect_success
"grep --max-depth 1 $L" '
213 git grep --max-depth 1 -n -e vvv $H >actual &&
214 test_cmp expected actual
217 test_expect_success
"grep --max-depth 0 -- t $L" '
221 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
222 test_cmp expected actual
225 test_expect_success
"grep --max-depth 0 -- . t $L" '
230 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
231 test_cmp expected actual
234 test_expect_success
"grep --max-depth 0 -- t . $L" '
239 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
240 test_cmp expected actual
242 test_expect_success
"grep $L with grep.extendedRegexp=false" '
243 echo "ab:a+bc" >expected &&
244 git -c grep.extendedRegexp=false grep "a+b*c" ab >actual &&
245 test_cmp expected actual
248 test_expect_success
"grep $L with grep.extendedRegexp=true" '
249 echo "ab:abc" >expected &&
250 git -c grep.extendedRegexp=true grep "a+b*c" ab >actual &&
251 test_cmp expected actual
258 test_expect_success
'grep -l -C' '
259 git grep -l -C1 foo >actual &&
260 test_cmp expected actual
266 test_expect_success
'grep -l -C' '
267 git grep -c -C1 foo >actual &&
268 test_cmp expected actual
271 test_expect_success
'grep -L -C' '
272 git ls-files >expected &&
273 git grep -L -C1 nonexistent_string >actual &&
274 test_cmp expected actual
278 file:foo mmap bar_mmap
281 test_expect_success
'grep -e A --and -e B' '
282 git grep -e "foo mmap" --and -e bar_mmap >actual &&
283 test_cmp expected actual
287 file:foo_mmap bar mmap
288 file:foo_mmap bar mmap baz
292 test_expect_success
'grep ( -e A --or -e B ) --and -e B' '
293 git grep \( -e foo_ --or -e baz \) \
294 --and -e " mmap" >actual &&
295 test_cmp expected actual
302 test_expect_success
'grep -e A --and --not -e B' '
303 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
304 test_cmp expected actual
307 test_expect_success
'grep should ignore GREP_OPTIONS' '
308 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
309 test_cmp expected actual
312 test_expect_success
'grep -f, non-existent file' '
313 test_must_fail git grep -f patterns
319 file:foo_mmap bar mmap
320 file:foo mmap bar_mmap
321 file:foo_mmap bar mmap baz
328 test_expect_success
'grep -f, one pattern' '
329 git grep -f pattern >actual &&
330 test_cmp expected actual
336 file:foo_mmap bar mmap
337 file:foo mmap bar_mmap
338 file:foo_mmap bar mmap baz
349 test_expect_success
'grep -f, multiple patterns' '
350 git grep -f patterns >actual &&
351 test_cmp expected actual
354 test_expect_success
'grep, multiple patterns' '
355 git grep "$(cat patterns)" >actual &&
356 test_cmp expected actual
362 file:foo_mmap bar mmap
363 file:foo mmap bar_mmap
364 file:foo_mmap bar mmap baz
378 test_expect_success
'grep -f, ignore empty lines' '
379 git grep -f patterns >actual &&
380 test_cmp expected actual
383 test_expect_success
'grep -f, ignore empty lines, read patterns from stdin' '
384 git grep -f - <patterns >actual &&
385 test_cmp expected actual
394 test_expect_success
'grep -q, silently report matches' '
396 git grep -q mmap >actual &&
397 test_cmp empty actual &&
398 test_must_fail git grep -q qfwfq >actual &&
399 test_cmp empty actual
402 # Create 1024 file names that sort between "y" and "z" to make sure
403 # the two files are handled by different calls to an external grep.
404 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
405 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"
406 test_expect_success
'grep -C1, hunk mark between files' '
407 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
409 git grep -C1 "^[yz]" >actual &&
410 test_cmp expected actual
413 test_expect_success
'grep -C1 hunk mark between files' '
414 git grep -C1 "^[yz]" >actual &&
415 test_cmp expected actual
418 test_expect_success
'log grep setup' '
421 GIT_AUTHOR_NAME="With * Asterisk" \
422 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
423 git commit -a -m "second" &&
427 git commit -a -m "third" &&
431 GIT_AUTHOR_NAME="Night Fall" \
432 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
433 git commit -a -m "fourth"
436 test_expect_success
'log grep (1)' '
437 git log --author=author --pretty=tformat:%s >actual &&
439 echo third && echo initial
441 test_cmp expect actual
444 test_expect_success
'log grep (2)' '
445 git log --author=" * " -F --pretty=tformat:%s >actual &&
449 test_cmp expect actual
452 test_expect_success
'log grep (3)' '
453 git log --author="^A U" --pretty=tformat:%s >actual &&
455 echo third && echo initial
457 test_cmp expect actual
460 test_expect_success
'log grep (4)' '
461 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
465 test_cmp expect actual
468 test_expect_success
'log grep (5)' '
469 git log --author=Thor -F --pretty=tformat:%s >actual &&
471 echo third && echo initial
473 test_cmp expect actual
476 test_expect_success
'log grep (6)' '
477 git log --author=-0700 --pretty=tformat:%s >actual &&
479 test_cmp expect actual
482 test_expect_success
'log with multiple --grep uses union' '
483 git log --grep=i --grep=r --format=%s >actual &&
485 echo fourth && echo third && echo initial
487 test_cmp expect actual
490 test_expect_success
'log --all-match with multiple --grep uses intersection' '
491 git log --all-match --grep=i --grep=r --format=%s >actual &&
495 test_cmp expect actual
498 test_expect_success
'log --grep --author implicitly uses all-match' '
499 # grep matches initial and second but not third
500 # author matches only initial and third
501 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
505 test_cmp expect actual
508 test_expect_success
'log with multiple --author uses union' '
509 git log --author="Thor" --author="Aster" --format=%s >actual &&
511 echo third && echo second && echo initial
513 test_cmp expect actual
516 test_expect_success
'log --all-match with multiple --author still uses union' '
517 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
519 echo third && echo second && echo initial
521 test_cmp expect actual
524 test_expect_success
'log with --grep and multiple --author uses all-match' '
525 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
527 echo third && echo initial
529 test_cmp expect actual
532 test_expect_success
'log with --grep and multiple --author uses all-match' '
533 git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
535 test_cmp expect actual
538 test_expect_success
'grep with CE_VALID file' '
539 git update-index --assume-unchanged t/t &&
541 test "$(git grep test)" = "t/t:test" &&
542 git update-index --no-assume-unchanged t/t &&
547 hello.c=#include <stdio.h>
551 test_expect_success
'grep -p with userdiff' '
552 git config diff.custom.funcname "^#" &&
553 echo "hello.c diff=custom" >.gitattributes &&
554 git grep -p return >actual &&
555 test_cmp expected actual
559 hello.c=int main(int argc, const char **argv)
563 test_expect_success
'grep -p' '
564 rm -f .gitattributes &&
565 git grep -p return >actual &&
566 test_cmp expected actual
570 hello.c-#include <stdio.h>
571 hello.c=int main(int argc, const char **argv)
573 hello.c- printf("Hello world.\n");
577 test_expect_success
'grep -p -B5' '
578 git grep -p -B5 return >actual &&
579 test_cmp expected actual
583 hello.c=int main(int argc, const char **argv)
585 hello.c- printf("Hello world.\n");
587 hello.c- /* char ?? */
591 test_expect_success
'grep -W' '
592 git grep -W return >actual &&
593 test_cmp expected actual
597 hello.c= printf("Hello world.\n");
599 hello.c- /* char ?? */
602 test_expect_success
'grep -W with userdiff' '
603 test_when_finished "rm -f .gitattributes" &&
604 git config diff.custom.xfuncname "(printf.*|})$" &&
605 echo "hello.c diff=custom" >.gitattributes &&
606 git grep -W return >actual &&
607 test_cmp expected actual
610 test_expect_success
'grep from a subdirectory to search wider area (1)' '
613 cd s && git grep "x x x" ..
617 test_expect_success
'grep from a subdirectory to search wider area (2)' '
621 ( git grep xxyyzz .. >out ; echo $? >status )
623 test 1 = $(cat status)
628 hello.c:int main(int argc, const char **argv)
631 test_expect_success
'grep -Fi' '
632 git grep -Fi "CHAR *" >actual &&
633 test_cmp expected actual
636 test_expect_success
'outside of git repository' '
638 mkdir -p non/git/sub &&
639 echo hello >non/git/file1 &&
640 echo world >non/git/sub/file2 &&
644 } >non/expect.full &&
645 echo file2:world >non/expect.sub &&
647 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
648 export GIT_CEILING_DIRECTORIES &&
650 test_must_fail git grep o &&
651 git grep --no-index o >../actual.full &&
652 test_cmp ../expect.full ../actual.full
654 test_must_fail git grep o &&
655 git grep --no-index o >../../actual.sub &&
656 test_cmp ../../expect.sub ../../actual.sub
659 echo ".*o*" >non/git/.gitignore &&
661 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
662 export GIT_CEILING_DIRECTORIES &&
664 test_must_fail git grep o &&
665 git grep --no-index --exclude-standard o >../actual.full &&
666 test_cmp ../expect.full ../actual.full &&
669 echo ".gitignore:.*o*"
671 } >../expect.with.ignored &&
672 git grep --no-index --no-exclude o >../actual.full &&
673 test_cmp ../expect.with.ignored ../actual.full
677 test_expect_success
'inside git repository but with --no-index' '
679 mkdir -p is/git/sub &&
680 echo hello >is/git/file1 &&
681 echo world >is/git/sub/file2 &&
682 echo ".*o*" >is/git/.gitignore &&
686 } >is/expect.unignored &&
688 echo ".gitignore:.*o*" &&
689 cat is/expect.unignored
691 : >is/expect.empty &&
692 echo file2:world >is/expect.sub &&
696 test_must_fail git grep o >../actual.full &&
697 test_cmp ../expect.empty ../actual.full &&
699 git grep --untracked o >../actual.unignored &&
700 test_cmp ../expect.unignored ../actual.unignored &&
702 git grep --no-index o >../actual.full &&
703 test_cmp ../expect.full ../actual.full &&
705 git grep --no-index --exclude-standard o >../actual.unignored &&
706 test_cmp ../expect.unignored ../actual.unignored &&
709 test_must_fail git grep o >../../actual.sub &&
710 test_cmp ../../expect.empty ../../actual.sub &&
712 git grep --no-index o >../../actual.sub &&
713 test_cmp ../../expect.sub ../../actual.sub &&
715 git grep --untracked o >../../actual.sub &&
716 test_cmp ../../expect.sub ../../actual.sub
720 test_expect_success
'setup double-dash tests' '
721 cat >double-dash <<EOF &&
732 test_expect_success
'grep -- pattern' '
733 git grep -- "->" >actual &&
734 test_cmp expected actual
736 test_expect_success
'grep -- pattern -- pathspec' '
737 git grep -- "->" -- double-dash >actual &&
738 test_cmp expected actual
740 test_expect_success
'grep -e pattern -- path' '
741 git grep -e "->" -- double-dash >actual &&
742 test_cmp expected actual
748 test_expect_success
'grep -e -- -- path' '
749 git grep -e -- -- double-dash >actual &&
750 test_cmp expected actual
754 hello.c:int main(int argc, const char **argv)
755 hello.c: printf("Hello world.\n");
758 test_expect_success LIBPCRE
'grep --perl-regexp pattern' '
759 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
760 test_cmp expected actual
763 test_expect_success LIBPCRE
'grep -P pattern' '
764 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
765 test_cmp expected actual
768 test_expect_success
'grep pattern with grep.extendedRegexp=true' '
770 test_must_fail git -c grep.extendedregexp=true \
771 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
772 test_cmp empty actual
775 test_expect_success LIBPCRE
'grep -P pattern with grep.extendedRegexp=true' '
776 git -c grep.extendedregexp=true \
777 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
778 test_cmp expected actual
781 test_expect_success LIBPCRE
'grep -P -v pattern' '
786 git grep -P -v "abc" ab >actual &&
787 test_cmp expected actual
790 test_expect_success LIBPCRE
'grep -P -i pattern' '
791 cat >expected <<-EOF &&
792 hello.c: printf("Hello world.\n");
794 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
795 test_cmp expected actual
798 test_expect_success LIBPCRE
'grep -P -w pattern' '
800 echo "hello_world:Hello world"
801 echo "hello_world:HeLLo world"
803 git grep -P -w "He((?i)ll)o" hello_world >actual &&
804 test_cmp expected actual
807 test_expect_success
'grep -G invalidpattern properly dies ' '
808 test_must_fail git grep -G "a["
811 test_expect_success
'grep -E invalidpattern properly dies ' '
812 test_must_fail git grep -E "a["
815 test_expect_success LIBPCRE
'grep -P invalidpattern properly dies ' '
816 test_must_fail git grep -P "a["
819 test_expect_success
'grep -G -E -F pattern' '
820 echo "ab:a+b*c" >expected &&
821 git grep -G -E -F "a+b*c" ab >actual &&
822 test_cmp expected actual
825 test_expect_success
'grep -E -F -G pattern' '
826 echo "ab:a+bc" >expected &&
827 git grep -E -F -G "a+b*c" ab >actual &&
828 test_cmp expected actual
831 test_expect_success
'grep -F -G -E pattern' '
832 echo "ab:abc" >expected &&
833 git grep -F -G -E "a+b*c" ab >actual &&
834 test_cmp expected actual
837 test_expect_success
'grep -G -F -P -E pattern' '
839 test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
840 test_cmp empty actual
843 test_expect_success LIBPCRE
'grep -G -F -E -P pattern' '
844 echo "ab:a+b*c" >expected &&
845 git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
846 test_cmp expected actual
850 git config
"$1" "$2" &&
851 test_when_finished
"git config --unset $1"
855 hello.c<RED>:<RESET>int main(int argc, const char **argv)
856 hello.c<RED>-<RESET>{
858 hello.c<RED>:<RESET> /* char ?? */
859 hello.c<RED>-<RESET>}
861 hello_world<RED>:<RESET>Hello_world
862 hello_world<RED>-<RESET>HeLLo_world
865 test_expect_success
'grep --color, separator' '
866 test_config color.grep.context normal &&
867 test_config color.grep.filename normal &&
868 test_config color.grep.function normal &&
869 test_config color.grep.linenumber normal &&
870 test_config color.grep.match normal &&
871 test_config color.grep.selected normal &&
872 test_config color.grep.separator red &&
874 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
875 test_decode_color >actual &&
876 test_cmp expected actual
880 hello.c:int main(int argc, const char **argv)
881 hello.c: /* char ?? */
883 hello_world:Hello_world
886 test_expect_success
'grep --break' '
887 git grep --break -e char -e lo_w hello.c hello_world >actual &&
888 test_cmp expected actual
892 hello.c:int main(int argc, const char **argv)
895 hello.c: /* char ?? */
898 hello_world:Hello_world
899 hello_world-HeLLo_world
902 test_expect_success
'grep --break with context' '
903 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
904 test_cmp expected actual
909 int main(int argc, const char **argv)
915 test_expect_success
'grep --heading' '
916 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
917 test_cmp expected actual
921 <BOLD;GREEN>hello.c<RESET>
922 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
923 6: /* <BLACK;BYELLOW>char<RESET> ?? */
925 <BOLD;GREEN>hello_world<RESET>
926 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
929 test_expect_success
'mimic ack-grep --group' '
930 test_config color.grep.context normal &&
931 test_config color.grep.filename "bold green" &&
932 test_config color.grep.function normal &&
933 test_config color.grep.linenumber normal &&
934 test_config color.grep.match "black yellow" &&
935 test_config color.grep.selected normal &&
936 test_config color.grep.separator normal &&
938 git grep --break --heading -n --color \
939 -e char -e lo_w hello.c hello_world |
940 test_decode_color >actual &&
941 test_cmp expected actual
945 space: line with leading space1
946 space: line with leading space2
947 space: line with leading space3
950 test_expect_success LIBPCRE
'grep -E "^ "' '
951 git grep -E "^ " space >actual &&
952 test_cmp expected actual
955 test_expect_success LIBPCRE
'grep -P "^ "' '
956 git grep -P "^ " space >actual &&
957 test_cmp expected actual