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