diff options
author | Shawn Pearce <spearce@spearce.org> | 2017-02-08 22:27:03 -0500 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org <gerrit@eclipse.org> | 2017-02-08 22:27:06 -0500 |
commit | 8fce17a995efa1b1fd91578b47be854fed12b462 (patch) | |
tree | 00a82cad3856d6f55a73d0584ec5cd09a1ff23b5 /org.eclipse.jgit.test | |
parent | 6450d956bca2374696f5ad8a566db76546ec6573 (diff) | |
parent | 46d35a85025ff2e5184422a79451ce724a69a9ec (diff) | |
download | jgit-8fce17a995efa1b1fd91578b47be854fed12b462.tar.gz jgit-8fce17a995efa1b1fd91578b47be854fed12b462.zip |
Merge "push: support per-ref force-with-lease"
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PushCommandTest.java | 54 | ||||
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/transport/TransportTest.java | 41 |
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(); |