]> source.dussan.org Git - jgit.git/commitdiff
CommitGraph: teach ObjectReader to get commit-graph 29/197629/12
authorkylezhao <kylezhao@tencent.com>
Mon, 12 Dec 2022 06:49:16 +0000 (14:49 +0800)
committerkylezhao <kylezhao@tencent.com>
Wed, 4 Jan 2023 06:50:38 +0000 (14:50 +0800)
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 <kylezhao@tencent.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/ObjectDirectoryTest.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/WindowCursor.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/ObjectReader.java

index b4ebdcdf8e662a16ab198ced99fe3df9a43b87c0..ddc4a9d0ba55eb9d38ef0b8ddbdfbb8b974562d9 100644 (file)
@@ -236,6 +236,26 @@ public class ObjectDirectoryTest extends RepositoryTestCase {
                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();
index cb91c7931e743485755056a5e4a15b3e114cf8a7..6be36208563ffb9e1b6d2de3b58e3edbfd51ca96 100644 (file)
@@ -41,6 +41,7 @@ import org.eclipse.jgit.lib.AnyObjectId;
 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 class ObjectDirectory extends FileObjectDatabase {
        /** {@inheritDoc} */
        @Override
        public Optional<CommitGraph> getCommitGraph() {
-               return Optional.ofNullable(fileCommitGraph.get());
+               if (config.get(CoreConfig.KEY).enableCommitGraph()) {
+                       return Optional.ofNullable(fileCommitGraph.get());
+               }
+               return Optional.empty();
        }
 
        /**
index e7fd7b9e76a5a47b220aabd307cf06e398f42e85..fa743babe7fa5ee553b81d044f75ef8985a65c0f 100644 (file)
@@ -16,6 +16,7 @@ import java.util.Collection;
 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.AbbreviatedObjectId;
 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 @@ final class WindowCursor extends ObjectReader implements ObjectReuseAsIs {
                return null;
        }
 
+       /** {@inheritDoc} */
+       @Override
+       public Optional<CommitGraph> getCommitGraph() {
+               return db.getCommitGraph();
+       }
+
        /** {@inheritDoc} */
        @Override
        public Collection<CachedPack> getCachedPacksAndUpdate(
index 081f40e9dbacf52703f0456f754961164ed199c0..ae0cf42b12d764092743d9cb013f0460ae6cf104 100644 (file)
@@ -17,6 +17,7 @@ import java.util.ArrayList;
 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.BitmappedObjectReachabilityChecker;
 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 abstract class ObjectReader implements AutoCloseable {
                return new PedestrianObjectReachabilityChecker(ow);
        }
 
+       /**
+        * Get the commit-graph for this repository if available.
+        * <p>
+        * 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<CommitGraph> 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 abstract class ObjectReader implements AutoCloseable {
                        return delegate().getBitmapIndex();
                }
 
+               @Override
+               public Optional<CommitGraph> getCommitGraph() {
+                       return delegate().getCommitGraph();
+               }
+
                @Override
                @Nullable
                public ObjectInserter getCreatedFromInserter() {