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
11test_expect_success 'Check for external grep support' '
12 case "$(git grep -h 2>&1|grep ext-grep)" in
13 *"(default)"*)
14 test_set_prereq EXTGREP
15 true;;
16 *"(ignored by this build)"*)
17 true;;
18 *)
19 false;;
20 esac
21'
22
23cat >hello.c <<EOF
24#include <stdio.h>
25int main(int argc, const char **argv)
26{
27 printf("Hello world.\n");
28 return 0;
29 /* char ?? */
30}
31EOF
32
33test_expect_success setup '
34 {
35 echo foo mmap bar
36 echo foo_mmap bar
37 echo foo_mmap bar mmap
38 echo foo mmap bar_mmap
39 echo foo_mmap bar mmap baz
40 } >file &&
41 echo vvv >v &&
42 echo ww w >w &&
43 echo x x xx x >x &&
44 echo y yy >y &&
45 echo zzz > z &&
46 mkdir t &&
47 echo test >t/t &&
48 echo vvv >t/v &&
49 mkdir t/a &&
50 echo vvv >t/a/v &&
51 git add . &&
52 test_tick &&
53 git commit -m initial
54'
55
56test_expect_success 'grep should not segfault with a bad input' '
57 test_must_fail git grep "("
58'
59
60for H in HEAD ''
61do
62 case "$H" in
63 HEAD) HC='HEAD:' L='HEAD' ;;
64 '') HC= L='in working tree' ;;
65 esac
66
67 test_expect_success "grep -w $L" '
68 {
69 echo ${HC}file:1:foo mmap bar
70 echo ${HC}file:3:foo_mmap bar mmap
71 echo ${HC}file:4:foo mmap bar_mmap
72 echo ${HC}file:5:foo_mmap bar mmap baz
73 } >expected &&
74 git grep -n -w -e mmap $H >actual &&
75 diff expected actual
76 '
77
78 test_expect_success "grep -w $L (w)" '
79 : >expected &&
80 ! git grep -n -w -e "^w" >actual &&
81 test_cmp expected actual
82 '
83
84 test_expect_success "grep -w $L (x)" '
85 {
86 echo ${HC}x:1:x x xx x
87 } >expected &&
88 git grep -n -w -e "x xx* x" $H >actual &&
89 diff expected actual
90 '
91
92 test_expect_success "grep -w $L (y-1)" '
93 {
94 echo ${HC}y:1:y yy
95 } >expected &&
96 git grep -n -w -e "^y" $H >actual &&
97 diff expected actual
98 '
99
100 test_expect_success "grep -w $L (y-2)" '
101 : >expected &&
102 if git grep -n -w -e "^y y" $H >actual
103 then
104 echo should not have matched
105 cat actual
106 false
107 else
108 diff expected actual
109 fi
110 '
111
112 test_expect_success "grep -w $L (z)" '
113 : >expected &&
114 if git grep -n -w -e "^z" $H >actual
115 then
116 echo should not have matched
117 cat actual
118 false
119 else
120 diff expected actual
121 fi
122 '
123
124 test_expect_success "grep $L (t-1)" '
125 echo "${HC}t/t:1:test" >expected &&
126 git grep -n -e test $H >actual &&
127 diff expected actual
128 '
129
130 test_expect_success "grep $L (t-2)" '
131 echo "${HC}t:1:test" >expected &&
132 (
133 cd t &&
134 git grep -n -e test $H
135 ) >actual &&
136 diff expected actual
137 '
138
139 test_expect_success "grep $L (t-3)" '
140 echo "${HC}t/t:1:test" >expected &&
141 (
142 cd t &&
143 git grep --full-name -n -e test $H
144 ) >actual &&
145 diff expected actual
146 '
147
148 test_expect_success "grep -c $L (no /dev/null)" '
149 ! git grep -c test $H | grep /dev/null
150 '
151
152 test_expect_success "grep --max-depth -1 $L" '
153 {
154 echo ${HC}t/a/v:1:vvv
155 echo ${HC}t/v:1:vvv
156 echo ${HC}v:1:vvv
157 } >expected &&
158 git grep --max-depth -1 -n -e vvv $H >actual &&
159 test_cmp expected actual
160 '
161
162 test_expect_success "grep --max-depth 0 $L" '
163 {
164 echo ${HC}v:1:vvv
165 } >expected &&
166 git grep --max-depth 0 -n -e vvv $H >actual &&
167 test_cmp expected actual
168 '
169
170 test_expect_success "grep --max-depth 0 -- '*' $L" '
171 {
172 echo ${HC}t/a/v:1:vvv
173 echo ${HC}t/v:1:vvv
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 1 $L" '
181 {
182 echo ${HC}t/v:1:vvv
183 echo ${HC}v:1:vvv
184 } >expected &&
185 git grep --max-depth 1 -n -e vvv $H >actual &&
186 test_cmp expected actual
187 '
188
189 test_expect_success "grep --max-depth 0 -- t $L" '
190 {
191 echo ${HC}t/v:1:vvv
192 } >expected &&
193 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
194 test_cmp expected actual
195 '
196
197done
198
199cat >expected <<EOF
200file:foo mmap bar_mmap
201EOF
202
203test_expect_success 'grep -e A --and -e B' '
204 git grep -e "foo mmap" --and -e bar_mmap >actual &&
205 test_cmp expected actual
206'
207
208cat >expected <<EOF
209file:foo_mmap bar mmap
210file:foo_mmap bar mmap baz
211EOF
212
213
214test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
215 git grep \( -e foo_ --or -e baz \) \
216 --and -e " mmap" >actual &&
217 test_cmp expected actual
218'
219
220cat >expected <<EOF
221file:foo mmap bar
222EOF
223
224test_expect_success 'grep -e A --and --not -e B' '
225 git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
226 test_cmp expected actual
227'
228
229test_expect_success 'grep should ignore GREP_OPTIONS' '
230 GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
231 test_cmp expected actual
232'
233
234test_expect_success 'grep -f, non-existent file' '
235 test_must_fail git grep -f patterns
236'
237
238cat >expected <<EOF
239file:foo mmap bar
240file:foo_mmap bar
241file:foo_mmap bar mmap
242file:foo mmap bar_mmap
243file:foo_mmap bar mmap baz
244EOF
245
246cat >pattern <<EOF
247mmap
248EOF
249
250test_expect_success 'grep -f, one pattern' '
251 git grep -f pattern >actual &&
252 test_cmp expected actual
253'
254
255cat >expected <<EOF
256file:foo mmap bar
257file:foo_mmap bar
258file:foo_mmap bar mmap
259file:foo mmap bar_mmap
260file:foo_mmap bar mmap baz
261t/a/v:vvv
262t/v:vvv
263v:vvv
264EOF
265
266cat >patterns <<EOF
267mmap
268vvv
269EOF
270
271test_expect_success 'grep -f, multiple patterns' '
272 git grep -f patterns >actual &&
273 test_cmp expected actual
274'
275
276cat >expected <<EOF
277file:foo mmap bar
278file:foo_mmap bar
279file:foo_mmap bar mmap
280file:foo mmap bar_mmap
281file:foo_mmap bar mmap baz
282t/a/v:vvv
283t/v:vvv
284v:vvv
285EOF
286
287cat >patterns <<EOF
288
289mmap
290
291vvv
292
293EOF
294
295test_expect_success 'grep -f, ignore empty lines' '
296 git grep -f patterns >actual &&
297 test_cmp expected actual
298'
299
300cat >expected <<EOF
301y:y yy
302--
303z:zzz
304EOF
305
306# Create 1024 file names that sort between "y" and "z" to make sure
307# the two files are handled by different calls to an external grep.
308# This depends on MAXARGS in builtin-grep.c being 1024 or less.
309c32="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"
310test_expect_success 'grep -C1, hunk mark between files' '
311 for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
312 git add y-?? &&
313 git grep -C1 "^[yz]" >actual &&
314 test_cmp expected actual
315'
316
317test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
318 git grep -C1 --no-ext-grep "^[yz]" >actual &&
319 test_cmp expected actual
320'
321
322test_expect_success 'log grep setup' '
323 echo a >>file &&
324 test_tick &&
325 GIT_AUTHOR_NAME="With * Asterisk" \
326 GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
327 git commit -a -m "second" &&
328
329 echo a >>file &&
330 test_tick &&
331 git commit -a -m "third"
332
333'
334
335test_expect_success 'log grep (1)' '
336 git log --author=author --pretty=tformat:%s >actual &&
337 ( echo third ; echo initial ) >expect &&
338 test_cmp expect actual
339'
340
341test_expect_success 'log grep (2)' '
342 git log --author=" * " -F --pretty=tformat:%s >actual &&
343 ( echo second ) >expect &&
344 test_cmp expect actual
345'
346
347test_expect_success 'log grep (3)' '
348 git log --author="^A U" --pretty=tformat:%s >actual &&
349 ( echo third ; echo initial ) >expect &&
350 test_cmp expect actual
351'
352
353test_expect_success 'log grep (4)' '
354 git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
355 ( echo second ) >expect &&
356 test_cmp expect actual
357'
358
359test_expect_success 'log grep (5)' '
360 git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
361 ( echo third ; echo initial ) >expect &&
362 test_cmp expect actual
363'
364
365test_expect_success 'log grep (6)' '
366 git log --author=-0700 --pretty=tformat:%s >actual &&
367 >expect &&
368 test_cmp expect actual
369'
370
371test_expect_success 'grep with CE_VALID file' '
372 git update-index --assume-unchanged t/t &&
373 rm t/t &&
374 test "$(git grep --no-ext-grep test)" = "t/t:test" &&
375 git update-index --no-assume-unchanged t/t &&
376 git checkout t/t
377'
378
379cat >expected <<EOF
380hello.c=#include <stdio.h>
381hello.c: return 0;
382EOF
383
384test_expect_success 'grep -p with userdiff' '
385 git config diff.custom.funcname "^#" &&
386 echo "hello.c diff=custom" >.gitattributes &&
387 git grep -p return >actual &&
388 test_cmp expected actual
389'
390
391cat >expected <<EOF
392hello.c=int main(int argc, const char **argv)
393hello.c: return 0;
394EOF
395
396test_expect_success 'grep -p' '
397 rm -f .gitattributes &&
398 git grep -p return >actual &&
399 test_cmp expected actual
400'
401
402cat >expected <<EOF
403hello.c-#include <stdio.h>
404hello.c=int main(int argc, const char **argv)
405hello.c-{
406hello.c- printf("Hello world.\n");
407hello.c: return 0;
408EOF
409
410test_expect_success 'grep -p -B5' '
411 git grep -p -B5 return >actual &&
412 test_cmp expected actual
413'
414
415test_expect_success 'grep from a subdirectory to search wider area (1)' '
416 mkdir -p s &&
417 (
418 cd s && git grep "x x x" ..
419 )
420'
421
422test_expect_success 'grep from a subdirectory to search wider area (2)' '
423 mkdir -p s &&
424 (
425 cd s || exit 1
426 ( git grep xxyyzz .. >out ; echo $? >status )
427 ! test -s out &&
428 test 1 = $(cat status)
429 )
430'
431
432cat >expected <<EOF
433hello.c:int main(int argc, const char **argv)
434EOF
435
436test_expect_success 'grep -Fi' '
437 git grep -Fi "CHAR *" >actual &&
438 test_cmp expected actual
439'
440
441test_expect_success EXTGREP 'external grep is called' '
442 GIT_TRACE=2 git grep foo >/dev/null 2>actual &&
443 grep "trace: grep:.*foo" actual >/dev/null
444'
445
446test_expect_success EXTGREP 'no external grep when skip-worktree entries exist' '
447 git update-index --skip-worktree file &&
448 GIT_TRACE=2 git grep foo >/dev/null 2>actual &&
449 ! grep "trace: grep:" actual >/dev/null &&
450 git update-index --no-skip-worktree file
451'
452
453test_done