summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test
diff options
context:
space:
mode:
authorMasaya Suzuki <masayasuzuki@google.com>2020-01-23 16:47:40 -0800
committerMasaya Suzuki <masayasuzuki@google.com>2020-09-03 22:58:37 +0000
commit9d2055152c1315819839988e44e030a63977e93c (patch)
tree8b0bf44c5d9b66987a1ebc11b6cd39ebee9417e2 /org.eclipse.jgit.test
parent957419610ad1af3791ff0c279bbc7cbadabd810d (diff)
downloadjgit-9d2055152c1315819839988e44e030a63977e93c.tar.gz
jgit-9d2055152c1315819839988e44e030a63977e93c.zip
jgit: Add DfsBundleWriter
DfsBundleWriter writes out the entire repository to a Git bundle file. It packs all objects included in the packfile by concatenating all pack files. This makes the bundle creation fast and cheap. Useful for backing up a repository as-is. Change-Id: Iee20e4b1ab45b2a178dde8c72093c0dd83f04805 Signed-off-by: Masaya Suzuki <masayasuzuki@google.com>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsBundleWriterTest.java85
1 files changed, 85 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsBundleWriterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsBundleWriterTest.java
new file mode 100644
index 0000000000..4238ee6bf0
--- /dev/null
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsBundleWriterTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2020, Google LLC and others
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+package org.eclipse.jgit.internal.storage.dfs;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Set;
+
+import org.eclipse.jgit.junit.TestRepository;
+import org.eclipse.jgit.lib.NullProgressMonitor;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.transport.FetchResult;
+import org.eclipse.jgit.transport.RefSpec;
+import org.eclipse.jgit.transport.TransportBundleStream;
+import org.eclipse.jgit.transport.URIish;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DfsBundleWriterTest {
+ private TestRepository<InMemoryRepository> git;
+
+ private InMemoryRepository repo;
+
+ @Before
+ public void setUp() throws IOException {
+ DfsRepositoryDescription desc = new DfsRepositoryDescription("test");
+ git = new TestRepository<>(new InMemoryRepository(desc));
+ repo = git.getRepository();
+ }
+
+ @Test
+ public void testRepo() throws Exception {
+ RevCommit commit0 = git.commit().message("0").create();
+ RevCommit commit1 = git.commit().message("1").parent(commit0).create();
+ git.update("master", commit1);
+
+ RevCommit commit2 = git.commit().message("0").create();
+
+ byte[] bundle = makeBundle();
+ try (Repository newRepo = new InMemoryRepository(
+ new DfsRepositoryDescription("copy"))) {
+ fetchFromBundle(newRepo, bundle);
+ Ref ref = newRepo.exactRef("refs/heads/master");
+ assertNotNull(ref);
+ assertEquals(commit1.toObjectId(), ref.getObjectId());
+
+ // Unreferenced objects are included as well.
+ assertTrue(newRepo.getObjectDatabase().has(commit2));
+ }
+ }
+
+ private byte[] makeBundle() throws IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ DfsBundleWriter.writeEntireRepositoryAsBundle(
+ NullProgressMonitor.INSTANCE, out, repo);
+ return out.toByteArray();
+ }
+
+ private static FetchResult fetchFromBundle(Repository newRepo,
+ byte[] bundle) throws Exception {
+ URIish uri = new URIish("in-memory://");
+ ByteArrayInputStream in = new ByteArrayInputStream(bundle);
+ RefSpec rs = new RefSpec("refs/heads/*:refs/heads/*");
+ Set<RefSpec> refs = Collections.singleton(rs);
+ try (TransportBundleStream transport = new TransportBundleStream(
+ newRepo, uri, in)) {
+ return transport.fetch(NullProgressMonitor.INSTANCE, refs);
+ }
+ }
+}