Code

99142fd6bda0e9c241bad1c18a18583b30fda700
[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 test_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 '
23 cat >hello.c <<EOF
24 #include <stdio.h>
25 int main(int argc, const char **argv)
26 {
27         printf("Hello world.\n");
28         return 0;
29 }
30 EOF
32 test_expect_success setup '
33         {
34                 echo foo mmap bar
35                 echo foo_mmap bar
36                 echo foo_mmap bar mmap
37                 echo foo mmap bar_mmap
38                 echo foo_mmap bar mmap baz
39         } >file &&
40         echo vvv >v &&
41         echo ww w >w &&
42         echo x x xx x >x &&
43         echo y yy >y &&
44         echo zzz > z &&
45         mkdir t &&
46         echo test >t/t &&
47         echo vvv >t/v &&
48         mkdir t/a &&
49         echo vvv >t/a/v &&
50         git add . &&
51         test_tick &&
52         git commit -m initial
53 '
55 test_expect_success 'grep should not segfault with a bad input' '
56         test_must_fail git grep "("
57 '
59 for H in HEAD ''
60 do
61         case "$H" in
62         HEAD)   HC='HEAD:' L='HEAD' ;;
63         '')     HC= L='in working tree' ;;
64         esac
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 grep -n -w -e mmap $H >actual &&
74                 diff expected actual
75         '
77         test_expect_success "grep -w $L (w)" '
78                 : >expected &&
79                 ! git grep -n -w -e "^w" >actual &&
80                 test_cmp expected actual
81         '
83         test_expect_success "grep -w $L (x)" '
84                 {
85                         echo ${HC}x:1:x x xx x
86                 } >expected &&
87                 git grep -n -w -e "x xx* x" $H >actual &&
88                 diff expected actual
89         '
91         test_expect_success "grep -w $L (y-1)" '
92                 {
93                         echo ${HC}y:1:y yy
94                 } >expected &&
95                 git grep -n -w -e "^y" $H >actual &&
96                 diff expected actual
97         '
99         test_expect_success "grep -w $L (y-2)" '
100                 : >expected &&
101                 if git grep -n -w -e "^y y" $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 -w $L (z)" '
112                 : >expected &&
113                 if git grep -n -w -e "^z" $H >actual
114                 then
115                         echo should not have matched
116                         cat actual
117                         false
118                 else
119                         diff expected actual
120                 fi
121         '
123         test_expect_success "grep $L (t-1)" '
124                 echo "${HC}t/t:1:test" >expected &&
125                 git grep -n -e test $H >actual &&
126                 diff expected actual
127         '
129         test_expect_success "grep $L (t-2)" '
130                 echo "${HC}t:1:test" >expected &&
131                 (
132                         cd t &&
133                         git grep -n -e test $H
134                 ) >actual &&
135                 diff expected actual
136         '
138         test_expect_success "grep $L (t-3)" '
139                 echo "${HC}t/t:1:test" >expected &&
140                 (
141                         cd t &&
142                         git grep --full-name -n -e test $H
143                 ) >actual &&
144                 diff expected actual
145         '
147         test_expect_success "grep -c $L (no /dev/null)" '
148                 ! git grep -c test $H | grep /dev/null
149         '
151         test_expect_success "grep --max-depth -1 $L" '
152                 {
153                         echo ${HC}t/a/v:1:vvv
154                         echo ${HC}t/v:1:vvv
155                         echo ${HC}v:1:vvv
156                 } >expected &&
157                 git grep --max-depth -1 -n -e vvv $H >actual &&
158                 test_cmp expected actual
159         '
161         test_expect_success "grep --max-depth 0 $L" '
162                 {
163                         echo ${HC}v:1:vvv
164                 } >expected &&
165                 git grep --max-depth 0 -n -e vvv $H >actual &&
166                 test_cmp expected actual
167         '
169         test_expect_success "grep --max-depth 0 -- '*' $L" '
170                 {
171                         echo ${HC}t/a/v:1:vvv
172                         echo ${HC}t/v:1:vvv
173                         echo ${HC}v:1:vvv
174                 } >expected &&
175                 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
176                 test_cmp expected actual
177         '
179         test_expect_success "grep --max-depth 1 $L" '
180                 {
181                         echo ${HC}t/v:1:vvv
182                         echo ${HC}v:1:vvv
183                 } >expected &&
184                 git grep --max-depth 1 -n -e vvv $H >actual &&
185                 test_cmp expected actual
186         '
188         test_expect_success "grep --max-depth 0 -- t $L" '
189                 {
190                         echo ${HC}t/v:1:vvv
191                 } >expected &&
192                 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
193                 test_cmp expected actual
194         '
196 done
198 cat >expected <<EOF
199 file:foo mmap bar_mmap
200 EOF
202 test_expect_success 'grep -e A --and -e B' '
203         git grep -e "foo mmap" --and -e bar_mmap >actual &&
204         test_cmp expected actual
207 cat >expected <<EOF
208 file:foo_mmap bar mmap
209 file:foo_mmap bar mmap baz
210 EOF
213 test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
214         git grep \( -e foo_ --or -e baz \) \
215                 --and -e " mmap" >actual &&
216         test_cmp expected actual
219 cat >expected <<EOF
220 file:foo mmap bar
221 EOF
223 test_expect_success 'grep -e A --and --not -e B' '
224         git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
225         test_cmp expected actual
228 cat >expected <<EOF
229 y:y yy
230 --
231 z:zzz
232 EOF
234 # Create 1024 file names that sort between "y" and "z" to make sure
235 # the two files are handled by different calls to an external grep.
236 # This depends on MAXARGS in builtin-grep.c being 1024 or less.
237 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"
238 test_expect_success 'grep -C1, hunk mark between files' '
239         for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
240         git add y-?? &&
241         git grep -C1 "^[yz]" >actual &&
242         test_cmp expected actual
245 test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
246         git grep -C1 --no-ext-grep "^[yz]" >actual &&
247         test_cmp expected actual
250 test_expect_success 'log grep setup' '
251         echo a >>file &&
252         test_tick &&
253         GIT_AUTHOR_NAME="With * Asterisk" \
254         GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \
255         git commit -a -m "second" &&
257         echo a >>file &&
258         test_tick &&
259         git commit -a -m "third"
263 test_expect_success 'log grep (1)' '
264         git log --author=author --pretty=tformat:%s >actual &&
265         ( echo third ; echo initial ) >expect &&
266         test_cmp expect actual
269 test_expect_success 'log grep (2)' '
270         git log --author=" * " -F --pretty=tformat:%s >actual &&
271         ( echo second ) >expect &&
272         test_cmp expect actual
275 test_expect_success 'log grep (3)' '
276         git log --author="^A U" --pretty=tformat:%s >actual &&
277         ( echo third ; echo initial ) >expect &&
278         test_cmp expect actual
281 test_expect_success 'log grep (4)' '
282         git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
283         ( echo second ) >expect &&
284         test_cmp expect actual
287 test_expect_success 'log grep (5)' '
288         git log --author=Thor -F --grep=Thu --pretty=tformat:%s >actual &&
289         ( echo third ; echo initial ) >expect &&
290         test_cmp expect actual
293 test_expect_success 'log grep (6)' '
294         git log --author=-0700  --pretty=tformat:%s >actual &&
295         >expect &&
296         test_cmp expect actual
299 test_expect_success 'grep with CE_VALID file' '
300         git update-index --assume-unchanged t/t &&
301         rm t/t &&
302         test "$(git grep --no-ext-grep test)" = "t/t:test" &&
303         git update-index --no-assume-unchanged t/t &&
304         git checkout t/t
307 cat >expected <<EOF
308 hello.c=#include <stdio.h>
309 hello.c:        return 0;
310 EOF
312 test_expect_success 'grep -p with userdiff' '
313         git config diff.custom.funcname "^#" &&
314         echo "hello.c diff=custom" >.gitattributes &&
315         git grep -p return >actual &&
316         test_cmp expected actual
319 cat >expected <<EOF
320 hello.c=int main(int argc, const char **argv)
321 hello.c:        return 0;
322 EOF
324 test_expect_success 'grep -p' '
325         rm -f .gitattributes &&
326         git grep -p return >actual &&
327         test_cmp expected actual
330 cat >expected <<EOF
331 hello.c-#include <stdio.h>
332 hello.c=int main(int argc, const char **argv)
333 hello.c-{
334 hello.c-        printf("Hello world.\n");
335 hello.c:        return 0;
336 EOF
338 test_expect_success 'grep -p -B5' '
339         git grep -p -B5 return >actual &&
340         test_cmp expected actual
343 test_expect_success EXTGREP 'external grep is called' '
344         GIT_TRACE=2 git grep foo >/dev/null 2>actual &&
345         grep "trace: grep:.*foo" actual >/dev/null
348 test_expect_success EXTGREP 'no external grep when skip-worktree entries exist' '
349         git update-index --skip-worktree file &&
350         GIT_TRACE=2 git grep foo >/dev/null 2>actual &&
351         ! grep "trace: grep:" actual >/dev/null &&
352         git update-index --no-skip-worktree file
355 test_done