Merge branch 'rj/cygwin-has-dev-tty'
[git/gitster.git] / t / t7810-grep.sh
blobaf2cf2f78ab890662c3743ae407aa6ac101b5d8d
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 LC_ALL=en_US.UTF-8 test-tool regex '^.$' '¿' &&
22 test_set_prereq MB_REGEX
24 cat >hello.c <<EOF
25 #include <assert.h>
26 #include <stdio.h>
28 int main(int argc, const char **argv)
30 printf("Hello world.\n");
31 return 0;
32 /* char ?? */
35 EOF
37 test_expect_success setup '
38 cat >file <<-\EOF &&
39 foo mmap bar
40 foo_mmap bar
41 foo_mmap bar mmap
42 foo mmap bar_mmap
43 foo_mmap bar mmap baz
44 EOF
45 cat >hello_world <<-\EOF &&
46 Hello world
47 HeLLo world
48 Hello_world
49 HeLLo_world
50 EOF
51 cat >ab <<-\EOF &&
52 a+b*c
53 a+bc
54 abc
55 EOF
56 cat >d0 <<-\EOF &&
59 EOF
60 echo vvv >v &&
61 echo ww w >w &&
62 echo x x xx x >x &&
63 echo y yy >y &&
64 echo zzz > z &&
65 mkdir t &&
66 echo test >t/t &&
67 echo vvv >t/v &&
68 mkdir t/a &&
69 echo vvv >t/a/v &&
70 qz_to_tab_space >space <<-\EOF &&
71 line without leading space1
72 Zline with leading space1
73 Zline with leading space2
74 Zline with leading space3
75 line without leading space2
76 EOF
77 cat >hello.ps1 <<-\EOF &&
78 # No-op.
79 function dummy() {}
81 # Say hello.
82 function hello() {
83 echo "Hello world."
84 echo "Hello again."
85 } # hello
87 # Still a no-op.
88 function dummy() {}
89 EOF
90 if test_have_prereq FUNNYNAMES
91 then
92 echo unusual >"\"unusual\" pathname" &&
93 echo unusual >"t/nested \"unusual\" pathname"
94 fi &&
95 if test_have_prereq MB_REGEX
96 then
97 echo "¿" >reverse-question-mark
98 fi &&
99 git add . &&
100 test_tick &&
101 git commit -m initial
104 test_expect_success 'grep should not segfault with a bad input' '
105 test_must_fail git grep "("
108 test_invalid_grep_expression --and -e A
110 test_pattern_type () {
111 H=$1 &&
112 HC=$2 &&
113 L=$3 &&
114 type=$4 &&
115 shift 4 &&
117 expected_str= &&
118 case "$type" in
119 BRE)
120 expected_str="${HC}ab:a+bc"
122 ERE)
123 expected_str="${HC}ab:abc"
125 FIX)
126 expected_str="${HC}ab:a+b*c"
129 BUG "unknown pattern type '$type'"
131 esac &&
132 config_str="$@" &&
134 test_expect_success "grep $L with '$config_str' interpreted as $type" '
135 echo $expected_str >expected &&
136 git $config_str grep "a+b*c" $H ab >actual &&
137 test_cmp expected actual
141 for H in HEAD ''
143 case "$H" in
144 HEAD) HC='HEAD:' L='HEAD' ;;
145 '') HC= L='in working tree' ;;
146 esac
148 test_expect_success "grep -w $L" '
149 cat >expected <<-EOF &&
150 ${HC}file:1:foo mmap bar
151 ${HC}file:3:foo_mmap bar mmap
152 ${HC}file:4:foo mmap bar_mmap
153 ${HC}file:5:foo_mmap bar mmap baz
155 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
156 test_cmp expected actual
159 test_expect_success "grep -w $L (with --column)" '
160 cat >expected <<-EOF &&
161 ${HC}file:5:foo mmap bar
162 ${HC}file:14:foo_mmap bar mmap
163 ${HC}file:5:foo mmap bar_mmap
164 ${HC}file:14:foo_mmap bar mmap baz
166 git grep --column -w -e mmap $H >actual &&
167 test_cmp expected actual
170 test_expect_success "grep -w $L (with --column, extended OR)" '
171 cat >expected <<-EOF &&
172 ${HC}file:14:foo_mmap bar mmap
173 ${HC}file:19:foo_mmap bar mmap baz
175 git grep --column -w -e mmap$ --or -e baz $H >actual &&
176 test_cmp expected actual
179 test_expect_success "grep -w $L (with --column, --invert-match)" '
180 cat >expected <<-EOF &&
181 ${HC}file:1:foo mmap bar
182 ${HC}file:1:foo_mmap bar
183 ${HC}file:1:foo_mmap bar mmap
184 ${HC}file:1:foo mmap bar_mmap
186 git grep --column --invert-match -w -e baz $H -- file >actual &&
187 test_cmp expected actual
190 test_expect_success "grep $L (with --column, --invert-match, extended OR)" '
191 cat >expected <<-EOF &&
192 ${HC}hello_world:6:HeLLo_world
194 git grep --column --invert-match -e ll --or --not -e _ $H -- hello_world \
195 >actual &&
196 test_cmp expected actual
199 test_expect_success "grep $L (with --column, --invert-match, extended AND)" '
200 cat >expected <<-EOF &&
201 ${HC}hello_world:3:Hello world
202 ${HC}hello_world:3:Hello_world
203 ${HC}hello_world:6:HeLLo_world
205 git grep --column --invert-match --not -e _ --and --not -e ll $H -- hello_world \
206 >actual &&
207 test_cmp expected actual
210 test_expect_success "grep $L (with --column, double-negation)" '
211 cat >expected <<-EOF &&
212 ${HC}file:1:foo_mmap bar mmap baz
214 git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
215 >actual &&
216 test_cmp expected actual
219 test_expect_success "grep -w $L (with --column, -C)" '
220 cat >expected <<-EOF &&
221 ${HC}file:5:foo mmap bar
222 ${HC}file-foo_mmap bar
223 ${HC}file:14:foo_mmap bar mmap
224 ${HC}file:5:foo mmap bar_mmap
225 ${HC}file:14:foo_mmap bar mmap baz
227 git grep --column -w -C1 -e mmap $H >actual &&
228 test_cmp expected actual
231 test_expect_success "grep -w $L (with --line-number, --column)" '
232 cat >expected <<-EOF &&
233 ${HC}file:1:5:foo mmap bar
234 ${HC}file:3:14:foo_mmap bar mmap
235 ${HC}file:4:5:foo mmap bar_mmap
236 ${HC}file:5:14:foo_mmap bar mmap baz
238 git grep -n --column -w -e mmap $H >actual &&
239 test_cmp expected actual
242 test_expect_success "grep -w $L (with non-extended patterns, --column)" '
243 cat >expected <<-EOF &&
244 ${HC}file:5:foo mmap bar
245 ${HC}file:10:foo_mmap bar
246 ${HC}file:10:foo_mmap bar mmap
247 ${HC}file:5:foo mmap bar_mmap
248 ${HC}file:10:foo_mmap bar mmap baz
250 git grep --column -w -e bar -e mmap $H >actual &&
251 test_cmp expected actual
254 test_expect_success "grep -w $L" '
255 cat >expected <<-EOF &&
256 ${HC}file:1:foo mmap bar
257 ${HC}file:3:foo_mmap bar mmap
258 ${HC}file:4:foo mmap bar_mmap
259 ${HC}file:5:foo_mmap bar mmap baz
261 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
262 test_cmp expected actual
265 test_expect_success "grep -w $L" '
266 cat >expected <<-EOF &&
267 ${HC}file:foo mmap bar
268 ${HC}file:foo_mmap bar mmap
269 ${HC}file:foo mmap bar_mmap
270 ${HC}file:foo_mmap bar mmap baz
272 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
273 test_cmp expected actual
276 test_expect_success "grep -w $L (w)" '
277 test_must_fail git grep -n -w -e "^w" $H >actual &&
278 test_must_be_empty actual
281 test_expect_success "grep -w $L (x)" '
282 cat >expected <<-EOF &&
283 ${HC}x:1:x x xx x
285 git grep -n -w -e "x xx* x" $H >actual &&
286 test_cmp expected actual
289 test_expect_success "grep -w $L (y-1)" '
290 cat >expected <<-EOF &&
291 ${HC}y:1:y yy
293 git grep -n -w -e "^y" $H >actual &&
294 test_cmp expected actual
297 test_expect_success "grep -w $L (y-2)" '
298 if git grep -n -w -e "^y y" $H >actual
299 then
300 echo should not have matched
301 cat actual
302 false
303 else
304 test_must_be_empty actual
308 test_expect_success "grep -w $L (z)" '
309 if git grep -n -w -e "^z" $H >actual
310 then
311 echo should not have matched
312 cat actual
313 false
314 else
315 test_must_be_empty actual
319 test_expect_success "grep $L (with --column, --only-matching)" '
320 cat >expected <<-EOF &&
321 ${HC}file:1:5:mmap
322 ${HC}file:2:5:mmap
323 ${HC}file:3:5:mmap
324 ${HC}file:3:13:mmap
325 ${HC}file:4:5:mmap
326 ${HC}file:4:13:mmap
327 ${HC}file:5:5:mmap
328 ${HC}file:5:13:mmap
330 git grep --column -n -o -e mmap $H >actual &&
331 test_cmp expected actual
334 test_expect_success "grep $L (t-1)" '
335 echo "${HC}t/t:1:test" >expected &&
336 git grep -n -e test $H >actual &&
337 test_cmp expected actual
340 test_expect_success "grep $L (t-2)" '
341 echo "${HC}t:1:test" >expected &&
343 cd t &&
344 git grep -n -e test $H
345 ) >actual &&
346 test_cmp expected actual
349 test_expect_success "grep $L (t-3)" '
350 echo "${HC}t/t:1:test" >expected &&
352 cd t &&
353 git grep --full-name -n -e test $H
354 ) >actual &&
355 test_cmp expected actual
358 test_expect_success "grep -c $L (no /dev/null)" '
359 ! git grep -c test $H | grep /dev/null
362 test_expect_success "grep --max-depth -1 $L" '
363 cat >expected <<-EOF &&
364 ${HC}t/a/v:1:vvv
365 ${HC}t/v:1:vvv
366 ${HC}v:1:vvv
368 git grep --max-depth -1 -n -e vvv $H >actual &&
369 test_cmp expected actual &&
370 git grep --recursive -n -e vvv $H >actual &&
371 test_cmp expected actual
374 test_expect_success "grep --max-depth 0 $L" '
375 cat >expected <<-EOF &&
376 ${HC}v:1:vvv
378 git grep --max-depth 0 -n -e vvv $H >actual &&
379 test_cmp expected actual &&
380 git grep --no-recursive -n -e vvv $H >actual &&
381 test_cmp expected actual
384 test_expect_success "grep --max-depth 0 -- '*' $L" '
385 cat >expected <<-EOF &&
386 ${HC}t/a/v:1:vvv
387 ${HC}t/v:1:vvv
388 ${HC}v:1:vvv
390 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
391 test_cmp expected actual &&
392 git grep --no-recursive -n -e vvv $H -- "*" >actual &&
393 test_cmp expected actual
396 test_expect_success "grep --max-depth 1 $L" '
397 cat >expected <<-EOF &&
398 ${HC}t/v:1:vvv
399 ${HC}v:1:vvv
401 git grep --max-depth 1 -n -e vvv $H >actual &&
402 test_cmp expected actual
405 test_expect_success "grep --max-depth 0 -- t $L" '
406 cat >expected <<-EOF &&
407 ${HC}t/v:1:vvv
409 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
410 test_cmp expected actual &&
411 git grep --no-recursive -n -e vvv $H -- t >actual &&
412 test_cmp expected actual
415 test_expect_success "grep --max-depth 0 -- . t $L" '
416 cat >expected <<-EOF &&
417 ${HC}t/v:1:vvv
418 ${HC}v:1:vvv
420 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
421 test_cmp expected actual &&
422 git grep --no-recursive -n -e vvv $H -- . t >actual &&
423 test_cmp expected actual
426 test_expect_success "grep --max-depth 0 -- t . $L" '
427 cat >expected <<-EOF &&
428 ${HC}t/v:1:vvv
429 ${HC}v:1:vvv
431 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
432 test_cmp expected actual &&
433 git grep --no-recursive -n -e vvv $H -- t . >actual &&
434 test_cmp expected actual
438 test_pattern_type "$H" "$HC" "$L" BRE -c grep.extendedRegexp=false
439 test_pattern_type "$H" "$HC" "$L" ERE -c grep.extendedRegexp=true
440 test_pattern_type "$H" "$HC" "$L" BRE -c grep.patternType=basic
441 test_pattern_type "$H" "$HC" "$L" ERE -c grep.patternType=extended
442 test_pattern_type "$H" "$HC" "$L" FIX -c grep.patternType=fixed
444 test_expect_success PCRE "grep $L with grep.patterntype=perl" '
445 echo "${HC}ab:a+b*c" >expected &&
446 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
447 test_cmp expected actual
450 test_expect_success !FAIL_PREREQS,!PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
451 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
454 test_pattern_type "$H" "$HC" "$L" ERE \
455 -c grep.patternType=default \
456 -c grep.extendedRegexp=true
457 test_pattern_type "$H" "$HC" "$L" ERE \
458 -c grep.extendedRegexp=true \
459 -c grep.patternType=default
460 test_pattern_type "$H" "$HC" "$L" ERE \
461 -c grep.patternType=extended \
462 -c grep.extendedRegexp=false
463 test_pattern_type "$H" "$HC" "$L" BRE \
464 -c grep.patternType=basic \
465 -c grep.extendedRegexp=true
466 test_pattern_type "$H" "$HC" "$L" ERE \
467 -c grep.extendedRegexp=false \
468 -c grep.patternType=extended
469 test_pattern_type "$H" "$HC" "$L" BRE \
470 -c grep.extendedRegexp=true \
471 -c grep.patternType=basic
473 # grep.extendedRegexp is last-one-wins
474 test_pattern_type "$H" "$HC" "$L" BRE \
475 -c grep.extendedRegexp=true \
476 -c grep.extendedRegexp=false
478 # grep.patternType=basic pays no attention to grep.extendedRegexp
479 test_pattern_type "$H" "$HC" "$L" BRE \
480 -c grep.extendedRegexp=true \
481 -c grep.patternType=basic \
482 -c grep.extendedRegexp=false
484 # grep.patternType=extended pays no attention to grep.extendedRegexp
485 test_pattern_type "$H" "$HC" "$L" ERE \
486 -c grep.extendedRegexp=true \
487 -c grep.patternType=extended \
488 -c grep.extendedRegexp=false
490 # grep.extendedRegexp is used with a last-one-wins grep.patternType=default
491 test_pattern_type "$H" "$HC" "$L" ERE \
492 -c grep.patternType=fixed \
493 -c grep.extendedRegexp=true \
494 -c grep.patternType=default
496 # grep.extendedRegexp is used with earlier grep.patternType=default
497 test_pattern_type "$H" "$HC" "$L" ERE \
498 -c grep.extendedRegexp=false \
499 -c grep.patternType=default \
500 -c grep.extendedRegexp=true
502 # grep.extendedRegexp is used with a last-one-loses grep.patternType=default
503 test_pattern_type "$H" "$HC" "$L" ERE \
504 -c grep.extendedRegexp=false \
505 -c grep.extendedRegexp=true \
506 -c grep.patternType=default
508 # grep.extendedRegexp and grep.patternType are both last-one-wins independently
509 test_pattern_type "$H" "$HC" "$L" BRE \
510 -c grep.patternType=default \
511 -c grep.extendedRegexp=true \
512 -c grep.patternType=basic
514 # grep.patternType=extended and grep.patternType=default
515 test_pattern_type "$H" "$HC" "$L" BRE \
516 -c grep.patternType=extended \
517 -c grep.patternType=default
519 # grep.patternType=[extended -> default -> fixed] (BRE)" '
520 test_pattern_type "$H" "$HC" "$L" FIX \
521 -c grep.patternType=extended \
522 -c grep.patternType=default \
523 -c grep.patternType=fixed
525 test_expect_success "grep --count $L" '
526 echo ${HC}ab:3 >expected &&
527 git grep --count -e b $H -- ab >actual &&
528 test_cmp expected actual
531 test_expect_success "grep --count -h $L" '
532 echo 3 >expected &&
533 git grep --count -h -e b $H -- ab >actual &&
534 test_cmp expected actual
537 test_expect_success FUNNYNAMES "grep $L should quote unusual pathnames" '
538 cat >expected <<-EOF &&
539 ${HC}"\"unusual\" pathname":unusual
540 ${HC}"t/nested \"unusual\" pathname":unusual
542 git grep unusual $H >actual &&
543 test_cmp expected actual
546 test_expect_success FUNNYNAMES "grep $L in subdir should quote unusual relative pathnames" '
547 cat >expected <<-EOF &&
548 ${HC}"nested \"unusual\" pathname":unusual
551 cd t &&
552 git grep unusual $H
553 ) >actual &&
554 test_cmp expected actual
557 test_expect_success FUNNYNAMES "grep -z $L with unusual pathnames" '
558 cat >expected <<-EOF &&
559 ${HC}"unusual" pathname:unusual
560 ${HC}t/nested "unusual" pathname:unusual
562 git grep -z unusual $H >actual &&
563 tr "\0" ":" <actual >actual-replace-null &&
564 test_cmp expected actual-replace-null
567 test_expect_success FUNNYNAMES "grep -z $L in subdir with unusual relative pathnames" '
568 cat >expected <<-EOF &&
569 ${HC}nested "unusual" pathname:unusual
572 cd t &&
573 git grep -z unusual $H
574 ) >actual &&
575 tr "\0" ":" <actual >actual-replace-null &&
576 test_cmp expected actual-replace-null
578 done
580 test_expect_success MB_REGEX 'grep exactly one char in single-char multibyte file' '
581 LC_ALL=en_US.UTF-8 git grep "^.$" reverse-question-mark
584 test_expect_success MB_REGEX 'grep two chars in single-char multibyte file' '
585 LC_ALL=en_US.UTF-8 test_expect_code 1 git grep ".." reverse-question-mark
588 cat >expected <<EOF
589 file
591 test_expect_success 'grep -l -C' '
592 git grep -l -C1 foo >actual &&
593 test_cmp expected actual
596 cat >expected <<EOF
597 file:5
599 test_expect_success 'grep -c -C' '
600 git grep -c -C1 foo >actual &&
601 test_cmp expected actual
604 test_expect_success 'grep -L -C' '
605 git ls-files >expected &&
606 git grep -L -C1 nonexistent_string >actual &&
607 test_cmp expected actual
610 test_expect_success 'grep --files-without-match --quiet' '
611 git grep --files-without-match --quiet nonexistent_string >actual &&
612 test_must_be_empty actual
615 test_expect_success 'grep --max-count 0 (must exit with non-zero)' '
616 test_must_fail git grep --max-count 0 foo >actual &&
617 test_must_be_empty actual
620 test_expect_success 'grep --max-count 3' '
621 cat >expected <<-EOF &&
622 file:foo mmap bar
623 file:foo_mmap bar
624 file:foo_mmap bar mmap
626 git grep --max-count 3 foo >actual &&
627 test_cmp expected actual
630 test_expect_success 'grep --max-count -1 (no limit)' '
631 cat >expected <<-EOF &&
632 file:foo mmap bar
633 file:foo_mmap bar
634 file:foo_mmap bar mmap
635 file:foo mmap bar_mmap
636 file:foo_mmap bar mmap baz
638 git grep --max-count -1 foo >actual &&
639 test_cmp expected actual
642 test_expect_success 'grep --max-count 1 --context 2' '
643 cat >expected <<-EOF &&
644 file-foo mmap bar
645 file:foo_mmap bar
646 file-foo_mmap bar mmap
648 git grep --max-count 1 --context 1 foo_mmap >actual &&
649 test_cmp expected actual
652 test_expect_success 'grep --max-count 1 --show-function' '
653 cat >expected <<-EOF &&
654 hello.ps1=function hello() {
655 hello.ps1: echo "Hello world."
657 git grep --max-count 1 --show-function Hello hello.ps1 >actual &&
658 test_cmp expected actual
661 test_expect_success 'grep --max-count 2 --show-function' '
662 cat >expected <<-EOF &&
663 hello.ps1=function hello() {
664 hello.ps1: echo "Hello world."
665 hello.ps1: echo "Hello again."
667 git grep --max-count 2 --show-function Hello hello.ps1 >actual &&
668 test_cmp expected actual
671 test_expect_success 'grep --max-count 1 --count' '
672 cat >expected <<-EOF &&
673 hello.ps1:1
675 git grep --max-count 1 --count Hello hello.ps1 >actual &&
676 test_cmp expected actual
679 test_expect_success 'grep --max-count 1 (multiple files)' '
680 cat >expected <<-EOF &&
681 hello.c:#include <stdio.h>
682 hello.ps1:# No-op.
684 git grep --max-count 1 -e o -- hello.\* >actual &&
685 test_cmp expected actual
688 test_expect_success 'grep --max-count 1 --context 1 (multiple files)' '
689 cat >expected <<-EOF &&
690 hello.c-#include <assert.h>
691 hello.c:#include <stdio.h>
692 hello.c-
694 hello.ps1:# No-op.
695 hello.ps1-function dummy() {}
697 git grep --max-count 1 --context 1 -e o -- hello.\* >actual &&
698 test_cmp expected actual
701 cat >expected <<EOF
702 file:foo mmap bar_mmap
705 test_expect_success 'grep -e A --and -e B' '
706 git grep -e "foo mmap" --and -e bar_mmap >actual &&
707 test_cmp expected actual
710 cat >expected <<EOF
711 file:foo_mmap bar mmap
712 file:foo_mmap bar mmap baz
716 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
717 git grep \( -e foo_ --or -e baz \) \
718 --and -e " mmap" >actual &&
719 test_cmp expected actual
722 cat >expected <<EOF
723 file:foo mmap bar
726 test_expect_success 'grep -e A --and --not -e B' '
727 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
728 test_cmp expected actual
731 test_expect_success 'grep should ignore GREP_OPTIONS' '
732 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
733 test_cmp expected actual
736 test_expect_success 'grep -f, non-existent file' '
737 test_must_fail git grep -f patterns
740 cat >expected <<EOF
741 file:foo mmap bar
742 file:foo_mmap bar
743 file:foo_mmap bar mmap
744 file:foo mmap bar_mmap
745 file:foo_mmap bar mmap baz
748 cat >pattern <<EOF
749 mmap
752 test_expect_success 'grep -f, one pattern' '
753 git grep -f pattern >actual &&
754 test_cmp expected actual
757 cat >expected <<EOF
758 file:foo mmap bar
759 file:foo_mmap bar
760 file:foo_mmap bar mmap
761 file:foo mmap bar_mmap
762 file:foo_mmap bar mmap baz
763 t/a/v:vvv
764 t/v:vvv
765 v:vvv
768 cat >patterns <<EOF
769 mmap
773 test_expect_success 'grep -f, multiple patterns' '
774 git grep -f patterns >actual &&
775 test_cmp expected actual
778 test_expect_success 'grep, multiple patterns' '
779 git grep "$(cat patterns)" >actual &&
780 test_cmp expected actual
783 cat >expected <<EOF
784 file:foo mmap bar
785 file:foo_mmap bar
786 file:foo_mmap bar mmap
787 file:foo mmap bar_mmap
788 file:foo_mmap bar mmap baz
789 t/a/v:vvv
790 t/v:vvv
791 v:vvv
794 cat >patterns <<EOF
796 mmap
802 test_expect_success 'grep -f, ignore empty lines' '
803 git grep -f patterns >actual &&
804 test_cmp expected actual
807 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
808 git grep -f - <patterns >actual &&
809 test_cmp expected actual
812 test_expect_success 'grep -f, use cwd relative file' '
813 test_when_finished "git rm -f sub/dir/file" &&
814 mkdir -p sub/dir &&
815 echo hit >sub/dir/file &&
816 git add sub/dir/file &&
817 echo hit >sub/dir/pattern &&
818 echo miss >pattern &&
820 cd sub/dir && git grep -f pattern file
821 ) &&
822 git -C sub/dir grep -f pattern file
825 cat >expected <<EOF
826 y:y yy
828 z:zzz
831 test_expect_success 'grep -q, silently report matches' '
832 git grep -q mmap >actual &&
833 test_must_be_empty actual &&
834 test_must_fail git grep -q qfwfq >actual &&
835 test_must_be_empty actual
838 test_expect_success 'grep -C1 hunk mark between files' '
839 git grep -C1 "^[yz]" >actual &&
840 test_cmp expected actual
843 test_expect_success 'log grep setup' '
844 test_commit --append --author "With * Asterisk <xyzzy@frotz.com>" second file a &&
845 test_commit --append third file a &&
846 test_commit --append --author "Night Fall <nitfol@frobozz.com>" fourth file a
849 test_expect_success 'log grep (1)' '
850 git log --author=author --pretty=tformat:%s >actual &&
852 echo third && echo initial
853 } >expect &&
854 test_cmp expect actual
857 test_expect_success 'log grep (2)' '
858 git log --author=" * " -F --pretty=tformat:%s >actual &&
860 echo second
861 } >expect &&
862 test_cmp expect actual
865 test_expect_success 'log grep (3)' '
866 git log --author="^A U" --pretty=tformat:%s >actual &&
868 echo third && echo initial
869 } >expect &&
870 test_cmp expect actual
873 test_expect_success 'log grep (4)' '
874 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
876 echo second
877 } >expect &&
878 test_cmp expect actual
881 test_expect_success 'log grep (5)' '
882 git log --author=Thor -F --pretty=tformat:%s >actual &&
884 echo third && echo initial
885 } >expect &&
886 test_cmp expect actual
889 test_expect_success 'log grep (6)' '
890 git log --author=-0700 --pretty=tformat:%s >actual &&
891 test_must_be_empty actual
894 test_expect_success 'log grep (7)' '
895 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
896 echo third >expect &&
897 test_cmp expect actual
900 test_expect_success 'log grep (8)' '
901 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
903 echo third && echo second
904 } >expect &&
905 test_cmp expect actual
908 test_expect_success 'log grep (9)' '
909 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
910 echo third >expect &&
911 test_cmp expect actual
914 test_expect_success 'log grep (9)' '
915 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
916 test_must_be_empty actual
919 test_expect_success 'log --grep-reflog can only be used under -g' '
920 test_must_fail git log --grep-reflog="commit: third"
923 test_expect_success 'log with multiple --grep uses union' '
924 git log --grep=i --grep=r --format=%s >actual &&
926 echo fourth && echo third && echo initial
927 } >expect &&
928 test_cmp expect actual
931 test_expect_success 'log --all-match with multiple --grep uses intersection' '
932 git log --all-match --grep=i --grep=r --format=%s >actual &&
934 echo third
935 } >expect &&
936 test_cmp expect actual
939 test_expect_success 'log with multiple --author uses union' '
940 git log --author="Thor" --author="Aster" --format=%s >actual &&
942 echo third && echo second && echo initial
943 } >expect &&
944 test_cmp expect actual
947 test_expect_success 'log --all-match with multiple --author still uses union' '
948 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
950 echo third && echo second && echo initial
951 } >expect &&
952 test_cmp expect actual
955 test_expect_success 'log --grep --author uses intersection' '
956 # grep matches only third and fourth
957 # author matches only initial and third
958 git log --author="A U Thor" --grep=r --format=%s >actual &&
960 echo third
961 } >expect &&
962 test_cmp expect actual
965 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
966 # grep matches initial and second but not third
967 # author matches only initial and third
968 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
970 echo initial
971 } >expect &&
972 test_cmp expect actual
975 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
976 # grep matches only initial and third
977 # author matches all but second
978 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
980 echo third && echo initial
981 } >expect &&
982 test_cmp expect actual
985 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
986 # grep matches only initial and third
987 # author matches all but second
988 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
990 echo third && echo initial
991 } >expect &&
992 test_cmp expect actual
995 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
996 # grep matches only third
997 # author matches only initial and third
998 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
1000 echo third
1001 } >expect &&
1002 test_cmp expect actual
1005 test_expect_success 'log --author does not search in timestamp' '
1006 git log --author="$GIT_AUTHOR_DATE" >actual &&
1007 test_must_be_empty actual
1010 test_expect_success 'log --committer does not search in timestamp' '
1011 git log --committer="$GIT_COMMITTER_DATE" >actual &&
1012 test_must_be_empty actual
1015 test_expect_success 'grep with CE_VALID file' '
1016 git update-index --assume-unchanged t/t &&
1017 rm t/t &&
1018 echo "t/t:test" >expect &&
1019 git grep test >actual &&
1020 test_cmp expect actual &&
1021 git update-index --no-assume-unchanged t/t &&
1022 git checkout t/t
1025 cat >expected <<EOF
1026 hello.c=#include <stdio.h>
1027 hello.c: return 0;
1030 test_expect_success 'grep -p with userdiff' '
1031 git config diff.custom.funcname "^#" &&
1032 echo "hello.c diff=custom" >.gitattributes &&
1033 git grep -p return >actual &&
1034 test_cmp expected actual
1037 cat >expected <<EOF
1038 hello.c=int main(int argc, const char **argv)
1039 hello.c: return 0;
1042 test_expect_success 'grep -p' '
1043 rm -f .gitattributes &&
1044 git grep -p return >actual &&
1045 test_cmp expected actual
1048 cat >expected <<EOF
1049 hello.c-#include <stdio.h>
1050 hello.c-
1051 hello.c=int main(int argc, const char **argv)
1052 hello.c-{
1053 hello.c- printf("Hello world.\n");
1054 hello.c: return 0;
1057 test_expect_success 'grep -p -B5' '
1058 git grep -p -B5 return >actual &&
1059 test_cmp expected actual
1062 cat >expected <<EOF
1063 hello.c=int main(int argc, const char **argv)
1064 hello.c-{
1065 hello.c- printf("Hello world.\n");
1066 hello.c: return 0;
1067 hello.c- /* char ?? */
1068 hello.c-}
1071 test_expect_success 'grep -W' '
1072 git grep -W return >actual &&
1073 test_cmp expected actual
1076 cat >expected <<EOF
1077 hello.c-#include <assert.h>
1078 hello.c:#include <stdio.h>
1081 test_expect_success 'grep -W shows no trailing empty lines' '
1082 git grep -W stdio >actual &&
1083 test_cmp expected actual
1086 test_expect_success 'grep -W with userdiff' '
1087 test_when_finished "rm -f .gitattributes" &&
1088 git config diff.custom.xfuncname "^function .*$" &&
1089 echo "hello.ps1 diff=custom" >.gitattributes &&
1090 git grep -W echo >function-context-userdiff-actual
1093 test_expect_success ' includes preceding comment' '
1094 grep "# Say hello" function-context-userdiff-actual
1097 test_expect_success ' includes function line' '
1098 grep "=function hello" function-context-userdiff-actual
1101 test_expect_success ' includes matching line' '
1102 grep ": echo" function-context-userdiff-actual
1105 test_expect_success ' includes last line of the function' '
1106 grep "} # hello" function-context-userdiff-actual
1109 for threads in $(test_seq 0 10)
1111 test_expect_success "grep --threads=$threads & -c grep.threads=$threads" "
1112 git grep --threads=$threads . >actual.$threads &&
1113 if test $threads -ge 1
1114 then
1115 test_cmp actual.\$(($threads - 1)) actual.$threads
1116 fi &&
1117 git -c grep.threads=$threads grep . >actual.$threads &&
1118 if test $threads -ge 1
1119 then
1120 test_cmp actual.\$(($threads - 1)) actual.$threads
1123 done
1125 test_expect_success !PTHREADS,!FAIL_PREREQS \
1126 'grep --threads=N or pack.threads=N warns when no pthreads' '
1127 git grep --threads=2 Hello hello_world 2>err &&
1128 grep ^warning: err >warnings &&
1129 test_line_count = 1 warnings &&
1130 grep -F "no threads support, ignoring --threads" err &&
1131 git -c grep.threads=2 grep Hello hello_world 2>err &&
1132 grep ^warning: err >warnings &&
1133 test_line_count = 1 warnings &&
1134 grep -F "no threads support, ignoring grep.threads" err &&
1135 git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
1136 grep ^warning: err >warnings &&
1137 test_line_count = 2 warnings &&
1138 grep -F "no threads support, ignoring --threads" err &&
1139 grep -F "no threads support, ignoring grep.threads" err &&
1140 git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
1141 test_line_count = 0 err
1144 test_expect_success 'grep from a subdirectory to search wider area (1)' '
1145 mkdir -p s &&
1147 cd s && git grep "x x x" ..
1151 test_expect_success 'grep from a subdirectory to search wider area (2)' '
1152 mkdir -p s &&
1154 cd s &&
1155 test_expect_code 1 git grep xxyyzz .. >out &&
1156 test_must_be_empty out
1160 cat >expected <<EOF
1161 hello.c:int main(int argc, const char **argv)
1164 test_expect_success 'grep -Fi' '
1165 git grep -Fi "CHAR *" >actual &&
1166 test_cmp expected actual
1169 test_expect_success 'outside of git repository' '
1170 rm -fr non &&
1171 mkdir -p non/git/sub &&
1172 echo hello >non/git/file1 &&
1173 echo world >non/git/sub/file2 &&
1175 echo file1:hello &&
1176 echo sub/file2:world
1177 } >non/expect.full &&
1178 echo file2:world >non/expect.sub &&
1180 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1181 export GIT_CEILING_DIRECTORIES &&
1182 cd non/git &&
1183 test_must_fail git grep o &&
1184 git grep --no-index o >../actual.full &&
1185 test_cmp ../expect.full ../actual.full &&
1186 cd sub &&
1187 test_must_fail git grep o &&
1188 git grep --no-index o >../../actual.sub &&
1189 test_cmp ../../expect.sub ../../actual.sub
1190 ) &&
1192 echo ".*o*" >non/git/.gitignore &&
1194 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1195 export GIT_CEILING_DIRECTORIES &&
1196 cd non/git &&
1197 test_must_fail git grep o &&
1198 git grep --no-index --exclude-standard o >../actual.full &&
1199 test_cmp ../expect.full ../actual.full &&
1202 echo ".gitignore:.*o*" &&
1203 cat ../expect.full
1204 } >../expect.with.ignored &&
1205 git grep --no-index --no-exclude-standard o >../actual.full &&
1206 test_cmp ../expect.with.ignored ../actual.full
1210 test_expect_success 'outside of git repository with fallbackToNoIndex' '
1211 rm -fr non &&
1212 mkdir -p non/git/sub &&
1213 echo hello >non/git/file1 &&
1214 echo world >non/git/sub/file2 &&
1215 cat <<-\EOF >non/expect.full &&
1216 file1:hello
1217 sub/file2:world
1219 echo file2:world >non/expect.sub &&
1221 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1222 export GIT_CEILING_DIRECTORIES &&
1223 cd non/git &&
1224 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1225 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
1226 test_cmp ../expect.full ../actual.full &&
1227 cd sub &&
1228 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1229 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
1230 test_cmp ../../expect.sub ../../actual.sub
1231 ) &&
1233 echo ".*o*" >non/git/.gitignore &&
1235 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1236 export GIT_CEILING_DIRECTORIES &&
1237 cd non/git &&
1238 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1239 git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
1240 test_cmp ../expect.full ../actual.full &&
1243 echo ".gitignore:.*o*" &&
1244 cat ../expect.full
1245 } >../expect.with.ignored &&
1246 git -c grep.fallbackToNoIndex grep --no-exclude-standard o >../actual.full &&
1247 test_cmp ../expect.with.ignored ../actual.full
1251 test_expect_success 'no repository with path outside $cwd' '
1252 test_when_finished rm -fr non &&
1253 rm -fr non &&
1254 mkdir -p non/git/sub non/tig &&
1256 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1257 export GIT_CEILING_DIRECTORIES &&
1258 cd non/git &&
1259 test_expect_code 128 git grep --no-index search .. 2>error &&
1260 grep "is outside the directory tree" error
1261 ) &&
1263 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1264 export GIT_CEILING_DIRECTORIES &&
1265 cd non/git &&
1266 test_expect_code 128 git grep --no-index search ../tig 2>error &&
1267 grep "is outside the directory tree" error
1268 ) &&
1270 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1271 export GIT_CEILING_DIRECTORIES &&
1272 cd non/git &&
1273 test_expect_code 128 git grep --no-index search ../non 2>error &&
1274 grep "no such path in the working tree" error
1278 test_expect_success 'inside git repository but with --no-index' '
1279 rm -fr is &&
1280 mkdir -p is/git/sub &&
1281 echo hello >is/git/file1 &&
1282 echo world >is/git/sub/file2 &&
1283 echo ".*o*" >is/git/.gitignore &&
1285 echo file1:hello &&
1286 echo sub/file2:world
1287 } >is/expect.unignored &&
1289 echo ".gitignore:.*o*" &&
1290 cat is/expect.unignored
1291 } >is/expect.full &&
1292 echo file2:world >is/expect.sub &&
1294 cd is/git &&
1295 git init &&
1296 test_must_fail git grep o >../actual.full &&
1297 test_must_be_empty ../actual.full &&
1299 git grep --untracked o >../actual.unignored &&
1300 test_cmp ../expect.unignored ../actual.unignored &&
1302 git grep --no-index o >../actual.full &&
1303 test_cmp ../expect.full ../actual.full &&
1305 git grep --no-index --exclude-standard o >../actual.unignored &&
1306 test_cmp ../expect.unignored ../actual.unignored &&
1308 cd sub &&
1309 test_must_fail git grep o >../../actual.sub &&
1310 test_must_be_empty ../../actual.sub &&
1312 git grep --no-index o >../../actual.sub &&
1313 test_cmp ../../expect.sub ../../actual.sub &&
1315 git grep --untracked o >../../actual.sub &&
1316 test_cmp ../../expect.sub ../../actual.sub
1320 test_expect_success 'grep --no-index descends into repos, but not .git' '
1321 rm -fr non &&
1322 mkdir -p non/git &&
1324 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1325 export GIT_CEILING_DIRECTORIES &&
1326 cd non/git &&
1328 echo magic >file &&
1329 git init repo &&
1331 cd repo &&
1332 echo magic >file &&
1333 git add file &&
1334 git commit -m foo &&
1335 echo magic >.git/file
1336 ) &&
1338 cat >expect <<-\EOF &&
1339 file
1340 repo/file
1342 git grep -l --no-index magic >actual &&
1343 test_cmp expect actual
1347 test_expect_success 'setup double-dash tests' '
1348 cat >double-dash <<EOF &&
1351 other
1353 git add double-dash
1356 cat >expected <<EOF
1357 double-dash:->
1359 test_expect_success 'grep -- pattern' '
1360 git grep -- "->" >actual &&
1361 test_cmp expected actual
1363 test_expect_success 'grep -- pattern -- pathspec' '
1364 git grep -- "->" -- double-dash >actual &&
1365 test_cmp expected actual
1367 test_expect_success 'grep -e pattern -- path' '
1368 git grep -e "->" -- double-dash >actual &&
1369 test_cmp expected actual
1372 cat >expected <<EOF
1373 double-dash:--
1375 test_expect_success 'grep -e -- -- path' '
1376 git grep -e -- -- double-dash >actual &&
1377 test_cmp expected actual
1380 test_expect_success 'dashdash disambiguates rev as rev' '
1381 test_when_finished "rm -f main" &&
1382 echo content >main &&
1383 echo main:hello.c >expect &&
1384 git grep -l o main -- hello.c >actual &&
1385 test_cmp expect actual
1388 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
1389 test_when_finished "git rm -f main" &&
1390 echo content >main &&
1391 git add main &&
1392 echo main:content >expect &&
1393 git grep o -- main >actual &&
1394 test_cmp expect actual
1397 test_expect_success 'report bogus arg without dashdash' '
1398 test_must_fail git grep o does-not-exist
1401 test_expect_success 'report bogus rev with dashdash' '
1402 test_must_fail git grep o hello.c --
1405 test_expect_success 'allow non-existent path with dashdash' '
1406 # We need a real match so grep exits with success.
1407 tree=$(git ls-tree HEAD |
1408 sed s/hello.c/not-in-working-tree/ |
1409 git mktree) &&
1410 git grep o "$tree" -- not-in-working-tree
1413 test_expect_success 'grep --no-index pattern -- path' '
1414 rm -fr non &&
1415 mkdir -p non/git &&
1417 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1418 export GIT_CEILING_DIRECTORIES &&
1419 cd non/git &&
1420 echo hello >hello &&
1421 echo goodbye >goodbye &&
1422 echo hello:hello >expect &&
1423 git grep --no-index o -- hello >actual &&
1424 test_cmp expect actual
1428 test_expect_success 'grep --no-index complains of revs' '
1429 test_must_fail git grep --no-index o main -- 2>err &&
1430 test_grep "cannot be used with revs" err
1433 test_expect_success 'grep --no-index prefers paths to revs' '
1434 test_when_finished "rm -f main" &&
1435 echo content >main &&
1436 echo main:content >expect &&
1437 git grep --no-index o main >actual &&
1438 test_cmp expect actual
1441 test_expect_success 'grep --no-index does not "diagnose" revs' '
1442 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1443 test_grep ! -i "did you mean" err
1446 cat >expected <<EOF
1447 hello.c:int main(int argc, const char **argv)
1448 hello.c: printf("Hello world.\n");
1451 test_expect_success PCRE 'grep --perl-regexp pattern' '
1452 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1453 test_cmp expected actual
1456 test_expect_success !FAIL_PREREQS,!PCRE 'grep --perl-regexp pattern errors without PCRE' '
1457 test_must_fail git grep --perl-regexp "foo.*bar"
1460 test_expect_success PCRE 'grep -P pattern' '
1461 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1462 test_cmp expected actual
1465 test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
1466 git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1467 test_cmp expected actual
1471 test_expect_success !FAIL_PREREQS,!PCRE 'grep -P pattern errors without PCRE' '
1472 test_must_fail git grep -P "foo.*bar"
1475 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1476 test_must_fail git -c grep.extendedregexp=true \
1477 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1478 test_must_be_empty actual
1481 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1482 git -c grep.extendedregexp=true \
1483 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1484 test_cmp expected actual
1487 test_expect_success PCRE 'grep -P -v pattern' '
1488 cat >expected <<-\EOF &&
1489 ab:a+b*c
1490 ab:a+bc
1492 git grep -P -v "abc" ab >actual &&
1493 test_cmp expected actual
1496 test_expect_success PCRE 'grep -P -i pattern' '
1497 cat >expected <<-EOF &&
1498 hello.c: printf("Hello world.\n");
1500 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1501 test_cmp expected actual
1504 test_expect_success PCRE 'grep -P -w pattern' '
1505 cat >expected <<-\EOF &&
1506 hello_world:Hello world
1507 hello_world:HeLLo world
1509 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1510 test_cmp expected actual
1513 test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1514 git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1515 test_cmp hello_world actual &&
1516 git grep -P -h "(.)\1" hello_world >actual &&
1517 test_cmp hello_world actual
1520 test_expect_success 'grep -G invalidpattern properly dies ' '
1521 test_must_fail git grep -G "a["
1524 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1525 test_must_fail git -c grep.patterntype=basic grep "a["
1528 test_expect_success 'grep -E invalidpattern properly dies ' '
1529 test_must_fail git grep -E "a["
1532 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1533 test_must_fail git -c grep.patterntype=extended grep "a["
1536 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1537 test_must_fail git grep -P "a["
1540 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1541 test_must_fail git -c grep.patterntype=perl grep "a["
1544 test_expect_success 'grep -G -E -F pattern' '
1545 echo "ab:a+b*c" >expected &&
1546 git grep -G -E -F "a+b*c" ab >actual &&
1547 test_cmp expected actual
1550 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1551 echo "ab:a+b*c" >expected &&
1552 git \
1553 -c grep.patterntype=basic \
1554 -c grep.patterntype=extended \
1555 -c grep.patterntype=fixed \
1556 grep "a+b*c" ab >actual &&
1557 test_cmp expected actual
1560 test_expect_success 'grep -E -F -G pattern' '
1561 echo "ab:a+bc" >expected &&
1562 git grep -E -F -G "a+b*c" ab >actual &&
1563 test_cmp expected actual
1566 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1567 echo "ab:a+bc" >expected &&
1568 git \
1569 -c grep.patterntype=extended \
1570 -c grep.patterntype=fixed \
1571 -c grep.patterntype=basic \
1572 grep "a+b*c" ab >actual &&
1573 test_cmp expected actual
1576 test_expect_success 'grep -F -G -E pattern' '
1577 echo "ab:abc" >expected &&
1578 git grep -F -G -E "a+b*c" ab >actual &&
1579 test_cmp expected actual
1582 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1583 echo "ab:abc" >expected &&
1584 git \
1585 -c grep.patterntype=fixed \
1586 -c grep.patterntype=basic \
1587 -c grep.patterntype=extended \
1588 grep "a+b*c" ab >actual &&
1589 test_cmp expected actual
1592 test_expect_success 'grep -G -F -P -E pattern' '
1593 echo "d0:d" >expected &&
1594 git grep -G -F -P -E "[\d]" d0 >actual &&
1595 test_cmp expected actual
1598 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1599 echo "d0:d" >expected &&
1600 git \
1601 -c grep.patterntype=fixed \
1602 -c grep.patterntype=basic \
1603 -c grep.patterntype=perl \
1604 -c grep.patterntype=extended \
1605 grep "[\d]" d0 >actual &&
1606 test_cmp expected actual
1609 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1610 echo "d0:0" >expected &&
1611 git grep -G -F -E -P "[\d]" d0 >actual &&
1612 test_cmp expected actual
1615 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1616 echo "d0:0" >expected &&
1617 git \
1618 -c grep.patterntype=fixed \
1619 -c grep.patterntype=basic \
1620 -c grep.patterntype=extended \
1621 -c grep.patterntype=perl \
1622 grep "[\d]" d0 >actual &&
1623 test_cmp expected actual
1626 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1627 echo "ab:a+b*c" >expected &&
1628 git \
1629 -c grep.patterntype=fixed \
1630 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1631 test_cmp expected actual
1634 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1635 echo "ab:a+b*c" >expected &&
1636 git \
1637 -c grep.patterntype=basic \
1638 grep -F "*c" ab >actual &&
1639 test_cmp expected actual
1642 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1643 cat >expected <<-\EOF &&
1644 ab:a+b*c
1645 ab:a+bc
1647 git \
1648 -c grep.patterntype=fixed \
1649 grep -G "a+b" ab >actual &&
1650 test_cmp expected actual
1653 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1654 cat >expected <<-\EOF &&
1655 ab:a+b*c
1656 ab:a+bc
1657 ab:abc
1659 git \
1660 -c grep.patterntype=fixed \
1661 grep -E "a+" ab >actual &&
1662 test_cmp expected actual
1665 cat >expected <<EOF
1666 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1667 hello.c<RED>-<RESET>{
1668 <RED>--<RESET>
1669 hello.c<RED>:<RESET> /* char ?? */
1670 hello.c<RED>-<RESET>}
1671 <RED>--<RESET>
1672 hello_world<RED>:<RESET>Hello_world
1673 hello_world<RED>-<RESET>HeLLo_world
1676 test_expect_success 'grep --color, separator' '
1677 test_config color.grep.context normal &&
1678 test_config color.grep.filename normal &&
1679 test_config color.grep.function normal &&
1680 test_config color.grep.linenumber normal &&
1681 test_config color.grep.match normal &&
1682 test_config color.grep.selected normal &&
1683 test_config color.grep.separator red &&
1685 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1686 test_decode_color >actual &&
1687 test_cmp expected actual
1690 cat >expected <<EOF
1691 hello.c:int main(int argc, const char **argv)
1692 hello.c: /* char ?? */
1694 hello_world:Hello_world
1697 test_expect_success 'grep --break' '
1698 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1699 test_cmp expected actual
1702 cat >expected <<EOF
1703 hello.c:int main(int argc, const char **argv)
1704 hello.c-{
1706 hello.c: /* char ?? */
1707 hello.c-}
1709 hello_world:Hello_world
1710 hello_world-HeLLo_world
1713 test_expect_success 'grep --break with context' '
1714 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1715 test_cmp expected actual
1718 cat >expected <<EOF
1719 hello.c
1720 int main(int argc, const char **argv)
1721 /* char ?? */
1722 hello_world
1723 Hello_world
1726 test_expect_success 'grep --heading' '
1727 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1728 test_cmp expected actual
1731 cat >expected <<EOF
1732 <BOLD;GREEN>hello.c<RESET>
1733 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1734 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1736 <BOLD;GREEN>hello_world<RESET>
1737 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1740 test_expect_success 'mimic ack-grep --group' '
1741 test_config color.grep.context normal &&
1742 test_config color.grep.filename "bold green" &&
1743 test_config color.grep.function normal &&
1744 test_config color.grep.linenumber normal &&
1745 test_config color.grep.match "black yellow" &&
1746 test_config color.grep.selected normal &&
1747 test_config color.grep.separator normal &&
1749 git grep --break --heading -n --color \
1750 -e char -e lo_w hello.c hello_world |
1751 test_decode_color >actual &&
1752 test_cmp expected actual
1755 cat >expected <<EOF
1756 space: line with leading space1
1757 space: line with leading space2
1758 space: line with leading space3
1761 test_expect_success PCRE 'grep -E "^ "' '
1762 git grep -E "^ " space >actual &&
1763 test_cmp expected actual
1766 test_expect_success PCRE 'grep -P "^ "' '
1767 git grep -P "^ " space >actual &&
1768 test_cmp expected actual
1771 cat >expected <<EOF
1772 space-line without leading space1
1773 space: line <RED>with <RESET>leading space1
1774 space: line <RED>with <RESET>leading <RED>space2<RESET>
1775 space: line <RED>with <RESET>leading space3
1776 space:line without leading <RED>space2<RESET>
1779 test_expect_success 'grep --color -e A -e B with context' '
1780 test_config color.grep.context normal &&
1781 test_config color.grep.filename normal &&
1782 test_config color.grep.function normal &&
1783 test_config color.grep.linenumber normal &&
1784 test_config color.grep.matchContext normal &&
1785 test_config color.grep.matchSelected red &&
1786 test_config color.grep.selected normal &&
1787 test_config color.grep.separator normal &&
1789 git grep --color=always -C2 -e "with " -e space2 space |
1790 test_decode_color >actual &&
1791 test_cmp expected actual
1794 cat >expected <<EOF
1795 space-line without leading space1
1796 space- line with leading space1
1797 space: line <RED>with <RESET>leading <RED>space2<RESET>
1798 space- line with leading space3
1799 space-line without leading space2
1802 test_expect_success 'grep --color -e A --and -e B with context' '
1803 test_config color.grep.context normal &&
1804 test_config color.grep.filename normal &&
1805 test_config color.grep.function normal &&
1806 test_config color.grep.linenumber normal &&
1807 test_config color.grep.matchContext normal &&
1808 test_config color.grep.matchSelected red &&
1809 test_config color.grep.selected normal &&
1810 test_config color.grep.separator normal &&
1812 git grep --color=always -C2 -e "with " --and -e space2 space |
1813 test_decode_color >actual &&
1814 test_cmp expected actual
1817 cat >expected <<EOF
1818 space-line without leading space1
1819 space: line <RED>with <RESET>leading space1
1820 space- line with leading space2
1821 space: line <RED>with <RESET>leading space3
1822 space-line without leading space2
1825 test_expect_success 'grep --color -e A --and --not -e B with context' '
1826 test_config color.grep.context normal &&
1827 test_config color.grep.filename normal &&
1828 test_config color.grep.function normal &&
1829 test_config color.grep.linenumber normal &&
1830 test_config color.grep.matchContext normal &&
1831 test_config color.grep.matchSelected red &&
1832 test_config color.grep.selected normal &&
1833 test_config color.grep.separator normal &&
1835 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1836 test_decode_color >actual &&
1837 test_cmp expected actual
1840 cat >expected <<EOF
1841 hello.c-
1842 hello.c=int main(int argc, const char **argv)
1843 hello.c-{
1844 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1845 hello.c- return 0;
1846 hello.c- /* char ?? */
1847 hello.c-}
1850 test_expect_success 'grep --color -e A --and -e B -p with context' '
1851 test_config color.grep.context normal &&
1852 test_config color.grep.filename normal &&
1853 test_config color.grep.function normal &&
1854 test_config color.grep.linenumber normal &&
1855 test_config color.grep.matchContext normal &&
1856 test_config color.grep.matchSelected red &&
1857 test_config color.grep.selected normal &&
1858 test_config color.grep.separator normal &&
1860 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1861 test_decode_color >actual &&
1862 test_cmp expected actual
1865 test_expect_success 'grep can find things only in the work tree' '
1866 : >work-tree-only &&
1867 git add work-tree-only &&
1868 test_when_finished "git rm -f work-tree-only" &&
1869 echo "find in work tree" >work-tree-only &&
1870 git grep --quiet "find in work tree" &&
1871 test_must_fail git grep --quiet --cached "find in work tree" &&
1872 test_must_fail git grep --quiet "find in work tree" HEAD
1875 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1876 echo "intend to add this" >intend-to-add &&
1877 git add -N intend-to-add &&
1878 test_when_finished "git rm -f intend-to-add" &&
1879 git grep --quiet "intend to add this" &&
1880 test_must_fail git grep --quiet --cached "intend to add this" &&
1881 test_must_fail git grep --quiet "intend to add this" HEAD
1884 test_expect_success 'grep does not search work tree with assume unchanged' '
1885 echo "intend to add this" >intend-to-add &&
1886 git add -N intend-to-add &&
1887 git update-index --assume-unchanged intend-to-add &&
1888 test_when_finished "git rm -f intend-to-add" &&
1889 test_must_fail git grep --quiet "intend to add this" &&
1890 test_must_fail git grep --quiet --cached "intend to add this" &&
1891 test_must_fail git grep --quiet "intend to add this" HEAD
1894 test_expect_success 'grep can find things only in the index' '
1895 echo "only in the index" >cache-this &&
1896 git add cache-this &&
1897 rm cache-this &&
1898 test_when_finished "git rm --cached cache-this" &&
1899 test_must_fail git grep --quiet "only in the index" &&
1900 git grep --quiet --cached "only in the index" &&
1901 test_must_fail git grep --quiet "only in the index" HEAD
1904 test_expect_success 'grep does not report i-t-a with -L --cached' '
1905 echo "intend to add this" >intend-to-add &&
1906 git add -N intend-to-add &&
1907 test_when_finished "git rm -f intend-to-add" &&
1908 git ls-files | grep -v "^intend-to-add\$" >expected &&
1909 git grep -L --cached "nonexistent_string" >actual &&
1910 test_cmp expected actual
1913 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1914 echo "intend to add this" >intend-to-add-assume-unchanged &&
1915 git add -N intend-to-add-assume-unchanged &&
1916 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1917 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1918 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1919 git grep -L "nonexistent_string" >actual &&
1920 test_cmp expected actual
1923 test_done