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