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.1KB

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