surround %s with quotes when failed to lookup commit
[git/debian.git] / t / t7810-grep.sh
blob39d6d713ecbe05e9638f8f6ee3f79ff49628b2cf
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 ?? */
34 EOF
36 test_expect_success setup '
37 cat >file <<-\EOF &&
38 foo mmap bar
39 foo_mmap bar
40 foo_mmap bar mmap
41 foo mmap bar_mmap
42 foo_mmap bar mmap baz
43 EOF
44 cat >hello_world <<-\EOF &&
45 Hello world
46 HeLLo world
47 Hello_world
48 HeLLo_world
49 EOF
50 cat >ab <<-\EOF &&
51 a+b*c
52 a+bc
53 abc
54 EOF
55 cat >d0 <<-\EOF &&
58 EOF
59 echo vvv >v &&
60 echo ww w >w &&
61 echo x x xx x >x &&
62 echo y yy >y &&
63 echo zzz > z &&
64 mkdir t &&
65 echo test >t/t &&
66 echo vvv >t/v &&
67 mkdir t/a &&
68 echo vvv >t/a/v &&
69 qz_to_tab_space >space <<-\EOF &&
70 line without leading space1
71 Zline with leading space1
72 Zline with leading space2
73 Zline with leading space3
74 line without leading space2
75 EOF
76 cat >hello.ps1 <<-\EOF &&
77 # No-op.
78 function dummy() {}
80 # Say hello.
81 function hello() {
82 echo "Hello world."
83 echo "Hello again."
84 } # hello
86 # Still a no-op.
87 function dummy() {}
88 EOF
89 if test_have_prereq FUNNYNAMES
90 then
91 echo unusual >"\"unusual\" pathname" &&
92 echo unusual >"t/nested \"unusual\" pathname"
93 fi &&
94 if test_have_prereq MB_REGEX
95 then
96 echo "¿" >reverse-question-mark
97 fi &&
98 git add . &&
99 test_tick &&
100 git commit -m initial
103 test_expect_success 'grep should not segfault with a bad input' '
104 test_must_fail git grep "("
107 test_invalid_grep_expression --and -e A
109 test_pattern_type () {
110 H=$1 &&
111 HC=$2 &&
112 L=$3 &&
113 type=$4 &&
114 shift 4 &&
116 expected_str= &&
117 case "$type" in
118 BRE)
119 expected_str="${HC}ab:a+bc"
121 ERE)
122 expected_str="${HC}ab:abc"
124 FIX)
125 expected_str="${HC}ab:a+b*c"
128 BUG "unknown pattern type '$type'"
130 esac &&
131 config_str="$@" &&
133 test_expect_success "grep $L with '$config_str' interpreted as $type" '
134 echo $expected_str >expected &&
135 git $config_str grep "a+b*c" $H ab >actual &&
136 test_cmp expected actual
140 for H in HEAD ''
142 case "$H" in
143 HEAD) HC='HEAD:' L='HEAD' ;;
144 '') HC= L='in working tree' ;;
145 esac
147 test_expect_success "grep -w $L" '
148 cat >expected <<-EOF &&
149 ${HC}file:1:foo mmap bar
150 ${HC}file:3:foo_mmap bar mmap
151 ${HC}file:4:foo mmap bar_mmap
152 ${HC}file:5:foo_mmap bar mmap baz
154 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
155 test_cmp expected actual
158 test_expect_success "grep -w $L (with --column)" '
159 cat >expected <<-EOF &&
160 ${HC}file:5:foo mmap bar
161 ${HC}file:14:foo_mmap bar mmap
162 ${HC}file:5:foo mmap bar_mmap
163 ${HC}file:14:foo_mmap bar mmap baz
165 git grep --column -w -e mmap $H >actual &&
166 test_cmp expected actual
169 test_expect_success "grep -w $L (with --column, extended OR)" '
170 cat >expected <<-EOF &&
171 ${HC}file:14:foo_mmap bar mmap
172 ${HC}file:19:foo_mmap bar mmap baz
174 git grep --column -w -e mmap$ --or -e baz $H >actual &&
175 test_cmp expected actual
178 test_expect_success "grep -w $L (with --column, --invert-match)" '
179 cat >expected <<-EOF &&
180 ${HC}file:1:foo mmap bar
181 ${HC}file:1:foo_mmap bar
182 ${HC}file:1:foo_mmap bar mmap
183 ${HC}file:1:foo mmap bar_mmap
185 git grep --column --invert-match -w -e baz $H -- file >actual &&
186 test_cmp expected actual
189 test_expect_success "grep $L (with --column, --invert-match, extended OR)" '
190 cat >expected <<-EOF &&
191 ${HC}hello_world:6:HeLLo_world
193 git grep --column --invert-match -e ll --or --not -e _ $H -- hello_world \
194 >actual &&
195 test_cmp expected actual
198 test_expect_success "grep $L (with --column, --invert-match, extended AND)" '
199 cat >expected <<-EOF &&
200 ${HC}hello_world:3:Hello world
201 ${HC}hello_world:3:Hello_world
202 ${HC}hello_world:6:HeLLo_world
204 git grep --column --invert-match --not -e _ --and --not -e ll $H -- hello_world \
205 >actual &&
206 test_cmp expected actual
209 test_expect_success "grep $L (with --column, double-negation)" '
210 cat >expected <<-EOF &&
211 ${HC}file:1:foo_mmap bar mmap baz
213 git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
214 >actual &&
215 test_cmp expected actual
218 test_expect_success "grep -w $L (with --column, -C)" '
219 cat >expected <<-EOF &&
220 ${HC}file:5:foo mmap bar
221 ${HC}file-foo_mmap bar
222 ${HC}file:14:foo_mmap bar mmap
223 ${HC}file:5:foo mmap bar_mmap
224 ${HC}file:14:foo_mmap bar mmap baz
226 git grep --column -w -C1 -e mmap $H >actual &&
227 test_cmp expected actual
230 test_expect_success "grep -w $L (with --line-number, --column)" '
231 cat >expected <<-EOF &&
232 ${HC}file:1:5:foo mmap bar
233 ${HC}file:3:14:foo_mmap bar mmap
234 ${HC}file:4:5:foo mmap bar_mmap
235 ${HC}file:5:14:foo_mmap bar mmap baz
237 git grep -n --column -w -e mmap $H >actual &&
238 test_cmp expected actual
241 test_expect_success "grep -w $L (with non-extended patterns, --column)" '
242 cat >expected <<-EOF &&
243 ${HC}file:5:foo mmap bar
244 ${HC}file:10:foo_mmap bar
245 ${HC}file:10:foo_mmap bar mmap
246 ${HC}file:5:foo mmap bar_mmap
247 ${HC}file:10:foo_mmap bar mmap baz
249 git grep --column -w -e bar -e mmap $H >actual &&
250 test_cmp expected actual
253 test_expect_success "grep -w $L" '
254 cat >expected <<-EOF &&
255 ${HC}file:1:foo mmap bar
256 ${HC}file:3:foo_mmap bar mmap
257 ${HC}file:4:foo mmap bar_mmap
258 ${HC}file:5:foo_mmap bar mmap baz
260 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
261 test_cmp expected actual
264 test_expect_success "grep -w $L" '
265 cat >expected <<-EOF &&
266 ${HC}file:foo mmap bar
267 ${HC}file:foo_mmap bar mmap
268 ${HC}file:foo mmap bar_mmap
269 ${HC}file:foo_mmap bar mmap baz
271 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
272 test_cmp expected actual
275 test_expect_success "grep -w $L (w)" '
276 test_must_fail git grep -n -w -e "^w" $H >actual &&
277 test_must_be_empty actual
280 test_expect_success "grep -w $L (x)" '
281 cat >expected <<-EOF &&
282 ${HC}x:1:x x xx x
284 git grep -n -w -e "x xx* x" $H >actual &&
285 test_cmp expected actual
288 test_expect_success "grep -w $L (y-1)" '
289 cat >expected <<-EOF &&
290 ${HC}y:1:y yy
292 git grep -n -w -e "^y" $H >actual &&
293 test_cmp expected actual
296 test_expect_success "grep -w $L (y-2)" '
297 if git grep -n -w -e "^y y" $H >actual
298 then
299 echo should not have matched
300 cat actual
301 false
302 else
303 test_must_be_empty actual
307 test_expect_success "grep -w $L (z)" '
308 if git grep -n -w -e "^z" $H >actual
309 then
310 echo should not have matched
311 cat actual
312 false
313 else
314 test_must_be_empty actual
318 test_expect_success "grep $L (with --column, --only-matching)" '
319 cat >expected <<-EOF &&
320 ${HC}file:1:5:mmap
321 ${HC}file:2:5:mmap
322 ${HC}file:3:5:mmap
323 ${HC}file:3:13:mmap
324 ${HC}file:4:5:mmap
325 ${HC}file:4:13:mmap
326 ${HC}file:5:5:mmap
327 ${HC}file:5:13:mmap
329 git grep --column -n -o -e mmap $H >actual &&
330 test_cmp expected actual
333 test_expect_success "grep $L (t-1)" '
334 echo "${HC}t/t:1:test" >expected &&
335 git grep -n -e test $H >actual &&
336 test_cmp expected actual
339 test_expect_success "grep $L (t-2)" '
340 echo "${HC}t:1:test" >expected &&
342 cd t &&
343 git grep -n -e test $H
344 ) >actual &&
345 test_cmp expected actual
348 test_expect_success "grep $L (t-3)" '
349 echo "${HC}t/t:1:test" >expected &&
351 cd t &&
352 git grep --full-name -n -e test $H
353 ) >actual &&
354 test_cmp expected actual
357 test_expect_success "grep -c $L (no /dev/null)" '
358 ! git grep -c test $H | grep /dev/null
361 test_expect_success "grep --max-depth -1 $L" '
362 cat >expected <<-EOF &&
363 ${HC}t/a/v:1:vvv
364 ${HC}t/v:1:vvv
365 ${HC}v:1:vvv
367 git grep --max-depth -1 -n -e vvv $H >actual &&
368 test_cmp expected actual &&
369 git grep --recursive -n -e vvv $H >actual &&
370 test_cmp expected actual
373 test_expect_success "grep --max-depth 0 $L" '
374 cat >expected <<-EOF &&
375 ${HC}v:1:vvv
377 git grep --max-depth 0 -n -e vvv $H >actual &&
378 test_cmp expected actual &&
379 git grep --no-recursive -n -e vvv $H >actual &&
380 test_cmp expected actual
383 test_expect_success "grep --max-depth 0 -- '*' $L" '
384 cat >expected <<-EOF &&
385 ${HC}t/a/v:1:vvv
386 ${HC}t/v:1:vvv
387 ${HC}v:1:vvv
389 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
390 test_cmp expected actual &&
391 git grep --no-recursive -n -e vvv $H -- "*" >actual &&
392 test_cmp expected actual
395 test_expect_success "grep --max-depth 1 $L" '
396 cat >expected <<-EOF &&
397 ${HC}t/v:1:vvv
398 ${HC}v:1:vvv
400 git grep --max-depth 1 -n -e vvv $H >actual &&
401 test_cmp expected actual
404 test_expect_success "grep --max-depth 0 -- t $L" '
405 cat >expected <<-EOF &&
406 ${HC}t/v:1:vvv
408 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
409 test_cmp expected actual &&
410 git grep --no-recursive -n -e vvv $H -- t >actual &&
411 test_cmp expected actual
414 test_expect_success "grep --max-depth 0 -- . t $L" '
415 cat >expected <<-EOF &&
416 ${HC}t/v:1:vvv
417 ${HC}v:1:vvv
419 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
420 test_cmp expected actual &&
421 git grep --no-recursive -n -e vvv $H -- . t >actual &&
422 test_cmp expected actual
425 test_expect_success "grep --max-depth 0 -- t . $L" '
426 cat >expected <<-EOF &&
427 ${HC}t/v:1:vvv
428 ${HC}v:1:vvv
430 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
431 test_cmp expected actual &&
432 git grep --no-recursive -n -e vvv $H -- t . >actual &&
433 test_cmp expected actual
437 test_pattern_type "$H" "$HC" "$L" BRE -c grep.extendedRegexp=false
438 test_pattern_type "$H" "$HC" "$L" ERE -c grep.extendedRegexp=true
439 test_pattern_type "$H" "$HC" "$L" BRE -c grep.patternType=basic
440 test_pattern_type "$H" "$HC" "$L" ERE -c grep.patternType=extended
441 test_pattern_type "$H" "$HC" "$L" FIX -c grep.patternType=fixed
443 test_expect_success PCRE "grep $L with grep.patterntype=perl" '
444 echo "${HC}ab:a+b*c" >expected &&
445 git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
446 test_cmp expected actual
449 test_expect_success !FAIL_PREREQS,!PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
450 test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
453 test_pattern_type "$H" "$HC" "$L" ERE \
454 -c grep.patternType=default \
455 -c grep.extendedRegexp=true
456 test_pattern_type "$H" "$HC" "$L" ERE \
457 -c grep.extendedRegexp=true \
458 -c grep.patternType=default
459 test_pattern_type "$H" "$HC" "$L" ERE \
460 -c grep.patternType=extended \
461 -c grep.extendedRegexp=false
462 test_pattern_type "$H" "$HC" "$L" BRE \
463 -c grep.patternType=basic \
464 -c grep.extendedRegexp=true
465 test_pattern_type "$H" "$HC" "$L" ERE \
466 -c grep.extendedRegexp=false \
467 -c grep.patternType=extended
468 test_pattern_type "$H" "$HC" "$L" BRE \
469 -c grep.extendedRegexp=true \
470 -c grep.patternType=basic
472 # grep.extendedRegexp is last-one-wins
473 test_pattern_type "$H" "$HC" "$L" BRE \
474 -c grep.extendedRegexp=true \
475 -c grep.extendedRegexp=false
477 # grep.patternType=basic pays no attention to grep.extendedRegexp
478 test_pattern_type "$H" "$HC" "$L" BRE \
479 -c grep.extendedRegexp=true \
480 -c grep.patternType=basic \
481 -c grep.extendedRegexp=false
483 # grep.patternType=extended pays no attention to grep.extendedRegexp
484 test_pattern_type "$H" "$HC" "$L" ERE \
485 -c grep.extendedRegexp=true \
486 -c grep.patternType=extended \
487 -c grep.extendedRegexp=false
489 # grep.extendedRegexp is used with a last-one-wins grep.patternType=default
490 test_pattern_type "$H" "$HC" "$L" ERE \
491 -c grep.patternType=fixed \
492 -c grep.extendedRegexp=true \
493 -c grep.patternType=default
495 # grep.extendedRegexp is used with earlier grep.patternType=default
496 test_pattern_type "$H" "$HC" "$L" ERE \
497 -c grep.extendedRegexp=false \
498 -c grep.patternType=default \
499 -c grep.extendedRegexp=true
501 # grep.extendedRegexp is used with a last-one-loses grep.patternType=default
502 test_pattern_type "$H" "$HC" "$L" ERE \
503 -c grep.extendedRegexp=false \
504 -c grep.extendedRegexp=true \
505 -c grep.patternType=default
507 # grep.extendedRegexp and grep.patternType are both last-one-wins independently
508 test_pattern_type "$H" "$HC" "$L" BRE \
509 -c grep.patternType=default \
510 -c grep.extendedRegexp=true \
511 -c grep.patternType=basic
513 # grep.patternType=extended and grep.patternType=default
514 test_pattern_type "$H" "$HC" "$L" BRE \
515 -c grep.patternType=extended \
516 -c grep.patternType=default
518 # grep.patternType=[extended -> default -> fixed] (BRE)" '
519 test_pattern_type "$H" "$HC" "$L" FIX \
520 -c grep.patternType=extended \
521 -c grep.patternType=default \
522 -c grep.patternType=fixed
524 test_expect_success "grep --count $L" '
525 echo ${HC}ab:3 >expected &&
526 git grep --count -e b $H -- ab >actual &&
527 test_cmp expected actual
530 test_expect_success "grep --count -h $L" '
531 echo 3 >expected &&
532 git grep --count -h -e b $H -- ab >actual &&
533 test_cmp expected actual
536 test_expect_success FUNNYNAMES "grep $L should quote unusual pathnames" '
537 cat >expected <<-EOF &&
538 ${HC}"\"unusual\" pathname":unusual
539 ${HC}"t/nested \"unusual\" pathname":unusual
541 git grep unusual $H >actual &&
542 test_cmp expected actual
545 test_expect_success FUNNYNAMES "grep $L in subdir should quote unusual relative pathnames" '
546 cat >expected <<-EOF &&
547 ${HC}"nested \"unusual\" pathname":unusual
550 cd t &&
551 git grep unusual $H
552 ) >actual &&
553 test_cmp expected actual
556 test_expect_success FUNNYNAMES "grep -z $L with unusual pathnames" '
557 cat >expected <<-EOF &&
558 ${HC}"unusual" pathname:unusual
559 ${HC}t/nested "unusual" pathname:unusual
561 git grep -z unusual $H >actual &&
562 tr "\0" ":" <actual >actual-replace-null &&
563 test_cmp expected actual-replace-null
566 test_expect_success FUNNYNAMES "grep -z $L in subdir with unusual relative pathnames" '
567 cat >expected <<-EOF &&
568 ${HC}nested "unusual" pathname:unusual
571 cd t &&
572 git grep -z unusual $H
573 ) >actual &&
574 tr "\0" ":" <actual >actual-replace-null &&
575 test_cmp expected actual-replace-null
577 done
579 test_expect_success MB_REGEX 'grep exactly one char in single-char multibyte file' '
580 LC_ALL=en_US.UTF-8 git grep "^.$" reverse-question-mark
583 test_expect_success MB_REGEX 'grep two chars in single-char multibyte file' '
584 LC_ALL=en_US.UTF-8 test_expect_code 1 git grep ".." reverse-question-mark
587 cat >expected <<EOF
588 file
590 test_expect_success 'grep -l -C' '
591 git grep -l -C1 foo >actual &&
592 test_cmp expected actual
595 cat >expected <<EOF
596 file:5
598 test_expect_success 'grep -c -C' '
599 git grep -c -C1 foo >actual &&
600 test_cmp expected actual
603 test_expect_success 'grep -L -C' '
604 git ls-files >expected &&
605 git grep -L -C1 nonexistent_string >actual &&
606 test_cmp expected actual
609 test_expect_success 'grep --files-without-match --quiet' '
610 git grep --files-without-match --quiet nonexistent_string >actual &&
611 test_must_be_empty actual
614 test_expect_success 'grep --max-count 0 (must exit with non-zero)' '
615 test_must_fail git grep --max-count 0 foo >actual &&
616 test_must_be_empty actual
619 test_expect_success 'grep --max-count 3' '
620 cat >expected <<-EOF &&
621 file:foo mmap bar
622 file:foo_mmap bar
623 file:foo_mmap bar mmap
625 git grep --max-count 3 foo >actual &&
626 test_cmp expected actual
629 test_expect_success 'grep --max-count -1 (no limit)' '
630 cat >expected <<-EOF &&
631 file:foo mmap bar
632 file:foo_mmap bar
633 file:foo_mmap bar mmap
634 file:foo mmap bar_mmap
635 file:foo_mmap bar mmap baz
637 git grep --max-count -1 foo >actual &&
638 test_cmp expected actual
641 test_expect_success 'grep --max-count 1 --context 2' '
642 cat >expected <<-EOF &&
643 file-foo mmap bar
644 file:foo_mmap bar
645 file-foo_mmap bar mmap
647 git grep --max-count 1 --context 1 foo_mmap >actual &&
648 test_cmp expected actual
651 test_expect_success 'grep --max-count 1 --show-function' '
652 cat >expected <<-EOF &&
653 hello.ps1=function hello() {
654 hello.ps1: echo "Hello world."
656 git grep --max-count 1 --show-function Hello hello.ps1 >actual &&
657 test_cmp expected actual
660 test_expect_success 'grep --max-count 2 --show-function' '
661 cat >expected <<-EOF &&
662 hello.ps1=function hello() {
663 hello.ps1: echo "Hello world."
664 hello.ps1: echo "Hello again."
666 git grep --max-count 2 --show-function Hello hello.ps1 >actual &&
667 test_cmp expected actual
670 test_expect_success 'grep --max-count 1 --count' '
671 cat >expected <<-EOF &&
672 hello.ps1:1
674 git grep --max-count 1 --count Hello hello.ps1 >actual &&
675 test_cmp expected actual
678 test_expect_success 'grep --max-count 1 (multiple files)' '
679 cat >expected <<-EOF &&
680 hello.c:#include <stdio.h>
681 hello.ps1:# No-op.
683 git grep --max-count 1 -e o -- hello.\* >actual &&
684 test_cmp expected actual
687 test_expect_success 'grep --max-count 1 --context 1 (multiple files)' '
688 cat >expected <<-EOF &&
689 hello.c-#include <assert.h>
690 hello.c:#include <stdio.h>
691 hello.c-
693 hello.ps1:# No-op.
694 hello.ps1-function dummy() {}
696 git grep --max-count 1 --context 1 -e o -- hello.\* >actual &&
697 test_cmp expected actual
700 cat >expected <<EOF
701 file:foo mmap bar_mmap
704 test_expect_success 'grep -e A --and -e B' '
705 git grep -e "foo mmap" --and -e bar_mmap >actual &&
706 test_cmp expected actual
709 cat >expected <<EOF
710 file:foo_mmap bar mmap
711 file:foo_mmap bar mmap baz
715 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
716 git grep \( -e foo_ --or -e baz \) \
717 --and -e " mmap" >actual &&
718 test_cmp expected actual
721 cat >expected <<EOF
722 file:foo mmap bar
725 test_expect_success 'grep -e A --and --not -e B' '
726 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
727 test_cmp expected actual
730 test_expect_success 'grep should ignore GREP_OPTIONS' '
731 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
732 test_cmp expected actual
735 test_expect_success 'grep -f, non-existent file' '
736 test_must_fail git grep -f patterns
739 cat >expected <<EOF
740 file:foo mmap bar
741 file:foo_mmap bar
742 file:foo_mmap bar mmap
743 file:foo mmap bar_mmap
744 file:foo_mmap bar mmap baz
747 cat >pattern <<EOF
748 mmap
751 test_expect_success 'grep -f, one pattern' '
752 git grep -f pattern >actual &&
753 test_cmp expected actual
756 cat >expected <<EOF
757 file:foo mmap bar
758 file:foo_mmap bar
759 file:foo_mmap bar mmap
760 file:foo mmap bar_mmap
761 file:foo_mmap bar mmap baz
762 t/a/v:vvv
763 t/v:vvv
764 v:vvv
767 cat >patterns <<EOF
768 mmap
772 test_expect_success 'grep -f, multiple patterns' '
773 git grep -f patterns >actual &&
774 test_cmp expected actual
777 test_expect_success 'grep, multiple patterns' '
778 git grep "$(cat patterns)" >actual &&
779 test_cmp expected actual
782 cat >expected <<EOF
783 file:foo mmap bar
784 file:foo_mmap bar
785 file:foo_mmap bar mmap
786 file:foo mmap bar_mmap
787 file:foo_mmap bar mmap baz
788 t/a/v:vvv
789 t/v:vvv
790 v:vvv
793 cat >patterns <<EOF
795 mmap
801 test_expect_success 'grep -f, ignore empty lines' '
802 git grep -f patterns >actual &&
803 test_cmp expected actual
806 test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
807 git grep -f - <patterns >actual &&
808 test_cmp expected actual
811 cat >expected <<EOF
812 y:y yy
814 z:zzz
817 test_expect_success 'grep -q, silently report matches' '
818 git grep -q mmap >actual &&
819 test_must_be_empty actual &&
820 test_must_fail git grep -q qfwfq >actual &&
821 test_must_be_empty actual
824 test_expect_success 'grep -C1 hunk mark between files' '
825 git grep -C1 "^[yz]" >actual &&
826 test_cmp expected actual
829 test_expect_success 'log grep setup' '
830 test_commit --append --author "With * Asterisk <xyzzy@frotz.com>" second file a &&
831 test_commit --append third file a &&
832 test_commit --append --author "Night Fall <nitfol@frobozz.com>" fourth file a
835 test_expect_success 'log grep (1)' '
836 git log --author=author --pretty=tformat:%s >actual &&
838 echo third && echo initial
839 } >expect &&
840 test_cmp expect actual
843 test_expect_success 'log grep (2)' '
844 git log --author=" * " -F --pretty=tformat:%s >actual &&
846 echo second
847 } >expect &&
848 test_cmp expect actual
851 test_expect_success 'log grep (3)' '
852 git log --author="^A U" --pretty=tformat:%s >actual &&
854 echo third && echo initial
855 } >expect &&
856 test_cmp expect actual
859 test_expect_success 'log grep (4)' '
860 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
862 echo second
863 } >expect &&
864 test_cmp expect actual
867 test_expect_success 'log grep (5)' '
868 git log --author=Thor -F --pretty=tformat:%s >actual &&
870 echo third && echo initial
871 } >expect &&
872 test_cmp expect actual
875 test_expect_success 'log grep (6)' '
876 git log --author=-0700 --pretty=tformat:%s >actual &&
877 test_must_be_empty actual
880 test_expect_success 'log grep (7)' '
881 git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
882 echo third >expect &&
883 test_cmp expect actual
886 test_expect_success 'log grep (8)' '
887 git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
889 echo third && echo second
890 } >expect &&
891 test_cmp expect actual
894 test_expect_success 'log grep (9)' '
895 git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
896 echo third >expect &&
897 test_cmp expect actual
900 test_expect_success 'log grep (9)' '
901 git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
902 test_must_be_empty actual
905 test_expect_success 'log --grep-reflog can only be used under -g' '
906 test_must_fail git log --grep-reflog="commit: third"
909 test_expect_success 'log with multiple --grep uses union' '
910 git log --grep=i --grep=r --format=%s >actual &&
912 echo fourth && echo third && echo initial
913 } >expect &&
914 test_cmp expect actual
917 test_expect_success 'log --all-match with multiple --grep uses intersection' '
918 git log --all-match --grep=i --grep=r --format=%s >actual &&
920 echo third
921 } >expect &&
922 test_cmp expect actual
925 test_expect_success 'log with multiple --author uses union' '
926 git log --author="Thor" --author="Aster" --format=%s >actual &&
928 echo third && echo second && echo initial
929 } >expect &&
930 test_cmp expect actual
933 test_expect_success 'log --all-match with multiple --author still uses union' '
934 git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
936 echo third && echo second && echo initial
937 } >expect &&
938 test_cmp expect actual
941 test_expect_success 'log --grep --author uses intersection' '
942 # grep matches only third and fourth
943 # author matches only initial and third
944 git log --author="A U Thor" --grep=r --format=%s >actual &&
946 echo third
947 } >expect &&
948 test_cmp expect actual
951 test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
952 # grep matches initial and second but not third
953 # author matches only initial and third
954 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
956 echo initial
957 } >expect &&
958 test_cmp expect actual
961 test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
962 # grep matches only initial and third
963 # author matches all but second
964 git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
966 echo third && echo initial
967 } >expect &&
968 test_cmp expect actual
971 test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
972 # grep matches only initial and third
973 # author matches all but second
974 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
976 echo third && echo initial
977 } >expect &&
978 test_cmp expect actual
981 test_expect_success 'log --all-match --grep --grep --author takes intersection' '
982 # grep matches only third
983 # author matches only initial and third
984 git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
986 echo third
987 } >expect &&
988 test_cmp expect actual
991 test_expect_success 'log --author does not search in timestamp' '
992 git log --author="$GIT_AUTHOR_DATE" >actual &&
993 test_must_be_empty actual
996 test_expect_success 'log --committer does not search in timestamp' '
997 git log --committer="$GIT_COMMITTER_DATE" >actual &&
998 test_must_be_empty actual
1001 test_expect_success 'grep with CE_VALID file' '
1002 git update-index --assume-unchanged t/t &&
1003 rm t/t &&
1004 echo "t/t:test" >expect &&
1005 git grep test >actual &&
1006 test_cmp expect actual &&
1007 git update-index --no-assume-unchanged t/t &&
1008 git checkout t/t
1011 cat >expected <<EOF
1012 hello.c=#include <stdio.h>
1013 hello.c: return 0;
1016 test_expect_success 'grep -p with userdiff' '
1017 git config diff.custom.funcname "^#" &&
1018 echo "hello.c diff=custom" >.gitattributes &&
1019 git grep -p return >actual &&
1020 test_cmp expected actual
1023 cat >expected <<EOF
1024 hello.c=int main(int argc, const char **argv)
1025 hello.c: return 0;
1028 test_expect_success 'grep -p' '
1029 rm -f .gitattributes &&
1030 git grep -p return >actual &&
1031 test_cmp expected actual
1034 cat >expected <<EOF
1035 hello.c-#include <stdio.h>
1036 hello.c-
1037 hello.c=int main(int argc, const char **argv)
1038 hello.c-{
1039 hello.c- printf("Hello world.\n");
1040 hello.c: return 0;
1043 test_expect_success 'grep -p -B5' '
1044 git grep -p -B5 return >actual &&
1045 test_cmp expected actual
1048 cat >expected <<EOF
1049 hello.c=int main(int argc, const char **argv)
1050 hello.c-{
1051 hello.c- printf("Hello world.\n");
1052 hello.c: return 0;
1053 hello.c- /* char ?? */
1054 hello.c-}
1057 test_expect_success 'grep -W' '
1058 git grep -W return >actual &&
1059 test_cmp expected actual
1062 cat >expected <<EOF
1063 hello.c-#include <assert.h>
1064 hello.c:#include <stdio.h>
1067 test_expect_success 'grep -W shows no trailing empty lines' '
1068 git grep -W stdio >actual &&
1069 test_cmp expected actual
1072 test_expect_success 'grep -W with userdiff' '
1073 test_when_finished "rm -f .gitattributes" &&
1074 git config diff.custom.xfuncname "^function .*$" &&
1075 echo "hello.ps1 diff=custom" >.gitattributes &&
1076 git grep -W echo >function-context-userdiff-actual
1079 test_expect_success ' includes preceding comment' '
1080 grep "# Say hello" function-context-userdiff-actual
1083 test_expect_success ' includes function line' '
1084 grep "=function hello" function-context-userdiff-actual
1087 test_expect_success ' includes matching line' '
1088 grep ": echo" function-context-userdiff-actual
1091 test_expect_success ' includes last line of the function' '
1092 grep "} # hello" function-context-userdiff-actual
1095 for threads in $(test_seq 0 10)
1097 test_expect_success "grep --threads=$threads & -c grep.threads=$threads" "
1098 git grep --threads=$threads . >actual.$threads &&
1099 if test $threads -ge 1
1100 then
1101 test_cmp actual.\$(($threads - 1)) actual.$threads
1102 fi &&
1103 git -c grep.threads=$threads grep . >actual.$threads &&
1104 if test $threads -ge 1
1105 then
1106 test_cmp actual.\$(($threads - 1)) actual.$threads
1109 done
1111 test_expect_success !PTHREADS,!FAIL_PREREQS \
1112 'grep --threads=N or pack.threads=N warns when no pthreads' '
1113 git grep --threads=2 Hello hello_world 2>err &&
1114 grep ^warning: err >warnings &&
1115 test_line_count = 1 warnings &&
1116 grep -F "no threads support, ignoring --threads" err &&
1117 git -c grep.threads=2 grep Hello hello_world 2>err &&
1118 grep ^warning: err >warnings &&
1119 test_line_count = 1 warnings &&
1120 grep -F "no threads support, ignoring grep.threads" err &&
1121 git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
1122 grep ^warning: err >warnings &&
1123 test_line_count = 2 warnings &&
1124 grep -F "no threads support, ignoring --threads" err &&
1125 grep -F "no threads support, ignoring grep.threads" err &&
1126 git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
1127 test_line_count = 0 err
1130 test_expect_success 'grep from a subdirectory to search wider area (1)' '
1131 mkdir -p s &&
1133 cd s && git grep "x x x" ..
1137 test_expect_success 'grep from a subdirectory to search wider area (2)' '
1138 mkdir -p s &&
1140 cd s &&
1141 test_expect_code 1 git grep xxyyzz .. >out &&
1142 test_must_be_empty out
1146 cat >expected <<EOF
1147 hello.c:int main(int argc, const char **argv)
1150 test_expect_success 'grep -Fi' '
1151 git grep -Fi "CHAR *" >actual &&
1152 test_cmp expected actual
1155 test_expect_success 'outside of git repository' '
1156 rm -fr non &&
1157 mkdir -p non/git/sub &&
1158 echo hello >non/git/file1 &&
1159 echo world >non/git/sub/file2 &&
1161 echo file1:hello &&
1162 echo sub/file2:world
1163 } >non/expect.full &&
1164 echo file2:world >non/expect.sub &&
1166 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1167 export GIT_CEILING_DIRECTORIES &&
1168 cd non/git &&
1169 test_must_fail git grep o &&
1170 git grep --no-index o >../actual.full &&
1171 test_cmp ../expect.full ../actual.full &&
1172 cd sub &&
1173 test_must_fail git grep o &&
1174 git grep --no-index o >../../actual.sub &&
1175 test_cmp ../../expect.sub ../../actual.sub
1176 ) &&
1178 echo ".*o*" >non/git/.gitignore &&
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 --exclude-standard o >../actual.full &&
1185 test_cmp ../expect.full ../actual.full &&
1188 echo ".gitignore:.*o*" &&
1189 cat ../expect.full
1190 } >../expect.with.ignored &&
1191 git grep --no-index --no-exclude-standard o >../actual.full &&
1192 test_cmp ../expect.with.ignored ../actual.full
1196 test_expect_success 'outside of git repository with fallbackToNoIndex' '
1197 rm -fr non &&
1198 mkdir -p non/git/sub &&
1199 echo hello >non/git/file1 &&
1200 echo world >non/git/sub/file2 &&
1201 cat <<-\EOF >non/expect.full &&
1202 file1:hello
1203 sub/file2:world
1205 echo file2:world >non/expect.sub &&
1207 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1208 export GIT_CEILING_DIRECTORIES &&
1209 cd non/git &&
1210 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1211 git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
1212 test_cmp ../expect.full ../actual.full &&
1213 cd sub &&
1214 test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
1215 git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
1216 test_cmp ../../expect.sub ../../actual.sub
1217 ) &&
1219 echo ".*o*" >non/git/.gitignore &&
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 --exclude-standard o >../actual.full &&
1226 test_cmp ../expect.full ../actual.full &&
1229 echo ".gitignore:.*o*" &&
1230 cat ../expect.full
1231 } >../expect.with.ignored &&
1232 git -c grep.fallbackToNoIndex grep --no-exclude-standard o >../actual.full &&
1233 test_cmp ../expect.with.ignored ../actual.full
1237 test_expect_success 'inside git repository but with --no-index' '
1238 rm -fr is &&
1239 mkdir -p is/git/sub &&
1240 echo hello >is/git/file1 &&
1241 echo world >is/git/sub/file2 &&
1242 echo ".*o*" >is/git/.gitignore &&
1244 echo file1:hello &&
1245 echo sub/file2:world
1246 } >is/expect.unignored &&
1248 echo ".gitignore:.*o*" &&
1249 cat is/expect.unignored
1250 } >is/expect.full &&
1251 echo file2:world >is/expect.sub &&
1253 cd is/git &&
1254 git init &&
1255 test_must_fail git grep o >../actual.full &&
1256 test_must_be_empty ../actual.full &&
1258 git grep --untracked o >../actual.unignored &&
1259 test_cmp ../expect.unignored ../actual.unignored &&
1261 git grep --no-index o >../actual.full &&
1262 test_cmp ../expect.full ../actual.full &&
1264 git grep --no-index --exclude-standard o >../actual.unignored &&
1265 test_cmp ../expect.unignored ../actual.unignored &&
1267 cd sub &&
1268 test_must_fail git grep o >../../actual.sub &&
1269 test_must_be_empty ../../actual.sub &&
1271 git grep --no-index o >../../actual.sub &&
1272 test_cmp ../../expect.sub ../../actual.sub &&
1274 git grep --untracked o >../../actual.sub &&
1275 test_cmp ../../expect.sub ../../actual.sub
1279 test_expect_success 'grep --no-index descends into repos, but not .git' '
1280 rm -fr non &&
1281 mkdir -p non/git &&
1283 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1284 export GIT_CEILING_DIRECTORIES &&
1285 cd non/git &&
1287 echo magic >file &&
1288 git init repo &&
1290 cd repo &&
1291 echo magic >file &&
1292 git add file &&
1293 git commit -m foo &&
1294 echo magic >.git/file
1295 ) &&
1297 cat >expect <<-\EOF &&
1298 file
1299 repo/file
1301 git grep -l --no-index magic >actual &&
1302 test_cmp expect actual
1306 test_expect_success 'setup double-dash tests' '
1307 cat >double-dash <<EOF &&
1310 other
1312 git add double-dash
1315 cat >expected <<EOF
1316 double-dash:->
1318 test_expect_success 'grep -- pattern' '
1319 git grep -- "->" >actual &&
1320 test_cmp expected actual
1322 test_expect_success 'grep -- pattern -- pathspec' '
1323 git grep -- "->" -- double-dash >actual &&
1324 test_cmp expected actual
1326 test_expect_success 'grep -e pattern -- path' '
1327 git grep -e "->" -- double-dash >actual &&
1328 test_cmp expected actual
1331 cat >expected <<EOF
1332 double-dash:--
1334 test_expect_success 'grep -e -- -- path' '
1335 git grep -e -- -- double-dash >actual &&
1336 test_cmp expected actual
1339 test_expect_success 'dashdash disambiguates rev as rev' '
1340 test_when_finished "rm -f main" &&
1341 echo content >main &&
1342 echo main:hello.c >expect &&
1343 git grep -l o main -- hello.c >actual &&
1344 test_cmp expect actual
1347 test_expect_success 'dashdash disambiguates pathspec as pathspec' '
1348 test_when_finished "git rm -f main" &&
1349 echo content >main &&
1350 git add main &&
1351 echo main:content >expect &&
1352 git grep o -- main >actual &&
1353 test_cmp expect actual
1356 test_expect_success 'report bogus arg without dashdash' '
1357 test_must_fail git grep o does-not-exist
1360 test_expect_success 'report bogus rev with dashdash' '
1361 test_must_fail git grep o hello.c --
1364 test_expect_success 'allow non-existent path with dashdash' '
1365 # We need a real match so grep exits with success.
1366 tree=$(git ls-tree HEAD |
1367 sed s/hello.c/not-in-working-tree/ |
1368 git mktree) &&
1369 git grep o "$tree" -- not-in-working-tree
1372 test_expect_success 'grep --no-index pattern -- path' '
1373 rm -fr non &&
1374 mkdir -p non/git &&
1376 GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
1377 export GIT_CEILING_DIRECTORIES &&
1378 cd non/git &&
1379 echo hello >hello &&
1380 echo goodbye >goodbye &&
1381 echo hello:hello >expect &&
1382 git grep --no-index o -- hello >actual &&
1383 test_cmp expect actual
1387 test_expect_success 'grep --no-index complains of revs' '
1388 test_must_fail git grep --no-index o main -- 2>err &&
1389 test_i18ngrep "cannot be used with revs" err
1392 test_expect_success 'grep --no-index prefers paths to revs' '
1393 test_when_finished "rm -f main" &&
1394 echo content >main &&
1395 echo main:content >expect &&
1396 git grep --no-index o main >actual &&
1397 test_cmp expect actual
1400 test_expect_success 'grep --no-index does not "diagnose" revs' '
1401 test_must_fail git grep --no-index o :1:hello.c 2>err &&
1402 test_i18ngrep ! -i "did you mean" err
1405 cat >expected <<EOF
1406 hello.c:int main(int argc, const char **argv)
1407 hello.c: printf("Hello world.\n");
1410 test_expect_success PCRE 'grep --perl-regexp pattern' '
1411 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1412 test_cmp expected actual
1415 test_expect_success !FAIL_PREREQS,!PCRE 'grep --perl-regexp pattern errors without PCRE' '
1416 test_must_fail git grep --perl-regexp "foo.*bar"
1419 test_expect_success PCRE 'grep -P pattern' '
1420 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1421 test_cmp expected actual
1424 test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
1425 git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
1426 test_cmp expected actual
1430 test_expect_success !FAIL_PREREQS,!PCRE 'grep -P pattern errors without PCRE' '
1431 test_must_fail git grep -P "foo.*bar"
1434 test_expect_success 'grep pattern with grep.extendedRegexp=true' '
1435 test_must_fail git -c grep.extendedregexp=true \
1436 grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1437 test_must_be_empty actual
1440 test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
1441 git -c grep.extendedregexp=true \
1442 grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
1443 test_cmp expected actual
1446 test_expect_success PCRE 'grep -P -v pattern' '
1447 cat >expected <<-\EOF &&
1448 ab:a+b*c
1449 ab:a+bc
1451 git grep -P -v "abc" ab >actual &&
1452 test_cmp expected actual
1455 test_expect_success PCRE 'grep -P -i pattern' '
1456 cat >expected <<-EOF &&
1457 hello.c: printf("Hello world.\n");
1459 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
1460 test_cmp expected actual
1463 test_expect_success PCRE 'grep -P -w pattern' '
1464 cat >expected <<-\EOF &&
1465 hello_world:Hello world
1466 hello_world:HeLLo world
1468 git grep -P -w "He((?i)ll)o" hello_world >actual &&
1469 test_cmp expected actual
1472 test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
1473 git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
1474 test_cmp hello_world actual &&
1475 git grep -P -h "(.)\1" hello_world >actual &&
1476 test_cmp hello_world actual
1479 test_expect_success 'grep -G invalidpattern properly dies ' '
1480 test_must_fail git grep -G "a["
1483 test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
1484 test_must_fail git -c grep.patterntype=basic grep "a["
1487 test_expect_success 'grep -E invalidpattern properly dies ' '
1488 test_must_fail git grep -E "a["
1491 test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
1492 test_must_fail git -c grep.patterntype=extended grep "a["
1495 test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
1496 test_must_fail git grep -P "a["
1499 test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
1500 test_must_fail git -c grep.patterntype=perl grep "a["
1503 test_expect_success 'grep -G -E -F pattern' '
1504 echo "ab:a+b*c" >expected &&
1505 git grep -G -E -F "a+b*c" ab >actual &&
1506 test_cmp expected actual
1509 test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
1510 echo "ab:a+b*c" >expected &&
1511 git \
1512 -c grep.patterntype=basic \
1513 -c grep.patterntype=extended \
1514 -c grep.patterntype=fixed \
1515 grep "a+b*c" ab >actual &&
1516 test_cmp expected actual
1519 test_expect_success 'grep -E -F -G pattern' '
1520 echo "ab:a+bc" >expected &&
1521 git grep -E -F -G "a+b*c" ab >actual &&
1522 test_cmp expected actual
1525 test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
1526 echo "ab:a+bc" >expected &&
1527 git \
1528 -c grep.patterntype=extended \
1529 -c grep.patterntype=fixed \
1530 -c grep.patterntype=basic \
1531 grep "a+b*c" ab >actual &&
1532 test_cmp expected actual
1535 test_expect_success 'grep -F -G -E pattern' '
1536 echo "ab:abc" >expected &&
1537 git grep -F -G -E "a+b*c" ab >actual &&
1538 test_cmp expected actual
1541 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
1542 echo "ab:abc" >expected &&
1543 git \
1544 -c grep.patterntype=fixed \
1545 -c grep.patterntype=basic \
1546 -c grep.patterntype=extended \
1547 grep "a+b*c" ab >actual &&
1548 test_cmp expected actual
1551 test_expect_success 'grep -G -F -P -E pattern' '
1552 echo "d0:d" >expected &&
1553 git grep -G -F -P -E "[\d]" d0 >actual &&
1554 test_cmp expected actual
1557 test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
1558 echo "d0:d" >expected &&
1559 git \
1560 -c grep.patterntype=fixed \
1561 -c grep.patterntype=basic \
1562 -c grep.patterntype=perl \
1563 -c grep.patterntype=extended \
1564 grep "[\d]" d0 >actual &&
1565 test_cmp expected actual
1568 test_expect_success PCRE 'grep -G -F -E -P pattern' '
1569 echo "d0:0" >expected &&
1570 git grep -G -F -E -P "[\d]" d0 >actual &&
1571 test_cmp expected actual
1574 test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
1575 echo "d0:0" >expected &&
1576 git \
1577 -c grep.patterntype=fixed \
1578 -c grep.patterntype=basic \
1579 -c grep.patterntype=extended \
1580 -c grep.patterntype=perl \
1581 grep "[\d]" d0 >actual &&
1582 test_cmp expected actual
1585 test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
1586 echo "ab:a+b*c" >expected &&
1587 git \
1588 -c grep.patterntype=fixed \
1589 grep -P "a\x{2b}b\x{2a}c" ab >actual &&
1590 test_cmp expected actual
1593 test_expect_success 'grep -F pattern with grep.patternType=basic' '
1594 echo "ab:a+b*c" >expected &&
1595 git \
1596 -c grep.patterntype=basic \
1597 grep -F "*c" ab >actual &&
1598 test_cmp expected actual
1601 test_expect_success 'grep -G pattern with grep.patternType=fixed' '
1602 cat >expected <<-\EOF &&
1603 ab:a+b*c
1604 ab:a+bc
1606 git \
1607 -c grep.patterntype=fixed \
1608 grep -G "a+b" ab >actual &&
1609 test_cmp expected actual
1612 test_expect_success 'grep -E pattern with grep.patternType=fixed' '
1613 cat >expected <<-\EOF &&
1614 ab:a+b*c
1615 ab:a+bc
1616 ab:abc
1618 git \
1619 -c grep.patterntype=fixed \
1620 grep -E "a+" ab >actual &&
1621 test_cmp expected actual
1624 cat >expected <<EOF
1625 hello.c<RED>:<RESET>int main(int argc, const char **argv)
1626 hello.c<RED>-<RESET>{
1627 <RED>--<RESET>
1628 hello.c<RED>:<RESET> /* char ?? */
1629 hello.c<RED>-<RESET>}
1630 <RED>--<RESET>
1631 hello_world<RED>:<RESET>Hello_world
1632 hello_world<RED>-<RESET>HeLLo_world
1635 test_expect_success 'grep --color, separator' '
1636 test_config color.grep.context normal &&
1637 test_config color.grep.filename normal &&
1638 test_config color.grep.function normal &&
1639 test_config color.grep.linenumber normal &&
1640 test_config color.grep.match normal &&
1641 test_config color.grep.selected normal &&
1642 test_config color.grep.separator red &&
1644 git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
1645 test_decode_color >actual &&
1646 test_cmp expected actual
1649 cat >expected <<EOF
1650 hello.c:int main(int argc, const char **argv)
1651 hello.c: /* char ?? */
1653 hello_world:Hello_world
1656 test_expect_success 'grep --break' '
1657 git grep --break -e char -e lo_w hello.c hello_world >actual &&
1658 test_cmp expected actual
1661 cat >expected <<EOF
1662 hello.c:int main(int argc, const char **argv)
1663 hello.c-{
1665 hello.c: /* char ?? */
1666 hello.c-}
1668 hello_world:Hello_world
1669 hello_world-HeLLo_world
1672 test_expect_success 'grep --break with context' '
1673 git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
1674 test_cmp expected actual
1677 cat >expected <<EOF
1678 hello.c
1679 int main(int argc, const char **argv)
1680 /* char ?? */
1681 hello_world
1682 Hello_world
1685 test_expect_success 'grep --heading' '
1686 git grep --heading -e char -e lo_w hello.c hello_world >actual &&
1687 test_cmp expected actual
1690 cat >expected <<EOF
1691 <BOLD;GREEN>hello.c<RESET>
1692 4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
1693 8: /* <BLACK;BYELLOW>char<RESET> ?? */
1695 <BOLD;GREEN>hello_world<RESET>
1696 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
1699 test_expect_success 'mimic ack-grep --group' '
1700 test_config color.grep.context normal &&
1701 test_config color.grep.filename "bold green" &&
1702 test_config color.grep.function normal &&
1703 test_config color.grep.linenumber normal &&
1704 test_config color.grep.match "black yellow" &&
1705 test_config color.grep.selected normal &&
1706 test_config color.grep.separator normal &&
1708 git grep --break --heading -n --color \
1709 -e char -e lo_w hello.c hello_world |
1710 test_decode_color >actual &&
1711 test_cmp expected actual
1714 cat >expected <<EOF
1715 space: line with leading space1
1716 space: line with leading space2
1717 space: line with leading space3
1720 test_expect_success PCRE 'grep -E "^ "' '
1721 git grep -E "^ " space >actual &&
1722 test_cmp expected actual
1725 test_expect_success PCRE 'grep -P "^ "' '
1726 git grep -P "^ " space >actual &&
1727 test_cmp expected actual
1730 cat >expected <<EOF
1731 space-line without leading space1
1732 space: line <RED>with <RESET>leading space1
1733 space: line <RED>with <RESET>leading <RED>space2<RESET>
1734 space: line <RED>with <RESET>leading space3
1735 space:line without leading <RED>space2<RESET>
1738 test_expect_success 'grep --color -e A -e B with context' '
1739 test_config color.grep.context normal &&
1740 test_config color.grep.filename normal &&
1741 test_config color.grep.function normal &&
1742 test_config color.grep.linenumber normal &&
1743 test_config color.grep.matchContext normal &&
1744 test_config color.grep.matchSelected red &&
1745 test_config color.grep.selected normal &&
1746 test_config color.grep.separator normal &&
1748 git grep --color=always -C2 -e "with " -e space2 space |
1749 test_decode_color >actual &&
1750 test_cmp expected actual
1753 cat >expected <<EOF
1754 space-line without leading space1
1755 space- line with leading space1
1756 space: line <RED>with <RESET>leading <RED>space2<RESET>
1757 space- line with leading space3
1758 space-line without leading space2
1761 test_expect_success 'grep --color -e A --and -e B with context' '
1762 test_config color.grep.context normal &&
1763 test_config color.grep.filename normal &&
1764 test_config color.grep.function normal &&
1765 test_config color.grep.linenumber normal &&
1766 test_config color.grep.matchContext normal &&
1767 test_config color.grep.matchSelected red &&
1768 test_config color.grep.selected normal &&
1769 test_config color.grep.separator normal &&
1771 git grep --color=always -C2 -e "with " --and -e space2 space |
1772 test_decode_color >actual &&
1773 test_cmp expected actual
1776 cat >expected <<EOF
1777 space-line without leading space1
1778 space: line <RED>with <RESET>leading space1
1779 space- line with leading space2
1780 space: line <RED>with <RESET>leading space3
1781 space-line without leading space2
1784 test_expect_success 'grep --color -e A --and --not -e B with context' '
1785 test_config color.grep.context normal &&
1786 test_config color.grep.filename normal &&
1787 test_config color.grep.function normal &&
1788 test_config color.grep.linenumber normal &&
1789 test_config color.grep.matchContext normal &&
1790 test_config color.grep.matchSelected red &&
1791 test_config color.grep.selected normal &&
1792 test_config color.grep.separator normal &&
1794 git grep --color=always -C2 -e "with " --and --not -e space2 space |
1795 test_decode_color >actual &&
1796 test_cmp expected actual
1799 cat >expected <<EOF
1800 hello.c-
1801 hello.c=int main(int argc, const char **argv)
1802 hello.c-{
1803 hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
1804 hello.c- return 0;
1805 hello.c- /* char ?? */
1806 hello.c-}
1809 test_expect_success 'grep --color -e A --and -e B -p with context' '
1810 test_config color.grep.context normal &&
1811 test_config color.grep.filename normal &&
1812 test_config color.grep.function normal &&
1813 test_config color.grep.linenumber normal &&
1814 test_config color.grep.matchContext normal &&
1815 test_config color.grep.matchSelected red &&
1816 test_config color.grep.selected normal &&
1817 test_config color.grep.separator normal &&
1819 git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
1820 test_decode_color >actual &&
1821 test_cmp expected actual
1824 test_expect_success 'grep can find things only in the work tree' '
1825 : >work-tree-only &&
1826 git add work-tree-only &&
1827 test_when_finished "git rm -f work-tree-only" &&
1828 echo "find in work tree" >work-tree-only &&
1829 git grep --quiet "find in work tree" &&
1830 test_must_fail git grep --quiet --cached "find in work tree" &&
1831 test_must_fail git grep --quiet "find in work tree" HEAD
1834 test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1835 echo "intend to add this" >intend-to-add &&
1836 git add -N intend-to-add &&
1837 test_when_finished "git rm -f intend-to-add" &&
1838 git grep --quiet "intend to add this" &&
1839 test_must_fail git grep --quiet --cached "intend to add this" &&
1840 test_must_fail git grep --quiet "intend to add this" HEAD
1843 test_expect_success 'grep does not search work tree with assume unchanged' '
1844 echo "intend to add this" >intend-to-add &&
1845 git add -N intend-to-add &&
1846 git update-index --assume-unchanged intend-to-add &&
1847 test_when_finished "git rm -f intend-to-add" &&
1848 test_must_fail git grep --quiet "intend to add this" &&
1849 test_must_fail git grep --quiet --cached "intend to add this" &&
1850 test_must_fail git grep --quiet "intend to add this" HEAD
1853 test_expect_success 'grep can find things only in the index' '
1854 echo "only in the index" >cache-this &&
1855 git add cache-this &&
1856 rm cache-this &&
1857 test_when_finished "git rm --cached cache-this" &&
1858 test_must_fail git grep --quiet "only in the index" &&
1859 git grep --quiet --cached "only in the index" &&
1860 test_must_fail git grep --quiet "only in the index" HEAD
1863 test_expect_success 'grep does not report i-t-a with -L --cached' '
1864 echo "intend to add this" >intend-to-add &&
1865 git add -N intend-to-add &&
1866 test_when_finished "git rm -f intend-to-add" &&
1867 git ls-files | grep -v "^intend-to-add\$" >expected &&
1868 git grep -L --cached "nonexistent_string" >actual &&
1869 test_cmp expected actual
1872 test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1873 echo "intend to add this" >intend-to-add-assume-unchanged &&
1874 git add -N intend-to-add-assume-unchanged &&
1875 test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1876 git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1877 git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1878 git grep -L "nonexistent_string" >actual &&
1879 test_cmp expected actual
1882 test_done