From 8a7348df6966da39c1402c8f51fa4f7a9aeb8e7e Mon Sep 17 00:00:00 2001 From: kylezhao Date: Wed, 14 Jul 2021 10:52:10 +0800 Subject: CommitGraph: add commit-graph for FileObjectDatabase This change makes JGit can read .git/objects/info/commit-graph file and then get CommitGraph. Loading a new commit-graph into memory requires additional time. After testing, loading a copy of the Linux's commit-graph(1039139 commits) is under 50ms. Bug: 574368 Change-Id: Iadfdd6ed437945d3cdfdbe988cf541198140a8bf Signed-off-by: kylezhao --- .../internal/storage/file/ObjectDirectoryTest.java | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'org.eclipse.jgit.test') 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 1a3b3787b1..b4ebdcdf8e 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 @@ -43,6 +43,7 @@ package org.eclipse.jgit.internal.storage.file; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; @@ -251,6 +252,50 @@ public class ObjectDirectoryTest extends RepositoryTestCase { IOException.class, () -> dir.getShallowCommits()); } + @Test + public void testGetCommitGraph() 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); + + // no commit-graph + ObjectDirectory dir = db.getObjectDatabase(); + assertTrue(dir.getCommitGraph().isEmpty()); + + // add commit-graph + commitFile("file.txt", "content", "master"); + GC gc = new GC(db); + gc.gc(); + File file = new File(db.getObjectsDirectory(), + Constants.INFO_COMMIT_GRAPH); + assertTrue(file.exists()); + assertTrue(file.isFile()); + assertTrue(dir.getCommitGraph().isPresent()); + assertEquals(1, dir.getCommitGraph().get().getCommitCnt()); + + // update commit-graph + commitFile("file2.txt", "content", "master"); + gc.gc(); + assertEquals(2, dir.getCommitGraph().get().getCommitCnt()); + + // delete commit-graph + file.delete(); + assertFalse(file.exists()); + assertTrue(dir.getCommitGraph().isEmpty()); + + // commit-graph is corrupt + try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) { + writer.println("this is a corrupt commit-graph"); + } + assertTrue(dir.getCommitGraph().isEmpty()); + + // add commit-graph again + gc.gc(); + assertTrue(dir.getCommitGraph().isPresent()); + assertEquals(2, dir.getCommitGraph().get().getCommitCnt()); + } + private Collection> blobInsertersForTheSameFanOutDir( final ObjectDirectory dir) { Callable callable = () -> dir.newInserter() -- cgit v1.2.3