Git 2.16.2
[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         elif test "$matches" = T1
26         then
27                 test_expect_failure "git grep -f f $flags '$pattern_human' a" "
28                         printf '$pattern' | q_to_nul >f &&
29                         git grep -f f $flags a
30                 "
31         elif test "$matches" = T0
32         then
33                 test_expect_failure "git grep -f f $flags '$pattern_human' a" "
34                         printf '$pattern' | q_to_nul >f &&
35                         test_must_fail git grep -f f $flags a
36                 "
37         else
38                 test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false'
39         fi
40 }
41
42 test_expect_success 'setup' "
43         echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
44         git add a &&
45         git commit -m.
46 "
47
48 test_expect_success 'git grep ina a' '
49         echo Binary file a matches >expect &&
50         git grep ina a >actual &&
51         test_cmp expect actual
52 '
53
54 test_expect_success 'git grep -ah ina a' '
55         git grep -ah ina a >actual &&
56         test_cmp a actual
57 '
58
59 test_expect_success 'git grep -I ina a' '
60         : >expect &&
61         test_must_fail git grep -I ina a >actual &&
62         test_cmp expect actual
63 '
64
65 test_expect_success 'git grep -c ina a' '
66         echo a:1 >expect &&
67         git grep -c ina a >actual &&
68         test_cmp expect actual
69 '
70
71 test_expect_success 'git grep -l ina a' '
72         echo a >expect &&
73         git grep -l ina a >actual &&
74         test_cmp expect actual
75 '
76
77 test_expect_success 'git grep -L bar a' '
78         echo a >expect &&
79         git grep -L bar a >actual &&
80         test_cmp expect actual
81 '
82
83 test_expect_success 'git grep -q ina a' '
84         : >expect &&
85         git grep -q ina a >actual &&
86         test_cmp expect actual
87 '
88
89 test_expect_success 'git grep -F ile a' '
90         git grep -F ile a
91 '
92
93 test_expect_success 'git grep -Fi iLE a' '
94         git grep -Fi iLE a
95 '
96
97 # This test actually passes on platforms where regexec() supports the
98 # flag REG_STARTEND.
99 test_expect_success 'git grep ile a' '
100         git grep ile a
101 '
102
103 test_expect_failure 'git grep .fi a' '
104         git grep .fi a
105 '
106
107 nul_match 1 '-F' 'yQf'
108 nul_match 0 '-F' 'yQx'
109 nul_match 1 '-Fi' 'YQf'
110 nul_match 0 '-Fi' 'YQx'
111 nul_match 1 '' 'yQf'
112 nul_match 0 '' 'yQx'
113 nul_match 1 '' 'æQð'
114 nul_match 1 '-F' 'eQm[*]c'
115 nul_match 1 '-Fi' 'EQM[*]C'
116
117 # Regex patterns that would match but shouldn't with -F
118 nul_match 0 '-F' 'yQ[f]'
119 nul_match 0 '-F' '[y]Qf'
120 nul_match 0 '-Fi' 'YQ[F]'
121 nul_match 0 '-Fi' '[Y]QF'
122 nul_match 0 '-F' 'æQ[ð]'
123 nul_match 0 '-F' '[æ]Qð'
124 nul_match 0 '-Fi' 'ÆQ[Ð]'
125 nul_match 0 '-Fi' '[Æ]QÐ'
126
127 # kwset is disabled on -i & non-ASCII. No way to match non-ASCII \0
128 # patterns case-insensitively.
129 nul_match T1 '-i' 'ÆQÐ'
130
131 # \0 implicitly disables regexes. This is an undocumented internal
132 # limitation.
133 nul_match T1 '' 'yQ[f]'
134 nul_match T1 '' '[y]Qf'
135 nul_match T1 '-i' 'YQ[F]'
136 nul_match T1 '-i' '[Y]Qf'
137 nul_match T1 '' 'æQ[ð]'
138 nul_match T1 '' '[æ]Qð'
139 nul_match T1 '-i' 'ÆQ[Ð]'
140
141 # ... because of \0 implicitly disabling regexes regexes that
142 # should/shouldn't match don't do the right thing.
143 nul_match T1 '' 'eQm.*cQ'
144 nul_match T1 '-i' 'EQM.*cQ'
145 nul_match T0 '' 'eQm[*]c'
146 nul_match T0 '-i' 'EQM[*]C'
147
148 # Due to the REG_STARTEND extension when kwset() is disabled on -i &
149 # non-ASCII the string will be matched in its entirety, but the
150 # pattern will be cut off at the first \0.
151 nul_match 0 '-i' 'NOMATCHQð'
152 nul_match T0 '-i' '[Æ]QNOMATCH'
153 nul_match T0 '-i' '[æ]QNOMATCH'
154 # Matches, but for the wrong reasons, just stops at [æ]
155 nul_match 1 '-i' '[Æ]Qð'
156 nul_match 1 '-i' '[æ]Qð'
157
158 # Ensure that the matcher doesn't regress to something that stops at
159 # \0
160 nul_match 0 '-F' 'yQ[f]'
161 nul_match 0 '-Fi' 'YQ[F]'
162 nul_match 0 '' 'yQNOMATCH'
163 nul_match 0 '' 'QNOMATCH'
164 nul_match 0 '-i' 'YQNOMATCH'
165 nul_match 0 '-i' 'QNOMATCH'
166 nul_match 0 '-F' 'æQ[ð]'
167 nul_match 0 '-Fi' 'ÆQ[Ð]'
168 nul_match 0 '' 'yQNÓMATCH'
169 nul_match 0 '' 'QNÓMATCH'
170 nul_match 0 '-i' 'YQNÓMATCH'
171 nul_match 0 '-i' 'QNÓMATCH'
172
173 test_expect_success 'grep respects binary diff attribute' '
174         echo text >t &&
175         git add t &&
176         echo t:text >expect &&
177         git grep text t >actual &&
178         test_cmp expect actual &&
179         echo "t -diff" >.gitattributes &&
180         echo "Binary file t matches" >expect &&
181         git grep text t >actual &&
182         test_cmp expect actual
183 '
184
185 test_expect_success 'grep --cached respects binary diff attribute' '
186         git grep --cached text t >actual &&
187         test_cmp expect actual
188 '
189
190 test_expect_success 'grep --cached respects binary diff attribute (2)' '
191         git add .gitattributes &&
192         rm .gitattributes &&
193         git grep --cached text t >actual &&
194         test_when_finished "git rm --cached .gitattributes" &&
195         test_when_finished "git checkout .gitattributes" &&
196         test_cmp expect actual
197 '
198
199 test_expect_success 'grep revision respects binary diff attribute' '
200         git commit -m new &&
201         echo "Binary file HEAD:t matches" >expect &&
202         git grep text HEAD -- t >actual &&
203         test_when_finished "git reset HEAD^" &&
204         test_cmp expect actual
205 '
206
207 test_expect_success 'grep respects not-binary diff attribute' '
208         echo binQary | q_to_nul >b &&
209         git add b &&
210         echo "Binary file b matches" >expect &&
211         git grep bin b >actual &&
212         test_cmp expect actual &&
213         echo "b diff" >.gitattributes &&
214         echo "b:binQary" >expect &&
215         git grep bin b >actual.raw &&
216         nul_to_q <actual.raw >actual &&
217         test_cmp expect actual
218 '
219
220 cat >nul_to_q_textconv <<'EOF'
221 #!/bin/sh
222 "$PERL_PATH" -pe 'y/\000/Q/' < "$1"
223 EOF
224 chmod +x nul_to_q_textconv
225
226 test_expect_success 'setup textconv filters' '
227         echo a diff=foo >.gitattributes &&
228         git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv
229 '
230
231 test_expect_success 'grep does not honor textconv' '
232         test_must_fail git grep Qfile
233 '
234
235 test_expect_success 'grep --textconv honors textconv' '
236         echo "a:binaryQfileQm[*]cQ*æQð" >expect &&
237         git grep --textconv Qfile >actual &&
238         test_cmp expect actual
239 '
240
241 test_expect_success 'grep --no-textconv does not honor textconv' '
242         test_must_fail git grep --no-textconv Qfile
243 '
244
245 test_expect_success 'grep --textconv blob honors textconv' '
246         echo "HEAD:a:binaryQfileQm[*]cQ*æQð" >expect &&
247         git grep --textconv Qfile HEAD:a >actual &&
248         test_cmp expect actual
249 '
250
251 test_done