diff options
author | Ivan Frade <ifrade@google.com> | 2025-04-15 15:34:29 -0700 |
---|---|---|
committer | Ivan Frade <ifrade@google.com> | 2025-04-15 15:41:24 -0700 |
commit | f627e89147c437de9d2f527b76bb599323919e73 (patch) | |
tree | c57678a6d51fa969efaebd1c2af4055d618f52b3 | |
parent | 670c17b80eea0ea7beaeb3c2d8f528d1a6a52cbf (diff) | |
download | jgit-f627e89147c437de9d2f527b76bb599323919e73.tar.gz jgit-f627e89147c437de9d2f527b76bb599323919e73.zip |
MultiPackIndexWriter: Handle empty packs
If a pack doesn't have data, its offsets are null, not empty. A pack
without objects is probably a pathological case, but I bumped into
this while writing other tests.
Check if the offsets are null (instead of empty) before trying to
write the ridx.
Change-Id: I8cadea086b302b2ead02a5a8d4e9e8adf85bc07b
2 files changed, 19 insertions, 1 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/midx/MultiPackIndexWriterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/midx/MultiPackIndexWriterTest.java index 82f3eb1e08..f75ae355b4 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/midx/MultiPackIndexWriterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/midx/MultiPackIndexWriterTest.java @@ -139,6 +139,24 @@ public class MultiPackIndexWriterTest { assertEquals(5, chunkIds.indexOf(MIDX_CHUNKID_PACKNAMES)); } + @Test + public void jgit_emptyMidx() throws IOException { + PackIndex idxOne = FakeIndexFactory.indexOf(List.of()); + PackIndex idxTwo = FakeIndexFactory.indexOf(List.of()); + Map<String, PackIndex> packs = Map.of("p1", idxOne, "p2", idxTwo); + MultiPackIndexWriter writer = new MultiPackIndexWriter(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + writer.write(NullProgressMonitor.INSTANCE, out, packs); + List<Integer> chunkIds = readChunkIds(out); + assertEquals(1134, out.size()); + assertEquals(5, chunkIds.size()); + assertEquals(0, chunkIds.indexOf(MIDX_CHUNKID_OIDFANOUT)); + assertEquals(1, chunkIds.indexOf(MIDX_CHUNKID_OIDLOOKUP)); + assertEquals(2, chunkIds.indexOf(MIDX_CHUNKID_OBJECTOFFSETS)); + assertEquals(3, chunkIds.indexOf(MIDX_CHUNKID_REVINDEX)); + assertEquals(4, chunkIds.indexOf(MIDX_CHUNKID_PACKNAMES)); + } + private List<Integer> readChunkIds(ByteArrayOutputStream out) { List<Integer> chunkIds = new ArrayList<>(); byte[] raw = out.toByteArray(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/midx/MultiPackIndexWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/midx/MultiPackIndexWriter.java index 5488188b7c..9d37450a46 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/midx/MultiPackIndexWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/midx/MultiPackIndexWriter.java @@ -299,7 +299,7 @@ public class MultiPackIndexWriter { for (int i = 0; i < ctx.data.getPackCount(); i++) { List<OffsetPosition> offsetsForPack = packOffsets .get(Integer.valueOf(i)); - if (offsetsForPack.isEmpty()) { + if (offsetsForPack == null) { continue; } offsetsForPack.sort(Comparator.comparing(OffsetPosition::offset)); |