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 Submodule 'foo/sub' ($pwd/withsubs/../rebasing) registered for path 'sub'
147 test_expect_success 'submodule update --init from and of subdirectory' '
151 git submodule add "$(pwd)/../rebasing" foo/sub &&
153 git submodule deinit -f sub &&
154 git submodule update --init sub 2>../../actual2
157 test_i18ncmp expect2 actual2
161 test_expect_success 'submodule update does not fetch already present commits' '
163 echo line3 >> file &&
166 git commit -m "upstream line3"
168 (cd super/submodule &&
169 head=$(git rev-parse --verify HEAD) &&
170 echo "Submodule path ${apos}submodule$apos: checked out $apos$head$apos" > ../../expected &&
171 git reset --hard HEAD~1
174 git submodule update > ../actual 2> ../actual.err
176 test_i18ncmp expected actual &&
180 test_expect_success 'submodule update should fail due to local changes' '
181 (cd super/submodule &&
182 git reset --hard HEAD~1 &&
183 echo "local change" > file
189 test_must_fail git submodule update submodule
192 test_expect_success 'submodule update should throw away changes with --force ' '
197 git submodule update --force submodule &&
203 test_expect_success 'submodule update --force forcibly checks out submodules' '
208 git submodule update --force submodule &&
210 test "$(git status -s file)" = ""
215 test_expect_success 'submodule update --remote should fetch upstream changes' '
217 echo line4 >> file &&
220 git commit -m "upstream line4"
223 git submodule update --remote --force submodule &&
225 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
229 test_expect_success 'submodule update --remote should fetch upstream changes with .' '
232 git config -f .gitmodules submodule."submodule".branch "." &&
233 git add .gitmodules &&
234 git commit -m "submodules: update from the respective superproject branch"
238 echo line4a >> file &&
241 git commit -m "upstream line4a" &&
242 git checkout -b test-branch &&
243 test_commit on-test-branch
247 git submodule update --remote --force submodule &&
248 git -C submodule log -1 --oneline >actual
249 git -C ../submodule log -1 --oneline master >expect
250 test_cmp expect actual &&
251 git checkout -b test-branch &&
252 git submodule update --remote --force submodule &&
253 git -C submodule log -1 --oneline >actual
254 git -C ../submodule log -1 --oneline test-branch >expect
255 test_cmp expect actual &&
256 git checkout master &&
257 git branch -d test-branch &&
258 git reset --hard HEAD^
262 test_expect_success 'local config should override .gitmodules branch' '
264 git checkout test-branch &&
265 echo line5 >> file &&
268 git commit -m "upstream line5" &&
272 git config submodule.submodule.branch test-branch &&
273 git submodule update --remote --force submodule &&
275 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
279 test_expect_success 'submodule update --rebase staying on master' '
280 (cd super/submodule &&
287 git submodule update --rebase submodule &&
293 test_expect_success 'submodule update --merge staying on master' '
294 (cd super/submodule &&
295 git reset --hard HEAD~1
301 git submodule update --merge submodule &&
307 test_expect_success 'submodule update - rebase in .git/config' '
309 git config submodule.submodule.update rebase
311 (cd super/submodule &&
312 git reset --hard HEAD~1
318 git submodule update submodule &&
324 test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
326 git config submodule.submodule.update checkout
328 (cd super/submodule &&
329 git reset --hard HEAD~1
335 git submodule update --rebase submodule &&
341 test_expect_success 'submodule update - merge in .git/config' '
343 git config submodule.submodule.update merge
345 (cd super/submodule &&
346 git reset --hard HEAD~1
352 git submodule update submodule &&
358 test_expect_success 'submodule update - checkout in .git/config but --merge given' '
360 git config submodule.submodule.update checkout
362 (cd super/submodule &&
363 git reset --hard HEAD~1
369 git submodule update --merge submodule &&
375 test_expect_success 'submodule update - checkout in .git/config' '
377 git config submodule.submodule.update checkout
379 (cd super/submodule &&
380 git reset --hard HEAD^
386 git submodule update submodule &&
392 test_expect_success 'submodule update - command in .git/config' '
394 git config submodule.submodule.update "!git checkout"
396 (cd super/submodule &&
397 git reset --hard HEAD^
403 git submodule update submodule &&
410 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
413 test_expect_success 'submodule update - command in .git/config catches failure' '
415 git config submodule.submodule.update "!false"
417 (cd super/submodule &&
418 git reset --hard $submodulesha1^
421 test_must_fail git submodule update submodule 2>../actual
423 test_i18ncmp actual expect
427 Execution of 'false $submodulesha1' failed in submodule path '../submodule'
430 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
432 git config submodule.submodule.update "!false"
434 (cd super/submodule &&
435 git reset --hard $submodulesha1^
438 mkdir tmp && cd tmp &&
439 test_must_fail git submodule update ../submodule 2>../../actual
441 test_i18ncmp actual expect
445 Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
446 Failed to recurse into submodule path '../super'
449 test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
450 (cd recursivesuper &&
451 git submodule update --remote super &&
453 git commit -m "update to latest to have more than one commit in submodules"
455 git -C recursivesuper/super config submodule.submodule.update "!false" &&
456 git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
457 (cd recursivesuper &&
458 mkdir -p tmp && cd tmp &&
459 test_must_fail git submodule update --recursive ../super 2>../../actual
461 test_i18ncmp actual expect
464 test_expect_success 'submodule init does not copy command into .git/config' '
466 H=$(git ls-files -s submodule | cut -d" " -f2) &&
468 git update-index --add --cacheinfo 160000 $H submodule1 &&
469 git config -f .gitmodules submodule.submodule1.path submodule1 &&
470 git config -f .gitmodules submodule.submodule1.url ../submodule &&
471 git config -f .gitmodules submodule.submodule1.update !false &&
472 git submodule init submodule1 &&
473 echo "none" >expect &&
474 git config submodule.submodule1.update >actual &&
475 test_cmp expect actual
479 test_expect_success 'submodule init picks up rebase' '
481 git config -f .gitmodules submodule.rebasing.update rebase &&
482 git submodule init rebasing &&
483 test "rebase" = "$(git config submodule.rebasing.update)"
487 test_expect_success 'submodule init picks up merge' '
489 git config -f .gitmodules submodule.merging.update merge &&
490 git submodule init merging &&
491 test "merge" = "$(git config submodule.merging.update)"
495 test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
498 git submodule update submodule &&
499 git status -s submodule >expect &&
501 git submodule update --merge submodule &&
502 git status -s submodule >actual &&
503 test_cmp expect actual
507 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
510 git submodule update submodule &&
511 git status -s submodule >expect &&
513 git submodule update --rebase submodule &&
514 git status -s submodule >actual &&
515 test_cmp expect actual
519 test_expect_success 'submodule update ignores update=merge config for new submodules' '
522 git submodule update submodule &&
523 git status -s submodule >expect &&
525 git config submodule.submodule.update merge &&
526 git submodule update submodule &&
527 git status -s submodule >actual &&
528 git config --unset submodule.submodule.update &&
529 test_cmp expect actual
533 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
536 git submodule update submodule &&
537 git status -s submodule >expect &&
539 git config submodule.submodule.update rebase &&
540 git submodule update submodule &&
541 git status -s submodule >actual &&
542 git config --unset submodule.submodule.update &&
543 test_cmp expect actual
547 test_expect_success 'submodule init picks up update=none' '
549 git config -f .gitmodules submodule.none.update none &&
550 git submodule init none &&
551 test "none" = "$(git config submodule.none.update)"
555 test_expect_success 'submodule update - update=none in .git/config' '
557 git config submodule.submodule.update none &&
559 git checkout master &&
562 git diff --raw | grep " submodule" &&
563 git submodule update &&
564 git diff --raw | grep " submodule" &&
568 git config --unset submodule.submodule.update &&
569 git submodule update submodule
573 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
575 git config submodule.submodule.update none &&
577 git checkout master &&
580 git diff --raw | grep " submodule" &&
581 git submodule update --checkout &&
582 test_must_fail git diff --raw \| grep " submodule" &&
584 test_must_fail compare_head
586 git config --unset submodule.submodule.update
590 test_expect_success 'submodule update --init skips submodule with update=none' '
592 git add .gitmodules &&
593 git commit -m ".gitmodules"
595 git clone super cloned &&
597 git submodule update --init &&
598 test -e submodule/.git &&
599 test_must_fail test -e none/.git
603 test_expect_success 'submodule update continues after checkout error' '
605 git reset --hard HEAD &&
606 git submodule add ../submodule submodule2 &&
607 git submodule init &&
608 git commit -am "new_submodule" &&
610 git rev-parse --verify HEAD >../expect
613 test_commit "update_submodule" file
616 test_commit "update_submodule2" file
619 git add submodule2 &&
620 git commit -m "two_new_submodule_commits" &&
624 git checkout HEAD^ &&
625 test_must_fail git submodule update &&
627 git rev-parse --verify HEAD >../actual
629 test_cmp expect actual
632 test_expect_success 'submodule update continues after recursive checkout error' '
634 git reset --hard HEAD &&
635 git checkout master &&
636 git submodule update &&
638 git submodule add ../submodule subsubmodule &&
639 git submodule init &&
640 git commit -m "new_subsubmodule"
643 git commit -m "update_submodule" &&
646 test_commit "update_subsubmodule" file
648 git add subsubmodule &&
649 test_commit "update_submodule_again" file &&
651 test_commit "update_subsubmodule_again" file
653 test_commit "update_submodule_again_again" file
656 git rev-parse --verify HEAD >../expect &&
657 test_commit "update_submodule2_again" file
660 git add submodule2 &&
661 git commit -m "new_commits" &&
662 git checkout HEAD^ &&
664 git checkout HEAD^ &&
669 test_must_fail git submodule update --recursive &&
671 git rev-parse --verify HEAD >../actual
673 test_cmp expect actual
677 test_expect_success 'submodule update exit immediately in case of merge conflict' '
679 git checkout master &&
680 git reset --hard HEAD &&
683 git reset --hard HEAD
686 git submodule update --recursive &&
688 test_commit "update_submodule_2" file
691 test_commit "update_submodule2_2" file
694 git add submodule2 &&
695 git commit -m "two_new_submodule_commits" &&
697 git checkout master &&
698 test_commit "conflict" file &&
699 echo "conflict" > file
701 git checkout HEAD^ &&
703 git rev-parse --verify HEAD >../expect
705 git config submodule.submodule.update merge &&
706 test_must_fail git submodule update &&
708 git rev-parse --verify HEAD >../actual
710 test_cmp expect actual
714 test_expect_success 'submodule update exit immediately after recursive rebase error' '
716 git checkout master &&
717 git reset --hard HEAD &&
719 git reset --hard HEAD &&
720 git submodule update --recursive
723 test_commit "update_submodule_3" file
726 test_commit "update_submodule2_3" file
729 git add submodule2 &&
730 git commit -m "two_new_submodule_commits" &&
732 git checkout master &&
733 test_commit "conflict2" file &&
734 echo "conflict" > file
736 git checkout HEAD^ &&
738 git rev-parse --verify HEAD >../expect
740 git config submodule.submodule.update rebase &&
741 test_must_fail git submodule update &&
743 git rev-parse --verify HEAD >../actual
745 test_cmp expect actual
749 test_expect_success 'add different submodules to the same path' '
751 git submodule add ../submodule s1 &&
752 test_must_fail git submodule add ../merging s1
756 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
759 git submodule add ../submodule deeper/submodule &&
760 (cd deeper/submodule &&
761 git log > ../../expected
763 (cd .git/modules/deeper/submodule &&
764 git log > ../../../../actual
766 test_cmp actual expected
770 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
772 git commit -m "added submodule"
774 git clone super super2 &&
776 git submodule init deeper/submodule &&
777 git submodule update &&
778 (cd deeper/submodule &&
779 git log > ../../expected
781 (cd .git/modules/deeper/submodule &&
782 git log > ../../../../actual
784 test_cmp actual expected
788 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
790 (cd deeper/submodule &&
791 git submodule add ../submodule subsubmodule &&
793 git log > ../../../expected
795 git commit -m "added subsubmodule" &&
798 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
799 git log > ../../../../../actual
801 git add deeper/submodule &&
802 git commit -m "update submodule" &&
804 test_cmp actual expected
808 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
809 mkdir super_update_r &&
810 (cd super_update_r &&
813 mkdir subsuper_update_r &&
814 (cd subsuper_update_r &&
817 mkdir subsubsuper_update_r &&
818 (cd subsubsuper_update_r &&
821 git clone subsubsuper_update_r subsubsuper_update_r2 &&
822 (cd subsubsuper_update_r2 &&
823 test_commit "update_subsubsuper" file &&
824 git push origin master
826 git clone subsuper_update_r subsuper_update_r2 &&
827 (cd subsuper_update_r2 &&
828 test_commit "update_subsuper" file &&
829 git submodule add ../subsubsuper_update_r subsubmodule &&
830 git commit -am "subsubmodule" &&
831 git push origin master
833 git clone super_update_r super_update_r2 &&
834 (cd super_update_r2 &&
835 test_commit "update_super" file &&
836 git submodule add ../subsuper_update_r submodule &&
837 git commit -am "submodule" &&
838 git push origin master
840 rm -rf super_update_r2 &&
841 git clone super_update_r super_update_r2 &&
842 (cd super_update_r2 &&
843 git submodule update --init --recursive >actual &&
844 test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
845 (cd submodule/subsubmodule &&
846 git log > ../../expected
848 (cd .git/modules/submodule/modules/subsubmodule
849 git log > ../../../../../actual
851 test_cmp actual expected
855 test_expect_success 'submodule add properly re-creates deeper level submodules' '
857 git reset --hard master &&
859 git submodule add --force ../submodule deeper/submodule
863 test_expect_success 'submodule update properly revives a moved submodule' '
865 H=$(git rev-parse --short HEAD) &&
866 git commit -am "pre move" &&
867 H2=$(git rev-parse --short HEAD) &&
868 git status | sed "s/$H/XXX/" >expect &&
869 H=$(cd submodule2; git rev-parse HEAD) &&
870 git rm --cached submodule2 &&
872 mkdir -p "moved/sub module" &&
873 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
874 git config -f .gitmodules submodule.submodule2.path "moved/sub module"
875 git commit -am "post move" &&
876 git submodule update &&
877 git status | sed "s/$H2/XXX/" >actual &&
878 test_cmp expect actual
882 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
883 mkdir -p linked/dir &&
884 ln -s linked/dir linkto &&
886 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
888 git submodule update --init --recursive
893 test_expect_success 'submodule update clone shallow submodule' '
894 test_when_finished "rm -rf super3" &&
895 first=$(git -C cloned submodule status submodule |cut -c2-41) &&
896 second=$(git -C submodule rev-parse HEAD) &&
897 commit_count=$(git -C submodule rev-list --count $first^..$second) &&
898 git clone cloned super3 &&
902 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
903 mv -f .gitmodules.tmp .gitmodules &&
904 git submodule update --init --depth=$commit_count &&
905 test 1 = $(git -C submodule log --oneline | wc -l)
909 test_expect_success 'submodule update clone shallow submodule outside of depth' '
910 test_when_finished "rm -rf super3" &&
911 git clone cloned super3 &&
915 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
916 mv -f .gitmodules.tmp .gitmodules &&
917 test_must_fail git submodule update --init --depth=1 2>actual &&
918 test_i18ngrep "Direct fetching of that commit failed." actual &&
919 git -C ../submodule config uploadpack.allowReachableSHA1InWant true &&
920 git submodule update --init --depth=1 >actual &&
921 test 1 = $(git -C submodule log --oneline | wc -l)
925 test_expect_success 'submodule update --recursive drops module name before recursing' '
927 (cd deeper/submodule/subsubmodule &&
930 git submodule update --recursive deeper/submodule >actual &&
931 test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
935 test_expect_success 'submodule update can be run in parallel' '
937 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
938 grep "7 tasks" trace.out &&
939 git config submodule.fetchJobs 8 &&
940 GIT_TRACE=$(pwd)/trace.out git submodule update &&
941 grep "8 tasks" trace.out &&
942 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
943 grep "9 tasks" trace.out
947 test_expect_success 'git clone passes the parallel jobs config on to submodules' '
948 test_when_finished "rm -rf super4" &&
949 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
950 grep "7 tasks" trace.out &&
952 git config --global submodule.fetchJobs 8 &&
953 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
954 grep "8 tasks" trace.out &&
956 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
957 grep "9 tasks" trace.out &&