3 # Copyright (c) 2009 Red Hat, Inc.
6 test_description='Test updating submodules
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.
17 sha_master=$(git rev-list --max-count=1 master)
18 sha_head=$(git rev-list --max-count=1 HEAD)
20 test "$sha_master" = "$sha_head"
24 test_expect_success 'setup a submodule tree' '
28 git commit -m upstream &&
30 git clone super submodule &&
31 git clone super rebasing &&
32 git clone super merging &&
33 git clone super none &&
35 git submodule add ../submodule submodule &&
37 git commit -m "submodule" &&
38 git submodule init submodule
41 echo "line2" > file &&
43 git commit -m "Commit 2"
47 git pull --rebase origin
50 git commit -m "submodule update"
53 git submodule add ../rebasing rebasing &&
55 git commit -m "rebasing"
58 git submodule add ../merging merging &&
60 git commit -m "rebasing"
63 git submodule add ../none none &&
67 git clone . recursivesuper &&
69 git submodule add ../super super
73 test_expect_success 'submodule update detaching the HEAD ' '
74 (cd super/submodule &&
75 git reset --hard HEAD~1
81 git submodule update submodule &&
87 test_expect_success 'submodule update from subdirectory' '
88 (cd super/submodule &&
89 git reset --hard HEAD~1
96 git submodule update ../submodule &&
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)
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'
118 Submodule 'merging' ($pwd/merging) registered for path '../super/merging'
119 Submodule 'none' ($pwd/none) registered for path '../super/none'
120 Submodule 'rebasing' ($pwd/rebasing) registered for path '../super/rebasing'
121 Submodule 'submodule' ($pwd/submodule) registered for path '../super/submodule'
122 Cloning into '$pwd/recursivesuper/super/merging'...
124 Cloning into '$pwd/recursivesuper/super/none'...
126 Cloning into '$pwd/recursivesuper/super/rebasing'...
128 Cloning into '$pwd/recursivesuper/super/submodule'...
132 test_expect_success 'submodule update --init --recursive from subdirectory' '
133 git -C recursivesuper/super reset --hard HEAD^ &&
134 (cd recursivesuper &&
137 git submodule update --init --recursive ../super >../../actual 2>../../actual2
139 test_i18ncmp expect actual &&
140 test_i18ncmp expect2 actual2
144 test_expect_success 'submodule update does not fetch already present commits' '
146 echo line3 >> file &&
149 git commit -m "upstream line3"
151 (cd super/submodule &&
152 head=$(git rev-parse --verify HEAD) &&
153 echo "Submodule path ${apos}submodule$apos: checked out $apos$head$apos" > ../../expected &&
154 git reset --hard HEAD~1
157 git submodule update > ../actual 2> ../actual.err
159 test_i18ncmp expected actual &&
163 test_expect_success 'submodule update should fail due to local changes' '
164 (cd super/submodule &&
165 git reset --hard HEAD~1 &&
166 echo "local change" > file
172 test_must_fail git submodule update submodule
175 test_expect_success 'submodule update should throw away changes with --force ' '
180 git submodule update --force submodule &&
186 test_expect_success 'submodule update --force forcibly checks out submodules' '
191 git submodule update --force submodule &&
193 test "$(git status -s file)" = ""
198 test_expect_success 'submodule update --remote should fetch upstream changes' '
200 echo line4 >> file &&
203 git commit -m "upstream line4"
206 git submodule update --remote --force submodule &&
208 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
212 test_expect_success 'submodule update --remote should fetch upstream changes with .' '
215 git config -f .gitmodules submodule."submodule".branch "." &&
216 git add .gitmodules &&
217 git commit -m "submodules: update from the respective superproject branch"
221 echo line4a >> file &&
224 git commit -m "upstream line4a" &&
225 git checkout -b test-branch &&
226 test_commit on-test-branch
230 git submodule update --remote --force submodule &&
231 git -C submodule log -1 --oneline >actual
232 git -C ../submodule log -1 --oneline master >expect
233 test_cmp expect actual &&
234 git checkout -b test-branch &&
235 git submodule update --remote --force submodule &&
236 git -C submodule log -1 --oneline >actual
237 git -C ../submodule log -1 --oneline test-branch >expect
238 test_cmp expect actual &&
239 git checkout master &&
240 git branch -d test-branch &&
241 git reset --hard HEAD^
245 test_expect_success 'local config should override .gitmodules branch' '
247 git checkout test-branch &&
248 echo line5 >> file &&
251 git commit -m "upstream line5" &&
255 git config submodule.submodule.branch test-branch &&
256 git submodule update --remote --force submodule &&
258 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
262 test_expect_success 'submodule update --rebase staying on master' '
263 (cd super/submodule &&
270 git submodule update --rebase submodule &&
276 test_expect_success 'submodule update --merge staying on master' '
277 (cd super/submodule &&
278 git reset --hard HEAD~1
284 git submodule update --merge submodule &&
290 test_expect_success 'submodule update - rebase in .git/config' '
292 git config submodule.submodule.update rebase
294 (cd super/submodule &&
295 git reset --hard HEAD~1
301 git submodule update submodule &&
307 test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
309 git config submodule.submodule.update checkout
311 (cd super/submodule &&
312 git reset --hard HEAD~1
318 git submodule update --rebase submodule &&
324 test_expect_success 'submodule update - merge in .git/config' '
326 git config submodule.submodule.update merge
328 (cd super/submodule &&
329 git reset --hard HEAD~1
335 git submodule update submodule &&
341 test_expect_success 'submodule update - checkout in .git/config but --merge given' '
343 git config submodule.submodule.update checkout
345 (cd super/submodule &&
346 git reset --hard HEAD~1
352 git submodule update --merge submodule &&
358 test_expect_success 'submodule update - checkout in .git/config' '
360 git config submodule.submodule.update checkout
362 (cd super/submodule &&
363 git reset --hard HEAD^
369 git submodule update submodule &&
375 test_expect_success 'submodule update - command in .git/config' '
377 git config submodule.submodule.update "!git checkout"
379 (cd super/submodule &&
380 git reset --hard HEAD^
386 git submodule update submodule &&
393 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
396 test_expect_success 'submodule update - command in .git/config catches failure' '
398 git config submodule.submodule.update "!false"
400 (cd super/submodule &&
401 git reset --hard $submodulesha1^
404 test_must_fail git submodule update submodule 2>../actual
406 test_i18ncmp actual expect
410 Execution of 'false $submodulesha1' failed in submodule path '../submodule'
413 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
415 git config submodule.submodule.update "!false"
417 (cd super/submodule &&
418 git reset --hard $submodulesha1^
421 mkdir tmp && cd tmp &&
422 test_must_fail git submodule update ../submodule 2>../../actual
424 test_i18ncmp actual expect
427 test_expect_success 'submodule update - command run for initial population of submodule' '
429 Execution of '\''false $submodulesha1'\'' failed in submodule path '\''submodule'\''
431 rm -rf super/submodule &&
432 test_must_fail git -C super submodule update >../actual &&
433 test_cmp expect actual &&
434 git -C super submodule update --checkout
438 Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
439 Failed to recurse into submodule path '../super'
442 test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
443 (cd recursivesuper &&
444 git submodule update --remote super &&
446 git commit -m "update to latest to have more than one commit in submodules"
448 git -C recursivesuper/super config submodule.submodule.update "!false" &&
449 git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
450 (cd recursivesuper &&
451 mkdir -p tmp && cd tmp &&
452 test_must_fail git submodule update --recursive ../super 2>../../actual
454 test_i18ncmp actual expect
457 test_expect_success 'submodule init does not copy command into .git/config' '
459 H=$(git ls-files -s submodule | cut -d" " -f2) &&
461 git update-index --add --cacheinfo 160000 $H submodule1 &&
462 git config -f .gitmodules submodule.submodule1.path submodule1 &&
463 git config -f .gitmodules submodule.submodule1.url ../submodule &&
464 git config -f .gitmodules submodule.submodule1.update !false &&
465 git submodule init submodule1 &&
466 echo "none" >expect &&
467 git config submodule.submodule1.update >actual &&
468 test_cmp expect actual
472 test_expect_success 'submodule init picks up rebase' '
474 git config -f .gitmodules submodule.rebasing.update rebase &&
475 git submodule init rebasing &&
476 test "rebase" = "$(git config submodule.rebasing.update)"
480 test_expect_success 'submodule init picks up merge' '
482 git config -f .gitmodules submodule.merging.update merge &&
483 git submodule init merging &&
484 test "merge" = "$(git config submodule.merging.update)"
488 test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
489 test_config -C super submodule.submodule.update checkout &&
492 git submodule update submodule &&
493 git status -s submodule >expect &&
495 git submodule update --merge submodule &&
496 git status -s submodule >actual &&
497 test_cmp expect actual
501 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
502 test_config -C super submodule.submodule.update checkout &&
505 git submodule update submodule &&
506 git status -s submodule >expect &&
508 git submodule update --rebase submodule &&
509 git status -s submodule >actual &&
510 test_cmp expect actual
514 test_expect_success 'submodule update ignores update=merge config for new submodules' '
517 git submodule update submodule &&
518 git status -s submodule >expect &&
520 git config submodule.submodule.update merge &&
521 git submodule update submodule &&
522 git status -s submodule >actual &&
523 git config --unset submodule.submodule.update &&
524 test_cmp expect actual
528 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
531 git submodule update submodule &&
532 git status -s submodule >expect &&
534 git config submodule.submodule.update rebase &&
535 git submodule update submodule &&
536 git status -s submodule >actual &&
537 git config --unset submodule.submodule.update &&
538 test_cmp expect actual
542 test_expect_success 'submodule init picks up update=none' '
544 git config -f .gitmodules submodule.none.update none &&
545 git submodule init none &&
546 test "none" = "$(git config submodule.none.update)"
550 test_expect_success 'submodule update - update=none in .git/config' '
552 git config submodule.submodule.update none &&
554 git checkout master &&
557 git diff --raw | grep " submodule" &&
558 git submodule update &&
559 git diff --raw | grep " submodule" &&
563 git config --unset submodule.submodule.update &&
564 git submodule update submodule
568 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
570 git config submodule.submodule.update none &&
572 git checkout master &&
575 git diff --raw | grep " submodule" &&
576 git submodule update --checkout &&
577 test_must_fail git diff --raw \| grep " submodule" &&
579 test_must_fail compare_head
581 git config --unset submodule.submodule.update
585 test_expect_success 'submodule update --init skips submodule with update=none' '
587 git add .gitmodules &&
588 git commit -m ".gitmodules"
590 git clone super cloned &&
592 git submodule update --init &&
593 test -e submodule/.git &&
594 test_must_fail test -e none/.git
598 test_expect_success 'submodule update continues after checkout error' '
600 git reset --hard HEAD &&
601 git submodule add ../submodule submodule2 &&
602 git submodule init &&
603 git commit -am "new_submodule" &&
605 git rev-parse --verify HEAD >../expect
608 test_commit "update_submodule" file
611 test_commit "update_submodule2" file
614 git add submodule2 &&
615 git commit -m "two_new_submodule_commits" &&
619 git checkout HEAD^ &&
620 test_must_fail git submodule update &&
622 git rev-parse --verify HEAD >../actual
624 test_cmp expect actual
627 test_expect_success 'submodule update continues after recursive checkout error' '
629 git reset --hard HEAD &&
630 git checkout master &&
631 git submodule update &&
633 git submodule add ../submodule subsubmodule &&
634 git submodule init &&
635 git commit -m "new_subsubmodule"
638 git commit -m "update_submodule" &&
641 test_commit "update_subsubmodule" file
643 git add subsubmodule &&
644 test_commit "update_submodule_again" file &&
646 test_commit "update_subsubmodule_again" file
648 test_commit "update_submodule_again_again" file
651 git rev-parse --verify HEAD >../expect &&
652 test_commit "update_submodule2_again" file
655 git add submodule2 &&
656 git commit -m "new_commits" &&
657 git checkout HEAD^ &&
659 git checkout HEAD^ &&
664 test_must_fail git submodule update --recursive &&
666 git rev-parse --verify HEAD >../actual
668 test_cmp expect actual
672 test_expect_success 'submodule update exit immediately in case of merge conflict' '
674 git checkout master &&
675 git reset --hard HEAD &&
678 git reset --hard HEAD
681 git submodule update --recursive &&
683 test_commit "update_submodule_2" file
686 test_commit "update_submodule2_2" file
689 git add submodule2 &&
690 git commit -m "two_new_submodule_commits" &&
692 git checkout master &&
693 test_commit "conflict" file &&
694 echo "conflict" > file
696 git checkout HEAD^ &&
698 git rev-parse --verify HEAD >../expect
700 git config submodule.submodule.update merge &&
701 test_must_fail git submodule update &&
703 git rev-parse --verify HEAD >../actual
705 test_cmp expect actual
709 test_expect_success 'submodule update exit immediately after recursive rebase error' '
711 git checkout master &&
712 git reset --hard HEAD &&
714 git reset --hard HEAD &&
715 git submodule update --recursive
718 test_commit "update_submodule_3" file
721 test_commit "update_submodule2_3" file
724 git add submodule2 &&
725 git commit -m "two_new_submodule_commits" &&
727 git checkout master &&
728 test_commit "conflict2" file &&
729 echo "conflict" > file
731 git checkout HEAD^ &&
733 git rev-parse --verify HEAD >../expect
735 git config submodule.submodule.update rebase &&
736 test_must_fail git submodule update &&
738 git rev-parse --verify HEAD >../actual
740 test_cmp expect actual
744 test_expect_success 'add different submodules to the same path' '
746 git submodule add ../submodule s1 &&
747 test_must_fail git submodule add ../merging s1
751 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
754 git submodule add ../submodule deeper/submodule &&
755 (cd deeper/submodule &&
756 git log > ../../expected
758 (cd .git/modules/deeper/submodule &&
759 git log > ../../../../actual
761 test_cmp actual expected
765 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
767 git commit -m "added submodule"
769 git clone super super2 &&
771 git submodule init deeper/submodule &&
772 git submodule update &&
773 (cd deeper/submodule &&
774 git log > ../../expected
776 (cd .git/modules/deeper/submodule &&
777 git log > ../../../../actual
779 test_cmp actual expected
783 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
785 (cd deeper/submodule &&
786 git submodule add ../submodule subsubmodule &&
788 git log > ../../../expected
790 git commit -m "added subsubmodule" &&
793 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
794 git log > ../../../../../actual
796 git add deeper/submodule &&
797 git commit -m "update submodule" &&
799 test_cmp actual expected
803 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
804 mkdir super_update_r &&
805 (cd super_update_r &&
808 mkdir subsuper_update_r &&
809 (cd subsuper_update_r &&
812 mkdir subsubsuper_update_r &&
813 (cd subsubsuper_update_r &&
816 git clone subsubsuper_update_r subsubsuper_update_r2 &&
817 (cd subsubsuper_update_r2 &&
818 test_commit "update_subsubsuper" file &&
819 git push origin master
821 git clone subsuper_update_r subsuper_update_r2 &&
822 (cd subsuper_update_r2 &&
823 test_commit "update_subsuper" file &&
824 git submodule add ../subsubsuper_update_r subsubmodule &&
825 git commit -am "subsubmodule" &&
826 git push origin master
828 git clone super_update_r super_update_r2 &&
829 (cd super_update_r2 &&
830 test_commit "update_super" file &&
831 git submodule add ../subsuper_update_r submodule &&
832 git commit -am "submodule" &&
833 git push origin master
835 rm -rf super_update_r2 &&
836 git clone super_update_r super_update_r2 &&
837 (cd super_update_r2 &&
838 git submodule update --init --recursive >actual &&
839 test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
840 (cd submodule/subsubmodule &&
841 git log > ../../expected
843 (cd .git/modules/submodule/modules/subsubmodule
844 git log > ../../../../../actual
846 test_cmp actual expected
850 test_expect_success 'submodule add properly re-creates deeper level submodules' '
852 git reset --hard master &&
854 git submodule add --force ../submodule deeper/submodule
858 test_expect_success 'submodule update properly revives a moved submodule' '
860 H=$(git rev-parse --short HEAD) &&
861 git commit -am "pre move" &&
862 H2=$(git rev-parse --short HEAD) &&
863 git status | sed "s/$H/XXX/" >expect &&
864 H=$(cd submodule2; git rev-parse HEAD) &&
865 git rm --cached submodule2 &&
867 mkdir -p "moved/sub module" &&
868 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
869 git config -f .gitmodules submodule.submodule2.path "moved/sub module"
870 git commit -am "post move" &&
871 git submodule update &&
872 git status | sed "s/$H2/XXX/" >actual &&
873 test_cmp expect actual
877 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
878 mkdir -p linked/dir &&
879 ln -s linked/dir linkto &&
881 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
883 git submodule update --init --recursive
888 test_expect_success 'submodule update clone shallow submodule' '
889 test_when_finished "rm -rf super3" &&
890 first=$(git -C cloned submodule status submodule |cut -c2-41) &&
891 second=$(git -C submodule rev-parse HEAD) &&
892 commit_count=$(git -C submodule rev-list --count $first^..$second) &&
893 git clone cloned super3 &&
897 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
898 mv -f .gitmodules.tmp .gitmodules &&
899 git submodule update --init --depth=$commit_count &&
900 test 1 = $(git -C submodule log --oneline | wc -l)
904 test_expect_success 'submodule update clone shallow submodule outside of depth' '
905 test_when_finished "rm -rf super3" &&
906 git clone cloned super3 &&
910 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
911 mv -f .gitmodules.tmp .gitmodules &&
912 test_must_fail git submodule update --init --depth=1 2>actual &&
913 test_i18ngrep "Direct fetching of that commit failed." actual &&
914 git -C ../submodule config uploadpack.allowReachableSHA1InWant true &&
915 git submodule update --init --depth=1 >actual &&
916 test 1 = $(git -C submodule log --oneline | wc -l)
920 test_expect_success 'submodule update --recursive drops module name before recursing' '
922 (cd deeper/submodule/subsubmodule &&
925 git submodule update --recursive deeper/submodule >actual &&
926 test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
930 test_expect_success 'submodule update can be run in parallel' '
932 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
933 grep "7 tasks" trace.out &&
934 git config submodule.fetchJobs 8 &&
935 GIT_TRACE=$(pwd)/trace.out git submodule update &&
936 grep "8 tasks" trace.out &&
937 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
938 grep "9 tasks" trace.out
942 test_expect_success 'git clone passes the parallel jobs config on to submodules' '
943 test_when_finished "rm -rf super4" &&
944 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
945 grep "7 tasks" trace.out &&
947 git config --global submodule.fetchJobs 8 &&
948 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
949 grep "8 tasks" trace.out &&
951 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
952 grep "9 tasks" trace.out &&