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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.Collection;
  47. import org.eclipse.jgit.junit.RepositoryTestCase;
  48. import org.eclipse.jgit.lib.Constants;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.lib.Ref;
  51. import org.eclipse.jgit.lib.RefUpdate;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.lib.StoredConfig;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.transport.FetchResult;
  56. import org.eclipse.jgit.transport.RemoteConfig;
  57. import org.eclipse.jgit.transport.TagOpt;
  58. import org.eclipse.jgit.transport.TrackingRefUpdate;
  59. import org.eclipse.jgit.transport.URIish;
  60. import org.junit.Before;
  61. import org.junit.Test;
  62. public class FetchCommandTest extends RepositoryTestCase {
  63. private Git git;
  64. private Git remoteGit;
  65. @Before
  66. public void setupRemoteRepository() throws Exception {
  67. git = new Git(db);
  68. // create other repository
  69. Repository remoteRepository = createWorkRepository();
  70. remoteGit = new Git(remoteRepository);
  71. // setup the first repository to fetch from the second repository
  72. final StoredConfig config = db.getConfig();
  73. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  74. URIish uri = new URIish(remoteRepository.getDirectory().toURI().toURL());
  75. remoteConfig.addURI(uri);
  76. remoteConfig.update(config);
  77. config.save();
  78. }
  79. @Test
  80. public void testFetch() throws Exception {
  81. // create some refs via commits and tag
  82. RevCommit commit = remoteGit.commit().setMessage("initial commit").call();
  83. Ref tagRef = remoteGit.tag().setName("tag").call();
  84. git.fetch().setRemote("test")
  85. .setRefSpecs("refs/heads/master:refs/heads/x").call();
  86. assertEquals(commit.getId(),
  87. db.resolve(commit.getId().getName() + "^{commit}"));
  88. assertEquals(tagRef.getObjectId(),
  89. db.resolve(tagRef.getObjectId().getName()));
  90. }
  91. @Test
  92. public void testForcedFetch() throws Exception {
  93. remoteGit.commit().setMessage("commit").call();
  94. remoteGit.commit().setMessage("commit2").call();
  95. git.fetch().setRemote("test")
  96. .setRefSpecs("refs/heads/master:refs/heads/master").call();
  97. remoteGit.commit().setAmend(true).setMessage("amended").call();
  98. FetchResult res = git.fetch().setRemote("test")
  99. .setRefSpecs("refs/heads/master:refs/heads/master").call();
  100. assertEquals(RefUpdate.Result.REJECTED,
  101. res.getTrackingRefUpdate("refs/heads/master").getResult());
  102. res = git.fetch().setRemote("test")
  103. .setRefSpecs("refs/heads/master:refs/heads/master")
  104. .setForceUpdate(true).call();
  105. assertEquals(RefUpdate.Result.FORCED,
  106. res.getTrackingRefUpdate("refs/heads/master").getResult());
  107. }
  108. @Test
  109. public void fetchShouldAutoFollowTag() throws Exception {
  110. remoteGit.commit().setMessage("commit").call();
  111. Ref tagRef = remoteGit.tag().setName("foo").call();
  112. git.fetch().setRemote("test")
  113. .setRefSpecs("refs/heads/*:refs/remotes/origin/*")
  114. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  115. assertEquals(tagRef.getObjectId(), db.resolve("foo"));
  116. }
  117. @Test
  118. public void fetchShouldAutoFollowTagForFetchedObjects() throws Exception {
  119. remoteGit.commit().setMessage("commit").call();
  120. Ref tagRef = remoteGit.tag().setName("foo").call();
  121. remoteGit.commit().setMessage("commit2").call();
  122. git.fetch().setRemote("test")
  123. .setRefSpecs("refs/heads/*:refs/remotes/origin/*")
  124. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  125. assertEquals(tagRef.getObjectId(), db.resolve("foo"));
  126. }
  127. @Test
  128. public void fetchShouldNotFetchTagsFromOtherBranches() throws Exception {
  129. remoteGit.commit().setMessage("commit").call();
  130. remoteGit.checkout().setName("other").setCreateBranch(true).call();
  131. remoteGit.commit().setMessage("commit2").call();
  132. remoteGit.tag().setName("foo").call();
  133. git.fetch().setRemote("test")
  134. .setRefSpecs("refs/heads/master:refs/remotes/origin/master")
  135. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  136. assertNull(db.resolve("foo"));
  137. }
  138. @Test
  139. public void fetchWithUpdatedTagShouldNotTryToUpdateLocal() throws Exception {
  140. final String tagName = "foo";
  141. remoteGit.commit().setMessage("commit").call();
  142. Ref tagRef = remoteGit.tag().setName(tagName).call();
  143. ObjectId originalId = tagRef.getObjectId();
  144. String spec = "refs/heads/*:refs/remotes/origin/*";
  145. git.fetch().setRemote("test").setRefSpecs(spec)
  146. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  147. assertEquals(originalId, db.resolve(tagName));
  148. remoteGit.commit().setMessage("commit 2").call();
  149. remoteGit.tag().setName(tagName).setForceUpdate(true).call();
  150. FetchResult result = git.fetch().setRemote("test").setRefSpecs(spec)
  151. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  152. Collection<TrackingRefUpdate> refUpdates = result
  153. .getTrackingRefUpdates();
  154. assertEquals(1, refUpdates.size());
  155. TrackingRefUpdate update = refUpdates.iterator().next();
  156. assertEquals("refs/heads/master", update.getRemoteName());
  157. assertEquals(originalId, db.resolve(tagName));
  158. }
  159. @Test
  160. public void fetchWithExplicitTagsShouldUpdateLocal() throws Exception {
  161. final String tagName = "foo";
  162. remoteGit.commit().setMessage("commit").call();
  163. Ref tagRef1 = remoteGit.tag().setName(tagName).call();
  164. String spec = "refs/heads/*:refs/remotes/origin/*";
  165. git.fetch().setRemote("test").setRefSpecs(spec)
  166. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  167. assertEquals(tagRef1.getObjectId(), db.resolve(tagName));
  168. remoteGit.commit().setMessage("commit 2").call();
  169. Ref tagRef2 = remoteGit.tag().setName(tagName).setForceUpdate(true)
  170. .call();
  171. FetchResult result = git.fetch().setRemote("test").setRefSpecs(spec)
  172. .setTagOpt(TagOpt.FETCH_TAGS).call();
  173. TrackingRefUpdate update = result.getTrackingRefUpdate(Constants.R_TAGS
  174. + tagName);
  175. assertEquals(RefUpdate.Result.FORCED, update.getResult());
  176. assertEquals(tagRef2.getObjectId(), db.resolve(tagName));
  177. }
  178. }