3 test_description='Test grep recurse-submodules feature
5 This test verifies the recurse-submodules feature correctly greps across
11 test_expect_success 'setup directory structure and submodule' '
12 echo "(1|2)d(3|4)" >a &&
16 git commit -m "add a and b" &&
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"
25 test_expect_success 'grep correctly finds patterns in a submodule' '
26 cat >expect <<-\EOF &&
29 submodule/a:(1|2)d(3|4)
32 git grep -e "(3|4)" --recurse-submodules >actual &&
33 test_cmp expect actual
36 test_expect_success 'grep finds patterns in a submodule via config' '
37 test_config submodule.recurse true &&
38 # expect from previous test
39 git grep -e "(3|4)" >actual &&
40 test_cmp expect actual
43 test_expect_success 'grep --no-recurse-submodules overrides config' '
44 test_config submodule.recurse true &&
45 cat >expect <<-\EOF &&
50 git grep -e "(3|4)" --no-recurse-submodules >actual &&
51 test_cmp expect actual
54 test_expect_success 'grep and basic pathspecs' '
55 cat >expect <<-\EOF &&
56 submodule/a:(1|2)d(3|4)
59 git grep -e. --recurse-submodules -- submodule >actual &&
60 test_cmp expect actual
63 test_expect_success 'grep and nested submodules' '
64 git init submodule/sub &&
65 echo "(1|2)d(3|4)" >submodule/sub/a &&
66 git -C submodule/sub add a &&
67 git -C submodule/sub commit -m "add a" &&
68 git -C submodule submodule add ./sub &&
69 git -C submodule add sub &&
70 git -C submodule commit -m "added sub" &&
72 git commit -m "updated submodule" &&
74 cat >expect <<-\EOF &&
77 submodule/a:(1|2)d(3|4)
78 submodule/sub/a:(1|2)d(3|4)
81 git grep -e "(3|4)" --recurse-submodules >actual &&
82 test_cmp expect actual
85 test_expect_success 'grep and multiple patterns' '
86 cat >expect <<-\EOF &&
88 submodule/a:(1|2)d(3|4)
89 submodule/sub/a:(1|2)d(3|4)
92 git grep -e "(3|4)" --and -e "(1|2)" --recurse-submodules >actual &&
93 test_cmp expect actual
96 test_expect_success 'grep and multiple patterns' '
97 cat >expect <<-\EOF &&
101 git grep -e "(3|4)" --and --not -e "(1|2)" --recurse-submodules >actual &&
102 test_cmp expect actual
105 test_expect_success 'basic grep tree' '
106 cat >expect <<-\EOF &&
109 HEAD:submodule/a:(1|2)d(3|4)
110 HEAD:submodule/sub/a:(1|2)d(3|4)
113 git grep -e "(3|4)" --recurse-submodules HEAD >actual &&
114 test_cmp expect actual
117 test_expect_success 'grep tree HEAD^' '
118 cat >expect <<-\EOF &&
121 HEAD^:submodule/a:(1|2)d(3|4)
124 git grep -e "(3|4)" --recurse-submodules HEAD^ >actual &&
125 test_cmp expect actual
128 test_expect_success 'grep tree HEAD^^' '
129 cat >expect <<-\EOF &&
134 git grep -e "(3|4)" --recurse-submodules HEAD^^ >actual &&
135 test_cmp expect actual
138 test_expect_success 'grep tree and pathspecs' '
139 cat >expect <<-\EOF &&
140 HEAD:submodule/a:(1|2)d(3|4)
141 HEAD:submodule/sub/a:(1|2)d(3|4)
144 git grep -e "(3|4)" --recurse-submodules HEAD -- submodule >actual &&
145 test_cmp expect actual
148 test_expect_success 'grep tree and pathspecs' '
149 cat >expect <<-\EOF &&
150 HEAD:submodule/a:(1|2)d(3|4)
151 HEAD:submodule/sub/a:(1|2)d(3|4)
154 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodule*a" >actual &&
155 test_cmp expect actual
158 test_expect_success 'grep tree and more pathspecs' '
159 cat >expect <<-\EOF &&
160 HEAD:submodule/a:(1|2)d(3|4)
163 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul?/a" >actual &&
164 test_cmp expect actual
167 test_expect_success 'grep tree and more pathspecs' '
168 cat >expect <<-\EOF &&
169 HEAD:submodule/sub/a:(1|2)d(3|4)
172 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul*/sub/a" >actual &&
173 test_cmp expect actual
176 test_expect_success !MINGW 'grep recurse submodule colon in name' '
178 test_when_finished "rm -rf parent" &&
179 echo "(1|2)d(3|4)" >"parent/fi:le" &&
180 git -C parent add "fi:le" &&
181 git -C parent commit -m "add fi:le" &&
184 test_when_finished "rm -rf su:b" &&
185 echo "(1|2)d(3|4)" >"su:b/fi:le" &&
186 git -C "su:b" add "fi:le" &&
187 git -C "su:b" commit -m "add fi:le" &&
189 git -C parent submodule add "../su:b" "su:b" &&
190 git -C parent commit -m "add submodule" &&
192 cat >expect <<-\EOF &&
194 su:b/fi:le:(1|2)d(3|4)
196 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
197 test_cmp expect actual &&
199 cat >expect <<-\EOF &&
200 HEAD:fi:le:(1|2)d(3|4)
201 HEAD:su:b/fi:le:(1|2)d(3|4)
203 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD >actual &&
204 test_cmp expect actual
207 test_expect_success 'grep history with moved submoules' '
209 test_when_finished "rm -rf parent" &&
210 echo "(1|2)d(3|4)" >parent/file &&
211 git -C parent add file &&
212 git -C parent commit -m "add file" &&
215 test_when_finished "rm -rf sub" &&
216 echo "(1|2)d(3|4)" >sub/file &&
217 git -C sub add file &&
218 git -C sub commit -m "add file" &&
220 git -C parent submodule add ../sub dir/sub &&
221 git -C parent commit -m "add submodule" &&
223 cat >expect <<-\EOF &&
224 dir/sub/file:(1|2)d(3|4)
227 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
228 test_cmp expect actual &&
230 git -C parent mv dir/sub sub-moved &&
231 git -C parent commit -m "moved submodule" &&
233 cat >expect <<-\EOF &&
235 sub-moved/file:(1|2)d(3|4)
237 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
238 test_cmp expect actual &&
240 cat >expect <<-\EOF &&
241 HEAD^:dir/sub/file:(1|2)d(3|4)
242 HEAD^:file:(1|2)d(3|4)
244 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD^ >actual &&
245 test_cmp expect actual
248 test_expect_success 'grep using relative path' '
249 test_when_finished "rm -rf parent sub" &&
251 echo "(1|2)d(3|4)" >sub/file &&
252 git -C sub add file &&
253 git -C sub commit -m "add file" &&
256 echo "(1|2)d(3|4)" >parent/file &&
257 git -C parent add file &&
259 echo "(1|2)d(3|4)" >parent/src/file2 &&
260 git -C parent add src/file2 &&
261 git -C parent submodule add ../sub &&
262 git -C parent commit -m "add files and submodule" &&
265 cat >expect <<-\EOF &&
267 src/file2:(1|2)d(3|4)
270 git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
271 test_cmp expect actual &&
273 # Relative path to top
274 cat >expect <<-\EOF &&
277 ../sub/file:(1|2)d(3|4)
279 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- .. >actual &&
280 test_cmp expect actual &&
282 # Relative path to submodule
283 cat >expect <<-\EOF &&
284 ../sub/file:(1|2)d(3|4)
286 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- ../sub >actual &&
287 test_cmp expect actual
290 test_expect_success 'grep from a subdir' '
291 test_when_finished "rm -rf parent sub" &&
293 echo "(1|2)d(3|4)" >sub/file &&
294 git -C sub add file &&
295 git -C sub commit -m "add file" &&
299 echo "(1|2)d(3|4)" >parent/src/file &&
300 git -C parent add src/file &&
301 git -C parent submodule add ../sub src/sub &&
302 git -C parent submodule add ../sub sub &&
303 git -C parent commit -m "add files and submodules" &&
305 # Verify grep from root works
306 cat >expect <<-\EOF &&
308 src/sub/file:(1|2)d(3|4)
311 git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
312 test_cmp expect actual &&
314 # Verify grep from a subdir works
315 cat >expect <<-\EOF &&
319 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
320 test_cmp expect actual
323 test_incompatible_with_recurse_submodules ()
325 test_expect_success "--recurse-submodules and $1 are incompatible" "
326 test_must_fail git grep -e. --recurse-submodules $1 2>actual &&
327 test_i18ngrep 'not supported with --recurse-submodules' actual
331 test_incompatible_with_recurse_submodules --untracked
332 test_incompatible_with_recurse_submodules --no-index
334 test_expect_success 'grep --recurse-submodules should pass the pattern type along' '
336 test_must_fail git grep -F --recurse-submodules -e "(.|.)[\d]" &&
337 test_must_fail git -c grep.patternType=fixed grep --recurse-submodules -e "(.|.)[\d]" &&
340 git grep -G --recurse-submodules -e "(.|.)[\d]" >actual &&
341 cat >expect <<-\EOF &&
343 submodule/a:(1|2)d(3|4)
344 submodule/sub/a:(1|2)d(3|4)
346 test_cmp expect actual &&
347 git -c grep.patternType=basic grep --recurse-submodules -e "(.|.)[\d]" >actual &&
348 test_cmp expect actual &&
351 git grep -E --recurse-submodules -e "(.|.)[\d]" >actual &&
352 cat >expect <<-\EOF &&
353 .gitmodules:[submodule "submodule"]
354 .gitmodules: path = submodule
355 .gitmodules: url = ./submodule
357 submodule/.gitmodules:[submodule "sub"]
358 submodule/a:(1|2)d(3|4)
359 submodule/sub/a:(1|2)d(3|4)
361 test_cmp expect actual &&
362 git -c grep.patternType=extended grep --recurse-submodules -e "(.|.)[\d]" >actual &&
363 test_cmp expect actual &&
364 git -c grep.extendedRegexp=true grep --recurse-submodules -e "(.|.)[\d]" >actual &&
365 test_cmp expect actual &&
368 if test_have_prereq PCRE
370 git grep -P --recurse-submodules -e "(.|.)[\d]" >actual &&
371 cat >expect <<-\EOF &&
374 submodule/a:(1|2)d(3|4)
375 submodule/sub/a:(1|2)d(3|4)
377 test_cmp expect actual &&
378 git -c grep.patternType=perl grep --recurse-submodules -e "(.|.)[\d]" >actual &&
379 test_cmp expect actual