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 public void apply(DirCacheEntry ent) {
99 ent.setFileMode(FileMode.GITLINK);
100 ent.setObjectId(id);
101 }
102 });
103 editor.commit();
104
105 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
106 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
107 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
108 ConfigConstants.CONFIG_KEY_PATH, path);
109 String url = "git://server/repo.git";
110 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
111 ConfigConstants.CONFIG_KEY_URL, url);
112 modulesConfig.save();
113
114 Repository subRepo = Git.cloneRepository()
115 .setURI(db.getDirectory().toURI().toString())
116 .setDirectory(new File(db.getWorkTree(), path)).call()
117 .getRepository();
118 addRepoToClose(subRepo);
119 assertNotNull(subRepo);
120
121 SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
122 assertTrue(generator.next());
123 assertNull(generator.getConfigUrl());
124 assertEquals(url, generator.getModulesUrl());
125
126 SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
127 Map<String, String> synced = command.call();
128 assertNotNull(synced);
129 assertEquals(1, synced.size());
130 Entry<String, String> module = synced.entrySet().iterator().next();
131 assertEquals(path, module.getKey());
132 assertEquals(url, module.getValue());
133
134 generator = SubmoduleWalk.forIndex(db);
135 assertTrue(generator.next());
136 assertEquals(url, generator.getConfigUrl());
137 Repository subModRepository = generator.getRepository();
138 StoredConfig submoduleConfig = subModRepository.getConfig();
139 subModRepository.close();
140 assertEquals(url, submoduleConfig.getString(
141 ConfigConstants.CONFIG_REMOTE_SECTION,
142 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
143 }
144
145 @Test
146 public void repositoryWithRelativeUriSubmodule() throws Exception {
147 writeTrashFile("file.txt", "content");
148 Git git = Git.wrap(db);
149 git.add().addFilepattern("file.txt").call();
150 git.commit().setMessage("create file").call();
151
152 final ObjectId id = ObjectId
153 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
154 final String path = "sub";
155 DirCache cache = db.lockDirCache();
156 DirCacheEditor editor = cache.editor();
157 editor.add(new PathEdit(path) {
158
159 public void apply(DirCacheEntry ent) {
160 ent.setFileMode(FileMode.GITLINK);
161 ent.setObjectId(id);
162 }
163 });
164 editor.commit();
165
166 String base = "git://server/repo.git";
167 FileBasedConfig config = db.getConfig();
168 config.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
169 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL,
170 base);
171 config.save();
172
173 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
174 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
175 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
176 ConfigConstants.CONFIG_KEY_PATH, path);
177 String current = "git://server/repo.git";
178 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
179 ConfigConstants.CONFIG_KEY_URL, current);
180 modulesConfig.save();
181
182 Repository subRepo = Git.cloneRepository()
183 .setURI(db.getDirectory().toURI().toString())
184 .setDirectory(new File(db.getWorkTree(), path)).call()
185 .getRepository();
186 assertNotNull(subRepo);
187 addRepoToClose(subRepo);
188
189 SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
190 assertTrue(generator.next());
191 assertNull(generator.getConfigUrl());
192 assertEquals(current, generator.getModulesUrl());
193
194 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
195 ConfigConstants.CONFIG_KEY_URL, "../sub.git");
196 modulesConfig.save();
197
198 SubmoduleSyncCommand command = new SubmoduleSyncCommand(db);
199 Map<String, String> synced = command.call();
200 assertNotNull(synced);
201 assertEquals(1, synced.size());
202 Entry<String, String> module = synced.entrySet().iterator().next();
203 assertEquals(path, module.getKey());
204 assertEquals("git://server/sub.git", module.getValue());
205
206 generator = SubmoduleWalk.forIndex(db);
207 assertTrue(generator.next());
208 assertEquals("git://server/sub.git", generator.getConfigUrl());
209 Repository subModRepository1 = generator.getRepository();
210 StoredConfig submoduleConfig = subModRepository1.getConfig();
211 subModRepository1.close();
212 assertEquals("git://server/sub.git", submoduleConfig.getString(
213 ConfigConstants.CONFIG_REMOTE_SECTION,
214 Constants.DEFAULT_REMOTE_NAME, ConfigConstants.CONFIG_KEY_URL));
215 }
216 }