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