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