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
'setup - enable local submodules' '
18 git config --global protocol.file.allow always
21 test_expect_success
'submodule usage: -h' '
22 git submodule -h >out 2>err &&
23 grep "^usage: git submodule" out &&
24 test_must_be_empty err
27 test_expect_success
'submodule usage: --recursive' '
28 test_expect_code 1 git submodule --recursive >out 2>err &&
29 grep "^usage: git submodule" err &&
30 test_must_be_empty out
33 test_expect_success
'submodule usage: status --' '
34 test_expect_code 1 git submodule -- &&
35 test_expect_code 1 git submodule --end-of-options
38 for opt
in '--quiet' '--cached'
40 test_expect_success
"submodule usage: status $opt" '
42 git submodule status $opt &&
43 git submodule $opt status
47 test_expect_success
'submodule deinit works on empty repository' '
48 git submodule deinit --all
51 test_expect_success
'setup - initial commit' '
54 git commit -m "initial commit" &&
58 test_expect_success
'submodule init aborts on missing .gitmodules file' '
59 test_when_finished "git update-index --remove sub" &&
60 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
61 # missing the .gitmodules file here
62 test_must_fail git submodule init 2>actual &&
63 test_grep "No url found for submodule path" actual
66 test_expect_success
'submodule update aborts on missing .gitmodules file' '
67 test_when_finished "git update-index --remove sub" &&
68 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
69 # missing the .gitmodules file here
70 git submodule update sub 2>actual &&
71 test_grep "Submodule path .sub. not initialized" actual
74 test_expect_success
'submodule update aborts on missing gitmodules url' '
75 test_when_finished "git update-index --remove sub" &&
76 git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
77 test_when_finished "rm -f .gitmodules" &&
78 git config -f .gitmodules submodule.s.path sub &&
79 test_must_fail git submodule init
82 test_expect_success
'add aborts on repository with no commits' '
83 cat >expect <<-\EOF &&
84 fatal: '"'repo-no-commits'"' does not have a commit checked out
86 git init repo-no-commits &&
87 test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
88 test_cmp expect actual
91 test_expect_success
'status should ignore inner git repo when not added' '
99 git commit -m "initial"
101 test_must_fail git submodule status inner 2>output.err &&
103 test_grep "^error: .*did not match any file(s) known to git" output.err
106 test_expect_success
'setup - repository in init subdirectory' '
113 git commit -m "submodule commit 1" &&
114 git tag -a -m "rev-1" rev-1
118 test_expect_success
'setup - commit with gitlink' '
122 git commit -m "super commit 1"
125 test_expect_success
'setup - hide init subdirectory' '
129 test_expect_success
'setup - repository to add submodules to' '
131 git init addtest-ignore
134 # The 'submodule add' tests need some repository to add as a submodule.
135 # The trash directory is a good one as any. We need to canonicalize
136 # the name, though, as some tests compare it to the absolute path git
137 # generates, which will expand symbolic links.
141 git for-each-ref
--format='%(refname)' 'refs/heads/*'
150 listbranches
>"$dotdot/heads" &&
151 { git symbolic-ref HEAD ||
:; } >"$dotdot/head" &&
152 git rev-parse HEAD
>"$dotdot/head-sha1" &&
153 git update-index
--refresh &&
154 git diff-files
--exit-code &&
155 git clean
-n -d -x >"$dotdot/untracked"
159 test_expect_success
'submodule add' '
160 echo "refs/heads/main" >expect &&
164 git submodule add -q "$submodurl" submod >actual &&
165 test_must_be_empty actual &&
166 echo "gitdir: ../.git/modules/submod" >expect &&
167 test_cmp expect submod/.git &&
170 git config core.worktree >actual &&
171 echo "../../../submod" >expect &&
172 test_cmp expect actual &&
178 rm -f heads head untracked &&
179 inspect addtest/submod ../.. &&
180 test_cmp expect heads &&
181 test_cmp expect head &&
182 test_must_be_empty untracked
185 test_expect_success
!WINDOWS
'submodule add (absolute path)' '
186 test_when_finished "git reset --hard" &&
187 git submodule add "$submodurl" "$submodurl/add-abs"
190 test_expect_success
'setup parent and one repository' '
191 test_create_repo parent &&
192 test_commit -C parent one
195 test_expect_success
'redirected submodule add does not show progress' '
196 git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
199 test_grep ! "Checking connectivity" err
202 test_expect_success
'redirected submodule add --progress does show progress' '
203 git -C addtest submodule add --progress "file://$submodurl/parent" \
204 submod-redirected-progress 2>err && \
208 test_expect_success
'submodule add to .gitignored path fails' '
211 cat <<-\EOF >expect &&
212 The following paths are ignored by one of your .gitignore files:
214 hint: Use -f if you really want to add them.
215 hint: Disable this message with "git config advice.addIgnoredFile false"
217 # Does not use test_commit due to the ignore
218 echo "*" > .gitignore &&
219 git add --force .gitignore &&
220 git commit -m"Ignore everything" &&
221 ! git submodule add "$submodurl" submod >actual 2>&1 &&
222 test_cmp expect actual
226 test_expect_success
'submodule add to .gitignored path with --force' '
229 git submodule add --force "$submodurl" submod
233 test_expect_success
'submodule add to path with tracked content fails' '
236 echo "fatal: '\''dir-tracked'\'' already exists in the index" >expect &&
238 test_commit foo dir-tracked/bar &&
239 test_must_fail git submodule add "$submodurl" dir-tracked >actual 2>&1 &&
240 test_cmp expect actual
244 test_expect_success
'submodule add to reconfigure existing submodule with --force' '
247 bogus_url="$(pwd)/bogus-url" &&
248 git submodule add --force "$bogus_url" submod &&
249 git submodule add --force -b initial "$submodurl" submod-branch &&
250 test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
251 test "$bogus_url" = "$(git config submodule.submod.url)" &&
253 git submodule add --force "$submodurl" submod &&
254 test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
255 test "$submodurl" = "$(git config submodule.submod.url)"
259 test_expect_success
'submodule add relays add --dry-run stderr' '
260 test_when_finished "rm -rf addtest/.git/index.lock" &&
263 : >.git/index.lock &&
264 ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
265 test_grep "^fatal: .*index\.lock" output.err &&
266 test_path_is_missing sub-while-locked
270 test_expect_success
'submodule add --branch' '
271 echo "refs/heads/initial" >expect-head &&
272 cat <<-\EOF >expect-heads &&
279 git submodule add -b initial "$submodurl" submod-branch &&
280 test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
284 rm -f heads head untracked &&
285 inspect addtest/submod-branch ../.. &&
286 test_cmp expect-heads heads &&
287 test_cmp expect-head head &&
288 test_must_be_empty untracked
291 test_expect_success
'submodule add with ./ in path' '
292 echo "refs/heads/main" >expect &&
296 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
300 rm -f heads head untracked &&
301 inspect addtest/dotsubmod/frotz ../../.. &&
302 test_cmp expect heads &&
303 test_cmp expect head &&
304 test_must_be_empty untracked
307 test_expect_success
'submodule add with /././ in path' '
308 echo "refs/heads/main" >expect &&
312 git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
316 rm -f heads head untracked &&
317 inspect addtest/dotslashdotsubmod/frotz ../../.. &&
318 test_cmp expect heads &&
319 test_cmp expect head &&
320 test_must_be_empty untracked
323 test_expect_success
'submodule add with // in path' '
324 echo "refs/heads/main" >expect &&
328 git submodule add "$submodurl" slashslashsubmod///frotz// &&
332 rm -f heads head untracked &&
333 inspect addtest/slashslashsubmod/frotz ../../.. &&
334 test_cmp expect heads &&
335 test_cmp expect head &&
336 test_must_be_empty untracked
339 test_expect_success
'submodule add with /.. in path' '
340 echo "refs/heads/main" >expect &&
344 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
348 rm -f heads head untracked &&
349 inspect addtest/realsubmod ../.. &&
350 test_cmp expect heads &&
351 test_cmp expect head &&
352 test_must_be_empty untracked
355 test_expect_success
'submodule add with ./, /.. and // in path' '
356 echo "refs/heads/main" >expect &&
360 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
364 rm -f heads head untracked &&
365 inspect addtest/realsubmod2 ../.. &&
366 test_cmp expect heads &&
367 test_cmp expect head &&
368 test_must_be_empty untracked
371 test_expect_success
!CYGWIN
'submodule add with \\ in path' '
372 test_when_finished "rm -rf parent sub\\with\\backslash" &&
374 # Initialize a repo with a backslash in its name
375 git init sub\\with\\backslash &&
376 touch sub\\with\\backslash/empty.file &&
377 git -C sub\\with\\backslash add empty.file &&
378 git -C sub\\with\\backslash commit -m "Added empty.file" &&
380 # Add that repository as a submodule
382 git -C parent submodule add ../sub\\with\\backslash
385 test_expect_success
'submodule add in subdirectory' '
386 echo "refs/heads/main" >expect &&
391 git submodule add "$submodurl" ../realsubmod3 &&
395 rm -f heads head untracked &&
396 inspect addtest/realsubmod3 ../.. &&
397 test_cmp expect heads &&
398 test_cmp expect head &&
399 test_must_be_empty untracked
402 test_expect_success
'submodule add in subdirectory with relative path should fail' '
405 test_must_fail git submodule add ../../ submod3 2>../../output.err
407 test_grep toplevel output.err
410 test_expect_success
'setup - add an example entry to .gitmodules' '
411 git config --file=.gitmodules submodule.example.url git://example.com/init.git
414 test_expect_success
'status should fail for unmapped paths' '
415 test_must_fail git submodule status
418 test_expect_success
'setup - map path in .gitmodules' '
419 cat <<\EOF >expect &&
420 [submodule "example"]
421 url = git://example.com/init.git
425 git config --file=.gitmodules submodule.example.path init &&
427 test_cmp expect .gitmodules
430 test_expect_success
'status should only print one line' '
431 git submodule status >lines &&
432 test_line_count = 1 lines
435 test_expect_success
'status from subdirectory should have the same SHA1' '
436 test_when_finished "rmdir addtest/subdir" &&
440 git submodule status >output &&
441 awk "{print \$1}" <output >expect &&
443 git submodule status >../output &&
444 awk "{print \$1}" <../output >../actual &&
445 test_cmp ../expect ../actual &&
446 git -C ../submod checkout HEAD^ &&
447 git submodule status >../output &&
448 awk "{print \$1}" <../output >../actual2 &&
450 git submodule status >output &&
451 awk "{print \$1}" <output >expect2 &&
452 test_cmp expect2 actual2 &&
453 ! test_cmp actual actual2
457 test_expect_success
'setup - fetch commit name from submodule' '
458 rev1=$(cd .subrepo && git rev-parse HEAD) &&
459 printf "rev1: %s\n" "$rev1" &&
463 test_expect_success
'status should initially be "missing"' '
464 git submodule status >lines &&
468 test_expect_success
'init should register submodule url in .git/config' '
469 echo git://example.com/init.git >expect &&
471 git submodule init &&
472 git config submodule.example.url >url &&
473 git config submodule.example.url ./.subrepo &&
478 test_expect_success
'status should still be "missing" after initializing' '
481 git submodule status >lines &&
486 test_failure_with_unknown_submodule
() {
487 test_must_fail git submodule
$1 no-such-submodule
2>output.err
&&
488 test_grep
"^error: .*no-such-submodule" output.err
491 test_expect_success
'init should fail with unknown submodule' '
492 test_failure_with_unknown_submodule init
495 test_expect_success
'update should fail with unknown submodule' '
496 test_failure_with_unknown_submodule update
499 test_expect_success
'status should fail with unknown submodule' '
500 test_failure_with_unknown_submodule status
503 test_expect_success
'sync should fail with unknown submodule' '
504 test_failure_with_unknown_submodule sync
507 test_expect_success
'update should fail when path is used by a file' '
508 echo hello >expect &&
510 echo "hello" >init &&
511 test_must_fail git submodule update &&
516 test_expect_success
'update should fail when path is used by a nonempty directory' '
517 echo hello >expect &&
521 echo "hello" >init/a &&
523 test_must_fail git submodule update &&
525 test_cmp expect init/a
528 test_expect_success
'update should work when path is an empty dir' '
531 echo "$rev1" >expect &&
534 git submodule update -q >update.out &&
535 test_must_be_empty update.out &&
538 test_cmp expect head-sha1
541 test_expect_success
'status should be "up-to-date" after update' '
542 git submodule status >list &&
546 test_expect_success
'status "up-to-date" from subdirectory' '
550 git submodule status >../list
552 grep "^ $rev1" list &&
553 grep "\\.\\./init" list
556 test_expect_success
'status "up-to-date" from subdirectory with path' '
560 git submodule status ../init >../list
562 grep "^ $rev1" list &&
563 grep "\\.\\./init" list
566 test_expect_success
'status should be "modified" after submodule commit' '
571 git commit -m "submodule commit 2"
574 rev2=$(cd init && git rev-parse HEAD) &&
576 git submodule status >list &&
581 test_expect_success
'"submodule --cached" command forms should be identical' '
582 git submodule status --cached >expect &&
584 git submodule --cached >actual &&
585 test_cmp expect actual &&
587 git submodule --cached status >actual &&
588 test_cmp expect actual
591 test_expect_success
'the --cached sha1 should be rev1' '
592 git submodule --cached status >list &&
596 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
598 grep "^+Subproject commit $rev2" diff
601 test_expect_success
'update should checkout rev1' '
603 echo "$rev1" >expect &&
605 git submodule update init &&
608 test_cmp expect head-sha1
611 test_expect_success
'status should be "up-to-date" after update' '
612 git submodule status >list &&
616 test_expect_success
'checkout superproject with subproject already present' '
617 git checkout initial &&
621 test_expect_success
'apply submodule diff' '
627 git commit -m "change subproject"
629 git update-index --add init &&
630 git commit -m "change init" &&
631 git format-patch -1 --stdout >P.diff &&
632 git checkout second &&
633 git apply --index P.diff &&
635 git diff --cached main >staged &&
636 test_must_be_empty staged
639 test_expect_success
'update --init' '
641 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
642 git config --remove-section submodule.example &&
643 test_must_fail git config submodule.example.url &&
645 git submodule update init 2> update.out &&
646 test_grep "not initialized" update.out &&
647 test_must_fail git rev-parse --resolve-git-dir init/.git &&
649 git submodule update --init init &&
650 git rev-parse --resolve-git-dir init/.git
653 test_expect_success
'update --init from subdirectory' '
655 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
656 git config --remove-section submodule.example &&
657 test_must_fail git config submodule.example.url &&
662 git submodule update ../init 2>update.out &&
663 test_grep "not initialized" update.out &&
664 test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
666 git submodule update --init ../init
668 git rev-parse --resolve-git-dir init/.git
671 test_expect_success
'do not add files from a submodule' '
674 test_must_fail git add init/a
678 test_expect_success
'gracefully add/reset submodule with a trailing slash' '
681 git commit -m "commit subproject" init &&
685 git diff --exit-code --cached init &&
687 git commit -m update a >/dev/null &&
688 git rev-parse HEAD) &&
690 test_must_fail git diff --exit-code --cached init &&
691 test $commit = $(git ls-files --stage |
692 sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
694 git diff --exit-code --cached init
698 test_expect_success
'ls-files gracefully handles trailing slash' '
700 test "init" = "$(git ls-files init/)"
704 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
708 git checkout initial &&
713 test_expect_success
'submodule <invalid-subcommand> fails' '
714 test_must_fail git submodule no-such-subcommand
717 test_expect_success
'add submodules without specifying an explicit path' '
724 git commit -m "repo commit 1"
726 git clone --bare repo/ bare.git &&
729 git submodule add "$submodurl/repo" &&
730 git config -f .gitmodules submodule.repo.path repo &&
731 git submodule add "$submodurl/bare.git" &&
732 git config -f .gitmodules submodule.bare.path bare
736 test_expect_success
'add should fail when path is used by a file' '
740 test_must_fail git submodule add "$submodurl/repo" file
744 test_expect_success
'add should fail when path is used by an existing directory' '
748 test_must_fail git submodule add "$submodurl/repo" empty-dir
752 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
755 git submodule add ../repo relative &&
756 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
757 git submodule sync relative &&
758 test "$(git config submodule.relative.url)" = "$submodurl/repo"
762 test_expect_success
'set up for relative path tests' '
774 git config -f .gitmodules submodule.sub.path sub &&
775 git config -f .gitmodules submodule.sub.url ../subrepo &&
776 cp .git/config pristine-.git-config &&
777 cp .gitmodules pristine-.gitmodules
781 test_expect_success
'../subrepo works with URL - ssh://hostname/repo' '
784 cp pristine-.git-config .git/config &&
785 cp pristine-.gitmodules .gitmodules &&
786 git config remote.origin.url ssh://hostname/repo &&
787 git submodule init &&
788 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
792 test_expect_success
'../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
795 cp pristine-.git-config .git/config &&
796 cp pristine-.gitmodules .gitmodules &&
797 git config remote.origin.url ssh://hostname:22/repo &&
798 git submodule init &&
799 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
803 # About the choice of the path in the next test:
804 # - double-slash side-steps path mangling issues on Windows
805 # - it is still an absolute local path
806 # - there cannot be a server with a blank in its name just in case the
807 # path is used erroneously to access a //server/share style path
808 test_expect_success
'../subrepo path works with local path - //somewhere else/repo' '
811 cp pristine-.git-config .git/config &&
812 cp pristine-.gitmodules .gitmodules &&
813 git config remote.origin.url "//somewhere else/repo" &&
814 git submodule init &&
815 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
819 test_expect_success
'../subrepo works with file URL - file:///tmp/repo' '
822 cp pristine-.git-config .git/config &&
823 cp pristine-.gitmodules .gitmodules &&
824 git config remote.origin.url file:///tmp/repo &&
825 git submodule init &&
826 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
830 test_expect_success
'../subrepo works with helper URL- helper:://hostname/repo' '
833 cp pristine-.git-config .git/config &&
834 cp pristine-.gitmodules .gitmodules &&
835 git config remote.origin.url helper:://hostname/repo &&
836 git submodule init &&
837 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
841 test_expect_success
'../subrepo works with scp-style URL - user@host:repo' '
844 cp pristine-.git-config .git/config &&
845 git config remote.origin.url user@host:repo &&
846 git submodule init &&
847 test "$(git config submodule.sub.url)" = user@host:subrepo
851 test_expect_success
'../subrepo works with scp-style URL - user@host:path/to/repo' '
854 cp pristine-.git-config .git/config &&
855 cp pristine-.gitmodules .gitmodules &&
856 git config remote.origin.url user@host:path/to/repo &&
857 git submodule init &&
858 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
862 test_expect_success
'../subrepo works with relative local path - foo' '
865 cp pristine-.git-config .git/config &&
866 cp pristine-.gitmodules .gitmodules &&
867 git config remote.origin.url foo &&
868 # actual: fails with an error
869 git submodule init &&
870 test "$(git config submodule.sub.url)" = subrepo
874 test_expect_success
'../subrepo works with relative local path - foo/bar' '
877 cp pristine-.git-config .git/config &&
878 cp pristine-.gitmodules .gitmodules &&
879 git config remote.origin.url foo/bar &&
880 git submodule init &&
881 test "$(git config submodule.sub.url)" = foo/subrepo
885 test_expect_success
'../subrepo works with relative local path - ./foo' '
888 cp pristine-.git-config .git/config &&
889 cp pristine-.gitmodules .gitmodules &&
890 git config remote.origin.url ./foo &&
891 git submodule init &&
892 test "$(git config submodule.sub.url)" = subrepo
896 test_expect_success
'../subrepo works with relative local path - ./foo/bar' '
899 cp pristine-.git-config .git/config &&
900 cp pristine-.gitmodules .gitmodules &&
901 git config remote.origin.url ./foo/bar &&
902 git submodule init &&
903 test "$(git config submodule.sub.url)" = foo/subrepo
907 test_expect_success
'../subrepo works with relative local path - ../foo' '
910 cp pristine-.git-config .git/config &&
911 cp pristine-.gitmodules .gitmodules &&
912 git config remote.origin.url ../foo &&
913 git submodule init &&
914 test "$(git config submodule.sub.url)" = ../subrepo
918 test_expect_success
'../subrepo works with relative local path - ../foo/bar' '
921 cp pristine-.git-config .git/config &&
922 cp pristine-.gitmodules .gitmodules &&
923 git config remote.origin.url ../foo/bar &&
924 git submodule init &&
925 test "$(git config submodule.sub.url)" = ../foo/subrepo
929 test_expect_success
'../bar/a/b/c works with relative local path - ../foo/bar.git' '
932 cp pristine-.git-config .git/config &&
933 cp pristine-.gitmodules .gitmodules &&
935 (cd a/b/c && git init && test_commit msg) &&
936 git config remote.origin.url ../foo/bar.git &&
937 git submodule add ../bar/a/b/c ./a/b/c &&
938 git submodule init &&
939 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
943 test_expect_success
'moving the superproject does not break submodules' '
946 git submodule status >expect
948 mv addtest addtest2 &&
951 git submodule status >actual &&
952 test_cmp expect actual
956 test_expect_success
'moving the submodule does not break the superproject' '
961 sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
962 mv addtest2/repo addtest2/repo.bak &&
963 test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
968 test_cmp expect actual
971 test_expect_success
'submodule add --name allows to replace a submodule with another at the same path' '
976 echo "$submodurl/repo" >expect &&
977 git config remote.origin.url >actual &&
978 test_cmp expect actual &&
979 echo "gitdir: ../.git/modules/repo" >expect &&
984 git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
985 test_must_be_empty actual &&
986 echo "gitdir: ../.git/modules/submod" >expect &&
987 test_cmp expect submod/.git &&
990 echo "$submodurl/bare.git" >expect &&
991 git config remote.origin.url >actual &&
992 test_cmp expect actual &&
993 echo "gitdir: ../.git/modules/repo_new" >expect &&
996 echo "repo" >expect &&
997 test_must_fail git config -f .gitmodules submodule.repo.path &&
998 git config -f .gitmodules submodule.repo_new.path >actual &&
999 test_cmp expect actual &&
1000 echo "$submodurl/repo" >expect &&
1001 test_must_fail git config -f .gitmodules submodule.repo.url &&
1002 echo "$submodurl/bare.git" >expect &&
1003 git config -f .gitmodules submodule.repo_new.url >actual &&
1004 test_cmp expect actual &&
1005 echo "$submodurl/repo" >expect &&
1006 git config submodule.repo.url >actual &&
1007 test_cmp expect actual &&
1008 echo "$submodurl/bare.git" >expect &&
1009 git config submodule.repo_new.url >actual &&
1010 test_cmp expect actual
1014 test_expect_success
'recursive relative submodules stay relative' '
1015 test_when_finished "rm -rf super clone2 subsub sub3" &&
1022 git commit -m "initial commit"
1030 git commit -m "initial commit" &&
1031 git submodule add ../subsub dirdir/subsub &&
1032 git commit -m "add submodule subsub"
1040 git commit -m "initial commit" &&
1041 git submodule add ../sub3 &&
1042 git commit -m "add submodule sub"
1044 git clone super clone2 &&
1047 git submodule update --init --recursive &&
1048 echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
1049 echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
1051 test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
1052 test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
1055 test_expect_success
'submodule add with an existing name fails unless forced' '
1060 test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
1062 test_must_fail git config -f .gitmodules submodule.repo_new.path &&
1063 test_must_fail git config -f .gitmodules submodule.repo_new.url &&
1064 echo "$submodurl/bare.git" >expect &&
1065 git config submodule.repo_new.url >actual &&
1066 test_cmp expect actual &&
1067 git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
1069 echo "repo" >expect &&
1070 git config -f .gitmodules submodule.repo_new.path >actual &&
1071 test_cmp expect actual &&
1072 echo "$submodurl/repo.git" >expect &&
1073 git config -f .gitmodules submodule.repo_new.url >actual &&
1074 test_cmp expect actual &&
1075 echo "$submodurl/repo.git" >expect &&
1076 git config submodule.repo_new.url >actual &&
1077 test_cmp expect actual
1081 test_expect_success
'set up a second submodule' '
1082 git submodule add ./init2 example2 &&
1083 git commit -m "submodule example2 added"
1086 test_expect_success
'submodule deinit works on repository without submodules' '
1087 test_when_finished "rm -rf newdirectory" &&
1088 mkdir newdirectory &&
1094 git commit -m "repo should not be empty" &&
1095 git submodule deinit . &&
1096 git submodule deinit --all
1100 test_expect_success
'submodule deinit should remove the whole submodule section from .git/config' '
1101 git config submodule.example.foo bar &&
1102 git config submodule.example2.frotz nitfol &&
1103 git submodule deinit init &&
1104 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1105 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1106 test -f example2/.git &&
1110 test_expect_success
'submodule deinit should unset core.worktree' '
1111 test_path_is_file .git/modules/example/config &&
1112 test_must_fail git config -f .git/modules/example/config core.worktree
1115 test_expect_success
'submodule deinit from subdirectory' '
1116 git submodule update --init &&
1117 git config submodule.example.foo bar &&
1121 git submodule deinit ../init >../output
1123 test_grep "\\.\\./init" output &&
1124 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1125 test -n "$(git config --get-regexp "submodule\.example2\.")" &&
1126 test -f example2/.git &&
1130 test_expect_success
'submodule deinit . deinits all initialized submodules' '
1131 git submodule update --init &&
1132 git config submodule.example.foo bar &&
1133 git config submodule.example2.frotz nitfol &&
1134 test_must_fail git submodule deinit &&
1135 git submodule deinit . >actual &&
1136 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1137 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1138 test_grep "Cleared directory .init" actual &&
1139 test_grep "Cleared directory .example2" actual &&
1143 test_expect_success
'submodule deinit --all deinits all initialized submodules' '
1144 git submodule update --init &&
1145 git config submodule.example.foo bar &&
1146 git config submodule.example2.frotz nitfol &&
1147 test_must_fail git submodule deinit &&
1148 git submodule deinit --all >actual &&
1149 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1150 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1151 test_grep "Cleared directory .init" actual &&
1152 test_grep "Cleared directory .example2" actual &&
1156 test_expect_success
'submodule deinit deinits a submodule when its work tree is missing or empty' '
1157 git submodule update --init &&
1158 rm -rf init example2/* example2/.git &&
1159 git submodule deinit init example2 >actual &&
1160 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1161 test -z "$(git config --get-regexp "submodule\.example2\.")" &&
1162 test_grep ! "Cleared directory .init" actual &&
1163 test_grep "Cleared directory .example2" actual &&
1167 test_expect_success
'submodule deinit fails when the submodule contains modifications unless forced' '
1168 git submodule update --init &&
1170 test_must_fail git submodule deinit init &&
1171 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1172 test -f example2/.git &&
1173 git submodule deinit -f init >actual &&
1174 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1175 test_grep "Cleared directory .init" actual &&
1179 test_expect_success
'submodule deinit fails when the submodule contains untracked files unless forced' '
1180 git submodule update --init &&
1181 echo X >>init/untracked &&
1182 test_must_fail git submodule deinit init &&
1183 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1184 test -f example2/.git &&
1185 git submodule deinit -f init >actual &&
1186 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1187 test_grep "Cleared directory .init" actual &&
1191 test_expect_success
'submodule deinit fails when the submodule HEAD does not match unless forced' '
1192 git submodule update --init &&
1197 test_must_fail git submodule deinit init &&
1198 test -n "$(git config --get-regexp "submodule\.example\.")" &&
1199 test -f example2/.git &&
1200 git submodule deinit -f init >actual &&
1201 test -z "$(git config --get-regexp "submodule\.example\.")" &&
1202 test_grep "Cleared directory .init" actual &&
1206 test_expect_success
'submodule deinit is silent when used on an uninitialized submodule' '
1207 git submodule update --init &&
1208 git submodule deinit init >actual &&
1209 test_grep "Submodule .example. (.*) unregistered for path .init" actual &&
1210 test_grep "Cleared directory .init" actual &&
1211 git submodule deinit init >actual &&
1212 test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1213 test_grep "Cleared directory .init" actual &&
1214 git submodule deinit . >actual &&
1215 test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1216 test_grep "Submodule .example2. (.*) unregistered for path .example2" actual &&
1217 test_grep "Cleared directory .init" actual &&
1218 git submodule deinit . >actual &&
1219 test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1220 test_grep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1221 test_grep "Cleared directory .init" actual &&
1222 git submodule deinit --all >actual &&
1223 test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
1224 test_grep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
1225 test_grep "Cleared directory .init" actual &&
1229 test_expect_success
'submodule deinit absorbs .git directory if .git is a directory' '
1230 git submodule update --init &&
1234 mv ../.git/modules/example .git &&
1235 GIT_WORK_TREE=. git config --unset core.worktree
1237 git submodule deinit init &&
1238 test_path_is_missing init/.git &&
1239 test -z "$(git config --get-regexp "submodule\.example\.")"
1242 test_expect_success
'submodule with UTF-8 name' '
1243 svname=$(printf "\303\245 \303\244\303\266") &&
1250 git commit -m "init sub"
1252 git submodule add ./"$svname" &&
1253 git submodule >&2 &&
1254 test -n "$(git submodule | grep "$svname")"
1257 test_expect_success
'submodule add clone shallow submodule' '
1263 git submodule add --depth=1 file://"$pwd"/example2 submodule &&
1266 test 1 = $(git log --oneline | wc -l)
1271 test_expect_success
'setup superproject with submodules' '
1273 test_commit -C sub1 test &&
1274 test_commit -C sub1 test2 &&
1275 git init multisuper &&
1276 git -C multisuper submodule add ../sub1 sub0 &&
1277 git -C multisuper submodule add ../sub1 sub1 &&
1278 git -C multisuper submodule add ../sub1 sub2 &&
1279 git -C multisuper submodule add ../sub1 sub3 &&
1280 git -C multisuper commit -m "add some submodules"
1290 test_expect_success
'submodule update --init with a specification' '
1291 test_when_finished "rm -rf multisuper_clone" &&
1293 git clone file://"$pwd"/multisuper multisuper_clone &&
1294 git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
1295 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1296 test_cmp expect actual
1299 test_expect_success
'submodule update --init with submodule.active set' '
1300 test_when_finished "rm -rf multisuper_clone" &&
1302 git clone file://"$pwd"/multisuper multisuper_clone &&
1303 git -C multisuper_clone config submodule.active "." &&
1304 git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
1305 git -C multisuper_clone submodule update --init &&
1306 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1307 test_cmp expect actual
1310 test_expect_success
'submodule update and setting submodule.<name>.active' '
1311 test_when_finished "rm -rf multisuper_clone" &&
1313 git clone file://"$pwd"/multisuper multisuper_clone &&
1314 git -C multisuper_clone config --bool submodule.sub0.active "true" &&
1315 git -C multisuper_clone config --bool submodule.sub1.active "false" &&
1316 git -C multisuper_clone config --bool submodule.sub2.active "true" &&
1318 cat >expect <<-\EOF &&
1324 git -C multisuper_clone submodule update &&
1325 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1326 test_cmp expect actual
1329 test_expect_success
'clone active submodule without submodule url set' '
1330 test_when_finished "rm -rf test/test" &&
1332 # another dir breaks accidental relative paths still being correct
1333 git clone file://"$pwd"/multisuper test/test &&
1336 git config submodule.active "." &&
1338 # do not pass --init flag, as the submodule is already active:
1339 git submodule update &&
1340 git submodule status >actual_raw &&
1342 cut -d" " -f3- actual_raw >actual &&
1343 cat >expect <<-\EOF &&
1349 test_cmp expect actual
1353 test_expect_success
'update submodules without url set in .gitconfig' '
1354 test_when_finished "rm -rf multisuper_clone" &&
1355 git clone file://"$pwd"/multisuper multisuper_clone &&
1357 git -C multisuper_clone submodule init &&
1358 for s in sub0 sub1 sub2 sub3
1360 key=submodule.$s.url &&
1361 git -C multisuper_clone config --local --unset $key &&
1362 git -C multisuper_clone config --file .gitmodules --unset $key || return 1
1365 test_must_fail git -C multisuper_clone submodule update 2>err &&
1366 grep "cannot clone submodule .sub[0-3]. without a URL" err
1369 test_expect_success
'clone --recurse-submodules with a pathspec works' '
1370 test_when_finished "rm -rf multisuper_clone" &&
1371 cat >expected <<-\EOF &&
1378 git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
1379 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1380 test_cmp expected actual
1383 test_expect_success
'clone with multiple --recurse-submodules options' '
1384 test_when_finished "rm -rf multisuper_clone" &&
1385 cat >expect <<-\EOF &&
1392 git clone --recurse-submodules="." \
1393 --recurse-submodules=":(exclude)sub0" \
1394 --recurse-submodules=":(exclude)sub2" \
1395 multisuper multisuper_clone &&
1396 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1397 test_cmp expect actual
1400 test_expect_success
'clone and subsequent updates correctly auto-initialize submodules' '
1401 test_when_finished "rm -rf multisuper_clone" &&
1402 cat <<-\EOF >expect &&
1409 cat <<-\EOF >expect2 &&
1418 git clone --recurse-submodules="." \
1419 --recurse-submodules=":(exclude)sub0" \
1420 --recurse-submodules=":(exclude)sub2" \
1421 --recurse-submodules=":(exclude)sub4" \
1422 multisuper multisuper_clone &&
1424 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1425 test_cmp expect actual &&
1427 git -C multisuper submodule add ../sub1 sub4 &&
1428 git -C multisuper submodule add ../sub1 sub5 &&
1429 git -C multisuper commit -m "add more submodules" &&
1430 # obtain the new superproject
1431 git -C multisuper_clone pull &&
1432 git -C multisuper_clone submodule update --init &&
1433 git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
1434 test_cmp expect2 actual
1437 test_expect_success
'init properly sets the config' '
1438 test_when_finished "rm -rf multisuper_clone" &&
1439 git clone --recurse-submodules="." \
1440 --recurse-submodules=":(exclude)sub0" \
1441 multisuper multisuper_clone &&
1443 git -C multisuper_clone submodule init -- sub0 sub1 &&
1444 git -C multisuper_clone config --get submodule.sub0.active &&
1445 test_must_fail git -C multisuper_clone config --get submodule.sub1.active
1448 test_expect_success
'recursive clone respects -q' '
1449 test_when_finished "rm -rf multisuper_clone" &&
1450 git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
1451 test_must_be_empty actual