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.

FetchAndPullCommandsRecurseSubmodulesTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (C) 2017 David Pursehouse <david.pursehouse@gmail.com>
  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.api;
  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 org.eclipse.jgit.api.ResetCommand.ResetType;
  49. import org.eclipse.jgit.junit.JGitTestUtil;
  50. import org.eclipse.jgit.junit.RepositoryTestCase;
  51. import org.eclipse.jgit.lib.ConfigConstants;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.lib.StoredConfig;
  56. import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
  57. import org.eclipse.jgit.revwalk.RevCommit;
  58. import org.eclipse.jgit.submodule.SubmoduleStatus;
  59. import org.eclipse.jgit.submodule.SubmoduleStatusType;
  60. import org.eclipse.jgit.submodule.SubmoduleWalk;
  61. import org.eclipse.jgit.transport.FetchResult;
  62. import org.eclipse.jgit.transport.RefSpec;
  63. import org.junit.Before;
  64. import org.junit.experimental.theories.DataPoints;
  65. import org.junit.experimental.theories.Theories;
  66. import org.junit.experimental.theories.Theory;
  67. import org.junit.runner.RunWith;
  68. @RunWith(Theories.class)
  69. public class FetchAndPullCommandsRecurseSubmodulesTest extends RepositoryTestCase {
  70. @DataPoints
  71. public static boolean[] useFetch = { true, false };
  72. private Git git;
  73. private Git git2;
  74. private Git sub1Git;
  75. private Git sub2Git;
  76. private RevCommit commit1;
  77. private RevCommit commit2;
  78. private ObjectId submodule1Head;
  79. private ObjectId submodule2Head;
  80. private final RefSpec REFSPEC = new RefSpec("refs/heads/master");
  81. private final String REMOTE = "origin";
  82. private final String PATH = "sub";
  83. @Before
  84. public void setUpSubmodules() throws Exception {
  85. git = new Git(db);
  86. // Create submodule 1
  87. File submodule1 = createTempDirectory(
  88. "testCloneRepositoryWithNestedSubmodules1");
  89. sub1Git = Git.init().setDirectory(submodule1).call();
  90. assertNotNull(sub1Git);
  91. Repository sub1 = sub1Git.getRepository();
  92. assertNotNull(sub1);
  93. addRepoToClose(sub1);
  94. String file = "file.txt";
  95. write(new File(sub1.getWorkTree(), file), "content");
  96. sub1Git.add().addFilepattern(file).call();
  97. RevCommit commit = sub1Git.commit().setMessage("create file").call();
  98. assertNotNull(commit);
  99. // Create submodule 2
  100. File submodule2 = createTempDirectory(
  101. "testCloneRepositoryWithNestedSubmodules2");
  102. sub2Git = Git.init().setDirectory(submodule2).call();
  103. assertNotNull(sub2Git);
  104. Repository sub2 = sub2Git.getRepository();
  105. assertNotNull(sub2);
  106. addRepoToClose(sub2);
  107. write(new File(sub2.getWorkTree(), file), "content");
  108. sub2Git.add().addFilepattern(file).call();
  109. RevCommit sub2Head = sub2Git.commit().setMessage("create file").call();
  110. assertNotNull(sub2Head);
  111. // Add submodule 2 to submodule 1
  112. Repository r2 = sub1Git.submoduleAdd().setPath(PATH)
  113. .setURI(sub2.getDirectory().toURI().toString()).call();
  114. assertNotNull(r2);
  115. addRepoToClose(r2);
  116. RevCommit sub1Head = sub1Git.commit().setAll(true)
  117. .setMessage("Adding submodule").call();
  118. assertNotNull(sub1Head);
  119. // Add submodule 1 to default repository
  120. Repository r1 = git.submoduleAdd().setPath(PATH)
  121. .setURI(sub1.getDirectory().toURI().toString()).call();
  122. assertNotNull(r1);
  123. addRepoToClose(r1);
  124. assertNotNull(git.commit().setAll(true).setMessage("Adding submodule")
  125. .call());
  126. // Clone default repository and include submodules
  127. File directory = createTempDirectory(
  128. "testCloneRepositoryWithNestedSubmodules");
  129. CloneCommand clone = Git.cloneRepository();
  130. clone.setDirectory(directory);
  131. clone.setCloneSubmodules(true);
  132. clone.setURI(git.getRepository().getDirectory().toURI().toString());
  133. git2 = clone.call();
  134. addRepoToClose(git2.getRepository());
  135. assertNotNull(git2);
  136. // Record current FETCH_HEAD of submodules
  137. try (SubmoduleWalk walk = SubmoduleWalk
  138. .forIndex(git2.getRepository())) {
  139. assertTrue(walk.next());
  140. Repository r = walk.getRepository();
  141. submodule1Head = r.resolve(Constants.FETCH_HEAD);
  142. try (SubmoduleWalk walk2 = SubmoduleWalk.forIndex(r)) {
  143. assertTrue(walk2.next());
  144. submodule2Head = walk2.getRepository()
  145. .resolve(Constants.FETCH_HEAD);
  146. }
  147. }
  148. // Commit in submodule 1
  149. JGitTestUtil.writeTrashFile(r1, "f1.txt", "test");
  150. sub1Git.add().addFilepattern("f1.txt").call();
  151. commit1 = sub1Git.commit().setMessage("new commit").call();
  152. // Commit in submodule 2
  153. JGitTestUtil.writeTrashFile(r2, "f2.txt", "test");
  154. sub2Git.add().addFilepattern("f2.txt").call();
  155. commit2 = sub2Git.commit().setMessage("new commit").call();
  156. }
  157. @Theory
  158. public void shouldNotFetchSubmodulesWhenNo(boolean fetch) throws Exception {
  159. FetchResult result = execute(FetchRecurseSubmodulesMode.NO, fetch);
  160. assertTrue(result.submoduleResults().isEmpty());
  161. assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
  162. }
  163. @Theory
  164. public void shouldFetchSubmodulesWhenYes(boolean fetch) throws Exception {
  165. FetchResult result = execute(FetchRecurseSubmodulesMode.YES, fetch);
  166. assertTrue(result.submoduleResults().containsKey("sub"));
  167. FetchResult subResult = result.submoduleResults().get("sub");
  168. assertTrue(subResult.submoduleResults().containsKey("sub"));
  169. assertSubmoduleFetchHeads(commit1, commit2);
  170. }
  171. @Theory
  172. public void shouldFetchSubmodulesWhenOnDemandAndRevisionChanged(
  173. boolean fetch) throws Exception {
  174. RevCommit update = updateSubmoduleRevision();
  175. FetchResult result = execute(FetchRecurseSubmodulesMode.ON_DEMAND,
  176. fetch);
  177. // The first submodule should have been updated
  178. assertTrue(result.submoduleResults().containsKey("sub"));
  179. FetchResult subResult = result.submoduleResults().get("sub");
  180. // The second submodule should not get updated
  181. assertTrue(subResult.submoduleResults().isEmpty());
  182. assertSubmoduleFetchHeads(commit1, submodule2Head);
  183. // After fetch the parent repo's fetch head should be the commit
  184. // that updated the submodule.
  185. assertEquals(update,
  186. git2.getRepository().resolve(Constants.FETCH_HEAD));
  187. }
  188. @Theory
  189. public void shouldNotFetchSubmodulesWhenOnDemandAndRevisionNotChanged(
  190. boolean fetch) throws Exception {
  191. FetchResult result = execute(FetchRecurseSubmodulesMode.ON_DEMAND,
  192. fetch);
  193. assertTrue(result.submoduleResults().isEmpty());
  194. assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
  195. }
  196. @Theory
  197. public void shouldNotFetchSubmodulesWhenSubmoduleConfigurationSetToNo(
  198. boolean fetch) throws Exception {
  199. StoredConfig config = git2.getRepository().getConfig();
  200. config.setEnum(ConfigConstants.CONFIG_SUBMODULE_SECTION, PATH,
  201. ConfigConstants.CONFIG_KEY_FETCH_RECURSE_SUBMODULES,
  202. FetchRecurseSubmodulesMode.NO);
  203. config.save();
  204. updateSubmoduleRevision();
  205. FetchResult result = execute(null, fetch);
  206. assertTrue(result.submoduleResults().isEmpty());
  207. assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
  208. }
  209. @Theory
  210. public void shouldFetchSubmodulesWhenSubmoduleConfigurationSetToYes(
  211. boolean fetch) throws Exception {
  212. StoredConfig config = git2.getRepository().getConfig();
  213. config.setEnum(ConfigConstants.CONFIG_SUBMODULE_SECTION, PATH,
  214. ConfigConstants.CONFIG_KEY_FETCH_RECURSE_SUBMODULES,
  215. FetchRecurseSubmodulesMode.YES);
  216. config.save();
  217. FetchResult result = execute(null, fetch);
  218. assertTrue(result.submoduleResults().containsKey("sub"));
  219. FetchResult subResult = result.submoduleResults().get("sub");
  220. assertTrue(subResult.submoduleResults().containsKey("sub"));
  221. assertSubmoduleFetchHeads(commit1, commit2);
  222. }
  223. @Theory
  224. public void shouldNotFetchSubmodulesWhenFetchConfigurationSetToNo(
  225. boolean fetch) throws Exception {
  226. StoredConfig config = git2.getRepository().getConfig();
  227. config.setEnum(ConfigConstants.CONFIG_FETCH_SECTION, null,
  228. ConfigConstants.CONFIG_KEY_RECURSE_SUBMODULES,
  229. FetchRecurseSubmodulesMode.NO);
  230. config.save();
  231. updateSubmoduleRevision();
  232. FetchResult result = execute(null, fetch);
  233. assertTrue(result.submoduleResults().isEmpty());
  234. assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
  235. }
  236. @Theory
  237. public void shouldFetchSubmodulesWhenFetchConfigurationSetToYes(
  238. boolean fetch) throws Exception {
  239. StoredConfig config = git2.getRepository().getConfig();
  240. config.setEnum(ConfigConstants.CONFIG_FETCH_SECTION, null,
  241. ConfigConstants.CONFIG_KEY_RECURSE_SUBMODULES,
  242. FetchRecurseSubmodulesMode.YES);
  243. config.save();
  244. FetchResult result = execute(null, fetch);
  245. assertTrue(result.submoduleResults().containsKey("sub"));
  246. FetchResult subResult = result.submoduleResults().get("sub");
  247. assertTrue(subResult.submoduleResults().containsKey("sub"));
  248. assertSubmoduleFetchHeads(commit1, commit2);
  249. }
  250. private RevCommit updateSubmoduleRevision() throws Exception {
  251. // Fetch the submodule in the original git and reset it to
  252. // the commit that was created
  253. try (SubmoduleWalk w = SubmoduleWalk.forIndex(git.getRepository())) {
  254. assertTrue(w.next());
  255. try (Git g = new Git(w.getRepository())) {
  256. g.fetch().setRemote(REMOTE).setRefSpecs(REFSPEC).call();
  257. g.reset().setMode(ResetType.HARD).setRef(commit1.name()).call();
  258. }
  259. }
  260. // Submodule index Id should be same as before, but head Id should be
  261. // updated to the new commit, and status should be "checked out".
  262. SubmoduleStatus subStatus = git.submoduleStatus().call().get("sub");
  263. assertEquals(submodule1Head, subStatus.getIndexId());
  264. assertEquals(commit1, subStatus.getHeadId());
  265. assertEquals(SubmoduleStatusType.REV_CHECKED_OUT, subStatus.getType());
  266. // Add and commit the submodule status
  267. git.add().addFilepattern("sub").call();
  268. RevCommit update = git.commit().setMessage("update sub").call();
  269. // Both submodule index and head should now be at the new commit, and
  270. // the status should be "initialized".
  271. subStatus = git.submoduleStatus().call().get("sub");
  272. assertEquals(commit1, subStatus.getIndexId());
  273. assertEquals(commit1, subStatus.getHeadId());
  274. assertEquals(SubmoduleStatusType.INITIALIZED, subStatus.getType());
  275. return update;
  276. }
  277. private FetchResult execute(FetchRecurseSubmodulesMode mode, boolean fetch)
  278. throws Exception {
  279. FetchResult result;
  280. if (fetch) {
  281. result = git2.fetch().setRemote(REMOTE).setRefSpecs(REFSPEC)
  282. .setRecurseSubmodules(mode).call();
  283. } else {
  284. // For the purposes of this test we don't need to care about the
  285. // pull result, or the result of pull with merge. We are only
  286. // interested in checking whether or not the submodules were updated
  287. // as expected. Setting to rebase makes it easier to assert about
  288. // the state of the parent repository head, i.e. we know it should
  289. // be at the submodule update commit, and don't need to consider a
  290. // merge commit created by the pull.
  291. result = git2.pull().setRemote(REMOTE).setRebase(true)
  292. .setRecurseSubmodules(mode).call().getFetchResult();
  293. }
  294. assertNotNull(result);
  295. return result;
  296. }
  297. private void assertSubmoduleFetchHeads(ObjectId expectedHead1,
  298. ObjectId expectedHead2) throws Exception {
  299. try (SubmoduleWalk walk = SubmoduleWalk
  300. .forIndex(git2.getRepository())) {
  301. assertTrue(walk.next());
  302. Repository r = walk.getRepository();
  303. ObjectId newHead1 = r.resolve(Constants.FETCH_HEAD);
  304. ObjectId newHead2;
  305. try (SubmoduleWalk walk2 = SubmoduleWalk.forIndex(r)) {
  306. assertTrue(walk2.next());
  307. newHead2 = walk2.getRepository().resolve(Constants.FETCH_HEAD);
  308. }
  309. assertEquals(expectedHead1, newHead1);
  310. assertEquals(expectedHead2, newHead2);
  311. }
  312. }
  313. }