]>
Commit | Line | Data |
---|---|---|
d69ecf6f JH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2009 Johan Herland | |
4 | # | |
5 | ||
6 | test_description='Test "git submodule foreach" | |
7 | ||
8 | This test verifies that "git submodule foreach" correctly visits all submodules | |
9 | that are currently checked out. | |
10 | ' | |
11 | ||
334afbc7 JS |
12 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master |
13 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME | |
14 | ||
d69ecf6f JH |
15 | . ./test-lib.sh |
16 | ||
17 | ||
18 | test_expect_success 'setup a submodule tree' ' | |
19 | echo file > file && | |
20 | git add file && | |
21 | test_tick && | |
4bf9dd97 | 22 | git commit -m upstream && |
d69ecf6f JH |
23 | git clone . super && |
24 | git clone super submodule && | |
25 | ( | |
26 | cd super && | |
27 | git submodule add ../submodule sub1 && | |
28 | git submodule add ../submodule sub2 && | |
29 | git submodule add ../submodule sub3 && | |
30 | git config -f .gitmodules --rename-section \ | |
31 | submodule.sub1 submodule.foo1 && | |
32 | git config -f .gitmodules --rename-section \ | |
33 | submodule.sub2 submodule.foo2 && | |
34 | git config -f .gitmodules --rename-section \ | |
35 | submodule.sub3 submodule.foo3 && | |
4bf9dd97 | 36 | git add .gitmodules && |
d69ecf6f JH |
37 | test_tick && |
38 | git commit -m "submodules" && | |
39 | git submodule init sub1 && | |
40 | git submodule init sub2 && | |
41 | git submodule init sub3 | |
42 | ) && | |
43 | ( | |
44 | cd submodule && | |
45 | echo different > file && | |
46 | git add file && | |
47 | test_tick && | |
48 | git commit -m "different" | |
49 | ) && | |
50 | ( | |
51 | cd super && | |
52 | ( | |
53 | cd sub3 && | |
54 | git pull | |
55 | ) && | |
56 | git add sub3 && | |
57 | test_tick && | |
58 | git commit -m "update sub3" | |
59 | ) | |
60 | ' | |
61 | ||
62 | sub1sha1=$(cd super/sub1 && git rev-parse HEAD) | |
63 | sub3sha1=$(cd super/sub3 && git rev-parse HEAD) | |
64 | ||
f030c96d ÆAB |
65 | pwd=$(pwd) |
66 | ||
d69ecf6f JH |
67 | cat > expect <<EOF |
68 | Entering 'sub1' | |
f030c96d | 69 | $pwd/clone-foo1-sub1-$sub1sha1 |
d69ecf6f | 70 | Entering 'sub3' |
f030c96d | 71 | $pwd/clone-foo3-sub3-$sub3sha1 |
d69ecf6f JH |
72 | EOF |
73 | ||
74 | test_expect_success 'test basic "submodule foreach" usage' ' | |
75 | git clone super clone && | |
76 | ( | |
77 | cd clone && | |
78 | git submodule update --init -- sub1 sub3 && | |
f030c96d ÆAB |
79 | git submodule foreach "echo \$toplevel-\$name-\$path-\$sha1" > ../actual && |
80 | git config foo.bar zar && | |
81 | git submodule foreach "git config --file \"\$toplevel/.git/config\" foo.bar" | |
d69ecf6f | 82 | ) && |
490b6d57 | 83 | test_i18ncmp expect actual |
d69ecf6f JH |
84 | ' |
85 | ||
091a6eb0 JK |
86 | cat >expect <<EOF |
87 | Entering '../sub1' | |
b6f7ac8f | 88 | $pwd/clone-foo1-sub1-../sub1-$sub1sha1 |
091a6eb0 | 89 | Entering '../sub3' |
b6f7ac8f | 90 | $pwd/clone-foo3-sub3-../sub3-$sub3sha1 |
091a6eb0 JK |
91 | EOF |
92 | ||
93 | test_expect_success 'test "submodule foreach" from subdirectory' ' | |
94 | mkdir clone/sub && | |
95 | ( | |
96 | cd clone/sub && | |
b6f7ac8f | 97 | git submodule foreach "echo \$toplevel-\$name-\$sm_path-\$displaypath-\$sha1" >../../actual |
091a6eb0 JK |
98 | ) && |
99 | test_i18ncmp expect actual | |
100 | ' | |
101 | ||
15fc56a8 JH |
102 | test_expect_success 'setup nested submodules' ' |
103 | git clone submodule nested1 && | |
104 | git clone submodule nested2 && | |
105 | git clone submodule nested3 && | |
106 | ( | |
107 | cd nested3 && | |
108 | git submodule add ../submodule submodule && | |
109 | test_tick && | |
110 | git commit -m "submodule" && | |
111 | git submodule init submodule | |
112 | ) && | |
113 | ( | |
114 | cd nested2 && | |
115 | git submodule add ../nested3 nested3 && | |
116 | test_tick && | |
117 | git commit -m "nested3" && | |
118 | git submodule init nested3 | |
119 | ) && | |
120 | ( | |
121 | cd nested1 && | |
122 | git submodule add ../nested2 nested2 && | |
123 | test_tick && | |
124 | git commit -m "nested2" && | |
125 | git submodule init nested2 | |
126 | ) && | |
127 | ( | |
128 | cd super && | |
129 | git submodule add ../nested1 nested1 && | |
130 | test_tick && | |
131 | git commit -m "nested1" && | |
132 | git submodule init nested1 | |
133 | ) | |
134 | ' | |
135 | ||
136 | test_expect_success 'use "submodule foreach" to checkout 2nd level submodule' ' | |
137 | git clone super clone2 && | |
138 | ( | |
139 | cd clone2 && | |
abc06822 FG |
140 | test_must_fail git rev-parse --resolve-git-dir sub1/.git && |
141 | test_must_fail git rev-parse --resolve-git-dir sub2/.git && | |
142 | test_must_fail git rev-parse --resolve-git-dir sub3/.git && | |
143 | test_must_fail git rev-parse --resolve-git-dir nested1/.git && | |
15fc56a8 | 144 | git submodule update --init && |
abc06822 FG |
145 | git rev-parse --resolve-git-dir sub1/.git && |
146 | git rev-parse --resolve-git-dir sub2/.git && | |
147 | git rev-parse --resolve-git-dir sub3/.git && | |
148 | git rev-parse --resolve-git-dir nested1/.git && | |
149 | test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git && | |
15fc56a8 | 150 | git submodule foreach "git submodule update --init" && |
dfe338ae | 151 | git rev-parse --resolve-git-dir nested1/nested2/.git && |
abc06822 | 152 | test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git |
15fc56a8 JH |
153 | ) |
154 | ' | |
155 | ||
156 | test_expect_success 'use "foreach --recursive" to checkout all submodules' ' | |
157 | ( | |
158 | cd clone2 && | |
159 | git submodule foreach --recursive "git submodule update --init" && | |
abc06822 FG |
160 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && |
161 | git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git | |
15fc56a8 JH |
162 | ) |
163 | ' | |
164 | ||
165 | cat > expect <<EOF | |
166 | Entering 'nested1' | |
167 | Entering 'nested1/nested2' | |
168 | Entering 'nested1/nested2/nested3' | |
169 | Entering 'nested1/nested2/nested3/submodule' | |
170 | Entering 'sub1' | |
171 | Entering 'sub2' | |
172 | Entering 'sub3' | |
173 | EOF | |
174 | ||
175 | test_expect_success 'test messages from "foreach --recursive"' ' | |
176 | ( | |
177 | cd clone2 && | |
178 | git submodule foreach --recursive "true" > ../actual | |
179 | ) && | |
490b6d57 | 180 | test_i18ncmp expect actual |
15fc56a8 JH |
181 | ' |
182 | ||
ea2fa104 SB |
183 | cat > expect <<EOF |
184 | Entering '../nested1' | |
185 | Entering '../nested1/nested2' | |
186 | Entering '../nested1/nested2/nested3' | |
187 | Entering '../nested1/nested2/nested3/submodule' | |
188 | Entering '../sub1' | |
189 | Entering '../sub2' | |
190 | Entering '../sub3' | |
191 | EOF | |
192 | ||
193 | test_expect_success 'test messages from "foreach --recursive" from subdirectory' ' | |
194 | ( | |
195 | cd clone2 && | |
196 | mkdir untracked && | |
197 | cd untracked && | |
198 | git submodule foreach --recursive >../../actual | |
199 | ) && | |
200 | test_i18ncmp expect actual | |
201 | ' | |
c033a2f6 PC |
202 | sub1sha1=$(cd clone2/sub1 && git rev-parse HEAD) |
203 | sub2sha1=$(cd clone2/sub2 && git rev-parse HEAD) | |
204 | sub3sha1=$(cd clone2/sub3 && git rev-parse HEAD) | |
205 | nested1sha1=$(cd clone2/nested1 && git rev-parse HEAD) | |
206 | nested2sha1=$(cd clone2/nested1/nested2 && git rev-parse HEAD) | |
207 | nested3sha1=$(cd clone2/nested1/nested2/nested3 && git rev-parse HEAD) | |
208 | submodulesha1=$(cd clone2/nested1/nested2/nested3/submodule && git rev-parse HEAD) | |
209 | ||
210 | cat >expect <<EOF | |
211 | Entering '../nested1' | |
b6f7ac8f | 212 | toplevel: $pwd/clone2 name: nested1 path: nested1 displaypath: ../nested1 hash: $nested1sha1 |
c033a2f6 | 213 | Entering '../nested1/nested2' |
b6f7ac8f | 214 | toplevel: $pwd/clone2/nested1 name: nested2 path: nested2 displaypath: ../nested1/nested2 hash: $nested2sha1 |
c033a2f6 | 215 | Entering '../nested1/nested2/nested3' |
b6f7ac8f | 216 | toplevel: $pwd/clone2/nested1/nested2 name: nested3 path: nested3 displaypath: ../nested1/nested2/nested3 hash: $nested3sha1 |
c033a2f6 | 217 | Entering '../nested1/nested2/nested3/submodule' |
b6f7ac8f | 218 | toplevel: $pwd/clone2/nested1/nested2/nested3 name: submodule path: submodule displaypath: ../nested1/nested2/nested3/submodule hash: $submodulesha1 |
c033a2f6 | 219 | Entering '../sub1' |
b6f7ac8f | 220 | toplevel: $pwd/clone2 name: foo1 path: sub1 displaypath: ../sub1 hash: $sub1sha1 |
c033a2f6 | 221 | Entering '../sub2' |
b6f7ac8f | 222 | toplevel: $pwd/clone2 name: foo2 path: sub2 displaypath: ../sub2 hash: $sub2sha1 |
c033a2f6 | 223 | Entering '../sub3' |
b6f7ac8f | 224 | toplevel: $pwd/clone2 name: foo3 path: sub3 displaypath: ../sub3 hash: $sub3sha1 |
c033a2f6 PC |
225 | EOF |
226 | ||
227 | test_expect_success 'test "submodule foreach --recursive" from subdirectory' ' | |
228 | ( | |
229 | cd clone2/untracked && | |
b6f7ac8f | 230 | git submodule foreach --recursive "echo toplevel: \$toplevel name: \$name path: \$sm_path displaypath: \$displaypath hash: \$sha1" >../../actual |
c033a2f6 PC |
231 | ) && |
232 | test_i18ncmp expect actual | |
233 | ' | |
ea2fa104 | 234 | |
15fc56a8 JH |
235 | cat > expect <<EOF |
236 | nested1-nested1 | |
237 | nested2-nested2 | |
238 | nested3-nested3 | |
239 | submodule-submodule | |
240 | foo1-sub1 | |
241 | foo2-sub2 | |
242 | foo3-sub3 | |
243 | EOF | |
244 | ||
245 | test_expect_success 'test "foreach --quiet --recursive"' ' | |
246 | ( | |
247 | cd clone2 && | |
248 | git submodule foreach -q --recursive "echo \$name-\$path" > ../actual | |
249 | ) && | |
250 | test_cmp expect actual | |
251 | ' | |
252 | ||
b13fd5c1 JH |
253 | test_expect_success 'use "update --recursive" to checkout all submodules' ' |
254 | git clone super clone3 && | |
255 | ( | |
256 | cd clone3 && | |
abc06822 FG |
257 | test_must_fail git rev-parse --resolve-git-dir sub1/.git && |
258 | test_must_fail git rev-parse --resolve-git-dir sub2/.git && | |
259 | test_must_fail git rev-parse --resolve-git-dir sub3/.git && | |
260 | test_must_fail git rev-parse --resolve-git-dir nested1/.git && | |
b13fd5c1 | 261 | git submodule update --init --recursive && |
abc06822 FG |
262 | git rev-parse --resolve-git-dir sub1/.git && |
263 | git rev-parse --resolve-git-dir sub2/.git && | |
264 | git rev-parse --resolve-git-dir sub3/.git && | |
265 | git rev-parse --resolve-git-dir nested1/.git && | |
266 | git rev-parse --resolve-git-dir nested1/nested2/.git && | |
267 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && | |
268 | git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git | |
b13fd5c1 JH |
269 | ) |
270 | ' | |
271 | ||
64b19ffe JH |
272 | nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD) |
273 | nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD) | |
274 | nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD) | |
275 | submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD) | |
276 | sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD) | |
277 | sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD) | |
278 | sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD) | |
e3ae4a86 JH |
279 | sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD) |
280 | sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD) | |
64b19ffe JH |
281 | |
282 | cat > expect <<EOF | |
283 | $nested1sha1 nested1 (heads/master) | |
284 | $nested2sha1 nested1/nested2 (heads/master) | |
285 | $nested3sha1 nested1/nested2/nested3 (heads/master) | |
286 | $submodulesha1 nested1/nested2/nested3/submodule (heads/master) | |
e3ae4a86 JH |
287 | $sub1sha1 sub1 ($sub1sha1_short) |
288 | $sub2sha1 sub2 ($sub2sha1_short) | |
64b19ffe JH |
289 | $sub3sha1 sub3 (heads/master) |
290 | EOF | |
291 | ||
292 | test_expect_success 'test "status --recursive"' ' | |
293 | ( | |
294 | cd clone3 && | |
295 | git submodule status --recursive > ../actual | |
296 | ) && | |
297 | test_cmp expect actual | |
298 | ' | |
299 | ||
2ab56603 SB |
300 | cat > expect <<EOF |
301 | $nested1sha1 nested1 (heads/master) | |
302 | +$nested2sha1 nested1/nested2 (file2~1) | |
303 | $nested3sha1 nested1/nested2/nested3 (heads/master) | |
304 | $submodulesha1 nested1/nested2/nested3/submodule (heads/master) | |
305 | EOF | |
98dbe63d KB |
306 | |
307 | test_expect_success 'ensure "status --cached --recursive" preserves the --cached flag' ' | |
308 | ( | |
309 | cd clone3 && | |
310 | ( | |
58ca9ad4 | 311 | cd nested1/nested2 && |
98dbe63d KB |
312 | test_commit file2 |
313 | ) && | |
314 | git submodule status --cached --recursive -- nested1 > ../actual | |
315 | ) && | |
316 | test_cmp expect actual | |
317 | ' | |
318 | ||
10450cf7 SB |
319 | nested2sha1=$(git -C clone3/nested1/nested2 rev-parse HEAD) |
320 | ||
321 | cat > expect <<EOF | |
322 | $nested1sha1 ../nested1 (heads/master) | |
323 | +$nested2sha1 ../nested1/nested2 (file2) | |
324 | $nested3sha1 ../nested1/nested2/nested3 (heads/master) | |
325 | $submodulesha1 ../nested1/nested2/nested3/submodule (heads/master) | |
326 | $sub1sha1 ../sub1 ($sub1sha1_short) | |
327 | $sub2sha1 ../sub2 ($sub2sha1_short) | |
328 | $sub3sha1 ../sub3 (heads/master) | |
329 | EOF | |
330 | ||
331 | test_expect_success 'test "status --recursive" from sub directory' ' | |
332 | ( | |
333 | cd clone3 && | |
334 | mkdir tmp && cd tmp && | |
335 | git submodule status --recursive > ../../actual | |
336 | ) && | |
337 | test_cmp expect actual | |
338 | ' | |
339 | ||
e7fed18a JH |
340 | test_expect_success 'use "git clone --recursive" to checkout all submodules' ' |
341 | git clone --recursive super clone4 && | |
abc06822 FG |
342 | ( |
343 | cd clone4 && | |
344 | git rev-parse --resolve-git-dir .git && | |
345 | git rev-parse --resolve-git-dir sub1/.git && | |
346 | git rev-parse --resolve-git-dir sub2/.git && | |
347 | git rev-parse --resolve-git-dir sub3/.git && | |
348 | git rev-parse --resolve-git-dir nested1/.git && | |
349 | git rev-parse --resolve-git-dir nested1/nested2/.git && | |
350 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && | |
351 | git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git | |
352 | ) | |
e7fed18a JH |
353 | ' |
354 | ||
a7eff1a8 KB |
355 | test_expect_success 'test "update --recursive" with a flag with spaces' ' |
356 | git clone super "common objects" && | |
357 | git clone super clone5 && | |
358 | ( | |
359 | cd clone5 && | |
abc06822 | 360 | test_must_fail git rev-parse --resolve-git-dir d nested1/.git && |
a7eff1a8 | 361 | git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" && |
abc06822 FG |
362 | git rev-parse --resolve-git-dir nested1/.git && |
363 | git rev-parse --resolve-git-dir nested1/nested2/.git && | |
364 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && | |
365 | test -f .git/modules/nested1/objects/info/alternates && | |
366 | test -f .git/modules/nested1/modules/nested2/objects/info/alternates && | |
367 | test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates | |
a7eff1a8 KB |
368 | ) |
369 | ' | |
370 | ||
98dbe63d KB |
371 | test_expect_success 'use "update --recursive nested1" to checkout all submodules rooted in nested1' ' |
372 | git clone super clone6 && | |
373 | ( | |
374 | cd clone6 && | |
abc06822 FG |
375 | test_must_fail git rev-parse --resolve-git-dir sub1/.git && |
376 | test_must_fail git rev-parse --resolve-git-dir sub2/.git && | |
377 | test_must_fail git rev-parse --resolve-git-dir sub3/.git && | |
378 | test_must_fail git rev-parse --resolve-git-dir nested1/.git && | |
98dbe63d | 379 | git submodule update --init --recursive -- nested1 && |
abc06822 FG |
380 | test_must_fail git rev-parse --resolve-git-dir sub1/.git && |
381 | test_must_fail git rev-parse --resolve-git-dir sub2/.git && | |
382 | test_must_fail git rev-parse --resolve-git-dir sub3/.git && | |
383 | git rev-parse --resolve-git-dir nested1/.git && | |
384 | git rev-parse --resolve-git-dir nested1/nested2/.git && | |
385 | git rev-parse --resolve-git-dir nested1/nested2/nested3/.git && | |
386 | git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git | |
98dbe63d KB |
387 | ) |
388 | ' | |
389 | ||
4dca1aa6 | 390 | test_expect_success 'command passed to foreach retains notion of stdin' ' |
91cd7e4b BC |
391 | ( |
392 | cd super && | |
393 | git submodule foreach echo success >../expected && | |
394 | yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual | |
395 | ) && | |
396 | test_cmp expected actual | |
397 | ' | |
398 | ||
4dca1aa6 | 399 | test_expect_success 'command passed to foreach --recursive retains notion of stdin' ' |
91cd7e4b BC |
400 | ( |
401 | cd clone2 && | |
402 | git submodule foreach --recursive echo success >../expected && | |
403 | yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual | |
404 | ) && | |
405 | test_cmp expected actual | |
406 | ' | |
407 | ||
1c4fb136 AK |
408 | test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' ' |
409 | ( | |
410 | cd super && | |
411 | git submodule foreach "echo \\\"quoted\\\"" > ../expected && | |
412 | git submodule foreach echo \"quoted\" > ../actual | |
413 | ) && | |
414 | test_cmp expected actual | |
415 | ' | |
416 | ||
a282f5a9 NTND |
417 | test_expect_success 'option-like arguments passed to foreach commands are not lost' ' |
418 | ( | |
419 | cd super && | |
420 | git submodule foreach "echo be --quiet" > ../expected && | |
421 | git submodule foreach echo be --quiet > ../actual | |
422 | ) && | |
423 | grep -sq -e "--quiet" expected && | |
424 | test_cmp expected actual | |
425 | ' | |
426 | ||
30db18b1 MS |
427 | test_expect_success 'option-like arguments passed to foreach recurse correctly' ' |
428 | git -C clone2 submodule foreach --recursive "echo be --an-option" >expect && | |
429 | git -C clone2 submodule foreach --recursive echo be --an-option >actual && | |
430 | grep -e "--an-option" expect && | |
431 | test_cmp expect actual | |
432 | ' | |
433 | ||
d69ecf6f | 434 | test_done |