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.
51 submodurl=$TRASH_DIRECTORY
54 git for-each-ref --format='%(refname)' 'refs/heads/*'
63 listbranches >"$dotdot/heads" &&
64 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
65 git rev-parse HEAD >"$dotdot/head-sha1" &&
66 git update-index --refresh &&
67 git diff-files --exit-code &&
68 git clean -n -d -x >"$dotdot/untracked"
72 test_expect_success 'submodule add' '
73 echo "refs/heads/master" >expect &&
78 git submodule add "$submodurl" submod &&
82 rm -f heads head untracked &&
83 inspect addtest/submod ../.. &&
84 test_cmp expect heads &&
85 test_cmp expect head &&
86 test_cmp empty untracked
89 test_expect_success 'submodule add to .gitignored path' '
90 echo "refs/heads/master" >expect &&
95 # Does not use test_commit due to the ignore
96 echo "*" > .gitignore &&
97 git add --force .gitignore &&
98 git commit -m"Ignore everything" &&
99 git submodule add "$submodurl" submod &&
103 rm -f heads head untracked &&
104 inspect addtest/submod ../.. &&
105 test_cmp expect heads &&
106 test_cmp expect head &&
107 test_cmp empty untracked
110 test_expect_success 'submodule add --branch' '
111 echo "refs/heads/initial" >expect-head &&
112 cat <<-\EOF >expect-heads &&
120 git submodule add -b initial "$submodurl" submod-branch &&
124 rm -f heads head untracked &&
125 inspect addtest/submod-branch ../.. &&
126 test_cmp expect-heads heads &&
127 test_cmp expect-head head &&
128 test_cmp empty untracked
131 test_expect_success 'submodule add with ./ in path' '
132 echo "refs/heads/master" >expect &&
137 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
141 rm -f heads head untracked &&
142 inspect addtest/dotsubmod/frotz ../../.. &&
143 test_cmp expect heads &&
144 test_cmp expect head &&
145 test_cmp empty untracked
148 test_expect_success 'submodule add with // in path' '
149 echo "refs/heads/master" >expect &&
154 git submodule add "$submodurl" slashslashsubmod///frotz// &&
158 rm -f heads head untracked &&
159 inspect addtest/slashslashsubmod/frotz ../../.. &&
160 test_cmp expect heads &&
161 test_cmp expect head &&
162 test_cmp empty untracked
165 test_expect_success 'submodule add with /.. in path' '
166 echo "refs/heads/master" >expect &&
171 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
175 rm -f heads head untracked &&
176 inspect addtest/realsubmod ../.. &&
177 test_cmp expect heads &&
178 test_cmp expect head &&
179 test_cmp empty untracked
182 test_expect_success 'submodule add with ./, /.. and // in path' '
183 echo "refs/heads/master" >expect &&
188 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
192 rm -f heads head untracked &&
193 inspect addtest/realsubmod2 ../.. &&
194 test_cmp expect heads &&
195 test_cmp expect head &&
196 test_cmp empty untracked
199 test_expect_success 'setup - add an example entry to .gitmodules' '
200 GIT_CONFIG=.gitmodules \
201 git config submodule.example.url git://example.com/init.git
204 test_expect_success 'status should fail for unmapped paths' '
205 test_must_fail git submodule status
208 test_expect_success 'setup - map path in .gitmodules' '
209 cat <<\EOF >expect &&
210 [submodule "example"]
211 url = git://example.com/init.git
215 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
217 test_cmp expect .gitmodules
220 test_expect_success 'status should only print one line' '
221 git submodule status >lines &&
222 test $(wc -l <lines) = 1
225 test_expect_success 'setup - fetch commit name from submodule' '
226 rev1=$(cd .subrepo && git rev-parse HEAD) &&
227 printf "rev1: %s\n" "$rev1" &&
231 test_expect_success 'status should initially be "missing"' '
232 git submodule status >lines &&
236 test_expect_success 'init should register submodule url in .git/config' '
237 echo git://example.com/init.git >expect &&
239 git submodule init &&
240 git config submodule.example.url >url &&
241 git config submodule.example.url ./.subrepo &&
246 test_expect_success 'update should fail when path is used by a file' '
247 echo hello >expect &&
249 echo "hello" >init &&
250 test_must_fail git submodule update &&
255 test_expect_success 'update should fail when path is used by a nonempty directory' '
256 echo hello >expect &&
260 echo "hello" >init/a &&
262 test_must_fail git submodule update &&
264 test_cmp expect init/a
267 test_expect_success 'update should work when path is an empty dir' '
270 echo "$rev1" >expect &&
273 git submodule update &&
276 test_cmp expect head-sha1
279 test_expect_success 'status should be "up-to-date" after update' '
280 git submodule status >list &&
284 test_expect_success 'status should be "modified" after submodule commit' '
289 git commit -m "submodule commit 2"
292 rev2=$(cd init && git rev-parse HEAD) &&
294 git submodule status >list &&
299 test_expect_success 'the --cached sha1 should be rev1' '
300 git submodule --cached status >list &&
304 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
306 grep "^+Subproject commit $rev2" diff
309 test_expect_success 'update should checkout rev1' '
311 echo "$rev1" >expect &&
313 git submodule update init &&
316 test_cmp expect head-sha1
319 test_expect_success 'status should be "up-to-date" after update' '
320 git submodule status >list &&
324 test_expect_success 'checkout superproject with subproject already present' '
325 git checkout initial &&
329 test_expect_success 'apply submodule diff' '
337 git commit -m "change subproject"
339 git update-index --add init &&
340 git commit -m "change init" &&
341 git format-patch -1 --stdout >P.diff &&
342 git checkout second &&
343 git apply --index P.diff &&
345 git diff --cached master >staged &&
346 test_cmp empty staged
349 test_expect_success 'update --init' '
351 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
352 git config --remove-section submodule.example &&
353 test_must_fail git config submodule.example.url &&
355 git submodule update init > update.out &&
357 grep "not initialized" update.out &&
358 ! test -d init/.git &&
360 git submodule update --init init &&
364 test_expect_success 'do not add files from a submodule' '
367 test_must_fail git add init/a
371 test_expect_success 'gracefully add submodule with a trailing slash' '
374 git commit -m "commit subproject" init &&
378 git diff --exit-code --cached init &&
380 git commit -m update a >/dev/null &&
381 git rev-parse HEAD) &&
383 test_must_fail git diff --exit-code --cached init &&
384 test $commit = $(git ls-files --stage |
385 sed -n "s/^160000 \([^ ]*\).*/\1/p")
389 test_expect_success 'ls-files gracefully handles trailing slash' '
391 test "init" = "$(git ls-files init/)"
395 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
399 git checkout initial &&
404 test_expect_success 'submodule <invalid-path> warns' '
406 git submodule no-such-submodule 2> output.err &&
407 grep "^error: .*no-such-submodule" output.err
411 test_expect_success 'add submodules without specifying an explicit path' '
417 git commit -m "repo commit 1" &&
419 git clone --bare repo/ bare.git &&
421 git submodule add "$submodurl/repo" &&
422 git config -f .gitmodules submodule.repo.path repo &&
423 git submodule add "$submodurl/bare.git" &&
424 git config -f .gitmodules submodule.bare.path bare