Merge branch 'ah/merge-ort-i18n'
[git] / t / t7406-submodule-update.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2009 Red Hat, Inc.
4 #
5
6 test_description='Test updating submodules
7
8 This test verifies that "git submodule update" detaches the HEAD of the
9 submodule and "git submodule update --rebase/--merge" does not detach the HEAD.
10 '
11
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14
15 . ./test-lib.sh
16
17
18 compare_head()
19 {
20     sha_main=$(git rev-list --max-count=1 main)
21     sha_head=$(git rev-list --max-count=1 HEAD)
22
23     test "$sha_main" = "$sha_head"
24 }
25
26
27 test_expect_success 'setup a submodule tree' '
28         echo file > file &&
29         git add file &&
30         test_tick &&
31         git commit -m upstream &&
32         git clone . super &&
33         git clone super submodule &&
34         git clone super rebasing &&
35         git clone super merging &&
36         git clone super none &&
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"
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"
64         ) &&
65         (cd super &&
66          git submodule add ../none none &&
67          test_tick &&
68          git commit -m "none"
69         ) &&
70         git clone . recursivesuper &&
71         ( cd recursivesuper &&
72          git submodule add ../super super
73         )
74 '
75
76 test_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
92 test_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 &&
102          ! compare_head
103         )
104 '
105
106 test_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 &&
117          ! compare_head
118         )
119 '
120
121 supersha1=$(git -C super rev-parse HEAD)
122 mergingsha1=$(git -C super/merging rev-parse HEAD)
123 nonesha1=$(git -C super/none rev-parse HEAD)
124 rebasingsha1=$(git -C super/rebasing rev-parse HEAD)
125 submodulesha1=$(git -C super/submodule rev-parse HEAD)
126 pwd=$(pwd)
127
128 cat <<EOF >expect
129 Submodule path '../super': checked out '$supersha1'
130 Submodule path '../super/merging': checked out '$mergingsha1'
131 Submodule path '../super/none': checked out '$nonesha1'
132 Submodule path '../super/rebasing': checked out '$rebasingsha1'
133 Submodule path '../super/submodule': checked out '$submodulesha1'
134 EOF
135
136 cat <<EOF >expect2
137 Cloning into '$pwd/recursivesuper/super/merging'...
138 Cloning into '$pwd/recursivesuper/super/none'...
139 Cloning into '$pwd/recursivesuper/super/rebasing'...
140 Cloning into '$pwd/recursivesuper/super/submodule'...
141 Submodule 'merging' ($pwd/merging) registered for path '../super/merging'
142 Submodule 'none' ($pwd/none) registered for path '../super/none'
143 Submodule 'rebasing' ($pwd/rebasing) registered for path '../super/rebasing'
144 Submodule 'submodule' ($pwd/submodule) registered for path '../super/submodule'
145 done.
146 done.
147 done.
148 done.
149 EOF
150
151 test_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 &&
156          git submodule update --init --recursive ../super >../../actual 2>../../actual2
157         ) &&
158         test_cmp expect actual &&
159         sort actual2 >actual2.sorted &&
160         test_cmp expect2 actual2.sorted
161 '
162
163 cat <<EOF >expect2
164 Submodule 'foo/sub' ($pwd/withsubs/../rebasing) registered for path 'sub'
165 EOF
166
167 test_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         ) &&
177         test_cmp expect2 actual2
178 '
179
180 test_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) &&
189           echo "Submodule path ${SQ}submodule$SQ: checked out $SQ$head$SQ" > ../../expected &&
190           git reset --hard HEAD~1
191         ) &&
192         (cd super &&
193           git submodule update > ../actual 2> ../actual.err
194         ) &&
195         test_cmp expected actual &&
196         test_must_be_empty actual.err
197 '
198
199 test_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          ) &&
208          test_must_fail git submodule update submodule
209         )
210 '
211 test_expect_success 'submodule update should throw away changes with --force ' '
212         (cd super &&
213          (cd submodule &&
214           compare_head
215          ) &&
216          git submodule update --force submodule &&
217          cd submodule &&
218          ! compare_head
219         )
220 '
221
222 test_expect_success 'submodule update --force forcibly checks out submodules' '
223         (cd super &&
224          (cd submodule &&
225           rm -f file
226          ) &&
227          git submodule update --force submodule &&
228          (cd submodule &&
229           test "$(git status -s file)" = ""
230          )
231         )
232 '
233
234 test_expect_success 'submodule update --remote should fetch upstream changes' '
235         (cd submodule &&
236          echo line4 >> file &&
237          git add file &&
238          test_tick &&
239          git commit -m "upstream line4"
240         ) &&
241         (cd super &&
242          git submodule update --remote --force submodule &&
243          cd submodule &&
244          test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
245         )
246 '
247
248 test_expect_success 'submodule update --remote should fetch upstream changes with .' '
249         (
250                 cd super &&
251                 git config -f .gitmodules submodule."submodule".branch "." &&
252                 git add .gitmodules &&
253                 git commit -m "submodules: update from the respective superproject branch"
254         ) &&
255         (
256                 cd submodule &&
257                 echo line4a >> file &&
258                 git add file &&
259                 test_tick &&
260                 git commit -m "upstream line4a" &&
261                 git checkout -b test-branch &&
262                 test_commit on-test-branch
263         ) &&
264         (
265                 cd super &&
266                 git submodule update --remote --force submodule &&
267                 git -C submodule log -1 --oneline >actual &&
268                 git -C ../submodule log -1 --oneline main >expect &&
269                 test_cmp expect actual &&
270                 git checkout -b test-branch &&
271                 git submodule update --remote --force submodule &&
272                 git -C submodule log -1 --oneline >actual &&
273                 git -C ../submodule log -1 --oneline test-branch >expect &&
274                 test_cmp expect actual &&
275                 git checkout main &&
276                 git branch -d test-branch &&
277                 git reset --hard HEAD^
278         )
279 '
280
281 test_expect_success 'local config should override .gitmodules branch' '
282         (cd submodule &&
283          git checkout test-branch &&
284          echo line5 >> file &&
285          git add file &&
286          test_tick &&
287          git commit -m "upstream line5" &&
288          git checkout main
289         ) &&
290         (cd super &&
291          git config submodule.submodule.branch test-branch &&
292          git submodule update --remote --force submodule &&
293          cd submodule &&
294          test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
295         )
296 '
297
298 test_expect_success 'submodule update --rebase staying on main' '
299         (cd super/submodule &&
300           git checkout main
301         ) &&
302         (cd super &&
303          (cd submodule &&
304           compare_head
305          ) &&
306          git submodule update --rebase submodule &&
307          cd submodule &&
308          compare_head
309         )
310 '
311
312 test_expect_success 'submodule update --merge staying on main' '
313         (cd super/submodule &&
314           git reset --hard HEAD~1
315         ) &&
316         (cd super &&
317          (cd submodule &&
318           compare_head
319          ) &&
320          git submodule update --merge submodule &&
321          cd submodule &&
322          compare_head
323         )
324 '
325
326 test_expect_success 'submodule update - rebase in .git/config' '
327         (cd super &&
328          git config submodule.submodule.update rebase
329         ) &&
330         (cd super/submodule &&
331           git reset --hard HEAD~1
332         ) &&
333         (cd super &&
334          (cd submodule &&
335           compare_head
336          ) &&
337          git submodule update submodule &&
338          cd submodule &&
339          compare_head
340         )
341 '
342
343 test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
344         (cd super &&
345          git config submodule.submodule.update checkout
346         ) &&
347         (cd super/submodule &&
348           git reset --hard HEAD~1
349         ) &&
350         (cd super &&
351          (cd submodule &&
352           compare_head
353          ) &&
354          git submodule update --rebase submodule &&
355          cd submodule &&
356          compare_head
357         )
358 '
359
360 test_expect_success 'submodule update - merge in .git/config' '
361         (cd super &&
362          git config submodule.submodule.update merge
363         ) &&
364         (cd super/submodule &&
365           git reset --hard HEAD~1
366         ) &&
367         (cd super &&
368          (cd submodule &&
369           compare_head
370          ) &&
371          git submodule update submodule &&
372          cd submodule &&
373          compare_head
374         )
375 '
376
377 test_expect_success 'submodule update - checkout in .git/config but --merge given' '
378         (cd super &&
379          git config submodule.submodule.update checkout
380         ) &&
381         (cd super/submodule &&
382           git reset --hard HEAD~1
383         ) &&
384         (cd super &&
385          (cd submodule &&
386           compare_head
387          ) &&
388          git submodule update --merge submodule &&
389          cd submodule &&
390          compare_head
391         )
392 '
393
394 test_expect_success 'submodule update - checkout in .git/config' '
395         (cd super &&
396          git config submodule.submodule.update checkout
397         ) &&
398         (cd super/submodule &&
399           git reset --hard HEAD^
400         ) &&
401         (cd super &&
402          (cd submodule &&
403           compare_head
404          ) &&
405          git submodule update submodule &&
406          cd submodule &&
407          ! compare_head
408         )
409 '
410
411 test_expect_success 'submodule update - command in .git/config' '
412         (cd super &&
413          git config submodule.submodule.update "!git checkout"
414         ) &&
415         (cd super/submodule &&
416           git reset --hard HEAD^
417         ) &&
418         (cd super &&
419          (cd submodule &&
420           compare_head
421          ) &&
422          git submodule update submodule &&
423          cd submodule &&
424          ! compare_head
425         )
426 '
427
428 test_expect_success 'submodule update - command in .gitmodules is rejected' '
429         test_when_finished "git -C super reset --hard HEAD^" &&
430         git -C super config -f .gitmodules submodule.submodule.update "!false" &&
431         git -C super commit -a -m "add command to .gitmodules file" &&
432         git -C super/submodule reset --hard $submodulesha1^ &&
433         test_must_fail git -C super submodule update submodule
434 '
435
436 test_expect_success 'fsck detects command in .gitmodules' '
437         git init command-in-gitmodules &&
438         (
439                 cd command-in-gitmodules &&
440                 git submodule add ../submodule submodule &&
441                 test_commit adding-submodule &&
442
443                 git config -f .gitmodules submodule.submodule.update "!false" &&
444                 git add .gitmodules &&
445                 test_commit configuring-update &&
446                 test_must_fail git fsck
447         )
448 '
449
450 cat << EOF >expect
451 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
452 EOF
453
454 test_expect_success 'submodule update - command in .git/config catches failure' '
455         (cd super &&
456          git config submodule.submodule.update "!false"
457         ) &&
458         (cd super/submodule &&
459           git reset --hard $submodulesha1^
460         ) &&
461         (cd super &&
462          test_must_fail git submodule update submodule 2>../actual
463         ) &&
464         test_cmp actual expect
465 '
466
467 cat << EOF >expect
468 Execution of 'false $submodulesha1' failed in submodule path '../submodule'
469 EOF
470
471 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
472         (cd super &&
473          git config submodule.submodule.update "!false"
474         ) &&
475         (cd super/submodule &&
476           git reset --hard $submodulesha1^
477         ) &&
478         (cd super &&
479          mkdir tmp && cd tmp &&
480          test_must_fail git submodule update ../submodule 2>../../actual
481         ) &&
482         test_cmp actual expect
483 '
484
485 test_expect_success 'submodule update - command run for initial population of submodule' '
486         cat >expect <<-EOF &&
487         Execution of '\''false $submodulesha1'\'' failed in submodule path '\''submodule'\''
488         EOF
489         rm -rf super/submodule &&
490         test_must_fail git -C super submodule update 2>actual &&
491         test_cmp expect actual &&
492         git -C super submodule update --checkout
493 '
494
495 cat << EOF >expect
496 Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
497 Failed to recurse into submodule path '../super'
498 EOF
499
500 test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
501         (cd recursivesuper &&
502          git submodule update --remote super &&
503          git add super &&
504          git commit -m "update to latest to have more than one commit in submodules"
505         ) &&
506         git -C recursivesuper/super config submodule.submodule.update "!false" &&
507         git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
508         (cd recursivesuper &&
509          mkdir -p tmp && cd tmp &&
510          test_must_fail git submodule update --recursive ../super 2>../../actual
511         ) &&
512         test_cmp actual expect
513 '
514
515 test_expect_success 'submodule init does not copy command into .git/config' '
516         test_when_finished "git -C super update-index --force-remove submodule1" &&
517         test_when_finished git config -f super/.gitmodules \
518                 --remove-section submodule.submodule1 &&
519         (cd super &&
520          git ls-files -s submodule >out &&
521          H=$(cut -d" " -f2 out) &&
522          mkdir submodule1 &&
523          git update-index --add --cacheinfo 160000 $H submodule1 &&
524          git config -f .gitmodules submodule.submodule1.path submodule1 &&
525          git config -f .gitmodules submodule.submodule1.url ../submodule &&
526          git config -f .gitmodules submodule.submodule1.update !false &&
527          test_must_fail git submodule init submodule1 &&
528          test_expect_code 1 git config submodule.submodule1.update >actual &&
529          test_must_be_empty actual
530         )
531 '
532
533 test_expect_success 'submodule init picks up rebase' '
534         (cd super &&
535          git config -f .gitmodules submodule.rebasing.update rebase &&
536          git submodule init rebasing &&
537          test "rebase" = "$(git config submodule.rebasing.update)"
538         )
539 '
540
541 test_expect_success 'submodule init picks up merge' '
542         (cd super &&
543          git config -f .gitmodules submodule.merging.update merge &&
544          git submodule init merging &&
545          test "merge" = "$(git config submodule.merging.update)"
546         )
547 '
548
549 test_expect_success 'submodule update --merge  - ignores --merge  for new submodules' '
550         test_config -C super submodule.submodule.update checkout &&
551         (cd super &&
552          rm -rf submodule &&
553          git submodule update submodule &&
554          git status -s submodule >expect &&
555          rm -rf submodule &&
556          git submodule update --merge submodule &&
557          git status -s submodule >actual &&
558          test_cmp expect actual
559         )
560 '
561
562 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
563         test_config -C super submodule.submodule.update checkout &&
564         (cd super &&
565          rm -rf submodule &&
566          git submodule update submodule &&
567          git status -s submodule >expect &&
568          rm -rf submodule &&
569          git submodule update --rebase submodule &&
570          git status -s submodule >actual &&
571          test_cmp expect actual
572         )
573 '
574
575 test_expect_success 'submodule update ignores update=merge config for new submodules' '
576         (cd super &&
577          rm -rf submodule &&
578          git submodule update submodule &&
579          git status -s submodule >expect &&
580          rm -rf submodule &&
581          git config submodule.submodule.update merge &&
582          git submodule update submodule &&
583          git status -s submodule >actual &&
584          git config --unset submodule.submodule.update &&
585          test_cmp expect actual
586         )
587 '
588
589 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
590         (cd super &&
591          rm -rf submodule &&
592          git submodule update submodule &&
593          git status -s submodule >expect &&
594          rm -rf submodule &&
595          git config submodule.submodule.update rebase &&
596          git submodule update submodule &&
597          git status -s submodule >actual &&
598          git config --unset submodule.submodule.update &&
599          test_cmp expect actual
600         )
601 '
602
603 test_expect_success 'submodule init picks up update=none' '
604         (cd super &&
605          git config -f .gitmodules submodule.none.update none &&
606          git submodule init none &&
607          test "none" = "$(git config submodule.none.update)"
608         )
609 '
610
611 test_expect_success 'submodule update - update=none in .git/config' '
612         (cd super &&
613          git config submodule.submodule.update none &&
614          (cd submodule &&
615           git checkout main &&
616           compare_head
617          ) &&
618          git diff --name-only >out &&
619          grep ^submodule$ out &&
620          git submodule update &&
621          git diff --name-only >out &&
622          grep ^submodule$ out &&
623          (cd submodule &&
624           compare_head
625          ) &&
626          git config --unset submodule.submodule.update &&
627          git submodule update submodule
628         )
629 '
630
631 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
632         (cd super &&
633          git config submodule.submodule.update none &&
634          (cd submodule &&
635           git checkout main &&
636           compare_head
637          ) &&
638          git diff --name-only >out &&
639          grep ^submodule$ out &&
640          git submodule update --checkout &&
641          git diff --name-only >out &&
642          ! grep ^submodule$ out &&
643          (cd submodule &&
644           ! compare_head
645          ) &&
646          git config --unset submodule.submodule.update
647         )
648 '
649
650 test_expect_success 'submodule update --init skips submodule with update=none' '
651         (cd super &&
652          git add .gitmodules &&
653          git commit -m ".gitmodules"
654         ) &&
655         git clone super cloned &&
656         (cd cloned &&
657          git submodule update --init &&
658          test_path_exists submodule/.git &&
659          test_path_is_missing none/.git
660         )
661 '
662
663 test_expect_success 'submodule update continues after checkout error' '
664         (cd super &&
665          git reset --hard HEAD &&
666          git submodule add ../submodule submodule2 &&
667          git submodule init &&
668          git commit -am "new_submodule" &&
669          (cd submodule2 &&
670           git rev-parse --verify HEAD >../expect
671          ) &&
672          (cd submodule &&
673           test_commit "update_submodule" file
674          ) &&
675          (cd submodule2 &&
676           test_commit "update_submodule2" file
677          ) &&
678          git add submodule &&
679          git add submodule2 &&
680          git commit -m "two_new_submodule_commits" &&
681          (cd submodule &&
682           echo "" > file
683          ) &&
684          git checkout HEAD^ &&
685          test_must_fail git submodule update &&
686          (cd submodule2 &&
687           git rev-parse --verify HEAD >../actual
688          ) &&
689          test_cmp expect actual
690         )
691 '
692 test_expect_success 'submodule update continues after recursive checkout error' '
693         (cd super &&
694          git reset --hard HEAD &&
695          git checkout main &&
696          git submodule update &&
697          (cd submodule &&
698           git submodule add ../submodule subsubmodule &&
699           git submodule init &&
700           git commit -m "new_subsubmodule"
701          ) &&
702          git add submodule &&
703          git commit -m "update_submodule" &&
704          (cd submodule &&
705           (cd subsubmodule &&
706            test_commit "update_subsubmodule" file
707           ) &&
708           git add subsubmodule &&
709           test_commit "update_submodule_again" file &&
710           (cd subsubmodule &&
711            test_commit "update_subsubmodule_again" file
712           ) &&
713           test_commit "update_submodule_again_again" file
714          ) &&
715          (cd submodule2 &&
716           git rev-parse --verify HEAD >../expect &&
717           test_commit "update_submodule2_again" file
718          ) &&
719          git add submodule &&
720          git add submodule2 &&
721          git commit -m "new_commits" &&
722          git checkout HEAD^ &&
723          (cd submodule &&
724           git checkout HEAD^ &&
725           (cd subsubmodule &&
726            echo "" > file
727           )
728          ) &&
729          test_must_fail git submodule update --recursive &&
730          (cd submodule2 &&
731           git rev-parse --verify HEAD >../actual
732          ) &&
733          test_cmp expect actual
734         )
735 '
736
737 test_expect_success 'submodule update exit immediately in case of merge conflict' '
738         (cd super &&
739          git checkout main &&
740          git reset --hard HEAD &&
741          (cd submodule &&
742           (cd subsubmodule &&
743            git reset --hard HEAD
744           )
745          ) &&
746          git submodule update --recursive &&
747          (cd submodule &&
748           test_commit "update_submodule_2" file
749          ) &&
750          (cd submodule2 &&
751           test_commit "update_submodule2_2" file
752          ) &&
753          git add submodule &&
754          git add submodule2 &&
755          git commit -m "two_new_submodule_commits" &&
756          (cd submodule &&
757           git checkout main &&
758           test_commit "conflict" file &&
759           echo "conflict" > file
760          ) &&
761          git checkout HEAD^ &&
762          (cd submodule2 &&
763           git rev-parse --verify HEAD >../expect
764          ) &&
765          git config submodule.submodule.update merge &&
766          test_must_fail git submodule update &&
767          (cd submodule2 &&
768           git rev-parse --verify HEAD >../actual
769          ) &&
770          test_cmp expect actual
771         )
772 '
773
774 test_expect_success 'submodule update exit immediately after recursive rebase error' '
775         (cd super &&
776          git checkout main &&
777          git reset --hard HEAD &&
778          (cd submodule &&
779           git reset --hard HEAD &&
780           git submodule update --recursive
781          ) &&
782          (cd submodule &&
783           test_commit "update_submodule_3" file
784          ) &&
785          (cd submodule2 &&
786           test_commit "update_submodule2_3" file
787          ) &&
788          git add submodule &&
789          git add submodule2 &&
790          git commit -m "two_new_submodule_commits" &&
791          (cd submodule &&
792           git checkout main &&
793           test_commit "conflict2" file &&
794           echo "conflict" > file
795          ) &&
796          git checkout HEAD^ &&
797          (cd submodule2 &&
798           git rev-parse --verify HEAD >../expect
799          ) &&
800          git config submodule.submodule.update rebase &&
801          test_must_fail git submodule update &&
802          (cd submodule2 &&
803           git rev-parse --verify HEAD >../actual
804          ) &&
805          test_cmp expect actual
806         )
807 '
808
809 test_expect_success 'add different submodules to the same path' '
810         (cd super &&
811          git submodule add ../submodule s1 &&
812          test_must_fail git submodule add ../merging s1
813         )
814 '
815
816 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
817         (cd super &&
818          mkdir deeper &&
819          git submodule add ../submodule deeper/submodule &&
820          (cd deeper/submodule &&
821           git log > ../../expected
822          ) &&
823          (cd .git/modules/deeper/submodule &&
824           git log > ../../../../actual
825          ) &&
826          test_cmp expected actual
827         )
828 '
829
830 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
831         (cd super &&
832          git commit -m "added submodule"
833         ) &&
834         git clone super super2 &&
835         (cd super2 &&
836          git submodule init deeper/submodule &&
837          git submodule update &&
838          (cd deeper/submodule &&
839           git log > ../../expected
840          ) &&
841          (cd .git/modules/deeper/submodule &&
842           git log > ../../../../actual
843          ) &&
844          test_cmp expected actual
845         )
846 '
847
848 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
849         (cd super2 &&
850          (cd deeper/submodule &&
851           git submodule add ../submodule subsubmodule &&
852           (cd subsubmodule &&
853            git log > ../../../expected
854           ) &&
855           git commit -m "added subsubmodule" &&
856           git push origin :
857          ) &&
858          (cd .git/modules/deeper/submodule/modules/subsubmodule &&
859           git log > ../../../../../actual
860          ) &&
861          git add deeper/submodule &&
862          git commit -m "update submodule" &&
863          git push origin : &&
864          test_cmp expected actual
865         )
866 '
867
868 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
869         mkdir super_update_r &&
870         (cd super_update_r &&
871          git init --bare
872         ) &&
873         mkdir subsuper_update_r &&
874         (cd subsuper_update_r &&
875          git init --bare
876         ) &&
877         mkdir subsubsuper_update_r &&
878         (cd subsubsuper_update_r &&
879          git init --bare
880         ) &&
881         git clone subsubsuper_update_r subsubsuper_update_r2 &&
882         (cd subsubsuper_update_r2 &&
883          test_commit "update_subsubsuper" file &&
884          git push origin main
885         ) &&
886         git clone subsuper_update_r subsuper_update_r2 &&
887         (cd subsuper_update_r2 &&
888          test_commit "update_subsuper" file &&
889          git submodule add ../subsubsuper_update_r subsubmodule &&
890          git commit -am "subsubmodule" &&
891          git push origin main
892         ) &&
893         git clone super_update_r super_update_r2 &&
894         (cd super_update_r2 &&
895          test_commit "update_super" file &&
896          git submodule add ../subsuper_update_r submodule &&
897          git commit -am "submodule" &&
898          git push origin main
899         ) &&
900         rm -rf super_update_r2 &&
901         git clone super_update_r super_update_r2 &&
902         (cd super_update_r2 &&
903          git submodule update --init --recursive >actual &&
904          test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
905          (cd submodule/subsubmodule &&
906           git log > ../../expected
907          ) &&
908          (cd .git/modules/submodule/modules/subsubmodule &&
909           git log > ../../../../../actual
910          ) &&
911          test_cmp expected actual
912         )
913 '
914
915 test_expect_success 'submodule add properly re-creates deeper level submodules' '
916         (cd super &&
917          git reset --hard main &&
918          rm -rf deeper/ &&
919          git submodule add --force ../submodule deeper/submodule
920         )
921 '
922
923 test_expect_success 'submodule update properly revives a moved submodule' '
924         (cd super &&
925          H=$(git rev-parse --short HEAD) &&
926          git commit -am "pre move" &&
927          H2=$(git rev-parse --short HEAD) &&
928          git status >out &&
929          sed "s/$H/XXX/" out >expect &&
930          H=$(cd submodule2 && git rev-parse HEAD) &&
931          git rm --cached submodule2 &&
932          rm -rf submodule2 &&
933          mkdir -p "moved/sub module" &&
934          git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
935          git config -f .gitmodules submodule.submodule2.path "moved/sub module" &&
936          git commit -am "post move" &&
937          git submodule update &&
938          git status > out &&
939          sed "s/$H2/XXX/" out >actual &&
940          test_cmp expect actual
941         )
942 '
943
944 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
945         mkdir -p linked/dir &&
946         ln -s linked/dir linkto &&
947         (cd linkto &&
948          git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
949          (cd super &&
950           git submodule update --init --recursive
951          )
952         )
953 '
954
955 test_expect_success 'submodule update clone shallow submodule' '
956         test_when_finished "rm -rf super3" &&
957         first=$(git -C cloned rev-parse HEAD:submodule) &&
958         second=$(git -C submodule rev-parse HEAD) &&
959         commit_count=$(git -C submodule rev-list --count $first^..$second) &&
960         git clone cloned super3 &&
961         pwd=$(pwd) &&
962         (
963                 cd super3 &&
964                 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
965                 mv -f .gitmodules.tmp .gitmodules &&
966                 git submodule update --init --depth=$commit_count &&
967                 git -C submodule log --oneline >out &&
968                 test_line_count = 1 out
969         )
970 '
971
972 test_expect_success 'submodule update clone shallow submodule outside of depth' '
973         test_when_finished "rm -rf super3" &&
974         git clone cloned super3 &&
975         pwd=$(pwd) &&
976         (
977                 cd super3 &&
978                 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
979                 mv -f .gitmodules.tmp .gitmodules &&
980                 # Some protocol versions (e.g. 2) support fetching
981                 # unadvertised objects, so restrict this test to v0.
982                 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
983                         git submodule update --init --depth=1 2>actual &&
984                 test_i18ngrep "Direct fetching of that commit failed." actual &&
985                 git -C ../submodule config uploadpack.allowReachableSHA1InWant true &&
986                 git submodule update --init --depth=1 >actual &&
987                 git -C submodule log --oneline >out &&
988                 test_line_count = 1 out
989         )
990 '
991
992 test_expect_success 'submodule update --recursive drops module name before recursing' '
993         (cd super2 &&
994          (cd deeper/submodule/subsubmodule &&
995           git checkout HEAD^
996          ) &&
997          git submodule update --recursive deeper/submodule >actual &&
998          test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
999         )
1000 '
1001
1002 test_expect_success 'submodule update can be run in parallel' '
1003         (cd super2 &&
1004          GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
1005          grep "7 tasks" trace.out &&
1006          git config submodule.fetchJobs 8 &&
1007          GIT_TRACE=$(pwd)/trace.out git submodule update &&
1008          grep "8 tasks" trace.out &&
1009          GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
1010          grep "9 tasks" trace.out
1011         )
1012 '
1013
1014 test_expect_success 'git clone passes the parallel jobs config on to submodules' '
1015         test_when_finished "rm -rf super4" &&
1016         GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
1017         grep "7 tasks" trace.out &&
1018         rm -rf super4 &&
1019         git config --global submodule.fetchJobs 8 &&
1020         GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
1021         grep "8 tasks" trace.out &&
1022         rm -rf super4 &&
1023         GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
1024         grep "9 tasks" trace.out &&
1025         rm -rf super4
1026 '
1027
1028 test_expect_success 'submodule update --quiet passes quietness to merge/rebase' '
1029         (cd super &&
1030          test_commit -C rebasing message &&
1031          git submodule update --rebase --quiet >out 2>err &&
1032          test_must_be_empty out &&
1033          test_must_be_empty err &&
1034          git submodule update --rebase -v >out 2>err &&
1035          test_file_not_empty out &&
1036          test_must_be_empty err
1037         )
1038 '
1039
1040 test_expect_success 'submodule update --quiet passes quietness to fetch with a shallow clone' '
1041         test_when_finished "rm -rf super4 super5 super6" &&
1042         git clone . super4 &&
1043         (cd super4 &&
1044          git submodule add --quiet file://"$TRASH_DIRECTORY"/submodule submodule3 &&
1045          git commit -am "setup submodule3"
1046         ) &&
1047         (cd submodule &&
1048           test_commit line6 file
1049         ) &&
1050         git clone super4 super5 &&
1051         (cd super5 &&
1052          git submodule update --quiet --init --depth=1 submodule3 >out 2>err &&
1053          test_must_be_empty out &&
1054          test_must_be_empty err
1055         ) &&
1056         git clone super4 super6 &&
1057         (cd super6 &&
1058          git submodule update --init --depth=1 submodule3 >out 2>err &&
1059          test_file_not_empty out &&
1060          test_file_not_empty err
1061         )
1062 '
1063
1064 test_done