aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcCommitGraphTest.java
blob: dbc9dba2c4c1ca07e2a00557afc54e51f7c237f3 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2022, Tencent.
 *
 * 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.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
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.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.util.IO;
import org.junit.Test;

public class GcCommitGraphTest extends GcTestCase {

	@Test
	public void testCommitGraphConfig() {
		StoredConfig config = repo.getConfig();
		assertFalse(gc.shouldWriteCommitGraphWhenGc());

		config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
				ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true);
		assertTrue(gc.shouldWriteCommitGraphWhenGc());

		config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
				ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, false);
		assertFalse(gc.shouldWriteCommitGraphWhenGc());
	}

	@Test
	public void testWriteEmptyRepo() throws Exception {
		StoredConfig config = repo.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_COMMIT_GRAPH, true);
		config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
				ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true);

		assertTrue(gc.shouldWriteCommitGraphWhenGc());
		gc.writeCommitGraph(Collections.emptySet());
		File graphFile = new File(repo.getObjectsDirectory(),
				Constants.INFO_COMMIT_GRAPH);
		assertFalse(graphFile.exists());
	}

	@Test
	public void testWriteShallowRepo() throws Exception {
		StoredConfig config = repo.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_COMMIT_GRAPH, true);
		config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
				ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true);

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

		gc.writeCommitGraph(Collections.singleton(tip));
		File graphFile = new File(repo.getObjectsDirectory(),
				Constants.INFO_COMMIT_GRAPH);
		assertFalse(graphFile.exists());
	}

	@Test
	public void testWriteWhenGc() throws Exception {
		StoredConfig config = repo.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_COMMIT_GRAPH, true);
		config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
				ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true);

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

		assertTrue(gc.shouldWriteCommitGraphWhenGc());
		gc.gc().get();
		File graphFile = new File(repo.getObjectsDirectory(),
				Constants.INFO_COMMIT_GRAPH);
		assertGraphFile(graphFile);
	}

	@Test
	public void testDefaultWriteWhenGc() throws Exception {
		RevCommit tip = commitChain(10);
		TestRepository.BranchBuilder bb = tr.branch("refs/heads/master");
		bb.update(tip);

		assertFalse(gc.shouldWriteCommitGraphWhenGc());
		gc.gc().get();
		File graphFile = new File(repo.getObjectsDirectory(),
				Constants.INFO_COMMIT_GRAPH);
		assertFalse(graphFile.exists());
	}

	@Test
	public void testDisableWriteWhenGc() throws Exception {
		RevCommit tip = commitChain(10);
		TestRepository.BranchBuilder bb = tr.branch("refs/heads/master");
		bb.update(tip);
		File graphFile = new File(repo.getObjectsDirectory(),
				Constants.INFO_COMMIT_GRAPH);

		StoredConfig config = repo.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_COMMIT_GRAPH, false);
		config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
				ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true);

		gc.gc().get();
		assertFalse(graphFile.exists());

		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_COMMIT_GRAPH, true);
		config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
				ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, false);
		gc.gc().get();
		assertFalse(graphFile.exists());

		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_COMMIT_GRAPH, false);
		config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
				ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, false);
		gc.gc().get();
		assertFalse(graphFile.exists());
	}

	@Test
	public void testWriteCommitGraphOnly() throws Exception {
		RevCommit tip = commitChain(10);
		TestRepository.BranchBuilder bb = tr.branch("refs/heads/master");
		bb.update(tip);

		StoredConfig config = repo.getConfig();
		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_COMMIT_GRAPH, false);
		gc.writeCommitGraph(Collections.singleton(tip));

		File graphFile = new File(repo.getObjectsDirectory(),
				Constants.INFO_COMMIT_GRAPH);
		assertFalse(graphFile.exists());

		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
				ConfigConstants.CONFIG_COMMIT_GRAPH, true);
		gc.writeCommitGraph(Collections.singleton(tip));
		assertGraphFile(graphFile);
	}

	private void assertGraphFile(File graphFile) throws Exception {
		assertTrue(graphFile.exists());
		try (InputStream os = new FileInputStream(graphFile)) {
			byte[] magic = new byte[4];
			IO.readFully(os, magic, 0, 4);
			assertArrayEquals(new byte[] { 'C', 'G', 'P', 'H' }, magic);
		}
	}
}