chainlint: recognize multi-line $(...) when command cuddled with "$("
[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 &&
849                 test_expect_code 1 git grep xxyyzz .. >out &&
850                 ! test -s out
851         )
852 '
853
854 cat >expected <<EOF
855 hello.c:int main(int argc, const char **argv)
856 EOF
857
858 test_expect_success 'grep -Fi' '
859         git grep -Fi "CHAR *" >actual &&
860         test_cmp expected actual
861 '
862
863 test_expect_success 'outside of git repository' '
864         rm -fr non &&
865         mkdir -p non/git/sub &&
866         echo hello >non/git/file1 &&
867         echo world >non/git/sub/file2 &&
868         {
869                 echo file1:hello &&
870                 echo sub/file2:world
871         } >non/expect.full &&
872         echo file2:world >non/expect.sub &&
873         (
874                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
875                 export GIT_CEILING_DIRECTORIES &&
876                 cd non/git &&
877                 test_must_fail git grep o &&
878                 git grep --no-index o >../actual.full &&
879                 test_cmp ../expect.full ../actual.full &&
880                 cd sub &&
881                 test_must_fail git grep o &&
882                 git grep --no-index o >../../actual.sub &&
883                 test_cmp ../../expect.sub ../../actual.sub
884         ) &&
885
886         echo ".*o*" >non/git/.gitignore &&
887         (
888                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
889                 export GIT_CEILING_DIRECTORIES &&
890                 cd non/git &&
891                 test_must_fail git grep o &&
892                 git grep --no-index --exclude-standard o >../actual.full &&
893                 test_cmp ../expect.full ../actual.full &&
894
895                 {
896                         echo ".gitignore:.*o*" &&
897                         cat ../expect.full
898                 } >../expect.with.ignored &&
899                 git grep --no-index --no-exclude o >../actual.full &&
900                 test_cmp ../expect.with.ignored ../actual.full
901         )
902 '
903
904 test_expect_success 'outside of git repository with fallbackToNoIndex' '
905         rm -fr non &&
906         mkdir -p non/git/sub &&
907         echo hello >non/git/file1 &&
908         echo world >non/git/sub/file2 &&
909         cat <<-\EOF >non/expect.full &&
910         file1:hello
911         sub/file2:world
912         EOF
913         echo file2:world >non/expect.sub &&
914         (
915                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
916                 export GIT_CEILING_DIRECTORIES &&
917                 cd non/git &&
918                 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
919                 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
920                 test_cmp ../expect.full ../actual.full &&
921                 cd sub &&
922                 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
923                 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
924                 test_cmp ../../expect.sub ../../actual.sub
925         ) &&
926
927         echo ".*o*" >non/git/.gitignore &&
928         (
929                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
930                 export GIT_CEILING_DIRECTORIES &&
931                 cd non/git &&
932                 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
933                 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
934                 test_cmp ../expect.full ../actual.full &&
935
936                 {
937                         echo ".gitignore:.*o*" &&
938                         cat ../expect.full
939                 } >../expect.with.ignored &&
940                 git -c grep.fallbackToNoIndex grep --no-exclude o >../actual.full &&
941                 test_cmp ../expect.with.ignored ../actual.full
942         )
943 '
944
945 test_expect_success 'inside git repository but with --no-index' '
946         rm -fr is &&
947         mkdir -p is/git/sub &&
948         echo hello >is/git/file1 &&
949         echo world >is/git/sub/file2 &&
950         echo ".*o*" >is/git/.gitignore &&
951         {
952                 echo file1:hello &&
953                 echo sub/file2:world
954         } >is/expect.unignored &&
955         {
956                 echo ".gitignore:.*o*" &&
957                 cat is/expect.unignored
958         } >is/expect.full &&
959         : >is/expect.empty &&
960         echo file2:world >is/expect.sub &&
961         (
962                 cd is/git &&
963                 git init &&
964                 test_must_fail git grep o >../actual.full &&
965                 test_cmp ../expect.empty ../actual.full &&
966
967                 git grep --untracked o >../actual.unignored &&
968                 test_cmp ../expect.unignored ../actual.unignored &&
969
970                 git grep --no-index o >../actual.full &&
971                 test_cmp ../expect.full ../actual.full &&
972
973                 git grep --no-index --exclude-standard o >../actual.unignored &&
974                 test_cmp ../expect.unignored ../actual.unignored &&
975
976                 cd sub &&
977                 test_must_fail git grep o >../../actual.sub &&
978                 test_cmp ../../expect.empty ../../actual.sub &&
979
980                 git grep --no-index o >../../actual.sub &&
981                 test_cmp ../../expect.sub ../../actual.sub &&
982
983                 git grep --untracked o >../../actual.sub &&
984                 test_cmp ../../expect.sub ../../actual.sub
985         )
986 '
987
988 test_expect_success 'grep --no-index descends into repos, but not .git' '
989         rm -fr non &&
990         mkdir -p non/git &&
991         (
992                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
993                 export GIT_CEILING_DIRECTORIES &&
994                 cd non/git &&
995
996                 echo magic >file &&
997                 git init repo &&
998                 (
999                         cd repo &&
1000                         echo magic >file &&
1001                         git add file &&
1002                         git commit -m foo &&
1003                         echo magic >.git/file
1004                 ) &&
1005
1006                 cat >expect <<-\EOF &&
1007                 file
1008                 repo/file
1009                 EOF
1010                 git grep -l --no-index magic >actual &&
1011                 test_cmp expect actual
1012         )
1013 '
1014
1015 test_expect_success 'setup double-dash tests' '
1016 cat >double-dash <<EOF &&
1017 --
1018 ->
1019 other
1020 EOF
1021 git add double-dash
1022 '
1023
1024 cat >expected <<EOF
1025 double-dash:->
1026 EOF
1027 test_expect_success 'grep -- pattern' '
1028         git grep -- "->" >actual &&
1029         test_cmp expected actual
1030 '
1031 test_expect_success 'grep -- pattern -- pathspec' '
1032         git grep -- "->" -- double-dash >actual &&
1033         test_cmp expected actual
1034 '
1035 test_expect_success 'grep -e pattern -- path' '
1036         git grep -e "->" -- double-dash >actual &&
1037         test_cmp expected actual
1038 '
1039
1040 cat >expected <<EOF
1041 double-dash:--
1042 EOF
1043 test_expect_success 'grep -e -- -- path' '
1044         git grep -e -- -- double-dash >actual &&
1045         test_cmp expected actual
1046 '
1047
1048 test_expect_success 'dashdash disambiguates rev as rev' '
1049         test_when_finished "rm -f master" &&
1050         echo content >master &&
1051         echo master:hello.c >expect &&
1052         git grep -l o master -- hello.c >actual &&
1053         test_cmp expect actual
1054 '
1055
1056 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
1057         test_when_finished "git rm -f master" &&
1058         echo content >master &&
1059         git add master &&
1060         echo master:content >expect &&
1061         git grep o -- master >actual &&
1062         test_cmp expect actual
1063 '
1064
1065 test_expect_success 'report bogus arg without dashdash' '
1066         test_must_fail git grep o does-not-exist
1067 '
1068
1069 test_expect_success 'report bogus rev with dashdash' '
1070         test_must_fail git grep o hello.c --
1071 '
1072
1073 test_expect_success 'allow non-existent path with dashdash' '
1074         # We need a real match so grep exits with success.
1075         tree=$(git ls-tree HEAD |
1076                sed s/hello.c/not-in-working-tree/ |
1077                git mktree) &&
1078         git grep o "$tree" -- not-in-working-tree
1079 '
1080
1081 test_expect_success 'grep --no-index pattern -- path' '
1082         rm -fr non &&
1083         mkdir -p non/git &&
1084         (
1085                 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1086                 export GIT_CEILING_DIRECTORIES &&
1087                 cd non/git &&
1088                 echo hello >hello &&
1089                 echo goodbye >goodbye &&
1090                 echo hello:hello >expect &&
1091                 git grep --no-index o -- hello >actual &&
1092                 test_cmp expect actual
1093         )
1094 '
1095
1096 test_expect_success 'grep --no-index complains of revs' '
1097         test_must_fail git grep --no-index o master -- 2>err &&
1098         test_i18ngrep "cannot be used with revs" err
1099 '
1100
1101 test_expect_success 'grep --no-index prefers paths to revs' '
1102         test_when_finished "rm -f master" &&
1103         echo content >master &&
1104         echo master:content >expect &&
1105         git grep --no-index o master >actual &&
1106         test_cmp expect actual
1107 '
1108
1109 test_expect_success 'grep --no-index does not "diagnose" revs' '
1110         test_must_fail git grep --no-index o :1:hello.c 2>err &&
1111         test_i18ngrep ! -i "did you mean" err
1112 '
1113
1114 cat >expected <<EOF
1115 hello.c:int main(int argc, const char **argv)
1116 hello.c:        printf("Hello world.\n");
1117 EOF
1118
1119 test_expect_success PCRE 'grep --perl-regexp pattern' '
1120         git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1121         test_cmp expected actual
1122 '
1123
1124 test_expect_success !PCRE 'grep --perl-regexp pattern errors without PCRE' '
1125         test_must_fail git grep --perl-regexp "foo.*bar"
1126 '
1127
1128 test_expect_success PCRE 'grep -P pattern' '
1129         git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1130         test_cmp expected actual
1131 '
1132
1133 test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
1134         git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1135         test_cmp expected actual
1136
1137 '
1138
1139 test_expect_success !PCRE 'grep -P pattern errors without PCRE' '
1140         test_must_fail git grep -P "foo.*bar"
1141 '
1142
1143 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1144         >empty &&
1145         test_must_fail git -c grep.extendedregexp=true \
1146                 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1147         test_cmp empty actual
1148 '
1149
1150 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1151         git -c grep.extendedregexp=true \
1152                 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1153         test_cmp expected actual
1154 '
1155
1156 test_expect_success PCRE 'grep -P -v pattern' '
1157         {
1158                 echo "ab:a+b*c"
1159                 echo "ab:a+bc"
1160         } >expected &&
1161         git grep -P -v "abc" ab >actual &&
1162         test_cmp expected actual
1163 '
1164
1165 test_expect_success PCRE 'grep -P -i pattern' '
1166         cat >expected <<-EOF &&
1167         hello.c:        printf("Hello world.\n");
1168         EOF
1169         git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1170         test_cmp expected actual
1171 '
1172
1173 test_expect_success PCRE 'grep -P -w pattern' '
1174         {
1175                 echo "hello_world:Hello world"
1176                 echo "hello_world:HeLLo world"
1177         } >expected &&
1178         git grep -P -w "He((?i)ll)o" hello_world >actual &&
1179         test_cmp expected actual
1180 '
1181
1182 test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1183         git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1184         test_cmp hello_world actual &&
1185         git grep -P -h "(.)\1" hello_world >actual &&
1186         test_cmp hello_world actual
1187 '
1188
1189 test_expect_success 'grep -G invalidpattern properly dies ' '
1190         test_must_fail git grep -G "a["
1191 '
1192
1193 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1194         test_must_fail git -c grep.patterntype=basic grep "a["
1195 '
1196
1197 test_expect_success 'grep -E invalidpattern properly dies ' '
1198         test_must_fail git grep -E "a["
1199 '
1200
1201 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1202         test_must_fail git -c grep.patterntype=extended grep "a["
1203 '
1204
1205 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1206         test_must_fail git grep -P "a["
1207 '
1208
1209 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1210         test_must_fail git -c grep.patterntype=perl grep "a["
1211 '
1212
1213 test_expect_success 'grep -G -E -F pattern' '
1214         echo "ab:a+b*c" >expected &&
1215         git grep -G -E -F "a+b*c" ab >actual &&
1216         test_cmp expected actual
1217 '
1218
1219 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1220         echo "ab:a+b*c" >expected &&
1221         git \
1222                 -c grep.patterntype=basic \
1223                 -c grep.patterntype=extended \
1224                 -c grep.patterntype=fixed \
1225                 grep "a+b*c" ab >actual &&
1226         test_cmp expected actual
1227 '
1228
1229 test_expect_success 'grep -E -F -G pattern' '
1230         echo "ab:a+bc" >expected &&
1231         git grep -E -F -G "a+b*c" ab >actual &&
1232         test_cmp expected actual
1233 '
1234
1235 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1236         echo "ab:a+bc" >expected &&
1237         git \
1238                 -c grep.patterntype=extended \
1239                 -c grep.patterntype=fixed \
1240                 -c grep.patterntype=basic \
1241                 grep "a+b*c" ab >actual &&
1242         test_cmp expected actual
1243 '
1244
1245 test_expect_success 'grep -F -G -E pattern' '
1246         echo "ab:abc" >expected &&
1247         git grep -F -G -E "a+b*c" ab >actual &&
1248         test_cmp expected actual
1249 '
1250
1251 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1252         echo "ab:abc" >expected &&
1253         git \
1254                 -c grep.patterntype=fixed \
1255                 -c grep.patterntype=basic \
1256                 -c grep.patterntype=extended \
1257                 grep "a+b*c" ab >actual &&
1258         test_cmp expected actual
1259 '
1260
1261 test_expect_success 'grep -G -F -P -E pattern' '
1262         echo "d0:d" >expected &&
1263         git grep -G -F -P -E "[\d]" d0 >actual &&
1264         test_cmp expected actual
1265 '
1266
1267 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1268         echo "d0:d" >expected &&
1269         git \
1270                 -c grep.patterntype=fixed \
1271                 -c grep.patterntype=basic \
1272                 -c grep.patterntype=perl \
1273                 -c grep.patterntype=extended \
1274                 grep "[\d]" d0 >actual &&
1275         test_cmp expected actual
1276 '
1277
1278 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1279         echo "d0:0" >expected &&
1280         git grep -G -F -E -P "[\d]" d0 >actual &&
1281         test_cmp expected actual
1282 '
1283
1284 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1285         echo "d0:0" >expected &&
1286         git \
1287                 -c grep.patterntype=fixed \
1288                 -c grep.patterntype=basic \
1289                 -c grep.patterntype=extended \
1290                 -c grep.patterntype=perl \
1291                 grep "[\d]" d0 >actual &&
1292         test_cmp expected actual
1293 '
1294
1295 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1296         echo "ab:a+b*c" >expected &&
1297         git \
1298                 -c grep.patterntype=fixed \
1299                 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1300         test_cmp expected actual
1301 '
1302
1303 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1304         echo "ab:a+b*c" >expected &&
1305         git \
1306                 -c grep.patterntype=basic \
1307                 grep -F "*c" ab >actual &&
1308         test_cmp expected actual
1309 '
1310
1311 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1312         {
1313                 echo "ab:a+b*c"
1314                 echo "ab:a+bc"
1315         } >expected &&
1316         git \
1317                 -c grep.patterntype=fixed \
1318                 grep -G "a+b" ab >actual &&
1319         test_cmp expected actual
1320 '
1321
1322 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1323         {
1324                 echo "ab:a+b*c"
1325                 echo "ab:a+bc"
1326                 echo "ab:abc"
1327         } >expected &&
1328         git \
1329                 -c grep.patterntype=fixed \
1330                 grep -E "a+" ab >actual &&
1331         test_cmp expected actual
1332 '
1333
1334 cat >expected <<EOF
1335 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1336 hello.c<RED>-<RESET>{
1337 <RED>--<RESET>
1338 hello.c<RED>:<RESET>    /* char ?? */
1339 hello.c<RED>-<RESET>}
1340 <RED>--<RESET>
1341 hello_world<RED>:<RESET>Hello_world
1342 hello_world<RED>-<RESET>HeLLo_world
1343 EOF
1344
1345 test_expect_success 'grep --color, separator' '
1346         test_config color.grep.context          normal &&
1347         test_config color.grep.filename         normal &&
1348         test_config color.grep.function         normal &&
1349         test_config color.grep.linenumber       normal &&
1350         test_config color.grep.match            normal &&
1351         test_config color.grep.selected         normal &&
1352         test_config color.grep.separator        red &&
1353
1354         git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1355         test_decode_color >actual &&
1356         test_cmp expected actual
1357 '
1358
1359 cat >expected <<EOF
1360 hello.c:int main(int argc, const char **argv)
1361 hello.c:        /* char ?? */
1362
1363 hello_world:Hello_world
1364 EOF
1365
1366 test_expect_success 'grep --break' '
1367         git grep --break -e char -e lo_w hello.c hello_world >actual &&
1368         test_cmp expected actual
1369 '
1370
1371 cat >expected <<EOF
1372 hello.c:int main(int argc, const char **argv)
1373 hello.c-{
1374 --
1375 hello.c:        /* char ?? */
1376 hello.c-}
1377
1378 hello_world:Hello_world
1379 hello_world-HeLLo_world
1380 EOF
1381
1382 test_expect_success 'grep --break with context' '
1383         git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1384         test_cmp expected actual
1385 '
1386
1387 cat >expected <<EOF
1388 hello.c
1389 int main(int argc, const char **argv)
1390         /* char ?? */
1391 hello_world
1392 Hello_world
1393 EOF
1394
1395 test_expect_success 'grep --heading' '
1396         git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1397         test_cmp expected actual
1398 '
1399
1400 cat >expected <<EOF
1401 <BOLD;GREEN>hello.c<RESET>
1402 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1403 8:      /* <BLACK;BYELLOW>char<RESET> ?? */
1404
1405 <BOLD;GREEN>hello_world<RESET>
1406 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1407 EOF
1408
1409 test_expect_success 'mimic ack-grep --group' '
1410         test_config color.grep.context          normal &&
1411         test_config color.grep.filename         "bold green" &&
1412         test_config color.grep.function         normal &&
1413         test_config color.grep.linenumber       normal &&
1414         test_config color.grep.match            "black yellow" &&
1415         test_config color.grep.selected         normal &&
1416         test_config color.grep.separator        normal &&
1417
1418         git grep --break --heading -n --color \
1419                 -e char -e lo_w hello.c hello_world |
1420         test_decode_color >actual &&
1421         test_cmp expected actual
1422 '
1423
1424 cat >expected <<EOF
1425 space: line with leading space1
1426 space: line with leading space2
1427 space: line with leading space3
1428 EOF
1429
1430 test_expect_success PCRE 'grep -E "^ "' '
1431         git grep -E "^ " space >actual &&
1432         test_cmp expected actual
1433 '
1434
1435 test_expect_success PCRE 'grep -P "^ "' '
1436         git grep -P "^ " space >actual &&
1437         test_cmp expected actual
1438 '
1439
1440 cat >expected <<EOF
1441 space-line without leading space1
1442 space: line <RED>with <RESET>leading space1
1443 space: line <RED>with <RESET>leading <RED>space2<RESET>
1444 space: line <RED>with <RESET>leading space3
1445 space:line without leading <RED>space2<RESET>
1446 EOF
1447
1448 test_expect_success 'grep --color -e A -e B with context' '
1449         test_config color.grep.context          normal &&
1450         test_config color.grep.filename         normal &&
1451         test_config color.grep.function         normal &&
1452         test_config color.grep.linenumber       normal &&
1453         test_config color.grep.matchContext     normal &&
1454         test_config color.grep.matchSelected    red &&
1455         test_config color.grep.selected         normal &&
1456         test_config color.grep.separator        normal &&
1457
1458         git grep --color=always -C2 -e "with " -e space2  space |
1459         test_decode_color >actual &&
1460         test_cmp expected actual
1461 '
1462
1463 cat >expected <<EOF
1464 space-line without leading space1
1465 space- line with leading space1
1466 space: line <RED>with <RESET>leading <RED>space2<RESET>
1467 space- line with leading space3
1468 space-line without leading space2
1469 EOF
1470
1471 test_expect_success 'grep --color -e A --and -e B with context' '
1472         test_config color.grep.context          normal &&
1473         test_config color.grep.filename         normal &&
1474         test_config color.grep.function         normal &&
1475         test_config color.grep.linenumber       normal &&
1476         test_config color.grep.matchContext     normal &&
1477         test_config color.grep.matchSelected    red &&
1478         test_config color.grep.selected         normal &&
1479         test_config color.grep.separator        normal &&
1480
1481         git grep --color=always -C2 -e "with " --and -e space2  space |
1482         test_decode_color >actual &&
1483         test_cmp expected actual
1484 '
1485
1486 cat >expected <<EOF
1487 space-line without leading space1
1488 space: line <RED>with <RESET>leading space1
1489 space- line with leading space2
1490 space: line <RED>with <RESET>leading space3
1491 space-line without leading space2
1492 EOF
1493
1494 test_expect_success 'grep --color -e A --and --not -e B with context' '
1495         test_config color.grep.context          normal &&
1496         test_config color.grep.filename         normal &&
1497         test_config color.grep.function         normal &&
1498         test_config color.grep.linenumber       normal &&
1499         test_config color.grep.matchContext     normal &&
1500         test_config color.grep.matchSelected    red &&
1501         test_config color.grep.selected         normal &&
1502         test_config color.grep.separator        normal &&
1503
1504         git grep --color=always -C2 -e "with " --and --not -e space2  space |
1505         test_decode_color >actual &&
1506         test_cmp expected actual
1507 '
1508
1509 cat >expected <<EOF
1510 hello.c-
1511 hello.c=int main(int argc, const char **argv)
1512 hello.c-{
1513 hello.c:        pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1514 hello.c-        return 0;
1515 hello.c-        /* char ?? */
1516 hello.c-}
1517 EOF
1518
1519 test_expect_success 'grep --color -e A --and -e B -p with context' '
1520         test_config color.grep.context          normal &&
1521         test_config color.grep.filename         normal &&
1522         test_config color.grep.function         normal &&
1523         test_config color.grep.linenumber       normal &&
1524         test_config color.grep.matchContext     normal &&
1525         test_config color.grep.matchSelected    red &&
1526         test_config color.grep.selected         normal &&
1527         test_config color.grep.separator        normal &&
1528
1529         git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1530         test_decode_color >actual &&
1531         test_cmp expected actual
1532 '
1533
1534 test_expect_success 'grep can find things only in the work tree' '
1535         : >work-tree-only &&
1536         git add work-tree-only &&
1537         test_when_finished "git rm -f work-tree-only" &&
1538         echo "find in work tree" >work-tree-only &&
1539         git grep --quiet "find in work tree" &&
1540         test_must_fail git grep --quiet --cached "find in work tree" &&
1541         test_must_fail git grep --quiet "find in work tree" HEAD
1542 '
1543
1544 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1545         echo "intend to add this" >intend-to-add &&
1546         git add -N intend-to-add &&
1547         test_when_finished "git rm -f intend-to-add" &&
1548         git grep --quiet "intend to add this" &&
1549         test_must_fail git grep --quiet --cached "intend to add this" &&
1550         test_must_fail git grep --quiet "intend to add this" HEAD
1551 '
1552
1553 test_expect_success 'grep does not search work tree with assume unchanged' '
1554         echo "intend to add this" >intend-to-add &&
1555         git add -N intend-to-add &&
1556         git update-index --assume-unchanged intend-to-add &&
1557         test_when_finished "git rm -f intend-to-add" &&
1558         test_must_fail git grep --quiet "intend to add this" &&
1559         test_must_fail git grep --quiet --cached "intend to add this" &&
1560         test_must_fail git grep --quiet "intend to add this" HEAD
1561 '
1562
1563 test_expect_success 'grep can find things only in the index' '
1564         echo "only in the index" >cache-this &&
1565         git add cache-this &&
1566         rm cache-this &&
1567         test_when_finished "git rm --cached cache-this" &&
1568         test_must_fail git grep --quiet "only in the index" &&
1569         git grep --quiet --cached "only in the index" &&
1570         test_must_fail git grep --quiet "only in the index" HEAD
1571 '
1572
1573 test_expect_success 'grep does not report i-t-a with -L --cached' '
1574         echo "intend to add this" >intend-to-add &&
1575         git add -N intend-to-add &&
1576         test_when_finished "git rm -f intend-to-add" &&
1577         git ls-files | grep -v "^intend-to-add\$" >expected &&
1578         git grep -L --cached "nonexistent_string" >actual &&
1579         test_cmp expected actual
1580 '
1581
1582 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1583         echo "intend to add this" >intend-to-add-assume-unchanged &&
1584         git add -N intend-to-add-assume-unchanged &&
1585         test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1586         git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1587         git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1588         git grep -L "nonexistent_string" >actual &&
1589         test_cmp expected actual
1590 '
1591
1592 test_done