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.

FetchCommandRecurseSubmodulesTest.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.Constants;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.Repository;
  54. import org.eclipse.jgit.lib.SubmoduleConfig.FetchRecurseSubmodulesMode;
  55. import org.eclipse.jgit.revwalk.RevCommit;
  56. import org.eclipse.jgit.submodule.SubmoduleStatus;
  57. import org.eclipse.jgit.submodule.SubmoduleStatusType;
  58. import org.eclipse.jgit.submodule.SubmoduleWalk;
  59. import org.eclipse.jgit.transport.FetchResult;
  60. import org.eclipse.jgit.transport.RefSpec;
  61. import org.junit.Before;
  62. import org.junit.Test;
  63. public class FetchCommandRecurseSubmodulesTest extends RepositoryTestCase {
  64. private Git git;
  65. private Git git2;
  66. private Git sub1Git;
  67. private Git sub2Git;
  68. private RevCommit commit1;
  69. private RevCommit commit2;
  70. private ObjectId submodule1Head;
  71. private ObjectId submodule2Head;
  72. private final RefSpec REFSPEC = new RefSpec("refs/heads/master");
  73. private final String REMOTE = "origin";
  74. @Before
  75. public void setUpSubmodules()
  76. throws Exception {
  77. git = new Git(db);
  78. // Create submodule 1
  79. File submodule1 = createTempDirectory(
  80. "testCloneRepositoryWithNestedSubmodules1");
  81. sub1Git = Git.init().setDirectory(submodule1).call();
  82. assertNotNull(sub1Git);
  83. Repository sub1 = sub1Git.getRepository();
  84. assertNotNull(sub1);
  85. addRepoToClose(sub1);
  86. String file = "file.txt";
  87. String path = "sub";
  88. write(new File(sub1.getWorkTree(), file), "content");
  89. sub1Git.add().addFilepattern(file).call();
  90. RevCommit commit = sub1Git.commit().setMessage("create file").call();
  91. assertNotNull(commit);
  92. // Create submodule 2
  93. File submodule2 = createTempDirectory(
  94. "testCloneRepositoryWithNestedSubmodules2");
  95. sub2Git = Git.init().setDirectory(submodule2).call();
  96. assertNotNull(sub2Git);
  97. Repository sub2 = sub2Git.getRepository();
  98. assertNotNull(sub2);
  99. addRepoToClose(sub2);
  100. write(new File(sub2.getWorkTree(), file), "content");
  101. sub2Git.add().addFilepattern(file).call();
  102. RevCommit sub2Head = sub2Git.commit().setMessage("create file").call();
  103. assertNotNull(sub2Head);
  104. // Add submodule 2 to submodule 1
  105. Repository r2 = sub1Git.submoduleAdd().setPath(path)
  106. .setURI(sub2.getDirectory().toURI().toString()).call();
  107. assertNotNull(r2);
  108. addRepoToClose(r2);
  109. RevCommit sub1Head = sub1Git.commit().setAll(true)
  110. .setMessage("Adding submodule").call();
  111. assertNotNull(sub1Head);
  112. // Add submodule 1 to default repository
  113. Repository r1 = git.submoduleAdd().setPath(path)
  114. .setURI(sub1.getDirectory().toURI().toString()).call();
  115. assertNotNull(r1);
  116. addRepoToClose(r1);
  117. assertNotNull(git.commit().setAll(true).setMessage("Adding submodule")
  118. .call());
  119. // Clone default repository and include submodules
  120. File directory = createTempDirectory(
  121. "testCloneRepositoryWithNestedSubmodules");
  122. CloneCommand clone = Git.cloneRepository();
  123. clone.setDirectory(directory);
  124. clone.setCloneSubmodules(true);
  125. clone.setURI(git.getRepository().getDirectory().toURI().toString());
  126. git2 = clone.call();
  127. addRepoToClose(git2.getRepository());
  128. assertNotNull(git2);
  129. // Record current FETCH_HEAD of submodules
  130. try (SubmoduleWalk walk = SubmoduleWalk
  131. .forIndex(git2.getRepository())) {
  132. assertTrue(walk.next());
  133. Repository r = walk.getRepository();
  134. submodule1Head = r.resolve(Constants.FETCH_HEAD);
  135. try (SubmoduleWalk walk2 = SubmoduleWalk.forIndex(r)) {
  136. assertTrue(walk2.next());
  137. submodule2Head = walk2.getRepository()
  138. .resolve(Constants.FETCH_HEAD);
  139. }
  140. }
  141. // Commit in submodule 1
  142. JGitTestUtil.writeTrashFile(r1, "f1.txt", "test");
  143. sub1Git.add().addFilepattern("f1.txt").call();
  144. commit1 = sub1Git.commit().setMessage("new commit").call();
  145. // Commit in submodule 2
  146. JGitTestUtil.writeTrashFile(r2, "f2.txt", "test");
  147. sub2Git.add().addFilepattern("f2.txt").call();
  148. commit2 = sub2Git.commit().setMessage("new commit").call();
  149. }
  150. @Test
  151. public void shouldNotFetchSubmodulesWhenNo() throws Exception {
  152. FetchResult result = fetch(FetchRecurseSubmodulesMode.NO);
  153. assertTrue(result.submoduleResults().isEmpty());
  154. assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
  155. }
  156. @Test
  157. public void shouldFetchSubmodulesWhenYes() throws Exception {
  158. FetchResult result = fetch(FetchRecurseSubmodulesMode.YES);
  159. assertTrue(result.submoduleResults().containsKey("sub"));
  160. FetchResult subResult = result.submoduleResults().get("sub");
  161. assertTrue(subResult.submoduleResults().containsKey("sub"));
  162. assertSubmoduleFetchHeads(commit1, commit2);
  163. }
  164. @Test
  165. public void shouldFetchSubmodulesWhenOnDemandAndRevisionChanged()
  166. throws Exception {
  167. // Fetch the submodule in the original git and reset it to
  168. // the commit that was created
  169. try (SubmoduleWalk w = SubmoduleWalk.forIndex(git.getRepository())) {
  170. assertTrue(w.next());
  171. try (Git g = new Git(w.getRepository())) {
  172. g.fetch().setRemote(REMOTE).setRefSpecs(REFSPEC).call();
  173. g.reset().setMode(ResetType.HARD).setRef(commit1.name()).call();
  174. }
  175. }
  176. // Submodule index Id should be same as before, but head Id should be
  177. // updated to the new commit, and status should be "checked out".
  178. SubmoduleStatus subStatus = git.submoduleStatus().call().get("sub");
  179. assertEquals(submodule1Head, subStatus.getIndexId());
  180. assertEquals(commit1, subStatus.getHeadId());
  181. assertEquals(SubmoduleStatusType.REV_CHECKED_OUT, subStatus.getType());
  182. // Add and commit the submodule status
  183. git.add().addFilepattern("sub").call();
  184. RevCommit update = git.commit().setMessage("update sub").call();
  185. // Both submodule index and head should now be at the new commit, and
  186. // the status should be "initialized".
  187. subStatus = git.submoduleStatus().call().get("sub");
  188. assertEquals(commit1, subStatus.getIndexId());
  189. assertEquals(commit1, subStatus.getHeadId());
  190. assertEquals(SubmoduleStatusType.INITIALIZED, subStatus.getType());
  191. FetchResult result = fetch(FetchRecurseSubmodulesMode.ON_DEMAND);
  192. // The first submodule should have been updated
  193. assertTrue(result.submoduleResults().containsKey("sub"));
  194. FetchResult subResult = result.submoduleResults().get("sub");
  195. // The second submodule should not get updated
  196. assertTrue(subResult.submoduleResults().isEmpty());
  197. assertSubmoduleFetchHeads(commit1, submodule2Head);
  198. // After fetch the parent repo's fetch head should be the commit
  199. // that updated the submodule.
  200. assertEquals(update,
  201. git2.getRepository().resolve(Constants.FETCH_HEAD));
  202. }
  203. @Test
  204. public void shouldNotFetchSubmodulesWhenOnDemandAndRevisionNotChanged()
  205. throws Exception {
  206. FetchResult result = fetch(FetchRecurseSubmodulesMode.ON_DEMAND);
  207. assertTrue(result.submoduleResults().isEmpty());
  208. assertSubmoduleFetchHeads(submodule1Head, submodule2Head);
  209. }
  210. private FetchResult fetch(FetchRecurseSubmodulesMode mode)
  211. throws Exception {
  212. FetchResult result = git2.fetch().setRemote(REMOTE).setRefSpecs(REFSPEC)
  213. .setRecurseSubmodules(mode).call();
  214. assertNotNull(result);
  215. return result;
  216. }
  217. private void assertSubmoduleFetchHeads(ObjectId expectedHead1,
  218. ObjectId expectedHead2) throws Exception {
  219. try (SubmoduleWalk walk = SubmoduleWalk
  220. .forIndex(git2.getRepository())) {
  221. assertTrue(walk.next());
  222. Repository r = walk.getRepository();
  223. ObjectId newHead1 = r.resolve(Constants.FETCH_HEAD);
  224. ObjectId newHead2;
  225. try (SubmoduleWalk walk2 = SubmoduleWalk.forIndex(r)) {
  226. assertTrue(walk2.next());
  227. newHead2 = walk2.getRepository().resolve(Constants.FETCH_HEAD);
  228. }
  229. assertEquals(expectedHead1, newHead1);
  230. assertEquals(expectedHead2, newHead2);
  231. }
  232. }
  233. }