3 # Copyright (c) 2007 Lars Hjemli
6 test_description
='Basic porcelain support for submodules
8 This test tries to verify basic sanity of the init, update and status
9 subcommands of git submodule.
14 test_expect_success
'setup - initial commit' '
17 git commit -m "initial commit" &&
21 test_expect_success
'setup - repository in init subdirectory' '
28 git commit -m "submodule commit 1" &&
29 git tag -a -m "rev-1" rev-1
33 test_expect_success
'setup - commit with gitlink' '
37 git commit -m "super commit 1"
40 test_expect_success
'setup - hide init subdirectory' '
44 test_expect_success
'setup - repository to add submodules to' '
46 git init addtest-ignore
49 # The 'submodule add' tests need some repository to add as a submodule.
50 # The trash directory is a good one as any. We need to canonicalize
51 # the name, though, as some tests compare it to the absolute path git
52 # generates, which will expand symbolic links.
56 git for-each-ref
--format='%(refname)' 'refs/heads/*'
65 listbranches
>"$dotdot/heads" &&
66 { git symbolic-ref HEAD ||
:; } >"$dotdot/head" &&
67 git rev-parse HEAD
>"$dotdot/head-sha1" &&
68 git update-index
--refresh &&
69 git diff-files
--exit-code &&
70 git clean
-n -d -x >"$dotdot/untracked"
74 test_expect_success
'submodule add' '
75 echo "refs/heads/master" >expect &&
80 git submodule add -q "$submodurl" submod >actual &&
82 echo "gitdir: ../.git/modules/submod" >expect &&
83 test_cmp expect submod/.git &&
86 git config core.worktree >actual &&
87 echo "../../../submod" >expect &&
88 test_cmp expect actual &&
94 rm -f heads head untracked &&
95 inspect addtest/submod ../.. &&
96 test_cmp expect heads &&
97 test_cmp expect head &&
98 test_cmp empty untracked
101 test_expect_success
'submodule add to .gitignored path fails' '
104 cat <<-\EOF >expect &&
105 The following path is ignored by one of your .gitignore files:
107 Use -f if you really want to add it.
109 # Does not use test_commit due to the ignore
110 echo "*" > .gitignore &&
111 git add --force .gitignore &&
112 git commit -m"Ignore everything" &&
113 ! git submodule add "$submodurl" submod >actual 2>&1 &&
114 test_i18ncmp expect actual
118 test_expect_success
'submodule add to .gitignored path with --force' '
121 git submodule add --force "$submodurl" submod
125 test_expect_success
'submodule add --branch' '
126 echo "refs/heads/initial" >expect-head &&
127 cat <<-\EOF >expect-heads &&
135 git submodule add -b initial "$submodurl" submod-branch &&
136 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
140 rm -f heads head untracked &&
141 inspect addtest/submod-branch ../.. &&
142 test_cmp expect-heads heads &&
143 test_cmp expect-head head &&
144 test_cmp empty untracked
147 test_expect_success
'submodule add with ./ in path' '
148 echo "refs/heads/master" >expect &&
153 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
157 rm -f heads head untracked &&
158 inspect addtest/dotsubmod/frotz ../../.. &&
159 test_cmp expect heads &&
160 test_cmp expect head &&
161 test_cmp empty untracked
164 test_expect_success
'submodule add with // in path' '
165 echo "refs/heads/master" >expect &&
170 git submodule add "$submodurl" slashslashsubmod///frotz// &&
174 rm -f heads head untracked &&
175 inspect addtest/slashslashsubmod/frotz ../../.. &&
176 test_cmp expect heads &&
177 test_cmp expect head &&
178 test_cmp empty untracked
181 test_expect_success
'submodule add with /.. in path' '
182 echo "refs/heads/master" >expect &&
187 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
191 rm -f heads head untracked &&
192 inspect addtest/realsubmod ../.. &&
193 test_cmp expect heads &&
194 test_cmp expect head &&
195 test_cmp empty untracked
198 test_expect_success
'submodule add with ./, /.. and // in path' '
199 echo "refs/heads/master" >expect &&
204 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
208 rm -f heads head untracked &&
209 inspect addtest/realsubmod2 ../.. &&
210 test_cmp expect heads &&
211 test_cmp expect head &&
212 test_cmp empty untracked
215 test_expect_success
'setup - add an example entry to .gitmodules' '
216 GIT_CONFIG=.gitmodules \
217 git config submodule.example.url git://example.com/init.git
220 test_expect_success
'status should fail for unmapped paths' '
221 test_must_fail git submodule status
224 test_expect_success
'setup - map path in .gitmodules' '
225 cat <<\EOF >expect &&
226 [submodule "example"]
227 url = git://example.com/init.git
231 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
233 test_cmp expect .gitmodules
236 test_expect_success
'status should only print one line' '
237 git submodule status >lines &&
238 test_line_count = 1 lines
241 test_expect_success
'setup - fetch commit name from submodule' '
242 rev1=$(cd .subrepo && git rev-parse HEAD) &&
243 printf "rev1: %s\n" "$rev1" &&
247 test_expect_success
'status should initially be "missing"' '
248 git submodule status >lines &&
252 test_expect_success
'init should register submodule url in .git/config' '
253 echo git://example.com/init.git >expect &&
255 git submodule init &&
256 git config submodule.example.url >url &&
257 git config submodule.example.url ./.subrepo &&
262 test_failure_with_unknown_submodule
() {
263 test_must_fail git submodule
$1 no-such-submodule
2>output.err
&&
264 grep "^error: .*no-such-submodule" output.err
267 test_expect_success
'init should fail with unknown submodule' '
268 test_failure_with_unknown_submodule init
271 test_expect_success
'update should fail with unknown submodule' '
272 test_failure_with_unknown_submodule update
275 test_expect_success
'status should fail with unknown submodule' '
276 test_failure_with_unknown_submodule status
279 test_expect_success
'sync should fail with unknown submodule' '
280 test_failure_with_unknown_submodule sync
283 test_expect_success
'update should fail when path is used by a file' '
284 echo hello >expect &&
286 echo "hello" >init &&
287 test_must_fail git submodule update &&
292 test_expect_success
'update should fail when path is used by a nonempty directory' '
293 echo hello >expect &&
297 echo "hello" >init/a &&
299 test_must_fail git submodule update &&
301 test_cmp expect init/a
304 test_expect_success
'update should work when path is an empty dir' '
307 echo "$rev1" >expect &&
310 git submodule update -q >update.out &&
311 test ! -s update.out &&
314 test_cmp expect head-sha1
317 test_expect_success
'status should be "up-to-date" after update' '
318 git submodule status >list &&
322 test_expect_success
'status should be "modified" after submodule commit' '
327 git commit -m "submodule commit 2"
330 rev2=$(cd init && git rev-parse HEAD) &&
332 git submodule status >list &&
337 test_expect_success
'the --cached sha1 should be rev1' '
338 git submodule --cached status >list &&
342 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
344 grep "^+Subproject commit $rev2" diff
347 test_expect_success
'update should checkout rev1' '
349 echo "$rev1" >expect &&
351 git submodule update init &&
354 test_cmp expect head-sha1
357 test_expect_success
'status should be "up-to-date" after update' '
358 git submodule status >list &&
362 test_expect_success
'checkout superproject with subproject already present' '
363 git checkout initial &&
367 test_expect_success
'apply submodule diff' '
375 git commit -m "change subproject"
377 git update-index --add init &&
378 git commit -m "change init" &&
379 git format-patch -1 --stdout >P.diff &&
380 git checkout second &&
381 git apply --index P.diff &&
383 git diff --cached master >staged &&
384 test_cmp empty staged
387 test_expect_success
'update --init' '
389 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
390 git config --remove-section submodule.example &&
391 test_must_fail git config submodule.example.url &&
393 git submodule update init > update.out &&
395 test_i18ngrep "not initialized" update.out &&
396 test_must_fail git rev-parse --resolve-git-dir init/.git &&
398 git submodule update --init init &&
399 git rev-parse --resolve-git-dir init/.git
402 test_expect_success
'do not add files from a submodule' '
405 test_must_fail git add init/a
409 test_expect_success
'gracefully add submodule with a trailing slash' '
412 git commit -m "commit subproject" init &&
416 git diff --exit-code --cached init &&
418 git commit -m update a >/dev/null &&
419 git rev-parse HEAD) &&
421 test_must_fail git diff --exit-code --cached init &&
422 test $commit = $(git ls-files --stage |
423 sed -n "s/^160000 \([^ ]*\).*/\1/p")
427 test_expect_success
'ls-files gracefully handles trailing slash' '
429 test "init" = "$(git ls-files init/)"
433 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
437 git checkout initial &&
442 test_expect_success
'submodule <invalid-subcommand> fails' '
443 test_must_fail git submodule no-such-subcommand
446 test_expect_success
'add submodules without specifying an explicit path' '
453 git commit -m "repo commit 1"
455 git clone --bare repo/ bare.git &&
458 git submodule add "$submodurl/repo" &&
459 git config -f .gitmodules submodule.repo.path repo &&
460 git submodule add "$submodurl/bare.git" &&
461 git config -f .gitmodules submodule.bare.path bare
465 test_expect_success
'add should fail when path is used by a file' '
469 test_must_fail git submodule add "$submodurl/repo" file
473 test_expect_success
'add should fail when path is used by an existing directory' '
477 test_must_fail git submodule add "$submodurl/repo" empty-dir
481 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
484 git submodule add ../repo relative &&
485 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
486 git submodule sync relative &&
487 test "$(git config submodule.relative.url)" = "$submodurl/repo"
491 test_expect_success
'set up for relative path tests' '
503 git config -f .gitmodules submodule.sub.path sub &&
504 git config -f .gitmodules submodule.sub.url ../subrepo &&
505 cp .git/config pristine-.git-config &&
506 cp .gitmodules pristine-.gitmodules
510 test_expect_success
'../subrepo works with URL - ssh://hostname/repo' '
513 cp pristine-.git-config .git/config &&
514 cp pristine-.gitmodules .gitmodules &&
515 git config remote.origin.url ssh://hostname/repo &&
516 git submodule init &&
517 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
521 test_expect_success
'../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
524 cp pristine-.git-config .git/config &&
525 cp pristine-.gitmodules .gitmodules &&
526 git config remote.origin.url ssh://hostname:22/repo &&
527 git submodule init &&
528 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
532 # About the choice of the path in the next test:
533 # - double-slash side-steps path mangling issues on Windows
534 # - it is still an absolute local path
535 # - there cannot be a server with a blank in its name just in case the
536 # path is used erroneously to access a //server/share style path
537 test_expect_success
'../subrepo path works with local path - //somewhere else/repo' '
540 cp pristine-.git-config .git/config &&
541 cp pristine-.gitmodules .gitmodules &&
542 git config remote.origin.url "//somewhere else/repo" &&
543 git submodule init &&
544 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
548 test_expect_success
'../subrepo works with file URL - file:///tmp/repo' '
551 cp pristine-.git-config .git/config &&
552 cp pristine-.gitmodules .gitmodules &&
553 git config remote.origin.url file:///tmp/repo &&
554 git submodule init &&
555 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
559 test_expect_success
'../subrepo works with helper URL- helper:://hostname/repo' '
562 cp pristine-.git-config .git/config &&
563 cp pristine-.gitmodules .gitmodules &&
564 git config remote.origin.url helper:://hostname/repo &&
565 git submodule init &&
566 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
570 test_expect_success
'../subrepo works with scp-style URL - user@host:repo' '
573 cp pristine-.git-config .git/config &&
574 git config remote.origin.url user@host:repo &&
575 git submodule init &&
576 test "$(git config submodule.sub.url)" = user@host:subrepo
580 test_expect_success
'../subrepo works with scp-style URL - user@host:path/to/repo' '
583 cp pristine-.git-config .git/config &&
584 cp pristine-.gitmodules .gitmodules &&
585 git config remote.origin.url user@host:path/to/repo &&
586 git submodule init &&
587 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
591 test_expect_success
'../subrepo works with relative local path - foo' '
594 cp pristine-.git-config .git/config &&
595 cp pristine-.gitmodules .gitmodules &&
596 git config remote.origin.url foo &&
597 # actual: fails with an error
598 git submodule init &&
599 test "$(git config submodule.sub.url)" = subrepo
603 test_expect_success
'../subrepo works with relative local path - foo/bar' '
606 cp pristine-.git-config .git/config &&
607 cp pristine-.gitmodules .gitmodules &&
608 git config remote.origin.url foo/bar &&
609 git submodule init &&
610 test "$(git config submodule.sub.url)" = foo/subrepo
614 test_expect_success
'../subrepo works with relative local path - ./foo' '
617 cp pristine-.git-config .git/config &&
618 cp pristine-.gitmodules .gitmodules &&
619 git config remote.origin.url ./foo &&
620 git submodule init &&
621 test "$(git config submodule.sub.url)" = subrepo
625 test_expect_success
'../subrepo works with relative local path - ./foo/bar' '
628 cp pristine-.git-config .git/config &&
629 cp pristine-.gitmodules .gitmodules &&
630 git config remote.origin.url ./foo/bar &&
631 git submodule init &&
632 test "$(git config submodule.sub.url)" = foo/subrepo
636 test_expect_success
'../subrepo works with relative local path - ../foo' '
639 cp pristine-.git-config .git/config &&
640 cp pristine-.gitmodules .gitmodules &&
641 git config remote.origin.url ../foo &&
642 git submodule init &&
643 test "$(git config submodule.sub.url)" = ../subrepo
647 test_expect_success
'../subrepo works with relative local path - ../foo/bar' '
650 cp pristine-.git-config .git/config &&
651 cp pristine-.gitmodules .gitmodules &&
652 git config remote.origin.url ../foo/bar &&
653 git submodule init &&
654 test "$(git config submodule.sub.url)" = ../foo/subrepo
658 test_expect_success
'../bar/a/b/c works with relative local path - ../foo/bar.git' '
661 cp pristine-.git-config .git/config &&
662 cp pristine-.gitmodules .gitmodules &&
664 (cd a/b/c; git init) &&
665 git config remote.origin.url ../foo/bar.git &&
666 git submodule add ../bar/a/b/c ./a/b/c &&
667 git submodule init &&
668 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
672 test_expect_success
'moving the superproject does not break submodules' '
675 git submodule status >expect
677 mv addtest addtest2 &&
680 git submodule status >actual &&
681 test_cmp expect actual
685 test_expect_success
'submodule add --name allows to replace a submodule with another at the same path' '
690 echo "$submodurl/repo" >expect &&
691 git config remote.origin.url >actual &&
692 test_cmp expect actual &&
693 echo "gitdir: ../.git/modules/repo" >expect &&
698 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
700 echo "gitdir: ../.git/modules/submod" >expect &&
701 test_cmp expect submod/.git &&
704 echo "$submodurl/bare.git" >expect &&
705 git config remote.origin.url >actual &&
706 test_cmp expect actual &&
707 echo "gitdir: ../.git/modules/repo_new" >expect &&
710 echo "repo" >expect &&
711 git config -f .gitmodules submodule.repo.path >actual &&
712 test_cmp expect actual &&
713 git config -f .gitmodules submodule.repo_new.path >actual &&
714 test_cmp expect actual&&
715 echo "$submodurl/repo" >expect &&
716 git config -f .gitmodules submodule.repo.url >actual &&
717 test_cmp expect actual &&
718 echo "$submodurl/bare.git" >expect &&
719 git config -f .gitmodules submodule.repo_new.url >actual &&
720 test_cmp expect actual &&
721 echo "$submodurl/repo" >expect &&
722 git config submodule.repo.url >actual &&
723 test_cmp expect actual &&
724 echo "$submodurl/bare.git" >expect &&
725 git config submodule.repo_new.url >actual &&
726 test_cmp expect actual
730 test_expect_success
'submodule add with an existing name fails unless forced' '
735 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
737 echo "repo" >expect &&
738 git config -f .gitmodules submodule.repo_new.path >actual &&
739 test_cmp expect actual&&
740 echo "$submodurl/bare.git" >expect &&
741 git config -f .gitmodules submodule.repo_new.url >actual &&
742 test_cmp expect actual &&
743 echo "$submodurl/bare.git" >expect &&
744 git config submodule.repo_new.url >actual &&
745 test_cmp expect actual &&
746 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
748 echo "repo" >expect &&
749 git config -f .gitmodules submodule.repo_new.path >actual &&
750 test_cmp expect actual&&
751 echo "$submodurl/repo.git" >expect &&
752 git config -f .gitmodules submodule.repo_new.url >actual &&
753 test_cmp expect actual &&
754 echo "$submodurl/repo.git" >expect &&
755 git config submodule.repo_new.url >actual &&
756 test_cmp expect actual
760 test_expect_success
'set up a second submodule' '
761 git submodule add ./init2 example2 &&
762 git commit -m "submodule example2 added"
765 test_expect_success
'submodule deinit should remove the whole submodule section from .git/config' '
766 git config submodule.example.foo bar &&
767 git config submodule.example2.frotz nitfol &&
768 git submodule deinit init &&
769 test -z "$(git config --get-regexp "submodule\.example\.")" &&
770 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
771 test -f example2/.git &&
775 test_expect_success
'submodule deinit . deinits all initialized submodules' '
776 git submodule update --init &&
777 git config submodule.example.foo bar &&
778 git config submodule.example2.frotz nitfol &&
779 test_must_fail git submodule deinit &&
780 git submodule deinit . >actual &&
781 test -z "$(git config --get-regexp "submodule\.example\.")" &&
782 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
783 test_i18ngrep "Cleared directory .init" actual &&
784 test_i18ngrep "Cleared directory .example2" actual &&
788 test_expect_success
'submodule deinit deinits a submodule when its work tree is missing or empty' '
789 git submodule update --init &&
790 rm -rf init example2/* example2/.git &&
791 git submodule deinit init example2 >actual &&
792 test -z "$(git config --get-regexp "submodule\.example\.")" &&
793 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
794 test_i18ngrep ! "Cleared directory .init" actual &&
795 test_i18ngrep "Cleared directory .example2" actual &&
799 test_expect_success
'submodule deinit fails when the submodule contains modifications unless forced' '
800 git submodule update --init &&
802 test_must_fail git submodule deinit init &&
803 test -n "$(git config --get-regexp "submodule\.example\.")" &&
804 test -f example2/.git &&
805 git submodule deinit -f init >actual &&
806 test -z "$(git config --get-regexp "submodule\.example\.")" &&
807 test_i18ngrep "Cleared directory .init" actual &&
811 test_expect_success
'submodule deinit fails when the submodule contains untracked files unless forced' '
812 git submodule update --init &&
813 echo X >>init/untracked &&
814 test_must_fail git submodule deinit init &&
815 test -n "$(git config --get-regexp "submodule\.example\.")" &&
816 test -f example2/.git &&
817 git submodule deinit -f init >actual &&
818 test -z "$(git config --get-regexp "submodule\.example\.")" &&
819 test_i18ngrep "Cleared directory .init" actual &&
823 test_expect_success
'submodule deinit fails when the submodule HEAD does not match unless forced' '
824 git submodule update --init &&
829 test_must_fail git submodule deinit init &&
830 test -n "$(git config --get-regexp "submodule\.example\.")" &&
831 test -f example2/.git &&
832 git submodule deinit -f init >actual &&
833 test -z "$(git config --get-regexp "submodule\.example\.")" &&
834 test_i18ngrep "Cleared directory .init" actual &&
838 test_expect_success
'submodule deinit is silent when used on an uninitialized submodule' '
839 git submodule update --init &&
840 git submodule deinit init >actual &&
841 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
842 test_i18ngrep "Cleared directory .init" actual &&
843 git submodule deinit init >actual &&
844 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
845 test_i18ngrep "Cleared directory .init" actual &&
846 git submodule deinit . >actual &&
847 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
848 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
849 test_i18ngrep "Cleared directory .init" actual &&
850 git submodule deinit . >actual &&
851 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
852 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
853 test_i18ngrep "Cleared directory .init" actual &&
857 test_expect_success
'submodule deinit fails when submodule has a .git directory even when forced' '
858 git submodule update --init &&
862 cp -R ../.git/modules/example .git &&
863 GIT_WORK_TREE=. git config --unset core.worktree
865 test_must_fail git submodule deinit init &&
866 test_must_fail git submodule deinit -f init &&
868 test -n "$(git config --get-regexp "submodule\.example\.")"