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
162 test_expect_success 'submodule update does not fetch already present commits' '
164 echo line3 >> file &&
167 git commit -m "upstream line3"
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
175 git submodule update > ../actual 2> ../actual.err
177 test_i18ncmp expected actual &&
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
190 test_must_fail git submodule update submodule
193 test_expect_success 'submodule update should throw away changes with --force ' '
198 git submodule update --force submodule &&
204 test_expect_success 'submodule update --force forcibly checks out submodules' '
209 git submodule update --force submodule &&
211 test "$(git status -s file)" = ""
216 test_expect_success 'submodule update --remote should fetch upstream changes' '
218 echo line4 >> file &&
221 git commit -m "upstream line4"
224 git submodule update --remote --force submodule &&
226 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
230 test_expect_success 'submodule update --remote should fetch upstream changes with .' '
233 git config -f .gitmodules submodule."submodule".branch "." &&
234 git add .gitmodules &&
235 git commit -m "submodules: update from the respective superproject branch"
239 echo line4a >> file &&
242 git commit -m "upstream line4a" &&
243 git checkout -b test-branch &&
244 test_commit on-test-branch
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^
263 test_expect_success 'local config should override .gitmodules branch' '
265 git checkout test-branch &&
266 echo line5 >> file &&
269 git commit -m "upstream line5" &&
273 git config submodule.submodule.branch test-branch &&
274 git submodule update --remote --force submodule &&
276 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
280 test_expect_success 'submodule update --rebase staying on master' '
281 (cd super/submodule &&
288 git submodule update --rebase submodule &&
294 test_expect_success 'submodule update --merge staying on master' '
295 (cd super/submodule &&
296 git reset --hard HEAD~1
302 git submodule update --merge submodule &&
308 test_expect_success 'submodule update - rebase in .git/config' '
310 git config submodule.submodule.update rebase
312 (cd super/submodule &&
313 git reset --hard HEAD~1
319 git submodule update submodule &&
325 test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
327 git config submodule.submodule.update checkout
329 (cd super/submodule &&
330 git reset --hard HEAD~1
336 git submodule update --rebase submodule &&
342 test_expect_success 'submodule update - merge in .git/config' '
344 git config submodule.submodule.update merge
346 (cd super/submodule &&
347 git reset --hard HEAD~1
353 git submodule update submodule &&
359 test_expect_success 'submodule update - checkout in .git/config but --merge given' '
361 git config submodule.submodule.update checkout
363 (cd super/submodule &&
364 git reset --hard HEAD~1
370 git submodule update --merge submodule &&
376 test_expect_success 'submodule update - checkout in .git/config' '
378 git config submodule.submodule.update checkout
380 (cd super/submodule &&
381 git reset --hard HEAD^
387 git submodule update submodule &&
393 test_expect_success 'submodule update - command in .git/config' '
395 git config submodule.submodule.update "!git checkout"
397 (cd super/submodule &&
398 git reset --hard HEAD^
404 git submodule update submodule &&
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
419 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
422 test_expect_success 'submodule update - command in .git/config catches failure' '
424 git config submodule.submodule.update "!false"
426 (cd super/submodule &&
427 git reset --hard $submodulesha1^
430 test_must_fail git submodule update submodule 2>../actual
432 test_i18ncmp actual expect
436 Execution of 'false $submodulesha1' failed in submodule path '../submodule'
439 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
441 git config submodule.submodule.update "!false"
443 (cd super/submodule &&
444 git reset --hard $submodulesha1^
447 mkdir tmp && cd tmp &&
448 test_must_fail git submodule update ../submodule 2>../../actual
450 test_i18ncmp actual expect
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'\''
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
464 Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
465 Failed to recurse into submodule path '../super'
468 test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
469 (cd recursivesuper &&
470 git submodule update --remote super &&
472 git commit -m "update to latest to have more than one commit in submodules"
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
480 test_i18ncmp actual expect
483 test_expect_success 'submodule init does not copy command into .git/config' '
485 H=$(git ls-files -s submodule | cut -d" " -f2) &&
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
498 test_expect_success 'submodule init picks up rebase' '
500 git config -f .gitmodules submodule.rebasing.update rebase &&
501 git submodule init rebasing &&
502 test "rebase" = "$(git config submodule.rebasing.update)"
506 test_expect_success 'submodule init picks up merge' '
508 git config -f .gitmodules submodule.merging.update merge &&
509 git submodule init merging &&
510 test "merge" = "$(git config submodule.merging.update)"
514 test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
515 test_config -C super submodule.submodule.update checkout &&
518 git submodule update submodule &&
519 git status -s submodule >expect &&
521 git submodule update --merge submodule &&
522 git status -s submodule >actual &&
523 test_cmp expect actual
527 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
528 test_config -C super submodule.submodule.update checkout &&
531 git submodule update submodule &&
532 git status -s submodule >expect &&
534 git submodule update --rebase submodule &&
535 git status -s submodule >actual &&
536 test_cmp expect actual
540 test_expect_success 'submodule update ignores update=merge config for new submodules' '
543 git submodule update submodule &&
544 git status -s submodule >expect &&
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
554 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
557 git submodule update submodule &&
558 git status -s submodule >expect &&
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
568 test_expect_success 'submodule init picks up update=none' '
570 git config -f .gitmodules submodule.none.update none &&
571 git submodule init none &&
572 test "none" = "$(git config submodule.none.update)"
576 test_expect_success 'submodule update - update=none in .git/config' '
578 git config submodule.submodule.update none &&
580 git checkout master &&
583 git diff --raw | grep " submodule" &&
584 git submodule update &&
585 git diff --raw | grep " submodule" &&
589 git config --unset submodule.submodule.update &&
590 git submodule update submodule
594 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
596 git config submodule.submodule.update none &&
598 git checkout master &&
601 git diff --raw | grep " submodule" &&
602 git submodule update --checkout &&
603 test_must_fail git diff --raw \| grep " submodule" &&
605 test_must_fail compare_head
607 git config --unset submodule.submodule.update
611 test_expect_success 'submodule update --init skips submodule with update=none' '
613 git add .gitmodules &&
614 git commit -m ".gitmodules"
616 git clone super cloned &&
618 git submodule update --init &&
619 test -e submodule/.git &&
620 test_must_fail test -e none/.git
624 test_expect_success 'submodule update continues after checkout error' '
626 git reset --hard HEAD &&
627 git submodule add ../submodule submodule2 &&
628 git submodule init &&
629 git commit -am "new_submodule" &&
631 git rev-parse --verify HEAD >../expect
634 test_commit "update_submodule" file
637 test_commit "update_submodule2" file
640 git add submodule2 &&
641 git commit -m "two_new_submodule_commits" &&
645 git checkout HEAD^ &&
646 test_must_fail git submodule update &&
648 git rev-parse --verify HEAD >../actual
650 test_cmp expect actual
653 test_expect_success 'submodule update continues after recursive checkout error' '
655 git reset --hard HEAD &&
656 git checkout master &&
657 git submodule update &&
659 git submodule add ../submodule subsubmodule &&
660 git submodule init &&
661 git commit -m "new_subsubmodule"
664 git commit -m "update_submodule" &&
667 test_commit "update_subsubmodule" file
669 git add subsubmodule &&
670 test_commit "update_submodule_again" file &&
672 test_commit "update_subsubmodule_again" file
674 test_commit "update_submodule_again_again" file
677 git rev-parse --verify HEAD >../expect &&
678 test_commit "update_submodule2_again" file
681 git add submodule2 &&
682 git commit -m "new_commits" &&
683 git checkout HEAD^ &&
685 git checkout HEAD^ &&
690 test_must_fail git submodule update --recursive &&
692 git rev-parse --verify HEAD >../actual
694 test_cmp expect actual
698 test_expect_success 'submodule update exit immediately in case of merge conflict' '
700 git checkout master &&
701 git reset --hard HEAD &&
704 git reset --hard HEAD
707 git submodule update --recursive &&
709 test_commit "update_submodule_2" file
712 test_commit "update_submodule2_2" file
715 git add submodule2 &&
716 git commit -m "two_new_submodule_commits" &&
718 git checkout master &&
719 test_commit "conflict" file &&
720 echo "conflict" > file
722 git checkout HEAD^ &&
724 git rev-parse --verify HEAD >../expect
726 git config submodule.submodule.update merge &&
727 test_must_fail git submodule update &&
729 git rev-parse --verify HEAD >../actual
731 test_cmp expect actual
735 test_expect_success 'submodule update exit immediately after recursive rebase error' '
737 git checkout master &&
738 git reset --hard HEAD &&
740 git reset --hard HEAD &&
741 git submodule update --recursive
744 test_commit "update_submodule_3" file
747 test_commit "update_submodule2_3" file
750 git add submodule2 &&
751 git commit -m "two_new_submodule_commits" &&
753 git checkout master &&
754 test_commit "conflict2" file &&
755 echo "conflict" > file
757 git checkout HEAD^ &&
759 git rev-parse --verify HEAD >../expect
761 git config submodule.submodule.update rebase &&
762 test_must_fail git submodule update &&
764 git rev-parse --verify HEAD >../actual
766 test_cmp expect actual
770 test_expect_success 'add different submodules to the same path' '
772 git submodule add ../submodule s1 &&
773 test_must_fail git submodule add ../merging s1
777 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
780 git submodule add ../submodule deeper/submodule &&
781 (cd deeper/submodule &&
782 git log > ../../expected
784 (cd .git/modules/deeper/submodule &&
785 git log > ../../../../actual
787 test_cmp actual expected
791 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
793 git commit -m "added submodule"
795 git clone super super2 &&
797 git submodule init deeper/submodule &&
798 git submodule update &&
799 (cd deeper/submodule &&
800 git log > ../../expected
802 (cd .git/modules/deeper/submodule &&
803 git log > ../../../../actual
805 test_cmp actual expected
809 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
811 (cd deeper/submodule &&
812 git submodule add ../submodule subsubmodule &&
814 git log > ../../../expected
816 git commit -m "added subsubmodule" &&
819 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
820 git log > ../../../../../actual
822 git add deeper/submodule &&
823 git commit -m "update submodule" &&
825 test_cmp actual expected
829 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
830 mkdir super_update_r &&
831 (cd super_update_r &&
834 mkdir subsuper_update_r &&
835 (cd subsuper_update_r &&
838 mkdir subsubsuper_update_r &&
839 (cd subsubsuper_update_r &&
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
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
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
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
869 (cd .git/modules/submodule/modules/subsubmodule &&
870 git log > ../../../../../actual
872 test_cmp actual expected
876 test_expect_success 'submodule add properly re-creates deeper level submodules' '
878 git reset --hard master &&
880 git submodule add --force ../submodule deeper/submodule
884 test_expect_success 'submodule update properly revives a moved submodule' '
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 &&
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
903 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
904 mkdir -p linked/dir &&
905 ln -s linked/dir linkto &&
907 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
909 git submodule update --init --recursive
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 &&
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)
930 test_expect_success 'submodule update clone shallow submodule outside of depth' '
931 test_when_finished "rm -rf super3" &&
932 git clone cloned 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)
946 test_expect_success 'submodule update --recursive drops module name before recursing' '
948 (cd deeper/submodule/subsubmodule &&
951 git submodule update --recursive deeper/submodule >actual &&
952 test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
956 test_expect_success 'submodule update can be run in parallel' '
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
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 &&
973 git config --global submodule.fetchJobs 8 &&
974 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
975 grep "8 tasks" trace.out &&
977 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
978 grep "9 tasks" trace.out &&