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 'merging' ($pwd/merging) registered for path '../super/merging'
112 Submodule 'none' ($pwd/none) registered for path '../super/none'
113 Submodule 'rebasing' ($pwd/rebasing) registered for path '../super/rebasing'
114 Submodule 'submodule' ($pwd/submodule) registered for path '../super/submodule'
115 Submodule path '../super/merging': checked out '$mergingsha1'
116 Submodule path '../super/none': checked out '$nonesha1'
117 Submodule path '../super/rebasing': checked out '$rebasingsha1'
118 Submodule path '../super/submodule': checked out '$submodulesha1'
121 test_expect_success 'submodule update --init --recursive from subdirectory' '
122 git -C recursivesuper/super reset --hard HEAD^ &&
123 (cd recursivesuper &&
126 git submodule update --init --recursive ../super >../../actual
128 test_cmp expect actual
132 test_expect_success 'submodule update does not fetch already present commits' '
134 echo line3 >> file &&
137 git commit -m "upstream line3"
139 (cd super/submodule &&
140 head=$(git rev-parse --verify HEAD) &&
141 echo "Submodule path ${apos}submodule$apos: checked out $apos$head$apos" > ../../expected &&
142 git reset --hard HEAD~1
145 git submodule update > ../actual 2> ../actual.err
147 test_i18ncmp expected actual &&
151 test_expect_success 'submodule update should fail due to local changes' '
152 (cd super/submodule &&
153 git reset --hard HEAD~1 &&
154 echo "local change" > file
160 test_must_fail git submodule update submodule
163 test_expect_success 'submodule update should throw away changes with --force ' '
168 git submodule update --force submodule &&
174 test_expect_success 'submodule update --force forcibly checks out submodules' '
179 git submodule update --force submodule &&
181 test "$(git status -s file)" = ""
186 test_expect_success 'submodule update --remote should fetch upstream changes' '
188 echo line4 >> file &&
191 git commit -m "upstream line4"
194 git submodule update --remote --force submodule &&
196 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
200 test_expect_success 'local config should override .gitmodules branch' '
202 git checkout -b test-branch &&
203 echo line5 >> file &&
206 git commit -m "upstream line5" &&
210 git config submodule.submodule.branch test-branch &&
211 git submodule update --remote --force submodule &&
213 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
217 test_expect_success 'submodule update --rebase staying on master' '
218 (cd super/submodule &&
225 git submodule update --rebase submodule &&
231 test_expect_success 'submodule update --merge staying on master' '
232 (cd super/submodule &&
233 git reset --hard HEAD~1
239 git submodule update --merge submodule &&
245 test_expect_success 'submodule update - rebase in .git/config' '
247 git config submodule.submodule.update rebase
249 (cd super/submodule &&
250 git reset --hard HEAD~1
256 git submodule update submodule &&
262 test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
264 git config submodule.submodule.update checkout
266 (cd super/submodule &&
267 git reset --hard HEAD~1
273 git submodule update --rebase submodule &&
279 test_expect_success 'submodule update - merge in .git/config' '
281 git config submodule.submodule.update merge
283 (cd super/submodule &&
284 git reset --hard HEAD~1
290 git submodule update submodule &&
296 test_expect_success 'submodule update - checkout in .git/config but --merge given' '
298 git config submodule.submodule.update checkout
300 (cd super/submodule &&
301 git reset --hard HEAD~1
307 git submodule update --merge submodule &&
313 test_expect_success 'submodule update - checkout in .git/config' '
315 git config submodule.submodule.update checkout
317 (cd super/submodule &&
318 git reset --hard HEAD^
324 git submodule update submodule &&
330 test_expect_success 'submodule update - command in .git/config' '
332 git config submodule.submodule.update "!git checkout"
334 (cd super/submodule &&
335 git reset --hard HEAD^
341 git submodule update submodule &&
348 Execution of 'false $submodulesha1' failed in submodule path 'submodule'
351 test_expect_success 'submodule update - command in .git/config catches failure' '
353 git config submodule.submodule.update "!false"
355 (cd super/submodule &&
356 git reset --hard $submodulesha1^
359 test_must_fail git submodule update submodule 2>../actual
361 test_cmp actual expect
365 Execution of 'false $submodulesha1' failed in submodule path '../submodule'
368 test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
370 git config submodule.submodule.update "!false"
372 (cd super/submodule &&
373 git reset --hard $submodulesha1^
376 mkdir tmp && cd tmp &&
377 test_must_fail git submodule update ../submodule 2>../../actual
379 test_cmp actual expect
382 test_expect_success 'submodule init does not copy command into .git/config' '
384 H=$(git ls-files -s submodule | cut -d" " -f2) &&
386 git update-index --add --cacheinfo 160000 $H submodule1 &&
387 git config -f .gitmodules submodule.submodule1.path submodule1 &&
388 git config -f .gitmodules submodule.submodule1.url ../submodule &&
389 git config -f .gitmodules submodule.submodule1.update !false &&
390 git submodule init submodule1 &&
391 echo "none" >expect &&
392 git config submodule.submodule1.update >actual &&
393 test_cmp expect actual
397 test_expect_success 'submodule init picks up rebase' '
399 git config -f .gitmodules submodule.rebasing.update rebase &&
400 git submodule init rebasing &&
401 test "rebase" = "$(git config submodule.rebasing.update)"
405 test_expect_success 'submodule init picks up merge' '
407 git config -f .gitmodules submodule.merging.update merge &&
408 git submodule init merging &&
409 test "merge" = "$(git config submodule.merging.update)"
413 test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
416 git submodule update submodule &&
417 git status -s submodule >expect &&
419 git submodule update --merge submodule &&
420 git status -s submodule >actual &&
421 test_cmp expect actual
425 test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
428 git submodule update submodule &&
429 git status -s submodule >expect &&
431 git submodule update --rebase submodule &&
432 git status -s submodule >actual &&
433 test_cmp expect actual
437 test_expect_success 'submodule update ignores update=merge config for new submodules' '
440 git submodule update submodule &&
441 git status -s submodule >expect &&
443 git config submodule.submodule.update merge &&
444 git submodule update submodule &&
445 git status -s submodule >actual &&
446 git config --unset submodule.submodule.update &&
447 test_cmp expect actual
451 test_expect_success 'submodule update ignores update=rebase config for new submodules' '
454 git submodule update submodule &&
455 git status -s submodule >expect &&
457 git config submodule.submodule.update rebase &&
458 git submodule update submodule &&
459 git status -s submodule >actual &&
460 git config --unset submodule.submodule.update &&
461 test_cmp expect actual
465 test_expect_success 'submodule init picks up update=none' '
467 git config -f .gitmodules submodule.none.update none &&
468 git submodule init none &&
469 test "none" = "$(git config submodule.none.update)"
473 test_expect_success 'submodule update - update=none in .git/config' '
475 git config submodule.submodule.update none &&
477 git checkout master &&
480 git diff --raw | grep " submodule" &&
481 git submodule update &&
482 git diff --raw | grep " submodule" &&
486 git config --unset submodule.submodule.update &&
487 git submodule update submodule
491 test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
493 git config submodule.submodule.update none &&
495 git checkout master &&
498 git diff --raw | grep " submodule" &&
499 git submodule update --checkout &&
500 test_must_fail git diff --raw \| grep " submodule" &&
502 test_must_fail compare_head
504 git config --unset submodule.submodule.update
508 test_expect_success 'submodule update --init skips submodule with update=none' '
510 git add .gitmodules &&
511 git commit -m ".gitmodules"
513 git clone super cloned &&
515 git submodule update --init &&
516 test -e submodule/.git &&
517 test_must_fail test -e none/.git
521 test_expect_success 'submodule update continues after checkout error' '
523 git reset --hard HEAD &&
524 git submodule add ../submodule submodule2 &&
525 git submodule init &&
526 git commit -am "new_submodule" &&
528 git rev-parse --verify HEAD >../expect
531 test_commit "update_submodule" file
534 test_commit "update_submodule2" file
537 git add submodule2 &&
538 git commit -m "two_new_submodule_commits" &&
542 git checkout HEAD^ &&
543 test_must_fail git submodule update &&
545 git rev-parse --verify HEAD >../actual
547 test_cmp expect actual
550 test_expect_success 'submodule update continues after recursive checkout error' '
552 git reset --hard HEAD &&
553 git checkout master &&
554 git submodule update &&
556 git submodule add ../submodule subsubmodule &&
557 git submodule init &&
558 git commit -m "new_subsubmodule"
561 git commit -m "update_submodule" &&
564 test_commit "update_subsubmodule" file
566 git add subsubmodule &&
567 test_commit "update_submodule_again" file &&
569 test_commit "update_subsubmodule_again" file
571 test_commit "update_submodule_again_again" file
574 git rev-parse --verify HEAD >../expect &&
575 test_commit "update_submodule2_again" file
578 git add submodule2 &&
579 git commit -m "new_commits" &&
580 git checkout HEAD^ &&
582 git checkout HEAD^ &&
587 test_must_fail git submodule update --recursive &&
589 git rev-parse --verify HEAD >../actual
591 test_cmp expect actual
595 test_expect_success 'submodule update exit immediately in case of merge conflict' '
597 git checkout master &&
598 git reset --hard HEAD &&
601 git reset --hard HEAD
604 git submodule update --recursive &&
606 test_commit "update_submodule_2" file
609 test_commit "update_submodule2_2" file
612 git add submodule2 &&
613 git commit -m "two_new_submodule_commits" &&
615 git checkout master &&
616 test_commit "conflict" file &&
617 echo "conflict" > file
619 git checkout HEAD^ &&
621 git rev-parse --verify HEAD >../expect
623 git config submodule.submodule.update merge &&
624 test_must_fail git submodule update &&
626 git rev-parse --verify HEAD >../actual
628 test_cmp expect actual
632 test_expect_success 'submodule update exit immediately after recursive rebase error' '
634 git checkout master &&
635 git reset --hard HEAD &&
637 git reset --hard HEAD &&
638 git submodule update --recursive
641 test_commit "update_submodule_3" file
644 test_commit "update_submodule2_3" file
647 git add submodule2 &&
648 git commit -m "two_new_submodule_commits" &&
650 git checkout master &&
651 test_commit "conflict2" file &&
652 echo "conflict" > file
654 git checkout HEAD^ &&
656 git rev-parse --verify HEAD >../expect
658 git config submodule.submodule.update rebase &&
659 test_must_fail git submodule update &&
661 git rev-parse --verify HEAD >../actual
663 test_cmp expect actual
667 test_expect_success 'add different submodules to the same path' '
669 git submodule add ../submodule s1 &&
670 test_must_fail git submodule add ../merging s1
674 test_expect_success 'submodule add places git-dir in superprojects git-dir' '
677 git submodule add ../submodule deeper/submodule &&
678 (cd deeper/submodule &&
679 git log > ../../expected
681 (cd .git/modules/deeper/submodule &&
682 git log > ../../../../actual
684 test_cmp actual expected
688 test_expect_success 'submodule update places git-dir in superprojects git-dir' '
690 git commit -m "added submodule"
692 git clone super super2 &&
694 git submodule init deeper/submodule &&
695 git submodule update &&
696 (cd deeper/submodule &&
697 git log > ../../expected
699 (cd .git/modules/deeper/submodule &&
700 git log > ../../../../actual
702 test_cmp actual expected
706 test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
708 (cd deeper/submodule &&
709 git submodule add ../submodule subsubmodule &&
711 git log > ../../../expected
713 git commit -m "added subsubmodule" &&
716 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
717 git log > ../../../../../actual
719 git add deeper/submodule &&
720 git commit -m "update submodule" &&
722 test_cmp actual expected
726 test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
727 mkdir super_update_r &&
728 (cd super_update_r &&
731 mkdir subsuper_update_r &&
732 (cd subsuper_update_r &&
735 mkdir subsubsuper_update_r &&
736 (cd subsubsuper_update_r &&
739 git clone subsubsuper_update_r subsubsuper_update_r2 &&
740 (cd subsubsuper_update_r2 &&
741 test_commit "update_subsubsuper" file &&
742 git push origin master
744 git clone subsuper_update_r subsuper_update_r2 &&
745 (cd subsuper_update_r2 &&
746 test_commit "update_subsuper" file &&
747 git submodule add ../subsubsuper_update_r subsubmodule &&
748 git commit -am "subsubmodule" &&
749 git push origin master
751 git clone super_update_r super_update_r2 &&
752 (cd super_update_r2 &&
753 test_commit "update_super" file &&
754 git submodule add ../subsuper_update_r submodule &&
755 git commit -am "submodule" &&
756 git push origin master
758 rm -rf super_update_r2 &&
759 git clone super_update_r super_update_r2 &&
760 (cd super_update_r2 &&
761 git submodule update --init --recursive >actual &&
762 test_i18ngrep "Submodule path .submodule/subsubmodule.: checked out" actual &&
763 (cd submodule/subsubmodule &&
764 git log > ../../expected
766 (cd .git/modules/submodule/modules/subsubmodule
767 git log > ../../../../../actual
769 test_cmp actual expected
773 test_expect_success 'submodule add properly re-creates deeper level submodules' '
775 git reset --hard master &&
777 git submodule add --force ../submodule deeper/submodule
781 test_expect_success 'submodule update properly revives a moved submodule' '
783 H=$(git rev-parse --short HEAD) &&
784 git commit -am "pre move" &&
785 H2=$(git rev-parse --short HEAD) &&
786 git status | sed "s/$H/XXX/" >expect &&
787 H=$(cd submodule2; git rev-parse HEAD) &&
788 git rm --cached submodule2 &&
790 mkdir -p "moved/sub module" &&
791 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
792 git config -f .gitmodules submodule.submodule2.path "moved/sub module"
793 git commit -am "post move" &&
794 git submodule update &&
795 git status | sed "s/$H2/XXX/" >actual &&
796 test_cmp expect actual
800 test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
801 mkdir -p linked/dir &&
802 ln -s linked/dir linkto &&
804 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
806 git submodule update --init --recursive
811 test_expect_success 'submodule update clone shallow submodule' '
812 git clone cloned super3 &&
815 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
816 mv -f .gitmodules.tmp .gitmodules &&
817 git submodule update --init --depth=3
819 test 1 = $(git log --oneline | wc -l)
824 test_expect_success 'submodule update --recursive drops module name before recursing' '
826 (cd deeper/submodule/subsubmodule &&
829 git submodule update --recursive deeper/submodule >actual &&
830 test_i18ngrep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual