From 46d35a85025ff2e5184422a79451ce724a69a9ec Mon Sep 17 00:00:00 2001 From: David Turner Date: Tue, 3 Jan 2017 23:56:08 -0500 Subject: push: support per-ref force-with-lease When rebasing, force-pushing has a race condition: someone else might have pushed a commit since the one you just rewrote. The force-with-lease option prevents this by ensuring that the ref's old value is the one that you expected. Change-Id: I97ca9f8395396c76332bdd07c486e60549ca4401 Signed-off-by: David Turner --- .../tst/org/eclipse/jgit/api/PushCommandTest.java | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/api') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java index 2a325405e8..eaf64b649f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java @@ -66,8 +66,10 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.transport.PushResult; +import org.eclipse.jgit.transport.RefLeaseSpec; import org.eclipse.jgit.transport.RefSpec; import org.eclipse.jgit.transport.RemoteConfig; +import org.eclipse.jgit.transport.RemoteRefUpdate; import org.eclipse.jgit.transport.TrackingRefUpdate; import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.util.FS; @@ -379,4 +381,56 @@ public class PushCommandTest extends RepositoryTestCase { db2.resolve(commit3.getId().getName() + "^{commit}")); } } + + @Test + public void testPushWithLease() throws JGitInternalException, IOException, + GitAPIException, URISyntaxException { + + // create other repository + Repository db2 = createWorkRepository(); + + // setup the first repository + final StoredConfig config = db.getConfig(); + RemoteConfig remoteConfig = new RemoteConfig(config, "test"); + URIish uri = new URIish(db2.getDirectory().toURI().toURL()); + remoteConfig.addURI(uri); + remoteConfig.update(config); + config.save(); + + try (Git git1 = new Git(db)) { + // create one commit and push it + RevCommit commit = git1.commit().setMessage("initial commit").call(); + Ref branchRef = git1.branchCreate().setName("initial").call(); + + RefSpec spec = new RefSpec("refs/heads/master:refs/heads/x"); + git1.push().setRemote("test").setRefSpecs(spec) + .call(); + + assertEquals(commit.getId(), + db2.resolve(commit.getId().getName() + "^{commit}")); + //now try to force-push a new commit, with a good lease + + RevCommit commit2 = git1.commit().setMessage("second commit").call(); + Iterable results = + git1.push().setRemote("test").setRefSpecs(spec) + .setRefLeaseSpecs(new RefLeaseSpec("refs/heads/x", "initial")) + .call(); + for (PushResult result : results) { + RemoteRefUpdate update = result.getRemoteUpdate("refs/heads/x"); + assertEquals(update.getStatus(), RemoteRefUpdate.Status.OK); + } + + RevCommit commit3 = git1.commit().setMessage("third commit").call(); + //now try to force-push a new commit, with a bad lease + + results = + git1.push().setRemote("test").setRefSpecs(spec) + .setRefLeaseSpecs(new RefLeaseSpec("refs/heads/x", "initial")) + .call(); + for (PushResult result : results) { + RemoteRefUpdate update = result.getRemoteUpdate("refs/heads/x"); + assertEquals(update.getStatus(), RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED); + } + } + } } -- cgit v1.2.3