1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.submodule;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Collection;
21
22 import org.eclipse.jgit.api.SubmoduleInitCommand;
23 import org.eclipse.jgit.api.errors.GitAPIException;
24 import org.eclipse.jgit.api.errors.JGitInternalException;
25 import org.eclipse.jgit.dircache.DirCache;
26 import org.eclipse.jgit.dircache.DirCacheEditor;
27 import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
28 import org.eclipse.jgit.dircache.DirCacheEntry;
29 import org.eclipse.jgit.errors.ConfigInvalidException;
30 import org.eclipse.jgit.junit.RepositoryTestCase;
31 import org.eclipse.jgit.lib.ConfigConstants;
32 import org.eclipse.jgit.lib.Constants;
33 import org.eclipse.jgit.lib.FileMode;
34 import org.eclipse.jgit.lib.ObjectId;
35 import org.eclipse.jgit.storage.file.FileBasedConfig;
36 import org.junit.Test;
37
38
39
40
41 public class SubmoduleInitTest extends RepositoryTestCase {
42
43 @Test
44 public void repositoryWithNoSubmodules() throws GitAPIException {
45 SubmoduleInitCommand command = new SubmoduleInitCommand(db);
46 Collection<String> modules = command.call();
47 assertNotNull(modules);
48 assertTrue(modules.isEmpty());
49 }
50
51 @Test
52 public void repositoryWithUninitializedModule() throws IOException,
53 ConfigInvalidException, GitAPIException {
54 final String path = addSubmoduleToIndex();
55
56 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
57 assertTrue(generator.next());
58 assertNull(generator.getConfigUrl());
59 assertNull(generator.getConfigUpdate());
60 }
61 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
62 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
63 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
64 ConfigConstants.CONFIG_KEY_PATH, path);
65 String url = "git://server/repo.git";
66 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
67 ConfigConstants.CONFIG_KEY_URL, url);
68 String update = "rebase";
69 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
70 ConfigConstants.CONFIG_KEY_UPDATE, update);
71 modulesConfig.save();
72
73 SubmoduleInitCommand command = new SubmoduleInitCommand(db);
74 Collection<String> modules = command.call();
75 assertNotNull(modules);
76 assertEquals(1, modules.size());
77 assertEquals(path, modules.iterator().next());
78
79 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
80 assertTrue(generator.next());
81 assertEquals(url, generator.getConfigUrl());
82 assertEquals(update, generator.getConfigUpdate());
83 }
84 }
85
86 @Test
87 public void resolveSameLevelRelativeUrl() throws Exception {
88 final String path = addSubmoduleToIndex();
89
90 String base = "git://server/repo.git";
91 FileBasedConfig config = db.getConfig();
92 config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
93 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
94 base);
95 config.save();
96
97 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
98 assertTrue(generator.next());
99 assertNull(generator.getConfigUrl());
100 assertNull(generator.getConfigUpdate());
101 }
102 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
103 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
104 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
105 ConfigConstants.CONFIG_KEY_PATH, path);
106 String url = "./sub.git";
107 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
108 ConfigConstants.CONFIG_KEY_URL, url);
109 String update = "rebase";
110 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
111 ConfigConstants.CONFIG_KEY_UPDATE, update);
112 modulesConfig.save();
113
114 SubmoduleInitCommand command = new SubmoduleInitCommand(db);
115 Collection<String> modules = command.call();
116 assertNotNull(modules);
117 assertEquals(1, modules.size());
118 assertEquals(path, modules.iterator().next());
119
120 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
121 assertTrue(generator.next());
122 assertEquals("git://server/repo.git/sub.git",
123 generator.getConfigUrl());
124 assertEquals(update, generator.getConfigUpdate());
125 }
126 }
127
128 @Test
129 public void resolveOneLevelHigherRelativeUrl() throws IOException,
130 ConfigInvalidException, GitAPIException {
131 final String path = addSubmoduleToIndex();
132
133 String base = "git://server/repo.git";
134 FileBasedConfig config = db.getConfig();
135 config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
136 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
137 base);
138 config.save();
139
140 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
141 assertTrue(generator.next());
142 assertNull(generator.getConfigUrl());
143 assertNull(generator.getConfigUpdate());
144 }
145 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
146 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
147 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
148 ConfigConstants.CONFIG_KEY_PATH, path);
149 String url = "../sub.git";
150 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
151 ConfigConstants.CONFIG_KEY_URL, url);
152 String update = "rebase";
153 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
154 ConfigConstants.CONFIG_KEY_UPDATE, update);
155 modulesConfig.save();
156
157 SubmoduleInitCommand command = new SubmoduleInitCommand(db);
158 Collection<String> modules = command.call();
159 assertNotNull(modules);
160 assertEquals(1, modules.size());
161 assertEquals(path, modules.iterator().next());
162
163 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
164 assertTrue(generator.next());
165 assertEquals("git://server/sub.git", generator.getConfigUrl());
166 assertEquals(update, generator.getConfigUpdate());
167 }
168 }
169
170 @Test
171 public void resolveTwoLevelHigherRelativeUrl() throws IOException,
172 ConfigInvalidException, GitAPIException {
173 final String path = addSubmoduleToIndex();
174
175 String base = "git://server/repo.git";
176 FileBasedConfig config = db.getConfig();
177 config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
178 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
179 base);
180 config.save();
181
182 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
183 assertTrue(generator.next());
184 assertNull(generator.getConfigUrl());
185 assertNull(generator.getConfigUpdate());
186 }
187 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
188 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
189 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
190 ConfigConstants.CONFIG_KEY_PATH, path);
191 String url = "../../server2/sub.git";
192 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
193 ConfigConstants.CONFIG_KEY_URL, url);
194 String update = "rebase";
195 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
196 ConfigConstants.CONFIG_KEY_UPDATE, update);
197 modulesConfig.save();
198
199 SubmoduleInitCommand command = new SubmoduleInitCommand(db);
200 Collection<String> modules = command.call();
201 assertNotNull(modules);
202 assertEquals(1, modules.size());
203 assertEquals(path, modules.iterator().next());
204
205 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
206 assertTrue(generator.next());
207 assertEquals("git://server2/sub.git", generator.getConfigUrl());
208 assertEquals(update, generator.getConfigUpdate());
209 }
210 }
211
212 @Test
213 public void resolveWorkingDirectoryRelativeUrl() throws IOException,
214 GitAPIException, ConfigInvalidException {
215 final String path = addSubmoduleToIndex();
216
217 String base = db.getWorkTree().getAbsolutePath();
218 if (File.separatorChar == '\\')
219 base = base.replace('\\', '/');
220 FileBasedConfig config = db.getConfig();
221 config.unset(ConfigConstants.CONFIG_REMOTE_SECTION,
222 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL);
223 config.save();
224
225 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
226 assertTrue(generator.next());
227 assertNull(generator.getConfigUrl());
228 assertNull(generator.getConfigUpdate());
229 }
230 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
231 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
232 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
233 ConfigConstants.CONFIG_KEY_PATH, path);
234 String url = "./sub.git";
235 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
236 ConfigConstants.CONFIG_KEY_URL, url);
237 String update = "rebase";
238 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
239 ConfigConstants.CONFIG_KEY_UPDATE, update);
240 modulesConfig.save();
241
242 SubmoduleInitCommand command = new SubmoduleInitCommand(db);
243 Collection<String> modules = command.call();
244 assertNotNull(modules);
245 assertEquals(1, modules.size());
246 assertEquals(path, modules.iterator().next());
247
248 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
249 assertTrue(generator.next());
250 assertEquals(base + "/sub.git", generator.getConfigUrl());
251 assertEquals(update, generator.getConfigUpdate());
252 }
253 }
254
255 @Test
256 public void resolveInvalidParentUrl() throws IOException,
257 ConfigInvalidException, GitAPIException {
258 final String path = addSubmoduleToIndex();
259
260 String base = "no_slash";
261 FileBasedConfig config = db.getConfig();
262 config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
263 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
264 base);
265 config.save();
266
267 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(db)) {
268 assertTrue(generator.next());
269 assertNull(generator.getConfigUrl());
270 assertNull(generator.getConfigUpdate());
271 }
272 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
273 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
274 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
275 ConfigConstants.CONFIG_KEY_PATH, path);
276 String url = "../sub.git";
277 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
278 ConfigConstants.CONFIG_KEY_URL, url);
279 modulesConfig.save();
280
281 try {
282 new SubmoduleInitCommand(db).call();
283 fail("Exception not thrown");
284 } catch (JGitInternalException e) {
285 assertTrue(e.getCause() instanceof IOException);
286 }
287 }
288
289 private String addSubmoduleToIndex() throws IOException {
290 final ObjectId id = ObjectId
291 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
292 final String path = "sub";
293 DirCache cache = db.lockDirCache();
294 DirCacheEditor editor = cache.editor();
295 editor.add(new PathEdit(path) {
296
297 @Override
298 public void apply(DirCacheEntry ent) {
299 ent.setFileMode(FileMode.GITLINK);
300 ent.setObjectId(id);
301 }
302 });
303 editor.commit();
304 return path;
305 }
306 }