Git 2.38
[alt-git.git] / t / t7810-grep.sh
blob0f937990a062e30b5f11baa296c88472019ac338
1 #!/bin/sh
3 # Copyright (c) 2006 Junio C Hamano
6 test_description='git grep various.
9 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
10 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 . ./test-lib.sh
14 test_invalid_grep_expression() {
15 params="$@" &&
16 test_expect_success "invalid expression: grep $params" '
17 test_must_fail git grep $params -- nonexisting
21 cat >hello.c <<EOF
22 #include <assert.h>
23 #include <stdio.h>
25 int main(int argc, const char **argv)
27 printf("Hello world.\n");
28 return 0;
29 /* char ?? */
31 EOF
33 test_expect_success setup '
34 cat >file <<-\EOF &&
35 foo mmap bar
36 foo_mmap bar
37 foo_mmap bar mmap
38 foo mmap bar_mmap
39 foo_mmap bar mmap baz
40 EOF
41 cat >hello_world <<-\EOF &&
42 Hello world
43 HeLLo world
44 Hello_world
45 HeLLo_world
46 EOF
47 cat >ab <<-\EOF &&
48 a+b*c
49 a+bc
50 abc
51 EOF
52 cat >d0 <<-\EOF &&
55 EOF
56 echo vvv >v &&
57 echo ww w >w &&
58 echo x x xx x >x &&
59 echo y yy >y &&
60 echo zzz > z &&
61 mkdir t &&
62 echo test >t/t &&
63 echo vvv >t/v &&
64 mkdir t/a &&
65 echo vvv >t/a/v &&
66 qz_to_tab_space >space <<-\EOF &&
67 line without leading space1
68 Zline with leading space1
69 Zline with leading space2
70 Zline with leading space3
71 line without leading space2
72 EOF
73 cat >hello.ps1 <<-\EOF &&
74 # No-op.
75 function dummy() {}
77 # Say hello.
78 function hello() {
79 echo "Hello world."
80 echo "Hello again."
81 } # hello
83 # Still a no-op.
84 function dummy() {}
85 EOF
86 if test_have_prereq FUNNYNAMES
87 then
88 echo unusual >"\"unusual\" pathname" &&
89 echo unusual >"t/nested \"unusual\" pathname"
90 fi &&
91 git add . &&
92 test_tick &&
93 git commit -m initial
96 test_expect_success 'grep should not segfault with a bad input' '
97 test_must_fail git grep "("
100 test_invalid_grep_expression --and -e A
102 test_pattern_type () {
103 H=$1 &&
104 HC=$2 &&
105 L=$3 &&
106 type=$4 &&
107 shift 4 &&
109 expected_str= &&
110 case "$type" in
111 BRE)
112 expected_str="${HC}ab:a+bc"
114 ERE)
115 expected_str="${HC}ab:abc"
117 FIX)
118 expected_str="${HC}ab:a+b*c"
121 BUG "unknown pattern type '$type'"
123 esac &&
124 config_str="$@" &&
126 test_expect_success "grep $L with '$config_str' interpreted as $type" '
127 echo $expected_str >expected &&
128 git $config_str grep "a+b*c" $H ab >actual &&
129 test_cmp expected actual
133 for H in HEAD ''
135 case "$H" in
136 HEAD) HC='HEAD:' L='HEAD' ;;
137 '') HC= L='in working tree' ;;
138 esac
140 test_expect_success "grep -w $L" '
141 cat >expected <<-EOF &&
142 ${HC}file:1:foo mmap bar
143 ${HC}file:3:foo_mmap bar mmap
144 ${HC}file:4:foo mmap bar_mmap
145 ${HC}file:5:foo_mmap bar mmap baz
147 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
148 test_cmp expected actual
151 test_expect_success "grep -w $L (with --column)" '
152 cat >expected <<-EOF &&
153 ${HC}file:5:foo mmap bar
154 ${HC}file:14:foo_mmap bar mmap
155 ${HC}file:5:foo mmap bar_mmap
156 ${HC}file:14:foo_mmap bar mmap baz
158 git grep --column -w -e mmap $H >actual &&
159 test_cmp expected actual
162 test_expect_success "grep -w $L (with --column, extended OR)" '
163 cat >expected <<-EOF &&
164 ${HC}file:14:foo_mmap bar mmap
165 ${HC}file:19:foo_mmap bar mmap baz
167 git grep --column -w -e mmap$ --or -e baz $H >actual &&
168 test_cmp expected actual
171 test_expect_success "grep -w $L (with --column, --invert-match)" '
172 cat >expected <<-EOF &&
173 ${HC}file:1:foo mmap bar
174 ${HC}file:1:foo_mmap bar
175 ${HC}file:1:foo_mmap bar mmap
176 ${HC}file:1:foo mmap bar_mmap
178 git grep --column --invert-match -w -e baz $H -- file >actual &&
179 test_cmp expected actual
182 test_expect_success "grep $L (with --column, --invert-match, extended OR)" '
183 cat >expected <<-EOF &&
184 ${HC}hello_world:6:HeLLo_world
186 git grep --column --invert-match -e ll --or --not -e _ $H -- hello_world \
187 >actual &&
188 test_cmp expected actual
191 test_expect_success "grep $L (with --column, --invert-match, extended AND)" '
192 cat >expected <<-EOF &&
193 ${HC}hello_world:3:Hello world
194 ${HC}hello_world:3:Hello_world
195 ${HC}hello_world:6:HeLLo_world
197 git grep --column --invert-match --not -e _ --and --not -e ll $H -- hello_world \
198 >actual &&
199 test_cmp expected actual
202 test_expect_success "grep $L (with --column, double-negation)" '
203 cat >expected <<-EOF &&
204 ${HC}file:1:foo_mmap bar mmap baz
206 git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
207 >actual &&
208 test_cmp expected actual
211 test_expect_success "grep -w $L (with --column, -C)" '
212 cat >expected <<-EOF &&
213 ${HC}file:5:foo mmap bar
214 ${HC}file-foo_mmap bar
215 ${HC}file:14:foo_mmap bar mmap
216 ${HC}file:5:foo mmap bar_mmap
217 ${HC}file:14:foo_mmap bar mmap baz
219 git grep --column -w -C1 -e mmap $H >actual &&
220 test_cmp expected actual
223 test_expect_success "grep -w $L (with --line-number, --column)" '
224 cat >expected <<-EOF &&
225 ${HC}file:1:5:foo mmap bar
226 ${HC}file:3:14:foo_mmap bar mmap
227 ${HC}file:4:5:foo mmap bar_mmap
228 ${HC}file:5:14:foo_mmap bar mmap baz
230 git grep -n --column -w -e mmap $H >actual &&
231 test_cmp expected actual
234 test_expect_success "grep -w $L (with non-extended patterns, --column)" '
235 cat >expected <<-EOF &&
236 ${HC}file:5:foo mmap bar
237 ${HC}file:10:foo_mmap bar
238 ${HC}file:10:foo_mmap bar mmap
239 ${HC}file:5:foo mmap bar_mmap
240 ${HC}file:10:foo_mmap bar mmap baz
242 git grep --column -w -e bar -e mmap $H >actual &&
243 test_cmp expected actual
246 test_expect_success "grep -w $L" '
247 cat >expected <<-EOF &&
248 ${HC}file:1:foo mmap bar
249 ${HC}file:3:foo_mmap bar mmap
250 ${HC}file:4:foo mmap bar_mmap
251 ${HC}file:5:foo_mmap bar mmap baz
253 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
254 test_cmp expected actual
257 test_expect_success "grep -w $L" '
258 cat >expected <<-EOF &&
259 ${HC}file:foo mmap bar
260 ${HC}file:foo_mmap bar mmap
261 ${HC}file:foo mmap bar_mmap
262 ${HC}file:foo_mmap bar mmap baz
264 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
265 test_cmp expected actual
268 test_expect_success "grep -w $L (w)" '
269 test_must_fail git grep -n -w -e "^w" $H >actual &&
270 test_must_be_empty actual
273 test_expect_success "grep -w $L (x)" '
274 cat >expected <<-EOF &&
275 ${HC}x:1:x x xx x
277 git grep -n -w -e "x xx* x" $H >actual &&
278 test_cmp expected actual
281 test_expect_success "grep -w $L (y-1)" '
282 cat >expected <<-EOF &&
283 ${HC}y:1:y yy
285 git grep -n -w -e "^y" $H >actual &&
286 test_cmp expected actual
289 test_expect_success "grep -w $L (y-2)" '
290 if git grep -n -w -e "^y y" $H >actual
291 then
292 echo should not have matched
293 cat actual
294 false
295 else
296 test_must_be_empty actual
300 test_expect_success "grep -w $L (z)" '
301 if git grep -n -w -e "^z" $H >actual
302 then
303 echo should not have matched
304 cat actual
305 false
306 else
307 test_must_be_empty actual
311 test_expect_success "grep $L (with --column, --only-matching)" '
312 cat >expected <<-EOF &&
313 ${HC}file:1:5:mmap
314 ${HC}file:2:5:mmap
315 ${HC}file:3:5:mmap
316 ${HC}file:3:13:mmap
317 ${HC}file:4:5:mmap
318 ${HC}file:4:13:mmap
319 ${HC}file:5:5:mmap
320 ${HC}file:5:13:mmap
322 git grep --column -n -o -e mmap $H >actual &&
323 test_cmp expected actual
326 test_expect_success "grep $L (t-1)" '
327 echo "${HC}t/t:1:test" >expected &&
328 git grep -n -e test $H >actual &&
329 test_cmp expected actual
332 test_expect_success "grep $L (t-2)" '
333 echo "${HC}t:1:test" >expected &&
335 cd t &&
336 git grep -n -e test $H
337 ) >actual &&
338 test_cmp expected actual
341 test_expect_success "grep $L (t-3)" '
342 echo "${HC}t/t:1:test" >expected &&
344 cd t &&
345 git grep --full-name -n -e test $H
346 ) >actual &&
347 test_cmp expected actual
350 test_expect_success "grep -c $L (no /dev/null)" '
351 ! git grep -c test $H | grep /dev/null
354 test_expect_success "grep --max-depth -1 $L" '
355 cat >expected <<-EOF &&
356 ${HC}t/a/v:1:vvv
357 ${HC}t/v:1:vvv
358 ${HC}v:1:vvv
360 git grep --max-depth -1 -n -e vvv $H >actual &&
361 test_cmp expected actual &&
362 git grep --recursive -n -e vvv $H >actual &&
363 test_cmp expected actual
366 test_expect_success "grep --max-depth 0 $L" '
367 cat >expected <<-EOF &&
368 ${HC}v:1:vvv
370 git grep --max-depth 0 -n -e vvv $H >actual &&
371 test_cmp expected actual &&
372 git grep --no-recursive -n -e vvv $H >actual &&
373 test_cmp expected actual
376 test_expect_success "grep --max-depth 0 -- '*' $L" '
377 cat >expected <<-EOF &&
378 ${HC}t/a/v:1:vvv
379 ${HC}t/v:1:vvv
380 ${HC}v:1:vvv
382 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
383 test_cmp expected actual &&
384 git grep --no-recursive -n -e vvv $H -- "*" >actual &&
385 test_cmp expected actual
388 test_expect_success "grep --max-depth 1 $L" '
389 cat >expected <<-EOF &&
390 ${HC}t/v:1:vvv
391 ${HC}v:1:vvv
393 git grep --max-depth 1 -n -e vvv $H >actual &&
394 test_cmp expected actual
397 test_expect_success "grep --max-depth 0 -- t $L" '
398 cat >expected <<-EOF &&
399 ${HC}t/v:1:vvv
401 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
402 test_cmp expected actual &&
403 git grep --no-recursive -n -e vvv $H -- t >actual &&
404 test_cmp expected actual
407 test_expect_success "grep --max-depth 0 -- . t $L" '
408 cat >expected <<-EOF &&
409 ${HC}t/v:1:vvv
410 ${HC}v:1:vvv
412 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
413 test_cmp expected actual &&
414 git grep --no-recursive -n -e vvv $H -- . t >actual &&
415 test_cmp expected actual
418 test_expect_success "grep --max-depth 0 -- t . $L" '
419 cat >expected <<-EOF &&
420 ${HC}t/v:1:vvv
421 ${HC}v:1:vvv
423 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
424 test_cmp expected actual &&
425 git grep --no-recursive -n -e vvv $H -- t . >actual &&
426 test_cmp expected actual
430 test_pattern_type "$H" "$HC" "$L" BRE -c grep.extendedRegexp=false
431 test_pattern_type "$H" "$HC" "$L" ERE -c grep.extendedRegexp=true
432 test_pattern_type "$H" "$HC" "$L" BRE -c grep.patternType=basic
433 test_pattern_type "$H" "$HC" "$L" ERE -c grep.patternType=extended
434 test_pattern_type "$H" "$HC" "$L" FIX -c grep.patternType=fixed
436 test_expect_success PCRE "grep $L with grep.patterntype=perl" '
437 echo "${HC}ab:a+b*c" >expected &&
438 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
439 test_cmp expected actual
442 test_expect_success !FAIL_PREREQS,!PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
443 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
446 test_pattern_type "$H" "$HC" "$L" ERE \
447 -c grep.patternType=default \
448 -c grep.extendedRegexp=true
449 test_pattern_type "$H" "$HC" "$L" ERE \
450 -c grep.extendedRegexp=true \
451 -c grep.patternType=default
452 test_pattern_type "$H" "$HC" "$L" ERE \
453 -c grep.patternType=extended \
454 -c grep.extendedRegexp=false
455 test_pattern_type "$H" "$HC" "$L" BRE \
456 -c grep.patternType=basic \
457 -c grep.extendedRegexp=true
458 test_pattern_type "$H" "$HC" "$L" ERE \
459 -c grep.extendedRegexp=false \
460 -c grep.patternType=extended
461 test_pattern_type "$H" "$HC" "$L" BRE \
462 -c grep.extendedRegexp=true \
463 -c grep.patternType=basic
465 # grep.extendedRegexp is last-one-wins
466 test_pattern_type "$H" "$HC" "$L" BRE \
467 -c grep.extendedRegexp=true \
468 -c grep.extendedRegexp=false
470 # grep.patternType=basic pays no attention to grep.extendedRegexp
471 test_pattern_type "$H" "$HC" "$L" BRE \
472 -c grep.extendedRegexp=true \
473 -c grep.patternType=basic \
474 -c grep.extendedRegexp=false
476 # grep.patternType=extended pays no attention to grep.extendedRegexp
477 test_pattern_type "$H" "$HC" "$L" ERE \
478 -c grep.extendedRegexp=true \
479 -c grep.patternType=extended \
480 -c grep.extendedRegexp=false
482 # grep.extendedRegexp is used with a last-one-wins grep.patternType=default
483 test_pattern_type "$H" "$HC" "$L" ERE \
484 -c grep.patternType=fixed \
485 -c grep.extendedRegexp=true \
486 -c grep.patternType=default
488 # grep.extendedRegexp is used with earlier grep.patternType=default
489 test_pattern_type "$H" "$HC" "$L" ERE \
490 -c grep.extendedRegexp=false \
491 -c grep.patternType=default \
492 -c grep.extendedRegexp=true
494 # grep.extendedRegexp is used with a last-one-loses grep.patternType=default
495 test_pattern_type "$H" "$HC" "$L" ERE \
496 -c grep.extendedRegexp=false \
497 -c grep.extendedRegexp=true \
498 -c grep.patternType=default
500 # grep.extendedRegexp and grep.patternType are both last-one-wins independently
501 test_pattern_type "$H" "$HC" "$L" BRE \
502 -c grep.patternType=default \
503 -c grep.extendedRegexp=true \
504 -c grep.patternType=basic
506 # grep.patternType=extended and grep.patternType=default
507 test_pattern_type "$H" "$HC" "$L" BRE \
508 -c grep.patternType=extended \
509 -c grep.patternType=default
511 # grep.patternType=[extended -> default -> fixed] (BRE)" '
512 test_pattern_type "$H" "$HC" "$L" FIX \
513 -c grep.patternType=extended \
514 -c grep.patternType=default \
515 -c grep.patternType=fixed
517 test_expect_success "grep --count $L" '
518 echo ${HC}ab:3 >expected &&
519 git grep --count -e b $H -- ab >actual &&
520 test_cmp expected actual
523 test_expect_success "grep --count -h $L" '
524 echo 3 >expected &&
525 git grep --count -h -e b $H -- ab >actual &&
526 test_cmp expected actual
529 test_expect_success FUNNYNAMES "grep $L should quote unusual pathnames" '
530 cat >expected <<-EOF &&
531 ${HC}"\"unusual\" pathname":unusual
532 ${HC}"t/nested \"unusual\" pathname":unusual
534 git grep unusual $H >actual &&
535 test_cmp expected actual
538 test_expect_success FUNNYNAMES "grep $L in subdir should quote unusual relative pathnames" '
539 cat >expected <<-EOF &&
540 ${HC}"nested \"unusual\" pathname":unusual
543 cd t &&
544 git grep unusual $H
545 ) >actual &&
546 test_cmp expected actual
549 test_expect_success FUNNYNAMES "grep -z $L with unusual pathnames" '
550 cat >expected <<-EOF &&
551 ${HC}"unusual" pathname:unusual
552 ${HC}t/nested "unusual" pathname:unusual
554 git grep -z unusual $H >actual &&
555 tr "\0" ":" <actual >actual-replace-null &&
556 test_cmp expected actual-replace-null
559 test_expect_success FUNNYNAMES "grep -z $L in subdir with unusual relative pathnames" '
560 cat >expected <<-EOF &&
561 ${HC}nested "unusual" pathname:unusual
564 cd t &&
565 git grep -z unusual $H
566 ) >actual &&
567 tr "\0" ":" <actual >actual-replace-null &&
568 test_cmp expected actual-replace-null
570 done
572 cat >expected <<EOF
573 file
575 test_expect_success 'grep -l -C' '
576 git grep -l -C1 foo >actual &&
577 test_cmp expected actual
580 cat >expected <<EOF
581 file:5
583 test_expect_success 'grep -c -C' '
584 git grep -c -C1 foo >actual &&
585 test_cmp expected actual
588 test_expect_success 'grep -L -C' '
589 git ls-files >expected &&
590 git grep -L -C1 nonexistent_string >actual &&
591 test_cmp expected actual
594 test_expect_success 'grep --files-without-match --quiet' '
595 git grep --files-without-match --quiet nonexistent_string >actual &&
596 test_must_be_empty actual
599 test_expect_success 'grep --max-count 0 (must exit with non-zero)' '
600 test_must_fail git grep --max-count 0 foo >actual &&
601 test_must_be_empty actual
604 test_expect_success 'grep --max-count 3' '
605 cat >expected <<-EOF &&
606 file:foo mmap bar
607 file:foo_mmap bar
608 file:foo_mmap bar mmap
610 git grep --max-count 3 foo >actual &&
611 test_cmp expected actual
614 test_expect_success 'grep --max-count -1 (no limit)' '
615 cat >expected <<-EOF &&
616 file:foo mmap bar
617 file:foo_mmap bar
618 file:foo_mmap bar mmap
619 file:foo mmap bar_mmap
620 file:foo_mmap bar mmap baz
622 git grep --max-count -1 foo >actual &&
623 test_cmp expected actual
626 test_expect_success 'grep --max-count 1 --context 2' '
627 cat >expected <<-EOF &&
628 file-foo mmap bar
629 file:foo_mmap bar
630 file-foo_mmap bar mmap
632 git grep --max-count 1 --context 1 foo_mmap >actual &&
633 test_cmp expected actual
636 test_expect_success 'grep --max-count 1 --show-function' '
637 cat >expected <<-EOF &&
638 hello.ps1=function hello() {
639 hello.ps1: echo "Hello world."
641 git grep --max-count 1 --show-function Hello hello.ps1 >actual &&
642 test_cmp expected actual
645 test_expect_success 'grep --max-count 2 --show-function' '
646 cat >expected <<-EOF &&
647 hello.ps1=function hello() {
648 hello.ps1: echo "Hello world."
649 hello.ps1: echo "Hello again."
651 git grep --max-count 2 --show-function Hello hello.ps1 >actual &&
652 test_cmp expected actual
655 test_expect_success 'grep --max-count 1 --count' '
656 cat >expected <<-EOF &&
657 hello.ps1:1
659 git grep --max-count 1 --count Hello hello.ps1 >actual &&
660 test_cmp expected actual
663 test_expect_success 'grep --max-count 1 (multiple files)' '
664 cat >expected <<-EOF &&
665 hello.c:#include <stdio.h>
666 hello.ps1:# No-op.
668 git grep --max-count 1 -e o -- hello.\* >actual &&
669 test_cmp expected actual
672 test_expect_success 'grep --max-count 1 --context 1 (multiple files)' '
673 cat >expected <<-EOF &&
674 hello.c-#include <assert.h>
675 hello.c:#include <stdio.h>
676 hello.c-
678 hello.ps1:# No-op.
679 hello.ps1-function dummy() {}
681 git grep --max-count 1 --context 1 -e o -- hello.\* >actual &&
682 test_cmp expected actual
685 cat >expected <<EOF
686 file:foo mmap bar_mmap
689 test_expect_success 'grep -e A --and -e B' '
690 git grep -e "foo mmap" --and -e bar_mmap >actual &&
691 test_cmp expected actual
694 cat >expected <<EOF
695 file:foo_mmap bar mmap
696 file:foo_mmap bar mmap baz
700 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
701 git grep \( -e foo_ --or -e baz \) \
702 --and -e " mmap" >actual &&
703 test_cmp expected actual
706 cat >expected <<EOF
707 file:foo mmap bar
710 test_expect_success 'grep -e A --and --not -e B' '
711 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
712 test_cmp expected actual
715 test_expect_success 'grep should ignore GREP_OPTIONS' '
716 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
717 test_cmp expected actual
720 test_expect_success 'grep -f, non-existent file' '
721 test_must_fail git grep -f patterns
724 cat >expected <<EOF
725 file:foo mmap bar
726 file:foo_mmap bar
727 file:foo_mmap bar mmap
728 file:foo mmap bar_mmap
729 file:foo_mmap bar mmap baz
732 cat >pattern <<EOF
733 mmap
736 test_expect_success 'grep -f, one pattern' '
737 git grep -f pattern >actual &&
738 test_cmp expected actual
741 cat >expected <<EOF
742 file:foo mmap bar
743 file:foo_mmap bar
744 file:foo_mmap bar mmap
745 file:foo mmap bar_mmap
746 file:foo_mmap bar mmap baz
747 t/a/v:vvv
748 t/v:vvv
749 v:vvv
752 cat >patterns <<EOF
753 mmap
757 test_expect_success 'grep -f, multiple patterns' '
758 git grep -f patterns >actual &&
759 test_cmp expected actual
762 test_expect_success 'grep, multiple patterns' '
763 git grep "$(cat patterns)" >actual &&
764 test_cmp expected actual
767 cat >expected <<EOF
768 file:foo mmap bar
769 file:foo_mmap bar
770 file:foo_mmap bar mmap
771 file:foo mmap bar_mmap
772 file:foo_mmap bar mmap baz
773 t/a/v:vvv
774 t/v:vvv
775 v:vvv
778 cat >patterns <<EOF
780 mmap
786 test_expect_success 'grep -f, ignore empty lines' '
787 git grep -f patterns >actual &&
788 test_cmp expected actual
791 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
792 git grep -f - <patterns >actual &&
793 test_cmp expected actual
796 cat >expected <<EOF
797 y:y yy
799 z:zzz
802 test_expect_success 'grep -q, silently report matches' '
803 git grep -q mmap >actual &&
804 test_must_be_empty actual &&
805 test_must_fail git grep -q qfwfq >actual &&
806 test_must_be_empty actual
809 test_expect_success 'grep -C1 hunk mark between files' '
810 git grep -C1 "^[yz]" >actual &&
811 test_cmp expected actual
814 test_expect_success 'log grep setup' '
815 test_commit --append --author "With * Asterisk <xyzzy@frotz.com>" second file a &&
816 test_commit --append third file a &&
817 test_commit --append --author "Night Fall <nitfol@frobozz.com>" fourth file a
820 test_expect_success 'log grep (1)' '
821 git log --author=author --pretty=tformat:%s >actual &&
823 echo third && echo initial
824 } >expect &&
825 test_cmp expect actual
828 test_expect_success 'log grep (2)' '
829 git log --author=" * " -F --pretty=tformat:%s >actual &&
831 echo second
832 } >expect &&
833 test_cmp expect actual
836 test_expect_success 'log grep (3)' '
837 git log --author="^A U" --pretty=tformat:%s >actual &&
839 echo third && echo initial
840 } >expect &&
841 test_cmp expect actual
844 test_expect_success 'log grep (4)' '
845 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
847 echo second
848 } >expect &&
849 test_cmp expect actual
852 test_expect_success 'log grep (5)' '
853 git log --author=Thor -F --pretty=tformat:%s >actual &&
855 echo third && echo initial
856 } >expect &&
857 test_cmp expect actual
860 test_expect_success 'log grep (6)' '
861 git log --author=-0700 --pretty=tformat:%s >actual &&
862 test_must_be_empty actual
865 test_expect_success 'log grep (7)' '
866 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
867 echo third >expect &&
868 test_cmp expect actual
871 test_expect_success 'log grep (8)' '
872 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
874 echo third && echo second
875 } >expect &&
876 test_cmp expect actual
879 test_expect_success 'log grep (9)' '
880 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
881 echo third >expect &&
882 test_cmp expect actual
885 test_expect_success 'log grep (9)' '
886 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
887 test_must_be_empty actual
890 test_expect_success 'log --grep-reflog can only be used under -g' '
891 test_must_fail git log --grep-reflog="commit: third"
894 test_expect_success 'log with multiple --grep uses union' '
895 git log --grep=i --grep=r --format=%s >actual &&
897 echo fourth && echo third && echo initial
898 } >expect &&
899 test_cmp expect actual
902 test_expect_success 'log --all-match with multiple --grep uses intersection' '
903 git log --all-match --grep=i --grep=r --format=%s >actual &&
905 echo third
906 } >expect &&
907 test_cmp expect actual
910 test_expect_success 'log with multiple --author uses union' '
911 git log --author="Thor" --author="Aster" --format=%s >actual &&
913 echo third && echo second && echo initial
914 } >expect &&
915 test_cmp expect actual
918 test_expect_success 'log --all-match with multiple --author still uses union' '
919 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
921 echo third && echo second && echo initial
922 } >expect &&
923 test_cmp expect actual
926 test_expect_success 'log --grep --author uses intersection' '
927 # grep matches only third and fourth
928 # author matches only initial and third
929 git log --author="A U Thor" --grep=r --format=%s >actual &&
931 echo third
932 } >expect &&
933 test_cmp expect actual
936 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
937 # grep matches initial and second but not third
938 # author matches only initial and third
939 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
941 echo initial
942 } >expect &&
943 test_cmp expect actual
946 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
947 # grep matches only initial and third
948 # author matches all but second
949 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
951 echo third && echo initial
952 } >expect &&
953 test_cmp expect actual
956 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
957 # grep matches only initial and third
958 # author matches all but second
959 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
961 echo third && echo initial
962 } >expect &&
963 test_cmp expect actual
966 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
967 # grep matches only third
968 # author matches only initial and third
969 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
971 echo third
972 } >expect &&
973 test_cmp expect actual
976 test_expect_success 'log --author does not search in timestamp' '
977 git log --author="$GIT_AUTHOR_DATE" >actual &&
978 test_must_be_empty actual
981 test_expect_success 'log --committer does not search in timestamp' '
982 git log --committer="$GIT_COMMITTER_DATE" >actual &&
983 test_must_be_empty actual
986 test_expect_success 'grep with CE_VALID file' '
987 git update-index --assume-unchanged t/t &&
988 rm t/t &&
989 test "$(git grep test)" = "t/t:test" &&
990 git update-index --no-assume-unchanged t/t &&
991 git checkout t/t
994 cat >expected <<EOF
995 hello.c=#include <stdio.h>
996 hello.c: return 0;
999 test_expect_success 'grep -p with userdiff' '
1000 git config diff.custom.funcname "^#" &&
1001 echo "hello.c diff=custom" >.gitattributes &&
1002 git grep -p return >actual &&
1003 test_cmp expected actual
1006 cat >expected <<EOF
1007 hello.c=int main(int argc, const char **argv)
1008 hello.c: return 0;
1011 test_expect_success 'grep -p' '
1012 rm -f .gitattributes &&
1013 git grep -p return >actual &&
1014 test_cmp expected actual
1017 cat >expected <<EOF
1018 hello.c-#include <stdio.h>
1019 hello.c-
1020 hello.c=int main(int argc, const char **argv)
1021 hello.c-{
1022 hello.c- printf("Hello world.\n");
1023 hello.c: return 0;
1026 test_expect_success 'grep -p -B5' '
1027 git grep -p -B5 return >actual &&
1028 test_cmp expected actual
1031 cat >expected <<EOF
1032 hello.c=int main(int argc, const char **argv)
1033 hello.c-{
1034 hello.c- printf("Hello world.\n");
1035 hello.c: return 0;
1036 hello.c- /* char ?? */
1037 hello.c-}
1040 test_expect_success 'grep -W' '
1041 git grep -W return >actual &&
1042 test_cmp expected actual
1045 cat >expected <<EOF
1046 hello.c-#include <assert.h>
1047 hello.c:#include <stdio.h>
1050 test_expect_success 'grep -W shows no trailing empty lines' '
1051 git grep -W stdio >actual &&
1052 test_cmp expected actual
1055 test_expect_success 'grep -W with userdiff' '
1056 test_when_finished "rm -f .gitattributes" &&
1057 git config diff.custom.xfuncname "^function .*$" &&
1058 echo "hello.ps1 diff=custom" >.gitattributes &&
1059 git grep -W echo >function-context-userdiff-actual
1062 test_expect_success ' includes preceding comment' '
1063 grep "# Say hello" function-context-userdiff-actual
1066 test_expect_success ' includes function line' '
1067 grep "=function hello" function-context-userdiff-actual
1070 test_expect_success ' includes matching line' '
1071 grep ": echo" function-context-userdiff-actual
1074 test_expect_success ' includes last line of the function' '
1075 grep "} # hello" function-context-userdiff-actual
1078 for threads in $(test_seq 0 10)
1080 test_expect_success "grep --threads=$threads & -c grep.threads=$threads" "
1081 git grep --threads=$threads . >actual.$threads &&
1082 if test $threads -ge 1
1083 then
1084 test_cmp actual.\$(($threads - 1)) actual.$threads
1085 fi &&
1086 git -c grep.threads=$threads grep . >actual.$threads &&
1087 if test $threads -ge 1
1088 then
1089 test_cmp actual.\$(($threads - 1)) actual.$threads
1092 done
1094 test_expect_success !PTHREADS,!FAIL_PREREQS \
1095 'grep --threads=N or pack.threads=N warns when no pthreads' '
1096 git grep --threads=2 Hello hello_world 2>err &&
1097 grep ^warning: err >warnings &&
1098 test_line_count = 1 warnings &&
1099 grep -F "no threads support, ignoring --threads" err &&
1100 git -c grep.threads=2 grep Hello hello_world 2>err &&
1101 grep ^warning: err >warnings &&
1102 test_line_count = 1 warnings &&
1103 grep -F "no threads support, ignoring grep.threads" err &&
1104 git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
1105 grep ^warning: err >warnings &&
1106 test_line_count = 2 warnings &&
1107 grep -F "no threads support, ignoring --threads" err &&
1108 grep -F "no threads support, ignoring grep.threads" err &&
1109 git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
1110 test_line_count = 0 err
1113 test_expect_success 'grep from a subdirectory to search wider area (1)' '
1114 mkdir -p s &&
1116 cd s && git grep "x x x" ..
1120 test_expect_success 'grep from a subdirectory to search wider area (2)' '
1121 mkdir -p s &&
1123 cd s &&
1124 test_expect_code 1 git grep xxyyzz .. >out &&
1125 test_must_be_empty out
1129 cat >expected <<EOF
1130 hello.c:int main(int argc, const char **argv)
1133 test_expect_success 'grep -Fi' '
1134 git grep -Fi "CHAR *" >actual &&
1135 test_cmp expected actual
1138 test_expect_success 'outside of git repository' '
1139 rm -fr non &&
1140 mkdir -p non/git/sub &&
1141 echo hello >non/git/file1 &&
1142 echo world >non/git/sub/file2 &&
1144 echo file1:hello &&
1145 echo sub/file2:world
1146 } >non/expect.full &&
1147 echo file2:world >non/expect.sub &&
1149 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1150 export GIT_CEILING_DIRECTORIES &&
1151 cd non/git &&
1152 test_must_fail git grep o &&
1153 git grep --no-index o >../actual.full &&
1154 test_cmp ../expect.full ../actual.full &&
1155 cd sub &&
1156 test_must_fail git grep o &&
1157 git grep --no-index o >../../actual.sub &&
1158 test_cmp ../../expect.sub ../../actual.sub
1159 ) &&
1161 echo ".*o*" >non/git/.gitignore &&
1163 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1164 export GIT_CEILING_DIRECTORIES &&
1165 cd non/git &&
1166 test_must_fail git grep o &&
1167 git grep --no-index --exclude-standard o >../actual.full &&
1168 test_cmp ../expect.full ../actual.full &&
1171 echo ".gitignore:.*o*" &&
1172 cat ../expect.full
1173 } >../expect.with.ignored &&
1174 git grep --no-index --no-exclude-standard o >../actual.full &&
1175 test_cmp ../expect.with.ignored ../actual.full
1179 test_expect_success 'outside of git repository with fallbackToNoIndex' '
1180 rm -fr non &&
1181 mkdir -p non/git/sub &&
1182 echo hello >non/git/file1 &&
1183 echo world >non/git/sub/file2 &&
1184 cat <<-\EOF >non/expect.full &&
1185 file1:hello
1186 sub/file2:world
1188 echo file2:world >non/expect.sub &&
1190 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1191 export GIT_CEILING_DIRECTORIES &&
1192 cd non/git &&
1193 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1194 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
1195 test_cmp ../expect.full ../actual.full &&
1196 cd sub &&
1197 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1198 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
1199 test_cmp ../../expect.sub ../../actual.sub
1200 ) &&
1202 echo ".*o*" >non/git/.gitignore &&
1204 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1205 export GIT_CEILING_DIRECTORIES &&
1206 cd non/git &&
1207 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1208 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
1209 test_cmp ../expect.full ../actual.full &&
1212 echo ".gitignore:.*o*" &&
1213 cat ../expect.full
1214 } >../expect.with.ignored &&
1215 git -c grep.fallbackToNoIndex grep --no-exclude-standard o >../actual.full &&
1216 test_cmp ../expect.with.ignored ../actual.full
1220 test_expect_success 'inside git repository but with --no-index' '
1221 rm -fr is &&
1222 mkdir -p is/git/sub &&
1223 echo hello >is/git/file1 &&
1224 echo world >is/git/sub/file2 &&
1225 echo ".*o*" >is/git/.gitignore &&
1227 echo file1:hello &&
1228 echo sub/file2:world
1229 } >is/expect.unignored &&
1231 echo ".gitignore:.*o*" &&
1232 cat is/expect.unignored
1233 } >is/expect.full &&
1234 echo file2:world >is/expect.sub &&
1236 cd is/git &&
1237 git init &&
1238 test_must_fail git grep o >../actual.full &&
1239 test_must_be_empty ../actual.full &&
1241 git grep --untracked o >../actual.unignored &&
1242 test_cmp ../expect.unignored ../actual.unignored &&
1244 git grep --no-index o >../actual.full &&
1245 test_cmp ../expect.full ../actual.full &&
1247 git grep --no-index --exclude-standard o >../actual.unignored &&
1248 test_cmp ../expect.unignored ../actual.unignored &&
1250 cd sub &&
1251 test_must_fail git grep o >../../actual.sub &&
1252 test_must_be_empty ../../actual.sub &&
1254 git grep --no-index o >../../actual.sub &&
1255 test_cmp ../../expect.sub ../../actual.sub &&
1257 git grep --untracked o >../../actual.sub &&
1258 test_cmp ../../expect.sub ../../actual.sub
1262 test_expect_success 'grep --no-index descends into repos, but not .git' '
1263 rm -fr non &&
1264 mkdir -p non/git &&
1266 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1267 export GIT_CEILING_DIRECTORIES &&
1268 cd non/git &&
1270 echo magic >file &&
1271 git init repo &&
1273 cd repo &&
1274 echo magic >file &&
1275 git add file &&
1276 git commit -m foo &&
1277 echo magic >.git/file
1278 ) &&
1280 cat >expect <<-\EOF &&
1281 file
1282 repo/file
1284 git grep -l --no-index magic >actual &&
1285 test_cmp expect actual
1289 test_expect_success 'setup double-dash tests' '
1290 cat >double-dash <<EOF &&
1293 other
1295 git add double-dash
1298 cat >expected <<EOF
1299 double-dash:->
1301 test_expect_success 'grep -- pattern' '
1302 git grep -- "->" >actual &&
1303 test_cmp expected actual
1305 test_expect_success 'grep -- pattern -- pathspec' '
1306 git grep -- "->" -- double-dash >actual &&
1307 test_cmp expected actual
1309 test_expect_success 'grep -e pattern -- path' '
1310 git grep -e "->" -- double-dash >actual &&
1311 test_cmp expected actual
1314 cat >expected <<EOF
1315 double-dash:--
1317 test_expect_success 'grep -e -- -- path' '
1318 git grep -e -- -- double-dash >actual &&
1319 test_cmp expected actual
1322 test_expect_success 'dashdash disambiguates rev as rev' '
1323 test_when_finished "rm -f main" &&
1324 echo content >main &&
1325 echo main:hello.c >expect &&
1326 git grep -l o main -- hello.c >actual &&
1327 test_cmp expect actual
1330 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
1331 test_when_finished "git rm -f main" &&
1332 echo content >main &&
1333 git add main &&
1334 echo main:content >expect &&
1335 git grep o -- main >actual &&
1336 test_cmp expect actual
1339 test_expect_success 'report bogus arg without dashdash' '
1340 test_must_fail git grep o does-not-exist
1343 test_expect_success 'report bogus rev with dashdash' '
1344 test_must_fail git grep o hello.c --
1347 test_expect_success 'allow non-existent path with dashdash' '
1348 # We need a real match so grep exits with success.
1349 tree=$(git ls-tree HEAD |
1350 sed s/hello.c/not-in-working-tree/ |
1351 git mktree) &&
1352 git grep o "$tree" -- not-in-working-tree
1355 test_expect_success 'grep --no-index pattern -- path' '
1356 rm -fr non &&
1357 mkdir -p non/git &&
1359 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1360 export GIT_CEILING_DIRECTORIES &&
1361 cd non/git &&
1362 echo hello >hello &&
1363 echo goodbye >goodbye &&
1364 echo hello:hello >expect &&
1365 git grep --no-index o -- hello >actual &&
1366 test_cmp expect actual
1370 test_expect_success 'grep --no-index complains of revs' '
1371 test_must_fail git grep --no-index o main -- 2>err &&
1372 test_i18ngrep "cannot be used with revs" err
1375 test_expect_success 'grep --no-index prefers paths to revs' '
1376 test_when_finished "rm -f main" &&
1377 echo content >main &&
1378 echo main:content >expect &&
1379 git grep --no-index o main >actual &&
1380 test_cmp expect actual
1383 test_expect_success 'grep --no-index does not "diagnose" revs' '
1384 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1385 test_i18ngrep ! -i "did you mean" err
1388 cat >expected <<EOF
1389 hello.c:int main(int argc, const char **argv)
1390 hello.c: printf("Hello world.\n");
1393 test_expect_success PCRE 'grep --perl-regexp pattern' '
1394 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1395 test_cmp expected actual
1398 test_expect_success !FAIL_PREREQS,!PCRE 'grep --perl-regexp pattern errors without PCRE' '
1399 test_must_fail git grep --perl-regexp "foo.*bar"
1402 test_expect_success PCRE 'grep -P pattern' '
1403 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1404 test_cmp expected actual
1407 test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
1408 git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1409 test_cmp expected actual
1413 test_expect_success !FAIL_PREREQS,!PCRE 'grep -P pattern errors without PCRE' '
1414 test_must_fail git grep -P "foo.*bar"
1417 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1418 test_must_fail git -c grep.extendedregexp=true \
1419 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1420 test_must_be_empty actual
1423 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1424 git -c grep.extendedregexp=true \
1425 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1426 test_cmp expected actual
1429 test_expect_success PCRE 'grep -P -v pattern' '
1430 cat >expected <<-\EOF &&
1431 ab:a+b*c
1432 ab:a+bc
1434 git grep -P -v "abc" ab >actual &&
1435 test_cmp expected actual
1438 test_expect_success PCRE 'grep -P -i pattern' '
1439 cat >expected <<-EOF &&
1440 hello.c: printf("Hello world.\n");
1442 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1443 test_cmp expected actual
1446 test_expect_success PCRE 'grep -P -w pattern' '
1447 cat >expected <<-\EOF &&
1448 hello_world:Hello world
1449 hello_world:HeLLo world
1451 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1452 test_cmp expected actual
1455 test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1456 git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1457 test_cmp hello_world actual &&
1458 git grep -P -h "(.)\1" hello_world >actual &&
1459 test_cmp hello_world actual
1462 test_expect_success 'grep -G invalidpattern properly dies ' '
1463 test_must_fail git grep -G "a["
1466 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1467 test_must_fail git -c grep.patterntype=basic grep "a["
1470 test_expect_success 'grep -E invalidpattern properly dies ' '
1471 test_must_fail git grep -E "a["
1474 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1475 test_must_fail git -c grep.patterntype=extended grep "a["
1478 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1479 test_must_fail git grep -P "a["
1482 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1483 test_must_fail git -c grep.patterntype=perl grep "a["
1486 test_expect_success 'grep -G -E -F pattern' '
1487 echo "ab:a+b*c" >expected &&
1488 git grep -G -E -F "a+b*c" ab >actual &&
1489 test_cmp expected actual
1492 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1493 echo "ab:a+b*c" >expected &&
1494 git \
1495 -c grep.patterntype=basic \
1496 -c grep.patterntype=extended \
1497 -c grep.patterntype=fixed \
1498 grep "a+b*c" ab >actual &&
1499 test_cmp expected actual
1502 test_expect_success 'grep -E -F -G pattern' '
1503 echo "ab:a+bc" >expected &&
1504 git grep -E -F -G "a+b*c" ab >actual &&
1505 test_cmp expected actual
1508 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1509 echo "ab:a+bc" >expected &&
1510 git \
1511 -c grep.patterntype=extended \
1512 -c grep.patterntype=fixed \
1513 -c grep.patterntype=basic \
1514 grep "a+b*c" ab >actual &&
1515 test_cmp expected actual
1518 test_expect_success 'grep -F -G -E pattern' '
1519 echo "ab:abc" >expected &&
1520 git grep -F -G -E "a+b*c" ab >actual &&
1521 test_cmp expected actual
1524 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1525 echo "ab:abc" >expected &&
1526 git \
1527 -c grep.patterntype=fixed \
1528 -c grep.patterntype=basic \
1529 -c grep.patterntype=extended \
1530 grep "a+b*c" ab >actual &&
1531 test_cmp expected actual
1534 test_expect_success 'grep -G -F -P -E pattern' '
1535 echo "d0:d" >expected &&
1536 git grep -G -F -P -E "[\d]" d0 >actual &&
1537 test_cmp expected actual
1540 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1541 echo "d0:d" >expected &&
1542 git \
1543 -c grep.patterntype=fixed \
1544 -c grep.patterntype=basic \
1545 -c grep.patterntype=perl \
1546 -c grep.patterntype=extended \
1547 grep "[\d]" d0 >actual &&
1548 test_cmp expected actual
1551 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1552 echo "d0:0" >expected &&
1553 git grep -G -F -E -P "[\d]" d0 >actual &&
1554 test_cmp expected actual
1557 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1558 echo "d0:0" >expected &&
1559 git \
1560 -c grep.patterntype=fixed \
1561 -c grep.patterntype=basic \
1562 -c grep.patterntype=extended \
1563 -c grep.patterntype=perl \
1564 grep "[\d]" d0 >actual &&
1565 test_cmp expected actual
1568 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1569 echo "ab:a+b*c" >expected &&
1570 git \
1571 -c grep.patterntype=fixed \
1572 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1573 test_cmp expected actual
1576 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1577 echo "ab:a+b*c" >expected &&
1578 git \
1579 -c grep.patterntype=basic \
1580 grep -F "*c" ab >actual &&
1581 test_cmp expected actual
1584 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1585 cat >expected <<-\EOF &&
1586 ab:a+b*c
1587 ab:a+bc
1589 git \
1590 -c grep.patterntype=fixed \
1591 grep -G "a+b" ab >actual &&
1592 test_cmp expected actual
1595 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1596 cat >expected <<-\EOF &&
1597 ab:a+b*c
1598 ab:a+bc
1599 ab:abc
1601 git \
1602 -c grep.patterntype=fixed \
1603 grep -E "a+" ab >actual &&
1604 test_cmp expected actual
1607 cat >expected <<EOF
1608 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1609 hello.c<RED>-<RESET>{
1610 <RED>--<RESET>
1611 hello.c<RED>:<RESET> /* char ?? */
1612 hello.c<RED>-<RESET>}
1613 <RED>--<RESET>
1614 hello_world<RED>:<RESET>Hello_world
1615 hello_world<RED>-<RESET>HeLLo_world
1618 test_expect_success 'grep --color, separator' '
1619 test_config color.grep.context normal &&
1620 test_config color.grep.filename normal &&
1621 test_config color.grep.function normal &&
1622 test_config color.grep.linenumber normal &&
1623 test_config color.grep.match normal &&
1624 test_config color.grep.selected normal &&
1625 test_config color.grep.separator red &&
1627 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1628 test_decode_color >actual &&
1629 test_cmp expected actual
1632 cat >expected <<EOF
1633 hello.c:int main(int argc, const char **argv)
1634 hello.c: /* char ?? */
1636 hello_world:Hello_world
1639 test_expect_success 'grep --break' '
1640 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1641 test_cmp expected actual
1644 cat >expected <<EOF
1645 hello.c:int main(int argc, const char **argv)
1646 hello.c-{
1648 hello.c: /* char ?? */
1649 hello.c-}
1651 hello_world:Hello_world
1652 hello_world-HeLLo_world
1655 test_expect_success 'grep --break with context' '
1656 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1657 test_cmp expected actual
1660 cat >expected <<EOF
1661 hello.c
1662 int main(int argc, const char **argv)
1663 /* char ?? */
1664 hello_world
1665 Hello_world
1668 test_expect_success 'grep --heading' '
1669 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1670 test_cmp expected actual
1673 cat >expected <<EOF
1674 <BOLD;GREEN>hello.c<RESET>
1675 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1676 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1678 <BOLD;GREEN>hello_world<RESET>
1679 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1682 test_expect_success 'mimic ack-grep --group' '
1683 test_config color.grep.context normal &&
1684 test_config color.grep.filename "bold green" &&
1685 test_config color.grep.function normal &&
1686 test_config color.grep.linenumber normal &&
1687 test_config color.grep.match "black yellow" &&
1688 test_config color.grep.selected normal &&
1689 test_config color.grep.separator normal &&
1691 git grep --break --heading -n --color \
1692 -e char -e lo_w hello.c hello_world |
1693 test_decode_color >actual &&
1694 test_cmp expected actual
1697 cat >expected <<EOF
1698 space: line with leading space1
1699 space: line with leading space2
1700 space: line with leading space3
1703 test_expect_success PCRE 'grep -E "^ "' '
1704 git grep -E "^ " space >actual &&
1705 test_cmp expected actual
1708 test_expect_success PCRE 'grep -P "^ "' '
1709 git grep -P "^ " space >actual &&
1710 test_cmp expected actual
1713 cat >expected <<EOF
1714 space-line without leading space1
1715 space: line <RED>with <RESET>leading space1
1716 space: line <RED>with <RESET>leading <RED>space2<RESET>
1717 space: line <RED>with <RESET>leading space3
1718 space:line without leading <RED>space2<RESET>
1721 test_expect_success 'grep --color -e A -e B with context' '
1722 test_config color.grep.context normal &&
1723 test_config color.grep.filename normal &&
1724 test_config color.grep.function normal &&
1725 test_config color.grep.linenumber normal &&
1726 test_config color.grep.matchContext normal &&
1727 test_config color.grep.matchSelected red &&
1728 test_config color.grep.selected normal &&
1729 test_config color.grep.separator normal &&
1731 git grep --color=always -C2 -e "with " -e space2 space |
1732 test_decode_color >actual &&
1733 test_cmp expected actual
1736 cat >expected <<EOF
1737 space-line without leading space1
1738 space- line with leading space1
1739 space: line <RED>with <RESET>leading <RED>space2<RESET>
1740 space- line with leading space3
1741 space-line without leading space2
1744 test_expect_success 'grep --color -e A --and -e B with context' '
1745 test_config color.grep.context normal &&
1746 test_config color.grep.filename normal &&
1747 test_config color.grep.function normal &&
1748 test_config color.grep.linenumber normal &&
1749 test_config color.grep.matchContext normal &&
1750 test_config color.grep.matchSelected red &&
1751 test_config color.grep.selected normal &&
1752 test_config color.grep.separator normal &&
1754 git grep --color=always -C2 -e "with " --and -e space2 space |
1755 test_decode_color >actual &&
1756 test_cmp expected actual
1759 cat >expected <<EOF
1760 space-line without leading space1
1761 space: line <RED>with <RESET>leading space1
1762 space- line with leading space2
1763 space: line <RED>with <RESET>leading space3
1764 space-line without leading space2
1767 test_expect_success 'grep --color -e A --and --not -e B with context' '
1768 test_config color.grep.context normal &&
1769 test_config color.grep.filename normal &&
1770 test_config color.grep.function normal &&
1771 test_config color.grep.linenumber normal &&
1772 test_config color.grep.matchContext normal &&
1773 test_config color.grep.matchSelected red &&
1774 test_config color.grep.selected normal &&
1775 test_config color.grep.separator normal &&
1777 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1778 test_decode_color >actual &&
1779 test_cmp expected actual
1782 cat >expected <<EOF
1783 hello.c-
1784 hello.c=int main(int argc, const char **argv)
1785 hello.c-{
1786 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1787 hello.c- return 0;
1788 hello.c- /* char ?? */
1789 hello.c-}
1792 test_expect_success 'grep --color -e A --and -e B -p with context' '
1793 test_config color.grep.context normal &&
1794 test_config color.grep.filename normal &&
1795 test_config color.grep.function normal &&
1796 test_config color.grep.linenumber normal &&
1797 test_config color.grep.matchContext normal &&
1798 test_config color.grep.matchSelected red &&
1799 test_config color.grep.selected normal &&
1800 test_config color.grep.separator normal &&
1802 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1803 test_decode_color >actual &&
1804 test_cmp expected actual
1807 test_expect_success 'grep can find things only in the work tree' '
1808 : >work-tree-only &&
1809 git add work-tree-only &&
1810 test_when_finished "git rm -f work-tree-only" &&
1811 echo "find in work tree" >work-tree-only &&
1812 git grep --quiet "find in work tree" &&
1813 test_must_fail git grep --quiet --cached "find in work tree" &&
1814 test_must_fail git grep --quiet "find in work tree" HEAD
1817 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1818 echo "intend to add this" >intend-to-add &&
1819 git add -N intend-to-add &&
1820 test_when_finished "git rm -f intend-to-add" &&
1821 git grep --quiet "intend to add this" &&
1822 test_must_fail git grep --quiet --cached "intend to add this" &&
1823 test_must_fail git grep --quiet "intend to add this" HEAD
1826 test_expect_success 'grep does not search work tree with assume unchanged' '
1827 echo "intend to add this" >intend-to-add &&
1828 git add -N intend-to-add &&
1829 git update-index --assume-unchanged intend-to-add &&
1830 test_when_finished "git rm -f intend-to-add" &&
1831 test_must_fail git grep --quiet "intend to add this" &&
1832 test_must_fail git grep --quiet --cached "intend to add this" &&
1833 test_must_fail git grep --quiet "intend to add this" HEAD
1836 test_expect_success 'grep can find things only in the index' '
1837 echo "only in the index" >cache-this &&
1838 git add cache-this &&
1839 rm cache-this &&
1840 test_when_finished "git rm --cached cache-this" &&
1841 test_must_fail git grep --quiet "only in the index" &&
1842 git grep --quiet --cached "only in the index" &&
1843 test_must_fail git grep --quiet "only in the index" HEAD
1846 test_expect_success 'grep does not report i-t-a with -L --cached' '
1847 echo "intend to add this" >intend-to-add &&
1848 git add -N intend-to-add &&
1849 test_when_finished "git rm -f intend-to-add" &&
1850 git ls-files | grep -v "^intend-to-add\$" >expected &&
1851 git grep -L --cached "nonexistent_string" >actual &&
1852 test_cmp expected actual
1855 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1856 echo "intend to add this" >intend-to-add-assume-unchanged &&
1857 git add -N intend-to-add-assume-unchanged &&
1858 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1859 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1860 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1861 git grep -L "nonexistent_string" >actual &&
1862 test_cmp expected actual
1865 test_done