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 <kylezhao@tencent.com>
This commit is contained in:
kylezhao 2022-12-12 14:49:16 +08:00
parent 93ac99b52a
commit 414bfe05ff
4 changed files with 57 additions and 1 deletions

View File

@ -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();

View File

@ -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<CommitGraph> getCommitGraph() {
return Optional.ofNullable(fileCommitGraph.get());
if (config.get(CoreConfig.KEY).enableCommitGraph()) {
return Optional.ofNullable(fileCommitGraph.get());
}
return Optional.empty();
}
/**

View File

@ -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<CommitGraph> getCommitGraph() {
return db.getCommitGraph();
}
/** {@inheritDoc} */
@Override
public Collection<CachedPack> getCachedPacksAndUpdate(

View File

@ -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.
* <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 BitmapIndex getBitmapIndex() throws IOException {
return delegate().getBitmapIndex();
}
@Override
public Optional<CommitGraph> getCommitGraph() {
return delegate().getCommitGraph();
}
@Override
@Nullable
public ObjectInserter getCreatedFromInserter() {