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