1#!/bin/sh
2#
3# Copyright (c) 2007 Lars Hjemli
4#
5
6test_description='Basic porcelain support for submodules
7
8This test tries to verify basic sanity of the init, update and status
9subcommands of git submodule.
10'
11
12. ./test-lib.sh
13
14#
15# Test setup:
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
20# -mv init subrepo
21# -add an entry to .gitmodules for submodule 'example'
22#
23test_expect_success 'Prepare submodule testing' '
24 : > t &&
25 git add t &&
26 git commit -m "initial commit" &&
27 git branch initial HEAD &&
28 mkdir init &&
29 cd init &&
30 git init &&
31 echo a >a &&
32 git add a &&
33 git commit -m "submodule commit 1" &&
34 git tag -a -m "rev-1" rev-1 &&
35 rev1=$(git rev-parse HEAD) &&
36 if test -z "$rev1"
37 then
38 echo "[OOPS] submodule git rev-parse returned nothing"
39 false
40 fi &&
41 cd .. &&
42 echo a >a &&
43 echo z >z &&
44 git add a init z &&
45 git commit -m "super commit 1" &&
46 mv init .subrepo &&
47 GIT_CONFIG=.gitmodules git config submodule.example.url git://example.com/init.git
48'
49
50test_expect_success 'Prepare submodule add testing' '
51 submodurl=$(pwd)
52 (
53 mkdir addtest &&
54 cd addtest &&
55 git init
56 )
57'
58
59test_expect_success 'submodule add' '
60 (
61 cd addtest &&
62 git submodule add "$submodurl" submod &&
63 git submodule init
64 )
65'
66
67test_expect_success 'submodule add --branch' '
68 (
69 cd addtest &&
70 git submodule add -b initial "$submodurl" submod-branch &&
71 git submodule init &&
72 cd submod-branch &&
73 git branch | grep initial
74 )
75'
76
77test_expect_success 'submodule add with ./ in path' '
78 (
79 cd addtest &&
80 git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
81 git submodule init
82 )
83'
84
85test_expect_success 'submodule add with // in path' '
86 (
87 cd addtest &&
88 git submodule add "$submodurl" slashslashsubmod///frotz// &&
89 git submodule init
90 )
91'
92
93test_expect_success 'submodule add with /.. in path' '
94 (
95 cd addtest &&
96 git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
97 git submodule init
98 )
99'
100
101test_expect_success 'submodule add with ./, /.. and // in path' '
102 (
103 cd addtest &&
104 git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
105 git submodule init
106 )
107'
108
109test_expect_success 'status should fail for unmapped paths' '
110 if git submodule status
111 then
112 echo "[OOPS] submodule status succeeded"
113 false
114 elif ! GIT_CONFIG=.gitmodules git config submodule.example.path init
115 then
116 echo "[OOPS] git config failed to update .gitmodules"
117 false
118 fi
119'
120
121test_expect_success 'status should only print one line' '
122 lines=$(git submodule status | wc -l) &&
123 test $lines = 1
124'
125
126test_expect_success 'status should initially be "missing"' '
127 git submodule status | grep "^-$rev1"
128'
129
130test_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"
134 then
135 echo "[OOPS] init succeeded but submodule url is wrong"
136 false
137 elif test_must_fail git config submodule.example.url ./.subrepo
138 then
139 echo "[OOPS] init succeeded but update of url failed"
140 false
141 fi
142'
143
144test_expect_success 'update should fail when path is used by a file' '
145 echo "hello" >init &&
146 if git submodule update
147 then
148 echo "[OOPS] update should have failed"
149 false
150 elif test "$(cat init)" != "hello"
151 then
152 echo "[OOPS] update failed but init file was molested"
153 false
154 else
155 rm init
156 fi
157'
158
159test_expect_success 'update should fail when path is used by a nonempty directory' '
160 mkdir init &&
161 echo "hello" >init/a &&
162 if git submodule update
163 then
164 echo "[OOPS] update should have failed"
165 false
166 elif test "$(cat init/a)" != "hello"
167 then
168 echo "[OOPS] update failed but init/a was molested"
169 false
170 else
171 rm init/a
172 fi
173'
174
175test_expect_success 'update should work when path is an empty dir' '
176 rm -rf init &&
177 mkdir init &&
178 git submodule update &&
179 head=$(cd init && git rev-parse HEAD) &&
180 if test -z "$head"
181 then
182 echo "[OOPS] Failed to obtain submodule head"
183 false
184 elif test "$head" != "$rev1"
185 then
186 echo "[OOPS] Submodule head is $head but should have been $rev1"
187 false
188 fi
189'
190
191test_expect_success 'status should be "up-to-date" after update' '
192 git submodule status | grep "^ $rev1"
193'
194
195test_expect_success 'status should be "modified" after submodule commit' '
196 cd init &&
197 echo b >b &&
198 git add b &&
199 git commit -m "submodule commit 2" &&
200 rev2=$(git rev-parse HEAD) &&
201 cd .. &&
202 if test -z "$rev2"
203 then
204 echo "[OOPS] submodule git rev-parse returned nothing"
205 false
206 fi &&
207 git submodule status | grep "^+$rev2"
208'
209
210test_expect_success 'the --cached sha1 should be rev1' '
211 git submodule --cached status | grep "^+$rev1"
212'
213
214test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
215 git diff | grep "^+Subproject commit $rev2"
216'
217
218test_expect_success 'update should checkout rev1' '
219 git submodule update init &&
220 head=$(cd init && git rev-parse HEAD) &&
221 if test -z "$head"
222 then
223 echo "[OOPS] submodule git rev-parse returned nothing"
224 false
225 elif test "$head" != "$rev1"
226 then
227 echo "[OOPS] init did not checkout correct head"
228 false
229 fi
230'
231
232test_expect_success 'status should be "up-to-date" after update' '
233 git submodule status | grep "^ $rev1"
234'
235
236test_expect_success 'checkout superproject with subproject already present' '
237 git checkout initial &&
238 git checkout master
239'
240
241test_expect_success 'apply submodule diff' '
242 git branch second &&
243 (
244 cd init &&
245 echo s >s &&
246 git add s &&
247 git commit -m "change subproject"
248 ) &&
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) &&
255 test -z "$D"
256'
257
258test_expect_success 'update --init' '
259
260 mv init init2 &&
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 &&
267 test -d init/.git
268
269'
270
271test_expect_success 'do not add files from a submodule' '
272
273 git reset --hard &&
274 test_must_fail git add init/a
275
276'
277
278test_expect_success 'gracefully add submodule with a trailing slash' '
279
280 git reset --hard &&
281 git commit -m "commit subproject" init &&
282 (cd init &&
283 echo b > a) &&
284 git add init/ &&
285 git diff --exit-code --cached init &&
286 commit=$(cd init &&
287 git commit -m update a >/dev/null &&
288 git rev-parse HEAD) &&
289 git add init/ &&
290 test_must_fail git diff --exit-code --cached init &&
291 test $commit = $(git ls-files --stage |
292 sed -n "s/^160000 \([^ ]*\).*/\1/p")
293
294'
295
296test_expect_success 'ls-files gracefully handles trailing slash' '
297
298 test "init" = "$(git ls-files init/)"
299
300'
301
302test_expect_success 'submodule <invalid-path> warns' '
303
304 git submodule no-such-submodule 2> output.err &&
305 grep "^error: .*no-such-submodule" output.err
306
307'
308
309test_expect_success 'add submodules without specifying an explicit path' '
310 mkdir repo &&
311 cd repo &&
312 git init &&
313 echo r >r &&
314 git add r &&
315 git commit -m "repo commit 1" &&
316 cd .. &&
317 git clone --bare repo/ bare.git &&
318 cd addtest &&
319 git submodule add "$submodurl/repo" &&
320 git config -f .gitmodules submodule.repo.path repo &&
321 git submodule add "$submodurl/bare.git" &&
322 git config -f .gitmodules submodule.bare.path bare
323'
324
325test_done