From 05e5e9907c6f85f8662ab9b78e5df90a62919f90 Mon Sep 17 00:00:00 2001 From: kylezhao Date: Fri, 6 Jan 2023 15:24:14 +0800 Subject: [PATCH] GC: disable writing commit-graph for shallow repos In shallow repos, GC writes to the commit-graph that shallow commits do not have parents. This won't be true after a "git fetch --unshallow" (and before another GC). Do not write the commit-graph from shallow clones of a repo. The commit-graph must have the real metadata of commits and that is not available in a shallow view of the repo. Change-Id: Ic9f2358ddaa607c74f4dbf289c9bf2a2f0af9ce0 Signed-off-by: kylezhao --- .../storage/file/GcCommitGraphTest.java | 19 +++++++++++++++++++ .../jgit/internal/storage/file/GC.java | 3 +++ 2 files changed, 22 insertions(+) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcCommitGraphTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcCommitGraphTest.java index 0a0d85c8a..358e19ef6 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcCommitGraphTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcCommitGraphTest.java @@ -58,6 +58,25 @@ public void testWriteEmptyRepo() throws Exception { assertFalse(graphFile.exists()); } + @Test + public void testWriteShallowRepo() throws Exception { + StoredConfig config = repo.getConfig(); + config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_COMMIT_GRAPH, true); + config.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null, + ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true); + + RevCommit tip = commitChain(2); + TestRepository.BranchBuilder bb = tr.branch("refs/heads/master"); + bb.update(tip); + repo.getObjectDatabase().setShallowCommits(Collections.singleton(tip)); + + gc.writeCommitGraph(Collections.singleton(tip)); + File graphFile = new File(repo.getObjectsDirectory(), + Constants.INFO_COMMIT_GRAPH); + assertFalse(graphFile.exists()); + } + @Test public void testWriteWhenGc() throws Exception { StoredConfig config = repo.getConfig(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java index c9ecebe80..273d658a1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java @@ -913,6 +913,9 @@ void writeCommitGraph(@NonNull Set wants) if (!repo.getConfig().get(CoreConfig.KEY).enableCommitGraph()) { return; } + if (repo.getObjectDatabase().getShallowCommits().size() > 0) { + return; + } checkCancelled(); if (wants.isEmpty()) { return;