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 'submodule deinit works on empty repository' '
15 git submodule deinit --all
18 test_expect_success 'setup - initial commit' '
21 git commit -m "initial commit" &&
25 test_expect_success 'submodule init aborts on missing .gitmodules file' '
26 test_when_finished "git update-index --remove sub" &&
27 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
28 # missing the .gitmodules file here
29 test_must_fail git submodule init 2>actual &&
30 test_i18ngrep "No url found for submodule path" actual
33 test_expect_success 'submodule update aborts on missing .gitmodules file' '
34 test_when_finished "git update-index --remove sub" &&
35 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
36 # missing the .gitmodules file here
37 git submodule update sub 2>actual &&
38 test_i18ngrep "Submodule path .sub. not initialized" actual
41 test_expect_success 'submodule update aborts on missing gitmodules url' '
42 test_when_finished "git update-index --remove sub" &&
43 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
44 test_when_finished "rm -f .gitmodules" &&
45 git config -f .gitmodules submodule.s.path sub &&
46 test_must_fail git submodule init
49 test_expect_success 'add aborts on repository with no commits' '
50 cat >expect <<-\EOF &&
51 '"'repo-no-commits'"' does not have a commit checked out
53 git init repo-no-commits &&
54 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
55 test_i18ncmp expect actual
58 test_expect_success 'status should ignore inner git repo when not added' '
66 git commit -m "initial"
68 test_must_fail git submodule status inner 2>output.err &&
70 test_i18ngrep "^error: .*did not match any file(s) known to git" output.err
73 test_expect_success 'setup - repository in init subdirectory' '
80 git commit -m "submodule commit 1" &&
81 git tag -a -m "rev-1" rev-1
85 test_expect_success 'setup - commit with gitlink' '
89 git commit -m "super commit 1"
92 test_expect_success 'setup - hide init subdirectory' '
96 test_expect_success 'setup - repository to add submodules to' '
98 git init addtest-ignore
101 # The 'submodule add' tests need some repository to add as a submodule.
102 # The trash directory is a good one as any. We need to canonicalize
103 # the name, though, as some tests compare it to the absolute path git
104 # generates, which will expand symbolic links.
108 git for-each-ref --format='%(refname)' 'refs/heads/*'
117 listbranches >"$dotdot/heads" &&
118 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
119 git rev-parse HEAD >"$dotdot/head-sha1" &&
120 git update-index --refresh &&
121 git diff-files --exit-code &&
122 git clean -n -d -x >"$dotdot/untracked"
126 test_expect_success 'submodule add' '
127 echo "refs/heads/master" >expect &&
131 git submodule add -q "$submodurl" submod >actual &&
132 test_must_be_empty actual &&
133 echo "gitdir: ../.git/modules/submod" >expect &&
134 test_cmp expect submod/.git &&
137 git config core.worktree >actual &&
138 echo "../../../submod" >expect &&
139 test_cmp expect actual &&
145 rm -f heads head untracked &&
146 inspect addtest/submod ../.. &&
147 test_cmp expect heads &&
148 test_cmp expect head &&
149 test_must_be_empty untracked
152 test_expect_success 'setup parent and one repository' '
153 test_create_repo parent &&
154 test_commit -C parent one
157 test_expect_success 'redirected submodule add does not show progress' '
158 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
161 test_i18ngrep ! "Checking connectivity" err
164 test_expect_success 'redirected submodule add --progress does show progress' '
165 git -C addtest submodule add --progress "file://$submodurl/parent" \
166 submod-redirected-progress 2>err && \
170 test_expect_success 'submodule add to .gitignored path fails' '
173 cat <<-\EOF >expect &&
174 The following path is ignored by one of your .gitignore files:
176 Use -f if you really want to add it.
178 # Does not use test_commit due to the ignore
179 echo "*" > .gitignore &&
180 git add --force .gitignore &&
181 git commit -m"Ignore everything" &&
182 ! git submodule add "$submodurl" submod >actual 2>&1 &&
183 test_i18ncmp expect actual
187 test_expect_success 'submodule add to .gitignored path with --force' '
190 git submodule add --force "$submodurl" submod
194 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
197 bogus_url="$(pwd)/bogus-url" &&
198 git submodule add --force "$bogus_url" submod &&
199 git submodule add --force -b initial "$submodurl" submod-branch &&
200 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
201 test "$bogus_url" = "$(git config submodule.submod.url)" &&
203 git submodule add --force "$submodurl" submod &&
204 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
205 test "$submodurl" = "$(git config submodule.submod.url)"
209 test_expect_success 'submodule add --branch' '
210 echo "refs/heads/initial" >expect-head &&
211 cat <<-\EOF >expect-heads &&
218 git submodule add -b initial "$submodurl" submod-branch &&
219 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
223 rm -f heads head untracked &&
224 inspect addtest/submod-branch ../.. &&
225 test_cmp expect-heads heads &&
226 test_cmp expect-head head &&
227 test_must_be_empty untracked
230 test_expect_success 'submodule add with ./ in path' '
231 echo "refs/heads/master" >expect &&
235 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
239 rm -f heads head untracked &&
240 inspect addtest/dotsubmod/frotz ../../.. &&
241 test_cmp expect heads &&
242 test_cmp expect head &&
243 test_must_be_empty untracked
246 test_expect_success 'submodule add with /././ in path' '
247 echo "refs/heads/master" >expect &&
251 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
255 rm -f heads head untracked &&
256 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
257 test_cmp expect heads &&
258 test_cmp expect head &&
259 test_must_be_empty untracked
262 test_expect_success 'submodule add with // in path' '
263 echo "refs/heads/master" >expect &&
267 git submodule add "$submodurl" slashslashsubmod///frotz// &&
271 rm -f heads head untracked &&
272 inspect addtest/slashslashsubmod/frotz ../../.. &&
273 test_cmp expect heads &&
274 test_cmp expect head &&
275 test_must_be_empty untracked
278 test_expect_success 'submodule add with /.. in path' '
279 echo "refs/heads/master" >expect &&
283 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
287 rm -f heads head untracked &&
288 inspect addtest/realsubmod ../.. &&
289 test_cmp expect heads &&
290 test_cmp expect head &&
291 test_must_be_empty untracked
294 test_expect_success 'submodule add with ./, /.. and // in path' '
295 echo "refs/heads/master" >expect &&
299 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
303 rm -f heads head untracked &&
304 inspect addtest/realsubmod2 ../.. &&
305 test_cmp expect heads &&
306 test_cmp expect head &&
307 test_must_be_empty untracked
310 test_expect_success !CYGWIN 'submodule add with \\ in path' '
311 test_when_finished "rm -rf parent sub\\with\\backslash" &&
313 # Initialize a repo with a backslash in its name
314 git init sub\\with\\backslash &&
315 touch sub\\with\\backslash/empty.file &&
316 git -C sub\\with\\backslash add empty.file &&
317 git -C sub\\with\\backslash commit -m "Added empty.file" &&
319 # Add that repository as a submodule
321 git -C parent submodule add ../sub\\with\\backslash
324 test_expect_success 'submodule add in subdirectory' '
325 echo "refs/heads/master" >expect &&
330 git submodule add "$submodurl" ../realsubmod3 &&
334 rm -f heads head untracked &&
335 inspect addtest/realsubmod3 ../.. &&
336 test_cmp expect heads &&
337 test_cmp expect head &&
338 test_must_be_empty untracked
341 test_expect_success 'submodule add in subdirectory with relative path should fail' '
344 test_must_fail git submodule add ../../ submod3 2>../../output.err
346 test_i18ngrep toplevel output.err
349 test_expect_success 'setup - add an example entry to .gitmodules' '
350 git config --file=.gitmodules submodule.example.url git://example.com/init.git
353 test_expect_success 'status should fail for unmapped paths' '
354 test_must_fail git submodule status
357 test_expect_success 'setup - map path in .gitmodules' '
358 cat <<\EOF >expect &&
359 [submodule "example"]
360 url = git://example.com/init.git
364 git config --file=.gitmodules submodule.example.path init &&
366 test_cmp expect .gitmodules
369 test_expect_success 'status should only print one line' '
370 git submodule status >lines &&
371 test_line_count = 1 lines
374 test_expect_success 'setup - fetch commit name from submodule' '
375 rev1=$(cd .subrepo && git rev-parse HEAD) &&
376 printf "rev1: %s\n" "$rev1" &&
380 test_expect_success 'status should initially be "missing"' '
381 git submodule status >lines &&
385 test_expect_success 'init should register submodule url in .git/config' '
386 echo git://example.com/init.git >expect &&
388 git submodule init &&
389 git config submodule.example.url >url &&
390 git config submodule.example.url ./.subrepo &&
395 test_expect_success 'status should still be "missing" after initializing' '
398 git submodule status >lines &&
403 test_failure_with_unknown_submodule () {
404 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
405 test_i18ngrep "^error: .*no-such-submodule" output.err
408 test_expect_success 'init should fail with unknown submodule' '
409 test_failure_with_unknown_submodule init
412 test_expect_success 'update should fail with unknown submodule' '
413 test_failure_with_unknown_submodule update
416 test_expect_success 'status should fail with unknown submodule' '
417 test_failure_with_unknown_submodule status
420 test_expect_success 'sync should fail with unknown submodule' '
421 test_failure_with_unknown_submodule sync
424 test_expect_success 'update should fail when path is used by a file' '
425 echo hello >expect &&
427 echo "hello" >init &&
428 test_must_fail git submodule update &&
433 test_expect_success 'update should fail when path is used by a nonempty directory' '
434 echo hello >expect &&
438 echo "hello" >init/a &&
440 test_must_fail git submodule update &&
442 test_cmp expect init/a
445 test_expect_success 'update should work when path is an empty dir' '
448 echo "$rev1" >expect &&
451 git submodule update -q >update.out &&
452 test_must_be_empty update.out &&
455 test_cmp expect head-sha1
458 test_expect_success 'status should be "up-to-date" after update' '
459 git submodule status >list &&
463 test_expect_success 'status "up-to-date" from subdirectory' '
467 git submodule status >../list
469 grep "^ $rev1" list &&
470 grep "\\.\\./init" list
473 test_expect_success 'status "up-to-date" from subdirectory with path' '
477 git submodule status ../init >../list
479 grep "^ $rev1" list &&
480 grep "\\.\\./init" list
483 test_expect_success 'status should be "modified" after submodule commit' '
488 git commit -m "submodule commit 2"
491 rev2=$(cd init && git rev-parse HEAD) &&
493 git submodule status >list &&
498 test_expect_success 'the --cached sha1 should be rev1' '
499 git submodule --cached status >list &&
503 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
505 grep "^+Subproject commit $rev2" diff
508 test_expect_success 'update should checkout rev1' '
510 echo "$rev1" >expect &&
512 git submodule update init &&
515 test_cmp expect head-sha1
518 test_expect_success 'status should be "up-to-date" after update' '
519 git submodule status >list &&
523 test_expect_success 'checkout superproject with subproject already present' '
524 git checkout initial &&
528 test_expect_success 'apply submodule diff' '
534 git commit -m "change subproject"
536 git update-index --add init &&
537 git commit -m "change init" &&
538 git format-patch -1 --stdout >P.diff &&
539 git checkout second &&
540 git apply --index P.diff &&
542 git diff --cached master >staged &&
543 test_must_be_empty staged
546 test_expect_success 'update --init' '
548 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
549 git config --remove-section submodule.example &&
550 test_must_fail git config submodule.example.url &&
552 git submodule update init 2> update.out &&
554 test_i18ngrep "not initialized" update.out &&
555 test_must_fail git rev-parse --resolve-git-dir init/.git &&
557 git submodule update --init init &&
558 git rev-parse --resolve-git-dir init/.git
561 test_expect_success 'update --init from subdirectory' '
563 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
564 git config --remove-section submodule.example &&
565 test_must_fail git config submodule.example.url &&
570 git submodule update ../init 2>update.out &&
572 test_i18ngrep "not initialized" update.out &&
573 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
575 git submodule update --init ../init
577 git rev-parse --resolve-git-dir init/.git
580 test_expect_success 'do not add files from a submodule' '
583 test_must_fail git add init/a
587 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
590 git commit -m "commit subproject" init &&
594 git diff --exit-code --cached init &&
596 git commit -m update a >/dev/null &&
597 git rev-parse HEAD) &&
599 test_must_fail git diff --exit-code --cached init &&
600 test $commit = $(git ls-files --stage |
601 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
603 git diff --exit-code --cached init
607 test_expect_success 'ls-files gracefully handles trailing slash' '
609 test "init" = "$(git ls-files init/)"
613 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
617 git checkout initial &&
622 test_expect_success 'submodule <invalid-subcommand> fails' '
623 test_must_fail git submodule no-such-subcommand
626 test_expect_success 'add submodules without specifying an explicit path' '
633 git commit -m "repo commit 1"
635 git clone --bare repo/ bare.git &&
638 git submodule add "$submodurl/repo" &&
639 git config -f .gitmodules submodule.repo.path repo &&
640 git submodule add "$submodurl/bare.git" &&
641 git config -f .gitmodules submodule.bare.path bare
645 test_expect_success 'add should fail when path is used by a file' '
649 test_must_fail git submodule add "$submodurl/repo" file
653 test_expect_success 'add should fail when path is used by an existing directory' '
657 test_must_fail git submodule add "$submodurl/repo" empty-dir
661 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
664 git submodule add ../repo relative &&
665 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
666 git submodule sync relative &&
667 test "$(git config submodule.relative.url)" = "$submodurl/repo"
671 test_expect_success 'set up for relative path tests' '
683 git config -f .gitmodules submodule.sub.path sub &&
684 git config -f .gitmodules submodule.sub.url ../subrepo &&
685 cp .git/config pristine-.git-config &&
686 cp .gitmodules pristine-.gitmodules
690 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
693 cp pristine-.git-config .git/config &&
694 cp pristine-.gitmodules .gitmodules &&
695 git config remote.origin.url ssh://hostname/repo &&
696 git submodule init &&
697 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
701 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
704 cp pristine-.git-config .git/config &&
705 cp pristine-.gitmodules .gitmodules &&
706 git config remote.origin.url ssh://hostname:22/repo &&
707 git submodule init &&
708 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
712 # About the choice of the path in the next test:
713 # - double-slash side-steps path mangling issues on Windows
714 # - it is still an absolute local path
715 # - there cannot be a server with a blank in its name just in case the
716 # path is used erroneously to access a //server/share style path
717 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
720 cp pristine-.git-config .git/config &&
721 cp pristine-.gitmodules .gitmodules &&
722 git config remote.origin.url "//somewhere else/repo" &&
723 git submodule init &&
724 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
728 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
731 cp pristine-.git-config .git/config &&
732 cp pristine-.gitmodules .gitmodules &&
733 git config remote.origin.url file:///tmp/repo &&
734 git submodule init &&
735 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
739 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
742 cp pristine-.git-config .git/config &&
743 cp pristine-.gitmodules .gitmodules &&
744 git config remote.origin.url helper:://hostname/repo &&
745 git submodule init &&
746 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
750 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
753 cp pristine-.git-config .git/config &&
754 git config remote.origin.url user@host:repo &&
755 git submodule init &&
756 test "$(git config submodule.sub.url)" = user@host:subrepo
760 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
763 cp pristine-.git-config .git/config &&
764 cp pristine-.gitmodules .gitmodules &&
765 git config remote.origin.url user@host:path/to/repo &&
766 git submodule init &&
767 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
771 test_expect_success '../subrepo works with relative local path - foo' '
774 cp pristine-.git-config .git/config &&
775 cp pristine-.gitmodules .gitmodules &&
776 git config remote.origin.url foo &&
777 # actual: fails with an error
778 git submodule init &&
779 test "$(git config submodule.sub.url)" = subrepo
783 test_expect_success '../subrepo works with relative local path - foo/bar' '
786 cp pristine-.git-config .git/config &&
787 cp pristine-.gitmodules .gitmodules &&
788 git config remote.origin.url foo/bar &&
789 git submodule init &&
790 test "$(git config submodule.sub.url)" = foo/subrepo
794 test_expect_success '../subrepo works with relative local path - ./foo' '
797 cp pristine-.git-config .git/config &&
798 cp pristine-.gitmodules .gitmodules &&
799 git config remote.origin.url ./foo &&
800 git submodule init &&
801 test "$(git config submodule.sub.url)" = subrepo
805 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
808 cp pristine-.git-config .git/config &&
809 cp pristine-.gitmodules .gitmodules &&
810 git config remote.origin.url ./foo/bar &&
811 git submodule init &&
812 test "$(git config submodule.sub.url)" = foo/subrepo
816 test_expect_success '../subrepo works with relative local path - ../foo' '
819 cp pristine-.git-config .git/config &&
820 cp pristine-.gitmodules .gitmodules &&
821 git config remote.origin.url ../foo &&
822 git submodule init &&
823 test "$(git config submodule.sub.url)" = ../subrepo
827 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
830 cp pristine-.git-config .git/config &&
831 cp pristine-.gitmodules .gitmodules &&
832 git config remote.origin.url ../foo/bar &&
833 git submodule init &&
834 test "$(git config submodule.sub.url)" = ../foo/subrepo
838 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
841 cp pristine-.git-config .git/config &&
842 cp pristine-.gitmodules .gitmodules &&
844 (cd a/b/c && git init && test_commit msg) &&
845 git config remote.origin.url ../foo/bar.git &&
846 git submodule add ../bar/a/b/c ./a/b/c &&
847 git submodule init &&
848 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
852 test_expect_success 'moving the superproject does not break submodules' '
855 git submodule status >expect
857 mv addtest addtest2 &&
860 git submodule status >actual &&
861 test_cmp expect actual
865 test_expect_success 'moving the submodule does not break the superproject' '
870 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
871 mv addtest2/repo addtest2/repo.bak &&
872 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
877 test_cmp expect actual
880 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
885 echo "$submodurl/repo" >expect &&
886 git config remote.origin.url >actual &&
887 test_cmp expect actual &&
888 echo "gitdir: ../.git/modules/repo" >expect &&
893 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
894 test_must_be_empty actual &&
895 echo "gitdir: ../.git/modules/submod" >expect &&
896 test_cmp expect submod/.git &&
899 echo "$submodurl/bare.git" >expect &&
900 git config remote.origin.url >actual &&
901 test_cmp expect actual &&
902 echo "gitdir: ../.git/modules/repo_new" >expect &&
905 echo "repo" >expect &&
906 test_must_fail git config -f .gitmodules submodule.repo.path &&
907 git config -f .gitmodules submodule.repo_new.path >actual &&
908 test_cmp expect actual&&
909 echo "$submodurl/repo" >expect &&
910 test_must_fail git config -f .gitmodules submodule.repo.url &&
911 echo "$submodurl/bare.git" >expect &&
912 git config -f .gitmodules submodule.repo_new.url >actual &&
913 test_cmp expect actual &&
914 echo "$submodurl/repo" >expect &&
915 git config submodule.repo.url >actual &&
916 test_cmp expect actual &&
917 echo "$submodurl/bare.git" >expect &&
918 git config submodule.repo_new.url >actual &&
919 test_cmp expect actual
923 test_expect_success 'recursive relative submodules stay relative' '
924 test_when_finished "rm -rf super clone2 subsub sub3" &&
931 git commit -m "initial commit"
939 git commit -m "initial commit" &&
940 git submodule add ../subsub dirdir/subsub &&
941 git commit -m "add submodule subsub"
949 git commit -m "initial commit" &&
950 git submodule add ../sub3 &&
951 git commit -m "add submodule sub"
953 git clone super clone2 &&
956 git submodule update --init --recursive &&
957 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
958 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
960 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
961 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
964 test_expect_success 'submodule add with an existing name fails unless forced' '
969 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
971 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
972 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
973 echo "$submodurl/bare.git" >expect &&
974 git config submodule.repo_new.url >actual &&
975 test_cmp expect actual &&
976 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
978 echo "repo" >expect &&
979 git config -f .gitmodules submodule.repo_new.path >actual &&
980 test_cmp expect actual&&
981 echo "$submodurl/repo.git" >expect &&
982 git config -f .gitmodules submodule.repo_new.url >actual &&
983 test_cmp expect actual &&
984 echo "$submodurl/repo.git" >expect &&
985 git config submodule.repo_new.url >actual &&
986 test_cmp expect actual
990 test_expect_success 'set up a second submodule' '
991 git submodule add ./init2 example2 &&
992 git commit -m "submodule example2 added"
995 test_expect_success 'submodule deinit works on repository without submodules' '
996 test_when_finished "rm -rf newdirectory" &&
997 mkdir newdirectory &&
1003 git commit -m "repo should not be empty" &&
1004 git submodule deinit . &&
1005 git submodule deinit --all
1009 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1010 git config submodule.example.foo bar &&
1011 git config submodule.example2.frotz nitfol &&
1012 git submodule deinit init &&
1013 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1014 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1015 test -f example2/.git &&
1019 test_expect_success 'submodule deinit should unset core.worktree' '
1020 test_path_is_file .git/modules/example/config &&
1021 test_must_fail git config -f .git/modules/example/config core.worktree
1024 test_expect_success 'submodule deinit from subdirectory' '
1025 git submodule update --init &&
1026 git config submodule.example.foo bar &&
1030 git submodule deinit ../init >../output
1032 test_i18ngrep "\\.\\./init" output &&
1033 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1034 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1035 test -f example2/.git &&
1039 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1040 git submodule update --init &&
1041 git config submodule.example.foo bar &&
1042 git config submodule.example2.frotz nitfol &&
1043 test_must_fail git submodule deinit &&
1044 git submodule deinit . >actual &&
1045 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1046 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1047 test_i18ngrep "Cleared directory .init" actual &&
1048 test_i18ngrep "Cleared directory .example2" actual &&
1052 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1053 git submodule update --init &&
1054 git config submodule.example.foo bar &&
1055 git config submodule.example2.frotz nitfol &&
1056 test_must_fail git submodule deinit &&
1057 git submodule deinit --all >actual &&
1058 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1059 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1060 test_i18ngrep "Cleared directory .init" actual &&
1061 test_i18ngrep "Cleared directory .example2" actual &&
1065 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1066 git submodule update --init &&
1067 rm -rf init example2/* example2/.git &&
1068 git submodule deinit init example2 >actual &&
1069 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1070 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1071 test_i18ngrep ! "Cleared directory .init" actual &&
1072 test_i18ngrep "Cleared directory .example2" actual &&
1076 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1077 git submodule update --init &&
1079 test_must_fail git submodule deinit init &&
1080 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1081 test -f example2/.git &&
1082 git submodule deinit -f init >actual &&
1083 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1084 test_i18ngrep "Cleared directory .init" actual &&
1088 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1089 git submodule update --init &&
1090 echo X >>init/untracked &&
1091 test_must_fail git submodule deinit init &&
1092 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1093 test -f example2/.git &&
1094 git submodule deinit -f init >actual &&
1095 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1096 test_i18ngrep "Cleared directory .init" actual &&
1100 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1101 git submodule update --init &&
1106 test_must_fail git submodule deinit init &&
1107 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1108 test -f example2/.git &&
1109 git submodule deinit -f init >actual &&
1110 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1111 test_i18ngrep "Cleared directory .init" actual &&
1115 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1116 git submodule update --init &&
1117 git submodule deinit init >actual &&
1118 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1119 test_i18ngrep "Cleared directory .init" actual &&
1120 git submodule deinit init >actual &&
1121 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1122 test_i18ngrep "Cleared directory .init" actual &&
1123 git submodule deinit . >actual &&
1124 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1125 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1126 test_i18ngrep "Cleared directory .init" actual &&
1127 git submodule deinit . >actual &&
1128 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1129 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1130 test_i18ngrep "Cleared directory .init" actual &&
1131 git submodule deinit --all >actual &&
1132 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1133 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1134 test_i18ngrep "Cleared directory .init" actual &&
1138 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1139 git submodule update --init &&
1143 cp -R ../.git/modules/example .git &&
1144 GIT_WORK_TREE=. git config --unset core.worktree
1146 test_must_fail git submodule deinit init &&
1147 test_must_fail git submodule deinit -f init &&
1148 test -d init/.git &&
1149 test -n "$(git config --get-regexp "submodule\.example\.")"
1152 test_expect_success 'submodule with UTF-8 name' '
1153 svname=$(printf "\303\245 \303\244\303\266") &&
1160 git commit -m "init sub"
1162 git submodule add ./"$svname" &&
1163 git submodule >&2 &&
1164 test -n "$(git submodule | grep "$svname")"
1167 test_expect_success 'submodule add clone shallow submodule' '
1173 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1176 test 1 = $(git log --oneline | wc -l)
1181 test_expect_success 'submodule helper list is not confused by common prefixes' '
1186 echo hi >testfile2 &&
1188 git commit -m "test1"
1194 echo hello >testfile1 &&
1196 git commit -m "test2"
1198 git submodule add /dir1/b dir1/b &&
1199 git submodule add /dir2/b dir2/b &&
1200 git commit -m "first submodule commit" &&
1201 git submodule--helper list dir1/b |cut -c51- >actual &&
1202 echo "dir1/b" >expect &&
1203 test_cmp expect actual
1206 test_expect_success 'setup superproject with submodules' '
1208 test_commit -C sub1 test &&
1209 test_commit -C sub1 test2 &&
1210 git init multisuper &&
1211 git -C multisuper submodule add ../sub1 sub0 &&
1212 git -C multisuper submodule add ../sub1 sub1 &&
1213 git -C multisuper submodule add ../sub1 sub2 &&
1214 git -C multisuper submodule add ../sub1 sub3 &&
1215 git -C multisuper commit -m "add some submodules"
1225 test_expect_success 'submodule update --init with a specification' '
1226 test_when_finished "rm -rf multisuper_clone" &&
1228 git clone file://"$pwd"/multisuper multisuper_clone &&
1229 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1230 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1231 test_cmp expect actual
1234 test_expect_success 'submodule update --init with submodule.active set' '
1235 test_when_finished "rm -rf multisuper_clone" &&
1237 git clone file://"$pwd"/multisuper multisuper_clone &&
1238 git -C multisuper_clone config submodule.active "." &&
1239 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1240 git -C multisuper_clone submodule update --init &&
1241 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1242 test_cmp expect actual
1245 test_expect_success 'submodule update and setting submodule.<name>.active' '
1246 test_when_finished "rm -rf multisuper_clone" &&
1248 git clone file://"$pwd"/multisuper multisuper_clone &&
1249 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1250 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1251 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1253 cat >expect <<-\EOF &&
1259 git -C multisuper_clone submodule update &&
1260 git -C multisuper_clone submodule status |cut -c 1,43- >actual &&
1261 test_cmp expect actual
1264 test_expect_success 'clone active submodule without submodule url set' '
1265 test_when_finished "rm -rf test/test" &&
1267 # another dir breaks accidental relative paths still being correct
1268 git clone file://"$pwd"/multisuper test/test &&
1271 git config submodule.active "." &&
1273 # do not pass --init flag, as the submodule is already active:
1274 git submodule update &&
1275 git submodule status >actual_raw &&
1277 cut -c 1,43- actual_raw >actual &&
1278 cat >expect <<-\EOF &&
1284 test_cmp expect actual
1288 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1289 test_when_finished "rm -rf multisuper_clone" &&
1290 cat >expected <<-\EOF &&
1297 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1298 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1299 test_cmp expected actual
1302 test_expect_success 'clone with multiple --recurse-submodules options' '
1303 test_when_finished "rm -rf multisuper_clone" &&
1304 cat >expect <<-\EOF &&
1311 git clone --recurse-submodules="." \
1312 --recurse-submodules=":(exclude)sub0" \
1313 --recurse-submodules=":(exclude)sub2" \
1314 multisuper multisuper_clone &&
1315 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1316 test_cmp expect actual
1319 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1320 test_when_finished "rm -rf multisuper_clone" &&
1321 cat <<-\EOF >expect &&
1328 cat <<-\EOF >expect2 &&
1337 git clone --recurse-submodules="." \
1338 --recurse-submodules=":(exclude)sub0" \
1339 --recurse-submodules=":(exclude)sub2" \
1340 --recurse-submodules=":(exclude)sub4" \
1341 multisuper multisuper_clone &&
1343 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1344 test_cmp expect actual &&
1346 git -C multisuper submodule add ../sub1 sub4 &&
1347 git -C multisuper submodule add ../sub1 sub5 &&
1348 git -C multisuper commit -m "add more submodules" &&
1349 # obtain the new superproject
1350 git -C multisuper_clone pull &&
1351 git -C multisuper_clone submodule update --init &&
1352 git -C multisuper_clone submodule status |cut -c1,43- >actual &&
1353 test_cmp expect2 actual
1356 test_expect_success 'init properly sets the config' '
1357 test_when_finished "rm -rf multisuper_clone" &&
1358 git clone --recurse-submodules="." \
1359 --recurse-submodules=":(exclude)sub0" \
1360 multisuper multisuper_clone &&
1362 git -C multisuper_clone submodule init -- sub0 sub1 &&
1363 git -C multisuper_clone config --get submodule.sub0.active &&
1364 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1367 test_expect_success 'recursive clone respects -q' '
1368 test_when_finished "rm -rf multisuper_clone" &&
1369 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1370 test_must_be_empty actual