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 'Prepare submodule add testing' '
61 test_expect_success 'submodule add' '
64 git submodule add "$submodurl" submod &&
69 test_expect_success 'submodule add --branch' '
72 git submodule add -b initial "$submodurl" submod-branch &&
75 git branch | grep initial
79 test_expect_success 'submodule add with ./ in path' '
82 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
87 test_expect_success 'submodule add with // in path' '
90 git submodule add "$submodurl" slashslashsubmod///frotz// &&
95 test_expect_success 'submodule add with /.. in path' '
98 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
103 test_expect_success 'submodule add with ./, /.. and // in path' '
106 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
111 test_expect_success 'status should fail for unmapped paths' '
112 if git submodule status
114 echo "[OOPS] submodule status succeeded"
116 elif ! GIT_CONFIG=.gitmodules git config submodule.example.path init
118 echo "[OOPS] git config failed to update .gitmodules"
123 test_expect_success 'status should only print one line' '
124 lines=$(git submodule status | wc -l) &&
128 test_expect_success 'status should initially be "missing"' '
129 git submodule status | grep "^-$rev1"
132 test_expect_success 'init should register submodule url in .git/config' '
133 git submodule init &&
134 url=$(git config submodule.example.url) &&
135 if test "$url" != "git://example.com/init.git"
137 echo "[OOPS] init succeeded but submodule url is wrong"
139 elif test_must_fail git config submodule.example.url ./.subrepo
141 echo "[OOPS] init succeeded but update of url failed"
146 test_expect_success 'update should fail when path is used by a file' '
147 echo "hello" >init &&
148 if git submodule update
150 echo "[OOPS] update should have failed"
152 elif test "$(cat init)" != "hello"
154 echo "[OOPS] update failed but init file was molested"
161 test_expect_success 'update should fail when path is used by a nonempty directory' '
163 echo "hello" >init/a &&
164 if git submodule update
166 echo "[OOPS] update should have failed"
168 elif test "$(cat init/a)" != "hello"
170 echo "[OOPS] update failed but init/a was molested"
177 test_expect_success 'update should work when path is an empty dir' '
180 git submodule update &&
181 head=$(cd init && git rev-parse HEAD) &&
184 echo "[OOPS] Failed to obtain submodule head"
186 elif test "$head" != "$rev1"
188 echo "[OOPS] Submodule head is $head but should have been $rev1"
193 test_expect_success 'status should be "up-to-date" after update' '
194 git submodule status | grep "^ $rev1"
197 test_expect_success 'status should be "modified" after submodule commit' '
201 git commit -m "submodule commit 2" &&
202 rev2=$(git rev-parse HEAD) &&
206 echo "[OOPS] submodule git rev-parse returned nothing"
209 git submodule status | grep "^+$rev2"
212 test_expect_success 'the --cached sha1 should be rev1' '
213 git submodule --cached status | grep "^+$rev1"
216 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
217 git diff | grep "^+Subproject commit $rev2"
220 test_expect_success 'update should checkout rev1' '
221 git submodule update init &&
222 head=$(cd init && git rev-parse HEAD) &&
225 echo "[OOPS] submodule git rev-parse returned nothing"
227 elif test "$head" != "$rev1"
229 echo "[OOPS] init did not checkout correct head"
234 test_expect_success 'status should be "up-to-date" after update' '
235 git submodule status | grep "^ $rev1"
238 test_expect_success 'checkout superproject with subproject already present' '
239 git checkout initial &&
243 test_expect_success 'apply submodule diff' '
249 git commit -m "change subproject"
251 git update-index --add init &&
252 git commit -m "change init" &&
253 git format-patch -1 --stdout >P.diff &&
254 git checkout second &&
255 git apply --index P.diff &&
256 D=$(git diff --cached master) &&
260 test_expect_success 'update --init' '
263 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
264 git config --remove-section submodule.example
265 git submodule update init > update.out &&
266 grep "not initialized" update.out &&
267 test ! -d init/.git &&
268 git submodule update --init init &&
273 test_expect_success 'do not add files from a submodule' '
276 test_must_fail git add init/a
280 test_expect_success 'gracefully add submodule with a trailing slash' '
283 git commit -m "commit subproject" init &&
287 git diff --exit-code --cached init &&
289 git commit -m update a >/dev/null &&
290 git rev-parse HEAD) &&
292 test_must_fail git diff --exit-code --cached init &&
293 test $commit = $(git ls-files --stage |
294 sed -n "s/^160000 \([^ ]*\).*/\1/p")
298 test_expect_success 'ls-files gracefully handles trailing slash' '
300 test "init" = "$(git ls-files init/)"
304 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
308 git checkout initial &&
313 test_expect_success 'submodule <invalid-path> warns' '
315 git submodule no-such-submodule 2> output.err &&
316 grep "^error: .*no-such-submodule" output.err
320 test_expect_success 'add submodules without specifying an explicit path' '
326 git commit -m "repo commit 1" &&
328 git clone --bare repo/ bare.git &&
330 git submodule add "$submodurl/repo" &&
331 git config -f .gitmodules submodule.repo.path repo &&
332 git submodule add "$submodurl/bare.git" &&
333 git config -f .gitmodules submodule.bare.path bare