From 207dd4c938830e84c9101d30edb7fe626e04bbe1 Mon Sep 17 00:00:00 2001 From: Robin Müller Date: Fri, 13 May 2022 13:46:13 +0200 Subject: Fetch: add support for shallow This adds support for shallow cloning. The CloneCommand and the FetchCommand now have the new methods setDepth, setShallowSince and addShallowExclude to tell the server that the client doesn't want to download the complete history. Bug: 475615 Change-Id: Ic80fb6efb5474543ae59be590ebe385bec21cc0d --- .../eclipse/jgit/http/test/HttpClientTests.java | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) (limited to 'org.eclipse.jgit.http.test/tst/org/eclipse') diff --git a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java index 3438c52c8d..6b067273a9 100644 --- a/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java +++ b/org.eclipse.jgit.http.test/tst/org/eclipse/jgit/http/test/HttpClientTests.java @@ -24,13 +24,16 @@ import java.io.OutputStream; import java.net.URI; import java.net.URL; import java.text.MessageFormat; +import java.time.Instant; import java.util.List; +import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jgit.api.Git; import org.eclipse.jgit.errors.NoRemoteRepositoryException; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.errors.TransportException; @@ -408,4 +411,110 @@ public class HttpClientTests extends AllFactoriesHttpTestCase { assertEquals(200, c.getResponseCode()); } + + @Test + public void testCloneWithDepth() throws Exception { + remoteRepository.getRepository().getConfig().setInt( + "protocol", null, "version", 0); + File directory = createTempDirectory("testCloneWithDepth"); + Git git = Git.cloneRepository() + .setDirectory(directory) + .setDepth(1) + .setURI(smartAuthNoneURI.toString()) + .call(); + + assertEquals(Set.of(git.getRepository().resolve(Constants.HEAD)), git.getRepository().getObjectDatabase().getShallowCommits()); + } + + @Test + public void testCloneWithDeepenSince() throws Exception { + remoteRepository.getRepository().getConfig().setInt( + "protocol", null, "version", 0); + RevCommit commit = remoteRepository.commit() + .parent(remoteRepository.git().log().call().iterator().next()) + .message("Test") + .add("test.txt", "Hello world") + .create(); + remoteRepository.update(master, commit); + + File directory = createTempDirectory("testCloneWithDeepenSince"); + Git git = Git.cloneRepository() + .setDirectory(directory) + .setShallowSince(Instant.ofEpochSecond(commit.getCommitTime())) + .setURI(smartAuthNoneURI.toString()) + .call(); + + assertEquals(Set.of(git.getRepository().resolve(Constants.HEAD)), git.getRepository().getObjectDatabase().getShallowCommits()); + } + + @Test + public void testCloneWithDeepenNot() throws Exception { + remoteRepository.getRepository().getConfig().setInt( + "protocol", null, "version", 0); + RevCommit commit = remoteRepository.git().log().call().iterator().next(); + remoteRepository.update(master, remoteRepository.commit() + .parent(commit) + .message("Test") + .add("test.txt", "Hello world") + .create()); + + File directory = createTempDirectory("testCloneWithDeepenNot"); + Git git = Git.cloneRepository() + .setDirectory(directory) + .addShallowExclude(commit.getId()) + .setURI(smartAuthNoneURI.toString()) + .call(); + + assertEquals(Set.of(git.getRepository().resolve(Constants.HEAD)), git.getRepository().getObjectDatabase().getShallowCommits()); + } + + @Test + public void testV2CloneWithDepth() throws Exception { + File directory = createTempDirectory("testV2CloneWithDepth"); + Git git = Git.cloneRepository() + .setDirectory(directory) + .setDepth(1) + .setURI(smartAuthNoneURI.toString()) + .call(); + + assertEquals(Set.of(git.getRepository().resolve(Constants.HEAD)), git.getRepository().getObjectDatabase().getShallowCommits()); + } + + @Test + public void testV2CloneWithDeepenSince() throws Exception { + RevCommit commit = remoteRepository.commit() + .parent(remoteRepository.git().log().call().iterator().next()) + .message("Test") + .add("test.txt", "Hello world") + .create(); + remoteRepository.update(master, commit); + + File directory = createTempDirectory("testV2CloneWithDeepenSince"); + Git git = Git.cloneRepository() + .setDirectory(directory) + .setShallowSince(Instant.ofEpochSecond(commit.getCommitTime())) + .setURI(smartAuthNoneURI.toString()) + .call(); + + assertEquals(Set.of(git.getRepository().resolve(Constants.HEAD)), git.getRepository().getObjectDatabase().getShallowCommits()); + } + + @Test + public void testV2CloneWithDeepenNot() throws Exception { + RevCommit commit = remoteRepository.git().log().call().iterator().next(); + remoteRepository.update(master, remoteRepository.commit() + .parent(commit) + .message("Test") + .add("test.txt", "Hello world") + .create()); + + File directory = createTempDirectory("testV2CloneWithDeepenNot"); + Git git = Git.cloneRepository() + .setDirectory(directory) + .addShallowExclude(commit.getId()) + .setURI(smartAuthNoneURI.toString()) + .call(); + + assertEquals(Set.of(git.getRepository().resolve(Constants.HEAD)), git.getRepository().getObjectDatabase().getShallowCommits()); + } } -- cgit v1.2.3