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 'status should fail for unmapped paths' '
51 if git submodule status
52 then
53 echo "[OOPS] submodule status succeeded"
54 false
55 elif ! GIT_CONFIG=.gitmodules git config submodule.example.path init
56 then
57 echo "[OOPS] git config failed to update .gitmodules"
58 false
59 fi
60'
61
62test_expect_success 'status should only print one line' '
63 lines=$(git submodule status | wc -l) &&
64 test $lines = 1
65'
66
67test_expect_success 'status should initially be "missing"' '
68 git submodule status | grep "^-$rev1"
69'
70
71test_expect_success 'init should register submodule url in .git/config' '
72 git submodule init &&
73 url=$(git config submodule.example.url) &&
74 if test "$url" != "git://example.com/init.git"
75 then
76 echo "[OOPS] init succeeded but submodule url is wrong"
77 false
78 elif test_must_fail git config submodule.example.url ./.subrepo
79 then
80 echo "[OOPS] init succeeded but update of url failed"
81 false
82 fi
83'
84
85test_expect_success 'update should fail when path is used by a file' '
86 echo "hello" >init &&
87 if git submodule update
88 then
89 echo "[OOPS] update should have failed"
90 false
91 elif test "$(cat init)" != "hello"
92 then
93 echo "[OOPS] update failed but init file was molested"
94 false
95 else
96 rm init
97 fi
98'
99
100test_expect_success 'update should fail when path is used by a nonempty directory' '
101 mkdir init &&
102 echo "hello" >init/a &&
103 if git submodule update
104 then
105 echo "[OOPS] update should have failed"
106 false
107 elif test "$(cat init/a)" != "hello"
108 then
109 echo "[OOPS] update failed but init/a was molested"
110 false
111 else
112 rm init/a
113 fi
114'
115
116test_expect_success 'update should work when path is an empty dir' '
117 rm -rf init &&
118 mkdir init &&
119 git submodule update &&
120 head=$(cd init && git rev-parse HEAD) &&
121 if test -z "$head"
122 then
123 echo "[OOPS] Failed to obtain submodule head"
124 false
125 elif test "$head" != "$rev1"
126 then
127 echo "[OOPS] Submodule head is $head but should have been $rev1"
128 false
129 fi
130'
131
132test_expect_success 'status should be "up-to-date" after update' '
133 git submodule status | grep "^ $rev1"
134'
135
136test_expect_success 'status should be "modified" after submodule commit' '
137 cd init &&
138 echo b >b &&
139 git add b &&
140 git commit -m "submodule commit 2" &&
141 rev2=$(git rev-parse HEAD) &&
142 cd .. &&
143 if test -z "$rev2"
144 then
145 echo "[OOPS] submodule git rev-parse returned nothing"
146 false
147 fi &&
148 git submodule status | grep "^+$rev2"
149'
150
151test_expect_success 'the --cached sha1 should be rev1' '
152 git submodule --cached status | grep "^+$rev1"
153'
154
155test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
156 git diff | grep "^+Subproject commit $rev2"
157'
158
159test_expect_success 'update should checkout rev1' '
160 git submodule update init &&
161 head=$(cd init && git rev-parse HEAD) &&
162 if test -z "$head"
163 then
164 echo "[OOPS] submodule git rev-parse returned nothing"
165 false
166 elif test "$head" != "$rev1"
167 then
168 echo "[OOPS] init did not checkout correct head"
169 false
170 fi
171'
172
173test_expect_success 'status should be "up-to-date" after update' '
174 git submodule status | grep "^ $rev1"
175'
176
177test_expect_success 'checkout superproject with subproject already present' '
178 git checkout initial &&
179 git checkout master
180'
181
182test_expect_success 'apply submodule diff' '
183 git branch second &&
184 (
185 cd init &&
186 echo s >s &&
187 git add s &&
188 git commit -m "change subproject"
189 ) &&
190 git update-index --add init &&
191 git commit -m "change init" &&
192 git format-patch -1 --stdout >P.diff &&
193 git checkout second &&
194 git apply --index P.diff &&
195 D=$(git diff --cached master) &&
196 test -z "$D"
197'
198
199test_expect_success 'update --init' '
200
201 mv init init2 &&
202 git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
203 git config --remove-section submodule.example
204 git submodule update init > update.out &&
205 grep "not initialized" update.out &&
206 test ! -d init/.git &&
207 git submodule update --init init &&
208 test -d init/.git
209
210'
211
212test_expect_success 'do not add files from a submodule' '
213
214 git reset --hard &&
215 test_must_fail git add init/a
216
217'
218
219test_expect_success 'gracefully add submodule with a trailing slash' '
220
221 git reset --hard &&
222 git commit -m "commit subproject" init &&
223 (cd init &&
224 echo b > a) &&
225 git add init/ &&
226 git diff --exit-code --cached init &&
227 commit=$(cd init &&
228 git commit -m update a >/dev/null &&
229 git rev-parse HEAD) &&
230 git add init/ &&
231 test_must_fail git diff --exit-code --cached init &&
232 test $commit = $(git ls-files --stage |
233 sed -n "s/^160000 \([^ ]*\).*/\1/p")
234
235'
236
237test_expect_success 'ls-files gracefully handles trailing slash' '
238
239 test "init" = "$(git ls-files init/)"
240
241'
242
243test_expect_success 'submodule <invalid-path> warns' '
244
245 git submodule no-such-submodule 2> output.err &&
246 grep "^error: .*no-such-submodule" output.err
247
248'
249
250test_done