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 @Override
97 public void apply(DirCacheEntry ent) {
98 ent.setFileMode(FileMode.GITLINK);
99 ent.setObjectId(commit);
100 }
101 });
102 editor.commit();
103
104 StoredConfig config = db.getConfig();
105 config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
106 ConfigConstants.CONFIG_KEY_URL, db.getDirectory().toURI()
107 .toString());
108 config.save();
109
110 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
111 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
112 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
113 ConfigConstants.CONFIG_KEY_PATH, path);
114 modulesConfig.save();
115
116 SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
117 Collection<String> updated = command.call();
118 assertNotNull(updated);
119 assertEquals(1, updated.size());
120 assertEquals(path, updated.iterator().next());
121
122 SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
123 assertTrue(generator.next());
124 try (Repository subRepo = generator.getRepository()) {
125 assertNotNull(subRepo);
126 assertEquals(commit, subRepo.resolve(Constants.HEAD));
127 }
128 }
129
130 @Test
131 public void repositoryWithUnconfiguredSubmodule() throws IOException,
132 GitAPIException {
133 final ObjectId id = ObjectId
134 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
135 final String path = "sub";
136 DirCache cache = db.lockDirCache();
137 DirCacheEditor editor = cache.editor();
138 editor.add(new PathEdit(path) {
139
140 @Override
141 public void apply(DirCacheEntry ent) {
142 ent.setFileMode(FileMode.GITLINK);
143 ent.setObjectId(id);
144 }
145 });
146 editor.commit();
147
148 FileBasedConfig modulesConfig = new FileBasedConfig(new File(
149 db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
150 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
151 ConfigConstants.CONFIG_KEY_PATH, path);
152 String url = "git://server/repo.git";
153 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
154 ConfigConstants.CONFIG_KEY_URL, url);
155 String update = "rebase";
156 modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
157 ConfigConstants.CONFIG_KEY_UPDATE, update);
158 modulesConfig.save();
159
160 SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
161 Collection<String> updated = command.call();
162 assertNotNull(updated);
163 assertTrue(updated.isEmpty());
164 }
165
166 @Test
167 public void repositoryWithInitializedSubmodule() throws IOException,
168 GitAPIException {
169 final ObjectId id = ObjectId
170 .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
171 final String path = "sub";
172 DirCache cache = db.lockDirCache();
173 DirCacheEditor editor = cache.editor();
174 editor.add(new PathEdit(path) {
175
176 @Override
177 public void apply(DirCacheEntry ent) {
178 ent.setFileMode(FileMode.GITLINK);
179 ent.setObjectId(id);
180 }
181 });
182 editor.commit();
183
184 Repository subRepo = Git.init().setBare(false)
185 .setDirectory(new File(db.getWorkTree(), path)).call()
186 .getRepository();
187 assertNotNull(subRepo);
188
189 SubmoduleUpdateCommand command = new SubmoduleUpdateCommand(db);
190 Collection<String> updated = command.call();
191 assertNotNull(updated);
192 assertTrue(updated.isEmpty());
193 }
194 }