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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 fetchShouldAutoFollowTag() throws Exception {
  93. remoteGit.commit().setMessage("commit").call();
  94. Ref tagRef = remoteGit.tag().setName("foo").call();
  95. git.fetch().setRemote("test")
  96. .setRefSpecs("refs/heads/*:refs/remotes/origin/*")
  97. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  98. assertEquals(tagRef.getObjectId(), db.resolve("foo"));
  99. }
  100. @Test
  101. public void fetchShouldAutoFollowTagForFetchedObjects() throws Exception {
  102. remoteGit.commit().setMessage("commit").call();
  103. Ref tagRef = remoteGit.tag().setName("foo").call();
  104. remoteGit.commit().setMessage("commit2").call();
  105. git.fetch().setRemote("test")
  106. .setRefSpecs("refs/heads/*:refs/remotes/origin/*")
  107. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  108. assertEquals(tagRef.getObjectId(), db.resolve("foo"));
  109. }
  110. @Test
  111. public void fetchShouldNotFetchTagsFromOtherBranches() throws Exception {
  112. remoteGit.commit().setMessage("commit").call();
  113. remoteGit.checkout().setName("other").setCreateBranch(true).call();
  114. remoteGit.commit().setMessage("commit2").call();
  115. remoteGit.tag().setName("foo").call();
  116. git.fetch().setRemote("test")
  117. .setRefSpecs("refs/heads/master:refs/remotes/origin/master")
  118. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  119. assertNull(db.resolve("foo"));
  120. }
  121. @Test
  122. public void fetchWithUpdatedTagShouldNotTryToUpdateLocal() throws Exception {
  123. final String tagName = "foo";
  124. remoteGit.commit().setMessage("commit").call();
  125. Ref tagRef = remoteGit.tag().setName(tagName).call();
  126. ObjectId originalId = tagRef.getObjectId();
  127. String spec = "refs/heads/*:refs/remotes/origin/*";
  128. git.fetch().setRemote("test").setRefSpecs(spec)
  129. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  130. assertEquals(originalId, db.resolve(tagName));
  131. remoteGit.commit().setMessage("commit 2").call();
  132. remoteGit.tag().setName(tagName).setForceUpdate(true).call();
  133. FetchResult result = git.fetch().setRemote("test").setRefSpecs(spec)
  134. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  135. Collection<TrackingRefUpdate> refUpdates = result
  136. .getTrackingRefUpdates();
  137. assertEquals(1, refUpdates.size());
  138. TrackingRefUpdate update = refUpdates.iterator().next();
  139. assertEquals("refs/heads/master", update.getRemoteName());
  140. assertEquals(originalId, db.resolve(tagName));
  141. }
  142. @Test
  143. public void fetchWithExplicitTagsShouldUpdateLocal() throws Exception {
  144. final String tagName = "foo";
  145. remoteGit.commit().setMessage("commit").call();
  146. Ref tagRef1 = remoteGit.tag().setName(tagName).call();
  147. String spec = "refs/heads/*:refs/remotes/origin/*";
  148. git.fetch().setRemote("test").setRefSpecs(spec)
  149. .setTagOpt(TagOpt.AUTO_FOLLOW).call();
  150. assertEquals(tagRef1.getObjectId(), db.resolve(tagName));
  151. remoteGit.commit().setMessage("commit 2").call();
  152. Ref tagRef2 = remoteGit.tag().setName(tagName).setForceUpdate(true)
  153. .call();
  154. FetchResult result = git.fetch().setRemote("test").setRefSpecs(spec)
  155. .setTagOpt(TagOpt.FETCH_TAGS).call();
  156. TrackingRefUpdate update = result.getTrackingRefUpdate(Constants.R_TAGS
  157. + tagName);
  158. assertEquals(RefUpdate.Result.FORCED, update.getResult());
  159. assertEquals(tagRef2.getObjectId(), db.resolve(tagName));
  160. }
  161. }