]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7810-grep.sh
revision: add --grep-reflog to filter commits by reflog messages
[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 <stdio.h>
13 int main(int argc, const char **argv)
14 {
15 printf("Hello world.\n");
16 return 0;
17 /* char ?? */
18 }
19 EOF
20
21 test_expect_success setup '
22 {
23 echo foo mmap bar
24 echo foo_mmap bar
25 echo foo_mmap bar mmap
26 echo foo mmap bar_mmap
27 echo foo_mmap bar mmap baz
28 } >file &&
29 {
30 echo Hello world
31 echo HeLLo world
32 echo Hello_world
33 echo HeLLo_world
34 } >hello_world &&
35 {
36 echo "a+b*c"
37 echo "a+bc"
38 echo "abc"
39 } >ab &&
40 echo vvv >v &&
41 echo ww w >w &&
42 echo x x xx x >x &&
43 echo y yy >y &&
44 echo zzz > z &&
45 mkdir t &&
46 echo test >t/t &&
47 echo vvv >t/v &&
48 mkdir t/a &&
49 echo vvv >t/a/v &&
50 {
51 echo "line without leading space1"
52 echo " line with leading space1"
53 echo " line with leading space2"
54 echo " line with leading space3"
55 echo "line without leading space2"
56 } >space &&
57 git add . &&
58 test_tick &&
59 git commit -m initial
60 '
61
62 test_expect_success 'grep should not segfault with a bad input' '
63 test_must_fail git grep "("
64 '
65
66 for H in HEAD ''
67 do
68 case "$H" in
69 HEAD) HC='HEAD:' L='HEAD' ;;
70 '') HC= L='in working tree' ;;
71 esac
72
73 test_expect_success "grep -w $L" '
74 {
75 echo ${HC}file:1:foo mmap bar
76 echo ${HC}file:3:foo_mmap bar mmap
77 echo ${HC}file:4:foo mmap bar_mmap
78 echo ${HC}file:5:foo_mmap bar mmap baz
79 } >expected &&
80 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
81 test_cmp expected actual
82 '
83
84 test_expect_success "grep -w $L" '
85 {
86 echo ${HC}file:1:foo mmap bar
87 echo ${HC}file:3:foo_mmap bar mmap
88 echo ${HC}file:4:foo mmap bar_mmap
89 echo ${HC}file:5:foo_mmap bar mmap baz
90 } >expected &&
91 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
92 test_cmp expected actual
93 '
94
95 test_expect_success "grep -w $L" '
96 {
97 echo ${HC}file:foo mmap bar
98 echo ${HC}file:foo_mmap bar mmap
99 echo ${HC}file:foo mmap bar_mmap
100 echo ${HC}file:foo_mmap bar mmap baz
101 } >expected &&
102 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
103 test_cmp expected actual
104 '
105
106 test_expect_success "grep -w $L (w)" '
107 : >expected &&
108 test_must_fail git grep -n -w -e "^w" >actual &&
109 test_cmp expected actual
110 '
111
112 test_expect_success "grep -w $L (x)" '
113 {
114 echo ${HC}x:1:x x xx x
115 } >expected &&
116 git grep -n -w -e "x xx* x" $H >actual &&
117 test_cmp expected actual
118 '
119
120 test_expect_success "grep -w $L (y-1)" '
121 {
122 echo ${HC}y:1:y yy
123 } >expected &&
124 git grep -n -w -e "^y" $H >actual &&
125 test_cmp expected actual
126 '
127
128 test_expect_success "grep -w $L (y-2)" '
129 : >expected &&
130 if git grep -n -w -e "^y y" $H >actual
131 then
132 echo should not have matched
133 cat actual
134 false
135 else
136 test_cmp expected actual
137 fi
138 '
139
140 test_expect_success "grep -w $L (z)" '
141 : >expected &&
142 if git grep -n -w -e "^z" $H >actual
143 then
144 echo should not have matched
145 cat actual
146 false
147 else
148 test_cmp expected actual
149 fi
150 '
151
152 test_expect_success "grep $L (t-1)" '
153 echo "${HC}t/t:1:test" >expected &&
154 git grep -n -e test $H >actual &&
155 test_cmp expected actual
156 '
157
158 test_expect_success "grep $L (t-2)" '
159 echo "${HC}t:1:test" >expected &&
160 (
161 cd t &&
162 git grep -n -e test $H
163 ) >actual &&
164 test_cmp expected actual
165 '
166
167 test_expect_success "grep $L (t-3)" '
168 echo "${HC}t/t:1:test" >expected &&
169 (
170 cd t &&
171 git grep --full-name -n -e test $H
172 ) >actual &&
173 test_cmp expected actual
174 '
175
176 test_expect_success "grep -c $L (no /dev/null)" '
177 ! git grep -c test $H | grep /dev/null
178 '
179
180 test_expect_success "grep --max-depth -1 $L" '
181 {
182 echo ${HC}t/a/v:1:vvv
183 echo ${HC}t/v:1:vvv
184 echo ${HC}v:1:vvv
185 } >expected &&
186 git grep --max-depth -1 -n -e vvv $H >actual &&
187 test_cmp expected actual
188 '
189
190 test_expect_success "grep --max-depth 0 $L" '
191 {
192 echo ${HC}v:1:vvv
193 } >expected &&
194 git grep --max-depth 0 -n -e vvv $H >actual &&
195 test_cmp expected actual
196 '
197
198 test_expect_success "grep --max-depth 0 -- '*' $L" '
199 {
200 echo ${HC}t/a/v:1:vvv
201 echo ${HC}t/v:1:vvv
202 echo ${HC}v:1:vvv
203 } >expected &&
204 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
205 test_cmp expected actual
206 '
207
208 test_expect_success "grep --max-depth 1 $L" '
209 {
210 echo ${HC}t/v:1:vvv
211 echo ${HC}v:1:vvv
212 } >expected &&
213 git grep --max-depth 1 -n -e vvv $H >actual &&
214 test_cmp expected actual
215 '
216
217 test_expect_success "grep --max-depth 0 -- t $L" '
218 {
219 echo ${HC}t/v:1:vvv
220 } >expected &&
221 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
222 test_cmp expected actual
223 '
224
225 test_expect_success "grep --max-depth 0 -- . t $L" '
226 {
227 echo ${HC}t/v:1:vvv
228 echo ${HC}v:1:vvv
229 } >expected &&
230 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
231 test_cmp expected actual
232 '
233
234 test_expect_success "grep --max-depth 0 -- t . $L" '
235 {
236 echo ${HC}t/v:1:vvv
237 echo ${HC}v:1:vvv
238 } >expected &&
239 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
240 test_cmp expected actual
241 '
242 test_expect_success "grep $L with grep.extendedRegexp=false" '
243 echo "ab:a+bc" >expected &&
244 git -c grep.extendedRegexp=false grep "a+b*c" ab >actual &&
245 test_cmp expected actual
246 '
247
248 test_expect_success "grep $L with grep.extendedRegexp=true" '
249 echo "ab:abc" >expected &&
250 git -c grep.extendedRegexp=true grep "a+b*c" ab >actual &&
251 test_cmp expected actual
252 '
253
254 test_expect_success "grep $L with grep.patterntype=basic" '
255 echo "ab:a+bc" >expected &&
256 git -c grep.patterntype=basic grep "a+b*c" ab >actual &&
257 test_cmp expected actual
258 '
259
260 test_expect_success "grep $L with grep.patterntype=extended" '
261 echo "ab:abc" >expected &&
262 git -c grep.patterntype=extended grep "a+b*c" ab >actual &&
263 test_cmp expected actual
264 '
265
266 test_expect_success "grep $L with grep.patterntype=fixed" '
267 echo "ab:a+b*c" >expected &&
268 git -c grep.patterntype=fixed grep "a+b*c" ab >actual &&
269 test_cmp expected actual
270 '
271
272 test_expect_success LIBPCRE "grep $L with grep.patterntype=perl" '
273 echo "ab:a+b*c" >expected &&
274 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" ab >actual &&
275 test_cmp expected actual
276 '
277
278 test_expect_success "grep $L with grep.patternType=default and grep.extendedRegexp=true" '
279 echo "ab:abc" >expected &&
280 git \
281 -c grep.patternType=default \
282 -c grep.extendedRegexp=true \
283 grep "a+b*c" ab >actual &&
284 test_cmp expected actual
285 '
286
287 test_expect_success "grep $L with grep.extendedRegexp=true and grep.patternType=default" '
288 echo "ab:abc" >expected &&
289 git \
290 -c grep.extendedRegexp=true \
291 -c grep.patternType=default \
292 grep "a+b*c" ab >actual &&
293 test_cmp expected actual
294 '
295
296 test_expect_success 'grep $L with grep.patternType=extended and grep.extendedRegexp=false' '
297 echo "ab:abc" >expected &&
298 git \
299 -c grep.patternType=extended \
300 -c grep.extendedRegexp=false \
301 grep "a+b*c" ab >actual &&
302 test_cmp expected actual
303 '
304
305 test_expect_success 'grep $L with grep.patternType=basic and grep.extendedRegexp=true' '
306 echo "ab:a+bc" >expected &&
307 git \
308 -c grep.patternType=basic \
309 -c grep.extendedRegexp=true \
310 grep "a+b*c" ab >actual &&
311 test_cmp expected actual
312 '
313
314 test_expect_success 'grep $L with grep.extendedRegexp=false and grep.patternType=extended' '
315 echo "ab:abc" >expected &&
316 git \
317 -c grep.extendedRegexp=false \
318 -c grep.patternType=extended \
319 grep "a+b*c" ab >actual &&
320 test_cmp expected actual
321 '
322
323 test_expect_success 'grep $L with grep.extendedRegexp=true and grep.patternType=basic' '
324 echo "ab:a+bc" >expected &&
325 git \
326 -c grep.extendedRegexp=true \
327 -c grep.patternType=basic \
328 grep "a+b*c" ab >actual &&
329 test_cmp expected actual
330 '
331 done
332
333 cat >expected <<EOF
334 file
335 EOF
336 test_expect_success 'grep -l -C' '
337 git grep -l -C1 foo >actual &&
338 test_cmp expected actual
339 '
340
341 cat >expected <<EOF
342 file:5
343 EOF
344 test_expect_success 'grep -l -C' '
345 git grep -c -C1 foo >actual &&
346 test_cmp expected actual
347 '
348
349 test_expect_success 'grep -L -C' '
350 git ls-files >expected &&
351 git grep -L -C1 nonexistent_string >actual &&
352 test_cmp expected actual
353 '
354
355 cat >expected <<EOF
356 file:foo mmap bar_mmap
357 EOF
358
359 test_expect_success 'grep -e A --and -e B' '
360 git grep -e "foo mmap" --and -e bar_mmap >actual &&
361 test_cmp expected actual
362 '
363
364 cat >expected <<EOF
365 file:foo_mmap bar mmap
366 file:foo_mmap bar mmap baz
367 EOF
368
369
370 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
371 git grep \( -e foo_ --or -e baz \) \
372 --and -e " mmap" >actual &&
373 test_cmp expected actual
374 '
375
376 cat >expected <<EOF
377 file:foo mmap bar
378 EOF
379
380 test_expect_success 'grep -e A --and --not -e B' '
381 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
382 test_cmp expected actual
383 '
384
385 test_expect_success 'grep should ignore GREP_OPTIONS' '
386 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
387 test_cmp expected actual
388 '
389
390 test_expect_success 'grep -f, non-existent file' '
391 test_must_fail git grep -f patterns
392 '
393
394 cat >expected <<EOF
395 file:foo mmap bar
396 file:foo_mmap bar
397 file:foo_mmap bar mmap
398 file:foo mmap bar_mmap
399 file:foo_mmap bar mmap baz
400 EOF
401
402 cat >pattern <<EOF
403 mmap
404 EOF
405
406 test_expect_success 'grep -f, one pattern' '
407 git grep -f pattern >actual &&
408 test_cmp expected actual
409 '
410
411 cat >expected <<EOF
412 file:foo mmap bar
413 file:foo_mmap bar
414 file:foo_mmap bar mmap
415 file:foo mmap bar_mmap
416 file:foo_mmap bar mmap baz
417 t/a/v:vvv
418 t/v:vvv
419 v:vvv
420 EOF
421
422 cat >patterns <<EOF
423 mmap
424 vvv
425 EOF
426
427 test_expect_success 'grep -f, multiple patterns' '
428 git grep -f patterns >actual &&
429 test_cmp expected actual
430 '
431
432 test_expect_success 'grep, multiple patterns' '
433 git grep "$(cat patterns)" >actual &&
434 test_cmp expected actual
435 '
436
437 cat >expected <<EOF
438 file:foo mmap bar
439 file:foo_mmap bar
440 file:foo_mmap bar mmap
441 file:foo mmap bar_mmap
442 file:foo_mmap bar mmap baz
443 t/a/v:vvv
444 t/v:vvv
445 v:vvv
446 EOF
447
448 cat >patterns <<EOF
449
450 mmap
451
452 vvv
453
454 EOF
455
456 test_expect_success 'grep -f, ignore empty lines' '
457 git grep -f patterns >actual &&
458 test_cmp expected actual
459 '
460
461 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
462 git grep -f - <patterns >actual &&
463 test_cmp expected actual
464 '
465
466 cat >expected <<EOF
467 y:y yy
468 --
469 z:zzz
470 EOF
471
472 test_expect_success 'grep -q, silently report matches' '
473 >empty &&
474 git grep -q mmap >actual &&
475 test_cmp empty actual &&
476 test_must_fail git grep -q qfwfq >actual &&
477 test_cmp empty actual
478 '
479
480 test_expect_success 'grep -C1 hunk mark between files' '
481 git grep -C1 "^[yz]" >actual &&
482 test_cmp expected actual
483 '
484
485 test_expect_success 'log grep setup' '
486 echo a >>file &&
487 test_tick &&
488 GIT_AUTHOR_NAME="With * Asterisk" \
489 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
490 git commit -a -m "second" &&
491
492 echo a >>file &&
493 test_tick &&
494 git commit -a -m "third" &&
495
496 echo a >>file &&
497 test_tick &&
498 GIT_AUTHOR_NAME="Night Fall" \
499 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
500 git commit -a -m "fourth"
501 '
502
503 test_expect_success 'log grep (1)' '
504 git log --author=author --pretty=tformat:%s >actual &&
505 {
506 echo third && echo initial
507 } >expect &&
508 test_cmp expect actual
509 '
510
511 test_expect_success 'log grep (2)' '
512 git log --author=" * " -F --pretty=tformat:%s >actual &&
513 {
514 echo second
515 } >expect &&
516 test_cmp expect actual
517 '
518
519 test_expect_success 'log grep (3)' '
520 git log --author="^A U" --pretty=tformat:%s >actual &&
521 {
522 echo third && echo initial
523 } >expect &&
524 test_cmp expect actual
525 '
526
527 test_expect_success 'log grep (4)' '
528 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
529 {
530 echo second
531 } >expect &&
532 test_cmp expect actual
533 '
534
535 test_expect_success 'log grep (5)' '
536 git log --author=Thor -F --pretty=tformat:%s >actual &&
537 {
538 echo third && echo initial
539 } >expect &&
540 test_cmp expect actual
541 '
542
543 test_expect_success 'log grep (6)' '
544 git log --author=-0700 --pretty=tformat:%s >actual &&
545 >expect &&
546 test_cmp expect actual
547 '
548
549 test_expect_success 'log grep (7)' '
550 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
551 echo third >expect &&
552 test_cmp expect actual
553 '
554
555 test_expect_success 'log grep (8)' '
556 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
557 {
558 echo third && echo second
559 } >expect &&
560 test_cmp expect actual
561 '
562
563 test_expect_success 'log grep (9)' '
564 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
565 echo third >expect &&
566 test_cmp expect actual
567 '
568
569 test_expect_success 'log grep (9)' '
570 git log -g --grep-reflog="commit: third" --author="non-existant" --pretty=tformat:%s >actual &&
571 : >expect &&
572 test_cmp expect actual
573 '
574
575 test_expect_success 'log with multiple --grep uses union' '
576 git log --grep=i --grep=r --format=%s >actual &&
577 {
578 echo fourth && echo third && echo initial
579 } >expect &&
580 test_cmp expect actual
581 '
582
583 test_expect_success 'log --all-match with multiple --grep uses intersection' '
584 git log --all-match --grep=i --grep=r --format=%s >actual &&
585 {
586 echo third
587 } >expect &&
588 test_cmp expect actual
589 '
590
591 test_expect_success 'log with multiple --author uses union' '
592 git log --author="Thor" --author="Aster" --format=%s >actual &&
593 {
594 echo third && echo second && echo initial
595 } >expect &&
596 test_cmp expect actual
597 '
598
599 test_expect_success 'log --all-match with multiple --author still uses union' '
600 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
601 {
602 echo third && echo second && echo initial
603 } >expect &&
604 test_cmp expect actual
605 '
606
607 test_expect_success 'log --grep --author uses intersection' '
608 # grep matches only third and fourth
609 # author matches only initial and third
610 git log --author="A U Thor" --grep=r --format=%s >actual &&
611 {
612 echo third
613 } >expect &&
614 test_cmp expect actual
615 '
616
617 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
618 # grep matches initial and second but not third
619 # author matches only initial and third
620 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
621 {
622 echo initial
623 } >expect &&
624 test_cmp expect actual
625 '
626
627 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
628 # grep matches only initial and third
629 # author matches all but second
630 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
631 {
632 echo third && echo initial
633 } >expect &&
634 test_cmp expect actual
635 '
636
637 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
638 # grep matches only initial and third
639 # author matches all but second
640 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
641 {
642 echo third && echo initial
643 } >expect &&
644 test_cmp expect actual
645 '
646
647 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
648 # grep matches only third
649 # author matches only initial and third
650 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
651 {
652 echo third
653 } >expect &&
654 test_cmp expect actual
655 '
656
657 test_expect_success 'log --author does not search in timestamp' '
658 : >expect &&
659 git log --author="$GIT_AUTHOR_DATE" >actual &&
660 test_cmp expect actual
661 '
662
663 test_expect_success 'log --committer does not search in timestamp' '
664 : >expect &&
665 git log --committer="$GIT_COMMITTER_DATE" >actual &&
666 test_cmp expect actual
667 '
668
669 test_expect_success 'grep with CE_VALID file' '
670 git update-index --assume-unchanged t/t &&
671 rm t/t &&
672 test "$(git grep test)" = "t/t:test" &&
673 git update-index --no-assume-unchanged t/t &&
674 git checkout t/t
675 '
676
677 cat >expected <<EOF
678 hello.c=#include <stdio.h>
679 hello.c: return 0;
680 EOF
681
682 test_expect_success 'grep -p with userdiff' '
683 git config diff.custom.funcname "^#" &&
684 echo "hello.c diff=custom" >.gitattributes &&
685 git grep -p return >actual &&
686 test_cmp expected actual
687 '
688
689 cat >expected <<EOF
690 hello.c=int main(int argc, const char **argv)
691 hello.c: return 0;
692 EOF
693
694 test_expect_success 'grep -p' '
695 rm -f .gitattributes &&
696 git grep -p return >actual &&
697 test_cmp expected actual
698 '
699
700 cat >expected <<EOF
701 hello.c-#include <stdio.h>
702 hello.c=int main(int argc, const char **argv)
703 hello.c-{
704 hello.c- printf("Hello world.\n");
705 hello.c: return 0;
706 EOF
707
708 test_expect_success 'grep -p -B5' '
709 git grep -p -B5 return >actual &&
710 test_cmp expected actual
711 '
712
713 cat >expected <<EOF
714 hello.c=int main(int argc, const char **argv)
715 hello.c-{
716 hello.c- printf("Hello world.\n");
717 hello.c: return 0;
718 hello.c- /* char ?? */
719 hello.c-}
720 EOF
721
722 test_expect_success 'grep -W' '
723 git grep -W return >actual &&
724 test_cmp expected actual
725 '
726
727 cat >expected <<EOF
728 hello.c= printf("Hello world.\n");
729 hello.c: return 0;
730 hello.c- /* char ?? */
731 EOF
732
733 test_expect_success 'grep -W with userdiff' '
734 test_when_finished "rm -f .gitattributes" &&
735 git config diff.custom.xfuncname "(printf.*|})$" &&
736 echo "hello.c diff=custom" >.gitattributes &&
737 git grep -W return >actual &&
738 test_cmp expected actual
739 '
740
741 test_expect_success 'grep from a subdirectory to search wider area (1)' '
742 mkdir -p s &&
743 (
744 cd s && git grep "x x x" ..
745 )
746 '
747
748 test_expect_success 'grep from a subdirectory to search wider area (2)' '
749 mkdir -p s &&
750 (
751 cd s || exit 1
752 ( git grep xxyyzz .. >out ; echo $? >status )
753 ! test -s out &&
754 test 1 = $(cat status)
755 )
756 '
757
758 cat >expected <<EOF
759 hello.c:int main(int argc, const char **argv)
760 EOF
761
762 test_expect_success 'grep -Fi' '
763 git grep -Fi "CHAR *" >actual &&
764 test_cmp expected actual
765 '
766
767 test_expect_success 'outside of git repository' '
768 rm -fr non &&
769 mkdir -p non/git/sub &&
770 echo hello >non/git/file1 &&
771 echo world >non/git/sub/file2 &&
772 {
773 echo file1:hello &&
774 echo sub/file2:world
775 } >non/expect.full &&
776 echo file2:world >non/expect.sub &&
777 (
778 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
779 export GIT_CEILING_DIRECTORIES &&
780 cd non/git &&
781 test_must_fail git grep o &&
782 git grep --no-index o >../actual.full &&
783 test_cmp ../expect.full ../actual.full
784 cd sub &&
785 test_must_fail git grep o &&
786 git grep --no-index o >../../actual.sub &&
787 test_cmp ../../expect.sub ../../actual.sub
788 ) &&
789
790 echo ".*o*" >non/git/.gitignore &&
791 (
792 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
793 export GIT_CEILING_DIRECTORIES &&
794 cd non/git &&
795 test_must_fail git grep o &&
796 git grep --no-index --exclude-standard o >../actual.full &&
797 test_cmp ../expect.full ../actual.full &&
798
799 {
800 echo ".gitignore:.*o*"
801 cat ../expect.full
802 } >../expect.with.ignored &&
803 git grep --no-index --no-exclude o >../actual.full &&
804 test_cmp ../expect.with.ignored ../actual.full
805 )
806 '
807
808 test_expect_success 'inside git repository but with --no-index' '
809 rm -fr is &&
810 mkdir -p is/git/sub &&
811 echo hello >is/git/file1 &&
812 echo world >is/git/sub/file2 &&
813 echo ".*o*" >is/git/.gitignore &&
814 {
815 echo file1:hello &&
816 echo sub/file2:world
817 } >is/expect.unignored &&
818 {
819 echo ".gitignore:.*o*" &&
820 cat is/expect.unignored
821 } >is/expect.full &&
822 : >is/expect.empty &&
823 echo file2:world >is/expect.sub &&
824 (
825 cd is/git &&
826 git init &&
827 test_must_fail git grep o >../actual.full &&
828 test_cmp ../expect.empty ../actual.full &&
829
830 git grep --untracked o >../actual.unignored &&
831 test_cmp ../expect.unignored ../actual.unignored &&
832
833 git grep --no-index o >../actual.full &&
834 test_cmp ../expect.full ../actual.full &&
835
836 git grep --no-index --exclude-standard o >../actual.unignored &&
837 test_cmp ../expect.unignored ../actual.unignored &&
838
839 cd sub &&
840 test_must_fail git grep o >../../actual.sub &&
841 test_cmp ../../expect.empty ../../actual.sub &&
842
843 git grep --no-index o >../../actual.sub &&
844 test_cmp ../../expect.sub ../../actual.sub &&
845
846 git grep --untracked o >../../actual.sub &&
847 test_cmp ../../expect.sub ../../actual.sub
848 )
849 '
850
851 test_expect_success 'setup double-dash tests' '
852 cat >double-dash <<EOF &&
853 --
854 ->
855 other
856 EOF
857 git add double-dash
858 '
859
860 cat >expected <<EOF
861 double-dash:->
862 EOF
863 test_expect_success 'grep -- pattern' '
864 git grep -- "->" >actual &&
865 test_cmp expected actual
866 '
867 test_expect_success 'grep -- pattern -- pathspec' '
868 git grep -- "->" -- double-dash >actual &&
869 test_cmp expected actual
870 '
871 test_expect_success 'grep -e pattern -- path' '
872 git grep -e "->" -- double-dash >actual &&
873 test_cmp expected actual
874 '
875
876 cat >expected <<EOF
877 double-dash:--
878 EOF
879 test_expect_success 'grep -e -- -- path' '
880 git grep -e -- -- double-dash >actual &&
881 test_cmp expected actual
882 '
883
884 cat >expected <<EOF
885 hello.c:int main(int argc, const char **argv)
886 hello.c: printf("Hello world.\n");
887 EOF
888
889 test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
890 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
891 test_cmp expected actual
892 '
893
894 test_expect_success LIBPCRE 'grep -P pattern' '
895 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
896 test_cmp expected actual
897 '
898
899 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
900 >empty &&
901 test_must_fail git -c grep.extendedregexp=true \
902 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
903 test_cmp empty actual
904 '
905
906 test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' '
907 git -c grep.extendedregexp=true \
908 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
909 test_cmp expected actual
910 '
911
912 test_expect_success LIBPCRE 'grep -P -v pattern' '
913 {
914 echo "ab:a+b*c"
915 echo "ab:a+bc"
916 } >expected &&
917 git grep -P -v "abc" ab >actual &&
918 test_cmp expected actual
919 '
920
921 test_expect_success LIBPCRE 'grep -P -i pattern' '
922 cat >expected <<-EOF &&
923 hello.c: printf("Hello world.\n");
924 EOF
925 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
926 test_cmp expected actual
927 '
928
929 test_expect_success LIBPCRE 'grep -P -w pattern' '
930 {
931 echo "hello_world:Hello world"
932 echo "hello_world:HeLLo world"
933 } >expected &&
934 git grep -P -w "He((?i)ll)o" hello_world >actual &&
935 test_cmp expected actual
936 '
937
938 test_expect_success 'grep -G invalidpattern properly dies ' '
939 test_must_fail git grep -G "a["
940 '
941
942 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
943 test_must_fail git -c grep.patterntype=basic grep "a["
944 '
945
946 test_expect_success 'grep -E invalidpattern properly dies ' '
947 test_must_fail git grep -E "a["
948 '
949
950 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
951 test_must_fail git -c grep.patterntype=extended grep "a["
952 '
953
954 test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' '
955 test_must_fail git grep -P "a["
956 '
957
958 test_expect_success LIBPCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
959 test_must_fail git -c grep.patterntype=perl grep "a["
960 '
961
962 test_expect_success 'grep -G -E -F pattern' '
963 echo "ab:a+b*c" >expected &&
964 git grep -G -E -F "a+b*c" ab >actual &&
965 test_cmp expected actual
966 '
967
968 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
969 echo "ab:a+b*c" >expected &&
970 git \
971 -c grep.patterntype=basic \
972 -c grep.patterntype=extended \
973 -c grep.patterntype=fixed \
974 grep "a+b*c" ab >actual &&
975 test_cmp expected actual
976 '
977
978 test_expect_success 'grep -E -F -G pattern' '
979 echo "ab:a+bc" >expected &&
980 git grep -E -F -G "a+b*c" ab >actual &&
981 test_cmp expected actual
982 '
983
984 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
985 echo "ab:a+bc" >expected &&
986 git \
987 -c grep.patterntype=extended \
988 -c grep.patterntype=fixed \
989 -c grep.patterntype=basic \
990 grep "a+b*c" ab >actual &&
991 test_cmp expected actual
992 '
993
994 test_expect_success 'grep -F -G -E pattern' '
995 echo "ab:abc" >expected &&
996 git grep -F -G -E "a+b*c" ab >actual &&
997 test_cmp expected actual
998 '
999
1000 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1001 echo "ab:abc" >expected &&
1002 git \
1003 -c grep.patterntype=fixed \
1004 -c grep.patterntype=basic \
1005 -c grep.patterntype=extended \
1006 grep "a+b*c" ab >actual &&
1007 test_cmp expected actual
1008 '
1009
1010 test_expect_success 'grep -G -F -P -E pattern' '
1011 >empty &&
1012 test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual &&
1013 test_cmp empty actual
1014 '
1015
1016 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1017 >empty &&
1018 test_must_fail git \
1019 -c grep.patterntype=fixed \
1020 -c grep.patterntype=basic \
1021 -c grep.patterntype=perl \
1022 -c grep.patterntype=extended \
1023 grep "a\x{2b}b\x{2a}c" ab >actual &&
1024 test_cmp empty actual
1025 '
1026
1027 test_expect_success LIBPCRE 'grep -G -F -E -P pattern' '
1028 echo "ab:a+b*c" >expected &&
1029 git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual &&
1030 test_cmp expected actual
1031 '
1032
1033 test_expect_success LIBPCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1034 echo "ab:a+b*c" >expected &&
1035 git \
1036 -c grep.patterntype=fixed \
1037 -c grep.patterntype=basic \
1038 -c grep.patterntype=extended \
1039 -c grep.patterntype=perl \
1040 grep "a\x{2b}b\x{2a}c" ab >actual &&
1041 test_cmp expected actual
1042 '
1043
1044 test_expect_success LIBPCRE 'grep -P pattern with grep.patternType=fixed' '
1045 echo "ab:a+b*c" >expected &&
1046 git \
1047 -c grep.patterntype=fixed \
1048 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1049 test_cmp expected actual
1050 '
1051
1052 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1053 echo "ab:a+b*c" >expected &&
1054 git \
1055 -c grep.patterntype=basic \
1056 grep -F "*c" ab >actual &&
1057 test_cmp expected actual
1058 '
1059
1060 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1061 {
1062 echo "ab:a+b*c"
1063 echo "ab:a+bc"
1064 } >expected &&
1065 git \
1066 -c grep.patterntype=fixed \
1067 grep -G "a+b" ab >actual &&
1068 test_cmp expected actual
1069 '
1070
1071 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1072 {
1073 echo "ab:a+b*c"
1074 echo "ab:a+bc"
1075 echo "ab:abc"
1076 } >expected &&
1077 git \
1078 -c grep.patterntype=fixed \
1079 grep -E "a+" ab >actual &&
1080 test_cmp expected actual
1081 '
1082
1083 test_config() {
1084 git config "$1" "$2" &&
1085 test_when_finished "git config --unset $1"
1086 }
1087
1088 cat >expected <<EOF
1089 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1090 hello.c<RED>-<RESET>{
1091 <RED>--<RESET>
1092 hello.c<RED>:<RESET> /* char ?? */
1093 hello.c<RED>-<RESET>}
1094 <RED>--<RESET>
1095 hello_world<RED>:<RESET>Hello_world
1096 hello_world<RED>-<RESET>HeLLo_world
1097 EOF
1098
1099 test_expect_success 'grep --color, separator' '
1100 test_config color.grep.context normal &&
1101 test_config color.grep.filename normal &&
1102 test_config color.grep.function normal &&
1103 test_config color.grep.linenumber normal &&
1104 test_config color.grep.match normal &&
1105 test_config color.grep.selected normal &&
1106 test_config color.grep.separator red &&
1107
1108 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1109 test_decode_color >actual &&
1110 test_cmp expected actual
1111 '
1112
1113 cat >expected <<EOF
1114 hello.c:int main(int argc, const char **argv)
1115 hello.c: /* char ?? */
1116
1117 hello_world:Hello_world
1118 EOF
1119
1120 test_expect_success 'grep --break' '
1121 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1122 test_cmp expected actual
1123 '
1124
1125 cat >expected <<EOF
1126 hello.c:int main(int argc, const char **argv)
1127 hello.c-{
1128 --
1129 hello.c: /* char ?? */
1130 hello.c-}
1131
1132 hello_world:Hello_world
1133 hello_world-HeLLo_world
1134 EOF
1135
1136 test_expect_success 'grep --break with context' '
1137 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1138 test_cmp expected actual
1139 '
1140
1141 cat >expected <<EOF
1142 hello.c
1143 int main(int argc, const char **argv)
1144 /* char ?? */
1145 hello_world
1146 Hello_world
1147 EOF
1148
1149 test_expect_success 'grep --heading' '
1150 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1151 test_cmp expected actual
1152 '
1153
1154 cat >expected <<EOF
1155 <BOLD;GREEN>hello.c<RESET>
1156 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1157 6: /* <BLACK;BYELLOW>char<RESET> ?? */
1158
1159 <BOLD;GREEN>hello_world<RESET>
1160 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1161 EOF
1162
1163 test_expect_success 'mimic ack-grep --group' '
1164 test_config color.grep.context normal &&
1165 test_config color.grep.filename "bold green" &&
1166 test_config color.grep.function normal &&
1167 test_config color.grep.linenumber normal &&
1168 test_config color.grep.match "black yellow" &&
1169 test_config color.grep.selected normal &&
1170 test_config color.grep.separator normal &&
1171
1172 git grep --break --heading -n --color \
1173 -e char -e lo_w hello.c hello_world |
1174 test_decode_color >actual &&
1175 test_cmp expected actual
1176 '
1177
1178 cat >expected <<EOF
1179 space: line with leading space1
1180 space: line with leading space2
1181 space: line with leading space3
1182 EOF
1183
1184 test_expect_success LIBPCRE 'grep -E "^ "' '
1185 git grep -E "^ " space >actual &&
1186 test_cmp expected actual
1187 '
1188
1189 test_expect_success LIBPCRE 'grep -P "^ "' '
1190 git grep -P "^ " space >actual &&
1191 test_cmp expected actual
1192 '
1193
1194 test_done