Merge branch 'js/pull-rebase-type-shorthand'
[git.git] / t / t7814-grep-recurse-submodules.sh
blob7184113b9b2b381a6e7e0def8a8c1ba06771f8a6
1 #!/bin/sh
3 test_description='Test grep recurse-submodules feature
5 This test verifies the recurse-submodules feature correctly greps across
6 submodules.
9 . ./test-lib.sh
11 test_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"
25 test_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
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 &&
46 a:(1|2)d(3|4)
47 b/b:(3|4)
48 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)
57 EOF
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" &&
71 git add submodule &&
72 git commit -m "updated submodule" &&
74 cat >expect <<-\EOF &&
75 a:(1|2)d(3|4)
76 b/b:(3|4)
77 submodule/a:(1|2)d(3|4)
78 submodule/sub/a:(1|2)d(3|4)
79 EOF
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 &&
87 a:(1|2)d(3|4)
88 submodule/a:(1|2)d(3|4)
89 submodule/sub/a:(1|2)d(3|4)
90 EOF
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 &&
98 b/b:(3|4)
99 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 &&
107 HEAD:a:(1|2)d(3|4)
108 HEAD:b/b:(3|4)
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 &&
119 HEAD^:a:(1|2)d(3|4)
120 HEAD^:b/b:(3|4)
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 &&
130 HEAD^^:a:(1|2)d(3|4)
131 HEAD^^:b/b:(3|4)
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' '
177 git init parent &&
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" &&
183 git init "su:b" &&
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 &&
193 fi:le:(1|2)d(3|4)
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' '
208 git init parent &&
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" &&
214 git init sub &&
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)
225 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 &&
234 file:(1|2)d(3|4)
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" &&
250 git init sub &&
251 echo "(1|2)d(3|4)" >sub/file &&
252 git -C sub add file &&
253 git -C sub commit -m "add file" &&
255 git init parent &&
256 echo "(1|2)d(3|4)" >parent/file &&
257 git -C parent add file &&
258 mkdir parent/src &&
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" &&
264 # From top works
265 cat >expect <<-\EOF &&
266 file:(1|2)d(3|4)
267 src/file2:(1|2)d(3|4)
268 sub/file:(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 &&
275 ../file:(1|2)d(3|4)
276 file2:(1|2)d(3|4)
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" &&
292 git init sub &&
293 echo "(1|2)d(3|4)" >sub/file &&
294 git -C sub add file &&
295 git -C sub commit -m "add file" &&
297 git init parent &&
298 mkdir parent/src &&
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 &&
307 src/file:(1|2)d(3|4)
308 src/sub/file:(1|2)d(3|4)
309 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 &&
316 file:(1|2)d(3|4)
317 sub/file:(1|2)d(3|4)
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' '
335 # Fixed
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]" &&
339 # Basic
340 git grep -G --recurse-submodules -e "(.|.)[\d]" >actual &&
341 cat >expect <<-\EOF &&
342 a:(1|2)d(3|4)
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 &&
350 # Extended
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
356 a:(1|2)d(3|4)
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 &&
367 # Perl
368 if test_have_prereq PCRE
369 then
370 git grep -P --recurse-submodules -e "(.|.)[\d]" >actual &&
371 cat >expect <<-\EOF &&
372 a:(1|2)d(3|4)
373 b/b:(3|4)
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
383 test_done