1#!/bin/sh
2
3test_description='Test grep recurse-submodules feature
4
5This test verifies the recurse-submodules feature correctly greps across
6submodules.
7'
8
9. ./test-lib.sh
10
11test_expect_success 'setup directory structure and submodule' '
12 echo "(1|2)d(3|4)" >a &&
13 mkdir b &&
14 echo "(3|4)" >b/b &&
15 git add a b &&
16 git commit -m "add a and b" &&
17 git init submodule &&
18 echo "(1|2)d(3|4)" >submodule/a &&
19 git -C submodule add a &&
20 git -C submodule commit -m "add a" &&
21 git submodule add ./submodule &&
22 git commit -m "added submodule"
23'
24
25test_expect_success 'grep correctly finds patterns in a submodule' '
26 cat >expect <<-\EOF &&
27 a:(1|2)d(3|4)
28 b/b:(3|4)
29 submodule/a:(1|2)d(3|4)
30 EOF
31
32 git grep -e "(3|4)" --recurse-submodules >actual &&
33 test_cmp expect actual
34'
35
36test_expect_success 'grep and basic pathspecs' '
37 cat >expect <<-\EOF &&
38 submodule/a:(1|2)d(3|4)
39 EOF
40
41 git grep -e. --recurse-submodules -- submodule >actual &&
42 test_cmp expect actual
43'
44
45test_expect_success 'grep and nested submodules' '
46 git init submodule/sub &&
47 echo "(1|2)d(3|4)" >submodule/sub/a &&
48 git -C submodule/sub add a &&
49 git -C submodule/sub commit -m "add a" &&
50 git -C submodule submodule add ./sub &&
51 git -C submodule add sub &&
52 git -C submodule commit -m "added sub" &&
53 git add submodule &&
54 git commit -m "updated submodule" &&
55
56 cat >expect <<-\EOF &&
57 a:(1|2)d(3|4)
58 b/b:(3|4)
59 submodule/a:(1|2)d(3|4)
60 submodule/sub/a:(1|2)d(3|4)
61 EOF
62
63 git grep -e "(3|4)" --recurse-submodules >actual &&
64 test_cmp expect actual
65'
66
67test_expect_success 'grep and multiple patterns' '
68 cat >expect <<-\EOF &&
69 a:(1|2)d(3|4)
70 submodule/a:(1|2)d(3|4)
71 submodule/sub/a:(1|2)d(3|4)
72 EOF
73
74 git grep -e "(3|4)" --and -e "(1|2)" --recurse-submodules >actual &&
75 test_cmp expect actual
76'
77
78test_expect_success 'grep and multiple patterns' '
79 cat >expect <<-\EOF &&
80 b/b:(3|4)
81 EOF
82
83 git grep -e "(3|4)" --and --not -e "(1|2)" --recurse-submodules >actual &&
84 test_cmp expect actual
85'
86
87test_expect_success 'basic grep tree' '
88 cat >expect <<-\EOF &&
89 HEAD:a:(1|2)d(3|4)
90 HEAD:b/b:(3|4)
91 HEAD:submodule/a:(1|2)d(3|4)
92 HEAD:submodule/sub/a:(1|2)d(3|4)
93 EOF
94
95 git grep -e "(3|4)" --recurse-submodules HEAD >actual &&
96 test_cmp expect actual
97'
98
99test_expect_success 'grep tree HEAD^' '
100 cat >expect <<-\EOF &&
101 HEAD^:a:(1|2)d(3|4)
102 HEAD^:b/b:(3|4)
103 HEAD^:submodule/a:(1|2)d(3|4)
104 EOF
105
106 git grep -e "(3|4)" --recurse-submodules HEAD^ >actual &&
107 test_cmp expect actual
108'
109
110test_expect_success 'grep tree HEAD^^' '
111 cat >expect <<-\EOF &&
112 HEAD^^:a:(1|2)d(3|4)
113 HEAD^^:b/b:(3|4)
114 EOF
115
116 git grep -e "(3|4)" --recurse-submodules HEAD^^ >actual &&
117 test_cmp expect actual
118'
119
120test_expect_success 'grep tree and pathspecs' '
121 cat >expect <<-\EOF &&
122 HEAD:submodule/a:(1|2)d(3|4)
123 HEAD:submodule/sub/a:(1|2)d(3|4)
124 EOF
125
126 git grep -e "(3|4)" --recurse-submodules HEAD -- submodule >actual &&
127 test_cmp expect actual
128'
129
130test_expect_success 'grep tree and pathspecs' '
131 cat >expect <<-\EOF &&
132 HEAD:submodule/a:(1|2)d(3|4)
133 HEAD:submodule/sub/a:(1|2)d(3|4)
134 EOF
135
136 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodule*a" >actual &&
137 test_cmp expect actual
138'
139
140test_expect_success 'grep tree and more pathspecs' '
141 cat >expect <<-\EOF &&
142 HEAD:submodule/a:(1|2)d(3|4)
143 EOF
144
145 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul?/a" >actual &&
146 test_cmp expect actual
147'
148
149test_expect_success 'grep tree and more pathspecs' '
150 cat >expect <<-\EOF &&
151 HEAD:submodule/sub/a:(1|2)d(3|4)
152 EOF
153
154 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul*/sub/a" >actual &&
155 test_cmp expect actual
156'
157
158test_expect_success !MINGW 'grep recurse submodule colon in name' '
159 git init parent &&
160 test_when_finished "rm -rf parent" &&
161 echo "(1|2)d(3|4)" >"parent/fi:le" &&
162 git -C parent add "fi:le" &&
163 git -C parent commit -m "add fi:le" &&
164
165 git init "su:b" &&
166 test_when_finished "rm -rf su:b" &&
167 echo "(1|2)d(3|4)" >"su:b/fi:le" &&
168 git -C "su:b" add "fi:le" &&
169 git -C "su:b" commit -m "add fi:le" &&
170
171 git -C parent submodule add "../su:b" "su:b" &&
172 git -C parent commit -m "add submodule" &&
173
174 cat >expect <<-\EOF &&
175 fi:le:(1|2)d(3|4)
176 su:b/fi:le:(1|2)d(3|4)
177 EOF
178 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
179 test_cmp expect actual &&
180
181 cat >expect <<-\EOF &&
182 HEAD:fi:le:(1|2)d(3|4)
183 HEAD:su:b/fi:le:(1|2)d(3|4)
184 EOF
185 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD >actual &&
186 test_cmp expect actual
187'
188
189test_expect_success 'grep history with moved submoules' '
190 git init parent &&
191 test_when_finished "rm -rf parent" &&
192 echo "(1|2)d(3|4)" >parent/file &&
193 git -C parent add file &&
194 git -C parent commit -m "add file" &&
195
196 git init sub &&
197 test_when_finished "rm -rf sub" &&
198 echo "(1|2)d(3|4)" >sub/file &&
199 git -C sub add file &&
200 git -C sub commit -m "add file" &&
201
202 git -C parent submodule add ../sub dir/sub &&
203 git -C parent commit -m "add submodule" &&
204
205 cat >expect <<-\EOF &&
206 dir/sub/file:(1|2)d(3|4)
207 file:(1|2)d(3|4)
208 EOF
209 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
210 test_cmp expect actual &&
211
212 git -C parent mv dir/sub sub-moved &&
213 git -C parent commit -m "moved submodule" &&
214
215 cat >expect <<-\EOF &&
216 file:(1|2)d(3|4)
217 sub-moved/file:(1|2)d(3|4)
218 EOF
219 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
220 test_cmp expect actual &&
221
222 cat >expect <<-\EOF &&
223 HEAD^:dir/sub/file:(1|2)d(3|4)
224 HEAD^:file:(1|2)d(3|4)
225 EOF
226 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD^ >actual &&
227 test_cmp expect actual
228'
229
230test_expect_success 'grep using relative path' '
231 test_when_finished "rm -rf parent sub" &&
232 git init sub &&
233 echo "(1|2)d(3|4)" >sub/file &&
234 git -C sub add file &&
235 git -C sub commit -m "add file" &&
236
237 git init parent &&
238 echo "(1|2)d(3|4)" >parent/file &&
239 git -C parent add file &&
240 mkdir parent/src &&
241 echo "(1|2)d(3|4)" >parent/src/file2 &&
242 git -C parent add src/file2 &&
243 git -C parent submodule add ../sub &&
244 git -C parent commit -m "add files and submodule" &&
245
246 # From top works
247 cat >expect <<-\EOF &&
248 file:(1|2)d(3|4)
249 src/file2:(1|2)d(3|4)
250 sub/file:(1|2)d(3|4)
251 EOF
252 git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
253 test_cmp expect actual &&
254
255 # Relative path to top
256 cat >expect <<-\EOF &&
257 ../file:(1|2)d(3|4)
258 file2:(1|2)d(3|4)
259 ../sub/file:(1|2)d(3|4)
260 EOF
261 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- .. >actual &&
262 test_cmp expect actual &&
263
264 # Relative path to submodule
265 cat >expect <<-\EOF &&
266 ../sub/file:(1|2)d(3|4)
267 EOF
268 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- ../sub >actual &&
269 test_cmp expect actual
270'
271
272test_expect_success 'grep from a subdir' '
273 test_when_finished "rm -rf parent sub" &&
274 git init sub &&
275 echo "(1|2)d(3|4)" >sub/file &&
276 git -C sub add file &&
277 git -C sub commit -m "add file" &&
278
279 git init parent &&
280 mkdir parent/src &&
281 echo "(1|2)d(3|4)" >parent/src/file &&
282 git -C parent add src/file &&
283 git -C parent submodule add ../sub src/sub &&
284 git -C parent submodule add ../sub sub &&
285 git -C parent commit -m "add files and submodules" &&
286
287 # Verify grep from root works
288 cat >expect <<-\EOF &&
289 src/file:(1|2)d(3|4)
290 src/sub/file:(1|2)d(3|4)
291 sub/file:(1|2)d(3|4)
292 EOF
293 git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
294 test_cmp expect actual &&
295
296 # Verify grep from a subdir works
297 cat >expect <<-\EOF &&
298 file:(1|2)d(3|4)
299 sub/file:(1|2)d(3|4)
300 EOF
301 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
302 test_cmp expect actual
303'
304
305test_incompatible_with_recurse_submodules ()
306{
307 test_expect_success "--recurse-submodules and $1 are incompatible" "
308 test_must_fail git grep -e. --recurse-submodules $1 2>actual &&
309 test_i18ngrep 'not supported with --recurse-submodules' actual
310 "
311}
312
313test_incompatible_with_recurse_submodules --untracked
314test_incompatible_with_recurse_submodules --no-index
315
316test_expect_success 'grep --recurse-submodules should pass the pattern type along' '
317 # Fixed
318 test_must_fail git grep -F --recurse-submodules -e "(.|.)[\d]" &&
319 test_must_fail git -c grep.patternType=fixed grep --recurse-submodules -e "(.|.)[\d]" &&
320
321 # Basic
322 git grep -G --recurse-submodules -e "(.|.)[\d]" >actual &&
323 cat >expect <<-\EOF &&
324 a:(1|2)d(3|4)
325 submodule/a:(1|2)d(3|4)
326 submodule/sub/a:(1|2)d(3|4)
327 EOF
328 test_cmp expect actual &&
329 git -c grep.patternType=basic grep --recurse-submodules -e "(.|.)[\d]" >actual &&
330 test_cmp expect actual &&
331
332 # Extended
333 git grep -E --recurse-submodules -e "(.|.)[\d]" >actual &&
334 cat >expect <<-\EOF &&
335 .gitmodules:[submodule "submodule"]
336 .gitmodules: path = submodule
337 .gitmodules: url = ./submodule
338 a:(1|2)d(3|4)
339 submodule/.gitmodules:[submodule "sub"]
340 submodule/a:(1|2)d(3|4)
341 submodule/sub/a:(1|2)d(3|4)
342 EOF
343 test_cmp expect actual &&
344 git -c grep.patternType=extended grep --recurse-submodules -e "(.|.)[\d]" >actual &&
345 test_cmp expect actual &&
346 git -c grep.extendedRegexp=true grep --recurse-submodules -e "(.|.)[\d]" >actual &&
347 test_cmp expect actual &&
348
349 # Perl
350 if test_have_prereq PCRE
351 then
352 git grep -P --recurse-submodules -e "(.|.)[\d]" >actual &&
353 cat >expect <<-\EOF &&
354 a:(1|2)d(3|4)
355 b/b:(3|4)
356 submodule/a:(1|2)d(3|4)
357 submodule/sub/a:(1|2)d(3|4)
358 EOF
359 test_cmp expect actual &&
360 git -c grep.patternType=perl grep --recurse-submodules -e "(.|.)[\d]" >actual &&
361 test_cmp expect actual
362 fi
363'
364
365test_done