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