Merge branch 'rd/doc-notes-prune-fix'
[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 <assert.h>
13 #include <stdio.h>
14
15 int main(int argc, const char **argv)
16 {
17         printf("Hello world.\n");
18         return 0;
19         /* char ?? */
20 }
21 EOF
22
23 test_expect_success setup '
24         {
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 &&
31         {
32                 echo Hello world
33                 echo HeLLo world
34                 echo Hello_world
35                 echo HeLLo_world
36         } >hello_world &&
37         {
38                 echo "a+b*c"
39                 echo "a+bc"
40                 echo "abc"
41         } >ab &&
42         {
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 &&
56         {
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         cat >hello.ps1 <<-\EOF &&
64         # No-op.
65         function dummy() {}
66
67         # Say hello.
68         function hello() {
69           echo "Hello world."
70         } # hello
71
72         # Still a no-op.
73         function dummy() {}
74         EOF
75         git add . &&
76         test_tick &&
77         git commit -m initial
78 '
79
80 test_expect_success 'grep should not segfault with a bad input' '
81         test_must_fail git grep "("
82 '
83
84 for H in HEAD ''
85 do
86         case "$H" in
87         HEAD)   HC='HEAD:' L='HEAD' ;;
88         '')     HC= L='in working tree' ;;
89         esac
90
91         test_expect_success "grep -w $L" '
92                 {
93                         echo ${HC}file:1:foo mmap bar
94                         echo ${HC}file:3:foo_mmap bar mmap
95                         echo ${HC}file:4:foo mmap bar_mmap
96                         echo ${HC}file:5:foo_mmap bar mmap baz
97                 } >expected &&
98                 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
99                 test_cmp expected actual
100         '
101
102         test_expect_success "grep -w $L" '
103                 {
104                         echo ${HC}file:1:foo mmap bar
105                         echo ${HC}file:3:foo_mmap bar mmap
106                         echo ${HC}file:4:foo mmap bar_mmap
107                         echo ${HC}file:5:foo_mmap bar mmap baz
108                 } >expected &&
109                 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
110                 test_cmp expected actual
111         '
112
113         test_expect_success "grep -w $L" '
114                 {
115                         echo ${HC}file:foo mmap bar
116                         echo ${HC}file:foo_mmap bar mmap
117                         echo ${HC}file:foo mmap bar_mmap
118                         echo ${HC}file:foo_mmap bar mmap baz
119                 } >expected &&
120                 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
121                 test_cmp expected actual
122         '
123
124         test_expect_success "grep -w $L (w)" '
125                 : >expected &&
126                 test_must_fail git grep -n -w -e "^w" $H >actual &&
127                 test_cmp expected actual
128         '
129
130         test_expect_success "grep -w $L (x)" '
131                 {
132                         echo ${HC}x:1:x x xx x
133                 } >expected &&
134                 git grep -n -w -e "x xx* x" $H >actual &&
135                 test_cmp expected actual
136         '
137
138         test_expect_success "grep -w $L (y-1)" '
139                 {
140                         echo ${HC}y:1:y yy
141                 } >expected &&
142                 git grep -n -w -e "^y" $H >actual &&
143                 test_cmp expected actual
144         '
145
146         test_expect_success "grep -w $L (y-2)" '
147                 : >expected &&
148                 if git grep -n -w -e "^y y" $H >actual
149                 then
150                         echo should not have matched
151                         cat actual
152                         false
153                 else
154                         test_cmp expected actual
155                 fi
156         '
157
158         test_expect_success "grep -w $L (z)" '
159                 : >expected &&
160                 if git grep -n -w -e "^z" $H >actual
161                 then
162                         echo should not have matched
163                         cat actual
164                         false
165                 else
166                         test_cmp expected actual
167                 fi
168         '
169
170         test_expect_success "grep $L (t-1)" '
171                 echo "${HC}t/t:1:test" >expected &&
172                 git grep -n -e test $H >actual &&
173                 test_cmp expected actual
174         '
175
176         test_expect_success "grep $L (t-2)" '
177                 echo "${HC}t:1:test" >expected &&
178                 (
179                         cd t &&
180                         git grep -n -e test $H
181                 ) >actual &&
182                 test_cmp expected actual
183         '
184
185         test_expect_success "grep $L (t-3)" '
186                 echo "${HC}t/t:1:test" >expected &&
187                 (
188                         cd t &&
189                         git grep --full-name -n -e test $H
190                 ) >actual &&
191                 test_cmp expected actual
192         '
193
194         test_expect_success "grep -c $L (no /dev/null)" '
195                 ! git grep -c test $H | grep /dev/null
196         '
197
198         test_expect_success "grep --max-depth -1 $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 -1 -n -e vvv $H >actual &&
205                 test_cmp expected actual
206         '
207
208         test_expect_success "grep --max-depth 0 $L" '
209                 {
210                         echo ${HC}v:1:vvv
211                 } >expected &&
212                 git grep --max-depth 0 -n -e vvv $H >actual &&
213                 test_cmp expected actual
214         '
215
216         test_expect_success "grep --max-depth 0 -- '*' $L" '
217                 {
218                         echo ${HC}t/a/v:1:vvv
219                         echo ${HC}t/v:1:vvv
220                         echo ${HC}v:1:vvv
221                 } >expected &&
222                 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
223                 test_cmp expected actual
224         '
225
226         test_expect_success "grep --max-depth 1 $L" '
227                 {
228                         echo ${HC}t/v:1:vvv
229                         echo ${HC}v:1:vvv
230                 } >expected &&
231                 git grep --max-depth 1 -n -e vvv $H >actual &&
232                 test_cmp expected actual
233         '
234
235         test_expect_success "grep --max-depth 0 -- t $L" '
236                 {
237                         echo ${HC}t/v:1:vvv
238                 } >expected &&
239                 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
240                 test_cmp expected actual
241         '
242
243         test_expect_success "grep --max-depth 0 -- . t $L" '
244                 {
245                         echo ${HC}t/v:1:vvv
246                         echo ${HC}v:1:vvv
247                 } >expected &&
248                 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
249                 test_cmp expected actual
250         '
251
252         test_expect_success "grep --max-depth 0 -- t . $L" '
253                 {
254                         echo ${HC}t/v:1:vvv
255                         echo ${HC}v:1:vvv
256                 } >expected &&
257                 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
258                 test_cmp expected actual
259         '
260         test_expect_success "grep $L with grep.extendedRegexp=false" '
261                 echo "${HC}ab:a+bc" >expected &&
262                 git -c grep.extendedRegexp=false grep "a+b*c" $H ab >actual &&
263                 test_cmp expected actual
264         '
265
266         test_expect_success "grep $L with grep.extendedRegexp=true" '
267                 echo "${HC}ab:abc" >expected &&
268                 git -c grep.extendedRegexp=true grep "a+b*c" $H ab >actual &&
269                 test_cmp expected actual
270         '
271
272         test_expect_success "grep $L with grep.patterntype=basic" '
273                 echo "${HC}ab:a+bc" >expected &&
274                 git -c grep.patterntype=basic grep "a+b*c" $H ab >actual &&
275                 test_cmp expected actual
276         '
277
278         test_expect_success "grep $L with grep.patterntype=extended" '
279                 echo "${HC}ab:abc" >expected &&
280                 git -c grep.patterntype=extended grep "a+b*c" $H ab >actual &&
281                 test_cmp expected actual
282         '
283
284         test_expect_success "grep $L with grep.patterntype=fixed" '
285                 echo "${HC}ab:a+b*c" >expected &&
286                 git -c grep.patterntype=fixed grep "a+b*c" $H ab >actual &&
287                 test_cmp expected actual
288         '
289
290         test_expect_success PCRE "grep $L with grep.patterntype=perl" '
291                 echo "${HC}ab:a+b*c" >expected &&
292                 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
293                 test_cmp expected actual
294         '
295
296         test_expect_success !PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
297                 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
298         '
299
300         test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
301                 echo "${HC}ab:abc" >expected &&
302                 git \
303                         -c grep.patternType=default \
304                         -c grep.extendedRegexp=true \
305                         grep "a+b*c" $H ab >actual &&
306                 test_cmp expected actual
307         '
308
309         test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
310                 echo "${HC}ab:abc" >expected &&
311                 git \
312                         -c grep.extendedRegexp=true \
313                         -c grep.patternType=default \
314                         grep "a+b*c" $H ab >actual &&
315                 test_cmp expected actual
316         '
317
318         test_expect_success "grep $L with grep.patternType=extended and grep.extendedRegexp=false" '
319                 echo "${HC}ab:abc" >expected &&
320                 git \
321                         -c grep.patternType=extended \
322                         -c grep.extendedRegexp=false \
323                         grep "a+b*c" $H ab >actual &&
324                 test_cmp expected actual
325         '
326
327         test_expect_success "grep $L with grep.patternType=basic and grep.extendedRegexp=true" '
328                 echo "${HC}ab:a+bc" >expected &&
329                 git \
330                         -c grep.patternType=basic \
331                         -c grep.extendedRegexp=true \
332                         grep "a+b*c" $H ab >actual &&
333                 test_cmp expected actual
334         '
335
336         test_expect_success "grep $L with grep.extendedRegexp=false and grep.patternType=extended" '
337                 echo "${HC}ab:abc" >expected &&
338                 git \
339                         -c grep.extendedRegexp=false \
340                         -c grep.patternType=extended \
341                         grep "a+b*c" $H ab >actual &&
342                 test_cmp expected actual
343         '
344
345         test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=basic" '
346                 echo "${HC}ab:a+bc" >expected &&
347                 git \
348                         -c grep.extendedRegexp=true \
349                         -c grep.patternType=basic \
350                         grep "a+b*c" $H ab >actual &&
351                 test_cmp expected actual
352         '
353
354         test_expect_success "grep --count $L" '
355                 echo ${HC}ab:3 >expected &&
356                 git grep --count -e b $H -- ab >actual &&
357                 test_cmp expected actual
358         '
359
360         test_expect_success "grep --count -h $L" '
361                 echo 3 >expected &&
362                 git grep --count -h -e b $H -- ab >actual &&
363                 test_cmp expected actual
364         '
365 done
366
367 cat >expected <<EOF
368 file
369 EOF
370 test_expect_success 'grep -l -C' '
371         git grep -l -C1 foo >actual &&
372         test_cmp expected actual
373 '
374
375 cat >expected <<EOF
376 file:5
377 EOF
378 test_expect_success 'grep -c -C' '
379         git grep -c -C1 foo >actual &&
380         test_cmp expected actual
381 '
382
383 test_expect_success 'grep -L -C' '
384         git ls-files >expected &&
385         git grep -L -C1 nonexistent_string >actual &&
386         test_cmp expected actual
387 '
388
389 test_expect_success 'grep --files-without-match --quiet' '
390         git grep --files-without-match --quiet nonexistent_string >actual &&
391         test_cmp /dev/null actual
392 '
393
394 cat >expected <<EOF
395 file:foo mmap bar_mmap
396 EOF
397
398 test_expect_success 'grep -e A --and -e B' '
399         git grep -e "foo mmap" --and -e bar_mmap >actual &&
400         test_cmp expected actual
401 '
402
403 cat >expected <<EOF
404 file:foo_mmap bar mmap
405 file:foo_mmap bar mmap baz
406 EOF
407
408
409 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
410         git grep \( -e foo_ --or -e baz \) \
411                 --and -e " mmap" >actual &&
412         test_cmp expected actual
413 '
414
415 cat >expected <<EOF
416 file:foo mmap bar
417 EOF
418
419 test_expect_success 'grep -e A --and --not -e B' '
420         git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
421         test_cmp expected actual
422 '
423
424 test_expect_success 'grep should ignore GREP_OPTIONS' '
425         GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
426         test_cmp expected actual
427 '
428
429 test_expect_success 'grep -f, non-existent file' '
430         test_must_fail git grep -f patterns
431 '
432
433 cat >expected <<EOF
434 file:foo mmap bar
435 file:foo_mmap bar
436 file:foo_mmap bar mmap
437 file:foo mmap bar_mmap
438 file:foo_mmap bar mmap baz
439 EOF
440
441 cat >pattern <<EOF
442 mmap
443 EOF
444
445 test_expect_success 'grep -f, one pattern' '
446         git grep -f pattern >actual &&
447         test_cmp expected actual
448 '
449
450 cat >expected <<EOF
451 file:foo mmap bar
452 file:foo_mmap bar
453 file:foo_mmap bar mmap
454 file:foo mmap bar_mmap
455 file:foo_mmap bar mmap baz
456 t/a/v:vvv
457 t/v:vvv
458 v:vvv
459 EOF
460
461 cat >patterns <<EOF
462 mmap
463 vvv
464 EOF
465
466 test_expect_success 'grep -f, multiple patterns' '
467         git grep -f patterns >actual &&
468         test_cmp expected actual
469 '
470
471 test_expect_success 'grep, multiple patterns' '
472         git grep "$(cat patterns)" >actual &&
473         test_cmp expected actual
474 '
475
476 cat >expected <<EOF
477 file:foo mmap bar
478 file:foo_mmap bar
479 file:foo_mmap bar mmap
480 file:foo mmap bar_mmap
481 file:foo_mmap bar mmap baz
482 t/a/v:vvv
483 t/v:vvv
484 v:vvv
485 EOF
486
487 cat >patterns <<EOF
488
489 mmap
490
491 vvv
492
493 EOF
494
495 test_expect_success 'grep -f, ignore empty lines' '
496         git grep -f patterns >actual &&
497         test_cmp expected actual
498 '
499
500 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
501         git grep -f - <patterns >actual &&
502         test_cmp expected actual
503 '
504
505 cat >expected <<EOF
506 y:y yy
507 --
508 z:zzz
509 EOF
510
511 test_expect_success 'grep -q, silently report matches' '
512         >empty &&
513         git grep -q mmap >actual &&
514         test_cmp empty actual &&
515         test_must_fail git grep -q qfwfq >actual &&
516         test_cmp empty actual
517 '
518
519 test_expect_success 'grep -C1 hunk mark between files' '
520         git grep -C1 "^[yz]" >actual &&
521         test_cmp expected actual
522 '
523
524 test_expect_success 'log grep setup' '
525         echo a >>file &&
526         test_tick &&
527         GIT_AUTHOR_NAME="With * Asterisk" \
528         GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
529         git commit -a -m "second" &&
530
531         echo a >>file &&
532         test_tick &&
533         git commit -a -m "third" &&
534
535         echo a >>file &&
536         test_tick &&
537         GIT_AUTHOR_NAME="Night Fall" \
538         GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
539         git commit -a -m "fourth"
540 '
541
542 test_expect_success 'log grep (1)' '
543         git log --author=author --pretty=tformat:%s >actual &&
544         {
545                 echo third && echo initial
546         } >expect &&
547         test_cmp expect actual
548 '
549
550 test_expect_success 'log grep (2)' '
551         git log --author=" * " -F --pretty=tformat:%s >actual &&
552         {
553                 echo second
554         } >expect &&
555         test_cmp expect actual
556 '
557
558 test_expect_success 'log grep (3)' '
559         git log --author="^A U" --pretty=tformat:%s >actual &&
560         {
561                 echo third && echo initial
562         } >expect &&
563         test_cmp expect actual
564 '
565
566 test_expect_success 'log grep (4)' '
567         git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
568         {
569                 echo second
570         } >expect &&
571         test_cmp expect actual
572 '
573
574 test_expect_success 'log grep (5)' '
575         git log --author=Thor -F --pretty=tformat:%s >actual &&
576         {
577                 echo third && echo initial
578         } >expect &&
579         test_cmp expect actual
580 '
581
582 test_expect_success 'log grep (6)' '
583         git log --author=-0700  --pretty=tformat:%s >actual &&
584         >expect &&
585         test_cmp expect actual
586 '
587
588 test_expect_success 'log grep (7)' '
589         git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
590         echo third >expect &&
591         test_cmp expect actual
592 '
593
594 test_expect_success 'log grep (8)' '
595         git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
596         {
597                 echo third && echo second
598         } >expect &&
599         test_cmp expect actual
600 '
601
602 test_expect_success 'log grep (9)' '
603         git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
604         echo third >expect &&
605         test_cmp expect actual
606 '
607
608 test_expect_success 'log grep (9)' '
609         git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
610         : >expect &&
611         test_cmp expect actual
612 '
613
614 test_expect_success 'log --grep-reflog can only be used under -g' '
615         test_must_fail git log --grep-reflog="commit: third"
616 '
617
618 test_expect_success 'log with multiple --grep uses union' '
619         git log --grep=i --grep=r --format=%s >actual &&
620         {
621                 echo fourth && echo third && echo initial
622         } >expect &&
623         test_cmp expect actual
624 '
625
626 test_expect_success 'log --all-match with multiple --grep uses intersection' '
627         git log --all-match --grep=i --grep=r --format=%s >actual &&
628         {
629                 echo third
630         } >expect &&
631         test_cmp expect actual
632 '
633
634 test_expect_success 'log with multiple --author uses union' '
635         git log --author="Thor" --author="Aster" --format=%s >actual &&
636         {
637             echo third && echo second && echo initial
638         } >expect &&
639         test_cmp expect actual
640 '
641
642 test_expect_success 'log --all-match with multiple --author still uses union' '
643         git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
644         {
645             echo third && echo second && echo initial
646         } >expect &&
647         test_cmp expect actual
648 '
649
650 test_expect_success 'log --grep --author uses intersection' '
651         # grep matches only third and fourth
652         # author matches only initial and third
653         git log --author="A U Thor" --grep=r --format=%s >actual &&
654         {
655                 echo third
656         } >expect &&
657         test_cmp expect actual
658 '
659
660 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
661         # grep matches initial and second but not third
662         # author matches only initial and third
663         git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
664         {
665                 echo initial
666         } >expect &&
667         test_cmp expect actual
668 '
669
670 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
671         # grep matches only initial and third
672         # author matches all but second
673         git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
674         {
675             echo third && echo initial
676         } >expect &&
677         test_cmp expect actual
678 '
679
680 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
681         # grep matches only initial and third
682         # author matches all but second
683         git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
684         {
685             echo third && echo initial
686         } >expect &&
687         test_cmp expect actual
688 '
689
690 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
691         # grep matches only third
692         # author matches only initial and third
693         git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
694         {
695                 echo third
696         } >expect &&
697         test_cmp expect actual
698 '
699
700 test_expect_success 'log --author does not search in timestamp' '
701         : >expect &&
702         git log --author="$GIT_AUTHOR_DATE" >actual &&
703         test_cmp expect actual
704 '
705
706 test_expect_success 'log --committer does not search in timestamp' '
707         : >expect &&
708         git log --committer="$GIT_COMMITTER_DATE" >actual &&
709         test_cmp expect actual
710 '
711
712 test_expect_success 'grep with CE_VALID file' '
713         git update-index --assume-unchanged t/t &&
714         rm t/t &&
715         test "$(git grep test)" = "t/t:test" &&
716         git update-index --no-assume-unchanged t/t &&
717         git checkout t/t
718 '
719
720 cat >expected <<EOF
721 hello.c=#include <stdio.h>
722 hello.c:        return 0;
723 EOF
724
725 test_expect_success 'grep -p with userdiff' '
726         git config diff.custom.funcname "^#" &&
727         echo "hello.c diff=custom" >.gitattributes &&
728         git grep -p return >actual &&
729         test_cmp expected actual
730 '
731
732 cat >expected <<EOF
733 hello.c=int main(int argc, const char **argv)
734 hello.c:        return 0;
735 EOF
736
737 test_expect_success 'grep -p' '
738         rm -f .gitattributes &&
739         git grep -p return >actual &&
740         test_cmp expected actual
741 '
742
743 cat >expected <<EOF
744 hello.c-#include <stdio.h>
745 hello.c-
746 hello.c=int main(int argc, const char **argv)
747 hello.c-{
748 hello.c-        printf("Hello world.\n");
749 hello.c:        return 0;
750 EOF
751
752 test_expect_success 'grep -p -B5' '
753         git grep -p -B5 return >actual &&
754         test_cmp expected actual
755 '
756
757 cat >expected <<EOF
758 hello.c=int main(int argc, const char **argv)
759 hello.c-{
760 hello.c-        printf("Hello world.\n");
761 hello.c:        return 0;
762 hello.c-        /* char ?? */
763 hello.c-}
764 EOF
765
766 test_expect_success 'grep -W' '
767         git grep -W return >actual &&
768         test_cmp expected actual
769 '
770
771 cat >expected <<EOF
772 hello.c-#include <assert.h>
773 hello.c:#include <stdio.h>
774 EOF
775
776 test_expect_success 'grep -W shows no trailing empty lines' '
777         git grep -W stdio >actual &&
778         test_cmp expected actual
779 '
780
781 test_expect_success 'grep -W with userdiff' '
782         test_when_finished "rm -f .gitattributes" &&
783         git config diff.custom.xfuncname "^function .*$" &&
784         echo "hello.ps1 diff=custom" >.gitattributes &&
785         git grep -W echo >function-context-userdiff-actual
786 '
787
788 test_expect_success ' includes preceding comment' '
789         grep "# Say hello" function-context-userdiff-actual
790 '
791
792 test_expect_success ' includes function line' '
793         grep "=function hello" function-context-userdiff-actual
794 '
795
796 test_expect_success ' includes matching line' '
797         grep ":  echo" function-context-userdiff-actual
798 '
799
800 test_expect_success ' includes last line of the function' '
801         grep "} # hello" function-context-userdiff-actual
802 '
803
804 for threads in $(test_seq 0 10)
805 do
806         test_expect_success "grep --threads=$threads & -c grep.threads=$threads" "
807                 git grep --threads=$threads . >actual.$threads &&
808                 if test $threads -ge 1
809                 then
810                         test_cmp actual.\$(($threads - 1)) actual.$threads
811                 fi &&
812                 git -c grep.threads=$threads grep . >actual.$threads &&
813                 if test $threads -ge 1
814                 then
815                         test_cmp actual.\$(($threads - 1)) actual.$threads
816                 fi
817         "
818 done
819
820 test_expect_success !PTHREADS,C_LOCALE_OUTPUT 'grep --threads=N or pack.threads=N warns when no pthreads' '
821         git grep --threads=2 Hello hello_world 2>err &&
822         grep ^warning: err >warnings &&
823         test_line_count = 1 warnings &&
824         grep -F "no threads support, ignoring --threads" err &&
825         git -c grep.threads=2 grep Hello hello_world 2>err &&
826         grep ^warning: err >warnings &&
827         test_line_count = 1 warnings &&
828         grep -F "no threads support, ignoring grep.threads" err &&
829         git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
830         grep ^warning: err >warnings &&
831         test_line_count = 2 warnings &&
832         grep -F "no threads support, ignoring --threads" err &&
833         grep -F "no threads support, ignoring grep.threads" err &&
834         git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
835         test_line_count = 0 err
836 '
837
838 test_expect_success 'grep from a subdirectory to search wider area (1)' '
839         mkdir -p s &&
840         (
841                 cd s && git grep "x x x" ..
842         )
843 '
844
845 test_expect_success 'grep from a subdirectory to search wider area (2)' '
846         mkdir -p s &&
847         (
848                 cd s || exit 1
849                 ( git grep xxyyzz .. >out ; echo $? >status )
850                 ! test -s out &&
851                 test 1 = $(cat status)
852         )
853 '
854
855 cat >expected <<EOF
856 hello.c:int main(int argc, const char **argv)
857 EOF
858
859 test_expect_success 'grep -Fi' '
860         git grep -Fi "CHAR *" >actual &&
861         test_cmp expected actual
862 '
863
864 test_expect_success 'outside of git repository' '
865         rm -fr non &&
866         mkdir -p non/git/sub &&
867         echo hello >non/git/file1 &&
868         echo world >non/git/sub/file2 &&
869         {
870                 echo file1:hello &&
871                 echo sub/file2:world
872         } >non/expect.full &&
873         echo file2:world >non/expect.sub &&
874         (
875                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
876                 export GIT_CEILING_DIRECTORIES &&
877                 cd non/git &&
878                 test_must_fail git grep o &&
879                 git grep --no-index o >../actual.full &&
880                 test_cmp ../expect.full ../actual.full &&
881                 cd sub &&
882                 test_must_fail git grep o &&
883                 git grep --no-index o >../../actual.sub &&
884                 test_cmp ../../expect.sub ../../actual.sub
885         ) &&
886
887         echo ".*o*" >non/git/.gitignore &&
888         (
889                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
890                 export GIT_CEILING_DIRECTORIES &&
891                 cd non/git &&
892                 test_must_fail git grep o &&
893                 git grep --no-index --exclude-standard o >../actual.full &&
894                 test_cmp ../expect.full ../actual.full &&
895
896                 {
897                         echo ".gitignore:.*o*" &&
898                         cat ../expect.full
899                 } >../expect.with.ignored &&
900                 git grep --no-index --no-exclude o >../actual.full &&
901                 test_cmp ../expect.with.ignored ../actual.full
902         )
903 '
904
905 test_expect_success 'outside of git repository with fallbackToNoIndex' '
906         rm -fr non &&
907         mkdir -p non/git/sub &&
908         echo hello >non/git/file1 &&
909         echo world >non/git/sub/file2 &&
910         cat <<-\EOF >non/expect.full &&
911         file1:hello
912         sub/file2:world
913         EOF
914         echo file2:world >non/expect.sub &&
915         (
916                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
917                 export GIT_CEILING_DIRECTORIES &&
918                 cd non/git &&
919                 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
920                 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
921                 test_cmp ../expect.full ../actual.full &&
922                 cd sub &&
923                 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
924                 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
925                 test_cmp ../../expect.sub ../../actual.sub
926         ) &&
927
928         echo ".*o*" >non/git/.gitignore &&
929         (
930                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
931                 export GIT_CEILING_DIRECTORIES &&
932                 cd non/git &&
933                 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
934                 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
935                 test_cmp ../expect.full ../actual.full &&
936
937                 {
938                         echo ".gitignore:.*o*" &&
939                         cat ../expect.full
940                 } >../expect.with.ignored &&
941                 git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
942                 test_cmp ../expect.with.ignored ../actual.full
943         )
944 '
945
946 test_expect_success 'inside git repository but with --no-index' '
947         rm -fr is &&
948         mkdir -p is/git/sub &&
949         echo hello >is/git/file1 &&
950         echo world >is/git/sub/file2 &&
951         echo ".*o*" >is/git/.gitignore &&
952         {
953                 echo file1:hello &&
954                 echo sub/file2:world
955         } >is/expect.unignored &&
956         {
957                 echo ".gitignore:.*o*" &&
958                 cat is/expect.unignored
959         } >is/expect.full &&
960         : >is/expect.empty &&
961         echo file2:world >is/expect.sub &&
962         (
963                 cd is/git &&
964                 git init &&
965                 test_must_fail git grep o >../actual.full &&
966                 test_cmp ../expect.empty ../actual.full &&
967
968                 git grep --untracked o >../actual.unignored &&
969                 test_cmp ../expect.unignored ../actual.unignored &&
970
971                 git grep --no-index o >../actual.full &&
972                 test_cmp ../expect.full ../actual.full &&
973
974                 git grep --no-index --exclude-standard o >../actual.unignored &&
975                 test_cmp ../expect.unignored ../actual.unignored &&
976
977                 cd sub &&
978                 test_must_fail git grep o >../../actual.sub &&
979                 test_cmp ../../expect.empty ../../actual.sub &&
980
981                 git grep --no-index o >../../actual.sub &&
982                 test_cmp ../../expect.sub ../../actual.sub &&
983
984                 git grep --untracked o >../../actual.sub &&
985                 test_cmp ../../expect.sub ../../actual.sub
986         )
987 '
988
989 test_expect_success 'grep --no-index descends into repos, but not .git' '
990         rm -fr non &&
991         mkdir -p non/git &&
992         (
993                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
994                 export GIT_CEILING_DIRECTORIES &&
995                 cd non/git &&
996
997                 echo magic >file &&
998                 git init repo &&
999                 (
1000                         cd repo &&
1001                         echo magic >file &&
1002                         git add file &&
1003                         git commit -m foo &&
1004                         echo magic >.git/file
1005                 ) &&
1006
1007                 cat >expect <<-\EOF &&
1008                 file
1009                 repo/file
1010                 EOF
1011                 git grep -l --no-index magic >actual &&
1012                 test_cmp expect actual
1013         )
1014 '
1015
1016 test_expect_success 'setup double-dash tests' '
1017 cat >double-dash <<EOF &&
1018 --
1019 ->
1020 other
1021 EOF
1022 git add double-dash
1023 '
1024
1025 cat >expected <<EOF
1026 double-dash:->
1027 EOF
1028 test_expect_success 'grep -- pattern' '
1029         git grep -- "->" >actual &&
1030         test_cmp expected actual
1031 '
1032 test_expect_success 'grep -- pattern -- pathspec' '
1033         git grep -- "->" -- double-dash >actual &&
1034         test_cmp expected actual
1035 '
1036 test_expect_success 'grep -e pattern -- path' '
1037         git grep -e "->" -- double-dash >actual &&
1038         test_cmp expected actual
1039 '
1040
1041 cat >expected <<EOF
1042 double-dash:--
1043 EOF
1044 test_expect_success 'grep -e -- -- path' '
1045         git grep -e -- -- double-dash >actual &&
1046         test_cmp expected actual
1047 '
1048
1049 test_expect_success 'dashdash disambiguates rev as rev' '
1050         test_when_finished "rm -f master" &&
1051         echo content >master &&
1052         echo master:hello.c >expect &&
1053         git grep -l o master -- hello.c >actual &&
1054         test_cmp expect actual
1055 '
1056
1057 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
1058         test_when_finished "git rm -f master" &&
1059         echo content >master &&
1060         git add master &&
1061         echo master:content >expect &&
1062         git grep o -- master >actual &&
1063         test_cmp expect actual
1064 '
1065
1066 test_expect_success 'report bogus arg without dashdash' '
1067         test_must_fail git grep o does-not-exist
1068 '
1069
1070 test_expect_success 'report bogus rev with dashdash' '
1071         test_must_fail git grep o hello.c --
1072 '
1073
1074 test_expect_success 'allow non-existent path with dashdash' '
1075         # We need a real match so grep exits with success.
1076         tree=$(git ls-tree HEAD |
1077                sed s/hello.c/not-in-working-tree/ |
1078                git mktree) &&
1079         git grep o "$tree" -- not-in-working-tree
1080 '
1081
1082 test_expect_success 'grep --no-index pattern -- path' '
1083         rm -fr non &&
1084         mkdir -p non/git &&
1085         (
1086                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1087                 export GIT_CEILING_DIRECTORIES &&
1088                 cd non/git &&
1089                 echo hello >hello &&
1090                 echo goodbye >goodbye &&
1091                 echo hello:hello >expect &&
1092                 git grep --no-index o -- hello >actual &&
1093                 test_cmp expect actual
1094         )
1095 '
1096
1097 test_expect_success 'grep --no-index complains of revs' '
1098         test_must_fail git grep --no-index o master -- 2>err &&
1099         test_i18ngrep "cannot be used with revs" err
1100 '
1101
1102 test_expect_success 'grep --no-index prefers paths to revs' '
1103         test_when_finished "rm -f master" &&
1104         echo content >master &&
1105         echo master:content >expect &&
1106         git grep --no-index o master >actual &&
1107         test_cmp expect actual
1108 '
1109
1110 test_expect_success 'grep --no-index does not "diagnose" revs' '
1111         test_must_fail git grep --no-index o :1:hello.c 2>err &&
1112         test_i18ngrep ! -i "did you mean" err
1113 '
1114
1115 cat >expected <<EOF
1116 hello.c:int main(int argc, const char **argv)
1117 hello.c:        printf("Hello world.\n");
1118 EOF
1119
1120 test_expect_success PCRE 'grep --perl-regexp pattern' '
1121         git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1122         test_cmp expected actual
1123 '
1124
1125 test_expect_success !PCRE 'grep --perl-regexp pattern errors without PCRE' '
1126         test_must_fail git grep --perl-regexp "foo.*bar"
1127 '
1128
1129 test_expect_success PCRE 'grep -P pattern' '
1130         git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1131         test_cmp expected actual
1132 '
1133
1134 test_expect_success !PCRE 'grep -P pattern errors without PCRE' '
1135         test_must_fail git grep -P "foo.*bar"
1136 '
1137
1138 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1139         >empty &&
1140         test_must_fail git -c grep.extendedregexp=true \
1141                 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1142         test_cmp empty actual
1143 '
1144
1145 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1146         git -c grep.extendedregexp=true \
1147                 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1148         test_cmp expected actual
1149 '
1150
1151 test_expect_success PCRE 'grep -P -v pattern' '
1152         {
1153                 echo "ab:a+b*c"
1154                 echo "ab:a+bc"
1155         } >expected &&
1156         git grep -P -v "abc" ab >actual &&
1157         test_cmp expected actual
1158 '
1159
1160 test_expect_success PCRE 'grep -P -i pattern' '
1161         cat >expected <<-EOF &&
1162         hello.c:        printf("Hello world.\n");
1163         EOF
1164         git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1165         test_cmp expected actual
1166 '
1167
1168 test_expect_success PCRE 'grep -P -w pattern' '
1169         {
1170                 echo "hello_world:Hello world"
1171                 echo "hello_world:HeLLo world"
1172         } >expected &&
1173         git grep -P -w "He((?i)ll)o" hello_world >actual &&
1174         test_cmp expected actual
1175 '
1176
1177 test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1178         git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1179         test_cmp hello_world actual &&
1180         git grep -P -h "(.)\1" hello_world >actual &&
1181         test_cmp hello_world actual
1182 '
1183
1184 test_expect_success 'grep -G invalidpattern properly dies ' '
1185         test_must_fail git grep -G "a["
1186 '
1187
1188 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1189         test_must_fail git -c grep.patterntype=basic grep "a["
1190 '
1191
1192 test_expect_success 'grep -E invalidpattern properly dies ' '
1193         test_must_fail git grep -E "a["
1194 '
1195
1196 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1197         test_must_fail git -c grep.patterntype=extended grep "a["
1198 '
1199
1200 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1201         test_must_fail git grep -P "a["
1202 '
1203
1204 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1205         test_must_fail git -c grep.patterntype=perl grep "a["
1206 '
1207
1208 test_expect_success 'grep -G -E -F pattern' '
1209         echo "ab:a+b*c" >expected &&
1210         git grep -G -E -F "a+b*c" ab >actual &&
1211         test_cmp expected actual
1212 '
1213
1214 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1215         echo "ab:a+b*c" >expected &&
1216         git \
1217                 -c grep.patterntype=basic \
1218                 -c grep.patterntype=extended \
1219                 -c grep.patterntype=fixed \
1220                 grep "a+b*c" ab >actual &&
1221         test_cmp expected actual
1222 '
1223
1224 test_expect_success 'grep -E -F -G pattern' '
1225         echo "ab:a+bc" >expected &&
1226         git grep -E -F -G "a+b*c" ab >actual &&
1227         test_cmp expected actual
1228 '
1229
1230 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1231         echo "ab:a+bc" >expected &&
1232         git \
1233                 -c grep.patterntype=extended \
1234                 -c grep.patterntype=fixed \
1235                 -c grep.patterntype=basic \
1236                 grep "a+b*c" ab >actual &&
1237         test_cmp expected actual
1238 '
1239
1240 test_expect_success 'grep -F -G -E pattern' '
1241         echo "ab:abc" >expected &&
1242         git grep -F -G -E "a+b*c" ab >actual &&
1243         test_cmp expected actual
1244 '
1245
1246 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1247         echo "ab:abc" >expected &&
1248         git \
1249                 -c grep.patterntype=fixed \
1250                 -c grep.patterntype=basic \
1251                 -c grep.patterntype=extended \
1252                 grep "a+b*c" ab >actual &&
1253         test_cmp expected actual
1254 '
1255
1256 test_expect_success 'grep -G -F -P -E pattern' '
1257         echo "d0:d" >expected &&
1258         git grep -G -F -P -E "[\d]" d0 >actual &&
1259         test_cmp expected actual
1260 '
1261
1262 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1263         echo "d0:d" >expected &&
1264         git \
1265                 -c grep.patterntype=fixed \
1266                 -c grep.patterntype=basic \
1267                 -c grep.patterntype=perl \
1268                 -c grep.patterntype=extended \
1269                 grep "[\d]" d0 >actual &&
1270         test_cmp expected actual
1271 '
1272
1273 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1274         echo "d0:0" >expected &&
1275         git grep -G -F -E -P "[\d]" d0 >actual &&
1276         test_cmp expected actual
1277 '
1278
1279 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1280         echo "d0:0" >expected &&
1281         git \
1282                 -c grep.patterntype=fixed \
1283                 -c grep.patterntype=basic \
1284                 -c grep.patterntype=extended \
1285                 -c grep.patterntype=perl \
1286                 grep "[\d]" d0 >actual &&
1287         test_cmp expected actual
1288 '
1289
1290 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1291         echo "ab:a+b*c" >expected &&
1292         git \
1293                 -c grep.patterntype=fixed \
1294                 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1295         test_cmp expected actual
1296 '
1297
1298 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1299         echo "ab:a+b*c" >expected &&
1300         git \
1301                 -c grep.patterntype=basic \
1302                 grep -F "*c" ab >actual &&
1303         test_cmp expected actual
1304 '
1305
1306 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1307         {
1308                 echo "ab:a+b*c"
1309                 echo "ab:a+bc"
1310         } >expected &&
1311         git \
1312                 -c grep.patterntype=fixed \
1313                 grep -G "a+b" ab >actual &&
1314         test_cmp expected actual
1315 '
1316
1317 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1318         {
1319                 echo "ab:a+b*c"
1320                 echo "ab:a+bc"
1321                 echo "ab:abc"
1322         } >expected &&
1323         git \
1324                 -c grep.patterntype=fixed \
1325                 grep -E "a+" ab >actual &&
1326         test_cmp expected actual
1327 '
1328
1329 cat >expected <<EOF
1330 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1331 hello.c<RED>-<RESET>{
1332 <RED>--<RESET>
1333 hello.c<RED>:<RESET>    /* char ?? */
1334 hello.c<RED>-<RESET>}
1335 <RED>--<RESET>
1336 hello_world<RED>:<RESET>Hello_world
1337 hello_world<RED>-<RESET>HeLLo_world
1338 EOF
1339
1340 test_expect_success 'grep --color, separator' '
1341         test_config color.grep.context          normal &&
1342         test_config color.grep.filename         normal &&
1343         test_config color.grep.function         normal &&
1344         test_config color.grep.linenumber       normal &&
1345         test_config color.grep.match            normal &&
1346         test_config color.grep.selected         normal &&
1347         test_config color.grep.separator        red &&
1348
1349         git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1350         test_decode_color >actual &&
1351         test_cmp expected actual
1352 '
1353
1354 cat >expected <<EOF
1355 hello.c:int main(int argc, const char **argv)
1356 hello.c:        /* char ?? */
1357
1358 hello_world:Hello_world
1359 EOF
1360
1361 test_expect_success 'grep --break' '
1362         git grep --break -e char -e lo_w hello.c hello_world >actual &&
1363         test_cmp expected actual
1364 '
1365
1366 cat >expected <<EOF
1367 hello.c:int main(int argc, const char **argv)
1368 hello.c-{
1369 --
1370 hello.c:        /* char ?? */
1371 hello.c-}
1372
1373 hello_world:Hello_world
1374 hello_world-HeLLo_world
1375 EOF
1376
1377 test_expect_success 'grep --break with context' '
1378         git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1379         test_cmp expected actual
1380 '
1381
1382 cat >expected <<EOF
1383 hello.c
1384 int main(int argc, const char **argv)
1385         /* char ?? */
1386 hello_world
1387 Hello_world
1388 EOF
1389
1390 test_expect_success 'grep --heading' '
1391         git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1392         test_cmp expected actual
1393 '
1394
1395 cat >expected <<EOF
1396 <BOLD;GREEN>hello.c<RESET>
1397 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1398 8:      /* <BLACK;BYELLOW>char<RESET> ?? */
1399
1400 <BOLD;GREEN>hello_world<RESET>
1401 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1402 EOF
1403
1404 test_expect_success 'mimic ack-grep --group' '
1405         test_config color.grep.context          normal &&
1406         test_config color.grep.filename         "bold green" &&
1407         test_config color.grep.function         normal &&
1408         test_config color.grep.linenumber       normal &&
1409         test_config color.grep.match            "black yellow" &&
1410         test_config color.grep.selected         normal &&
1411         test_config color.grep.separator        normal &&
1412
1413         git grep --break --heading -n --color \
1414                 -e char -e lo_w hello.c hello_world |
1415         test_decode_color >actual &&
1416         test_cmp expected actual
1417 '
1418
1419 cat >expected <<EOF
1420 space: line with leading space1
1421 space: line with leading space2
1422 space: line with leading space3
1423 EOF
1424
1425 test_expect_success PCRE 'grep -E "^ "' '
1426         git grep -E "^ " space >actual &&
1427         test_cmp expected actual
1428 '
1429
1430 test_expect_success PCRE 'grep -P "^ "' '
1431         git grep -P "^ " space >actual &&
1432         test_cmp expected actual
1433 '
1434
1435 cat >expected <<EOF
1436 space-line without leading space1
1437 space: line <RED>with <RESET>leading space1
1438 space: line <RED>with <RESET>leading <RED>space2<RESET>
1439 space: line <RED>with <RESET>leading space3
1440 space:line without leading <RED>space2<RESET>
1441 EOF
1442
1443 test_expect_success 'grep --color -e A -e B with context' '
1444         test_config color.grep.context          normal &&
1445         test_config color.grep.filename         normal &&
1446         test_config color.grep.function         normal &&
1447         test_config color.grep.linenumber       normal &&
1448         test_config color.grep.matchContext     normal &&
1449         test_config color.grep.matchSelected    red &&
1450         test_config color.grep.selected         normal &&
1451         test_config color.grep.separator        normal &&
1452
1453         git grep --color=always -C2 -e "with " -e space2  space |
1454         test_decode_color >actual &&
1455         test_cmp expected actual
1456 '
1457
1458 cat >expected <<EOF
1459 space-line without leading space1
1460 space- line with leading space1
1461 space: line <RED>with <RESET>leading <RED>space2<RESET>
1462 space- line with leading space3
1463 space-line without leading space2
1464 EOF
1465
1466 test_expect_success 'grep --color -e A --and -e B with context' '
1467         test_config color.grep.context          normal &&
1468         test_config color.grep.filename         normal &&
1469         test_config color.grep.function         normal &&
1470         test_config color.grep.linenumber       normal &&
1471         test_config color.grep.matchContext     normal &&
1472         test_config color.grep.matchSelected    red &&
1473         test_config color.grep.selected         normal &&
1474         test_config color.grep.separator        normal &&
1475
1476         git grep --color=always -C2 -e "with " --and -e space2  space |
1477         test_decode_color >actual &&
1478         test_cmp expected actual
1479 '
1480
1481 cat >expected <<EOF
1482 space-line without leading space1
1483 space: line <RED>with <RESET>leading space1
1484 space- line with leading space2
1485 space: line <RED>with <RESET>leading space3
1486 space-line without leading space2
1487 EOF
1488
1489 test_expect_success 'grep --color -e A --and --not -e B with context' '
1490         test_config color.grep.context          normal &&
1491         test_config color.grep.filename         normal &&
1492         test_config color.grep.function         normal &&
1493         test_config color.grep.linenumber       normal &&
1494         test_config color.grep.matchContext     normal &&
1495         test_config color.grep.matchSelected    red &&
1496         test_config color.grep.selected         normal &&
1497         test_config color.grep.separator        normal &&
1498
1499         git grep --color=always -C2 -e "with " --and --not -e space2  space |
1500         test_decode_color >actual &&
1501         test_cmp expected actual
1502 '
1503
1504 cat >expected <<EOF
1505 hello.c-
1506 hello.c=int main(int argc, const char **argv)
1507 hello.c-{
1508 hello.c:        pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1509 hello.c-        return 0;
1510 hello.c-        /* char ?? */
1511 hello.c-}
1512 EOF
1513
1514 test_expect_success 'grep --color -e A --and -e B -p with context' '
1515         test_config color.grep.context          normal &&
1516         test_config color.grep.filename         normal &&
1517         test_config color.grep.function         normal &&
1518         test_config color.grep.linenumber       normal &&
1519         test_config color.grep.matchContext     normal &&
1520         test_config color.grep.matchSelected    red &&
1521         test_config color.grep.selected         normal &&
1522         test_config color.grep.separator        normal &&
1523
1524         git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1525         test_decode_color >actual &&
1526         test_cmp expected actual
1527 '
1528
1529 test_expect_success 'grep can find things only in the work tree' '
1530         : >work-tree-only &&
1531         git add work-tree-only &&
1532         test_when_finished "git rm -f work-tree-only" &&
1533         echo "find in work tree" >work-tree-only &&
1534         git grep --quiet "find in work tree" &&
1535         test_must_fail git grep --quiet --cached "find in work tree" &&
1536         test_must_fail git grep --quiet "find in work tree" HEAD
1537 '
1538
1539 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1540         echo "intend to add this" >intend-to-add &&
1541         git add -N intend-to-add &&
1542         test_when_finished "git rm -f intend-to-add" &&
1543         git grep --quiet "intend to add this" &&
1544         test_must_fail git grep --quiet --cached "intend to add this" &&
1545         test_must_fail git grep --quiet "intend to add this" HEAD
1546 '
1547
1548 test_expect_success 'grep does not search work tree with assume unchanged' '
1549         echo "intend to add this" >intend-to-add &&
1550         git add -N intend-to-add &&
1551         git update-index --assume-unchanged intend-to-add &&
1552         test_when_finished "git rm -f intend-to-add" &&
1553         test_must_fail git grep --quiet "intend to add this" &&
1554         test_must_fail git grep --quiet --cached "intend to add this" &&
1555         test_must_fail git grep --quiet "intend to add this" HEAD
1556 '
1557
1558 test_expect_success 'grep can find things only in the index' '
1559         echo "only in the index" >cache-this &&
1560         git add cache-this &&
1561         rm cache-this &&
1562         test_when_finished "git rm --cached cache-this" &&
1563         test_must_fail git grep --quiet "only in the index" &&
1564         git grep --quiet --cached "only in the index" &&
1565         test_must_fail git grep --quiet "only in the index" HEAD
1566 '
1567
1568 test_expect_success 'grep does not report i-t-a with -L --cached' '
1569         echo "intend to add this" >intend-to-add &&
1570         git add -N intend-to-add &&
1571         test_when_finished "git rm -f intend-to-add" &&
1572         git ls-files | grep -v "^intend-to-add\$" >expected &&
1573         git grep -L --cached "nonexistent_string" >actual &&
1574         test_cmp expected actual
1575 '
1576
1577 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1578         echo "intend to add this" >intend-to-add-assume-unchanged &&
1579         git add -N intend-to-add-assume-unchanged &&
1580         test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1581         git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1582         git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1583         git grep -L "nonexistent_string" >actual &&
1584         test_cmp expected actual
1585 '
1586
1587 test_done