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 &&
87 rm -f heads head untracked &&
88 inspect addtest/submod ../.. &&
89 test_cmp expect heads &&
90 test_cmp expect head &&
91 test_cmp empty untracked
94 test_expect_success
'submodule add to .gitignored path fails' '
97 cat <<-\EOF >expect &&
98 The following path is ignored by one of your .gitignore files:
100 Use -f if you really want to add it.
102 # Does not use test_commit due to the ignore
103 echo "*" > .gitignore &&
104 git add --force .gitignore &&
105 git commit -m"Ignore everything" &&
106 ! git submodule add "$submodurl" submod >actual 2>&1 &&
107 test_i18ncmp expect actual
111 test_expect_success
'submodule add to .gitignored path with --force' '
114 git submodule add --force "$submodurl" submod
118 test_expect_success
'submodule add --branch' '
119 echo "refs/heads/initial" >expect-head &&
120 cat <<-\EOF >expect-heads &&
128 git submodule add -b initial "$submodurl" submod-branch &&
132 rm -f heads head untracked &&
133 inspect addtest/submod-branch ../.. &&
134 test_cmp expect-heads heads &&
135 test_cmp expect-head head &&
136 test_cmp empty untracked
139 test_expect_success
'submodule add with ./ in path' '
140 echo "refs/heads/master" >expect &&
145 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
149 rm -f heads head untracked &&
150 inspect addtest/dotsubmod/frotz ../../.. &&
151 test_cmp expect heads &&
152 test_cmp expect head &&
153 test_cmp empty untracked
156 test_expect_success
'submodule add with // in path' '
157 echo "refs/heads/master" >expect &&
162 git submodule add "$submodurl" slashslashsubmod///frotz// &&
166 rm -f heads head untracked &&
167 inspect addtest/slashslashsubmod/frotz ../../.. &&
168 test_cmp expect heads &&
169 test_cmp expect head &&
170 test_cmp empty untracked
173 test_expect_success
'submodule add with /.. in path' '
174 echo "refs/heads/master" >expect &&
179 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
183 rm -f heads head untracked &&
184 inspect addtest/realsubmod ../.. &&
185 test_cmp expect heads &&
186 test_cmp expect head &&
187 test_cmp empty untracked
190 test_expect_success
'submodule add with ./, /.. and // in path' '
191 echo "refs/heads/master" >expect &&
196 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
200 rm -f heads head untracked &&
201 inspect addtest/realsubmod2 ../.. &&
202 test_cmp expect heads &&
203 test_cmp expect head &&
204 test_cmp empty untracked
207 test_expect_success
'setup - add an example entry to .gitmodules' '
208 GIT_CONFIG=.gitmodules \
209 git config submodule.example.url git://example.com/init.git
212 test_expect_success
'status should fail for unmapped paths' '
213 test_must_fail git submodule status
216 test_expect_success
'setup - map path in .gitmodules' '
217 cat <<\EOF >expect &&
218 [submodule "example"]
219 url = git://example.com/init.git
223 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
225 test_cmp expect .gitmodules
228 test_expect_success
'status should only print one line' '
229 git submodule status >lines &&
230 test $(wc -l <lines) = 1
233 test_expect_success
'setup - fetch commit name from submodule' '
234 rev1=$(cd .subrepo && git rev-parse HEAD) &&
235 printf "rev1: %s\n" "$rev1" &&
239 test_expect_success
'status should initially be "missing"' '
240 git submodule status >lines &&
244 test_expect_success
'init should register submodule url in .git/config' '
245 echo git://example.com/init.git >expect &&
247 git submodule init &&
248 git config submodule.example.url >url &&
249 git config submodule.example.url ./.subrepo &&
254 test_expect_success
'update should fail when path is used by a file' '
255 echo hello >expect &&
257 echo "hello" >init &&
258 test_must_fail git submodule update &&
263 test_expect_success
'update should fail when path is used by a nonempty directory' '
264 echo hello >expect &&
268 echo "hello" >init/a &&
270 test_must_fail git submodule update &&
272 test_cmp expect init/a
275 test_expect_success
'update should work when path is an empty dir' '
278 echo "$rev1" >expect &&
281 git submodule update -q >update.out &&
282 test ! -s update.out &&
285 test_cmp expect head-sha1
288 test_expect_success
'status should be "up-to-date" after update' '
289 git submodule status >list &&
293 test_expect_success
'status should be "modified" after submodule commit' '
298 git commit -m "submodule commit 2"
301 rev2=$(cd init && git rev-parse HEAD) &&
303 git submodule status >list &&
308 test_expect_success
'the --cached sha1 should be rev1' '
309 git submodule --cached status >list &&
313 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
315 grep "^+Subproject commit $rev2" diff
318 test_expect_success
'update should checkout rev1' '
320 echo "$rev1" >expect &&
322 git submodule update init &&
325 test_cmp expect head-sha1
328 test_expect_success
'status should be "up-to-date" after update' '
329 git submodule status >list &&
333 test_expect_success
'checkout superproject with subproject already present' '
334 git checkout initial &&
338 test_expect_success
'apply submodule diff' '
346 git commit -m "change subproject"
348 git update-index --add init &&
349 git commit -m "change init" &&
350 git format-patch -1 --stdout >P.diff &&
351 git checkout second &&
352 git apply --index P.diff &&
354 git diff --cached master >staged &&
355 test_cmp empty staged
358 test_expect_success
'update --init' '
360 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
361 git config --remove-section submodule.example &&
362 test_must_fail git config submodule.example.url &&
364 git submodule update init > update.out &&
366 test_i18ngrep "not initialized" update.out &&
367 test_must_fail git rev-parse --resolve-git-dir init/.git &&
369 git submodule update --init init &&
370 git rev-parse --resolve-git-dir init/.git
373 test_expect_success
'do not add files from a submodule' '
376 test_must_fail git add init/a
380 test_expect_success
'gracefully add submodule with a trailing slash' '
383 git commit -m "commit subproject" init &&
387 git diff --exit-code --cached init &&
389 git commit -m update a >/dev/null &&
390 git rev-parse HEAD) &&
392 test_must_fail git diff --exit-code --cached init &&
393 test $commit = $(git ls-files --stage |
394 sed -n "s/^160000 \([^ ]*\).*/\1/p")
398 test_expect_success
'ls-files gracefully handles trailing slash' '
400 test "init" = "$(git ls-files init/)"
404 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
408 git checkout initial &&
413 test_expect_success
'submodule <invalid-path> warns' '
415 git submodule no-such-submodule 2> output.err &&
416 grep "^error: .*no-such-submodule" output.err
420 test_expect_success
'add submodules without specifying an explicit path' '
427 git commit -m "repo commit 1"
429 git clone --bare repo/ bare.git &&
432 git submodule add "$submodurl/repo" &&
433 git config -f .gitmodules submodule.repo.path repo &&
434 git submodule add "$submodurl/bare.git" &&
435 git config -f .gitmodules submodule.bare.path bare
439 test_expect_success
'add should fail when path is used by a file' '
443 test_must_fail git submodule add "$submodurl/repo" file
447 test_expect_success
'add should fail when path is used by an existing directory' '
451 test_must_fail git submodule add "$submodurl/repo" empty-dir
455 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
458 git submodule add ../repo relative &&
459 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
460 git submodule sync relative &&
461 test "$(git config submodule.relative.url)" = "$submodurl/repo"
465 test_expect_success
'set up for relative path tests' '
477 git config -f .gitmodules submodule.sub.path sub &&
478 git config -f .gitmodules submodule.sub.url ../subrepo &&
479 cp .git/config pristine-.git-config
483 test_expect_success
'relative path works with URL' '
486 cp pristine-.git-config .git/config &&
487 git config remote.origin.url ssh://hostname/repo &&
488 git submodule init &&
489 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
493 test_expect_success
'relative path works with user@host:path' '
496 cp pristine-.git-config .git/config &&
497 git config remote.origin.url user@host:repo &&
498 git submodule init &&
499 test "$(git config submodule.sub.url)" = user@host:subrepo