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 &&
72 test_expect_success
'submodule update detaching the HEAD ' '
73 (cd super/submodule &&
74 git reset --hard HEAD~1
80 git submodule update submodule &&
86 test_expect_success
'submodule update from subdirectory' '
87 (cd super/submodule &&
88 git reset --hard HEAD~1
95 git submodule update ../submodule &&
102 test_expect_success
'submodule update does not fetch already present commits' '
104 echo line3 >> file &&
107 git commit -m "upstream line3"
109 (cd super/submodule &&
110 head=$(git rev-parse --verify HEAD) &&
111 echo "Submodule path ${apos}submodule$apos: checked out $apos$head$apos" > ../../expected &&
112 git reset --hard HEAD~1
115 git submodule update > ../actual 2> ../actual.err
117 test_i18ncmp expected actual &&
121 test_expect_success
'submodule update should fail due to local changes' '
122 (cd super/submodule &&
123 git reset --hard HEAD~1 &&
124 echo "local change" > file
130 test_must_fail git submodule update submodule
133 test_expect_success
'submodule update should throw away changes with --force ' '
138 git submodule update --force submodule &&
144 test_expect_success
'submodule update --force forcibly checks out submodules' '
149 git submodule update --force submodule &&
151 test "$(git status -s file)" = ""
156 test_expect_success
'submodule update --remote should fetch upstream changes' '
158 echo line4 >> file &&
161 git commit -m "upstream line4"
164 git submodule update --remote --force submodule &&
166 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
170 test_expect_success
'local config should override .gitmodules branch' '
172 git checkout -b test-branch &&
173 echo line5 >> file &&
176 git commit -m "upstream line5" &&
180 git config submodule.submodule.branch test-branch &&
181 git submodule update --remote --force submodule &&
183 test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
187 test_expect_success
'submodule update --rebase staying on master' '
188 (cd super/submodule &&
195 git submodule update --rebase submodule &&
201 test_expect_success
'submodule update --merge staying on master' '
202 (cd super/submodule &&
203 git reset --hard HEAD~1
209 git submodule update --merge submodule &&
215 test_expect_success
'submodule update - rebase in .git/config' '
217 git config submodule.submodule.update rebase
219 (cd super/submodule &&
220 git reset --hard HEAD~1
226 git submodule update submodule &&
232 test_expect_success
'submodule update - checkout in .git/config but --rebase given' '
234 git config submodule.submodule.update checkout
236 (cd super/submodule &&
237 git reset --hard HEAD~1
243 git submodule update --rebase submodule &&
249 test_expect_success
'submodule update - merge in .git/config' '
251 git config submodule.submodule.update merge
253 (cd super/submodule &&
254 git reset --hard HEAD~1
260 git submodule update submodule &&
266 test_expect_success
'submodule update - checkout in .git/config but --merge given' '
268 git config submodule.submodule.update checkout
270 (cd super/submodule &&
271 git reset --hard HEAD~1
277 git submodule update --merge submodule &&
283 test_expect_success
'submodule update - checkout in .git/config' '
285 git config submodule.submodule.update checkout
287 (cd super/submodule &&
288 git reset --hard HEAD^
294 git submodule update submodule &&
300 test_expect_success
'submodule update - command in .git/config' '
302 git config submodule.submodule.update "!git checkout"
304 (cd super/submodule &&
305 git reset --hard HEAD^
311 git submodule update submodule &&
317 test_expect_success
'submodule update - command in .git/config catches failure' '
319 git config submodule.submodule.update "!false"
321 (cd super/submodule &&
322 git reset --hard HEAD^
325 test_must_fail git submodule update submodule
329 test_expect_success
'submodule init does not copy command into .git/config' '
331 H=$(git ls-files -s submodule | cut -d" " -f2) &&
333 git update-index --add --cacheinfo 160000 $H submodule1 &&
334 git config -f .gitmodules submodule.submodule1.path submodule1 &&
335 git config -f .gitmodules submodule.submodule1.url ../submodule &&
336 git config -f .gitmodules submodule.submodule1.update !false &&
337 git submodule init submodule1 &&
338 echo "none" >expect &&
339 git config submodule.submodule1.update >actual &&
340 test_cmp expect actual
344 test_expect_success
'submodule init picks up rebase' '
346 git config -f .gitmodules submodule.rebasing.update rebase &&
347 git submodule init rebasing &&
348 test "rebase" = "$(git config submodule.rebasing.update)"
352 test_expect_success
'submodule init picks up merge' '
354 git config -f .gitmodules submodule.merging.update merge &&
355 git submodule init merging &&
356 test "merge" = "$(git config submodule.merging.update)"
360 test_expect_success
'submodule update --merge - ignores --merge for new submodules' '
363 git submodule update submodule &&
364 git status -s submodule >expect &&
366 git submodule update --merge submodule &&
367 git status -s submodule >actual &&
368 test_cmp expect actual
372 test_expect_success
'submodule update --rebase - ignores --rebase for new submodules' '
375 git submodule update submodule &&
376 git status -s submodule >expect &&
378 git submodule update --rebase submodule &&
379 git status -s submodule >actual &&
380 test_cmp expect actual
384 test_expect_success
'submodule update ignores update=merge config for new submodules' '
387 git submodule update submodule &&
388 git status -s submodule >expect &&
390 git config submodule.submodule.update merge &&
391 git submodule update submodule &&
392 git status -s submodule >actual &&
393 git config --unset submodule.submodule.update &&
394 test_cmp expect actual
398 test_expect_success
'submodule update ignores update=rebase config for new submodules' '
401 git submodule update submodule &&
402 git status -s submodule >expect &&
404 git config submodule.submodule.update rebase &&
405 git submodule update submodule &&
406 git status -s submodule >actual &&
407 git config --unset submodule.submodule.update &&
408 test_cmp expect actual
412 test_expect_success
'submodule init picks up update=none' '
414 git config -f .gitmodules submodule.none.update none &&
415 git submodule init none &&
416 test "none" = "$(git config submodule.none.update)"
420 test_expect_success
'submodule update - update=none in .git/config' '
422 git config submodule.submodule.update none &&
424 git checkout master &&
427 git diff --raw | grep " submodule" &&
428 git submodule update &&
429 git diff --raw | grep " submodule" &&
433 git config --unset submodule.submodule.update &&
434 git submodule update submodule
438 test_expect_success
'submodule update - update=none in .git/config but --checkout given' '
440 git config submodule.submodule.update none &&
442 git checkout master &&
445 git diff --raw | grep " submodule" &&
446 git submodule update --checkout &&
447 test_must_fail git diff --raw \| grep " submodule" &&
449 test_must_fail compare_head
451 git config --unset submodule.submodule.update
455 test_expect_success
'submodule update --init skips submodule with update=none' '
457 git add .gitmodules &&
458 git commit -m ".gitmodules"
460 git clone super cloned &&
462 git submodule update --init &&
463 test -e submodule/.git &&
464 test_must_fail test -e none/.git
468 test_expect_success
'submodule update continues after checkout error' '
470 git reset --hard HEAD &&
471 git submodule add ../submodule submodule2 &&
472 git submodule init &&
473 git commit -am "new_submodule" &&
475 git rev-parse --verify HEAD >../expect
478 test_commit "update_submodule" file
481 test_commit "update_submodule2" file
484 git add submodule2 &&
485 git commit -m "two_new_submodule_commits" &&
489 git checkout HEAD^ &&
490 test_must_fail git submodule update &&
492 git rev-parse --verify HEAD >../actual
494 test_cmp expect actual
497 test_expect_success
'submodule update continues after recursive checkout error' '
499 git reset --hard HEAD &&
500 git checkout master &&
501 git submodule update &&
503 git submodule add ../submodule subsubmodule &&
504 git submodule init &&
505 git commit -m "new_subsubmodule"
508 git commit -m "update_submodule" &&
511 test_commit "update_subsubmodule" file
513 git add subsubmodule &&
514 test_commit "update_submodule_again" file &&
516 test_commit "update_subsubmodule_again" file
518 test_commit "update_submodule_again_again" file
521 git rev-parse --verify HEAD >../expect &&
522 test_commit "update_submodule2_again" file
525 git add submodule2 &&
526 git commit -m "new_commits" &&
527 git checkout HEAD^ &&
529 git checkout HEAD^ &&
534 test_must_fail git submodule update --recursive &&
536 git rev-parse --verify HEAD >../actual
538 test_cmp expect actual
542 test_expect_success
'submodule update exit immediately in case of merge conflict' '
544 git checkout master &&
545 git reset --hard HEAD &&
548 git reset --hard HEAD
551 git submodule update --recursive &&
553 test_commit "update_submodule_2" file
556 test_commit "update_submodule2_2" file
559 git add submodule2 &&
560 git commit -m "two_new_submodule_commits" &&
562 git checkout master &&
563 test_commit "conflict" file &&
564 echo "conflict" > file
566 git checkout HEAD^ &&
568 git rev-parse --verify HEAD >../expect
570 git config submodule.submodule.update merge &&
571 test_must_fail git submodule update &&
573 git rev-parse --verify HEAD >../actual
575 test_cmp expect actual
579 test_expect_success
'submodule update exit immediately after recursive rebase error' '
581 git checkout master &&
582 git reset --hard HEAD &&
584 git reset --hard HEAD &&
585 git submodule update --recursive
588 test_commit "update_submodule_3" file
591 test_commit "update_submodule2_3" file
594 git add submodule2 &&
595 git commit -m "two_new_submodule_commits" &&
597 git checkout master &&
598 test_commit "conflict2" file &&
599 echo "conflict" > file
601 git checkout HEAD^ &&
603 git rev-parse --verify HEAD >../expect
605 git config submodule.submodule.update rebase &&
606 test_must_fail git submodule update &&
608 git rev-parse --verify HEAD >../actual
610 test_cmp expect actual
614 test_expect_success
'add different submodules to the same path' '
616 git submodule add ../submodule s1 &&
617 test_must_fail git submodule add ../merging s1
621 test_expect_success
'submodule add places git-dir in superprojects git-dir' '
624 git submodule add ../submodule deeper/submodule &&
625 (cd deeper/submodule &&
626 git log > ../../expected
628 (cd .git/modules/deeper/submodule &&
629 git log > ../../../../actual
631 test_cmp actual expected
635 test_expect_success
'submodule update places git-dir in superprojects git-dir' '
637 git commit -m "added submodule"
639 git clone super super2 &&
641 git submodule init deeper/submodule &&
642 git submodule update &&
643 (cd deeper/submodule &&
644 git log > ../../expected
646 (cd .git/modules/deeper/submodule &&
647 git log > ../../../../actual
649 test_cmp actual expected
653 test_expect_success
'submodule add places git-dir in superprojects git-dir recursive' '
655 (cd deeper/submodule &&
656 git submodule add ../submodule subsubmodule &&
658 git log > ../../../expected
660 git commit -m "added subsubmodule" &&
663 (cd .git/modules/deeper/submodule/modules/subsubmodule &&
664 git log > ../../../../../actual
666 git add deeper/submodule &&
667 git commit -m "update submodule" &&
669 test_cmp actual expected
673 test_expect_success
'submodule update places git-dir in superprojects git-dir recursive' '
674 mkdir super_update_r &&
675 (cd super_update_r &&
678 mkdir subsuper_update_r &&
679 (cd subsuper_update_r &&
682 mkdir subsubsuper_update_r &&
683 (cd subsubsuper_update_r &&
686 git clone subsubsuper_update_r subsubsuper_update_r2 &&
687 (cd subsubsuper_update_r2 &&
688 test_commit "update_subsubsuper" file &&
689 git push origin master
691 git clone subsuper_update_r subsuper_update_r2 &&
692 (cd subsuper_update_r2 &&
693 test_commit "update_subsuper" file &&
694 git submodule add ../subsubsuper_update_r subsubmodule &&
695 git commit -am "subsubmodule" &&
696 git push origin master
698 git clone super_update_r super_update_r2 &&
699 (cd super_update_r2 &&
700 test_commit "update_super" file &&
701 git submodule add ../subsuper_update_r submodule &&
702 git commit -am "submodule" &&
703 git push origin master
705 rm -rf super_update_r2 &&
706 git clone super_update_r super_update_r2 &&
707 (cd super_update_r2 &&
708 git submodule update --init --recursive >actual &&
709 test_i18ngrep "Submodule path .submodule/subsubmodule.: .git reset --hard -q" actual &&
710 (cd submodule/subsubmodule &&
711 git log > ../../expected
713 (cd .git/modules/submodule/modules/subsubmodule
714 git log > ../../../../../actual
716 test_cmp actual expected
720 test_expect_success
'submodule add properly re-creates deeper level submodules' '
722 git reset --hard master &&
724 git submodule add --force ../submodule deeper/submodule
728 test_expect_success
'submodule update properly revives a moved submodule' '
730 H=$(git rev-parse --short HEAD) &&
731 git commit -am "pre move" &&
732 H2=$(git rev-parse --short HEAD) &&
733 git status | sed "s/$H/XXX/" >expect &&
734 H=$(cd submodule2; git rev-parse HEAD) &&
735 git rm --cached submodule2 &&
737 mkdir -p "moved/sub module" &&
738 git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
739 git config -f .gitmodules submodule.submodule2.path "moved/sub module"
740 git commit -am "post move" &&
741 git submodule update &&
742 git status | sed "s/$H2/XXX/" >actual &&
743 test_cmp expect actual
747 test_expect_success SYMLINKS
'submodule update can handle symbolic links in pwd' '
748 mkdir -p linked/dir &&
749 ln -s linked/dir linkto &&
751 git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
753 git submodule update --init --recursive
758 test_expect_success
'submodule update clone shallow submodule' '
759 git clone cloned super3 &&
762 sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
763 mv -f .gitmodules.tmp .gitmodules &&
764 git submodule update --init --depth=3
766 test 1 = $(git log --oneline | wc -l)
771 test_expect_success
'submodule update --checkout clones detached HEAD' '
772 git clone super super4 &&
773 echo "detached HEAD" >expected &&
775 git reset --hard initial-setup &&
776 git submodule init submodule &&
777 git submodule update >> /tmp/log 2>&1 &&
779 git symbolic-ref HEAD > ../../actual ||
780 echo "detached HEAD" > ../../actual
783 test_cmp actual expected &&
787 test_expect_success
'submodule update --merge clones attached HEAD' '
788 git clone super super4 &&
789 echo "refs/heads/master" >expected &&
791 git reset --hard initial-setup &&
792 git submodule init submodule &&
793 git config submodule.submodule.update merge &&
794 git submodule update --merge &&
796 git symbolic-ref HEAD > ../../actual ||
797 echo "detached HEAD" > ../../actual
800 test_cmp actual expected &&