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 with ./ in path' '
70 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
75 test_expect_success 'submodule add with // in path' '
78 git submodule add "$submodurl" slashslashsubmod///frotz// &&
83 test_expect_success 'submodule add with /.. in path' '
86 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
91 test_expect_success 'submodule add with ./, /.. and // in path' '
94 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
99 test_expect_success 'status should fail for unmapped paths' '
100 if git submodule status
102 echo "[OOPS] submodule status succeeded"
104 elif ! GIT_CONFIG=.gitmodules git config submodule.example.path init
106 echo "[OOPS] git config failed to update .gitmodules"
111 test_expect_success 'status should only print one line' '
112 lines=$(git submodule status | wc -l) &&
116 test_expect_success 'status should initially be "missing"' '
117 git submodule status | grep "^-$rev1"
120 test_expect_success 'init should register submodule url in .git/config' '
121 git submodule init &&
122 url=$(git config submodule.example.url) &&
123 if test "$url" != "git://example.com/init.git"
125 echo "[OOPS] init succeeded but submodule url is wrong"
127 elif test_must_fail git config submodule.example.url ./.subrepo
129 echo "[OOPS] init succeeded but update of url failed"
134 test_expect_success 'update should fail when path is used by a file' '
135 echo "hello" >init &&
136 if git submodule update
138 echo "[OOPS] update should have failed"
140 elif test "$(cat init)" != "hello"
142 echo "[OOPS] update failed but init file was molested"
149 test_expect_success 'update should fail when path is used by a nonempty directory' '
151 echo "hello" >init/a &&
152 if git submodule update
154 echo "[OOPS] update should have failed"
156 elif test "$(cat init/a)" != "hello"
158 echo "[OOPS] update failed but init/a was molested"
165 test_expect_success 'update should work when path is an empty dir' '
168 git submodule update &&
169 head=$(cd init && git rev-parse HEAD) &&
172 echo "[OOPS] Failed to obtain submodule head"
174 elif test "$head" != "$rev1"
176 echo "[OOPS] Submodule head is $head but should have been $rev1"
181 test_expect_success 'status should be "up-to-date" after update' '
182 git submodule status | grep "^ $rev1"
185 test_expect_success 'status should be "modified" after submodule commit' '
189 git commit -m "submodule commit 2" &&
190 rev2=$(git rev-parse HEAD) &&
194 echo "[OOPS] submodule git rev-parse returned nothing"
197 git submodule status | grep "^+$rev2"
200 test_expect_success 'the --cached sha1 should be rev1' '
201 git submodule --cached status | grep "^+$rev1"
204 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
205 git diff | grep "^+Subproject commit $rev2"
208 test_expect_success 'update should checkout rev1' '
209 git submodule update init &&
210 head=$(cd init && git rev-parse HEAD) &&
213 echo "[OOPS] submodule git rev-parse returned nothing"
215 elif test "$head" != "$rev1"
217 echo "[OOPS] init did not checkout correct head"
222 test_expect_success 'status should be "up-to-date" after update' '
223 git submodule status | grep "^ $rev1"
226 test_expect_success 'checkout superproject with subproject already present' '
227 git checkout initial &&
231 test_expect_success 'apply submodule diff' '
237 git commit -m "change subproject"
239 git update-index --add init &&
240 git commit -m "change init" &&
241 git format-patch -1 --stdout >P.diff &&
242 git checkout second &&
243 git apply --index P.diff &&
244 D=$(git diff --cached master) &&
248 test_expect_success 'update --init' '
251 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
252 git config --remove-section submodule.example
253 git submodule update init > update.out &&
254 grep "not initialized" update.out &&
255 test ! -d init/.git &&
256 git submodule update --init init &&
261 test_expect_success 'do not add files from a submodule' '
264 test_must_fail git add init/a
268 test_expect_success 'gracefully add submodule with a trailing slash' '
271 git commit -m "commit subproject" init &&
275 git diff --exit-code --cached init &&
277 git commit -m update a >/dev/null &&
278 git rev-parse HEAD) &&
280 test_must_fail git diff --exit-code --cached init &&
281 test $commit = $(git ls-files --stage |
282 sed -n "s/^160000 \([^ ]*\).*/\1/p")
286 test_expect_success 'ls-files gracefully handles trailing slash' '
288 test "init" = "$(git ls-files init/)"
292 test_expect_success 'submodule <invalid-path> warns' '
294 git submodule no-such-submodule 2> output.err &&
295 grep "^error: .*no-such-submodule" output.err