git-grep: correct exit code with --quiet and -L
[git.git] / t / t7810-grep.sh
blob7395038f7e42b9b2683ca5bf91e471bcc9e55de0
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 LIBPCRE "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 "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
285 echo "${HC}ab:abc" >expected &&
286 git \
287 -c grep.patternType=default \
288 -c grep.extendedRegexp=true \
289 grep "a+b*c" $H ab >actual &&
290 test_cmp expected actual
293 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
294 echo "${HC}ab:abc" >expected &&
295 git \
296 -c grep.extendedRegexp=true \
297 -c grep.patternType=default \
298 grep "a+b*c" $H ab >actual &&
299 test_cmp expected actual
302 test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
303 echo "${HC}ab:abc" >expected &&
304 git \
305 -c grep.patternType=extended \
306 -c grep.extendedRegexp=false \
307 grep "a+b*c" $H ab >actual &&
308 test_cmp expected actual
311 test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
312 echo "${HC}ab:a+bc" >expected &&
313 git \
314 -c grep.patternType=basic \
315 -c grep.extendedRegexp=true \
316 grep "a+b*c" $H ab >actual &&
317 test_cmp expected actual
320 test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
321 echo "${HC}ab:abc" >expected &&
322 git \
323 -c grep.extendedRegexp=false \
324 -c grep.patternType=extended \
325 grep "a+b*c" $H ab >actual &&
326 test_cmp expected actual
329 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
330 echo "${HC}ab:a+bc" >expected &&
331 git \
332 -c grep.extendedRegexp=true \
333 -c grep.patternType=basic \
334 grep "a+b*c" $H ab >actual &&
335 test_cmp expected actual
338 test_expect_success "grep --count $L" '
339 echo ${HC}ab:3 >expected &&
340 git grep --count -e b $H -- ab >actual &&
341 test_cmp expected actual
344 test_expect_success "grep --count -h $L" '
345 echo 3 >expected &&
346 git grep --count -h -e b $H -- ab >actual &&
347 test_cmp expected actual
349 done
351 cat >expected <<EOF
352 file
354 test_expect_success 'grep -l -C' '
355 git grep -l -C1 foo >actual &&
356 test_cmp expected actual
359 cat >expected <<EOF
360 file:5
362 test_expect_success 'grep -c -C' '
363 git grep -c -C1 foo >actual &&
364 test_cmp expected actual
367 test_expect_success 'grep -L -C' '
368 git ls-files >expected &&
369 git grep -L -C1 nonexistent_string >actual &&
370 test_cmp expected actual
373 test_expect_success 'grep --files-without-match --quiet' '
374 git grep --files-without-match --quiet nonexistent_string >actual &&
375 test_cmp /dev/null actual
378 cat >expected <<EOF
379 file:foo mmap bar_mmap
382 test_expect_success 'grep -e A --and -e B' '
383 git grep -e "foo mmap" --and -e bar_mmap >actual &&
384 test_cmp expected actual
387 cat >expected <<EOF
388 file:foo_mmap bar mmap
389 file:foo_mmap bar mmap baz
393 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
394 git grep \( -e foo_ --or -e baz \) \
395 --and -e " mmap" >actual &&
396 test_cmp expected actual
399 cat >expected <<EOF
400 file:foo mmap bar
403 test_expect_success 'grep -e A --and --not -e B' '
404 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
405 test_cmp expected actual
408 test_expect_success 'grep should ignore GREP_OPTIONS' '
409 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
410 test_cmp expected actual
413 test_expect_success 'grep -f, non-existent file' '
414 test_must_fail git grep -f patterns
417 cat >expected <<EOF
418 file:foo mmap bar
419 file:foo_mmap bar
420 file:foo_mmap bar mmap
421 file:foo mmap bar_mmap
422 file:foo_mmap bar mmap baz
425 cat >pattern <<EOF
426 mmap
429 test_expect_success 'grep -f, one pattern' '
430 git grep -f pattern >actual &&
431 test_cmp expected actual
434 cat >expected <<EOF
435 file:foo mmap bar
436 file:foo_mmap bar
437 file:foo_mmap bar mmap
438 file:foo mmap bar_mmap
439 file:foo_mmap bar mmap baz
440 t/a/v:vvv
441 t/v:vvv
442 v:vvv
445 cat >patterns <<EOF
446 mmap
450 test_expect_success 'grep -f, multiple patterns' '
451 git grep -f patterns >actual &&
452 test_cmp expected actual
455 test_expect_success 'grep, multiple patterns' '
456 git grep "$(cat patterns)" >actual &&
457 test_cmp expected actual
460 cat >expected <<EOF
461 file:foo mmap bar
462 file:foo_mmap bar
463 file:foo_mmap bar mmap
464 file:foo mmap bar_mmap
465 file:foo_mmap bar mmap baz
466 t/a/v:vvv
467 t/v:vvv
468 v:vvv
471 cat >patterns <<EOF
473 mmap
479 test_expect_success 'grep -f, ignore empty lines' '
480 git grep -f patterns >actual &&
481 test_cmp expected actual
484 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
485 git grep -f - <patterns >actual &&
486 test_cmp expected actual
489 cat >expected <<EOF
490 y:y yy
492 z:zzz
495 test_expect_success 'grep -q, silently report matches' '
496 >empty &&
497 git grep -q mmap >actual &&
498 test_cmp empty actual &&
499 test_must_fail git grep -q qfwfq >actual &&
500 test_cmp empty actual
503 test_expect_success 'grep -C1 hunk mark between files' '
504 git grep -C1 "^[yz]" >actual &&
505 test_cmp expected actual
508 test_expect_success 'log grep setup' '
509 echo a >>file &&
510 test_tick &&
511 GIT_AUTHOR_NAME="With * Asterisk" \
512 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
513 git commit -a -m "second" &&
515 echo a >>file &&
516 test_tick &&
517 git commit -a -m "third" &&
519 echo a >>file &&
520 test_tick &&
521 GIT_AUTHOR_NAME="Night Fall" \
522 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
523 git commit -a -m "fourth"
526 test_expect_success 'log grep (1)' '
527 git log --author=author --pretty=tformat:%s >actual &&
529 echo third && echo initial
530 } >expect &&
531 test_cmp expect actual
534 test_expect_success 'log grep (2)' '
535 git log --author=" * " -F --pretty=tformat:%s >actual &&
537 echo second
538 } >expect &&
539 test_cmp expect actual
542 test_expect_success 'log grep (3)' '
543 git log --author="^A U" --pretty=tformat:%s >actual &&
545 echo third && echo initial
546 } >expect &&
547 test_cmp expect actual
550 test_expect_success 'log grep (4)' '
551 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
553 echo second
554 } >expect &&
555 test_cmp expect actual
558 test_expect_success 'log grep (5)' '
559 git log --author=Thor -F --pretty=tformat:%s >actual &&
561 echo third && echo initial
562 } >expect &&
563 test_cmp expect actual
566 test_expect_success 'log grep (6)' '
567 git log --author=-0700 --pretty=tformat:%s >actual &&
568 >expect &&
569 test_cmp expect actual
572 test_expect_success 'log grep (7)' '
573 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
574 echo third >expect &&
575 test_cmp expect actual
578 test_expect_success 'log grep (8)' '
579 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
581 echo third && echo second
582 } >expect &&
583 test_cmp expect actual
586 test_expect_success 'log grep (9)' '
587 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
588 echo third >expect &&
589 test_cmp expect actual
592 test_expect_success 'log grep (9)' '
593 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
594 : >expect &&
595 test_cmp expect actual
598 test_expect_success 'log --grep-reflog can only be used under -g' '
599 test_must_fail git log --grep-reflog="commit: third"
602 test_expect_success 'log with multiple --grep uses union' '
603 git log --grep=i --grep=r --format=%s >actual &&
605 echo fourth && echo third && echo initial
606 } >expect &&
607 test_cmp expect actual
610 test_expect_success 'log --all-match with multiple --grep uses intersection' '
611 git log --all-match --grep=i --grep=r --format=%s >actual &&
613 echo third
614 } >expect &&
615 test_cmp expect actual
618 test_expect_success 'log with multiple --author uses union' '
619 git log --author="Thor" --author="Aster" --format=%s >actual &&
621 echo third && echo second && echo initial
622 } >expect &&
623 test_cmp expect actual
626 test_expect_success 'log --all-match with multiple --author still uses union' '
627 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
629 echo third && echo second && echo initial
630 } >expect &&
631 test_cmp expect actual
634 test_expect_success 'log --grep --author uses intersection' '
635 # grep matches only third and fourth
636 # author matches only initial and third
637 git log --author="A U Thor" --grep=r --format=%s >actual &&
639 echo third
640 } >expect &&
641 test_cmp expect actual
644 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
645 # grep matches initial and second but not third
646 # author matches only initial and third
647 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
649 echo initial
650 } >expect &&
651 test_cmp expect actual
654 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
655 # grep matches only initial and third
656 # author matches all but second
657 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
659 echo third && echo initial
660 } >expect &&
661 test_cmp expect actual
664 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
665 # grep matches only initial and third
666 # author matches all but second
667 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
669 echo third && echo initial
670 } >expect &&
671 test_cmp expect actual
674 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
675 # grep matches only third
676 # author matches only initial and third
677 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
679 echo third
680 } >expect &&
681 test_cmp expect actual
684 test_expect_success 'log --author does not search in timestamp' '
685 : >expect &&
686 git log --author="$GIT_AUTHOR_DATE" >actual &&
687 test_cmp expect actual
690 test_expect_success 'log --committer does not search in timestamp' '
691 : >expect &&
692 git log --committer="$GIT_COMMITTER_DATE" >actual &&
693 test_cmp expect actual
696 test_expect_success 'grep with CE_VALID file' '
697 git update-index --assume-unchanged t/t &&
698 rm t/t &&
699 test "$(git grep test)" = "t/t:test" &&
700 git update-index --no-assume-unchanged t/t &&
701 git checkout t/t
704 cat >expected <<EOF
705 hello.c=#include <stdio.h>
706 hello.c: return 0;
709 test_expect_success 'grep -p with userdiff' '
710 git config diff.custom.funcname "^#" &&
711 echo "hello.c diff=custom" >.gitattributes &&
712 git grep -p return >actual &&
713 test_cmp expected actual
716 cat >expected <<EOF
717 hello.c=int main(int argc, const char **argv)
718 hello.c: return 0;
721 test_expect_success 'grep -p' '
722 rm -f .gitattributes &&
723 git grep -p return >actual &&
724 test_cmp expected actual
727 cat >expected <<EOF
728 hello.c-#include <stdio.h>
729 hello.c-
730 hello.c=int main(int argc, const char **argv)
731 hello.c-{
732 hello.c- printf("Hello world.\n");
733 hello.c: return 0;
736 test_expect_success 'grep -p -B5' '
737 git grep -p -B5 return >actual &&
738 test_cmp expected actual
741 cat >expected <<EOF
742 hello.c=int main(int argc, const char **argv)
743 hello.c-{
744 hello.c- printf("Hello world.\n");
745 hello.c: return 0;
746 hello.c- /* char ?? */
747 hello.c-}
750 test_expect_success 'grep -W' '
751 git grep -W return >actual &&
752 test_cmp expected actual
755 cat >expected <<EOF
756 hello.c-#include <assert.h>
757 hello.c:#include <stdio.h>
760 test_expect_success 'grep -W shows no trailing empty lines' '
761 git grep -W stdio >actual &&
762 test_cmp expected actual
765 cat >expected <<EOF
766 hello.c= printf("Hello world.\n");
767 hello.c: return 0;
768 hello.c- /* char ?? */
771 test_expect_success 'grep -W with userdiff' '
772 test_when_finished "rm -f .gitattributes" &&
773 git config diff.custom.xfuncname "(printf.*|})$" &&
774 echo "hello.c diff=custom" >.gitattributes &&
775 git grep -W return >actual &&
776 test_cmp expected actual
779 test_expect_success 'grep from a subdirectory to search wider area (1)' '
780 mkdir -p s &&
782 cd s && git grep "x x x" ..
786 test_expect_success 'grep from a subdirectory to search wider area (2)' '
787 mkdir -p s &&
789 cd s || exit 1
790 ( git grep xxyyzz .. >out ; echo $? >status )
791 ! test -s out &&
792 test 1 = $(cat status)
796 cat >expected <<EOF
797 hello.c:int main(int argc, const char **argv)
800 test_expect_success 'grep -Fi' '
801 git grep -Fi "CHAR *" >actual &&
802 test_cmp expected actual
805 test_expect_success 'outside of git repository' '
806 rm -fr non &&
807 mkdir -p non/git/sub &&
808 echo hello >non/git/file1 &&
809 echo world >non/git/sub/file2 &&
811 echo file1:hello &&
812 echo sub/file2:world
813 } >non/expect.full &&
814 echo file2:world >non/expect.sub &&
816 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
817 export GIT_CEILING_DIRECTORIES &&
818 cd non/git &&
819 test_must_fail git grep o &&
820 git grep --no-index o >../actual.full &&
821 test_cmp ../expect.full ../actual.full &&
822 cd sub &&
823 test_must_fail git grep o &&
824 git grep --no-index o >../../actual.sub &&
825 test_cmp ../../expect.sub ../../actual.sub
826 ) &&
828 echo ".*o*" >non/git/.gitignore &&
830 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
831 export GIT_CEILING_DIRECTORIES &&
832 cd non/git &&
833 test_must_fail git grep o &&
834 git grep --no-index --exclude-standard o >../actual.full &&
835 test_cmp ../expect.full ../actual.full &&
838 echo ".gitignore:.*o*" &&
839 cat ../expect.full
840 } >../expect.with.ignored &&
841 git grep --no-index --no-exclude o >../actual.full &&
842 test_cmp ../expect.with.ignored ../actual.full
846 test_expect_success 'outside of git repository with fallbackToNoIndex' '
847 rm -fr non &&
848 mkdir -p non/git/sub &&
849 echo hello >non/git/file1 &&
850 echo world >non/git/sub/file2 &&
851 cat <<-\EOF >non/expect.full &&
852 file1:hello
853 sub/file2:world
855 echo file2:world >non/expect.sub &&
857 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
858 export GIT_CEILING_DIRECTORIES &&
859 cd non/git &&
860 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
861 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
862 test_cmp ../expect.full ../actual.full &&
863 cd sub &&
864 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
865 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
866 test_cmp ../../expect.sub ../../actual.sub
867 ) &&
869 echo ".*o*" >non/git/.gitignore &&
871 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
872 export GIT_CEILING_DIRECTORIES &&
873 cd non/git &&
874 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
875 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
876 test_cmp ../expect.full ../actual.full &&
879 echo ".gitignore:.*o*" &&
880 cat ../expect.full
881 } >../expect.with.ignored &&
882 git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
883 test_cmp ../expect.with.ignored ../actual.full
887 test_expect_success 'inside git repository but with --no-index' '
888 rm -fr is &&
889 mkdir -p is/git/sub &&
890 echo hello >is/git/file1 &&
891 echo world >is/git/sub/file2 &&
892 echo ".*o*" >is/git/.gitignore &&
894 echo file1:hello &&
895 echo sub/file2:world
896 } >is/expect.unignored &&
898 echo ".gitignore:.*o*" &&
899 cat is/expect.unignored
900 } >is/expect.full &&
901 : >is/expect.empty &&
902 echo file2:world >is/expect.sub &&
904 cd is/git &&
905 git init &&
906 test_must_fail git grep o >../actual.full &&
907 test_cmp ../expect.empty ../actual.full &&
909 git grep --untracked o >../actual.unignored &&
910 test_cmp ../expect.unignored ../actual.unignored &&
912 git grep --no-index o >../actual.full &&
913 test_cmp ../expect.full ../actual.full &&
915 git grep --no-index --exclude-standard o >../actual.unignored &&
916 test_cmp ../expect.unignored ../actual.unignored &&
918 cd sub &&
919 test_must_fail git grep o >../../actual.sub &&
920 test_cmp ../../expect.empty ../../actual.sub &&
922 git grep --no-index o >../../actual.sub &&
923 test_cmp ../../expect.sub ../../actual.sub &&
925 git grep --untracked o >../../actual.sub &&
926 test_cmp ../../expect.sub ../../actual.sub
930 test_expect_success 'grep --no-index descends into repos, but not .git' '
931 rm -fr non &&
932 mkdir -p non/git &&
934 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
935 export GIT_CEILING_DIRECTORIES &&
936 cd non/git &&
938 echo magic >file &&
939 git init repo &&
941 cd repo &&
942 echo magic >file &&
943 git add file &&
944 git commit -m foo &&
945 echo magic >.git/file
946 ) &&
948 cat >expect <<-\EOF &&
949 file
950 repo/file
952 git grep -l --no-index magic >actual &&
953 test_cmp expect actual
957 test_expect_success 'setup double-dash tests' '
958 cat >double-dash <<EOF &&
961 other
963 git add double-dash
966 cat >expected <<EOF
967 double-dash:->
969 test_expect_success 'grep -- pattern' '
970 git grep -- "->" >actual &&
971 test_cmp expected actual
973 test_expect_success 'grep -- pattern -- pathspec' '
974 git grep -- "->" -- double-dash >actual &&
975 test_cmp expected actual
977 test_expect_success 'grep -e pattern -- path' '
978 git grep -e "->" -- double-dash >actual &&
979 test_cmp expected actual
982 cat >expected <<EOF
983 double-dash:--
985 test_expect_success 'grep -e -- -- path' '
986 git grep -e -- -- double-dash >actual &&
987 test_cmp expected actual
990 test_expect_success 'dashdash disambiguates rev as rev' '
991 test_when_finished "rm -f master" &&
992 echo content >master &&
993 echo master:hello.c >expect &&
994 git grep -l o master -- hello.c >actual &&
995 test_cmp expect actual
998 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
999 test_when_finished "git rm -f master" &&
1000 echo content >master &&
1001 git add master &&
1002 echo master:content >expect &&
1003 git grep o -- master >actual &&
1004 test_cmp expect actual
1007 test_expect_success 'report bogus arg without dashdash' '
1008 test_must_fail git grep o does-not-exist
1011 test_expect_success 'report bogus rev with dashdash' '
1012 test_must_fail git grep o hello.c --
1015 test_expect_success 'allow non-existent path with dashdash' '
1016 # We need a real match so grep exits with success.
1017 tree=$(git ls-tree HEAD |
1018 sed s/hello.c/not-in-working-tree/ |
1019 git mktree) &&
1020 git grep o "$tree" -- not-in-working-tree
1023 test_expect_success 'grep --no-index pattern -- path' '
1024 rm -fr non &&
1025 mkdir -p non/git &&
1027 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1028 export GIT_CEILING_DIRECTORIES &&
1029 cd non/git &&
1030 echo hello >hello &&
1031 echo goodbye >goodbye &&
1032 echo hello:hello >expect &&
1033 git grep --no-index o -- hello >actual &&
1034 test_cmp expect actual
1038 test_expect_success 'grep --no-index complains of revs' '
1039 test_must_fail git grep --no-index o master -- 2>err &&
1040 test_i18ngrep "cannot be used with revs" err
1043 test_expect_success 'grep --no-index prefers paths to revs' '
1044 test_when_finished "rm -f master" &&
1045 echo content >master &&
1046 echo master:content >expect &&
1047 git grep --no-index o master >actual &&
1048 test_cmp expect actual
1051 test_expect_success 'grep --no-index does not "diagnose" revs' '
1052 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1053 test_i18ngrep ! -i "did you mean" err
1056 cat >expected <<EOF
1057 hello.c:int main(int argc, const char **argv)
1058 hello.c: printf("Hello world.\n");
1061 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
1062 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1063 test_cmp expected actual
1066 test_expect_success LIBPCRE 'grep -P pattern' '
1067 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1068 test_cmp expected actual
1071 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1072 >empty &&
1073 test_must_fail git -c grep.extendedregexp=true \
1074 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1075 test_cmp empty actual
1078 test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
1079 git -c grep.extendedregexp=true \
1080 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1081 test_cmp expected actual
1084 test_expect_success LIBPCRE 'grep -P -v pattern' '
1086 echo "ab:a+b*c"
1087 echo "ab:a+bc"
1088 } >expected &&
1089 git grep -P -v "abc" ab >actual &&
1090 test_cmp expected actual
1093 test_expect_success LIBPCRE 'grep -P -i pattern' '
1094 cat >expected <<-EOF &&
1095 hello.c: printf("Hello world.\n");
1097 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1098 test_cmp expected actual
1101 test_expect_success LIBPCRE 'grep -P -w pattern' '
1103 echo "hello_world:Hello world"
1104 echo "hello_world:HeLLo world"
1105 } >expected &&
1106 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1107 test_cmp expected actual
1110 test_expect_success 'grep -G invalidpattern properly dies ' '
1111 test_must_fail git grep -G "a["
1114 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1115 test_must_fail git -c grep.patterntype=basic grep "a["
1118 test_expect_success 'grep -E invalidpattern properly dies ' '
1119 test_must_fail git grep -E "a["
1122 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1123 test_must_fail git -c grep.patterntype=extended grep "a["
1126 test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
1127 test_must_fail git grep -P "a["
1130 test_expect_success LIBPCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1131 test_must_fail git -c grep.patterntype=perl grep "a["
1134 test_expect_success 'grep -G -E -F pattern' '
1135 echo "ab:a+b*c" >expected &&
1136 git grep -G -E -F "a+b*c" ab >actual &&
1137 test_cmp expected actual
1140 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1141 echo "ab:a+b*c" >expected &&
1142 git \
1143 -c grep.patterntype=basic \
1144 -c grep.patterntype=extended \
1145 -c grep.patterntype=fixed \
1146 grep "a+b*c" ab >actual &&
1147 test_cmp expected actual
1150 test_expect_success 'grep -E -F -G pattern' '
1151 echo "ab:a+bc" >expected &&
1152 git grep -E -F -G "a+b*c" ab >actual &&
1153 test_cmp expected actual
1156 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1157 echo "ab:a+bc" >expected &&
1158 git \
1159 -c grep.patterntype=extended \
1160 -c grep.patterntype=fixed \
1161 -c grep.patterntype=basic \
1162 grep "a+b*c" ab >actual &&
1163 test_cmp expected actual
1166 test_expect_success 'grep -F -G -E pattern' '
1167 echo "ab:abc" >expected &&
1168 git grep -F -G -E "a+b*c" ab >actual &&
1169 test_cmp expected actual
1172 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1173 echo "ab:abc" >expected &&
1174 git \
1175 -c grep.patterntype=fixed \
1176 -c grep.patterntype=basic \
1177 -c grep.patterntype=extended \
1178 grep "a+b*c" ab >actual &&
1179 test_cmp expected actual
1182 test_expect_success 'grep -G -F -P -E pattern' '
1183 echo "d0:d" >expected &&
1184 git grep -G -F -P -E "[\d]" d0 >actual &&
1185 test_cmp expected actual
1188 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1189 echo "d0:d" >expected &&
1190 git \
1191 -c grep.patterntype=fixed \
1192 -c grep.patterntype=basic \
1193 -c grep.patterntype=perl \
1194 -c grep.patterntype=extended \
1195 grep "[\d]" d0 >actual &&
1196 test_cmp expected actual
1199 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1200 echo "d0:0" >expected &&
1201 git grep -G -F -E -P "[\d]" d0 >actual &&
1202 test_cmp expected actual
1205 test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1206 echo "d0:0" >expected &&
1207 git \
1208 -c grep.patterntype=fixed \
1209 -c grep.patterntype=basic \
1210 -c grep.patterntype=extended \
1211 -c grep.patterntype=perl \
1212 grep "[\d]" d0 >actual &&
1213 test_cmp expected actual
1216 test_expect_success LIBPCRE 'grep -P pattern with grep.patternType=fixed' '
1217 echo "ab:a+b*c" >expected &&
1218 git \
1219 -c grep.patterntype=fixed \
1220 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1221 test_cmp expected actual
1224 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1225 echo "ab:a+b*c" >expected &&
1226 git \
1227 -c grep.patterntype=basic \
1228 grep -F "*c" ab >actual &&
1229 test_cmp expected actual
1232 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1234 echo "ab:a+b*c"
1235 echo "ab:a+bc"
1236 } >expected &&
1237 git \
1238 -c grep.patterntype=fixed \
1239 grep -G "a+b" ab >actual &&
1240 test_cmp expected actual
1243 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1245 echo "ab:a+b*c"
1246 echo "ab:a+bc"
1247 echo "ab:abc"
1248 } >expected &&
1249 git \
1250 -c grep.patterntype=fixed \
1251 grep -E "a+" ab >actual &&
1252 test_cmp expected actual
1255 cat >expected <<EOF
1256 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1257 hello.c<RED>-<RESET>{
1258 <RED>--<RESET>
1259 hello.c<RED>:<RESET> /* char ?? */
1260 hello.c<RED>-<RESET>}
1261 <RED>--<RESET>
1262 hello_world<RED>:<RESET>Hello_world
1263 hello_world<RED>-<RESET>HeLLo_world
1266 test_expect_success 'grep --color, separator' '
1267 test_config color.grep.context normal &&
1268 test_config color.grep.filename normal &&
1269 test_config color.grep.function normal &&
1270 test_config color.grep.linenumber normal &&
1271 test_config color.grep.match normal &&
1272 test_config color.grep.selected normal &&
1273 test_config color.grep.separator red &&
1275 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1276 test_decode_color >actual &&
1277 test_cmp expected actual
1280 cat >expected <<EOF
1281 hello.c:int main(int argc, const char **argv)
1282 hello.c: /* char ?? */
1284 hello_world:Hello_world
1287 test_expect_success 'grep --break' '
1288 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1289 test_cmp expected actual
1292 cat >expected <<EOF
1293 hello.c:int main(int argc, const char **argv)
1294 hello.c-{
1296 hello.c: /* char ?? */
1297 hello.c-}
1299 hello_world:Hello_world
1300 hello_world-HeLLo_world
1303 test_expect_success 'grep --break with context' '
1304 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1305 test_cmp expected actual
1308 cat >expected <<EOF
1309 hello.c
1310 int main(int argc, const char **argv)
1311 /* char ?? */
1312 hello_world
1313 Hello_world
1316 test_expect_success 'grep --heading' '
1317 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1318 test_cmp expected actual
1321 cat >expected <<EOF
1322 <BOLD;GREEN>hello.c<RESET>
1323 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1324 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1326 <BOLD;GREEN>hello_world<RESET>
1327 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1330 test_expect_success 'mimic ack-grep --group' '
1331 test_config color.grep.context normal &&
1332 test_config color.grep.filename "bold green" &&
1333 test_config color.grep.function normal &&
1334 test_config color.grep.linenumber normal &&
1335 test_config color.grep.match "black yellow" &&
1336 test_config color.grep.selected normal &&
1337 test_config color.grep.separator normal &&
1339 git grep --break --heading -n --color \
1340 -e char -e lo_w hello.c hello_world |
1341 test_decode_color >actual &&
1342 test_cmp expected actual
1345 cat >expected <<EOF
1346 space: line with leading space1
1347 space: line with leading space2
1348 space: line with leading space3
1351 test_expect_success LIBPCRE 'grep -E "^ "' '
1352 git grep -E "^ " space >actual &&
1353 test_cmp expected actual
1356 test_expect_success LIBPCRE 'grep -P "^ "' '
1357 git grep -P "^ " space >actual &&
1358 test_cmp expected actual
1361 cat >expected <<EOF
1362 space-line without leading space1
1363 space: line <RED>with <RESET>leading space1
1364 space: line <RED>with <RESET>leading <RED>space2<RESET>
1365 space: line <RED>with <RESET>leading space3
1366 space:line without leading <RED>space2<RESET>
1369 test_expect_success 'grep --color -e A -e B with context' '
1370 test_config color.grep.context normal &&
1371 test_config color.grep.filename normal &&
1372 test_config color.grep.function normal &&
1373 test_config color.grep.linenumber normal &&
1374 test_config color.grep.matchContext normal &&
1375 test_config color.grep.matchSelected red &&
1376 test_config color.grep.selected normal &&
1377 test_config color.grep.separator normal &&
1379 git grep --color=always -C2 -e "with " -e space2 space |
1380 test_decode_color >actual &&
1381 test_cmp expected actual
1384 cat >expected <<EOF
1385 space-line without leading space1
1386 space- line with leading space1
1387 space: line <RED>with <RESET>leading <RED>space2<RESET>
1388 space- line with leading space3
1389 space-line without leading space2
1392 test_expect_success 'grep --color -e A --and -e B with context' '
1393 test_config color.grep.context normal &&
1394 test_config color.grep.filename normal &&
1395 test_config color.grep.function normal &&
1396 test_config color.grep.linenumber normal &&
1397 test_config color.grep.matchContext normal &&
1398 test_config color.grep.matchSelected red &&
1399 test_config color.grep.selected normal &&
1400 test_config color.grep.separator normal &&
1402 git grep --color=always -C2 -e "with " --and -e space2 space |
1403 test_decode_color >actual &&
1404 test_cmp expected actual
1407 cat >expected <<EOF
1408 space-line without leading space1
1409 space: line <RED>with <RESET>leading space1
1410 space- line with leading space2
1411 space: line <RED>with <RESET>leading space3
1412 space-line without leading space2
1415 test_expect_success 'grep --color -e A --and --not -e B with context' '
1416 test_config color.grep.context normal &&
1417 test_config color.grep.filename normal &&
1418 test_config color.grep.function normal &&
1419 test_config color.grep.linenumber normal &&
1420 test_config color.grep.matchContext normal &&
1421 test_config color.grep.matchSelected red &&
1422 test_config color.grep.selected normal &&
1423 test_config color.grep.separator normal &&
1425 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1426 test_decode_color >actual &&
1427 test_cmp expected actual
1430 cat >expected <<EOF
1431 hello.c-
1432 hello.c=int main(int argc, const char **argv)
1433 hello.c-{
1434 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1435 hello.c- return 0;
1436 hello.c- /* char ?? */
1437 hello.c-}
1440 test_expect_success 'grep --color -e A --and -e B -p with context' '
1441 test_config color.grep.context normal &&
1442 test_config color.grep.filename normal &&
1443 test_config color.grep.function normal &&
1444 test_config color.grep.linenumber normal &&
1445 test_config color.grep.matchContext normal &&
1446 test_config color.grep.matchSelected red &&
1447 test_config color.grep.selected normal &&
1448 test_config color.grep.separator normal &&
1450 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1451 test_decode_color >actual &&
1452 test_cmp expected actual
1455 test_expect_success 'grep can find things only in the work tree' '
1456 : >work-tree-only &&
1457 git add work-tree-only &&
1458 test_when_finished "git rm -f work-tree-only" &&
1459 echo "find in work tree" >work-tree-only &&
1460 git grep --quiet "find in work tree" &&
1461 test_must_fail git grep --quiet --cached "find in work tree" &&
1462 test_must_fail git grep --quiet "find in work tree" HEAD
1465 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1466 echo "intend to add this" >intend-to-add &&
1467 git add -N intend-to-add &&
1468 test_when_finished "git rm -f intend-to-add" &&
1469 git grep --quiet "intend to add this" &&
1470 test_must_fail git grep --quiet --cached "intend to add this" &&
1471 test_must_fail git grep --quiet "intend to add this" HEAD
1474 test_expect_success 'grep does not search work tree with assume unchanged' '
1475 echo "intend to add this" >intend-to-add &&
1476 git add -N intend-to-add &&
1477 git update-index --assume-unchanged intend-to-add &&
1478 test_when_finished "git rm -f intend-to-add" &&
1479 test_must_fail git grep --quiet "intend to add this" &&
1480 test_must_fail git grep --quiet --cached "intend to add this" &&
1481 test_must_fail git grep --quiet "intend to add this" HEAD
1484 test_expect_success 'grep can find things only in the index' '
1485 echo "only in the index" >cache-this &&
1486 git add cache-this &&
1487 rm cache-this &&
1488 test_when_finished "git rm --cached cache-this" &&
1489 test_must_fail git grep --quiet "only in the index" &&
1490 git grep --quiet --cached "only in the index" &&
1491 test_must_fail git grep --quiet "only in the index" HEAD
1494 test_expect_success 'grep does not report i-t-a with -L --cached' '
1495 echo "intend to add this" >intend-to-add &&
1496 git add -N intend-to-add &&
1497 test_when_finished "git rm -f intend-to-add" &&
1498 git ls-files | grep -v "^intend-to-add\$" >expected &&
1499 git grep -L --cached "nonexistent_string" >actual &&
1500 test_cmp expected actual
1503 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1504 echo "intend to add this" >intend-to-add-assume-unchanged &&
1505 git add -N intend-to-add-assume-unchanged &&
1506 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1507 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1508 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1509 git grep -L "nonexistent_string" >actual &&
1510 test_cmp expected actual
1513 test_done