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