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 and basic pathspecs' '
37 cat >expect <<-\EOF &&
38 submodule/a:(1|2)d(3|4)
41 git grep -e. --recurse-submodules -- submodule >actual &&
42 test_cmp expect actual
45 test_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" &&
54 git commit -m "updated submodule" &&
56 cat >expect <<-\EOF &&
59 submodule/a:(1|2)d(3|4)
60 submodule/sub/a:(1|2)d(3|4)
63 git grep -e "(3|4)" --recurse-submodules >actual &&
64 test_cmp expect actual
67 test_expect_success 'grep and multiple patterns' '
68 cat >expect <<-\EOF &&
70 submodule/a:(1|2)d(3|4)
71 submodule/sub/a:(1|2)d(3|4)
74 git grep -e "(3|4)" --and -e "(1|2)" --recurse-submodules >actual &&
75 test_cmp expect actual
78 test_expect_success 'grep and multiple patterns' '
79 cat >expect <<-\EOF &&
83 git grep -e "(3|4)" --and --not -e "(1|2)" --recurse-submodules >actual &&
84 test_cmp expect actual
87 test_expect_success 'basic grep tree' '
88 cat >expect <<-\EOF &&
91 HEAD:submodule/a:(1|2)d(3|4)
92 HEAD:submodule/sub/a:(1|2)d(3|4)
95 git grep -e "(3|4)" --recurse-submodules HEAD >actual &&
96 test_cmp expect actual
99 test_expect_success 'grep tree HEAD^' '
100 cat >expect <<-\EOF &&
103 HEAD^:submodule/a:(1|2)d(3|4)
106 git grep -e "(3|4)" --recurse-submodules HEAD^ >actual &&
107 test_cmp expect actual
110 test_expect_success 'grep tree HEAD^^' '
111 cat >expect <<-\EOF &&
116 git grep -e "(3|4)" --recurse-submodules HEAD^^ >actual &&
117 test_cmp expect actual
120 test_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)
126 git grep -e "(3|4)" --recurse-submodules HEAD -- submodule >actual &&
127 test_cmp expect actual
130 test_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)
136 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodule*a" >actual &&
137 test_cmp expect actual
140 test_expect_success 'grep tree and more pathspecs' '
141 cat >expect <<-\EOF &&
142 HEAD:submodule/a:(1|2)d(3|4)
145 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul?/a" >actual &&
146 test_cmp expect actual
149 test_expect_success 'grep tree and more pathspecs' '
150 cat >expect <<-\EOF &&
151 HEAD:submodule/sub/a:(1|2)d(3|4)
154 git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul*/sub/a" >actual &&
155 test_cmp expect actual
158 test_expect_success !MINGW 'grep recurse submodule colon in name' '
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" &&
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" &&
171 git -C parent submodule add "../su:b" "su:b" &&
172 git -C parent commit -m "add submodule" &&
174 cat >expect <<-\EOF &&
176 su:b/fi:le:(1|2)d(3|4)
178 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
179 test_cmp expect actual &&
181 cat >expect <<-\EOF &&
182 HEAD:fi:le:(1|2)d(3|4)
183 HEAD:su:b/fi:le:(1|2)d(3|4)
185 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD >actual &&
186 test_cmp expect actual
189 test_expect_success 'grep history with moved submoules' '
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" &&
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" &&
202 git -C parent submodule add ../sub dir/sub &&
203 git -C parent commit -m "add submodule" &&
205 cat >expect <<-\EOF &&
206 dir/sub/file:(1|2)d(3|4)
209 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
210 test_cmp expect actual &&
212 git -C parent mv dir/sub sub-moved &&
213 git -C parent commit -m "moved submodule" &&
215 cat >expect <<-\EOF &&
217 sub-moved/file:(1|2)d(3|4)
219 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
220 test_cmp expect actual &&
222 cat >expect <<-\EOF &&
223 HEAD^:dir/sub/file:(1|2)d(3|4)
224 HEAD^:file:(1|2)d(3|4)
226 git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD^ >actual &&
227 test_cmp expect actual
230 test_expect_success 'grep using relative path' '
231 test_when_finished "rm -rf parent sub" &&
233 echo "(1|2)d(3|4)" >sub/file &&
234 git -C sub add file &&
235 git -C sub commit -m "add file" &&
238 echo "(1|2)d(3|4)" >parent/file &&
239 git -C parent add file &&
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" &&
247 cat >expect <<-\EOF &&
249 src/file2:(1|2)d(3|4)
252 git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
253 test_cmp expect actual &&
255 # Relative path to top
256 cat >expect <<-\EOF &&
259 ../sub/file:(1|2)d(3|4)
261 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- .. >actual &&
262 test_cmp expect actual &&
264 # Relative path to submodule
265 cat >expect <<-\EOF &&
266 ../sub/file:(1|2)d(3|4)
268 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- ../sub >actual &&
269 test_cmp expect actual
272 test_expect_success 'grep from a subdir' '
273 test_when_finished "rm -rf parent sub" &&
275 echo "(1|2)d(3|4)" >sub/file &&
276 git -C sub add file &&
277 git -C sub commit -m "add file" &&
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" &&
287 # Verify grep from root works
288 cat >expect <<-\EOF &&
290 src/sub/file:(1|2)d(3|4)
293 git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
294 test_cmp expect actual &&
296 # Verify grep from a subdir works
297 cat >expect <<-\EOF &&
301 git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
302 test_cmp expect actual
305 test_incompatible_with_recurse_submodules ()
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
313 test_incompatible_with_recurse_submodules --untracked
314 test_incompatible_with_recurse_submodules --no-index
316 test_expect_success 'grep --recurse-submodules should pass the pattern type along' '
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]" &&
322 git grep -G --recurse-submodules -e "(.|.)[\d]" >actual &&
323 cat >expect <<-\EOF &&
325 submodule/a:(1|2)d(3|4)
326 submodule/sub/a:(1|2)d(3|4)
328 test_cmp expect actual &&
329 git -c grep.patternType=basic grep --recurse-submodules -e "(.|.)[\d]" >actual &&
330 test_cmp expect actual &&
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
339 submodule/.gitmodules:[submodule "sub"]
340 submodule/a:(1|2)d(3|4)
341 submodule/sub/a:(1|2)d(3|4)
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 &&
350 if test_have_prereq PCRE
352 git grep -P --recurse-submodules -e "(.|.)[\d]" >actual &&
353 cat >expect <<-\EOF &&
356 submodule/a:(1|2)d(3|4)
357 submodule/sub/a:(1|2)d(3|4)
359 test_cmp expect actual &&
360 git -c grep.patternType=perl grep --recurse-submodules -e "(.|.)[\d]" >actual &&
361 test_cmp expect actual