-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht7810-grep.sh
executable file
·1932 lines (1679 loc) · 48.4 KB
/
t7810-grep.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# Copyright (c) 2006 Junio C Hamano
#
test_description='git grep various.
'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh
test_invalid_grep_expression() {
params="$@" &&
test_expect_success "invalid expression: grep $params" '
test_must_fail git grep $params -- nonexisting
'
}
LC_ALL=en_US.UTF-8 test-tool regex '^.$' '¿' &&
test_set_prereq MB_REGEX
cat >hello.c <<EOF
#include <assert.h>
#include <stdio.h>
int main(int argc, const char **argv)
{
printf("Hello world.\n");
return 0;
/* char ?? */
}
EOF
test_expect_success setup '
cat >file <<-\EOF &&
foo mmap bar
foo_mmap bar
foo_mmap bar mmap
foo mmap bar_mmap
foo_mmap bar mmap baz
EOF
cat >hello_world <<-\EOF &&
Hello world
HeLLo world
Hello_world
HeLLo_world
EOF
cat >ab <<-\EOF &&
a+b*c
a+bc
abc
EOF
cat >d0 <<-\EOF &&
d
0
EOF
echo vvv >v &&
echo ww w >w &&
echo x x xx x >x &&
echo y yy >y &&
echo zzz > z &&
mkdir t &&
echo test >t/t &&
echo vvv >t/v &&
mkdir t/a &&
echo vvv >t/a/v &&
qz_to_tab_space >space <<-\EOF &&
line without leading space1
Zline with leading space1
Zline with leading space2
Zline with leading space3
line without leading space2
EOF
cat >hello.ps1 <<-\EOF &&
# No-op.
function dummy() {}
# Say hello.
function hello() {
echo "Hello world."
echo "Hello again."
} # hello
# Still a no-op.
function dummy() {}
EOF
printf "\200\nASCII\n" >invalid-utf8 &&
if test_have_prereq FUNNYNAMES
then
echo unusual >"\"unusual\" pathname" &&
echo unusual >"t/nested \"unusual\" pathname"
fi &&
if test_have_prereq MB_REGEX
then
echo "¿" >reverse-question-mark
fi &&
git add . &&
test_tick &&
git commit -m initial
'
test_expect_success 'grep should not segfault with a bad input' '
test_must_fail git grep "("
'
test_invalid_grep_expression --and -e A
test_pattern_type () {
H=$1 &&
HC=$2 &&
L=$3 &&
type=$4 &&
shift 4 &&
expected_str= &&
case "$type" in
BRE)
expected_str="${HC}ab:a+bc"
;;
ERE)
expected_str="${HC}ab:abc"
;;
FIX)
expected_str="${HC}ab:a+b*c"
;;
*)
BUG "unknown pattern type '$type'"
;;
esac &&
config_str="$@" &&
test_expect_success "grep $L with '$config_str' interpreted as $type" '
echo $expected_str >expected &&
git $config_str grep "a+b*c" $H ab >actual &&
test_cmp expected actual
'
}
for H in HEAD ''
do
case "$H" in
HEAD) HC='HEAD:' L='HEAD' ;;
'') HC= L='in working tree' ;;
esac
test_expect_success "grep -w $L" '
cat >expected <<-EOF &&
${HC}file:1:foo mmap bar
${HC}file:3:foo_mmap bar mmap
${HC}file:4:foo mmap bar_mmap
${HC}file:5:foo_mmap bar mmap baz
EOF
git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (with --column)" '
cat >expected <<-EOF &&
${HC}file:5:foo mmap bar
${HC}file:14:foo_mmap bar mmap
${HC}file:5:foo mmap bar_mmap
${HC}file:14:foo_mmap bar mmap baz
EOF
git grep --column -w -e mmap $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (with --column, extended OR)" '
cat >expected <<-EOF &&
${HC}file:14:foo_mmap bar mmap
${HC}file:19:foo_mmap bar mmap baz
EOF
git grep --column -w -e mmap$ --or -e baz $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (with --column, --invert-match)" '
cat >expected <<-EOF &&
${HC}file:1:foo mmap bar
${HC}file:1:foo_mmap bar
${HC}file:1:foo_mmap bar mmap
${HC}file:1:foo mmap bar_mmap
EOF
git grep --column --invert-match -w -e baz $H -- file >actual &&
test_cmp expected actual
'
test_expect_success "grep $L (with --column, --invert-match, extended OR)" '
cat >expected <<-EOF &&
${HC}hello_world:6:HeLLo_world
EOF
git grep --column --invert-match -e ll --or --not -e _ $H -- hello_world \
>actual &&
test_cmp expected actual
'
test_expect_success "grep $L (with --column, --invert-match, extended AND)" '
cat >expected <<-EOF &&
${HC}hello_world:3:Hello world
${HC}hello_world:3:Hello_world
${HC}hello_world:6:HeLLo_world
EOF
git grep --column --invert-match --not -e _ --and --not -e ll $H -- hello_world \
>actual &&
test_cmp expected actual
'
test_expect_success "grep $L (with --column, double-negation)" '
cat >expected <<-EOF &&
${HC}file:1:foo_mmap bar mmap baz
EOF
git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
>actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (with --column, -C)" '
cat >expected <<-EOF &&
${HC}file:5:foo mmap bar
${HC}file-foo_mmap bar
${HC}file:14:foo_mmap bar mmap
${HC}file:5:foo mmap bar_mmap
${HC}file:14:foo_mmap bar mmap baz
EOF
git grep --column -w -C1 -e mmap $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (with --line-number, --column)" '
cat >expected <<-EOF &&
${HC}file:1:5:foo mmap bar
${HC}file:3:14:foo_mmap bar mmap
${HC}file:4:5:foo mmap bar_mmap
${HC}file:5:14:foo_mmap bar mmap baz
EOF
git grep -n --column -w -e mmap $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (with non-extended patterns, --column)" '
cat >expected <<-EOF &&
${HC}file:5:foo mmap bar
${HC}file:10:foo_mmap bar
${HC}file:10:foo_mmap bar mmap
${HC}file:5:foo mmap bar_mmap
${HC}file:10:foo_mmap bar mmap baz
EOF
git grep --column -w -e bar -e mmap $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L" '
cat >expected <<-EOF &&
${HC}file:1:foo mmap bar
${HC}file:3:foo_mmap bar mmap
${HC}file:4:foo mmap bar_mmap
${HC}file:5:foo_mmap bar mmap baz
EOF
git -c grep.linenumber=true grep -w -e mmap $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L" '
cat >expected <<-EOF &&
${HC}file:foo mmap bar
${HC}file:foo_mmap bar mmap
${HC}file:foo mmap bar_mmap
${HC}file:foo_mmap bar mmap baz
EOF
git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (w)" '
test_must_fail git grep -n -w -e "^w" $H >actual &&
test_must_be_empty actual
'
test_expect_success "grep -w $L (x)" '
cat >expected <<-EOF &&
${HC}x:1:x x xx x
EOF
git grep -n -w -e "x xx* x" $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (y-1)" '
cat >expected <<-EOF &&
${HC}y:1:y yy
EOF
git grep -n -w -e "^y" $H >actual &&
test_cmp expected actual
'
test_expect_success "grep -w $L (y-2)" '
if git grep -n -w -e "^y y" $H >actual
then
echo should not have matched
cat actual
false
else
test_must_be_empty actual
fi
'
test_expect_success "grep -w $L (z)" '
if git grep -n -w -e "^z" $H >actual
then
echo should not have matched
cat actual
false
else
test_must_be_empty actual
fi
'
test_expect_success "grep $L (with --column, --only-matching)" '
cat >expected <<-EOF &&
${HC}file:1:5:mmap
${HC}file:2:5:mmap
${HC}file:3:5:mmap
${HC}file:3:13:mmap
${HC}file:4:5:mmap
${HC}file:4:13:mmap
${HC}file:5:5:mmap
${HC}file:5:13:mmap
EOF
git grep --column -n -o -e mmap $H >actual &&
test_cmp expected actual
'
test_expect_success "grep $L (t-1)" '
echo "${HC}t/t:1:test" >expected &&
git grep -n -e test $H >actual &&
test_cmp expected actual
'
test_expect_success "grep $L (t-2)" '
echo "${HC}t:1:test" >expected &&
(
cd t &&
git grep -n -e test $H
) >actual &&
test_cmp expected actual
'
test_expect_success "grep $L (t-3)" '
echo "${HC}t/t:1:test" >expected &&
(
cd t &&
git grep --full-name -n -e test $H
) >actual &&
test_cmp expected actual
'
test_expect_success "grep -c $L (no /dev/null)" '
! git grep -c test $H | grep /dev/null
'
test_expect_success "grep --max-depth -1 $L" '
cat >expected <<-EOF &&
${HC}t/a/v:1:vvv
${HC}t/v:1:vvv
${HC}v:1:vvv
EOF
git grep --max-depth -1 -n -e vvv $H >actual &&
test_cmp expected actual &&
git grep --recursive -n -e vvv $H >actual &&
test_cmp expected actual
'
test_expect_success "grep --max-depth 0 $L" '
cat >expected <<-EOF &&
${HC}v:1:vvv
EOF
git grep --max-depth 0 -n -e vvv $H >actual &&
test_cmp expected actual &&
git grep --no-recursive -n -e vvv $H >actual &&
test_cmp expected actual
'
test_expect_success "grep --max-depth 0 -- '*' $L" '
cat >expected <<-EOF &&
${HC}t/a/v:1:vvv
${HC}t/v:1:vvv
${HC}v:1:vvv
EOF
git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
test_cmp expected actual &&
git grep --no-recursive -n -e vvv $H -- "*" >actual &&
test_cmp expected actual
'
test_expect_success "grep --max-depth 1 $L" '
cat >expected <<-EOF &&
${HC}t/v:1:vvv
${HC}v:1:vvv
EOF
git grep --max-depth 1 -n -e vvv $H >actual &&
test_cmp expected actual
'
test_expect_success "grep --max-depth 0 -- t $L" '
cat >expected <<-EOF &&
${HC}t/v:1:vvv
EOF
git grep --max-depth 0 -n -e vvv $H -- t >actual &&
test_cmp expected actual &&
git grep --no-recursive -n -e vvv $H -- t >actual &&
test_cmp expected actual
'
test_expect_success "grep --max-depth 0 -- . t $L" '
cat >expected <<-EOF &&
${HC}t/v:1:vvv
${HC}v:1:vvv
EOF
git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
test_cmp expected actual &&
git grep --no-recursive -n -e vvv $H -- . t >actual &&
test_cmp expected actual
'
test_expect_success "grep --max-depth 0 -- t . $L" '
cat >expected <<-EOF &&
${HC}t/v:1:vvv
${HC}v:1:vvv
EOF
git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
test_cmp expected actual &&
git grep --no-recursive -n -e vvv $H -- t . >actual &&
test_cmp expected actual
'
test_pattern_type "$H" "$HC" "$L" BRE -c grep.extendedRegexp=false
test_pattern_type "$H" "$HC" "$L" ERE -c grep.extendedRegexp=true
test_pattern_type "$H" "$HC" "$L" BRE -c grep.patternType=basic
test_pattern_type "$H" "$HC" "$L" ERE -c grep.patternType=extended
test_pattern_type "$H" "$HC" "$L" FIX -c grep.patternType=fixed
test_expect_success PCRE "grep $L with grep.patterntype=perl" '
echo "${HC}ab:a+b*c" >expected &&
git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
test_cmp expected actual
'
test_expect_success !FAIL_PREREQS,!PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
'
test_pattern_type "$H" "$HC" "$L" ERE \
-c grep.patternType=default \
-c grep.extendedRegexp=true
test_pattern_type "$H" "$HC" "$L" ERE \
-c grep.extendedRegexp=true \
-c grep.patternType=default
test_pattern_type "$H" "$HC" "$L" ERE \
-c grep.patternType=extended \
-c grep.extendedRegexp=false
test_pattern_type "$H" "$HC" "$L" BRE \
-c grep.patternType=basic \
-c grep.extendedRegexp=true
test_pattern_type "$H" "$HC" "$L" ERE \
-c grep.extendedRegexp=false \
-c grep.patternType=extended
test_pattern_type "$H" "$HC" "$L" BRE \
-c grep.extendedRegexp=true \
-c grep.patternType=basic
# grep.extendedRegexp is last-one-wins
test_pattern_type "$H" "$HC" "$L" BRE \
-c grep.extendedRegexp=true \
-c grep.extendedRegexp=false
# grep.patternType=basic pays no attention to grep.extendedRegexp
test_pattern_type "$H" "$HC" "$L" BRE \
-c grep.extendedRegexp=true \
-c grep.patternType=basic \
-c grep.extendedRegexp=false
# grep.patternType=extended pays no attention to grep.extendedRegexp
test_pattern_type "$H" "$HC" "$L" ERE \
-c grep.extendedRegexp=true \
-c grep.patternType=extended \
-c grep.extendedRegexp=false
# grep.extendedRegexp is used with a last-one-wins grep.patternType=default
test_pattern_type "$H" "$HC" "$L" ERE \
-c grep.patternType=fixed \
-c grep.extendedRegexp=true \
-c grep.patternType=default
# grep.extendedRegexp is used with earlier grep.patternType=default
test_pattern_type "$H" "$HC" "$L" ERE \
-c grep.extendedRegexp=false \
-c grep.patternType=default \
-c grep.extendedRegexp=true
# grep.extendedRegexp is used with a last-one-loses grep.patternType=default
test_pattern_type "$H" "$HC" "$L" ERE \
-c grep.extendedRegexp=false \
-c grep.extendedRegexp=true \
-c grep.patternType=default
# grep.extendedRegexp and grep.patternType are both last-one-wins independently
test_pattern_type "$H" "$HC" "$L" BRE \
-c grep.patternType=default \
-c grep.extendedRegexp=true \
-c grep.patternType=basic
# grep.patternType=extended and grep.patternType=default
test_pattern_type "$H" "$HC" "$L" BRE \
-c grep.patternType=extended \
-c grep.patternType=default
# grep.patternType=[extended -> default -> fixed] (BRE)" '
test_pattern_type "$H" "$HC" "$L" FIX \
-c grep.patternType=extended \
-c grep.patternType=default \
-c grep.patternType=fixed
test_expect_success "grep --count $L" '
echo ${HC}ab:3 >expected &&
git grep --count -e b $H -- ab >actual &&
test_cmp expected actual
'
test_expect_success "grep --count -h $L" '
echo 3 >expected &&
git grep --count -h -e b $H -- ab >actual &&
test_cmp expected actual
'
test_expect_success "grep $L searches past invalid lines on UTF-8 locale" '
LC_ALL=en_US.UTF-8 git grep A. invalid-utf8 >actual &&
cat >expected <<-EOF &&
invalid-utf8:ASCII
EOF
test_cmp expected actual
'
test_expect_success FUNNYNAMES "grep $L should quote unusual pathnames" '
cat >expected <<-EOF &&
${HC}"\"unusual\" pathname":unusual
${HC}"t/nested \"unusual\" pathname":unusual
EOF
git grep unusual $H >actual &&
test_cmp expected actual
'
test_expect_success FUNNYNAMES "grep $L in subdir should quote unusual relative pathnames" '
cat >expected <<-EOF &&
${HC}"nested \"unusual\" pathname":unusual
EOF
(
cd t &&
git grep unusual $H
) >actual &&
test_cmp expected actual
'
test_expect_success FUNNYNAMES "grep -z $L with unusual pathnames" '
cat >expected <<-EOF &&
${HC}"unusual" pathname:unusual
${HC}t/nested "unusual" pathname:unusual
EOF
git grep -z unusual $H >actual &&
tr "\0" ":" <actual >actual-replace-null &&
test_cmp expected actual-replace-null
'
test_expect_success FUNNYNAMES "grep -z $L in subdir with unusual relative pathnames" '
cat >expected <<-EOF &&
${HC}nested "unusual" pathname:unusual
EOF
(
cd t &&
git grep -z unusual $H
) >actual &&
tr "\0" ":" <actual >actual-replace-null &&
test_cmp expected actual-replace-null
'
done
test_expect_success MB_REGEX 'grep exactly one char in single-char multibyte file' '
LC_ALL=en_US.UTF-8 git grep "^.$" reverse-question-mark
'
test_expect_success MB_REGEX 'grep two chars in single-char multibyte file' '
LC_ALL=en_US.UTF-8 test_expect_code 1 git grep ".." reverse-question-mark
'
cat >expected <<EOF
file
EOF
test_expect_success 'grep -l -C' '
git grep -l -C1 foo >actual &&
test_cmp expected actual
'
cat >expected <<EOF
file:5
EOF
test_expect_success 'grep -c -C' '
git grep -c -C1 foo >actual &&
test_cmp expected actual
'
test_expect_success 'grep -L -C' '
git ls-files >expected &&
git grep -L -C1 nonexistent_string >actual &&
test_cmp expected actual
'
test_expect_success 'grep --files-without-match --quiet' '
git grep --files-without-match --quiet nonexistent_string >actual &&
test_must_be_empty actual
'
test_expect_success 'grep --max-count 0 (must exit with non-zero)' '
test_must_fail git grep --max-count 0 foo >actual &&
test_must_be_empty actual
'
test_expect_success 'grep --max-count 3' '
cat >expected <<-EOF &&
file:foo mmap bar
file:foo_mmap bar
file:foo_mmap bar mmap
EOF
git grep --max-count 3 foo >actual &&
test_cmp expected actual
'
test_expect_success 'grep --max-count -1 (no limit)' '
cat >expected <<-EOF &&
file:foo mmap bar
file:foo_mmap bar
file:foo_mmap bar mmap
file:foo mmap bar_mmap
file:foo_mmap bar mmap baz
EOF
git grep --max-count -1 foo >actual &&
test_cmp expected actual
'
test_expect_success 'grep --max-count 1 --context 2' '
cat >expected <<-EOF &&
file-foo mmap bar
file:foo_mmap bar
file-foo_mmap bar mmap
EOF
git grep --max-count 1 --context 1 foo_mmap >actual &&
test_cmp expected actual
'
test_expect_success 'grep --max-count 1 --show-function' '
cat >expected <<-EOF &&
hello.ps1=function hello() {
hello.ps1: echo "Hello world."
EOF
git grep --max-count 1 --show-function Hello hello.ps1 >actual &&
test_cmp expected actual
'
test_expect_success 'grep --max-count 2 --show-function' '
cat >expected <<-EOF &&
hello.ps1=function hello() {
hello.ps1: echo "Hello world."
hello.ps1: echo "Hello again."
EOF
git grep --max-count 2 --show-function Hello hello.ps1 >actual &&
test_cmp expected actual
'
test_expect_success 'grep --max-count 1 --count' '
cat >expected <<-EOF &&
hello.ps1:1
EOF
git grep --max-count 1 --count Hello hello.ps1 >actual &&
test_cmp expected actual
'
test_expect_success 'grep --max-count 1 (multiple files)' '
cat >expected <<-EOF &&
hello.c:#include <stdio.h>
hello.ps1:# No-op.
EOF
git grep --max-count 1 -e o -- hello.\* >actual &&
test_cmp expected actual
'
test_expect_success 'grep --max-count 1 --context 1 (multiple files)' '
cat >expected <<-EOF &&
hello.c-#include <assert.h>
hello.c:#include <stdio.h>
hello.c-
--
hello.ps1:# No-op.
hello.ps1-function dummy() {}
EOF
git grep --max-count 1 --context 1 -e o -- hello.\* >actual &&
test_cmp expected actual
'
cat >expected <<EOF
file:foo mmap bar_mmap
EOF
test_expect_success 'grep -e A --and -e B' '
git grep -e "foo mmap" --and -e bar_mmap >actual &&
test_cmp expected actual
'
cat >expected <<EOF
file:foo_mmap bar mmap
file:foo_mmap bar mmap baz
EOF
test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
git grep \( -e foo_ --or -e baz \) \
--and -e " mmap" >actual &&
test_cmp expected actual
'
cat >expected <<EOF
file:foo mmap bar
EOF
test_expect_success 'grep -e A --and --not -e B' '
git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
test_cmp expected actual
'
test_expect_success 'grep should ignore GREP_OPTIONS' '
GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
test_cmp expected actual
'
test_expect_success 'grep -f, non-existent file' '
test_must_fail git grep -f patterns
'
cat >expected <<EOF
file:foo mmap bar
file:foo_mmap bar
file:foo_mmap bar mmap
file:foo mmap bar_mmap
file:foo_mmap bar mmap baz
EOF
cat >pattern <<EOF
mmap
EOF
test_expect_success 'grep -f, one pattern' '
git grep -f pattern >actual &&
test_cmp expected actual
'
cat >expected <<EOF
file:foo mmap bar
file:foo_mmap bar
file:foo_mmap bar mmap
file:foo mmap bar_mmap
file:foo_mmap bar mmap baz
t/a/v:vvv
t/v:vvv
v:vvv
EOF
cat >patterns <<EOF
mmap
vvv
EOF
test_expect_success 'grep -f, multiple patterns' '
git grep -f patterns >actual &&
test_cmp expected actual
'
test_expect_success 'grep, multiple patterns' '
git grep "$(cat patterns)" >actual &&
test_cmp expected actual
'
cat >expected <<EOF
file:foo mmap bar
file:foo_mmap bar
file:foo_mmap bar mmap
file:foo mmap bar_mmap
file:foo_mmap bar mmap baz
t/a/v:vvv
t/v:vvv
v:vvv
EOF
cat >patterns <<EOF
mmap
vvv
EOF
test_expect_success 'grep -f, ignore empty lines' '
git grep -f patterns >actual &&
test_cmp expected actual
'
test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
git grep -f - <patterns >actual &&
test_cmp expected actual
'
test_expect_success 'grep -f, use cwd relative file' '
test_when_finished "git rm -f sub/dir/file" &&
mkdir -p sub/dir &&
echo hit >sub/dir/file &&
git add sub/dir/file &&
echo hit >sub/dir/pattern &&
echo miss >pattern &&
(
cd sub/dir && git grep -f pattern file
) &&
git -C sub/dir grep -f pattern file
'
cat >expected <<EOF
y:y yy
--
z:zzz
EOF
test_expect_success 'grep -q, silently report matches' '
git grep -q mmap >actual &&
test_must_be_empty actual &&
test_must_fail git grep -q qfwfq >actual &&
test_must_be_empty actual
'
test_expect_success 'grep -C1 hunk mark between files' '
git grep -C1 "^[yz]" >actual &&
test_cmp expected actual
'
test_expect_success 'log grep setup' '
test_commit --append --author "With * Asterisk <[email protected]>" second file a &&
test_commit --append third file a &&
test_commit --append --author "Night Fall <[email protected]>" fourth file a
'
test_expect_success 'log grep (1)' '
git log --author=author --pretty=tformat:%s >actual &&
{
echo third && echo initial
} >expect &&
test_cmp expect actual
'
test_expect_success 'log grep (2)' '
git log --author=" * " -F --pretty=tformat:%s >actual &&
{
echo second
} >expect &&
test_cmp expect actual
'
test_expect_success 'log grep (3)' '
git log --author="^A U" --pretty=tformat:%s >actual &&
{
echo third && echo initial
} >expect &&
test_cmp expect actual
'
test_expect_success 'log grep (4)' '
git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
{
echo second
} >expect &&
test_cmp expect actual
'
test_expect_success 'log grep (5)' '
git log --author=Thor -F --pretty=tformat:%s >actual &&
{
echo third && echo initial
} >expect &&
test_cmp expect actual
'
test_expect_success 'log grep (6)' '
git log --author=-0700 --pretty=tformat:%s >actual &&
test_must_be_empty actual
'
test_expect_success 'log grep (7)' '
git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
echo third >expect &&
test_cmp expect actual
'
test_expect_success 'log grep (8)' '
git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
{
echo third && echo second
} >expect &&
test_cmp expect actual
'
test_expect_success 'log grep (9)' '
git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
echo third >expect &&
test_cmp expect actual
'
test_expect_success 'log grep (9)' '
git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
test_must_be_empty actual
'
test_expect_success 'log --grep-reflog can only be used under -g' '
test_must_fail git log --grep-reflog="commit: third"
'
test_expect_success 'log with multiple --grep uses union' '
git log --grep=i --grep=r --format=%s >actual &&
{
echo fourth && echo third && echo initial
} >expect &&
test_cmp expect actual
'
test_expect_success 'log --all-match with multiple --grep uses intersection' '
git log --all-match --grep=i --grep=r --format=%s >actual &&
{
echo third
} >expect &&
test_cmp expect actual
'
test_expect_success 'log with multiple --author uses union' '
git log --author="Thor" --author="Aster" --format=%s >actual &&
{
echo third && echo second && echo initial
} >expect &&
test_cmp expect actual
'
test_expect_success 'log --all-match with multiple --author still uses union' '
git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
{
echo third && echo second && echo initial
} >expect &&
test_cmp expect actual
'
test_expect_success 'log --grep --author uses intersection' '
# grep matches only third and fourth
# author matches only initial and third
git log --author="A U Thor" --grep=r --format=%s >actual &&
{
echo third
} >expect &&
test_cmp expect actual
'
test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
# grep matches initial and second but not third
# author matches only initial and third
git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
{
echo initial
} >expect &&
test_cmp expect actual
'
test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
# grep matches only initial and third
# author matches all but second
git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
{
echo third && echo initial
} >expect &&
test_cmp expect actual
'
test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
# grep matches only initial and third
# author matches all but second
git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
{
echo third && echo initial
} >expect &&