grep: add a test asserting that --perl-regexp dies when !PCRE
[alt-git.git] / t / t7810-grep.sh
blob8d69113695ef600057c92144633cf00cb6bb94ef
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git grep various.
9 . ./test-lib.sh
11 cat >hello.c <<EOF
12 #include <assert.h>
13 #include <stdio.h>
15 int main(int argc, const char **argv)
17 printf("Hello world.\n");
18 return 0;
19 /* char ?? */
21 EOF
23 test_expect_success setup '
25 echo foo mmap bar
26 echo foo_mmap bar
27 echo foo_mmap bar mmap
28 echo foo mmap bar_mmap
29 echo foo_mmap bar mmap baz
30 } >file &&
32 echo Hello world
33 echo HeLLo world
34 echo Hello_world
35 echo HeLLo_world
36 } >hello_world &&
38 echo "a+b*c"
39 echo "a+bc"
40 echo "abc"
41 } >ab &&
43 echo d &&
44 echo 0
45 } >d0 &&
46 echo vvv >v &&
47 echo ww w >w &&
48 echo x x xx x >x &&
49 echo y yy >y &&
50 echo zzz > z &&
51 mkdir t &&
52 echo test >t/t &&
53 echo vvv >t/v &&
54 mkdir t/a &&
55 echo vvv >t/a/v &&
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"
62 } >space &&
63 git add . &&
64 test_tick &&
65 git commit -m initial
68 test_expect_success 'grep should not segfault with a bad input' '
69 test_must_fail git grep "("
72 for H in HEAD ''
74 case "$H" in
75 HEAD) HC='HEAD:' L='HEAD' ;;
76 '') HC= L='in working tree' ;;
77 esac
79 test_expect_success "grep -w $L" '
81 echo ${HC}file:1:foo mmap bar
82 echo ${HC}file:3:foo_mmap bar mmap
83 echo ${HC}file:4:foo mmap bar_mmap
84 echo ${HC}file:5:foo_mmap bar mmap baz
85 } >expected &&
86 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
87 test_cmp expected actual
90 test_expect_success "grep -w $L" '
92 echo ${HC}file:1:foo mmap bar
93 echo ${HC}file:3:foo_mmap bar mmap
94 echo ${HC}file:4:foo mmap bar_mmap
95 echo ${HC}file:5:foo_mmap bar mmap baz
96 } >expected &&
97 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
98 test_cmp expected actual
101 test_expect_success "grep -w $L" '
103 echo ${HC}file:foo mmap bar
104 echo ${HC}file:foo_mmap bar mmap
105 echo ${HC}file:foo mmap bar_mmap
106 echo ${HC}file:foo_mmap bar mmap baz
107 } >expected &&
108 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
109 test_cmp expected actual
112 test_expect_success "grep -w $L (w)" '
113 : >expected &&
114 test_must_fail git grep -n -w -e "^w" $H >actual &&
115 test_cmp expected actual
118 test_expect_success "grep -w $L (x)" '
120 echo ${HC}x:1:x x xx x
121 } >expected &&
122 git grep -n -w -e "x xx* x" $H >actual &&
123 test_cmp expected actual
126 test_expect_success "grep -w $L (y-1)" '
128 echo ${HC}y:1:y yy
129 } >expected &&
130 git grep -n -w -e "^y" $H >actual &&
131 test_cmp expected actual
134 test_expect_success "grep -w $L (y-2)" '
135 : >expected &&
136 if git grep -n -w -e "^y y" $H >actual
137 then
138 echo should not have matched
139 cat actual
140 false
141 else
142 test_cmp expected actual
146 test_expect_success "grep -w $L (z)" '
147 : >expected &&
148 if git grep -n -w -e "^z" $H >actual
149 then
150 echo should not have matched
151 cat actual
152 false
153 else
154 test_cmp expected actual
158 test_expect_success "grep $L (t-1)" '
159 echo "${HC}t/t:1:test" >expected &&
160 git grep -n -e test $H >actual &&
161 test_cmp expected actual
164 test_expect_success "grep $L (t-2)" '
165 echo "${HC}t:1:test" >expected &&
167 cd t &&
168 git grep -n -e test $H
169 ) >actual &&
170 test_cmp expected actual
173 test_expect_success "grep $L (t-3)" '
174 echo "${HC}t/t:1:test" >expected &&
176 cd t &&
177 git grep --full-name -n -e test $H
178 ) >actual &&
179 test_cmp expected actual
182 test_expect_success "grep -c $L (no /dev/null)" '
183 ! git grep -c test $H | grep /dev/null
186 test_expect_success "grep --max-depth -1 $L" '
188 echo ${HC}t/a/v:1:vvv
189 echo ${HC}t/v:1:vvv
190 echo ${HC}v:1:vvv
191 } >expected &&
192 git grep --max-depth -1 -n -e vvv $H >actual &&
193 test_cmp expected actual
196 test_expect_success "grep --max-depth 0 $L" '
198 echo ${HC}v:1:vvv
199 } >expected &&
200 git grep --max-depth 0 -n -e vvv $H >actual &&
201 test_cmp expected actual
204 test_expect_success "grep --max-depth 0 -- '*' $L" '
206 echo ${HC}t/a/v:1:vvv
207 echo ${HC}t/v:1:vvv
208 echo ${HC}v:1:vvv
209 } >expected &&
210 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
211 test_cmp expected actual
214 test_expect_success "grep --max-depth 1 $L" '
216 echo ${HC}t/v:1:vvv
217 echo ${HC}v:1:vvv
218 } >expected &&
219 git grep --max-depth 1 -n -e vvv $H >actual &&
220 test_cmp expected actual
223 test_expect_success "grep --max-depth 0 -- t $L" '
225 echo ${HC}t/v:1:vvv
226 } >expected &&
227 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
228 test_cmp expected actual
231 test_expect_success "grep --max-depth 0 -- . t $L" '
233 echo ${HC}t/v:1:vvv
234 echo ${HC}v:1:vvv
235 } >expected &&
236 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
237 test_cmp expected actual
240 test_expect_success "grep --max-depth 0 -- t . $L" '
242 echo ${HC}t/v:1:vvv
243 echo ${HC}v:1:vvv
244 } >expected &&
245 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
246 test_cmp expected actual
248 test_expect_success "grep $L with grep.extendedRegexp=false" '
249 echo "${HC}ab:a+bc" >expected &&
250 git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
251 test_cmp expected actual
254 test_expect_success "grep $L with grep.extendedRegexp=true" '
255 echo "${HC}ab:abc" >expected &&
256 git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
257 test_cmp expected actual
260 test_expect_success "grep $L with grep.patterntype=basic" '
261 echo "${HC}ab:a+bc" >expected &&
262 git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
263 test_cmp expected actual
266 test_expect_success "grep $L with grep.patterntype=extended" '
267 echo "${HC}ab:abc" >expected &&
268 git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
269 test_cmp expected actual
272 test_expect_success "grep $L with grep.patterntype=fixed" '
273 echo "${HC}ab:a+b*c" >expected &&
274 git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
275 test_cmp expected actual
278 test_expect_success PCRE "grep $L with grep.patterntype=perl" '
279 echo "${HC}ab:a+b*c" >expected &&
280 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
281 test_cmp expected actual
284 test_expect_success !PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
285 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
288 test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
289 echo "${HC}ab:abc" >expected &&
290 git \
291 -c grep.patternType=default \
292 -c grep.extendedRegexp=true \
293 grep "a+b*c" $H ab >actual &&
294 test_cmp expected actual
297 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
298 echo "${HC}ab:abc" >expected &&
299 git \
300 -c grep.extendedRegexp=true \
301 -c grep.patternType=default \
302 grep "a+b*c" $H ab >actual &&
303 test_cmp expected actual
306 test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
307 echo "${HC}ab:abc" >expected &&
308 git \
309 -c grep.patternType=extended \
310 -c grep.extendedRegexp=false \
311 grep "a+b*c" $H ab >actual &&
312 test_cmp expected actual
315 test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
316 echo "${HC}ab:a+bc" >expected &&
317 git \
318 -c grep.patternType=basic \
319 -c grep.extendedRegexp=true \
320 grep "a+b*c" $H ab >actual &&
321 test_cmp expected actual
324 test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
325 echo "${HC}ab:abc" >expected &&
326 git \
327 -c grep.extendedRegexp=false \
328 -c grep.patternType=extended \
329 grep "a+b*c" $H ab >actual &&
330 test_cmp expected actual
333 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
334 echo "${HC}ab:a+bc" >expected &&
335 git \
336 -c grep.extendedRegexp=true \
337 -c grep.patternType=basic \
338 grep "a+b*c" $H ab >actual &&
339 test_cmp expected actual
342 test_expect_success "grep --count $L" '
343 echo ${HC}ab:3 >expected &&
344 git grep --count -e b $H -- ab >actual &&
345 test_cmp expected actual
348 test_expect_success "grep --count -h $L" '
349 echo 3 >expected &&
350 git grep --count -h -e b $H -- ab >actual &&
351 test_cmp expected actual
353 done
355 cat >expected <<EOF
356 file
358 test_expect_success 'grep -l -C' '
359 git grep -l -C1 foo >actual &&
360 test_cmp expected actual
363 cat >expected <<EOF
364 file:5
366 test_expect_success 'grep -c -C' '
367 git grep -c -C1 foo >actual &&
368 test_cmp expected actual
371 test_expect_success 'grep -L -C' '
372 git ls-files >expected &&
373 git grep -L -C1 nonexistent_string >actual &&
374 test_cmp expected actual
377 cat >expected <<EOF
378 file:foo mmap bar_mmap
381 test_expect_success 'grep -e A --and -e B' '
382 git grep -e "foo mmap" --and -e bar_mmap >actual &&
383 test_cmp expected actual
386 cat >expected <<EOF
387 file:foo_mmap bar mmap
388 file:foo_mmap bar mmap baz
392 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
393 git grep \( -e foo_ --or -e baz \) \
394 --and -e " mmap" >actual &&
395 test_cmp expected actual
398 cat >expected <<EOF
399 file:foo mmap bar
402 test_expect_success 'grep -e A --and --not -e B' '
403 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
404 test_cmp expected actual
407 test_expect_success 'grep should ignore GREP_OPTIONS' '
408 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
409 test_cmp expected actual
412 test_expect_success 'grep -f, non-existent file' '
413 test_must_fail git grep -f patterns
416 cat >expected <<EOF
417 file:foo mmap bar
418 file:foo_mmap bar
419 file:foo_mmap bar mmap
420 file:foo mmap bar_mmap
421 file:foo_mmap bar mmap baz
424 cat >pattern <<EOF
425 mmap
428 test_expect_success 'grep -f, one pattern' '
429 git grep -f pattern >actual &&
430 test_cmp expected actual
433 cat >expected <<EOF
434 file:foo mmap bar
435 file:foo_mmap bar
436 file:foo_mmap bar mmap
437 file:foo mmap bar_mmap
438 file:foo_mmap bar mmap baz
439 t/a/v:vvv
440 t/v:vvv
441 v:vvv
444 cat >patterns <<EOF
445 mmap
449 test_expect_success 'grep -f, multiple patterns' '
450 git grep -f patterns >actual &&
451 test_cmp expected actual
454 test_expect_success 'grep, multiple patterns' '
455 git grep "$(cat patterns)" >actual &&
456 test_cmp expected actual
459 cat >expected <<EOF
460 file:foo mmap bar
461 file:foo_mmap bar
462 file:foo_mmap bar mmap
463 file:foo mmap bar_mmap
464 file:foo_mmap bar mmap baz
465 t/a/v:vvv
466 t/v:vvv
467 v:vvv
470 cat >patterns <<EOF
472 mmap
478 test_expect_success 'grep -f, ignore empty lines' '
479 git grep -f patterns >actual &&
480 test_cmp expected actual
483 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
484 git grep -f - <patterns >actual &&
485 test_cmp expected actual
488 cat >expected <<EOF
489 y:y yy
491 z:zzz
494 test_expect_success 'grep -q, silently report matches' '
495 >empty &&
496 git grep -q mmap >actual &&
497 test_cmp empty actual &&
498 test_must_fail git grep -q qfwfq >actual &&
499 test_cmp empty actual
502 test_expect_success 'grep -C1 hunk mark between files' '
503 git grep -C1 "^[yz]" >actual &&
504 test_cmp expected actual
507 test_expect_success 'log grep setup' '
508 echo a >>file &&
509 test_tick &&
510 GIT_AUTHOR_NAME="With * Asterisk" \
511 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
512 git commit -a -m "second" &&
514 echo a >>file &&
515 test_tick &&
516 git commit -a -m "third" &&
518 echo a >>file &&
519 test_tick &&
520 GIT_AUTHOR_NAME="Night Fall" \
521 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
522 git commit -a -m "fourth"
525 test_expect_success 'log grep (1)' '
526 git log --author=author --pretty=tformat:%s >actual &&
528 echo third && echo initial
529 } >expect &&
530 test_cmp expect actual
533 test_expect_success 'log grep (2)' '
534 git log --author=" * " -F --pretty=tformat:%s >actual &&
536 echo second
537 } >expect &&
538 test_cmp expect actual
541 test_expect_success 'log grep (3)' '
542 git log --author="^A U" --pretty=tformat:%s >actual &&
544 echo third && echo initial
545 } >expect &&
546 test_cmp expect actual
549 test_expect_success 'log grep (4)' '
550 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
552 echo second
553 } >expect &&
554 test_cmp expect actual
557 test_expect_success 'log grep (5)' '
558 git log --author=Thor -F --pretty=tformat:%s >actual &&
560 echo third && echo initial
561 } >expect &&
562 test_cmp expect actual
565 test_expect_success 'log grep (6)' '
566 git log --author=-0700 --pretty=tformat:%s >actual &&
567 >expect &&
568 test_cmp expect actual
571 test_expect_success 'log grep (7)' '
572 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
573 echo third >expect &&
574 test_cmp expect actual
577 test_expect_success 'log grep (8)' '
578 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
580 echo third && echo second
581 } >expect &&
582 test_cmp expect actual
585 test_expect_success 'log grep (9)' '
586 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
587 echo third >expect &&
588 test_cmp expect actual
591 test_expect_success 'log grep (9)' '
592 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
593 : >expect &&
594 test_cmp expect actual
597 test_expect_success 'log --grep-reflog can only be used under -g' '
598 test_must_fail git log --grep-reflog="commit: third"
601 test_expect_success 'log with multiple --grep uses union' '
602 git log --grep=i --grep=r --format=%s >actual &&
604 echo fourth && echo third && echo initial
605 } >expect &&
606 test_cmp expect actual
609 test_expect_success 'log --all-match with multiple --grep uses intersection' '
610 git log --all-match --grep=i --grep=r --format=%s >actual &&
612 echo third
613 } >expect &&
614 test_cmp expect actual
617 test_expect_success 'log with multiple --author uses union' '
618 git log --author="Thor" --author="Aster" --format=%s >actual &&
620 echo third && echo second && echo initial
621 } >expect &&
622 test_cmp expect actual
625 test_expect_success 'log --all-match with multiple --author still uses union' '
626 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
628 echo third && echo second && echo initial
629 } >expect &&
630 test_cmp expect actual
633 test_expect_success 'log --grep --author uses intersection' '
634 # grep matches only third and fourth
635 # author matches only initial and third
636 git log --author="A U Thor" --grep=r --format=%s >actual &&
638 echo third
639 } >expect &&
640 test_cmp expect actual
643 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
644 # grep matches initial and second but not third
645 # author matches only initial and third
646 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
648 echo initial
649 } >expect &&
650 test_cmp expect actual
653 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
654 # grep matches only initial and third
655 # author matches all but second
656 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
658 echo third && echo initial
659 } >expect &&
660 test_cmp expect actual
663 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
664 # grep matches only initial and third
665 # author matches all but second
666 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
668 echo third && echo initial
669 } >expect &&
670 test_cmp expect actual
673 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
674 # grep matches only third
675 # author matches only initial and third
676 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
678 echo third
679 } >expect &&
680 test_cmp expect actual
683 test_expect_success 'log --author does not search in timestamp' '
684 : >expect &&
685 git log --author="$GIT_AUTHOR_DATE" >actual &&
686 test_cmp expect actual
689 test_expect_success 'log --committer does not search in timestamp' '
690 : >expect &&
691 git log --committer="$GIT_COMMITTER_DATE" >actual &&
692 test_cmp expect actual
695 test_expect_success 'grep with CE_VALID file' '
696 git update-index --assume-unchanged t/t &&
697 rm t/t &&
698 test "$(git grep test)" = "t/t:test" &&
699 git update-index --no-assume-unchanged t/t &&
700 git checkout t/t
703 cat >expected <<EOF
704 hello.c=#include <stdio.h>
705 hello.c: return 0;
708 test_expect_success 'grep -p with userdiff' '
709 git config diff.custom.funcname "^#" &&
710 echo "hello.c diff=custom" >.gitattributes &&
711 git grep -p return >actual &&
712 test_cmp expected actual
715 cat >expected <<EOF
716 hello.c=int main(int argc, const char **argv)
717 hello.c: return 0;
720 test_expect_success 'grep -p' '
721 rm -f .gitattributes &&
722 git grep -p return >actual &&
723 test_cmp expected actual
726 cat >expected <<EOF
727 hello.c-#include <stdio.h>
728 hello.c-
729 hello.c=int main(int argc, const char **argv)
730 hello.c-{
731 hello.c- printf("Hello world.\n");
732 hello.c: return 0;
735 test_expect_success 'grep -p -B5' '
736 git grep -p -B5 return >actual &&
737 test_cmp expected actual
740 cat >expected <<EOF
741 hello.c=int main(int argc, const char **argv)
742 hello.c-{
743 hello.c- printf("Hello world.\n");
744 hello.c: return 0;
745 hello.c- /* char ?? */
746 hello.c-}
749 test_expect_success 'grep -W' '
750 git grep -W return >actual &&
751 test_cmp expected actual
754 cat >expected <<EOF
755 hello.c-#include <assert.h>
756 hello.c:#include <stdio.h>
759 test_expect_success 'grep -W shows no trailing empty lines' '
760 git grep -W stdio >actual &&
761 test_cmp expected actual
764 cat >expected <<EOF
765 hello.c= printf("Hello world.\n");
766 hello.c: return 0;
767 hello.c- /* char ?? */
770 test_expect_success 'grep -W with userdiff' '
771 test_when_finished "rm -f .gitattributes" &&
772 git config diff.custom.xfuncname "(printf.*|})$" &&
773 echo "hello.c diff=custom" >.gitattributes &&
774 git grep -W return >actual &&
775 test_cmp expected actual
778 test_expect_success 'grep from a subdirectory to search wider area (1)' '
779 mkdir -p s &&
781 cd s && git grep "x x x" ..
785 test_expect_success 'grep from a subdirectory to search wider area (2)' '
786 mkdir -p s &&
788 cd s || exit 1
789 ( git grep xxyyzz .. >out ; echo $? >status )
790 ! test -s out &&
791 test 1 = $(cat status)
795 cat >expected <<EOF
796 hello.c:int main(int argc, const char **argv)
799 test_expect_success 'grep -Fi' '
800 git grep -Fi "CHAR *" >actual &&
801 test_cmp expected actual
804 test_expect_success 'outside of git repository' '
805 rm -fr non &&
806 mkdir -p non/git/sub &&
807 echo hello >non/git/file1 &&
808 echo world >non/git/sub/file2 &&
810 echo file1:hello &&
811 echo sub/file2:world
812 } >non/expect.full &&
813 echo file2:world >non/expect.sub &&
815 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
816 export GIT_CEILING_DIRECTORIES &&
817 cd non/git &&
818 test_must_fail git grep o &&
819 git grep --no-index o >../actual.full &&
820 test_cmp ../expect.full ../actual.full &&
821 cd sub &&
822 test_must_fail git grep o &&
823 git grep --no-index o >../../actual.sub &&
824 test_cmp ../../expect.sub ../../actual.sub
825 ) &&
827 echo ".*o*" >non/git/.gitignore &&
829 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
830 export GIT_CEILING_DIRECTORIES &&
831 cd non/git &&
832 test_must_fail git grep o &&
833 git grep --no-index --exclude-standard o >../actual.full &&
834 test_cmp ../expect.full ../actual.full &&
837 echo ".gitignore:.*o*" &&
838 cat ../expect.full
839 } >../expect.with.ignored &&
840 git grep --no-index --no-exclude o >../actual.full &&
841 test_cmp ../expect.with.ignored ../actual.full
845 test_expect_success 'outside of git repository with fallbackToNoIndex' '
846 rm -fr non &&
847 mkdir -p non/git/sub &&
848 echo hello >non/git/file1 &&
849 echo world >non/git/sub/file2 &&
850 cat <<-\EOF >non/expect.full &&
851 file1:hello
852 sub/file2:world
854 echo file2:world >non/expect.sub &&
856 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
857 export GIT_CEILING_DIRECTORIES &&
858 cd non/git &&
859 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
860 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
861 test_cmp ../expect.full ../actual.full &&
862 cd sub &&
863 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
864 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
865 test_cmp ../../expect.sub ../../actual.sub
866 ) &&
868 echo ".*o*" >non/git/.gitignore &&
870 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
871 export GIT_CEILING_DIRECTORIES &&
872 cd non/git &&
873 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
874 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
875 test_cmp ../expect.full ../actual.full &&
878 echo ".gitignore:.*o*" &&
879 cat ../expect.full
880 } >../expect.with.ignored &&
881 git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
882 test_cmp ../expect.with.ignored ../actual.full
886 test_expect_success 'inside git repository but with --no-index' '
887 rm -fr is &&
888 mkdir -p is/git/sub &&
889 echo hello >is/git/file1 &&
890 echo world >is/git/sub/file2 &&
891 echo ".*o*" >is/git/.gitignore &&
893 echo file1:hello &&
894 echo sub/file2:world
895 } >is/expect.unignored &&
897 echo ".gitignore:.*o*" &&
898 cat is/expect.unignored
899 } >is/expect.full &&
900 : >is/expect.empty &&
901 echo file2:world >is/expect.sub &&
903 cd is/git &&
904 git init &&
905 test_must_fail git grep o >../actual.full &&
906 test_cmp ../expect.empty ../actual.full &&
908 git grep --untracked o >../actual.unignored &&
909 test_cmp ../expect.unignored ../actual.unignored &&
911 git grep --no-index o >../actual.full &&
912 test_cmp ../expect.full ../actual.full &&
914 git grep --no-index --exclude-standard o >../actual.unignored &&
915 test_cmp ../expect.unignored ../actual.unignored &&
917 cd sub &&
918 test_must_fail git grep o >../../actual.sub &&
919 test_cmp ../../expect.empty ../../actual.sub &&
921 git grep --no-index o >../../actual.sub &&
922 test_cmp ../../expect.sub ../../actual.sub &&
924 git grep --untracked o >../../actual.sub &&
925 test_cmp ../../expect.sub ../../actual.sub
929 test_expect_success 'grep --no-index descends into repos, but not .git' '
930 rm -fr non &&
931 mkdir -p non/git &&
933 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
934 export GIT_CEILING_DIRECTORIES &&
935 cd non/git &&
937 echo magic >file &&
938 git init repo &&
940 cd repo &&
941 echo magic >file &&
942 git add file &&
943 git commit -m foo &&
944 echo magic >.git/file
945 ) &&
947 cat >expect <<-\EOF &&
948 file
949 repo/file
951 git grep -l --no-index magic >actual &&
952 test_cmp expect actual
956 test_expect_success 'setup double-dash tests' '
957 cat >double-dash <<EOF &&
960 other
962 git add double-dash
965 cat >expected <<EOF
966 double-dash:->
968 test_expect_success 'grep -- pattern' '
969 git grep -- "->" >actual &&
970 test_cmp expected actual
972 test_expect_success 'grep -- pattern -- pathspec' '
973 git grep -- "->" -- double-dash >actual &&
974 test_cmp expected actual
976 test_expect_success 'grep -e pattern -- path' '
977 git grep -e "->" -- double-dash >actual &&
978 test_cmp expected actual
981 cat >expected <<EOF
982 double-dash:--
984 test_expect_success 'grep -e -- -- path' '
985 git grep -e -- -- double-dash >actual &&
986 test_cmp expected actual
989 test_expect_success 'dashdash disambiguates rev as rev' '
990 test_when_finished "rm -f master" &&
991 echo content >master &&
992 echo master:hello.c >expect &&
993 git grep -l o master -- hello.c >actual &&
994 test_cmp expect actual
997 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
998 test_when_finished "git rm -f master" &&
999 echo content >master &&
1000 git add master &&
1001 echo master:content >expect &&
1002 git grep o -- master >actual &&
1003 test_cmp expect actual
1006 test_expect_success 'report bogus arg without dashdash' '
1007 test_must_fail git grep o does-not-exist
1010 test_expect_success 'report bogus rev with dashdash' '
1011 test_must_fail git grep o hello.c --
1014 test_expect_success 'allow non-existent path with dashdash' '
1015 # We need a real match so grep exits with success.
1016 tree=$(git ls-tree HEAD |
1017 sed s/hello.c/not-in-working-tree/ |
1018 git mktree) &&
1019 git grep o "$tree" -- not-in-working-tree
1022 test_expect_success 'grep --no-index pattern -- path' '
1023 rm -fr non &&
1024 mkdir -p non/git &&
1026 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1027 export GIT_CEILING_DIRECTORIES &&
1028 cd non/git &&
1029 echo hello >hello &&
1030 echo goodbye >goodbye &&
1031 echo hello:hello >expect &&
1032 git grep --no-index o -- hello >actual &&
1033 test_cmp expect actual
1037 test_expect_success 'grep --no-index complains of revs' '
1038 test_must_fail git grep --no-index o master -- 2>err &&
1039 test_i18ngrep "cannot be used with revs" err
1042 test_expect_success 'grep --no-index prefers paths to revs' '
1043 test_when_finished "rm -f master" &&
1044 echo content >master &&
1045 echo master:content >expect &&
1046 git grep --no-index o master >actual &&
1047 test_cmp expect actual
1050 test_expect_success 'grep --no-index does not "diagnose" revs' '
1051 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1052 test_i18ngrep ! -i "did you mean" err
1055 cat >expected <<EOF
1056 hello.c:int main(int argc, const char **argv)
1057 hello.c: printf("Hello world.\n");
1060 test_expect_success PCRE 'grep --perl-regexp pattern' '
1061 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1062 test_cmp expected actual
1065 test_expect_success !PCRE 'grep --perl-regexp pattern errors without PCRE' '
1066 test_must_fail git grep --perl-regexp "foo.*bar"
1069 test_expect_success PCRE 'grep -P pattern' '
1070 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1071 test_cmp expected actual
1074 test_expect_success !PCRE 'grep -P pattern errors without PCRE' '
1075 test_must_fail git grep -P "foo.*bar"
1078 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1079 >empty &&
1080 test_must_fail git -c grep.extendedregexp=true \
1081 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1082 test_cmp empty actual
1085 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1086 git -c grep.extendedregexp=true \
1087 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1088 test_cmp expected actual
1091 test_expect_success PCRE 'grep -P -v pattern' '
1093 echo "ab:a+b*c"
1094 echo "ab:a+bc"
1095 } >expected &&
1096 git grep -P -v "abc" ab >actual &&
1097 test_cmp expected actual
1100 test_expect_success PCRE 'grep -P -i pattern' '
1101 cat >expected <<-EOF &&
1102 hello.c: printf("Hello world.\n");
1104 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1105 test_cmp expected actual
1108 test_expect_success PCRE 'grep -P -w pattern' '
1110 echo "hello_world:Hello world"
1111 echo "hello_world:HeLLo world"
1112 } >expected &&
1113 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1114 test_cmp expected actual
1117 test_expect_success 'grep -G invalidpattern properly dies ' '
1118 test_must_fail git grep -G "a["
1121 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1122 test_must_fail git -c grep.patterntype=basic grep "a["
1125 test_expect_success 'grep -E invalidpattern properly dies ' '
1126 test_must_fail git grep -E "a["
1129 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1130 test_must_fail git -c grep.patterntype=extended grep "a["
1133 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1134 test_must_fail git grep -P "a["
1137 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1138 test_must_fail git -c grep.patterntype=perl grep "a["
1141 test_expect_success 'grep -G -E -F pattern' '
1142 echo "ab:a+b*c" >expected &&
1143 git grep -G -E -F "a+b*c" ab >actual &&
1144 test_cmp expected actual
1147 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1148 echo "ab:a+b*c" >expected &&
1149 git \
1150 -c grep.patterntype=basic \
1151 -c grep.patterntype=extended \
1152 -c grep.patterntype=fixed \
1153 grep "a+b*c" ab >actual &&
1154 test_cmp expected actual
1157 test_expect_success 'grep -E -F -G pattern' '
1158 echo "ab:a+bc" >expected &&
1159 git grep -E -F -G "a+b*c" ab >actual &&
1160 test_cmp expected actual
1163 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1164 echo "ab:a+bc" >expected &&
1165 git \
1166 -c grep.patterntype=extended \
1167 -c grep.patterntype=fixed \
1168 -c grep.patterntype=basic \
1169 grep "a+b*c" ab >actual &&
1170 test_cmp expected actual
1173 test_expect_success 'grep -F -G -E pattern' '
1174 echo "ab:abc" >expected &&
1175 git grep -F -G -E "a+b*c" ab >actual &&
1176 test_cmp expected actual
1179 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1180 echo "ab:abc" >expected &&
1181 git \
1182 -c grep.patterntype=fixed \
1183 -c grep.patterntype=basic \
1184 -c grep.patterntype=extended \
1185 grep "a+b*c" ab >actual &&
1186 test_cmp expected actual
1189 test_expect_success 'grep -G -F -P -E pattern' '
1190 echo "d0:d" >expected &&
1191 git grep -G -F -P -E "[\d]" d0 >actual &&
1192 test_cmp expected actual
1195 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1196 echo "d0:d" >expected &&
1197 git \
1198 -c grep.patterntype=fixed \
1199 -c grep.patterntype=basic \
1200 -c grep.patterntype=perl \
1201 -c grep.patterntype=extended \
1202 grep "[\d]" d0 >actual &&
1203 test_cmp expected actual
1206 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1207 echo "d0:0" >expected &&
1208 git grep -G -F -E -P "[\d]" d0 >actual &&
1209 test_cmp expected actual
1212 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1213 echo "d0:0" >expected &&
1214 git \
1215 -c grep.patterntype=fixed \
1216 -c grep.patterntype=basic \
1217 -c grep.patterntype=extended \
1218 -c grep.patterntype=perl \
1219 grep "[\d]" d0 >actual &&
1220 test_cmp expected actual
1223 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1224 echo "ab:a+b*c" >expected &&
1225 git \
1226 -c grep.patterntype=fixed \
1227 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1228 test_cmp expected actual
1231 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1232 echo "ab:a+b*c" >expected &&
1233 git \
1234 -c grep.patterntype=basic \
1235 grep -F "*c" ab >actual &&
1236 test_cmp expected actual
1239 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1241 echo "ab:a+b*c"
1242 echo "ab:a+bc"
1243 } >expected &&
1244 git \
1245 -c grep.patterntype=fixed \
1246 grep -G "a+b" ab >actual &&
1247 test_cmp expected actual
1250 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1252 echo "ab:a+b*c"
1253 echo "ab:a+bc"
1254 echo "ab:abc"
1255 } >expected &&
1256 git \
1257 -c grep.patterntype=fixed \
1258 grep -E "a+" ab >actual &&
1259 test_cmp expected actual
1262 cat >expected <<EOF
1263 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1264 hello.c<RED>-<RESET>{
1265 <RED>--<RESET>
1266 hello.c<RED>:<RESET> /* char ?? */
1267 hello.c<RED>-<RESET>}
1268 <RED>--<RESET>
1269 hello_world<RED>:<RESET>Hello_world
1270 hello_world<RED>-<RESET>HeLLo_world
1273 test_expect_success 'grep --color, separator' '
1274 test_config color.grep.context normal &&
1275 test_config color.grep.filename normal &&
1276 test_config color.grep.function normal &&
1277 test_config color.grep.linenumber normal &&
1278 test_config color.grep.match normal &&
1279 test_config color.grep.selected normal &&
1280 test_config color.grep.separator red &&
1282 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1283 test_decode_color >actual &&
1284 test_cmp expected actual
1287 cat >expected <<EOF
1288 hello.c:int main(int argc, const char **argv)
1289 hello.c: /* char ?? */
1291 hello_world:Hello_world
1294 test_expect_success 'grep --break' '
1295 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1296 test_cmp expected actual
1299 cat >expected <<EOF
1300 hello.c:int main(int argc, const char **argv)
1301 hello.c-{
1303 hello.c: /* char ?? */
1304 hello.c-}
1306 hello_world:Hello_world
1307 hello_world-HeLLo_world
1310 test_expect_success 'grep --break with context' '
1311 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1312 test_cmp expected actual
1315 cat >expected <<EOF
1316 hello.c
1317 int main(int argc, const char **argv)
1318 /* char ?? */
1319 hello_world
1320 Hello_world
1323 test_expect_success 'grep --heading' '
1324 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1325 test_cmp expected actual
1328 cat >expected <<EOF
1329 <BOLD;GREEN>hello.c<RESET>
1330 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1331 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1333 <BOLD;GREEN>hello_world<RESET>
1334 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1337 test_expect_success 'mimic ack-grep --group' '
1338 test_config color.grep.context normal &&
1339 test_config color.grep.filename "bold green" &&
1340 test_config color.grep.function normal &&
1341 test_config color.grep.linenumber normal &&
1342 test_config color.grep.match "black yellow" &&
1343 test_config color.grep.selected normal &&
1344 test_config color.grep.separator normal &&
1346 git grep --break --heading -n --color \
1347 -e char -e lo_w hello.c hello_world |
1348 test_decode_color >actual &&
1349 test_cmp expected actual
1352 cat >expected <<EOF
1353 space: line with leading space1
1354 space: line with leading space2
1355 space: line with leading space3
1358 test_expect_success PCRE 'grep -E "^ "' '
1359 git grep -E "^ " space >actual &&
1360 test_cmp expected actual
1363 test_expect_success PCRE 'grep -P "^ "' '
1364 git grep -P "^ " space >actual &&
1365 test_cmp expected actual
1368 cat >expected <<EOF
1369 space-line without leading space1
1370 space: line <RED>with <RESET>leading space1
1371 space: line <RED>with <RESET>leading <RED>space2<RESET>
1372 space: line <RED>with <RESET>leading space3
1373 space:line without leading <RED>space2<RESET>
1376 test_expect_success 'grep --color -e A -e B with context' '
1377 test_config color.grep.context normal &&
1378 test_config color.grep.filename normal &&
1379 test_config color.grep.function normal &&
1380 test_config color.grep.linenumber normal &&
1381 test_config color.grep.matchContext normal &&
1382 test_config color.grep.matchSelected red &&
1383 test_config color.grep.selected normal &&
1384 test_config color.grep.separator normal &&
1386 git grep --color=always -C2 -e "with " -e space2 space |
1387 test_decode_color >actual &&
1388 test_cmp expected actual
1391 cat >expected <<EOF
1392 space-line without leading space1
1393 space- line with leading space1
1394 space: line <RED>with <RESET>leading <RED>space2<RESET>
1395 space- line with leading space3
1396 space-line without leading space2
1399 test_expect_success 'grep --color -e A --and -e B with context' '
1400 test_config color.grep.context normal &&
1401 test_config color.grep.filename normal &&
1402 test_config color.grep.function normal &&
1403 test_config color.grep.linenumber normal &&
1404 test_config color.grep.matchContext normal &&
1405 test_config color.grep.matchSelected red &&
1406 test_config color.grep.selected normal &&
1407 test_config color.grep.separator normal &&
1409 git grep --color=always -C2 -e "with " --and -e space2 space |
1410 test_decode_color >actual &&
1411 test_cmp expected actual
1414 cat >expected <<EOF
1415 space-line without leading space1
1416 space: line <RED>with <RESET>leading space1
1417 space- line with leading space2
1418 space: line <RED>with <RESET>leading space3
1419 space-line without leading space2
1422 test_expect_success 'grep --color -e A --and --not -e B with context' '
1423 test_config color.grep.context normal &&
1424 test_config color.grep.filename normal &&
1425 test_config color.grep.function normal &&
1426 test_config color.grep.linenumber normal &&
1427 test_config color.grep.matchContext normal &&
1428 test_config color.grep.matchSelected red &&
1429 test_config color.grep.selected normal &&
1430 test_config color.grep.separator normal &&
1432 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1433 test_decode_color >actual &&
1434 test_cmp expected actual
1437 cat >expected <<EOF
1438 hello.c-
1439 hello.c=int main(int argc, const char **argv)
1440 hello.c-{
1441 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1442 hello.c- return 0;
1443 hello.c- /* char ?? */
1444 hello.c-}
1447 test_expect_success 'grep --color -e A --and -e B -p with context' '
1448 test_config color.grep.context normal &&
1449 test_config color.grep.filename normal &&
1450 test_config color.grep.function normal &&
1451 test_config color.grep.linenumber normal &&
1452 test_config color.grep.matchContext normal &&
1453 test_config color.grep.matchSelected red &&
1454 test_config color.grep.selected normal &&
1455 test_config color.grep.separator normal &&
1457 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1458 test_decode_color >actual &&
1459 test_cmp expected actual
1462 test_expect_success 'grep can find things only in the work tree' '
1463 : >work-tree-only &&
1464 git add work-tree-only &&
1465 test_when_finished "git rm -f work-tree-only" &&
1466 echo "find in work tree" >work-tree-only &&
1467 git grep --quiet "find in work tree" &&
1468 test_must_fail git grep --quiet --cached "find in work tree" &&
1469 test_must_fail git grep --quiet "find in work tree" HEAD
1472 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1473 echo "intend to add this" >intend-to-add &&
1474 git add -N intend-to-add &&
1475 test_when_finished "git rm -f intend-to-add" &&
1476 git grep --quiet "intend to add this" &&
1477 test_must_fail git grep --quiet --cached "intend to add this" &&
1478 test_must_fail git grep --quiet "intend to add this" HEAD
1481 test_expect_success 'grep does not search work tree with assume unchanged' '
1482 echo "intend to add this" >intend-to-add &&
1483 git add -N intend-to-add &&
1484 git update-index --assume-unchanged intend-to-add &&
1485 test_when_finished "git rm -f intend-to-add" &&
1486 test_must_fail git grep --quiet "intend to add this" &&
1487 test_must_fail git grep --quiet --cached "intend to add this" &&
1488 test_must_fail git grep --quiet "intend to add this" HEAD
1491 test_expect_success 'grep can find things only in the index' '
1492 echo "only in the index" >cache-this &&
1493 git add cache-this &&
1494 rm cache-this &&
1495 test_when_finished "git rm --cached cache-this" &&
1496 test_must_fail git grep --quiet "only in the index" &&
1497 git grep --quiet --cached "only in the index" &&
1498 test_must_fail git grep --quiet "only in the index" HEAD
1501 test_expect_success 'grep does not report i-t-a with -L --cached' '
1502 echo "intend to add this" >intend-to-add &&
1503 git add -N intend-to-add &&
1504 test_when_finished "git rm -f intend-to-add" &&
1505 git ls-files | grep -v "^intend-to-add\$" >expected &&
1506 git grep -L --cached "nonexistent_string" >actual &&
1507 test_cmp expected actual
1510 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1511 echo "intend to add this" >intend-to-add-assume-unchanged &&
1512 git add -N intend-to-add-assume-unchanged &&
1513 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1514 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1515 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1516 git grep -L "nonexistent_string" >actual &&
1517 test_cmp expected actual
1520 test_done