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