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_cmp expect actual &&
140 test_cmp 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 'local config should override .gitmodules branch' '
214 git checkout -b test-branch &&
215 echo line5 >> file &&
218 git commit -m "upstream line5" &&
222 git config submodule.submodule.branch test-branch &&
223 git submodule update --remote --force submodule &&
225 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
229 test_expect_success 'submodule update --rebase staying on master' '
230 (cd super/submodule &&
237 git submodule update --rebase submodule &&
243 test_expect_success 'submodule update --merge staying on master' '
244 (cd super/submodule &&
245 git reset --hard HEAD~1
251 git submodule update --merge submodule &&
257 test_expect_success 'submodule update - rebase in .git/config' '
259 git config submodule.submodule.update rebase
261 (cd super/submodule &&
262 git reset --hard HEAD~1
268 git submodule update submodule &&
274 test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
276 git config submodule.submodule.update checkout
278 (cd super/submodule &&
279 git reset --hard HEAD~1
285 git submodule update --rebase submodule &&
291 test_expect_success 'submodule update - merge in .git/config' '
293 git config submodule.submodule.update merge
295 (cd super/submodule &&
296 git reset --hard HEAD~1
302 git submodule update submodule &&
308 test_expect_success 'submodule update - checkout in .git/config but --merge given' '
310 git config submodule.submodule.update checkout
312 (cd super/submodule &&
313 git reset --hard HEAD~1
319 git submodule update --merge submodule &&
325 test_expect_success 'submodule update - checkout in .git/config' '
327 git config submodule.submodule.update checkout
329 (cd super/submodule &&
330 git reset --hard HEAD^
336 git submodule update submodule &&
342 test_expect_success 'submodule update - command in .git/config' '
344 git config submodule.submodule.update "!git checkout"
346 (cd super/submodule &&
347 git reset --hard HEAD^
353 git submodule update submodule &&
360 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
363 test_expect_success 'submodule update - command in .git/config catches failure' '
365 git config submodule.submodule.update "!false"
367 (cd super/submodule &&
368 git reset --hard $submodulesha1^
371 test_must_fail git submodule update submodule 2>../actual
373 test_cmp actual expect
377 Execution of 'false $submodulesha1' failed in submodule path '../submodule'
380 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
382 git config submodule.submodule.update "!false"
384 (cd super/submodule &&
385 git reset --hard $submodulesha1^
388 mkdir tmp && cd tmp &&
389 test_must_fail git submodule update ../submodule 2>../../actual
391 test_cmp actual expect
395 Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
396 Failed to recurse into submodule path '../super'
399 test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
400 (cd recursivesuper &&
401 git submodule update --remote super &&
403 git commit -m "update to latest to have more than one commit in submodules"
405 git -C recursivesuper/super config submodule.submodule.update "!false" &&
406 git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
407 (cd recursivesuper &&
408 mkdir -p tmp && cd tmp &&
409 test_must_fail git submodule update --recursive ../super 2>../../actual
411 test_cmp actual expect
414 test_expect_success 'submodule init does not copy command into .git/config' '
416 H=$(git ls-files -s submodule | cut -d" " -f2) &&
418 git update-index --add --cacheinfo 160000 $H submodule1 &&
419 git config -f .gitmodules submodule.submodule1.path submodule1 &&
420 git config -f .gitmodules submodule.submodule1.url ../submodule &&
421 git config -f .gitmodules submodule.submodule1.update !false &&
422 git submodule init submodule1 &&
423 echo "none" >expect &&
424 git config submodule.submodule1.update >actual &&
425 test_cmp expect actual
429 test_expect_success 'submodule init picks up rebase' '
431 git config -f .gitmodules submodule.rebasing.update rebase &&
432 git submodule init rebasing &&
433 test "rebase" = "$(git config submodule.rebasing.update)"
437 test_expect_success 'submodule init picks up merge' '
439 git config -f .gitmodules submodule.merging.update merge &&
440 git submodule init merging &&
441 test "merge" = "$(git config submodule.merging.update)"
445 test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
448 git submodule update submodule &&
449 git status -s submodule >expect &&
451 git submodule update --merge submodule &&
452 git status -s submodule >actual &&
453 test_cmp expect actual
457 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
460 git submodule update submodule &&
461 git status -s submodule >expect &&
463 git submodule update --rebase submodule &&
464 git status -s submodule >actual &&
465 test_cmp expect actual
469 test_expect_success 'submodule update ignores update=merge config for new submodules' '
472 git submodule update submodule &&
473 git status -s submodule >expect &&
475 git config submodule.submodule.update merge &&
476 git submodule update submodule &&
477 git status -s submodule >actual &&
478 git config --unset submodule.submodule.update &&
479 test_cmp expect actual
483 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
486 git submodule update submodule &&
487 git status -s submodule >expect &&
489 git config submodule.submodule.update rebase &&
490 git submodule update submodule &&
491 git status -s submodule >actual &&
492 git config --unset submodule.submodule.update &&
493 test_cmp expect actual
497 test_expect_success 'submodule init picks up update=none' '
499 git config -f .gitmodules submodule.none.update none &&
500 git submodule init none &&
501 test "none" = "$(git config submodule.none.update)"
505 test_expect_success 'submodule update - update=none in .git/config' '
507 git config submodule.submodule.update none &&
509 git checkout master &&
512 git diff --raw | grep " submodule" &&
513 git submodule update &&
514 git diff --raw | grep " submodule" &&
518 git config --unset submodule.submodule.update &&
519 git submodule update submodule
523 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
525 git config submodule.submodule.update none &&
527 git checkout master &&
530 git diff --raw | grep " submodule" &&
531 git submodule update --checkout &&
532 test_must_fail git diff --raw \| grep " submodule" &&
534 test_must_fail compare_head
536 git config --unset submodule.submodule.update
540 test_expect_success 'submodule update --init skips submodule with update=none' '
542 git add .gitmodules &&
543 git commit -m ".gitmodules"
545 git clone super cloned &&
547 git submodule update --init &&
548 test -e submodule/.git &&
549 test_must_fail test -e none/.git
553 test_expect_success 'submodule update continues after checkout error' '
555 git reset --hard HEAD &&
556 git submodule add ../submodule submodule2 &&
557 git submodule init &&
558 git commit -am "new_submodule" &&
560 git rev-parse --verify HEAD >../expect
563 test_commit "update_submodule" file
566 test_commit "update_submodule2" file
569 git add submodule2 &&
570 git commit -m "two_new_submodule_commits" &&
574 git checkout HEAD^ &&
575 test_must_fail git submodule update &&
577 git rev-parse --verify HEAD >../actual
579 test_cmp expect actual
582 test_expect_success 'submodule update continues after recursive checkout error' '
584 git reset --hard HEAD &&
585 git checkout master &&
586 git submodule update &&
588 git submodule add ../submodule subsubmodule &&
589 git submodule init &&
590 git commit -m "new_subsubmodule"
593 git commit -m "update_submodule" &&
596 test_commit "update_subsubmodule" file
598 git add subsubmodule &&
599 test_commit "update_submodule_again" file &&
601 test_commit "update_subsubmodule_again" file
603 test_commit "update_submodule_again_again" file
606 git rev-parse --verify HEAD >../expect &&
607 test_commit "update_submodule2_again" file
610 git add submodule2 &&
611 git commit -m "new_commits" &&
612 git checkout HEAD^ &&
614 git checkout HEAD^ &&
619 test_must_fail git submodule update --recursive &&
621 git rev-parse --verify HEAD >../actual
623 test_cmp expect actual
627 test_expect_success 'submodule update exit immediately in case of merge conflict' '
629 git checkout master &&
630 git reset --hard HEAD &&
633 git reset --hard HEAD
636 git submodule update --recursive &&
638 test_commit "update_submodule_2" file
641 test_commit "update_submodule2_2" file
644 git add submodule2 &&
645 git commit -m "two_new_submodule_commits" &&
647 git checkout master &&
648 test_commit "conflict" file &&
649 echo "conflict" > file
651 git checkout HEAD^ &&
653 git rev-parse --verify HEAD >../expect
655 git config submodule.submodule.update merge &&
656 test_must_fail git submodule update &&
658 git rev-parse --verify HEAD >../actual
660 test_cmp expect actual
664 test_expect_success 'submodule update exit immediately after recursive rebase error' '
666 git checkout master &&
667 git reset --hard HEAD &&
669 git reset --hard HEAD &&
670 git submodule update --recursive
673 test_commit "update_submodule_3" file
676 test_commit "update_submodule2_3" file
679 git add submodule2 &&
680 git commit -m "two_new_submodule_commits" &&
682 git checkout master &&
683 test_commit "conflict2" file &&
684 echo "conflict" > file
686 git checkout HEAD^ &&
688 git rev-parse --verify HEAD >../expect
690 git config submodule.submodule.update rebase &&
691 test_must_fail git submodule update &&
693 git rev-parse --verify HEAD >../actual
695 test_cmp expect actual
699 test_expect_success 'add different submodules to the same path' '
701 git submodule add ../submodule s1 &&
702 test_must_fail git submodule add ../merging s1
706 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
709 git submodule add ../submodule deeper/submodule &&
710 (cd deeper/submodule &&
711 git log > ../../expected
713 (cd .git/modules/deeper/submodule &&
714 git log > ../../../../actual
716 test_cmp actual expected
720 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
722 git commit -m "added submodule"
724 git clone super super2 &&
726 git submodule init deeper/submodule &&
727 git submodule update &&
728 (cd deeper/submodule &&
729 git log > ../../expected
731 (cd .git/modules/deeper/submodule &&
732 git log > ../../../../actual
734 test_cmp actual expected
738 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
740 (cd deeper/submodule &&
741 git submodule add ../submodule subsubmodule &&
743 git log > ../../../expected
745 git commit -m "added subsubmodule" &&
748 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
749 git log > ../../../../../actual
751 git add deeper/submodule &&
752 git commit -m "update submodule" &&
754 test_cmp actual expected
758 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
759 mkdir super_update_r &&
760 (cd super_update_r &&
763 mkdir subsuper_update_r &&
764 (cd subsuper_update_r &&
767 mkdir subsubsuper_update_r &&
768 (cd subsubsuper_update_r &&
771 git clone subsubsuper_update_r subsubsuper_update_r2 &&
772 (cd subsubsuper_update_r2 &&
773 test_commit "update_subsubsuper" file &&
774 git push origin master
776 git clone subsuper_update_r subsuper_update_r2 &&
777 (cd subsuper_update_r2 &&
778 test_commit "update_subsuper" file &&
779 git submodule add ../subsubsuper_update_r subsubmodule &&
780 git commit -am "subsubmodule" &&
781 git push origin master
783 git clone super_update_r super_update_r2 &&
784 (cd super_update_r2 &&
785 test_commit "update_super" file &&
786 git submodule add ../subsuper_update_r submodule &&
787 git commit -am "submodule" &&
788 git push origin master
790 rm -rf super_update_r2 &&
791 git clone super_update_r super_update_r2 &&
792 (cd super_update_r2 &&
793 git submodule update --init --recursive >actual &&
794 test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
795 (cd submodule/subsubmodule &&
796 git log > ../../expected
798 (cd .git/modules/submodule/modules/subsubmodule
799 git log > ../../../../../actual
801 test_cmp actual expected
805 test_expect_success 'submodule add properly re-creates deeper level submodules' '
807 git reset --hard master &&
809 git submodule add --force ../submodule deeper/submodule
813 test_expect_success 'submodule update properly revives a moved submodule' '
815 H=$(git rev-parse --short HEAD) &&
816 git commit -am "pre move" &&
817 H2=$(git rev-parse --short HEAD) &&
818 git status | sed "s/$H/XXX/" >expect &&
819 H=$(cd submodule2; git rev-parse HEAD) &&
820 git rm --cached submodule2 &&
822 mkdir -p "moved/sub module" &&
823 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
824 git config -f .gitmodules submodule.submodule2.path "moved/sub module"
825 git commit -am "post move" &&
826 git submodule update &&
827 git status | sed "s/$H2/XXX/" >actual &&
828 test_cmp expect actual
832 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
833 mkdir -p linked/dir &&
834 ln -s linked/dir linkto &&
836 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
838 git submodule update --init --recursive
843 test_expect_success 'submodule update clone shallow submodule' '
844 git clone cloned super3 &&
847 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
848 mv -f .gitmodules.tmp .gitmodules &&
849 git submodule update --init --depth=3
851 test 1 = $(git log --oneline | wc -l)
856 test_expect_success 'submodule update --recursive drops module name before recursing' '
858 (cd deeper/submodule/subsubmodule &&
861 git submodule update --recursive deeper/submodule >actual &&
862 test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
866 test_expect_success 'submodule update can be run in parallel' '
868 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
869 grep "7 tasks" trace.out &&
870 git config submodule.fetchJobs 8 &&
871 GIT_TRACE=$(pwd)/trace.out git submodule update &&
872 grep "8 tasks" trace.out &&
873 GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
874 grep "9 tasks" trace.out
878 test_expect_success 'git clone passes the parallel jobs config on to submodules' '
879 test_when_finished "rm -rf super4" &&
880 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
881 grep "7 tasks" trace.out &&
883 git config --global submodule.fetchJobs 8 &&
884 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
885 grep "8 tasks" trace.out &&
887 GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
888 grep "9 tasks" trace.out &&