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