format-patch: return an allocated string from log_write_email_headers()
[git.git] / t / t7407-submodule-foreach.sh
blob8d7b234beb8b09b2f6dbe459aad56315612925b8
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 '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' '
164 cd clone2 &&
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
171 cat > expect <<EOF
172 Entering 'nested1'
173 Entering 'nested1/nested2'
174 Entering 'nested1/nested2/nested3'
175 Entering 'nested1/nested2/nested3/submodule'
176 Entering 'sub1'
177 Entering 'sub2'
178 Entering 'sub3'
181 test_expect_success 'test messages from "foreach --recursive"' '
183 cd clone2 &&
184 git submodule foreach --recursive "true" > ../actual
185 ) &&
186 test_cmp expect actual
189 cat > expect <<EOF
190 Entering '../nested1'
191 Entering '../nested1/nested2'
192 Entering '../nested1/nested2/nested3'
193 Entering '../nested1/nested2/nested3/submodule'
194 Entering '../sub1'
195 Entering '../sub2'
196 Entering '../sub3'
199 test_expect_success 'test messages from "foreach --recursive" from subdirectory' '
201 cd clone2 &&
202 mkdir untracked &&
203 cd untracked &&
204 git submodule foreach --recursive >../../actual
205 ) &&
206 test_cmp expect actual
208 sub1sha1=$(cd clone2/sub1 && git rev-parse HEAD)
209 sub2sha1=$(cd clone2/sub2 && git rev-parse HEAD)
210 sub3sha1=$(cd clone2/sub3 && git rev-parse HEAD)
211 nested1sha1=$(cd clone2/nested1 && git rev-parse HEAD)
212 nested2sha1=$(cd clone2/nested1/nested2 && git rev-parse HEAD)
213 nested3sha1=$(cd clone2/nested1/nested2/nested3 && git rev-parse HEAD)
214 submodulesha1=$(cd clone2/nested1/nested2/nested3/submodule && git rev-parse HEAD)
216 cat >expect <<EOF
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
225 Entering '../sub1'
226 toplevel: $pwd/clone2 name: foo1 path: sub1 displaypath: ../sub1 hash: $sub1sha1
227 Entering '../sub2'
228 toplevel: $pwd/clone2 name: foo2 path: sub2 displaypath: ../sub2 hash: $sub2sha1
229 Entering '../sub3'
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
237 ) &&
238 test_cmp expect actual
241 cat > expect <<EOF
242 nested1-nested1
243 nested2-nested2
244 nested3-nested3
245 submodule-submodule
246 foo1-sub1
247 foo2-sub2
248 foo3-sub3
251 test_expect_success 'test "foreach --quiet --recursive"' '
253 cd clone2 &&
254 git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
255 ) &&
256 test_cmp expect actual
259 test_expect_success 'use "update --recursive" to checkout all submodules' '
260 git clone super clone3 &&
262 cd 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 clone3/nested1 && git rev-parse HEAD)
279 nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
280 nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
281 submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
282 sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
283 sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
284 sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
285 sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
286 sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD)
288 cat > expect <<EOF
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"' '
300 cd clone3 &&
301 git submodule status --recursive > ../actual
302 ) &&
303 test_cmp expect actual
306 cat > expect <<EOF
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' '
315 cd clone3 &&
317 cd nested1/nested2 &&
318 test_commit file2
319 ) &&
320 git submodule status --cached --recursive -- nested1 > ../actual
321 ) &&
322 test_cmp expect actual
325 nested2sha1=$(git -C clone3/nested1/nested2 rev-parse HEAD)
327 cat > expect <<EOF
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' '
339 cd clone3 &&
340 mkdir tmp && cd tmp &&
341 git submodule status --recursive > ../../actual
342 ) &&
343 test_cmp expect actual
346 test_expect_success 'use "git clone --recursive" to checkout all submodules' '
347 git clone --recursive super clone4 &&
349 cd 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 &&
365 cd 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 &&
380 cd 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' '
398 cd super &&
399 git submodule foreach echo success >../expected &&
400 yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
401 ) &&
402 test_cmp expected actual
405 test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
407 cd clone2 &&
408 git submodule foreach --recursive echo success >../expected &&
409 yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
410 ) &&
411 test_cmp expected actual
414 test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
416 cd super &&
417 git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
418 git submodule foreach echo \"quoted\" > ../actual
419 ) &&
420 test_cmp expected actual
423 test_expect_success 'option-like arguments passed to foreach commands are not lost' '
425 cd super &&
426 git submodule foreach "echo be --quiet" > ../expected &&
427 git submodule foreach echo be --quiet > ../actual
428 ) &&
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
440 test_done