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
31 rev1=$(cd init && git rev-parse HEAD) &&
32 printf "rev1: %s\n" "$rev1" &&
36 test_expect_success
'setup - commit with gitlink' '
40 git commit -m "super commit 1"
43 test_expect_success
'setup - hide init subdirectory' '
47 test_expect_success
'setup - add an example entry to .gitmodules' '
48 GIT_CONFIG=.gitmodules \
49 git config submodule.example.url git://example.com/init.git
52 test_expect_success
'setup - repository to add submodules to' '
56 # The 'submodule add' tests need some repository to add as a submodule.
57 # The trash directory is a good one as any.
58 submodurl
=$TRASH_DIRECTORY
61 git for-each-ref
--format='%(refname)' 'refs/heads/*'
70 listbranches
>"$dotdot/heads" &&
71 { git symbolic-ref HEAD ||
:; } >"$dotdot/head" &&
72 git update-index
--refresh &&
73 git diff-files
--exit-code &&
74 git clean
-n -d -x >"$dotdot/untracked"
78 test_expect_success
'submodule add' '
79 echo "refs/heads/master" >expect &&
84 git submodule add "$submodurl" submod &&
88 rm -f heads head untracked &&
89 inspect addtest/submod ../.. &&
90 test_cmp expect heads &&
91 test_cmp expect head &&
92 test_cmp empty untracked
95 test_expect_success
'submodule add --branch' '
96 echo "refs/heads/initial" >expect-head &&
97 cat <<-\EOF >expect-heads &&
105 git submodule add -b initial "$submodurl" submod-branch &&
109 rm -f heads head untracked &&
110 inspect addtest/submod-branch ../.. &&
111 test_cmp expect-heads heads &&
112 test_cmp expect-head head &&
113 test_cmp empty untracked
116 test_expect_success
'submodule add with ./ in path' '
117 echo "refs/heads/master" >expect &&
122 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
126 rm -f heads head untracked &&
127 inspect addtest/dotsubmod/frotz ../../.. &&
128 test_cmp expect heads &&
129 test_cmp expect head &&
130 test_cmp empty untracked
133 test_expect_success
'submodule add with // in path' '
134 echo "refs/heads/master" >expect &&
139 git submodule add "$submodurl" slashslashsubmod///frotz// &&
143 rm -f heads head untracked &&
144 inspect addtest/slashslashsubmod/frotz ../../.. &&
145 test_cmp expect heads &&
146 test_cmp expect head &&
147 test_cmp empty untracked
150 test_expect_success
'submodule add with /.. in path' '
151 echo "refs/heads/master" >expect &&
156 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
160 rm -f heads head untracked &&
161 inspect addtest/realsubmod ../.. &&
162 test_cmp expect heads &&
163 test_cmp expect head &&
164 test_cmp empty untracked
167 test_expect_success
'submodule add with ./, /.. and // in path' '
168 echo "refs/heads/master" >expect &&
173 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
177 rm -f heads head untracked &&
178 inspect addtest/realsubmod2 ../.. &&
179 test_cmp expect heads &&
180 test_cmp expect head &&
181 test_cmp empty untracked
184 test_expect_success
'status should fail for unmapped paths' '
185 if git submodule status
187 echo "[OOPS] submodule status succeeded"
189 elif ! GIT_CONFIG=.gitmodules git config submodule.example.path init
191 echo "[OOPS] git config failed to update .gitmodules"
196 test_expect_success
'status should only print one line' '
197 lines=$(git submodule status | wc -l) &&
201 test_expect_success
'status should initially be "missing"' '
202 git submodule status | grep "^-$rev1"
205 test_expect_success
'init should register submodule url in .git/config' '
206 git submodule init &&
207 url=$(git config submodule.example.url) &&
208 if test "$url" != "git://example.com/init.git"
210 echo "[OOPS] init succeeded but submodule url is wrong"
212 elif test_must_fail git config submodule.example.url ./.subrepo
214 echo "[OOPS] init succeeded but update of url failed"
219 test_expect_success
'update should fail when path is used by a file' '
220 echo "hello" >init &&
221 if git submodule update
223 echo "[OOPS] update should have failed"
225 elif test "$(cat init)" != "hello"
227 echo "[OOPS] update failed but init file was molested"
234 test_expect_success
'update should fail when path is used by a nonempty directory' '
236 echo "hello" >init/a &&
237 if git submodule update
239 echo "[OOPS] update should have failed"
241 elif test "$(cat init/a)" != "hello"
243 echo "[OOPS] update failed but init/a was molested"
250 test_expect_success
'update should work when path is an empty dir' '
253 git submodule update &&
254 head=$(cd init && git rev-parse HEAD) &&
257 echo "[OOPS] Failed to obtain submodule head"
259 elif test "$head" != "$rev1"
261 echo "[OOPS] Submodule head is $head but should have been $rev1"
266 test_expect_success
'status should be "up-to-date" after update' '
267 git submodule status | grep "^ $rev1"
270 test_expect_success
'status should be "modified" after submodule commit' '
274 git commit -m "submodule commit 2" &&
275 rev2=$(git rev-parse HEAD) &&
279 echo "[OOPS] submodule git rev-parse returned nothing"
282 git submodule status | grep "^+$rev2"
285 test_expect_success
'the --cached sha1 should be rev1' '
286 git submodule --cached status | grep "^+$rev1"
289 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
290 git diff | grep "^+Subproject commit $rev2"
293 test_expect_success
'update should checkout rev1' '
294 git submodule update init &&
295 head=$(cd init && git rev-parse HEAD) &&
298 echo "[OOPS] submodule git rev-parse returned nothing"
300 elif test "$head" != "$rev1"
302 echo "[OOPS] init did not checkout correct head"
307 test_expect_success
'status should be "up-to-date" after update' '
308 git submodule status | grep "^ $rev1"
311 test_expect_success
'checkout superproject with subproject already present' '
312 git checkout initial &&
316 test_expect_success
'apply submodule diff' '
322 git commit -m "change subproject"
324 git update-index --add init &&
325 git commit -m "change init" &&
326 git format-patch -1 --stdout >P.diff &&
327 git checkout second &&
328 git apply --index P.diff &&
329 D=$(git diff --cached master) &&
333 test_expect_success
'update --init' '
336 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
337 git config --remove-section submodule.example
338 git submodule update init > update.out &&
339 grep "not initialized" update.out &&
340 test ! -d init/.git &&
341 git submodule update --init init &&
346 test_expect_success
'do not add files from a submodule' '
349 test_must_fail git add init/a
353 test_expect_success
'gracefully add submodule with a trailing slash' '
356 git commit -m "commit subproject" init &&
360 git diff --exit-code --cached init &&
362 git commit -m update a >/dev/null &&
363 git rev-parse HEAD) &&
365 test_must_fail git diff --exit-code --cached init &&
366 test $commit = $(git ls-files --stage |
367 sed -n "s/^160000 \([^ ]*\).*/\1/p")
371 test_expect_success
'ls-files gracefully handles trailing slash' '
373 test "init" = "$(git ls-files init/)"
377 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
381 git checkout initial &&
386 test_expect_success
'submodule <invalid-path> warns' '
388 git submodule no-such-submodule 2> output.err &&
389 grep "^error: .*no-such-submodule" output.err
393 test_expect_success
'add submodules without specifying an explicit path' '
399 git commit -m "repo commit 1" &&
401 git clone --bare repo/ bare.git &&
403 git submodule add "$submodurl/repo" &&
404 git config -f .gitmodules submodule.repo.path repo &&
405 git submodule add "$submodurl/bare.git" &&
406 git config -f .gitmodules submodule.bare.path bare