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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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.File;
  49. import java.io.IOException;
  50. import java.net.URISyntaxException;
  51. import java.util.Properties;
  52. import org.eclipse.jgit.api.errors.GitAPIException;
  53. import org.eclipse.jgit.api.errors.JGitInternalException;
  54. import org.eclipse.jgit.api.errors.TransportException;
  55. import org.eclipse.jgit.errors.MissingObjectException;
  56. import org.eclipse.jgit.hooks.PrePushHook;
  57. import org.eclipse.jgit.junit.JGitTestUtil;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.eclipse.jgit.lib.Ref;
  61. import org.eclipse.jgit.lib.RefUpdate;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.lib.StoredConfig;
  64. import org.eclipse.jgit.revwalk.RevCommit;
  65. import org.eclipse.jgit.transport.PushResult;
  66. import org.eclipse.jgit.transport.RefLeaseSpec;
  67. import org.eclipse.jgit.transport.RefSpec;
  68. import org.eclipse.jgit.transport.RemoteConfig;
  69. import org.eclipse.jgit.transport.RemoteRefUpdate;
  70. import org.eclipse.jgit.transport.TrackingRefUpdate;
  71. import org.eclipse.jgit.transport.URIish;
  72. import org.eclipse.jgit.util.FS;
  73. import org.junit.Test;
  74. public class PushCommandTest extends RepositoryTestCase {
  75. @Test
  76. public void testPush() throws JGitInternalException, IOException,
  77. GitAPIException, URISyntaxException {
  78. // create other repository
  79. Repository db2 = createWorkRepository();
  80. final StoredConfig config2 = db2.getConfig();
  81. // this tests that this config can be parsed properly
  82. config2.setString("fsck", "", "missingEmail", "ignore");
  83. config2.save();
  84. // setup the first repository
  85. final StoredConfig config = db.getConfig();
  86. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  87. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  88. remoteConfig.addURI(uri);
  89. remoteConfig.update(config);
  90. config.save();
  91. try (Git git1 = new Git(db)) {
  92. // create some refs via commits and tag
  93. RevCommit commit = git1.commit().setMessage("initial commit").call();
  94. Ref tagRef = git1.tag().setName("tag").call();
  95. try {
  96. db2.resolve(commit.getId().getName() + "^{commit}");
  97. fail("id shouldn't exist yet");
  98. } catch (MissingObjectException e) {
  99. // we should get here
  100. }
  101. RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x");
  102. git1.push().setRemote("test").setRefSpecs(spec)
  103. .call();
  104. assertEquals(commit.getId(),
  105. db2.resolve(commit.getId().getName() + "^{commit}"));
  106. assertEquals(tagRef.getObjectId(),
  107. db2.resolve(tagRef.getObjectId().getName()));
  108. }
  109. }
  110. @Test
  111. public void testPrePushHook() throws JGitInternalException, IOException,
  112. GitAPIException, URISyntaxException {
  113. // create other repository
  114. Repository db2 = createWorkRepository();
  115. // setup the first repository
  116. final StoredConfig config = db.getConfig();
  117. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  118. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  119. remoteConfig.addURI(uri);
  120. remoteConfig.update(config);
  121. config.save();
  122. File hookOutput = new File(getTemporaryDirectory(), "hookOutput");
  123. writeHookFile(PrePushHook.NAME, "#!/bin/sh\necho 1:$1, 2:$2, 3:$3 >\""
  124. + hookOutput.toPath() + "\"\ncat - >>\"" + hookOutput.toPath()
  125. + "\"\nexit 0");
  126. try (Git git1 = new Git(db)) {
  127. // create some refs via commits and tag
  128. RevCommit commit = git1.commit().setMessage("initial commit").call();
  129. RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x");
  130. git1.push().setRemote("test").setRefSpecs(spec).call();
  131. assertEquals("1:test, 2:" + uri + ", 3:\n" + "refs/heads/master "
  132. + commit.getName() + " refs/heads/x "
  133. + ObjectId.zeroId().name() + "\n", read(hookOutput));
  134. }
  135. }
  136. private File writeHookFile(String name, String data)
  137. throws IOException {
  138. File path = new File(db.getWorkTree() + "/.git/hooks/", name);
  139. JGitTestUtil.write(path, data);
  140. FS.DETECTED.setExecute(path, true);
  141. return path;
  142. }
  143. @Test
  144. public void testTrackingUpdate() throws Exception {
  145. Repository db2 = createBareRepository();
  146. String remote = "origin";
  147. String branch = "refs/heads/master";
  148. String trackingBranch = "refs/remotes/" + remote + "/master";
  149. try (Git git = new Git(db)) {
  150. RevCommit commit1 = git.commit().setMessage("Initial commit")
  151. .call();
  152. RefUpdate branchRefUpdate = db.updateRef(branch);
  153. branchRefUpdate.setNewObjectId(commit1.getId());
  154. branchRefUpdate.update();
  155. RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch);
  156. trackingBranchRefUpdate.setNewObjectId(commit1.getId());
  157. trackingBranchRefUpdate.update();
  158. final StoredConfig config = db.getConfig();
  159. RemoteConfig remoteConfig = new RemoteConfig(config, remote);
  160. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  161. remoteConfig.addURI(uri);
  162. remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
  163. + remote + "/*"));
  164. remoteConfig.update(config);
  165. config.save();
  166. RevCommit commit2 = git.commit().setMessage("Commit to push").call();
  167. RefSpec spec = new RefSpec(branch + ":" + branch);
  168. Iterable<PushResult> resultIterable = git.push().setRemote(remote)
  169. .setRefSpecs(spec).call();
  170. PushResult result = resultIterable.iterator().next();
  171. TrackingRefUpdate trackingRefUpdate = result
  172. .getTrackingRefUpdate(trackingBranch);
  173. assertNotNull(trackingRefUpdate);
  174. assertEquals(trackingBranch, trackingRefUpdate.getLocalName());
  175. assertEquals(branch, trackingRefUpdate.getRemoteName());
  176. assertEquals(commit2.getId(), trackingRefUpdate.getNewObjectId());
  177. assertEquals(commit2.getId(), db.resolve(trackingBranch));
  178. assertEquals(commit2.getId(), db2.resolve(branch));
  179. }
  180. }
  181. /**
  182. * Check that pushes over file protocol lead to appropriate ref-updates.
  183. *
  184. * @throws Exception
  185. */
  186. @Test
  187. public void testPushRefUpdate() throws Exception {
  188. try (Git git = new Git(db);
  189. Git git2 = new Git(createBareRepository())) {
  190. final StoredConfig config = git.getRepository().getConfig();
  191. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  192. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  193. .toURL());
  194. remoteConfig.addURI(uri);
  195. remoteConfig.addPushRefSpec(new RefSpec("+refs/heads/*:refs/heads/*"));
  196. remoteConfig.update(config);
  197. config.save();
  198. writeTrashFile("f", "content of f");
  199. git.add().addFilepattern("f").call();
  200. RevCommit commit = git.commit().setMessage("adding f").call();
  201. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  202. git.push().setRemote("test").call();
  203. assertEquals(commit.getId(),
  204. git2.getRepository().resolve("refs/heads/master"));
  205. git.branchCreate().setName("refs/heads/test").call();
  206. git.checkout().setName("refs/heads/test").call();
  207. for (int i = 0; i < 6; i++) {
  208. writeTrashFile("f" + i, "content of f" + i);
  209. git.add().addFilepattern("f" + i).call();
  210. commit = git.commit().setMessage("adding f" + i).call();
  211. git.push().setRemote("test").call();
  212. git2.getRepository().getRefDatabase().getRefs();
  213. assertEquals("failed to update on attempt " + i, commit.getId(),
  214. git2.getRepository().resolve("refs/heads/test"));
  215. }
  216. }
  217. }
  218. /**
  219. * Check that the push refspec is read from config.
  220. *
  221. * @throws Exception
  222. */
  223. @Test
  224. public void testPushWithRefSpecFromConfig() throws Exception {
  225. try (Git git = new Git(db);
  226. Git git2 = new Git(createBareRepository())) {
  227. final StoredConfig config = git.getRepository().getConfig();
  228. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  229. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  230. .toURL());
  231. remoteConfig.addURI(uri);
  232. remoteConfig.addPushRefSpec(new RefSpec("HEAD:refs/heads/newbranch"));
  233. remoteConfig.update(config);
  234. config.save();
  235. writeTrashFile("f", "content of f");
  236. git.add().addFilepattern("f").call();
  237. RevCommit commit = git.commit().setMessage("adding f").call();
  238. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  239. git.push().setRemote("test").call();
  240. assertEquals(commit.getId(),
  241. git2.getRepository().resolve("refs/heads/newbranch"));
  242. }
  243. }
  244. /**
  245. * Check that only HEAD is pushed if no refspec is given.
  246. *
  247. * @throws Exception
  248. */
  249. @Test
  250. public void testPushWithoutPushRefSpec() throws Exception {
  251. try (Git git = new Git(db);
  252. Git git2 = new Git(createBareRepository())) {
  253. final StoredConfig config = git.getRepository().getConfig();
  254. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  255. URIish uri = new URIish(git2.getRepository().getDirectory().toURI()
  256. .toURL());
  257. remoteConfig.addURI(uri);
  258. remoteConfig.addFetchRefSpec(new RefSpec(
  259. "+refs/heads/*:refs/remotes/origin/*"));
  260. remoteConfig.update(config);
  261. config.save();
  262. writeTrashFile("f", "content of f");
  263. git.add().addFilepattern("f").call();
  264. RevCommit commit = git.commit().setMessage("adding f").call();
  265. git.checkout().setName("not-pushed").setCreateBranch(true).call();
  266. git.checkout().setName("branchtopush").setCreateBranch(true).call();
  267. assertEquals(null,
  268. git2.getRepository().resolve("refs/heads/branchtopush"));
  269. assertEquals(null, git2.getRepository()
  270. .resolve("refs/heads/not-pushed"));
  271. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  272. git.push().setRemote("test").call();
  273. assertEquals(commit.getId(),
  274. git2.getRepository().resolve("refs/heads/branchtopush"));
  275. assertEquals(null, git2.getRepository()
  276. .resolve("refs/heads/not-pushed"));
  277. assertEquals(null, git2.getRepository().resolve("refs/heads/master"));
  278. }
  279. }
  280. /**
  281. * Check that missing refs don't cause errors during push
  282. *
  283. * @throws Exception
  284. */
  285. @Test
  286. public void testPushAfterGC() throws Exception {
  287. // create other repository
  288. Repository db2 = createWorkRepository();
  289. // setup the first repository
  290. final StoredConfig config = db.getConfig();
  291. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  292. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  293. remoteConfig.addURI(uri);
  294. remoteConfig.update(config);
  295. config.save();
  296. try (Git git1 = new Git(db);
  297. Git git2 = new Git(db2)) {
  298. // push master (with a new commit) to the remote
  299. git1.commit().setMessage("initial commit").call();
  300. RefSpec spec = new RefSpec("refs/heads/*:refs/heads/*");
  301. git1.push().setRemote("test").setRefSpecs(spec).call();
  302. // create an unrelated ref and a commit on our remote
  303. git2.branchCreate().setName("refs/heads/other").call();
  304. git2.checkout().setName("refs/heads/other").call();
  305. writeTrashFile("a", "content of a");
  306. git2.add().addFilepattern("a").call();
  307. RevCommit commit2 = git2.commit().setMessage("adding a").call();
  308. // run a gc to ensure we have a bitmap index
  309. Properties res = git1.gc().setExpire(null).call();
  310. assertEquals(7, res.size());
  311. // create another commit so we have something else to push
  312. writeTrashFile("b", "content of b");
  313. git1.add().addFilepattern("b").call();
  314. RevCommit commit3 = git1.commit().setMessage("adding b").call();
  315. try {
  316. // Re-run the push. Failure may happen here.
  317. git1.push().setRemote("test").setRefSpecs(spec).call();
  318. } catch (TransportException e) {
  319. assertTrue("should be caused by a MissingObjectException", e
  320. .getCause().getCause() instanceof MissingObjectException);
  321. fail("caught MissingObjectException for a change we don't have");
  322. }
  323. // Remote will have both a and b. Master will have only b
  324. try {
  325. db.resolve(commit2.getId().getName() + "^{commit}");
  326. fail("id shouldn't exist locally");
  327. } catch (MissingObjectException e) {
  328. // we should get here
  329. }
  330. assertEquals(commit2.getId(),
  331. db2.resolve(commit2.getId().getName() + "^{commit}"));
  332. assertEquals(commit3.getId(),
  333. db2.resolve(commit3.getId().getName() + "^{commit}"));
  334. }
  335. }
  336. @Test
  337. public void testPushWithLease() throws JGitInternalException, IOException,
  338. GitAPIException, URISyntaxException {
  339. // create other repository
  340. Repository db2 = createWorkRepository();
  341. // setup the first repository
  342. final StoredConfig config = db.getConfig();
  343. RemoteConfig remoteConfig = new RemoteConfig(config, "test");
  344. URIish uri = new URIish(db2.getDirectory().toURI().toURL());
  345. remoteConfig.addURI(uri);
  346. remoteConfig.update(config);
  347. config.save();
  348. try (Git git1 = new Git(db)) {
  349. // create one commit and push it
  350. RevCommit commit = git1.commit().setMessage("initial commit").call();
  351. git1.branchCreate().setName("initial").call();
  352. RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x");
  353. git1.push().setRemote("test").setRefSpecs(spec)
  354. .call();
  355. assertEquals(commit.getId(),
  356. db2.resolve(commit.getId().getName() + "^{commit}"));
  357. //now try to force-push a new commit, with a good lease
  358. git1.commit().setMessage("second commit").call();
  359. Iterable<PushResult> results =
  360. git1.push().setRemote("test").setRefSpecs(spec)
  361. .setRefLeaseSpecs(new RefLeaseSpec("refs/heads/x", "initial"))
  362. .call();
  363. for (PushResult result : results) {
  364. RemoteRefUpdate update = result.getRemoteUpdate("refs/heads/x");
  365. assertEquals(update.getStatus(), RemoteRefUpdate.Status.OK);
  366. }
  367. git1.commit().setMessage("third commit").call();
  368. //now try to force-push a new commit, with a bad lease
  369. results =
  370. git1.push().setRemote("test").setRefSpecs(spec)
  371. .setRefLeaseSpecs(new RefLeaseSpec("refs/heads/x", "initial"))
  372. .call();
  373. for (PushResult result : results) {
  374. RemoteRefUpdate update = result.getRemoteUpdate("refs/heads/x");
  375. assertEquals(update.getStatus(), RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED);
  376. }
  377. }
  378. }
  379. }