View Javadoc
1   /*
2    * Copyright (C) 2011, GitHub Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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.Map;
52  import java.util.Map.Entry;
53  
54  import org.eclipse.jgit.api.Git;
55  import org.eclipse.jgit.api.SubmoduleStatusCommand;
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.junit.TestRepository;
63  import org.eclipse.jgit.lib.ConfigConstants;
64  import org.eclipse.jgit.lib.Constants;
65  import org.eclipse.jgit.lib.FileMode;
66  import org.eclipse.jgit.lib.ObjectId;
67  import org.eclipse.jgit.lib.Repository;
68  import org.eclipse.jgit.lib.StoredConfig;
69  import org.eclipse.jgit.storage.file.FileBasedConfig;
70  import org.junit.Test;
71  
72  /**
73   * Unit tests of {@link SubmoduleStatusCommand}
74   */
75  public class SubmoduleStatusTest extends RepositoryTestCase {
76  
77  	@Test
78  	public void repositoryWithNoSubmodules() throws GitAPIException {
79  		SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
80  		Map<String, SubmoduleStatus> statuses = command.call();
81  		assertNotNull(statuses);
82  		assertTrue(statuses.isEmpty());
83  	}
84  
85  	@Test
86  	public void repositoryWithMissingSubmodule() throws IOException,
87  			GitAPIException {
88  		final ObjectId id = ObjectId
89  				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
90  		final String path = "sub";
91  		DirCache cache = db.lockDirCache();
92  		DirCacheEditor editor = cache.editor();
93  		editor.add(new PathEdit(path) {
94  
95  			@Override
96  			public void apply(DirCacheEntry ent) {
97  				ent.setFileMode(FileMode.GITLINK);
98  				ent.setObjectId(id);
99  			}
100 		});
101 		editor.commit();
102 
103 		SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
104 		Map<String, SubmoduleStatus> statuses = command.call();
105 		assertNotNull(statuses);
106 		assertEquals(1, statuses.size());
107 		Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
108 				.next();
109 		assertNotNull(module);
110 		assertEquals(path, module.getKey());
111 		SubmoduleStatus status = module.getValue();
112 		assertNotNull(status);
113 		assertEquals(path, status.getPath());
114 		assertEquals(id, status.getIndexId());
115 		assertEquals(SubmoduleStatusType.MISSING, status.getType());
116 	}
117 
118 	@Test
119 	public void repositoryWithUninitializedSubmodule() throws IOException,
120 			GitAPIException {
121 		final ObjectId id = ObjectId
122 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
123 		final String path = "sub";
124 		DirCache cache = db.lockDirCache();
125 		DirCacheEditor editor = cache.editor();
126 		editor.add(new PathEdit(path) {
127 
128 			@Override
129 			public void apply(DirCacheEntry ent) {
130 				ent.setFileMode(FileMode.GITLINK);
131 				ent.setObjectId(id);
132 			}
133 		});
134 		editor.commit();
135 
136 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
137 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
138 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
139 				ConfigConstants.CONFIG_KEY_PATH, path);
140 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
141 				ConfigConstants.CONFIG_KEY_URL, "git://server/repo.git");
142 		modulesConfig.save();
143 
144 		SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
145 		Map<String, SubmoduleStatus> statuses = command.call();
146 		assertNotNull(statuses);
147 		assertEquals(1, statuses.size());
148 		Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
149 				.next();
150 		assertNotNull(module);
151 		assertEquals(path, module.getKey());
152 		SubmoduleStatus status = module.getValue();
153 		assertNotNull(status);
154 		assertEquals(path, status.getPath());
155 		assertEquals(id, status.getIndexId());
156 		assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
157 	}
158 
159 	@Test
160 	public void repositoryWithNoHeadInSubmodule() throws IOException,
161 			GitAPIException {
162 		final ObjectId id = ObjectId
163 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
164 		final String path = "sub";
165 		DirCache cache = db.lockDirCache();
166 		DirCacheEditor editor = cache.editor();
167 		editor.add(new PathEdit(path) {
168 
169 			@Override
170 			public void apply(DirCacheEntry ent) {
171 				ent.setFileMode(FileMode.GITLINK);
172 				ent.setObjectId(id);
173 			}
174 		});
175 		editor.commit();
176 
177 		String url = "git://server/repo.git";
178 		StoredConfig config = db.getConfig();
179 		config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
180 				ConfigConstants.CONFIG_KEY_URL, url);
181 		config.save();
182 
183 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
184 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
185 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
186 				ConfigConstants.CONFIG_KEY_PATH, path);
187 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
188 				ConfigConstants.CONFIG_KEY_URL, url);
189 		modulesConfig.save();
190 
191 		Repository subRepo = Git.init().setBare(false)
192 				.setDirectory(new File(db.getWorkTree(), path)).call()
193 				.getRepository();
194 		assertNotNull(subRepo);
195 
196 		SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
197 		Map<String, SubmoduleStatus> statuses = command.call();
198 		assertNotNull(statuses);
199 		assertEquals(1, statuses.size());
200 		Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
201 				.next();
202 		assertNotNull(module);
203 		assertEquals(path, module.getKey());
204 		SubmoduleStatus status = module.getValue();
205 		assertNotNull(status);
206 		assertEquals(path, status.getPath());
207 		assertEquals(id, status.getIndexId());
208 		assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
209 	}
210 
211 	@Test
212 	public void repositoryWithNoSubmoduleRepository() throws IOException,
213 			GitAPIException {
214 		final ObjectId id = ObjectId
215 				.fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
216 		final String path = "sub";
217 		DirCache cache = db.lockDirCache();
218 		DirCacheEditor editor = cache.editor();
219 		editor.add(new PathEdit(path) {
220 
221 			@Override
222 			public void apply(DirCacheEntry ent) {
223 				ent.setFileMode(FileMode.GITLINK);
224 				ent.setObjectId(id);
225 			}
226 		});
227 		editor.commit();
228 
229 		String url = "git://server/repo.git";
230 		StoredConfig config = db.getConfig();
231 		config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
232 				ConfigConstants.CONFIG_KEY_URL, url);
233 		config.save();
234 
235 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
236 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
237 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
238 				ConfigConstants.CONFIG_KEY_PATH, path);
239 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
240 				ConfigConstants.CONFIG_KEY_URL, url);
241 		modulesConfig.save();
242 
243 		SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
244 		Map<String, SubmoduleStatus> statuses = command.call();
245 		assertNotNull(statuses);
246 		assertEquals(1, statuses.size());
247 		Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
248 				.next();
249 		assertNotNull(module);
250 		assertEquals(path, module.getKey());
251 		SubmoduleStatus status = module.getValue();
252 		assertNotNull(status);
253 		assertEquals(path, status.getPath());
254 		assertEquals(id, status.getIndexId());
255 		assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
256 	}
257 
258 	@Test
259 	public void repositoryWithInitializedSubmodule() throws Exception {
260 		String path = "sub";
261 		Repository subRepo = Git.init().setBare(false)
262 				.setDirectory(new File(db.getWorkTree(), path)).call()
263 				.getRepository();
264 		assertNotNull(subRepo);
265 
266 		TestRepository<?> subTr = new TestRepository<>(subRepo);
267 		ObjectId id = subTr.branch(Constants.HEAD).commit().create().copy();
268 
269 		DirCache cache = db.lockDirCache();
270 		DirCacheEditor editor = cache.editor();
271 		editor.add(new PathEdit(path) {
272 
273 			@Override
274 			public void apply(DirCacheEntry ent) {
275 				ent.setFileMode(FileMode.GITLINK);
276 				ent.setObjectId(id);
277 			}
278 		});
279 		editor.commit();
280 
281 		String url = "git://server/repo.git";
282 		StoredConfig config = db.getConfig();
283 		config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
284 				ConfigConstants.CONFIG_KEY_URL, url);
285 		config.save();
286 
287 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
288 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
289 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
290 				ConfigConstants.CONFIG_KEY_PATH, path);
291 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
292 				ConfigConstants.CONFIG_KEY_URL, url);
293 		modulesConfig.save();
294 
295 		SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
296 		Map<String, SubmoduleStatus> statuses = command.call();
297 		assertNotNull(statuses);
298 		assertEquals(1, statuses.size());
299 		Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
300 				.next();
301 		assertNotNull(module);
302 		assertEquals(path, module.getKey());
303 		SubmoduleStatus status = module.getValue();
304 		assertNotNull(status);
305 		assertEquals(path, status.getPath());
306 		assertEquals(id, status.getIndexId());
307 		assertEquals(SubmoduleStatusType.INITIALIZED, status.getType());
308 	}
309 
310 	@Test
311 	public void repositoryWithDifferentRevCheckedOutSubmodule() throws Exception {
312 		String path = "sub";
313 		Repository subRepo = Git.init().setBare(false)
314 				.setDirectory(new File(db.getWorkTree(), path)).call()
315 				.getRepository();
316 		assertNotNull(subRepo);
317 
318 		TestRepository<?> subTr = new TestRepository<>(subRepo);
319 		ObjectId id = subTr.branch(Constants.HEAD).commit().create().copy();
320 
321 		DirCache cache = db.lockDirCache();
322 		DirCacheEditor editor = cache.editor();
323 		editor.add(new PathEdit(path) {
324 
325 			@Override
326 			public void apply(DirCacheEntry ent) {
327 				ent.setFileMode(FileMode.GITLINK);
328 				ent.setObjectId(id);
329 			}
330 		});
331 		editor.commit();
332 
333 		String url = "git://server/repo.git";
334 		StoredConfig config = db.getConfig();
335 		config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
336 				ConfigConstants.CONFIG_KEY_URL, url);
337 		config.save();
338 
339 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
340 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
341 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
342 				ConfigConstants.CONFIG_KEY_PATH, path);
343 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
344 				ConfigConstants.CONFIG_KEY_URL, url);
345 		modulesConfig.save();
346 
347 		ObjectId newId = subTr.branch(Constants.HEAD).commit().create().copy();
348 
349 		SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
350 		Map<String, SubmoduleStatus> statuses = command.call();
351 		assertNotNull(statuses);
352 		assertEquals(1, statuses.size());
353 		Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
354 				.next();
355 		assertNotNull(module);
356 		assertEquals(path, module.getKey());
357 		SubmoduleStatus status = module.getValue();
358 		assertNotNull(status);
359 		assertEquals(path, status.getPath());
360 		assertEquals(id, status.getIndexId());
361 		assertEquals(newId, status.getHeadId());
362 		assertEquals(SubmoduleStatusType.REV_CHECKED_OUT, status.getType());
363 	}
364 }