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_expect_success
'update should fail when path is used by a file' '
262 echo hello >expect &&
264 echo "hello" >init &&
265 test_must_fail git submodule update &&
270 test_expect_success
'update should fail when path is used by a nonempty directory' '
271 echo hello >expect &&
275 echo "hello" >init/a &&
277 test_must_fail git submodule update &&
279 test_cmp expect init/a
282 test_expect_success
'update should work when path is an empty dir' '
285 echo "$rev1" >expect &&
288 git submodule update -q >update.out &&
289 test ! -s update.out &&
292 test_cmp expect head-sha1
295 test_expect_success
'status should be "up-to-date" after update' '
296 git submodule status >list &&
300 test_expect_success
'status should be "modified" after submodule commit' '
305 git commit -m "submodule commit 2"
308 rev2=$(cd init && git rev-parse HEAD) &&
310 git submodule status >list &&
315 test_expect_success
'the --cached sha1 should be rev1' '
316 git submodule --cached status >list &&
320 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
322 grep "^+Subproject commit $rev2" diff
325 test_expect_success
'update should checkout rev1' '
327 echo "$rev1" >expect &&
329 git submodule update init &&
332 test_cmp expect head-sha1
335 test_expect_success
'status should be "up-to-date" after update' '
336 git submodule status >list &&
340 test_expect_success
'checkout superproject with subproject already present' '
341 git checkout initial &&
345 test_expect_success
'apply submodule diff' '
353 git commit -m "change subproject"
355 git update-index --add init &&
356 git commit -m "change init" &&
357 git format-patch -1 --stdout >P.diff &&
358 git checkout second &&
359 git apply --index P.diff &&
361 git diff --cached master >staged &&
362 test_cmp empty staged
365 test_expect_success
'update --init' '
367 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
368 git config --remove-section submodule.example &&
369 test_must_fail git config submodule.example.url &&
371 git submodule update init > update.out &&
373 test_i18ngrep "not initialized" update.out &&
374 test_must_fail git rev-parse --resolve-git-dir init/.git &&
376 git submodule update --init init &&
377 git rev-parse --resolve-git-dir init/.git
380 test_expect_success
'do not add files from a submodule' '
383 test_must_fail git add init/a
387 test_expect_success
'gracefully add submodule with a trailing slash' '
390 git commit -m "commit subproject" init &&
394 git diff --exit-code --cached init &&
396 git commit -m update a >/dev/null &&
397 git rev-parse HEAD) &&
399 test_must_fail git diff --exit-code --cached init &&
400 test $commit = $(git ls-files --stage |
401 sed -n "s/^160000 \([^ ]*\).*/\1/p")
405 test_expect_success
'ls-files gracefully handles trailing slash' '
407 test "init" = "$(git ls-files init/)"
411 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
415 git checkout initial &&
420 test_expect_success
'submodule <invalid-path> warns' '
422 git submodule no-such-submodule 2> output.err &&
423 grep "^error: .*no-such-submodule" output.err
427 test_expect_success
'add submodules without specifying an explicit path' '
434 git commit -m "repo commit 1"
436 git clone --bare repo/ bare.git &&
439 git submodule add "$submodurl/repo" &&
440 git config -f .gitmodules submodule.repo.path repo &&
441 git submodule add "$submodurl/bare.git" &&
442 git config -f .gitmodules submodule.bare.path bare
446 test_expect_success
'add should fail when path is used by a file' '
450 test_must_fail git submodule add "$submodurl/repo" file
454 test_expect_success
'add should fail when path is used by an existing directory' '
458 test_must_fail git submodule add "$submodurl/repo" empty-dir
462 test_expect_success
'use superproject as upstream when path is relative and no url is set there' '
465 git submodule add ../repo relative &&
466 test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
467 git submodule sync relative &&
468 test "$(git config submodule.relative.url)" = "$submodurl/repo"
472 test_expect_success
'set up for relative path tests' '
484 git config -f .gitmodules submodule.sub.path sub &&
485 git config -f .gitmodules submodule.sub.url ../subrepo &&
486 cp .git/config pristine-.git-config
490 test_expect_success
'relative path works with URL' '
493 cp pristine-.git-config .git/config &&
494 git config remote.origin.url ssh://hostname/repo &&
495 git submodule init &&
496 test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
500 test_expect_success
'relative path works with user@host:path' '
503 cp pristine-.git-config .git/config &&
504 git config remote.origin.url user@host:repo &&
505 git submodule init &&
506 test "$(git config submodule.sub.url)" = user@host:subrepo
510 test_expect_success
'moving the superproject does not break submodules' '
513 git submodule status >expect
515 mv addtest addtest2 &&
518 git submodule status >actual &&
519 test_cmp expect actual