aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsBundleWriterTest.java
blob: bce62b93713fb8de10e9d8c6ab5f83acda1b3cac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * 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.api.GarbageCollectCommand;
import org.eclipse.jgit.api.Git;
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 makeBundle_containsUnreferencedObject() 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));
		}
	}

	@Test
	public void makeBundle_containsObjectInGcRestPack() 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();

		// This moves unreachable commit2 to GC_REST pack.
		GarbageCollectCommand gc = Git.wrap(repo).gc();
		gc.call();

		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 in GC_REST pack 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);
		}
	}
}