3 # Copyright (c) 2009 Johan Herland
6 test_description
='Test "git submodule foreach"
8 This test verifies that "git submodule foreach" correctly visits all submodules
9 that are currently checked out.
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
18 test_expect_success
'setup a submodule tree' '
19 git config --global protocol.file.allow always &&
23 git commit -m upstream &&
25 git clone super submodule &&
28 git submodule add ../submodule sub1 &&
29 git submodule add ../submodule sub2 &&
30 git submodule add ../submodule sub3 &&
31 git config -f .gitmodules --rename-section \
32 submodule.sub1 submodule.foo1 &&
33 git config -f .gitmodules --rename-section \
34 submodule.sub2 submodule.foo2 &&
35 git config -f .gitmodules --rename-section \
36 submodule.sub3 submodule.foo3 &&
37 git add .gitmodules &&
39 git commit -m "submodules" &&
40 git submodule init sub1 &&
41 git submodule init sub2 &&
42 git submodule init sub3
46 echo different > file &&
49 git commit -m "different"
59 git commit -m "update sub3"
63 sub1sha1
=$
(cd super
/sub1
&& git rev-parse HEAD
)
64 sub3sha1
=$
(cd super
/sub3
&& git rev-parse HEAD
)
70 $pwd/clone-foo1-sub1-$sub1sha1
72 $pwd/clone-foo3-sub3-$sub3sha1
75 test_expect_success
'test basic "submodule foreach" usage' '
76 git clone super clone &&
79 git submodule update --init -- sub1 sub3 &&
80 git submodule foreach "echo \$toplevel-\$name-\$path-\$sha1" > ../actual &&
81 git config foo.bar zar &&
82 git submodule foreach "git config --file \"\$toplevel/.git/config\" foo.bar"
84 test_cmp expect actual
89 $pwd/clone-foo1-sub1-../sub1-$sub1sha1
91 $pwd/clone-foo3-sub3-../sub3-$sub3sha1
94 test_expect_success
'test "submodule foreach" from subdirectory' '
98 git submodule foreach "echo \$toplevel-\$name-\$sm_path-\$displaypath-\$sha1" >../../actual
100 test_cmp expect actual
103 test_expect_success
'setup nested submodules' '
104 git clone submodule nested1 &&
105 git clone submodule nested2 &&
106 git clone submodule nested3 &&
109 git submodule add ../submodule submodule &&
111 git commit -m "submodule" &&
112 git submodule init submodule
116 git submodule add ../nested3 nested3 &&
118 git commit -m "nested3" &&
119 git submodule init nested3
123 git submodule add ../nested2 nested2 &&
125 git commit -m "nested2" &&
126 git submodule init nested2
130 git submodule add ../nested1 nested1 &&
132 git commit -m "nested1" &&
133 git submodule init nested1
137 test_expect_success
'use "submodule foreach" to checkout 2nd level submodule' '
138 git clone super clone2 &&
141 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
142 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
143 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
144 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
145 git submodule update --init &&
146 git rev-parse --resolve-git-dir sub1/.git &&
147 git rev-parse --resolve-git-dir sub2/.git &&
148 git rev-parse --resolve-git-dir sub3/.git &&
149 git rev-parse --resolve-git-dir nested1/.git &&
150 test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git &&
151 git submodule foreach "git submodule update --init" &&
152 git rev-parse --resolve-git-dir nested1/nested2/.git &&
153 test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git
157 test_expect_success
'usage: foreach -- --not-an-option' '
158 test_expect_code 1 git submodule foreach -- --not-an-option &&
159 test_expect_code 1 git -C clone2 submodule foreach -- --not-an-option
162 test_expect_success
'use "foreach --recursive" to checkout all submodules' '
165 git submodule foreach --recursive "git submodule update --init" &&
166 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
167 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
173 Entering 'nested1/nested2'
174 Entering 'nested1/nested2/nested3'
175 Entering 'nested1/nested2/nested3/submodule'
181 test_expect_success
'test messages from "foreach --recursive"' '
184 git submodule foreach --recursive "true" > ../actual
186 test_cmp expect actual
190 Entering '../nested1'
191 Entering '../nested1/nested2'
192 Entering '../nested1/nested2/nested3'
193 Entering '../nested1/nested2/nested3/submodule'
199 test_expect_success
'test messages from "foreach --recursive" from subdirectory' '
204 git submodule foreach --recursive >../../actual
206 test_cmp expect actual
208 sub1sha1
=$
(cd clone
2/sub1
&& git rev-parse HEAD
)
209 sub2sha1
=$
(cd clone
2/sub2
&& git rev-parse HEAD
)
210 sub3sha1
=$
(cd clone
2/sub3
&& git rev-parse HEAD
)
211 nested1sha1
=$
(cd clone
2/nested1
&& git rev-parse HEAD
)
212 nested2sha1
=$
(cd clone
2/nested
1/nested2
&& git rev-parse HEAD
)
213 nested3sha1
=$
(cd clone
2/nested
1/nested
2/nested3
&& git rev-parse HEAD
)
214 submodulesha1
=$
(cd clone
2/nested
1/nested
2/nested
3/submodule
&& git rev-parse HEAD
)
217 Entering '../nested1'
218 toplevel: $pwd/clone2 name: nested1 path: nested1 displaypath: ../nested1 hash: $nested1sha1
219 Entering '../nested1/nested2'
220 toplevel: $pwd/clone2/nested1 name: nested2 path: nested2 displaypath: ../nested1/nested2 hash: $nested2sha1
221 Entering '../nested1/nested2/nested3'
222 toplevel: $pwd/clone2/nested1/nested2 name: nested3 path: nested3 displaypath: ../nested1/nested2/nested3 hash: $nested3sha1
223 Entering '../nested1/nested2/nested3/submodule'
224 toplevel: $pwd/clone2/nested1/nested2/nested3 name: submodule path: submodule displaypath: ../nested1/nested2/nested3/submodule hash: $submodulesha1
226 toplevel: $pwd/clone2 name: foo1 path: sub1 displaypath: ../sub1 hash: $sub1sha1
228 toplevel: $pwd/clone2 name: foo2 path: sub2 displaypath: ../sub2 hash: $sub2sha1
230 toplevel: $pwd/clone2 name: foo3 path: sub3 displaypath: ../sub3 hash: $sub3sha1
233 test_expect_success
'test "submodule foreach --recursive" from subdirectory' '
235 cd clone2/untracked &&
236 git submodule foreach --recursive "echo toplevel: \$toplevel name: \$name path: \$sm_path displaypath: \$displaypath hash: \$sha1" >../../actual
238 test_cmp expect actual
251 test_expect_success
'test "foreach --quiet --recursive"' '
254 git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
256 test_cmp expect actual
259 test_expect_success
'use "update --recursive" to checkout all submodules' '
260 git clone super clone3 &&
263 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
264 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
265 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
266 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
267 git submodule update --init --recursive &&
268 git rev-parse --resolve-git-dir sub1/.git &&
269 git rev-parse --resolve-git-dir sub2/.git &&
270 git rev-parse --resolve-git-dir sub3/.git &&
271 git rev-parse --resolve-git-dir nested1/.git &&
272 git rev-parse --resolve-git-dir nested1/nested2/.git &&
273 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
274 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
278 nested1sha1
=$
(cd clone
3/nested1
&& git rev-parse HEAD
)
279 nested2sha1
=$
(cd clone
3/nested
1/nested2
&& git rev-parse HEAD
)
280 nested3sha1
=$
(cd clone
3/nested
1/nested
2/nested3
&& git rev-parse HEAD
)
281 submodulesha1
=$
(cd clone
3/nested
1/nested
2/nested
3/submodule
&& git rev-parse HEAD
)
282 sub1sha1
=$
(cd clone
3/sub1
&& git rev-parse HEAD
)
283 sub2sha1
=$
(cd clone
3/sub2
&& git rev-parse HEAD
)
284 sub3sha1
=$
(cd clone
3/sub3
&& git rev-parse HEAD
)
285 sub1sha1_short
=$
(cd clone
3/sub1
&& git rev-parse
--short HEAD
)
286 sub2sha1_short
=$
(cd clone
3/sub2
&& git rev-parse
--short HEAD
)
289 $nested1sha1 nested1 (heads/main)
290 $nested2sha1 nested1/nested2 (heads/main)
291 $nested3sha1 nested1/nested2/nested3 (heads/main)
292 $submodulesha1 nested1/nested2/nested3/submodule (heads/main)
293 $sub1sha1 sub1 ($sub1sha1_short)
294 $sub2sha1 sub2 ($sub2sha1_short)
295 $sub3sha1 sub3 (heads/main)
298 test_expect_success
'test "status --recursive"' '
301 git submodule status --recursive > ../actual
303 test_cmp expect actual
307 $nested1sha1 nested1 (heads/main)
308 +$nested2sha1 nested1/nested2 (file2~1)
309 $nested3sha1 nested1/nested2/nested3 (heads/main)
310 $submodulesha1 nested1/nested2/nested3/submodule (heads/main)
313 test_expect_success
'ensure "status --cached --recursive" preserves the --cached flag' '
317 cd nested1/nested2 &&
320 git submodule status --cached --recursive -- nested1 > ../actual
322 test_cmp expect actual
325 nested2sha1
=$
(git
-C clone
3/nested
1/nested2 rev-parse HEAD
)
328 $nested1sha1 ../nested1 (heads/main)
329 +$nested2sha1 ../nested1/nested2 (file2)
330 $nested3sha1 ../nested1/nested2/nested3 (heads/main)
331 $submodulesha1 ../nested1/nested2/nested3/submodule (heads/main)
332 $sub1sha1 ../sub1 ($sub1sha1_short)
333 $sub2sha1 ../sub2 ($sub2sha1_short)
334 $sub3sha1 ../sub3 (heads/main)
337 test_expect_success
'test "status --recursive" from sub directory' '
340 mkdir tmp && cd tmp &&
341 git submodule status --recursive > ../../actual
343 test_cmp expect actual
346 test_expect_success
'use "git clone --recursive" to checkout all submodules' '
347 git clone --recursive super clone4 &&
350 git rev-parse --resolve-git-dir .git &&
351 git rev-parse --resolve-git-dir sub1/.git &&
352 git rev-parse --resolve-git-dir sub2/.git &&
353 git rev-parse --resolve-git-dir sub3/.git &&
354 git rev-parse --resolve-git-dir nested1/.git &&
355 git rev-parse --resolve-git-dir nested1/nested2/.git &&
356 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
357 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
361 test_expect_success
'test "update --recursive" with a flag with spaces' '
362 git clone super "common objects" &&
363 git clone super clone5 &&
366 test_must_fail git rev-parse --resolve-git-dir d nested1/.git &&
367 git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
368 git rev-parse --resolve-git-dir nested1/.git &&
369 git rev-parse --resolve-git-dir nested1/nested2/.git &&
370 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
371 test -f .git/modules/nested1/objects/info/alternates &&
372 test -f .git/modules/nested1/modules/nested2/objects/info/alternates &&
373 test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates
377 test_expect_success
'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
378 git clone super clone6 &&
381 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
382 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
383 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
384 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
385 git submodule update --init --recursive -- nested1 &&
386 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
387 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
388 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
389 git rev-parse --resolve-git-dir nested1/.git &&
390 git rev-parse --resolve-git-dir nested1/nested2/.git &&
391 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
392 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
396 test_expect_success
'command passed to foreach retains notion of stdin' '
399 git submodule foreach echo success >../expected &&
400 yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
402 test_cmp expected actual
405 test_expect_success
'command passed to foreach --recursive retains notion of stdin' '
408 git submodule foreach --recursive echo success >../expected &&
409 yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
411 test_cmp expected actual
414 test_expect_success
'multi-argument command passed to foreach is not shell-evaluated twice' '
417 git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
418 git submodule foreach echo \"quoted\" > ../actual
420 test_cmp expected actual
423 test_expect_success
'option-like arguments passed to foreach commands are not lost' '
426 git submodule foreach "echo be --quiet" > ../expected &&
427 git submodule foreach echo be --quiet > ../actual
429 grep -sq -e "--quiet" expected &&
430 test_cmp expected actual
433 test_expect_success
'option-like arguments passed to foreach recurse correctly' '
434 git -C clone2 submodule foreach --recursive "echo be --an-option" >expect &&
435 git -C clone2 submodule foreach --recursive echo be --an-option >actual &&
436 grep -e "--an-option" expect &&
437 test_cmp expect actual