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