From aa4d277e873c8c77a13cc82736b3697e5fe15da0 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 8 Feb 2009 12:46:13 -0800 Subject: [PATCH] Don't keep an empty pack uploaded through receive-pack The receive-pack protocol sometimes gets an empty pack file if there are commands to process but no data was needed to be sent. In such cases we shouldn't create an empty 32 byte pack on disk, but instead should discard it. Since ReceivePack doesn't read the pack header, we only know the empty state inside of IndexPack. So we fix it here, to avoid duplicating the header parsing code into ReceivePack. Signed-off-by: Shawn O. Pearce Signed-off-by: Robin Rosenberg --- .../tst/org/spearce/jgit/lib/PackWriterTest.java | 1 + .../src/org/spearce/jgit/transport/IndexPack.java | 34 ++++++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/PackWriterTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/PackWriterTest.java index 98e0d3af..e828cccc 100644 --- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/PackWriterTest.java +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/PackWriterTest.java @@ -468,6 +468,7 @@ public class PackWriterTest extends RepositoryTestCase { } final InputStream is = new ByteArrayInputStream(os.toByteArray()); final IndexPack indexer = new IndexPack(db, is, packBase); + indexer.setKeepEmpty(true); indexer.setFixThin(thin); indexer.index(new TextProgressMonitor()); pack = new PackFile(db, indexFile, packFile); diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java b/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java index 82cd6155..e0e48556 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java @@ -143,6 +143,8 @@ public class IndexPack { private boolean fixThin; + private boolean keepEmpty; + private int outputVersion; private final File dstPack; @@ -239,6 +241,19 @@ public class IndexPack { } /** + * Configure this index pack instance to keep an empty pack. + *

+ * By default an empty pack (a pack with no objects) is not kept, as doing + * so is completely pointless. With no objects in the pack there is no data + * stored by it, so the pack is unnecessary. + * + * @param empty true to enable keeping an empty pack. + */ + public void setKeepEmpty(final boolean empty) { + keepEmpty = empty; + } + + /** * Configure the checker used to validate received objects. *

* Usually object checking isn't necessary, as Git implementations only @@ -313,14 +328,14 @@ public class IndexPack { fixThinPack(progress); } } - if (packOut != null) + if (packOut != null && (keepEmpty || entryCount > 0)) packOut.getChannel().force(true); packDigest = null; baseById = null; baseByPos = null; - if (dstIdx != null) + if (dstIdx != null && (keepEmpty || entryCount > 0)) writeIdx(); } finally { @@ -336,10 +351,12 @@ public class IndexPack { packOut.close(); } - if (dstPack != null) - dstPack.setReadOnly(); - if (dstIdx != null) - dstIdx.setReadOnly(); + if (keepEmpty || entryCount > 0) { + if (dstPack != null) + dstPack.setReadOnly(); + if (dstIdx != null) + dstIdx.setReadOnly(); + } } catch (IOException err) { if (dstPack != null) dstPack.delete(); @@ -946,6 +963,11 @@ public class IndexPack { * removed prior to throwing the exception to the caller. */ public void renameAndOpenPack() throws IOException { + if (!keepEmpty && entryCount == 0) { + cleanupTemporaryFiles(); + return; + } + final MessageDigest d = Constants.newMessageDigest(); final byte[] oeBytes = new byte[Constants.OBJECT_ID_LENGTH]; for (int i = 0; i < entryCount; i++) { -- 2.11.4.GIT