aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcReverseIndexTest.java
blob: cbb09434268b699f5b9680153534212c5722b7fb (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
113
114
115
116
/*
 * Copyright (C) 2023, 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
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.internal.storage.file;

import static org.eclipse.jgit.internal.storage.pack.PackExt.REVERSE_INDEX;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collections;

import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.pack.PackConfig;
import org.eclipse.jgit.util.IO;
import org.junit.Test;

public class GcReverseIndexTest extends GcTestCase {

	@Test
	public void testWriteDefault() throws Exception {
		PackConfig config = new PackConfig(repo);
		gc.setPackConfig(config);

		RevCommit tip = commitChain(10);
		TestRepository.BranchBuilder bb = tr.branch("refs/heads/main");
		bb.update(tip);

		gc.gc().get();
		assertRidxDoesNotExist(repo);
	}

	@Test
	public void testWriteDisabled() throws Exception {
		PackConfig config = new PackConfig(repo);
		config.setWriteReverseIndex(false);
		gc.setPackConfig(config);

		RevCommit tip = commitChain(10);
		TestRepository.BranchBuilder bb = tr.branch("refs/heads/main");
		bb.update(tip);

		gc.gc().get();
		assertRidxDoesNotExist(repo);
	}

	@Test
	public void testWriteEmptyRepo() throws Exception {
		PackConfig config = new PackConfig(repo);
		config.setWriteReverseIndex(true);
		gc.setPackConfig(config);

		gc.gc().get();
		assertRidxDoesNotExist(repo);
	}

	@Test
	public void testWriteShallowRepo() throws Exception {
		PackConfig config = new PackConfig(repo);
		config.setWriteReverseIndex(true);
		gc.setPackConfig(config);

		RevCommit tip = commitChain(2);
		TestRepository.BranchBuilder bb = tr.branch("refs/heads/main");
		bb.update(tip);
		repo.getObjectDatabase().setShallowCommits(Collections.singleton(tip));

		gc.gc().get();
		assertValidRidxExists(repo);
	}

	@Test
	public void testWriteEnabled() throws Exception {
		PackConfig config = new PackConfig(repo);
		config.setWriteReverseIndex(true);
		gc.setPackConfig(config);

		RevCommit tip = commitChain(10);
		TestRepository.BranchBuilder bb = tr.branch("refs/heads/main");
		bb.update(tip);

		gc.gc().get();
		assertValidRidxExists(repo);
	}

	private static void assertValidRidxExists(FileRepository repo)
			throws Exception {
		PackFile packFile = repo.getObjectDatabase().getPacks().iterator()
				.next().getPackFile();
		File file = packFile.create(REVERSE_INDEX);
		assertTrue(file.exists());
		try (InputStream os = new FileInputStream(file)) {
			byte[] magic = new byte[4];
			IO.readFully(os, magic, 0, 4);
			assertArrayEquals(new byte[] { 'R', 'I', 'D', 'X' }, magic);
		}
	}

	private static void assertRidxDoesNotExist(FileRepository repo) {
		File packDir = repo.getObjectDatabase().getPackDirectory();
		String[] reverseIndexFilenames = packDir.list(
				(dir, name) -> name.endsWith(REVERSE_INDEX.getExtension()));
		assertEquals(0, reverseIndexFilenames.length);
	}
}