From 414bfe05ff3c9fbca5767116348a4de1d7b95841 Mon Sep 17 00:00:00 2001 From: kylezhao Date: Mon, 12 Dec 2022 14:49:16 +0800 Subject: [PATCH] CommitGraph: teach ObjectReader to get commit-graph FileRepository's ObjectReader#getCommitGraph will return commit-graph when it exists and core.commitGraph is true. DfsRepository is not supported currently. Change-Id: I992d43d104cf542797e6949470e95e56de025107 Signed-off-by: kylezhao --- .../storage/file/ObjectDirectoryTest.java | 20 ++++++++++++++++ .../storage/file/ObjectDirectory.java | 6 ++++- .../internal/storage/file/WindowCursor.java | 8 +++++++ .../org/eclipse/jgit/lib/ObjectReader.java | 24 +++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) 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 b4ebdcdf8..ddc4a9d0b 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 @@ -236,6 +236,26 @@ public void testOpenLooseObjectPropagatesIOExceptions() throws Exception { assertThrows(IOException.class, () -> mock.open(curs, id)); } + @Test + public void testWindowCursorGetCommitGraph() throws Exception { + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_COMMIT_GRAPH, true); + db.getConfig().setBoolean(ConfigConstants.CONFIG_GC_SECTION, null, + ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true); + + WindowCursor curs = new WindowCursor(db.getObjectDatabase()); + assertTrue(curs.getCommitGraph().isEmpty()); + commitFile("file.txt", "content", "master"); + GC gc = new GC(db); + gc.gc(); + assertTrue(curs.getCommitGraph().isPresent()); + + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_COMMIT_GRAPH, false); + + assertTrue(curs.getCommitGraph().isEmpty()); + } + @Test public void testShallowFileCorrupt() throws Exception { FileRepository repository = createBareRepository(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java index cb91c7931..6be362085 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java @@ -41,6 +41,7 @@ import org.eclipse.jgit.internal.storage.commitgraph.CommitGraph; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.CoreConfig; import org.eclipse.jgit.lib.ObjectDatabase; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectLoader; @@ -235,7 +236,10 @@ public long getApproximateObjectCount() { /** {@inheritDoc} */ @Override public Optional getCommitGraph() { - return Optional.ofNullable(fileCommitGraph.get()); + if (config.get(CoreConfig.KEY).enableCommitGraph()) { + return Optional.ofNullable(fileCommitGraph.get()); + } + return Optional.empty(); } /** diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java index e7fd7b9e7..fa743babe 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java @@ -16,6 +16,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.zip.DataFormatException; import java.util.zip.Inflater; @@ -34,6 +35,7 @@ import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.BitmapIndex; import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder; +import org.eclipse.jgit.internal.storage.commitgraph.CommitGraph; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.InflaterCache; import org.eclipse.jgit.lib.ObjectId; @@ -94,6 +96,12 @@ public BitmapIndex getBitmapIndex() throws IOException { return null; } + /** {@inheritDoc} */ + @Override + public Optional getCommitGraph() { + return db.getCommitGraph(); + } + /** {@inheritDoc} */ @Override public Collection getCachedPacksAndUpdate( diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java index 081f40e9d..ae0cf42b1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java @@ -17,6 +17,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.Optional; import java.util.Set; import org.eclipse.jgit.annotations.NonNull; @@ -27,6 +28,7 @@ import org.eclipse.jgit.internal.revwalk.BitmappedReachabilityChecker; import org.eclipse.jgit.internal.revwalk.PedestrianObjectReachabilityChecker; import org.eclipse.jgit.internal.revwalk.PedestrianReachabilityChecker; +import org.eclipse.jgit.internal.storage.commitgraph.CommitGraph; import org.eclipse.jgit.revwalk.ObjectReachabilityChecker; import org.eclipse.jgit.revwalk.ObjectWalk; import org.eclipse.jgit.revwalk.ReachabilityChecker; @@ -499,6 +501,23 @@ public ObjectReachabilityChecker createObjectReachabilityChecker( return new PedestrianObjectReachabilityChecker(ow); } + /** + * Get the commit-graph for this repository if available. + *

+ * The commit graph can be created/modified/deleted while the repository is + * open and specific implementations decide when to refresh it. + * + * @return the commit-graph or empty if the commit-graph does not exist or + * is invalid; always returns empty when core.commitGraph is false + * (default is + * {@value org.eclipse.jgit.lib.CoreConfig#DEFAULT_COMMIT_GRAPH_ENABLE}). + * + * @since 6.5 + */ + public Optional getCommitGraph() { + return Optional.empty(); + } + /** * Get the {@link org.eclipse.jgit.lib.ObjectInserter} from which this * reader was created using {@code inserter.newReader()} @@ -641,6 +660,11 @@ public BitmapIndex getBitmapIndex() throws IOException { return delegate().getBitmapIndex(); } + @Override + public Optional getCommitGraph() { + return delegate().getCommitGraph(); + } + @Override @Nullable public ObjectInserter getCreatedFromInserter() {