Sync with 2.36.6
[git/debian.git] / t / t7407-submodule-foreach.sh
blob59bd150166793000ed60e70fc9e570519f53f055
1 #!/bin/sh
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
15 . ./test-lib.sh
18 test_expect_success 'setup a submodule tree' '
19 git config --global protocol.file.allow always &&
20 echo file > file &&
21 git add file &&
22 test_tick &&
23 git commit -m upstream &&
24 git clone . super &&
25 git clone super submodule &&
27 cd super &&
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 &&
38 test_tick &&
39 git commit -m "submodules" &&
40 git submodule init sub1 &&
41 git submodule init sub2 &&
42 git submodule init sub3
43 ) &&
45 cd submodule &&
46 echo different > file &&
47 git add file &&
48 test_tick &&
49 git commit -m "different"
50 ) &&
52 cd super &&
54 cd sub3 &&
55 git pull
56 ) &&
57 git add sub3 &&
58 test_tick &&
59 git commit -m "update sub3"
63 sub1sha1=$(cd super/sub1 && git rev-parse HEAD)
64 sub3sha1=$(cd super/sub3 && git rev-parse HEAD)
66 pwd=$(pwd)
68 cat > expect <<EOF
69 Entering 'sub1'
70 $pwd/clone-foo1-sub1-$sub1sha1
71 Entering 'sub3'
72 $pwd/clone-foo3-sub3-$sub3sha1
73 EOF
75 test_expect_success 'test basic "submodule foreach" usage' '
76 git clone super clone &&
78 cd 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"
83 ) &&
84 test_cmp expect actual
87 cat >expect <<EOF
88 Entering '../sub1'
89 $pwd/clone-foo1-sub1-../sub1-$sub1sha1
90 Entering '../sub3'
91 $pwd/clone-foo3-sub3-../sub3-$sub3sha1
92 EOF
94 test_expect_success 'test "submodule foreach" from subdirectory' '
95 mkdir clone/sub &&
97 cd clone/sub &&
98 git submodule foreach "echo \$toplevel-\$name-\$sm_path-\$displaypath-\$sha1" >../../actual
99 ) &&
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 &&
108 cd nested3 &&
109 git submodule add ../submodule submodule &&
110 test_tick &&
111 git commit -m "submodule" &&
112 git submodule init submodule
113 ) &&
115 cd nested2 &&
116 git submodule add ../nested3 nested3 &&
117 test_tick &&
118 git commit -m "nested3" &&
119 git submodule init nested3
120 ) &&
122 cd nested1 &&
123 git submodule add ../nested2 nested2 &&
124 test_tick &&
125 git commit -m "nested2" &&
126 git submodule init nested2
127 ) &&
129 cd super &&
130 git submodule add ../nested1 nested1 &&
131 test_tick &&
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 &&
140 cd 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 'use "foreach --recursive" to checkout all submodules' '
159 cd clone2 &&
160 git submodule foreach --recursive "git submodule update --init" &&
161 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
162 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
166 cat > expect <<EOF
167 Entering 'nested1'
168 Entering 'nested1/nested2'
169 Entering 'nested1/nested2/nested3'
170 Entering 'nested1/nested2/nested3/submodule'
171 Entering 'sub1'
172 Entering 'sub2'
173 Entering 'sub3'
176 test_expect_success 'test messages from "foreach --recursive"' '
178 cd clone2 &&
179 git submodule foreach --recursive "true" > ../actual
180 ) &&
181 test_cmp expect actual
184 cat > expect <<EOF
185 Entering '../nested1'
186 Entering '../nested1/nested2'
187 Entering '../nested1/nested2/nested3'
188 Entering '../nested1/nested2/nested3/submodule'
189 Entering '../sub1'
190 Entering '../sub2'
191 Entering '../sub3'
194 test_expect_success 'test messages from "foreach --recursive" from subdirectory' '
196 cd clone2 &&
197 mkdir untracked &&
198 cd untracked &&
199 git submodule foreach --recursive >../../actual
200 ) &&
201 test_cmp expect actual
203 sub1sha1=$(cd clone2/sub1 && git rev-parse HEAD)
204 sub2sha1=$(cd clone2/sub2 && git rev-parse HEAD)
205 sub3sha1=$(cd clone2/sub3 && git rev-parse HEAD)
206 nested1sha1=$(cd clone2/nested1 && git rev-parse HEAD)
207 nested2sha1=$(cd clone2/nested1/nested2 && git rev-parse HEAD)
208 nested3sha1=$(cd clone2/nested1/nested2/nested3 && git rev-parse HEAD)
209 submodulesha1=$(cd clone2/nested1/nested2/nested3/submodule && git rev-parse HEAD)
211 cat >expect <<EOF
212 Entering '../nested1'
213 toplevel: $pwd/clone2 name: nested1 path: nested1 displaypath: ../nested1 hash: $nested1sha1
214 Entering '../nested1/nested2'
215 toplevel: $pwd/clone2/nested1 name: nested2 path: nested2 displaypath: ../nested1/nested2 hash: $nested2sha1
216 Entering '../nested1/nested2/nested3'
217 toplevel: $pwd/clone2/nested1/nested2 name: nested3 path: nested3 displaypath: ../nested1/nested2/nested3 hash: $nested3sha1
218 Entering '../nested1/nested2/nested3/submodule'
219 toplevel: $pwd/clone2/nested1/nested2/nested3 name: submodule path: submodule displaypath: ../nested1/nested2/nested3/submodule hash: $submodulesha1
220 Entering '../sub1'
221 toplevel: $pwd/clone2 name: foo1 path: sub1 displaypath: ../sub1 hash: $sub1sha1
222 Entering '../sub2'
223 toplevel: $pwd/clone2 name: foo2 path: sub2 displaypath: ../sub2 hash: $sub2sha1
224 Entering '../sub3'
225 toplevel: $pwd/clone2 name: foo3 path: sub3 displaypath: ../sub3 hash: $sub3sha1
228 test_expect_success 'test "submodule foreach --recursive" from subdirectory' '
230 cd clone2/untracked &&
231 git submodule foreach --recursive "echo toplevel: \$toplevel name: \$name path: \$sm_path displaypath: \$displaypath hash: \$sha1" >../../actual
232 ) &&
233 test_cmp expect actual
236 cat > expect <<EOF
237 nested1-nested1
238 nested2-nested2
239 nested3-nested3
240 submodule-submodule
241 foo1-sub1
242 foo2-sub2
243 foo3-sub3
246 test_expect_success 'test "foreach --quiet --recursive"' '
248 cd clone2 &&
249 git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
250 ) &&
251 test_cmp expect actual
254 test_expect_success 'use "update --recursive" to checkout all submodules' '
255 git clone super clone3 &&
257 cd clone3 &&
258 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
259 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
260 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
261 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
262 git submodule update --init --recursive &&
263 git rev-parse --resolve-git-dir sub1/.git &&
264 git rev-parse --resolve-git-dir sub2/.git &&
265 git rev-parse --resolve-git-dir sub3/.git &&
266 git rev-parse --resolve-git-dir nested1/.git &&
267 git rev-parse --resolve-git-dir nested1/nested2/.git &&
268 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
269 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
273 nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD)
274 nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
275 nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
276 submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
277 sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
278 sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
279 sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
280 sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
281 sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD)
283 cat > expect <<EOF
284 $nested1sha1 nested1 (heads/main)
285 $nested2sha1 nested1/nested2 (heads/main)
286 $nested3sha1 nested1/nested2/nested3 (heads/main)
287 $submodulesha1 nested1/nested2/nested3/submodule (heads/main)
288 $sub1sha1 sub1 ($sub1sha1_short)
289 $sub2sha1 sub2 ($sub2sha1_short)
290 $sub3sha1 sub3 (heads/main)
293 test_expect_success 'test "status --recursive"' '
295 cd clone3 &&
296 git submodule status --recursive > ../actual
297 ) &&
298 test_cmp expect actual
301 cat > expect <<EOF
302 $nested1sha1 nested1 (heads/main)
303 +$nested2sha1 nested1/nested2 (file2~1)
304 $nested3sha1 nested1/nested2/nested3 (heads/main)
305 $submodulesha1 nested1/nested2/nested3/submodule (heads/main)
308 test_expect_success 'ensure "status --cached --recursive" preserves the --cached flag' '
310 cd clone3 &&
312 cd nested1/nested2 &&
313 test_commit file2
314 ) &&
315 git submodule status --cached --recursive -- nested1 > ../actual
316 ) &&
317 test_cmp expect actual
320 nested2sha1=$(git -C clone3/nested1/nested2 rev-parse HEAD)
322 cat > expect <<EOF
323 $nested1sha1 ../nested1 (heads/main)
324 +$nested2sha1 ../nested1/nested2 (file2)
325 $nested3sha1 ../nested1/nested2/nested3 (heads/main)
326 $submodulesha1 ../nested1/nested2/nested3/submodule (heads/main)
327 $sub1sha1 ../sub1 ($sub1sha1_short)
328 $sub2sha1 ../sub2 ($sub2sha1_short)
329 $sub3sha1 ../sub3 (heads/main)
332 test_expect_success 'test "status --recursive" from sub directory' '
334 cd clone3 &&
335 mkdir tmp && cd tmp &&
336 git submodule status --recursive > ../../actual
337 ) &&
338 test_cmp expect actual
341 test_expect_success 'use "git clone --recursive" to checkout all submodules' '
342 git clone --recursive super clone4 &&
344 cd clone4 &&
345 git rev-parse --resolve-git-dir .git &&
346 git rev-parse --resolve-git-dir sub1/.git &&
347 git rev-parse --resolve-git-dir sub2/.git &&
348 git rev-parse --resolve-git-dir sub3/.git &&
349 git rev-parse --resolve-git-dir nested1/.git &&
350 git rev-parse --resolve-git-dir nested1/nested2/.git &&
351 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
352 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
356 test_expect_success 'test "update --recursive" with a flag with spaces' '
357 git clone super "common objects" &&
358 git clone super clone5 &&
360 cd clone5 &&
361 test_must_fail git rev-parse --resolve-git-dir d nested1/.git &&
362 git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
363 git rev-parse --resolve-git-dir nested1/.git &&
364 git rev-parse --resolve-git-dir nested1/nested2/.git &&
365 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
366 test -f .git/modules/nested1/objects/info/alternates &&
367 test -f .git/modules/nested1/modules/nested2/objects/info/alternates &&
368 test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates
372 test_expect_success 'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
373 git clone super clone6 &&
375 cd clone6 &&
376 test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
377 test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
378 test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
379 test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
380 git submodule update --init --recursive -- nested1 &&
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 git rev-parse --resolve-git-dir nested1/.git &&
385 git rev-parse --resolve-git-dir nested1/nested2/.git &&
386 git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
387 git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
391 test_expect_success 'command passed to foreach retains notion of stdin' '
393 cd super &&
394 git submodule foreach echo success >../expected &&
395 yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
396 ) &&
397 test_cmp expected actual
400 test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
402 cd clone2 &&
403 git submodule foreach --recursive echo success >../expected &&
404 yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
405 ) &&
406 test_cmp expected actual
409 test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
411 cd super &&
412 git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
413 git submodule foreach echo \"quoted\" > ../actual
414 ) &&
415 test_cmp expected actual
418 test_expect_success 'option-like arguments passed to foreach commands are not lost' '
420 cd super &&
421 git submodule foreach "echo be --quiet" > ../expected &&
422 git submodule foreach echo be --quiet > ../actual
423 ) &&
424 grep -sq -e "--quiet" expected &&
425 test_cmp expected actual
428 test_expect_success 'option-like arguments passed to foreach recurse correctly' '
429 git -C clone2 submodule foreach --recursive "echo be --an-option" >expect &&
430 git -C clone2 submodule foreach --recursive echo be --an-option >actual &&
431 grep -e "--an-option" expect &&
432 test_cmp expect actual
435 test_done