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.
15 test_expect_success
'setup a submodule tree' '
19 git commit -m upstream &&
21 git clone super submodule &&
24 git submodule add ../submodule sub1 &&
25 git submodule add ../submodule sub2 &&
26 git submodule add ../submodule sub3 &&
27 git config -f .gitmodules --rename-section \
28 submodule.sub1 submodule.foo1 &&
29 git config -f .gitmodules --rename-section \
30 submodule.sub2 submodule.foo2 &&
31 git config -f .gitmodules --rename-section \
32 submodule.sub3 submodule.foo3 &&
33 git add .gitmodules &&
35 git commit -m "submodules" &&
36 git submodule init sub1 &&
37 git submodule init sub2 &&
38 git submodule init sub3
42 echo different > file &&
45 git commit -m "different"
55 git commit -m "update sub3"
59 sub1sha1
=$
(cd super
/sub1
&& git rev-parse HEAD
)
60 sub3sha1
=$
(cd super
/sub3
&& git rev-parse HEAD
)
66 $pwd/clone-foo1-sub1-$sub1sha1
68 $pwd/clone-foo3-sub3-$sub3sha1
71 test_expect_success
'test basic "submodule foreach" usage' '
72 git clone super clone &&
75 git submodule update --init -- sub1 sub3 &&
76 git submodule foreach "echo \$toplevel-\$name-\$path-\$sha1" > ../actual &&
77 git config foo.bar zar &&
78 git submodule foreach "git config --file \"\$toplevel/.git/config\" foo.bar"
80 if test_have_prereq MINGW
84 test_i18ncmp expect actual
87 test_expect_success
'setup nested submodules' '
88 git clone submodule nested1 &&
89 git clone submodule nested2 &&
90 git clone submodule nested3 &&
93 git submodule add ../submodule submodule &&
95 git commit -m "submodule" &&
96 git submodule init submodule
100 git submodule add ../nested3 nested3 &&
102 git commit -m "nested3" &&
103 git submodule init nested3
107 git submodule add ../nested2 nested2 &&
109 git commit -m "nested2" &&
110 git submodule init nested2
114 git submodule add ../nested1 nested1 &&
116 git commit -m "nested1" &&
117 git submodule init nested1
121 test_expect_success
'use "submodule foreach" to checkout 2nd level submodule' '
122 git clone super clone2 &&
125 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
126 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
127 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
128 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
129 git submodule update --init &&
130 git rev-parse --resolve-git-dir sub1/.git &&
131 git rev-parse --resolve-git-dir sub2/.git &&
132 git rev-parse --resolve-git-dir sub3/.git &&
133 git rev-parse --resolve-git-dir nested1/.git &&
134 test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git &&
135 git submodule foreach "git submodule update --init" &&
136 git rev-parse --resolve-git-dir nested1/nested1/nested2/.git
137 test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git
141 test_expect_success
'use "foreach --recursive" to checkout all submodules' '
144 git submodule foreach --recursive "git submodule update --init" &&
145 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
146 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
152 Entering 'nested1/nested2'
153 Entering 'nested1/nested2/nested3'
154 Entering 'nested1/nested2/nested3/submodule'
160 test_expect_success
'test messages from "foreach --recursive"' '
163 git submodule foreach --recursive "true" > ../actual
165 if test_have_prereq MINGW
169 test_i18ncmp expect actual
182 test_expect_success
'test "foreach --quiet --recursive"' '
185 git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
187 if test_have_prereq MINGW
191 test_cmp expect actual
194 test_expect_success
'use "update --recursive" to checkout all submodules' '
195 git clone super clone3 &&
198 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
199 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
200 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
201 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
202 git submodule update --init --recursive &&
203 git rev-parse --resolve-git-dir sub1/.git &&
204 git rev-parse --resolve-git-dir sub2/.git &&
205 git rev-parse --resolve-git-dir sub3/.git &&
206 git rev-parse --resolve-git-dir nested1/.git &&
207 git rev-parse --resolve-git-dir nested1/nested2/.git &&
208 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
209 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
213 nested1sha1
=$
(cd clone
3/nested1
&& git rev-parse HEAD
)
214 nested2sha1
=$
(cd clone
3/nested
1/nested2
&& git rev-parse HEAD
)
215 nested3sha1
=$
(cd clone
3/nested
1/nested
2/nested3
&& git rev-parse HEAD
)
216 submodulesha1
=$
(cd clone
3/nested
1/nested
2/nested
3/submodule
&& git rev-parse HEAD
)
217 sub1sha1
=$
(cd clone
3/sub1
&& git rev-parse HEAD
)
218 sub2sha1
=$
(cd clone
3/sub2
&& git rev-parse HEAD
)
219 sub3sha1
=$
(cd clone
3/sub3
&& git rev-parse HEAD
)
220 sub1sha1_short
=$
(cd clone
3/sub1
&& git rev-parse
--short HEAD
)
221 sub2sha1_short
=$
(cd clone
3/sub2
&& git rev-parse
--short HEAD
)
224 $nested1sha1 nested1 (heads/master)
225 $nested2sha1 nested1/nested2 (heads/master)
226 $nested3sha1 nested1/nested2/nested3 (heads/master)
227 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
228 $sub1sha1 sub1 ($sub1sha1_short)
229 $sub2sha1 sub2 ($sub2sha1_short)
230 $sub3sha1 sub3 (heads/master)
233 test_expect_success
'test "status --recursive"' '
236 git submodule status --recursive > ../actual
238 if test_have_prereq MINGW
242 test_cmp expect actual
245 sed -e "/nested1 /s/.*/+$nested1sha1 nested1 (file2~1)/;/sub[1-3]/d" < expect
> expect2
248 test_expect_success
'ensure "status --cached --recursive" preserves the --cached flag' '
255 git submodule status --cached --recursive -- nested1 > ../actual
257 if test_have_prereq MINGW
261 test_cmp expect actual
264 test_expect_success
'use "git clone --recursive" to checkout all submodules' '
265 git clone --recursive super clone4 &&
268 git rev-parse --resolve-git-dir .git &&
269 git rev-parse --resolve-git-dir sub1/.git &&
270 git rev-parse --resolve-git-dir sub2/.git &&
271 git rev-parse --resolve-git-dir sub3/.git &&
272 git rev-parse --resolve-git-dir nested1/.git &&
273 git rev-parse --resolve-git-dir nested1/nested2/.git &&
274 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
275 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
279 test_expect_success
'test "update --recursive" with a flag with spaces' '
280 git clone super "common objects" &&
281 git clone super clone5 &&
284 test_must_fail git rev-parse --resolve-git-dir d nested1/.git &&
285 git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
286 git rev-parse --resolve-git-dir nested1/.git &&
287 git rev-parse --resolve-git-dir nested1/nested2/.git &&
288 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
289 test -f .git/modules/nested1/objects/info/alternates &&
290 test -f .git/modules/nested1/modules/nested2/objects/info/alternates &&
291 test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates
295 test_expect_success
'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
296 git clone super clone6 &&
299 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
300 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
301 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
302 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
303 git submodule update --init --recursive -- nested1 &&
304 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
305 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
306 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
307 git rev-parse --resolve-git-dir nested1/.git &&
308 git rev-parse --resolve-git-dir nested1/nested2/.git &&
309 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
310 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
314 test_expect_success
'command passed to foreach retains notion of stdin' '
317 git submodule foreach echo success >../expected &&
318 yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
320 test_cmp expected actual
323 test_expect_success
'command passed to foreach --recursive retains notion of stdin' '
326 git submodule foreach --recursive echo success >../expected &&
327 yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
329 test_cmp expected actual