]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7406-submodule-update.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / t7406-submodule-update.sh
CommitLineData
ca2cedba
PH
1#!/bin/sh
2#
3# Copyright (c) 2009 Red Hat, Inc.
4#
5
6test_description='Test updating submodules
7
8This test verifies that "git submodule update" detaches the HEAD of the
42b49178 9submodule and "git submodule update --rebase/--merge" does not detach the HEAD.
ca2cedba
PH
10'
11
01dc8133 12GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
13export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14
ca2cedba
PH
15. ./test-lib.sh
16
17
18compare_head()
19{
01dc8133 20 sha_main=$(git rev-list --max-count=1 main)
848351b2 21 sha_head=$(git rev-list --max-count=1 HEAD)
ca2cedba 22
01dc8133 23 test "$sha_main" = "$sha_head"
ca2cedba
PH
24}
25
26
27test_expect_success 'setup a submodule tree' '
28 echo file > file &&
29 git add file &&
30 test_tick &&
4bf9dd97 31 git commit -m upstream &&
ca2cedba
PH
32 git clone . super &&
33 git clone super submodule &&
c9c8c56e
SB
34 git clone super rebasing &&
35 git clone super merging &&
322bb6e1 36 git clone super none &&
ca2cedba
PH
37 (cd super &&
38 git submodule add ../submodule submodule &&
39 test_tick &&
40 git commit -m "submodule" &&
41 git submodule init submodule
42 ) &&
43 (cd submodule &&
44 echo "line2" > file &&
45 git add file &&
46 git commit -m "Commit 2"
47 ) &&
48 (cd super &&
49 (cd submodule &&
50 git pull --rebase origin
51 ) &&
52 git add submodule &&
53 git commit -m "submodule update"
c9c8c56e
SB
54 ) &&
55 (cd super &&
56 git submodule add ../rebasing rebasing &&
57 test_tick &&
58 git commit -m "rebasing"
59 ) &&
60 (cd super &&
61 git submodule add ../merging merging &&
62 test_tick &&
63 git commit -m "rebasing"
d5b99f35 64 ) &&
322bb6e1
HV
65 (cd super &&
66 git submodule add ../none none &&
67 test_tick &&
68 git commit -m "none"
c1ab00fb
SB
69 ) &&
70 git clone . recursivesuper &&
8de19d6b 71 ( cd recursivesuper &&
c1ab00fb 72 git submodule add ../super super
322bb6e1 73 )
ca2cedba
PH
74'
75
f0a96e8d
JS
76test_expect_success 'update --remote falls back to using HEAD' '
77 test_create_repo main-branch-submodule &&
78 test_commit -C main-branch-submodule initial &&
79
80 test_create_repo main-branch &&
81 git -C main-branch submodule add ../main-branch-submodule &&
82 git -C main-branch commit -m add-submodule &&
83
84 git -C main-branch-submodule switch -c hello &&
85 test_commit -C main-branch-submodule world &&
86
87 git clone --recursive main-branch main-branch-clone &&
88 git -C main-branch-clone submodule update --remote main-branch-submodule &&
89 test_path_exists main-branch-clone/main-branch-submodule/world.t
90'
91
ca2cedba
PH
92test_expect_success 'submodule update detaching the HEAD ' '
93 (cd super/submodule &&
94 git reset --hard HEAD~1
95 ) &&
96 (cd super &&
97 (cd submodule &&
98 compare_head
99 ) &&
100 git submodule update submodule &&
101 cd submodule &&
091a6eb0
JK
102 ! compare_head
103 )
104'
105
106test_expect_success 'submodule update from subdirectory' '
107 (cd super/submodule &&
108 git reset --hard HEAD~1
109 ) &&
110 mkdir super/sub &&
111 (cd super/sub &&
112 (cd ../submodule &&
113 compare_head
114 ) &&
115 git submodule update ../submodule &&
116 cd ../submodule &&
ca2cedba
PH
117 ! compare_head
118 )
119'
120
c1ab00fb
SB
121supersha1=$(git -C super rev-parse HEAD)
122mergingsha1=$(git -C super/merging rev-parse HEAD)
123nonesha1=$(git -C super/none rev-parse HEAD)
124rebasingsha1=$(git -C super/rebasing rev-parse HEAD)
125submodulesha1=$(git -C super/submodule rev-parse HEAD)
126pwd=$(pwd)
127
128cat <<EOF >expect
129Submodule path '../super': checked out '$supersha1'
c1ab00fb
SB
130Submodule path '../super/merging': checked out '$mergingsha1'
131Submodule path '../super/none': checked out '$nonesha1'
132Submodule path '../super/rebasing': checked out '$rebasingsha1'
133Submodule path '../super/submodule': checked out '$submodulesha1'
134EOF
135
c66410ed 136cat <<EOF >expect2
0b7d324e
JS
137Cloning into '$pwd/recursivesuper/super/merging'...
138Cloning into '$pwd/recursivesuper/super/none'...
139Cloning into '$pwd/recursivesuper/super/rebasing'...
140Cloning into '$pwd/recursivesuper/super/submodule'...
c66410ed
SB
141Submodule 'merging' ($pwd/merging) registered for path '../super/merging'
142Submodule 'none' ($pwd/none) registered for path '../super/none'
143Submodule 'rebasing' ($pwd/rebasing) registered for path '../super/rebasing'
144Submodule 'submodule' ($pwd/submodule) registered for path '../super/submodule'
c66410ed 145done.
c66410ed 146done.
c66410ed 147done.
c66410ed
SB
148done.
149EOF
150
c1ab00fb
SB
151test_expect_success 'submodule update --init --recursive from subdirectory' '
152 git -C recursivesuper/super reset --hard HEAD^ &&
153 (cd recursivesuper &&
154 mkdir tmp &&
155 cd tmp &&
c66410ed 156 git submodule update --init --recursive ../super >../../actual 2>../../actual2
c1ab00fb 157 ) &&
1108cea7 158 test_cmp expect actual &&
0b7d324e 159 sort actual2 >actual2.sorted &&
1108cea7 160 test_cmp expect2 actual2.sorted
c1ab00fb
SB
161'
162
6e7c14e6
SB
163cat <<EOF >expect2
164Submodule 'foo/sub' ($pwd/withsubs/../rebasing) registered for path 'sub'
165EOF
166
167test_expect_success 'submodule update --init from and of subdirectory' '
168 git init withsubs &&
169 (cd withsubs &&
170 mkdir foo &&
171 git submodule add "$(pwd)/../rebasing" foo/sub &&
172 (cd foo &&
173 git submodule deinit -f sub &&
174 git submodule update --init sub 2>../../actual2
175 )
176 ) &&
1108cea7 177 test_cmp expect2 actual2
6e7c14e6
SB
178'
179
e5f522d6
JL
180test_expect_success 'submodule update does not fetch already present commits' '
181 (cd submodule &&
182 echo line3 >> file &&
183 git add file &&
184 test_tick &&
185 git commit -m "upstream line3"
186 ) &&
187 (cd super/submodule &&
188 head=$(git rev-parse --verify HEAD) &&
bd482d6e 189 echo "Submodule path ${SQ}submodule$SQ: checked out $SQ$head$SQ" > ../../expected &&
e5f522d6
JL
190 git reset --hard HEAD~1
191 ) &&
192 (cd super &&
193 git submodule update > ../actual 2> ../actual.err
194 ) &&
1108cea7 195 test_cmp expected actual &&
7e9055bb 196 test_must_be_empty actual.err
e5f522d6
JL
197'
198
9db31bdf
NMC
199test_expect_success 'submodule update should fail due to local changes' '
200 (cd super/submodule &&
201 git reset --hard HEAD~1 &&
202 echo "local change" > file
203 ) &&
204 (cd super &&
205 (cd submodule &&
206 compare_head
207 ) &&
aca8568e
ÆAB
208 test_must_fail git submodule update submodule 2>../actual.raw
209 ) &&
210 sed "s/^> //" >expect <<-\EOF &&
211 > error: Your local changes to the following files would be overwritten by checkout:
212 > file
213 > Please commit your changes or stash them before you switch branches.
214 > Aborting
215 > fatal: Unable to checkout OID in submodule path '\''submodule'\''
216 EOF
217 sed -e "s/checkout $SQ[^$SQ]*$SQ/checkout OID/" <actual.raw >actual &&
218 test_cmp expect actual
219
9db31bdf
NMC
220'
221test_expect_success 'submodule update should throw away changes with --force ' '
222 (cd super &&
223 (cd submodule &&
224 compare_head
225 ) &&
226 git submodule update --force submodule &&
227 cd submodule &&
228 ! compare_head
229 )
230'
231
01d47215
SZ
232test_expect_success 'submodule update --force forcibly checks out submodules' '
233 (cd super &&
234 (cd submodule &&
235 rm -f file
236 ) &&
237 git submodule update --force submodule &&
238 (cd submodule &&
239 test "$(git status -s file)" = ""
240 )
241 )
242'
243
06b1abb5
TK
244test_expect_success 'submodule update --remote should fetch upstream changes' '
245 (cd submodule &&
246 echo line4 >> file &&
247 git add file &&
248 test_tick &&
249 git commit -m "upstream line4"
250 ) &&
251 (cd super &&
252 git submodule update --remote --force submodule &&
253 cd submodule &&
254 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
255 )
256'
257
4d7bc52b
SB
258test_expect_success 'submodule update --remote should fetch upstream changes with .' '
259 (
260 cd super &&
261 git config -f .gitmodules submodule."submodule".branch "." &&
262 git add .gitmodules &&
263 git commit -m "submodules: update from the respective superproject branch"
264 ) &&
265 (
266 cd submodule &&
267 echo line4a >> file &&
268 git add file &&
269 test_tick &&
270 git commit -m "upstream line4a" &&
271 git checkout -b test-branch &&
272 test_commit on-test-branch
273 ) &&
274 (
275 cd super &&
276 git submodule update --remote --force submodule &&
8de19d6b 277 git -C submodule log -1 --oneline >actual &&
01dc8133 278 git -C ../submodule log -1 --oneline main >expect &&
4d7bc52b
SB
279 test_cmp expect actual &&
280 git checkout -b test-branch &&
281 git submodule update --remote --force submodule &&
8de19d6b
SG
282 git -C submodule log -1 --oneline >actual &&
283 git -C ../submodule log -1 --oneline test-branch >expect &&
4d7bc52b 284 test_cmp expect actual &&
01dc8133 285 git checkout main &&
4d7bc52b
SB
286 git branch -d test-branch &&
287 git reset --hard HEAD^
288 )
289'
290
06b1abb5
TK
291test_expect_success 'local config should override .gitmodules branch' '
292 (cd submodule &&
4d7bc52b 293 git checkout test-branch &&
06b1abb5
TK
294 echo line5 >> file &&
295 git add file &&
296 test_tick &&
297 git commit -m "upstream line5" &&
01dc8133 298 git checkout main
06b1abb5
TK
299 ) &&
300 (cd super &&
301 git config submodule.submodule.branch test-branch &&
302 git submodule update --remote --force submodule &&
303 cd submodule &&
304 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
305 )
306'
307
01dc8133 308test_expect_success 'submodule update --rebase staying on main' '
ca2cedba 309 (cd super/submodule &&
01dc8133 310 git checkout main
ca2cedba
PH
311 ) &&
312 (cd super &&
313 (cd submodule &&
314 compare_head
315 ) &&
316 git submodule update --rebase submodule &&
317 cd submodule &&
318 compare_head
319 )
320'
321
01dc8133 322test_expect_success 'submodule update --merge staying on main' '
42b49178
JH
323 (cd super/submodule &&
324 git reset --hard HEAD~1
325 ) &&
326 (cd super &&
327 (cd submodule &&
328 compare_head
329 ) &&
330 git submodule update --merge submodule &&
331 cd submodule &&
332 compare_head
333 )
334'
335
32948425 336test_expect_success 'submodule update - rebase in .git/config' '
ca2cedba 337 (cd super &&
32948425 338 git config submodule.submodule.update rebase
ca2cedba
PH
339 ) &&
340 (cd super/submodule &&
341 git reset --hard HEAD~1
342 ) &&
343 (cd super &&
344 (cd submodule &&
345 compare_head
346 ) &&
347 git submodule update submodule &&
348 cd submodule &&
349 compare_head
350 )
351'
352
32948425 353test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
ca2cedba 354 (cd super &&
32948425 355 git config submodule.submodule.update checkout
ca2cedba
PH
356 ) &&
357 (cd super/submodule &&
358 git reset --hard HEAD~1
359 ) &&
360 (cd super &&
361 (cd submodule &&
362 compare_head
363 ) &&
364 git submodule update --rebase submodule &&
365 cd submodule &&
366 compare_head
367 )
368'
369
42b49178
JH
370test_expect_success 'submodule update - merge in .git/config' '
371 (cd super &&
372 git config submodule.submodule.update merge
373 ) &&
374 (cd super/submodule &&
375 git reset --hard HEAD~1
376 ) &&
377 (cd super &&
378 (cd submodule &&
379 compare_head
380 ) &&
381 git submodule update submodule &&
382 cd submodule &&
383 compare_head
384 )
385'
386
387test_expect_success 'submodule update - checkout in .git/config but --merge given' '
388 (cd super &&
389 git config submodule.submodule.update checkout
390 ) &&
391 (cd super/submodule &&
392 git reset --hard HEAD~1
393 ) &&
394 (cd super &&
395 (cd submodule &&
396 compare_head
397 ) &&
398 git submodule update --merge submodule &&
399 cd submodule &&
400 compare_head
401 )
402'
403
32948425 404test_expect_success 'submodule update - checkout in .git/config' '
ca2cedba 405 (cd super &&
32948425 406 git config submodule.submodule.update checkout
ca2cedba
PH
407 ) &&
408 (cd super/submodule &&
409 git reset --hard HEAD^
410 ) &&
411 (cd super &&
412 (cd submodule &&
413 compare_head
414 ) &&
415 git submodule update submodule &&
416 cd submodule &&
417 ! compare_head
418 )
419'
420
6cb5728c
CP
421test_expect_success 'submodule update - command in .git/config' '
422 (cd super &&
423 git config submodule.submodule.update "!git checkout"
424 ) &&
425 (cd super/submodule &&
426 git reset --hard HEAD^
427 ) &&
428 (cd super &&
429 (cd submodule &&
430 compare_head
431 ) &&
432 git submodule update submodule &&
433 cd submodule &&
434 ! compare_head
435 )
436'
437
e904deb8 438test_expect_success 'submodule update - command in .gitmodules is rejected' '
83a17fa8
SB
439 test_when_finished "git -C super reset --hard HEAD^" &&
440 git -C super config -f .gitmodules submodule.submodule.update "!false" &&
441 git -C super commit -a -m "add command to .gitmodules file" &&
442 git -C super/submodule reset --hard $submodulesha1^ &&
e904deb8 443 test_must_fail git -C super submodule update submodule
83a17fa8
SB
444'
445
bb92255e
JN
446test_expect_success 'fsck detects command in .gitmodules' '
447 git init command-in-gitmodules &&
448 (
449 cd command-in-gitmodules &&
450 git submodule add ../submodule submodule &&
451 test_commit adding-submodule &&
452
453 git config -f .gitmodules submodule.submodule.update "!false" &&
454 git add .gitmodules &&
455 test_commit configuring-update &&
456 test_must_fail git fsck
457 )
83a17fa8
SB
458'
459
b08238ac 460cat << EOF >expect
0008d122 461fatal: Execution of 'false $submodulesha1' failed in submodule path 'submodule'
b08238ac
SB
462EOF
463
6cb5728c
CP
464test_expect_success 'submodule update - command in .git/config catches failure' '
465 (cd super &&
466 git config submodule.submodule.update "!false"
467 ) &&
468 (cd super/submodule &&
b08238ac 469 git reset --hard $submodulesha1^
6cb5728c
CP
470 ) &&
471 (cd super &&
b08238ac
SB
472 test_must_fail git submodule update submodule 2>../actual
473 ) &&
1108cea7 474 test_cmp actual expect
b08238ac
SB
475'
476
477cat << EOF >expect
0008d122 478fatal: Execution of 'false $submodulesha1' failed in submodule path '../submodule'
b08238ac
SB
479EOF
480
481test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
482 (cd super &&
483 git config submodule.submodule.update "!false"
484 ) &&
485 (cd super/submodule &&
486 git reset --hard $submodulesha1^
487 ) &&
488 (cd super &&
489 mkdir tmp && cd tmp &&
490 test_must_fail git submodule update ../submodule 2>../../actual
c1e06d11 491 ) &&
1108cea7 492 test_cmp actual expect
c1e06d11
SB
493'
494
e7b37caf 495test_expect_success 'submodule update - command run for initial population of submodule' '
a4dded01 496 cat >expect <<-EOF &&
0008d122 497 fatal: Execution of '\''false $submodulesha1'\'' failed in submodule path '\''submodule'\''
a4dded01 498 EOF
e7b37caf 499 rm -rf super/submodule &&
a4dded01 500 test_must_fail git -C super submodule update 2>actual &&
1108cea7 501 test_cmp expect actual &&
e7b37caf
SB
502 git -C super submodule update --checkout
503'
504
c1e06d11 505cat << EOF >expect
0008d122
AR
506fatal: Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
507fatal: Failed to recurse into submodule path '../super'
c1e06d11
SB
508EOF
509
510test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
511 (cd recursivesuper &&
512 git submodule update --remote super &&
513 git add super &&
514 git commit -m "update to latest to have more than one commit in submodules"
515 ) &&
516 git -C recursivesuper/super config submodule.submodule.update "!false" &&
517 git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
518 (cd recursivesuper &&
519 mkdir -p tmp && cd tmp &&
520 test_must_fail git submodule update --recursive ../super 2>../../actual
b08238ac 521 ) &&
1108cea7 522 test_cmp actual expect
6cb5728c
CP
523'
524
ac1fbbda 525test_expect_success 'submodule init does not copy command into .git/config' '
e904deb8
JN
526 test_when_finished "git -C super update-index --force-remove submodule1" &&
527 test_when_finished git config -f super/.gitmodules \
528 --remove-section submodule.submodule1 &&
ac1fbbda 529 (cd super &&
65799fbc
EN
530 git ls-files -s submodule >out &&
531 H=$(cut -d" " -f2 out) &&
ac1fbbda
JH
532 mkdir submodule1 &&
533 git update-index --add --cacheinfo 160000 $H submodule1 &&
534 git config -f .gitmodules submodule.submodule1.path submodule1 &&
535 git config -f .gitmodules submodule.submodule1.url ../submodule &&
536 git config -f .gitmodules submodule.submodule1.update !false &&
e904deb8
JN
537 test_must_fail git submodule init submodule1 &&
538 test_expect_code 1 git config submodule.submodule1.update >actual &&
539 test_must_be_empty actual
ac1fbbda
JH
540 )
541'
542
ca2cedba
PH
543test_expect_success 'submodule init picks up rebase' '
544 (cd super &&
c9c8c56e 545 git config -f .gitmodules submodule.rebasing.update rebase &&
ca2cedba 546 git submodule init rebasing &&
c9c8c56e 547 test "rebase" = "$(git config submodule.rebasing.update)"
ca2cedba
PH
548 )
549'
550
42b49178
JH
551test_expect_success 'submodule init picks up merge' '
552 (cd super &&
c9c8c56e 553 git config -f .gitmodules submodule.merging.update merge &&
42b49178 554 git submodule init merging &&
c9c8c56e 555 test "merge" = "$(git config submodule.merging.update)"
42b49178
JH
556 )
557'
558
b200021e 559test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
e7b37caf 560 test_config -C super submodule.submodule.update checkout &&
b200021e
SO
561 (cd super &&
562 rm -rf submodule &&
563 git submodule update submodule &&
564 git status -s submodule >expect &&
565 rm -rf submodule &&
566 git submodule update --merge submodule &&
567 git status -s submodule >actual &&
568 test_cmp expect actual
569 )
570'
571
572test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
e7b37caf 573 test_config -C super submodule.submodule.update checkout &&
b200021e
SO
574 (cd super &&
575 rm -rf submodule &&
576 git submodule update submodule &&
577 git status -s submodule >expect &&
578 rm -rf submodule &&
579 git submodule update --rebase submodule &&
580 git status -s submodule >actual &&
581 test_cmp expect actual
582 )
583'
584
585test_expect_success 'submodule update ignores update=merge config for new submodules' '
586 (cd super &&
587 rm -rf submodule &&
588 git submodule update submodule &&
589 git status -s submodule >expect &&
590 rm -rf submodule &&
591 git config submodule.submodule.update merge &&
592 git submodule update submodule &&
593 git status -s submodule >actual &&
594 git config --unset submodule.submodule.update &&
595 test_cmp expect actual
596 )
597'
598
599test_expect_success 'submodule update ignores update=rebase config for new submodules' '
600 (cd super &&
601 rm -rf submodule &&
602 git submodule update submodule &&
603 git status -s submodule >expect &&
604 rm -rf submodule &&
605 git config submodule.submodule.update rebase &&
606 git submodule update submodule &&
607 git status -s submodule >actual &&
608 git config --unset submodule.submodule.update &&
609 test_cmp expect actual
610 )
611'
612
322bb6e1
HV
613test_expect_success 'submodule init picks up update=none' '
614 (cd super &&
615 git config -f .gitmodules submodule.none.update none &&
616 git submodule init none &&
617 test "none" = "$(git config submodule.none.update)"
618 )
619'
620
621test_expect_success 'submodule update - update=none in .git/config' '
622 (cd super &&
623 git config submodule.submodule.update none &&
624 (cd submodule &&
01dc8133 625 git checkout main &&
322bb6e1
HV
626 compare_head
627 ) &&
65799fbc
EN
628 git diff --name-only >out &&
629 grep ^submodule$ out &&
322bb6e1 630 git submodule update &&
65799fbc
EN
631 git diff --name-only >out &&
632 grep ^submodule$ out &&
322bb6e1
HV
633 (cd submodule &&
634 compare_head
635 ) &&
636 git config --unset submodule.submodule.update &&
637 git submodule update submodule
638 )
639'
640
641test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
642 (cd super &&
643 git config submodule.submodule.update none &&
644 (cd submodule &&
01dc8133 645 git checkout main &&
322bb6e1
HV
646 compare_head
647 ) &&
65799fbc
EN
648 git diff --name-only >out &&
649 grep ^submodule$ out &&
322bb6e1 650 git submodule update --checkout &&
602813cf
EN
651 git diff --name-only >out &&
652 ! grep ^submodule$ out &&
322bb6e1 653 (cd submodule &&
9fd1080a 654 ! compare_head
322bb6e1
HV
655 ) &&
656 git config --unset submodule.submodule.update
657 )
658'
659
660test_expect_success 'submodule update --init skips submodule with update=none' '
661 (cd super &&
662 git add .gitmodules &&
663 git commit -m ".gitmodules"
664 ) &&
665 git clone super cloned &&
666 (cd cloned &&
667 git submodule update --init &&
7e9055bb
EN
668 test_path_exists submodule/.git &&
669 test_path_is_missing none/.git
322bb6e1
HV
670 )
671'
672
4f1ccef8
OS
673test_expect_success 'submodule update with pathspec warns against uninitialized ones' '
674 test_when_finished "rm -fr selective" &&
675 git clone super selective &&
676 (
677 cd selective &&
678 git submodule init submodule &&
679
680 git submodule update submodule 2>err &&
681 ! grep "Submodule path .* not initialized" err &&
682
683 git submodule update rebasing 2>err &&
684 grep "Submodule path .rebasing. not initialized" err &&
685
686 test_path_exists submodule/.git &&
687 test_path_is_missing rebasing/.git
688 )
689
690'
691
692test_expect_success 'submodule update without pathspec updates only initialized ones' '
693 test_when_finished "rm -fr selective" &&
694 git clone super selective &&
695 (
696 cd selective &&
697 git submodule init submodule &&
698 git submodule update 2>err &&
699 test_path_exists submodule/.git &&
700 test_path_is_missing rebasing/.git &&
701 ! grep "Submodule path .* not initialized" err
702 )
703
704'
705
15ffb7cd
FG
706test_expect_success 'submodule update continues after checkout error' '
707 (cd super &&
708 git reset --hard HEAD &&
709 git submodule add ../submodule submodule2 &&
710 git submodule init &&
711 git commit -am "new_submodule" &&
712 (cd submodule2 &&
ad6a599c 713 git rev-parse --verify HEAD >../expect
15ffb7cd
FG
714 ) &&
715 (cd submodule &&
716 test_commit "update_submodule" file
717 ) &&
718 (cd submodule2 &&
719 test_commit "update_submodule2" file
720 ) &&
721 git add submodule &&
722 git add submodule2 &&
723 git commit -m "two_new_submodule_commits" &&
724 (cd submodule &&
725 echo "" > file
726 ) &&
727 git checkout HEAD^ &&
728 test_must_fail git submodule update &&
729 (cd submodule2 &&
ad6a599c 730 git rev-parse --verify HEAD >../actual
15ffb7cd
FG
731 ) &&
732 test_cmp expect actual
733 )
734'
735test_expect_success 'submodule update continues after recursive checkout error' '
736 (cd super &&
737 git reset --hard HEAD &&
01dc8133 738 git checkout main &&
15ffb7cd
FG
739 git submodule update &&
740 (cd submodule &&
741 git submodule add ../submodule subsubmodule &&
742 git submodule init &&
743 git commit -m "new_subsubmodule"
744 ) &&
745 git add submodule &&
746 git commit -m "update_submodule" &&
747 (cd submodule &&
748 (cd subsubmodule &&
749 test_commit "update_subsubmodule" file
750 ) &&
751 git add subsubmodule &&
752 test_commit "update_submodule_again" file &&
753 (cd subsubmodule &&
754 test_commit "update_subsubmodule_again" file
755 ) &&
756 test_commit "update_submodule_again_again" file
757 ) &&
758 (cd submodule2 &&
ad6a599c 759 git rev-parse --verify HEAD >../expect &&
15ffb7cd
FG
760 test_commit "update_submodule2_again" file
761 ) &&
762 git add submodule &&
763 git add submodule2 &&
764 git commit -m "new_commits" &&
765 git checkout HEAD^ &&
766 (cd submodule &&
767 git checkout HEAD^ &&
768 (cd subsubmodule &&
769 echo "" > file
770 )
771 ) &&
0b917a9f 772 test_expect_code 1 git submodule update --recursive &&
15ffb7cd 773 (cd submodule2 &&
ad6a599c 774 git rev-parse --verify HEAD >../actual
15ffb7cd
FG
775 ) &&
776 test_cmp expect actual
777 )
778'
779
780test_expect_success 'submodule update exit immediately in case of merge conflict' '
781 (cd super &&
01dc8133 782 git checkout main &&
15ffb7cd
FG
783 git reset --hard HEAD &&
784 (cd submodule &&
785 (cd subsubmodule &&
786 git reset --hard HEAD
787 )
788 ) &&
789 git submodule update --recursive &&
790 (cd submodule &&
791 test_commit "update_submodule_2" file
792 ) &&
793 (cd submodule2 &&
794 test_commit "update_submodule2_2" file
795 ) &&
796 git add submodule &&
797 git add submodule2 &&
798 git commit -m "two_new_submodule_commits" &&
799 (cd submodule &&
01dc8133 800 git checkout main &&
15ffb7cd
FG
801 test_commit "conflict" file &&
802 echo "conflict" > file
803 ) &&
804 git checkout HEAD^ &&
805 (cd submodule2 &&
ad6a599c 806 git rev-parse --verify HEAD >../expect
15ffb7cd
FG
807 ) &&
808 git config submodule.submodule.update merge &&
809 test_must_fail git submodule update &&
810 (cd submodule2 &&
ad6a599c 811 git rev-parse --verify HEAD >../actual
15ffb7cd
FG
812 ) &&
813 test_cmp expect actual
814 )
815'
501770e1 816
15ffb7cd
FG
817test_expect_success 'submodule update exit immediately after recursive rebase error' '
818 (cd super &&
01dc8133 819 git checkout main &&
15ffb7cd
FG
820 git reset --hard HEAD &&
821 (cd submodule &&
822 git reset --hard HEAD &&
823 git submodule update --recursive
824 ) &&
825 (cd submodule &&
826 test_commit "update_submodule_3" file
827 ) &&
828 (cd submodule2 &&
829 test_commit "update_submodule2_3" file
830 ) &&
831 git add submodule &&
832 git add submodule2 &&
833 git commit -m "two_new_submodule_commits" &&
834 (cd submodule &&
01dc8133 835 git checkout main &&
15ffb7cd
FG
836 test_commit "conflict2" file &&
837 echo "conflict" > file
838 ) &&
839 git checkout HEAD^ &&
840 (cd submodule2 &&
ad6a599c 841 git rev-parse --verify HEAD >../expect
15ffb7cd
FG
842 ) &&
843 git config submodule.submodule.update rebase &&
844 test_must_fail git submodule update &&
845 (cd submodule2 &&
ad6a599c 846 git rev-parse --verify HEAD >../actual
15ffb7cd
FG
847 ) &&
848 test_cmp expect actual
849 )
850'
501770e1
FG
851
852test_expect_success 'add different submodules to the same path' '
853 (cd super &&
854 git submodule add ../submodule s1 &&
855 test_must_fail git submodule add ../merging s1
856 )
857'
858
859test_expect_success 'submodule add places git-dir in superprojects git-dir' '
860 (cd super &&
861 mkdir deeper &&
862 git submodule add ../submodule deeper/submodule &&
863 (cd deeper/submodule &&
864 git log > ../../expected
865 ) &&
866 (cd .git/modules/deeper/submodule &&
867 git log > ../../../../actual
868 ) &&
dcbaa0b3 869 test_cmp expected actual
501770e1
FG
870 )
871'
872
873test_expect_success 'submodule update places git-dir in superprojects git-dir' '
874 (cd super &&
875 git commit -m "added submodule"
876 ) &&
877 git clone super super2 &&
878 (cd super2 &&
879 git submodule init deeper/submodule &&
880 git submodule update &&
881 (cd deeper/submodule &&
882 git log > ../../expected
883 ) &&
884 (cd .git/modules/deeper/submodule &&
885 git log > ../../../../actual
886 ) &&
dcbaa0b3 887 test_cmp expected actual
501770e1
FG
888 )
889'
890
891test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
892 (cd super2 &&
893 (cd deeper/submodule &&
894 git submodule add ../submodule subsubmodule &&
895 (cd subsubmodule &&
896 git log > ../../../expected
897 ) &&
898 git commit -m "added subsubmodule" &&
ae74f7d2 899 git push origin :
501770e1
FG
900 ) &&
901 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
902 git log > ../../../../../actual
903 ) &&
904 git add deeper/submodule &&
905 git commit -m "update submodule" &&
ae74f7d2 906 git push origin : &&
dcbaa0b3 907 test_cmp expected actual
501770e1
FG
908 )
909'
910
911test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
912 mkdir super_update_r &&
913 (cd super_update_r &&
914 git init --bare
915 ) &&
916 mkdir subsuper_update_r &&
917 (cd subsuper_update_r &&
918 git init --bare
919 ) &&
920 mkdir subsubsuper_update_r &&
921 (cd subsubsuper_update_r &&
922 git init --bare
923 ) &&
924 git clone subsubsuper_update_r subsubsuper_update_r2 &&
925 (cd subsubsuper_update_r2 &&
926 test_commit "update_subsubsuper" file &&
01dc8133 927 git push origin main
501770e1
FG
928 ) &&
929 git clone subsuper_update_r subsuper_update_r2 &&
930 (cd subsuper_update_r2 &&
931 test_commit "update_subsuper" file &&
932 git submodule add ../subsubsuper_update_r subsubmodule &&
933 git commit -am "subsubmodule" &&
01dc8133 934 git push origin main
501770e1
FG
935 ) &&
936 git clone super_update_r super_update_r2 &&
937 (cd super_update_r2 &&
938 test_commit "update_super" file &&
939 git submodule add ../subsuper_update_r submodule &&
940 git commit -am "submodule" &&
01dc8133 941 git push origin main
501770e1
FG
942 ) &&
943 rm -rf super_update_r2 &&
944 git clone super_update_r super_update_r2 &&
945 (cd super_update_r2 &&
75bf5e60 946 git submodule update --init --recursive >actual &&
d851ffb9 947 test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
501770e1
FG
948 (cd submodule/subsubmodule &&
949 git log > ../../expected
950 ) &&
e974e06d 951 (cd .git/modules/submodule/modules/subsubmodule &&
501770e1 952 git log > ../../../../../actual
e974e06d 953 ) &&
dcbaa0b3 954 test_cmp expected actual
501770e1
FG
955 )
956'
957
1017c1ab
JL
958test_expect_success 'submodule add properly re-creates deeper level submodules' '
959 (cd super &&
01dc8133 960 git reset --hard main &&
1017c1ab 961 rm -rf deeper/ &&
4b7c286e 962 git submodule add --force ../submodule deeper/submodule
1017c1ab
JL
963 )
964'
965
69c30517
JL
966test_expect_success 'submodule update properly revives a moved submodule' '
967 (cd super &&
b397ea48 968 H=$(git rev-parse --short HEAD) &&
69c30517 969 git commit -am "pre move" &&
b397ea48 970 H2=$(git rev-parse --short HEAD) &&
65799fbc
EN
971 git status >out &&
972 sed "s/$H/XXX/" out >expect &&
e974e06d 973 H=$(cd submodule2 && git rev-parse HEAD) &&
69c30517
JL
974 git rm --cached submodule2 &&
975 rm -rf submodule2 &&
976 mkdir -p "moved/sub module" &&
977 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
8de19d6b 978 git config -f .gitmodules submodule.submodule2.path "moved/sub module" &&
69c30517
JL
979 git commit -am "post move" &&
980 git submodule update &&
65799fbc
EN
981 git status > out &&
982 sed "s/$H2/XXX/" out >actual &&
69c30517
JL
983 test_cmp expect actual
984 )
985'
986
6eafa6d0
JL
987test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
988 mkdir -p linked/dir &&
989 ln -s linked/dir linkto &&
275cd184
FG
990 (cd linkto &&
991 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
992 (cd super &&
993 git submodule update --init --recursive
994 )
6eafa6d0
JL
995 )
996'
997
275cd184 998test_expect_success 'submodule update clone shallow submodule' '
d4470c5a 999 test_when_finished "rm -rf super3" &&
65799fbc 1000 first=$(git -C cloned rev-parse HEAD:submodule) &&
d4470c5a 1001 second=$(git -C submodule rev-parse HEAD) &&
967d7f89 1002 commit_count=$(git -C submodule rev-list --count $first^..$second) &&
275cd184 1003 git clone cloned super3 &&
99094a7a 1004 pwd=$(pwd) &&
d4470c5a
SB
1005 (
1006 cd super3 &&
1007 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
1008 mv -f .gitmodules.tmp .gitmodules &&
1009 git submodule update --init --depth=$commit_count &&
65799fbc
EN
1010 git -C submodule log --oneline >out &&
1011 test_line_count = 1 out
d4470c5a 1012 )
36141282
JL
1013'
1014
6cbf454a
SB
1015test_expect_success 'submodule update clone shallow submodule outside of depth' '
1016 test_when_finished "rm -rf super3" &&
1017 git clone cloned super3 &&
1018 pwd=$(pwd) &&
1019 (
1020 cd super3 &&
1021 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
1022 mv -f .gitmodules.tmp .gitmodules &&
ab0c5f50
JT
1023 # Some protocol versions (e.g. 2) support fetching
1024 # unadvertised objects, so restrict this test to v0.
8a1b0978 1025 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
ab0c5f50 1026 git submodule update --init --depth=1 2>actual &&
6cbf454a
SB
1027 test_i18ngrep "Direct fetching of that commit failed." actual &&
1028 git -C ../submodule config uploadpack.allowReachableSHA1InWant true &&
1029 git submodule update --init --depth=1 >actual &&
65799fbc
EN
1030 git -C submodule log --oneline >out &&
1031 test_line_count = 1 out
6cbf454a
SB
1032 )
1033'
1034
36141282
JL
1035test_expect_success 'submodule update --recursive drops module name before recursing' '
1036 (cd super2 &&
1037 (cd deeper/submodule/subsubmodule &&
1038 git checkout HEAD^
1039 ) &&
1040 git submodule update --recursive deeper/submodule >actual &&
1041 test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
275cd184
FG
1042 )
1043'
2335b870
SB
1044
1045test_expect_success 'submodule update can be run in parallel' '
1046 (cd super2 &&
1047 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
1048 grep "7 tasks" trace.out &&
1049 git config submodule.fetchJobs 8 &&
1050 GIT_TRACE=$(pwd)/trace.out git submodule update &&
1051 grep "8 tasks" trace.out &&
1052 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
1053 grep "9 tasks" trace.out
1054 )
1055'
72290d6a
SB
1056
1057test_expect_success 'git clone passes the parallel jobs config on to submodules' '
1058 test_when_finished "rm -rf super4" &&
1059 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
1060 grep "7 tasks" trace.out &&
1061 rm -rf super4 &&
1062 git config --global submodule.fetchJobs 8 &&
1063 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
1064 grep "8 tasks" trace.out &&
1065 rm -rf super4 &&
1066 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
1067 grep "9 tasks" trace.out &&
1068 rm -rf super4
1069'
1070
3ad0401e
TD
1071test_expect_success 'submodule update --quiet passes quietness to merge/rebase' '
1072 (cd super &&
1073 test_commit -C rebasing message &&
1074 git submodule update --rebase --quiet >out 2>err &&
1075 test_must_be_empty out &&
1076 test_must_be_empty err &&
0d68ee72 1077 git submodule update --rebase >out 2>err &&
3ad0401e
TD
1078 test_file_not_empty out &&
1079 test_must_be_empty err
1080 )
1081'
1082
62af4bdd
NC
1083test_expect_success 'submodule update --quiet passes quietness to fetch with a shallow clone' '
1084 test_when_finished "rm -rf super4 super5 super6" &&
1085 git clone . super4 &&
1086 (cd super4 &&
1087 git submodule add --quiet file://"$TRASH_DIRECTORY"/submodule submodule3 &&
1088 git commit -am "setup submodule3"
1089 ) &&
1090 (cd submodule &&
1091 test_commit line6 file
1092 ) &&
1093 git clone super4 super5 &&
1094 (cd super5 &&
1095 git submodule update --quiet --init --depth=1 submodule3 >out 2>err &&
1096 test_must_be_empty out &&
1097 test_must_be_empty err
1098 ) &&
1099 git clone super4 super6 &&
1100 (cd super6 &&
1101 git submodule update --init --depth=1 submodule3 >out 2>err &&
1102 test_file_not_empty out &&
1103 test_file_not_empty err
1104 )
1105'
1106
104744f9
GC
1107test_expect_success 'submodule update --filter requires --init' '
1108 test_expect_code 129 git -C super submodule update --filter blob:none
1109'
1110
1111test_expect_success 'submodule update --filter sets partial clone settings' '
1112 test_when_finished "rm -rf super-filter" &&
1113 git clone cloned super-filter &&
1114 git -C super-filter submodule update --init --filter blob:none &&
1115 test_cmp_config -C super-filter/submodule true remote.origin.promisor &&
1116 test_cmp_config -C super-filter/submodule blob:none remote.origin.partialclonefilter
1117'
1118
8fc36c39
GC
1119# NEEDSWORK: Clean up the tests so that we can reuse the test setup.
1120# Don't reuse the existing repos because the earlier tests have
1121# intentionally disruptive configurations.
1122test_expect_success 'setup clean recursive superproject' '
1123 git init bottom &&
1124 test_commit -C bottom "bottom" &&
1125 git init middle &&
1126 git -C middle submodule add ../bottom bottom &&
1127 git -C middle commit -m "middle" &&
1128 git init top &&
1129 git -C top submodule add ../middle middle &&
1130 git -C top commit -m "top" &&
1131 git clone --recurse-submodules top top-clean
1132'
1133
1134test_expect_success 'submodule update should skip unmerged submodules' '
1135 test_when_finished "rm -fr top-cloned" &&
1136 cp -r top-clean top-cloned &&
1137
1138 # Create an upstream commit in each repo, starting with bottom
1139 test_commit -C bottom upstream_commit &&
1140 # Create middle commit
1141 git -C middle/bottom fetch &&
1142 git -C middle/bottom checkout -f FETCH_HEAD &&
1143 git -C middle add bottom &&
1144 git -C middle commit -m "upstream_commit" &&
1145 # Create top commit
1146 git -C top/middle fetch &&
1147 git -C top/middle checkout -f FETCH_HEAD &&
1148 git -C top add middle &&
1149 git -C top commit -m "upstream_commit" &&
1150
1151 # Create a downstream conflict
1152 test_commit -C top-cloned/middle/bottom downstream_commit &&
1153 git -C top-cloned/middle add bottom &&
1154 git -C top-cloned/middle commit -m "downstream_commit" &&
1155 git -C top-cloned/middle fetch --recurse-submodules origin &&
1156 test_must_fail git -C top-cloned/middle merge origin/main &&
1157
1158 # Make the update of "middle" a no-op, otherwise we error out
1159 # because of its unmerged state
1160 test_config -C top-cloned submodule.middle.update !true &&
1161 git -C top-cloned submodule update --recursive 2>actual.err &&
1162 cat >expect.err <<-\EOF &&
618b8445 1163 Skipping unmerged submodule middle/bottom
8fc36c39
GC
1164 EOF
1165 test_cmp expect.err actual.err
1166'
1167
1168test_expect_success 'submodule update --recursive skip submodules with strategy=none' '
1169 test_when_finished "rm -fr top-cloned" &&
1170 cp -r top-clean top-cloned &&
1171
1172 test_commit -C top-cloned/middle/bottom downstream_commit &&
1173 git -C top-cloned/middle config submodule.bottom.update none &&
1174 git -C top-cloned submodule update --recursive 2>actual.err &&
1175 cat >expect.err <<-\EOF &&
618b8445 1176 Skipping submodule '\''middle/bottom'\''
8fc36c39
GC
1177 EOF
1178 test_cmp expect.err actual.err
1179'
1180
ca2cedba 1181test_done