diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/PackWriterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/PackWriterTest.java index 7a829dee0..20e32b22c 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/PackWriterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/storage/file/PackWriterTest.java @@ -58,7 +58,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; -import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -125,9 +124,11 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { /** * Test constructor for exceptions, default settings, initialization. + * + * @throws IOException */ @Test - public void testContructor() { + public void testContructor() throws IOException { writer = new PackWriter(config, db.newObjectReader()); assertEquals(false, writer.isDeltaBaseAsOffset()); assertEquals(true, config.isReuseDeltas()); @@ -177,7 +178,7 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { */ @Test public void testWriteEmptyPack2() throws IOException { - createVerifyOpenPack(EMPTY_LIST_REVS.iterator()); + createVerifyOpenPack(EMPTY_LIST_REVS); assertEquals(0, writer.getObjectsNumber()); assertEquals(0, pack.getObjectCount()); @@ -320,7 +321,7 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { for (int i = 0; i < forcedOrder.length; i++) forcedOrderRevs[i] = parser.parseAny(forcedOrder[i]); - createVerifyOpenPack(Arrays.asList(forcedOrderRevs).iterator()); + createVerifyOpenPack(Arrays.asList(forcedOrderRevs)); assertEquals(forcedOrder.length, writer.getObjectsNumber()); verifyObjectsOrder(forcedOrder); @@ -544,11 +545,12 @@ public class PackWriterTest extends SampleDataRepositoryTestCase { verifyOpenPack(thin); } - private void createVerifyOpenPack(final Iterator objectSource) + private void createVerifyOpenPack(final List objectSource) throws MissingObjectException, IOException { NullProgressMonitor m = NullProgressMonitor.INSTANCE; writer = new PackWriter(config, db.newObjectReader()); - writer.preparePack(objectSource); + writer.preparePack(objectSource.iterator()); + assertEquals(objectSource.size(), writer.getObjectsNumber()); writer.writePack(m, m, os); writer.release(); verifyOpenPack(false); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java b/org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java index 2eb2700dd..5039eed2a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/storage/pack/PackWriter.java @@ -402,8 +402,18 @@ public class PackWriter { * Returns objects number in a pack file that was created by this writer. * * @return number of objects in pack. + * @throws IOException + * a cached pack cannot supply its object count. */ - public long getObjectsNumber() { + public long getObjectsNumber() throws IOException { + if (stats.totalObjects == 0) { + long objCnt = 0; + for (List list : objectsLists) + objCnt += list.size(); + for (CachedPack pack : cachedPacks) + objCnt += pack.getObjectCount(); + return objCnt; + } return stats.totalObjects; } @@ -635,13 +645,8 @@ public class PackWriter { final PackOutputStream out = new PackOutputStream(writeMonitor, packStream, this); - long objCnt = 0; - for (List list : objectsLists) - objCnt += list.size(); - for (CachedPack pack : cachedPacks) - objCnt += pack.getObjectCount(); + long objCnt = getObjectsNumber(); stats.totalObjects = objCnt; - writeMonitor.beginTask(JGitText.get().writingObjects, (int) objCnt); long writeStart = System.currentTimeMillis();