3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git grep various.
15 int main(int argc, const char **argv)
17 printf("Hello world.\n");
23 test_expect_success setup '
27 echo foo_mmap bar mmap
28 echo foo mmap bar_mmap
29 echo foo_mmap bar mmap baz
57 echo "line without leading space1"
58 echo " line with leading space1"
59 echo " line with leading space2"
60 echo " line with leading space3"
61 echo "line without leading space2"
68 test_expect_success 'grep should not segfault with a bad input' '
69 test_must_fail git grep "("
75 HEAD) HC='HEAD:' L='HEAD' ;;
76 '') HC= L='in working tree' ;;
79 test_expect_success "grep -w $L" '
81 echo ${HC}file:1:foo mmap bar
82 echo ${HC}file:3:foo_mmap bar mmap
83 echo ${HC}file:4:foo mmap bar_mmap
84 echo ${HC}file:5:foo_mmap bar mmap baz
86 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
87 test_cmp expected actual
90 test_expect_success "grep -w $L" '
92 echo ${HC}file:1:foo mmap bar
93 echo ${HC}file:3:foo_mmap bar mmap
94 echo ${HC}file:4:foo mmap bar_mmap
95 echo ${HC}file:5:foo_mmap bar mmap baz
97 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
98 test_cmp expected actual
101 test_expect_success "grep -w $L" '
103 echo ${HC}file:foo mmap bar
104 echo ${HC}file:foo_mmap bar mmap
105 echo ${HC}file:foo mmap bar_mmap
106 echo ${HC}file:foo_mmap bar mmap baz
108 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
109 test_cmp expected actual
112 test_expect_success "grep -w $L (w)" '
114 test_must_fail git grep -n -w -e "^w" $H >actual &&
115 test_cmp expected actual
118 test_expect_success "grep -w $L (x)" '
120 echo ${HC}x:1:x x xx x
122 git grep -n -w -e "x xx* x" $H >actual &&
123 test_cmp expected actual
126 test_expect_success "grep -w $L (y-1)" '
130 git grep -n -w -e "^y" $H >actual &&
131 test_cmp expected actual
134 test_expect_success "grep -w $L (y-2)" '
136 if git grep -n -w -e "^y y" $H >actual
138 echo should not have matched
142 test_cmp expected actual
146 test_expect_success "grep -w $L (z)" '
148 if git grep -n -w -e "^z" $H >actual
150 echo should not have matched
154 test_cmp expected actual
158 test_expect_success "grep $L (t-1)" '
159 echo "${HC}t/t:1:test" >expected &&
160 git grep -n -e test $H >actual &&
161 test_cmp expected actual
164 test_expect_success "grep $L (t-2)" '
165 echo "${HC}t:1:test" >expected &&
168 git grep -n -e test $H
170 test_cmp expected actual
173 test_expect_success "grep $L (t-3)" '
174 echo "${HC}t/t:1:test" >expected &&
177 git grep --full-name -n -e test $H
179 test_cmp expected actual
182 test_expect_success "grep -c $L (no /dev/null)" '
183 ! git grep -c test $H | grep /dev/null
186 test_expect_success "grep --max-depth -1 $L" '
188 echo ${HC}t/a/v:1:vvv
192 git grep --max-depth -1 -n -e vvv $H >actual &&
193 test_cmp expected actual
196 test_expect_success "grep --max-depth 0 $L" '
200 git grep --max-depth 0 -n -e vvv $H >actual &&
201 test_cmp expected actual
204 test_expect_success "grep --max-depth 0 -- '*' $L" '
206 echo ${HC}t/a/v:1:vvv
210 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
211 test_cmp expected actual
214 test_expect_success "grep --max-depth 1 $L" '
219 git grep --max-depth 1 -n -e vvv $H >actual &&
220 test_cmp expected actual
223 test_expect_success "grep --max-depth 0 -- t $L" '
227 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
228 test_cmp expected actual
231 test_expect_success "grep --max-depth 0 -- . t $L" '
236 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
237 test_cmp expected actual
240 test_expect_success "grep --max-depth 0 -- t . $L" '
245 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
246 test_cmp expected actual
248 test_expect_success "grep $L with grep.extendedRegexp=false" '
249 echo "${HC}ab:a+bc" >expected &&
250 git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
251 test_cmp expected actual
254 test_expect_success "grep $L with grep.extendedRegexp=true" '
255 echo "${HC}ab:abc" >expected &&
256 git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
257 test_cmp expected actual
260 test_expect_success "grep $L with grep.patterntype=basic" '
261 echo "${HC}ab:a+bc" >expected &&
262 git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
263 test_cmp expected actual
266 test_expect_success "grep $L with grep.patterntype=extended" '
267 echo "${HC}ab:abc" >expected &&
268 git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
269 test_cmp expected actual
272 test_expect_success "grep $L with grep.patterntype=fixed" '
273 echo "${HC}ab:a+b*c" >expected &&
274 git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
275 test_cmp expected actual
278 test_expect_success LIBPCRE "grep $L with grep.patterntype=perl" '
279 echo "${HC}ab:a+b*c" >expected &&
280 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
281 test_cmp expected actual
284 test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
285 echo "${HC}ab:abc" >expected &&
287 -c grep.patternType=default \
288 -c grep.extendedRegexp=true \
289 grep "a+b*c" $H ab >actual &&
290 test_cmp expected actual
293 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
294 echo "${HC}ab:abc" >expected &&
296 -c grep.extendedRegexp=true \
297 -c grep.patternType=default \
298 grep "a+b*c" $H ab >actual &&
299 test_cmp expected actual
302 test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
303 echo "${HC}ab:abc" >expected &&
305 -c grep.patternType=extended \
306 -c grep.extendedRegexp=false \
307 grep "a+b*c" $H ab >actual &&
308 test_cmp expected actual
311 test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
312 echo "${HC}ab:a+bc" >expected &&
314 -c grep.patternType=basic \
315 -c grep.extendedRegexp=true \
316 grep "a+b*c" $H ab >actual &&
317 test_cmp expected actual
320 test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
321 echo "${HC}ab:abc" >expected &&
323 -c grep.extendedRegexp=false \
324 -c grep.patternType=extended \
325 grep "a+b*c" $H ab >actual &&
326 test_cmp expected actual
329 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
330 echo "${HC}ab:a+bc" >expected &&
332 -c grep.extendedRegexp=true \
333 -c grep.patternType=basic \
334 grep "a+b*c" $H ab >actual &&
335 test_cmp expected actual
338 test_expect_success "grep --count $L" '
339 echo ${HC}ab:3 >expected &&
340 git grep --count -e b $H -- ab >actual &&
341 test_cmp expected actual
344 test_expect_success "grep --count -h $L" '
346 git grep --count -h -e b $H -- ab >actual &&
347 test_cmp expected actual
354 test_expect_success 'grep -l -C' '
355 git grep -l -C1 foo >actual &&
356 test_cmp expected actual
362 test_expect_success 'grep -c -C' '
363 git grep -c -C1 foo >actual &&
364 test_cmp expected actual
367 test_expect_success 'grep -L -C' '
368 git ls-files >expected &&
369 git grep -L -C1 nonexistent_string >actual &&
370 test_cmp expected actual
374 file:foo mmap bar_mmap
377 test_expect_success 'grep -e A --and -e B' '
378 git grep -e "foo mmap" --and -e bar_mmap >actual &&
379 test_cmp expected actual
383 file:foo_mmap bar mmap
384 file:foo_mmap bar mmap baz
388 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
389 git grep \( -e foo_ --or -e baz \) \
390 --and -e " mmap" >actual &&
391 test_cmp expected actual
398 test_expect_success 'grep -e A --and --not -e B' '
399 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
400 test_cmp expected actual
403 test_expect_success 'grep should ignore GREP_OPTIONS' '
404 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
405 test_cmp expected actual
408 test_expect_success 'grep -f, non-existent file' '
409 test_must_fail git grep -f patterns
415 file:foo_mmap bar mmap
416 file:foo mmap bar_mmap
417 file:foo_mmap bar mmap baz
424 test_expect_success 'grep -f, one pattern' '
425 git grep -f pattern >actual &&
426 test_cmp expected actual
432 file:foo_mmap bar mmap
433 file:foo mmap bar_mmap
434 file:foo_mmap bar mmap baz
445 test_expect_success 'grep -f, multiple patterns' '
446 git grep -f patterns >actual &&
447 test_cmp expected actual
450 test_expect_success 'grep, multiple patterns' '
451 git grep "$(cat patterns)" >actual &&
452 test_cmp expected actual
458 file:foo_mmap bar mmap
459 file:foo mmap bar_mmap
460 file:foo_mmap bar mmap baz
474 test_expect_success 'grep -f, ignore empty lines' '
475 git grep -f patterns >actual &&
476 test_cmp expected actual
479 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
480 git grep -f - <patterns >actual &&
481 test_cmp expected actual
490 test_expect_success 'grep -q, silently report matches' '
492 git grep -q mmap >actual &&
493 test_cmp empty actual &&
494 test_must_fail git grep -q qfwfq >actual &&
495 test_cmp empty actual
498 test_expect_success 'grep -C1 hunk mark between files' '
499 git grep -C1 "^[yz]" >actual &&
500 test_cmp expected actual
503 test_expect_success 'log grep setup' '
506 GIT_AUTHOR_NAME="With * Asterisk" \
507 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
508 git commit -a -m "second" &&
512 git commit -a -m "third" &&
516 GIT_AUTHOR_NAME="Night Fall" \
517 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
518 git commit -a -m "fourth"
521 test_expect_success 'log grep (1)' '
522 git log --author=author --pretty=tformat:%s >actual &&
524 echo third && echo initial
526 test_cmp expect actual
529 test_expect_success 'log grep (2)' '
530 git log --author=" * " -F --pretty=tformat:%s >actual &&
534 test_cmp expect actual
537 test_expect_success 'log grep (3)' '
538 git log --author="^A U" --pretty=tformat:%s >actual &&
540 echo third && echo initial
542 test_cmp expect actual
545 test_expect_success 'log grep (4)' '
546 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
550 test_cmp expect actual
553 test_expect_success 'log grep (5)' '
554 git log --author=Thor -F --pretty=tformat:%s >actual &&
556 echo third && echo initial
558 test_cmp expect actual
561 test_expect_success 'log grep (6)' '
562 git log --author=-0700 --pretty=tformat:%s >actual &&
564 test_cmp expect actual
567 test_expect_success 'log grep (7)' '
568 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
569 echo third >expect &&
570 test_cmp expect actual
573 test_expect_success 'log grep (8)' '
574 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
576 echo third && echo second
578 test_cmp expect actual
581 test_expect_success 'log grep (9)' '
582 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
583 echo third >expect &&
584 test_cmp expect actual
587 test_expect_success 'log grep (9)' '
588 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
590 test_cmp expect actual
593 test_expect_success 'log --grep-reflog can only be used under -g' '
594 test_must_fail git log --grep-reflog="commit: third"
597 test_expect_success 'log with multiple --grep uses union' '
598 git log --grep=i --grep=r --format=%s >actual &&
600 echo fourth && echo third && echo initial
602 test_cmp expect actual
605 test_expect_success 'log --all-match with multiple --grep uses intersection' '
606 git log --all-match --grep=i --grep=r --format=%s >actual &&
610 test_cmp expect actual
613 test_expect_success 'log with multiple --author uses union' '
614 git log --author="Thor" --author="Aster" --format=%s >actual &&
616 echo third && echo second && echo initial
618 test_cmp expect actual
621 test_expect_success 'log --all-match with multiple --author still uses union' '
622 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
624 echo third && echo second && echo initial
626 test_cmp expect actual
629 test_expect_success 'log --grep --author uses intersection' '
630 # grep matches only third and fourth
631 # author matches only initial and third
632 git log --author="A U Thor" --grep=r --format=%s >actual &&
636 test_cmp expect actual
639 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
640 # grep matches initial and second but not third
641 # author matches only initial and third
642 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
646 test_cmp expect actual
649 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
650 # grep matches only initial and third
651 # author matches all but second
652 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
654 echo third && echo initial
656 test_cmp expect actual
659 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
660 # grep matches only initial and third
661 # author matches all but second
662 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
664 echo third && echo initial
666 test_cmp expect actual
669 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
670 # grep matches only third
671 # author matches only initial and third
672 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
676 test_cmp expect actual
679 test_expect_success 'log --author does not search in timestamp' '
681 git log --author="$GIT_AUTHOR_DATE" >actual &&
682 test_cmp expect actual
685 test_expect_success 'log --committer does not search in timestamp' '
687 git log --committer="$GIT_COMMITTER_DATE" >actual &&
688 test_cmp expect actual
691 test_expect_success 'grep with CE_VALID file' '
692 git update-index --assume-unchanged t/t &&
694 test "$(git grep test)" = "t/t:test" &&
695 git update-index --no-assume-unchanged t/t &&
700 hello.c=#include <stdio.h>
704 test_expect_success 'grep -p with userdiff' '
705 git config diff.custom.funcname "^#" &&
706 echo "hello.c diff=custom" >.gitattributes &&
707 git grep -p return >actual &&
708 test_cmp expected actual
712 hello.c=int main(int argc, const char **argv)
716 test_expect_success 'grep -p' '
717 rm -f .gitattributes &&
718 git grep -p return >actual &&
719 test_cmp expected actual
723 hello.c-#include <stdio.h>
725 hello.c=int main(int argc, const char **argv)
727 hello.c- printf("Hello world.\n");
731 test_expect_success 'grep -p -B5' '
732 git grep -p -B5 return >actual &&
733 test_cmp expected actual
737 hello.c=int main(int argc, const char **argv)
739 hello.c- printf("Hello world.\n");
741 hello.c- /* char ?? */
745 test_expect_success 'grep -W' '
746 git grep -W return >actual &&
747 test_cmp expected actual
751 hello.c-#include <assert.h>
752 hello.c:#include <stdio.h>
755 test_expect_success 'grep -W shows no trailing empty lines' '
756 git grep -W stdio >actual &&
757 test_cmp expected actual
761 hello.c= printf("Hello world.\n");
763 hello.c- /* char ?? */
766 test_expect_success 'grep -W with userdiff' '
767 test_when_finished "rm -f .gitattributes" &&
768 git config diff.custom.xfuncname "(printf.*|})$" &&
769 echo "hello.c diff=custom" >.gitattributes &&
770 git grep -W return >actual &&
771 test_cmp expected actual
774 test_expect_success 'grep from a subdirectory to search wider area (1)' '
777 cd s && git grep "x x x" ..
781 test_expect_success 'grep from a subdirectory to search wider area (2)' '
785 ( git grep xxyyzz .. >out ; echo $? >status )
787 test 1 = $(cat status)
792 hello.c:int main(int argc, const char **argv)
795 test_expect_success 'grep -Fi' '
796 git grep -Fi "CHAR *" >actual &&
797 test_cmp expected actual
800 test_expect_success 'outside of git repository' '
802 mkdir -p non/git/sub &&
803 echo hello >non/git/file1 &&
804 echo world >non/git/sub/file2 &&
808 } >non/expect.full &&
809 echo file2:world >non/expect.sub &&
811 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
812 export GIT_CEILING_DIRECTORIES &&
814 test_must_fail git grep o &&
815 git grep --no-index o >../actual.full &&
816 test_cmp ../expect.full ../actual.full &&
818 test_must_fail git grep o &&
819 git grep --no-index o >../../actual.sub &&
820 test_cmp ../../expect.sub ../../actual.sub
823 echo ".*o*" >non/git/.gitignore &&
825 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
826 export GIT_CEILING_DIRECTORIES &&
828 test_must_fail git grep o &&
829 git grep --no-index --exclude-standard o >../actual.full &&
830 test_cmp ../expect.full ../actual.full &&
833 echo ".gitignore:.*o*" &&
835 } >../expect.with.ignored &&
836 git grep --no-index --no-exclude o >../actual.full &&
837 test_cmp ../expect.with.ignored ../actual.full
841 test_expect_success 'outside of git repository with fallbackToNoIndex' '
843 mkdir -p non/git/sub &&
844 echo hello >non/git/file1 &&
845 echo world >non/git/sub/file2 &&
846 cat <<-\EOF >non/expect.full &&
850 echo file2:world >non/expect.sub &&
852 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
853 export GIT_CEILING_DIRECTORIES &&
855 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
856 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
857 test_cmp ../expect.full ../actual.full &&
859 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
860 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
861 test_cmp ../../expect.sub ../../actual.sub
864 echo ".*o*" >non/git/.gitignore &&
866 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
867 export GIT_CEILING_DIRECTORIES &&
869 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
870 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
871 test_cmp ../expect.full ../actual.full &&
874 echo ".gitignore:.*o*" &&
876 } >../expect.with.ignored &&
877 git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
878 test_cmp ../expect.with.ignored ../actual.full
882 test_expect_success 'inside git repository but with --no-index' '
884 mkdir -p is/git/sub &&
885 echo hello >is/git/file1 &&
886 echo world >is/git/sub/file2 &&
887 echo ".*o*" >is/git/.gitignore &&
891 } >is/expect.unignored &&
893 echo ".gitignore:.*o*" &&
894 cat is/expect.unignored
896 : >is/expect.empty &&
897 echo file2:world >is/expect.sub &&
901 test_must_fail git grep o >../actual.full &&
902 test_cmp ../expect.empty ../actual.full &&
904 git grep --untracked o >../actual.unignored &&
905 test_cmp ../expect.unignored ../actual.unignored &&
907 git grep --no-index o >../actual.full &&
908 test_cmp ../expect.full ../actual.full &&
910 git grep --no-index --exclude-standard o >../actual.unignored &&
911 test_cmp ../expect.unignored ../actual.unignored &&
914 test_must_fail git grep o >../../actual.sub &&
915 test_cmp ../../expect.empty ../../actual.sub &&
917 git grep --no-index o >../../actual.sub &&
918 test_cmp ../../expect.sub ../../actual.sub &&
920 git grep --untracked o >../../actual.sub &&
921 test_cmp ../../expect.sub ../../actual.sub
925 test_expect_success 'grep --no-index descends into repos, but not .git' '
929 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
930 export GIT_CEILING_DIRECTORIES &&
940 echo magic >.git/file
943 cat >expect <<-\EOF &&
947 git grep -l --no-index magic >actual &&
948 test_cmp expect actual
952 test_expect_success 'setup double-dash tests' '
953 cat >double-dash <<EOF &&
964 test_expect_success 'grep -- pattern' '
965 git grep -- "->" >actual &&
966 test_cmp expected actual
968 test_expect_success 'grep -- pattern -- pathspec' '
969 git grep -- "->" -- double-dash >actual &&
970 test_cmp expected actual
972 test_expect_success 'grep -e pattern -- path' '
973 git grep -e "->" -- double-dash >actual &&
974 test_cmp expected actual
980 test_expect_success 'grep -e -- -- path' '
981 git grep -e -- -- double-dash >actual &&
982 test_cmp expected actual
985 test_expect_success 'dashdash disambiguates rev as rev' '
986 test_when_finished "rm -f master" &&
987 echo content >master &&
988 echo master:hello.c >expect &&
989 git grep -l o master -- hello.c >actual &&
990 test_cmp expect actual
993 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
994 test_when_finished "git rm -f master" &&
995 echo content >master &&
997 echo master:content >expect &&
998 git grep o -- master >actual &&
999 test_cmp expect actual
1002 test_expect_success 'report bogus arg without dashdash' '
1003 test_must_fail git grep o does-not-exist
1006 test_expect_success 'report bogus rev with dashdash' '
1007 test_must_fail git grep o hello.c --
1010 test_expect_success 'allow non-existent path with dashdash' '
1011 # We need a real match so grep exits with success.
1012 tree=$(git ls-tree HEAD |
1013 sed s/hello.c/not-in-working-tree/ |
1015 git grep o "$tree" -- not-in-working-tree
1018 test_expect_success 'grep --no-index pattern -- path' '
1022 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1023 export GIT_CEILING_DIRECTORIES &&
1025 echo hello >hello &&
1026 echo goodbye >goodbye &&
1027 echo hello:hello >expect &&
1028 git grep --no-index o -- hello >actual &&
1029 test_cmp expect actual
1033 test_expect_success 'grep --no-index complains of revs' '
1034 test_must_fail git grep --no-index o master -- 2>err &&
1035 test_i18ngrep "no-index cannot be used with revs" err
1038 test_expect_success 'grep --no-index prefers paths to revs' '
1039 test_when_finished "rm -f master" &&
1040 echo content >master &&
1041 echo master:content >expect &&
1042 git grep --no-index o master >actual &&
1043 test_cmp expect actual
1047 hello.c:int main(int argc, const char **argv)
1048 hello.c: printf("Hello world.\n");
1051 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
1052 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1053 test_cmp expected actual
1056 test_expect_success LIBPCRE 'grep -P pattern' '
1057 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1058 test_cmp expected actual
1061 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1063 test_must_fail git -c grep.extendedregexp=true \
1064 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1065 test_cmp empty actual
1068 test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
1069 git -c grep.extendedregexp=true \
1070 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1071 test_cmp expected actual
1074 test_expect_success LIBPCRE 'grep -P -v pattern' '
1079 git grep -P -v "abc" ab >actual &&
1080 test_cmp expected actual
1083 test_expect_success LIBPCRE 'grep -P -i pattern' '
1084 cat >expected <<-EOF &&
1085 hello.c: printf("Hello world.\n");
1087 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1088 test_cmp expected actual
1091 test_expect_success LIBPCRE 'grep -P -w pattern' '
1093 echo "hello_world:Hello world"
1094 echo "hello_world:HeLLo world"
1096 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1097 test_cmp expected actual
1100 test_expect_success 'grep -G invalidpattern properly dies ' '
1101 test_must_fail git grep -G "a["
1104 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1105 test_must_fail git -c grep.patterntype=basic grep "a["
1108 test_expect_success 'grep -E invalidpattern properly dies ' '
1109 test_must_fail git grep -E "a["
1112 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1113 test_must_fail git -c grep.patterntype=extended grep "a["
1116 test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
1117 test_must_fail git grep -P "a["
1120 test_expect_success LIBPCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1121 test_must_fail git -c grep.patterntype=perl grep "a["
1124 test_expect_success 'grep -G -E -F pattern' '
1125 echo "ab:a+b*c" >expected &&
1126 git grep -G -E -F "a+b*c" ab >actual &&
1127 test_cmp expected actual
1130 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1131 echo "ab:a+b*c" >expected &&
1133 -c grep.patterntype=basic \
1134 -c grep.patterntype=extended \
1135 -c grep.patterntype=fixed \
1136 grep "a+b*c" ab >actual &&
1137 test_cmp expected actual
1140 test_expect_success 'grep -E -F -G pattern' '
1141 echo "ab:a+bc" >expected &&
1142 git grep -E -F -G "a+b*c" ab >actual &&
1143 test_cmp expected actual
1146 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1147 echo "ab:a+bc" >expected &&
1149 -c grep.patterntype=extended \
1150 -c grep.patterntype=fixed \
1151 -c grep.patterntype=basic \
1152 grep "a+b*c" ab >actual &&
1153 test_cmp expected actual
1156 test_expect_success 'grep -F -G -E pattern' '
1157 echo "ab:abc" >expected &&
1158 git grep -F -G -E "a+b*c" ab >actual &&
1159 test_cmp expected actual
1162 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1163 echo "ab:abc" >expected &&
1165 -c grep.patterntype=fixed \
1166 -c grep.patterntype=basic \
1167 -c grep.patterntype=extended \
1168 grep "a+b*c" ab >actual &&
1169 test_cmp expected actual
1172 test_expect_success 'grep -G -F -P -E pattern' '
1173 echo "d0:d" >expected &&
1174 git grep -G -F -P -E "[\d]" d0 >actual &&
1175 test_cmp expected actual
1178 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1179 echo "d0:d" >expected &&
1181 -c grep.patterntype=fixed \
1182 -c grep.patterntype=basic \
1183 -c grep.patterntype=perl \
1184 -c grep.patterntype=extended \
1185 grep "[\d]" d0 >actual &&
1186 test_cmp expected actual
1189 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1190 echo "d0:0" >expected &&
1191 git grep -G -F -E -P "[\d]" d0 >actual &&
1192 test_cmp expected actual
1195 test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1196 echo "d0:0" >expected &&
1198 -c grep.patterntype=fixed \
1199 -c grep.patterntype=basic \
1200 -c grep.patterntype=extended \
1201 -c grep.patterntype=perl \
1202 grep "[\d]" d0 >actual &&
1203 test_cmp expected actual
1206 test_expect_success LIBPCRE 'grep -P pattern with grep.patternType=fixed' '
1207 echo "ab:a+b*c" >expected &&
1209 -c grep.patterntype=fixed \
1210 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1211 test_cmp expected actual
1214 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1215 echo "ab:a+b*c" >expected &&
1217 -c grep.patterntype=basic \
1218 grep -F "*c" ab >actual &&
1219 test_cmp expected actual
1222 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1228 -c grep.patterntype=fixed \
1229 grep -G "a+b" ab >actual &&
1230 test_cmp expected actual
1233 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1240 -c grep.patterntype=fixed \
1241 grep -E "a+" ab >actual &&
1242 test_cmp expected actual
1246 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1247 hello.c<RED>-<RESET>{
1249 hello.c<RED>:<RESET> /* char ?? */
1250 hello.c<RED>-<RESET>}
1252 hello_world<RED>:<RESET>Hello_world
1253 hello_world<RED>-<RESET>HeLLo_world
1256 test_expect_success 'grep --color, separator' '
1257 test_config color.grep.context normal &&
1258 test_config color.grep.filename normal &&
1259 test_config color.grep.function normal &&
1260 test_config color.grep.linenumber normal &&
1261 test_config color.grep.match normal &&
1262 test_config color.grep.selected normal &&
1263 test_config color.grep.separator red &&
1265 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1266 test_decode_color >actual &&
1267 test_cmp expected actual
1271 hello.c:int main(int argc, const char **argv)
1272 hello.c: /* char ?? */
1274 hello_world:Hello_world
1277 test_expect_success 'grep --break' '
1278 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1279 test_cmp expected actual
1283 hello.c:int main(int argc, const char **argv)
1286 hello.c: /* char ?? */
1289 hello_world:Hello_world
1290 hello_world-HeLLo_world
1293 test_expect_success 'grep --break with context' '
1294 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1295 test_cmp expected actual
1300 int main(int argc, const char **argv)
1306 test_expect_success 'grep --heading' '
1307 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1308 test_cmp expected actual
1312 <BOLD;GREEN>hello.c<RESET>
1313 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1314 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1316 <BOLD;GREEN>hello_world<RESET>
1317 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1320 test_expect_success 'mimic ack-grep --group' '
1321 test_config color.grep.context normal &&
1322 test_config color.grep.filename "bold green" &&
1323 test_config color.grep.function normal &&
1324 test_config color.grep.linenumber normal &&
1325 test_config color.grep.match "black yellow" &&
1326 test_config color.grep.selected normal &&
1327 test_config color.grep.separator normal &&
1329 git grep --break --heading -n --color \
1330 -e char -e lo_w hello.c hello_world |
1331 test_decode_color >actual &&
1332 test_cmp expected actual
1336 space: line with leading space1
1337 space: line with leading space2
1338 space: line with leading space3
1341 test_expect_success LIBPCRE 'grep -E "^ "' '
1342 git grep -E "^ " space >actual &&
1343 test_cmp expected actual
1346 test_expect_success LIBPCRE 'grep -P "^ "' '
1347 git grep -P "^ " space >actual &&
1348 test_cmp expected actual
1352 space-line without leading space1
1353 space: line <RED>with <RESET>leading space1
1354 space: line <RED>with <RESET>leading <RED>space2<RESET>
1355 space: line <RED>with <RESET>leading space3
1356 space:line without leading <RED>space2<RESET>
1359 test_expect_success 'grep --color -e A -e B with context' '
1360 test_config color.grep.context normal &&
1361 test_config color.grep.filename normal &&
1362 test_config color.grep.function normal &&
1363 test_config color.grep.linenumber normal &&
1364 test_config color.grep.matchContext normal &&
1365 test_config color.grep.matchSelected red &&
1366 test_config color.grep.selected normal &&
1367 test_config color.grep.separator normal &&
1369 git grep --color=always -C2 -e "with " -e space2 space |
1370 test_decode_color >actual &&
1371 test_cmp expected actual
1375 space-line without leading space1
1376 space- line with leading space1
1377 space: line <RED>with <RESET>leading <RED>space2<RESET>
1378 space- line with leading space3
1379 space-line without leading space2
1382 test_expect_success 'grep --color -e A --and -e B with context' '
1383 test_config color.grep.context normal &&
1384 test_config color.grep.filename normal &&
1385 test_config color.grep.function normal &&
1386 test_config color.grep.linenumber normal &&
1387 test_config color.grep.matchContext normal &&
1388 test_config color.grep.matchSelected red &&
1389 test_config color.grep.selected normal &&
1390 test_config color.grep.separator normal &&
1392 git grep --color=always -C2 -e "with " --and -e space2 space |
1393 test_decode_color >actual &&
1394 test_cmp expected actual
1398 space-line without leading space1
1399 space: line <RED>with <RESET>leading space1
1400 space- line with leading space2
1401 space: line <RED>with <RESET>leading space3
1402 space-line without leading space2
1405 test_expect_success 'grep --color -e A --and --not -e B with context' '
1406 test_config color.grep.context normal &&
1407 test_config color.grep.filename normal &&
1408 test_config color.grep.function normal &&
1409 test_config color.grep.linenumber normal &&
1410 test_config color.grep.matchContext normal &&
1411 test_config color.grep.matchSelected red &&
1412 test_config color.grep.selected normal &&
1413 test_config color.grep.separator normal &&
1415 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1416 test_decode_color >actual &&
1417 test_cmp expected actual
1422 hello.c=int main(int argc, const char **argv)
1424 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1426 hello.c- /* char ?? */
1430 test_expect_success 'grep --color -e A --and -e B -p with context' '
1431 test_config color.grep.context normal &&
1432 test_config color.grep.filename normal &&
1433 test_config color.grep.function normal &&
1434 test_config color.grep.linenumber normal &&
1435 test_config color.grep.matchContext normal &&
1436 test_config color.grep.matchSelected red &&
1437 test_config color.grep.selected normal &&
1438 test_config color.grep.separator normal &&
1440 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1441 test_decode_color >actual &&
1442 test_cmp expected actual
1445 test_expect_success 'grep can find things only in the work tree' '
1446 : >work-tree-only &&
1447 git add work-tree-only &&
1448 test_when_finished "git rm -f work-tree-only" &&
1449 echo "find in work tree" >work-tree-only &&
1450 git grep --quiet "find in work tree" &&
1451 test_must_fail git grep --quiet --cached "find in work tree" &&
1452 test_must_fail git grep --quiet "find in work tree" HEAD
1455 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1456 echo "intend to add this" >intend-to-add &&
1457 git add -N intend-to-add &&
1458 test_when_finished "git rm -f intend-to-add" &&
1459 git grep --quiet "intend to add this" &&
1460 test_must_fail git grep --quiet --cached "intend to add this" &&
1461 test_must_fail git grep --quiet "intend to add this" HEAD
1464 test_expect_success 'grep does not search work tree with assume unchanged' '
1465 echo "intend to add this" >intend-to-add &&
1466 git add -N intend-to-add &&
1467 git update-index --assume-unchanged intend-to-add &&
1468 test_when_finished "git rm -f intend-to-add" &&
1469 test_must_fail git grep --quiet "intend to add this" &&
1470 test_must_fail git grep --quiet --cached "intend to add this" &&
1471 test_must_fail git grep --quiet "intend to add this" HEAD
1474 test_expect_success 'grep can find things only in the index' '
1475 echo "only in the index" >cache-this &&
1476 git add cache-this &&
1478 test_when_finished "git rm --cached cache-this" &&
1479 test_must_fail git grep --quiet "only in the index" &&
1480 git grep --quiet --cached "only in the index" &&
1481 test_must_fail git grep --quiet "only in the index" HEAD
1484 test_expect_success 'grep does not report i-t-a with -L --cached' '
1485 echo "intend to add this" >intend-to-add &&
1486 git add -N intend-to-add &&
1487 test_when_finished "git rm -f intend-to-add" &&
1488 git ls-files | grep -v "^intend-to-add\$" >expected &&
1489 git grep -L --cached "nonexistent_string" >actual &&
1490 test_cmp expected actual
1493 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1494 echo "intend to add this" >intend-to-add-assume-unchanged &&
1495 git add -N intend-to-add-assume-unchanged &&
1496 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1497 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1498 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1499 git grep -L "nonexistent_string" >actual &&
1500 test_cmp expected actual