Code

Merge branch 'jn/maint-1.6.3-check-ref-format-doc'
[git.git] / t / t7002-grep.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Junio C Hamano
4 #
6 test_description='git grep various.
7 '
9 . ./test-lib.sh
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
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 vvv >v &&
29         echo ww w >w &&
30         echo x x xx x >x &&
31         echo y yy >y &&
32         echo zzz > z &&
33         mkdir t &&
34         echo test >t/t &&
35         echo vvv >t/v &&
36         mkdir t/a &&
37         echo vvv >t/a/v &&
38         git add . &&
39         test_tick &&
40         git commit -m initial
41 '
43 test_expect_success 'grep should not segfault with a bad input' '
44         test_must_fail git grep "("
45 '
47 for H in HEAD ''
48 do
49         case "$H" in
50         HEAD)   HC='HEAD:' L='HEAD' ;;
51         '')     HC= L='in working tree' ;;
52         esac
54         test_expect_success "grep -w $L" '
55                 {
56                         echo ${HC}file:1:foo mmap bar
57                         echo ${HC}file:3:foo_mmap bar mmap
58                         echo ${HC}file:4:foo mmap bar_mmap
59                         echo ${HC}file:5:foo_mmap bar mmap baz
60                 } >expected &&
61                 git grep -n -w -e mmap $H >actual &&
62                 diff expected actual
63         '
65         test_expect_success "grep -w $L (w)" '
66                 : >expected &&
67                 ! git grep -n -w -e "^w" >actual &&
68                 test_cmp expected actual
69         '
71         test_expect_success "grep -w $L (x)" '
72                 {
73                         echo ${HC}x:1:x x xx x
74                 } >expected &&
75                 git grep -n -w -e "x xx* x" $H >actual &&
76                 diff expected actual
77         '
79         test_expect_success "grep -w $L (y-1)" '
80                 {
81                         echo ${HC}y:1:y yy
82                 } >expected &&
83                 git grep -n -w -e "^y" $H >actual &&
84                 diff expected actual
85         '
87         test_expect_success "grep -w $L (y-2)" '
88                 : >expected &&
89                 if git grep -n -w -e "^y y" $H >actual
90                 then
91                         echo should not have matched
92                         cat actual
93                         false
94                 else
95                         diff expected actual
96                 fi
97         '
99         test_expect_success "grep -w $L (z)" '
100                 : >expected &&
101                 if git grep -n -w -e "^z" $H >actual
102                 then
103                         echo should not have matched
104                         cat actual
105                         false
106                 else
107                         diff expected actual
108                 fi
109         '
111         test_expect_success "grep $L (t-1)" '
112                 echo "${HC}t/t:1:test" >expected &&
113                 git grep -n -e test $H >actual &&
114                 diff expected actual
115         '
117         test_expect_success "grep $L (t-2)" '
118                 echo "${HC}t:1:test" >expected &&
119                 (
120                         cd t &&
121                         git grep -n -e test $H
122                 ) >actual &&
123                 diff expected actual
124         '
126         test_expect_success "grep $L (t-3)" '
127                 echo "${HC}t/t:1:test" >expected &&
128                 (
129                         cd t &&
130                         git grep --full-name -n -e test $H
131                 ) >actual &&
132                 diff expected actual
133         '
135         test_expect_success "grep -c $L (no /dev/null)" '
136                 ! git grep -c test $H | grep /dev/null
137         '
139         test_expect_success "grep --max-depth -1 $L" '
140                 {
141                         echo ${HC}t/a/v:1:vvv
142                         echo ${HC}t/v:1:vvv
143                         echo ${HC}v:1:vvv
144                 } >expected &&
145                 git grep --max-depth -1 -n -e vvv $H >actual &&
146                 test_cmp expected actual
147         '
149         test_expect_success "grep --max-depth 0 $L" '
150                 {
151                         echo ${HC}v:1:vvv
152                 } >expected &&
153                 git grep --max-depth 0 -n -e vvv $H >actual &&
154                 test_cmp expected actual
155         '
157         test_expect_success "grep --max-depth 0 -- '*' $L" '
158                 {
159                         echo ${HC}t/a/v:1:vvv
160                         echo ${HC}t/v:1:vvv
161                         echo ${HC}v:1:vvv
162                 } >expected &&
163                 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
164                 test_cmp expected actual
165         '
167         test_expect_success "grep --max-depth 1 $L" '
168                 {
169                         echo ${HC}t/v:1:vvv
170                         echo ${HC}v:1:vvv
171                 } >expected &&
172                 git grep --max-depth 1 -n -e vvv $H >actual &&
173                 test_cmp expected actual
174         '
176         test_expect_success "grep --max-depth 0 -- t $L" '
177                 {
178                         echo ${HC}t/v:1:vvv
179                 } >expected &&
180                 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
181                 test_cmp expected actual
182         '
184 done
186 cat >expected <<EOF
187 file:foo mmap bar_mmap
188 EOF
190 test_expect_success 'grep -e A --and -e B' '
191         git grep -e "foo mmap" --and -e bar_mmap >actual &&
192         test_cmp expected actual
195 cat >expected <<EOF
196 file:foo_mmap bar mmap
197 file:foo_mmap bar mmap baz
198 EOF
201 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
202         git grep \( -e foo_ --or -e baz \) \
203                 --and -e " mmap" >actual &&
204         test_cmp expected actual
207 cat >expected <<EOF
208 file:foo mmap bar
209 EOF
211 test_expect_success 'grep -e A --and --not -e B' '
212         git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
213         test_cmp expected actual
216 test_expect_success 'grep -f, non-existent file' '
217         test_must_fail git grep -f patterns
220 cat >expected <<EOF
221 file:foo mmap bar
222 file:foo_mmap bar
223 file:foo_mmap bar mmap
224 file:foo mmap bar_mmap
225 file:foo_mmap bar mmap baz
226 EOF
228 cat >pattern <<EOF
229 mmap
230 EOF
232 test_expect_success 'grep -f, one pattern' '
233         git grep -f pattern >actual &&
234         test_cmp expected actual
237 cat >expected <<EOF
238 file:foo mmap bar
239 file:foo_mmap bar
240 file:foo_mmap bar mmap
241 file:foo mmap bar_mmap
242 file:foo_mmap bar mmap baz
243 t/a/v:vvv
244 t/v:vvv
245 v:vvv
246 EOF
248 cat >patterns <<EOF
249 mmap
250 vvv
251 EOF
253 test_expect_success 'grep -f, multiple patterns' '
254         git grep -f patterns >actual &&
255         test_cmp expected actual
258 cat >expected <<EOF
259 file:foo mmap bar
260 file:foo_mmap bar
261 file:foo_mmap bar mmap
262 file:foo mmap bar_mmap
263 file:foo_mmap bar mmap baz
264 t/a/v:vvv
265 t/v:vvv
266 v:vvv
267 EOF
269 cat >patterns <<EOF
271 mmap
273 vvv
275 EOF
277 test_expect_success 'grep -f, ignore empty lines' '
278         git grep -f patterns >actual &&
279         test_cmp expected actual
282 cat >expected <<EOF
283 y:y yy
284 --
285 z:zzz
286 EOF
288 # Create 1024 file names that sort between "y" and "z" to make sure
289 # the two files are handled by different calls to an external grep.
290 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
291 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"
292 test_expect_success 'grep -C1, hunk mark between files' '
293         for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
294         git add y-?? &&
295         git grep -C1 "^[yz]" >actual &&
296         test_cmp expected actual
299 test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
300         git grep -C1 --no-ext-grep "^[yz]" >actual &&
301         test_cmp expected actual
304 test_expect_success 'log grep setup' '
305         echo a >>file &&
306         test_tick &&
307         GIT_AUTHOR_NAME="With * Asterisk" \
308         GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
309         git commit -a -m "second" &&
311         echo a >>file &&
312         test_tick &&
313         git commit -a -m "third"
317 test_expect_success 'log grep (1)' '
318         git log --author=author --pretty=tformat:%s >actual &&
319         ( echo third ; echo initial ) >expect &&
320         test_cmp expect actual
323 test_expect_success 'log grep (2)' '
324         git log --author=" * " -F --pretty=tformat:%s >actual &&
325         ( echo second ) >expect &&
326         test_cmp expect actual
329 test_expect_success 'log grep (3)' '
330         git log --author="^A U" --pretty=tformat:%s >actual &&
331         ( echo third ; echo initial ) >expect &&
332         test_cmp expect actual
335 test_expect_success 'log grep (4)' '
336         git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
337         ( echo second ) >expect &&
338         test_cmp expect actual
341 test_expect_success 'log grep (5)' '
342         git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
343         ( echo third ; echo initial ) >expect &&
344         test_cmp expect actual
347 test_expect_success 'log grep (6)' '
348         git log --author=-0700  --pretty=tformat:%s >actual &&
349         >expect &&
350         test_cmp expect actual
353 test_expect_success 'grep with CE_VALID file' '
354         git update-index --assume-unchanged t/t &&
355         rm t/t &&
356         test "$(git grep --no-ext-grep test)" = "t/t:test" &&
357         git update-index --no-assume-unchanged t/t &&
358         git checkout t/t
361 cat >expected <<EOF
362 hello.c=#include <stdio.h>
363 hello.c:        return 0;
364 EOF
366 test_expect_success 'grep -p with userdiff' '
367         git config diff.custom.funcname "^#" &&
368         echo "hello.c diff=custom" >.gitattributes &&
369         git grep -p return >actual &&
370         test_cmp expected actual
373 cat >expected <<EOF
374 hello.c=int main(int argc, const char **argv)
375 hello.c:        return 0;
376 EOF
378 test_expect_success 'grep -p' '
379         rm -f .gitattributes &&
380         git grep -p return >actual &&
381         test_cmp expected actual
384 cat >expected <<EOF
385 hello.c-#include <stdio.h>
386 hello.c=int main(int argc, const char **argv)
387 hello.c-{
388 hello.c-        printf("Hello world.\n");
389 hello.c:        return 0;
390 EOF
392 test_expect_success 'grep -p -B5' '
393         git grep -p -B5 return >actual &&
394         test_cmp expected actual
397 test_expect_success 'grep from a subdirectory to search wider area (1)' '
398         mkdir -p s &&
399         (
400                 cd s && git grep "x x x" ..
401         )
404 test_expect_success 'grep from a subdirectory to search wider area (2)' '
405         mkdir -p s &&
406         (
407                 cd s || exit 1
408                 ( git grep xxyyzz .. >out ; echo $? >status )
409                 ! test -s out &&
410                 test 1 = $(cat status)
411         )
414 test_done