3 # Copyright (c) 2006 Junio C Hamano
6 test_description
='git grep various.
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14 test_invalid_grep_expression
() {
16 test_expect_success
"invalid expression: grep $params" '
17 test_must_fail git grep $params -- nonexisting
25 int main(int argc, const char **argv)
27 printf("Hello world.\n");
33 test_expect_success setup
'
37 echo foo_mmap bar mmap
38 echo foo mmap bar_mmap
39 echo foo_mmap bar mmap baz
67 echo "line without leading space1"
68 echo " line with leading space1"
69 echo " line with leading space2"
70 echo " line with leading space3"
71 echo "line without leading space2"
73 cat >hello.ps1 <<-\EOF &&
85 if test_have_prereq FUNNYNAMES
87 echo unusual >"\"unusual\" pathname" &&
88 echo unusual >"t/nested \"unusual\" pathname"
95 test_expect_success
'grep should not segfault with a bad input' '
96 test_must_fail git grep "("
99 test_invalid_grep_expression
--and -e A
104 HEAD
) HC
='HEAD:' L
='HEAD' ;;
105 '') HC
= L
='in working tree' ;;
108 test_expect_success
"grep -w $L" '
110 echo ${HC}file:1:foo mmap bar
111 echo ${HC}file:3:foo_mmap bar mmap
112 echo ${HC}file:4:foo mmap bar_mmap
113 echo ${HC}file:5:foo_mmap bar mmap baz
115 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
116 test_cmp expected actual
119 test_expect_success
"grep -w $L (with --column)" '
121 echo ${HC}file:5:foo mmap bar
122 echo ${HC}file:14:foo_mmap bar mmap
123 echo ${HC}file:5:foo mmap bar_mmap
124 echo ${HC}file:14:foo_mmap bar mmap baz
126 git grep --column -w -e mmap $H >actual &&
127 test_cmp expected actual
130 test_expect_success
"grep -w $L (with --column, extended OR)" '
132 echo ${HC}file:14:foo_mmap bar mmap
133 echo ${HC}file:19:foo_mmap bar mmap baz
135 git grep --column -w -e mmap$ --or -e baz $H >actual &&
136 test_cmp expected actual
139 test_expect_success
"grep -w $L (with --column, --invert-match)" '
141 echo ${HC}file:1:foo mmap bar
142 echo ${HC}file:1:foo_mmap bar
143 echo ${HC}file:1:foo_mmap bar mmap
144 echo ${HC}file:1:foo mmap bar_mmap
146 git grep --column --invert-match -w -e baz $H -- file >actual &&
147 test_cmp expected actual
150 test_expect_success
"grep $L (with --column, --invert-match, extended OR)" '
152 echo ${HC}hello_world:6:HeLLo_world
154 git grep --column --invert-match -e ll --or --not -e _ $H -- hello_world \
156 test_cmp expected actual
159 test_expect_success
"grep $L (with --column, --invert-match, extended AND)" '
161 echo ${HC}hello_world:3:Hello world
162 echo ${HC}hello_world:3:Hello_world
163 echo ${HC}hello_world:6:HeLLo_world
165 git grep --column --invert-match --not -e _ --and --not -e ll $H -- hello_world \
167 test_cmp expected actual
170 test_expect_success
"grep $L (with --column, double-negation)" '
172 echo ${HC}file:1:foo_mmap bar mmap baz
174 git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
176 test_cmp expected actual
179 test_expect_success
"grep -w $L (with --column, -C)" '
181 echo ${HC}file:5:foo mmap bar
182 echo ${HC}file-foo_mmap bar
183 echo ${HC}file:14:foo_mmap bar mmap
184 echo ${HC}file:5:foo mmap bar_mmap
185 echo ${HC}file:14:foo_mmap bar mmap baz
187 git grep --column -w -C1 -e mmap $H >actual &&
188 test_cmp expected actual
191 test_expect_success
"grep -w $L (with --line-number, --column)" '
193 echo ${HC}file:1:5:foo mmap bar
194 echo ${HC}file:3:14:foo_mmap bar mmap
195 echo ${HC}file:4:5:foo mmap bar_mmap
196 echo ${HC}file:5:14:foo_mmap bar mmap baz
198 git grep -n --column -w -e mmap $H >actual &&
199 test_cmp expected actual
202 test_expect_success
"grep -w $L (with non-extended patterns, --column)" '
204 echo ${HC}file:5:foo mmap bar
205 echo ${HC}file:10:foo_mmap bar
206 echo ${HC}file:10:foo_mmap bar mmap
207 echo ${HC}file:5:foo mmap bar_mmap
208 echo ${HC}file:10:foo_mmap bar mmap baz
210 git grep --column -w -e bar -e mmap $H >actual &&
211 test_cmp expected actual
214 test_expect_success
"grep -w $L" '
216 echo ${HC}file:1:foo mmap bar
217 echo ${HC}file:3:foo_mmap bar mmap
218 echo ${HC}file:4:foo mmap bar_mmap
219 echo ${HC}file:5:foo_mmap bar mmap baz
221 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
222 test_cmp expected actual
225 test_expect_success
"grep -w $L" '
227 echo ${HC}file:foo mmap bar
228 echo ${HC}file:foo_mmap bar mmap
229 echo ${HC}file:foo mmap bar_mmap
230 echo ${HC}file:foo_mmap bar mmap baz
232 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
233 test_cmp expected actual
236 test_expect_success
"grep -w $L (w)" '
237 test_must_fail git grep -n -w -e "^w" $H >actual &&
238 test_must_be_empty actual
241 test_expect_success
"grep -w $L (x)" '
243 echo ${HC}x:1:x x xx x
245 git grep -n -w -e "x xx* x" $H >actual &&
246 test_cmp expected actual
249 test_expect_success
"grep -w $L (y-1)" '
253 git grep -n -w -e "^y" $H >actual &&
254 test_cmp expected actual
257 test_expect_success
"grep -w $L (y-2)" '
258 if git grep -n -w -e "^y y" $H >actual
260 echo should not have matched
264 test_must_be_empty actual
268 test_expect_success
"grep -w $L (z)" '
269 if git grep -n -w -e "^z" $H >actual
271 echo should not have matched
275 test_must_be_empty actual
279 test_expect_success
"grep $L (with --column, --only-matching)" '
281 echo ${HC}file:1:5:mmap
282 echo ${HC}file:2:5:mmap
283 echo ${HC}file:3:5:mmap
284 echo ${HC}file:3:13:mmap
285 echo ${HC}file:4:5:mmap
286 echo ${HC}file:4:13:mmap
287 echo ${HC}file:5:5:mmap
288 echo ${HC}file:5:13:mmap
290 git grep --column -n -o -e mmap $H >actual &&
291 test_cmp expected actual
294 test_expect_success
"grep $L (t-1)" '
295 echo "${HC}t/t:1:test" >expected &&
296 git grep -n -e test $H >actual &&
297 test_cmp expected actual
300 test_expect_success
"grep $L (t-2)" '
301 echo "${HC}t:1:test" >expected &&
304 git grep -n -e test $H
306 test_cmp expected actual
309 test_expect_success
"grep $L (t-3)" '
310 echo "${HC}t/t:1:test" >expected &&
313 git grep --full-name -n -e test $H
315 test_cmp expected actual
318 test_expect_success
"grep -c $L (no /dev/null)" '
319 ! git grep -c test $H | grep /dev/null
322 test_expect_success
"grep --max-depth -1 $L" '
324 echo ${HC}t/a/v:1:vvv
328 git grep --max-depth -1 -n -e vvv $H >actual &&
329 test_cmp expected actual &&
330 git grep --recursive -n -e vvv $H >actual &&
331 test_cmp expected actual
334 test_expect_success
"grep --max-depth 0 $L" '
338 git grep --max-depth 0 -n -e vvv $H >actual &&
339 test_cmp expected actual &&
340 git grep --no-recursive -n -e vvv $H >actual &&
341 test_cmp expected actual
344 test_expect_success
"grep --max-depth 0 -- '*' $L" '
346 echo ${HC}t/a/v:1:vvv
350 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
351 test_cmp expected actual &&
352 git grep --no-recursive -n -e vvv $H -- "*" >actual &&
353 test_cmp expected actual
356 test_expect_success
"grep --max-depth 1 $L" '
361 git grep --max-depth 1 -n -e vvv $H >actual &&
362 test_cmp expected actual
365 test_expect_success
"grep --max-depth 0 -- t $L" '
369 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
370 test_cmp expected actual &&
371 git grep --no-recursive -n -e vvv $H -- t >actual &&
372 test_cmp expected actual
375 test_expect_success
"grep --max-depth 0 -- . t $L" '
380 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
381 test_cmp expected actual &&
382 git grep --no-recursive -n -e vvv $H -- . t >actual &&
383 test_cmp expected actual
386 test_expect_success
"grep --max-depth 0 -- t . $L" '
391 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
392 test_cmp expected actual &&
393 git grep --no-recursive -n -e vvv $H -- t . >actual &&
394 test_cmp expected actual
396 test_expect_success
"grep $L with grep.extendedRegexp=false" '
397 echo "${HC}ab:a+bc" >expected &&
398 git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
399 test_cmp expected actual
402 test_expect_success
"grep $L with grep.extendedRegexp=true" '
403 echo "${HC}ab:abc" >expected &&
404 git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
405 test_cmp expected actual
408 test_expect_success
"grep $L with grep.patterntype=basic" '
409 echo "${HC}ab:a+bc" >expected &&
410 git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
411 test_cmp expected actual
414 test_expect_success
"grep $L with grep.patterntype=extended" '
415 echo "${HC}ab:abc" >expected &&
416 git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
417 test_cmp expected actual
420 test_expect_success
"grep $L with grep.patterntype=fixed" '
421 echo "${HC}ab:a+b*c" >expected &&
422 git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
423 test_cmp expected actual
426 test_expect_success PCRE
"grep $L with grep.patterntype=perl" '
427 echo "${HC}ab:a+b*c" >expected &&
428 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
429 test_cmp expected actual
432 test_expect_success
!FAIL_PREREQS
,!PCRE
"grep $L with grep.patterntype=perl errors without PCRE" '
433 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
436 test_expect_success
"grep $L with grep.patternType=default and grep.extendedRegexp=true" '
437 echo "${HC}ab:abc" >expected &&
439 -c grep.patternType=default \
440 -c grep.extendedRegexp=true \
441 grep "a+b*c" $H ab >actual &&
442 test_cmp expected actual
445 test_expect_success
"grep $L with grep.extendedRegexp=true and grep.patternType=default" '
446 echo "${HC}ab:abc" >expected &&
448 -c grep.extendedRegexp=true \
449 -c grep.patternType=default \
450 grep "a+b*c" $H ab >actual &&
451 test_cmp expected actual
454 test_expect_success
"grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
455 echo "${HC}ab:abc" >expected &&
457 -c grep.patternType=extended \
458 -c grep.extendedRegexp=false \
459 grep "a+b*c" $H ab >actual &&
460 test_cmp expected actual
463 test_expect_success
"grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
464 echo "${HC}ab:a+bc" >expected &&
466 -c grep.patternType=basic \
467 -c grep.extendedRegexp=true \
468 grep "a+b*c" $H ab >actual &&
469 test_cmp expected actual
472 test_expect_success
"grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
473 echo "${HC}ab:abc" >expected &&
475 -c grep.extendedRegexp=false \
476 -c grep.patternType=extended \
477 grep "a+b*c" $H ab >actual &&
478 test_cmp expected actual
481 test_expect_success
"grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
482 echo "${HC}ab:a+bc" >expected &&
484 -c grep.extendedRegexp=true \
485 -c grep.patternType=basic \
486 grep "a+b*c" $H ab >actual &&
487 test_cmp expected actual
490 test_expect_success
"grep --count $L" '
491 echo ${HC}ab:3 >expected &&
492 git grep --count -e b $H -- ab >actual &&
493 test_cmp expected actual
496 test_expect_success
"grep --count -h $L" '
498 git grep --count -h -e b $H -- ab >actual &&
499 test_cmp expected actual
502 test_expect_success FUNNYNAMES
"grep $L should quote unusual pathnames" '
503 cat >expected <<-EOF &&
504 ${HC}"\"unusual\" pathname":unusual
505 ${HC}"t/nested \"unusual\" pathname":unusual
507 git grep unusual $H >actual &&
508 test_cmp expected actual
511 test_expect_success FUNNYNAMES
"grep $L in subdir should quote unusual relative pathnames" '
512 cat >expected <<-EOF &&
513 ${HC}"nested \"unusual\" pathname":unusual
519 test_cmp expected actual
522 test_expect_success FUNNYNAMES
"grep -z $L with unusual pathnames" '
523 cat >expected <<-EOF &&
524 ${HC}"unusual" pathname:unusual
525 ${HC}t/nested "unusual" pathname:unusual
527 git grep -z unusual $H >actual &&
528 tr "\0" ":" <actual >actual-replace-null &&
529 test_cmp expected actual-replace-null
532 test_expect_success FUNNYNAMES
"grep -z $L in subdir with unusual relative pathnames" '
533 cat >expected <<-EOF &&
534 ${HC}nested "unusual" pathname:unusual
538 git grep -z unusual $H
540 tr "\0" ":" <actual >actual-replace-null &&
541 test_cmp expected actual-replace-null
548 test_expect_success
'grep -l -C' '
549 git grep -l -C1 foo >actual &&
550 test_cmp expected actual
556 test_expect_success
'grep -c -C' '
557 git grep -c -C1 foo >actual &&
558 test_cmp expected actual
561 test_expect_success
'grep -L -C' '
562 git ls-files >expected &&
563 git grep -L -C1 nonexistent_string >actual &&
564 test_cmp expected actual
567 test_expect_success
'grep --files-without-match --quiet' '
568 git grep --files-without-match --quiet nonexistent_string >actual &&
569 test_must_be_empty actual
573 file:foo mmap bar_mmap
576 test_expect_success
'grep -e A --and -e B' '
577 git grep -e "foo mmap" --and -e bar_mmap >actual &&
578 test_cmp expected actual
582 file:foo_mmap bar mmap
583 file:foo_mmap bar mmap baz
587 test_expect_success
'grep ( -e A --or -e B ) --and -e B' '
588 git grep \( -e foo_ --or -e baz \) \
589 --and -e " mmap" >actual &&
590 test_cmp expected actual
597 test_expect_success
'grep -e A --and --not -e B' '
598 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
599 test_cmp expected actual
602 test_expect_success
'grep should ignore GREP_OPTIONS' '
603 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
604 test_cmp expected actual
607 test_expect_success
'grep -f, non-existent file' '
608 test_must_fail git grep -f patterns
614 file:foo_mmap bar mmap
615 file:foo mmap bar_mmap
616 file:foo_mmap bar mmap baz
623 test_expect_success
'grep -f, one pattern' '
624 git grep -f pattern >actual &&
625 test_cmp expected actual
631 file:foo_mmap bar mmap
632 file:foo mmap bar_mmap
633 file:foo_mmap bar mmap baz
644 test_expect_success
'grep -f, multiple patterns' '
645 git grep -f patterns >actual &&
646 test_cmp expected actual
649 test_expect_success
'grep, multiple patterns' '
650 git grep "$(cat patterns)" >actual &&
651 test_cmp expected actual
657 file:foo_mmap bar mmap
658 file:foo mmap bar_mmap
659 file:foo_mmap bar mmap baz
673 test_expect_success
'grep -f, ignore empty lines' '
674 git grep -f patterns >actual &&
675 test_cmp expected actual
678 test_expect_success
'grep -f, ignore empty lines, read patterns from stdin' '
679 git grep -f - <patterns >actual &&
680 test_cmp expected actual
689 test_expect_success
'grep -q, silently report matches' '
690 git grep -q mmap >actual &&
691 test_must_be_empty actual &&
692 test_must_fail git grep -q qfwfq >actual &&
693 test_must_be_empty actual
696 test_expect_success
'grep -C1 hunk mark between files' '
697 git grep -C1 "^[yz]" >actual &&
698 test_cmp expected actual
701 test_expect_success
'log grep setup' '
702 test_commit --append --author "With * Asterisk <xyzzy@frotz.com>" second file a &&
703 test_commit --append third file a &&
704 test_commit --append --author "Night Fall <nitfol@frobozz.com>" fourth file a
707 test_expect_success
'log grep (1)' '
708 git log --author=author --pretty=tformat:%s >actual &&
710 echo third && echo initial
712 test_cmp expect actual
715 test_expect_success
'log grep (2)' '
716 git log --author=" * " -F --pretty=tformat:%s >actual &&
720 test_cmp expect actual
723 test_expect_success
'log grep (3)' '
724 git log --author="^A U" --pretty=tformat:%s >actual &&
726 echo third && echo initial
728 test_cmp expect actual
731 test_expect_success
'log grep (4)' '
732 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
736 test_cmp expect actual
739 test_expect_success
'log grep (5)' '
740 git log --author=Thor -F --pretty=tformat:%s >actual &&
742 echo third && echo initial
744 test_cmp expect actual
747 test_expect_success
'log grep (6)' '
748 git log --author=-0700 --pretty=tformat:%s >actual &&
749 test_must_be_empty actual
752 test_expect_success
'log grep (7)' '
753 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
754 echo third >expect &&
755 test_cmp expect actual
758 test_expect_success
'log grep (8)' '
759 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
761 echo third && echo second
763 test_cmp expect actual
766 test_expect_success
'log grep (9)' '
767 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
768 echo third >expect &&
769 test_cmp expect actual
772 test_expect_success
'log grep (9)' '
773 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
774 test_must_be_empty actual
777 test_expect_success
'log --grep-reflog can only be used under -g' '
778 test_must_fail git log --grep-reflog="commit: third"
781 test_expect_success
'log with multiple --grep uses union' '
782 git log --grep=i --grep=r --format=%s >actual &&
784 echo fourth && echo third && echo initial
786 test_cmp expect actual
789 test_expect_success
'log --all-match with multiple --grep uses intersection' '
790 git log --all-match --grep=i --grep=r --format=%s >actual &&
794 test_cmp expect actual
797 test_expect_success
'log with multiple --author uses union' '
798 git log --author="Thor" --author="Aster" --format=%s >actual &&
800 echo third && echo second && echo initial
802 test_cmp expect actual
805 test_expect_success
'log --all-match with multiple --author still uses union' '
806 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
808 echo third && echo second && echo initial
810 test_cmp expect actual
813 test_expect_success
'log --grep --author uses intersection' '
814 # grep matches only third and fourth
815 # author matches only initial and third
816 git log --author="A U Thor" --grep=r --format=%s >actual &&
820 test_cmp expect actual
823 test_expect_success
'log --grep --grep --author takes union of greps and intersects with author' '
824 # grep matches initial and second but not third
825 # author matches only initial and third
826 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
830 test_cmp expect actual
833 test_expect_success
'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
834 # grep matches only initial and third
835 # author matches all but second
836 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
838 echo third && echo initial
840 test_cmp expect actual
843 test_expect_success
'log --grep --author --author takes union of authors and intersects with grep' '
844 # grep matches only initial and third
845 # author matches all but second
846 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
848 echo third && echo initial
850 test_cmp expect actual
853 test_expect_success
'log --all-match --grep --grep --author takes intersection' '
854 # grep matches only third
855 # author matches only initial and third
856 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
860 test_cmp expect actual
863 test_expect_success
'log --author does not search in timestamp' '
864 git log --author="$GIT_AUTHOR_DATE" >actual &&
865 test_must_be_empty actual
868 test_expect_success
'log --committer does not search in timestamp' '
869 git log --committer="$GIT_COMMITTER_DATE" >actual &&
870 test_must_be_empty actual
873 test_expect_success
'grep with CE_VALID file' '
874 git update-index --assume-unchanged t/t &&
876 test "$(git grep test)" = "t/t:test" &&
877 git update-index --no-assume-unchanged t/t &&
882 hello.c=#include <stdio.h>
886 test_expect_success
'grep -p with userdiff' '
887 git config diff.custom.funcname "^#" &&
888 echo "hello.c diff=custom" >.gitattributes &&
889 git grep -p return >actual &&
890 test_cmp expected actual
894 hello.c=int main(int argc, const char **argv)
898 test_expect_success
'grep -p' '
899 rm -f .gitattributes &&
900 git grep -p return >actual &&
901 test_cmp expected actual
905 hello.c-#include <stdio.h>
907 hello.c=int main(int argc, const char **argv)
909 hello.c- printf("Hello world.\n");
913 test_expect_success
'grep -p -B5' '
914 git grep -p -B5 return >actual &&
915 test_cmp expected actual
919 hello.c=int main(int argc, const char **argv)
921 hello.c- printf("Hello world.\n");
923 hello.c- /* char ?? */
927 test_expect_success
'grep -W' '
928 git grep -W return >actual &&
929 test_cmp expected actual
933 hello.c-#include <assert.h>
934 hello.c:#include <stdio.h>
937 test_expect_success
'grep -W shows no trailing empty lines' '
938 git grep -W stdio >actual &&
939 test_cmp expected actual
942 test_expect_success
'grep -W with userdiff' '
943 test_when_finished "rm -f .gitattributes" &&
944 git config diff.custom.xfuncname "^function .*$" &&
945 echo "hello.ps1 diff=custom" >.gitattributes &&
946 git grep -W echo >function-context-userdiff-actual
949 test_expect_success
' includes preceding comment' '
950 grep "# Say hello" function-context-userdiff-actual
953 test_expect_success
' includes function line' '
954 grep "=function hello" function-context-userdiff-actual
957 test_expect_success
' includes matching line' '
958 grep ": echo" function-context-userdiff-actual
961 test_expect_success
' includes last line of the function' '
962 grep "} # hello" function-context-userdiff-actual
965 for threads
in $
(test_seq
0 10)
967 test_expect_success
"grep --threads=$threads & -c grep.threads=$threads" "
968 git grep --threads=$threads . >actual.$threads &&
969 if test $threads -ge 1
971 test_cmp actual.\$(($threads - 1)) actual.$threads
973 git -c grep.threads=$threads grep . >actual.$threads &&
974 if test $threads -ge 1
976 test_cmp actual.\$(($threads - 1)) actual.$threads
981 test_expect_success
!PTHREADS
,!FAIL_PREREQS \
982 'grep --threads=N or pack.threads=N warns when no pthreads' '
983 git grep --threads=2 Hello hello_world 2>err &&
984 grep ^warning: err >warnings &&
985 test_line_count = 1 warnings &&
986 grep -F "no threads support, ignoring --threads" err &&
987 git -c grep.threads=2 grep Hello hello_world 2>err &&
988 grep ^warning: err >warnings &&
989 test_line_count = 1 warnings &&
990 grep -F "no threads support, ignoring grep.threads" err &&
991 git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
992 grep ^warning: err >warnings &&
993 test_line_count = 2 warnings &&
994 grep -F "no threads support, ignoring --threads" err &&
995 grep -F "no threads support, ignoring grep.threads" err &&
996 git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
997 test_line_count = 0 err
1000 test_expect_success
'grep from a subdirectory to search wider area (1)' '
1003 cd s && git grep "x x x" ..
1007 test_expect_success
'grep from a subdirectory to search wider area (2)' '
1011 test_expect_code 1 git grep xxyyzz .. >out &&
1012 test_must_be_empty out
1017 hello.c:int main(int argc, const char **argv)
1020 test_expect_success
'grep -Fi' '
1021 git grep -Fi "CHAR *" >actual &&
1022 test_cmp expected actual
1025 test_expect_success
'outside of git repository' '
1027 mkdir -p non/git/sub &&
1028 echo hello >non/git/file1 &&
1029 echo world >non/git/sub/file2 &&
1032 echo sub/file2:world
1033 } >non/expect.full &&
1034 echo file2:world >non/expect.sub &&
1036 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1037 export GIT_CEILING_DIRECTORIES &&
1039 test_must_fail git grep o &&
1040 git grep --no-index o >../actual.full &&
1041 test_cmp ../expect.full ../actual.full &&
1043 test_must_fail git grep o &&
1044 git grep --no-index o >../../actual.sub &&
1045 test_cmp ../../expect.sub ../../actual.sub
1048 echo ".*o*" >non/git/.gitignore &&
1050 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1051 export GIT_CEILING_DIRECTORIES &&
1053 test_must_fail git grep o &&
1054 git grep --no-index --exclude-standard o >../actual.full &&
1055 test_cmp ../expect.full ../actual.full &&
1058 echo ".gitignore:.*o*" &&
1060 } >../expect.with.ignored &&
1061 git grep --no-index --no-exclude-standard o >../actual.full &&
1062 test_cmp ../expect.with.ignored ../actual.full
1066 test_expect_success
'outside of git repository with fallbackToNoIndex' '
1068 mkdir -p non/git/sub &&
1069 echo hello >non/git/file1 &&
1070 echo world >non/git/sub/file2 &&
1071 cat <<-\EOF >non/expect.full &&
1075 echo file2:world >non/expect.sub &&
1077 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1078 export GIT_CEILING_DIRECTORIES &&
1080 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1081 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
1082 test_cmp ../expect.full ../actual.full &&
1084 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1085 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
1086 test_cmp ../../expect.sub ../../actual.sub
1089 echo ".*o*" >non/git/.gitignore &&
1091 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1092 export GIT_CEILING_DIRECTORIES &&
1094 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1095 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
1096 test_cmp ../expect.full ../actual.full &&
1099 echo ".gitignore:.*o*" &&
1101 } >../expect.with.ignored &&
1102 git -c grep.fallbackToNoIndex grep --no-exclude-standard o >../actual.full &&
1103 test_cmp ../expect.with.ignored ../actual.full
1107 test_expect_success
'inside git repository but with --no-index' '
1109 mkdir -p is/git/sub &&
1110 echo hello >is/git/file1 &&
1111 echo world >is/git/sub/file2 &&
1112 echo ".*o*" >is/git/.gitignore &&
1115 echo sub/file2:world
1116 } >is/expect.unignored &&
1118 echo ".gitignore:.*o*" &&
1119 cat is/expect.unignored
1120 } >is/expect.full &&
1121 echo file2:world >is/expect.sub &&
1125 test_must_fail git grep o >../actual.full &&
1126 test_must_be_empty ../actual.full &&
1128 git grep --untracked o >../actual.unignored &&
1129 test_cmp ../expect.unignored ../actual.unignored &&
1131 git grep --no-index o >../actual.full &&
1132 test_cmp ../expect.full ../actual.full &&
1134 git grep --no-index --exclude-standard o >../actual.unignored &&
1135 test_cmp ../expect.unignored ../actual.unignored &&
1138 test_must_fail git grep o >../../actual.sub &&
1139 test_must_be_empty ../../actual.sub &&
1141 git grep --no-index o >../../actual.sub &&
1142 test_cmp ../../expect.sub ../../actual.sub &&
1144 git grep --untracked o >../../actual.sub &&
1145 test_cmp ../../expect.sub ../../actual.sub
1149 test_expect_success
'grep --no-index descends into repos, but not .git' '
1153 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1154 export GIT_CEILING_DIRECTORIES &&
1163 git commit -m foo &&
1164 echo magic >.git/file
1167 cat >expect <<-\EOF &&
1171 git grep -l --no-index magic >actual &&
1172 test_cmp expect actual
1176 test_expect_success
'setup double-dash tests' '
1177 cat >double-dash <<EOF &&
1188 test_expect_success
'grep -- pattern' '
1189 git grep -- "->" >actual &&
1190 test_cmp expected actual
1192 test_expect_success
'grep -- pattern -- pathspec' '
1193 git grep -- "->" -- double-dash >actual &&
1194 test_cmp expected actual
1196 test_expect_success
'grep -e pattern -- path' '
1197 git grep -e "->" -- double-dash >actual &&
1198 test_cmp expected actual
1204 test_expect_success
'grep -e -- -- path' '
1205 git grep -e -- -- double-dash >actual &&
1206 test_cmp expected actual
1209 test_expect_success
'dashdash disambiguates rev as rev' '
1210 test_when_finished "rm -f main" &&
1211 echo content >main &&
1212 echo main:hello.c >expect &&
1213 git grep -l o main -- hello.c >actual &&
1214 test_cmp expect actual
1217 test_expect_success
'dashdash disambiguates pathspec as pathspec' '
1218 test_when_finished "git rm -f main" &&
1219 echo content >main &&
1221 echo main:content >expect &&
1222 git grep o -- main >actual &&
1223 test_cmp expect actual
1226 test_expect_success
'report bogus arg without dashdash' '
1227 test_must_fail git grep o does-not-exist
1230 test_expect_success
'report bogus rev with dashdash' '
1231 test_must_fail git grep o hello.c --
1234 test_expect_success
'allow non-existent path with dashdash' '
1235 # We need a real match so grep exits with success.
1236 tree=$(git ls-tree HEAD |
1237 sed s/hello.c/not-in-working-tree/ |
1239 git grep o "$tree" -- not-in-working-tree
1242 test_expect_success
'grep --no-index pattern -- path' '
1246 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1247 export GIT_CEILING_DIRECTORIES &&
1249 echo hello >hello &&
1250 echo goodbye >goodbye &&
1251 echo hello:hello >expect &&
1252 git grep --no-index o -- hello >actual &&
1253 test_cmp expect actual
1257 test_expect_success
'grep --no-index complains of revs' '
1258 test_must_fail git grep --no-index o main -- 2>err &&
1259 test_i18ngrep "cannot be used with revs" err
1262 test_expect_success
'grep --no-index prefers paths to revs' '
1263 test_when_finished "rm -f main" &&
1264 echo content >main &&
1265 echo main:content >expect &&
1266 git grep --no-index o main >actual &&
1267 test_cmp expect actual
1270 test_expect_success
'grep --no-index does not "diagnose" revs' '
1271 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1272 test_i18ngrep ! -i "did you mean" err
1276 hello.c:int main(int argc, const char **argv)
1277 hello.c: printf("Hello world.\n");
1280 test_expect_success PCRE
'grep --perl-regexp pattern' '
1281 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1282 test_cmp expected actual
1285 test_expect_success
!FAIL_PREREQS
,!PCRE
'grep --perl-regexp pattern errors without PCRE' '
1286 test_must_fail git grep --perl-regexp "foo.*bar"
1289 test_expect_success PCRE
'grep -P pattern' '
1290 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1291 test_cmp expected actual
1294 test_expect_success LIBPCRE2
"grep -P with (*NO_JIT) doesn't error out" '
1295 git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1296 test_cmp expected actual
1300 test_expect_success
!FAIL_PREREQS
,!PCRE
'grep -P pattern errors without PCRE' '
1301 test_must_fail git grep -P "foo.*bar"
1304 test_expect_success
'grep pattern with grep.extendedRegexp=true' '
1305 test_must_fail git -c grep.extendedregexp=true \
1306 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1307 test_must_be_empty actual
1310 test_expect_success PCRE
'grep -P pattern with grep.extendedRegexp=true' '
1311 git -c grep.extendedregexp=true \
1312 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1313 test_cmp expected actual
1316 test_expect_success PCRE
'grep -P -v pattern' '
1321 git grep -P -v "abc" ab >actual &&
1322 test_cmp expected actual
1325 test_expect_success PCRE
'grep -P -i pattern' '
1326 cat >expected <<-EOF &&
1327 hello.c: printf("Hello world.\n");
1329 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1330 test_cmp expected actual
1333 test_expect_success PCRE
'grep -P -w pattern' '
1335 echo "hello_world:Hello world"
1336 echo "hello_world:HeLLo world"
1338 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1339 test_cmp expected actual
1342 test_expect_success PCRE
'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1343 git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1344 test_cmp hello_world actual &&
1345 git grep -P -h "(.)\1" hello_world >actual &&
1346 test_cmp hello_world actual
1349 test_expect_success
'grep -G invalidpattern properly dies ' '
1350 test_must_fail git grep -G "a["
1353 test_expect_success
'grep invalidpattern properly dies with grep.patternType=basic' '
1354 test_must_fail git -c grep.patterntype=basic grep "a["
1357 test_expect_success
'grep -E invalidpattern properly dies ' '
1358 test_must_fail git grep -E "a["
1361 test_expect_success
'grep invalidpattern properly dies with grep.patternType=extended' '
1362 test_must_fail git -c grep.patterntype=extended grep "a["
1365 test_expect_success PCRE
'grep -P invalidpattern properly dies ' '
1366 test_must_fail git grep -P "a["
1369 test_expect_success PCRE
'grep invalidpattern properly dies with grep.patternType=perl' '
1370 test_must_fail git -c grep.patterntype=perl grep "a["
1373 test_expect_success
'grep -G -E -F pattern' '
1374 echo "ab:a+b*c" >expected &&
1375 git grep -G -E -F "a+b*c" ab >actual &&
1376 test_cmp expected actual
1379 test_expect_success
'grep pattern with grep.patternType=basic, =extended, =fixed' '
1380 echo "ab:a+b*c" >expected &&
1382 -c grep.patterntype=basic \
1383 -c grep.patterntype=extended \
1384 -c grep.patterntype=fixed \
1385 grep "a+b*c" ab >actual &&
1386 test_cmp expected actual
1389 test_expect_success
'grep -E -F -G pattern' '
1390 echo "ab:a+bc" >expected &&
1391 git grep -E -F -G "a+b*c" ab >actual &&
1392 test_cmp expected actual
1395 test_expect_success
'grep pattern with grep.patternType=extended, =fixed, =basic' '
1396 echo "ab:a+bc" >expected &&
1398 -c grep.patterntype=extended \
1399 -c grep.patterntype=fixed \
1400 -c grep.patterntype=basic \
1401 grep "a+b*c" ab >actual &&
1402 test_cmp expected actual
1405 test_expect_success
'grep -F -G -E pattern' '
1406 echo "ab:abc" >expected &&
1407 git grep -F -G -E "a+b*c" ab >actual &&
1408 test_cmp expected actual
1411 test_expect_success
'grep pattern with grep.patternType=fixed, =basic, =extended' '
1412 echo "ab:abc" >expected &&
1414 -c grep.patterntype=fixed \
1415 -c grep.patterntype=basic \
1416 -c grep.patterntype=extended \
1417 grep "a+b*c" ab >actual &&
1418 test_cmp expected actual
1421 test_expect_success
'grep -G -F -P -E pattern' '
1422 echo "d0:d" >expected &&
1423 git grep -G -F -P -E "[\d]" d0 >actual &&
1424 test_cmp expected actual
1427 test_expect_success
'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1428 echo "d0:d" >expected &&
1430 -c grep.patterntype=fixed \
1431 -c grep.patterntype=basic \
1432 -c grep.patterntype=perl \
1433 -c grep.patterntype=extended \
1434 grep "[\d]" d0 >actual &&
1435 test_cmp expected actual
1438 test_expect_success PCRE
'grep -G -F -E -P pattern' '
1439 echo "d0:0" >expected &&
1440 git grep -G -F -E -P "[\d]" d0 >actual &&
1441 test_cmp expected actual
1444 test_expect_success PCRE
'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1445 echo "d0:0" >expected &&
1447 -c grep.patterntype=fixed \
1448 -c grep.patterntype=basic \
1449 -c grep.patterntype=extended \
1450 -c grep.patterntype=perl \
1451 grep "[\d]" d0 >actual &&
1452 test_cmp expected actual
1455 test_expect_success PCRE
'grep -P pattern with grep.patternType=fixed' '
1456 echo "ab:a+b*c" >expected &&
1458 -c grep.patterntype=fixed \
1459 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1460 test_cmp expected actual
1463 test_expect_success
'grep -F pattern with grep.patternType=basic' '
1464 echo "ab:a+b*c" >expected &&
1466 -c grep.patterntype=basic \
1467 grep -F "*c" ab >actual &&
1468 test_cmp expected actual
1471 test_expect_success
'grep -G pattern with grep.patternType=fixed' '
1477 -c grep.patterntype=fixed \
1478 grep -G "a+b" ab >actual &&
1479 test_cmp expected actual
1482 test_expect_success
'grep -E pattern with grep.patternType=fixed' '
1489 -c grep.patterntype=fixed \
1490 grep -E "a+" ab >actual &&
1491 test_cmp expected actual
1495 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1496 hello.c<RED>-<RESET>{
1498 hello.c<RED>:<RESET> /* char ?? */
1499 hello.c<RED>-<RESET>}
1501 hello_world<RED>:<RESET>Hello_world
1502 hello_world<RED>-<RESET>HeLLo_world
1505 test_expect_success
'grep --color, separator' '
1506 test_config color.grep.context normal &&
1507 test_config color.grep.filename normal &&
1508 test_config color.grep.function normal &&
1509 test_config color.grep.linenumber normal &&
1510 test_config color.grep.match normal &&
1511 test_config color.grep.selected normal &&
1512 test_config color.grep.separator red &&
1514 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1515 test_decode_color >actual &&
1516 test_cmp expected actual
1520 hello.c:int main(int argc, const char **argv)
1521 hello.c: /* char ?? */
1523 hello_world:Hello_world
1526 test_expect_success
'grep --break' '
1527 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1528 test_cmp expected actual
1532 hello.c:int main(int argc, const char **argv)
1535 hello.c: /* char ?? */
1538 hello_world:Hello_world
1539 hello_world-HeLLo_world
1542 test_expect_success
'grep --break with context' '
1543 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1544 test_cmp expected actual
1549 int main(int argc, const char **argv)
1555 test_expect_success
'grep --heading' '
1556 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1557 test_cmp expected actual
1561 <BOLD;GREEN>hello.c<RESET>
1562 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1563 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1565 <BOLD;GREEN>hello_world<RESET>
1566 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1569 test_expect_success
'mimic ack-grep --group' '
1570 test_config color.grep.context normal &&
1571 test_config color.grep.filename "bold green" &&
1572 test_config color.grep.function normal &&
1573 test_config color.grep.linenumber normal &&
1574 test_config color.grep.match "black yellow" &&
1575 test_config color.grep.selected normal &&
1576 test_config color.grep.separator normal &&
1578 git grep --break --heading -n --color \
1579 -e char -e lo_w hello.c hello_world |
1580 test_decode_color >actual &&
1581 test_cmp expected actual
1585 space: line with leading space1
1586 space: line with leading space2
1587 space: line with leading space3
1590 test_expect_success PCRE
'grep -E "^ "' '
1591 git grep -E "^ " space >actual &&
1592 test_cmp expected actual
1595 test_expect_success PCRE
'grep -P "^ "' '
1596 git grep -P "^ " space >actual &&
1597 test_cmp expected actual
1601 space-line without leading space1
1602 space: line <RED>with <RESET>leading space1
1603 space: line <RED>with <RESET>leading <RED>space2<RESET>
1604 space: line <RED>with <RESET>leading space3
1605 space:line without leading <RED>space2<RESET>
1608 test_expect_success
'grep --color -e A -e B with context' '
1609 test_config color.grep.context normal &&
1610 test_config color.grep.filename normal &&
1611 test_config color.grep.function normal &&
1612 test_config color.grep.linenumber normal &&
1613 test_config color.grep.matchContext normal &&
1614 test_config color.grep.matchSelected red &&
1615 test_config color.grep.selected normal &&
1616 test_config color.grep.separator normal &&
1618 git grep --color=always -C2 -e "with " -e space2 space |
1619 test_decode_color >actual &&
1620 test_cmp expected actual
1624 space-line without leading space1
1625 space- line with leading space1
1626 space: line <RED>with <RESET>leading <RED>space2<RESET>
1627 space- line with leading space3
1628 space-line without leading space2
1631 test_expect_success
'grep --color -e A --and -e B with context' '
1632 test_config color.grep.context normal &&
1633 test_config color.grep.filename normal &&
1634 test_config color.grep.function normal &&
1635 test_config color.grep.linenumber normal &&
1636 test_config color.grep.matchContext normal &&
1637 test_config color.grep.matchSelected red &&
1638 test_config color.grep.selected normal &&
1639 test_config color.grep.separator normal &&
1641 git grep --color=always -C2 -e "with " --and -e space2 space |
1642 test_decode_color >actual &&
1643 test_cmp expected actual
1647 space-line without leading space1
1648 space: line <RED>with <RESET>leading space1
1649 space- line with leading space2
1650 space: line <RED>with <RESET>leading space3
1651 space-line without leading space2
1654 test_expect_success
'grep --color -e A --and --not -e B with context' '
1655 test_config color.grep.context normal &&
1656 test_config color.grep.filename normal &&
1657 test_config color.grep.function normal &&
1658 test_config color.grep.linenumber normal &&
1659 test_config color.grep.matchContext normal &&
1660 test_config color.grep.matchSelected red &&
1661 test_config color.grep.selected normal &&
1662 test_config color.grep.separator normal &&
1664 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1665 test_decode_color >actual &&
1666 test_cmp expected actual
1671 hello.c=int main(int argc, const char **argv)
1673 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1675 hello.c- /* char ?? */
1679 test_expect_success
'grep --color -e A --and -e B -p with context' '
1680 test_config color.grep.context normal &&
1681 test_config color.grep.filename normal &&
1682 test_config color.grep.function normal &&
1683 test_config color.grep.linenumber normal &&
1684 test_config color.grep.matchContext normal &&
1685 test_config color.grep.matchSelected red &&
1686 test_config color.grep.selected normal &&
1687 test_config color.grep.separator normal &&
1689 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1690 test_decode_color >actual &&
1691 test_cmp expected actual
1694 test_expect_success
'grep can find things only in the work tree' '
1695 : >work-tree-only &&
1696 git add work-tree-only &&
1697 test_when_finished "git rm -f work-tree-only" &&
1698 echo "find in work tree" >work-tree-only &&
1699 git grep --quiet "find in work tree" &&
1700 test_must_fail git grep --quiet --cached "find in work tree" &&
1701 test_must_fail git grep --quiet "find in work tree" HEAD
1704 test_expect_success
'grep can find things only in the work tree (i-t-a)' '
1705 echo "intend to add this" >intend-to-add &&
1706 git add -N intend-to-add &&
1707 test_when_finished "git rm -f intend-to-add" &&
1708 git grep --quiet "intend to add this" &&
1709 test_must_fail git grep --quiet --cached "intend to add this" &&
1710 test_must_fail git grep --quiet "intend to add this" HEAD
1713 test_expect_success
'grep does not search work tree with assume unchanged' '
1714 echo "intend to add this" >intend-to-add &&
1715 git add -N intend-to-add &&
1716 git update-index --assume-unchanged intend-to-add &&
1717 test_when_finished "git rm -f intend-to-add" &&
1718 test_must_fail git grep --quiet "intend to add this" &&
1719 test_must_fail git grep --quiet --cached "intend to add this" &&
1720 test_must_fail git grep --quiet "intend to add this" HEAD
1723 test_expect_success
'grep can find things only in the index' '
1724 echo "only in the index" >cache-this &&
1725 git add cache-this &&
1727 test_when_finished "git rm --cached cache-this" &&
1728 test_must_fail git grep --quiet "only in the index" &&
1729 git grep --quiet --cached "only in the index" &&
1730 test_must_fail git grep --quiet "only in the index" HEAD
1733 test_expect_success
'grep does not report i-t-a with -L --cached' '
1734 echo "intend to add this" >intend-to-add &&
1735 git add -N intend-to-add &&
1736 test_when_finished "git rm -f intend-to-add" &&
1737 git ls-files | grep -v "^intend-to-add\$" >expected &&
1738 git grep -L --cached "nonexistent_string" >actual &&
1739 test_cmp expected actual
1742 test_expect_success
'grep does not report i-t-a and assume unchanged with -L' '
1743 echo "intend to add this" >intend-to-add-assume-unchanged &&
1744 git add -N intend-to-add-assume-unchanged &&
1745 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1746 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1747 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1748 git grep -L "nonexistent_string" >actual &&
1749 test_cmp expected actual