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 &&
81 test_must_be_empty 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 subdirectory' '
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 'submodule add in subdirectory with relative path should fail' '
236 test_must_fail git submodule add ../../ submod3 2>../../output.err
238 test_i18ngrep toplevel output.err
241 test_expect_success 'setup - add an example entry to .gitmodules' '
242 GIT_CONFIG=.gitmodules \
243 git config submodule.example.url git://example.com/init.git
246 test_expect_success 'status should fail for unmapped paths' '
247 test_must_fail git submodule status
250 test_expect_success 'setup - map path in .gitmodules' '
251 cat <<\EOF >expect &&
252 [submodule "example"]
253 url = git://example.com/init.git
257 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
259 test_cmp expect .gitmodules
262 test_expect_success 'status should only print one line' '
263 git submodule status >lines &&
264 test_line_count = 1 lines
267 test_expect_success 'setup - fetch commit name from submodule' '
268 rev1=$(cd .subrepo && git rev-parse HEAD) &&
269 printf "rev1: %s\n" "$rev1" &&
273 test_expect_success 'status should initially be "missing"' '
274 git submodule status >lines &&
278 test_expect_success 'init should register submodule url in .git/config' '
279 echo git://example.com/init.git >expect &&
281 git submodule init &&
282 git config submodule.example.url >url &&
283 git config submodule.example.url ./.subrepo &&
288 test_failure_with_unknown_submodule () {
289 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
290 grep "^error: .*no-such-submodule" output.err
293 test_expect_success 'init should fail with unknown submodule' '
294 test_failure_with_unknown_submodule init
297 test_expect_success 'update should fail with unknown submodule' '
298 test_failure_with_unknown_submodule update
301 test_expect_success 'status should fail with unknown submodule' '
302 test_failure_with_unknown_submodule status
305 test_expect_success 'sync should fail with unknown submodule' '
306 test_failure_with_unknown_submodule sync
309 test_expect_success 'update should fail when path is used by a file' '
310 echo hello >expect &&
312 echo "hello" >init &&
313 test_must_fail git submodule update &&
318 test_expect_success 'update should fail when path is used by a nonempty directory' '
319 echo hello >expect &&
323 echo "hello" >init/a &&
325 test_must_fail git submodule update &&
327 test_cmp expect init/a
330 test_expect_success 'update should work when path is an empty dir' '
333 echo "$rev1" >expect &&
336 git submodule update -q >update.out &&
337 test_must_be_empty update.out &&
340 test_cmp expect head-sha1
343 test_expect_success 'status should be "up-to-date" after update' '
344 git submodule status >list &&
348 test_expect_success 'status "up-to-date" from subdirectory' '
352 git submodule status >../list
354 grep "^ $rev1" list &&
355 grep "\\.\\./init" list
358 test_expect_success 'status "up-to-date" from subdirectory with path' '
362 git submodule status ../init >../list
364 grep "^ $rev1" list &&
365 grep "\\.\\./init" list
368 test_expect_success 'status should be "modified" after submodule commit' '
373 git commit -m "submodule commit 2"
376 rev2=$(cd init && git rev-parse HEAD) &&
378 git submodule status >list &&
383 test_expect_success 'the --cached sha1 should be rev1' '
384 git submodule --cached status >list &&
388 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
390 grep "^+Subproject commit $rev2" diff
393 test_expect_success 'update should checkout rev1' '
395 echo "$rev1" >expect &&
397 git submodule update init &&
400 test_cmp expect head-sha1
403 test_expect_success 'status should be "up-to-date" after update' '
404 git submodule status >list &&
408 test_expect_success 'checkout superproject with subproject already present' '
409 git checkout initial &&
413 test_expect_success 'apply submodule diff' '
421 git commit -m "change subproject"
423 git update-index --add init &&
424 git commit -m "change init" &&
425 git format-patch -1 --stdout >P.diff &&
426 git checkout second &&
427 git apply --index P.diff &&
429 git diff --cached master >staged &&
430 test_cmp empty staged
433 test_expect_success 'update --init' '
435 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
436 git config --remove-section submodule.example &&
437 test_must_fail git config submodule.example.url &&
439 git submodule update init > update.out &&
441 test_i18ngrep "not initialized" update.out &&
442 test_must_fail git rev-parse --resolve-git-dir init/.git &&
444 git submodule update --init init &&
445 git rev-parse --resolve-git-dir init/.git
448 test_expect_success 'update --init from subdirectory' '
450 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
451 git config --remove-section submodule.example &&
452 test_must_fail git config submodule.example.url &&
457 git submodule update ../init >update.out &&
459 test_i18ngrep "not initialized" update.out &&
460 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
462 git submodule update --init ../init
464 git rev-parse --resolve-git-dir init/.git
467 test_expect_success 'do not add files from a submodule' '
470 test_must_fail git add init/a
474 test_expect_success 'gracefully add submodule with a trailing slash' '
477 git commit -m "commit subproject" init &&
481 git diff --exit-code --cached init &&
483 git commit -m update a >/dev/null &&
484 git rev-parse HEAD) &&
486 test_must_fail git diff --exit-code --cached init &&
487 test $commit = $(git ls-files --stage |
488 sed -n "s/^160000 \([^ ]*\).*/\1/p")
492 test_expect_success 'ls-files gracefully handles trailing slash' '
494 test "init" = "$(git ls-files init/)"
498 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
502 git checkout initial &&
507 test_expect_success 'submodule <invalid-subcommand> fails' '
508 test_must_fail git submodule no-such-subcommand
511 test_expect_success 'add submodules without specifying an explicit path' '
518 git commit -m "repo commit 1"
520 git clone --bare repo/ bare.git &&
523 git submodule add "$submodurl/repo" &&
524 git config -f .gitmodules submodule.repo.path repo &&
525 git submodule add "$submodurl/bare.git" &&
526 git config -f .gitmodules submodule.bare.path bare
530 test_expect_success 'add should fail when path is used by a file' '
534 test_must_fail git submodule add "$submodurl/repo" file
538 test_expect_success 'add should fail when path is used by an existing directory' '
542 test_must_fail git submodule add "$submodurl/repo" empty-dir
546 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
549 git submodule add ../repo relative &&
550 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
551 git submodule sync relative &&
552 test "$(git config submodule.relative.url)" = "$submodurl/repo"
556 test_expect_success 'set up for relative path tests' '
568 git config -f .gitmodules submodule.sub.path sub &&
569 git config -f .gitmodules submodule.sub.url ../subrepo &&
570 cp .git/config pristine-.git-config &&
571 cp .gitmodules pristine-.gitmodules
575 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
578 cp pristine-.git-config .git/config &&
579 cp pristine-.gitmodules .gitmodules &&
580 git config remote.origin.url ssh://hostname/repo &&
581 git submodule init &&
582 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
586 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
589 cp pristine-.git-config .git/config &&
590 cp pristine-.gitmodules .gitmodules &&
591 git config remote.origin.url ssh://hostname:22/repo &&
592 git submodule init &&
593 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
597 # About the choice of the path in the next test:
598 # - double-slash side-steps path mangling issues on Windows
599 # - it is still an absolute local path
600 # - there cannot be a server with a blank in its name just in case the
601 # path is used erroneously to access a //server/share style path
602 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
605 cp pristine-.git-config .git/config &&
606 cp pristine-.gitmodules .gitmodules &&
607 git config remote.origin.url "//somewhere else/repo" &&
608 git submodule init &&
609 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
613 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
616 cp pristine-.git-config .git/config &&
617 cp pristine-.gitmodules .gitmodules &&
618 git config remote.origin.url file:///tmp/repo &&
619 git submodule init &&
620 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
624 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
627 cp pristine-.git-config .git/config &&
628 cp pristine-.gitmodules .gitmodules &&
629 git config remote.origin.url helper:://hostname/repo &&
630 git submodule init &&
631 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
635 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
638 cp pristine-.git-config .git/config &&
639 git config remote.origin.url user@host:repo &&
640 git submodule init &&
641 test "$(git config submodule.sub.url)" = user@host:subrepo
645 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
648 cp pristine-.git-config .git/config &&
649 cp pristine-.gitmodules .gitmodules &&
650 git config remote.origin.url user@host:path/to/repo &&
651 git submodule init &&
652 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
656 test_expect_success '../subrepo works with relative local path - foo' '
659 cp pristine-.git-config .git/config &&
660 cp pristine-.gitmodules .gitmodules &&
661 git config remote.origin.url foo &&
662 # actual: fails with an error
663 git submodule init &&
664 test "$(git config submodule.sub.url)" = subrepo
668 test_expect_success '../subrepo works with relative local path - foo/bar' '
671 cp pristine-.git-config .git/config &&
672 cp pristine-.gitmodules .gitmodules &&
673 git config remote.origin.url foo/bar &&
674 git submodule init &&
675 test "$(git config submodule.sub.url)" = foo/subrepo
679 test_expect_success '../subrepo works with relative local path - ./foo' '
682 cp pristine-.git-config .git/config &&
683 cp pristine-.gitmodules .gitmodules &&
684 git config remote.origin.url ./foo &&
685 git submodule init &&
686 test "$(git config submodule.sub.url)" = subrepo
690 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
693 cp pristine-.git-config .git/config &&
694 cp pristine-.gitmodules .gitmodules &&
695 git config remote.origin.url ./foo/bar &&
696 git submodule init &&
697 test "$(git config submodule.sub.url)" = foo/subrepo
701 test_expect_success '../subrepo works with relative local path - ../foo' '
704 cp pristine-.git-config .git/config &&
705 cp pristine-.gitmodules .gitmodules &&
706 git config remote.origin.url ../foo &&
707 git submodule init &&
708 test "$(git config submodule.sub.url)" = ../subrepo
712 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
715 cp pristine-.git-config .git/config &&
716 cp pristine-.gitmodules .gitmodules &&
717 git config remote.origin.url ../foo/bar &&
718 git submodule init &&
719 test "$(git config submodule.sub.url)" = ../foo/subrepo
723 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
726 cp pristine-.git-config .git/config &&
727 cp pristine-.gitmodules .gitmodules &&
729 (cd a/b/c; git init) &&
730 git config remote.origin.url ../foo/bar.git &&
731 git submodule add ../bar/a/b/c ./a/b/c &&
732 git submodule init &&
733 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
737 test_expect_success 'moving the superproject does not break submodules' '
740 git submodule status >expect
742 mv addtest addtest2 &&
745 git submodule status >actual &&
746 test_cmp expect actual
750 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
755 echo "$submodurl/repo" >expect &&
756 git config remote.origin.url >actual &&
757 test_cmp expect actual &&
758 echo "gitdir: ../.git/modules/repo" >expect &&
763 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
764 test_must_be_empty actual &&
765 echo "gitdir: ../.git/modules/submod" >expect &&
766 test_cmp expect submod/.git &&
769 echo "$submodurl/bare.git" >expect &&
770 git config remote.origin.url >actual &&
771 test_cmp expect actual &&
772 echo "gitdir: ../.git/modules/repo_new" >expect &&
775 echo "repo" >expect &&
776 git config -f .gitmodules submodule.repo.path >actual &&
777 test_cmp expect actual &&
778 git config -f .gitmodules submodule.repo_new.path >actual &&
779 test_cmp expect actual&&
780 echo "$submodurl/repo" >expect &&
781 git config -f .gitmodules submodule.repo.url >actual &&
782 test_cmp expect actual &&
783 echo "$submodurl/bare.git" >expect &&
784 git config -f .gitmodules submodule.repo_new.url >actual &&
785 test_cmp expect actual &&
786 echo "$submodurl/repo" >expect &&
787 git config submodule.repo.url >actual &&
788 test_cmp expect actual &&
789 echo "$submodurl/bare.git" >expect &&
790 git config submodule.repo_new.url >actual &&
791 test_cmp expect actual
795 test_expect_success 'submodule add with an existing name fails unless forced' '
800 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
802 echo "repo" >expect &&
803 git config -f .gitmodules submodule.repo_new.path >actual &&
804 test_cmp expect actual&&
805 echo "$submodurl/bare.git" >expect &&
806 git config -f .gitmodules submodule.repo_new.url >actual &&
807 test_cmp expect actual &&
808 echo "$submodurl/bare.git" >expect &&
809 git config submodule.repo_new.url >actual &&
810 test_cmp expect actual &&
811 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
813 echo "repo" >expect &&
814 git config -f .gitmodules submodule.repo_new.path >actual &&
815 test_cmp expect actual&&
816 echo "$submodurl/repo.git" >expect &&
817 git config -f .gitmodules submodule.repo_new.url >actual &&
818 test_cmp expect actual &&
819 echo "$submodurl/repo.git" >expect &&
820 git config submodule.repo_new.url >actual &&
821 test_cmp expect actual
825 test_expect_success 'set up a second submodule' '
826 git submodule add ./init2 example2 &&
827 git commit -m "submodule example2 added"
830 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
831 git config submodule.example.foo bar &&
832 git config submodule.example2.frotz nitfol &&
833 git submodule deinit init &&
834 test -z "$(git config --get-regexp "submodule\.example\.")" &&
835 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
836 test -f example2/.git &&
840 test_expect_success 'submodule deinit from subdirectory' '
841 git submodule update --init &&
842 git config submodule.example.foo bar &&
846 git submodule deinit ../init >../output
848 grep "\\.\\./init" output &&
849 test -z "$(git config --get-regexp "submodule\.example\.")" &&
850 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
851 test -f example2/.git &&
855 test_expect_success 'submodule deinit . deinits all initialized submodules' '
856 git submodule update --init &&
857 git config submodule.example.foo bar &&
858 git config submodule.example2.frotz nitfol &&
859 test_must_fail git submodule deinit &&
860 git submodule deinit . >actual &&
861 test -z "$(git config --get-regexp "submodule\.example\.")" &&
862 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
863 test_i18ngrep "Cleared directory .init" actual &&
864 test_i18ngrep "Cleared directory .example2" actual &&
868 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
869 git submodule update --init &&
870 rm -rf init example2/* example2/.git &&
871 git submodule deinit init example2 >actual &&
872 test -z "$(git config --get-regexp "submodule\.example\.")" &&
873 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
874 test_i18ngrep ! "Cleared directory .init" actual &&
875 test_i18ngrep "Cleared directory .example2" actual &&
879 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
880 git submodule update --init &&
882 test_must_fail git submodule deinit init &&
883 test -n "$(git config --get-regexp "submodule\.example\.")" &&
884 test -f example2/.git &&
885 git submodule deinit -f init >actual &&
886 test -z "$(git config --get-regexp "submodule\.example\.")" &&
887 test_i18ngrep "Cleared directory .init" actual &&
891 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
892 git submodule update --init &&
893 echo X >>init/untracked &&
894 test_must_fail git submodule deinit init &&
895 test -n "$(git config --get-regexp "submodule\.example\.")" &&
896 test -f example2/.git &&
897 git submodule deinit -f init >actual &&
898 test -z "$(git config --get-regexp "submodule\.example\.")" &&
899 test_i18ngrep "Cleared directory .init" actual &&
903 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
904 git submodule update --init &&
909 test_must_fail git submodule deinit init &&
910 test -n "$(git config --get-regexp "submodule\.example\.")" &&
911 test -f example2/.git &&
912 git submodule deinit -f init >actual &&
913 test -z "$(git config --get-regexp "submodule\.example\.")" &&
914 test_i18ngrep "Cleared directory .init" actual &&
918 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
919 git submodule update --init &&
920 git submodule deinit init >actual &&
921 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
922 test_i18ngrep "Cleared directory .init" actual &&
923 git submodule deinit init >actual &&
924 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
925 test_i18ngrep "Cleared directory .init" actual &&
926 git submodule deinit . >actual &&
927 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
928 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
929 test_i18ngrep "Cleared directory .init" actual &&
930 git submodule deinit . >actual &&
931 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
932 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
933 test_i18ngrep "Cleared directory .init" actual &&
937 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
938 git submodule update --init &&
942 cp -R ../.git/modules/example .git &&
943 GIT_WORK_TREE=. git config --unset core.worktree
945 test_must_fail git submodule deinit init &&
946 test_must_fail git submodule deinit -f init &&
948 test -n "$(git config --get-regexp "submodule\.example\.")"
951 test_expect_success 'submodule with UTF-8 name' '
952 svname=$(printf "\303\245 \303\244\303\266") &&
959 git commit -m "init sub"
961 test_config core.precomposeunicode true &&
962 git submodule add ./"$svname" &&
964 test -n "$(git submodule | grep "$svname")"
967 test_expect_success 'submodule add clone shallow submodule' '
973 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
976 test 1 = $(git log --oneline | wc -l)