1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.submodule;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48 import static org.junit.Assert.fail;
49
50 import java.io.File;
51 import java.text.MessageFormat;
52
53 import org.eclipse.jgit.api.Git;
54 import org.eclipse.jgit.api.Status;
55 import org.eclipse.jgit.api.SubmoduleAddCommand;
56 import org.eclipse.jgit.api.errors.GitAPIException;
57 import org.eclipse.jgit.api.errors.JGitInternalException;
58 import org.eclipse.jgit.dircache.DirCache;
59 import org.eclipse.jgit.dircache.DirCacheEditor;
60 import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
61 import org.eclipse.jgit.dircache.DirCacheEntry;
62 import org.eclipse.jgit.internal.JGitText;
63 import org.eclipse.jgit.junit.RepositoryTestCase;
64 import org.eclipse.jgit.lib.ConfigConstants;
65 import org.eclipse.jgit.lib.Constants;
66 import org.eclipse.jgit.lib.FileMode;
67 import org.eclipse.jgit.lib.ObjectId;
68 import org.eclipse.jgit.lib.Repository;
69 import org.eclipse.jgit.revwalk.RevCommit;
70 import org.eclipse.jgit.storage.file.FileBasedConfig;
71 import org.junit.Test;
72
73
74
75
76 public class SubmoduleAddTest extends RepositoryTestCase {
77
78 @Test
79 public void commandWithNullPath() throws GitAPIException {
80 try {
81 new SubmoduleAddCommand(db).setURI("uri").call().close();
82 fail("Exception not thrown");
83 } catch (IllegalArgumentException e) {
84 assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
85 }
86 }
87
88 @Test
89 public void commandWithEmptyPath() throws GitAPIException {
90 try {
91 new SubmoduleAddCommand(db).setPath("").setURI("uri").call()
92 .close();
93 fail("Exception not thrown");
94 } catch (IllegalArgumentException e) {
95 assertEquals(JGitText.get().pathNotConfigured, e.getMessage());
96 }
97 }
98
99 @Test
100 public void commandWithNullUri() throws GitAPIException {
101 try {
102 new SubmoduleAddCommand(db).setPath("sub").call().close();
103 fail("Exception not thrown");
104 } catch (IllegalArgumentException e) {
105 assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
106 }
107 }
108
109 @Test
110 public void commandWithEmptyUri() throws GitAPIException {
111 try {
112 new SubmoduleAddCommand(db).setPath("sub").setURI("").call()
113 .close();
114 fail("Exception not thrown");
115 } catch (IllegalArgumentException e) {
116 assertEquals(JGitText.get().uriNotConfigured, e.getMessage());
117 }
118 }
119
120 @Test
121 public void addSubmodule() throws Exception {
122 try (Git git = new Git(db)) {
123 writeTrashFile("file.txt", "content");
124 git.add().addFilepattern("file.txt").call();
125 RevCommit commit = git.commit().setMessage("create file").call();
126
127 SubmoduleAddCommand command = new SubmoduleAddCommand(db);
128 String path = "sub";
129 command.setPath(path);
130 String uri = db.getDirectory().toURI().toString();
131 command.setURI(uri);
132 Repository repo = command.call();
133 assertNotNull(repo);
134 ObjectId subCommit = repo.resolve(Constants.HEAD);
135 repo.close();
136
137 SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
138 assertTrue(generator.next());
139 assertEquals(path, generator.getPath());
140 assertEquals(commit, generator.getObjectId());
141 assertEquals(uri, generator.getModulesUrl());
142 assertEquals(path, generator.getModulesPath());
143 assertEquals(uri, generator.getConfigUrl());
144 Repository subModRepo = generator.getRepository();
145 assertNotNull(subModRepo);
146 assertEquals(subCommit, commit);
147 subModRepo.close();
148
149 Status status = Git.wrap(db).status().call();
150 assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
151 assertTrue(status.getAdded().contains(path));
152 }
153 }
154
155 @Test
156 public void addExistentSubmodule() throws Exception {
157 final ObjectId id = ObjectId
158 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
159 final String path = "sub";
160 DirCache cache = db.lockDirCache();
161 DirCacheEditor editor = cache.editor();
162 editor.add(new PathEdit(path) {
163
164 public void apply(DirCacheEntry ent) {
165 ent.setFileMode(FileMode.GITLINK);
166 ent.setObjectId(id);
167 }
168 });
169 editor.commit();
170
171 SubmoduleAddCommand command = new SubmoduleAddCommand(db);
172 command.setPath(path);
173 command.setURI("git://server/repo.git");
174 try {
175 command.call().close();
176 fail("Exception not thrown");
177 } catch (JGitInternalException e) {
178 assertEquals(
179 MessageFormat.format(JGitText.get().submoduleExists, path),
180 e.getMessage());
181 }
182 }
183
184 @Test
185 public void addSubmoduleWithRelativeUri() throws Exception {
186 try (Git git = new Git(db)) {
187 writeTrashFile("file.txt", "content");
188 git.add().addFilepattern("file.txt").call();
189 RevCommit commit = git.commit().setMessage("create file").call();
190
191 SubmoduleAddCommand command = new SubmoduleAddCommand(db);
192 String path = "sub";
193 String uri = "./.git";
194 command.setPath(path);
195 command.setURI(uri);
196 Repository repo = command.call();
197 assertNotNull(repo);
198 addRepoToClose(repo);
199
200 SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
201 assertTrue(generator.next());
202 assertEquals(path, generator.getPath());
203 assertEquals(commit, generator.getObjectId());
204 assertEquals(uri, generator.getModulesUrl());
205 assertEquals(path, generator.getModulesPath());
206 String fullUri = db.getDirectory().getAbsolutePath();
207 if (File.separatorChar == '\\') {
208 fullUri = fullUri.replace('\\', '/');
209 }
210 assertEquals(fullUri, generator.getConfigUrl());
211 Repository subModRepo = generator.getRepository();
212 assertNotNull(subModRepo);
213 assertEquals(
214 fullUri,
215 subModRepo
216 .getConfig()
217 .getString(ConfigConstants.CONFIG_REMOTE_SECTION,
218 Constants.DEFAULT_REMOTE_NAME,
219 ConfigConstants.CONFIG_KEY_URL));
220 subModRepo.close();
221 assertEquals(commit, repo.resolve(Constants.HEAD));
222
223 Status status = Git.wrap(db).status().call();
224 assertTrue(status.getAdded().contains(Constants.DOT_GIT_MODULES));
225 assertTrue(status.getAdded().contains(path));
226 }
227 }
228
229 @Test
230 public void addSubmoduleWithExistingSubmoduleDefined() throws Exception {
231 String path1 = "sub1";
232 String url1 = "git://server/repo1.git";
233 String path2 = "sub2";
234
235 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
236 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
237 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
238 path1, ConfigConstants.CONFIG_KEY_PATH, path1);
239 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
240 path1, ConfigConstants.CONFIG_KEY_URL, url1);
241 modulesConfig.save();
242
243 try (Git git = new Git(db)) {
244 writeTrashFile("file.txt", "content");
245 git.add().addFilepattern("file.txt").call();
246 assertNotNull(git.commit().setMessage("create file").call());
247
248 SubmoduleAddCommand command = new SubmoduleAddCommand(db);
249 command.setPath(path2);
250 String url2 = db.getDirectory().toURI().toString();
251 command.setURI(url2);
252 Repository r = command.call();
253 assertNotNull(r);
254 addRepoToClose(r);
255
256 modulesConfig.load();
257 assertEquals(path1, modulesConfig.getString(
258 ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
259 ConfigConstants.CONFIG_KEY_PATH));
260 assertEquals(url1, modulesConfig.getString(
261 ConfigConstants.CONFIG_SUBMODULE_SECTION, path1,
262 ConfigConstants.CONFIG_KEY_URL));
263 assertEquals(path2, modulesConfig.getString(
264 ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
265 ConfigConstants.CONFIG_KEY_PATH));
266 assertEquals(url2, modulesConfig.getString(
267 ConfigConstants.CONFIG_SUBMODULE_SECTION, path2,
268 ConfigConstants.CONFIG_KEY_URL));
269 }
270 }
271 }