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