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

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