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 &&
139 rm -f heads head untracked &&
140 inspect addtest/submod-branch ../.. &&
141 test_cmp expect-heads heads &&
142 test_cmp expect-head head &&
143 test_cmp empty untracked
146 test_expect_success 'submodule add with ./ in path' '
147 echo "refs/heads/master" >expect &&
152 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
156 rm -f heads head untracked &&
157 inspect addtest/dotsubmod/frotz ../../.. &&
158 test_cmp expect heads &&
159 test_cmp expect head &&
160 test_cmp empty untracked
163 test_expect_success 'submodule add with // in path' '
164 echo "refs/heads/master" >expect &&
169 git submodule add "$submodurl" slashslashsubmod///frotz// &&
173 rm -f heads head untracked &&
174 inspect addtest/slashslashsubmod/frotz ../../.. &&
175 test_cmp expect heads &&
176 test_cmp expect head &&
177 test_cmp empty untracked
180 test_expect_success 'submodule add with /.. in path' '
181 echo "refs/heads/master" >expect &&
186 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
190 rm -f heads head untracked &&
191 inspect addtest/realsubmod ../.. &&
192 test_cmp expect heads &&
193 test_cmp expect head &&
194 test_cmp empty untracked
197 test_expect_success 'submodule add with ./, /.. and // in path' '
198 echo "refs/heads/master" >expect &&
203 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
207 rm -f heads head untracked &&
208 inspect addtest/realsubmod2 ../.. &&
209 test_cmp expect heads &&
210 test_cmp expect head &&
211 test_cmp empty untracked
214 test_expect_success 'setup - add an example entry to .gitmodules' '
215 GIT_CONFIG=.gitmodules \
216 git config submodule.example.url git://example.com/init.git
219 test_expect_success 'status should fail for unmapped paths' '
220 test_must_fail git submodule status
223 test_expect_success 'setup - map path in .gitmodules' '
224 cat <<\EOF >expect &&
225 [submodule "example"]
226 url = git://example.com/init.git
230 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
232 test_cmp expect .gitmodules
235 test_expect_success 'status should only print one line' '
236 git submodule status >lines &&
237 test_line_count = 1 lines
240 test_expect_success 'setup - fetch commit name from submodule' '
241 rev1=$(cd .subrepo && git rev-parse HEAD) &&
242 printf "rev1: %s\n" "$rev1" &&
246 test_expect_success 'status should initially be "missing"' '
247 git submodule status >lines &&
251 test_expect_success 'init should register submodule url in .git/config' '
252 echo git://example.com/init.git >expect &&
254 git submodule init &&
255 git config submodule.example.url >url &&
256 git config submodule.example.url ./.subrepo &&
261 test_failure_with_unknown_submodule () {
262 test_must_fail git submodule $1 no-such-submodule 2>output.err &&
263 grep "^error: .*no-such-submodule" output.err
266 test_expect_success 'init should fail with unknown submodule' '
267 test_failure_with_unknown_submodule init
270 test_expect_success 'update should fail with unknown submodule' '
271 test_failure_with_unknown_submodule update
274 test_expect_success 'status should fail with unknown submodule' '
275 test_failure_with_unknown_submodule status
278 test_expect_success 'sync should fail with unknown submodule' '
279 test_failure_with_unknown_submodule sync
282 test_expect_success 'update should fail when path is used by a file' '
283 echo hello >expect &&
285 echo "hello" >init &&
286 test_must_fail git submodule update &&
291 test_expect_success 'update should fail when path is used by a nonempty directory' '
292 echo hello >expect &&
296 echo "hello" >init/a &&
298 test_must_fail git submodule update &&
300 test_cmp expect init/a
303 test_expect_success 'update should work when path is an empty dir' '
306 echo "$rev1" >expect &&
309 git submodule update -q >update.out &&
310 test ! -s update.out &&
313 test_cmp expect head-sha1
316 test_expect_success 'status should be "up-to-date" after update' '
317 git submodule status >list &&
321 test_expect_success 'status should be "modified" after submodule commit' '
326 git commit -m "submodule commit 2"
329 rev2=$(cd init && git rev-parse HEAD) &&
331 git submodule status >list &&
336 test_expect_success 'the --cached sha1 should be rev1' '
337 git submodule --cached status >list &&
341 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
343 grep "^+Subproject commit $rev2" diff
346 test_expect_success 'update should checkout rev1' '
348 echo "$rev1" >expect &&
350 git submodule update init &&
353 test_cmp expect head-sha1
356 test_expect_success 'status should be "up-to-date" after update' '
357 git submodule status >list &&
361 test_expect_success 'checkout superproject with subproject already present' '
362 git checkout initial &&
366 test_expect_success 'apply submodule diff' '
374 git commit -m "change subproject"
376 git update-index --add init &&
377 git commit -m "change init" &&
378 git format-patch -1 --stdout >P.diff &&
379 git checkout second &&
380 git apply --index P.diff &&
382 git diff --cached master >staged &&
383 test_cmp empty staged
386 test_expect_success 'update --init' '
388 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
389 git config --remove-section submodule.example &&
390 test_must_fail git config submodule.example.url &&
392 git submodule update init > update.out &&
394 test_i18ngrep "not initialized" update.out &&
395 test_must_fail git rev-parse --resolve-git-dir init/.git &&
397 git submodule update --init init &&
398 git rev-parse --resolve-git-dir init/.git
401 test_expect_success 'do not add files from a submodule' '
404 test_must_fail git add init/a
408 test_expect_success 'gracefully add submodule with a trailing slash' '
411 git commit -m "commit subproject" init &&
415 git diff --exit-code --cached init &&
417 git commit -m update a >/dev/null &&
418 git rev-parse HEAD) &&
420 test_must_fail git diff --exit-code --cached init &&
421 test $commit = $(git ls-files --stage |
422 sed -n "s/^160000 \([^ ]*\).*/\1/p")
426 test_expect_success 'ls-files gracefully handles trailing slash' '
428 test "init" = "$(git ls-files init/)"
432 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
436 git checkout initial &&
441 test_expect_success 'submodule <invalid-subcommand> fails' '
442 test_must_fail git submodule no-such-subcommand
445 test_expect_success 'add submodules without specifying an explicit path' '
452 git commit -m "repo commit 1"
454 git clone --bare repo/ bare.git &&
457 git submodule add "$submodurl/repo" &&
458 git config -f .gitmodules submodule.repo.path repo &&
459 git submodule add "$submodurl/bare.git" &&
460 git config -f .gitmodules submodule.bare.path bare
464 test_expect_success 'add should fail when path is used by a file' '
468 test_must_fail git submodule add "$submodurl/repo" file
472 test_expect_success 'add should fail when path is used by an existing directory' '
476 test_must_fail git submodule add "$submodurl/repo" empty-dir
480 test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
483 git submodule add ../repo relative &&
484 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
485 git submodule sync relative &&
486 test "$(git config submodule.relative.url)" = "$submodurl/repo"
490 test_expect_success 'set up for relative path tests' '
502 git config -f .gitmodules submodule.sub.path sub &&
503 git config -f .gitmodules submodule.sub.url ../subrepo &&
504 cp .git/config pristine-.git-config &&
505 cp .gitmodules pristine-.gitmodules
509 test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
512 cp pristine-.git-config .git/config &&
513 cp pristine-.gitmodules .gitmodules &&
514 git config remote.origin.url ssh://hostname/repo &&
515 git submodule init &&
516 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
520 test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
523 cp pristine-.git-config .git/config &&
524 cp pristine-.gitmodules .gitmodules &&
525 git config remote.origin.url ssh://hostname:22/repo &&
526 git submodule init &&
527 test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
531 # About the choice of the path in the next test:
532 # - double-slash side-steps path mangling issues on Windows
533 # - it is still an absolute local path
534 # - there cannot be a server with a blank in its name just in case the
535 # path is used erroneously to access a //server/share style path
536 test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
539 cp pristine-.git-config .git/config &&
540 cp pristine-.gitmodules .gitmodules &&
541 git config remote.origin.url "//somewhere else/repo" &&
542 git submodule init &&
543 test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
547 test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
550 cp pristine-.git-config .git/config &&
551 cp pristine-.gitmodules .gitmodules &&
552 git config remote.origin.url file:///tmp/repo &&
553 git submodule init &&
554 test "$(git config submodule.sub.url)" = file:///tmp/subrepo
558 test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
561 cp pristine-.git-config .git/config &&
562 cp pristine-.gitmodules .gitmodules &&
563 git config remote.origin.url helper:://hostname/repo &&
564 git submodule init &&
565 test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
569 test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
572 cp pristine-.git-config .git/config &&
573 git config remote.origin.url user@host:repo &&
574 git submodule init &&
575 test "$(git config submodule.sub.url)" = user@host:subrepo
579 test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
582 cp pristine-.git-config .git/config &&
583 cp pristine-.gitmodules .gitmodules &&
584 git config remote.origin.url user@host:path/to/repo &&
585 git submodule init &&
586 test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
590 test_expect_success '../subrepo works with relative local path - foo' '
593 cp pristine-.git-config .git/config &&
594 cp pristine-.gitmodules .gitmodules &&
595 git config remote.origin.url foo &&
596 # actual: fails with an error
597 git submodule init &&
598 test "$(git config submodule.sub.url)" = subrepo
602 test_expect_success '../subrepo works with relative local path - foo/bar' '
605 cp pristine-.git-config .git/config &&
606 cp pristine-.gitmodules .gitmodules &&
607 git config remote.origin.url foo/bar &&
608 git submodule init &&
609 test "$(git config submodule.sub.url)" = foo/subrepo
613 test_expect_success '../subrepo works with relative local path - ./foo' '
616 cp pristine-.git-config .git/config &&
617 cp pristine-.gitmodules .gitmodules &&
618 git config remote.origin.url ./foo &&
619 git submodule init &&
620 test "$(git config submodule.sub.url)" = subrepo
624 test_expect_success '../subrepo works with relative local path - ./foo/bar' '
627 cp pristine-.git-config .git/config &&
628 cp pristine-.gitmodules .gitmodules &&
629 git config remote.origin.url ./foo/bar &&
630 git submodule init &&
631 test "$(git config submodule.sub.url)" = foo/subrepo
635 test_expect_success '../subrepo works with relative local path - ../foo' '
638 cp pristine-.git-config .git/config &&
639 cp pristine-.gitmodules .gitmodules &&
640 git config remote.origin.url ../foo &&
641 git submodule init &&
642 test "$(git config submodule.sub.url)" = ../subrepo
646 test_expect_success '../subrepo works with relative local path - ../foo/bar' '
649 cp pristine-.git-config .git/config &&
650 cp pristine-.gitmodules .gitmodules &&
651 git config remote.origin.url ../foo/bar &&
652 git submodule init &&
653 test "$(git config submodule.sub.url)" = ../foo/subrepo
657 test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
660 cp pristine-.git-config .git/config &&
661 cp pristine-.gitmodules .gitmodules &&
663 (cd a/b/c; git init) &&
664 git config remote.origin.url ../foo/bar.git &&
665 git submodule add ../bar/a/b/c ./a/b/c &&
666 git submodule init &&
667 test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
671 test_expect_success 'moving the superproject does not break submodules' '
674 git submodule status >expect
676 mv addtest addtest2 &&
679 git submodule status >actual &&
680 test_cmp expect actual