3 # Copyright (c) 2006 Junio C Hamano
6 test_description
='git grep various.
13 int main(int argc, const char **argv)
15 printf("Hello world.\n");
21 test_expect_success setup
'
25 echo foo_mmap bar mmap
26 echo foo mmap bar_mmap
27 echo foo_mmap bar mmap baz
55 test_expect_success
'grep should not segfault with a bad input' '
56 test_must_fail git grep "("
62 HEAD
) HC
='HEAD:' L
='HEAD' ;;
63 '') HC
= L
='in working tree' ;;
66 test_expect_success
"grep -w $L" '
68 echo ${HC}file:1:foo mmap bar
69 echo ${HC}file:3:foo_mmap bar mmap
70 echo ${HC}file:4:foo mmap bar_mmap
71 echo ${HC}file:5:foo_mmap bar mmap baz
73 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
74 test_cmp expected actual
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
84 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
85 test_cmp expected actual
88 test_expect_success
"grep -w $L" '
90 echo ${HC}file:foo mmap bar
91 echo ${HC}file:foo_mmap bar mmap
92 echo ${HC}file:foo mmap bar_mmap
93 echo ${HC}file:foo_mmap bar mmap baz
95 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
96 test_cmp expected actual
99 test_expect_success
"grep -w $L (w)" '
101 test_must_fail git grep -n -w -e "^w" >actual &&
102 test_cmp expected actual
105 test_expect_success
"grep -w $L (x)" '
107 echo ${HC}x:1:x x xx x
109 git grep -n -w -e "x xx* x" $H >actual &&
110 test_cmp expected actual
113 test_expect_success
"grep -w $L (y-1)" '
117 git grep -n -w -e "^y" $H >actual &&
118 test_cmp expected actual
121 test_expect_success
"grep -w $L (y-2)" '
123 if git grep -n -w -e "^y y" $H >actual
125 echo should not have matched
129 test_cmp expected actual
133 test_expect_success
"grep -w $L (z)" '
135 if git grep -n -w -e "^z" $H >actual
137 echo should not have matched
141 test_cmp expected actual
145 test_expect_success
"grep $L (t-1)" '
146 echo "${HC}t/t:1:test" >expected &&
147 git grep -n -e test $H >actual &&
148 test_cmp expected actual
151 test_expect_success
"grep $L (t-2)" '
152 echo "${HC}t:1:test" >expected &&
155 git grep -n -e test $H
157 test_cmp expected actual
160 test_expect_success
"grep $L (t-3)" '
161 echo "${HC}t/t:1:test" >expected &&
164 git grep --full-name -n -e test $H
166 test_cmp expected actual
169 test_expect_success
"grep -c $L (no /dev/null)" '
170 ! git grep -c test $H | grep /dev/null
173 test_expect_success
"grep --max-depth -1 $L" '
175 echo ${HC}t/a/v:1:vvv
179 git grep --max-depth -1 -n -e vvv $H >actual &&
180 test_cmp expected actual
183 test_expect_success
"grep --max-depth 0 $L" '
187 git grep --max-depth 0 -n -e vvv $H >actual &&
188 test_cmp expected actual
191 test_expect_success
"grep --max-depth 0 -- '*' $L" '
193 echo ${HC}t/a/v:1:vvv
197 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
198 test_cmp expected actual
201 test_expect_success
"grep --max-depth 1 $L" '
206 git grep --max-depth 1 -n -e vvv $H >actual &&
207 test_cmp expected actual
210 test_expect_success
"grep --max-depth 0 -- t $L" '
214 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
215 test_cmp expected actual
218 test_expect_success
"grep --max-depth 0 -- . t $L" '
223 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
224 test_cmp expected actual
227 test_expect_success
"grep --max-depth 0 -- t . $L" '
232 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
233 test_cmp expected actual
235 test_expect_success
"grep $L with grep.extendedRegexp=false" '
236 echo "ab:a+bc" >expected &&
237 git -c grep.extendedRegexp=false grep "a+b*c" ab >actual &&
238 test_cmp expected actual
241 test_expect_success
"grep $L with grep.extendedRegexp=true" '
242 echo "ab:abc" >expected &&
243 git -c grep.extendedRegexp=true grep "a+b*c" ab >actual &&
244 test_cmp expected actual
251 test_expect_success
'grep -l -C' '
252 git grep -l -C1 foo >actual &&
253 test_cmp expected actual
259 test_expect_success
'grep -l -C' '
260 git grep -c -C1 foo >actual &&
261 test_cmp expected actual
264 test_expect_success
'grep -L -C' '
265 git ls-files >expected &&
266 git grep -L -C1 nonexistent_string >actual &&
267 test_cmp expected actual
271 file:foo mmap bar_mmap
274 test_expect_success
'grep -e A --and -e B' '
275 git grep -e "foo mmap" --and -e bar_mmap >actual &&
276 test_cmp expected actual
280 file:foo_mmap bar mmap
281 file:foo_mmap bar mmap baz
285 test_expect_success
'grep ( -e A --or -e B ) --and -e B' '
286 git grep \( -e foo_ --or -e baz \) \
287 --and -e " mmap" >actual &&
288 test_cmp expected actual
295 test_expect_success
'grep -e A --and --not -e B' '
296 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
297 test_cmp expected actual
300 test_expect_success
'grep should ignore GREP_OPTIONS' '
301 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
302 test_cmp expected actual
305 test_expect_success
'grep -f, non-existent file' '
306 test_must_fail git grep -f patterns
312 file:foo_mmap bar mmap
313 file:foo mmap bar_mmap
314 file:foo_mmap bar mmap baz
321 test_expect_success
'grep -f, one pattern' '
322 git grep -f pattern >actual &&
323 test_cmp expected actual
329 file:foo_mmap bar mmap
330 file:foo mmap bar_mmap
331 file:foo_mmap bar mmap baz
342 test_expect_success
'grep -f, multiple patterns' '
343 git grep -f patterns >actual &&
344 test_cmp expected actual
350 file:foo_mmap bar mmap
351 file:foo mmap bar_mmap
352 file:foo_mmap bar mmap baz
366 test_expect_success
'grep -f, ignore empty lines' '
367 git grep -f patterns >actual &&
368 test_cmp expected actual
371 test_expect_success
'grep -f, ignore empty lines, read patterns from stdin' '
372 git grep -f - <patterns >actual &&
373 test_cmp expected actual
382 test_expect_success
'grep -q, silently report matches' '
384 git grep -q mmap >actual &&
385 test_cmp empty actual &&
386 test_must_fail git grep -q qfwfq >actual &&
387 test_cmp empty actual
390 test_expect_success
'grep -C1 hunk mark between files' '
391 git grep -C1 "^[yz]" >actual &&
392 test_cmp expected actual
395 test_expect_success
'log grep setup' '
398 GIT_AUTHOR_NAME="With * Asterisk" \
399 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
400 git commit -a -m "second" &&
404 git commit -a -m "third" &&
408 GIT_AUTHOR_NAME="Night Fall" \
409 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
410 git commit -a -m "fourth"
413 test_expect_success
'log grep (1)' '
414 git log --author=author --pretty=tformat:%s >actual &&
415 ( echo third ; echo initial ) >expect &&
416 test_cmp expect actual
419 test_expect_success
'log grep (2)' '
420 git log --author=" * " -F --pretty=tformat:%s >actual &&
421 ( echo second ) >expect &&
422 test_cmp expect actual
425 test_expect_success
'log grep (3)' '
426 git log --author="^A U" --pretty=tformat:%s >actual &&
427 ( echo third ; echo initial ) >expect &&
428 test_cmp expect actual
431 test_expect_success
'log grep (4)' '
432 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
433 ( echo second ) >expect &&
434 test_cmp expect actual
437 test_expect_success
'log grep (5)' '
438 git log --author=Thor -F --pretty=tformat:%s >actual &&
439 ( echo third ; echo initial ) >expect &&
440 test_cmp expect actual
443 test_expect_success
'log grep (6)' '
444 git log --author=-0700 --pretty=tformat:%s >actual &&
446 test_cmp expect actual
449 test_expect_success
'log --grep --author implicitly uses all-match' '
450 # grep matches initial and second but not third
451 # author matches only initial and third
452 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
453 echo initial >expect &&
454 test_cmp expect actual
457 test_expect_success
'log with multiple --author uses union' '
458 git log --author="Thor" --author="Aster" --format=%s >actual &&
460 echo third && echo second && echo initial
462 test_cmp expect actual
465 test_expect_success
'log with --grep and multiple --author uses all-match' '
466 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
468 echo third && echo initial
470 test_cmp expect actual
473 test_expect_success
'log with --grep and multiple --author uses all-match' '
474 git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
476 test_cmp expect actual
479 test_expect_success
'grep with CE_VALID file' '
480 git update-index --assume-unchanged t/t &&
482 test "$(git grep test)" = "t/t:test" &&
483 git update-index --no-assume-unchanged t/t &&
488 hello.c=#include <stdio.h>
492 test_expect_success
'grep -p with userdiff' '
493 git config diff.custom.funcname "^#" &&
494 echo "hello.c diff=custom" >.gitattributes &&
495 git grep -p return >actual &&
496 test_cmp expected actual
500 hello.c=int main(int argc, const char **argv)
504 test_expect_success
'grep -p' '
505 rm -f .gitattributes &&
506 git grep -p return >actual &&
507 test_cmp expected actual
511 hello.c-#include <stdio.h>
512 hello.c=int main(int argc, const char **argv)
514 hello.c- printf("Hello world.\n");
518 test_expect_success
'grep -p -B5' '
519 git grep -p -B5 return >actual &&
520 test_cmp expected actual
524 hello.c=int main(int argc, const char **argv)
526 hello.c- printf("Hello world.\n");
528 hello.c- /* char ?? */
532 test_expect_success
'grep -W' '
533 git grep -W return >actual &&
534 test_cmp expected actual
538 hello.c= printf("Hello world.\n");
540 hello.c- /* char ?? */
543 test_expect_success
'grep -W with userdiff' '
544 test_when_finished "rm -f .gitattributes" &&
545 git config diff.custom.xfuncname "(printf.*|})$" &&
546 echo "hello.c diff=custom" >.gitattributes &&
547 git grep -W return >actual &&
548 test_cmp expected actual
551 test_expect_success
'grep from a subdirectory to search wider area (1)' '
554 cd s && git grep "x x x" ..
558 test_expect_success
'grep from a subdirectory to search wider area (2)' '
562 ( git grep xxyyzz .. >out ; echo $? >status )
564 test 1 = $(cat status)
569 hello.c:int main(int argc, const char **argv)
572 test_expect_success
'grep -Fi' '
573 git grep -Fi "CHAR *" >actual &&
574 test_cmp expected actual
577 test_expect_success
'outside of git repository' '
579 mkdir -p non/git/sub &&
580 echo hello >non/git/file1 &&
581 echo world >non/git/sub/file2 &&
585 } >non/expect.full &&
586 echo file2:world >non/expect.sub &&
588 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
589 export GIT_CEILING_DIRECTORIES &&
591 test_must_fail git grep o &&
592 git grep --no-index o >../actual.full &&
593 test_cmp ../expect.full ../actual.full
595 test_must_fail git grep o &&
596 git grep --no-index o >../../actual.sub &&
597 test_cmp ../../expect.sub ../../actual.sub
600 echo ".*o*" >non/git/.gitignore &&
602 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
603 export GIT_CEILING_DIRECTORIES &&
605 test_must_fail git grep o &&
606 git grep --no-index --exclude-standard o >../actual.full &&
607 test_cmp ../expect.full ../actual.full &&
610 echo ".gitignore:.*o*"
612 } >../expect.with.ignored &&
613 git grep --no-index --no-exclude o >../actual.full &&
614 test_cmp ../expect.with.ignored ../actual.full
618 test_expect_success
'inside git repository but with --no-index' '
620 mkdir -p is/git/sub &&
621 echo hello >is/git/file1 &&
622 echo world >is/git/sub/file2 &&
623 echo ".*o*" >is/git/.gitignore &&
627 } >is/expect.unignored &&
629 echo ".gitignore:.*o*" &&
630 cat is/expect.unignored
632 : >is/expect.empty &&
633 echo file2:world >is/expect.sub &&
637 test_must_fail git grep o >../actual.full &&
638 test_cmp ../expect.empty ../actual.full &&
640 git grep --untracked o >../actual.unignored &&
641 test_cmp ../expect.unignored ../actual.unignored &&
643 git grep --no-index o >../actual.full &&
644 test_cmp ../expect.full ../actual.full &&
646 git grep --no-index --exclude-standard o >../actual.unignored &&
647 test_cmp ../expect.unignored ../actual.unignored &&
650 test_must_fail git grep o >../../actual.sub &&
651 test_cmp ../../expect.empty ../../actual.sub &&
653 git grep --no-index o >../../actual.sub &&
654 test_cmp ../../expect.sub ../../actual.sub &&
656 git grep --untracked o >../../actual.sub &&
657 test_cmp ../../expect.sub ../../actual.sub
661 test_expect_success
'setup double-dash tests' '
662 cat >double-dash <<EOF &&
673 test_expect_success
'grep -- pattern' '
674 git grep -- "->" >actual &&
675 test_cmp expected actual
677 test_expect_success
'grep -- pattern -- pathspec' '
678 git grep -- "->" -- double-dash >actual &&
679 test_cmp expected actual
681 test_expect_success
'grep -e pattern -- path' '
682 git grep -e "->" -- double-dash >actual &&
683 test_cmp expected actual
689 test_expect_success
'grep -e -- -- path' '
690 git grep -e -- -- double-dash >actual &&
691 test_cmp expected actual
695 hello.c:int main(int argc, const char **argv)
696 hello.c: printf("Hello world.\n");
699 test_expect_success LIBPCRE
'grep --perl-regexp pattern' '
700 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
701 test_cmp expected actual
704 test_expect_success LIBPCRE
'grep -P pattern' '
705 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
706 test_cmp expected actual
709 test_expect_success
'grep pattern with grep.extendedRegexp=true' '
711 test_must_fail git -c grep.extendedregexp=true \
712 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
713 test_cmp empty actual
716 test_expect_success LIBPCRE
'grep -P pattern with grep.extendedRegexp=true' '
717 git -c grep.extendedregexp=true \
718 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
719 test_cmp expected actual
722 test_expect_success LIBPCRE
'grep -P -v pattern' '
727 git grep -P -v "abc" ab >actual &&
728 test_cmp expected actual
731 test_expect_success LIBPCRE
'grep -P -i pattern' '
732 cat >expected <<-EOF &&
733 hello.c: printf("Hello world.\n");
735 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
736 test_cmp expected actual
739 test_expect_success LIBPCRE
'grep -P -w pattern' '
741 echo "hello_world:Hello world"
742 echo "hello_world:HeLLo world"
744 git grep -P -w "He((?i)ll)o" hello_world >actual &&
745 test_cmp expected actual
748 test_expect_success
'grep -G invalidpattern properly dies ' '
749 test_must_fail git grep -G "a["
752 test_expect_success
'grep -E invalidpattern properly dies ' '
753 test_must_fail git grep -E "a["
756 test_expect_success LIBPCRE
'grep -P invalidpattern properly dies ' '
757 test_must_fail git grep -P "a["
760 test_expect_success
'grep -G -E -F pattern' '
761 echo "ab:a+b*c" >expected &&
762 git grep -G -E -F "a+b*c" ab >actual &&
763 test_cmp expected actual
766 test_expect_success
'grep -E -F -G pattern' '
767 echo "ab:a+bc" >expected &&
768 git grep -E -F -G "a+b*c" ab >actual &&
769 test_cmp expected actual
772 test_expect_success
'grep -F -G -E pattern' '
773 echo "ab:abc" >expected &&
774 git grep -F -G -E "a+b*c" ab >actual &&
775 test_cmp expected actual
778 test_expect_success
'grep -G -F -P -E pattern' '
780 test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
781 test_cmp empty actual
784 test_expect_success LIBPCRE
'grep -G -F -E -P pattern' '
785 echo "ab:a+b*c" >expected &&
786 git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
787 test_cmp expected actual
791 git config
"$1" "$2" &&
792 test_when_finished
"git config --unset $1"
796 hello.c<RED>:<RESET>int main(int argc, const char **argv)
797 hello.c<RED>-<RESET>{
799 hello.c<RED>:<RESET> /* char ?? */
800 hello.c<RED>-<RESET>}
802 hello_world<RED>:<RESET>Hello_world
803 hello_world<RED>-<RESET>HeLLo_world
806 test_expect_success
'grep --color, separator' '
807 test_config color.grep.context normal &&
808 test_config color.grep.filename normal &&
809 test_config color.grep.function normal &&
810 test_config color.grep.linenumber normal &&
811 test_config color.grep.match normal &&
812 test_config color.grep.selected normal &&
813 test_config color.grep.separator red &&
815 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
816 test_decode_color >actual &&
817 test_cmp expected actual
821 hello.c:int main(int argc, const char **argv)
822 hello.c: /* char ?? */
824 hello_world:Hello_world
827 test_expect_success
'grep --break' '
828 git grep --break -e char -e lo_w hello.c hello_world >actual &&
829 test_cmp expected actual
833 hello.c:int main(int argc, const char **argv)
836 hello.c: /* char ?? */
839 hello_world:Hello_world
840 hello_world-HeLLo_world
843 test_expect_success
'grep --break with context' '
844 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
845 test_cmp expected actual
850 int main(int argc, const char **argv)
856 test_expect_success
'grep --heading' '
857 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
858 test_cmp expected actual
862 <BOLD;GREEN>hello.c<RESET>
863 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
864 6: /* <BLACK;BYELLOW>char<RESET> ?? */
866 <BOLD;GREEN>hello_world<RESET>
867 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
870 test_expect_success
'mimic ack-grep --group' '
871 test_config color.grep.context normal &&
872 test_config color.grep.filename "bold green" &&
873 test_config color.grep.function normal &&
874 test_config color.grep.linenumber normal &&
875 test_config color.grep.match "black yellow" &&
876 test_config color.grep.selected normal &&
877 test_config color.grep.separator normal &&
879 git grep --break --heading -n --color \
880 -e char -e lo_w hello.c hello_world |
881 test_decode_color >actual &&
882 test_cmp expected actual