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
18 int main(int argc, const char **argv)
20 printf("Hello world.\n");
26 test_expect_success setup '
30 echo foo_mmap bar mmap
31 echo foo mmap bar_mmap
32 echo foo_mmap bar mmap baz
60 echo "line without leading space1"
61 echo " line with leading space1"
62 echo " line with leading space2"
63 echo " line with leading space3"
64 echo "line without leading space2"
66 cat >hello.ps1 <<-\EOF &&
78 if test_have_prereq FUNNYNAMES
80 echo unusual >"\"unusual\" pathname" &&
81 echo unusual >"t/nested \"unusual\" pathname"
88 test_expect_success 'grep should not segfault with a bad input' '
89 test_must_fail git grep "("
95 HEAD) HC='HEAD:' L='HEAD' ;;
96 '') HC= L='in working tree' ;;
99 test_expect_success "grep -w $L" '
101 echo ${HC}file:1:foo mmap bar
102 echo ${HC}file:3:foo_mmap bar mmap
103 echo ${HC}file:4:foo mmap bar_mmap
104 echo ${HC}file:5:foo_mmap bar mmap baz
106 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
107 test_cmp expected actual
110 test_expect_success "grep -w $L (with --column)" '
112 echo ${HC}file:5:foo mmap bar
113 echo ${HC}file:14:foo_mmap bar mmap
114 echo ${HC}file:5:foo mmap bar_mmap
115 echo ${HC}file:14:foo_mmap bar mmap baz
117 git grep --column -w -e mmap $H >actual &&
118 test_cmp expected actual
121 test_expect_success "grep -w $L (with --column, extended OR)" '
123 echo ${HC}file:14:foo_mmap bar mmap
124 echo ${HC}file:19:foo_mmap bar mmap baz
126 git grep --column -w -e mmap$ --or -e baz $H >actual &&
127 test_cmp expected actual
130 test_expect_success "grep -w $L (with --column, --invert-match)" '
132 echo ${HC}file:1:foo mmap bar
133 echo ${HC}file:1:foo_mmap bar
134 echo ${HC}file:1:foo_mmap bar mmap
135 echo ${HC}file:1:foo mmap bar_mmap
137 git grep --column --invert-match -w -e baz $H -- file >actual &&
138 test_cmp expected actual
141 test_expect_success "grep $L (with --column, --invert-match, extended OR)" '
143 echo ${HC}hello_world:6:HeLLo_world
145 git grep --column --invert-match -e ll --or --not -e _ $H -- hello_world \
147 test_cmp expected actual
150 test_expect_success "grep $L (with --column, --invert-match, extended AND)" '
152 echo ${HC}hello_world:3:Hello world
153 echo ${HC}hello_world:3:Hello_world
154 echo ${HC}hello_world:6:HeLLo_world
156 git grep --column --invert-match --not -e _ --and --not -e ll $H -- hello_world \
158 test_cmp expected actual
161 test_expect_success "grep $L (with --column, double-negation)" '
163 echo ${HC}file:1:foo_mmap bar mmap baz
165 git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
167 test_cmp expected actual
170 test_expect_success "grep -w $L (with --column, -C)" '
172 echo ${HC}file:5:foo mmap bar
173 echo ${HC}file-foo_mmap bar
174 echo ${HC}file:14:foo_mmap bar mmap
175 echo ${HC}file:5:foo mmap bar_mmap
176 echo ${HC}file:14:foo_mmap bar mmap baz
178 git grep --column -w -C1 -e mmap $H >actual &&
179 test_cmp expected actual
182 test_expect_success "grep -w $L (with --line-number, --column)" '
184 echo ${HC}file:1:5:foo mmap bar
185 echo ${HC}file:3:14:foo_mmap bar mmap
186 echo ${HC}file:4:5:foo mmap bar_mmap
187 echo ${HC}file:5:14:foo_mmap bar mmap baz
189 git grep -n --column -w -e mmap $H >actual &&
190 test_cmp expected actual
193 test_expect_success "grep -w $L (with non-extended patterns, --column)" '
195 echo ${HC}file:5:foo mmap bar
196 echo ${HC}file:10:foo_mmap bar
197 echo ${HC}file:10:foo_mmap bar mmap
198 echo ${HC}file:5:foo mmap bar_mmap
199 echo ${HC}file:10:foo_mmap bar mmap baz
201 git grep --column -w -e bar -e mmap $H >actual &&
202 test_cmp expected actual
205 test_expect_success "grep -w $L" '
207 echo ${HC}file:1:foo mmap bar
208 echo ${HC}file:3:foo_mmap bar mmap
209 echo ${HC}file:4:foo mmap bar_mmap
210 echo ${HC}file:5:foo_mmap bar mmap baz
212 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
213 test_cmp expected actual
216 test_expect_success "grep -w $L" '
218 echo ${HC}file:foo mmap bar
219 echo ${HC}file:foo_mmap bar mmap
220 echo ${HC}file:foo mmap bar_mmap
221 echo ${HC}file:foo_mmap bar mmap baz
223 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
224 test_cmp expected actual
227 test_expect_success "grep -w $L (w)" '
228 test_must_fail git grep -n -w -e "^w" $H >actual &&
229 test_must_be_empty actual
232 test_expect_success "grep -w $L (x)" '
234 echo ${HC}x:1:x x xx x
236 git grep -n -w -e "x xx* x" $H >actual &&
237 test_cmp expected actual
240 test_expect_success "grep -w $L (y-1)" '
244 git grep -n -w -e "^y" $H >actual &&
245 test_cmp expected actual
248 test_expect_success "grep -w $L (y-2)" '
249 if git grep -n -w -e "^y y" $H >actual
251 echo should not have matched
255 test_must_be_empty actual
259 test_expect_success "grep -w $L (z)" '
260 if git grep -n -w -e "^z" $H >actual
262 echo should not have matched
266 test_must_be_empty actual
270 test_expect_success "grep $L (with --column, --only-matching)" '
272 echo ${HC}file:1:5:mmap
273 echo ${HC}file:2:5:mmap
274 echo ${HC}file:3:5:mmap
275 echo ${HC}file:3:13:mmap
276 echo ${HC}file:4:5:mmap
277 echo ${HC}file:4:13:mmap
278 echo ${HC}file:5:5:mmap
279 echo ${HC}file:5:13:mmap
281 git grep --column -n -o -e mmap $H >actual &&
282 test_cmp expected actual
285 test_expect_success "grep $L (t-1)" '
286 echo "${HC}t/t:1:test" >expected &&
287 git grep -n -e test $H >actual &&
288 test_cmp expected actual
291 test_expect_success "grep $L (t-2)" '
292 echo "${HC}t:1:test" >expected &&
295 git grep -n -e test $H
297 test_cmp expected actual
300 test_expect_success "grep $L (t-3)" '
301 echo "${HC}t/t:1:test" >expected &&
304 git grep --full-name -n -e test $H
306 test_cmp expected actual
309 test_expect_success "grep -c $L (no /dev/null)" '
310 ! git grep -c test $H | grep /dev/null
313 test_expect_success "grep --max-depth -1 $L" '
315 echo ${HC}t/a/v:1:vvv
319 git grep --max-depth -1 -n -e vvv $H >actual &&
320 test_cmp expected actual &&
321 git grep --recursive -n -e vvv $H >actual &&
322 test_cmp expected actual
325 test_expect_success "grep --max-depth 0 $L" '
329 git grep --max-depth 0 -n -e vvv $H >actual &&
330 test_cmp expected actual &&
331 git grep --no-recursive -n -e vvv $H >actual &&
332 test_cmp expected actual
335 test_expect_success "grep --max-depth 0 -- '*' $L" '
337 echo ${HC}t/a/v:1:vvv
341 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
342 test_cmp expected actual &&
343 git grep --no-recursive -n -e vvv $H -- "*" >actual &&
344 test_cmp expected actual
347 test_expect_success "grep --max-depth 1 $L" '
352 git grep --max-depth 1 -n -e vvv $H >actual &&
353 test_cmp expected actual
356 test_expect_success "grep --max-depth 0 -- t $L" '
360 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
361 test_cmp expected actual &&
362 git grep --no-recursive -n -e vvv $H -- t >actual &&
363 test_cmp expected actual
366 test_expect_success "grep --max-depth 0 -- . t $L" '
371 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
372 test_cmp expected actual &&
373 git grep --no-recursive -n -e vvv $H -- . t >actual &&
374 test_cmp expected actual
377 test_expect_success "grep --max-depth 0 -- t . $L" '
382 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
383 test_cmp expected actual &&
384 git grep --no-recursive -n -e vvv $H -- t . >actual &&
385 test_cmp expected actual
387 test_expect_success "grep $L with grep.extendedRegexp=false" '
388 echo "${HC}ab:a+bc" >expected &&
389 git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
390 test_cmp expected actual
393 test_expect_success "grep $L with grep.extendedRegexp=true" '
394 echo "${HC}ab:abc" >expected &&
395 git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
396 test_cmp expected actual
399 test_expect_success "grep $L with grep.patterntype=basic" '
400 echo "${HC}ab:a+bc" >expected &&
401 git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
402 test_cmp expected actual
405 test_expect_success "grep $L with grep.patterntype=extended" '
406 echo "${HC}ab:abc" >expected &&
407 git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
408 test_cmp expected actual
411 test_expect_success "grep $L with grep.patterntype=fixed" '
412 echo "${HC}ab:a+b*c" >expected &&
413 git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
414 test_cmp expected actual
417 test_expect_success PCRE "grep $L with grep.patterntype=perl" '
418 echo "${HC}ab:a+b*c" >expected &&
419 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
420 test_cmp expected actual
423 test_expect_success !FAIL_PREREQS,!PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
424 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
427 test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
428 echo "${HC}ab:abc" >expected &&
430 -c grep.patternType=default \
431 -c grep.extendedRegexp=true \
432 grep "a+b*c" $H ab >actual &&
433 test_cmp expected actual
436 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
437 echo "${HC}ab:abc" >expected &&
439 -c grep.extendedRegexp=true \
440 -c grep.patternType=default \
441 grep "a+b*c" $H ab >actual &&
442 test_cmp expected actual
445 test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
446 echo "${HC}ab:abc" >expected &&
448 -c grep.patternType=extended \
449 -c grep.extendedRegexp=false \
450 grep "a+b*c" $H ab >actual &&
451 test_cmp expected actual
454 test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
455 echo "${HC}ab:a+bc" >expected &&
457 -c grep.patternType=basic \
458 -c grep.extendedRegexp=true \
459 grep "a+b*c" $H ab >actual &&
460 test_cmp expected actual
463 test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
464 echo "${HC}ab:abc" >expected &&
466 -c grep.extendedRegexp=false \
467 -c grep.patternType=extended \
468 grep "a+b*c" $H ab >actual &&
469 test_cmp expected actual
472 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
473 echo "${HC}ab:a+bc" >expected &&
475 -c grep.extendedRegexp=true \
476 -c grep.patternType=basic \
477 grep "a+b*c" $H ab >actual &&
478 test_cmp expected actual
481 test_expect_success "grep --count $L" '
482 echo ${HC}ab:3 >expected &&
483 git grep --count -e b $H -- ab >actual &&
484 test_cmp expected actual
487 test_expect_success "grep --count -h $L" '
489 git grep --count -h -e b $H -- ab >actual &&
490 test_cmp expected actual
493 test_expect_success FUNNYNAMES "grep $L should quote unusual pathnames" '
494 cat >expected <<-EOF &&
495 ${HC}"\"unusual\" pathname":unusual
496 ${HC}"t/nested \"unusual\" pathname":unusual
498 git grep unusual $H >actual &&
499 test_cmp expected actual
502 test_expect_success FUNNYNAMES "grep $L in subdir should quote unusual relative pathnames" '
503 cat >expected <<-EOF &&
504 ${HC}"nested \"unusual\" pathname":unusual
510 test_cmp expected actual
513 test_expect_success FUNNYNAMES "grep -z $L with unusual pathnames" '
514 cat >expected <<-EOF &&
515 ${HC}"unusual" pathname:unusual
516 ${HC}t/nested "unusual" pathname:unusual
518 git grep -z unusual $H >actual &&
519 tr "\0" ":" <actual >actual-replace-null &&
520 test_cmp expected actual-replace-null
523 test_expect_success FUNNYNAMES "grep -z $L in subdir with unusual relative pathnames" '
524 cat >expected <<-EOF &&
525 ${HC}nested "unusual" pathname:unusual
529 git grep -z unusual $H
531 tr "\0" ":" <actual >actual-replace-null &&
532 test_cmp expected actual-replace-null
539 test_expect_success 'grep -l -C' '
540 git grep -l -C1 foo >actual &&
541 test_cmp expected actual
547 test_expect_success 'grep -c -C' '
548 git grep -c -C1 foo >actual &&
549 test_cmp expected actual
552 test_expect_success 'grep -L -C' '
553 git ls-files >expected &&
554 git grep -L -C1 nonexistent_string >actual &&
555 test_cmp expected actual
558 test_expect_success 'grep --files-without-match --quiet' '
559 git grep --files-without-match --quiet nonexistent_string >actual &&
560 test_must_be_empty actual
564 file:foo mmap bar_mmap
567 test_expect_success 'grep -e A --and -e B' '
568 git grep -e "foo mmap" --and -e bar_mmap >actual &&
569 test_cmp expected actual
573 file:foo_mmap bar mmap
574 file:foo_mmap bar mmap baz
578 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
579 git grep \( -e foo_ --or -e baz \) \
580 --and -e " mmap" >actual &&
581 test_cmp expected actual
588 test_expect_success 'grep -e A --and --not -e B' '
589 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
590 test_cmp expected actual
593 test_expect_success 'grep should ignore GREP_OPTIONS' '
594 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
595 test_cmp expected actual
598 test_expect_success 'grep -f, non-existent file' '
599 test_must_fail git grep -f patterns
605 file:foo_mmap bar mmap
606 file:foo mmap bar_mmap
607 file:foo_mmap bar mmap baz
614 test_expect_success 'grep -f, one pattern' '
615 git grep -f pattern >actual &&
616 test_cmp expected actual
622 file:foo_mmap bar mmap
623 file:foo mmap bar_mmap
624 file:foo_mmap bar mmap baz
635 test_expect_success 'grep -f, multiple patterns' '
636 git grep -f patterns >actual &&
637 test_cmp expected actual
640 test_expect_success 'grep, multiple patterns' '
641 git grep "$(cat patterns)" >actual &&
642 test_cmp expected actual
648 file:foo_mmap bar mmap
649 file:foo mmap bar_mmap
650 file:foo_mmap bar mmap baz
664 test_expect_success 'grep -f, ignore empty lines' '
665 git grep -f patterns >actual &&
666 test_cmp expected actual
669 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
670 git grep -f - <patterns >actual &&
671 test_cmp expected actual
680 test_expect_success 'grep -q, silently report matches' '
681 git grep -q mmap >actual &&
682 test_must_be_empty actual &&
683 test_must_fail git grep -q qfwfq >actual &&
684 test_must_be_empty actual
687 test_expect_success 'grep -C1 hunk mark between files' '
688 git grep -C1 "^[yz]" >actual &&
689 test_cmp expected actual
692 test_expect_success 'log grep setup' '
693 test_commit --append --author "With * Asterisk <xyzzy@frotz.com>" second file a &&
694 test_commit --append third file a &&
695 test_commit --append --author "Night Fall <nitfol@frobozz.com>" fourth file a
698 test_expect_success 'log grep (1)' '
699 git log --author=author --pretty=tformat:%s >actual &&
701 echo third && echo initial
703 test_cmp expect actual
706 test_expect_success 'log grep (2)' '
707 git log --author=" * " -F --pretty=tformat:%s >actual &&
711 test_cmp expect actual
714 test_expect_success 'log grep (3)' '
715 git log --author="^A U" --pretty=tformat:%s >actual &&
717 echo third && echo initial
719 test_cmp expect actual
722 test_expect_success 'log grep (4)' '
723 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
727 test_cmp expect actual
730 test_expect_success 'log grep (5)' '
731 git log --author=Thor -F --pretty=tformat:%s >actual &&
733 echo third && echo initial
735 test_cmp expect actual
738 test_expect_success 'log grep (6)' '
739 git log --author=-0700 --pretty=tformat:%s >actual &&
740 test_must_be_empty actual
743 test_expect_success 'log grep (7)' '
744 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
745 echo third >expect &&
746 test_cmp expect actual
749 test_expect_success 'log grep (8)' '
750 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
752 echo third && echo second
754 test_cmp expect actual
757 test_expect_success 'log grep (9)' '
758 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
759 echo third >expect &&
760 test_cmp expect actual
763 test_expect_success 'log grep (9)' '
764 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
765 test_must_be_empty actual
768 test_expect_success 'log --grep-reflog can only be used under -g' '
769 test_must_fail git log --grep-reflog="commit: third"
772 test_expect_success 'log with multiple --grep uses union' '
773 git log --grep=i --grep=r --format=%s >actual &&
775 echo fourth && echo third && echo initial
777 test_cmp expect actual
780 test_expect_success 'log --all-match with multiple --grep uses intersection' '
781 git log --all-match --grep=i --grep=r --format=%s >actual &&
785 test_cmp expect actual
788 test_expect_success 'log with multiple --author uses union' '
789 git log --author="Thor" --author="Aster" --format=%s >actual &&
791 echo third && echo second && echo initial
793 test_cmp expect actual
796 test_expect_success 'log --all-match with multiple --author still uses union' '
797 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
799 echo third && echo second && echo initial
801 test_cmp expect actual
804 test_expect_success 'log --grep --author uses intersection' '
805 # grep matches only third and fourth
806 # author matches only initial and third
807 git log --author="A U Thor" --grep=r --format=%s >actual &&
811 test_cmp expect actual
814 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
815 # grep matches initial and second but not third
816 # author matches only initial and third
817 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
821 test_cmp expect actual
824 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
825 # grep matches only initial and third
826 # author matches all but second
827 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
829 echo third && echo initial
831 test_cmp expect actual
834 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
835 # grep matches only initial and third
836 # author matches all but second
837 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
839 echo third && echo initial
841 test_cmp expect actual
844 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
845 # grep matches only third
846 # author matches only initial and third
847 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
851 test_cmp expect actual
854 test_expect_success 'log --author does not search in timestamp' '
855 git log --author="$GIT_AUTHOR_DATE" >actual &&
856 test_must_be_empty actual
859 test_expect_success 'log --committer does not search in timestamp' '
860 git log --committer="$GIT_COMMITTER_DATE" >actual &&
861 test_must_be_empty actual
864 test_expect_success 'grep with CE_VALID file' '
865 git update-index --assume-unchanged t/t &&
867 test "$(git grep test)" = "t/t:test" &&
868 git update-index --no-assume-unchanged t/t &&
873 hello.c=#include <stdio.h>
877 test_expect_success 'grep -p with userdiff' '
878 git config diff.custom.funcname "^#" &&
879 echo "hello.c diff=custom" >.gitattributes &&
880 git grep -p return >actual &&
881 test_cmp expected actual
885 hello.c=int main(int argc, const char **argv)
889 test_expect_success 'grep -p' '
890 rm -f .gitattributes &&
891 git grep -p return >actual &&
892 test_cmp expected actual
896 hello.c-#include <stdio.h>
898 hello.c=int main(int argc, const char **argv)
900 hello.c- printf("Hello world.\n");
904 test_expect_success 'grep -p -B5' '
905 git grep -p -B5 return >actual &&
906 test_cmp expected actual
910 hello.c=int main(int argc, const char **argv)
912 hello.c- printf("Hello world.\n");
914 hello.c- /* char ?? */
918 test_expect_success 'grep -W' '
919 git grep -W return >actual &&
920 test_cmp expected actual
924 hello.c-#include <assert.h>
925 hello.c:#include <stdio.h>
928 test_expect_success 'grep -W shows no trailing empty lines' '
929 git grep -W stdio >actual &&
930 test_cmp expected actual
933 test_expect_success 'grep -W with userdiff' '
934 test_when_finished "rm -f .gitattributes" &&
935 git config diff.custom.xfuncname "^function .*$" &&
936 echo "hello.ps1 diff=custom" >.gitattributes &&
937 git grep -W echo >function-context-userdiff-actual
940 test_expect_success ' includes preceding comment' '
941 grep "# Say hello" function-context-userdiff-actual
944 test_expect_success ' includes function line' '
945 grep "=function hello" function-context-userdiff-actual
948 test_expect_success ' includes matching line' '
949 grep ": echo" function-context-userdiff-actual
952 test_expect_success ' includes last line of the function' '
953 grep "} # hello" function-context-userdiff-actual
956 for threads in $(test_seq 0 10)
958 test_expect_success "grep --threads=$threads & -c grep.threads=$threads" "
959 git grep --threads=$threads . >actual.$threads &&
960 if test $threads -ge 1
962 test_cmp actual.\$(($threads - 1)) actual.$threads
964 git -c grep.threads=$threads grep . >actual.$threads &&
965 if test $threads -ge 1
967 test_cmp actual.\$(($threads - 1)) actual.$threads
972 test_expect_success !PTHREADS,!FAIL_PREREQS \
973 'grep --threads=N or pack.threads=N warns when no pthreads' '
974 git grep --threads=2 Hello hello_world 2>err &&
975 grep ^warning: err >warnings &&
976 test_line_count = 1 warnings &&
977 grep -F "no threads support, ignoring --threads" err &&
978 git -c grep.threads=2 grep Hello hello_world 2>err &&
979 grep ^warning: err >warnings &&
980 test_line_count = 1 warnings &&
981 grep -F "no threads support, ignoring grep.threads" err &&
982 git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
983 grep ^warning: err >warnings &&
984 test_line_count = 2 warnings &&
985 grep -F "no threads support, ignoring --threads" err &&
986 grep -F "no threads support, ignoring grep.threads" err &&
987 git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
988 test_line_count = 0 err
991 test_expect_success 'grep from a subdirectory to search wider area (1)' '
994 cd s && git grep "x x x" ..
998 test_expect_success 'grep from a subdirectory to search wider area (2)' '
1002 test_expect_code 1 git grep xxyyzz .. >out &&
1003 test_must_be_empty out
1008 hello.c:int main(int argc, const char **argv)
1011 test_expect_success 'grep -Fi' '
1012 git grep -Fi "CHAR *" >actual &&
1013 test_cmp expected actual
1016 test_expect_success 'outside of git repository' '
1018 mkdir -p non/git/sub &&
1019 echo hello >non/git/file1 &&
1020 echo world >non/git/sub/file2 &&
1023 echo sub/file2:world
1024 } >non/expect.full &&
1025 echo file2:world >non/expect.sub &&
1027 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1028 export GIT_CEILING_DIRECTORIES &&
1030 test_must_fail git grep o &&
1031 git grep --no-index o >../actual.full &&
1032 test_cmp ../expect.full ../actual.full &&
1034 test_must_fail git grep o &&
1035 git grep --no-index o >../../actual.sub &&
1036 test_cmp ../../expect.sub ../../actual.sub
1039 echo ".*o*" >non/git/.gitignore &&
1041 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1042 export GIT_CEILING_DIRECTORIES &&
1044 test_must_fail git grep o &&
1045 git grep --no-index --exclude-standard o >../actual.full &&
1046 test_cmp ../expect.full ../actual.full &&
1049 echo ".gitignore:.*o*" &&
1051 } >../expect.with.ignored &&
1052 git grep --no-index --no-exclude-standard o >../actual.full &&
1053 test_cmp ../expect.with.ignored ../actual.full
1057 test_expect_success 'outside of git repository with fallbackToNoIndex' '
1059 mkdir -p non/git/sub &&
1060 echo hello >non/git/file1 &&
1061 echo world >non/git/sub/file2 &&
1062 cat <<-\EOF >non/expect.full &&
1066 echo file2:world >non/expect.sub &&
1068 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1069 export GIT_CEILING_DIRECTORIES &&
1071 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1072 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
1073 test_cmp ../expect.full ../actual.full &&
1075 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1076 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
1077 test_cmp ../../expect.sub ../../actual.sub
1080 echo ".*o*" >non/git/.gitignore &&
1082 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1083 export GIT_CEILING_DIRECTORIES &&
1085 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1086 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
1087 test_cmp ../expect.full ../actual.full &&
1090 echo ".gitignore:.*o*" &&
1092 } >../expect.with.ignored &&
1093 git -c grep.fallbackToNoIndex grep --no-exclude-standard o >../actual.full &&
1094 test_cmp ../expect.with.ignored ../actual.full
1098 test_expect_success 'inside git repository but with --no-index' '
1100 mkdir -p is/git/sub &&
1101 echo hello >is/git/file1 &&
1102 echo world >is/git/sub/file2 &&
1103 echo ".*o*" >is/git/.gitignore &&
1106 echo sub/file2:world
1107 } >is/expect.unignored &&
1109 echo ".gitignore:.*o*" &&
1110 cat is/expect.unignored
1111 } >is/expect.full &&
1112 echo file2:world >is/expect.sub &&
1116 test_must_fail git grep o >../actual.full &&
1117 test_must_be_empty ../actual.full &&
1119 git grep --untracked o >../actual.unignored &&
1120 test_cmp ../expect.unignored ../actual.unignored &&
1122 git grep --no-index o >../actual.full &&
1123 test_cmp ../expect.full ../actual.full &&
1125 git grep --no-index --exclude-standard o >../actual.unignored &&
1126 test_cmp ../expect.unignored ../actual.unignored &&
1129 test_must_fail git grep o >../../actual.sub &&
1130 test_must_be_empty ../../actual.sub &&
1132 git grep --no-index o >../../actual.sub &&
1133 test_cmp ../../expect.sub ../../actual.sub &&
1135 git grep --untracked o >../../actual.sub &&
1136 test_cmp ../../expect.sub ../../actual.sub
1140 test_expect_success 'grep --no-index descends into repos, but not .git' '
1144 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1145 export GIT_CEILING_DIRECTORIES &&
1154 git commit -m foo &&
1155 echo magic >.git/file
1158 cat >expect <<-\EOF &&
1162 git grep -l --no-index magic >actual &&
1163 test_cmp expect actual
1167 test_expect_success 'setup double-dash tests' '
1168 cat >double-dash <<EOF &&
1179 test_expect_success 'grep -- pattern' '
1180 git grep -- "->" >actual &&
1181 test_cmp expected actual
1183 test_expect_success 'grep -- pattern -- pathspec' '
1184 git grep -- "->" -- double-dash >actual &&
1185 test_cmp expected actual
1187 test_expect_success 'grep -e pattern -- path' '
1188 git grep -e "->" -- double-dash >actual &&
1189 test_cmp expected actual
1195 test_expect_success 'grep -e -- -- path' '
1196 git grep -e -- -- double-dash >actual &&
1197 test_cmp expected actual
1200 test_expect_success 'dashdash disambiguates rev as rev' '
1201 test_when_finished "rm -f main" &&
1202 echo content >main &&
1203 echo main:hello.c >expect &&
1204 git grep -l o main -- hello.c >actual &&
1205 test_cmp expect actual
1208 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
1209 test_when_finished "git rm -f main" &&
1210 echo content >main &&
1212 echo main:content >expect &&
1213 git grep o -- main >actual &&
1214 test_cmp expect actual
1217 test_expect_success 'report bogus arg without dashdash' '
1218 test_must_fail git grep o does-not-exist
1221 test_expect_success 'report bogus rev with dashdash' '
1222 test_must_fail git grep o hello.c --
1225 test_expect_success 'allow non-existent path with dashdash' '
1226 # We need a real match so grep exits with success.
1227 tree=$(git ls-tree HEAD |
1228 sed s/hello.c/not-in-working-tree/ |
1230 git grep o "$tree" -- not-in-working-tree
1233 test_expect_success 'grep --no-index pattern -- path' '
1237 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1238 export GIT_CEILING_DIRECTORIES &&
1240 echo hello >hello &&
1241 echo goodbye >goodbye &&
1242 echo hello:hello >expect &&
1243 git grep --no-index o -- hello >actual &&
1244 test_cmp expect actual
1248 test_expect_success 'grep --no-index complains of revs' '
1249 test_must_fail git grep --no-index o main -- 2>err &&
1250 test_i18ngrep "cannot be used with revs" err
1253 test_expect_success 'grep --no-index prefers paths to revs' '
1254 test_when_finished "rm -f main" &&
1255 echo content >main &&
1256 echo main:content >expect &&
1257 git grep --no-index o main >actual &&
1258 test_cmp expect actual
1261 test_expect_success 'grep --no-index does not "diagnose" revs' '
1262 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1263 test_i18ngrep ! -i "did you mean" err
1267 hello.c:int main(int argc, const char **argv)
1268 hello.c: printf("Hello world.\n");
1271 test_expect_success PCRE 'grep --perl-regexp pattern' '
1272 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1273 test_cmp expected actual
1276 test_expect_success !FAIL_PREREQS,!PCRE 'grep --perl-regexp pattern errors without PCRE' '
1277 test_must_fail git grep --perl-regexp "foo.*bar"
1280 test_expect_success PCRE 'grep -P pattern' '
1281 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1282 test_cmp expected actual
1285 test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
1286 git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1287 test_cmp expected actual
1291 test_expect_success !FAIL_PREREQS,!PCRE 'grep -P pattern errors without PCRE' '
1292 test_must_fail git grep -P "foo.*bar"
1295 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1296 test_must_fail git -c grep.extendedregexp=true \
1297 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1298 test_must_be_empty actual
1301 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1302 git -c grep.extendedregexp=true \
1303 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1304 test_cmp expected actual
1307 test_expect_success PCRE 'grep -P -v pattern' '
1312 git grep -P -v "abc" ab >actual &&
1313 test_cmp expected actual
1316 test_expect_success PCRE 'grep -P -i pattern' '
1317 cat >expected <<-EOF &&
1318 hello.c: printf("Hello world.\n");
1320 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1321 test_cmp expected actual
1324 test_expect_success PCRE 'grep -P -w pattern' '
1326 echo "hello_world:Hello world"
1327 echo "hello_world:HeLLo world"
1329 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1330 test_cmp expected actual
1333 test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1334 git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1335 test_cmp hello_world actual &&
1336 git grep -P -h "(.)\1" hello_world >actual &&
1337 test_cmp hello_world actual
1340 test_expect_success 'grep -G invalidpattern properly dies ' '
1341 test_must_fail git grep -G "a["
1344 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1345 test_must_fail git -c grep.patterntype=basic grep "a["
1348 test_expect_success 'grep -E invalidpattern properly dies ' '
1349 test_must_fail git grep -E "a["
1352 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1353 test_must_fail git -c grep.patterntype=extended grep "a["
1356 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1357 test_must_fail git grep -P "a["
1360 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1361 test_must_fail git -c grep.patterntype=perl grep "a["
1364 test_expect_success 'grep -G -E -F pattern' '
1365 echo "ab:a+b*c" >expected &&
1366 git grep -G -E -F "a+b*c" ab >actual &&
1367 test_cmp expected actual
1370 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1371 echo "ab:a+b*c" >expected &&
1373 -c grep.patterntype=basic \
1374 -c grep.patterntype=extended \
1375 -c grep.patterntype=fixed \
1376 grep "a+b*c" ab >actual &&
1377 test_cmp expected actual
1380 test_expect_success 'grep -E -F -G pattern' '
1381 echo "ab:a+bc" >expected &&
1382 git grep -E -F -G "a+b*c" ab >actual &&
1383 test_cmp expected actual
1386 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1387 echo "ab:a+bc" >expected &&
1389 -c grep.patterntype=extended \
1390 -c grep.patterntype=fixed \
1391 -c grep.patterntype=basic \
1392 grep "a+b*c" ab >actual &&
1393 test_cmp expected actual
1396 test_expect_success 'grep -F -G -E pattern' '
1397 echo "ab:abc" >expected &&
1398 git grep -F -G -E "a+b*c" ab >actual &&
1399 test_cmp expected actual
1402 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1403 echo "ab:abc" >expected &&
1405 -c grep.patterntype=fixed \
1406 -c grep.patterntype=basic \
1407 -c grep.patterntype=extended \
1408 grep "a+b*c" ab >actual &&
1409 test_cmp expected actual
1412 test_expect_success 'grep -G -F -P -E pattern' '
1413 echo "d0:d" >expected &&
1414 git grep -G -F -P -E "[\d]" d0 >actual &&
1415 test_cmp expected actual
1418 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1419 echo "d0:d" >expected &&
1421 -c grep.patterntype=fixed \
1422 -c grep.patterntype=basic \
1423 -c grep.patterntype=perl \
1424 -c grep.patterntype=extended \
1425 grep "[\d]" d0 >actual &&
1426 test_cmp expected actual
1429 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1430 echo "d0:0" >expected &&
1431 git grep -G -F -E -P "[\d]" d0 >actual &&
1432 test_cmp expected actual
1435 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1436 echo "d0:0" >expected &&
1438 -c grep.patterntype=fixed \
1439 -c grep.patterntype=basic \
1440 -c grep.patterntype=extended \
1441 -c grep.patterntype=perl \
1442 grep "[\d]" d0 >actual &&
1443 test_cmp expected actual
1446 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1447 echo "ab:a+b*c" >expected &&
1449 -c grep.patterntype=fixed \
1450 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1451 test_cmp expected actual
1454 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1455 echo "ab:a+b*c" >expected &&
1457 -c grep.patterntype=basic \
1458 grep -F "*c" ab >actual &&
1459 test_cmp expected actual
1462 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1468 -c grep.patterntype=fixed \
1469 grep -G "a+b" ab >actual &&
1470 test_cmp expected actual
1473 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1480 -c grep.patterntype=fixed \
1481 grep -E "a+" ab >actual &&
1482 test_cmp expected actual
1486 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1487 hello.c<RED>-<RESET>{
1489 hello.c<RED>:<RESET> /* char ?? */
1490 hello.c<RED>-<RESET>}
1492 hello_world<RED>:<RESET>Hello_world
1493 hello_world<RED>-<RESET>HeLLo_world
1496 test_expect_success 'grep --color, separator' '
1497 test_config color.grep.context normal &&
1498 test_config color.grep.filename normal &&
1499 test_config color.grep.function normal &&
1500 test_config color.grep.linenumber normal &&
1501 test_config color.grep.match normal &&
1502 test_config color.grep.selected normal &&
1503 test_config color.grep.separator red &&
1505 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1506 test_decode_color >actual &&
1507 test_cmp expected actual
1511 hello.c:int main(int argc, const char **argv)
1512 hello.c: /* char ?? */
1514 hello_world:Hello_world
1517 test_expect_success 'grep --break' '
1518 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1519 test_cmp expected actual
1523 hello.c:int main(int argc, const char **argv)
1526 hello.c: /* char ?? */
1529 hello_world:Hello_world
1530 hello_world-HeLLo_world
1533 test_expect_success 'grep --break with context' '
1534 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1535 test_cmp expected actual
1540 int main(int argc, const char **argv)
1546 test_expect_success 'grep --heading' '
1547 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1548 test_cmp expected actual
1552 <BOLD;GREEN>hello.c<RESET>
1553 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1554 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1556 <BOLD;GREEN>hello_world<RESET>
1557 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1560 test_expect_success 'mimic ack-grep --group' '
1561 test_config color.grep.context normal &&
1562 test_config color.grep.filename "bold green" &&
1563 test_config color.grep.function normal &&
1564 test_config color.grep.linenumber normal &&
1565 test_config color.grep.match "black yellow" &&
1566 test_config color.grep.selected normal &&
1567 test_config color.grep.separator normal &&
1569 git grep --break --heading -n --color \
1570 -e char -e lo_w hello.c hello_world |
1571 test_decode_color >actual &&
1572 test_cmp expected actual
1576 space: line with leading space1
1577 space: line with leading space2
1578 space: line with leading space3
1581 test_expect_success PCRE 'grep -E "^ "' '
1582 git grep -E "^ " space >actual &&
1583 test_cmp expected actual
1586 test_expect_success PCRE 'grep -P "^ "' '
1587 git grep -P "^ " space >actual &&
1588 test_cmp expected actual
1592 space-line without leading space1
1593 space: line <RED>with <RESET>leading space1
1594 space: line <RED>with <RESET>leading <RED>space2<RESET>
1595 space: line <RED>with <RESET>leading space3
1596 space:line without leading <RED>space2<RESET>
1599 test_expect_success 'grep --color -e A -e B with context' '
1600 test_config color.grep.context normal &&
1601 test_config color.grep.filename normal &&
1602 test_config color.grep.function normal &&
1603 test_config color.grep.linenumber normal &&
1604 test_config color.grep.matchContext normal &&
1605 test_config color.grep.matchSelected red &&
1606 test_config color.grep.selected normal &&
1607 test_config color.grep.separator normal &&
1609 git grep --color=always -C2 -e "with " -e space2 space |
1610 test_decode_color >actual &&
1611 test_cmp expected actual
1615 space-line without leading space1
1616 space- line with leading space1
1617 space: line <RED>with <RESET>leading <RED>space2<RESET>
1618 space- line with leading space3
1619 space-line without leading space2
1622 test_expect_success 'grep --color -e A --and -e B with context' '
1623 test_config color.grep.context normal &&
1624 test_config color.grep.filename normal &&
1625 test_config color.grep.function normal &&
1626 test_config color.grep.linenumber normal &&
1627 test_config color.grep.matchContext normal &&
1628 test_config color.grep.matchSelected red &&
1629 test_config color.grep.selected normal &&
1630 test_config color.grep.separator normal &&
1632 git grep --color=always -C2 -e "with " --and -e space2 space |
1633 test_decode_color >actual &&
1634 test_cmp expected actual
1638 space-line without leading space1
1639 space: line <RED>with <RESET>leading space1
1640 space- line with leading space2
1641 space: line <RED>with <RESET>leading space3
1642 space-line without leading space2
1645 test_expect_success 'grep --color -e A --and --not -e B with context' '
1646 test_config color.grep.context normal &&
1647 test_config color.grep.filename normal &&
1648 test_config color.grep.function normal &&
1649 test_config color.grep.linenumber normal &&
1650 test_config color.grep.matchContext normal &&
1651 test_config color.grep.matchSelected red &&
1652 test_config color.grep.selected normal &&
1653 test_config color.grep.separator normal &&
1655 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1656 test_decode_color >actual &&
1657 test_cmp expected actual
1662 hello.c=int main(int argc, const char **argv)
1664 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1666 hello.c- /* char ?? */
1670 test_expect_success 'grep --color -e A --and -e B -p with context' '
1671 test_config color.grep.context normal &&
1672 test_config color.grep.filename normal &&
1673 test_config color.grep.function normal &&
1674 test_config color.grep.linenumber normal &&
1675 test_config color.grep.matchContext normal &&
1676 test_config color.grep.matchSelected red &&
1677 test_config color.grep.selected normal &&
1678 test_config color.grep.separator normal &&
1680 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1681 test_decode_color >actual &&
1682 test_cmp expected actual
1685 test_expect_success 'grep can find things only in the work tree' '
1686 : >work-tree-only &&
1687 git add work-tree-only &&
1688 test_when_finished "git rm -f work-tree-only" &&
1689 echo "find in work tree" >work-tree-only &&
1690 git grep --quiet "find in work tree" &&
1691 test_must_fail git grep --quiet --cached "find in work tree" &&
1692 test_must_fail git grep --quiet "find in work tree" HEAD
1695 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1696 echo "intend to add this" >intend-to-add &&
1697 git add -N intend-to-add &&
1698 test_when_finished "git rm -f intend-to-add" &&
1699 git grep --quiet "intend to add this" &&
1700 test_must_fail git grep --quiet --cached "intend to add this" &&
1701 test_must_fail git grep --quiet "intend to add this" HEAD
1704 test_expect_success 'grep does not search work tree with assume unchanged' '
1705 echo "intend to add this" >intend-to-add &&
1706 git add -N intend-to-add &&
1707 git update-index --assume-unchanged intend-to-add &&
1708 test_when_finished "git rm -f intend-to-add" &&
1709 test_must_fail git grep --quiet "intend to add this" &&
1710 test_must_fail git grep --quiet --cached "intend to add this" &&
1711 test_must_fail git grep --quiet "intend to add this" HEAD
1714 test_expect_success 'grep can find things only in the index' '
1715 echo "only in the index" >cache-this &&
1716 git add cache-this &&
1718 test_when_finished "git rm --cached cache-this" &&
1719 test_must_fail git grep --quiet "only in the index" &&
1720 git grep --quiet --cached "only in the index" &&
1721 test_must_fail git grep --quiet "only in the index" HEAD
1724 test_expect_success 'grep does not report i-t-a with -L --cached' '
1725 echo "intend to add this" >intend-to-add &&
1726 git add -N intend-to-add &&
1727 test_when_finished "git rm -f intend-to-add" &&
1728 git ls-files | grep -v "^intend-to-add\$" >expected &&
1729 git grep -L --cached "nonexistent_string" >actual &&
1730 test_cmp expected actual
1733 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1734 echo "intend to add this" >intend-to-add-assume-unchanged &&
1735 git add -N intend-to-add-assume-unchanged &&
1736 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1737 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1738 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1739 git grep -L "nonexistent_string" >actual &&
1740 test_cmp expected actual