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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2011, GitHub Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.submodule;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertTrue;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.util.Map;
  17. import java.util.Map.Entry;
  18. import org.eclipse.jgit.api.Git;
  19. import org.eclipse.jgit.api.SubmoduleStatusCommand;
  20. import org.eclipse.jgit.api.errors.GitAPIException;
  21. import org.eclipse.jgit.dircache.DirCache;
  22. import org.eclipse.jgit.dircache.DirCacheEditor;
  23. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  24. import org.eclipse.jgit.dircache.DirCacheEntry;
  25. import org.eclipse.jgit.junit.RepositoryTestCase;
  26. import org.eclipse.jgit.junit.TestRepository;
  27. import org.eclipse.jgit.lib.ConfigConstants;
  28. import org.eclipse.jgit.lib.Constants;
  29. import org.eclipse.jgit.lib.FileMode;
  30. import org.eclipse.jgit.lib.ObjectId;
  31. import org.eclipse.jgit.lib.Repository;
  32. import org.eclipse.jgit.lib.StoredConfig;
  33. import org.eclipse.jgit.storage.file.FileBasedConfig;
  34. import org.junit.Test;
  35. /**
  36. * Unit tests of {@link SubmoduleStatusCommand}
  37. */
  38. public class SubmoduleStatusTest extends RepositoryTestCase {
  39. @Test
  40. public void repositoryWithNoSubmodules() throws GitAPIException {
  41. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  42. Map<String, SubmoduleStatus> statuses = command.call();
  43. assertNotNull(statuses);
  44. assertTrue(statuses.isEmpty());
  45. }
  46. @Test
  47. public void repositoryWithMissingSubmodule() throws IOException,
  48. GitAPIException {
  49. final ObjectId id = ObjectId
  50. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  51. final String path = "sub";
  52. DirCache cache = db.lockDirCache();
  53. DirCacheEditor editor = cache.editor();
  54. editor.add(new PathEdit(path) {
  55. @Override
  56. public void apply(DirCacheEntry ent) {
  57. ent.setFileMode(FileMode.GITLINK);
  58. ent.setObjectId(id);
  59. }
  60. });
  61. editor.commit();
  62. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  63. Map<String, SubmoduleStatus> statuses = command.call();
  64. assertNotNull(statuses);
  65. assertEquals(1, statuses.size());
  66. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  67. .next();
  68. assertNotNull(module);
  69. assertEquals(path, module.getKey());
  70. SubmoduleStatus status = module.getValue();
  71. assertNotNull(status);
  72. assertEquals(path, status.getPath());
  73. assertEquals(id, status.getIndexId());
  74. assertEquals(SubmoduleStatusType.MISSING, status.getType());
  75. }
  76. @Test
  77. public void repositoryWithUninitializedSubmodule() throws IOException,
  78. GitAPIException {
  79. final ObjectId id = ObjectId
  80. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  81. final String path = "sub";
  82. DirCache cache = db.lockDirCache();
  83. DirCacheEditor editor = cache.editor();
  84. editor.add(new PathEdit(path) {
  85. @Override
  86. public void apply(DirCacheEntry ent) {
  87. ent.setFileMode(FileMode.GITLINK);
  88. ent.setObjectId(id);
  89. }
  90. });
  91. editor.commit();
  92. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  93. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  94. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  95. ConfigConstants.CONFIG_KEY_PATH, path);
  96. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  97. ConfigConstants.CONFIG_KEY_URL, "git://server/repo.git");
  98. modulesConfig.save();
  99. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  100. Map<String, SubmoduleStatus> statuses = command.call();
  101. assertNotNull(statuses);
  102. assertEquals(1, statuses.size());
  103. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  104. .next();
  105. assertNotNull(module);
  106. assertEquals(path, module.getKey());
  107. SubmoduleStatus status = module.getValue();
  108. assertNotNull(status);
  109. assertEquals(path, status.getPath());
  110. assertEquals(id, status.getIndexId());
  111. assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
  112. }
  113. @Test
  114. public void repositoryWithNoHeadInSubmodule() throws IOException,
  115. GitAPIException {
  116. final ObjectId id = ObjectId
  117. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  118. final String path = "sub";
  119. DirCache cache = db.lockDirCache();
  120. DirCacheEditor editor = cache.editor();
  121. editor.add(new PathEdit(path) {
  122. @Override
  123. public void apply(DirCacheEntry ent) {
  124. ent.setFileMode(FileMode.GITLINK);
  125. ent.setObjectId(id);
  126. }
  127. });
  128. editor.commit();
  129. String url = "git://server/repo.git";
  130. StoredConfig config = db.getConfig();
  131. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  132. ConfigConstants.CONFIG_KEY_URL, url);
  133. config.save();
  134. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  135. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  136. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  137. ConfigConstants.CONFIG_KEY_PATH, path);
  138. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  139. ConfigConstants.CONFIG_KEY_URL, url);
  140. modulesConfig.save();
  141. Repository subRepo = Git.init().setBare(false)
  142. .setDirectory(new File(db.getWorkTree(), path)).call()
  143. .getRepository();
  144. assertNotNull(subRepo);
  145. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  146. Map<String, SubmoduleStatus> statuses = command.call();
  147. assertNotNull(statuses);
  148. assertEquals(1, statuses.size());
  149. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  150. .next();
  151. assertNotNull(module);
  152. assertEquals(path, module.getKey());
  153. SubmoduleStatus status = module.getValue();
  154. assertNotNull(status);
  155. assertEquals(path, status.getPath());
  156. assertEquals(id, status.getIndexId());
  157. assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
  158. }
  159. @Test
  160. public void repositoryWithNoSubmoduleRepository() 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. @Override
  169. public void apply(DirCacheEntry ent) {
  170. ent.setFileMode(FileMode.GITLINK);
  171. ent.setObjectId(id);
  172. }
  173. });
  174. editor.commit();
  175. String url = "git://server/repo.git";
  176. StoredConfig config = db.getConfig();
  177. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  178. ConfigConstants.CONFIG_KEY_URL, url);
  179. config.save();
  180. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  181. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  182. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  183. ConfigConstants.CONFIG_KEY_PATH, path);
  184. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  185. ConfigConstants.CONFIG_KEY_URL, url);
  186. modulesConfig.save();
  187. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  188. Map<String, SubmoduleStatus> statuses = command.call();
  189. assertNotNull(statuses);
  190. assertEquals(1, statuses.size());
  191. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  192. .next();
  193. assertNotNull(module);
  194. assertEquals(path, module.getKey());
  195. SubmoduleStatus status = module.getValue();
  196. assertNotNull(status);
  197. assertEquals(path, status.getPath());
  198. assertEquals(id, status.getIndexId());
  199. assertEquals(SubmoduleStatusType.UNINITIALIZED, status.getType());
  200. }
  201. @Test
  202. public void repositoryWithInitializedSubmodule() throws Exception {
  203. String path = "sub";
  204. Repository subRepo = Git.init().setBare(false)
  205. .setDirectory(new File(db.getWorkTree(), path)).call()
  206. .getRepository();
  207. assertNotNull(subRepo);
  208. ObjectId id;
  209. try (TestRepository<?> subTr = new TestRepository<>(subRepo)) {
  210. id = subTr.branch(Constants.HEAD).commit().create().copy();
  211. }
  212. DirCache cache = db.lockDirCache();
  213. DirCacheEditor editor = cache.editor();
  214. editor.add(new PathEdit(path) {
  215. @Override
  216. public void apply(DirCacheEntry ent) {
  217. ent.setFileMode(FileMode.GITLINK);
  218. ent.setObjectId(id);
  219. }
  220. });
  221. editor.commit();
  222. String url = "git://server/repo.git";
  223. StoredConfig config = db.getConfig();
  224. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  225. ConfigConstants.CONFIG_KEY_URL, url);
  226. config.save();
  227. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  228. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  229. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  230. ConfigConstants.CONFIG_KEY_PATH, path);
  231. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  232. ConfigConstants.CONFIG_KEY_URL, url);
  233. modulesConfig.save();
  234. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  235. Map<String, SubmoduleStatus> statuses = command.call();
  236. assertNotNull(statuses);
  237. assertEquals(1, statuses.size());
  238. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  239. .next();
  240. assertNotNull(module);
  241. assertEquals(path, module.getKey());
  242. SubmoduleStatus status = module.getValue();
  243. assertNotNull(status);
  244. assertEquals(path, status.getPath());
  245. assertEquals(id, status.getIndexId());
  246. assertEquals(SubmoduleStatusType.INITIALIZED, status.getType());
  247. }
  248. @Test
  249. public void repositoryWithDifferentRevCheckedOutSubmodule() throws Exception {
  250. String path = "sub";
  251. Repository subRepo = Git.init().setBare(false)
  252. .setDirectory(new File(db.getWorkTree(), path)).call()
  253. .getRepository();
  254. assertNotNull(subRepo);
  255. try (TestRepository<?> subTr = new TestRepository<>(subRepo)) {
  256. ObjectId id = subTr.branch(Constants.HEAD).commit().create().copy();
  257. DirCache cache = db.lockDirCache();
  258. DirCacheEditor editor = cache.editor();
  259. editor.add(new PathEdit(path) {
  260. @Override
  261. public void apply(DirCacheEntry ent) {
  262. ent.setFileMode(FileMode.GITLINK);
  263. ent.setObjectId(id);
  264. }
  265. });
  266. editor.commit();
  267. String url = "git://server/repo.git";
  268. StoredConfig config = db.getConfig();
  269. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  270. ConfigConstants.CONFIG_KEY_URL, url);
  271. config.save();
  272. FileBasedConfig modulesConfig = new FileBasedConfig(
  273. new File(db.getWorkTree(), Constants.DOT_GIT_MODULES),
  274. db.getFS());
  275. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  276. path, ConfigConstants.CONFIG_KEY_PATH, path);
  277. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
  278. path, ConfigConstants.CONFIG_KEY_URL, url);
  279. modulesConfig.save();
  280. ObjectId newId = subTr.branch(Constants.HEAD).commit().create()
  281. .copy();
  282. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  283. Map<String, SubmoduleStatus> statuses = command.call();
  284. assertNotNull(statuses);
  285. assertEquals(1, statuses.size());
  286. Entry<String, SubmoduleStatus> module = statuses.entrySet()
  287. .iterator().next();
  288. assertNotNull(module);
  289. assertEquals(path, module.getKey());
  290. SubmoduleStatus status = module.getValue();
  291. assertNotNull(status);
  292. assertEquals(path, status.getPath());
  293. assertEquals(id, status.getIndexId());
  294. assertEquals(newId, status.getHeadId());
  295. assertEquals(SubmoduleStatusType.REV_CHECKED_OUT, status.getType());
  296. }
  297. }
  298. }