3 # Copyright (c) 2007 Lars Hjemli
6 test_description='Basic porcelain support for submodules
8 This test tries to verify basic sanity of the init, update and status
9 subcommands of git submodule.
14 test_expect_success 'setup - initial commit' '
17 git commit -m "initial commit" &&
21 test_expect_success 'setup - repository in init subdirectory' '
28 git commit -m "submodule commit 1" &&
29 git tag -a -m "rev-1" rev-1
33 test_expect_success 'setup - commit with gitlink' '
37 git commit -m "super commit 1"
40 test_expect_success 'setup - hide init subdirectory' '
44 test_expect_success 'setup - repository to add submodules to' '
46 git init addtest-ignore
49 # The 'submodule add' tests need some repository to add as a submodule.
50 # The trash directory is a good one as any. We need to canonicalize
51 # the name, though, as some tests compare it to the absolute path git
52 # generates, which will expand symbolic links.
56 git for-each-ref --format='%(refname)' 'refs/heads/*'
65 listbranches >"$dotdot/heads" &&
66 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
67 git rev-parse HEAD >"$dotdot/head-sha1" &&
68 git update-index --refresh &&
69 git diff-files --exit-code &&
70 git clean -n -d -x >"$dotdot/untracked"
74 test_expect_success 'submodule add' '
75 echo "refs/heads/master" >expect &&
80 git submodule add -q "$submodurl" submod >actual &&
82 echo "gitdir: ../.git/modules/submod" >expect &&
83 test_cmp expect submod/.git &&
86 git config core.worktree >actual &&
87 echo "../../../submod" >expect &&
88 test_cmp expect actual &&
94 rm -f heads head untracked &&
95 inspect addtest/submod ../.. &&
96 test_cmp expect heads &&
97 test_cmp expect head &&
98 test_cmp empty untracked
101 test_expect_success 'submodule add to .gitignored path fails' '
104 cat <<-\EOF >expect &&
105 The following path is ignored by one of your .gitignore files:
107 Use -f if you really want to add it.
109 # Does not use test_commit due to the ignore
110 echo "*" > .gitignore &&
111 git add --force .gitignore &&
112 git commit -m"Ignore everything" &&
113 ! git submodule add "$submodurl" submod >actual 2>&1 &&
114 test_i18ncmp expect actual
118 test_expect_success 'submodule add to .gitignored path with --force' '
121 git submodule add --force "$submodurl" submod
125 test_expect_success 'submodule add --branch' '
126 echo "refs/heads/initial" >expect-head &&
127 cat <<-\EOF >expect-heads &&
135 git submodule add -b initial "$submodurl" submod-branch &&
136 test -z "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
140 rm -f heads head untracked &&
141 inspect addtest/submod-branch ../.. &&
142 test_cmp expect-heads heads &&
143 test_cmp expect-head head &&
144 test_cmp empty untracked
147 test_expect_success 'submodule add with ./ in path' '
148 echo "refs/heads/master" >expect &&
153 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
157 rm -f heads head untracked &&
158 inspect addtest/dotsubmod/frotz ../../.. &&
159 test_cmp expect heads &&
160 test_cmp expect head &&
161 test_cmp empty untracked
164 test_expect_success 'submodule add with // in path' '
165 echo "refs/heads/master" >expect &&
170 git submodule add "$submodurl" slashslashsubmod///frotz// &&
174 rm -f heads head untracked &&
175 inspect addtest/slashslashsubmod/frotz ../../.. &&
176 test_cmp expect heads &&
177 test_cmp expect head &&
178 test_cmp empty untracked
181 test_expect_success 'submodule add with /.. in path' '
182 echo "refs/heads/master" >expect &&
187 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
191 rm -f heads head untracked &&
192 inspect addtest/realsubmod ../.. &&
193 test_cmp expect heads &&
194 test_cmp expect head &&
195 test_cmp empty untracked
198 test_expect_success 'submodule add with ./, /.. and // in path' '
199 echo "refs/heads/master" >expect &&
204 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
208 rm -f heads head untracked &&
209 inspect addtest/realsubmod2 ../.. &&
210 test_cmp expect heads &&
211 test_cmp expect head &&
212 test_cmp empty untracked
215 test_expect_success 'submodule add --local-branch' '
218 git submodule add --local-branch "$submodurl" submod-follow-head &&
219 test "$(git config -f .gitmodules submodule.submod-follow-head.branch)" = "HEAD"
223 test_expect_success 'submodule add --local-branch --branch' '
226 git submodule add --local-branch -b initial "$submodurl" submod-auto-follow &&
227 test "$(git config -f .gitmodules submodule.submod-auto-follow.branch)" = "initial"
231 test_expect_success 'submodule add --local-branch=<name> --branch' '
234 git submodule add --local-branch=final -b initial "$submodurl" submod-follow &&
235 test "$(git config -f .gitmodules submodule.submod-follow.branch)" = "final"
239 test_expect_success 'setup - add an example entry to .gitmodules' '
240 GIT_CONFIG=.gitmodules \
241 git config submodule.example.url git://example.com/init.git
244 test_expect_success 'status should fail for unmapped paths' '
245 test_must_fail git submodule status
248 test_expect_success 'setup - map path in .gitmodules' '
249 cat <<\EOF >expect &&
250 [submodule "example"]
251 url = git://example.com/init.git
255 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
257 test_cmp expect .gitmodules
260 test_expect_success 'status should only print one line' '
261 git submodule status >lines &&
262 test_line_count = 1 lines
265 test_expect_success 'setup - fetch commit name from submodule' '
266 rev1=$(cd .subrepo && git rev-parse HEAD) &&
267 printf "rev1: %s\n" "$rev1" &&
271 test_expect_success 'status should initially be "missing"' '
272 git submodule status >lines &&
276 test_expect_success 'init should register submodule url in .git/config' '
277 echo git://example.com/init.git >expect &&
279 git submodule init &&
280 git config submodule.example.url >url &&
281 git config submodule.example.url ./.subrepo &&
286 test_failure_with_unknown_submodule () {
287 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
288 grep "^error: .*no-such-submodule" output.err
291 test_expect_success 'init should fail with unknown submodule' '
292 test_failure_with_unknown_submodule init
295 test_expect_success 'update should fail with unknown submodule' '
296 test_failure_with_unknown_submodule update
299 test_expect_success 'status should fail with unknown submodule' '
300 test_failure_with_unknown_submodule status
303 test_expect_success 'sync should fail with unknown submodule' '
304 test_failure_with_unknown_submodule sync
307 test_expect_success 'update should fail when path is used by a file' '
308 echo hello >expect &&
310 echo "hello" >init &&
311 test_must_fail git submodule update &&
316 test_expect_success 'update should fail when path is used by a nonempty directory' '
317 echo hello >expect &&
321 echo "hello" >init/a &&
323 test_must_fail git submodule update &&
325 test_cmp expect init/a
328 test_expect_success 'update should work when path is an empty dir' '
331 echo "$rev1" >expect &&
334 git submodule update -q >update.out &&
335 test ! -s update.out &&
338 test_cmp expect head-sha1
341 test_expect_success 'status should be "up-to-date" after update' '
342 git submodule status >list &&
346 test_expect_success 'status should be "modified" after submodule commit' '
351 git commit -m "submodule commit 2"
354 rev2=$(cd init && git rev-parse HEAD) &&
356 git submodule status >list &&
361 test_expect_success 'the --cached sha1 should be rev1' '
362 git submodule --cached status >list &&
366 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
368 grep "^+Subproject commit $rev2" diff
371 test_expect_success 'update should checkout rev1' '
373 echo "$rev1" >expect &&
375 git submodule update init &&
378 test_cmp expect head-sha1
381 test_expect_success 'status should be "up-to-date" after update' '
382 git submodule status >list &&
386 test_expect_success 'checkout superproject with subproject already present' '
387 git checkout initial &&
391 test_expect_success 'apply submodule diff' '
399 git commit -m "change subproject"
401 git update-index --add init &&
402 git commit -m "change init" &&
403 git format-patch -1 --stdout >P.diff &&
404 git checkout second &&
405 git apply --index P.diff &&
407 git diff --cached master >staged &&
408 test_cmp empty staged
411 test_expect_success 'update --init' '
413 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
414 git config --remove-section submodule.example &&
415 test_must_fail git config submodule.example.url &&
417 git submodule update init > update.out &&
419 test_i18ngrep "not initialized" update.out &&
420 test_must_fail git rev-parse --resolve-git-dir init/.git &&
422 git submodule update --init init &&
423 git rev-parse --resolve-git-dir init/.git
426 test_expect_success 'do not add files from a submodule' '
429 test_must_fail git add init/a
433 test_expect_success 'gracefully add submodule with a trailing slash' '
436 git commit -m "commit subproject" init &&
440 git diff --exit-code --cached init &&
442 git commit -m update a >/dev/null &&
443 git rev-parse HEAD) &&
445 test_must_fail git diff --exit-code --cached init &&
446 test $commit = $(git ls-files --stage |
447 sed -n "s/^160000 \([^ ]*\).*/\1/p")
451 test_expect_success 'ls-files gracefully handles trailing slash' '
453 test "init" = "$(git ls-files init/)"
457 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
461 git checkout initial &&
466 test_expect_success 'submodule <invalid-subcommand> fails' '
467 test_must_fail git submodule no-such-subcommand
470 test_expect_success 'add submodules without specifying an explicit path' '
477 git commit -m "repo commit 1"
479 git clone --bare repo/ bare.git &&
482 git submodule add "$submodurl/repo" &&
483 git config -f .gitmodules submodule.repo.path repo &&
484 git submodule add "$submodurl/bare.git" &&
485 git config -f .gitmodules submodule.bare.path bare
489 test_expect_success 'add should fail when path is used by a file' '
493 test_must_fail git submodule add "$submodurl/repo" file
497 test_expect_success 'add should fail when path is used by an existing directory' '
501 test_must_fail git submodule add "$submodurl/repo" empty-dir
505 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
508 git submodule add ../repo relative &&
509 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
510 git submodule sync relative &&
511 test "$(git config submodule.relative.url)" = "$submodurl/repo"
515 test_expect_success 'set up for relative path tests' '
527 git config -f .gitmodules submodule.sub.path sub &&
528 git config -f .gitmodules submodule.sub.url ../subrepo &&
529 cp .git/config pristine-.git-config &&
530 cp .gitmodules pristine-.gitmodules
534 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
537 cp pristine-.git-config .git/config &&
538 cp pristine-.gitmodules .gitmodules &&
539 git config remote.origin.url ssh://hostname/repo &&
540 git submodule init &&
541 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
545 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
548 cp pristine-.git-config .git/config &&
549 cp pristine-.gitmodules .gitmodules &&
550 git config remote.origin.url ssh://hostname:22/repo &&
551 git submodule init &&
552 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
556 # About the choice of the path in the next test:
557 # - double-slash side-steps path mangling issues on Windows
558 # - it is still an absolute local path
559 # - there cannot be a server with a blank in its name just in case the
560 # path is used erroneously to access a //server/share style path
561 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
564 cp pristine-.git-config .git/config &&
565 cp pristine-.gitmodules .gitmodules &&
566 git config remote.origin.url "//somewhere else/repo" &&
567 git submodule init &&
568 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
572 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
575 cp pristine-.git-config .git/config &&
576 cp pristine-.gitmodules .gitmodules &&
577 git config remote.origin.url file:///tmp/repo &&
578 git submodule init &&
579 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
583 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
586 cp pristine-.git-config .git/config &&
587 cp pristine-.gitmodules .gitmodules &&
588 git config remote.origin.url helper:://hostname/repo &&
589 git submodule init &&
590 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
594 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
597 cp pristine-.git-config .git/config &&
598 git config remote.origin.url user@host:repo &&
599 git submodule init &&
600 test "$(git config submodule.sub.url)" = user@host:subrepo
604 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
607 cp pristine-.git-config .git/config &&
608 cp pristine-.gitmodules .gitmodules &&
609 git config remote.origin.url user@host:path/to/repo &&
610 git submodule init &&
611 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
615 test_expect_success '../subrepo works with relative local path - foo' '
618 cp pristine-.git-config .git/config &&
619 cp pristine-.gitmodules .gitmodules &&
620 git config remote.origin.url foo &&
621 # actual: fails with an error
622 git submodule init &&
623 test "$(git config submodule.sub.url)" = subrepo
627 test_expect_success '../subrepo works with relative local path - foo/bar' '
630 cp pristine-.git-config .git/config &&
631 cp pristine-.gitmodules .gitmodules &&
632 git config remote.origin.url foo/bar &&
633 git submodule init &&
634 test "$(git config submodule.sub.url)" = foo/subrepo
638 test_expect_success '../subrepo works with relative local path - ./foo' '
641 cp pristine-.git-config .git/config &&
642 cp pristine-.gitmodules .gitmodules &&
643 git config remote.origin.url ./foo &&
644 git submodule init &&
645 test "$(git config submodule.sub.url)" = subrepo
649 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
652 cp pristine-.git-config .git/config &&
653 cp pristine-.gitmodules .gitmodules &&
654 git config remote.origin.url ./foo/bar &&
655 git submodule init &&
656 test "$(git config submodule.sub.url)" = foo/subrepo
660 test_expect_success '../subrepo works with relative local path - ../foo' '
663 cp pristine-.git-config .git/config &&
664 cp pristine-.gitmodules .gitmodules &&
665 git config remote.origin.url ../foo &&
666 git submodule init &&
667 test "$(git config submodule.sub.url)" = ../subrepo
671 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
674 cp pristine-.git-config .git/config &&
675 cp pristine-.gitmodules .gitmodules &&
676 git config remote.origin.url ../foo/bar &&
677 git submodule init &&
678 test "$(git config submodule.sub.url)" = ../foo/subrepo
682 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
685 cp pristine-.git-config .git/config &&
686 cp pristine-.gitmodules .gitmodules &&
688 (cd a/b/c; git init) &&
689 git config remote.origin.url ../foo/bar.git &&
690 git submodule add ../bar/a/b/c ./a/b/c &&
691 git submodule init &&
692 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
696 test_expect_success 'moving the superproject does not break submodules' '
699 git submodule status >expect
701 mv addtest addtest2 &&
704 git submodule status >actual &&
705 test_cmp expect actual