From 65f9a6e58bd9296cbbe1cffc7cf079fd65991686 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 14 Mar 2011 07:06:35 -0700 Subject: [PATCH] Fix dumb transport push PackWriter incorrectly returned 0 from getObjectsNumber() when the pack has not been written yet. This caused dumb transports like amazon-s3:// and sftp:// to abort early and never write out a pack, under the assumption that the pack had no objects. Until the pack header is written to the output stream, compute the current object count each time it is requested. Once the header is started, use the object count from the stats object. Change-Id: I041a2368ae0cfe6f649ec28658d41a6355933900 Signed-off-by: Shawn O. Pearce --- .../jgit/storage/file/PackWriterTest.java | 14 ++++++++------ .../eclipse/jgit/storage/pack/PackWriter.java | 19 ++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) 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();