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