Merge branch 'nd/maint-diffstat-summary' into maint
[git.git] / t / t7810-grep.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
5
6 test_description='git grep various.
7 '
8
9 . ./test-lib.sh
10
11 cat >hello.c <<EOF
12 #include <stdio.h>
13 int main(int argc, const char **argv)
14 {
15         printf("Hello world.\n");
16         return 0;
17         /* char ?? */
18 }
19 EOF
20
21 test_expect_success setup '
22         {
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 &&
29         {
30                 echo Hello world
31                 echo HeLLo world
32                 echo Hello_world
33                 echo HeLLo_world
34         } >hello_world &&
35         {
36                 echo "a+b*c"
37                 echo "a+bc"
38                 echo "abc"
39         } >ab &&
40         echo vvv >v &&
41         echo ww w >w &&
42         echo x x xx x >x &&
43         echo y yy >y &&
44         echo zzz > z &&
45         mkdir t &&
46         echo test >t/t &&
47         echo vvv >t/v &&
48         mkdir t/a &&
49         echo vvv >t/a/v &&
50         {
51                 echo "line without leading space1"
52                 echo " line with leading space1"
53                 echo " line with leading space2"
54                 echo " line with leading space3"
55                 echo "line without leading space2"
56         } >space &&
57         git add . &&
58         test_tick &&
59         git commit -m initial
60 '
61
62 test_expect_success 'grep should not segfault with a bad input' '
63         test_must_fail git grep "("
64 '
65
66 for H in HEAD ''
67 do
68         case "$H" in
69         HEAD)   HC='HEAD:' L='HEAD' ;;
70         '')     HC= L='in working tree' ;;
71         esac
72
73         test_expect_success "grep -w $L" '
74                 {
75                         echo ${HC}file:1:foo mmap bar
76                         echo ${HC}file:3:foo_mmap bar mmap
77                         echo ${HC}file:4:foo mmap bar_mmap
78                         echo ${HC}file:5:foo_mmap bar mmap baz
79                 } >expected &&
80                 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
81                 test_cmp expected actual
82         '
83
84         test_expect_success "grep -w $L" '
85                 {
86                         echo ${HC}file:1:foo mmap bar
87                         echo ${HC}file:3:foo_mmap bar mmap
88                         echo ${HC}file:4:foo mmap bar_mmap
89                         echo ${HC}file:5:foo_mmap bar mmap baz
90                 } >expected &&
91                 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
92                 test_cmp expected actual
93         '
94
95         test_expect_success "grep -w $L" '
96                 {
97                         echo ${HC}file:foo mmap bar
98                         echo ${HC}file:foo_mmap bar mmap
99                         echo ${HC}file:foo mmap bar_mmap
100                         echo ${HC}file:foo_mmap bar mmap baz
101                 } >expected &&
102                 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
103                 test_cmp expected actual
104         '
105
106         test_expect_success "grep -w $L (w)" '
107                 : >expected &&
108                 test_must_fail git grep -n -w -e "^w" >actual &&
109                 test_cmp expected actual
110         '
111
112         test_expect_success "grep -w $L (x)" '
113                 {
114                         echo ${HC}x:1:x x xx x
115                 } >expected &&
116                 git grep -n -w -e "x xx* x" $H >actual &&
117                 test_cmp expected actual
118         '
119
120         test_expect_success "grep -w $L (y-1)" '
121                 {
122                         echo ${HC}y:1:y yy
123                 } >expected &&
124                 git grep -n -w -e "^y" $H >actual &&
125                 test_cmp expected actual
126         '
127
128         test_expect_success "grep -w $L (y-2)" '
129                 : >expected &&
130                 if git grep -n -w -e "^y y" $H >actual
131                 then
132                         echo should not have matched
133                         cat actual
134                         false
135                 else
136                         test_cmp expected actual
137                 fi
138         '
139
140         test_expect_success "grep -w $L (z)" '
141                 : >expected &&
142                 if git grep -n -w -e "^z" $H >actual
143                 then
144                         echo should not have matched
145                         cat actual
146                         false
147                 else
148                         test_cmp expected actual
149                 fi
150         '
151
152         test_expect_success "grep $L (t-1)" '
153                 echo "${HC}t/t:1:test" >expected &&
154                 git grep -n -e test $H >actual &&
155                 test_cmp expected actual
156         '
157
158         test_expect_success "grep $L (t-2)" '
159                 echo "${HC}t:1:test" >expected &&
160                 (
161                         cd t &&
162                         git grep -n -e test $H
163                 ) >actual &&
164                 test_cmp expected actual
165         '
166
167         test_expect_success "grep $L (t-3)" '
168                 echo "${HC}t/t:1:test" >expected &&
169                 (
170                         cd t &&
171                         git grep --full-name -n -e test $H
172                 ) >actual &&
173                 test_cmp expected actual
174         '
175
176         test_expect_success "grep -c $L (no /dev/null)" '
177                 ! git grep -c test $H | grep /dev/null
178         '
179
180         test_expect_success "grep --max-depth -1 $L" '
181                 {
182                         echo ${HC}t/a/v:1:vvv
183                         echo ${HC}t/v:1:vvv
184                         echo ${HC}v:1:vvv
185                 } >expected &&
186                 git grep --max-depth -1 -n -e vvv $H >actual &&
187                 test_cmp expected actual
188         '
189
190         test_expect_success "grep --max-depth 0 $L" '
191                 {
192                         echo ${HC}v:1:vvv
193                 } >expected &&
194                 git grep --max-depth 0 -n -e vvv $H >actual &&
195                 test_cmp expected actual
196         '
197
198         test_expect_success "grep --max-depth 0 -- '*' $L" '
199                 {
200                         echo ${HC}t/a/v:1:vvv
201                         echo ${HC}t/v:1:vvv
202                         echo ${HC}v:1:vvv
203                 } >expected &&
204                 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
205                 test_cmp expected actual
206         '
207
208         test_expect_success "grep --max-depth 1 $L" '
209                 {
210                         echo ${HC}t/v:1:vvv
211                         echo ${HC}v:1:vvv
212                 } >expected &&
213                 git grep --max-depth 1 -n -e vvv $H >actual &&
214                 test_cmp expected actual
215         '
216
217         test_expect_success "grep --max-depth 0 -- t $L" '
218                 {
219                         echo ${HC}t/v:1:vvv
220                 } >expected &&
221                 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
222                 test_cmp expected actual
223         '
224
225         test_expect_success "grep --max-depth 0 -- . t $L" '
226                 {
227                         echo ${HC}t/v:1:vvv
228                         echo ${HC}v:1:vvv
229                 } >expected &&
230                 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
231                 test_cmp expected actual
232         '
233
234         test_expect_success "grep --max-depth 0 -- t . $L" '
235                 {
236                         echo ${HC}t/v:1:vvv
237                         echo ${HC}v:1:vvv
238                 } >expected &&
239                 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
240                 test_cmp expected actual
241         '
242         test_expect_success "grep $L with grep.extendedRegexp=false" '
243                 echo "ab:a+bc" >expected &&
244                 git -c grep.extendedRegexp=false grep "a+b*c" ab >actual &&
245                 test_cmp expected actual
246         '
247
248         test_expect_success "grep $L with grep.extendedRegexp=true" '
249                 echo "ab:abc" >expected &&
250                 git -c grep.extendedRegexp=true grep "a+b*c" ab >actual &&
251                 test_cmp expected actual
252         '
253 done
254
255 cat >expected <<EOF
256 file
257 EOF
258 test_expect_success 'grep -l -C' '
259         git grep -l -C1 foo >actual &&
260         test_cmp expected actual
261 '
262
263 cat >expected <<EOF
264 file:5
265 EOF
266 test_expect_success 'grep -l -C' '
267         git grep -c -C1 foo >actual &&
268         test_cmp expected actual
269 '
270
271 test_expect_success 'grep -L -C' '
272         git ls-files >expected &&
273         git grep -L -C1 nonexistent_string >actual &&
274         test_cmp expected actual
275 '
276
277 cat >expected <<EOF
278 file:foo mmap bar_mmap
279 EOF
280
281 test_expect_success 'grep -e A --and -e B' '
282         git grep -e "foo mmap" --and -e bar_mmap >actual &&
283         test_cmp expected actual
284 '
285
286 cat >expected <<EOF
287 file:foo_mmap bar mmap
288 file:foo_mmap bar mmap baz
289 EOF
290
291
292 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
293         git grep \( -e foo_ --or -e baz \) \
294                 --and -e " mmap" >actual &&
295         test_cmp expected actual
296 '
297
298 cat >expected <<EOF
299 file:foo mmap bar
300 EOF
301
302 test_expect_success 'grep -e A --and --not -e B' '
303         git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
304         test_cmp expected actual
305 '
306
307 test_expect_success 'grep should ignore GREP_OPTIONS' '
308         GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
309         test_cmp expected actual
310 '
311
312 test_expect_success 'grep -f, non-existent file' '
313         test_must_fail git grep -f patterns
314 '
315
316 cat >expected <<EOF
317 file:foo mmap bar
318 file:foo_mmap bar
319 file:foo_mmap bar mmap
320 file:foo mmap bar_mmap
321 file:foo_mmap bar mmap baz
322 EOF
323
324 cat >pattern <<EOF
325 mmap
326 EOF
327
328 test_expect_success 'grep -f, one pattern' '
329         git grep -f pattern >actual &&
330         test_cmp expected actual
331 '
332
333 cat >expected <<EOF
334 file:foo mmap bar
335 file:foo_mmap bar
336 file:foo_mmap bar mmap
337 file:foo mmap bar_mmap
338 file:foo_mmap bar mmap baz
339 t/a/v:vvv
340 t/v:vvv
341 v:vvv
342 EOF
343
344 cat >patterns <<EOF
345 mmap
346 vvv
347 EOF
348
349 test_expect_success 'grep -f, multiple patterns' '
350         git grep -f patterns >actual &&
351         test_cmp expected actual
352 '
353
354 test_expect_success 'grep, multiple patterns' '
355         git grep "$(cat patterns)" >actual &&
356         test_cmp expected actual
357 '
358
359 cat >expected <<EOF
360 file:foo mmap bar
361 file:foo_mmap bar
362 file:foo_mmap bar mmap
363 file:foo mmap bar_mmap
364 file:foo_mmap bar mmap baz
365 t/a/v:vvv
366 t/v:vvv
367 v:vvv
368 EOF
369
370 cat >patterns <<EOF
371
372 mmap
373
374 vvv
375
376 EOF
377
378 test_expect_success 'grep -f, ignore empty lines' '
379         git grep -f patterns >actual &&
380         test_cmp expected actual
381 '
382
383 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
384         git grep -f - <patterns >actual &&
385         test_cmp expected actual
386 '
387
388 cat >expected <<EOF
389 y:y yy
390 --
391 z:zzz
392 EOF
393
394 test_expect_success 'grep -q, silently report matches' '
395         >empty &&
396         git grep -q mmap >actual &&
397         test_cmp empty actual &&
398         test_must_fail git grep -q qfwfq >actual &&
399         test_cmp empty actual
400 '
401
402 test_expect_success 'grep -C1 hunk mark between files' '
403         git grep -C1 "^[yz]" >actual &&
404         test_cmp expected actual
405 '
406
407 test_expect_success 'log grep setup' '
408         echo a >>file &&
409         test_tick &&
410         GIT_AUTHOR_NAME="With * Asterisk" \
411         GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
412         git commit -a -m "second" &&
413
414         echo a >>file &&
415         test_tick &&
416         git commit -a -m "third" &&
417
418         echo a >>file &&
419         test_tick &&
420         GIT_AUTHOR_NAME="Night Fall" \
421         GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
422         git commit -a -m "fourth"
423 '
424
425 test_expect_success 'log grep (1)' '
426         git log --author=author --pretty=tformat:%s >actual &&
427         ( echo third ; echo initial ) >expect &&
428         test_cmp expect actual
429 '
430
431 test_expect_success 'log grep (2)' '
432         git log --author=" * " -F --pretty=tformat:%s >actual &&
433         ( echo second ) >expect &&
434         test_cmp expect actual
435 '
436
437 test_expect_success 'log grep (3)' '
438         git log --author="^A U" --pretty=tformat:%s >actual &&
439         ( echo third ; echo initial ) >expect &&
440         test_cmp expect actual
441 '
442
443 test_expect_success 'log grep (4)' '
444         git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
445         ( echo second ) >expect &&
446         test_cmp expect actual
447 '
448
449 test_expect_success 'log grep (5)' '
450         git log --author=Thor -F --pretty=tformat:%s >actual &&
451         ( echo third ; echo initial ) >expect &&
452         test_cmp expect actual
453 '
454
455 test_expect_success 'log grep (6)' '
456         git log --author=-0700  --pretty=tformat:%s >actual &&
457         >expect &&
458         test_cmp expect actual
459 '
460
461 test_expect_success 'log --grep --author implicitly uses all-match' '
462         # grep matches initial and second but not third
463         # author matches only initial and third
464         git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
465         echo initial >expect &&
466         test_cmp expect actual
467 '
468
469 test_expect_success 'log with multiple --author uses union' '
470         git log --author="Thor" --author="Aster" --format=%s >actual &&
471         {
472             echo third && echo second && echo initial
473         } >expect &&
474         test_cmp expect actual
475 '
476
477 test_expect_success 'log with --grep and multiple --author uses all-match' '
478         git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
479         {
480             echo third && echo initial
481         } >expect &&
482         test_cmp expect actual
483 '
484
485 test_expect_success 'log with --grep and multiple --author uses all-match' '
486         git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
487         >expect &&
488         test_cmp expect actual
489 '
490
491 test_expect_success 'grep with CE_VALID file' '
492         git update-index --assume-unchanged t/t &&
493         rm t/t &&
494         test "$(git grep test)" = "t/t:test" &&
495         git update-index --no-assume-unchanged t/t &&
496         git checkout t/t
497 '
498
499 cat >expected <<EOF
500 hello.c=#include <stdio.h>
501 hello.c:        return 0;
502 EOF
503
504 test_expect_success 'grep -p with userdiff' '
505         git config diff.custom.funcname "^#" &&
506         echo "hello.c diff=custom" >.gitattributes &&
507         git grep -p return >actual &&
508         test_cmp expected actual
509 '
510
511 cat >expected <<EOF
512 hello.c=int main(int argc, const char **argv)
513 hello.c:        return 0;
514 EOF
515
516 test_expect_success 'grep -p' '
517         rm -f .gitattributes &&
518         git grep -p return >actual &&
519         test_cmp expected actual
520 '
521
522 cat >expected <<EOF
523 hello.c-#include <stdio.h>
524 hello.c=int main(int argc, const char **argv)
525 hello.c-{
526 hello.c-        printf("Hello world.\n");
527 hello.c:        return 0;
528 EOF
529
530 test_expect_success 'grep -p -B5' '
531         git grep -p -B5 return >actual &&
532         test_cmp expected actual
533 '
534
535 cat >expected <<EOF
536 hello.c=int main(int argc, const char **argv)
537 hello.c-{
538 hello.c-        printf("Hello world.\n");
539 hello.c:        return 0;
540 hello.c-        /* char ?? */
541 hello.c-}
542 EOF
543
544 test_expect_success 'grep -W' '
545         git grep -W return >actual &&
546         test_cmp expected actual
547 '
548
549 cat >expected <<EOF
550 hello.c=        printf("Hello world.\n");
551 hello.c:        return 0;
552 hello.c-        /* char ?? */
553 EOF
554
555 test_expect_success 'grep -W with userdiff' '
556         test_when_finished "rm -f .gitattributes" &&
557         git config diff.custom.xfuncname "(printf.*|})$" &&
558         echo "hello.c diff=custom" >.gitattributes &&
559         git grep -W return >actual &&
560         test_cmp expected actual
561 '
562
563 test_expect_success 'grep from a subdirectory to search wider area (1)' '
564         mkdir -p s &&
565         (
566                 cd s && git grep "x x x" ..
567         )
568 '
569
570 test_expect_success 'grep from a subdirectory to search wider area (2)' '
571         mkdir -p s &&
572         (
573                 cd s || exit 1
574                 ( git grep xxyyzz .. >out ; echo $? >status )
575                 ! test -s out &&
576                 test 1 = $(cat status)
577         )
578 '
579
580 cat >expected <<EOF
581 hello.c:int main(int argc, const char **argv)
582 EOF
583
584 test_expect_success 'grep -Fi' '
585         git grep -Fi "CHAR *" >actual &&
586         test_cmp expected actual
587 '
588
589 test_expect_success 'outside of git repository' '
590         rm -fr non &&
591         mkdir -p non/git/sub &&
592         echo hello >non/git/file1 &&
593         echo world >non/git/sub/file2 &&
594         {
595                 echo file1:hello &&
596                 echo sub/file2:world
597         } >non/expect.full &&
598         echo file2:world >non/expect.sub &&
599         (
600                 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
601                 export GIT_CEILING_DIRECTORIES &&
602                 cd non/git &&
603                 test_must_fail git grep o &&
604                 git grep --no-index o >../actual.full &&
605                 test_cmp ../expect.full ../actual.full
606                 cd sub &&
607                 test_must_fail git grep o &&
608                 git grep --no-index o >../../actual.sub &&
609                 test_cmp ../../expect.sub ../../actual.sub
610         ) &&
611
612         echo ".*o*" >non/git/.gitignore &&
613         (
614                 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
615                 export GIT_CEILING_DIRECTORIES &&
616                 cd non/git &&
617                 test_must_fail git grep o &&
618                 git grep --no-index --exclude-standard o >../actual.full &&
619                 test_cmp ../expect.full ../actual.full &&
620
621                 {
622                         echo ".gitignore:.*o*"
623                         cat ../expect.full
624                 } >../expect.with.ignored &&
625                 git grep --no-index --no-exclude o >../actual.full &&
626                 test_cmp ../expect.with.ignored ../actual.full
627         )
628 '
629
630 test_expect_success 'inside git repository but with --no-index' '
631         rm -fr is &&
632         mkdir -p is/git/sub &&
633         echo hello >is/git/file1 &&
634         echo world >is/git/sub/file2 &&
635         echo ".*o*" >is/git/.gitignore &&
636         {
637                 echo file1:hello &&
638                 echo sub/file2:world
639         } >is/expect.unignored &&
640         {
641                 echo ".gitignore:.*o*" &&
642                 cat is/expect.unignored
643         } >is/expect.full &&
644         : >is/expect.empty &&
645         echo file2:world >is/expect.sub &&
646         (
647                 cd is/git &&
648                 git init &&
649                 test_must_fail git grep o >../actual.full &&
650                 test_cmp ../expect.empty ../actual.full &&
651
652                 git grep --untracked o >../actual.unignored &&
653                 test_cmp ../expect.unignored ../actual.unignored &&
654
655                 git grep --no-index o >../actual.full &&
656                 test_cmp ../expect.full ../actual.full &&
657
658                 git grep --no-index --exclude-standard o >../actual.unignored &&
659                 test_cmp ../expect.unignored ../actual.unignored &&
660
661                 cd sub &&
662                 test_must_fail git grep o >../../actual.sub &&
663                 test_cmp ../../expect.empty ../../actual.sub &&
664
665                 git grep --no-index o >../../actual.sub &&
666                 test_cmp ../../expect.sub ../../actual.sub &&
667
668                 git grep --untracked o >../../actual.sub &&
669                 test_cmp ../../expect.sub ../../actual.sub
670         )
671 '
672
673 test_expect_success 'setup double-dash tests' '
674 cat >double-dash <<EOF &&
675 --
676 ->
677 other
678 EOF
679 git add double-dash
680 '
681
682 cat >expected <<EOF
683 double-dash:->
684 EOF
685 test_expect_success 'grep -- pattern' '
686         git grep -- "->" >actual &&
687         test_cmp expected actual
688 '
689 test_expect_success 'grep -- pattern -- pathspec' '
690         git grep -- "->" -- double-dash >actual &&
691         test_cmp expected actual
692 '
693 test_expect_success 'grep -e pattern -- path' '
694         git grep -e "->" -- double-dash >actual &&
695         test_cmp expected actual
696 '
697
698 cat >expected <<EOF
699 double-dash:--
700 EOF
701 test_expect_success 'grep -e -- -- path' '
702         git grep -e -- -- double-dash >actual &&
703         test_cmp expected actual
704 '
705
706 cat >expected <<EOF
707 hello.c:int main(int argc, const char **argv)
708 hello.c:        printf("Hello world.\n");
709 EOF
710
711 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
712         git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
713         test_cmp expected actual
714 '
715
716 test_expect_success LIBPCRE 'grep -P pattern' '
717         git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
718         test_cmp expected actual
719 '
720
721 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
722         >empty &&
723         test_must_fail git -c grep.extendedregexp=true \
724                 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
725         test_cmp empty actual
726 '
727
728 test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
729         git -c grep.extendedregexp=true \
730                 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
731         test_cmp expected actual
732 '
733
734 test_expect_success LIBPCRE 'grep -P -v pattern' '
735         {
736                 echo "ab:a+b*c"
737                 echo "ab:a+bc"
738         } >expected &&
739         git grep -P -v "abc" ab >actual &&
740         test_cmp expected actual
741 '
742
743 test_expect_success LIBPCRE 'grep -P -i pattern' '
744         cat >expected <<-EOF &&
745         hello.c:        printf("Hello world.\n");
746         EOF
747         git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
748         test_cmp expected actual
749 '
750
751 test_expect_success LIBPCRE 'grep -P -w pattern' '
752         {
753                 echo "hello_world:Hello world"
754                 echo "hello_world:HeLLo world"
755         } >expected &&
756         git grep -P -w "He((?i)ll)o" hello_world >actual &&
757         test_cmp expected actual
758 '
759
760 test_expect_success 'grep -G invalidpattern properly dies ' '
761         test_must_fail git grep -G "a["
762 '
763
764 test_expect_success 'grep -E invalidpattern properly dies ' '
765         test_must_fail git grep -E "a["
766 '
767
768 test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
769         test_must_fail git grep -P "a["
770 '
771
772 test_expect_success 'grep -G -E -F pattern' '
773         echo "ab:a+b*c" >expected &&
774         git grep -G -E -F "a+b*c" ab >actual &&
775         test_cmp expected actual
776 '
777
778 test_expect_success 'grep -E -F -G pattern' '
779         echo "ab:a+bc" >expected &&
780         git grep -E -F -G "a+b*c" ab >actual &&
781         test_cmp expected actual
782 '
783
784 test_expect_success 'grep -F -G -E pattern' '
785         echo "ab:abc" >expected &&
786         git grep -F -G -E "a+b*c" ab >actual &&
787         test_cmp expected actual
788 '
789
790 test_expect_success 'grep -G -F -P -E pattern' '
791         >empty &&
792         test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
793         test_cmp empty actual
794 '
795
796 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
797         echo "ab:a+b*c" >expected &&
798         git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
799         test_cmp expected actual
800 '
801
802 test_config() {
803         git config "$1" "$2" &&
804         test_when_finished "git config --unset $1"
805 }
806
807 cat >expected <<EOF
808 hello.c<RED>:<RESET>int main(int argc, const char **argv)
809 hello.c<RED>-<RESET>{
810 <RED>--<RESET>
811 hello.c<RED>:<RESET>    /* char ?? */
812 hello.c<RED>-<RESET>}
813 <RED>--<RESET>
814 hello_world<RED>:<RESET>Hello_world
815 hello_world<RED>-<RESET>HeLLo_world
816 EOF
817
818 test_expect_success 'grep --color, separator' '
819         test_config color.grep.context          normal &&
820         test_config color.grep.filename         normal &&
821         test_config color.grep.function         normal &&
822         test_config color.grep.linenumber       normal &&
823         test_config color.grep.match            normal &&
824         test_config color.grep.selected         normal &&
825         test_config color.grep.separator        red &&
826
827         git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
828         test_decode_color >actual &&
829         test_cmp expected actual
830 '
831
832 cat >expected <<EOF
833 hello.c:int main(int argc, const char **argv)
834 hello.c:        /* char ?? */
835
836 hello_world:Hello_world
837 EOF
838
839 test_expect_success 'grep --break' '
840         git grep --break -e char -e lo_w hello.c hello_world >actual &&
841         test_cmp expected actual
842 '
843
844 cat >expected <<EOF
845 hello.c:int main(int argc, const char **argv)
846 hello.c-{
847 --
848 hello.c:        /* char ?? */
849 hello.c-}
850
851 hello_world:Hello_world
852 hello_world-HeLLo_world
853 EOF
854
855 test_expect_success 'grep --break with context' '
856         git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
857         test_cmp expected actual
858 '
859
860 cat >expected <<EOF
861 hello.c
862 int main(int argc, const char **argv)
863         /* char ?? */
864 hello_world
865 Hello_world
866 EOF
867
868 test_expect_success 'grep --heading' '
869         git grep --heading -e char -e lo_w hello.c hello_world >actual &&
870         test_cmp expected actual
871 '
872
873 cat >expected <<EOF
874 <BOLD;GREEN>hello.c<RESET>
875 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
876 6:      /* <BLACK;BYELLOW>char<RESET> ?? */
877
878 <BOLD;GREEN>hello_world<RESET>
879 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
880 EOF
881
882 test_expect_success 'mimic ack-grep --group' '
883         test_config color.grep.context          normal &&
884         test_config color.grep.filename         "bold green" &&
885         test_config color.grep.function         normal &&
886         test_config color.grep.linenumber       normal &&
887         test_config color.grep.match            "black yellow" &&
888         test_config color.grep.selected         normal &&
889         test_config color.grep.separator        normal &&
890
891         git grep --break --heading -n --color \
892                 -e char -e lo_w hello.c hello_world |
893         test_decode_color >actual &&
894         test_cmp expected actual
895 '
896
897 cat >expected <<EOF
898 space: line with leading space1
899 space: line with leading space2
900 space: line with leading space3
901 EOF
902
903 test_expect_success LIBPCRE 'grep -E "^ "' '
904         git grep -E "^ " space >actual &&
905         test_cmp expected actual
906 '
907
908 test_expect_success LIBPCRE 'grep -P "^ "' '
909         git grep -P "^ " space >actual &&
910         test_cmp expected actual
911 '
912
913 test_done