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"
63 cat >hello.ps1 <<-\EOF &&
80 test_expect_success 'grep should not segfault with a bad input' '
81 test_must_fail git grep "("
87 HEAD) HC='HEAD:' L='HEAD' ;;
88 '') HC= L='in working tree' ;;
91 test_expect_success "grep -w $L" '
93 echo ${HC}file:1:foo mmap bar
94 echo ${HC}file:3:foo_mmap bar mmap
95 echo ${HC}file:4:foo mmap bar_mmap
96 echo ${HC}file:5:foo_mmap bar mmap baz
98 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
99 test_cmp expected actual
102 test_expect_success "grep -w $L" '
104 echo ${HC}file:1:foo mmap bar
105 echo ${HC}file:3:foo_mmap bar mmap
106 echo ${HC}file:4:foo mmap bar_mmap
107 echo ${HC}file:5:foo_mmap bar mmap baz
109 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
110 test_cmp expected actual
113 test_expect_success "grep -w $L" '
115 echo ${HC}file:foo mmap bar
116 echo ${HC}file:foo_mmap bar mmap
117 echo ${HC}file:foo mmap bar_mmap
118 echo ${HC}file:foo_mmap bar mmap baz
120 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
121 test_cmp expected actual
124 test_expect_success "grep -w $L (w)" '
126 test_must_fail git grep -n -w -e "^w" $H >actual &&
127 test_cmp expected actual
130 test_expect_success "grep -w $L (x)" '
132 echo ${HC}x:1:x x xx x
134 git grep -n -w -e "x xx* x" $H >actual &&
135 test_cmp expected actual
138 test_expect_success "grep -w $L (y-1)" '
142 git grep -n -w -e "^y" $H >actual &&
143 test_cmp expected actual
146 test_expect_success "grep -w $L (y-2)" '
148 if git grep -n -w -e "^y y" $H >actual
150 echo should not have matched
154 test_cmp expected actual
158 test_expect_success "grep -w $L (z)" '
160 if git grep -n -w -e "^z" $H >actual
162 echo should not have matched
166 test_cmp expected actual
170 test_expect_success "grep $L (t-1)" '
171 echo "${HC}t/t:1:test" >expected &&
172 git grep -n -e test $H >actual &&
173 test_cmp expected actual
176 test_expect_success "grep $L (t-2)" '
177 echo "${HC}t:1:test" >expected &&
180 git grep -n -e test $H
182 test_cmp expected actual
185 test_expect_success "grep $L (t-3)" '
186 echo "${HC}t/t:1:test" >expected &&
189 git grep --full-name -n -e test $H
191 test_cmp expected actual
194 test_expect_success "grep -c $L (no /dev/null)" '
195 ! git grep -c test $H | grep /dev/null
198 test_expect_success "grep --max-depth -1 $L" '
200 echo ${HC}t/a/v:1:vvv
204 git grep --max-depth -1 -n -e vvv $H >actual &&
205 test_cmp expected actual
208 test_expect_success "grep --max-depth 0 $L" '
212 git grep --max-depth 0 -n -e vvv $H >actual &&
213 test_cmp expected actual
216 test_expect_success "grep --max-depth 0 -- '*' $L" '
218 echo ${HC}t/a/v:1:vvv
222 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
223 test_cmp expected actual
226 test_expect_success "grep --max-depth 1 $L" '
231 git grep --max-depth 1 -n -e vvv $H >actual &&
232 test_cmp expected actual
235 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
243 test_expect_success "grep --max-depth 0 -- . t $L" '
248 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
249 test_cmp expected actual
252 test_expect_success "grep --max-depth 0 -- t . $L" '
257 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
258 test_cmp expected actual
260 test_expect_success "grep $L with grep.extendedRegexp=false" '
261 echo "${HC}ab:a+bc" >expected &&
262 git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
263 test_cmp expected actual
266 test_expect_success "grep $L with grep.extendedRegexp=true" '
267 echo "${HC}ab:abc" >expected &&
268 git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
269 test_cmp expected actual
272 test_expect_success "grep $L with grep.patterntype=basic" '
273 echo "${HC}ab:a+bc" >expected &&
274 git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
275 test_cmp expected actual
278 test_expect_success "grep $L with grep.patterntype=extended" '
279 echo "${HC}ab:abc" >expected &&
280 git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
281 test_cmp expected actual
284 test_expect_success "grep $L with grep.patterntype=fixed" '
285 echo "${HC}ab:a+b*c" >expected &&
286 git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
287 test_cmp expected actual
290 test_expect_success PCRE "grep $L with grep.patterntype=perl" '
291 echo "${HC}ab:a+b*c" >expected &&
292 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
293 test_cmp expected actual
296 test_expect_success !PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
297 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
300 test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
301 echo "${HC}ab:abc" >expected &&
303 -c grep.patternType=default \
304 -c grep.extendedRegexp=true \
305 grep "a+b*c" $H ab >actual &&
306 test_cmp expected actual
309 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
310 echo "${HC}ab:abc" >expected &&
312 -c grep.extendedRegexp=true \
313 -c grep.patternType=default \
314 grep "a+b*c" $H ab >actual &&
315 test_cmp expected actual
318 test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
319 echo "${HC}ab:abc" >expected &&
321 -c grep.patternType=extended \
322 -c grep.extendedRegexp=false \
323 grep "a+b*c" $H ab >actual &&
324 test_cmp expected actual
327 test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
328 echo "${HC}ab:a+bc" >expected &&
330 -c grep.patternType=basic \
331 -c grep.extendedRegexp=true \
332 grep "a+b*c" $H ab >actual &&
333 test_cmp expected actual
336 test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
337 echo "${HC}ab:abc" >expected &&
339 -c grep.extendedRegexp=false \
340 -c grep.patternType=extended \
341 grep "a+b*c" $H ab >actual &&
342 test_cmp expected actual
345 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
346 echo "${HC}ab:a+bc" >expected &&
348 -c grep.extendedRegexp=true \
349 -c grep.patternType=basic \
350 grep "a+b*c" $H ab >actual &&
351 test_cmp expected actual
354 test_expect_success "grep --count $L" '
355 echo ${HC}ab:3 >expected &&
356 git grep --count -e b $H -- ab >actual &&
357 test_cmp expected actual
360 test_expect_success "grep --count -h $L" '
362 git grep --count -h -e b $H -- ab >actual &&
363 test_cmp expected actual
370 test_expect_success 'grep -l -C' '
371 git grep -l -C1 foo >actual &&
372 test_cmp expected actual
378 test_expect_success 'grep -c -C' '
379 git grep -c -C1 foo >actual &&
380 test_cmp expected actual
383 test_expect_success 'grep -L -C' '
384 git ls-files >expected &&
385 git grep -L -C1 nonexistent_string >actual &&
386 test_cmp expected actual
389 test_expect_success 'grep --files-without-match --quiet' '
390 git grep --files-without-match --quiet nonexistent_string >actual &&
391 test_cmp /dev/null actual
395 file:foo mmap bar_mmap
398 test_expect_success 'grep -e A --and -e B' '
399 git grep -e "foo mmap" --and -e bar_mmap >actual &&
400 test_cmp expected actual
404 file:foo_mmap bar mmap
405 file:foo_mmap bar mmap baz
409 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
410 git grep \( -e foo_ --or -e baz \) \
411 --and -e " mmap" >actual &&
412 test_cmp expected actual
419 test_expect_success 'grep -e A --and --not -e B' '
420 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
421 test_cmp expected actual
424 test_expect_success 'grep should ignore GREP_OPTIONS' '
425 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
426 test_cmp expected actual
429 test_expect_success 'grep -f, non-existent file' '
430 test_must_fail git grep -f patterns
436 file:foo_mmap bar mmap
437 file:foo mmap bar_mmap
438 file:foo_mmap bar mmap baz
445 test_expect_success 'grep -f, one pattern' '
446 git grep -f pattern >actual &&
447 test_cmp expected actual
453 file:foo_mmap bar mmap
454 file:foo mmap bar_mmap
455 file:foo_mmap bar mmap baz
466 test_expect_success 'grep -f, multiple patterns' '
467 git grep -f patterns >actual &&
468 test_cmp expected actual
471 test_expect_success 'grep, multiple patterns' '
472 git grep "$(cat patterns)" >actual &&
473 test_cmp expected actual
479 file:foo_mmap bar mmap
480 file:foo mmap bar_mmap
481 file:foo_mmap bar mmap baz
495 test_expect_success 'grep -f, ignore empty lines' '
496 git grep -f patterns >actual &&
497 test_cmp expected actual
500 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
501 git grep -f - <patterns >actual &&
502 test_cmp expected actual
511 test_expect_success 'grep -q, silently report matches' '
513 git grep -q mmap >actual &&
514 test_cmp empty actual &&
515 test_must_fail git grep -q qfwfq >actual &&
516 test_cmp empty actual
519 test_expect_success 'grep -C1 hunk mark between files' '
520 git grep -C1 "^[yz]" >actual &&
521 test_cmp expected actual
524 test_expect_success 'log grep setup' '
527 GIT_AUTHOR_NAME="With * Asterisk" \
528 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
529 git commit -a -m "second" &&
533 git commit -a -m "third" &&
537 GIT_AUTHOR_NAME="Night Fall" \
538 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
539 git commit -a -m "fourth"
542 test_expect_success 'log grep (1)' '
543 git log --author=author --pretty=tformat:%s >actual &&
545 echo third && echo initial
547 test_cmp expect actual
550 test_expect_success 'log grep (2)' '
551 git log --author=" * " -F --pretty=tformat:%s >actual &&
555 test_cmp expect actual
558 test_expect_success 'log grep (3)' '
559 git log --author="^A U" --pretty=tformat:%s >actual &&
561 echo third && echo initial
563 test_cmp expect actual
566 test_expect_success 'log grep (4)' '
567 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
571 test_cmp expect actual
574 test_expect_success 'log grep (5)' '
575 git log --author=Thor -F --pretty=tformat:%s >actual &&
577 echo third && echo initial
579 test_cmp expect actual
582 test_expect_success 'log grep (6)' '
583 git log --author=-0700 --pretty=tformat:%s >actual &&
585 test_cmp expect actual
588 test_expect_success 'log grep (7)' '
589 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
590 echo third >expect &&
591 test_cmp expect actual
594 test_expect_success 'log grep (8)' '
595 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
597 echo third && echo second
599 test_cmp expect actual
602 test_expect_success 'log grep (9)' '
603 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
604 echo third >expect &&
605 test_cmp expect actual
608 test_expect_success 'log grep (9)' '
609 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
611 test_cmp expect actual
614 test_expect_success 'log --grep-reflog can only be used under -g' '
615 test_must_fail git log --grep-reflog="commit: third"
618 test_expect_success 'log with multiple --grep uses union' '
619 git log --grep=i --grep=r --format=%s >actual &&
621 echo fourth && echo third && echo initial
623 test_cmp expect actual
626 test_expect_success 'log --all-match with multiple --grep uses intersection' '
627 git log --all-match --grep=i --grep=r --format=%s >actual &&
631 test_cmp expect actual
634 test_expect_success 'log with multiple --author uses union' '
635 git log --author="Thor" --author="Aster" --format=%s >actual &&
637 echo third && echo second && echo initial
639 test_cmp expect actual
642 test_expect_success 'log --all-match with multiple --author still uses union' '
643 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
645 echo third && echo second && echo initial
647 test_cmp expect actual
650 test_expect_success 'log --grep --author uses intersection' '
651 # grep matches only third and fourth
652 # author matches only initial and third
653 git log --author="A U Thor" --grep=r --format=%s >actual &&
657 test_cmp expect actual
660 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
661 # grep matches initial and second but not third
662 # author matches only initial and third
663 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
667 test_cmp expect actual
670 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
671 # grep matches only initial and third
672 # author matches all but second
673 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
675 echo third && echo initial
677 test_cmp expect actual
680 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
681 # grep matches only initial and third
682 # author matches all but second
683 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
685 echo third && echo initial
687 test_cmp expect actual
690 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
691 # grep matches only third
692 # author matches only initial and third
693 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
697 test_cmp expect actual
700 test_expect_success 'log --author does not search in timestamp' '
702 git log --author="$GIT_AUTHOR_DATE" >actual &&
703 test_cmp expect actual
706 test_expect_success 'log --committer does not search in timestamp' '
708 git log --committer="$GIT_COMMITTER_DATE" >actual &&
709 test_cmp expect actual
712 test_expect_success 'grep with CE_VALID file' '
713 git update-index --assume-unchanged t/t &&
715 test "$(git grep test)" = "t/t:test" &&
716 git update-index --no-assume-unchanged t/t &&
721 hello.c=#include <stdio.h>
725 test_expect_success 'grep -p with userdiff' '
726 git config diff.custom.funcname "^#" &&
727 echo "hello.c diff=custom" >.gitattributes &&
728 git grep -p return >actual &&
729 test_cmp expected actual
733 hello.c=int main(int argc, const char **argv)
737 test_expect_success 'grep -p' '
738 rm -f .gitattributes &&
739 git grep -p return >actual &&
740 test_cmp expected actual
744 hello.c-#include <stdio.h>
746 hello.c=int main(int argc, const char **argv)
748 hello.c- printf("Hello world.\n");
752 test_expect_success 'grep -p -B5' '
753 git grep -p -B5 return >actual &&
754 test_cmp expected actual
758 hello.c=int main(int argc, const char **argv)
760 hello.c- printf("Hello world.\n");
762 hello.c- /* char ?? */
766 test_expect_success 'grep -W' '
767 git grep -W return >actual &&
768 test_cmp expected actual
772 hello.c-#include <assert.h>
773 hello.c:#include <stdio.h>
776 test_expect_success 'grep -W shows no trailing empty lines' '
777 git grep -W stdio >actual &&
778 test_cmp expected actual
781 test_expect_success 'grep -W with userdiff' '
782 test_when_finished "rm -f .gitattributes" &&
783 git config diff.custom.xfuncname "^function .*$" &&
784 echo "hello.ps1 diff=custom" >.gitattributes &&
785 git grep -W echo >function-context-userdiff-actual
788 test_expect_success ' includes preceding comment' '
789 grep "# Say hello" function-context-userdiff-actual
792 test_expect_success ' includes function line' '
793 grep "=function hello" function-context-userdiff-actual
796 test_expect_success ' includes matching line' '
797 grep ": echo" function-context-userdiff-actual
800 test_expect_success ' includes last line of the function' '
801 grep "} # hello" function-context-userdiff-actual
804 for threads in $(test_seq 0 10)
806 test_expect_success "grep --threads=$threads & -c grep.threads=$threads" "
807 git grep --threads=$threads . >actual.$threads &&
808 if test $threads -ge 1
810 test_cmp actual.\$(($threads - 1)) actual.$threads
812 git -c grep.threads=$threads grep . >actual.$threads &&
813 if test $threads -ge 1
815 test_cmp actual.\$(($threads - 1)) actual.$threads
820 test_expect_success !PTHREADS,C_LOCALE_OUTPUT 'grep --threads=N or pack.threads=N warns when no pthreads' '
821 git grep --threads=2 Hello hello_world 2>err &&
822 grep ^warning: err >warnings &&
823 test_line_count = 1 warnings &&
824 grep -F "no threads support, ignoring --threads" err &&
825 git -c grep.threads=2 grep Hello hello_world 2>err &&
826 grep ^warning: err >warnings &&
827 test_line_count = 1 warnings &&
828 grep -F "no threads support, ignoring grep.threads" err &&
829 git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
830 grep ^warning: err >warnings &&
831 test_line_count = 2 warnings &&
832 grep -F "no threads support, ignoring --threads" err &&
833 grep -F "no threads support, ignoring grep.threads" err &&
834 git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
835 test_line_count = 0 err
838 test_expect_success 'grep from a subdirectory to search wider area (1)' '
841 cd s && git grep "x x x" ..
845 test_expect_success 'grep from a subdirectory to search wider area (2)' '
849 ( git grep xxyyzz .. >out ; echo $? >status )
851 test 1 = $(cat status)
856 hello.c:int main(int argc, const char **argv)
859 test_expect_success 'grep -Fi' '
860 git grep -Fi "CHAR *" >actual &&
861 test_cmp expected actual
864 test_expect_success 'outside of git repository' '
866 mkdir -p non/git/sub &&
867 echo hello >non/git/file1 &&
868 echo world >non/git/sub/file2 &&
872 } >non/expect.full &&
873 echo file2:world >non/expect.sub &&
875 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
876 export GIT_CEILING_DIRECTORIES &&
878 test_must_fail git grep o &&
879 git grep --no-index o >../actual.full &&
880 test_cmp ../expect.full ../actual.full &&
882 test_must_fail git grep o &&
883 git grep --no-index o >../../actual.sub &&
884 test_cmp ../../expect.sub ../../actual.sub
887 echo ".*o*" >non/git/.gitignore &&
889 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
890 export GIT_CEILING_DIRECTORIES &&
892 test_must_fail git grep o &&
893 git grep --no-index --exclude-standard o >../actual.full &&
894 test_cmp ../expect.full ../actual.full &&
897 echo ".gitignore:.*o*" &&
899 } >../expect.with.ignored &&
900 git grep --no-index --no-exclude o >../actual.full &&
901 test_cmp ../expect.with.ignored ../actual.full
905 test_expect_success 'outside of git repository with fallbackToNoIndex' '
907 mkdir -p non/git/sub &&
908 echo hello >non/git/file1 &&
909 echo world >non/git/sub/file2 &&
910 cat <<-\EOF >non/expect.full &&
914 echo file2:world >non/expect.sub &&
916 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
917 export GIT_CEILING_DIRECTORIES &&
919 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
920 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
921 test_cmp ../expect.full ../actual.full &&
923 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
924 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
925 test_cmp ../../expect.sub ../../actual.sub
928 echo ".*o*" >non/git/.gitignore &&
930 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
931 export GIT_CEILING_DIRECTORIES &&
933 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
934 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
935 test_cmp ../expect.full ../actual.full &&
938 echo ".gitignore:.*o*" &&
940 } >../expect.with.ignored &&
941 git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
942 test_cmp ../expect.with.ignored ../actual.full
946 test_expect_success 'inside git repository but with --no-index' '
948 mkdir -p is/git/sub &&
949 echo hello >is/git/file1 &&
950 echo world >is/git/sub/file2 &&
951 echo ".*o*" >is/git/.gitignore &&
955 } >is/expect.unignored &&
957 echo ".gitignore:.*o*" &&
958 cat is/expect.unignored
960 : >is/expect.empty &&
961 echo file2:world >is/expect.sub &&
965 test_must_fail git grep o >../actual.full &&
966 test_cmp ../expect.empty ../actual.full &&
968 git grep --untracked o >../actual.unignored &&
969 test_cmp ../expect.unignored ../actual.unignored &&
971 git grep --no-index o >../actual.full &&
972 test_cmp ../expect.full ../actual.full &&
974 git grep --no-index --exclude-standard o >../actual.unignored &&
975 test_cmp ../expect.unignored ../actual.unignored &&
978 test_must_fail git grep o >../../actual.sub &&
979 test_cmp ../../expect.empty ../../actual.sub &&
981 git grep --no-index o >../../actual.sub &&
982 test_cmp ../../expect.sub ../../actual.sub &&
984 git grep --untracked o >../../actual.sub &&
985 test_cmp ../../expect.sub ../../actual.sub
989 test_expect_success 'grep --no-index descends into repos, but not .git' '
993 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
994 export GIT_CEILING_DIRECTORIES &&
1003 git commit -m foo &&
1004 echo magic >.git/file
1007 cat >expect <<-\EOF &&
1011 git grep -l --no-index magic >actual &&
1012 test_cmp expect actual
1016 test_expect_success 'setup double-dash tests' '
1017 cat >double-dash <<EOF &&
1028 test_expect_success 'grep -- pattern' '
1029 git grep -- "->" >actual &&
1030 test_cmp expected actual
1032 test_expect_success 'grep -- pattern -- pathspec' '
1033 git grep -- "->" -- double-dash >actual &&
1034 test_cmp expected actual
1036 test_expect_success 'grep -e pattern -- path' '
1037 git grep -e "->" -- double-dash >actual &&
1038 test_cmp expected actual
1044 test_expect_success 'grep -e -- -- path' '
1045 git grep -e -- -- double-dash >actual &&
1046 test_cmp expected actual
1049 test_expect_success 'dashdash disambiguates rev as rev' '
1050 test_when_finished "rm -f master" &&
1051 echo content >master &&
1052 echo master:hello.c >expect &&
1053 git grep -l o master -- hello.c >actual &&
1054 test_cmp expect actual
1057 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
1058 test_when_finished "git rm -f master" &&
1059 echo content >master &&
1061 echo master:content >expect &&
1062 git grep o -- master >actual &&
1063 test_cmp expect actual
1066 test_expect_success 'report bogus arg without dashdash' '
1067 test_must_fail git grep o does-not-exist
1070 test_expect_success 'report bogus rev with dashdash' '
1071 test_must_fail git grep o hello.c --
1074 test_expect_success 'allow non-existent path with dashdash' '
1075 # We need a real match so grep exits with success.
1076 tree=$(git ls-tree HEAD |
1077 sed s/hello.c/not-in-working-tree/ |
1079 git grep o "$tree" -- not-in-working-tree
1082 test_expect_success 'grep --no-index pattern -- path' '
1086 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1087 export GIT_CEILING_DIRECTORIES &&
1089 echo hello >hello &&
1090 echo goodbye >goodbye &&
1091 echo hello:hello >expect &&
1092 git grep --no-index o -- hello >actual &&
1093 test_cmp expect actual
1097 test_expect_success 'grep --no-index complains of revs' '
1098 test_must_fail git grep --no-index o master -- 2>err &&
1099 test_i18ngrep "cannot be used with revs" err
1102 test_expect_success 'grep --no-index prefers paths to revs' '
1103 test_when_finished "rm -f master" &&
1104 echo content >master &&
1105 echo master:content >expect &&
1106 git grep --no-index o master >actual &&
1107 test_cmp expect actual
1110 test_expect_success 'grep --no-index does not "diagnose" revs' '
1111 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1112 test_i18ngrep ! -i "did you mean" err
1116 hello.c:int main(int argc, const char **argv)
1117 hello.c: printf("Hello world.\n");
1120 test_expect_success PCRE 'grep --perl-regexp pattern' '
1121 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1122 test_cmp expected actual
1125 test_expect_success !PCRE 'grep --perl-regexp pattern errors without PCRE' '
1126 test_must_fail git grep --perl-regexp "foo.*bar"
1129 test_expect_success PCRE 'grep -P pattern' '
1130 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1131 test_cmp expected actual
1134 test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
1135 git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1136 test_cmp expected actual
1140 test_expect_success !PCRE 'grep -P pattern errors without PCRE' '
1141 test_must_fail git grep -P "foo.*bar"
1144 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1146 test_must_fail git -c grep.extendedregexp=true \
1147 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1148 test_cmp empty actual
1151 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1152 git -c grep.extendedregexp=true \
1153 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1154 test_cmp expected actual
1157 test_expect_success PCRE 'grep -P -v pattern' '
1162 git grep -P -v "abc" ab >actual &&
1163 test_cmp expected actual
1166 test_expect_success PCRE 'grep -P -i pattern' '
1167 cat >expected <<-EOF &&
1168 hello.c: printf("Hello world.\n");
1170 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1171 test_cmp expected actual
1174 test_expect_success PCRE 'grep -P -w pattern' '
1176 echo "hello_world:Hello world"
1177 echo "hello_world:HeLLo world"
1179 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1180 test_cmp expected actual
1183 test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1184 git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1185 test_cmp hello_world actual &&
1186 git grep -P -h "(.)\1" hello_world >actual &&
1187 test_cmp hello_world actual
1190 test_expect_success 'grep -G invalidpattern properly dies ' '
1191 test_must_fail git grep -G "a["
1194 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1195 test_must_fail git -c grep.patterntype=basic grep "a["
1198 test_expect_success 'grep -E invalidpattern properly dies ' '
1199 test_must_fail git grep -E "a["
1202 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1203 test_must_fail git -c grep.patterntype=extended grep "a["
1206 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1207 test_must_fail git grep -P "a["
1210 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1211 test_must_fail git -c grep.patterntype=perl grep "a["
1214 test_expect_success 'grep -G -E -F pattern' '
1215 echo "ab:a+b*c" >expected &&
1216 git grep -G -E -F "a+b*c" ab >actual &&
1217 test_cmp expected actual
1220 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1221 echo "ab:a+b*c" >expected &&
1223 -c grep.patterntype=basic \
1224 -c grep.patterntype=extended \
1225 -c grep.patterntype=fixed \
1226 grep "a+b*c" ab >actual &&
1227 test_cmp expected actual
1230 test_expect_success 'grep -E -F -G pattern' '
1231 echo "ab:a+bc" >expected &&
1232 git grep -E -F -G "a+b*c" ab >actual &&
1233 test_cmp expected actual
1236 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1237 echo "ab:a+bc" >expected &&
1239 -c grep.patterntype=extended \
1240 -c grep.patterntype=fixed \
1241 -c grep.patterntype=basic \
1242 grep "a+b*c" ab >actual &&
1243 test_cmp expected actual
1246 test_expect_success 'grep -F -G -E pattern' '
1247 echo "ab:abc" >expected &&
1248 git grep -F -G -E "a+b*c" ab >actual &&
1249 test_cmp expected actual
1252 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1253 echo "ab:abc" >expected &&
1255 -c grep.patterntype=fixed \
1256 -c grep.patterntype=basic \
1257 -c grep.patterntype=extended \
1258 grep "a+b*c" ab >actual &&
1259 test_cmp expected actual
1262 test_expect_success 'grep -G -F -P -E pattern' '
1263 echo "d0:d" >expected &&
1264 git grep -G -F -P -E "[\d]" d0 >actual &&
1265 test_cmp expected actual
1268 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1269 echo "d0:d" >expected &&
1271 -c grep.patterntype=fixed \
1272 -c grep.patterntype=basic \
1273 -c grep.patterntype=perl \
1274 -c grep.patterntype=extended \
1275 grep "[\d]" d0 >actual &&
1276 test_cmp expected actual
1279 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1280 echo "d0:0" >expected &&
1281 git grep -G -F -E -P "[\d]" d0 >actual &&
1282 test_cmp expected actual
1285 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1286 echo "d0:0" >expected &&
1288 -c grep.patterntype=fixed \
1289 -c grep.patterntype=basic \
1290 -c grep.patterntype=extended \
1291 -c grep.patterntype=perl \
1292 grep "[\d]" d0 >actual &&
1293 test_cmp expected actual
1296 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1297 echo "ab:a+b*c" >expected &&
1299 -c grep.patterntype=fixed \
1300 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1301 test_cmp expected actual
1304 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1305 echo "ab:a+b*c" >expected &&
1307 -c grep.patterntype=basic \
1308 grep -F "*c" ab >actual &&
1309 test_cmp expected actual
1312 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1318 -c grep.patterntype=fixed \
1319 grep -G "a+b" ab >actual &&
1320 test_cmp expected actual
1323 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1330 -c grep.patterntype=fixed \
1331 grep -E "a+" ab >actual &&
1332 test_cmp expected actual
1336 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1337 hello.c<RED>-<RESET>{
1339 hello.c<RED>:<RESET> /* char ?? */
1340 hello.c<RED>-<RESET>}
1342 hello_world<RED>:<RESET>Hello_world
1343 hello_world<RED>-<RESET>HeLLo_world
1346 test_expect_success 'grep --color, separator' '
1347 test_config color.grep.context normal &&
1348 test_config color.grep.filename normal &&
1349 test_config color.grep.function normal &&
1350 test_config color.grep.linenumber normal &&
1351 test_config color.grep.match normal &&
1352 test_config color.grep.selected normal &&
1353 test_config color.grep.separator red &&
1355 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1356 test_decode_color >actual &&
1357 test_cmp expected actual
1361 hello.c:int main(int argc, const char **argv)
1362 hello.c: /* char ?? */
1364 hello_world:Hello_world
1367 test_expect_success 'grep --break' '
1368 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1369 test_cmp expected actual
1373 hello.c:int main(int argc, const char **argv)
1376 hello.c: /* char ?? */
1379 hello_world:Hello_world
1380 hello_world-HeLLo_world
1383 test_expect_success 'grep --break with context' '
1384 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1385 test_cmp expected actual
1390 int main(int argc, const char **argv)
1396 test_expect_success 'grep --heading' '
1397 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1398 test_cmp expected actual
1402 <BOLD;GREEN>hello.c<RESET>
1403 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1404 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1406 <BOLD;GREEN>hello_world<RESET>
1407 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1410 test_expect_success 'mimic ack-grep --group' '
1411 test_config color.grep.context normal &&
1412 test_config color.grep.filename "bold green" &&
1413 test_config color.grep.function normal &&
1414 test_config color.grep.linenumber normal &&
1415 test_config color.grep.match "black yellow" &&
1416 test_config color.grep.selected normal &&
1417 test_config color.grep.separator normal &&
1419 git grep --break --heading -n --color \
1420 -e char -e lo_w hello.c hello_world |
1421 test_decode_color >actual &&
1422 test_cmp expected actual
1426 space: line with leading space1
1427 space: line with leading space2
1428 space: line with leading space3
1431 test_expect_success PCRE 'grep -E "^ "' '
1432 git grep -E "^ " space >actual &&
1433 test_cmp expected actual
1436 test_expect_success PCRE 'grep -P "^ "' '
1437 git grep -P "^ " space >actual &&
1438 test_cmp expected actual
1442 space-line without leading space1
1443 space: line <RED>with <RESET>leading space1
1444 space: line <RED>with <RESET>leading <RED>space2<RESET>
1445 space: line <RED>with <RESET>leading space3
1446 space:line without leading <RED>space2<RESET>
1449 test_expect_success 'grep --color -e A -e B with context' '
1450 test_config color.grep.context normal &&
1451 test_config color.grep.filename normal &&
1452 test_config color.grep.function normal &&
1453 test_config color.grep.linenumber normal &&
1454 test_config color.grep.matchContext normal &&
1455 test_config color.grep.matchSelected red &&
1456 test_config color.grep.selected normal &&
1457 test_config color.grep.separator normal &&
1459 git grep --color=always -C2 -e "with " -e space2 space |
1460 test_decode_color >actual &&
1461 test_cmp expected actual
1465 space-line without leading space1
1466 space- line with leading space1
1467 space: line <RED>with <RESET>leading <RED>space2<RESET>
1468 space- line with leading space3
1469 space-line without leading space2
1472 test_expect_success 'grep --color -e A --and -e B with context' '
1473 test_config color.grep.context normal &&
1474 test_config color.grep.filename normal &&
1475 test_config color.grep.function normal &&
1476 test_config color.grep.linenumber normal &&
1477 test_config color.grep.matchContext normal &&
1478 test_config color.grep.matchSelected red &&
1479 test_config color.grep.selected normal &&
1480 test_config color.grep.separator normal &&
1482 git grep --color=always -C2 -e "with " --and -e space2 space |
1483 test_decode_color >actual &&
1484 test_cmp expected actual
1488 space-line without leading space1
1489 space: line <RED>with <RESET>leading space1
1490 space- line with leading space2
1491 space: line <RED>with <RESET>leading space3
1492 space-line without leading space2
1495 test_expect_success 'grep --color -e A --and --not -e B with context' '
1496 test_config color.grep.context normal &&
1497 test_config color.grep.filename normal &&
1498 test_config color.grep.function normal &&
1499 test_config color.grep.linenumber normal &&
1500 test_config color.grep.matchContext normal &&
1501 test_config color.grep.matchSelected red &&
1502 test_config color.grep.selected normal &&
1503 test_config color.grep.separator normal &&
1505 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1506 test_decode_color >actual &&
1507 test_cmp expected actual
1512 hello.c=int main(int argc, const char **argv)
1514 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1516 hello.c- /* char ?? */
1520 test_expect_success 'grep --color -e A --and -e B -p with context' '
1521 test_config color.grep.context normal &&
1522 test_config color.grep.filename normal &&
1523 test_config color.grep.function normal &&
1524 test_config color.grep.linenumber normal &&
1525 test_config color.grep.matchContext normal &&
1526 test_config color.grep.matchSelected red &&
1527 test_config color.grep.selected normal &&
1528 test_config color.grep.separator normal &&
1530 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1531 test_decode_color >actual &&
1532 test_cmp expected actual
1535 test_expect_success 'grep can find things only in the work tree' '
1536 : >work-tree-only &&
1537 git add work-tree-only &&
1538 test_when_finished "git rm -f work-tree-only" &&
1539 echo "find in work tree" >work-tree-only &&
1540 git grep --quiet "find in work tree" &&
1541 test_must_fail git grep --quiet --cached "find in work tree" &&
1542 test_must_fail git grep --quiet "find in work tree" HEAD
1545 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1546 echo "intend to add this" >intend-to-add &&
1547 git add -N intend-to-add &&
1548 test_when_finished "git rm -f intend-to-add" &&
1549 git grep --quiet "intend to add this" &&
1550 test_must_fail git grep --quiet --cached "intend to add this" &&
1551 test_must_fail git grep --quiet "intend to add this" HEAD
1554 test_expect_success 'grep does not search work tree with assume unchanged' '
1555 echo "intend to add this" >intend-to-add &&
1556 git add -N intend-to-add &&
1557 git update-index --assume-unchanged intend-to-add &&
1558 test_when_finished "git rm -f intend-to-add" &&
1559 test_must_fail git grep --quiet "intend to add this" &&
1560 test_must_fail git grep --quiet --cached "intend to add this" &&
1561 test_must_fail git grep --quiet "intend to add this" HEAD
1564 test_expect_success 'grep can find things only in the index' '
1565 echo "only in the index" >cache-this &&
1566 git add cache-this &&
1568 test_when_finished "git rm --cached cache-this" &&
1569 test_must_fail git grep --quiet "only in the index" &&
1570 git grep --quiet --cached "only in the index" &&
1571 test_must_fail git grep --quiet "only in the index" HEAD
1574 test_expect_success 'grep does not report i-t-a with -L --cached' '
1575 echo "intend to add this" >intend-to-add &&
1576 git add -N intend-to-add &&
1577 test_when_finished "git rm -f intend-to-add" &&
1578 git ls-files | grep -v "^intend-to-add\$" >expected &&
1579 git grep -L --cached "nonexistent_string" >actual &&
1580 test_cmp expected actual
1583 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1584 echo "intend to add this" >intend-to-add-assume-unchanged &&
1585 git add -N intend-to-add-assume-unchanged &&
1586 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1587 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1588 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1589 git grep -L "nonexistent_string" >actual &&
1590 test_cmp expected actual