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 &&
85 rm -f heads head untracked &&
86 inspect addtest/submod ../.. &&
87 test_cmp expect heads &&
88 test_cmp expect head &&
89 test_cmp empty untracked
92 test_expect_success
'submodule add to .gitignored path fails' '
95 cat <<-\EOF >expect &&
96 The following path is ignored by one of your .gitignore files:
98 Use -f if you really want to add it.
100 # Does not use test_commit due to the ignore
101 echo "*" > .gitignore &&
102 git add --force .gitignore &&
103 git commit -m"Ignore everything" &&
104 ! git submodule add "$submodurl" submod >actual 2>&1 &&
105 test_i18ncmp expect actual
109 test_expect_success
'submodule add to .gitignored path with --force' '
112 git submodule add --force "$submodurl" submod
116 test_expect_success
'submodule add --branch' '
117 echo "refs/heads/initial" >expect-head &&
118 cat <<-\EOF >expect-heads &&
126 git submodule add -b initial "$submodurl" submod-branch &&
130 rm -f heads head untracked &&
131 inspect addtest/submod-branch ../.. &&
132 test_cmp expect-heads heads &&
133 test_cmp expect-head head &&
134 test_cmp empty untracked
137 test_expect_success
'submodule add with ./ in path' '
138 echo "refs/heads/master" >expect &&
143 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
147 rm -f heads head untracked &&
148 inspect addtest/dotsubmod/frotz ../../.. &&
149 test_cmp expect heads &&
150 test_cmp expect head &&
151 test_cmp empty untracked
154 test_expect_success
'submodule add with // in path' '
155 echo "refs/heads/master" >expect &&
160 git submodule add "$submodurl" slashslashsubmod///frotz// &&
164 rm -f heads head untracked &&
165 inspect addtest/slashslashsubmod/frotz ../../.. &&
166 test_cmp expect heads &&
167 test_cmp expect head &&
168 test_cmp empty untracked
171 test_expect_success
'submodule add with /.. in path' '
172 echo "refs/heads/master" >expect &&
177 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
181 rm -f heads head untracked &&
182 inspect addtest/realsubmod ../.. &&
183 test_cmp expect heads &&
184 test_cmp expect head &&
185 test_cmp empty untracked
188 test_expect_success
'submodule add with ./, /.. and // in path' '
189 echo "refs/heads/master" >expect &&
194 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
198 rm -f heads head untracked &&
199 inspect addtest/realsubmod2 ../.. &&
200 test_cmp expect heads &&
201 test_cmp expect head &&
202 test_cmp empty untracked
205 test_expect_success
'setup - add an example entry to .gitmodules' '
206 GIT_CONFIG=.gitmodules \
207 git config submodule.example.url git://example.com/init.git
210 test_expect_success
'status should fail for unmapped paths' '
211 test_must_fail git submodule status
214 test_expect_success
'setup - map path in .gitmodules' '
215 cat <<\EOF >expect &&
216 [submodule "example"]
217 url = git://example.com/init.git
221 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
223 test_cmp expect .gitmodules
226 test_expect_success
'status should only print one line' '
227 git submodule status >lines &&
228 test $(wc -l <lines) = 1
231 test_expect_success
'setup - fetch commit name from submodule' '
232 rev1=$(cd .subrepo && git rev-parse HEAD) &&
233 printf "rev1: %s\n" "$rev1" &&
237 test_expect_success
'status should initially be "missing"' '
238 git submodule status >lines &&
242 test_expect_success
'init should register submodule url in .git/config' '
243 echo git://example.com/init.git >expect &&
245 git submodule init &&
246 git config submodule.example.url >url &&
247 git config submodule.example.url ./.subrepo &&
252 test_expect_success
'update should fail when path is used by a file' '
253 echo hello >expect &&
255 echo "hello" >init &&
256 test_must_fail git submodule update &&
261 test_expect_success
'update should fail when path is used by a nonempty directory' '
262 echo hello >expect &&
266 echo "hello" >init/a &&
268 test_must_fail git submodule update &&
270 test_cmp expect init/a
273 test_expect_success
'update should work when path is an empty dir' '
276 echo "$rev1" >expect &&
279 git submodule update -q >update.out &&
280 test ! -s update.out &&
283 test_cmp expect head-sha1
286 test_expect_success
'status should be "up-to-date" after update' '
287 git submodule status >list &&
291 test_expect_success
'status should be "modified" after submodule commit' '
296 git commit -m "submodule commit 2"
299 rev2=$(cd init && git rev-parse HEAD) &&
301 git submodule status >list &&
306 test_expect_success
'the --cached sha1 should be rev1' '
307 git submodule --cached status >list &&
311 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
313 grep "^+Subproject commit $rev2" diff
316 test_expect_success
'update should checkout rev1' '
318 echo "$rev1" >expect &&
320 git submodule update init &&
323 test_cmp expect head-sha1
326 test_expect_success
'status should be "up-to-date" after update' '
327 git submodule status >list &&
331 test_expect_success
'checkout superproject with subproject already present' '
332 git checkout initial &&
336 test_expect_success
'apply submodule diff' '
344 git commit -m "change subproject"
346 git update-index --add init &&
347 git commit -m "change init" &&
348 git format-patch -1 --stdout >P.diff &&
349 git checkout second &&
350 git apply --index P.diff &&
352 git diff --cached master >staged &&
353 test_cmp empty staged
356 test_expect_success
'update --init' '
358 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
359 git config --remove-section submodule.example &&
360 test_must_fail git config submodule.example.url &&
362 git submodule update init > update.out &&
364 test_i18ngrep "not initialized" update.out &&
365 test_must_fail git rev-parse --resolve-git-dir init/.git &&
367 git submodule update --init init &&
368 git rev-parse --resolve-git-dir init/.git
371 test_expect_success
'do not add files from a submodule' '
374 test_must_fail git add init/a
378 test_expect_success
'gracefully add submodule with a trailing slash' '
381 git commit -m "commit subproject" init &&
385 git diff --exit-code --cached init &&
387 git commit -m update a >/dev/null &&
388 git rev-parse HEAD) &&
390 test_must_fail git diff --exit-code --cached init &&
391 test $commit = $(git ls-files --stage |
392 sed -n "s/^160000 \([^ ]*\).*/\1/p")
396 test_expect_success
'ls-files gracefully handles trailing slash' '
398 test "init" = "$(git ls-files init/)"
402 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
406 git checkout initial &&
411 test_expect_success
'submodule <invalid-path> warns' '
413 git submodule no-such-submodule 2> output.err &&
414 grep "^error: .*no-such-submodule" output.err
418 test_expect_success
'add submodules without specifying an explicit path' '
425 git commit -m "repo commit 1"
427 git clone --bare repo/ bare.git &&
430 git submodule add "$submodurl/repo" &&
431 git config -f .gitmodules submodule.repo.path repo &&
432 git submodule add "$submodurl/bare.git" &&
433 git config -f .gitmodules submodule.bare.path bare
437 test_expect_success
'add should fail when path is used by a file' '
441 test_must_fail git submodule add "$submodurl/repo" file
445 test_expect_success
'add should fail when path is used by an existing directory' '
449 test_must_fail git submodule add "$submodurl/repo" empty-dir
453 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
456 git submodule add ../repo relative &&
457 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
458 git submodule sync relative &&
459 test "$(git config submodule.relative.url)" = "$submodurl/repo"
463 test_expect_success
'set up for relative path tests' '
475 git config -f .gitmodules submodule.sub.path sub &&
476 git config -f .gitmodules submodule.sub.url ../subrepo &&
477 cp .git/config pristine-.git-config
481 test_expect_success
'relative path works with URL' '
484 cp pristine-.git-config .git/config &&
485 git config remote.origin.url ssh://hostname/repo &&
486 git submodule init &&
487 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
491 test_expect_success
'relative path works with user@host:path' '
494 cp pristine-.git-config .git/config &&
495 git config remote.origin.url user@host:repo &&
496 git submodule init &&
497 test "$(git config submodule.sub.url)" = user@host:subrepo