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.

PushCommandTest.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2010, 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.assertNotNull;
  46. import static org.junit.Assert.fail;
  47. import java.io.IOException;
  48. import java.net.URISyntaxException;
  49. import org.eclipse.jgit.api.errors.GitAPIException;
  50. import org.eclipse.jgit.api.errors.JGitInternalException;
  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. import org.eclipse.jgit.lib.RefUpdate;
  53. import org.eclipse.jgit.lib.Repository;
  54. import org.eclipse.jgit.lib.RepositoryTestCase;
  55. import org.eclipse.jgit.lib.StoredConfig;
  56. import org.eclipse.jgit.revwalk.RevCommit;
  57. import org.eclipse.jgit.revwalk.RevTag;
  58. import org.eclipse.jgit.transport.PushResult;
  59. import org.eclipse.jgit.transport.RefSpec;
  60. import org.eclipse.jgit.transport.RemoteConfig;
  61. import org.eclipse.jgit.transport.TrackingRefUpdate;
  62. import org.eclipse.jgit.transport.URIish;
  63. import org.junit.Test;
  64. public class PushCommandTest extends RepositoryTestCase {
  65. @Test
  66. public void testPush() throws JGitInternalException, IOException,
  67. GitAPIException, URISyntaxException {
  68. // create other repository
  69. Repository db2 = createWorkRepository();
  70. // setup the first repository
  71. final StoredConfig config = db.getConfig();
  72. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  73. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  74. remoteConfig.addURI(uri);
  75. remoteConfig.update(config);
  76. config.save();
  77. Git git1 = new Git(db);
  78. // create some refs via commits and tag
  79. RevCommit commit = git1.commit().setMessage("initial commit").call();
  80. RevTag tag = git1.tag().setName("tag").call();
  81. try {
  82. db2.resolve(commit.getId().getName() + "^{commit}");
  83. fail("id shouldn't exist yet");
  84. } catch (MissingObjectException e) {
  85. // we should get here
  86. }
  87. RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x");
  88. git1.push().setRemote("test").setRefSpecs(spec)
  89. .call();
  90. assertEquals(commit.getId(),
  91. db2.resolve(commit.getId().getName() + "^{commit}"));
  92. assertEquals(tag.getId(), db2.resolve(tag.getId().getName()));
  93. }
  94. @Test
  95. public void testTrackingUpdate() throws Exception {
  96. Repository db2 = createBareRepository();
  97. String remote = "origin";
  98. String branch = "refs/heads/master";
  99. String trackingBranch = "refs/remotes/" + remote + "/master";
  100. Git git = new Git(db);
  101. RevCommit commit1 = git.commit().setMessage("Initial commit")
  102. .call();
  103. RefUpdate branchRefUpdate = db.updateRef(branch);
  104. branchRefUpdate.setNewObjectId(commit1.getId());
  105. branchRefUpdate.update();
  106. RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch);
  107. trackingBranchRefUpdate.setNewObjectId(commit1.getId());
  108. trackingBranchRefUpdate.update();
  109. final StoredConfig config = db.getConfig();
  110. RemoteConfig remoteConfig = new RemoteConfig(config, remote);
  111. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  112. remoteConfig.addURI(uri);
  113. remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  114. + remote + "/*"));
  115. remoteConfig.update(config);
  116. config.save();
  117. RevCommit commit2 = git.commit().setMessage("Commit to push").call();
  118. RefSpec spec = new RefSpec(branch + ":" + branch);
  119. Iterable<PushResult> resultIterable = git.push().setRemote(remote)
  120. .setRefSpecs(spec).call();
  121. PushResult result = resultIterable.iterator().next();
  122. TrackingRefUpdate trackingRefUpdate = result
  123. .getTrackingRefUpdate(trackingBranch);
  124. assertNotNull(trackingRefUpdate);
  125. assertEquals(trackingBranch, trackingRefUpdate.getLocalName());
  126. assertEquals(branch, trackingRefUpdate.getRemoteName());
  127. assertEquals(commit2.getId(), trackingRefUpdate.getNewObjectId());
  128. assertEquals(commit2.getId(), db.resolve(trackingBranch));
  129. assertEquals(commit2.getId(), db2.resolve(branch));
  130. }
  131. /**
  132. * Check that pushes over file protocol lead to appropriate ref-updates.
  133. *
  134. * @throws Exception
  135. */
  136. @Test
  137. public void testPushRefUpdate() throws Exception {
  138. Git git = new Git(db);
  139. Git git2 = new Git(createBareRepository());
  140. final StoredConfig config = git.getRepository().getConfig();
  141. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  142. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  143. .toURL());
  144. remoteConfig.addURI(uri);
  145. remoteConfig.addPushRefSpec(new RefSpec("+refs/heads/*:refs/heads/*"));
  146. remoteConfig.update(config);
  147. config.save();
  148. writeTrashFile("f", "content of f");
  149. git.add().addFilepattern("f").call();
  150. RevCommit commit = git.commit().setMessage("adding f").call();
  151. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  152. git.push().setRemote("test").call();
  153. assertEquals(commit.getId(),
  154. git2.getRepository().resolve("refs/heads/master"));
  155. git.branchCreate().setName("refs/heads/test").call();
  156. git.checkout().setName("refs/heads/test").call();
  157. for (int i = 0; i < 6; i++) {
  158. writeTrashFile("f" + i, "content of f" + i);
  159. git.add().addFilepattern("f" + i).call();
  160. commit = git.commit().setMessage("adding f" + i).call();
  161. git.push().setRemote("test").call();
  162. git2.getRepository().getAllRefs();
  163. assertEquals("failed to update on attempt " + i, commit.getId(),
  164. git2.getRepository().resolve("refs/heads/test"));
  165. }
  166. }
  167. /**
  168. * Check that the push refspec is read from config.
  169. *
  170. * @throws Exception
  171. */
  172. @Test
  173. public void testPushWithRefSpecFromConfig() throws Exception {
  174. Git git = new Git(db);
  175. Git git2 = new Git(createBareRepository());
  176. final StoredConfig config = git.getRepository().getConfig();
  177. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  178. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  179. .toURL());
  180. remoteConfig.addURI(uri);
  181. remoteConfig.addPushRefSpec(new RefSpec("HEAD:refs/heads/newbranch"));
  182. remoteConfig.update(config);
  183. config.save();
  184. writeTrashFile("f", "content of f");
  185. git.add().addFilepattern("f").call();
  186. RevCommit commit = git.commit().setMessage("adding f").call();
  187. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  188. git.push().setRemote("test").call();
  189. assertEquals(commit.getId(),
  190. git2.getRepository().resolve("refs/heads/newbranch"));
  191. }
  192. /**
  193. * Check that only HEAD is pushed if no refspec is given.
  194. *
  195. * @throws Exception
  196. */
  197. @Test
  198. public void testPushWithoutPushRefSpec() throws Exception {
  199. Git git = new Git(db);
  200. Git git2 = new Git(createBareRepository());
  201. final StoredConfig config = git.getRepository().getConfig();
  202. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  203. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  204. .toURL());
  205. remoteConfig.addURI(uri);
  206. remoteConfig.addFetchRefSpec(new RefSpec(
  207. "+refs/heads/*:refs/remotes/origin/*"));
  208. remoteConfig.update(config);
  209. config.save();
  210. writeTrashFile("f", "content of f");
  211. git.add().addFilepattern("f").call();
  212. RevCommit commit = git.commit().setMessage("adding f").call();
  213. git.checkout().setName("not-pushed").setCreateBranch(true).call();
  214. git.checkout().setName("branchtopush").setCreateBranch(true).call();
  215. assertEquals(null,
  216. git2.getRepository().resolve("refs/heads/branchtopush"));
  217. assertEquals(null, git2.getRepository()
  218. .resolve("refs/heads/not-pushed"));
  219. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  220. git.push().setRemote("test").call();
  221. assertEquals(commit.getId(),
  222. git2.getRepository().resolve("refs/heads/branchtopush"));
  223. assertEquals(null, git2.getRepository()
  224. .resolve("refs/heads/not-pushed"));
  225. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  226. }
  227. }