t7810: avoid assumption about invalid regex syntax
[git/gitweb.git] / t / t7810-grep.sh
blobd69f76bf906e7b302f4f430adfd8c55589ef1260
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 <stdio.h>
13 int main(int argc, const char **argv)
15 printf("Hello world.\n");
16 return 0;
17 /* char ?? */
19 EOF
21 test_expect_success setup '
23 echo foo mmap bar
24 echo foo_mmap bar
25 echo foo_mmap bar mmap
26 echo foo mmap bar_mmap
27 echo foo_mmap bar mmap baz
28 } >file &&
30 echo Hello world
31 echo HeLLo world
32 echo Hello_world
33 echo HeLLo_world
34 } >hello_world &&
36 echo "a+b*c"
37 echo "a+bc"
38 echo "abc"
39 } >ab &&
41 echo d &&
42 echo 0
43 } >d0 &&
44 echo vvv >v &&
45 echo ww w >w &&
46 echo x x xx x >x &&
47 echo y yy >y &&
48 echo zzz > z &&
49 mkdir t &&
50 echo test >t/t &&
51 echo vvv >t/v &&
52 mkdir t/a &&
53 echo vvv >t/a/v &&
55 echo "line without leading space1"
56 echo " line with leading space1"
57 echo " line with leading space2"
58 echo " line with leading space3"
59 echo "line without leading space2"
60 } >space &&
61 git add . &&
62 test_tick &&
63 git commit -m initial
66 test_expect_success 'grep should not segfault with a bad input' '
67 test_must_fail git grep "("
70 for H in HEAD ''
72 case "$H" in
73 HEAD) HC='HEAD:' L='HEAD' ;;
74 '') HC= L='in working tree' ;;
75 esac
77 test_expect_success "grep -w $L" '
79 echo ${HC}file:1:foo mmap bar
80 echo ${HC}file:3:foo_mmap bar mmap
81 echo ${HC}file:4:foo mmap bar_mmap
82 echo ${HC}file:5:foo_mmap bar mmap baz
83 } >expected &&
84 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
85 test_cmp expected actual
88 test_expect_success "grep -w $L" '
90 echo ${HC}file:1:foo mmap bar
91 echo ${HC}file:3:foo_mmap bar mmap
92 echo ${HC}file:4:foo mmap bar_mmap
93 echo ${HC}file:5:foo_mmap bar mmap baz
94 } >expected &&
95 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
96 test_cmp expected actual
99 test_expect_success "grep -w $L" '
101 echo ${HC}file:foo mmap bar
102 echo ${HC}file:foo_mmap bar mmap
103 echo ${HC}file:foo mmap bar_mmap
104 echo ${HC}file:foo_mmap bar mmap baz
105 } >expected &&
106 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
107 test_cmp expected actual
110 test_expect_success "grep -w $L (w)" '
111 : >expected &&
112 test_must_fail git grep -n -w -e "^w" $H >actual &&
113 test_cmp expected actual
116 test_expect_success "grep -w $L (x)" '
118 echo ${HC}x:1:x x xx x
119 } >expected &&
120 git grep -n -w -e "x xx* x" $H >actual &&
121 test_cmp expected actual
124 test_expect_success "grep -w $L (y-1)" '
126 echo ${HC}y:1:y yy
127 } >expected &&
128 git grep -n -w -e "^y" $H >actual &&
129 test_cmp expected actual
132 test_expect_success "grep -w $L (y-2)" '
133 : >expected &&
134 if git grep -n -w -e "^y y" $H >actual
135 then
136 echo should not have matched
137 cat actual
138 false
139 else
140 test_cmp expected actual
144 test_expect_success "grep -w $L (z)" '
145 : >expected &&
146 if git grep -n -w -e "^z" $H >actual
147 then
148 echo should not have matched
149 cat actual
150 false
151 else
152 test_cmp expected actual
156 test_expect_success "grep $L (t-1)" '
157 echo "${HC}t/t:1:test" >expected &&
158 git grep -n -e test $H >actual &&
159 test_cmp expected actual
162 test_expect_success "grep $L (t-2)" '
163 echo "${HC}t:1:test" >expected &&
165 cd t &&
166 git grep -n -e test $H
167 ) >actual &&
168 test_cmp expected actual
171 test_expect_success "grep $L (t-3)" '
172 echo "${HC}t/t:1:test" >expected &&
174 cd t &&
175 git grep --full-name -n -e test $H
176 ) >actual &&
177 test_cmp expected actual
180 test_expect_success "grep -c $L (no /dev/null)" '
181 ! git grep -c test $H | grep /dev/null
184 test_expect_success "grep --max-depth -1 $L" '
186 echo ${HC}t/a/v:1:vvv
187 echo ${HC}t/v:1:vvv
188 echo ${HC}v:1:vvv
189 } >expected &&
190 git grep --max-depth -1 -n -e vvv $H >actual &&
191 test_cmp expected actual
194 test_expect_success "grep --max-depth 0 $L" '
196 echo ${HC}v:1:vvv
197 } >expected &&
198 git grep --max-depth 0 -n -e vvv $H >actual &&
199 test_cmp expected actual
202 test_expect_success "grep --max-depth 0 -- '*' $L" '
204 echo ${HC}t/a/v:1:vvv
205 echo ${HC}t/v:1:vvv
206 echo ${HC}v:1:vvv
207 } >expected &&
208 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
209 test_cmp expected actual
212 test_expect_success "grep --max-depth 1 $L" '
214 echo ${HC}t/v:1:vvv
215 echo ${HC}v:1:vvv
216 } >expected &&
217 git grep --max-depth 1 -n -e vvv $H >actual &&
218 test_cmp expected actual
221 test_expect_success "grep --max-depth 0 -- t $L" '
223 echo ${HC}t/v:1:vvv
224 } >expected &&
225 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
226 test_cmp expected actual
229 test_expect_success "grep --max-depth 0 -- . t $L" '
231 echo ${HC}t/v:1:vvv
232 echo ${HC}v:1:vvv
233 } >expected &&
234 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
235 test_cmp expected actual
238 test_expect_success "grep --max-depth 0 -- t . $L" '
240 echo ${HC}t/v:1:vvv
241 echo ${HC}v:1:vvv
242 } >expected &&
243 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
244 test_cmp expected actual
246 test_expect_success "grep $L with grep.extendedRegexp=false" '
247 echo "${HC}ab:a+bc" >expected &&
248 git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
249 test_cmp expected actual
252 test_expect_success "grep $L with grep.extendedRegexp=true" '
253 echo "${HC}ab:abc" >expected &&
254 git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
255 test_cmp expected actual
258 test_expect_success "grep $L with grep.patterntype=basic" '
259 echo "${HC}ab:a+bc" >expected &&
260 git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
261 test_cmp expected actual
264 test_expect_success "grep $L with grep.patterntype=extended" '
265 echo "${HC}ab:abc" >expected &&
266 git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
267 test_cmp expected actual
270 test_expect_success "grep $L with grep.patterntype=fixed" '
271 echo "${HC}ab:a+b*c" >expected &&
272 git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
273 test_cmp expected actual
276 test_expect_success LIBPCRE "grep $L with grep.patterntype=perl" '
277 echo "${HC}ab:a+b*c" >expected &&
278 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
279 test_cmp expected actual
282 test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
283 echo "${HC}ab:abc" >expected &&
284 git \
285 -c grep.patternType=default \
286 -c grep.extendedRegexp=true \
287 grep "a+b*c" $H ab >actual &&
288 test_cmp expected actual
291 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
292 echo "${HC}ab:abc" >expected &&
293 git \
294 -c grep.extendedRegexp=true \
295 -c grep.patternType=default \
296 grep "a+b*c" $H ab >actual &&
297 test_cmp expected actual
300 test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
301 echo "${HC}ab:abc" >expected &&
302 git \
303 -c grep.patternType=extended \
304 -c grep.extendedRegexp=false \
305 grep "a+b*c" $H ab >actual &&
306 test_cmp expected actual
309 test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
310 echo "${HC}ab:a+bc" >expected &&
311 git \
312 -c grep.patternType=basic \
313 -c grep.extendedRegexp=true \
314 grep "a+b*c" $H ab >actual &&
315 test_cmp expected actual
318 test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
319 echo "${HC}ab:abc" >expected &&
320 git \
321 -c grep.extendedRegexp=false \
322 -c grep.patternType=extended \
323 grep "a+b*c" $H ab >actual &&
324 test_cmp expected actual
327 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
328 echo "${HC}ab:a+bc" >expected &&
329 git \
330 -c grep.extendedRegexp=true \
331 -c grep.patternType=basic \
332 grep "a+b*c" $H ab >actual &&
333 test_cmp expected actual
336 test_expect_success "grep --count $L" '
337 echo ${HC}ab:3 >expected &&
338 git grep --count -e b $H -- ab >actual &&
339 test_cmp expected actual
342 test_expect_success "grep --count -h $L" '
343 echo 3 >expected &&
344 git grep --count -h -e b $H -- ab >actual &&
345 test_cmp expected actual
347 done
349 cat >expected <<EOF
350 file
352 test_expect_success 'grep -l -C' '
353 git grep -l -C1 foo >actual &&
354 test_cmp expected actual
357 cat >expected <<EOF
358 file:5
360 test_expect_success 'grep -l -C' '
361 git grep -c -C1 foo >actual &&
362 test_cmp expected actual
365 test_expect_success 'grep -L -C' '
366 git ls-files >expected &&
367 git grep -L -C1 nonexistent_string >actual &&
368 test_cmp expected actual
371 cat >expected <<EOF
372 file:foo mmap bar_mmap
375 test_expect_success 'grep -e A --and -e B' '
376 git grep -e "foo mmap" --and -e bar_mmap >actual &&
377 test_cmp expected actual
380 cat >expected <<EOF
381 file:foo_mmap bar mmap
382 file:foo_mmap bar mmap baz
386 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
387 git grep \( -e foo_ --or -e baz \) \
388 --and -e " mmap" >actual &&
389 test_cmp expected actual
392 cat >expected <<EOF
393 file:foo mmap bar
396 test_expect_success 'grep -e A --and --not -e B' '
397 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
398 test_cmp expected actual
401 test_expect_success 'grep should ignore GREP_OPTIONS' '
402 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
403 test_cmp expected actual
406 test_expect_success 'grep -f, non-existent file' '
407 test_must_fail git grep -f patterns
410 cat >expected <<EOF
411 file:foo mmap bar
412 file:foo_mmap bar
413 file:foo_mmap bar mmap
414 file:foo mmap bar_mmap
415 file:foo_mmap bar mmap baz
418 cat >pattern <<EOF
419 mmap
422 test_expect_success 'grep -f, one pattern' '
423 git grep -f pattern >actual &&
424 test_cmp expected actual
427 cat >expected <<EOF
428 file:foo mmap bar
429 file:foo_mmap bar
430 file:foo_mmap bar mmap
431 file:foo mmap bar_mmap
432 file:foo_mmap bar mmap baz
433 t/a/v:vvv
434 t/v:vvv
435 v:vvv
438 cat >patterns <<EOF
439 mmap
443 test_expect_success 'grep -f, multiple patterns' '
444 git grep -f patterns >actual &&
445 test_cmp expected actual
448 test_expect_success 'grep, multiple patterns' '
449 git grep "$(cat patterns)" >actual &&
450 test_cmp expected actual
453 cat >expected <<EOF
454 file:foo mmap bar
455 file:foo_mmap bar
456 file:foo_mmap bar mmap
457 file:foo mmap bar_mmap
458 file:foo_mmap bar mmap baz
459 t/a/v:vvv
460 t/v:vvv
461 v:vvv
464 cat >patterns <<EOF
466 mmap
472 test_expect_success 'grep -f, ignore empty lines' '
473 git grep -f patterns >actual &&
474 test_cmp expected actual
477 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
478 git grep -f - <patterns >actual &&
479 test_cmp expected actual
482 cat >expected <<EOF
483 y:y yy
485 z:zzz
488 test_expect_success 'grep -q, silently report matches' '
489 >empty &&
490 git grep -q mmap >actual &&
491 test_cmp empty actual &&
492 test_must_fail git grep -q qfwfq >actual &&
493 test_cmp empty actual
496 test_expect_success 'grep -C1 hunk mark between files' '
497 git grep -C1 "^[yz]" >actual &&
498 test_cmp expected actual
501 test_expect_success 'log grep setup' '
502 echo a >>file &&
503 test_tick &&
504 GIT_AUTHOR_NAME="With * Asterisk" \
505 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
506 git commit -a -m "second" &&
508 echo a >>file &&
509 test_tick &&
510 git commit -a -m "third" &&
512 echo a >>file &&
513 test_tick &&
514 GIT_AUTHOR_NAME="Night Fall" \
515 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
516 git commit -a -m "fourth"
519 test_expect_success 'log grep (1)' '
520 git log --author=author --pretty=tformat:%s >actual &&
522 echo third && echo initial
523 } >expect &&
524 test_cmp expect actual
527 test_expect_success 'log grep (2)' '
528 git log --author=" * " -F --pretty=tformat:%s >actual &&
530 echo second
531 } >expect &&
532 test_cmp expect actual
535 test_expect_success 'log grep (3)' '
536 git log --author="^A U" --pretty=tformat:%s >actual &&
538 echo third && echo initial
539 } >expect &&
540 test_cmp expect actual
543 test_expect_success 'log grep (4)' '
544 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
546 echo second
547 } >expect &&
548 test_cmp expect actual
551 test_expect_success 'log grep (5)' '
552 git log --author=Thor -F --pretty=tformat:%s >actual &&
554 echo third && echo initial
555 } >expect &&
556 test_cmp expect actual
559 test_expect_success 'log grep (6)' '
560 git log --author=-0700 --pretty=tformat:%s >actual &&
561 >expect &&
562 test_cmp expect actual
565 test_expect_success 'log grep (7)' '
566 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
567 echo third >expect &&
568 test_cmp expect actual
571 test_expect_success 'log grep (8)' '
572 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
574 echo third && echo second
575 } >expect &&
576 test_cmp expect actual
579 test_expect_success 'log grep (9)' '
580 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
581 echo third >expect &&
582 test_cmp expect actual
585 test_expect_success 'log grep (9)' '
586 git log -g --grep-reflog="commit: third" --author="non-existant" --pretty=tformat:%s >actual &&
587 : >expect &&
588 test_cmp expect actual
591 test_expect_success 'log --grep-reflog can only be used under -g' '
592 test_must_fail git log --grep-reflog="commit: third"
595 test_expect_success 'log with multiple --grep uses union' '
596 git log --grep=i --grep=r --format=%s >actual &&
598 echo fourth && echo third && echo initial
599 } >expect &&
600 test_cmp expect actual
603 test_expect_success 'log --all-match with multiple --grep uses intersection' '
604 git log --all-match --grep=i --grep=r --format=%s >actual &&
606 echo third
607 } >expect &&
608 test_cmp expect actual
611 test_expect_success 'log with multiple --author uses union' '
612 git log --author="Thor" --author="Aster" --format=%s >actual &&
614 echo third && echo second && echo initial
615 } >expect &&
616 test_cmp expect actual
619 test_expect_success 'log --all-match with multiple --author still uses union' '
620 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
622 echo third && echo second && echo initial
623 } >expect &&
624 test_cmp expect actual
627 test_expect_success 'log --grep --author uses intersection' '
628 # grep matches only third and fourth
629 # author matches only initial and third
630 git log --author="A U Thor" --grep=r --format=%s >actual &&
632 echo third
633 } >expect &&
634 test_cmp expect actual
637 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
638 # grep matches initial and second but not third
639 # author matches only initial and third
640 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
642 echo initial
643 } >expect &&
644 test_cmp expect actual
647 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
648 # grep matches only initial and third
649 # author matches all but second
650 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
652 echo third && echo initial
653 } >expect &&
654 test_cmp expect actual
657 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
658 # grep matches only initial and third
659 # author matches all but second
660 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
662 echo third && echo initial
663 } >expect &&
664 test_cmp expect actual
667 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
668 # grep matches only third
669 # author matches only initial and third
670 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
672 echo third
673 } >expect &&
674 test_cmp expect actual
677 test_expect_success 'log --author does not search in timestamp' '
678 : >expect &&
679 git log --author="$GIT_AUTHOR_DATE" >actual &&
680 test_cmp expect actual
683 test_expect_success 'log --committer does not search in timestamp' '
684 : >expect &&
685 git log --committer="$GIT_COMMITTER_DATE" >actual &&
686 test_cmp expect actual
689 test_expect_success 'grep with CE_VALID file' '
690 git update-index --assume-unchanged t/t &&
691 rm t/t &&
692 test "$(git grep test)" = "t/t:test" &&
693 git update-index --no-assume-unchanged t/t &&
694 git checkout t/t
697 cat >expected <<EOF
698 hello.c=#include <stdio.h>
699 hello.c: return 0;
702 test_expect_success 'grep -p with userdiff' '
703 git config diff.custom.funcname "^#" &&
704 echo "hello.c diff=custom" >.gitattributes &&
705 git grep -p return >actual &&
706 test_cmp expected actual
709 cat >expected <<EOF
710 hello.c=int main(int argc, const char **argv)
711 hello.c: return 0;
714 test_expect_success 'grep -p' '
715 rm -f .gitattributes &&
716 git grep -p return >actual &&
717 test_cmp expected actual
720 cat >expected <<EOF
721 hello.c-#include <stdio.h>
722 hello.c=int main(int argc, const char **argv)
723 hello.c-{
724 hello.c- printf("Hello world.\n");
725 hello.c: return 0;
728 test_expect_success 'grep -p -B5' '
729 git grep -p -B5 return >actual &&
730 test_cmp expected actual
733 cat >expected <<EOF
734 hello.c=int main(int argc, const char **argv)
735 hello.c-{
736 hello.c- printf("Hello world.\n");
737 hello.c: return 0;
738 hello.c- /* char ?? */
739 hello.c-}
742 test_expect_success 'grep -W' '
743 git grep -W return >actual &&
744 test_cmp expected actual
747 cat >expected <<EOF
748 hello.c= printf("Hello world.\n");
749 hello.c: return 0;
750 hello.c- /* char ?? */
753 test_expect_success 'grep -W with userdiff' '
754 test_when_finished "rm -f .gitattributes" &&
755 git config diff.custom.xfuncname "(printf.*|})$" &&
756 echo "hello.c diff=custom" >.gitattributes &&
757 git grep -W return >actual &&
758 test_cmp expected actual
761 test_expect_success 'grep from a subdirectory to search wider area (1)' '
762 mkdir -p s &&
764 cd s && git grep "x x x" ..
768 test_expect_success 'grep from a subdirectory to search wider area (2)' '
769 mkdir -p s &&
771 cd s || exit 1
772 ( git grep xxyyzz .. >out ; echo $? >status )
773 ! test -s out &&
774 test 1 = $(cat status)
778 cat >expected <<EOF
779 hello.c:int main(int argc, const char **argv)
782 test_expect_success 'grep -Fi' '
783 git grep -Fi "CHAR *" >actual &&
784 test_cmp expected actual
787 test_expect_success 'outside of git repository' '
788 rm -fr non &&
789 mkdir -p non/git/sub &&
790 echo hello >non/git/file1 &&
791 echo world >non/git/sub/file2 &&
793 echo file1:hello &&
794 echo sub/file2:world
795 } >non/expect.full &&
796 echo file2:world >non/expect.sub &&
798 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
799 export GIT_CEILING_DIRECTORIES &&
800 cd non/git &&
801 test_must_fail git grep o &&
802 git grep --no-index o >../actual.full &&
803 test_cmp ../expect.full ../actual.full &&
804 cd sub &&
805 test_must_fail git grep o &&
806 git grep --no-index o >../../actual.sub &&
807 test_cmp ../../expect.sub ../../actual.sub
808 ) &&
810 echo ".*o*" >non/git/.gitignore &&
812 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
813 export GIT_CEILING_DIRECTORIES &&
814 cd non/git &&
815 test_must_fail git grep o &&
816 git grep --no-index --exclude-standard o >../actual.full &&
817 test_cmp ../expect.full ../actual.full &&
820 echo ".gitignore:.*o*" &&
821 cat ../expect.full
822 } >../expect.with.ignored &&
823 git grep --no-index --no-exclude o >../actual.full &&
824 test_cmp ../expect.with.ignored ../actual.full
828 test_expect_success 'outside of git repository with fallbackToNoIndex' '
829 rm -fr non &&
830 mkdir -p non/git/sub &&
831 echo hello >non/git/file1 &&
832 echo world >non/git/sub/file2 &&
833 cat <<-\EOF >non/expect.full &&
834 file1:hello
835 sub/file2:world
837 echo file2:world >non/expect.sub &&
839 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
840 export GIT_CEILING_DIRECTORIES &&
841 cd non/git &&
842 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
843 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
844 test_cmp ../expect.full ../actual.full &&
845 cd sub &&
846 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
847 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
848 test_cmp ../../expect.sub ../../actual.sub
849 ) &&
851 echo ".*o*" >non/git/.gitignore &&
853 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
854 export GIT_CEILING_DIRECTORIES &&
855 cd non/git &&
856 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
857 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
858 test_cmp ../expect.full ../actual.full &&
861 echo ".gitignore:.*o*" &&
862 cat ../expect.full
863 } >../expect.with.ignored &&
864 git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
865 test_cmp ../expect.with.ignored ../actual.full
869 test_expect_success 'inside git repository but with --no-index' '
870 rm -fr is &&
871 mkdir -p is/git/sub &&
872 echo hello >is/git/file1 &&
873 echo world >is/git/sub/file2 &&
874 echo ".*o*" >is/git/.gitignore &&
876 echo file1:hello &&
877 echo sub/file2:world
878 } >is/expect.unignored &&
880 echo ".gitignore:.*o*" &&
881 cat is/expect.unignored
882 } >is/expect.full &&
883 : >is/expect.empty &&
884 echo file2:world >is/expect.sub &&
886 cd is/git &&
887 git init &&
888 test_must_fail git grep o >../actual.full &&
889 test_cmp ../expect.empty ../actual.full &&
891 git grep --untracked o >../actual.unignored &&
892 test_cmp ../expect.unignored ../actual.unignored &&
894 git grep --no-index o >../actual.full &&
895 test_cmp ../expect.full ../actual.full &&
897 git grep --no-index --exclude-standard o >../actual.unignored &&
898 test_cmp ../expect.unignored ../actual.unignored &&
900 cd sub &&
901 test_must_fail git grep o >../../actual.sub &&
902 test_cmp ../../expect.empty ../../actual.sub &&
904 git grep --no-index o >../../actual.sub &&
905 test_cmp ../../expect.sub ../../actual.sub &&
907 git grep --untracked o >../../actual.sub &&
908 test_cmp ../../expect.sub ../../actual.sub
912 test_expect_success 'grep --no-index descends into repos, but not .git' '
913 rm -fr non &&
914 mkdir -p non/git &&
916 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
917 export GIT_CEILING_DIRECTORIES &&
918 cd non/git &&
920 echo magic >file &&
921 git init repo &&
923 cd repo &&
924 echo magic >file &&
925 git add file &&
926 git commit -m foo &&
927 echo magic >.git/file
928 ) &&
930 cat >expect <<-\EOF &&
931 file
932 repo/file
934 git grep -l --no-index magic >actual &&
935 test_cmp expect actual
939 test_expect_success 'setup double-dash tests' '
940 cat >double-dash <<EOF &&
943 other
945 git add double-dash
948 cat >expected <<EOF
949 double-dash:->
951 test_expect_success 'grep -- pattern' '
952 git grep -- "->" >actual &&
953 test_cmp expected actual
955 test_expect_success 'grep -- pattern -- pathspec' '
956 git grep -- "->" -- double-dash >actual &&
957 test_cmp expected actual
959 test_expect_success 'grep -e pattern -- path' '
960 git grep -e "->" -- double-dash >actual &&
961 test_cmp expected actual
964 cat >expected <<EOF
965 double-dash:--
967 test_expect_success 'grep -e -- -- path' '
968 git grep -e -- -- double-dash >actual &&
969 test_cmp expected actual
972 cat >expected <<EOF
973 hello.c:int main(int argc, const char **argv)
974 hello.c: printf("Hello world.\n");
977 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
978 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
979 test_cmp expected actual
982 test_expect_success LIBPCRE 'grep -P pattern' '
983 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
984 test_cmp expected actual
987 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
988 >empty &&
989 test_must_fail git -c grep.extendedregexp=true \
990 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
991 test_cmp empty actual
994 test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
995 git -c grep.extendedregexp=true \
996 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
997 test_cmp expected actual
1000 test_expect_success LIBPCRE 'grep -P -v pattern' '
1002 echo "ab:a+b*c"
1003 echo "ab:a+bc"
1004 } >expected &&
1005 git grep -P -v "abc" ab >actual &&
1006 test_cmp expected actual
1009 test_expect_success LIBPCRE 'grep -P -i pattern' '
1010 cat >expected <<-EOF &&
1011 hello.c: printf("Hello world.\n");
1013 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1014 test_cmp expected actual
1017 test_expect_success LIBPCRE 'grep -P -w pattern' '
1019 echo "hello_world:Hello world"
1020 echo "hello_world:HeLLo world"
1021 } >expected &&
1022 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1023 test_cmp expected actual
1026 test_expect_success 'grep -G invalidpattern properly dies ' '
1027 test_must_fail git grep -G "a["
1030 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1031 test_must_fail git -c grep.patterntype=basic grep "a["
1034 test_expect_success 'grep -E invalidpattern properly dies ' '
1035 test_must_fail git grep -E "a["
1038 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1039 test_must_fail git -c grep.patterntype=extended grep "a["
1042 test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
1043 test_must_fail git grep -P "a["
1046 test_expect_success LIBPCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1047 test_must_fail git -c grep.patterntype=perl grep "a["
1050 test_expect_success 'grep -G -E -F pattern' '
1051 echo "ab:a+b*c" >expected &&
1052 git grep -G -E -F "a+b*c" ab >actual &&
1053 test_cmp expected actual
1056 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1057 echo "ab:a+b*c" >expected &&
1058 git \
1059 -c grep.patterntype=basic \
1060 -c grep.patterntype=extended \
1061 -c grep.patterntype=fixed \
1062 grep "a+b*c" ab >actual &&
1063 test_cmp expected actual
1066 test_expect_success 'grep -E -F -G pattern' '
1067 echo "ab:a+bc" >expected &&
1068 git grep -E -F -G "a+b*c" ab >actual &&
1069 test_cmp expected actual
1072 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1073 echo "ab:a+bc" >expected &&
1074 git \
1075 -c grep.patterntype=extended \
1076 -c grep.patterntype=fixed \
1077 -c grep.patterntype=basic \
1078 grep "a+b*c" ab >actual &&
1079 test_cmp expected actual
1082 test_expect_success 'grep -F -G -E pattern' '
1083 echo "ab:abc" >expected &&
1084 git grep -F -G -E "a+b*c" ab >actual &&
1085 test_cmp expected actual
1088 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1089 echo "ab:abc" >expected &&
1090 git \
1091 -c grep.patterntype=fixed \
1092 -c grep.patterntype=basic \
1093 -c grep.patterntype=extended \
1094 grep "a+b*c" ab >actual &&
1095 test_cmp expected actual
1098 test_expect_success 'grep -G -F -P -E pattern' '
1099 echo "d0:d" >expected &&
1100 git grep -G -F -P -E "[\d]" d0 >actual &&
1101 test_cmp expected actual
1104 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1105 echo "d0:d" >expected &&
1106 git \
1107 -c grep.patterntype=fixed \
1108 -c grep.patterntype=basic \
1109 -c grep.patterntype=perl \
1110 -c grep.patterntype=extended \
1111 grep "[\d]" d0 >actual &&
1112 test_cmp expected actual
1115 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1116 echo "d0:0" >expected &&
1117 git grep -G -F -E -P "[\d]" d0 >actual &&
1118 test_cmp expected actual
1121 test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1122 echo "d0:0" >expected &&
1123 git \
1124 -c grep.patterntype=fixed \
1125 -c grep.patterntype=basic \
1126 -c grep.patterntype=extended \
1127 -c grep.patterntype=perl \
1128 grep "[\d]" d0 >actual &&
1129 test_cmp expected actual
1132 test_expect_success LIBPCRE 'grep -P pattern with grep.patternType=fixed' '
1133 echo "ab:a+b*c" >expected &&
1134 git \
1135 -c grep.patterntype=fixed \
1136 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1137 test_cmp expected actual
1140 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1141 echo "ab:a+b*c" >expected &&
1142 git \
1143 -c grep.patterntype=basic \
1144 grep -F "*c" ab >actual &&
1145 test_cmp expected actual
1148 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1150 echo "ab:a+b*c"
1151 echo "ab:a+bc"
1152 } >expected &&
1153 git \
1154 -c grep.patterntype=fixed \
1155 grep -G "a+b" ab >actual &&
1156 test_cmp expected actual
1159 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1161 echo "ab:a+b*c"
1162 echo "ab:a+bc"
1163 echo "ab:abc"
1164 } >expected &&
1165 git \
1166 -c grep.patterntype=fixed \
1167 grep -E "a+" ab >actual &&
1168 test_cmp expected actual
1171 cat >expected <<EOF
1172 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1173 hello.c<RED>-<RESET>{
1174 <RED>--<RESET>
1175 hello.c<RED>:<RESET> /* char ?? */
1176 hello.c<RED>-<RESET>}
1177 <RED>--<RESET>
1178 hello_world<RED>:<RESET>Hello_world
1179 hello_world<RED>-<RESET>HeLLo_world
1182 test_expect_success 'grep --color, separator' '
1183 test_config color.grep.context normal &&
1184 test_config color.grep.filename normal &&
1185 test_config color.grep.function normal &&
1186 test_config color.grep.linenumber normal &&
1187 test_config color.grep.match normal &&
1188 test_config color.grep.selected normal &&
1189 test_config color.grep.separator red &&
1191 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1192 test_decode_color >actual &&
1193 test_cmp expected actual
1196 cat >expected <<EOF
1197 hello.c:int main(int argc, const char **argv)
1198 hello.c: /* char ?? */
1200 hello_world:Hello_world
1203 test_expect_success 'grep --break' '
1204 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1205 test_cmp expected actual
1208 cat >expected <<EOF
1209 hello.c:int main(int argc, const char **argv)
1210 hello.c-{
1212 hello.c: /* char ?? */
1213 hello.c-}
1215 hello_world:Hello_world
1216 hello_world-HeLLo_world
1219 test_expect_success 'grep --break with context' '
1220 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1221 test_cmp expected actual
1224 cat >expected <<EOF
1225 hello.c
1226 int main(int argc, const char **argv)
1227 /* char ?? */
1228 hello_world
1229 Hello_world
1232 test_expect_success 'grep --heading' '
1233 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1234 test_cmp expected actual
1237 cat >expected <<EOF
1238 <BOLD;GREEN>hello.c<RESET>
1239 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1240 6: /* <BLACK;BYELLOW>char<RESET> ?? */
1242 <BOLD;GREEN>hello_world<RESET>
1243 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1246 test_expect_success 'mimic ack-grep --group' '
1247 test_config color.grep.context normal &&
1248 test_config color.grep.filename "bold green" &&
1249 test_config color.grep.function normal &&
1250 test_config color.grep.linenumber normal &&
1251 test_config color.grep.match "black yellow" &&
1252 test_config color.grep.selected normal &&
1253 test_config color.grep.separator normal &&
1255 git grep --break --heading -n --color \
1256 -e char -e lo_w hello.c hello_world |
1257 test_decode_color >actual &&
1258 test_cmp expected actual
1261 cat >expected <<EOF
1262 space: line with leading space1
1263 space: line with leading space2
1264 space: line with leading space3
1267 test_expect_success LIBPCRE 'grep -E "^ "' '
1268 git grep -E "^ " space >actual &&
1269 test_cmp expected actual
1272 test_expect_success LIBPCRE 'grep -P "^ "' '
1273 git grep -P "^ " space >actual &&
1274 test_cmp expected actual
1277 cat >expected <<EOF
1278 space-line without leading space1
1279 space: line <RED>with <RESET>leading space1
1280 space: line <RED>with <RESET>leading <RED>space2<RESET>
1281 space: line <RED>with <RESET>leading space3
1282 space:line without leading <RED>space2<RESET>
1285 test_expect_success 'grep --color -e A -e B with context' '
1286 test_config color.grep.context normal &&
1287 test_config color.grep.filename normal &&
1288 test_config color.grep.function normal &&
1289 test_config color.grep.linenumber normal &&
1290 test_config color.grep.matchContext normal &&
1291 test_config color.grep.matchSelected red &&
1292 test_config color.grep.selected normal &&
1293 test_config color.grep.separator normal &&
1295 git grep --color=always -C2 -e "with " -e space2 space |
1296 test_decode_color >actual &&
1297 test_cmp expected actual
1300 cat >expected <<EOF
1301 space-line without leading space1
1302 space- line with leading space1
1303 space: line <RED>with <RESET>leading <RED>space2<RESET>
1304 space- line with leading space3
1305 space-line without leading space2
1308 test_expect_success 'grep --color -e A --and -e B with context' '
1309 test_config color.grep.context normal &&
1310 test_config color.grep.filename normal &&
1311 test_config color.grep.function normal &&
1312 test_config color.grep.linenumber normal &&
1313 test_config color.grep.matchContext normal &&
1314 test_config color.grep.matchSelected red &&
1315 test_config color.grep.selected normal &&
1316 test_config color.grep.separator normal &&
1318 git grep --color=always -C2 -e "with " --and -e space2 space |
1319 test_decode_color >actual &&
1320 test_cmp expected actual
1323 cat >expected <<EOF
1324 space-line without leading space1
1325 space: line <RED>with <RESET>leading space1
1326 space- line with leading space2
1327 space: line <RED>with <RESET>leading space3
1328 space-line without leading space2
1331 test_expect_success 'grep --color -e A --and --not -e B with context' '
1332 test_config color.grep.context normal &&
1333 test_config color.grep.filename normal &&
1334 test_config color.grep.function normal &&
1335 test_config color.grep.linenumber normal &&
1336 test_config color.grep.matchContext normal &&
1337 test_config color.grep.matchSelected red &&
1338 test_config color.grep.selected normal &&
1339 test_config color.grep.separator normal &&
1341 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1342 test_decode_color >actual &&
1343 test_cmp expected actual
1346 cat >expected <<EOF
1347 hello.c-#include <stdio.h>
1348 hello.c=int main(int argc, const char **argv)
1349 hello.c-{
1350 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1351 hello.c- return 0;
1352 hello.c- /* char ?? */
1353 hello.c-}
1356 test_expect_success 'grep --color -e A --and -e B -p with context' '
1357 test_config color.grep.context normal &&
1358 test_config color.grep.filename normal &&
1359 test_config color.grep.function normal &&
1360 test_config color.grep.linenumber normal &&
1361 test_config color.grep.matchContext normal &&
1362 test_config color.grep.matchSelected red &&
1363 test_config color.grep.selected normal &&
1364 test_config color.grep.separator normal &&
1366 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1367 test_decode_color >actual &&
1368 test_cmp expected actual
1371 test_done