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

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