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 "$submodurl" submod &&
84 rm -f heads head untracked &&
85 inspect addtest/submod ../.. &&
86 test_cmp expect heads &&
87 test_cmp expect head &&
88 test_cmp empty untracked
91 test_expect_success
'submodule add to .gitignored path fails' '
94 cat <<-\EOF >expect &&
95 The following path is ignored by one of your .gitignore files:
97 Use -f if you really want to add it.
99 # Does not use test_commit due to the ignore
100 echo "*" > .gitignore &&
101 git add --force .gitignore &&
102 git commit -m"Ignore everything" &&
103 ! git submodule add "$submodurl" submod >actual 2>&1 &&
104 test_i18ncmp expect actual
108 test_expect_success
'submodule add to .gitignored path with --force' '
111 git submodule add --force "$submodurl" submod
115 test_expect_success
'submodule add --branch' '
116 echo "refs/heads/initial" >expect-head &&
117 cat <<-\EOF >expect-heads &&
125 git submodule add -b initial "$submodurl" submod-branch &&
129 rm -f heads head untracked &&
130 inspect addtest/submod-branch ../.. &&
131 test_cmp expect-heads heads &&
132 test_cmp expect-head head &&
133 test_cmp empty untracked
136 test_expect_success
'submodule add with ./ in path' '
137 echo "refs/heads/master" >expect &&
142 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
146 rm -f heads head untracked &&
147 inspect addtest/dotsubmod/frotz ../../.. &&
148 test_cmp expect heads &&
149 test_cmp expect head &&
150 test_cmp empty untracked
153 test_expect_success
'submodule add with // in path' '
154 echo "refs/heads/master" >expect &&
159 git submodule add "$submodurl" slashslashsubmod///frotz// &&
163 rm -f heads head untracked &&
164 inspect addtest/slashslashsubmod/frotz ../../.. &&
165 test_cmp expect heads &&
166 test_cmp expect head &&
167 test_cmp empty untracked
170 test_expect_success
'submodule add with /.. in path' '
171 echo "refs/heads/master" >expect &&
176 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
180 rm -f heads head untracked &&
181 inspect addtest/realsubmod ../.. &&
182 test_cmp expect heads &&
183 test_cmp expect head &&
184 test_cmp empty untracked
187 test_expect_success
'submodule add with ./, /.. and // in path' '
188 echo "refs/heads/master" >expect &&
193 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
197 rm -f heads head untracked &&
198 inspect addtest/realsubmod2 ../.. &&
199 test_cmp expect heads &&
200 test_cmp expect head &&
201 test_cmp empty untracked
204 test_expect_success
'setup - add an example entry to .gitmodules' '
205 GIT_CONFIG=.gitmodules \
206 git config submodule.example.url git://example.com/init.git
209 test_expect_success
'status should fail for unmapped paths' '
210 test_must_fail git submodule status
213 test_expect_success
'setup - map path in .gitmodules' '
214 cat <<\EOF >expect &&
215 [submodule "example"]
216 url = git://example.com/init.git
220 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
222 test_cmp expect .gitmodules
225 test_expect_success
'status should only print one line' '
226 git submodule status >lines &&
227 test $(wc -l <lines) = 1
230 test_expect_success
'setup - fetch commit name from submodule' '
231 rev1=$(cd .subrepo && git rev-parse HEAD) &&
232 printf "rev1: %s\n" "$rev1" &&
236 test_expect_success
'status should initially be "missing"' '
237 git submodule status >lines &&
241 test_expect_success
'init should register submodule url in .git/config' '
242 echo git://example.com/init.git >expect &&
244 git submodule init &&
245 git config submodule.example.url >url &&
246 git config submodule.example.url ./.subrepo &&
251 test_expect_success
'update should fail when path is used by a file' '
252 echo hello >expect &&
254 echo "hello" >init &&
255 test_must_fail git submodule update &&
260 test_expect_success
'update should fail when path is used by a nonempty directory' '
261 echo hello >expect &&
265 echo "hello" >init/a &&
267 test_must_fail git submodule update &&
269 test_cmp expect init/a
272 test_expect_success
'update should work when path is an empty dir' '
275 echo "$rev1" >expect &&
278 git submodule update &&
281 test_cmp expect head-sha1
284 test_expect_success
'status should be "up-to-date" after update' '
285 git submodule status >list &&
289 test_expect_success
'status should be "modified" after submodule commit' '
294 git commit -m "submodule commit 2"
297 rev2=$(cd init && git rev-parse HEAD) &&
299 git submodule status >list &&
304 test_expect_success
'the --cached sha1 should be rev1' '
305 git submodule --cached status >list &&
309 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
311 grep "^+Subproject commit $rev2" diff
314 test_expect_success
'update should checkout rev1' '
316 echo "$rev1" >expect &&
318 git submodule update init &&
321 test_cmp expect head-sha1
324 test_expect_success
'status should be "up-to-date" after update' '
325 git submodule status >list &&
329 test_expect_success
'checkout superproject with subproject already present' '
330 git checkout initial &&
334 test_expect_success
'apply submodule diff' '
342 git commit -m "change subproject"
344 git update-index --add init &&
345 git commit -m "change init" &&
346 git format-patch -1 --stdout >P.diff &&
347 git checkout second &&
348 git apply --index P.diff &&
350 git diff --cached master >staged &&
351 test_cmp empty staged
354 test_expect_success
'update --init' '
356 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
357 git config --remove-section submodule.example &&
358 test_must_fail git config submodule.example.url &&
360 git submodule update init > update.out &&
362 test_i18ngrep "not initialized" update.out &&
363 test_must_fail git rev-parse --resolve-git-dir init/.git &&
365 git submodule update --init init &&
366 git rev-parse --resolve-git-dir init/.git
369 test_expect_success
'do not add files from a submodule' '
372 test_must_fail git add init/a
376 test_expect_success
'gracefully add submodule with a trailing slash' '
379 git commit -m "commit subproject" init &&
383 git diff --exit-code --cached init &&
385 git commit -m update a >/dev/null &&
386 git rev-parse HEAD) &&
388 test_must_fail git diff --exit-code --cached init &&
389 test $commit = $(git ls-files --stage |
390 sed -n "s/^160000 \([^ ]*\).*/\1/p")
394 test_expect_success
'ls-files gracefully handles trailing slash' '
396 test "init" = "$(git ls-files init/)"
400 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
404 git checkout initial &&
409 test_expect_success
'submodule <invalid-path> warns' '
411 git submodule no-such-submodule 2> output.err &&
412 grep "^error: .*no-such-submodule" output.err
416 test_expect_success
'add submodules without specifying an explicit path' '
423 git commit -m "repo commit 1"
425 git clone --bare repo/ bare.git &&
428 git submodule add "$submodurl/repo" &&
429 git config -f .gitmodules submodule.repo.path repo &&
430 git submodule add "$submodurl/bare.git" &&
431 git config -f .gitmodules submodule.bare.path bare
435 test_expect_success
'add should fail when path is used by a file' '
439 test_must_fail git submodule add "$submodurl/repo" file
443 test_expect_success
'add should fail when path is used by an existing directory' '
447 test_must_fail git submodule add "$submodurl/repo" empty-dir
451 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
454 git submodule add ../repo relative &&
455 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
456 git submodule sync relative &&
457 test "$(git config submodule.relative.url)" = "$submodurl/repo"
461 test_expect_success
'set up for relative path tests' '
473 git config -f .gitmodules submodule.sub.path sub &&
474 git config -f .gitmodules submodule.sub.url ../subrepo &&
475 cp .git/config pristine-.git-config
479 test_expect_success
'relative path works with URL' '
482 cp pristine-.git-config .git/config &&
483 git config remote.origin.url ssh://hostname/repo &&
484 git submodule init &&
485 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
489 test_expect_success
'relative path works with user@host:path' '
492 cp pristine-.git-config .git/config &&
493 git config remote.origin.url user@host:repo &&
494 git submodule init &&
495 test "$(git config submodule.sub.url)" = user@host:subrepo