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
'submodule add in subdir' '
216 echo "refs/heads/master" >expect &&
222 git submodule add "$submodurl" ../realsubmod3 &&
226 rm -f heads head untracked &&
227 inspect addtest/realsubmod3 ../.. &&
228 test_cmp expect heads &&
229 test_cmp expect head &&
230 test_cmp empty untracked
233 test_expect_success
'setup - add an example entry to .gitmodules' '
234 GIT_CONFIG=.gitmodules \
235 git config submodule.example.url git://example.com/init.git
238 test_expect_success
'status should fail for unmapped paths' '
239 test_must_fail git submodule status
242 test_expect_success
'setup - map path in .gitmodules' '
243 cat <<\EOF >expect &&
244 [submodule "example"]
245 url = git://example.com/init.git
249 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
251 test_cmp expect .gitmodules
254 test_expect_success
'status should only print one line' '
255 git submodule status >lines &&
256 test_line_count = 1 lines
259 test_expect_success
'setup - fetch commit name from submodule' '
260 rev1=$(cd .subrepo && git rev-parse HEAD) &&
261 printf "rev1: %s\n" "$rev1" &&
265 test_expect_success
'status should initially be "missing"' '
266 git submodule status >lines &&
270 test_expect_success
'init should register submodule url in .git/config' '
271 echo git://example.com/init.git >expect &&
273 git submodule init &&
274 git config submodule.example.url >url &&
275 git config submodule.example.url ./.subrepo &&
280 test_failure_with_unknown_submodule
() {
281 test_must_fail git submodule
$1 no-such-submodule
2>output.err
&&
282 grep "^error: .*no-such-submodule" output.err
285 test_expect_success
'init should fail with unknown submodule' '
286 test_failure_with_unknown_submodule init
289 test_expect_success
'update should fail with unknown submodule' '
290 test_failure_with_unknown_submodule update
293 test_expect_success
'status should fail with unknown submodule' '
294 test_failure_with_unknown_submodule status
297 test_expect_success
'sync should fail with unknown submodule' '
298 test_failure_with_unknown_submodule sync
301 test_expect_success
'update should fail when path is used by a file' '
302 echo hello >expect &&
304 echo "hello" >init &&
305 test_must_fail git submodule update &&
310 test_expect_success
'update should fail when path is used by a nonempty directory' '
311 echo hello >expect &&
315 echo "hello" >init/a &&
317 test_must_fail git submodule update &&
319 test_cmp expect init/a
322 test_expect_success
'update should work when path is an empty dir' '
325 echo "$rev1" >expect &&
328 git submodule update -q >update.out &&
329 test ! -s update.out &&
332 test_cmp expect head-sha1
335 test_expect_success
'status should be "up-to-date" after update' '
336 git submodule status >list &&
340 test_expect_success
'status works correctly from a subdirectory' '
344 git submodule status >../list
349 test_expect_success
'status should be "modified" after submodule commit' '
354 git commit -m "submodule commit 2"
357 rev2=$(cd init && git rev-parse HEAD) &&
359 git submodule status >list &&
364 test_expect_success
'the --cached sha1 should be rev1' '
365 git submodule --cached status >list &&
369 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
371 grep "^+Subproject commit $rev2" diff
374 test_expect_success
'update should checkout rev1' '
376 echo "$rev1" >expect &&
378 git submodule update init &&
381 test_cmp expect head-sha1
384 test_expect_success
'status should be "up-to-date" after update' '
385 git submodule status >list &&
389 test_expect_success
'checkout superproject with subproject already present' '
390 git checkout initial &&
394 test_expect_success
'apply submodule diff' '
402 git commit -m "change subproject"
404 git update-index --add init &&
405 git commit -m "change init" &&
406 git format-patch -1 --stdout >P.diff &&
407 git checkout second &&
408 git apply --index P.diff &&
410 git diff --cached master >staged &&
411 test_cmp empty staged
414 test_expect_success
'update --init' '
416 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
417 git config --remove-section submodule.example &&
418 test_must_fail git config submodule.example.url &&
420 git submodule update init > update.out &&
422 test_i18ngrep "not initialized" update.out &&
423 test_must_fail git rev-parse --resolve-git-dir init/.git &&
425 git submodule update --init init &&
426 git rev-parse --resolve-git-dir init/.git
429 test_expect_success
'do not add files from a submodule' '
432 test_must_fail git add init/a
436 test_expect_success
'gracefully add submodule with a trailing slash' '
439 git commit -m "commit subproject" init &&
443 git diff --exit-code --cached init &&
445 git commit -m update a >/dev/null &&
446 git rev-parse HEAD) &&
448 test_must_fail git diff --exit-code --cached init &&
449 test $commit = $(git ls-files --stage |
450 sed -n "s/^160000 \([^ ]*\).*/\1/p")
454 test_expect_success
'ls-files gracefully handles trailing slash' '
456 test "init" = "$(git ls-files init/)"
460 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
464 git checkout initial &&
469 test_expect_success
'submodule <invalid-subcommand> fails' '
470 test_must_fail git submodule no-such-subcommand
473 test_expect_success
'add submodules without specifying an explicit path' '
480 git commit -m "repo commit 1"
482 git clone --bare repo/ bare.git &&
485 git submodule add "$submodurl/repo" &&
486 git config -f .gitmodules submodule.repo.path repo &&
487 git submodule add "$submodurl/bare.git" &&
488 git config -f .gitmodules submodule.bare.path bare
492 test_expect_success
'add should fail when path is used by a file' '
496 test_must_fail git submodule add "$submodurl/repo" file
500 test_expect_success
'add should fail when path is used by an existing directory' '
504 test_must_fail git submodule add "$submodurl/repo" empty-dir
508 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
511 git submodule add ../repo relative &&
512 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
513 git submodule sync relative &&
514 test "$(git config submodule.relative.url)" = "$submodurl/repo"
518 test_expect_success
'set up for relative path tests' '
530 git config -f .gitmodules submodule.sub.path sub &&
531 git config -f .gitmodules submodule.sub.url ../subrepo &&
532 cp .git/config pristine-.git-config &&
533 cp .gitmodules pristine-.gitmodules
537 test_expect_success
'../subrepo works with URL - ssh://hostname/repo' '
540 cp pristine-.git-config .git/config &&
541 cp pristine-.gitmodules .gitmodules &&
542 git config remote.origin.url ssh://hostname/repo &&
543 git submodule init &&
544 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
548 test_expect_success
'../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
551 cp pristine-.git-config .git/config &&
552 cp pristine-.gitmodules .gitmodules &&
553 git config remote.origin.url ssh://hostname:22/repo &&
554 git submodule init &&
555 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
559 # About the choice of the path in the next test:
560 # - double-slash side-steps path mangling issues on Windows
561 # - it is still an absolute local path
562 # - there cannot be a server with a blank in its name just in case the
563 # path is used erroneously to access a //server/share style path
564 test_expect_success
'../subrepo path works with local path - //somewhere else/repo' '
567 cp pristine-.git-config .git/config &&
568 cp pristine-.gitmodules .gitmodules &&
569 git config remote.origin.url "//somewhere else/repo" &&
570 git submodule init &&
571 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
575 test_expect_success
'../subrepo works with file URL - file:///tmp/repo' '
578 cp pristine-.git-config .git/config &&
579 cp pristine-.gitmodules .gitmodules &&
580 git config remote.origin.url file:///tmp/repo &&
581 git submodule init &&
582 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
586 test_expect_success
'../subrepo works with helper URL- helper:://hostname/repo' '
589 cp pristine-.git-config .git/config &&
590 cp pristine-.gitmodules .gitmodules &&
591 git config remote.origin.url helper:://hostname/repo &&
592 git submodule init &&
593 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
597 test_expect_success
'../subrepo works with scp-style URL - user@host:repo' '
600 cp pristine-.git-config .git/config &&
601 git config remote.origin.url user@host:repo &&
602 git submodule init &&
603 test "$(git config submodule.sub.url)" = user@host:subrepo
607 test_expect_success
'../subrepo works with scp-style URL - user@host:path/to/repo' '
610 cp pristine-.git-config .git/config &&
611 cp pristine-.gitmodules .gitmodules &&
612 git config remote.origin.url user@host:path/to/repo &&
613 git submodule init &&
614 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
618 test_expect_success
'../subrepo works with relative local path - foo' '
621 cp pristine-.git-config .git/config &&
622 cp pristine-.gitmodules .gitmodules &&
623 git config remote.origin.url foo &&
624 # actual: fails with an error
625 git submodule init &&
626 test "$(git config submodule.sub.url)" = subrepo
630 test_expect_success
'../subrepo works with relative local path - foo/bar' '
633 cp pristine-.git-config .git/config &&
634 cp pristine-.gitmodules .gitmodules &&
635 git config remote.origin.url foo/bar &&
636 git submodule init &&
637 test "$(git config submodule.sub.url)" = foo/subrepo
641 test_expect_success
'../subrepo works with relative local path - ./foo' '
644 cp pristine-.git-config .git/config &&
645 cp pristine-.gitmodules .gitmodules &&
646 git config remote.origin.url ./foo &&
647 git submodule init &&
648 test "$(git config submodule.sub.url)" = subrepo
652 test_expect_success
'../subrepo works with relative local path - ./foo/bar' '
655 cp pristine-.git-config .git/config &&
656 cp pristine-.gitmodules .gitmodules &&
657 git config remote.origin.url ./foo/bar &&
658 git submodule init &&
659 test "$(git config submodule.sub.url)" = foo/subrepo
663 test_expect_success
'../subrepo works with relative local path - ../foo' '
666 cp pristine-.git-config .git/config &&
667 cp pristine-.gitmodules .gitmodules &&
668 git config remote.origin.url ../foo &&
669 git submodule init &&
670 test "$(git config submodule.sub.url)" = ../subrepo
674 test_expect_success
'../subrepo works with relative local path - ../foo/bar' '
677 cp pristine-.git-config .git/config &&
678 cp pristine-.gitmodules .gitmodules &&
679 git config remote.origin.url ../foo/bar &&
680 git submodule init &&
681 test "$(git config submodule.sub.url)" = ../foo/subrepo
685 test_expect_success
'../bar/a/b/c works with relative local path - ../foo/bar.git' '
688 cp pristine-.git-config .git/config &&
689 cp pristine-.gitmodules .gitmodules &&
691 (cd a/b/c; git init) &&
692 git config remote.origin.url ../foo/bar.git &&
693 git submodule add ../bar/a/b/c ./a/b/c &&
694 git submodule init &&
695 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
699 test_expect_success
'moving the superproject does not break submodules' '
702 git submodule status >expect
704 mv addtest addtest2 &&
707 git submodule status >actual &&
708 test_cmp expect actual
712 test_expect_success
'submodule add --name allows to replace a submodule with another at the same path' '
717 echo "$submodurl/repo" >expect &&
718 git config remote.origin.url >actual &&
719 test_cmp expect actual &&
720 echo "gitdir: ../.git/modules/repo" >expect &&
725 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
727 echo "gitdir: ../.git/modules/submod" >expect &&
728 test_cmp expect submod/.git &&
731 echo "$submodurl/bare.git" >expect &&
732 git config remote.origin.url >actual &&
733 test_cmp expect actual &&
734 echo "gitdir: ../.git/modules/repo_new" >expect &&
737 echo "repo" >expect &&
738 test_must_fail git config -f .gitmodules submodule.repo.path &&
739 git config -f .gitmodules submodule.repo_new.path >actual &&
740 test_cmp expect actual&&
741 echo "$submodurl/repo" >expect &&
742 test_must_fail git config -f .gitmodules submodule.repo.url &&
743 echo "$submodurl/bare.git" >expect &&
744 git config -f .gitmodules submodule.repo_new.url >actual &&
745 test_cmp expect actual &&
746 echo "$submodurl/repo" >expect &&
747 git config submodule.repo.url >actual &&
748 test_cmp expect actual &&
749 echo "$submodurl/bare.git" >expect &&
750 git config submodule.repo_new.url >actual &&
751 test_cmp expect actual
755 test_expect_success
'submodule add with an existing name fails unless forced' '
760 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
762 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
763 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
764 echo "$submodurl/bare.git" >expect &&
765 git config submodule.repo_new.url >actual &&
766 test_cmp expect actual &&
767 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
769 echo "repo" >expect &&
770 git config -f .gitmodules submodule.repo_new.path >actual &&
771 test_cmp expect actual&&
772 echo "$submodurl/repo.git" >expect &&
773 git config -f .gitmodules submodule.repo_new.url >actual &&
774 test_cmp expect actual &&
775 echo "$submodurl/repo.git" >expect &&
776 git config submodule.repo_new.url >actual &&
777 test_cmp expect actual
781 test_expect_success
'set up a second submodule' '
782 git submodule add ./init2 example2 &&
783 git commit -m "submodule example2 added"
786 test_expect_success
'submodule deinit should remove the whole submodule section from .git/config' '
787 git config submodule.example.foo bar &&
788 git config submodule.example2.frotz nitfol &&
789 git submodule deinit init &&
790 test -z "$(git config --get-regexp "submodule\.example\.")" &&
791 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
792 test -f example2/.git &&
796 test_expect_success
'submodule deinit . deinits all initialized submodules' '
797 git submodule update --init &&
798 git config submodule.example.foo bar &&
799 git config submodule.example2.frotz nitfol &&
800 test_must_fail git submodule deinit &&
801 git submodule deinit . >actual &&
802 test -z "$(git config --get-regexp "submodule\.example\.")" &&
803 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
804 test_i18ngrep "Cleared directory .init" actual &&
805 test_i18ngrep "Cleared directory .example2" actual &&
809 test_expect_success
'submodule deinit deinits a submodule when its work tree is missing or empty' '
810 git submodule update --init &&
811 rm -rf init example2/* example2/.git &&
812 git submodule deinit init example2 >actual &&
813 test -z "$(git config --get-regexp "submodule\.example\.")" &&
814 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
815 test_i18ngrep ! "Cleared directory .init" actual &&
816 test_i18ngrep "Cleared directory .example2" actual &&
820 test_expect_success
'submodule deinit fails when the submodule contains modifications unless forced' '
821 git submodule update --init &&
823 test_must_fail git submodule deinit init &&
824 test -n "$(git config --get-regexp "submodule\.example\.")" &&
825 test -f example2/.git &&
826 git submodule deinit -f init >actual &&
827 test -z "$(git config --get-regexp "submodule\.example\.")" &&
828 test_i18ngrep "Cleared directory .init" actual &&
832 test_expect_success
'submodule deinit fails when the submodule contains untracked files unless forced' '
833 git submodule update --init &&
834 echo X >>init/untracked &&
835 test_must_fail git submodule deinit init &&
836 test -n "$(git config --get-regexp "submodule\.example\.")" &&
837 test -f example2/.git &&
838 git submodule deinit -f init >actual &&
839 test -z "$(git config --get-regexp "submodule\.example\.")" &&
840 test_i18ngrep "Cleared directory .init" actual &&
844 test_expect_success
'submodule deinit fails when the submodule HEAD does not match unless forced' '
845 git submodule update --init &&
850 test_must_fail git submodule deinit init &&
851 test -n "$(git config --get-regexp "submodule\.example\.")" &&
852 test -f example2/.git &&
853 git submodule deinit -f init >actual &&
854 test -z "$(git config --get-regexp "submodule\.example\.")" &&
855 test_i18ngrep "Cleared directory .init" actual &&
859 test_expect_success
'submodule deinit is silent when used on an uninitialized submodule' '
860 git submodule update --init &&
861 git submodule deinit init >actual &&
862 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
863 test_i18ngrep "Cleared directory .init" actual &&
864 git submodule deinit init >actual &&
865 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
866 test_i18ngrep "Cleared directory .init" actual &&
867 git submodule deinit . >actual &&
868 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
869 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
870 test_i18ngrep "Cleared directory .init" actual &&
871 git submodule deinit . >actual &&
872 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
873 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
874 test_i18ngrep "Cleared directory .init" actual &&
878 test_expect_success
'submodule deinit fails when submodule has a .git directory even when forced' '
879 git submodule update --init &&
883 cp -R ../.git/modules/example .git &&
884 GIT_WORK_TREE=. git config --unset core.worktree
886 test_must_fail git submodule deinit init &&
887 test_must_fail git submodule deinit -f init &&
889 test -n "$(git config --get-regexp "submodule\.example\.")"