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
49 import java.io.File;
50 import java.io.IOException;
51 import java.util.Collection;
52
53 import org.eclipse.jgit.api.Git;
54 import org.eclipse.jgit.api.SubmoduleUpdateCommand;
55 import org.eclipse.jgit.api.errors.GitAPIException;
56 import org.eclipse.jgit.dircache.DirCache;
57 import org.eclipse.jgit.dircache.DirCacheEditor;
58 import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
59 import org.eclipse.jgit.dircache.DirCacheEntry;
60 import org.eclipse.jgit.junit.RepositoryTestCase;
61 import org.eclipse.jgit.lib.ConfigConstants;
62 import org.eclipse.jgit.lib.Constants;
63 import org.eclipse.jgit.lib.FileMode;
64 import org.eclipse.jgit.lib.ObjectId;
65 import org.eclipse.jgit.lib.Repository;
66 import org.eclipse.jgit.lib.StoredConfig;
67 import org.eclipse.jgit.revwalk.RevCommit;
68 import org.eclipse.jgit.storage.file.FileBasedConfig;
69 import org.junit.Test;
70
71
72
73
74 public class SubmoduleUpdateTest extends RepositoryTestCase {
75
76 @Test
77 public void repositoryWithNoSubmodules() throws GitAPIException {
78 SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
79 Collection<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 final RevCommit commit = git.commit().setMessage("create file").call();
90
91 final String path = "sub";
92 DirCache cache = db.lockDirCache();
93 DirCacheEditor editor = cache.editor();
94 editor.add(new PathEdit(path) {
95
96 public void apply(DirCacheEntry ent) {
97 ent.setFileMode(FileMode.GITLINK);
98 ent.setObjectId(commit);
99 }
100 });
101 editor.commit();
102
103 StoredConfig config = db.getConfig();
104 config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
105 ConfigConstants.CONFIG_KEY_URL, db.getDirectory().toURI()
106 .toString());
107 config.save();
108
109 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
110 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
111 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
112 ConfigConstants.CONFIG_KEY_PATH, path);
113 modulesConfig.save();
114
115 SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
116 Collection<String> updated = command.call();
117 assertNotNull(updated);
118 assertEquals(1, updated.size());
119 assertEquals(path, updated.iterator().next());
120
121 SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
122 assertTrue(generator.next());
123 Repository subRepo = generator.getRepository();
124 assertNotNull(subRepo);
125 assertEquals(commit, subRepo.resolve(Constants.HEAD));
126 subRepo.close();
127 }
128
129 @Test
130 public void repositoryWithUnconfiguredSubmodule() throws IOException,
131 GitAPIException {
132 final ObjectId id = ObjectId
133 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
134 final String path = "sub";
135 DirCache cache = db.lockDirCache();
136 DirCacheEditor editor = cache.editor();
137 editor.add(new PathEdit(path) {
138
139 public void apply(DirCacheEntry ent) {
140 ent.setFileMode(FileMode.GITLINK);
141 ent.setObjectId(id);
142 }
143 });
144 editor.commit();
145
146 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
147 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
148 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
149 ConfigConstants.CONFIG_KEY_PATH, path);
150 String url = "git://server/repo.git";
151 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
152 ConfigConstants.CONFIG_KEY_URL, url);
153 String update = "rebase";
154 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
155 ConfigConstants.CONFIG_KEY_UPDATE, update);
156 modulesConfig.save();
157
158 SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
159 Collection<String> updated = command.call();
160 assertNotNull(updated);
161 assertTrue(updated.isEmpty());
162 }
163
164 @Test
165 public void repositoryWithInitializedSubmodule() throws IOException,
166 GitAPIException {
167 final ObjectId id = ObjectId
168 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
169 final String path = "sub";
170 DirCache cache = db.lockDirCache();
171 DirCacheEditor editor = cache.editor();
172 editor.add(new PathEdit(path) {
173
174 public void apply(DirCacheEntry ent) {
175 ent.setFileMode(FileMode.GITLINK);
176 ent.setObjectId(id);
177 }
178 });
179 editor.commit();
180
181 Repository subRepo = Git.init().setBare(false)
182 .setDirectory(new File(db.getWorkTree(), path)).call()
183 .getRepository();
184 assertNotNull(subRepo);
185
186 SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
187 Collection<String> updated = command.call();
188 assertNotNull(updated);
189 assertTrue(updated.isEmpty());
190 }
191 }