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.assertNull;
48 import static org.junit.Assert.assertTrue;
49
50 import java.io.File;
51 import java.util.Map;
52 import java.util.Map.Entry;
53
54 import org.eclipse.jgit.api.Git;
55 import org.eclipse.jgit.api.SubmoduleSyncCommand;
56 import org.eclipse.jgit.api.errors.GitAPIException;
57 import org.eclipse.jgit.dircache.DirCache;
58 import org.eclipse.jgit.dircache.DirCacheEditor;
59 import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
60 import org.eclipse.jgit.dircache.DirCacheEntry;
61 import org.eclipse.jgit.junit.RepositoryTestCase;
62 import org.eclipse.jgit.lib.ConfigConstants;
63 import org.eclipse.jgit.lib.Constants;
64 import org.eclipse.jgit.lib.FileMode;
65 import org.eclipse.jgit.lib.ObjectId;
66 import org.eclipse.jgit.lib.Repository;
67 import org.eclipse.jgit.lib.StoredConfig;
68 import org.eclipse.jgit.storage.file.FileBasedConfig;
69 import org.junit.Test;
70
71
72
73
74 public class SubmoduleSyncTest extends RepositoryTestCase {
75
76 @Test
77 public void repositoryWithNoSubmodules() throws GitAPIException {
78 SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
79 Map<String, String> modules = command.call();
80 assertNotNull(modules);
81 assertTrue(modules.isEmpty());
82 }
83
84 @Test
85 public void repositoryWithSubmodule() throws Exception {
86 writeTrashFile("file.txt", "content");
87 Git git = Git.wrap(db);
88 git.add().addFilepattern("file.txt").call();
89 git.commit().setMessage("create file").call();
90
91 final ObjectId id = ObjectId
92 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
93 final String path = "sub";
94 DirCache cache = db.lockDirCache();
95 DirCacheEditor editor = cache.editor();
96 editor.add(new PathEdit(path) {
97
98 @Override
99 public void apply(DirCacheEntry ent) {
100 ent.setFileMode(FileMode.GITLINK);
101 ent.setObjectId(id);
102 }
103 });
104 editor.commit();
105
106 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
107 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
108 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
109 ConfigConstants.CONFIG_KEY_PATH, path);
110 String url = "git://server/repo.git";
111 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
112 ConfigConstants.CONFIG_KEY_URL, url);
113 modulesConfig.save();
114
115 Repository subRepo = Git.cloneRepository()
116 .setURI(db.getDirectory().toURI().toString())
117 .setDirectory(new File(db.getWorkTree(), path)).call()
118 .getRepository();
119 addRepoToClose(subRepo);
120 assertNotNull(subRepo);
121
122 SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
123 assertTrue(generator.next());
124 assertNull(generator.getConfigUrl());
125 assertEquals(url, generator.getModulesUrl());
126
127 SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
128 Map<String, String> synced = command.call();
129 assertNotNull(synced);
130 assertEquals(1, synced.size());
131 Entry<String, String> module = synced.entrySet().iterator().next();
132 assertEquals(path, module.getKey());
133 assertEquals(url, module.getValue());
134
135 generator = SubmoduleWalk.forIndex(db);
136 assertTrue(generator.next());
137 assertEquals(url, generator.getConfigUrl());
138 try (Repository subModRepository = generator.getRepository()) {
139 StoredConfig submoduleConfig = subModRepository.getConfig();
140 assertEquals(url,
141 submoduleConfig.getString(
142 ConfigConstants.CONFIG_REMOTE_SECTION,
143 Constants.DEFAULT_REMOTE_NAME,
144 ConfigConstants.CONFIG_KEY_URL));
145 }
146 }
147
148 @Test
149 public void repositoryWithRelativeUriSubmodule() throws Exception {
150 writeTrashFile("file.txt", "content");
151 Git git = Git.wrap(db);
152 git.add().addFilepattern("file.txt").call();
153 git.commit().setMessage("create file").call();
154
155 final ObjectId id = ObjectId
156 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
157 final String path = "sub";
158 DirCache cache = db.lockDirCache();
159 DirCacheEditor editor = cache.editor();
160 editor.add(new PathEdit(path) {
161
162 @Override
163 public void apply(DirCacheEntry ent) {
164 ent.setFileMode(FileMode.GITLINK);
165 ent.setObjectId(id);
166 }
167 });
168 editor.commit();
169
170 String base = "git://server/repo.git";
171 FileBasedConfig config = db.getConfig();
172 config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
173 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
174 base);
175 config.save();
176
177 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
178 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
179 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
180 ConfigConstants.CONFIG_KEY_PATH, path);
181 String current = "git://server/repo.git";
182 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
183 ConfigConstants.CONFIG_KEY_URL, current);
184 modulesConfig.save();
185
186 Repository subRepo = Git.cloneRepository()
187 .setURI(db.getDirectory().toURI().toString())
188 .setDirectory(new File(db.getWorkTree(), path)).call()
189 .getRepository();
190 assertNotNull(subRepo);
191 addRepoToClose(subRepo);
192
193 SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
194 assertTrue(generator.next());
195 assertNull(generator.getConfigUrl());
196 assertEquals(current, generator.getModulesUrl());
197
198 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
199 ConfigConstants.CONFIG_KEY_URL, "../sub.git");
200 modulesConfig.save();
201
202 SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
203 Map<String, String> synced = command.call();
204 assertNotNull(synced);
205 assertEquals(1, synced.size());
206 Entry<String, String> module = synced.entrySet().iterator().next();
207 assertEquals(path, module.getKey());
208 assertEquals("git://server/sub.git", module.getValue());
209
210 generator = SubmoduleWalk.forIndex(db);
211 assertTrue(generator.next());
212 assertEquals("git://server/sub.git", generator.getConfigUrl());
213 try (Repository subModRepository1 = generator.getRepository()) {
214 StoredConfig submoduleConfig = subModRepository1.getConfig();
215 assertEquals("git://server/sub.git",
216 submoduleConfig.getString(
217 ConfigConstants.CONFIG_REMOTE_SECTION,
218 Constants.DEFAULT_REMOTE_NAME,
219 ConfigConstants.CONFIG_KEY_URL));
220 }
221 }
222 }