]> source.dussan.org Git - jgit.git/commitdiff
Ensure FileCommitGraph scans commit-graph file if it already exists 71/200871/5
authorkylezhao <kylezhao@tencent.com>
Mon, 27 Mar 2023 06:48:31 +0000 (14:48 +0800)
committerMatthias Sohn <matthias.sohn@sap.com>
Mon, 27 Mar 2023 08:51:07 +0000 (10:51 +0200)
When commit-graph file already exists in the repository, a newly
created FileCommitGraph didn't scan CommitGraph until the file was
modified, resulting in wrong result.

Change-Id: Ic85676f2d3b6a88f3ae28d4065729926b6fb2f23
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/FileCommitGraph.java

index d74766dbf52b82374011bfcb231c17e3a30f4710..746a0a1ff3e6b5a87d2fad47838a04ae618b1e01 100644 (file)
@@ -278,6 +278,7 @@ public class ObjectDirectoryTest extends RepositoryTestCase {
                                ConfigConstants.CONFIG_COMMIT_GRAPH, true);
                db.getConfig().setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
                                ConfigConstants.CONFIG_KEY_WRITE_COMMIT_GRAPH, true);
+               db.getConfig().save();
 
                // no commit-graph
                ObjectDirectory dir = db.getObjectDatabase();
@@ -294,6 +295,13 @@ public class ObjectDirectoryTest extends RepositoryTestCase {
                assertTrue(dir.getCommitGraph().isPresent());
                assertEquals(1, dir.getCommitGraph().get().getCommitCnt());
 
+               // get commit-graph in a newly created db
+               try (FileRepository repo2 = new FileRepository(db.getDirectory())) {
+                       ObjectDirectory dir2 = repo2.getObjectDatabase();
+                       assertTrue(dir2.getCommitGraph().isPresent());
+                       assertEquals(1, dir2.getCommitGraph().get().getCommitCnt());
+               }
+
                // update commit-graph
                commitFile("file2.txt", "content", "master");
                gc.gc().get();
index 3e411a125a8d7777b07524432a6d2dd38776a081..44429a7786dd4311de713daf8b090f77976cbc09 100644 (file)
@@ -85,10 +85,10 @@ public class FileCommitGraph {
                private final CommitGraph graph;
 
                GraphSnapshot(@NonNull File file) {
-                       this(file, FileSnapshot.save(file), null);
+                       this(file, null, null);
                }
 
-               GraphSnapshot(@NonNull File file, @NonNull FileSnapshot snapshot,
+               GraphSnapshot(@NonNull File file, FileSnapshot snapshot,
                                CommitGraph graph) {
                        this.file = file;
                        this.snapshot = snapshot;
@@ -104,7 +104,7 @@ public class FileCommitGraph {
                                // commit-graph file didn't exist
                                return this;
                        }
-                       if (!snapshot.isModified(file)) {
+                       if (snapshot != null && !snapshot.isModified(file)) {
                                // commit-graph file was not modified
                                return this;
                        }