From 827849017dc7a6ae269a37727d1e28e18b5aa966 Mon Sep 17 00:00:00 2001 From: kylezhao Date: Mon, 27 Mar 2023 14:48:31 +0800 Subject: [PATCH] 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 --- .../jgit/internal/storage/file/ObjectDirectoryTest.java | 8 ++++++++ .../jgit/internal/storage/file/FileCommitGraph.java | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java index d74766dbf..746a0a1ff 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java @@ -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(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileCommitGraph.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileCommitGraph.java index 3e411a125..44429a778 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileCommitGraph.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileCommitGraph.java @@ -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; }