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 !PCRE 'grep -P pattern errors without PCRE' '
1135 test_must_fail git grep -P "foo.*bar"
1138 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1140 test_must_fail git -c grep.extendedregexp=true \
1141 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1142 test_cmp empty actual
1145 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1146 git -c grep.extendedregexp=true \
1147 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1148 test_cmp expected actual
1151 test_expect_success PCRE 'grep -P -v pattern' '
1156 git grep -P -v "abc" ab >actual &&
1157 test_cmp expected actual
1160 test_expect_success PCRE 'grep -P -i pattern' '
1161 cat >expected <<-EOF &&
1162 hello.c: printf("Hello world.\n");
1164 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1165 test_cmp expected actual
1168 test_expect_success PCRE 'grep -P -w pattern' '
1170 echo "hello_world:Hello world"
1171 echo "hello_world:HeLLo world"
1173 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1174 test_cmp expected actual
1177 test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1178 git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1179 test_cmp hello_world actual &&
1180 git grep -P -h "(.)\1" hello_world >actual &&
1181 test_cmp hello_world actual
1184 test_expect_success 'grep -G invalidpattern properly dies ' '
1185 test_must_fail git grep -G "a["
1188 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1189 test_must_fail git -c grep.patterntype=basic grep "a["
1192 test_expect_success 'grep -E invalidpattern properly dies ' '
1193 test_must_fail git grep -E "a["
1196 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1197 test_must_fail git -c grep.patterntype=extended grep "a["
1200 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1201 test_must_fail git grep -P "a["
1204 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1205 test_must_fail git -c grep.patterntype=perl grep "a["
1208 test_expect_success 'grep -G -E -F pattern' '
1209 echo "ab:a+b*c" >expected &&
1210 git grep -G -E -F "a+b*c" ab >actual &&
1211 test_cmp expected actual
1214 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1215 echo "ab:a+b*c" >expected &&
1217 -c grep.patterntype=basic \
1218 -c grep.patterntype=extended \
1219 -c grep.patterntype=fixed \
1220 grep "a+b*c" ab >actual &&
1221 test_cmp expected actual
1224 test_expect_success 'grep -E -F -G pattern' '
1225 echo "ab:a+bc" >expected &&
1226 git grep -E -F -G "a+b*c" ab >actual &&
1227 test_cmp expected actual
1230 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1231 echo "ab:a+bc" >expected &&
1233 -c grep.patterntype=extended \
1234 -c grep.patterntype=fixed \
1235 -c grep.patterntype=basic \
1236 grep "a+b*c" ab >actual &&
1237 test_cmp expected actual
1240 test_expect_success 'grep -F -G -E pattern' '
1241 echo "ab:abc" >expected &&
1242 git grep -F -G -E "a+b*c" ab >actual &&
1243 test_cmp expected actual
1246 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1247 echo "ab:abc" >expected &&
1249 -c grep.patterntype=fixed \
1250 -c grep.patterntype=basic \
1251 -c grep.patterntype=extended \
1252 grep "a+b*c" ab >actual &&
1253 test_cmp expected actual
1256 test_expect_success 'grep -G -F -P -E pattern' '
1257 echo "d0:d" >expected &&
1258 git grep -G -F -P -E "[\d]" d0 >actual &&
1259 test_cmp expected actual
1262 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1263 echo "d0:d" >expected &&
1265 -c grep.patterntype=fixed \
1266 -c grep.patterntype=basic \
1267 -c grep.patterntype=perl \
1268 -c grep.patterntype=extended \
1269 grep "[\d]" d0 >actual &&
1270 test_cmp expected actual
1273 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1274 echo "d0:0" >expected &&
1275 git grep -G -F -E -P "[\d]" d0 >actual &&
1276 test_cmp expected actual
1279 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1280 echo "d0:0" >expected &&
1282 -c grep.patterntype=fixed \
1283 -c grep.patterntype=basic \
1284 -c grep.patterntype=extended \
1285 -c grep.patterntype=perl \
1286 grep "[\d]" d0 >actual &&
1287 test_cmp expected actual
1290 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1291 echo "ab:a+b*c" >expected &&
1293 -c grep.patterntype=fixed \
1294 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1295 test_cmp expected actual
1298 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1299 echo "ab:a+b*c" >expected &&
1301 -c grep.patterntype=basic \
1302 grep -F "*c" ab >actual &&
1303 test_cmp expected actual
1306 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1312 -c grep.patterntype=fixed \
1313 grep -G "a+b" ab >actual &&
1314 test_cmp expected actual
1317 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1324 -c grep.patterntype=fixed \
1325 grep -E "a+" ab >actual &&
1326 test_cmp expected actual
1330 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1331 hello.c<RED>-<RESET>{
1333 hello.c<RED>:<RESET> /* char ?? */
1334 hello.c<RED>-<RESET>}
1336 hello_world<RED>:<RESET>Hello_world
1337 hello_world<RED>-<RESET>HeLLo_world
1340 test_expect_success 'grep --color, separator' '
1341 test_config color.grep.context normal &&
1342 test_config color.grep.filename normal &&
1343 test_config color.grep.function normal &&
1344 test_config color.grep.linenumber normal &&
1345 test_config color.grep.match normal &&
1346 test_config color.grep.selected normal &&
1347 test_config color.grep.separator red &&
1349 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1350 test_decode_color >actual &&
1351 test_cmp expected actual
1355 hello.c:int main(int argc, const char **argv)
1356 hello.c: /* char ?? */
1358 hello_world:Hello_world
1361 test_expect_success 'grep --break' '
1362 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1363 test_cmp expected actual
1367 hello.c:int main(int argc, const char **argv)
1370 hello.c: /* char ?? */
1373 hello_world:Hello_world
1374 hello_world-HeLLo_world
1377 test_expect_success 'grep --break with context' '
1378 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1379 test_cmp expected actual
1384 int main(int argc, const char **argv)
1390 test_expect_success 'grep --heading' '
1391 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1392 test_cmp expected actual
1396 <BOLD;GREEN>hello.c<RESET>
1397 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1398 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1400 <BOLD;GREEN>hello_world<RESET>
1401 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1404 test_expect_success 'mimic ack-grep --group' '
1405 test_config color.grep.context normal &&
1406 test_config color.grep.filename "bold green" &&
1407 test_config color.grep.function normal &&
1408 test_config color.grep.linenumber normal &&
1409 test_config color.grep.match "black yellow" &&
1410 test_config color.grep.selected normal &&
1411 test_config color.grep.separator normal &&
1413 git grep --break --heading -n --color \
1414 -e char -e lo_w hello.c hello_world |
1415 test_decode_color >actual &&
1416 test_cmp expected actual
1420 space: line with leading space1
1421 space: line with leading space2
1422 space: line with leading space3
1425 test_expect_success PCRE 'grep -E "^ "' '
1426 git grep -E "^ " space >actual &&
1427 test_cmp expected actual
1430 test_expect_success PCRE 'grep -P "^ "' '
1431 git grep -P "^ " space >actual &&
1432 test_cmp expected actual
1436 space-line without leading space1
1437 space: line <RED>with <RESET>leading space1
1438 space: line <RED>with <RESET>leading <RED>space2<RESET>
1439 space: line <RED>with <RESET>leading space3
1440 space:line without leading <RED>space2<RESET>
1443 test_expect_success 'grep --color -e A -e B with context' '
1444 test_config color.grep.context normal &&
1445 test_config color.grep.filename normal &&
1446 test_config color.grep.function normal &&
1447 test_config color.grep.linenumber normal &&
1448 test_config color.grep.matchContext normal &&
1449 test_config color.grep.matchSelected red &&
1450 test_config color.grep.selected normal &&
1451 test_config color.grep.separator normal &&
1453 git grep --color=always -C2 -e "with " -e space2 space |
1454 test_decode_color >actual &&
1455 test_cmp expected actual
1459 space-line without leading space1
1460 space- line with leading space1
1461 space: line <RED>with <RESET>leading <RED>space2<RESET>
1462 space- line with leading space3
1463 space-line without leading space2
1466 test_expect_success 'grep --color -e A --and -e B with context' '
1467 test_config color.grep.context normal &&
1468 test_config color.grep.filename normal &&
1469 test_config color.grep.function normal &&
1470 test_config color.grep.linenumber normal &&
1471 test_config color.grep.matchContext normal &&
1472 test_config color.grep.matchSelected red &&
1473 test_config color.grep.selected normal &&
1474 test_config color.grep.separator normal &&
1476 git grep --color=always -C2 -e "with " --and -e space2 space |
1477 test_decode_color >actual &&
1478 test_cmp expected actual
1482 space-line without leading space1
1483 space: line <RED>with <RESET>leading space1
1484 space- line with leading space2
1485 space: line <RED>with <RESET>leading space3
1486 space-line without leading space2
1489 test_expect_success 'grep --color -e A --and --not -e B with context' '
1490 test_config color.grep.context normal &&
1491 test_config color.grep.filename normal &&
1492 test_config color.grep.function normal &&
1493 test_config color.grep.linenumber normal &&
1494 test_config color.grep.matchContext normal &&
1495 test_config color.grep.matchSelected red &&
1496 test_config color.grep.selected normal &&
1497 test_config color.grep.separator normal &&
1499 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1500 test_decode_color >actual &&
1501 test_cmp expected actual
1506 hello.c=int main(int argc, const char **argv)
1508 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1510 hello.c- /* char ?? */
1514 test_expect_success 'grep --color -e A --and -e B -p with context' '
1515 test_config color.grep.context normal &&
1516 test_config color.grep.filename normal &&
1517 test_config color.grep.function normal &&
1518 test_config color.grep.linenumber normal &&
1519 test_config color.grep.matchContext normal &&
1520 test_config color.grep.matchSelected red &&
1521 test_config color.grep.selected normal &&
1522 test_config color.grep.separator normal &&
1524 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1525 test_decode_color >actual &&
1526 test_cmp expected actual
1529 test_expect_success 'grep can find things only in the work tree' '
1530 : >work-tree-only &&
1531 git add work-tree-only &&
1532 test_when_finished "git rm -f work-tree-only" &&
1533 echo "find in work tree" >work-tree-only &&
1534 git grep --quiet "find in work tree" &&
1535 test_must_fail git grep --quiet --cached "find in work tree" &&
1536 test_must_fail git grep --quiet "find in work tree" HEAD
1539 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1540 echo "intend to add this" >intend-to-add &&
1541 git add -N intend-to-add &&
1542 test_when_finished "git rm -f intend-to-add" &&
1543 git grep --quiet "intend to add this" &&
1544 test_must_fail git grep --quiet --cached "intend to add this" &&
1545 test_must_fail git grep --quiet "intend to add this" HEAD
1548 test_expect_success 'grep does not search work tree with assume unchanged' '
1549 echo "intend to add this" >intend-to-add &&
1550 git add -N intend-to-add &&
1551 git update-index --assume-unchanged intend-to-add &&
1552 test_when_finished "git rm -f intend-to-add" &&
1553 test_must_fail git grep --quiet "intend to add this" &&
1554 test_must_fail git grep --quiet --cached "intend to add this" &&
1555 test_must_fail git grep --quiet "intend to add this" HEAD
1558 test_expect_success 'grep can find things only in the index' '
1559 echo "only in the index" >cache-this &&
1560 git add cache-this &&
1562 test_when_finished "git rm --cached cache-this" &&
1563 test_must_fail git grep --quiet "only in the index" &&
1564 git grep --quiet --cached "only in the index" &&
1565 test_must_fail git grep --quiet "only in the index" HEAD
1568 test_expect_success 'grep does not report i-t-a with -L --cached' '
1569 echo "intend to add this" >intend-to-add &&
1570 git add -N intend-to-add &&
1571 test_when_finished "git rm -f intend-to-add" &&
1572 git ls-files | grep -v "^intend-to-add\$" >expected &&
1573 git grep -L --cached "nonexistent_string" >actual &&
1574 test_cmp expected actual
1577 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1578 echo "intend to add this" >intend-to-add-assume-unchanged &&
1579 git add -N intend-to-add-assume-unchanged &&
1580 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1581 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1582 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1583 git grep -L "nonexistent_string" >actual &&
1584 test_cmp expected actual