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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.Ref;
  53. import org.eclipse.jgit.lib.RefUpdate;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.lib.RepositoryTestCase;
  56. import org.eclipse.jgit.lib.StoredConfig;
  57. import org.eclipse.jgit.revwalk.RevCommit;
  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. Ref tagRef = 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(tagRef.getObjectId(),
  93. db2.resolve(tagRef.getObjectId().getName()));
  94. }
  95. @Test
  96. public void testTrackingUpdate() throws Exception {
  97. Repository db2 = createBareRepository();
  98. String remote = "origin";
  99. String branch = "refs/heads/master";
  100. String trackingBranch = "refs/remotes/" + remote + "/master";
  101. Git git = new Git(db);
  102. RevCommit commit1 = git.commit().setMessage("Initial commit")
  103. .call();
  104. RefUpdate branchRefUpdate = db.updateRef(branch);
  105. branchRefUpdate.setNewObjectId(commit1.getId());
  106. branchRefUpdate.update();
  107. RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch);
  108. trackingBranchRefUpdate.setNewObjectId(commit1.getId());
  109. trackingBranchRefUpdate.update();
  110. final StoredConfig config = db.getConfig();
  111. RemoteConfig remoteConfig = new RemoteConfig(config, remote);
  112. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  113. remoteConfig.addURI(uri);
  114. remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  115. + remote + "/*"));
  116. remoteConfig.update(config);
  117. config.save();
  118. RevCommit commit2 = git.commit().setMessage("Commit to push").call();
  119. RefSpec spec = new RefSpec(branch + ":" + branch);
  120. Iterable<PushResult> resultIterable = git.push().setRemote(remote)
  121. .setRefSpecs(spec).call();
  122. PushResult result = resultIterable.iterator().next();
  123. TrackingRefUpdate trackingRefUpdate = result
  124. .getTrackingRefUpdate(trackingBranch);
  125. assertNotNull(trackingRefUpdate);
  126. assertEquals(trackingBranch, trackingRefUpdate.getLocalName());
  127. assertEquals(branch, trackingRefUpdate.getRemoteName());
  128. assertEquals(commit2.getId(), trackingRefUpdate.getNewObjectId());
  129. assertEquals(commit2.getId(), db.resolve(trackingBranch));
  130. assertEquals(commit2.getId(), db2.resolve(branch));
  131. }
  132. /**
  133. * Check that pushes over file protocol lead to appropriate ref-updates.
  134. *
  135. * @throws Exception
  136. */
  137. @Test
  138. public void testPushRefUpdate() throws Exception {
  139. Git git = new Git(db);
  140. Git git2 = new Git(createBareRepository());
  141. final StoredConfig config = git.getRepository().getConfig();
  142. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  143. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  144. .toURL());
  145. remoteConfig.addURI(uri);
  146. remoteConfig.addPushRefSpec(new RefSpec("+refs/heads/*:refs/heads/*"));
  147. remoteConfig.update(config);
  148. config.save();
  149. writeTrashFile("f", "content of f");
  150. git.add().addFilepattern("f").call();
  151. RevCommit commit = git.commit().setMessage("adding f").call();
  152. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  153. git.push().setRemote("test").call();
  154. assertEquals(commit.getId(),
  155. git2.getRepository().resolve("refs/heads/master"));
  156. git.branchCreate().setName("refs/heads/test").call();
  157. git.checkout().setName("refs/heads/test").call();
  158. for (int i = 0; i < 6; i++) {
  159. writeTrashFile("f" + i, "content of f" + i);
  160. git.add().addFilepattern("f" + i).call();
  161. commit = git.commit().setMessage("adding f" + i).call();
  162. git.push().setRemote("test").call();
  163. git2.getRepository().getAllRefs();
  164. assertEquals("failed to update on attempt " + i, commit.getId(),
  165. git2.getRepository().resolve("refs/heads/test"));
  166. }
  167. }
  168. /**
  169. * Check that the push refspec is read from config.
  170. *
  171. * @throws Exception
  172. */
  173. @Test
  174. public void testPushWithRefSpecFromConfig() throws Exception {
  175. Git git = new Git(db);
  176. Git git2 = new Git(createBareRepository());
  177. final StoredConfig config = git.getRepository().getConfig();
  178. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  179. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  180. .toURL());
  181. remoteConfig.addURI(uri);
  182. remoteConfig.addPushRefSpec(new RefSpec("HEAD:refs/heads/newbranch"));
  183. remoteConfig.update(config);
  184. config.save();
  185. writeTrashFile("f", "content of f");
  186. git.add().addFilepattern("f").call();
  187. RevCommit commit = git.commit().setMessage("adding f").call();
  188. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  189. git.push().setRemote("test").call();
  190. assertEquals(commit.getId(),
  191. git2.getRepository().resolve("refs/heads/newbranch"));
  192. }
  193. /**
  194. * Check that only HEAD is pushed if no refspec is given.
  195. *
  196. * @throws Exception
  197. */
  198. @Test
  199. public void testPushWithoutPushRefSpec() throws Exception {
  200. Git git = new Git(db);
  201. Git git2 = new Git(createBareRepository());
  202. final StoredConfig config = git.getRepository().getConfig();
  203. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  204. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  205. .toURL());
  206. remoteConfig.addURI(uri);
  207. remoteConfig.addFetchRefSpec(new RefSpec(
  208. "+refs/heads/*:refs/remotes/origin/*"));
  209. remoteConfig.update(config);
  210. config.save();
  211. writeTrashFile("f", "content of f");
  212. git.add().addFilepattern("f").call();
  213. RevCommit commit = git.commit().setMessage("adding f").call();
  214. git.checkout().setName("not-pushed").setCreateBranch(true).call();
  215. git.checkout().setName("branchtopush").setCreateBranch(true).call();
  216. assertEquals(null,
  217. git2.getRepository().resolve("refs/heads/branchtopush"));
  218. assertEquals(null, git2.getRepository()
  219. .resolve("refs/heads/not-pushed"));
  220. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  221. git.push().setRemote("test").call();
  222. assertEquals(commit.getId(),
  223. git2.getRepository().resolve("refs/heads/branchtopush"));
  224. assertEquals(null, git2.getRepository()
  225. .resolve("refs/heads/not-pushed"));
  226. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  227. }
  228. }