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_i18ncmp expect actual
85 $pwd/clone-foo1-sub1-../sub1-$sub1sha1
87 $pwd/clone-foo3-sub3-../sub3-$sub3sha1
90 test_expect_success 'test "submodule foreach" from subdirectory' '
94 git submodule foreach "echo \$toplevel-\$name-\$sm_path-\$displaypath-\$sha1" >../../actual
96 test_i18ncmp expect actual
99 test_expect_success 'setup nested submodules' '
100 git clone submodule nested1 &&
101 git clone submodule nested2 &&
102 git clone submodule nested3 &&
105 git submodule add ../submodule submodule &&
107 git commit -m "submodule" &&
108 git submodule init submodule
112 git submodule add ../nested3 nested3 &&
114 git commit -m "nested3" &&
115 git submodule init nested3
119 git submodule add ../nested2 nested2 &&
121 git commit -m "nested2" &&
122 git submodule init nested2
126 git submodule add ../nested1 nested1 &&
128 git commit -m "nested1" &&
129 git submodule init nested1
133 test_expect_success 'use "submodule foreach" to checkout 2nd level submodule' '
134 git clone super clone2 &&
137 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
138 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
139 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
140 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
141 git submodule update --init &&
142 git rev-parse --resolve-git-dir sub1/.git &&
143 git rev-parse --resolve-git-dir sub2/.git &&
144 git rev-parse --resolve-git-dir sub3/.git &&
145 git rev-parse --resolve-git-dir nested1/.git &&
146 test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git &&
147 git submodule foreach "git submodule update --init" &&
148 git rev-parse --resolve-git-dir nested1/nested2/.git &&
149 test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git
153 test_expect_success 'use "foreach --recursive" to checkout all submodules' '
156 git submodule foreach --recursive "git submodule update --init" &&
157 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
158 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
164 Entering 'nested1/nested2'
165 Entering 'nested1/nested2/nested3'
166 Entering 'nested1/nested2/nested3/submodule'
172 test_expect_success 'test messages from "foreach --recursive"' '
175 git submodule foreach --recursive "true" > ../actual
177 test_i18ncmp expect actual
181 Entering '../nested1'
182 Entering '../nested1/nested2'
183 Entering '../nested1/nested2/nested3'
184 Entering '../nested1/nested2/nested3/submodule'
190 test_expect_success 'test messages from "foreach --recursive" from subdirectory' '
195 git submodule foreach --recursive >../../actual
197 test_i18ncmp expect actual
199 sub1sha1=$(cd clone2/sub1 && git rev-parse HEAD)
200 sub2sha1=$(cd clone2/sub2 && git rev-parse HEAD)
201 sub3sha1=$(cd clone2/sub3 && git rev-parse HEAD)
202 nested1sha1=$(cd clone2/nested1 && git rev-parse HEAD)
203 nested2sha1=$(cd clone2/nested1/nested2 && git rev-parse HEAD)
204 nested3sha1=$(cd clone2/nested1/nested2/nested3 && git rev-parse HEAD)
205 submodulesha1=$(cd clone2/nested1/nested2/nested3/submodule && git rev-parse HEAD)
208 Entering '../nested1'
209 toplevel: $pwd/clone2 name: nested1 path: nested1 displaypath: ../nested1 hash: $nested1sha1
210 Entering '../nested1/nested2'
211 toplevel: $pwd/clone2/nested1 name: nested2 path: nested2 displaypath: ../nested1/nested2 hash: $nested2sha1
212 Entering '../nested1/nested2/nested3'
213 toplevel: $pwd/clone2/nested1/nested2 name: nested3 path: nested3 displaypath: ../nested1/nested2/nested3 hash: $nested3sha1
214 Entering '../nested1/nested2/nested3/submodule'
215 toplevel: $pwd/clone2/nested1/nested2/nested3 name: submodule path: submodule displaypath: ../nested1/nested2/nested3/submodule hash: $submodulesha1
217 toplevel: $pwd/clone2 name: foo1 path: sub1 displaypath: ../sub1 hash: $sub1sha1
219 toplevel: $pwd/clone2 name: foo2 path: sub2 displaypath: ../sub2 hash: $sub2sha1
221 toplevel: $pwd/clone2 name: foo3 path: sub3 displaypath: ../sub3 hash: $sub3sha1
224 test_expect_success 'test "submodule foreach --recursive" from subdirectory' '
226 cd clone2/untracked &&
227 git submodule foreach --recursive "echo toplevel: \$toplevel name: \$name path: \$sm_path displaypath: \$displaypath hash: \$sha1" >../../actual
229 test_i18ncmp expect actual
242 test_expect_success 'test "foreach --quiet --recursive"' '
245 git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
247 test_cmp expect actual
250 test_expect_success 'use "update --recursive" to checkout all submodules' '
251 git clone super clone3 &&
254 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
255 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
256 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
257 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
258 git submodule update --init --recursive &&
259 git rev-parse --resolve-git-dir sub1/.git &&
260 git rev-parse --resolve-git-dir sub2/.git &&
261 git rev-parse --resolve-git-dir sub3/.git &&
262 git rev-parse --resolve-git-dir nested1/.git &&
263 git rev-parse --resolve-git-dir nested1/nested2/.git &&
264 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
265 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
269 nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD)
270 nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
271 nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
272 submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
273 sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
274 sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
275 sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
276 sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
277 sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD)
280 $nested1sha1 nested1 (heads/master)
281 $nested2sha1 nested1/nested2 (heads/master)
282 $nested3sha1 nested1/nested2/nested3 (heads/master)
283 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
284 $sub1sha1 sub1 ($sub1sha1_short)
285 $sub2sha1 sub2 ($sub2sha1_short)
286 $sub3sha1 sub3 (heads/master)
289 test_expect_success 'test "status --recursive"' '
292 git submodule status --recursive > ../actual
294 test_cmp expect actual
298 $nested1sha1 nested1 (heads/master)
299 +$nested2sha1 nested1/nested2 (file2~1)
300 $nested3sha1 nested1/nested2/nested3 (heads/master)
301 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
304 test_expect_success 'ensure "status --cached --recursive" preserves the --cached flag' '
308 cd nested1/nested2 &&
311 git submodule status --cached --recursive -- nested1 > ../actual
313 test_cmp expect actual
316 nested2sha1=$(git -C clone3/nested1/nested2 rev-parse HEAD)
319 $nested1sha1 ../nested1 (heads/master)
320 +$nested2sha1 ../nested1/nested2 (file2)
321 $nested3sha1 ../nested1/nested2/nested3 (heads/master)
322 $submodulesha1 ../nested1/nested2/nested3/submodule (heads/master)
323 $sub1sha1 ../sub1 ($sub1sha1_short)
324 $sub2sha1 ../sub2 ($sub2sha1_short)
325 $sub3sha1 ../sub3 (heads/master)
328 test_expect_success 'test "status --recursive" from sub directory' '
331 mkdir tmp && cd tmp &&
332 git submodule status --recursive > ../../actual
334 test_cmp expect actual
337 test_expect_success 'use "git clone --recursive" to checkout all submodules' '
338 git clone --recursive super clone4 &&
341 git rev-parse --resolve-git-dir .git &&
342 git rev-parse --resolve-git-dir sub1/.git &&
343 git rev-parse --resolve-git-dir sub2/.git &&
344 git rev-parse --resolve-git-dir sub3/.git &&
345 git rev-parse --resolve-git-dir nested1/.git &&
346 git rev-parse --resolve-git-dir nested1/nested2/.git &&
347 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
348 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
352 test_expect_success 'test "update --recursive" with a flag with spaces' '
353 git clone super "common objects" &&
354 git clone super clone5 &&
357 test_must_fail git rev-parse --resolve-git-dir d nested1/.git &&
358 git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
359 git rev-parse --resolve-git-dir nested1/.git &&
360 git rev-parse --resolve-git-dir nested1/nested2/.git &&
361 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
362 test -f .git/modules/nested1/objects/info/alternates &&
363 test -f .git/modules/nested1/modules/nested2/objects/info/alternates &&
364 test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates
368 test_expect_success 'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
369 git clone super clone6 &&
372 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
373 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
374 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
375 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
376 git submodule update --init --recursive -- nested1 &&
377 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
378 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
379 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
380 git rev-parse --resolve-git-dir nested1/.git &&
381 git rev-parse --resolve-git-dir nested1/nested2/.git &&
382 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
383 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
387 test_expect_success 'command passed to foreach retains notion of stdin' '
390 git submodule foreach echo success >../expected &&
391 yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
393 test_cmp expected actual
396 test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
399 git submodule foreach --recursive echo success >../expected &&
400 yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
402 test_cmp expected actual
405 test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
408 git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
409 git submodule foreach echo \"quoted\" > ../actual
411 test_cmp expected actual
414 test_expect_success 'option-like arguments passed to foreach commands are not lost' '
417 git submodule foreach "echo be --quiet" > ../expected &&
418 git submodule foreach echo be --quiet > ../actual
420 grep -sq -e "--quiet" expected &&
421 test_cmp expected actual
424 test_expect_success 'option-like arguments passed to foreach recurse correctly' '
425 git -C clone2 submodule foreach --recursive "echo be --an-option" >expect &&
426 git -C clone2 submodule foreach --recursive echo be --an-option >actual &&
427 grep -e "--an-option" expect &&
428 test_cmp expect actual