Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FetchCommandTest.java 7.3KB

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