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' '
48 # The 'submodule add' tests need some repository to add as a submodule.
49 # The trash directory is a good one as any.
50 submodurl=$TRASH_DIRECTORY
53 git for-each-ref --format='%(refname)' 'refs/heads/*'
62 listbranches >"$dotdot/heads" &&
63 { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
64 git rev-parse HEAD >"$dotdot/head-sha1" &&
65 git update-index --refresh &&
66 git diff-files --exit-code &&
67 git clean -n -d -x >"$dotdot/untracked"
71 test_expect_success 'submodule add' '
72 echo "refs/heads/master" >expect &&
77 git submodule add "$submodurl" submod &&
81 rm -f heads head untracked &&
82 inspect addtest/submod ../.. &&
83 test_cmp expect heads &&
84 test_cmp expect head &&
85 test_cmp empty untracked
88 test_expect_success 'submodule add --branch' '
89 echo "refs/heads/initial" >expect-head &&
90 cat <<-\EOF >expect-heads &&
98 git submodule add -b initial "$submodurl" submod-branch &&
102 rm -f heads head untracked &&
103 inspect addtest/submod-branch ../.. &&
104 test_cmp expect-heads heads &&
105 test_cmp expect-head head &&
106 test_cmp empty untracked
109 test_expect_success 'submodule add with ./ in path' '
110 echo "refs/heads/master" >expect &&
115 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
119 rm -f heads head untracked &&
120 inspect addtest/dotsubmod/frotz ../../.. &&
121 test_cmp expect heads &&
122 test_cmp expect head &&
123 test_cmp empty untracked
126 test_expect_success 'submodule add with // in path' '
127 echo "refs/heads/master" >expect &&
132 git submodule add "$submodurl" slashslashsubmod///frotz// &&
136 rm -f heads head untracked &&
137 inspect addtest/slashslashsubmod/frotz ../../.. &&
138 test_cmp expect heads &&
139 test_cmp expect head &&
140 test_cmp empty untracked
143 test_expect_success 'submodule add with /.. in path' '
144 echo "refs/heads/master" >expect &&
149 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
153 rm -f heads head untracked &&
154 inspect addtest/realsubmod ../.. &&
155 test_cmp expect heads &&
156 test_cmp expect head &&
157 test_cmp empty untracked
160 test_expect_success 'submodule add with ./, /.. and // in path' '
161 echo "refs/heads/master" >expect &&
166 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
170 rm -f heads head untracked &&
171 inspect addtest/realsubmod2 ../.. &&
172 test_cmp expect heads &&
173 test_cmp expect head &&
174 test_cmp empty untracked
177 test_expect_success 'setup - add an example entry to .gitmodules' '
178 GIT_CONFIG=.gitmodules \
179 git config submodule.example.url git://example.com/init.git
182 test_expect_success 'status should fail for unmapped paths' '
183 test_must_fail git submodule status
186 test_expect_success 'setup - map path in .gitmodules' '
187 cat <<\EOF >expect &&
188 [submodule "example"]
189 url = git://example.com/init.git
193 GIT_CONFIG=.gitmodules git config submodule.example.path init &&
195 test_cmp expect .gitmodules
198 test_expect_success 'status should only print one line' '
199 git submodule status >lines &&
200 test $(wc -l <lines) = 1
203 test_expect_success 'setup - fetch commit name from submodule' '
204 rev1=$(cd .subrepo && git rev-parse HEAD) &&
205 printf "rev1: %s\n" "$rev1" &&
209 test_expect_success 'status should initially be "missing"' '
210 git submodule status >lines &&
214 test_expect_success 'init should register submodule url in .git/config' '
215 echo git://example.com/init.git >expect &&
217 git submodule init &&
218 git config submodule.example.url >url &&
219 git config submodule.example.url ./.subrepo &&
224 test_expect_success 'update should fail when path is used by a file' '
225 echo hello >expect &&
227 echo "hello" >init &&
228 test_must_fail git submodule update &&
233 test_expect_success 'update should fail when path is used by a nonempty directory' '
234 echo hello >expect &&
238 echo "hello" >init/a &&
240 test_must_fail git submodule update &&
242 test_cmp expect init/a
245 test_expect_success 'update should work when path is an empty dir' '
248 echo "$rev1" >expect &&
251 git submodule update &&
254 test_cmp expect head-sha1
257 test_expect_success 'status should be "up-to-date" after update' '
258 git submodule status >list &&
262 test_expect_success 'status should be "modified" after submodule commit' '
267 git commit -m "submodule commit 2"
270 rev2=$(cd init && git rev-parse HEAD) &&
272 git submodule status >list &&
277 test_expect_success 'the --cached sha1 should be rev1' '
278 git submodule --cached status >list &&
282 test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
284 grep "^+Subproject commit $rev2" diff
287 test_expect_success 'update should checkout rev1' '
289 echo "$rev1" >expect &&
291 git submodule update init &&
294 test_cmp expect head-sha1
297 test_expect_success 'status should be "up-to-date" after update' '
298 git submodule status >list &&
302 test_expect_success 'checkout superproject with subproject already present' '
303 git checkout initial &&
307 test_expect_success 'apply submodule diff' '
315 git commit -m "change subproject"
317 git update-index --add init &&
318 git commit -m "change init" &&
319 git format-patch -1 --stdout >P.diff &&
320 git checkout second &&
321 git apply --index P.diff &&
323 git diff --cached master >staged &&
324 test_cmp empty staged
327 test_expect_success 'update --init' '
329 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
330 git config --remove-section submodule.example &&
331 test_must_fail git config submodule.example.url &&
333 git submodule update init > update.out &&
335 grep "not initialized" update.out &&
336 ! test -d init/.git &&
338 git submodule update --init init &&
342 test_expect_success 'do not add files from a submodule' '
345 test_must_fail git add init/a
349 test_expect_success 'gracefully add submodule with a trailing slash' '
352 git commit -m "commit subproject" init &&
356 git diff --exit-code --cached init &&
358 git commit -m update a >/dev/null &&
359 git rev-parse HEAD) &&
361 test_must_fail git diff --exit-code --cached init &&
362 test $commit = $(git ls-files --stage |
363 sed -n "s/^160000 \([^ ]*\).*/\1/p")
367 test_expect_success 'ls-files gracefully handles trailing slash' '
369 test "init" = "$(git ls-files init/)"
373 test_expect_success 'moving to a commit without submodule does not leave empty dir' '
377 git checkout initial &&
382 test_expect_success 'submodule <invalid-path> warns' '
384 git submodule no-such-submodule 2> output.err &&
385 grep "^error: .*no-such-submodule" output.err
389 test_expect_success 'add submodules without specifying an explicit path' '
395 git commit -m "repo commit 1" &&
397 git clone --bare repo/ bare.git &&
399 git submodule add "$submodurl/repo" &&
400 git config -f .gitmodules submodule.repo.path repo &&
401 git submodule add "$submodurl/bare.git" &&
402 git config -f .gitmodules submodule.bare.path bare