Ensure FileCommitGraph scans commit-graph file if it already exists

When commit-graph file already exists in the repository, a newly
created FileCommitGraph didn't scan CommitGraph until the file was
modified, resulting in wrong result.

Change-Id: Ic85676f2d3b6a88f3ae28d4065729926b6fb2f23
Signed-off-by: kylezhao <kylezhao@tencent.com>
This commit is contained in:
kylezhao 2023-03-27 14:48:31 +08:00 committed by Matthias Sohn
parent 67fcf76b4b
commit 827849017d
2 changed files with 11 additions and 3 deletions

View File

@ -278,6 +278,7 @@ public void testGetCommitGraph() throws Exception {
ConfigConstants.CONFIG_COMMIT_GRAPH, true);
db.getConfig().setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true);
db.getConfig().save();
// no commit-graph
ObjectDirectory dir = db.getObjectDatabase();
@ -294,6 +295,13 @@ public void testGetCommitGraph() throws Exception {
assertTrue(dir.getCommitGraph().isPresent());
assertEquals(1, dir.getCommitGraph().get().getCommitCnt());
// get commit-graph in a newly created db
try (FileRepository repo2 = new FileRepository(db.getDirectory())) {
ObjectDirectory dir2 = repo2.getObjectDatabase();
assertTrue(dir2.getCommitGraph().isPresent());
assertEquals(1, dir2.getCommitGraph().get().getCommitCnt());
}
// update commit-graph
commitFile("file2.txt", "content", "master");
gc.gc().get();

View File

@ -85,10 +85,10 @@ private static final class GraphSnapshot {
private final CommitGraph graph;
GraphSnapshot(@NonNull File file) {
this(file, FileSnapshot.save(file), null);
this(file, null, null);
}
GraphSnapshot(@NonNull File file, @NonNull FileSnapshot snapshot,
GraphSnapshot(@NonNull File file, FileSnapshot snapshot,
CommitGraph graph) {
this.file = file;
this.snapshot = snapshot;
@ -104,7 +104,7 @@ GraphSnapshot refresh() {
// commit-graph file didn't exist
return this;
}
if (!snapshot.isModified(file)) {
if (snapshot != null && !snapshot.isModified(file)) {
// commit-graph file was not modified
return this;
}