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