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.
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
17 test_expect_success 'submodule deinit works on empty repository' '
18 git submodule deinit --all
21 test_expect_success 'setup - initial commit' '
24 git commit -m "initial commit" &&
28 test_expect_success 'submodule init aborts on missing .gitmodules file' '
29 test_when_finished "git update-index --remove sub" &&
30 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
31 # missing the .gitmodules file here
32 test_must_fail git submodule init 2>actual &&
33 test_i18ngrep "No url found for submodule path" actual
36 test_expect_success 'submodule update aborts on missing .gitmodules file' '
37 test_when_finished "git update-index --remove sub" &&
38 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
39 # missing the .gitmodules file here
40 git submodule update sub 2>actual &&
41 test_i18ngrep "Submodule path .sub. not initialized" actual
44 test_expect_success 'submodule update aborts on missing gitmodules url' '
45 test_when_finished "git update-index --remove sub" &&
46 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
47 test_when_finished "rm -f .gitmodules" &&
48 git config -f .gitmodules submodule.s.path sub &&
49 test_must_fail git submodule init
52 test_expect_success 'add aborts on repository with no commits' '
53 cat >expect <<-\EOF &&
54 '"'repo-no-commits'"' does not have a commit checked out
56 git init repo-no-commits &&
57 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
58 test_cmp expect actual
61 test_expect_success 'status should ignore inner git repo when not added' '
69 git commit -m "initial"
71 test_must_fail git submodule status inner 2>output.err &&
73 test_i18ngrep "^error: .*did not match any file(s) known to git" output.err
76 test_expect_success 'setup - repository in init subdirectory' '
83 git commit -m "submodule commit 1" &&
84 git tag -a -m "rev-1" rev-1
88 test_expect_success 'setup - commit with gitlink' '
92 git commit -m "super commit 1"
95 test_expect_success 'setup - hide init subdirectory' '
99 test_expect_success 'setup - repository to add submodules to' '
101 git init addtest-ignore
104 # The 'submodule add' tests need some repository to add as a submodule.
105 # The trash directory is a good one as any. We need to canonicalize
106 # the name, though, as some tests compare it to the absolute path git
107 # generates, which will expand symbolic links.
111 git for-each-ref --format='%(refname)' 'refs/heads/*'
120 listbranches >"$dotdot/heads" &&
121 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
122 git rev-parse HEAD >"$dotdot/head-sha1" &&
123 git update-index --refresh &&
124 git diff-files --exit-code &&
125 git clean -n -d -x >"$dotdot/untracked"
129 test_expect_success 'submodule add' '
130 echo "refs/heads/main" >expect &&
134 git submodule add -q "$submodurl" submod >actual &&
135 test_must_be_empty actual &&
136 echo "gitdir: ../.git/modules/submod" >expect &&
137 test_cmp expect submod/.git &&
140 git config core.worktree >actual &&
141 echo "../../../submod" >expect &&
142 test_cmp expect actual &&
148 rm -f heads head untracked &&
149 inspect addtest/submod ../.. &&
150 test_cmp expect heads &&
151 test_cmp expect head &&
152 test_must_be_empty untracked
155 test_expect_success 'setup parent and one repository' '
156 test_create_repo parent &&
157 test_commit -C parent one
160 test_expect_success 'redirected submodule add does not show progress' '
161 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
164 test_i18ngrep ! "Checking connectivity" err
167 test_expect_success 'redirected submodule add --progress does show progress' '
168 git -C addtest submodule add --progress "file://$submodurl/parent" \
169 submod-redirected-progress 2>err && \
173 test_expect_success 'submodule add to .gitignored path fails' '
176 cat <<-\EOF >expect &&
177 The following paths are ignored by one of your .gitignore files:
179 hint: Use -f if you really want to add them.
180 hint: Turn this message off by running
181 hint: "git config advice.addIgnoredFile false"
183 # Does not use test_commit due to the ignore
184 echo "*" > .gitignore &&
185 git add --force .gitignore &&
186 git commit -m"Ignore everything" &&
187 ! git submodule add "$submodurl" submod >actual 2>&1 &&
188 test_cmp expect actual
192 test_expect_success 'submodule add to .gitignored path with --force' '
195 git submodule add --force "$submodurl" submod
199 test_expect_success 'submodule add to reconfigure existing submodule with --force' '
202 bogus_url="$(pwd)/bogus-url" &&
203 git submodule add --force "$bogus_url" submod &&
204 git submodule add --force -b initial "$submodurl" submod-branch &&
205 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
206 test "$bogus_url" = "$(git config submodule.submod.url)" &&
208 git submodule add --force "$submodurl" submod &&
209 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
210 test "$submodurl" = "$(git config submodule.submod.url)"
214 test_expect_success 'submodule add relays add --dry-run stderr' '
215 test_when_finished "rm -rf addtest/.git/index.lock" &&
218 : >.git/index.lock &&
219 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
220 test_i18ngrep "^fatal: .*index\.lock" output.err &&
221 test_path_is_missing sub-while-locked
225 test_expect_success 'submodule add --branch' '
226 echo "refs/heads/initial" >expect-head &&
227 cat <<-\EOF >expect-heads &&
234 git submodule add -b initial "$submodurl" submod-branch &&
235 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
239 rm -f heads head untracked &&
240 inspect addtest/submod-branch ../.. &&
241 test_cmp expect-heads heads &&
242 test_cmp expect-head head &&
243 test_must_be_empty untracked
246 test_expect_success 'submodule add with ./ in path' '
247 echo "refs/heads/main" >expect &&
251 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
255 rm -f heads head untracked &&
256 inspect addtest/dotsubmod/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/main" >expect &&
267 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
271 rm -f heads head untracked &&
272 inspect addtest/dotslashdotsubmod/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/main" >expect &&
283 git submodule add "$submodurl" slashslashsubmod///frotz// &&
287 rm -f heads head untracked &&
288 inspect addtest/slashslashsubmod/frotz ../../.. &&
289 test_cmp expect heads &&
290 test_cmp expect head &&
291 test_must_be_empty untracked
294 test_expect_success 'submodule add with /.. in path' '
295 echo "refs/heads/main" >expect &&
299 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
303 rm -f heads head untracked &&
304 inspect addtest/realsubmod ../.. &&
305 test_cmp expect heads &&
306 test_cmp expect head &&
307 test_must_be_empty untracked
310 test_expect_success 'submodule add with ./, /.. and // in path' '
311 echo "refs/heads/main" >expect &&
315 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
319 rm -f heads head untracked &&
320 inspect addtest/realsubmod2 ../.. &&
321 test_cmp expect heads &&
322 test_cmp expect head &&
323 test_must_be_empty untracked
326 test_expect_success !CYGWIN 'submodule add with \\ in path' '
327 test_when_finished "rm -rf parent sub\\with\\backslash" &&
329 # Initialize a repo with a backslash in its name
330 git init sub\\with\\backslash &&
331 touch sub\\with\\backslash/empty.file &&
332 git -C sub\\with\\backslash add empty.file &&
333 git -C sub\\with\\backslash commit -m "Added empty.file" &&
335 # Add that repository as a submodule
337 git -C parent submodule add ../sub\\with\\backslash
340 test_expect_success 'submodule add in subdirectory' '
341 echo "refs/heads/main" >expect &&
346 git submodule add "$submodurl" ../realsubmod3 &&
350 rm -f heads head untracked &&
351 inspect addtest/realsubmod3 ../.. &&
352 test_cmp expect heads &&
353 test_cmp expect head &&
354 test_must_be_empty untracked
357 test_expect_success 'submodule add in subdirectory with relative path should fail' '
360 test_must_fail git submodule add ../../ submod3 2>../../output.err
362 test_i18ngrep toplevel output.err
365 test_expect_success 'setup - add an example entry to .gitmodules' '
366 git config --file=.gitmodules submodule.example.url git://example.com/init.git
369 test_expect_success 'status should fail for unmapped paths' '
370 test_must_fail git submodule status
373 test_expect_success 'setup - map path in .gitmodules' '
374 cat <<\EOF >expect &&
375 [submodule "example"]
376 url = git://example.com/init.git
380 git config --file=.gitmodules submodule.example.path init &&
382 test_cmp expect .gitmodules
385 test_expect_success 'status should only print one line' '
386 git submodule status >lines &&
387 test_line_count = 1 lines
390 test_expect_success 'status from subdirectory should have the same SHA1' '
391 test_when_finished "rmdir addtest/subdir" &&
395 git submodule status >output &&
396 awk "{print \$1}" <output >expect &&
398 git submodule status >../output &&
399 awk "{print \$1}" <../output >../actual &&
400 test_cmp ../expect ../actual &&
401 git -C ../submod checkout HEAD^ &&
402 git submodule status >../output &&
403 awk "{print \$1}" <../output >../actual2 &&
405 git submodule status >output &&
406 awk "{print \$1}" <output >expect2 &&
407 test_cmp expect2 actual2 &&
408 ! test_cmp actual actual2
412 test_expect_success 'setup - fetch commit name from submodule' '
413 rev1=$(cd .subrepo && git rev-parse HEAD) &&
414 printf "rev1: %s\n" "$rev1" &&
418 test_expect_success 'status should initially be "missing"' '
419 git submodule status >lines &&
423 test_expect_success 'init should register submodule url in .git/config' '
424 echo git://example.com/init.git >expect &&
426 git submodule init &&
427 git config submodule.example.url >url &&
428 git config submodule.example.url ./.subrepo &&
433 test_expect_success 'status should still be "missing" after initializing' '
436 git submodule status >lines &&
441 test_failure_with_unknown_submodule () {
442 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
443 test_i18ngrep "^error: .*no-such-submodule" output.err
446 test_expect_success 'init should fail with unknown submodule' '
447 test_failure_with_unknown_submodule init
450 test_expect_success 'update should fail with unknown submodule' '
451 test_failure_with_unknown_submodule update
454 test_expect_success 'status should fail with unknown submodule' '
455 test_failure_with_unknown_submodule status
458 test_expect_success 'sync should fail with unknown submodule' '
459 test_failure_with_unknown_submodule sync
462 test_expect_success 'update should fail when path is used by a file' '
463 echo hello >expect &&
465 echo "hello" >init &&
466 test_must_fail git submodule update &&
471 test_expect_success 'update should fail when path is used by a nonempty directory' '
472 echo hello >expect &&
476 echo "hello" >init/a &&
478 test_must_fail git submodule update &&
480 test_cmp expect init/a
483 test_expect_success 'update should work when path is an empty dir' '
486 echo "$rev1" >expect &&
489 git submodule update -q >update.out &&
490 test_must_be_empty update.out &&
493 test_cmp expect head-sha1
496 test_expect_success 'status should be "up-to-date" after update' '
497 git submodule status >list &&
501 test_expect_success 'status "up-to-date" from subdirectory' '
505 git submodule status >../list
507 grep "^ $rev1" list &&
508 grep "\\.\\./init" list
511 test_expect_success 'status "up-to-date" from subdirectory with path' '
515 git submodule status ../init >../list
517 grep "^ $rev1" list &&
518 grep "\\.\\./init" list
521 test_expect_success 'status should be "modified" after submodule commit' '
526 git commit -m "submodule commit 2"
529 rev2=$(cd init && git rev-parse HEAD) &&
531 git submodule status >list &&
536 test_expect_success 'the --cached sha1 should be rev1' '
537 git submodule --cached status >list &&
541 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
543 grep "^+Subproject commit $rev2" diff
546 test_expect_success 'update should checkout rev1' '
548 echo "$rev1" >expect &&
550 git submodule update init &&
553 test_cmp expect head-sha1
556 test_expect_success 'status should be "up-to-date" after update' '
557 git submodule status >list &&
561 test_expect_success 'checkout superproject with subproject already present' '
562 git checkout initial &&
566 test_expect_success 'apply submodule diff' '
572 git commit -m "change subproject"
574 git update-index --add init &&
575 git commit -m "change init" &&
576 git format-patch -1 --stdout >P.diff &&
577 git checkout second &&
578 git apply --index P.diff &&
580 git diff --cached main >staged &&
581 test_must_be_empty staged
584 test_expect_success 'update --init' '
586 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
587 git config --remove-section submodule.example &&
588 test_must_fail git config submodule.example.url &&
590 git submodule update init 2> update.out &&
591 test_i18ngrep "not initialized" update.out &&
592 test_must_fail git rev-parse --resolve-git-dir init/.git &&
594 git submodule update --init init &&
595 git rev-parse --resolve-git-dir init/.git
598 test_expect_success 'update --init from subdirectory' '
600 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
601 git config --remove-section submodule.example &&
602 test_must_fail git config submodule.example.url &&
607 git submodule update ../init 2>update.out &&
608 test_i18ngrep "not initialized" update.out &&
609 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
611 git submodule update --init ../init
613 git rev-parse --resolve-git-dir init/.git
616 test_expect_success 'do not add files from a submodule' '
619 test_must_fail git add init/a
623 test_expect_success 'gracefully add/reset submodule with a trailing slash' '
626 git commit -m "commit subproject" init &&
630 git diff --exit-code --cached init &&
632 git commit -m update a >/dev/null &&
633 git rev-parse HEAD) &&
635 test_must_fail git diff --exit-code --cached init &&
636 test $commit = $(git ls-files --stage |
637 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
639 git diff --exit-code --cached init
643 test_expect_success 'ls-files gracefully handles trailing slash' '
645 test "init" = "$(git ls-files init/)"
649 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
653 git checkout initial &&
658 test_expect_success 'submodule <invalid-subcommand> fails' '
659 test_must_fail git submodule no-such-subcommand
662 test_expect_success 'add submodules without specifying an explicit path' '
669 git commit -m "repo commit 1"
671 git clone --bare repo/ bare.git &&
674 git submodule add "$submodurl/repo" &&
675 git config -f .gitmodules submodule.repo.path repo &&
676 git submodule add "$submodurl/bare.git" &&
677 git config -f .gitmodules submodule.bare.path bare
681 test_expect_success 'add should fail when path is used by a file' '
685 test_must_fail git submodule add "$submodurl/repo" file
689 test_expect_success 'add should fail when path is used by an existing directory' '
693 test_must_fail git submodule add "$submodurl/repo" empty-dir
697 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
700 git submodule add ../repo relative &&
701 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
702 git submodule sync relative &&
703 test "$(git config submodule.relative.url)" = "$submodurl/repo"
707 test_expect_success 'set up for relative path tests' '
719 git config -f .gitmodules submodule.sub.path sub &&
720 git config -f .gitmodules submodule.sub.url ../subrepo &&
721 cp .git/config pristine-.git-config &&
722 cp .gitmodules pristine-.gitmodules
726 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
729 cp pristine-.git-config .git/config &&
730 cp pristine-.gitmodules .gitmodules &&
731 git config remote.origin.url ssh://hostname/repo &&
732 git submodule init &&
733 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
737 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
740 cp pristine-.git-config .git/config &&
741 cp pristine-.gitmodules .gitmodules &&
742 git config remote.origin.url ssh://hostname:22/repo &&
743 git submodule init &&
744 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
748 # About the choice of the path in the next test:
749 # - double-slash side-steps path mangling issues on Windows
750 # - it is still an absolute local path
751 # - there cannot be a server with a blank in its name just in case the
752 # path is used erroneously to access a //server/share style path
753 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
756 cp pristine-.git-config .git/config &&
757 cp pristine-.gitmodules .gitmodules &&
758 git config remote.origin.url "//somewhere else/repo" &&
759 git submodule init &&
760 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
764 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
767 cp pristine-.git-config .git/config &&
768 cp pristine-.gitmodules .gitmodules &&
769 git config remote.origin.url file:///tmp/repo &&
770 git submodule init &&
771 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
775 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
778 cp pristine-.git-config .git/config &&
779 cp pristine-.gitmodules .gitmodules &&
780 git config remote.origin.url helper:://hostname/repo &&
781 git submodule init &&
782 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
786 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
789 cp pristine-.git-config .git/config &&
790 git config remote.origin.url user@host:repo &&
791 git submodule init &&
792 test "$(git config submodule.sub.url)" = user@host:subrepo
796 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
799 cp pristine-.git-config .git/config &&
800 cp pristine-.gitmodules .gitmodules &&
801 git config remote.origin.url user@host:path/to/repo &&
802 git submodule init &&
803 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
807 test_expect_success '../subrepo works with relative local path - foo' '
810 cp pristine-.git-config .git/config &&
811 cp pristine-.gitmodules .gitmodules &&
812 git config remote.origin.url foo &&
813 # actual: fails with an error
814 git submodule init &&
815 test "$(git config submodule.sub.url)" = subrepo
819 test_expect_success '../subrepo works with relative local path - foo/bar' '
822 cp pristine-.git-config .git/config &&
823 cp pristine-.gitmodules .gitmodules &&
824 git config remote.origin.url foo/bar &&
825 git submodule init &&
826 test "$(git config submodule.sub.url)" = foo/subrepo
830 test_expect_success '../subrepo works with relative local path - ./foo' '
833 cp pristine-.git-config .git/config &&
834 cp pristine-.gitmodules .gitmodules &&
835 git config remote.origin.url ./foo &&
836 git submodule init &&
837 test "$(git config submodule.sub.url)" = subrepo
841 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
844 cp pristine-.git-config .git/config &&
845 cp pristine-.gitmodules .gitmodules &&
846 git config remote.origin.url ./foo/bar &&
847 git submodule init &&
848 test "$(git config submodule.sub.url)" = foo/subrepo
852 test_expect_success '../subrepo works with relative local path - ../foo' '
855 cp pristine-.git-config .git/config &&
856 cp pristine-.gitmodules .gitmodules &&
857 git config remote.origin.url ../foo &&
858 git submodule init &&
859 test "$(git config submodule.sub.url)" = ../subrepo
863 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
866 cp pristine-.git-config .git/config &&
867 cp pristine-.gitmodules .gitmodules &&
868 git config remote.origin.url ../foo/bar &&
869 git submodule init &&
870 test "$(git config submodule.sub.url)" = ../foo/subrepo
874 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
877 cp pristine-.git-config .git/config &&
878 cp pristine-.gitmodules .gitmodules &&
880 (cd a/b/c && git init && test_commit msg) &&
881 git config remote.origin.url ../foo/bar.git &&
882 git submodule add ../bar/a/b/c ./a/b/c &&
883 git submodule init &&
884 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
888 test_expect_success 'moving the superproject does not break submodules' '
891 git submodule status >expect
893 mv addtest addtest2 &&
896 git submodule status >actual &&
897 test_cmp expect actual
901 test_expect_success 'moving the submodule does not break the superproject' '
906 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
907 mv addtest2/repo addtest2/repo.bak &&
908 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
913 test_cmp expect actual
916 test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
921 echo "$submodurl/repo" >expect &&
922 git config remote.origin.url >actual &&
923 test_cmp expect actual &&
924 echo "gitdir: ../.git/modules/repo" >expect &&
929 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
930 test_must_be_empty actual &&
931 echo "gitdir: ../.git/modules/submod" >expect &&
932 test_cmp expect submod/.git &&
935 echo "$submodurl/bare.git" >expect &&
936 git config remote.origin.url >actual &&
937 test_cmp expect actual &&
938 echo "gitdir: ../.git/modules/repo_new" >expect &&
941 echo "repo" >expect &&
942 test_must_fail git config -f .gitmodules submodule.repo.path &&
943 git config -f .gitmodules submodule.repo_new.path >actual &&
944 test_cmp expect actual &&
945 echo "$submodurl/repo" >expect &&
946 test_must_fail git config -f .gitmodules submodule.repo.url &&
947 echo "$submodurl/bare.git" >expect &&
948 git config -f .gitmodules submodule.repo_new.url >actual &&
949 test_cmp expect actual &&
950 echo "$submodurl/repo" >expect &&
951 git config submodule.repo.url >actual &&
952 test_cmp expect actual &&
953 echo "$submodurl/bare.git" >expect &&
954 git config submodule.repo_new.url >actual &&
955 test_cmp expect actual
959 test_expect_success 'recursive relative submodules stay relative' '
960 test_when_finished "rm -rf super clone2 subsub sub3" &&
967 git commit -m "initial commit"
975 git commit -m "initial commit" &&
976 git submodule add ../subsub dirdir/subsub &&
977 git commit -m "add submodule subsub"
985 git commit -m "initial commit" &&
986 git submodule add ../sub3 &&
987 git commit -m "add submodule sub"
989 git clone super clone2 &&
992 git submodule update --init --recursive &&
993 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
994 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
996 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
997 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
1000 test_expect_success 'submodule add with an existing name fails unless forced' '
1005 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
1007 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
1008 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
1009 echo "$submodurl/bare.git" >expect &&
1010 git config submodule.repo_new.url >actual &&
1011 test_cmp expect actual &&
1012 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
1014 echo "repo" >expect &&
1015 git config -f .gitmodules submodule.repo_new.path >actual &&
1016 test_cmp expect actual &&
1017 echo "$submodurl/repo.git" >expect &&
1018 git config -f .gitmodules submodule.repo_new.url >actual &&
1019 test_cmp expect actual &&
1020 echo "$submodurl/repo.git" >expect &&
1021 git config submodule.repo_new.url >actual &&
1022 test_cmp expect actual
1026 test_expect_success 'set up a second submodule' '
1027 git submodule add ./init2 example2 &&
1028 git commit -m "submodule example2 added"
1031 test_expect_success 'submodule deinit works on repository without submodules' '
1032 test_when_finished "rm -rf newdirectory" &&
1033 mkdir newdirectory &&
1039 git commit -m "repo should not be empty" &&
1040 git submodule deinit . &&
1041 git submodule deinit --all
1045 test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
1046 git config submodule.example.foo bar &&
1047 git config submodule.example2.frotz nitfol &&
1048 git submodule deinit init &&
1049 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1050 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1051 test -f example2/.git &&
1055 test_expect_success 'submodule deinit should unset core.worktree' '
1056 test_path_is_file .git/modules/example/config &&
1057 test_must_fail git config -f .git/modules/example/config core.worktree
1060 test_expect_success 'submodule deinit from subdirectory' '
1061 git submodule update --init &&
1062 git config submodule.example.foo bar &&
1066 git submodule deinit ../init >../output
1068 test_i18ngrep "\\.\\./init" output &&
1069 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1070 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1071 test -f example2/.git &&
1075 test_expect_success 'submodule deinit . deinits all initialized submodules' '
1076 git submodule update --init &&
1077 git config submodule.example.foo bar &&
1078 git config submodule.example2.frotz nitfol &&
1079 test_must_fail git submodule deinit &&
1080 git submodule deinit . >actual &&
1081 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1082 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1083 test_i18ngrep "Cleared directory .init" actual &&
1084 test_i18ngrep "Cleared directory .example2" actual &&
1088 test_expect_success 'submodule deinit --all deinits all initialized submodules' '
1089 git submodule update --init &&
1090 git config submodule.example.foo bar &&
1091 git config submodule.example2.frotz nitfol &&
1092 test_must_fail git submodule deinit &&
1093 git submodule deinit --all >actual &&
1094 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1095 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1096 test_i18ngrep "Cleared directory .init" actual &&
1097 test_i18ngrep "Cleared directory .example2" actual &&
1101 test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
1102 git submodule update --init &&
1103 rm -rf init example2/* example2/.git &&
1104 git submodule deinit init example2 >actual &&
1105 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1106 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1107 test_i18ngrep ! "Cleared directory .init" actual &&
1108 test_i18ngrep "Cleared directory .example2" actual &&
1112 test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
1113 git submodule update --init &&
1115 test_must_fail git submodule deinit init &&
1116 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1117 test -f example2/.git &&
1118 git submodule deinit -f init >actual &&
1119 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1120 test_i18ngrep "Cleared directory .init" actual &&
1124 test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
1125 git submodule update --init &&
1126 echo X >>init/untracked &&
1127 test_must_fail git submodule deinit init &&
1128 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1129 test -f example2/.git &&
1130 git submodule deinit -f init >actual &&
1131 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1132 test_i18ngrep "Cleared directory .init" actual &&
1136 test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
1137 git submodule update --init &&
1142 test_must_fail git submodule deinit init &&
1143 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1144 test -f example2/.git &&
1145 git submodule deinit -f init >actual &&
1146 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1147 test_i18ngrep "Cleared directory .init" actual &&
1151 test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
1152 git submodule update --init &&
1153 git submodule deinit init >actual &&
1154 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1155 test_i18ngrep "Cleared directory .init" actual &&
1156 git submodule deinit init >actual &&
1157 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1158 test_i18ngrep "Cleared directory .init" actual &&
1159 git submodule deinit . >actual &&
1160 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1161 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1162 test_i18ngrep "Cleared directory .init" actual &&
1163 git submodule deinit . >actual &&
1164 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1165 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1166 test_i18ngrep "Cleared directory .init" actual &&
1167 git submodule deinit --all >actual &&
1168 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1169 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1170 test_i18ngrep "Cleared directory .init" actual &&
1174 test_expect_success 'submodule deinit fails when submodule has a .git directory even when forced' '
1175 git submodule update --init &&
1179 cp -R ../.git/modules/example .git &&
1180 GIT_WORK_TREE=. git config --unset core.worktree
1182 test_must_fail git submodule deinit init &&
1183 test_must_fail git submodule deinit -f init &&
1184 test -d init/.git &&
1185 test -n "$(git config --get-regexp "submodule\.example\.")"
1188 test_expect_success 'submodule with UTF-8 name' '
1189 svname=$(printf "\303\245 \303\244\303\266") &&
1196 git commit -m "init sub"
1198 git submodule add ./"$svname" &&
1199 git submodule >&2 &&
1200 test -n "$(git submodule | grep "$svname")"
1203 test_expect_success 'submodule add clone shallow submodule' '
1209 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1212 test 1 = $(git log --oneline | wc -l)
1217 test_expect_success 'submodule helper list is not confused by common prefixes' '
1222 echo hi >testfile2 &&
1224 git commit -m "test1"
1230 echo hello >testfile1 &&
1232 git commit -m "test2"
1234 git submodule add /dir1/b dir1/b &&
1235 git submodule add /dir2/b dir2/b &&
1236 git commit -m "first submodule commit" &&
1237 git submodule--helper list dir1/b | cut -f 2 >actual &&
1238 echo "dir1/b" >expect &&
1239 test_cmp expect actual
1242 test_expect_success 'setup superproject with submodules' '
1244 test_commit -C sub1 test &&
1245 test_commit -C sub1 test2 &&
1246 git init multisuper &&
1247 git -C multisuper submodule add ../sub1 sub0 &&
1248 git -C multisuper submodule add ../sub1 sub1 &&
1249 git -C multisuper submodule add ../sub1 sub2 &&
1250 git -C multisuper submodule add ../sub1 sub3 &&
1251 git -C multisuper commit -m "add some submodules"
1261 test_expect_success 'submodule update --init with a specification' '
1262 test_when_finished "rm -rf multisuper_clone" &&
1264 git clone file://"$pwd"/multisuper multisuper_clone &&
1265 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1266 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1267 test_cmp expect actual
1270 test_expect_success 'submodule update --init with submodule.active set' '
1271 test_when_finished "rm -rf multisuper_clone" &&
1273 git clone file://"$pwd"/multisuper multisuper_clone &&
1274 git -C multisuper_clone config submodule.active "." &&
1275 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1276 git -C multisuper_clone submodule update --init &&
1277 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1278 test_cmp expect actual
1281 test_expect_success 'submodule update and setting submodule.<name>.active' '
1282 test_when_finished "rm -rf multisuper_clone" &&
1284 git clone file://"$pwd"/multisuper multisuper_clone &&
1285 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1286 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1287 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1289 cat >expect <<-\EOF &&
1295 git -C multisuper_clone submodule update &&
1296 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1297 test_cmp expect actual
1300 test_expect_success 'clone active submodule without submodule url set' '
1301 test_when_finished "rm -rf test/test" &&
1303 # another dir breaks accidental relative paths still being correct
1304 git clone file://"$pwd"/multisuper test/test &&
1307 git config submodule.active "." &&
1309 # do not pass --init flag, as the submodule is already active:
1310 git submodule update &&
1311 git submodule status >actual_raw &&
1313 cut -d" " -f3- actual_raw >actual &&
1314 cat >expect <<-\EOF &&
1320 test_cmp expect actual
1324 test_expect_success 'clone --recurse-submodules with a pathspec works' '
1325 test_when_finished "rm -rf multisuper_clone" &&
1326 cat >expected <<-\EOF &&
1333 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1334 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1335 test_cmp expected actual
1338 test_expect_success 'clone with multiple --recurse-submodules options' '
1339 test_when_finished "rm -rf multisuper_clone" &&
1340 cat >expect <<-\EOF &&
1347 git clone --recurse-submodules="." \
1348 --recurse-submodules=":(exclude)sub0" \
1349 --recurse-submodules=":(exclude)sub2" \
1350 multisuper multisuper_clone &&
1351 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1352 test_cmp expect actual
1355 test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
1356 test_when_finished "rm -rf multisuper_clone" &&
1357 cat <<-\EOF >expect &&
1364 cat <<-\EOF >expect2 &&
1373 git clone --recurse-submodules="." \
1374 --recurse-submodules=":(exclude)sub0" \
1375 --recurse-submodules=":(exclude)sub2" \
1376 --recurse-submodules=":(exclude)sub4" \
1377 multisuper multisuper_clone &&
1379 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1380 test_cmp expect actual &&
1382 git -C multisuper submodule add ../sub1 sub4 &&
1383 git -C multisuper submodule add ../sub1 sub5 &&
1384 git -C multisuper commit -m "add more submodules" &&
1385 # obtain the new superproject
1386 git -C multisuper_clone pull &&
1387 git -C multisuper_clone submodule update --init &&
1388 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1389 test_cmp expect2 actual
1392 test_expect_success 'init properly sets the config' '
1393 test_when_finished "rm -rf multisuper_clone" &&
1394 git clone --recurse-submodules="." \
1395 --recurse-submodules=":(exclude)sub0" \
1396 multisuper multisuper_clone &&
1398 git -C multisuper_clone submodule init -- sub0 sub1 &&
1399 git -C multisuper_clone config --get submodule.sub0.active &&
1400 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1403 test_expect_success 'recursive clone respects -q' '
1404 test_when_finished "rm -rf multisuper_clone" &&
1405 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1406 test_must_be_empty actual