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.

FetchCommandTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (C) 2010, 2013 Chris Aniszczyk <caniszczyk@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.assertNull;
  46. import java.util.ArrayList;
  47. import java.util.Collection;
  48. import java.util.List;
  49. import org.eclipse.jgit.junit.RepositoryTestCase;
  50. import org.eclipse.jgit.lib.Constants;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.eclipse.jgit.lib.Ref;
  53. import org.eclipse.jgit.lib.RefUpdate;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.lib.StoredConfig;
  56. import org.eclipse.jgit.revwalk.RevCommit;
  57. import org.eclipse.jgit.transport.FetchResult;
  58. import org.eclipse.jgit.transport.RefSpec;
  59. import org.eclipse.jgit.transport.RemoteConfig;
  60. import org.eclipse.jgit.transport.TagOpt;
  61. import org.eclipse.jgit.transport.TrackingRefUpdate;
  62. import org.eclipse.jgit.transport.URIish;
  63. import org.junit.Before;
  64. import org.junit.Test;
  65. public class FetchCommandTest extends RepositoryTestCase {
  66. private Git git;
  67. private Git remoteGit;
  68. @Before
  69. public void setupRemoteRepository() throws Exception {
  70. git = new Git(db);
  71. // create other repository
  72. Repository remoteRepository = createWorkRepository();
  73. remoteGit = new Git(remoteRepository);
  74. // setup the first repository to fetch from the second repository
  75. final StoredConfig config = db.getConfig();
  76. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  77. URIish uri = new URIish(remoteRepository.getDirectory().toURI().toURL());
  78. remoteConfig.addURI(uri);
  79. remoteConfig.update(config);
  80. config.save();
  81. }
  82. @Test
  83. public void testFetch() throws Exception {
  84. // create some refs via commits and tag
  85. RevCommit commit = remoteGit.commit().setMessage("initial commit").call();
  86. Ref tagRef = remoteGit.tag().setName("tag").call();
  87. git.fetch().setRemote("test")
  88. .setRefSpecs("refs/heads/master:refs/heads/x").call();
  89. assertEquals(commit.getId(),
  90. db.resolve(commit.getId().getName() + "^{commit}"));
  91. assertEquals(tagRef.getObjectId(),
  92. db.resolve(tagRef.getObjectId().getName()));
  93. }
  94. @Test
  95. public void fetchAddsBranches() throws Exception {
  96. final String branch1 = "b1";
  97. final String branch2 = "b2";
  98. final String remoteBranch1 = "test/" + branch1;
  99. final String remoteBranch2 = "test/" + branch2;
  100. remoteGit.commit().setMessage("commit").call();
  101. Ref branchRef1 = remoteGit.branchCreate().setName(branch1).call();
  102. remoteGit.commit().setMessage("commit").call();
  103. Ref branchRef2 = remoteGit.branchCreate().setName(branch2).call();
  104. String spec = "refs/heads/*:refs/remotes/test/*";
  105. git.fetch().setRemote("test").setRefSpecs(spec).call();
  106. assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
  107. assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
  108. }
  109. @Test
  110. public void fetchDoesntDeleteBranches() throws Exception {
  111. final String branch1 = "b1";
  112. final String branch2 = "b2";
  113. final String remoteBranch1 = "test/" + branch1;
  114. final String remoteBranch2 = "test/" + branch2;
  115. remoteGit.commit().setMessage("commit").call();
  116. Ref branchRef1 = remoteGit.branchCreate().setName(branch1).call();
  117. remoteGit.commit().setMessage("commit").call();
  118. Ref branchRef2 = remoteGit.branchCreate().setName(branch2).call();
  119. String spec = "refs/heads/*:refs/remotes/test/*";
  120. git.fetch().setRemote("test").setRefSpecs(spec).call();
  121. assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
  122. assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
  123. remoteGit.branchDelete().setBranchNames(branch1).call();
  124. git.fetch().setRemote("test").setRefSpecs(spec).call();
  125. assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
  126. assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
  127. }
  128. @Test
  129. public void fetchUpdatesBranches() throws Exception {
  130. final String branch1 = "b1";
  131. final String branch2 = "b2";
  132. final String remoteBranch1 = "test/" + branch1;
  133. final String remoteBranch2 = "test/" + branch2;
  134. remoteGit.commit().setMessage("commit").call();
  135. Ref branchRef1 = remoteGit.branchCreate().setName(branch1).call();
  136. remoteGit.commit().setMessage("commit").call();
  137. Ref branchRef2 = remoteGit.branchCreate().setName(branch2).call();
  138. String spec = "refs/heads/*:refs/remotes/test/*";
  139. git.fetch().setRemote("test").setRefSpecs(spec).call();
  140. assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
  141. assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
  142. remoteGit.commit().setMessage("commit").call();
  143. branchRef2 = remoteGit.branchCreate().setName(branch2).setForce(true).call();
  144. git.fetch().setRemote("test").setRefSpecs(spec).call();
  145. assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
  146. assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
  147. }
  148. @Test
  149. public void fetchPrunesBranches() throws Exception {
  150. final String branch1 = "b1";
  151. final String branch2 = "b2";
  152. final String remoteBranch1 = "test/" + branch1;
  153. final String remoteBranch2 = "test/" + branch2;
  154. remoteGit.commit().setMessage("commit").call();
  155. Ref branchRef1 = remoteGit.branchCreate().setName(branch1).call();
  156. remoteGit.commit().setMessage("commit").call();
  157. Ref branchRef2 = remoteGit.branchCreate().setName(branch2).call();
  158. String spec = "refs/heads/*:refs/remotes/test/*";
  159. git.fetch().setRemote("test").setRefSpecs(spec).call();
  160. assertEquals(branchRef1.getObjectId(), db.resolve(remoteBranch1));
  161. assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
  162. remoteGit.branchDelete().setBranchNames(branch1).call();
  163. git.fetch().setRemote("test").setRefSpecs(spec)
  164. .setRemoveDeletedRefs(true).call();
  165. assertNull(db.resolve(remoteBranch1));
  166. assertEquals(branchRef2.getObjectId(), db.resolve(remoteBranch2));
  167. }
  168. @Test
  169. public void fetchShouldAutoFollowTag() throws Exception {
  170. remoteGit.commit().setMessage("commit").call();
  171. Ref tagRef = remoteGit.tag().setName("foo").call();
  172. git.fetch().setRemote("test")
  173. .setRefSpecs("refs/heads/*:refs/remotes/origin/*")
  174. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  175. assertEquals(tagRef.getObjectId(), db.resolve("foo"));
  176. }
  177. @Test
  178. public void fetchShouldAutoFollowTagForFetchedObjects() throws Exception {
  179. remoteGit.commit().setMessage("commit").call();
  180. Ref tagRef = remoteGit.tag().setName("foo").call();
  181. remoteGit.commit().setMessage("commit2").call();
  182. git.fetch().setRemote("test")
  183. .setRefSpecs("refs/heads/*:refs/remotes/origin/*")
  184. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  185. assertEquals(tagRef.getObjectId(), db.resolve("foo"));
  186. }
  187. @Test
  188. public void fetchShouldNotFetchTagsFromOtherBranches() throws Exception {
  189. remoteGit.commit().setMessage("commit").call();
  190. remoteGit.checkout().setName("other").setCreateBranch(true).call();
  191. remoteGit.commit().setMessage("commit2").call();
  192. remoteGit.tag().setName("foo").call();
  193. git.fetch().setRemote("test")
  194. .setRefSpecs("refs/heads/master:refs/remotes/origin/master")
  195. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  196. assertNull(db.resolve("foo"));
  197. }
  198. @Test
  199. public void fetchWithUpdatedTagShouldNotTryToUpdateLocal() throws Exception {
  200. final String tagName = "foo";
  201. remoteGit.commit().setMessage("commit").call();
  202. Ref tagRef = remoteGit.tag().setName(tagName).call();
  203. ObjectId originalId = tagRef.getObjectId();
  204. String spec = "refs/heads/*:refs/remotes/origin/*";
  205. git.fetch().setRemote("test").setRefSpecs(spec)
  206. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  207. assertEquals(originalId, db.resolve(tagName));
  208. remoteGit.commit().setMessage("commit 2").call();
  209. remoteGit.tag().setName(tagName).setForceUpdate(true).call();
  210. FetchResult result = git.fetch().setRemote("test").setRefSpecs(spec)
  211. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  212. Collection<TrackingRefUpdate> refUpdates = result
  213. .getTrackingRefUpdates();
  214. assertEquals(1, refUpdates.size());
  215. TrackingRefUpdate update = refUpdates.iterator().next();
  216. assertEquals("refs/heads/master", update.getRemoteName());
  217. assertEquals(originalId, db.resolve(tagName));
  218. }
  219. @Test
  220. public void fetchWithExplicitTagsShouldUpdateLocal() throws Exception {
  221. final String tagName = "foo";
  222. remoteGit.commit().setMessage("commit").call();
  223. Ref tagRef1 = remoteGit.tag().setName(tagName).call();
  224. String spec = "refs/heads/*:refs/remotes/origin/*";
  225. git.fetch().setRemote("test").setRefSpecs(spec)
  226. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  227. assertEquals(tagRef1.getObjectId(), db.resolve(tagName));
  228. remoteGit.commit().setMessage("commit 2").call();
  229. Ref tagRef2 = remoteGit.tag().setName(tagName).setForceUpdate(true)
  230. .call();
  231. FetchResult result = git.fetch().setRemote("test").setRefSpecs(spec)
  232. .setTagOpt(TagOpt.FETCH_TAGS).call();
  233. TrackingRefUpdate update = result.getTrackingRefUpdate(Constants.R_TAGS
  234. + tagName);
  235. assertEquals(RefUpdate.Result.FORCED, update.getResult());
  236. assertEquals(tagRef2.getObjectId(), db.resolve(tagName));
  237. }
  238. @Test
  239. public void fetchAddRefsWithDuplicateRefspec() throws Exception {
  240. final String branchName = "branch";
  241. final String remoteBranchName = "test/" + branchName;
  242. remoteGit.commit().setMessage("commit").call();
  243. Ref branchRef = remoteGit.branchCreate().setName(branchName).call();
  244. final String spec1 = "+refs/heads/*:refs/remotes/test/*";
  245. final String spec2 = "refs/heads/*:refs/remotes/test/*";
  246. final StoredConfig config = db.getConfig();
  247. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  248. remoteConfig.addFetchRefSpec(new RefSpec(spec1));
  249. remoteConfig.addFetchRefSpec(new RefSpec(spec2));
  250. remoteConfig.update(config);
  251. git.fetch().setRemote("test").setRefSpecs(spec1).call();
  252. assertEquals(branchRef.getObjectId(), db.resolve(remoteBranchName));
  253. }
  254. @Test
  255. public void fetchPruneRefsWithDuplicateRefspec()
  256. throws Exception {
  257. final String branchName = "branch";
  258. final String remoteBranchName = "test/" + branchName;
  259. remoteGit.commit().setMessage("commit").call();
  260. Ref branchRef = remoteGit.branchCreate().setName(branchName).call();
  261. final String spec1 = "+refs/heads/*:refs/remotes/test/*";
  262. final String spec2 = "refs/heads/*:refs/remotes/test/*";
  263. final StoredConfig config = db.getConfig();
  264. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  265. remoteConfig.addFetchRefSpec(new RefSpec(spec1));
  266. remoteConfig.addFetchRefSpec(new RefSpec(spec2));
  267. remoteConfig.update(config);
  268. git.fetch().setRemote("test").setRefSpecs(spec1).call();
  269. assertEquals(branchRef.getObjectId(), db.resolve(remoteBranchName));
  270. remoteGit.branchDelete().setBranchNames(branchName).call();
  271. git.fetch().setRemote("test").setRefSpecs(spec1)
  272. .setRemoveDeletedRefs(true).call();
  273. assertNull(db.resolve(remoteBranchName));
  274. }
  275. @Test
  276. public void fetchUpdateRefsWithDuplicateRefspec() throws Exception {
  277. final String tagName = "foo";
  278. remoteGit.commit().setMessage("commit").call();
  279. Ref tagRef1 = remoteGit.tag().setName(tagName).call();
  280. List<RefSpec> refSpecs = new ArrayList<>();
  281. refSpecs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
  282. refSpecs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
  283. // Updating tags via the RefSpecs and setting TagOpt.FETCH_TAGS (or
  284. // AUTO_FOLLOW) will result internally in *two* updates for the same
  285. // ref.
  286. git.fetch().setRemote("test").setRefSpecs(refSpecs)
  287. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  288. assertEquals(tagRef1.getObjectId(), db.resolve(tagName));
  289. remoteGit.commit().setMessage("commit 2").call();
  290. Ref tagRef2 = remoteGit.tag().setName(tagName).setForceUpdate(true)
  291. .call();
  292. FetchResult result = git.fetch().setRemote("test").setRefSpecs(refSpecs)
  293. .setTagOpt(TagOpt.FETCH_TAGS).call();
  294. assertEquals(2, result.getTrackingRefUpdates().size());
  295. TrackingRefUpdate update = result
  296. .getTrackingRefUpdate(Constants.R_TAGS + tagName);
  297. assertEquals(RefUpdate.Result.FORCED, update.getResult());
  298. assertEquals(tagRef2.getObjectId(), db.resolve(tagName));
  299. }
  300. }