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 test_cmp expect actual
83 test_expect_success
'setup nested submodules' '
84 git clone submodule nested1 &&
85 git clone submodule nested2 &&
86 git clone submodule nested3 &&
89 git submodule add ../submodule submodule &&
91 git commit -m "submodule" &&
92 git submodule init submodule
96 git submodule add ../nested3 nested3 &&
98 git commit -m "nested3" &&
99 git submodule init nested3
103 git submodule add ../nested2 nested2 &&
105 git commit -m "nested2" &&
106 git submodule init nested2
110 git submodule add ../nested1 nested1 &&
112 git commit -m "nested1" &&
113 git submodule init nested1
117 test_expect_success
'use "submodule foreach" to checkout 2nd level submodule' '
118 git clone super clone2 &&
121 test ! -d sub1/.git &&
122 test ! -d sub2/.git &&
123 test ! -d sub3/.git &&
124 test ! -d nested1/.git &&
125 git submodule update --init &&
129 test -d nested1/.git &&
130 test ! -d nested1/nested2/.git &&
131 git submodule foreach "git submodule update --init" &&
132 test -d nested1/nested2/.git &&
133 test ! -d nested1/nested2/nested3/.git
137 test_expect_success
'use "foreach --recursive" to checkout all submodules' '
140 git submodule foreach --recursive "git submodule update --init" &&
141 test -d nested1/nested2/nested3/.git &&
142 test -d nested1/nested2/nested3/submodule/.git
148 Entering 'nested1/nested2'
149 Entering 'nested1/nested2/nested3'
150 Entering 'nested1/nested2/nested3/submodule'
156 test_expect_success
'test messages from "foreach --recursive"' '
159 git submodule foreach --recursive "true" > ../actual
161 test_cmp expect actual
174 test_expect_success
'test "foreach --quiet --recursive"' '
177 git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
179 test_cmp expect actual
182 test_expect_success
'use "update --recursive" to checkout all submodules' '
183 git clone super clone3 &&
186 test ! -d sub1/.git &&
187 test ! -d sub2/.git &&
188 test ! -d sub3/.git &&
189 test ! -d nested1/.git &&
190 git submodule update --init --recursive &&
194 test -d nested1/.git &&
195 test -d nested1/nested2/.git &&
196 test -d nested1/nested2/nested3/.git &&
197 test -d nested1/nested2/nested3/submodule/.git
201 nested1sha1
=$
(cd clone
3/nested1
&& git rev-parse HEAD
)
202 nested2sha1
=$
(cd clone
3/nested
1/nested2
&& git rev-parse HEAD
)
203 nested3sha1
=$
(cd clone
3/nested
1/nested
2/nested3
&& git rev-parse HEAD
)
204 submodulesha1
=$
(cd clone
3/nested
1/nested
2/nested
3/submodule
&& git rev-parse HEAD
)
205 sub1sha1
=$
(cd clone
3/sub1
&& git rev-parse HEAD
)
206 sub2sha1
=$
(cd clone
3/sub2
&& git rev-parse HEAD
)
207 sub3sha1
=$
(cd clone
3/sub3
&& git rev-parse HEAD
)
208 sub1sha1_short
=$
(cd clone
3/sub1
&& git rev-parse
--short HEAD
)
209 sub2sha1_short
=$
(cd clone
3/sub2
&& git rev-parse
--short HEAD
)
212 $nested1sha1 nested1 (heads/master)
213 $nested2sha1 nested1/nested2 (heads/master)
214 $nested3sha1 nested1/nested2/nested3 (heads/master)
215 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
216 $sub1sha1 sub1 ($sub1sha1_short)
217 $sub2sha1 sub2 ($sub2sha1_short)
218 $sub3sha1 sub3 (heads/master)
221 test_expect_success
'test "status --recursive"' '
224 git submodule status --recursive > ../actual
226 test_cmp expect actual
229 sed -e "/nested1 /s/.*/+$nested1sha1 nested1 (file2~1)/;/sub[1-3]/d" < expect
> expect2
232 test_expect_success
'ensure "status --cached --recursive" preserves the --cached flag' '
239 git submodule status --cached --recursive -- nested1 > ../actual
241 if test_have_prereq MINGW
245 test_cmp expect actual
248 test_expect_success
'use "git clone --recursive" to checkout all submodules' '
249 git clone --recursive super clone4 &&
250 test -d clone4/.git &&
251 test -d clone4/sub1/.git &&
252 test -d clone4/sub2/.git &&
253 test -d clone4/sub3/.git &&
254 test -d clone4/nested1/.git &&
255 test -d clone4/nested1/nested2/.git &&
256 test -d clone4/nested1/nested2/nested3/.git &&
257 test -d clone4/nested1/nested2/nested3/submodule/.git
260 test_expect_success
'test "update --recursive" with a flag with spaces' '
261 git clone super "common objects" &&
262 git clone super clone5 &&
265 test ! -d nested1/.git &&
266 git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
267 test -d nested1/.git &&
268 test -d nested1/nested2/.git &&
269 test -d nested1/nested2/nested3/.git &&
270 test -f nested1/.git/objects/info/alternates &&
271 test -f nested1/nested2/.git/objects/info/alternates &&
272 test -f nested1/nested2/nested3/.git/objects/info/alternates
276 test_expect_success
'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
277 git clone super clone6 &&
280 test ! -d sub1/.git &&
281 test ! -d sub2/.git &&
282 test ! -d sub3/.git &&
283 test ! -d nested1/.git &&
284 git submodule update --init --recursive -- nested1 &&
285 test ! -d sub1/.git &&
286 test ! -d sub2/.git &&
287 test ! -d sub3/.git &&
288 test -d nested1/.git &&
289 test -d nested1/nested2/.git &&
290 test -d nested1/nested2/nested3/.git &&
291 test -d nested1/nested2/nested3/submodule/.git
295 test_expect_success
'command passed to foreach retains notion of stdin' '
298 git submodule foreach echo success >../expected &&
299 yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
301 test_cmp expected actual
304 test_expect_success
'command passed to foreach --recursive retains notion of stdin' '
307 git submodule foreach --recursive echo success >../expected &&
308 yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
310 test_cmp expected actual