grep: prepare for testing binary regexes containing rx metacharacters
[git] / t / t7008-grep-binary.sh
1 #!/bin/sh
2
3 test_description='git grep in binary files'
4
5 . ./test-lib.sh
6
7 nul_match () {
8         matches=$1
9         flags=$2
10         pattern=$3
11         pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
12
13         if test "$matches" = 1
14         then
15                 test_expect_success "git grep -f f $flags '$pattern_human' a" "
16                         printf '$pattern' | q_to_nul >f &&
17                         git grep -f f $flags a
18                 "
19         elif test "$matches" = 0
20         then
21                 test_expect_success "git grep -f f $flags '$pattern_human' a" "
22                         printf '$pattern' | q_to_nul >f &&
23                         test_must_fail git grep -f f $flags a
24                 "
25         else
26                 test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false'
27         fi
28 }
29
30 test_expect_success 'setup' "
31         echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
32         git add a &&
33         git commit -m.
34 "
35
36 test_expect_success 'git grep ina a' '
37         echo Binary file a matches >expect &&
38         git grep ina a >actual &&
39         test_cmp expect actual
40 '
41
42 test_expect_success 'git grep -ah ina a' '
43         git grep -ah ina a >actual &&
44         test_cmp a actual
45 '
46
47 test_expect_success 'git grep -I ina a' '
48         : >expect &&
49         test_must_fail git grep -I ina a >actual &&
50         test_cmp expect actual
51 '
52
53 test_expect_success 'git grep -c ina a' '
54         echo a:1 >expect &&
55         git grep -c ina a >actual &&
56         test_cmp expect actual
57 '
58
59 test_expect_success 'git grep -l ina a' '
60         echo a >expect &&
61         git grep -l ina a >actual &&
62         test_cmp expect actual
63 '
64
65 test_expect_success 'git grep -L bar a' '
66         echo a >expect &&
67         git grep -L bar a >actual &&
68         test_cmp expect actual
69 '
70
71 test_expect_success 'git grep -q ina a' '
72         : >expect &&
73         git grep -q ina a >actual &&
74         test_cmp expect actual
75 '
76
77 test_expect_success 'git grep -F ile a' '
78         git grep -F ile a
79 '
80
81 test_expect_success 'git grep -Fi iLE a' '
82         git grep -Fi iLE a
83 '
84
85 # This test actually passes on platforms where regexec() supports the
86 # flag REG_STARTEND.
87 test_expect_success 'git grep ile a' '
88         git grep ile a
89 '
90
91 test_expect_failure 'git grep .fi a' '
92         git grep .fi a
93 '
94
95 nul_match 1 '-F' 'yQf'
96 nul_match 0 '-F' 'yQx'
97 nul_match 1 '-Fi' 'YQf'
98 nul_match 0 '-Fi' 'YQx'
99 nul_match 1 '' 'yQf'
100 nul_match 0 '' 'yQx'
101
102 test_expect_success 'grep respects binary diff attribute' '
103         echo text >t &&
104         git add t &&
105         echo t:text >expect &&
106         git grep text t >actual &&
107         test_cmp expect actual &&
108         echo "t -diff" >.gitattributes &&
109         echo "Binary file t matches" >expect &&
110         git grep text t >actual &&
111         test_cmp expect actual
112 '
113
114 test_expect_success 'grep --cached respects binary diff attribute' '
115         git grep --cached text t >actual &&
116         test_cmp expect actual
117 '
118
119 test_expect_success 'grep --cached respects binary diff attribute (2)' '
120         git add .gitattributes &&
121         rm .gitattributes &&
122         git grep --cached text t >actual &&
123         test_when_finished "git rm --cached .gitattributes" &&
124         test_when_finished "git checkout .gitattributes" &&
125         test_cmp expect actual
126 '
127
128 test_expect_success 'grep revision respects binary diff attribute' '
129         git commit -m new &&
130         echo "Binary file HEAD:t matches" >expect &&
131         git grep text HEAD -- t >actual &&
132         test_when_finished "git reset HEAD^" &&
133         test_cmp expect actual
134 '
135
136 test_expect_success 'grep respects not-binary diff attribute' '
137         echo binQary | q_to_nul >b &&
138         git add b &&
139         echo "Binary file b matches" >expect &&
140         git grep bin b >actual &&
141         test_cmp expect actual &&
142         echo "b diff" >.gitattributes &&
143         echo "b:binQary" >expect &&
144         git grep bin b >actual.raw &&
145         nul_to_q <actual.raw >actual &&
146         test_cmp expect actual
147 '
148
149 cat >nul_to_q_textconv <<'EOF'
150 #!/bin/sh
151 "$PERL_PATH" -pe 'y/\000/Q/' < "$1"
152 EOF
153 chmod +x nul_to_q_textconv
154
155 test_expect_success 'setup textconv filters' '
156         echo a diff=foo >.gitattributes &&
157         git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv
158 '
159
160 test_expect_success 'grep does not honor textconv' '
161         test_must_fail git grep Qfile
162 '
163
164 test_expect_success 'grep --textconv honors textconv' '
165         echo "a:binaryQfileQm[*]cQ*æQð" >expect &&
166         git grep --textconv Qfile >actual &&
167         test_cmp expect actual
168 '
169
170 test_expect_success 'grep --no-textconv does not honor textconv' '
171         test_must_fail git grep --no-textconv Qfile
172 '
173
174 test_expect_success 'grep --textconv blob honors textconv' '
175         echo "HEAD:a:binaryQfileQm[*]cQ*æQð" >expect &&
176         git grep --textconv Qfile HEAD:a >actual &&
177         test_cmp expect actual
178 '
179
180 test_done