1#!/bin/sh
2#
3# Copyright (c) 2006 Junio C Hamano
4#
5
6test_description='git grep various.
7'
8
9. ./test-lib.sh
10
11cat >hello.c <<EOF
12#include <stdio.h>
13int main(int argc, const char **argv)
14{
15 printf("Hello world.\n");
16 return 0;
17 /* char ?? */
18}
19EOF
20
21test_expect_success setup '
22 {
23 echo foo mmap bar
24 echo foo_mmap bar
25 echo foo_mmap bar mmap
26 echo foo mmap bar_mmap
27 echo foo_mmap bar mmap baz
28 } >file &&
29 {
30 echo Hello world
31 echo HeLLo world
32 echo Hello_world
33 echo HeLLo_world
34 } >hello_world &&
35 echo vvv >v &&
36 echo ww w >w &&
37 echo x x xx x >x &&
38 echo y yy >y &&
39 echo zzz > z &&
40 mkdir t &&
41 echo test >t/t &&
42 echo vvv >t/v &&
43 mkdir t/a &&
44 echo vvv >t/a/v &&
45 git add . &&
46 test_tick &&
47 git commit -m initial
48'
49
50test_expect_success 'grep should not segfault with a bad input' '
51 test_must_fail git grep "("
52'
53
54for H in HEAD ''
55do
56 case "$H" in
57 HEAD) HC='HEAD:' L='HEAD' ;;
58 '') HC= L='in working tree' ;;
59 esac
60
61 test_expect_success "grep -w $L" '
62 {
63 echo ${HC}file:1:foo mmap bar
64 echo ${HC}file:3:foo_mmap bar mmap
65 echo ${HC}file:4:foo mmap bar_mmap
66 echo ${HC}file:5:foo_mmap bar mmap baz
67 } >expected &&
68 git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
69 test_cmp expected actual
70 '
71
72 test_expect_success "grep -w $L" '
73 {
74 echo ${HC}file:1:foo mmap bar
75 echo ${HC}file:3:foo_mmap bar mmap
76 echo ${HC}file:4:foo mmap bar_mmap
77 echo ${HC}file:5:foo_mmap bar mmap baz
78 } >expected &&
79 git -c grep.linenumber=true grep -w -e mmap $H >actual &&
80 test_cmp expected actual
81 '
82
83 test_expect_success "grep -w $L" '
84 {
85 echo ${HC}file:foo mmap bar
86 echo ${HC}file:foo_mmap bar mmap
87 echo ${HC}file:foo mmap bar_mmap
88 echo ${HC}file:foo_mmap bar mmap baz
89 } >expected &&
90 git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
91 test_cmp expected actual
92 '
93
94 test_expect_success "grep -w $L (w)" '
95 : >expected &&
96 test_must_fail git grep -n -w -e "^w" >actual &&
97 test_cmp expected actual
98 '
99
100 test_expect_success "grep -w $L (x)" '
101 {
102 echo ${HC}x:1:x x xx x
103 } >expected &&
104 git grep -n -w -e "x xx* x" $H >actual &&
105 test_cmp expected actual
106 '
107
108 test_expect_success "grep -w $L (y-1)" '
109 {
110 echo ${HC}y:1:y yy
111 } >expected &&
112 git grep -n -w -e "^y" $H >actual &&
113 test_cmp expected actual
114 '
115
116 test_expect_success "grep -w $L (y-2)" '
117 : >expected &&
118 if git grep -n -w -e "^y y" $H >actual
119 then
120 echo should not have matched
121 cat actual
122 false
123 else
124 test_cmp expected actual
125 fi
126 '
127
128 test_expect_success "grep -w $L (z)" '
129 : >expected &&
130 if git grep -n -w -e "^z" $H >actual
131 then
132 echo should not have matched
133 cat actual
134 false
135 else
136 test_cmp expected actual
137 fi
138 '
139
140 test_expect_success "grep $L (t-1)" '
141 echo "${HC}t/t:1:test" >expected &&
142 git grep -n -e test $H >actual &&
143 test_cmp expected actual
144 '
145
146 test_expect_success "grep $L (t-2)" '
147 echo "${HC}t:1:test" >expected &&
148 (
149 cd t &&
150 git grep -n -e test $H
151 ) >actual &&
152 test_cmp expected actual
153 '
154
155 test_expect_success "grep $L (t-3)" '
156 echo "${HC}t/t:1:test" >expected &&
157 (
158 cd t &&
159 git grep --full-name -n -e test $H
160 ) >actual &&
161 test_cmp expected actual
162 '
163
164 test_expect_success "grep -c $L (no /dev/null)" '
165 ! git grep -c test $H | grep /dev/null
166 '
167
168 test_expect_success "grep --max-depth -1 $L" '
169 {
170 echo ${HC}t/a/v:1:vvv
171 echo ${HC}t/v:1:vvv
172 echo ${HC}v:1:vvv
173 } >expected &&
174 git grep --max-depth -1 -n -e vvv $H >actual &&
175 test_cmp expected actual
176 '
177
178 test_expect_success "grep --max-depth 0 $L" '
179 {
180 echo ${HC}v:1:vvv
181 } >expected &&
182 git grep --max-depth 0 -n -e vvv $H >actual &&
183 test_cmp expected actual
184 '
185
186 test_expect_success "grep --max-depth 0 -- '*' $L" '
187 {
188 echo ${HC}t/a/v:1:vvv
189 echo ${HC}t/v:1:vvv
190 echo ${HC}v:1:vvv
191 } >expected &&
192 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
193 test_cmp expected actual
194 '
195
196 test_expect_success "grep --max-depth 1 $L" '
197 {
198 echo ${HC}t/v:1:vvv
199 echo ${HC}v:1:vvv
200 } >expected &&
201 git grep --max-depth 1 -n -e vvv $H >actual &&
202 test_cmp expected actual
203 '
204
205 test_expect_success "grep --max-depth 0 -- t $L" '
206 {
207 echo ${HC}t/v:1:vvv
208 } >expected &&
209 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
210 test_cmp expected actual
211 '
212
213 test_expect_success "grep --max-depth 0 -- . t $L" '
214 {
215 echo ${HC}t/v:1:vvv
216 echo ${HC}v:1:vvv
217 } >expected &&
218 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
219 test_cmp expected actual
220 '
221
222 test_expect_success "grep --max-depth 0 -- t . $L" '
223 {
224 echo ${HC}t/v:1:vvv
225 echo ${HC}v:1:vvv
226 } >expected &&
227 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
228 test_cmp expected actual
229 '
230
231done
232
233cat >expected <<EOF
234file:foo mmap bar_mmap
235EOF
236
237test_expect_success 'grep -e A --and -e B' '
238 git grep -e "foo mmap" --and -e bar_mmap >actual &&
239 test_cmp expected actual
240'
241
242cat >expected <<EOF
243file:foo_mmap bar mmap
244file:foo_mmap bar mmap baz
245EOF
246
247
248test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
249 git grep \( -e foo_ --or -e baz \) \
250 --and -e " mmap" >actual &&
251 test_cmp expected actual
252'
253
254cat >expected <<EOF
255file:foo mmap bar
256EOF
257
258test_expect_success 'grep -e A --and --not -e B' '
259 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
260 test_cmp expected actual
261'
262
263test_expect_success 'grep should ignore GREP_OPTIONS' '
264 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
265 test_cmp expected actual
266'
267
268test_expect_success 'grep -f, non-existent file' '
269 test_must_fail git grep -f patterns
270'
271
272cat >expected <<EOF
273file:foo mmap bar
274file:foo_mmap bar
275file:foo_mmap bar mmap
276file:foo mmap bar_mmap
277file:foo_mmap bar mmap baz
278EOF
279
280cat >pattern <<EOF
281mmap
282EOF
283
284test_expect_success 'grep -f, one pattern' '
285 git grep -f pattern >actual &&
286 test_cmp expected actual
287'
288
289cat >expected <<EOF
290file:foo mmap bar
291file:foo_mmap bar
292file:foo_mmap bar mmap
293file:foo mmap bar_mmap
294file:foo_mmap bar mmap baz
295t/a/v:vvv
296t/v:vvv
297v:vvv
298EOF
299
300cat >patterns <<EOF
301mmap
302vvv
303EOF
304
305test_expect_success 'grep -f, multiple patterns' '
306 git grep -f patterns >actual &&
307 test_cmp expected actual
308'
309
310cat >expected <<EOF
311file:foo mmap bar
312file:foo_mmap bar
313file:foo_mmap bar mmap
314file:foo mmap bar_mmap
315file:foo_mmap bar mmap baz
316t/a/v:vvv
317t/v:vvv
318v:vvv
319EOF
320
321cat >patterns <<EOF
322
323mmap
324
325vvv
326
327EOF
328
329test_expect_success 'grep -f, ignore empty lines' '
330 git grep -f patterns >actual &&
331 test_cmp expected actual
332'
333
334test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
335 git grep -f - <patterns >actual &&
336 test_cmp expected actual
337'
338
339cat >expected <<EOF
340y:y yy
341--
342z:zzz
343EOF
344
345test_expect_success 'grep -q, silently report matches' '
346 >empty &&
347 git grep -q mmap >actual &&
348 test_cmp empty actual &&
349 test_must_fail git grep -q qfwfq >actual &&
350 test_cmp empty actual
351'
352
353# Create 1024 file names that sort between "y" and "z" to make sure
354# the two files are handled by different calls to an external grep.
355# This depends on MAXARGS in builtin-grep.c being 1024 or less.
356c32="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v"
357test_expect_success 'grep -C1, hunk mark between files' '
358 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
359 git add y-?? &&
360 git grep -C1 "^[yz]" >actual &&
361 test_cmp expected actual
362'
363
364test_expect_success 'grep -C1 hunk mark between files' '
365 git grep -C1 "^[yz]" >actual &&
366 test_cmp expected actual
367'
368
369test_expect_success 'log grep setup' '
370 echo a >>file &&
371 test_tick &&
372 GIT_AUTHOR_NAME="With * Asterisk" \
373 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
374 git commit -a -m "second" &&
375
376 echo a >>file &&
377 test_tick &&
378 git commit -a -m "third" &&
379
380 echo a >>file &&
381 test_tick &&
382 GIT_AUTHOR_NAME="Night Fall" \
383 GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \
384 git commit -a -m "fourth"
385'
386
387test_expect_success 'log grep (1)' '
388 git log --author=author --pretty=tformat:%s >actual &&
389 ( echo third ; echo initial ) >expect &&
390 test_cmp expect actual
391'
392
393test_expect_success 'log grep (2)' '
394 git log --author=" * " -F --pretty=tformat:%s >actual &&
395 ( echo second ) >expect &&
396 test_cmp expect actual
397'
398
399test_expect_success 'log grep (3)' '
400 git log --author="^A U" --pretty=tformat:%s >actual &&
401 ( echo third ; echo initial ) >expect &&
402 test_cmp expect actual
403'
404
405test_expect_success 'log grep (4)' '
406 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
407 ( echo second ) >expect &&
408 test_cmp expect actual
409'
410
411test_expect_success 'log grep (5)' '
412 git log --author=Thor -F --pretty=tformat:%s >actual &&
413 ( echo third ; echo initial ) >expect &&
414 test_cmp expect actual
415'
416
417test_expect_success 'log grep (6)' '
418 git log --author=-0700 --pretty=tformat:%s >actual &&
419 >expect &&
420 test_cmp expect actual
421'
422
423test_expect_success 'log --grep --author implicitly uses all-match' '
424 # grep matches initial and second but not third
425 # author matches only initial and third
426 git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
427 echo initial >expect &&
428 test_cmp expect actual
429'
430
431test_expect_success 'log with multiple --author uses union' '
432 git log --author="Thor" --author="Aster" --format=%s >actual &&
433 {
434 echo third && echo second && echo initial
435 } >expect &&
436 test_cmp expect actual
437'
438
439test_expect_success 'log with --grep and multiple --author uses all-match' '
440 git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
441 {
442 echo third && echo initial
443 } >expect &&
444 test_cmp expect actual
445'
446
447test_expect_success 'log with --grep and multiple --author uses all-match' '
448 git log --author="Thor" --author="Night" --grep=q --format=%s >actual &&
449 >expect &&
450 test_cmp expect actual
451'
452
453test_expect_success 'grep with CE_VALID file' '
454 git update-index --assume-unchanged t/t &&
455 rm t/t &&
456 test "$(git grep test)" = "t/t:test" &&
457 git update-index --no-assume-unchanged t/t &&
458 git checkout t/t
459'
460
461cat >expected <<EOF
462hello.c=#include <stdio.h>
463hello.c: return 0;
464EOF
465
466test_expect_success 'grep -p with userdiff' '
467 git config diff.custom.funcname "^#" &&
468 echo "hello.c diff=custom" >.gitattributes &&
469 git grep -p return >actual &&
470 test_cmp expected actual
471'
472
473cat >expected <<EOF
474hello.c=int main(int argc, const char **argv)
475hello.c: return 0;
476EOF
477
478test_expect_success 'grep -p' '
479 rm -f .gitattributes &&
480 git grep -p return >actual &&
481 test_cmp expected actual
482'
483
484cat >expected <<EOF
485hello.c-#include <stdio.h>
486hello.c=int main(int argc, const char **argv)
487hello.c-{
488hello.c- printf("Hello world.\n");
489hello.c: return 0;
490EOF
491
492test_expect_success 'grep -p -B5' '
493 git grep -p -B5 return >actual &&
494 test_cmp expected actual
495'
496
497test_expect_success 'grep from a subdirectory to search wider area (1)' '
498 mkdir -p s &&
499 (
500 cd s && git grep "x x x" ..
501 )
502'
503
504test_expect_success 'grep from a subdirectory to search wider area (2)' '
505 mkdir -p s &&
506 (
507 cd s || exit 1
508 ( git grep xxyyzz .. >out ; echo $? >status )
509 ! test -s out &&
510 test 1 = $(cat status)
511 )
512'
513
514cat >expected <<EOF
515hello.c:int main(int argc, const char **argv)
516EOF
517
518test_expect_success 'grep -Fi' '
519 git grep -Fi "CHAR *" >actual &&
520 test_cmp expected actual
521'
522
523test_expect_success 'outside of git repository' '
524 rm -fr non &&
525 mkdir -p non/git/sub &&
526 echo hello >non/git/file1 &&
527 echo world >non/git/sub/file2 &&
528 echo ".*o*" >non/git/.gitignore &&
529 {
530 echo file1:hello &&
531 echo sub/file2:world
532 } >non/expect.full &&
533 echo file2:world >non/expect.sub &&
534 (
535 GIT_CEILING_DIRECTORIES="$(pwd)/non/git" &&
536 export GIT_CEILING_DIRECTORIES &&
537 cd non/git &&
538 test_must_fail git grep o &&
539 git grep --no-index o >../actual.full &&
540 test_cmp ../expect.full ../actual.full
541 cd sub &&
542 test_must_fail git grep o &&
543 git grep --no-index o >../../actual.sub &&
544 test_cmp ../../expect.sub ../../actual.sub
545 )
546'
547
548test_expect_success 'inside git repository but with --no-index' '
549 rm -fr is &&
550 mkdir -p is/git/sub &&
551 echo hello >is/git/file1 &&
552 echo world >is/git/sub/file2 &&
553 echo ".*o*" >is/git/.gitignore &&
554 {
555 echo file1:hello &&
556 echo sub/file2:world
557 } >is/expect.full &&
558 : >is/expect.empty &&
559 echo file2:world >is/expect.sub &&
560 (
561 cd is/git &&
562 git init &&
563 test_must_fail git grep o >../actual.full &&
564 test_cmp ../expect.empty ../actual.full &&
565 git grep --no-index o >../actual.full &&
566 test_cmp ../expect.full ../actual.full &&
567 cd sub &&
568 test_must_fail git grep o >../../actual.sub &&
569 test_cmp ../../expect.empty ../../actual.sub &&
570 git grep --no-index o >../../actual.sub &&
571 test_cmp ../../expect.sub ../../actual.sub
572 )
573'
574
575test_expect_success 'setup double-dash tests' '
576cat >double-dash <<EOF &&
577--
578->
579other
580EOF
581git add double-dash
582'
583
584cat >expected <<EOF
585double-dash:->
586EOF
587test_expect_success 'grep -- pattern' '
588 git grep -- "->" >actual &&
589 test_cmp expected actual
590'
591test_expect_success 'grep -- pattern -- pathspec' '
592 git grep -- "->" -- double-dash >actual &&
593 test_cmp expected actual
594'
595test_expect_success 'grep -e pattern -- path' '
596 git grep -e "->" -- double-dash >actual &&
597 test_cmp expected actual
598'
599
600cat >expected <<EOF
601double-dash:--
602EOF
603test_expect_success 'grep -e -- -- path' '
604 git grep -e -- -- double-dash >actual &&
605 test_cmp expected actual
606'
607
608cat >expected <<EOF
609hello.c:int main(int argc, const char **argv)
610hello.c: printf("Hello world.\n");
611EOF
612
613test_expect_success LIBPCRE 'grep --perl-regexp pattern' '
614 git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
615 test_cmp expected actual
616'
617
618test_expect_success LIBPCRE 'grep -P pattern' '
619 git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
620 test_cmp expected actual
621'
622
623test_expect_success LIBPCRE 'grep -P -i pattern' '
624 {
625 echo "hello.c: printf(\"Hello world.\n\");"
626 } >expected &&
627 git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
628 test_cmp expected actual
629'
630
631test_expect_success LIBPCRE 'grep -P -w pattern' '
632 {
633 echo "hello_world:Hello world"
634 echo "hello_world:HeLLo world"
635 } >expected &&
636 git grep -P -w "He((?i)ll)o" hello_world >actual &&
637 test_cmp expected actual
638'
639
640test_expect_success LIBPCRE 'grep -P -F returns error' '
641 test_expect_code 128 git grep -P -F main
642'
643
644test_expect_success LIBPCRE 'grep -P -E returns error' '
645 test_expect_code 128 git grep -P -E main
646'
647
648test_expect_failure LIBPCRE 'grep -P -G returns error' '
649 test_expect_code 128 git grep -P -G main
650'
651
652test_expect_failure LIBPCRE 'grep -P -E -G returns error' '
653 test_expect_code 128 git grep -P -E -G main
654'
655
656test_done