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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (C) 2010, 2014 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.assertTrue;
  47. import static org.junit.Assert.fail;
  48. import java.io.IOException;
  49. import java.net.URISyntaxException;
  50. import java.util.Properties;
  51. import org.eclipse.jgit.api.errors.GitAPIException;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.api.errors.TransportException;
  54. import org.eclipse.jgit.errors.MissingObjectException;
  55. import org.eclipse.jgit.junit.RepositoryTestCase;
  56. import org.eclipse.jgit.lib.Ref;
  57. import org.eclipse.jgit.lib.RefUpdate;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.lib.StoredConfig;
  60. import org.eclipse.jgit.revwalk.RevCommit;
  61. import org.eclipse.jgit.transport.PushResult;
  62. import org.eclipse.jgit.transport.RefSpec;
  63. import org.eclipse.jgit.transport.RemoteConfig;
  64. import org.eclipse.jgit.transport.TrackingRefUpdate;
  65. import org.eclipse.jgit.transport.URIish;
  66. import org.junit.Test;
  67. public class PushCommandTest extends RepositoryTestCase {
  68. @Test
  69. public void testPush() throws JGitInternalException, IOException,
  70. GitAPIException, URISyntaxException {
  71. // create other repository
  72. Repository db2 = createWorkRepository();
  73. // setup the first repository
  74. final StoredConfig config = db.getConfig();
  75. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  76. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  77. remoteConfig.addURI(uri);
  78. remoteConfig.update(config);
  79. config.save();
  80. Git git1 = new Git(db);
  81. // create some refs via commits and tag
  82. RevCommit commit = git1.commit().setMessage("initial commit").call();
  83. Ref tagRef = git1.tag().setName("tag").call();
  84. try {
  85. db2.resolve(commit.getId().getName() + "^{commit}");
  86. fail("id shouldn't exist yet");
  87. } catch (MissingObjectException e) {
  88. // we should get here
  89. }
  90. RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x");
  91. git1.push().setRemote("test").setRefSpecs(spec)
  92. .call();
  93. assertEquals(commit.getId(),
  94. db2.resolve(commit.getId().getName() + "^{commit}"));
  95. assertEquals(tagRef.getObjectId(),
  96. db2.resolve(tagRef.getObjectId().getName()));
  97. }
  98. @Test
  99. public void testTrackingUpdate() throws Exception {
  100. Repository db2 = createBareRepository();
  101. String remote = "origin";
  102. String branch = "refs/heads/master";
  103. String trackingBranch = "refs/remotes/" + remote + "/master";
  104. Git git = new Git(db);
  105. RevCommit commit1 = git.commit().setMessage("Initial commit")
  106. .call();
  107. RefUpdate branchRefUpdate = db.updateRef(branch);
  108. branchRefUpdate.setNewObjectId(commit1.getId());
  109. branchRefUpdate.update();
  110. RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch);
  111. trackingBranchRefUpdate.setNewObjectId(commit1.getId());
  112. trackingBranchRefUpdate.update();
  113. final StoredConfig config = db.getConfig();
  114. RemoteConfig remoteConfig = new RemoteConfig(config, remote);
  115. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  116. remoteConfig.addURI(uri);
  117. remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  118. + remote + "/*"));
  119. remoteConfig.update(config);
  120. config.save();
  121. RevCommit commit2 = git.commit().setMessage("Commit to push").call();
  122. RefSpec spec = new RefSpec(branch + ":" + branch);
  123. Iterable<PushResult> resultIterable = git.push().setRemote(remote)
  124. .setRefSpecs(spec).call();
  125. PushResult result = resultIterable.iterator().next();
  126. TrackingRefUpdate trackingRefUpdate = result
  127. .getTrackingRefUpdate(trackingBranch);
  128. assertNotNull(trackingRefUpdate);
  129. assertEquals(trackingBranch, trackingRefUpdate.getLocalName());
  130. assertEquals(branch, trackingRefUpdate.getRemoteName());
  131. assertEquals(commit2.getId(), trackingRefUpdate.getNewObjectId());
  132. assertEquals(commit2.getId(), db.resolve(trackingBranch));
  133. assertEquals(commit2.getId(), db2.resolve(branch));
  134. }
  135. /**
  136. * Check that pushes over file protocol lead to appropriate ref-updates.
  137. *
  138. * @throws Exception
  139. */
  140. @Test
  141. public void testPushRefUpdate() throws Exception {
  142. Git git = new Git(db);
  143. Git git2 = new Git(createBareRepository());
  144. final StoredConfig config = git.getRepository().getConfig();
  145. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  146. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  147. .toURL());
  148. remoteConfig.addURI(uri);
  149. remoteConfig.addPushRefSpec(new RefSpec("+refs/heads/*:refs/heads/*"));
  150. remoteConfig.update(config);
  151. config.save();
  152. writeTrashFile("f", "content of f");
  153. git.add().addFilepattern("f").call();
  154. RevCommit commit = git.commit().setMessage("adding f").call();
  155. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  156. git.push().setRemote("test").call();
  157. assertEquals(commit.getId(),
  158. git2.getRepository().resolve("refs/heads/master"));
  159. git.branchCreate().setName("refs/heads/test").call();
  160. git.checkout().setName("refs/heads/test").call();
  161. for (int i = 0; i < 6; i++) {
  162. writeTrashFile("f" + i, "content of f" + i);
  163. git.add().addFilepattern("f" + i).call();
  164. commit = git.commit().setMessage("adding f" + i).call();
  165. git.push().setRemote("test").call();
  166. git2.getRepository().getAllRefs();
  167. assertEquals("failed to update on attempt " + i, commit.getId(),
  168. git2.getRepository().resolve("refs/heads/test"));
  169. }
  170. }
  171. /**
  172. * Check that the push refspec is read from config.
  173. *
  174. * @throws Exception
  175. */
  176. @Test
  177. public void testPushWithRefSpecFromConfig() throws Exception {
  178. Git git = new Git(db);
  179. Git git2 = new Git(createBareRepository());
  180. final StoredConfig config = git.getRepository().getConfig();
  181. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  182. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  183. .toURL());
  184. remoteConfig.addURI(uri);
  185. remoteConfig.addPushRefSpec(new RefSpec("HEAD:refs/heads/newbranch"));
  186. remoteConfig.update(config);
  187. config.save();
  188. writeTrashFile("f", "content of f");
  189. git.add().addFilepattern("f").call();
  190. RevCommit commit = git.commit().setMessage("adding f").call();
  191. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  192. git.push().setRemote("test").call();
  193. assertEquals(commit.getId(),
  194. git2.getRepository().resolve("refs/heads/newbranch"));
  195. }
  196. /**
  197. * Check that only HEAD is pushed if no refspec is given.
  198. *
  199. * @throws Exception
  200. */
  201. @Test
  202. public void testPushWithoutPushRefSpec() throws Exception {
  203. Git git = new Git(db);
  204. Git git2 = new Git(createBareRepository());
  205. final StoredConfig config = git.getRepository().getConfig();
  206. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  207. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  208. .toURL());
  209. remoteConfig.addURI(uri);
  210. remoteConfig.addFetchRefSpec(new RefSpec(
  211. "+refs/heads/*:refs/remotes/origin/*"));
  212. remoteConfig.update(config);
  213. config.save();
  214. writeTrashFile("f", "content of f");
  215. git.add().addFilepattern("f").call();
  216. RevCommit commit = git.commit().setMessage("adding f").call();
  217. git.checkout().setName("not-pushed").setCreateBranch(true).call();
  218. git.checkout().setName("branchtopush").setCreateBranch(true).call();
  219. assertEquals(null,
  220. git2.getRepository().resolve("refs/heads/branchtopush"));
  221. assertEquals(null, git2.getRepository()
  222. .resolve("refs/heads/not-pushed"));
  223. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  224. git.push().setRemote("test").call();
  225. assertEquals(commit.getId(),
  226. git2.getRepository().resolve("refs/heads/branchtopush"));
  227. assertEquals(null, git2.getRepository()
  228. .resolve("refs/heads/not-pushed"));
  229. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  230. }
  231. /**
  232. * Check that missing refs don't cause errors during push
  233. *
  234. * @throws Exception
  235. */
  236. @Test
  237. public void testPushAfterGC() throws Exception {
  238. // create other repository
  239. Repository db2 = createWorkRepository();
  240. // setup the first repository
  241. final StoredConfig config = db.getConfig();
  242. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  243. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  244. remoteConfig.addURI(uri);
  245. remoteConfig.update(config);
  246. config.save();
  247. Git git1 = new Git(db);
  248. Git git2 = new Git(db2);
  249. // push master (with a new commit) to the remote
  250. git1.commit().setMessage("initial commit").call();
  251. RefSpec spec = new RefSpec("refs/heads/*:refs/heads/*");
  252. git1.push().setRemote("test").setRefSpecs(spec).call();
  253. // create an unrelated ref and a commit on our remote
  254. git2.branchCreate().setName("refs/heads/other").call();
  255. git2.checkout().setName("refs/heads/other").call();
  256. writeTrashFile("a", "content of a");
  257. git2.add().addFilepattern("a").call();
  258. RevCommit commit2 = git2.commit().setMessage("adding a").call();
  259. // run a gc to ensure we have a bitmap index
  260. Properties res = git1.gc().setExpire(null).call();
  261. assertEquals(7, res.size());
  262. // create another commit so we have something else to push
  263. writeTrashFile("b", "content of b");
  264. git1.add().addFilepattern("b").call();
  265. RevCommit commit3 = git1.commit().setMessage("adding b").call();
  266. try {
  267. // Re-run the push. Failure may happen here.
  268. git1.push().setRemote("test").setRefSpecs(spec).call();
  269. } catch (TransportException e) {
  270. assertTrue("should be caused by a MissingObjectException", e
  271. .getCause().getCause() instanceof MissingObjectException);
  272. fail("caught MissingObjectException for a change we don't have");
  273. }
  274. // Remote will have both a and b. Master will have only b
  275. try {
  276. db.resolve(commit2.getId().getName() + "^{commit}");
  277. fail("id shouldn't exist locally");
  278. } catch (MissingObjectException e) {
  279. // we should get here
  280. }
  281. assertEquals(commit2.getId(),
  282. db2.resolve(commit2.getId().getName() + "^{commit}"));
  283. assertEquals(commit3.getId(),
  284. db2.resolve(commit3.getId().getName() + "^{commit}"));
  285. }
  286. }