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 (with --column)" '
104 echo ${HC}file:5:foo mmap bar
105 echo ${HC}file:14:foo_mmap bar mmap
106 echo ${HC}file:5:foo mmap bar_mmap
107 echo ${HC}file:14:foo_mmap bar mmap baz
109 git grep --column -w -e mmap $H >actual &&
110 test_cmp expected actual
113 test_expect_success
"grep -w $L (with --column, extended OR)" '
115 echo ${HC}file:14:foo_mmap bar mmap
116 echo ${HC}file:19:foo_mmap bar mmap baz
118 git grep --column -w -e mmap$ --or -e baz $H >actual &&
119 test_cmp expected actual
122 test_expect_success
"grep -w $L (with --column, --invert)" '
124 echo ${HC}file:1:foo mmap bar
125 echo ${HC}file:1:foo_mmap bar
126 echo ${HC}file:1:foo_mmap bar mmap
127 echo ${HC}file:1:foo mmap bar_mmap
129 git grep --column --invert -w -e baz $H -- file >actual &&
130 test_cmp expected actual
133 test_expect_success
"grep $L (with --column, --invert, extended OR)" '
135 echo ${HC}hello_world:6:HeLLo_world
137 git grep --column --invert -e ll --or --not -e _ $H -- hello_world \
139 test_cmp expected actual
142 test_expect_success
"grep $L (with --column, --invert, extended AND)" '
144 echo ${HC}hello_world:3:Hello world
145 echo ${HC}hello_world:3:Hello_world
146 echo ${HC}hello_world:6:HeLLo_world
148 git grep --column --invert --not -e _ --and --not -e ll $H -- hello_world \
150 test_cmp expected actual
153 test_expect_success
"grep $L (with --column, double-negation)" '
155 echo ${HC}file:1:foo_mmap bar mmap baz
157 git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
159 test_cmp expected actual
162 test_expect_success
"grep -w $L (with --column, -C)" '
164 echo ${HC}file:5:foo mmap bar
165 echo ${HC}file-foo_mmap bar
166 echo ${HC}file:14:foo_mmap bar mmap
167 echo ${HC}file:5:foo mmap bar_mmap
168 echo ${HC}file:14:foo_mmap bar mmap baz
170 git grep --column -w -C1 -e mmap $H >actual &&
171 test_cmp expected actual
174 test_expect_success
"grep -w $L (with --line-number, --column)" '
176 echo ${HC}file:1:5:foo mmap bar
177 echo ${HC}file:3:14:foo_mmap bar mmap
178 echo ${HC}file:4:5:foo mmap bar_mmap
179 echo ${HC}file:5:14:foo_mmap bar mmap baz
181 git grep -n --column -w -e mmap $H >actual &&
182 test_cmp expected actual
185 test_expect_success
"grep -w $L (with non-extended patterns, --column)" '
187 echo ${HC}file:5:foo mmap bar
188 echo ${HC}file:10:foo_mmap bar
189 echo ${HC}file:10:foo_mmap bar mmap
190 echo ${HC}file:5:foo mmap bar_mmap
191 echo ${HC}file:10:foo_mmap bar mmap baz
193 git grep --column -w -e bar -e mmap $H >actual &&
194 test_cmp expected actual
197 test_expect_success
"grep -w $L" '
199 echo ${HC}file:1:foo mmap bar
200 echo ${HC}file:3:foo_mmap bar mmap
201 echo ${HC}file:4:foo mmap bar_mmap
202 echo ${HC}file:5:foo_mmap bar mmap baz
204 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
205 test_cmp expected actual
208 test_expect_success
"grep -w $L" '
210 echo ${HC}file:foo mmap bar
211 echo ${HC}file:foo_mmap bar mmap
212 echo ${HC}file:foo mmap bar_mmap
213 echo ${HC}file:foo_mmap bar mmap baz
215 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
216 test_cmp expected actual
219 test_expect_success
"grep -w $L (w)" '
221 test_must_fail git grep -n -w -e "^w" $H >actual &&
222 test_cmp expected actual
225 test_expect_success
"grep -w $L (x)" '
227 echo ${HC}x:1:x x xx x
229 git grep -n -w -e "x xx* x" $H >actual &&
230 test_cmp expected actual
233 test_expect_success
"grep -w $L (y-1)" '
237 git grep -n -w -e "^y" $H >actual &&
238 test_cmp expected actual
241 test_expect_success
"grep -w $L (y-2)" '
243 if git grep -n -w -e "^y y" $H >actual
245 echo should not have matched
249 test_cmp expected actual
253 test_expect_success
"grep -w $L (z)" '
255 if git grep -n -w -e "^z" $H >actual
257 echo should not have matched
261 test_cmp expected actual
265 test_expect_success
"grep $L (with --column, --only-matching)" '
267 echo ${HC}file:1:5:mmap
268 echo ${HC}file:2:5:mmap
269 echo ${HC}file:3:5:mmap
270 echo ${HC}file:3:13:mmap
271 echo ${HC}file:4:5:mmap
272 echo ${HC}file:4:13:mmap
273 echo ${HC}file:5:5:mmap
274 echo ${HC}file:5:13:mmap
276 git grep --column -n -o -e mmap $H >actual &&
277 test_cmp expected actual
280 test_expect_success
"grep $L (t-1)" '
281 echo "${HC}t/t:1:test" >expected &&
282 git grep -n -e test $H >actual &&
283 test_cmp expected actual
286 test_expect_success
"grep $L (t-2)" '
287 echo "${HC}t:1:test" >expected &&
290 git grep -n -e test $H
292 test_cmp expected actual
295 test_expect_success
"grep $L (t-3)" '
296 echo "${HC}t/t:1:test" >expected &&
299 git grep --full-name -n -e test $H
301 test_cmp expected actual
304 test_expect_success
"grep -c $L (no /dev/null)" '
305 ! git grep -c test $H | grep /dev/null
308 test_expect_success
"grep --max-depth -1 $L" '
310 echo ${HC}t/a/v:1:vvv
314 git grep --max-depth -1 -n -e vvv $H >actual &&
315 test_cmp expected actual
318 test_expect_success
"grep --max-depth 0 $L" '
322 git grep --max-depth 0 -n -e vvv $H >actual &&
323 test_cmp expected actual
326 test_expect_success
"grep --max-depth 0 -- '*' $L" '
328 echo ${HC}t/a/v:1:vvv
332 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
333 test_cmp expected actual
336 test_expect_success
"grep --max-depth 1 $L" '
341 git grep --max-depth 1 -n -e vvv $H >actual &&
342 test_cmp expected actual
345 test_expect_success
"grep --max-depth 0 -- t $L" '
349 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
350 test_cmp expected actual
353 test_expect_success
"grep --max-depth 0 -- . t $L" '
358 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
359 test_cmp expected actual
362 test_expect_success
"grep --max-depth 0 -- t . $L" '
367 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
368 test_cmp expected actual
370 test_expect_success
"grep $L with grep.extendedRegexp=false" '
371 echo "${HC}ab:a+bc" >expected &&
372 git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
373 test_cmp expected actual
376 test_expect_success
"grep $L with grep.extendedRegexp=true" '
377 echo "${HC}ab:abc" >expected &&
378 git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
379 test_cmp expected actual
382 test_expect_success
"grep $L with grep.patterntype=basic" '
383 echo "${HC}ab:a+bc" >expected &&
384 git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
385 test_cmp expected actual
388 test_expect_success
"grep $L with grep.patterntype=extended" '
389 echo "${HC}ab:abc" >expected &&
390 git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
391 test_cmp expected actual
394 test_expect_success
"grep $L with grep.patterntype=fixed" '
395 echo "${HC}ab:a+b*c" >expected &&
396 git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
397 test_cmp expected actual
400 test_expect_success PCRE
"grep $L with grep.patterntype=perl" '
401 echo "${HC}ab:a+b*c" >expected &&
402 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
403 test_cmp expected actual
406 test_expect_success
!PCRE
"grep $L with grep.patterntype=perl errors without PCRE" '
407 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
410 test_expect_success
"grep $L with grep.patternType=default and grep.extendedRegexp=true" '
411 echo "${HC}ab:abc" >expected &&
413 -c grep.patternType=default \
414 -c grep.extendedRegexp=true \
415 grep "a+b*c" $H ab >actual &&
416 test_cmp expected actual
419 test_expect_success
"grep $L with grep.extendedRegexp=true and grep.patternType=default" '
420 echo "${HC}ab:abc" >expected &&
422 -c grep.extendedRegexp=true \
423 -c grep.patternType=default \
424 grep "a+b*c" $H ab >actual &&
425 test_cmp expected actual
428 test_expect_success
"grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
429 echo "${HC}ab:abc" >expected &&
431 -c grep.patternType=extended \
432 -c grep.extendedRegexp=false \
433 grep "a+b*c" $H ab >actual &&
434 test_cmp expected actual
437 test_expect_success
"grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
438 echo "${HC}ab:a+bc" >expected &&
440 -c grep.patternType=basic \
441 -c grep.extendedRegexp=true \
442 grep "a+b*c" $H ab >actual &&
443 test_cmp expected actual
446 test_expect_success
"grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
447 echo "${HC}ab:abc" >expected &&
449 -c grep.extendedRegexp=false \
450 -c grep.patternType=extended \
451 grep "a+b*c" $H ab >actual &&
452 test_cmp expected actual
455 test_expect_success
"grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
456 echo "${HC}ab:a+bc" >expected &&
458 -c grep.extendedRegexp=true \
459 -c grep.patternType=basic \
460 grep "a+b*c" $H ab >actual &&
461 test_cmp expected actual
464 test_expect_success
"grep --count $L" '
465 echo ${HC}ab:3 >expected &&
466 git grep --count -e b $H -- ab >actual &&
467 test_cmp expected actual
470 test_expect_success
"grep --count -h $L" '
472 git grep --count -h -e b $H -- ab >actual &&
473 test_cmp expected actual
480 test_expect_success
'grep -l -C' '
481 git grep -l -C1 foo >actual &&
482 test_cmp expected actual
488 test_expect_success
'grep -c -C' '
489 git grep -c -C1 foo >actual &&
490 test_cmp expected actual
493 test_expect_success
'grep -L -C' '
494 git ls-files >expected &&
495 git grep -L -C1 nonexistent_string >actual &&
496 test_cmp expected actual
499 test_expect_success
'grep --files-without-match --quiet' '
500 git grep --files-without-match --quiet nonexistent_string >actual &&
501 test_cmp /dev/null actual
505 file:foo mmap bar_mmap
508 test_expect_success
'grep -e A --and -e B' '
509 git grep -e "foo mmap" --and -e bar_mmap >actual &&
510 test_cmp expected actual
514 file:foo_mmap bar mmap
515 file:foo_mmap bar mmap baz
519 test_expect_success
'grep ( -e A --or -e B ) --and -e B' '
520 git grep \( -e foo_ --or -e baz \) \
521 --and -e " mmap" >actual &&
522 test_cmp expected actual
529 test_expect_success
'grep -e A --and --not -e B' '
530 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
531 test_cmp expected actual
534 test_expect_success
'grep should ignore GREP_OPTIONS' '
535 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
536 test_cmp expected actual
539 test_expect_success
'grep -f, non-existent file' '
540 test_must_fail git grep -f patterns
546 file:foo_mmap bar mmap
547 file:foo mmap bar_mmap
548 file:foo_mmap bar mmap baz
555 test_expect_success
'grep -f, one pattern' '
556 git grep -f pattern >actual &&
557 test_cmp expected actual
563 file:foo_mmap bar mmap
564 file:foo mmap bar_mmap
565 file:foo_mmap bar mmap baz
576 test_expect_success
'grep -f, multiple patterns' '
577 git grep -f patterns >actual &&
578 test_cmp expected actual
581 test_expect_success
'grep, multiple patterns' '
582 git grep "$(cat patterns)" >actual &&
583 test_cmp expected actual
589 file:foo_mmap bar mmap
590 file:foo mmap bar_mmap
591 file:foo_mmap bar mmap baz
605 test_expect_success
'grep -f, ignore empty lines' '
606 git grep -f patterns >actual &&
607 test_cmp expected actual
610 test_expect_success
'grep -f, ignore empty lines, read patterns from stdin' '
611 git grep -f - <patterns >actual &&
612 test_cmp expected actual
621 test_expect_success
'grep -q, silently report matches' '
623 git grep -q mmap >actual &&
624 test_cmp empty actual &&
625 test_must_fail git grep -q qfwfq >actual &&
626 test_cmp empty actual
629 test_expect_success
'grep -C1 hunk mark between files' '
630 git grep -C1 "^[yz]" >actual &&
631 test_cmp expected actual
634 test_expect_success
'log grep setup' '
637 GIT_AUTHOR_NAME="With * Asterisk" \
638 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
639 git commit -a -m "second" &&
643 git commit -a -m "third" &&
647 GIT_AUTHOR_NAME="Night Fall" \
648 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
649 git commit -a -m "fourth"
652 test_expect_success
'log grep (1)' '
653 git log --author=author --pretty=tformat:%s >actual &&
655 echo third && echo initial
657 test_cmp expect actual
660 test_expect_success
'log grep (2)' '
661 git log --author=" * " -F --pretty=tformat:%s >actual &&
665 test_cmp expect actual
668 test_expect_success
'log grep (3)' '
669 git log --author="^A U" --pretty=tformat:%s >actual &&
671 echo third && echo initial
673 test_cmp expect actual
676 test_expect_success
'log grep (4)' '
677 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
681 test_cmp expect actual
684 test_expect_success
'log grep (5)' '
685 git log --author=Thor -F --pretty=tformat:%s >actual &&
687 echo third && echo initial
689 test_cmp expect actual
692 test_expect_success
'log grep (6)' '
693 git log --author=-0700 --pretty=tformat:%s >actual &&
694 test_must_be_empty actual
697 test_expect_success
'log grep (7)' '
698 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
699 echo third >expect &&
700 test_cmp expect actual
703 test_expect_success
'log grep (8)' '
704 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
706 echo third && echo second
708 test_cmp expect actual
711 test_expect_success
'log grep (9)' '
712 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
713 echo third >expect &&
714 test_cmp expect actual
717 test_expect_success
'log grep (9)' '
718 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
720 test_cmp expect actual
723 test_expect_success
'log --grep-reflog can only be used under -g' '
724 test_must_fail git log --grep-reflog="commit: third"
727 test_expect_success
'log with multiple --grep uses union' '
728 git log --grep=i --grep=r --format=%s >actual &&
730 echo fourth && echo third && echo initial
732 test_cmp expect actual
735 test_expect_success
'log --all-match with multiple --grep uses intersection' '
736 git log --all-match --grep=i --grep=r --format=%s >actual &&
740 test_cmp expect actual
743 test_expect_success
'log with multiple --author uses union' '
744 git log --author="Thor" --author="Aster" --format=%s >actual &&
746 echo third && echo second && echo initial
748 test_cmp expect actual
751 test_expect_success
'log --all-match with multiple --author still uses union' '
752 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
754 echo third && echo second && echo initial
756 test_cmp expect actual
759 test_expect_success
'log --grep --author uses intersection' '
760 # grep matches only third and fourth
761 # author matches only initial and third
762 git log --author="A U Thor" --grep=r --format=%s >actual &&
766 test_cmp expect actual
769 test_expect_success
'log --grep --grep --author takes union of greps and intersects with author' '
770 # grep matches initial and second but not third
771 # author matches only initial and third
772 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
776 test_cmp expect actual
779 test_expect_success
'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
780 # grep matches only initial and third
781 # author matches all but second
782 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
784 echo third && echo initial
786 test_cmp expect actual
789 test_expect_success
'log --grep --author --author takes union of authors and intersects with grep' '
790 # grep matches only initial and third
791 # author matches all but second
792 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
794 echo third && echo initial
796 test_cmp expect actual
799 test_expect_success
'log --all-match --grep --grep --author takes intersection' '
800 # grep matches only third
801 # author matches only initial and third
802 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
806 test_cmp expect actual
809 test_expect_success
'log --author does not search in timestamp' '
811 git log --author="$GIT_AUTHOR_DATE" >actual &&
812 test_cmp expect actual
815 test_expect_success
'log --committer does not search in timestamp' '
817 git log --committer="$GIT_COMMITTER_DATE" >actual &&
818 test_cmp expect actual
821 test_expect_success
'grep with CE_VALID file' '
822 git update-index --assume-unchanged t/t &&
824 test "$(git grep test)" = "t/t:test" &&
825 git update-index --no-assume-unchanged t/t &&
830 hello.c=#include <stdio.h>
834 test_expect_success
'grep -p with userdiff' '
835 git config diff.custom.funcname "^#" &&
836 echo "hello.c diff=custom" >.gitattributes &&
837 git grep -p return >actual &&
838 test_cmp expected actual
842 hello.c=int main(int argc, const char **argv)
846 test_expect_success
'grep -p' '
847 rm -f .gitattributes &&
848 git grep -p return >actual &&
849 test_cmp expected actual
853 hello.c-#include <stdio.h>
855 hello.c=int main(int argc, const char **argv)
857 hello.c- printf("Hello world.\n");
861 test_expect_success
'grep -p -B5' '
862 git grep -p -B5 return >actual &&
863 test_cmp expected actual
867 hello.c=int main(int argc, const char **argv)
869 hello.c- printf("Hello world.\n");
871 hello.c- /* char ?? */
875 test_expect_success
'grep -W' '
876 git grep -W return >actual &&
877 test_cmp expected actual
881 hello.c-#include <assert.h>
882 hello.c:#include <stdio.h>
885 test_expect_success
'grep -W shows no trailing empty lines' '
886 git grep -W stdio >actual &&
887 test_cmp expected actual
890 test_expect_success
'grep -W with userdiff' '
891 test_when_finished "rm -f .gitattributes" &&
892 git config diff.custom.xfuncname "^function .*$" &&
893 echo "hello.ps1 diff=custom" >.gitattributes &&
894 git grep -W echo >function-context-userdiff-actual
897 test_expect_success
' includes preceding comment' '
898 grep "# Say hello" function-context-userdiff-actual
901 test_expect_success
' includes function line' '
902 grep "=function hello" function-context-userdiff-actual
905 test_expect_success
' includes matching line' '
906 grep ": echo" function-context-userdiff-actual
909 test_expect_success
' includes last line of the function' '
910 grep "} # hello" function-context-userdiff-actual
913 for threads
in $
(test_seq
0 10)
915 test_expect_success
"grep --threads=$threads & -c grep.threads=$threads" "
916 git grep --threads=$threads . >actual.$threads &&
917 if test $threads -ge 1
919 test_cmp actual.\$(($threads - 1)) actual.$threads
921 git -c grep.threads=$threads grep . >actual.$threads &&
922 if test $threads -ge 1
924 test_cmp actual.\$(($threads - 1)) actual.$threads
929 test_expect_success
!PTHREADS
,C_LOCALE_OUTPUT
'grep --threads=N or pack.threads=N warns when no pthreads' '
930 git grep --threads=2 Hello hello_world 2>err &&
931 grep ^warning: err >warnings &&
932 test_line_count = 1 warnings &&
933 grep -F "no threads support, ignoring --threads" err &&
934 git -c grep.threads=2 grep Hello hello_world 2>err &&
935 grep ^warning: err >warnings &&
936 test_line_count = 1 warnings &&
937 grep -F "no threads support, ignoring grep.threads" err &&
938 git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
939 grep ^warning: err >warnings &&
940 test_line_count = 2 warnings &&
941 grep -F "no threads support, ignoring --threads" err &&
942 grep -F "no threads support, ignoring grep.threads" err &&
943 git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
944 test_line_count = 0 err
947 test_expect_success
'grep from a subdirectory to search wider area (1)' '
950 cd s && git grep "x x x" ..
954 test_expect_success
'grep from a subdirectory to search wider area (2)' '
958 test_expect_code 1 git grep xxyyzz .. >out &&
964 hello.c:int main(int argc, const char **argv)
967 test_expect_success
'grep -Fi' '
968 git grep -Fi "CHAR *" >actual &&
969 test_cmp expected actual
972 test_expect_success
'outside of git repository' '
974 mkdir -p non/git/sub &&
975 echo hello >non/git/file1 &&
976 echo world >non/git/sub/file2 &&
980 } >non/expect.full &&
981 echo file2:world >non/expect.sub &&
983 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
984 export GIT_CEILING_DIRECTORIES &&
986 test_must_fail git grep o &&
987 git grep --no-index o >../actual.full &&
988 test_cmp ../expect.full ../actual.full &&
990 test_must_fail git grep o &&
991 git grep --no-index o >../../actual.sub &&
992 test_cmp ../../expect.sub ../../actual.sub
995 echo ".*o*" >non/git/.gitignore &&
997 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
998 export GIT_CEILING_DIRECTORIES &&
1000 test_must_fail git grep o &&
1001 git grep --no-index --exclude-standard o >../actual.full &&
1002 test_cmp ../expect.full ../actual.full &&
1005 echo ".gitignore:.*o*" &&
1007 } >../expect.with.ignored &&
1008 git grep --no-index --no-exclude o >../actual.full &&
1009 test_cmp ../expect.with.ignored ../actual.full
1013 test_expect_success
'outside of git repository with fallbackToNoIndex' '
1015 mkdir -p non/git/sub &&
1016 echo hello >non/git/file1 &&
1017 echo world >non/git/sub/file2 &&
1018 cat <<-\EOF >non/expect.full &&
1022 echo file2:world >non/expect.sub &&
1024 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1025 export GIT_CEILING_DIRECTORIES &&
1027 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1028 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
1029 test_cmp ../expect.full ../actual.full &&
1031 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1032 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
1033 test_cmp ../../expect.sub ../../actual.sub
1036 echo ".*o*" >non/git/.gitignore &&
1038 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1039 export GIT_CEILING_DIRECTORIES &&
1041 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1042 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
1043 test_cmp ../expect.full ../actual.full &&
1046 echo ".gitignore:.*o*" &&
1048 } >../expect.with.ignored &&
1049 git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
1050 test_cmp ../expect.with.ignored ../actual.full
1054 test_expect_success
'inside git repository but with --no-index' '
1056 mkdir -p is/git/sub &&
1057 echo hello >is/git/file1 &&
1058 echo world >is/git/sub/file2 &&
1059 echo ".*o*" >is/git/.gitignore &&
1062 echo sub/file2:world
1063 } >is/expect.unignored &&
1065 echo ".gitignore:.*o*" &&
1066 cat is/expect.unignored
1067 } >is/expect.full &&
1068 : >is/expect.empty &&
1069 echo file2:world >is/expect.sub &&
1073 test_must_fail git grep o >../actual.full &&
1074 test_cmp ../expect.empty ../actual.full &&
1076 git grep --untracked o >../actual.unignored &&
1077 test_cmp ../expect.unignored ../actual.unignored &&
1079 git grep --no-index o >../actual.full &&
1080 test_cmp ../expect.full ../actual.full &&
1082 git grep --no-index --exclude-standard o >../actual.unignored &&
1083 test_cmp ../expect.unignored ../actual.unignored &&
1086 test_must_fail git grep o >../../actual.sub &&
1087 test_cmp ../../expect.empty ../../actual.sub &&
1089 git grep --no-index o >../../actual.sub &&
1090 test_cmp ../../expect.sub ../../actual.sub &&
1092 git grep --untracked o >../../actual.sub &&
1093 test_cmp ../../expect.sub ../../actual.sub
1097 test_expect_success
'grep --no-index descends into repos, but not .git' '
1101 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1102 export GIT_CEILING_DIRECTORIES &&
1111 git commit -m foo &&
1112 echo magic >.git/file
1115 cat >expect <<-\EOF &&
1119 git grep -l --no-index magic >actual &&
1120 test_cmp expect actual
1124 test_expect_success
'setup double-dash tests' '
1125 cat >double-dash <<EOF &&
1136 test_expect_success
'grep -- pattern' '
1137 git grep -- "->" >actual &&
1138 test_cmp expected actual
1140 test_expect_success
'grep -- pattern -- pathspec' '
1141 git grep -- "->" -- double-dash >actual &&
1142 test_cmp expected actual
1144 test_expect_success
'grep -e pattern -- path' '
1145 git grep -e "->" -- double-dash >actual &&
1146 test_cmp expected actual
1152 test_expect_success
'grep -e -- -- path' '
1153 git grep -e -- -- double-dash >actual &&
1154 test_cmp expected actual
1157 test_expect_success
'dashdash disambiguates rev as rev' '
1158 test_when_finished "rm -f master" &&
1159 echo content >master &&
1160 echo master:hello.c >expect &&
1161 git grep -l o master -- hello.c >actual &&
1162 test_cmp expect actual
1165 test_expect_success
'dashdash disambiguates pathspec as pathspec' '
1166 test_when_finished "git rm -f master" &&
1167 echo content >master &&
1169 echo master:content >expect &&
1170 git grep o -- master >actual &&
1171 test_cmp expect actual
1174 test_expect_success
'report bogus arg without dashdash' '
1175 test_must_fail git grep o does-not-exist
1178 test_expect_success
'report bogus rev with dashdash' '
1179 test_must_fail git grep o hello.c --
1182 test_expect_success
'allow non-existent path with dashdash' '
1183 # We need a real match so grep exits with success.
1184 tree=$(git ls-tree HEAD |
1185 sed s/hello.c/not-in-working-tree/ |
1187 git grep o "$tree" -- not-in-working-tree
1190 test_expect_success
'grep --no-index pattern -- path' '
1194 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1195 export GIT_CEILING_DIRECTORIES &&
1197 echo hello >hello &&
1198 echo goodbye >goodbye &&
1199 echo hello:hello >expect &&
1200 git grep --no-index o -- hello >actual &&
1201 test_cmp expect actual
1205 test_expect_success
'grep --no-index complains of revs' '
1206 test_must_fail git grep --no-index o master -- 2>err &&
1207 test_i18ngrep "cannot be used with revs" err
1210 test_expect_success
'grep --no-index prefers paths to revs' '
1211 test_when_finished "rm -f master" &&
1212 echo content >master &&
1213 echo master:content >expect &&
1214 git grep --no-index o master >actual &&
1215 test_cmp expect actual
1218 test_expect_success
'grep --no-index does not "diagnose" revs' '
1219 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1220 test_i18ngrep ! -i "did you mean" err
1224 hello.c:int main(int argc, const char **argv)
1225 hello.c: printf("Hello world.\n");
1228 test_expect_success PCRE
'grep --perl-regexp pattern' '
1229 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1230 test_cmp expected actual
1233 test_expect_success
!PCRE
'grep --perl-regexp pattern errors without PCRE' '
1234 test_must_fail git grep --perl-regexp "foo.*bar"
1237 test_expect_success PCRE
'grep -P pattern' '
1238 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1239 test_cmp expected actual
1242 test_expect_success LIBPCRE2
"grep -P with (*NO_JIT) doesn't error out" '
1243 git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1244 test_cmp expected actual
1248 test_expect_success
!PCRE
'grep -P pattern errors without PCRE' '
1249 test_must_fail git grep -P "foo.*bar"
1252 test_expect_success
'grep pattern with grep.extendedRegexp=true' '
1254 test_must_fail git -c grep.extendedregexp=true \
1255 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1256 test_cmp empty actual
1259 test_expect_success PCRE
'grep -P pattern with grep.extendedRegexp=true' '
1260 git -c grep.extendedregexp=true \
1261 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1262 test_cmp expected actual
1265 test_expect_success PCRE
'grep -P -v pattern' '
1270 git grep -P -v "abc" ab >actual &&
1271 test_cmp expected actual
1274 test_expect_success PCRE
'grep -P -i pattern' '
1275 cat >expected <<-EOF &&
1276 hello.c: printf("Hello world.\n");
1278 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1279 test_cmp expected actual
1282 test_expect_success PCRE
'grep -P -w pattern' '
1284 echo "hello_world:Hello world"
1285 echo "hello_world:HeLLo world"
1287 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1288 test_cmp expected actual
1291 test_expect_success PCRE
'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1292 git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1293 test_cmp hello_world actual &&
1294 git grep -P -h "(.)\1" hello_world >actual &&
1295 test_cmp hello_world actual
1298 test_expect_success
'grep -G invalidpattern properly dies ' '
1299 test_must_fail git grep -G "a["
1302 test_expect_success
'grep invalidpattern properly dies with grep.patternType=basic' '
1303 test_must_fail git -c grep.patterntype=basic grep "a["
1306 test_expect_success
'grep -E invalidpattern properly dies ' '
1307 test_must_fail git grep -E "a["
1310 test_expect_success
'grep invalidpattern properly dies with grep.patternType=extended' '
1311 test_must_fail git -c grep.patterntype=extended grep "a["
1314 test_expect_success PCRE
'grep -P invalidpattern properly dies ' '
1315 test_must_fail git grep -P "a["
1318 test_expect_success PCRE
'grep invalidpattern properly dies with grep.patternType=perl' '
1319 test_must_fail git -c grep.patterntype=perl grep "a["
1322 test_expect_success
'grep -G -E -F pattern' '
1323 echo "ab:a+b*c" >expected &&
1324 git grep -G -E -F "a+b*c" ab >actual &&
1325 test_cmp expected actual
1328 test_expect_success
'grep pattern with grep.patternType=basic, =extended, =fixed' '
1329 echo "ab:a+b*c" >expected &&
1331 -c grep.patterntype=basic \
1332 -c grep.patterntype=extended \
1333 -c grep.patterntype=fixed \
1334 grep "a+b*c" ab >actual &&
1335 test_cmp expected actual
1338 test_expect_success
'grep -E -F -G pattern' '
1339 echo "ab:a+bc" >expected &&
1340 git grep -E -F -G "a+b*c" ab >actual &&
1341 test_cmp expected actual
1344 test_expect_success
'grep pattern with grep.patternType=extended, =fixed, =basic' '
1345 echo "ab:a+bc" >expected &&
1347 -c grep.patterntype=extended \
1348 -c grep.patterntype=fixed \
1349 -c grep.patterntype=basic \
1350 grep "a+b*c" ab >actual &&
1351 test_cmp expected actual
1354 test_expect_success
'grep -F -G -E pattern' '
1355 echo "ab:abc" >expected &&
1356 git grep -F -G -E "a+b*c" ab >actual &&
1357 test_cmp expected actual
1360 test_expect_success
'grep pattern with grep.patternType=fixed, =basic, =extended' '
1361 echo "ab:abc" >expected &&
1363 -c grep.patterntype=fixed \
1364 -c grep.patterntype=basic \
1365 -c grep.patterntype=extended \
1366 grep "a+b*c" ab >actual &&
1367 test_cmp expected actual
1370 test_expect_success
'grep -G -F -P -E pattern' '
1371 echo "d0:d" >expected &&
1372 git grep -G -F -P -E "[\d]" d0 >actual &&
1373 test_cmp expected actual
1376 test_expect_success
'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1377 echo "d0:d" >expected &&
1379 -c grep.patterntype=fixed \
1380 -c grep.patterntype=basic \
1381 -c grep.patterntype=perl \
1382 -c grep.patterntype=extended \
1383 grep "[\d]" d0 >actual &&
1384 test_cmp expected actual
1387 test_expect_success PCRE
'grep -G -F -E -P pattern' '
1388 echo "d0:0" >expected &&
1389 git grep -G -F -E -P "[\d]" d0 >actual &&
1390 test_cmp expected actual
1393 test_expect_success PCRE
'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1394 echo "d0:0" >expected &&
1396 -c grep.patterntype=fixed \
1397 -c grep.patterntype=basic \
1398 -c grep.patterntype=extended \
1399 -c grep.patterntype=perl \
1400 grep "[\d]" d0 >actual &&
1401 test_cmp expected actual
1404 test_expect_success PCRE
'grep -P pattern with grep.patternType=fixed' '
1405 echo "ab:a+b*c" >expected &&
1407 -c grep.patterntype=fixed \
1408 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1409 test_cmp expected actual
1412 test_expect_success
'grep -F pattern with grep.patternType=basic' '
1413 echo "ab:a+b*c" >expected &&
1415 -c grep.patterntype=basic \
1416 grep -F "*c" ab >actual &&
1417 test_cmp expected actual
1420 test_expect_success
'grep -G pattern with grep.patternType=fixed' '
1426 -c grep.patterntype=fixed \
1427 grep -G "a+b" ab >actual &&
1428 test_cmp expected actual
1431 test_expect_success
'grep -E pattern with grep.patternType=fixed' '
1438 -c grep.patterntype=fixed \
1439 grep -E "a+" ab >actual &&
1440 test_cmp expected actual
1444 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1445 hello.c<RED>-<RESET>{
1447 hello.c<RED>:<RESET> /* char ?? */
1448 hello.c<RED>-<RESET>}
1450 hello_world<RED>:<RESET>Hello_world
1451 hello_world<RED>-<RESET>HeLLo_world
1454 test_expect_success
'grep --color, separator' '
1455 test_config color.grep.context normal &&
1456 test_config color.grep.filename normal &&
1457 test_config color.grep.function normal &&
1458 test_config color.grep.linenumber normal &&
1459 test_config color.grep.match normal &&
1460 test_config color.grep.selected normal &&
1461 test_config color.grep.separator red &&
1463 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1464 test_decode_color >actual &&
1465 test_cmp expected actual
1469 hello.c:int main(int argc, const char **argv)
1470 hello.c: /* char ?? */
1472 hello_world:Hello_world
1475 test_expect_success
'grep --break' '
1476 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1477 test_cmp expected actual
1481 hello.c:int main(int argc, const char **argv)
1484 hello.c: /* char ?? */
1487 hello_world:Hello_world
1488 hello_world-HeLLo_world
1491 test_expect_success
'grep --break with context' '
1492 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1493 test_cmp expected actual
1498 int main(int argc, const char **argv)
1504 test_expect_success
'grep --heading' '
1505 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1506 test_cmp expected actual
1510 <BOLD;GREEN>hello.c<RESET>
1511 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1512 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1514 <BOLD;GREEN>hello_world<RESET>
1515 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1518 test_expect_success
'mimic ack-grep --group' '
1519 test_config color.grep.context normal &&
1520 test_config color.grep.filename "bold green" &&
1521 test_config color.grep.function normal &&
1522 test_config color.grep.linenumber normal &&
1523 test_config color.grep.match "black yellow" &&
1524 test_config color.grep.selected normal &&
1525 test_config color.grep.separator normal &&
1527 git grep --break --heading -n --color \
1528 -e char -e lo_w hello.c hello_world |
1529 test_decode_color >actual &&
1530 test_cmp expected actual
1534 space: line with leading space1
1535 space: line with leading space2
1536 space: line with leading space3
1539 test_expect_success PCRE
'grep -E "^ "' '
1540 git grep -E "^ " space >actual &&
1541 test_cmp expected actual
1544 test_expect_success PCRE
'grep -P "^ "' '
1545 git grep -P "^ " space >actual &&
1546 test_cmp expected actual
1550 space-line without leading space1
1551 space: line <RED>with <RESET>leading space1
1552 space: line <RED>with <RESET>leading <RED>space2<RESET>
1553 space: line <RED>with <RESET>leading space3
1554 space:line without leading <RED>space2<RESET>
1557 test_expect_success
'grep --color -e A -e B with context' '
1558 test_config color.grep.context normal &&
1559 test_config color.grep.filename normal &&
1560 test_config color.grep.function normal &&
1561 test_config color.grep.linenumber normal &&
1562 test_config color.grep.matchContext normal &&
1563 test_config color.grep.matchSelected red &&
1564 test_config color.grep.selected normal &&
1565 test_config color.grep.separator normal &&
1567 git grep --color=always -C2 -e "with " -e space2 space |
1568 test_decode_color >actual &&
1569 test_cmp expected actual
1573 space-line without leading space1
1574 space- line with leading space1
1575 space: line <RED>with <RESET>leading <RED>space2<RESET>
1576 space- line with leading space3
1577 space-line without leading space2
1580 test_expect_success
'grep --color -e A --and -e B with context' '
1581 test_config color.grep.context normal &&
1582 test_config color.grep.filename normal &&
1583 test_config color.grep.function normal &&
1584 test_config color.grep.linenumber normal &&
1585 test_config color.grep.matchContext normal &&
1586 test_config color.grep.matchSelected red &&
1587 test_config color.grep.selected normal &&
1588 test_config color.grep.separator normal &&
1590 git grep --color=always -C2 -e "with " --and -e space2 space |
1591 test_decode_color >actual &&
1592 test_cmp expected actual
1596 space-line without leading space1
1597 space: line <RED>with <RESET>leading space1
1598 space- line with leading space2
1599 space: line <RED>with <RESET>leading space3
1600 space-line without leading space2
1603 test_expect_success
'grep --color -e A --and --not -e B with context' '
1604 test_config color.grep.context normal &&
1605 test_config color.grep.filename normal &&
1606 test_config color.grep.function normal &&
1607 test_config color.grep.linenumber normal &&
1608 test_config color.grep.matchContext normal &&
1609 test_config color.grep.matchSelected red &&
1610 test_config color.grep.selected normal &&
1611 test_config color.grep.separator normal &&
1613 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1614 test_decode_color >actual &&
1615 test_cmp expected actual
1620 hello.c=int main(int argc, const char **argv)
1622 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1624 hello.c- /* char ?? */
1628 test_expect_success
'grep --color -e A --and -e B -p with context' '
1629 test_config color.grep.context normal &&
1630 test_config color.grep.filename normal &&
1631 test_config color.grep.function normal &&
1632 test_config color.grep.linenumber normal &&
1633 test_config color.grep.matchContext normal &&
1634 test_config color.grep.matchSelected red &&
1635 test_config color.grep.selected normal &&
1636 test_config color.grep.separator normal &&
1638 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1639 test_decode_color >actual &&
1640 test_cmp expected actual
1643 test_expect_success
'grep can find things only in the work tree' '
1644 : >work-tree-only &&
1645 git add work-tree-only &&
1646 test_when_finished "git rm -f work-tree-only" &&
1647 echo "find in work tree" >work-tree-only &&
1648 git grep --quiet "find in work tree" &&
1649 test_must_fail git grep --quiet --cached "find in work tree" &&
1650 test_must_fail git grep --quiet "find in work tree" HEAD
1653 test_expect_success
'grep can find things only in the work tree (i-t-a)' '
1654 echo "intend to add this" >intend-to-add &&
1655 git add -N intend-to-add &&
1656 test_when_finished "git rm -f intend-to-add" &&
1657 git grep --quiet "intend to add this" &&
1658 test_must_fail git grep --quiet --cached "intend to add this" &&
1659 test_must_fail git grep --quiet "intend to add this" HEAD
1662 test_expect_success
'grep does not search work tree with assume unchanged' '
1663 echo "intend to add this" >intend-to-add &&
1664 git add -N intend-to-add &&
1665 git update-index --assume-unchanged intend-to-add &&
1666 test_when_finished "git rm -f intend-to-add" &&
1667 test_must_fail git grep --quiet "intend to add this" &&
1668 test_must_fail git grep --quiet --cached "intend to add this" &&
1669 test_must_fail git grep --quiet "intend to add this" HEAD
1672 test_expect_success
'grep can find things only in the index' '
1673 echo "only in the index" >cache-this &&
1674 git add cache-this &&
1676 test_when_finished "git rm --cached cache-this" &&
1677 test_must_fail git grep --quiet "only in the index" &&
1678 git grep --quiet --cached "only in the index" &&
1679 test_must_fail git grep --quiet "only in the index" HEAD
1682 test_expect_success
'grep does not report i-t-a with -L --cached' '
1683 echo "intend to add this" >intend-to-add &&
1684 git add -N intend-to-add &&
1685 test_when_finished "git rm -f intend-to-add" &&
1686 git ls-files | grep -v "^intend-to-add\$" >expected &&
1687 git grep -L --cached "nonexistent_string" >actual &&
1688 test_cmp expected actual
1691 test_expect_success
'grep does not report i-t-a and assume unchanged with -L' '
1692 echo "intend to add this" >intend-to-add-assume-unchanged &&
1693 git add -N intend-to-add-assume-unchanged &&
1694 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1695 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1696 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1697 git grep -L "nonexistent_string" >actual &&
1698 test_cmp expected actual