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 - enable local submodules' '
15 git config --global protocol.file.allow always
18 test_expect_success
'submodule deinit works on empty repository' '
19 git submodule deinit --all
22 test_expect_success
'setup - initial commit' '
25 git commit -m "initial commit" &&
29 test_expect_success
'submodule init aborts on missing .gitmodules file' '
30 test_when_finished "git update-index --remove sub" &&
31 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
32 # missing the .gitmodules file here
33 test_must_fail git submodule init 2>actual &&
34 test_i18ngrep "No url found for submodule path" actual
37 test_expect_success
'submodule update aborts on missing .gitmodules file' '
38 test_when_finished "git update-index --remove sub" &&
39 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
40 # missing the .gitmodules file here
41 git submodule update sub 2>actual &&
42 test_i18ngrep "Submodule path .sub. not initialized" actual
45 test_expect_success
'submodule update aborts on missing gitmodules url' '
46 test_when_finished "git update-index --remove sub" &&
47 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
48 test_when_finished "rm -f .gitmodules" &&
49 git config -f .gitmodules submodule.s.path sub &&
50 test_must_fail git submodule init
53 test_expect_success
'add aborts on repository with no commits' '
54 cat >expect <<-\EOF &&
55 '"'repo-no-commits'"' does not have a commit checked out
57 git init repo-no-commits &&
58 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
59 test_i18ncmp expect actual
62 test_expect_success
'status should ignore inner git repo when not added' '
70 git commit -m "initial"
72 test_must_fail git submodule status inner 2>output.err &&
74 test_i18ngrep "^error: .*did not match any file(s) known to git" output.err
77 test_expect_success
'setup - repository in init subdirectory' '
84 git commit -m "submodule commit 1" &&
85 git tag -a -m "rev-1" rev-1
89 test_expect_success
'setup - commit with gitlink' '
93 git commit -m "super commit 1"
96 test_expect_success
'setup - hide init subdirectory' '
100 test_expect_success
'setup - repository to add submodules to' '
102 git init addtest-ignore
105 # The 'submodule add' tests need some repository to add as a submodule.
106 # The trash directory is a good one as any. We need to canonicalize
107 # the name, though, as some tests compare it to the absolute path git
108 # generates, which will expand symbolic links.
112 git for-each-ref
--format='%(refname)' 'refs/heads/*'
121 listbranches
>"$dotdot/heads" &&
122 { git symbolic-ref HEAD ||
:; } >"$dotdot/head" &&
123 git rev-parse HEAD
>"$dotdot/head-sha1" &&
124 git update-index
--refresh &&
125 git diff-files
--exit-code &&
126 git clean
-n -d -x >"$dotdot/untracked"
130 test_expect_success
'submodule add' '
131 echo "refs/heads/master" >expect &&
135 git submodule add -q "$submodurl" submod >actual &&
136 test_must_be_empty actual &&
137 echo "gitdir: ../.git/modules/submod" >expect &&
138 test_cmp expect submod/.git &&
141 git config core.worktree >actual &&
142 echo "../../../submod" >expect &&
143 test_cmp expect actual &&
149 rm -f heads head untracked &&
150 inspect addtest/submod ../.. &&
151 test_cmp expect heads &&
152 test_cmp expect head &&
153 test_must_be_empty untracked
156 test_expect_success
'setup parent and one repository' '
157 test_create_repo parent &&
158 test_commit -C parent one
161 test_expect_success
'redirected submodule add does not show progress' '
162 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
165 test_i18ngrep ! "Checking connectivity" err
168 test_expect_success
'redirected submodule add --progress does show progress' '
169 git -C addtest submodule add --progress "file://$submodurl/parent" \
170 submod-redirected-progress 2>err && \
174 test_expect_success
'submodule add to .gitignored path fails' '
177 cat <<-\EOF >expect &&
178 The following paths are ignored by one of your .gitignore files:
180 hint: Use -f if you really want to add them.
181 hint: Turn this message off by running
182 hint: "git config advice.addIgnoredFile false"
184 # Does not use test_commit due to the ignore
185 echo "*" > .gitignore &&
186 git add --force .gitignore &&
187 git commit -m"Ignore everything" &&
188 ! git submodule add "$submodurl" submod >actual 2>&1 &&
189 test_i18ncmp expect actual
193 test_expect_success
'submodule add to .gitignored path with --force' '
196 git submodule add --force "$submodurl" submod
200 test_expect_success
'submodule add to reconfigure existing submodule with --force' '
203 bogus_url="$(pwd)/bogus-url" &&
204 git submodule add --force "$bogus_url" submod &&
205 git submodule add --force -b initial "$submodurl" submod-branch &&
206 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
207 test "$bogus_url" = "$(git config submodule.submod.url)" &&
209 git submodule add --force "$submodurl" submod &&
210 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
211 test "$submodurl" = "$(git config submodule.submod.url)"
215 test_expect_success
'submodule add relays add --dry-run stderr' '
216 test_when_finished "rm -rf addtest/.git/index.lock" &&
219 : >.git/index.lock &&
220 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
221 test_i18ngrep "^fatal: .*index\.lock" output.err &&
222 test_path_is_missing sub-while-locked
226 test_expect_success
'submodule add --branch' '
227 echo "refs/heads/initial" >expect-head &&
228 cat <<-\EOF >expect-heads &&
235 git submodule add -b initial "$submodurl" submod-branch &&
236 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
240 rm -f heads head untracked &&
241 inspect addtest/submod-branch ../.. &&
242 test_cmp expect-heads heads &&
243 test_cmp expect-head head &&
244 test_must_be_empty untracked
247 test_expect_success
'submodule add with ./ in path' '
248 echo "refs/heads/master" >expect &&
252 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
256 rm -f heads head untracked &&
257 inspect addtest/dotsubmod/frotz ../../.. &&
258 test_cmp expect heads &&
259 test_cmp expect head &&
260 test_must_be_empty untracked
263 test_expect_success
'submodule add with /././ in path' '
264 echo "refs/heads/master" >expect &&
268 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
272 rm -f heads head untracked &&
273 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
274 test_cmp expect heads &&
275 test_cmp expect head &&
276 test_must_be_empty untracked
279 test_expect_success
'submodule add with // in path' '
280 echo "refs/heads/master" >expect &&
284 git submodule add "$submodurl" slashslashsubmod///frotz// &&
288 rm -f heads head untracked &&
289 inspect addtest/slashslashsubmod/frotz ../../.. &&
290 test_cmp expect heads &&
291 test_cmp expect head &&
292 test_must_be_empty untracked
295 test_expect_success
'submodule add with /.. in path' '
296 echo "refs/heads/master" >expect &&
300 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
304 rm -f heads head untracked &&
305 inspect addtest/realsubmod ../.. &&
306 test_cmp expect heads &&
307 test_cmp expect head &&
308 test_must_be_empty untracked
311 test_expect_success
'submodule add with ./, /.. and // in path' '
312 echo "refs/heads/master" >expect &&
316 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
320 rm -f heads head untracked &&
321 inspect addtest/realsubmod2 ../.. &&
322 test_cmp expect heads &&
323 test_cmp expect head &&
324 test_must_be_empty untracked
327 test_expect_success
!CYGWIN
'submodule add with \\ in path' '
328 test_when_finished "rm -rf parent sub\\with\\backslash" &&
330 # Initialize a repo with a backslash in its name
331 git init sub\\with\\backslash &&
332 touch sub\\with\\backslash/empty.file &&
333 git -C sub\\with\\backslash add empty.file &&
334 git -C sub\\with\\backslash commit -m "Added empty.file" &&
336 # Add that repository as a submodule
338 git -C parent submodule add ../sub\\with\\backslash
341 test_expect_success
'submodule add in subdirectory' '
342 echo "refs/heads/master" >expect &&
347 git submodule add "$submodurl" ../realsubmod3 &&
351 rm -f heads head untracked &&
352 inspect addtest/realsubmod3 ../.. &&
353 test_cmp expect heads &&
354 test_cmp expect head &&
355 test_must_be_empty untracked
358 test_expect_success
'submodule add in subdirectory with relative path should fail' '
361 test_must_fail git submodule add ../../ submod3 2>../../output.err
363 test_i18ngrep toplevel output.err
366 test_expect_success
'setup - add an example entry to .gitmodules' '
367 git config --file=.gitmodules submodule.example.url git://example.com/init.git
370 test_expect_success
'status should fail for unmapped paths' '
371 test_must_fail git submodule status
374 test_expect_success
'setup - map path in .gitmodules' '
375 cat <<\EOF >expect &&
376 [submodule "example"]
377 url = git://example.com/init.git
381 git config --file=.gitmodules submodule.example.path init &&
383 test_cmp expect .gitmodules
386 test_expect_success
'status should only print one line' '
387 git submodule status >lines &&
388 test_line_count = 1 lines
391 test_expect_success
'status from subdirectory should have the same SHA1' '
392 test_when_finished "rmdir addtest/subdir" &&
396 git submodule status >output &&
397 awk "{print \$1}" <output >expect &&
399 git submodule status >../output &&
400 awk "{print \$1}" <../output >../actual &&
401 test_cmp ../expect ../actual &&
402 git -C ../submod checkout HEAD^ &&
403 git submodule status >../output &&
404 awk "{print \$1}" <../output >../actual2 &&
406 git submodule status >output &&
407 awk "{print \$1}" <output >expect2 &&
408 test_cmp expect2 actual2 &&
409 ! test_cmp actual actual2
413 test_expect_success
'setup - fetch commit name from submodule' '
414 rev1=$(cd .subrepo && git rev-parse HEAD) &&
415 printf "rev1: %s\n" "$rev1" &&
419 test_expect_success
'status should initially be "missing"' '
420 git submodule status >lines &&
424 test_expect_success
'init should register submodule url in .git/config' '
425 echo git://example.com/init.git >expect &&
427 git submodule init &&
428 git config submodule.example.url >url &&
429 git config submodule.example.url ./.subrepo &&
434 test_expect_success
'status should still be "missing" after initializing' '
437 git submodule status >lines &&
442 test_failure_with_unknown_submodule
() {
443 test_must_fail git submodule
$1 no-such-submodule
2>output.err
&&
444 test_i18ngrep
"^error: .*no-such-submodule" output.err
447 test_expect_success
'init should fail with unknown submodule' '
448 test_failure_with_unknown_submodule init
451 test_expect_success
'update should fail with unknown submodule' '
452 test_failure_with_unknown_submodule update
455 test_expect_success
'status should fail with unknown submodule' '
456 test_failure_with_unknown_submodule status
459 test_expect_success
'sync should fail with unknown submodule' '
460 test_failure_with_unknown_submodule sync
463 test_expect_success
'update should fail when path is used by a file' '
464 echo hello >expect &&
466 echo "hello" >init &&
467 test_must_fail git submodule update &&
472 test_expect_success
'update should fail when path is used by a nonempty directory' '
473 echo hello >expect &&
477 echo "hello" >init/a &&
479 test_must_fail git submodule update &&
481 test_cmp expect init/a
484 test_expect_success
'update should work when path is an empty dir' '
487 echo "$rev1" >expect &&
490 git submodule update -q >update.out &&
491 test_must_be_empty update.out &&
494 test_cmp expect head-sha1
497 test_expect_success
'status should be "up-to-date" after update' '
498 git submodule status >list &&
502 test_expect_success
'status "up-to-date" from subdirectory' '
506 git submodule status >../list
508 grep "^ $rev1" list &&
509 grep "\\.\\./init" list
512 test_expect_success
'status "up-to-date" from subdirectory with path' '
516 git submodule status ../init >../list
518 grep "^ $rev1" list &&
519 grep "\\.\\./init" list
522 test_expect_success
'status should be "modified" after submodule commit' '
527 git commit -m "submodule commit 2"
530 rev2=$(cd init && git rev-parse HEAD) &&
532 git submodule status >list &&
537 test_expect_success
'the --cached sha1 should be rev1' '
538 git submodule --cached status >list &&
542 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
544 grep "^+Subproject commit $rev2" diff
547 test_expect_success
'update should checkout rev1' '
549 echo "$rev1" >expect &&
551 git submodule update init &&
554 test_cmp expect head-sha1
557 test_expect_success
'status should be "up-to-date" after update' '
558 git submodule status >list &&
562 test_expect_success
'checkout superproject with subproject already present' '
563 git checkout initial &&
567 test_expect_success
'apply submodule diff' '
573 git commit -m "change subproject"
575 git update-index --add init &&
576 git commit -m "change init" &&
577 git format-patch -1 --stdout >P.diff &&
578 git checkout second &&
579 git apply --index P.diff &&
581 git diff --cached master >staged &&
582 test_must_be_empty staged
585 test_expect_success
'update --init' '
587 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
588 git config --remove-section submodule.example &&
589 test_must_fail git config submodule.example.url &&
591 git submodule update init 2> update.out &&
592 test_i18ngrep "not initialized" update.out &&
593 test_must_fail git rev-parse --resolve-git-dir init/.git &&
595 git submodule update --init init &&
596 git rev-parse --resolve-git-dir init/.git
599 test_expect_success
'update --init from subdirectory' '
601 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
602 git config --remove-section submodule.example &&
603 test_must_fail git config submodule.example.url &&
608 git submodule update ../init 2>update.out &&
609 test_i18ngrep "not initialized" update.out &&
610 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
612 git submodule update --init ../init
614 git rev-parse --resolve-git-dir init/.git
617 test_expect_success
'do not add files from a submodule' '
620 test_must_fail git add init/a
624 test_expect_success
'gracefully add/reset submodule with a trailing slash' '
627 git commit -m "commit subproject" init &&
631 git diff --exit-code --cached init &&
633 git commit -m update a >/dev/null &&
634 git rev-parse HEAD) &&
636 test_must_fail git diff --exit-code --cached init &&
637 test $commit = $(git ls-files --stage |
638 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
640 git diff --exit-code --cached init
644 test_expect_success
'ls-files gracefully handles trailing slash' '
646 test "init" = "$(git ls-files init/)"
650 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
654 git checkout initial &&
659 test_expect_success
'submodule <invalid-subcommand> fails' '
660 test_must_fail git submodule no-such-subcommand
663 test_expect_success
'add submodules without specifying an explicit path' '
670 git commit -m "repo commit 1"
672 git clone --bare repo/ bare.git &&
675 git submodule add "$submodurl/repo" &&
676 git config -f .gitmodules submodule.repo.path repo &&
677 git submodule add "$submodurl/bare.git" &&
678 git config -f .gitmodules submodule.bare.path bare
682 test_expect_success
'add should fail when path is used by a file' '
686 test_must_fail git submodule add "$submodurl/repo" file
690 test_expect_success
'add should fail when path is used by an existing directory' '
694 test_must_fail git submodule add "$submodurl/repo" empty-dir
698 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
701 git submodule add ../repo relative &&
702 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
703 git submodule sync relative &&
704 test "$(git config submodule.relative.url)" = "$submodurl/repo"
708 test_expect_success
'set up for relative path tests' '
720 git config -f .gitmodules submodule.sub.path sub &&
721 git config -f .gitmodules submodule.sub.url ../subrepo &&
722 cp .git/config pristine-.git-config &&
723 cp .gitmodules pristine-.gitmodules
727 test_expect_success
'../subrepo works with URL - ssh://hostname/repo' '
730 cp pristine-.git-config .git/config &&
731 cp pristine-.gitmodules .gitmodules &&
732 git config remote.origin.url ssh://hostname/repo &&
733 git submodule init &&
734 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
738 test_expect_success
'../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
741 cp pristine-.git-config .git/config &&
742 cp pristine-.gitmodules .gitmodules &&
743 git config remote.origin.url ssh://hostname:22/repo &&
744 git submodule init &&
745 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
749 # About the choice of the path in the next test:
750 # - double-slash side-steps path mangling issues on Windows
751 # - it is still an absolute local path
752 # - there cannot be a server with a blank in its name just in case the
753 # path is used erroneously to access a //server/share style path
754 test_expect_success
'../subrepo path works with local path - //somewhere else/repo' '
757 cp pristine-.git-config .git/config &&
758 cp pristine-.gitmodules .gitmodules &&
759 git config remote.origin.url "//somewhere else/repo" &&
760 git submodule init &&
761 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
765 test_expect_success
'../subrepo works with file URL - file:///tmp/repo' '
768 cp pristine-.git-config .git/config &&
769 cp pristine-.gitmodules .gitmodules &&
770 git config remote.origin.url file:///tmp/repo &&
771 git submodule init &&
772 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
776 test_expect_success
'../subrepo works with helper URL- helper:://hostname/repo' '
779 cp pristine-.git-config .git/config &&
780 cp pristine-.gitmodules .gitmodules &&
781 git config remote.origin.url helper:://hostname/repo &&
782 git submodule init &&
783 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
787 test_expect_success
'../subrepo works with scp-style URL - user@host:repo' '
790 cp pristine-.git-config .git/config &&
791 git config remote.origin.url user@host:repo &&
792 git submodule init &&
793 test "$(git config submodule.sub.url)" = user@host:subrepo
797 test_expect_success
'../subrepo works with scp-style URL - user@host:path/to/repo' '
800 cp pristine-.git-config .git/config &&
801 cp pristine-.gitmodules .gitmodules &&
802 git config remote.origin.url user@host:path/to/repo &&
803 git submodule init &&
804 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
808 test_expect_success
'../subrepo works with relative local path - foo' '
811 cp pristine-.git-config .git/config &&
812 cp pristine-.gitmodules .gitmodules &&
813 git config remote.origin.url foo &&
814 # actual: fails with an error
815 git submodule init &&
816 test "$(git config submodule.sub.url)" = subrepo
820 test_expect_success
'../subrepo works with relative local path - foo/bar' '
823 cp pristine-.git-config .git/config &&
824 cp pristine-.gitmodules .gitmodules &&
825 git config remote.origin.url foo/bar &&
826 git submodule init &&
827 test "$(git config submodule.sub.url)" = foo/subrepo
831 test_expect_success
'../subrepo works with relative local path - ./foo' '
834 cp pristine-.git-config .git/config &&
835 cp pristine-.gitmodules .gitmodules &&
836 git config remote.origin.url ./foo &&
837 git submodule init &&
838 test "$(git config submodule.sub.url)" = subrepo
842 test_expect_success
'../subrepo works with relative local path - ./foo/bar' '
845 cp pristine-.git-config .git/config &&
846 cp pristine-.gitmodules .gitmodules &&
847 git config remote.origin.url ./foo/bar &&
848 git submodule init &&
849 test "$(git config submodule.sub.url)" = foo/subrepo
853 test_expect_success
'../subrepo works with relative local path - ../foo' '
856 cp pristine-.git-config .git/config &&
857 cp pristine-.gitmodules .gitmodules &&
858 git config remote.origin.url ../foo &&
859 git submodule init &&
860 test "$(git config submodule.sub.url)" = ../subrepo
864 test_expect_success
'../subrepo works with relative local path - ../foo/bar' '
867 cp pristine-.git-config .git/config &&
868 cp pristine-.gitmodules .gitmodules &&
869 git config remote.origin.url ../foo/bar &&
870 git submodule init &&
871 test "$(git config submodule.sub.url)" = ../foo/subrepo
875 test_expect_success
'../bar/a/b/c works with relative local path - ../foo/bar.git' '
878 cp pristine-.git-config .git/config &&
879 cp pristine-.gitmodules .gitmodules &&
881 (cd a/b/c && git init && test_commit msg) &&
882 git config remote.origin.url ../foo/bar.git &&
883 git submodule add ../bar/a/b/c ./a/b/c &&
884 git submodule init &&
885 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
889 test_expect_success
'moving the superproject does not break submodules' '
892 git submodule status >expect
894 mv addtest addtest2 &&
897 git submodule status >actual &&
898 test_cmp expect actual
902 test_expect_success
'moving the submodule does not break the superproject' '
907 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
908 mv addtest2/repo addtest2/repo.bak &&
909 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
914 test_cmp expect actual
917 test_expect_success
'submodule add --name allows to replace a submodule with another at the same path' '
922 echo "$submodurl/repo" >expect &&
923 git config remote.origin.url >actual &&
924 test_cmp expect actual &&
925 echo "gitdir: ../.git/modules/repo" >expect &&
930 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
931 test_must_be_empty actual &&
932 echo "gitdir: ../.git/modules/submod" >expect &&
933 test_cmp expect submod/.git &&
936 echo "$submodurl/bare.git" >expect &&
937 git config remote.origin.url >actual &&
938 test_cmp expect actual &&
939 echo "gitdir: ../.git/modules/repo_new" >expect &&
942 echo "repo" >expect &&
943 test_must_fail git config -f .gitmodules submodule.repo.path &&
944 git config -f .gitmodules submodule.repo_new.path >actual &&
945 test_cmp expect actual &&
946 echo "$submodurl/repo" >expect &&
947 test_must_fail git config -f .gitmodules submodule.repo.url &&
948 echo "$submodurl/bare.git" >expect &&
949 git config -f .gitmodules submodule.repo_new.url >actual &&
950 test_cmp expect actual &&
951 echo "$submodurl/repo" >expect &&
952 git config submodule.repo.url >actual &&
953 test_cmp expect actual &&
954 echo "$submodurl/bare.git" >expect &&
955 git config submodule.repo_new.url >actual &&
956 test_cmp expect actual
960 test_expect_success
'recursive relative submodules stay relative' '
961 test_when_finished "rm -rf super clone2 subsub sub3" &&
968 git commit -m "initial commit"
976 git commit -m "initial commit" &&
977 git submodule add ../subsub dirdir/subsub &&
978 git commit -m "add submodule subsub"
986 git commit -m "initial commit" &&
987 git submodule add ../sub3 &&
988 git commit -m "add submodule sub"
990 git clone super clone2 &&
993 git submodule update --init --recursive &&
994 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
995 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
997 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
998 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
1001 test_expect_success
'submodule add with an existing name fails unless forced' '
1006 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
1008 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
1009 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
1010 echo "$submodurl/bare.git" >expect &&
1011 git config submodule.repo_new.url >actual &&
1012 test_cmp expect actual &&
1013 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
1015 echo "repo" >expect &&
1016 git config -f .gitmodules submodule.repo_new.path >actual &&
1017 test_cmp expect actual &&
1018 echo "$submodurl/repo.git" >expect &&
1019 git config -f .gitmodules submodule.repo_new.url >actual &&
1020 test_cmp expect actual &&
1021 echo "$submodurl/repo.git" >expect &&
1022 git config submodule.repo_new.url >actual &&
1023 test_cmp expect actual
1027 test_expect_success
'set up a second submodule' '
1028 git submodule add ./init2 example2 &&
1029 git commit -m "submodule example2 added"
1032 test_expect_success
'submodule deinit works on repository without submodules' '
1033 test_when_finished "rm -rf newdirectory" &&
1034 mkdir newdirectory &&
1040 git commit -m "repo should not be empty" &&
1041 git submodule deinit . &&
1042 git submodule deinit --all
1046 test_expect_success
'submodule deinit should remove the whole submodule section from .git/config' '
1047 git config submodule.example.foo bar &&
1048 git config submodule.example2.frotz nitfol &&
1049 git submodule deinit init &&
1050 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1051 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1052 test -f example2/.git &&
1056 test_expect_success
'submodule deinit should unset core.worktree' '
1057 test_path_is_file .git/modules/example/config &&
1058 test_must_fail git config -f .git/modules/example/config core.worktree
1061 test_expect_success
'submodule deinit from subdirectory' '
1062 git submodule update --init &&
1063 git config submodule.example.foo bar &&
1067 git submodule deinit ../init >../output
1069 test_i18ngrep "\\.\\./init" output &&
1070 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1071 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1072 test -f example2/.git &&
1076 test_expect_success
'submodule deinit . deinits all initialized submodules' '
1077 git submodule update --init &&
1078 git config submodule.example.foo bar &&
1079 git config submodule.example2.frotz nitfol &&
1080 test_must_fail git submodule deinit &&
1081 git submodule deinit . >actual &&
1082 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1083 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1084 test_i18ngrep "Cleared directory .init" actual &&
1085 test_i18ngrep "Cleared directory .example2" actual &&
1089 test_expect_success
'submodule deinit --all deinits all initialized submodules' '
1090 git submodule update --init &&
1091 git config submodule.example.foo bar &&
1092 git config submodule.example2.frotz nitfol &&
1093 test_must_fail git submodule deinit &&
1094 git submodule deinit --all >actual &&
1095 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1096 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1097 test_i18ngrep "Cleared directory .init" actual &&
1098 test_i18ngrep "Cleared directory .example2" actual &&
1102 test_expect_success
'submodule deinit deinits a submodule when its work tree is missing or empty' '
1103 git submodule update --init &&
1104 rm -rf init example2/* example2/.git &&
1105 git submodule deinit init example2 >actual &&
1106 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1107 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1108 test_i18ngrep ! "Cleared directory .init" actual &&
1109 test_i18ngrep "Cleared directory .example2" actual &&
1113 test_expect_success
'submodule deinit fails when the submodule contains modifications unless forced' '
1114 git submodule update --init &&
1116 test_must_fail git submodule deinit init &&
1117 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1118 test -f example2/.git &&
1119 git submodule deinit -f init >actual &&
1120 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1121 test_i18ngrep "Cleared directory .init" actual &&
1125 test_expect_success
'submodule deinit fails when the submodule contains untracked files unless forced' '
1126 git submodule update --init &&
1127 echo X >>init/untracked &&
1128 test_must_fail git submodule deinit init &&
1129 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1130 test -f example2/.git &&
1131 git submodule deinit -f init >actual &&
1132 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1133 test_i18ngrep "Cleared directory .init" actual &&
1137 test_expect_success
'submodule deinit fails when the submodule HEAD does not match unless forced' '
1138 git submodule update --init &&
1143 test_must_fail git submodule deinit init &&
1144 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1145 test -f example2/.git &&
1146 git submodule deinit -f init >actual &&
1147 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1148 test_i18ngrep "Cleared directory .init" actual &&
1152 test_expect_success
'submodule deinit is silent when used on an uninitialized submodule' '
1153 git submodule update --init &&
1154 git submodule deinit init >actual &&
1155 test_i18ngrep "Submodule .example. (.*) unregistered for path .init" actual &&
1156 test_i18ngrep "Cleared directory .init" actual &&
1157 git submodule deinit init >actual &&
1158 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1159 test_i18ngrep "Cleared directory .init" actual &&
1160 git submodule deinit . >actual &&
1161 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1162 test_i18ngrep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1163 test_i18ngrep "Cleared directory .init" actual &&
1164 git submodule deinit . >actual &&
1165 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1166 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1167 test_i18ngrep "Cleared directory .init" actual &&
1168 git submodule deinit --all >actual &&
1169 test_i18ngrep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1170 test_i18ngrep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1171 test_i18ngrep "Cleared directory .init" actual &&
1175 test_expect_success
'submodule deinit fails when submodule has a .git directory even when forced' '
1176 git submodule update --init &&
1180 cp -R ../.git/modules/example .git &&
1181 GIT_WORK_TREE=. git config --unset core.worktree
1183 test_must_fail git submodule deinit init &&
1184 test_must_fail git submodule deinit -f init &&
1185 test -d init/.git &&
1186 test -n "$(git config --get-regexp "submodule\.example\.")"
1189 test_expect_success
'submodule with UTF-8 name' '
1190 svname=$(printf "\303\245 \303\244\303\266") &&
1197 git commit -m "init sub"
1199 git submodule add ./"$svname" &&
1200 git submodule >&2 &&
1201 test -n "$(git submodule | grep "$svname")"
1204 test_expect_success
'submodule add clone shallow submodule' '
1210 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1213 test 1 = $(git log --oneline | wc -l)
1218 test_expect_success
'submodule helper list is not confused by common prefixes' '
1223 echo hi >testfile2 &&
1225 git commit -m "test1"
1231 echo hello >testfile1 &&
1233 git commit -m "test2"
1235 git submodule add /dir1/b dir1/b &&
1236 git submodule add /dir2/b dir2/b &&
1237 git commit -m "first submodule commit" &&
1238 git submodule--helper list dir1/b | cut -f 2 >actual &&
1239 echo "dir1/b" >expect &&
1240 test_cmp expect actual
1243 test_expect_success
'setup superproject with submodules' '
1245 test_commit -C sub1 test &&
1246 test_commit -C sub1 test2 &&
1247 git init multisuper &&
1248 git -C multisuper submodule add ../sub1 sub0 &&
1249 git -C multisuper submodule add ../sub1 sub1 &&
1250 git -C multisuper submodule add ../sub1 sub2 &&
1251 git -C multisuper submodule add ../sub1 sub3 &&
1252 git -C multisuper commit -m "add some submodules"
1262 test_expect_success
'submodule update --init with a specification' '
1263 test_when_finished "rm -rf multisuper_clone" &&
1265 git clone file://"$pwd"/multisuper multisuper_clone &&
1266 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1267 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1268 test_cmp expect actual
1271 test_expect_success
'submodule update --init with submodule.active set' '
1272 test_when_finished "rm -rf multisuper_clone" &&
1274 git clone file://"$pwd"/multisuper multisuper_clone &&
1275 git -C multisuper_clone config submodule.active "." &&
1276 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1277 git -C multisuper_clone submodule update --init &&
1278 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1279 test_cmp expect actual
1282 test_expect_success
'submodule update and setting submodule.<name>.active' '
1283 test_when_finished "rm -rf multisuper_clone" &&
1285 git clone file://"$pwd"/multisuper multisuper_clone &&
1286 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1287 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1288 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1290 cat >expect <<-\EOF &&
1296 git -C multisuper_clone submodule update &&
1297 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1298 test_cmp expect actual
1301 test_expect_success
'clone active submodule without submodule url set' '
1302 test_when_finished "rm -rf test/test" &&
1304 # another dir breaks accidental relative paths still being correct
1305 git clone file://"$pwd"/multisuper test/test &&
1308 git config submodule.active "." &&
1310 # do not pass --init flag, as the submodule is already active:
1311 git submodule update &&
1312 git submodule status >actual_raw &&
1314 cut -d" " -f3- actual_raw >actual &&
1315 cat >expect <<-\EOF &&
1321 test_cmp expect actual
1325 test_expect_success
'clone --recurse-submodules with a pathspec works' '
1326 test_when_finished "rm -rf multisuper_clone" &&
1327 cat >expected <<-\EOF &&
1334 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1335 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1336 test_cmp expected actual
1339 test_expect_success
'clone with multiple --recurse-submodules options' '
1340 test_when_finished "rm -rf multisuper_clone" &&
1341 cat >expect <<-\EOF &&
1348 git clone --recurse-submodules="." \
1349 --recurse-submodules=":(exclude)sub0" \
1350 --recurse-submodules=":(exclude)sub2" \
1351 multisuper multisuper_clone &&
1352 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1353 test_cmp expect actual
1356 test_expect_success
'clone and subsequent updates correctly auto-initialize submodules' '
1357 test_when_finished "rm -rf multisuper_clone" &&
1358 cat <<-\EOF >expect &&
1365 cat <<-\EOF >expect2 &&
1374 git clone --recurse-submodules="." \
1375 --recurse-submodules=":(exclude)sub0" \
1376 --recurse-submodules=":(exclude)sub2" \
1377 --recurse-submodules=":(exclude)sub4" \
1378 multisuper multisuper_clone &&
1380 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1381 test_cmp expect actual &&
1383 git -C multisuper submodule add ../sub1 sub4 &&
1384 git -C multisuper submodule add ../sub1 sub5 &&
1385 git -C multisuper commit -m "add more submodules" &&
1386 # obtain the new superproject
1387 git -C multisuper_clone pull &&
1388 git -C multisuper_clone submodule update --init &&
1389 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1390 test_cmp expect2 actual
1393 test_expect_success
'init properly sets the config' '
1394 test_when_finished "rm -rf multisuper_clone" &&
1395 git clone --recurse-submodules="." \
1396 --recurse-submodules=":(exclude)sub0" \
1397 multisuper multisuper_clone &&
1399 git -C multisuper_clone submodule init -- sub0 sub1 &&
1400 git -C multisuper_clone config --get submodule.sub0.active &&
1401 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1404 test_expect_success
'recursive clone respects -q' '
1405 test_when_finished "rm -rf multisuper_clone" &&
1406 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1407 test_must_be_empty actual