summaryrefslogtreecommitdiffstats
path: root/t/t7407-submodule-foreach.sh
blob: 6b2aa917e11871eb403c8c79725b1e1944f8d29f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/bin/sh
#
# Copyright (c) 2009 Johan Herland
#

test_description='Test "git submodule foreach"

This test verifies that "git submodule foreach" correctly visits all submodules
that are currently checked out.
'

. ./test-lib.sh


test_expect_success 'setup a submodule tree' '
	echo file > file &&
	git add file &&
	test_tick &&
	git commit -m upstream &&
	git clone . super &&
	git clone super submodule &&
	(
		cd super &&
		git submodule add ../submodule sub1 &&
		git submodule add ../submodule sub2 &&
		git submodule add ../submodule sub3 &&
		git config -f .gitmodules --rename-section \
			submodule.sub1 submodule.foo1 &&
		git config -f .gitmodules --rename-section \
			submodule.sub2 submodule.foo2 &&
		git config -f .gitmodules --rename-section \
			submodule.sub3 submodule.foo3 &&
		git add .gitmodules &&
		test_tick &&
		git commit -m "submodules" &&
		git submodule init sub1 &&
		git submodule init sub2 &&
		git submodule init sub3
	) &&
	(
		cd submodule &&
		echo different > file &&
		git add file &&
		test_tick &&
		git commit -m "different"
	) &&
	(
		cd super &&
		(
			cd sub3 &&
			git pull
		) &&
		git add sub3 &&
		test_tick &&
		git commit -m "update sub3"
	)
'

sub1sha1=$(cd super/sub1 && git rev-parse HEAD)
sub3sha1=$(cd super/sub3 && git rev-parse HEAD)

pwd=$(pwd)

cat > expect <<EOF
Entering 'sub1'
$pwd/clone-foo1-sub1-$sub1sha1
Entering 'sub3'
$pwd/clone-foo3-sub3-$sub3sha1
EOF

test_expect_success 'test basic "submodule foreach" usage' '
	git clone super clone &&
	(
		cd clone &&
		git submodule update --init -- sub1 sub3 &&
		git submodule foreach "echo \$toplevel-\$name-\$path-\$sha1" > ../actual &&
		git config foo.bar zar &&
		git submodule foreach "git config --file \"\$toplevel/.git/config\" foo.bar"
	) &&
	test_i18ncmp expect actual
'

cat >expect <<EOF
Entering '../sub1'
$pwd/clone-foo1-sub1-../sub1-$sub1sha1
Entering '../sub3'
$pwd/clone-foo3-sub3-../sub3-$sub3sha1
EOF

test_expect_success 'test "submodule foreach" from subdirectory' '
	mkdir clone/sub &&
	(
		cd clone/sub &&
		git submodule foreach "echo \$toplevel-\$name-\$sm_path-\$displaypath-\$sha1" >../../actual
	) &&
	test_i18ncmp expect actual
'

test_expect_success 'setup nested submodules' '
	git clone submodule nested1 &&
	git clone submodule nested2 &&
	git clone submodule nested3 &&
	(
		cd nested3 &&
		git submodule add ../submodule submodule &&
		test_tick &&
		git commit -m "submodule" &&
		git submodule init submodule
	) &&
	(
		cd nested2 &&
		git submodule add ../nested3 nested3 &&
		test_tick &&
		git commit -m "nested3" &&
		git submodule init nested3
	) &&
	(
		cd nested1 &&
		git submodule add ../nested2 nested2 &&
		test_tick &&
		git commit -m "nested2" &&
		git submodule init nested2
	) &&
	(
		cd super &&
		git submodule add ../nested1 nested1 &&
		test_tick &&
		git commit -m "nested1" &&
		git submodule init nested1
	)
'

test_expect_success 'use "submodule foreach" to checkout 2nd level submodule' '
	git clone super clone2 &&
	(
		cd clone2 &&
		test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
		test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
		test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
		test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
		git submodule update --init &&
		git rev-parse --resolve-git-dir sub1/.git &&
		git rev-parse --resolve-git-dir sub2/.git &&
		git rev-parse --resolve-git-dir sub3/.git &&
		git rev-parse --resolve-git-dir nested1/.git &&
		test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git &&
		git submodule foreach "git submodule update --init" &&
		git rev-parse --resolve-git-dir nested1/nested2/.git &&
		test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git
	)
'

test_expect_success 'use "foreach --recursive" to checkout all submodules' '
	(
		cd clone2 &&
		git submodule foreach --recursive "git submodule update --init" &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
	)
'

cat > expect <<EOF
Entering 'nested1'
Entering 'nested1/nested2'
Entering 'nested1/nested2/nested3'
Entering 'nested1/nested2/nested3/submodule'
Entering 'sub1'
Entering 'sub2'
Entering 'sub3'
EOF

test_expect_success 'test messages from "foreach --recursive"' '
	(
		cd clone2 &&
		git submodule foreach --recursive "true" > ../actual
	) &&
	test_i18ncmp expect actual
'

cat > expect <<EOF
Entering '../nested1'
Entering '../nested1/nested2'
Entering '../nested1/nested2/nested3'
Entering '../nested1/nested2/nested3/submodule'
Entering '../sub1'
Entering '../sub2'
Entering '../sub3'
EOF

test_expect_success 'test messages from "foreach --recursive" from subdirectory' '
	(
		cd clone2 &&
		mkdir untracked &&
		cd untracked &&
		git submodule foreach --recursive >../../actual
	) &&
	test_i18ncmp expect actual
'
sub1sha1=$(cd clone2/sub1 && git rev-parse HEAD)
sub2sha1=$(cd clone2/sub2 && git rev-parse HEAD)
sub3sha1=$(cd clone2/sub3 && git rev-parse HEAD)
nested1sha1=$(cd clone2/nested1 && git rev-parse HEAD)
nested2sha1=$(cd clone2/nested1/nested2 && git rev-parse HEAD)
nested3sha1=$(cd clone2/nested1/nested2/nested3 && git rev-parse HEAD)
submodulesha1=$(cd clone2/nested1/nested2/nested3/submodule && git rev-parse HEAD)

cat >expect <<EOF
Entering '../nested1'
toplevel: $pwd/clone2 name: nested1 path: nested1 displaypath: ../nested1 hash: $nested1sha1
Entering '../nested1/nested2'
toplevel: $pwd/clone2/nested1 name: nested2 path: nested2 displaypath: ../nested1/nested2 hash: $nested2sha1
Entering '../nested1/nested2/nested3'
toplevel: $pwd/clone2/nested1/nested2 name: nested3 path: nested3 displaypath: ../nested1/nested2/nested3 hash: $nested3sha1
Entering '../nested1/nested2/nested3/submodule'
toplevel: $pwd/clone2/nested1/nested2/nested3 name: submodule path: submodule displaypath: ../nested1/nested2/nested3/submodule hash: $submodulesha1
Entering '../sub1'
toplevel: $pwd/clone2 name: foo1 path: sub1 displaypath: ../sub1 hash: $sub1sha1
Entering '../sub2'
toplevel: $pwd/clone2 name: foo2 path: sub2 displaypath: ../sub2 hash: $sub2sha1
Entering '../sub3'
toplevel: $pwd/clone2 name: foo3 path: sub3 displaypath: ../sub3 hash: $sub3sha1
EOF

test_expect_success 'test "submodule foreach --recursive" from subdirectory' '
	(
		cd clone2/untracked &&
		git submodule foreach --recursive "echo toplevel: \$toplevel name: \$name path: \$sm_path displaypath: \$displaypath hash: \$sha1" >../../actual
	) &&
	test_i18ncmp expect actual
'

cat > expect <<EOF
nested1-nested1
nested2-nested2
nested3-nested3
submodule-submodule
foo1-sub1
foo2-sub2
foo3-sub3
EOF

test_expect_success 'test "foreach --quiet --recursive"' '
	(
		cd clone2 &&
		git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
	) &&
	test_cmp expect actual
'

test_expect_success 'use "update --recursive" to checkout all submodules' '
	git clone super clone3 &&
	(
		cd clone3 &&
		test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
		test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
		test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
		test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
		git submodule update --init --recursive &&
		git rev-parse --resolve-git-dir sub1/.git &&
		git rev-parse --resolve-git-dir sub2/.git &&
		git rev-parse --resolve-git-dir sub3/.git &&
		git rev-parse --resolve-git-dir nested1/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
	)
'

nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD)
nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD)

cat > expect <<EOF
 $nested1sha1 nested1 (heads/master)
 $nested2sha1 nested1/nested2 (heads/master)
 $nested3sha1 nested1/nested2/nested3 (heads/master)
 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
 $sub1sha1 sub1 ($sub1sha1_short)
 $sub2sha1 sub2 ($sub2sha1_short)
 $sub3sha1 sub3 (heads/master)
EOF

test_expect_success 'test "status --recursive"' '
	(
		cd clone3 &&
		git submodule status --recursive > ../actual
	) &&
	test_cmp expect actual
'

cat > expect <<EOF
 $nested1sha1 nested1 (heads/master)
+$nested2sha1 nested1/nested2 (file2~1)
 $nested3sha1 nested1/nested2/nested3 (heads/master)
 $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
EOF

test_expect_success 'ensure "status --cached --recursive" preserves the --cached flag' '
	(
		cd clone3 &&
		(
			cd nested1/nested2 &&
			test_commit file2
		) &&
		git submodule status --cached --recursive -- nested1 > ../actual
	) &&
	test_cmp expect actual
'

nested2sha1=$(git -C clone3/nested1/nested2 rev-parse HEAD)

cat > expect <<EOF
 $nested1sha1 ../nested1 (heads/master)
+$nested2sha1 ../nested1/nested2 (file2)
 $nested3sha1 ../nested1/nested2/nested3 (heads/master)
 $submodulesha1 ../nested1/nested2/nested3/submodule (heads/master)
 $sub1sha1 ../sub1 ($sub1sha1_short)
 $sub2sha1 ../sub2 ($sub2sha1_short)
 $sub3sha1 ../sub3 (heads/master)
EOF

test_expect_success 'test "status --recursive" from sub directory' '
	(
		cd clone3 &&
		mkdir tmp && cd tmp &&
		git submodule status --recursive > ../../actual
	) &&
	test_cmp expect actual
'

test_expect_success 'use "git clone --recursive" to checkout all submodules' '
	git clone --recursive super clone4 &&
	(
		cd clone4 &&
		git rev-parse --resolve-git-dir .git &&
		git rev-parse --resolve-git-dir sub1/.git &&
		git rev-parse --resolve-git-dir sub2/.git &&
		git rev-parse --resolve-git-dir sub3/.git &&
		git rev-parse --resolve-git-dir nested1/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
	)
'

test_expect_success 'test "update --recursive" with a flag with spaces' '
	git clone super "common objects" &&
	git clone super clone5 &&
	(
		cd clone5 &&
		test_must_fail git rev-parse --resolve-git-dir d nested1/.git &&
		git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
		git rev-parse --resolve-git-dir nested1/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
		test -f .git/modules/nested1/objects/info/alternates &&
		test -f .git/modules/nested1/modules/nested2/objects/info/alternates &&
		test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates
	)
'

test_expect_success 'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
	git clone super clone6 &&
	(
		cd clone6 &&
		test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
		test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
		test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
		test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
		git submodule update --init --recursive -- nested1 &&
		test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
		test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
		test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
		git rev-parse --resolve-git-dir nested1/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
		git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
	)
'

test_expect_success 'command passed to foreach retains notion of stdin' '
	(
		cd super &&
		git submodule foreach echo success >../expected &&
		yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
	) &&
	test_cmp expected actual
'

test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
	(
		cd clone2 &&
		git submodule foreach --recursive echo success >../expected &&
		yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
	) &&
	test_cmp expected actual
'

test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
	(
		cd super &&
		git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
		git submodule foreach echo \"quoted\" > ../actual
	) &&
	test_cmp expected actual
'

test_expect_success 'option-like arguments passed to foreach commands are not lost' '
	(
		cd super &&
		git submodule foreach "echo be --quiet" > ../expected &&
		git submodule foreach echo be --quiet > ../actual
	) &&
	grep -sq -e "--quiet" expected &&
	test_cmp expected actual
'

test_expect_success 'option-like arguments passed to foreach recurse correctly' '
	git -C clone2 submodule foreach --recursive "echo be --an-option" >expect &&
	git -C clone2 submodule foreach --recursive echo be --an-option >actual &&
	grep -e "--an-option" expect &&
	test_cmp expect actual
'

test_done