aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit
diff options
context:
space:
mode:
authorDavid Turner <dturner@twosigma.com>2017-01-03 23:56:08 -0500
committerDavid Turner <dturner@twosigma.com>2017-02-08 19:42:33 -0500
commit46d35a85025ff2e5184422a79451ce724a69a9ec (patch)
tree694c512aafccb4b47ab6804461fa3f3953c18e91 /org.eclipse.jgit.test/tst/org/eclipse/jgit
parentdb776102566a472e76073997cddf5417cc02389b (diff)
downloadjgit-46d35a85025ff2e5184422a79451ce724a69a9ec.tar.gz
jgit-46d35a85025ff2e5184422a79451ce724a69a9ec.zip
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 <dturner@twosigma.com>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java54
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TransportTest.java41
2 files changed, 95 insertions, 0 deletions
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<PushResult> 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);
+ }
+ }
+ }
}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TransportTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TransportTest.java
index 5519f61ac2..d4c47d37e3 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TransportTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TransportTest.java
@@ -53,7 +53,9 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Constants;
@@ -209,6 +211,45 @@ public class TransportTest extends SampleDataRepositoryTestCase {
assertEquals(ObjectId.zeroId(), tru.getOldObjectId());
}
+ /**
+ * Test RefSpec to RemoteRefUpdate conversion with leases.
+ *
+ * @throws IOException
+ */
+ @Test
+ public void testFindRemoteRefUpdatesWithLeases() throws IOException {
+ final RefSpec specA = new RefSpec("+refs/heads/a:refs/heads/b");
+ final RefSpec specC = new RefSpec("+refs/heads/c:refs/heads/d");
+ final Collection<RefSpec> specs = Arrays.asList(specA, specC);
+ final Map<String, RefLeaseSpec> leases = new HashMap<>();
+ leases.put("refs/heads/b",
+ new RefLeaseSpec("refs/heads/b", "refs/heads/c"));
+
+ Collection<RemoteRefUpdate> result;
+ try (Transport transport = Transport.open(db, remoteConfig)) {
+ result = transport.findRemoteRefUpdatesFor(specs, leases);
+ }
+
+ assertEquals(2, result.size());
+ boolean foundA = false;
+ boolean foundC = false;
+ for (final RemoteRefUpdate rru : result) {
+ if ("refs/heads/a".equals(rru.getSrcRef())
+ && "refs/heads/b".equals(rru.getRemoteName())) {
+ foundA = true;
+ assertEquals(db.exactRef("refs/heads/c").getObjectId(),
+ rru.getExpectedOldObjectId());
+ }
+ if ("refs/heads/c".equals(rru.getSrcRef())
+ && "refs/heads/d".equals(rru.getRemoteName())) {
+ foundC = true;
+ assertNull(rru.getExpectedOldObjectId());
+ }
+ }
+ assertTrue(foundA);
+ assertTrue(foundC);
+ }
+
@Test
public void testLocalTransportWithRelativePath() throws Exception {
Repository other = createWorkRepository();