]>
Commit | Line | Data |
---|---|---|
f25b7939 JH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Junio C Hamano | |
4 | # | |
5 | ||
0d042fec | 6 | test_description='git grep various. |
f25b7939 JH |
7 | ' |
8 | ||
9 | . ./test-lib.sh | |
10 | ||
2944e4e6 RS |
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; | |
5183bf67 | 17 | /* char ?? */ |
2944e4e6 RS |
18 | } |
19 | EOF | |
20 | ||
f25b7939 JH |
21 | test_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 && | |
8f852ce6 MK |
29 | { |
30 | echo Hello world | |
31 | echo HeLLo world | |
32 | echo Hello_world | |
33 | echo HeLLo_world | |
34 | } >hello_world && | |
f556e4af | 35 | { |
d0042abe MK |
36 | echo "a+b*c" |
37 | echo "a+bc" | |
38 | echo "abc" | |
f556e4af | 39 | } >ab && |
a91f453f | 40 | echo vvv >v && |
dbb6a4ad | 41 | echo ww w >w && |
f25b7939 JH |
42 | echo x x xx x >x && |
43 | echo y yy >y && | |
44 | echo zzz > z && | |
0d042fec JH |
45 | mkdir t && |
46 | echo test >t/t && | |
a91f453f MK |
47 | echo vvv >t/v && |
48 | mkdir t/a && | |
49 | echo vvv >t/a/v && | |
50 | git add . && | |
a4d7d2c6 | 51 | test_tick && |
f25b7939 JH |
52 | git commit -m initial |
53 | ' | |
54 | ||
c922b01f LT |
55 | test_expect_success 'grep should not segfault with a bad input' ' |
56 | test_must_fail git grep "(" | |
57 | ' | |
58 | ||
f25b7939 JH |
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 | |
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 && | |
b22520a3 JR |
73 | git -c grep.linenumber=false grep -n -w -e mmap $H >actual && |
74 | test_cmp expected actual | |
75 | ' | |
76 | ||
77 | test_expect_success "grep -w $L" ' | |
78 | { | |
79 | echo ${HC}file:1:foo mmap bar | |
80 | echo ${HC}file:3:foo_mmap bar mmap | |
81 | echo ${HC}file:4:foo mmap bar_mmap | |
82 | echo ${HC}file:5:foo_mmap bar mmap baz | |
83 | } >expected && | |
84 | git -c grep.linenumber=true grep -w -e mmap $H >actual && | |
85 | test_cmp expected actual | |
86 | ' | |
87 | ||
88 | test_expect_success "grep -w $L" ' | |
89 | { | |
90 | echo ${HC}file:foo mmap bar | |
91 | echo ${HC}file:foo_mmap bar mmap | |
92 | echo ${HC}file:foo mmap bar_mmap | |
93 | echo ${HC}file:foo_mmap bar mmap baz | |
94 | } >expected && | |
95 | git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual && | |
4fdf71be | 96 | test_cmp expected actual |
f25b7939 JH |
97 | ' |
98 | ||
dbb6a4ad RS |
99 | test_expect_success "grep -w $L (w)" ' |
100 | : >expected && | |
ce14e0b2 | 101 | test_must_fail git grep -n -w -e "^w" >actual && |
dbb6a4ad RS |
102 | test_cmp expected actual |
103 | ' | |
104 | ||
f25b7939 JH |
105 | test_expect_success "grep -w $L (x)" ' |
106 | { | |
107 | echo ${HC}x:1:x x xx x | |
108 | } >expected && | |
109 | git grep -n -w -e "x xx* x" $H >actual && | |
4fdf71be | 110 | test_cmp expected actual |
f25b7939 JH |
111 | ' |
112 | ||
113 | test_expect_success "grep -w $L (y-1)" ' | |
114 | { | |
115 | echo ${HC}y:1:y yy | |
116 | } >expected && | |
117 | git grep -n -w -e "^y" $H >actual && | |
4fdf71be | 118 | test_cmp expected actual |
f25b7939 JH |
119 | ' |
120 | ||
121 | test_expect_success "grep -w $L (y-2)" ' | |
122 | : >expected && | |
123 | if git grep -n -w -e "^y y" $H >actual | |
124 | then | |
125 | echo should not have matched | |
126 | cat actual | |
127 | false | |
128 | else | |
4fdf71be | 129 | test_cmp expected actual |
f25b7939 JH |
130 | fi |
131 | ' | |
132 | ||
133 | test_expect_success "grep -w $L (z)" ' | |
134 | : >expected && | |
135 | if git grep -n -w -e "^z" $H >actual | |
136 | then | |
137 | echo should not have matched | |
138 | cat actual | |
139 | false | |
140 | else | |
4fdf71be | 141 | test_cmp expected actual |
f25b7939 JH |
142 | fi |
143 | ' | |
0d042fec JH |
144 | |
145 | test_expect_success "grep $L (t-1)" ' | |
146 | echo "${HC}t/t:1:test" >expected && | |
147 | git grep -n -e test $H >actual && | |
4fdf71be | 148 | test_cmp expected actual |
0d042fec JH |
149 | ' |
150 | ||
151 | test_expect_success "grep $L (t-2)" ' | |
152 | echo "${HC}t:1:test" >expected && | |
153 | ( | |
154 | cd t && | |
155 | git grep -n -e test $H | |
156 | ) >actual && | |
4fdf71be | 157 | test_cmp expected actual |
0d042fec JH |
158 | ' |
159 | ||
160 | test_expect_success "grep $L (t-3)" ' | |
161 | echo "${HC}t/t:1:test" >expected && | |
162 | ( | |
163 | cd t && | |
164 | git grep --full-name -n -e test $H | |
165 | ) >actual && | |
4fdf71be | 166 | test_cmp expected actual |
0d042fec JH |
167 | ' |
168 | ||
41ac414e | 169 | test_expect_success "grep -c $L (no /dev/null)" ' |
87539416 | 170 | ! git grep -c test $H | grep /dev/null |
d99ebf08 JH |
171 | ' |
172 | ||
a91f453f MK |
173 | test_expect_success "grep --max-depth -1 $L" ' |
174 | { | |
175 | echo ${HC}t/a/v:1:vvv | |
176 | echo ${HC}t/v:1:vvv | |
177 | echo ${HC}v:1:vvv | |
178 | } >expected && | |
179 | git grep --max-depth -1 -n -e vvv $H >actual && | |
180 | test_cmp expected actual | |
181 | ' | |
182 | ||
183 | test_expect_success "grep --max-depth 0 $L" ' | |
184 | { | |
185 | echo ${HC}v:1:vvv | |
186 | } >expected && | |
187 | git grep --max-depth 0 -n -e vvv $H >actual && | |
188 | test_cmp expected actual | |
189 | ' | |
190 | ||
191 | test_expect_success "grep --max-depth 0 -- '*' $L" ' | |
192 | { | |
193 | echo ${HC}t/a/v:1:vvv | |
194 | echo ${HC}t/v:1:vvv | |
195 | echo ${HC}v:1:vvv | |
196 | } >expected && | |
197 | git grep --max-depth 0 -n -e vvv $H -- "*" >actual && | |
198 | test_cmp expected actual | |
199 | ' | |
200 | ||
201 | test_expect_success "grep --max-depth 1 $L" ' | |
202 | { | |
203 | echo ${HC}t/v:1:vvv | |
204 | echo ${HC}v:1:vvv | |
205 | } >expected && | |
206 | git grep --max-depth 1 -n -e vvv $H >actual && | |
207 | test_cmp expected actual | |
208 | ' | |
209 | ||
210 | test_expect_success "grep --max-depth 0 -- t $L" ' | |
211 | { | |
212 | echo ${HC}t/v:1:vvv | |
213 | } >expected && | |
214 | git grep --max-depth 0 -n -e vvv $H -- t >actual && | |
215 | test_cmp expected actual | |
216 | ' | |
217 | ||
b31f6880 NTND |
218 | test_expect_success "grep --max-depth 0 -- . t $L" ' |
219 | { | |
220 | echo ${HC}t/v:1:vvv | |
221 | echo ${HC}v:1:vvv | |
222 | } >expected && | |
223 | git grep --max-depth 0 -n -e vvv $H -- . t >actual && | |
224 | test_cmp expected actual | |
225 | ' | |
226 | ||
227 | test_expect_success "grep --max-depth 0 -- t . $L" ' | |
228 | { | |
229 | echo ${HC}t/v:1:vvv | |
230 | echo ${HC}v:1:vvv | |
231 | } >expected && | |
232 | git grep --max-depth 0 -n -e vvv $H -- t . >actual && | |
233 | test_cmp expected actual | |
234 | ' | |
f556e4af | 235 | test_expect_success "grep $L with grep.extendedRegexp=false" ' |
d0042abe MK |
236 | echo "ab:a+bc" >expected && |
237 | git -c grep.extendedRegexp=false grep "a+b*c" ab >actual && | |
f556e4af MK |
238 | test_cmp expected actual |
239 | ' | |
b31f6880 | 240 | |
f556e4af | 241 | test_expect_success "grep $L with grep.extendedRegexp=true" ' |
d0042abe MK |
242 | echo "ab:abc" >expected && |
243 | git -c grep.extendedRegexp=true grep "a+b*c" ab >actual && | |
f556e4af MK |
244 | test_cmp expected actual |
245 | ' | |
f25b7939 JH |
246 | done |
247 | ||
0f705046 TR |
248 | cat >expected <<EOF |
249 | file:foo mmap bar_mmap | |
250 | EOF | |
251 | ||
252 | test_expect_success 'grep -e A --and -e B' ' | |
253 | git grep -e "foo mmap" --and -e bar_mmap >actual && | |
254 | test_cmp expected actual | |
255 | ' | |
256 | ||
257 | cat >expected <<EOF | |
258 | file:foo_mmap bar mmap | |
259 | file:foo_mmap bar mmap baz | |
260 | EOF | |
261 | ||
262 | ||
263 | test_expect_success 'grep ( -e A --or -e B ) --and -e B' ' | |
264 | git grep \( -e foo_ --or -e baz \) \ | |
265 | --and -e " mmap" >actual && | |
266 | test_cmp expected actual | |
267 | ' | |
268 | ||
269 | cat >expected <<EOF | |
270 | file:foo mmap bar | |
271 | EOF | |
272 | ||
273 | test_expect_success 'grep -e A --and --not -e B' ' | |
274 | git grep -e "foo mmap" --and --not -e bar_mmap >actual && | |
275 | test_cmp expected actual | |
276 | ' | |
277 | ||
b4827599 RS |
278 | test_expect_success 'grep should ignore GREP_OPTIONS' ' |
279 | GREP_OPTIONS=-v git grep " mmap bar\$" >actual && | |
280 | test_cmp expected actual | |
281 | ' | |
282 | ||
cfe370c6 MK |
283 | test_expect_success 'grep -f, non-existent file' ' |
284 | test_must_fail git grep -f patterns | |
285 | ' | |
286 | ||
287 | cat >expected <<EOF | |
288 | file:foo mmap bar | |
289 | file:foo_mmap bar | |
290 | file:foo_mmap bar mmap | |
291 | file:foo mmap bar_mmap | |
292 | file:foo_mmap bar mmap baz | |
293 | EOF | |
294 | ||
295 | cat >pattern <<EOF | |
296 | mmap | |
297 | EOF | |
298 | ||
299 | test_expect_success 'grep -f, one pattern' ' | |
300 | git grep -f pattern >actual && | |
301 | test_cmp expected actual | |
302 | ' | |
303 | ||
304 | cat >expected <<EOF | |
305 | file:foo mmap bar | |
306 | file:foo_mmap bar | |
307 | file:foo_mmap bar mmap | |
308 | file:foo mmap bar_mmap | |
309 | file:foo_mmap bar mmap baz | |
310 | t/a/v:vvv | |
311 | t/v:vvv | |
312 | v:vvv | |
313 | EOF | |
314 | ||
315 | cat >patterns <<EOF | |
316 | mmap | |
317 | vvv | |
318 | EOF | |
319 | ||
320 | test_expect_success 'grep -f, multiple patterns' ' | |
321 | git grep -f patterns >actual && | |
322 | test_cmp expected actual | |
323 | ' | |
324 | ||
325 | cat >expected <<EOF | |
326 | file:foo mmap bar | |
327 | file:foo_mmap bar | |
328 | file:foo_mmap bar mmap | |
329 | file:foo mmap bar_mmap | |
330 | file:foo_mmap bar mmap baz | |
331 | t/a/v:vvv | |
332 | t/v:vvv | |
333 | v:vvv | |
334 | EOF | |
335 | ||
336 | cat >patterns <<EOF | |
337 | ||
338 | mmap | |
339 | ||
340 | vvv | |
341 | ||
342 | EOF | |
343 | ||
344 | test_expect_success 'grep -f, ignore empty lines' ' | |
345 | git grep -f patterns >actual && | |
346 | test_cmp expected actual | |
347 | ' | |
348 | ||
c41dd2fd RS |
349 | test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' ' |
350 | git grep -f - <patterns >actual && | |
351 | test_cmp expected actual | |
352 | ' | |
353 | ||
046802d0 RS |
354 | cat >expected <<EOF |
355 | y:y yy | |
356 | -- | |
357 | z:zzz | |
358 | EOF | |
359 | ||
4ff61c21 JH |
360 | test_expect_success 'grep -q, silently report matches' ' |
361 | >empty && | |
362 | git grep -q mmap >actual && | |
363 | test_cmp empty actual && | |
364 | test_must_fail git grep -q qfwfq >actual && | |
365 | test_cmp empty actual | |
366 | ' | |
367 | ||
046802d0 RS |
368 | # Create 1024 file names that sort between "y" and "z" to make sure |
369 | # the two files are handled by different calls to an external grep. | |
370 | # This depends on MAXARGS in builtin-grep.c being 1024 or less. | |
371 | 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" | |
372 | test_expect_success 'grep -C1, hunk mark between files' ' | |
373 | for a in $c32; do for b in $c32; do : >y-$a$b; done; done && | |
374 | git add y-?? && | |
375 | git grep -C1 "^[yz]" >actual && | |
376 | test_cmp expected actual | |
377 | ' | |
378 | ||
bbc09c22 JH |
379 | test_expect_success 'grep -C1 hunk mark between files' ' |
380 | git grep -C1 "^[yz]" >actual && | |
046802d0 RS |
381 | test_cmp expected actual |
382 | ' | |
383 | ||
a4d7d2c6 JH |
384 | test_expect_success 'log grep setup' ' |
385 | echo a >>file && | |
386 | test_tick && | |
387 | GIT_AUTHOR_NAME="With * Asterisk" \ | |
388 | GIT_AUTHOR_EMAIL="xyzzy@frotz.com" \ | |
389 | git commit -a -m "second" && | |
390 | ||
391 | echo a >>file && | |
392 | test_tick && | |
5aaeb733 | 393 | git commit -a -m "third" && |
a4d7d2c6 | 394 | |
5aaeb733 JH |
395 | echo a >>file && |
396 | test_tick && | |
397 | GIT_AUTHOR_NAME="Night Fall" \ | |
398 | GIT_AUTHOR_EMAIL="nitfol@frobozz.com" \ | |
399 | git commit -a -m "fourth" | |
a4d7d2c6 JH |
400 | ' |
401 | ||
402 | test_expect_success 'log grep (1)' ' | |
403 | git log --author=author --pretty=tformat:%s >actual && | |
404 | ( echo third ; echo initial ) >expect && | |
405 | test_cmp expect actual | |
406 | ' | |
407 | ||
408 | test_expect_success 'log grep (2)' ' | |
409 | git log --author=" * " -F --pretty=tformat:%s >actual && | |
410 | ( echo second ) >expect && | |
411 | test_cmp expect actual | |
412 | ' | |
413 | ||
414 | test_expect_success 'log grep (3)' ' | |
415 | git log --author="^A U" --pretty=tformat:%s >actual && | |
416 | ( echo third ; echo initial ) >expect && | |
417 | test_cmp expect actual | |
418 | ' | |
419 | ||
420 | test_expect_success 'log grep (4)' ' | |
421 | git log --author="frotz\.com>$" --pretty=tformat:%s >actual && | |
422 | ( echo second ) >expect && | |
423 | test_cmp expect actual | |
424 | ' | |
425 | ||
426 | test_expect_success 'log grep (5)' ' | |
80235ba7 | 427 | git log --author=Thor -F --pretty=tformat:%s >actual && |
a4d7d2c6 JH |
428 | ( echo third ; echo initial ) >expect && |
429 | test_cmp expect actual | |
430 | ' | |
431 | ||
432 | test_expect_success 'log grep (6)' ' | |
433 | git log --author=-0700 --pretty=tformat:%s >actual && | |
434 | >expect && | |
435 | test_cmp expect actual | |
57d43466 | 436 | ' |
a4d7d2c6 | 437 | |
80235ba7 JH |
438 | test_expect_success 'log --grep --author implicitly uses all-match' ' |
439 | # grep matches initial and second but not third | |
440 | # author matches only initial and third | |
441 | git log --author="A U Thor" --grep=s --grep=l --format=%s >actual && | |
442 | echo initial >expect && | |
443 | test_cmp expect actual | |
444 | ' | |
445 | ||
5aaeb733 JH |
446 | test_expect_success 'log with multiple --author uses union' ' |
447 | git log --author="Thor" --author="Aster" --format=%s >actual && | |
448 | { | |
449 | echo third && echo second && echo initial | |
450 | } >expect && | |
451 | test_cmp expect actual | |
452 | ' | |
453 | ||
454 | test_expect_success 'log with --grep and multiple --author uses all-match' ' | |
455 | git log --author="Thor" --author="Night" --grep=i --format=%s >actual && | |
456 | { | |
457 | echo third && echo initial | |
458 | } >expect && | |
459 | test_cmp expect actual | |
460 | ' | |
461 | ||
462 | test_expect_success 'log with --grep and multiple --author uses all-match' ' | |
463 | git log --author="Thor" --author="Night" --grep=q --format=%s >actual && | |
464 | >expect && | |
465 | test_cmp expect actual | |
466 | ' | |
467 | ||
57d43466 NTND |
468 | test_expect_success 'grep with CE_VALID file' ' |
469 | git update-index --assume-unchanged t/t && | |
470 | rm t/t && | |
bbc09c22 | 471 | test "$(git grep test)" = "t/t:test" && |
57d43466 NTND |
472 | git update-index --no-assume-unchanged t/t && |
473 | git checkout t/t | |
a4d7d2c6 JH |
474 | ' |
475 | ||
60ecac98 RS |
476 | cat >expected <<EOF |
477 | hello.c=#include <stdio.h> | |
478 | hello.c: return 0; | |
479 | EOF | |
480 | ||
481 | test_expect_success 'grep -p with userdiff' ' | |
482 | git config diff.custom.funcname "^#" && | |
483 | echo "hello.c diff=custom" >.gitattributes && | |
484 | git grep -p return >actual && | |
485 | test_cmp expected actual | |
486 | ' | |
487 | ||
2944e4e6 RS |
488 | cat >expected <<EOF |
489 | hello.c=int main(int argc, const char **argv) | |
490 | hello.c: return 0; | |
491 | EOF | |
492 | ||
493 | test_expect_success 'grep -p' ' | |
60ecac98 | 494 | rm -f .gitattributes && |
2944e4e6 RS |
495 | git grep -p return >actual && |
496 | test_cmp expected actual | |
497 | ' | |
498 | ||
499 | cat >expected <<EOF | |
500 | hello.c-#include <stdio.h> | |
501 | hello.c=int main(int argc, const char **argv) | |
502 | hello.c-{ | |
503 | hello.c- printf("Hello world.\n"); | |
504 | hello.c: return 0; | |
505 | EOF | |
506 | ||
507 | test_expect_success 'grep -p -B5' ' | |
508 | git grep -p -B5 return >actual && | |
509 | test_cmp expected actual | |
510 | ' | |
511 | ||
ba8ea749 RS |
512 | cat >expected <<EOF |
513 | hello.c=int main(int argc, const char **argv) | |
514 | hello.c-{ | |
515 | hello.c- printf("Hello world.\n"); | |
516 | hello.c: return 0; | |
517 | hello.c- /* char ?? */ | |
518 | hello.c-} | |
519 | EOF | |
520 | ||
521 | test_expect_success 'grep -W' ' | |
522 | git grep -W return >actual && | |
523 | test_cmp expected actual | |
524 | ' | |
525 | ||
493b7a08 CB |
526 | test_expect_success 'grep from a subdirectory to search wider area (1)' ' |
527 | mkdir -p s && | |
528 | ( | |
529 | cd s && git grep "x x x" .. | |
530 | ) | |
531 | ' | |
532 | ||
533 | test_expect_success 'grep from a subdirectory to search wider area (2)' ' | |
534 | mkdir -p s && | |
535 | ( | |
536 | cd s || exit 1 | |
537 | ( git grep xxyyzz .. >out ; echo $? >status ) | |
538 | ! test -s out && | |
539 | test 1 = $(cat status) | |
540 | ) | |
541 | ' | |
542 | ||
5183bf67 BC |
543 | cat >expected <<EOF |
544 | hello.c:int main(int argc, const char **argv) | |
545 | EOF | |
546 | ||
547 | test_expect_success 'grep -Fi' ' | |
548 | git grep -Fi "CHAR *" >actual && | |
549 | test_cmp expected actual | |
550 | ' | |
551 | ||
59332d13 JH |
552 | test_expect_success 'outside of git repository' ' |
553 | rm -fr non && | |
554 | mkdir -p non/git/sub && | |
555 | echo hello >non/git/file1 && | |
556 | echo world >non/git/sub/file2 && | |
59332d13 JH |
557 | { |
558 | echo file1:hello && | |
559 | echo sub/file2:world | |
560 | } >non/expect.full && | |
a48fcd83 | 561 | echo file2:world >non/expect.sub && |
59332d13 JH |
562 | ( |
563 | GIT_CEILING_DIRECTORIES="$(pwd)/non/git" && | |
564 | export GIT_CEILING_DIRECTORIES && | |
565 | cd non/git && | |
566 | test_must_fail git grep o && | |
567 | git grep --no-index o >../actual.full && | |
568 | test_cmp ../expect.full ../actual.full | |
569 | cd sub && | |
570 | test_must_fail git grep o && | |
571 | git grep --no-index o >../../actual.sub && | |
572 | test_cmp ../../expect.sub ../../actual.sub | |
0a93fb8a JH |
573 | ) && |
574 | ||
575 | echo ".*o*" >non/git/.gitignore && | |
576 | ( | |
577 | GIT_CEILING_DIRECTORIES="$(pwd)/non/git" && | |
578 | export GIT_CEILING_DIRECTORIES && | |
579 | cd non/git && | |
580 | test_must_fail git grep o && | |
581 | git grep --no-index --exclude-standard o >../actual.full && | |
582 | test_cmp ../expect.full ../actual.full && | |
583 | ||
584 | { | |
585 | echo ".gitignore:.*o*" | |
586 | cat ../expect.full | |
587 | } >../expect.with.ignored && | |
588 | git grep --no-index --no-exclude o >../actual.full && | |
589 | test_cmp ../expect.with.ignored ../actual.full | |
59332d13 JH |
590 | ) |
591 | ' | |
592 | ||
593 | test_expect_success 'inside git repository but with --no-index' ' | |
594 | rm -fr is && | |
595 | mkdir -p is/git/sub && | |
596 | echo hello >is/git/file1 && | |
597 | echo world >is/git/sub/file2 && | |
598 | echo ".*o*" >is/git/.gitignore && | |
599 | { | |
600 | echo file1:hello && | |
601 | echo sub/file2:world | |
0a93fb8a JH |
602 | } >is/expect.unignored && |
603 | { | |
604 | echo ".gitignore:.*o*" && | |
605 | cat is/expect.unignored | |
59332d13 JH |
606 | } >is/expect.full && |
607 | : >is/expect.empty && | |
a48fcd83 | 608 | echo file2:world >is/expect.sub && |
59332d13 JH |
609 | ( |
610 | cd is/git && | |
611 | git init && | |
612 | test_must_fail git grep o >../actual.full && | |
613 | test_cmp ../expect.empty ../actual.full && | |
0a93fb8a JH |
614 | |
615 | git grep --untracked o >../actual.unignored && | |
616 | test_cmp ../expect.unignored ../actual.unignored && | |
617 | ||
59332d13 JH |
618 | git grep --no-index o >../actual.full && |
619 | test_cmp ../expect.full ../actual.full && | |
0a93fb8a JH |
620 | |
621 | git grep --no-index --exclude-standard o >../actual.unignored && | |
622 | test_cmp ../expect.unignored ../actual.unignored && | |
623 | ||
59332d13 JH |
624 | cd sub && |
625 | test_must_fail git grep o >../../actual.sub && | |
626 | test_cmp ../../expect.empty ../../actual.sub && | |
0a93fb8a | 627 | |
59332d13 | 628 | git grep --no-index o >../../actual.sub && |
0a93fb8a JH |
629 | test_cmp ../../expect.sub ../../actual.sub && |
630 | ||
631 | git grep --untracked o >../../actual.sub && | |
59332d13 JH |
632 | test_cmp ../../expect.sub ../../actual.sub |
633 | ) | |
634 | ' | |
635 | ||
1123c67c JK |
636 | test_expect_success 'setup double-dash tests' ' |
637 | cat >double-dash <<EOF && | |
638 | -- | |
639 | -> | |
640 | other | |
641 | EOF | |
642 | git add double-dash | |
643 | ' | |
644 | ||
645 | cat >expected <<EOF | |
646 | double-dash:-> | |
647 | EOF | |
648 | test_expect_success 'grep -- pattern' ' | |
649 | git grep -- "->" >actual && | |
650 | test_cmp expected actual | |
651 | ' | |
652 | test_expect_success 'grep -- pattern -- pathspec' ' | |
653 | git grep -- "->" -- double-dash >actual && | |
654 | test_cmp expected actual | |
655 | ' | |
656 | test_expect_success 'grep -e pattern -- path' ' | |
657 | git grep -e "->" -- double-dash >actual && | |
658 | test_cmp expected actual | |
659 | ' | |
660 | ||
661 | cat >expected <<EOF | |
662 | double-dash:-- | |
663 | EOF | |
664 | test_expect_success 'grep -e -- -- path' ' | |
665 | git grep -e -- -- double-dash >actual && | |
666 | test_cmp expected actual | |
667 | ' | |
668 | ||
8f852ce6 MK |
669 | cat >expected <<EOF |
670 | hello.c:int main(int argc, const char **argv) | |
671 | hello.c: printf("Hello world.\n"); | |
672 | EOF | |
673 | ||
674 | test_expect_success LIBPCRE 'grep --perl-regexp pattern' ' | |
675 | git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual && | |
676 | test_cmp expected actual | |
677 | ' | |
678 | ||
679 | test_expect_success LIBPCRE 'grep -P pattern' ' | |
680 | git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual && | |
681 | test_cmp expected actual | |
682 | ' | |
683 | ||
f556e4af | 684 | test_expect_success 'grep pattern with grep.extendedRegexp=true' ' |
d0042abe | 685 | >empty && |
f556e4af MK |
686 | test_must_fail git -c grep.extendedregexp=true \ |
687 | grep "\p{Ps}.*?\p{Pe}" hello.c >actual && | |
688 | test_cmp empty actual | |
689 | ' | |
690 | ||
691 | test_expect_success LIBPCRE 'grep -P pattern with grep.extendedRegexp=true' ' | |
692 | git -c grep.extendedregexp=true \ | |
693 | grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual && | |
694 | test_cmp expected actual | |
695 | ' | |
696 | ||
697 | test_expect_success LIBPCRE 'grep -P -v pattern' ' | |
698 | { | |
d0042abe MK |
699 | echo "ab:a+b*c" |
700 | echo "ab:a+bc" | |
f556e4af | 701 | } >expected && |
d0042abe | 702 | git grep -P -v "abc" ab >actual && |
f556e4af MK |
703 | test_cmp expected actual |
704 | ' | |
705 | ||
8f852ce6 | 706 | test_expect_success LIBPCRE 'grep -P -i pattern' ' |
93d5e0c2 JH |
707 | cat >expected <<-EOF && |
708 | hello.c: printf("Hello world.\n"); | |
709 | EOF | |
8f852ce6 MK |
710 | git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual && |
711 | test_cmp expected actual | |
712 | ' | |
713 | ||
714 | test_expect_success LIBPCRE 'grep -P -w pattern' ' | |
715 | { | |
716 | echo "hello_world:Hello world" | |
717 | echo "hello_world:HeLLo world" | |
718 | } >expected && | |
719 | git grep -P -w "He((?i)ll)o" hello_world >actual && | |
720 | test_cmp expected actual | |
721 | ' | |
722 | ||
f556e4af MK |
723 | test_expect_success 'grep -G invalidpattern properly dies ' ' |
724 | test_must_fail git grep -G "a[" | |
725 | ' | |
726 | ||
727 | test_expect_success 'grep -E invalidpattern properly dies ' ' | |
728 | test_must_fail git grep -E "a[" | |
729 | ' | |
730 | ||
731 | test_expect_success LIBPCRE 'grep -P invalidpattern properly dies ' ' | |
732 | test_must_fail git grep -P "a[" | |
733 | ' | |
734 | ||
d0042abe MK |
735 | test_expect_success 'grep -G -E -F pattern' ' |
736 | echo "ab:a+b*c" >expected && | |
737 | git grep -G -E -F "a+b*c" ab >actual && | |
f556e4af MK |
738 | test_cmp expected actual |
739 | ' | |
740 | ||
741 | test_expect_success 'grep -E -F -G pattern' ' | |
d0042abe MK |
742 | echo "ab:a+bc" >expected && |
743 | git grep -E -F -G "a+b*c" ab >actual && | |
f556e4af MK |
744 | test_cmp expected actual |
745 | ' | |
746 | ||
d0042abe MK |
747 | test_expect_success 'grep -F -G -E pattern' ' |
748 | echo "ab:abc" >expected && | |
749 | git grep -F -G -E "a+b*c" ab >actual && | |
f556e4af MK |
750 | test_cmp expected actual |
751 | ' | |
752 | ||
d0042abe MK |
753 | test_expect_success 'grep -G -F -P -E pattern' ' |
754 | >empty && | |
755 | test_must_fail git grep -G -F -P -E "a\x{2b}b\x{2a}c" ab >actual && | |
756 | test_cmp empty actual | |
f556e4af MK |
757 | ' |
758 | ||
d0042abe MK |
759 | test_expect_success LIBPCRE 'grep -G -F -E -P pattern' ' |
760 | echo "ab:a+b*c" >expected && | |
761 | git grep -G -F -E -P "a\x{2b}b\x{2a}c" ab >actual && | |
f556e4af MK |
762 | test_cmp expected actual |
763 | ' | |
764 | ||
08303c36 RS |
765 | test_config() { |
766 | git config "$1" "$2" && | |
767 | test_when_finished "git config --unset $1" | |
768 | } | |
769 | ||
770 | cat >expected <<EOF | |
771 | hello.c<RED>:<RESET>int main(int argc, const char **argv) | |
772 | hello.c<RED>-<RESET>{ | |
773 | <RED>--<RESET> | |
774 | hello.c<RED>:<RESET> /* char ?? */ | |
775 | hello.c<RED>-<RESET>} | |
776 | <RED>--<RESET> | |
777 | hello_world<RED>:<RESET>Hello_world | |
778 | hello_world<RED>-<RESET>HeLLo_world | |
779 | EOF | |
780 | ||
781 | test_expect_success 'grep --color, separator' ' | |
782 | test_config color.grep.context normal && | |
783 | test_config color.grep.filename normal && | |
784 | test_config color.grep.function normal && | |
785 | test_config color.grep.linenumber normal && | |
786 | test_config color.grep.match normal && | |
787 | test_config color.grep.selected normal && | |
788 | test_config color.grep.separator red && | |
789 | ||
790 | git grep --color=always -A1 -e char -e lo_w hello.c hello_world | | |
791 | test_decode_color >actual && | |
792 | test_cmp expected actual | |
793 | ' | |
794 | ||
a8f0e764 RS |
795 | cat >expected <<EOF |
796 | hello.c:int main(int argc, const char **argv) | |
797 | hello.c: /* char ?? */ | |
798 | ||
799 | hello_world:Hello_world | |
800 | EOF | |
801 | ||
802 | test_expect_success 'grep --break' ' | |
803 | git grep --break -e char -e lo_w hello.c hello_world >actual && | |
804 | test_cmp expected actual | |
805 | ' | |
806 | ||
807 | cat >expected <<EOF | |
808 | hello.c:int main(int argc, const char **argv) | |
809 | hello.c-{ | |
810 | -- | |
811 | hello.c: /* char ?? */ | |
812 | hello.c-} | |
813 | ||
814 | hello_world:Hello_world | |
815 | hello_world-HeLLo_world | |
816 | EOF | |
817 | ||
818 | test_expect_success 'grep --break with context' ' | |
819 | git grep --break -A1 -e char -e lo_w hello.c hello_world >actual && | |
820 | test_cmp expected actual | |
821 | ' | |
822 | ||
1d84f72e RS |
823 | cat >expected <<EOF |
824 | hello.c | |
825 | int main(int argc, const char **argv) | |
826 | /* char ?? */ | |
827 | hello_world | |
828 | Hello_world | |
829 | EOF | |
830 | ||
831 | test_expect_success 'grep --heading' ' | |
832 | git grep --heading -e char -e lo_w hello.c hello_world >actual && | |
833 | test_cmp expected actual | |
834 | ' | |
835 | ||
836 | cat >expected <<EOF | |
837 | <BOLD;GREEN>hello.c<RESET> | |
838 | 2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv) | |
839 | 6: /* <BLACK;BYELLOW>char<RESET> ?? */ | |
840 | ||
841 | <BOLD;GREEN>hello_world<RESET> | |
842 | 3:Hel<BLACK;BYELLOW>lo_w<RESET>orld | |
843 | EOF | |
844 | ||
845 | test_expect_success 'mimic ack-grep --group' ' | |
846 | test_config color.grep.context normal && | |
847 | test_config color.grep.filename "bold green" && | |
848 | test_config color.grep.function normal && | |
849 | test_config color.grep.linenumber normal && | |
850 | test_config color.grep.match "black yellow" && | |
851 | test_config color.grep.selected normal && | |
852 | test_config color.grep.separator normal && | |
853 | ||
854 | git grep --break --heading -n --color \ | |
855 | -e char -e lo_w hello.c hello_world | | |
856 | test_decode_color >actual && | |
857 | test_cmp expected actual | |
858 | ' | |
859 | ||
f25b7939 | 860 | test_done |