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 		ObjectId id;
267 		try (TestRepository<?> subTr = new TestRepository<>(subRepo)) {
268 			id = subTr.branch(Constants.HEAD).commit().create().copy();
269 		}
270 
271 		DirCache cache = db.lockDirCache();
272 		DirCacheEditor editor = cache.editor();
273 		editor.add(new PathEdit(path) {
274 
275 			@Override
276 			public void apply(DirCacheEntry ent) {
277 				ent.setFileMode(FileMode.GITLINK);
278 				ent.setObjectId(id);
279 			}
280 		});
281 		editor.commit();
282 
283 		String url = "git://server/repo.git";
284 		StoredConfig config = db.getConfig();
285 		config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
286 				ConfigConstants.CONFIG_KEY_URL, url);
287 		config.save();
288 
289 		FileBasedConfig modulesConfig = new FileBasedConfig(new File(
290 				db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
291 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
292 				ConfigConstants.CONFIG_KEY_PATH, path);
293 		modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
294 				ConfigConstants.CONFIG_KEY_URL, url);
295 		modulesConfig.save();
296 
297 		SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
298 		Map<String, SubmoduleStatus> statuses = command.call();
299 		assertNotNull(statuses);
300 		assertEquals(1, statuses.size());
301 		Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
302 				.next();
303 		assertNotNull(module);
304 		assertEquals(path, module.getKey());
305 		SubmoduleStatus status = module.getValue();
306 		assertNotNull(status);
307 		assertEquals(path, status.getPath());
308 		assertEquals(id, status.getIndexId());
309 		assertEquals(SubmoduleStatusType.INITIALIZED, status.getType());
310 	}
311 
312 	@Test
313 	public void repositoryWithDifferentRevCheckedOutSubmodule() throws Exception {
314 		String path = "sub";
315 		Repository subRepo = Git.init().setBare(false)
316 				.setDirectory(new File(db.getWorkTree(), path)).call()
317 				.getRepository();
318 		assertNotNull(subRepo);
319 
320 		try (TestRepository<?> subTr = new TestRepository<>(subRepo)) {
321 			ObjectId id = subTr.branch(Constants.HEAD).commit().create().copy();
322 			DirCache cache = db.lockDirCache();
323 			DirCacheEditor editor = cache.editor();
324 			editor.add(new PathEdit(path) {
325 
326 				@Override
327 				public void apply(DirCacheEntry ent) {
328 					ent.setFileMode(FileMode.GITLINK);
329 					ent.setObjectId(id);
330 				}
331 			});
332 			editor.commit();
333 
334 			String url = "git://server/repo.git";
335 			StoredConfig config = db.getConfig();
336 			config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
337 					ConfigConstants.CONFIG_KEY_URL, url);
338 			config.save();
339 
340 			FileBasedConfig modulesConfig = new FileBasedConfig(
341 					new File(db.getWorkTree(), Constants.DOT_GIT_MODULES),
342 					db.getFS());
343 			modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
344 					path, ConfigConstants.CONFIG_KEY_PATH, path);
345 			modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
346 					path, ConfigConstants.CONFIG_KEY_URL, url);
347 			modulesConfig.save();
348 
349 			ObjectId newId = subTr.branch(Constants.HEAD).commit().create()
350 					.copy();
351 
352 			SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
353 			Map<String, SubmoduleStatus> statuses = command.call();
354 			assertNotNull(statuses);
355 			assertEquals(1, statuses.size());
356 			Entry<String, SubmoduleStatus> module = statuses.entrySet()
357 					.iterator().next();
358 			assertNotNull(module);
359 			assertEquals(path, module.getKey());
360 			SubmoduleStatus status = module.getValue();
361 			assertNotNull(status);
362 			assertEquals(path, status.getPath());
363 			assertEquals(id, status.getIndexId());
364 			assertEquals(newId, status.getHeadId());
365 			assertEquals(SubmoduleStatusType.REV_CHECKED_OUT, status.getType());
366 		}
367 	}
368 }