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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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.lib.ConfigConstants;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.FileMode;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.RefUpdate;
  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 IOException,
  236. GitAPIException {
  237. final ObjectId id = ObjectId
  238. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  239. final String path = "sub";
  240. DirCache cache = db.lockDirCache();
  241. DirCacheEditor editor = cache.editor();
  242. editor.add(new PathEdit(path) {
  243. @Override
  244. public void apply(DirCacheEntry ent) {
  245. ent.setFileMode(FileMode.GITLINK);
  246. ent.setObjectId(id);
  247. }
  248. });
  249. editor.commit();
  250. String url = "git://server/repo.git";
  251. StoredConfig config = db.getConfig();
  252. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  253. ConfigConstants.CONFIG_KEY_URL, url);
  254. config.save();
  255. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  256. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  257. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  258. ConfigConstants.CONFIG_KEY_PATH, path);
  259. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  260. ConfigConstants.CONFIG_KEY_URL, url);
  261. modulesConfig.save();
  262. Repository subRepo = Git.init().setBare(false)
  263. .setDirectory(new File(db.getWorkTree(), path)).call()
  264. .getRepository();
  265. assertNotNull(subRepo);
  266. RefUpdate update = subRepo.updateRef(Constants.HEAD, true);
  267. update.setNewObjectId(id);
  268. update.forceUpdate();
  269. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  270. Map<String, SubmoduleStatus> statuses = command.call();
  271. assertNotNull(statuses);
  272. assertEquals(1, statuses.size());
  273. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  274. .next();
  275. assertNotNull(module);
  276. assertEquals(path, module.getKey());
  277. SubmoduleStatus status = module.getValue();
  278. assertNotNull(status);
  279. assertEquals(path, status.getPath());
  280. assertEquals(id, status.getIndexId());
  281. assertEquals(SubmoduleStatusType.INITIALIZED, status.getType());
  282. }
  283. @Test
  284. public void repositoryWithDifferentRevCheckedOutSubmodule()
  285. throws IOException, GitAPIException {
  286. final ObjectId id = ObjectId
  287. .fromString("abcd1234abcd1234abcd1234abcd1234abcd1234");
  288. final String path = "sub";
  289. DirCache cache = db.lockDirCache();
  290. DirCacheEditor editor = cache.editor();
  291. editor.add(new PathEdit(path) {
  292. @Override
  293. public void apply(DirCacheEntry ent) {
  294. ent.setFileMode(FileMode.GITLINK);
  295. ent.setObjectId(id);
  296. }
  297. });
  298. editor.commit();
  299. String url = "git://server/repo.git";
  300. StoredConfig config = db.getConfig();
  301. config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  302. ConfigConstants.CONFIG_KEY_URL, url);
  303. config.save();
  304. FileBasedConfig modulesConfig = new FileBasedConfig(new File(
  305. db.getWorkTree(), Constants.DOT_GIT_MODULES), db.getFS());
  306. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  307. ConfigConstants.CONFIG_KEY_PATH, path);
  308. modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path,
  309. ConfigConstants.CONFIG_KEY_URL, url);
  310. modulesConfig.save();
  311. Repository subRepo = Git.init().setBare(false)
  312. .setDirectory(new File(db.getWorkTree(), path)).call()
  313. .getRepository();
  314. assertNotNull(subRepo);
  315. RefUpdate update = subRepo.updateRef(Constants.HEAD, true);
  316. update.setNewObjectId(ObjectId
  317. .fromString("aaaa0000aaaa0000aaaa0000aaaa0000aaaa0000"));
  318. update.forceUpdate();
  319. SubmoduleStatusCommand command = new SubmoduleStatusCommand(db);
  320. Map<String, SubmoduleStatus> statuses = command.call();
  321. assertNotNull(statuses);
  322. assertEquals(1, statuses.size());
  323. Entry<String, SubmoduleStatus> module = statuses.entrySet().iterator()
  324. .next();
  325. assertNotNull(module);
  326. assertEquals(path, module.getKey());
  327. SubmoduleStatus status = module.getValue();
  328. assertNotNull(status);
  329. assertEquals(path, status.getPath());
  330. assertEquals(id, status.getIndexId());
  331. assertEquals(update.getNewObjectId(), status.getHeadId());
  332. assertEquals(SubmoduleStatusType.REV_CHECKED_OUT, status.getType());
  333. }
  334. }