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 &&
68 ( cd 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 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'
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 sort actual2 >actual2.sorted &&
141 test_i18ncmp expect2 actual2.sorted
145 Submodule 'foo/sub' ($pwd/withsubs/../rebasing) registered for path 'sub'
148 test_expect_success 'submodule update --init from and of subdirectory' '
152 git submodule add "$(pwd)/../rebasing" foo/sub &&
154 git submodule deinit -f sub &&
155 git submodule update --init sub 2>../../actual2
158 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 ${SQ}submodule$SQ: checked out $SQ$head$SQ" > ../../expected &&
171 git reset --hard HEAD~1
174 git submodule update > ../actual 2> ../actual.err
176 test_i18ncmp expected actual &&
177 test_must_be_empty actual.err
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 &&
409 test_expect_success 'submodule update - command in .gitmodules is rejected' '
410 test_when_finished "git -C super reset --hard HEAD^" &&
411 git -C super config -f .gitmodules submodule.submodule.update "!false" &&
412 git -C super commit -a -m "add command to .gitmodules file" &&
413 git -C super/submodule reset --hard $submodulesha1^ &&
414 test_must_fail git -C super submodule update submodule
417 test_expect_success 'fsck detects command in .gitmodules' '
418 git init command-in-gitmodules &&
420 cd command-in-gitmodules &&
421 git submodule add ../submodule submodule &&
422 test_commit adding-submodule &&
424 git config -f .gitmodules submodule.submodule.update "!false" &&
425 git add .gitmodules &&
426 test_commit configuring-update &&
427 test_must_fail git fsck
432 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
435 test_expect_success 'submodule update - command in .git/config catches failure' '
437 git config submodule.submodule.update "!false"
439 (cd super/submodule &&
440 git reset --hard $submodulesha1^
443 test_must_fail git submodule update submodule 2>../actual
445 test_i18ncmp actual expect
449 Execution of 'false $submodulesha1' failed in submodule path '../submodule'
452 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
454 git config submodule.submodule.update "!false"
456 (cd super/submodule &&
457 git reset --hard $submodulesha1^
460 mkdir tmp && cd tmp &&
461 test_must_fail git submodule update ../submodule 2>../../actual
463 test_i18ncmp actual expect
466 test_expect_success 'submodule update - command run for initial population of submodule' '
467 cat >expect <<-EOF &&
468 Execution of '\''false $submodulesha1'\'' failed in submodule path '\''submodule'\''
470 rm -rf super/submodule &&
471 test_must_fail git -C super submodule update 2>actual &&
472 test_i18ncmp expect actual &&
473 git -C super submodule update --checkout
477 Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
478 Failed to recurse into submodule path '../super'
481 test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
482 (cd recursivesuper &&
483 git submodule update --remote super &&
485 git commit -m "update to latest to have more than one commit in submodules"
487 git -C recursivesuper/super config submodule.submodule.update "!false" &&
488 git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
489 (cd recursivesuper &&
490 mkdir -p tmp && cd tmp &&
491 test_must_fail git submodule update --recursive ../super 2>../../actual
493 test_i18ncmp actual expect
496 test_expect_success 'submodule init does not copy command into .git/config' '
497 test_when_finished "git -C super update-index --force-remove submodule1" &&
498 test_when_finished git config -f super/.gitmodules \
499 --remove-section submodule.submodule1 &&
501 git ls-files -s submodule >out &&
502 H=$(cut -d" " -f2 out) &&
504 git update-index --add --cacheinfo 160000 $H submodule1 &&
505 git config -f .gitmodules submodule.submodule1.path submodule1 &&
506 git config -f .gitmodules submodule.submodule1.url ../submodule &&
507 git config -f .gitmodules submodule.submodule1.update !false &&
508 test_must_fail git submodule init submodule1 &&
509 test_expect_code 1 git config submodule.submodule1.update >actual &&
510 test_must_be_empty actual
514 test_expect_success 'submodule init picks up rebase' '
516 git config -f .gitmodules submodule.rebasing.update rebase &&
517 git submodule init rebasing &&
518 test "rebase" = "$(git config submodule.rebasing.update)"
522 test_expect_success 'submodule init picks up merge' '
524 git config -f .gitmodules submodule.merging.update merge &&
525 git submodule init merging &&
526 test "merge" = "$(git config submodule.merging.update)"
530 test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
531 test_config -C super submodule.submodule.update checkout &&
534 git submodule update submodule &&
535 git status -s submodule >expect &&
537 git submodule update --merge submodule &&
538 git status -s submodule >actual &&
539 test_cmp expect actual
543 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
544 test_config -C super submodule.submodule.update checkout &&
547 git submodule update submodule &&
548 git status -s submodule >expect &&
550 git submodule update --rebase submodule &&
551 git status -s submodule >actual &&
552 test_cmp expect actual
556 test_expect_success 'submodule update ignores update=merge config for new submodules' '
559 git submodule update submodule &&
560 git status -s submodule >expect &&
562 git config submodule.submodule.update merge &&
563 git submodule update submodule &&
564 git status -s submodule >actual &&
565 git config --unset submodule.submodule.update &&
566 test_cmp expect actual
570 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
573 git submodule update submodule &&
574 git status -s submodule >expect &&
576 git config submodule.submodule.update rebase &&
577 git submodule update submodule &&
578 git status -s submodule >actual &&
579 git config --unset submodule.submodule.update &&
580 test_cmp expect actual
584 test_expect_success 'submodule init picks up update=none' '
586 git config -f .gitmodules submodule.none.update none &&
587 git submodule init none &&
588 test "none" = "$(git config submodule.none.update)"
592 test_expect_success 'submodule update - update=none in .git/config' '
594 git config submodule.submodule.update none &&
596 git checkout master &&
599 git diff --name-only >out &&
600 grep ^submodule$ out &&
601 git submodule update &&
602 git diff --name-only >out &&
603 grep ^submodule$ out &&
607 git config --unset submodule.submodule.update &&
608 git submodule update submodule
612 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
614 git config submodule.submodule.update none &&
616 git checkout master &&
619 git diff --name-only >out &&
620 grep ^submodule$ out &&
621 git submodule update --checkout &&
622 git diff --name-only >out &&
623 ! grep ^submodule$ out &&
627 git config --unset submodule.submodule.update
631 test_expect_success 'submodule update --init skips submodule with update=none' '
633 git add .gitmodules &&
634 git commit -m ".gitmodules"
636 git clone super cloned &&
638 git submodule update --init &&
639 test_path_exists submodule/.git &&
640 test_path_is_missing none/.git
644 test_expect_success 'submodule update continues after checkout error' '
646 git reset --hard HEAD &&
647 git submodule add ../submodule submodule2 &&
648 git submodule init &&
649 git commit -am "new_submodule" &&
651 git rev-parse --verify HEAD >../expect
654 test_commit "update_submodule" file
657 test_commit "update_submodule2" file
660 git add submodule2 &&
661 git commit -m "two_new_submodule_commits" &&
665 git checkout HEAD^ &&
666 test_must_fail git submodule update &&
668 git rev-parse --verify HEAD >../actual
670 test_cmp expect actual
673 test_expect_success 'submodule update continues after recursive checkout error' '
675 git reset --hard HEAD &&
676 git checkout master &&
677 git submodule update &&
679 git submodule add ../submodule subsubmodule &&
680 git submodule init &&
681 git commit -m "new_subsubmodule"
684 git commit -m "update_submodule" &&
687 test_commit "update_subsubmodule" file
689 git add subsubmodule &&
690 test_commit "update_submodule_again" file &&
692 test_commit "update_subsubmodule_again" file
694 test_commit "update_submodule_again_again" file
697 git rev-parse --verify HEAD >../expect &&
698 test_commit "update_submodule2_again" file
701 git add submodule2 &&
702 git commit -m "new_commits" &&
703 git checkout HEAD^ &&
705 git checkout HEAD^ &&
710 test_must_fail git submodule update --recursive &&
712 git rev-parse --verify HEAD >../actual
714 test_cmp expect actual
718 test_expect_success 'submodule update exit immediately in case of merge conflict' '
720 git checkout master &&
721 git reset --hard HEAD &&
724 git reset --hard HEAD
727 git submodule update --recursive &&
729 test_commit "update_submodule_2" file
732 test_commit "update_submodule2_2" file
735 git add submodule2 &&
736 git commit -m "two_new_submodule_commits" &&
738 git checkout master &&
739 test_commit "conflict" file &&
740 echo "conflict" > file
742 git checkout HEAD^ &&
744 git rev-parse --verify HEAD >../expect
746 git config submodule.submodule.update merge &&
747 test_must_fail git submodule update &&
749 git rev-parse --verify HEAD >../actual
751 test_cmp expect actual
755 test_expect_success 'submodule update exit immediately after recursive rebase error' '
757 git checkout master &&
758 git reset --hard HEAD &&
760 git reset --hard HEAD &&
761 git submodule update --recursive
764 test_commit "update_submodule_3" file
767 test_commit "update_submodule2_3" file
770 git add submodule2 &&
771 git commit -m "two_new_submodule_commits" &&
773 git checkout master &&
774 test_commit "conflict2" file &&
775 echo "conflict" > file
777 git checkout HEAD^ &&
779 git rev-parse --verify HEAD >../expect
781 git config submodule.submodule.update rebase &&
782 test_must_fail git submodule update &&
784 git rev-parse --verify HEAD >../actual
786 test_cmp expect actual
790 test_expect_success 'add different submodules to the same path' '
792 git submodule add ../submodule s1 &&
793 test_must_fail git submodule add ../merging s1
797 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
800 git submodule add ../submodule deeper/submodule &&
801 (cd deeper/submodule &&
802 git log > ../../expected
804 (cd .git/modules/deeper/submodule &&
805 git log > ../../../../actual
807 test_cmp expected actual
811 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
813 git commit -m "added submodule"
815 git clone super super2 &&
817 git submodule init deeper/submodule &&
818 git submodule update &&
819 (cd deeper/submodule &&
820 git log > ../../expected
822 (cd .git/modules/deeper/submodule &&
823 git log > ../../../../actual
825 test_cmp expected actual
829 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
831 (cd deeper/submodule &&
832 git submodule add ../submodule subsubmodule &&
834 git log > ../../../expected
836 git commit -m "added subsubmodule" &&
839 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
840 git log > ../../../../../actual
842 git add deeper/submodule &&
843 git commit -m "update submodule" &&
845 test_cmp expected actual
849 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
850 mkdir super_update_r &&
851 (cd super_update_r &&
854 mkdir subsuper_update_r &&
855 (cd subsuper_update_r &&
858 mkdir subsubsuper_update_r &&
859 (cd subsubsuper_update_r &&
862 git clone subsubsuper_update_r subsubsuper_update_r2 &&
863 (cd subsubsuper_update_r2 &&
864 test_commit "update_subsubsuper" file &&
865 git push origin master
867 git clone subsuper_update_r subsuper_update_r2 &&
868 (cd subsuper_update_r2 &&
869 test_commit "update_subsuper" file &&
870 git submodule add ../subsubsuper_update_r subsubmodule &&
871 git commit -am "subsubmodule" &&
872 git push origin master
874 git clone super_update_r super_update_r2 &&
875 (cd super_update_r2 &&
876 test_commit "update_super" file &&
877 git submodule add ../subsuper_update_r submodule &&
878 git commit -am "submodule" &&
879 git push origin master
881 rm -rf super_update_r2 &&
882 git clone super_update_r super_update_r2 &&
883 (cd super_update_r2 &&
884 git submodule update --init --recursive >actual &&
885 test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
886 (cd submodule/subsubmodule &&
887 git log > ../../expected
889 (cd .git/modules/submodule/modules/subsubmodule &&
890 git log > ../../../../../actual
892 test_cmp expected actual
896 test_expect_success 'submodule add properly re-creates deeper level submodules' '
898 git reset --hard master &&
900 git submodule add --force ../submodule deeper/submodule
904 test_expect_success 'submodule update properly revives a moved submodule' '
906 H=$(git rev-parse --short HEAD) &&
907 git commit -am "pre move" &&
908 H2=$(git rev-parse --short HEAD) &&
910 sed "s/$H/XXX/" out >expect &&
911 H=$(cd submodule2 && git rev-parse HEAD) &&
912 git rm --cached submodule2 &&
914 mkdir -p "moved/sub module" &&
915 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
916 git config -f .gitmodules submodule.submodule2.path "moved/sub module" &&
917 git commit -am "post move" &&
918 git submodule update &&
920 sed "s/$H2/XXX/" out >actual &&
921 test_cmp expect actual
925 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
926 mkdir -p linked/dir &&
927 ln -s linked/dir linkto &&
929 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
931 git submodule update --init --recursive
936 test_expect_success 'submodule update clone shallow submodule' '
937 test_when_finished "rm -rf super3" &&
938 first=$(git -C cloned rev-parse HEAD:submodule) &&
939 second=$(git -C submodule rev-parse HEAD) &&
940 commit_count=$(git -C submodule rev-list --count $first^..$second) &&
941 git clone cloned super3 &&
945 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
946 mv -f .gitmodules.tmp .gitmodules &&
947 git submodule update --init --depth=$commit_count &&
948 git -C submodule log --oneline >out &&
949 test_line_count = 1 out
953 test_expect_success 'submodule update clone shallow submodule outside of depth' '
954 test_when_finished "rm -rf super3" &&
955 git clone cloned super3 &&
959 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
960 mv -f .gitmodules.tmp .gitmodules &&
961 # Some protocol versions (e.g. 2) support fetching
962 # unadvertised objects, so restrict this test to v0.
963 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
964 git submodule update --init --depth=1 2>actual &&
965 test_i18ngrep "Direct fetching of that commit failed." actual &&
966 git -C ../submodule config uploadpack.allowReachableSHA1InWant true &&
967 git submodule update --init --depth=1 >actual &&
968 git -C submodule log --oneline >out &&
969 test_line_count = 1 out
973 test_expect_success 'submodule update --recursive drops module name before recursing' '
975 (cd deeper/submodule/subsubmodule &&
978 git submodule update --recursive deeper/submodule >actual &&
979 test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
983 test_expect_success 'submodule update can be run in parallel' '
985 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
986 grep "7 tasks" trace.out &&
987 git config submodule.fetchJobs 8 &&
988 GIT_TRACE=$(pwd)/trace.out git submodule update &&
989 grep "8 tasks" trace.out &&
990 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
991 grep "9 tasks" trace.out
995 test_expect_success 'git clone passes the parallel jobs config on to submodules' '
996 test_when_finished "rm -rf super4" &&
997 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
998 grep "7 tasks" trace.out &&
1000 git config --global submodule.fetchJobs 8 &&
1001 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
1002 grep "8 tasks" trace.out &&
1004 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
1005 grep "9 tasks" trace.out &&