You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SubmoduleStatusTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.io.IOException;
  49. import java.util.Map;
  50. import java.util.Map.Entry;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.api.SubmoduleStatusCommand;
  53. import org.eclipse.jgit.api.errors.GitAPIException;
  54. import org.eclipse.jgit.dircache.DirCache;
  55. import org.eclipse.jgit.dircache.DirCacheEditor;
  56. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  57. import org.eclipse.jgit.dircache.DirCacheEntry;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.junit.TestRepository;
  60. import org.eclipse.jgit.lib.ConfigConstants;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.FileMode;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.Repository;
  65. import org.eclipse.jgit.lib.StoredConfig;
  66. import org.eclipse.jgit.storage.file.FileBasedConfig;
  67. import org.junit.Test;
  68. /**
  69. * Unit tests of {@link SubmoduleStatusCommand}
  70. */
  71. public class SubmoduleStatusTest extends RepositoryTestCase {
  72. @Test
  73. public void repositoryWithNoSubmodules() throws GitAPIException {
  74. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  75. Map<String, SubmoduleStatus> statuses = command.call();
  76. assertNotNull(statuses);
  77. assertTrue(statuses.isEmpty());
  78. }
  79. @Test
  80. public void repositoryWithMissingSubmodule() throws IOException,
  81. GitAPIException {
  82. final ObjectId id = ObjectId
  83. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  84. final String path = "sub";
  85. DirCache cache = db.lockDirCache();
  86. DirCacheEditor editor = cache.editor();
  87. editor.add(new PathEdit(path) {
  88. @Override
  89. public void apply(DirCacheEntry ent) {
  90. ent.setFileMode(FileMode.GITLINK);
  91. ent.setObjectId(id);
  92. }
  93. });
  94. editor.commit();
  95. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  96. Map<String, SubmoduleStatus> statuses = command.call();
  97. assertNotNull(statuses);
  98. assertEquals(1, statuses.size());
  99. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  100. .next();
  101. assertNotNull(module);
  102. assertEquals(path, module.getKey());
  103. SubmoduleStatus status = module.getValue();
  104. assertNotNull(status);
  105. assertEquals(path, status.getPath());
  106. assertEquals(id, status.getIndexId());
  107. assertEquals(SubmoduleStatusType.MISSING, status.getType());
  108. }
  109. @Test
  110. public void repositoryWithUninitializedSubmodule() throws IOException,
  111. GitAPIException {
  112. final ObjectId id = ObjectId
  113. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  114. final String path = "sub";
  115. DirCache cache = db.lockDirCache();
  116. DirCacheEditor editor = cache.editor();
  117. editor.add(new PathEdit(path) {
  118. @Override
  119. public void apply(DirCacheEntry ent) {
  120. ent.setFileMode(FileMode.GITLINK);
  121. ent.setObjectId(id);
  122. }
  123. });
  124. editor.commit();
  125. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  126. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  127. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  128. ConfigConstants.CONFIG_KEY_PATH, path);
  129. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  130. ConfigConstants.CONFIG_KEY_URL, "git://server/repo.git");
  131. modulesConfig.save();
  132. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  133. Map<String, SubmoduleStatus> statuses = command.call();
  134. assertNotNull(statuses);
  135. assertEquals(1, statuses.size());
  136. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  137. .next();
  138. assertNotNull(module);
  139. assertEquals(path, module.getKey());
  140. SubmoduleStatus status = module.getValue();
  141. assertNotNull(status);
  142. assertEquals(path, status.getPath());
  143. assertEquals(id, status.getIndexId());
  144. assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
  145. }
  146. @Test
  147. public void repositoryWithNoHeadInSubmodule() throws IOException,
  148. GitAPIException {
  149. final ObjectId id = ObjectId
  150. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  151. final String path = "sub";
  152. DirCache cache = db.lockDirCache();
  153. DirCacheEditor editor = cache.editor();
  154. editor.add(new PathEdit(path) {
  155. @Override
  156. public void apply(DirCacheEntry ent) {
  157. ent.setFileMode(FileMode.GITLINK);
  158. ent.setObjectId(id);
  159. }
  160. });
  161. editor.commit();
  162. String url = "git://server/repo.git";
  163. StoredConfig config = db.getConfig();
  164. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  165. ConfigConstants.CONFIG_KEY_URL, url);
  166. config.save();
  167. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  168. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  169. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  170. ConfigConstants.CONFIG_KEY_PATH, path);
  171. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  172. ConfigConstants.CONFIG_KEY_URL, url);
  173. modulesConfig.save();
  174. Repository subRepo = Git.init().setBare(false)
  175. .setDirectory(new File(db.getWorkTree(), path)).call()
  176. .getRepository();
  177. assertNotNull(subRepo);
  178. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  179. Map<String, SubmoduleStatus> statuses = command.call();
  180. assertNotNull(statuses);
  181. assertEquals(1, statuses.size());
  182. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  183. .next();
  184. assertNotNull(module);
  185. assertEquals(path, module.getKey());
  186. SubmoduleStatus status = module.getValue();
  187. assertNotNull(status);
  188. assertEquals(path, status.getPath());
  189. assertEquals(id, status.getIndexId());
  190. assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
  191. }
  192. @Test
  193. public void repositoryWithNoSubmoduleRepository() throws IOException,
  194. GitAPIException {
  195. final ObjectId id = ObjectId
  196. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  197. final String path = "sub";
  198. DirCache cache = db.lockDirCache();
  199. DirCacheEditor editor = cache.editor();
  200. editor.add(new PathEdit(path) {
  201. @Override
  202. public void apply(DirCacheEntry ent) {
  203. ent.setFileMode(FileMode.GITLINK);
  204. ent.setObjectId(id);
  205. }
  206. });
  207. editor.commit();
  208. String url = "git://server/repo.git";
  209. StoredConfig config = db.getConfig();
  210. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  211. ConfigConstants.CONFIG_KEY_URL, url);
  212. config.save();
  213. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  214. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  215. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  216. ConfigConstants.CONFIG_KEY_PATH, path);
  217. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  218. ConfigConstants.CONFIG_KEY_URL, url);
  219. modulesConfig.save();
  220. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  221. Map<String, SubmoduleStatus> statuses = command.call();
  222. assertNotNull(statuses);
  223. assertEquals(1, statuses.size());
  224. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  225. .next();
  226. assertNotNull(module);
  227. assertEquals(path, module.getKey());
  228. SubmoduleStatus status = module.getValue();
  229. assertNotNull(status);
  230. assertEquals(path, status.getPath());
  231. assertEquals(id, status.getIndexId());
  232. assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
  233. }
  234. @Test
  235. public void repositoryWithInitializedSubmodule() throws Exception {
  236. String path = "sub";
  237. Repository subRepo = Git.init().setBare(false)
  238. .setDirectory(new File(db.getWorkTree(), path)).call()
  239. .getRepository();
  240. assertNotNull(subRepo);
  241. ObjectId id;
  242. try (TestRepository<?> subTr = new TestRepository<>(subRepo)) {
  243. id = subTr.branch(Constants.HEAD).commit().create().copy();
  244. }
  245. DirCache cache = db.lockDirCache();
  246. DirCacheEditor editor = cache.editor();
  247. editor.add(new PathEdit(path) {
  248. @Override
  249. public void apply(DirCacheEntry ent) {
  250. ent.setFileMode(FileMode.GITLINK);
  251. ent.setObjectId(id);
  252. }
  253. });
  254. editor.commit();
  255. String url = "git://server/repo.git";
  256. StoredConfig config = db.getConfig();
  257. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  258. ConfigConstants.CONFIG_KEY_URL, url);
  259. config.save();
  260. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  261. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  262. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  263. ConfigConstants.CONFIG_KEY_PATH, path);
  264. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  265. ConfigConstants.CONFIG_KEY_URL, url);
  266. modulesConfig.save();
  267. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  268. Map<String, SubmoduleStatus> statuses = command.call();
  269. assertNotNull(statuses);
  270. assertEquals(1, statuses.size());
  271. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  272. .next();
  273. assertNotNull(module);
  274. assertEquals(path, module.getKey());
  275. SubmoduleStatus status = module.getValue();
  276. assertNotNull(status);
  277. assertEquals(path, status.getPath());
  278. assertEquals(id, status.getIndexId());
  279. assertEquals(SubmoduleStatusType.INITIALIZED, status.getType());
  280. }
  281. @Test
  282. public void repositoryWithDifferentRevCheckedOutSubmodule() throws Exception {
  283. String path = "sub";
  284. Repository subRepo = Git.init().setBare(false)
  285. .setDirectory(new File(db.getWorkTree(), path)).call()
  286. .getRepository();
  287. assertNotNull(subRepo);
  288. try (TestRepository<?> subTr = new TestRepository<>(subRepo)) {
  289. ObjectId id = subTr.branch(Constants.HEAD).commit().create().copy();
  290. DirCache cache = db.lockDirCache();
  291. DirCacheEditor editor = cache.editor();
  292. editor.add(new PathEdit(path) {
  293. @Override
  294. public void apply(DirCacheEntry ent) {
  295. ent.setFileMode(FileMode.GITLINK);
  296. ent.setObjectId(id);
  297. }
  298. });
  299. editor.commit();
  300. String url = "git://server/repo.git";
  301. StoredConfig config = db.getConfig();
  302. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  303. ConfigConstants.CONFIG_KEY_URL, url);
  304. config.save();
  305. FileBasedConfig modulesConfig = new FileBasedConfig(
  306. new File(db.getWorkTree(), Constants.DOT_GIT_MODULES),
  307. db.getFS());
  308. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  309. path, ConfigConstants.CONFIG_KEY_PATH, path);
  310. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  311. path, ConfigConstants.CONFIG_KEY_URL, url);
  312. modulesConfig.save();
  313. ObjectId newId = subTr.branch(Constants.HEAD).commit().create()
  314. .copy();
  315. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  316. Map<String, SubmoduleStatus> statuses = command.call();
  317. assertNotNull(statuses);
  318. assertEquals(1, statuses.size());
  319. Entry<String, SubmoduleStatus> module = statuses.entrySet()
  320. .iterator().next();
  321. assertNotNull(module);
  322. assertEquals(path, module.getKey());
  323. SubmoduleStatus status = module.getValue();
  324. assertNotNull(status);
  325. assertEquals(path, status.getPath());
  326. assertEquals(id, status.getIndexId());
  327. assertEquals(newId, status.getHeadId());
  328. assertEquals(SubmoduleStatusType.REV_CHECKED_OUT, status.getType());
  329. }
  330. }
  331. }