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.
16 # -create a repository in directory init
17 # -add a couple of files
18 # -add directory init to 'superproject', this creates a DIRLINK entry
19 # -add a couple of regular files to enable testing of submodule filtering
21 # -add an entry to .gitmodules for submodule 'example'
23 test_expect_success
'Prepare submodule testing' '
26 git commit -m "initial commit" &&
27 git branch initial HEAD &&
33 git commit -m "submodule commit 1" &&
34 git tag -a -m "rev-1" rev-1 &&
35 rev1=$(git rev-parse HEAD) &&
38 echo "[OOPS] submodule git rev-parse returned nothing"
45 git commit -m "super commit 1" &&
47 GIT_CONFIG=.gitmodules git config submodule.example.url git://example.com/init.git
50 test_expect_success
'Prepare submodule add testing' '
59 test_expect_success
'submodule add' '
62 git submodule add "$submodurl" submod &&
67 test_expect_success
'submodule add --branch' '
70 git submodule add -b initial "$submodurl" submod-branch &&
73 git branch | grep initial
77 test_expect_success
'submodule add with ./ in path' '
80 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
85 test_expect_success
'submodule add with // in path' '
88 git submodule add "$submodurl" slashslashsubmod///frotz// &&
93 test_expect_success
'submodule add with /.. in path' '
96 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
101 test_expect_success
'submodule add with ./, /.. and // in path' '
104 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
109 test_expect_success
'status should fail for unmapped paths' '
110 if git submodule status
112 echo "[OOPS] submodule status succeeded"
114 elif ! GIT_CONFIG=.gitmodules git config submodule.example.path init
116 echo "[OOPS] git config failed to update .gitmodules"
121 test_expect_success
'status should only print one line' '
122 lines=$(git submodule status | wc -l) &&
126 test_expect_success
'status should initially be "missing"' '
127 git submodule status | grep "^-$rev1"
130 test_expect_success
'init should register submodule url in .git/config' '
131 git submodule init &&
132 url=$(git config submodule.example.url) &&
133 if test "$url" != "git://example.com/init.git"
135 echo "[OOPS] init succeeded but submodule url is wrong"
137 elif test_must_fail git config submodule.example.url ./.subrepo
139 echo "[OOPS] init succeeded but update of url failed"
144 test_expect_success
'update should fail when path is used by a file' '
145 echo "hello" >init &&
146 if git submodule update
148 echo "[OOPS] update should have failed"
150 elif test "$(cat init)" != "hello"
152 echo "[OOPS] update failed but init file was molested"
159 test_expect_success
'update should fail when path is used by a nonempty directory' '
161 echo "hello" >init/a &&
162 if git submodule update
164 echo "[OOPS] update should have failed"
166 elif test "$(cat init/a)" != "hello"
168 echo "[OOPS] update failed but init/a was molested"
175 test_expect_success
'update should work when path is an empty dir' '
178 git submodule update &&
179 head=$(cd init && git rev-parse HEAD) &&
182 echo "[OOPS] Failed to obtain submodule head"
184 elif test "$head" != "$rev1"
186 echo "[OOPS] Submodule head is $head but should have been $rev1"
191 test_expect_success
'status should be "up-to-date" after update' '
192 git submodule status | grep "^ $rev1"
195 test_expect_success
'status should be "modified" after submodule commit' '
199 git commit -m "submodule commit 2" &&
200 rev2=$(git rev-parse HEAD) &&
204 echo "[OOPS] submodule git rev-parse returned nothing"
207 git submodule status | grep "^+$rev2"
210 test_expect_success
'the --cached sha1 should be rev1' '
211 git submodule --cached status | grep "^+$rev1"
214 test_expect_success
'git diff should report the SHA1 of the new submodule commit' '
215 git diff | grep "^+Subproject commit $rev2"
218 test_expect_success
'update should checkout rev1' '
219 git submodule update init &&
220 head=$(cd init && git rev-parse HEAD) &&
223 echo "[OOPS] submodule git rev-parse returned nothing"
225 elif test "$head" != "$rev1"
227 echo "[OOPS] init did not checkout correct head"
232 test_expect_success
'status should be "up-to-date" after update' '
233 git submodule status | grep "^ $rev1"
236 test_expect_success
'checkout superproject with subproject already present' '
237 git checkout initial &&
241 test_expect_success
'apply submodule diff' '
247 git commit -m "change subproject"
249 git update-index --add init &&
250 git commit -m "change init" &&
251 git format-patch -1 --stdout >P.diff &&
252 git checkout second &&
253 git apply --index P.diff &&
254 D=$(git diff --cached master) &&
258 test_expect_success
'update --init' '
261 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
262 git config --remove-section submodule.example
263 git submodule update init > update.out &&
264 grep "not initialized" update.out &&
265 test ! -d init/.git &&
266 git submodule update --init init &&
271 test_expect_success
'do not add files from a submodule' '
274 test_must_fail git add init/a
278 test_expect_success
'gracefully add submodule with a trailing slash' '
281 git commit -m "commit subproject" init &&
285 git diff --exit-code --cached init &&
287 git commit -m update a >/dev/null &&
288 git rev-parse HEAD) &&
290 test_must_fail git diff --exit-code --cached init &&
291 test $commit = $(git ls-files --stage |
292 sed -n "s/^160000 \([^ ]*\).*/\1/p")
296 test_expect_success
'ls-files gracefully handles trailing slash' '
298 test "init" = "$(git ls-files init/)"
302 test_expect_success
'moving to a commit without submodule does not leave empty dir' '
306 git checkout initial &&
311 test_expect_success
'submodule <invalid-path> warns' '
313 git submodule no-such-submodule 2> output.err &&
314 grep "^error: .*no-such-submodule" output.err
318 test_expect_success
'add submodules without specifying an explicit path' '
324 git commit -m "repo commit 1" &&
326 git clone --bare repo/ bare.git &&
328 git submodule add "$submodurl/repo" &&
329 git config -f .gitmodules submodule.repo.path repo &&
330 git submodule add "$submodurl/bare.git" &&
331 git config -f .gitmodules submodule.bare.path bare