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.
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
20 sha_main=$(git rev-list --max-count=1 main)
21 sha_head=$(git rev-list --max-count=1 HEAD)
23 test "$sha_main" = "$sha_head"
27 test_expect_success 'setup a submodule tree' '
31 git commit -m upstream &&
33 git clone super submodule &&
34 git clone super rebasing &&
35 git clone super merging &&
36 git clone super none &&
38 git submodule add ../submodule submodule &&
40 git commit -m "submodule" &&
41 git submodule init submodule
44 echo "line2" > file &&
46 git commit -m "Commit 2"
50 git pull --rebase origin
53 git commit -m "submodule update"
56 git submodule add ../rebasing rebasing &&
58 git commit -m "rebasing"
61 git submodule add ../merging merging &&
63 git commit -m "rebasing"
66 git submodule add ../none none &&
70 git clone . recursivesuper &&
71 ( cd recursivesuper &&
72 git submodule add ../super super
76 test_expect_success 'update --remote falls back to using HEAD' '
77 test_create_repo main-branch-submodule &&
78 test_commit -C main-branch-submodule initial &&
80 test_create_repo main-branch &&
81 git -C main-branch submodule add ../main-branch-submodule &&
82 git -C main-branch commit -m add-submodule &&
84 git -C main-branch-submodule switch -c hello &&
85 test_commit -C main-branch-submodule world &&
87 git clone --recursive main-branch main-branch-clone &&
88 git -C main-branch-clone submodule update --remote main-branch-submodule &&
89 test_path_exists main-branch-clone/main-branch-submodule/world.t
92 test_expect_success 'submodule update detaching the HEAD ' '
93 (cd super/submodule &&
94 git reset --hard HEAD~1
100 git submodule update submodule &&
106 test_expect_success 'submodule update from subdirectory' '
107 (cd super/submodule &&
108 git reset --hard HEAD~1
115 git submodule update ../submodule &&
121 supersha1=$(git -C super rev-parse HEAD)
122 mergingsha1=$(git -C super/merging rev-parse HEAD)
123 nonesha1=$(git -C super/none rev-parse HEAD)
124 rebasingsha1=$(git -C super/rebasing rev-parse HEAD)
125 submodulesha1=$(git -C super/submodule rev-parse HEAD)
129 Submodule path '../super': checked out '$supersha1'
130 Submodule path '../super/merging': checked out '$mergingsha1'
131 Submodule path '../super/none': checked out '$nonesha1'
132 Submodule path '../super/rebasing': checked out '$rebasingsha1'
133 Submodule path '../super/submodule': checked out '$submodulesha1'
137 Cloning into '$pwd/recursivesuper/super/merging'...
138 Cloning into '$pwd/recursivesuper/super/none'...
139 Cloning into '$pwd/recursivesuper/super/rebasing'...
140 Cloning into '$pwd/recursivesuper/super/submodule'...
141 Submodule 'merging' ($pwd/merging) registered for path '../super/merging'
142 Submodule 'none' ($pwd/none) registered for path '../super/none'
143 Submodule 'rebasing' ($pwd/rebasing) registered for path '../super/rebasing'
144 Submodule 'submodule' ($pwd/submodule) registered for path '../super/submodule'
151 test_expect_success 'submodule update --init --recursive from subdirectory' '
152 git -C recursivesuper/super reset --hard HEAD^ &&
153 (cd recursivesuper &&
156 git submodule update --init --recursive ../super >../../actual 2>../../actual2
158 test_cmp expect actual &&
159 sort actual2 >actual2.sorted &&
160 test_cmp expect2 actual2.sorted
164 Submodule 'foo/sub' ($pwd/withsubs/../rebasing) registered for path 'sub'
167 test_expect_success 'submodule update --init from and of subdirectory' '
171 git submodule add "$(pwd)/../rebasing" foo/sub &&
173 git submodule deinit -f sub &&
174 git submodule update --init sub 2>../../actual2
177 test_cmp expect2 actual2
180 test_expect_success 'submodule update does not fetch already present commits' '
182 echo line3 >> file &&
185 git commit -m "upstream line3"
187 (cd super/submodule &&
188 head=$(git rev-parse --verify HEAD) &&
189 echo "Submodule path ${SQ}submodule$SQ: checked out $SQ$head$SQ" > ../../expected &&
190 git reset --hard HEAD~1
193 git submodule update > ../actual 2> ../actual.err
195 test_cmp expected actual &&
196 test_must_be_empty actual.err
199 test_expect_success 'submodule update should fail due to local changes' '
200 (cd super/submodule &&
201 git reset --hard HEAD~1 &&
202 echo "local change" > file
208 test_must_fail git submodule update submodule
211 test_expect_success 'submodule update should throw away changes with --force ' '
216 git submodule update --force submodule &&
222 test_expect_success 'submodule update --force forcibly checks out submodules' '
227 git submodule update --force submodule &&
229 test "$(git status -s file)" = ""
234 test_expect_success 'submodule update --remote should fetch upstream changes' '
236 echo line4 >> file &&
239 git commit -m "upstream line4"
242 git submodule update --remote --force submodule &&
244 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
248 test_expect_success 'submodule update --remote should fetch upstream changes with .' '
251 git config -f .gitmodules submodule."submodule".branch "." &&
252 git add .gitmodules &&
253 git commit -m "submodules: update from the respective superproject branch"
257 echo line4a >> file &&
260 git commit -m "upstream line4a" &&
261 git checkout -b test-branch &&
262 test_commit on-test-branch
266 git submodule update --remote --force submodule &&
267 git -C submodule log -1 --oneline >actual &&
268 git -C ../submodule log -1 --oneline main >expect &&
269 test_cmp expect actual &&
270 git checkout -b test-branch &&
271 git submodule update --remote --force submodule &&
272 git -C submodule log -1 --oneline >actual &&
273 git -C ../submodule log -1 --oneline test-branch >expect &&
274 test_cmp expect actual &&
276 git branch -d test-branch &&
277 git reset --hard HEAD^
281 test_expect_success 'local config should override .gitmodules branch' '
283 git checkout test-branch &&
284 echo line5 >> file &&
287 git commit -m "upstream line5" &&
291 git config submodule.submodule.branch test-branch &&
292 git submodule update --remote --force submodule &&
294 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
298 test_expect_success 'submodule update --rebase staying on main' '
299 (cd super/submodule &&
306 git submodule update --rebase submodule &&
312 test_expect_success 'submodule update --merge staying on main' '
313 (cd super/submodule &&
314 git reset --hard HEAD~1
320 git submodule update --merge submodule &&
326 test_expect_success 'submodule update - rebase in .git/config' '
328 git config submodule.submodule.update rebase
330 (cd super/submodule &&
331 git reset --hard HEAD~1
337 git submodule update submodule &&
343 test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
345 git config submodule.submodule.update checkout
347 (cd super/submodule &&
348 git reset --hard HEAD~1
354 git submodule update --rebase submodule &&
360 test_expect_success 'submodule update - merge in .git/config' '
362 git config submodule.submodule.update merge
364 (cd super/submodule &&
365 git reset --hard HEAD~1
371 git submodule update submodule &&
377 test_expect_success 'submodule update - checkout in .git/config but --merge given' '
379 git config submodule.submodule.update checkout
381 (cd super/submodule &&
382 git reset --hard HEAD~1
388 git submodule update --merge submodule &&
394 test_expect_success 'submodule update - checkout in .git/config' '
396 git config submodule.submodule.update checkout
398 (cd super/submodule &&
399 git reset --hard HEAD^
405 git submodule update submodule &&
411 test_expect_success 'submodule update - command in .git/config' '
413 git config submodule.submodule.update "!git checkout"
415 (cd super/submodule &&
416 git reset --hard HEAD^
422 git submodule update submodule &&
428 test_expect_success 'submodule update - command in .gitmodules is rejected' '
429 test_when_finished "git -C super reset --hard HEAD^" &&
430 git -C super config -f .gitmodules submodule.submodule.update "!false" &&
431 git -C super commit -a -m "add command to .gitmodules file" &&
432 git -C super/submodule reset --hard $submodulesha1^ &&
433 test_must_fail git -C super submodule update submodule
436 test_expect_success 'fsck detects command in .gitmodules' '
437 git init command-in-gitmodules &&
439 cd command-in-gitmodules &&
440 git submodule add ../submodule submodule &&
441 test_commit adding-submodule &&
443 git config -f .gitmodules submodule.submodule.update "!false" &&
444 git add .gitmodules &&
445 test_commit configuring-update &&
446 test_must_fail git fsck
451 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
454 test_expect_success 'submodule update - command in .git/config catches failure' '
456 git config submodule.submodule.update "!false"
458 (cd super/submodule &&
459 git reset --hard $submodulesha1^
462 test_must_fail git submodule update submodule 2>../actual
464 test_cmp actual expect
468 Execution of 'false $submodulesha1' failed in submodule path '../submodule'
471 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
473 git config submodule.submodule.update "!false"
475 (cd super/submodule &&
476 git reset --hard $submodulesha1^
479 mkdir tmp && cd tmp &&
480 test_must_fail git submodule update ../submodule 2>../../actual
482 test_cmp actual expect
485 test_expect_success 'submodule update - command run for initial population of submodule' '
486 cat >expect <<-EOF &&
487 Execution of '\''false $submodulesha1'\'' failed in submodule path '\''submodule'\''
489 rm -rf super/submodule &&
490 test_must_fail git -C super submodule update 2>actual &&
491 test_cmp expect actual &&
492 git -C super submodule update --checkout
496 Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
497 Failed to recurse into submodule path '../super'
500 test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
501 (cd recursivesuper &&
502 git submodule update --remote super &&
504 git commit -m "update to latest to have more than one commit in submodules"
506 git -C recursivesuper/super config submodule.submodule.update "!false" &&
507 git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
508 (cd recursivesuper &&
509 mkdir -p tmp && cd tmp &&
510 test_must_fail git submodule update --recursive ../super 2>../../actual
512 test_cmp actual expect
515 test_expect_success 'submodule init does not copy command into .git/config' '
516 test_when_finished "git -C super update-index --force-remove submodule1" &&
517 test_when_finished git config -f super/.gitmodules \
518 --remove-section submodule.submodule1 &&
520 git ls-files -s submodule >out &&
521 H=$(cut -d" " -f2 out) &&
523 git update-index --add --cacheinfo 160000 $H submodule1 &&
524 git config -f .gitmodules submodule.submodule1.path submodule1 &&
525 git config -f .gitmodules submodule.submodule1.url ../submodule &&
526 git config -f .gitmodules submodule.submodule1.update !false &&
527 test_must_fail git submodule init submodule1 &&
528 test_expect_code 1 git config submodule.submodule1.update >actual &&
529 test_must_be_empty actual
533 test_expect_success 'submodule init picks up rebase' '
535 git config -f .gitmodules submodule.rebasing.update rebase &&
536 git submodule init rebasing &&
537 test "rebase" = "$(git config submodule.rebasing.update)"
541 test_expect_success 'submodule init picks up merge' '
543 git config -f .gitmodules submodule.merging.update merge &&
544 git submodule init merging &&
545 test "merge" = "$(git config submodule.merging.update)"
549 test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
550 test_config -C super submodule.submodule.update checkout &&
553 git submodule update submodule &&
554 git status -s submodule >expect &&
556 git submodule update --merge submodule &&
557 git status -s submodule >actual &&
558 test_cmp expect actual
562 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
563 test_config -C super submodule.submodule.update checkout &&
566 git submodule update submodule &&
567 git status -s submodule >expect &&
569 git submodule update --rebase submodule &&
570 git status -s submodule >actual &&
571 test_cmp expect actual
575 test_expect_success 'submodule update ignores update=merge config for new submodules' '
578 git submodule update submodule &&
579 git status -s submodule >expect &&
581 git config submodule.submodule.update merge &&
582 git submodule update submodule &&
583 git status -s submodule >actual &&
584 git config --unset submodule.submodule.update &&
585 test_cmp expect actual
589 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
592 git submodule update submodule &&
593 git status -s submodule >expect &&
595 git config submodule.submodule.update rebase &&
596 git submodule update submodule &&
597 git status -s submodule >actual &&
598 git config --unset submodule.submodule.update &&
599 test_cmp expect actual
603 test_expect_success 'submodule init picks up update=none' '
605 git config -f .gitmodules submodule.none.update none &&
606 git submodule init none &&
607 test "none" = "$(git config submodule.none.update)"
611 test_expect_success 'submodule update - update=none in .git/config' '
613 git config submodule.submodule.update none &&
618 git diff --name-only >out &&
619 grep ^submodule$ out &&
620 git submodule update &&
621 git diff --name-only >out &&
622 grep ^submodule$ out &&
626 git config --unset submodule.submodule.update &&
627 git submodule update submodule
631 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
633 git config submodule.submodule.update none &&
638 git diff --name-only >out &&
639 grep ^submodule$ out &&
640 git submodule update --checkout &&
641 git diff --name-only >out &&
642 ! grep ^submodule$ out &&
646 git config --unset submodule.submodule.update
650 test_expect_success 'submodule update --init skips submodule with update=none' '
652 git add .gitmodules &&
653 git commit -m ".gitmodules"
655 git clone super cloned &&
657 git submodule update --init &&
658 test_path_exists submodule/.git &&
659 test_path_is_missing none/.git
663 test_expect_success 'submodule update continues after checkout error' '
665 git reset --hard HEAD &&
666 git submodule add ../submodule submodule2 &&
667 git submodule init &&
668 git commit -am "new_submodule" &&
670 git rev-parse --verify HEAD >../expect
673 test_commit "update_submodule" file
676 test_commit "update_submodule2" file
679 git add submodule2 &&
680 git commit -m "two_new_submodule_commits" &&
684 git checkout HEAD^ &&
685 test_must_fail git submodule update &&
687 git rev-parse --verify HEAD >../actual
689 test_cmp expect actual
692 test_expect_success 'submodule update continues after recursive checkout error' '
694 git reset --hard HEAD &&
696 git submodule update &&
698 git submodule add ../submodule subsubmodule &&
699 git submodule init &&
700 git commit -m "new_subsubmodule"
703 git commit -m "update_submodule" &&
706 test_commit "update_subsubmodule" file
708 git add subsubmodule &&
709 test_commit "update_submodule_again" file &&
711 test_commit "update_subsubmodule_again" file
713 test_commit "update_submodule_again_again" file
716 git rev-parse --verify HEAD >../expect &&
717 test_commit "update_submodule2_again" file
720 git add submodule2 &&
721 git commit -m "new_commits" &&
722 git checkout HEAD^ &&
724 git checkout HEAD^ &&
729 test_must_fail git submodule update --recursive &&
731 git rev-parse --verify HEAD >../actual
733 test_cmp expect actual
737 test_expect_success 'submodule update exit immediately in case of merge conflict' '
740 git reset --hard HEAD &&
743 git reset --hard HEAD
746 git submodule update --recursive &&
748 test_commit "update_submodule_2" file
751 test_commit "update_submodule2_2" file
754 git add submodule2 &&
755 git commit -m "two_new_submodule_commits" &&
758 test_commit "conflict" file &&
759 echo "conflict" > file
761 git checkout HEAD^ &&
763 git rev-parse --verify HEAD >../expect
765 git config submodule.submodule.update merge &&
766 test_must_fail git submodule update &&
768 git rev-parse --verify HEAD >../actual
770 test_cmp expect actual
774 test_expect_success 'submodule update exit immediately after recursive rebase error' '
777 git reset --hard HEAD &&
779 git reset --hard HEAD &&
780 git submodule update --recursive
783 test_commit "update_submodule_3" file
786 test_commit "update_submodule2_3" file
789 git add submodule2 &&
790 git commit -m "two_new_submodule_commits" &&
793 test_commit "conflict2" file &&
794 echo "conflict" > file
796 git checkout HEAD^ &&
798 git rev-parse --verify HEAD >../expect
800 git config submodule.submodule.update rebase &&
801 test_must_fail git submodule update &&
803 git rev-parse --verify HEAD >../actual
805 test_cmp expect actual
809 test_expect_success 'add different submodules to the same path' '
811 git submodule add ../submodule s1 &&
812 test_must_fail git submodule add ../merging s1
816 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
819 git submodule add ../submodule deeper/submodule &&
820 (cd deeper/submodule &&
821 git log > ../../expected
823 (cd .git/modules/deeper/submodule &&
824 git log > ../../../../actual
826 test_cmp expected actual
830 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
832 git commit -m "added submodule"
834 git clone super super2 &&
836 git submodule init deeper/submodule &&
837 git submodule update &&
838 (cd deeper/submodule &&
839 git log > ../../expected
841 (cd .git/modules/deeper/submodule &&
842 git log > ../../../../actual
844 test_cmp expected actual
848 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
850 (cd deeper/submodule &&
851 git submodule add ../submodule subsubmodule &&
853 git log > ../../../expected
855 git commit -m "added subsubmodule" &&
858 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
859 git log > ../../../../../actual
861 git add deeper/submodule &&
862 git commit -m "update submodule" &&
864 test_cmp expected actual
868 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
869 mkdir super_update_r &&
870 (cd super_update_r &&
873 mkdir subsuper_update_r &&
874 (cd subsuper_update_r &&
877 mkdir subsubsuper_update_r &&
878 (cd subsubsuper_update_r &&
881 git clone subsubsuper_update_r subsubsuper_update_r2 &&
882 (cd subsubsuper_update_r2 &&
883 test_commit "update_subsubsuper" file &&
886 git clone subsuper_update_r subsuper_update_r2 &&
887 (cd subsuper_update_r2 &&
888 test_commit "update_subsuper" file &&
889 git submodule add ../subsubsuper_update_r subsubmodule &&
890 git commit -am "subsubmodule" &&
893 git clone super_update_r super_update_r2 &&
894 (cd super_update_r2 &&
895 test_commit "update_super" file &&
896 git submodule add ../subsuper_update_r submodule &&
897 git commit -am "submodule" &&
900 rm -rf super_update_r2 &&
901 git clone super_update_r super_update_r2 &&
902 (cd super_update_r2 &&
903 git submodule update --init --recursive >actual &&
904 test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
905 (cd submodule/subsubmodule &&
906 git log > ../../expected
908 (cd .git/modules/submodule/modules/subsubmodule &&
909 git log > ../../../../../actual
911 test_cmp expected actual
915 test_expect_success 'submodule add properly re-creates deeper level submodules' '
917 git reset --hard main &&
919 git submodule add --force ../submodule deeper/submodule
923 test_expect_success 'submodule update properly revives a moved submodule' '
925 H=$(git rev-parse --short HEAD) &&
926 git commit -am "pre move" &&
927 H2=$(git rev-parse --short HEAD) &&
929 sed "s/$H/XXX/" out >expect &&
930 H=$(cd submodule2 && git rev-parse HEAD) &&
931 git rm --cached submodule2 &&
933 mkdir -p "moved/sub module" &&
934 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
935 git config -f .gitmodules submodule.submodule2.path "moved/sub module" &&
936 git commit -am "post move" &&
937 git submodule update &&
939 sed "s/$H2/XXX/" out >actual &&
940 test_cmp expect actual
944 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
945 mkdir -p linked/dir &&
946 ln -s linked/dir linkto &&
948 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
950 git submodule update --init --recursive
955 test_expect_success 'submodule update clone shallow submodule' '
956 test_when_finished "rm -rf super3" &&
957 first=$(git -C cloned rev-parse HEAD:submodule) &&
958 second=$(git -C submodule rev-parse HEAD) &&
959 commit_count=$(git -C submodule rev-list --count $first^..$second) &&
960 git clone cloned super3 &&
964 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
965 mv -f .gitmodules.tmp .gitmodules &&
966 git submodule update --init --depth=$commit_count &&
967 git -C submodule log --oneline >out &&
968 test_line_count = 1 out
972 test_expect_success 'submodule update clone shallow submodule outside of depth' '
973 test_when_finished "rm -rf super3" &&
974 git clone cloned super3 &&
978 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
979 mv -f .gitmodules.tmp .gitmodules &&
980 # Some protocol versions (e.g. 2) support fetching
981 # unadvertised objects, so restrict this test to v0.
982 test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
983 git submodule update --init --depth=1 2>actual &&
984 test_i18ngrep "Direct fetching of that commit failed." actual &&
985 git -C ../submodule config uploadpack.allowReachableSHA1InWant true &&
986 git submodule update --init --depth=1 >actual &&
987 git -C submodule log --oneline >out &&
988 test_line_count = 1 out
992 test_expect_success 'submodule update --recursive drops module name before recursing' '
994 (cd deeper/submodule/subsubmodule &&
997 git submodule update --recursive deeper/submodule >actual &&
998 test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
1002 test_expect_success 'submodule update can be run in parallel' '
1004 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
1005 grep "7 tasks" trace.out &&
1006 git config submodule.fetchJobs 8 &&
1007 GIT_TRACE=$(pwd)/trace.out git submodule update &&
1008 grep "8 tasks" trace.out &&
1009 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
1010 grep "9 tasks" trace.out
1014 test_expect_success 'git clone passes the parallel jobs config on to submodules' '
1015 test_when_finished "rm -rf super4" &&
1016 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
1017 grep "7 tasks" trace.out &&
1019 git config --global submodule.fetchJobs 8 &&
1020 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
1021 grep "8 tasks" trace.out &&
1023 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
1024 grep "9 tasks" trace.out &&
1028 test_expect_success 'submodule update --quiet passes quietness to merge/rebase' '
1030 test_commit -C rebasing message &&
1031 git submodule update --rebase --quiet >out 2>err &&
1032 test_must_be_empty out &&
1033 test_must_be_empty err &&
1034 git submodule update --rebase -v >out 2>err &&
1035 test_file_not_empty out &&
1036 test_must_be_empty err