diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java index bab262231..afbd7e654 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/commitgraph/CommitGraphWriter.java @@ -122,6 +122,7 @@ public Stats write(@NonNull ProgressMonitor monitor, } List chunks = createChunks(stats); + long expectedSize = calculateExpectedSize(chunks); long writeCount = 256 + 2 * graphCommits.size() + graphCommits.getExtraEdgeCnt(); monitor.beginTask( @@ -135,6 +136,11 @@ public Stats write(@NonNull ProgressMonitor monitor, writeChunkLookup(out, chunks); writeChunks(monitor, out, chunks); writeCheckSum(out); + if (expectedSize != out.length()) { + throw new IllegalStateException(String.format( + "Commit-graph: expected %d bytes but out has %d bytes", //$NON-NLS-1$ + expectedSize, out.length())); + } } catch (InterruptedIOException e) { throw new IOException(JGitText.get().commitGraphWritingCancelled, e); @@ -168,6 +174,12 @@ private List createChunks(Stats stats) return chunks; } + private static long calculateExpectedSize(List chunks) { + int chunkLookup = (chunks.size() + 1) * CHUNK_LOOKUP_WIDTH; + long chunkContent = chunks.stream().mapToLong(c -> c.size).sum(); + return /* header */ 8 + chunkLookup + chunkContent + /* CRC */ 20; + } + private void writeHeader(CancellableDigestOutputStream out, int numChunks) throws IOException { byte[] headerBuffer = new byte[8];