RFC: Allow missing objects during packing
So I have this horribly bizarre case that I just feel dirty telling you about. But I have to deal with it. So here goes...
I want to publish a fork of an open source project, but I'm not allowed to publish the base history of the project. For political reasons I can only publish patches/deltas, even though the project is licensed under the LGPL. Don't ask. Seriously. In 6 months everyone will probably forget about this and I can just publish the whole thing. But not right now.
With this silly little patch to builtin-pack-objects I can make it work by publishing what amounts to a shallow clone. The fork was created by doing something like this:
$ git clone --bare git://upstream.org/proj.git upstream.git $ cd fork $ git remote add -f upstream ../upstream.git $ echo ../upstream.git/objects >.git/objects/info/alternates $ git repack -a -d -f -l
# now fork/.git has only its "delta" # add the base trees, but not blobs # $ jgit --git-dir=../upstream.git tree-copy upstream/master .
# remove the shared odb
#
$ rm .git/objects/info/alternatesUsers who want to clone from fork.git have to do this:
$ git clone -o upstream git://upstream.org/proj.git proj $ cd proj $ git remote add -f origin git://forky.org/fork.git remote: Counting objects: 5, done. remote: error: unable to find d30dcb7f07b6bf1a6cc5da4f3c1d0e6fc690dc45 remote: git: unable to get type of object d30dremote: cb7f07b6bf1a6cc5da4f3c1d0e6fc690dc45: No such file or directory remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done.
Basically this little patch just makes pack-objects issue the warning, but then skip over the object and move on.
Later during deltification the selected base object has a type which does not match any other object, so it never gets considered, and there is no error caused by it not being present. We just cannot generate a delta during the fetch and the client winds up getting a little larger download.
Thoughts? Should I work this up into a real patch and try to get it into the tree? It seems pretty harmless to allow an object we aren't going to transmit but that we want to use as a delta base in a thin pack to be missing. At worst we just get a little bit more data transfer.
--8<--
builtin-pack-objects.c +1 -1
@@ -1097,7 +1097,7 @@ static void check_object(struct object_entry *entry) entry->type =sha1_object_info (entry->idx.sha1, &entry->size);if (entry->type <0 ) -die ("unable to get type of object %s" , +warn ("unable to get type of object %s" ,sha1_to_hex (entry->idx.sha1)); }
And this is the Java program to build up the base tree, as otherwise the thin pack creation code crashes out trying to build a list of what the client already has/knows. Since this is only the base commit and its trees (no blobs, and no history beyond the base commit) I should be able to get away with publishing it as part of the fork.
--8<--
import org.kohsuke.args4j.Argument;
import org.spearce.jgit.lib.Constants;
import org.spearce.jgit.lib.ObjectId;
import org.spearce.jgit.lib.PackWriter;
import org.spearce.jgit.lib.Repository;
import org.spearce.jgit.lib.TextProgressMonitor;
import org.spearce.jgit.pgm.Command;
import org.spearce.jgit.pgm.TextBuiltin;
import org.spearce.jgit.revwalk.RevCommit;
import org.spearce.jgit.revwalk.RevObject;
import org.spearce.jgit.treewalk.TreeWalk;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedList;
import java.util.List;
@Command(name = "tree-copy", usage = "Copy a base commit and its trees") public class TreeCopy extends TextBuiltin {
@Argument(index = 0, required = true, metaVar = "commit", usage = "base commit") private RevCommit baseCommit;
@Argument(index = 1, required = true, metaVar = "dir", usage = "destination repository") private File destGitDir;
@Override
protected void run() throws Exception {
final List<RevObject> toCopy = new LinkedList<RevObject>(); toCopy.add(baseCommit);
toCopy.add(baseCommit.getTree());
final TreeWalk tw = new TreeWalk(db);
tw.reset();
tw.addTree(baseCommit.getTree());
while (tw.next()) {
switch (tw.getFileMode(0).getObjectType()) {
case Constants.OBJ_TREE:
toCopy.add(argWalk.lookupTree(tw.getObjectId(0)));
tw.enterSubtree();
break;
default:
break;
}
}
final Repository destdb = new Repository(destGitDir);
final PackWriter packer = new PackWriter(db, new TextProgressMonitor());
packer.preparePack(toCopy.iterator());
final String packName = "pack-" + packer.computeName();
final File packDir = new File(destdb.getDirectory(), "objects/pack");
final File packPath = new File(packDir, packName + ".pack");
final File idxPath = new File(packDir, packName + ".idx");
if (packPath.exists() && idxPath.exists())
throw die(packPath.getName() + " already exists.");
{
final BufferedOutputStream os =
new BufferedOutputStream(new FileOutputStream(packPath));
packer.writePack(os);
os.close();
packPath.setReadOnly();
}
{
final BufferedOutputStream os =
new BufferedOutputStream(new FileOutputStream(idxPath));
packer.writeIndex(os);
os.close();
idxPath.setReadOnly();
} }
}
Signature · 1 line
Shawn.
Quoted 4 lines · Shawn O. Pearce
... It seems pretty harmless to allow an object we aren't going to transmit but that we want to use as a delta base in a thin pack to be missing. At worst we just get a little bit more data transfer.
If the check is only about a thin delta base that is not going to be transmit, I'd agree. But I do not see how you are distinguishing that case and the case where an object you are actually sending is missing (in which case we would want to error out, wouldn't we?)
Quoted 11 lines · Junio C Hamano
Quoted 4 lines · Shawn O. Pearce
... It seems pretty harmless to allow an object we aren't going to transmit but that we want to use as a delta base in a thin pack to be missing. At worst we just get a little bit more data transfer.
If the check is only about a thin delta base that is not going to be transmit, I'd agree. But I do not see how you are distinguishing that case and the case where an object you are actually sending is missing (in which case we would want to error out, wouldn't we?)
Arrgh. Good catch. My patch is flawed in that it does not correctly fail if we really needed the missing object in this output pack.
I don't think that would be hard to fix. I'll respin something shortly.
Signature · 1 line
Shawn.
Quoted 5 lines
Quoted 4 lines · Junio C Hamano
If the check is only about a thin delta base that is not going to be transmit, I'd agree. But I do not see how you are distinguishing that case and the case where an object you are actually sending is missing (in which case we would want to error out, wouldn't we?)
Turns out to be pretty simple I think. We just delay the error handling for ->type < 0 until write_object(). If we get this far we know we wanted to include the object but we really don't have it. Up until that point its fine for us to get objects which are missing, we'll just wind up with a suboptimal pack.
We also don't even need to report the error from sha1_object_info as it already issues an error message (see sha1_loose_object_info).
--8<--
pack-objects: Allow missing base objects when creating thin packs
If we are building a thin pack and one of the base objects we would consider for deltification is missing its OK, the other side already has that base object. We may be able to get a delta from another object, or we can simply send the new object whole (no delta).
This allows a shallow clone which may have only commits and trees (but only partial blobs) to generate a pack for a fetch client, so the shallow clone only needs to contain objects that are not in the common base.
--- builtin-pack-objects.c | 5 ++--- 1 files changed, 2 insertions(+), 3 deletions(-)
builtin-pack-objects.c +2 -3
@@ -243,6 +243,8 @@ static unsigned long write_object(struct sha1file *f,crc32_begin (f); type = entry->type; +if (type <0 ) +die ("unable to read object %s" ,sha1_to_hex (entry->idx.sha1));/* write limit if limited packsize and not first object */ limit = pack_size_limit && nr_written ?
@@ -1096,9 +1098,6 @@ static void check_object(struct object_entry *entry) } entry->type =sha1_object_info (entry->idx.sha1, &entry->size); -if (entry->type <0 ) -die ("unable to get type of object %s" , -sha1_to_hex (entry->idx.sha1)); }static int pack_offset_sort (const void *_a,const void *_b)
Signature · 4 lines
1.6.0.rc2.22.g71b99 -- Shawn.
Quoted 9 lines · Shawn O. Pearce
If we are building a thin pack and one of the base objects we would consider for deltification is missing its OK, the other side already has that base object. We may be able to get a delta from another object, or we can simply send the new object whole (no delta).
This allows a shallow clone which may have only commits and trees (but only partial blobs) to generate a pack for a fetch client, so the shallow clone only needs to contain objects that are not in the common base.
I'd queue this to 'pu' (or 'next' if you want, but that amounts to about the same thing as we will be rebuilding 'next' soon after 1.6.0 ships), but this would probably be better demonstrated with a test script that demonstrates in what workflow this would be useful.
Quoted 6 lines · Shawn O. Pearce
Quoted 5 lines
Quoted 4 lines · Junio C Hamano
If the check is only about a thin delta base that is not going to be transmit, I'd agree. But I do not see how you are distinguishing that case and the case where an object you are actually sending is missing (in which case we would want to error out, wouldn't we?)
Quoted 6 lines
Turns out to be pretty simple I think. We just delay the error handling for ->type < 0 until
write_object(). If we get this far we know we wanted to include the object but we really don't have it. Up until that point its fine for us to get objects which are missing, we'll just wind up with a suboptimal pack.
If you're going to die anyway due to an object with unknown type, better do so _before_ going through the delta search phase and leaving a partial pack behind. IOW, the type check can be performed in prepare_pack() instead of write_object() like:
builtin-pack-objects.c +5 -1
@@ -1722,8 +1733,12 @@ static void prepare_pack(int window, int depth)if (entry->no_try_delta)continue ; -if (!entry->preferred_base) +if (!entry->preferred_base) { nr_deltas++; +if (entry->type <0 ) +die ("unable to get type of object %s" , +sha1_to_hex (entry->idx.sha1)); + } delta_list[n++] = entry; }
Also a comment in check_object() mentioning where the return value of sha1_object_info() is verified would be in order.
And I also agree with Junio about a test script for this so the usage is fully demonstrated, and to ensure it keeps on working as intended (most people will simply never exercise this otherwise).
Nicolas
PATCH 1/1 pack-objects: Allow missing base objects when creating thin packs
If we are building a thin pack and one of the base objects we would consider for deltification is missing its OK, the other side already has that base object. We may be able to get a delta from another object, or we can simply send the new object whole (no delta).
This change allows a shallow clone to store only the objects which are unique to it, as well as the boundary commit and its trees, but avoids storing the boundary blobs. This special form of a shallow clone is able to represent just the difference between two trees.
Pack objects change suggested by Nicolas Pitre.
---
Quoted 8 lines · Nicolas Pitre
Quoted 3 lines · Shawn O. Pearce
Quoted 3 lines
Quoted 2 lines · Junio C Hamano
If the check is only about a thin delta base that is not going to be transmit, I'd agree.
If you're going to die anyway due to an object with unknown type, better do so _before_ going through the delta search phase and leaving a partial pack behind.
Now with a test! :-)
builtin-pack-objects.c | 15 ++++++-- t/t5306-pack-nobase.sh | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 4 deletions(-) create mode 100755 t/t5306-pack-nobase.sh
builtin-pack-objects.c +11 -4
@@ -1096,9 +1096,12 @@ static void check_object(struct object_entry *entry) } entry->type =sha1_object_info (entry->idx.sha1, &entry->size); -if (entry->type <0 ) -die ("unable to get type of object %s" , -sha1_to_hex (entry->idx.sha1)); +/* +* The error condition is checked in prepare_pack(). This is +* to permit a missing preferred base object to be ignored +* as a preferred base. Doing so can result in a larger +* pack file, but the transfer will still take place. +*/ }static int pack_offset_sort (const void *_a,const void *_b)
@@ -1722,8 +1725,12 @@ static void prepare_pack(int window, int depth)if (entry->no_try_delta)continue ; -if (!entry->preferred_base) +if (!entry->preferred_base) { nr_deltas++; +if (entry->type <0 ) +die ("unable to get type of object %s" , +sha1_to_hex (entry->idx.sha1)); + } delta_list[n++] = entry; }
t/t5306-pack-nobase.sh new file +80 -0
80 lines not on this page · raw message
Signature · 5 lines
1.6.0.rc2.22.g71b99 -- Shawn.
Re: [PATCH] pack-objects: Allow missing base objects when creating thin packs
Quoted 2 lines · Shawn O. Pearce
Now with a test! :-)
[...]
Quoted 4 lines
t/t5306-pack-nobase.sh new file +4
-0+# Clone patch_clone indirectly by cloning base and fetching. +# +test_expect_success \ + 'indirectly clone patch_clone' \
t/t5306-pack-nobase.sh line 67
Quoted 10 lines
t/t5306-pack-nobase.sh new file +10
-0+ '(mkdir user_clone && + cd user_clone && + git init && + git pull ../.git && + test $(git rev-parse HEAD) = $B + + git pull ../patch_clone/.git && + test $(git rev-parse HEAD) = $C + ) + '
What if the first test command fails? Won't its result be ignored?
Nicolas
Re: [PATCH] pack-objects: Allow missing base objects when creating thin packs
Quoted 17 lines · Nicolas Pitre
Quoted 4 lines · Shawn O. Pearce
(quoted diff) +4
-0+# Clone patch_clone indirectly by cloning base and fetching. +# +test_expect_success \ + 'indirectly clone patch_clone' \Review
Quoted 10 lines
(quoted diff) +10
-0+ '(mkdir user_clone && + cd user_clone && + git init && + git pull ../.git && + test $(git rev-parse HEAD) = $B + + git pull ../patch_clone/.git && + test $(git rev-parse HEAD) = $C + ) + 'What if the first test command fails? Won't its result be ignored?
Isn't the exit status of the subshell the exit status of the last command in the subshell?
I just changed the test line to compare to "x$C" instead of $C and it correctly detected the error condition:
$ git diff
t/t5306-pack-nobase.sh +1 -1
@@ -62,7 +62,7 @@ test_expect_success \ test $(git rev-parse HEAD) = $B git pull ../patch_clone/.git && - test $(git rev-parse HEAD) = $C + test $(git rev-parse HEAD) = x$C ) '
$ ./t5306-pack-nobase.sh
* ok 1: setup base
* ok 2: setup patch_clone
* FAIL 3: indirectly clone patch_clone
(mkdir user_clone &&
cd user_clone &&
git init &&
git pull ../.git &&
test $(git rev-parse HEAD) = $B git pull ../patch_clone/.git &&
test $(git rev-parse HEAD) = x$C
)* ok 4: clone of patch_clone is incomplete
* failed 1 among 4 test(s)
Signature · 1 line
Shawn.
Re: [PATCH] pack-objects: Allow missing base objects when creating thin packs
Quoted 19 lines · Shawn O. Pearce
Quoted 17 lines · Nicolas Pitre
Quoted 4 lines · Shawn O. Pearce
(quoted diff) +4
-0+# Clone patch_clone indirectly by cloning base and fetching. +# +test_expect_success \ + 'indirectly clone patch_clone' \Review
Quoted 10 lines
(quoted diff) +10
-0+ '(mkdir user_clone && + cd user_clone && + git init && + git pull ../.git && + test $(git rev-parse HEAD) = $B + + git pull ../patch_clone/.git && + test $(git rev-parse HEAD) = $C + ) + 'What if the first test command fails? Won't its result be ignored?
Quoted 2 lines
Isn't the exit status of the subshell the exit status of the last command in the subshell?
I'm not talking about the last command but the "test $(git rev-parse HEAD) = $B" line.
Nicolas
PATCH v2 1/1 pack-objects: Allow missing base objects when creating thin packs
If we are building a thin pack and one of the base objects we would consider for deltification is missing its OK, the other side already has that base object. We may be able to get a delta from another object, or we can simply send the new object whole (no delta).
This change allows a shallow clone to store only the objects which are unique to it, as well as the boundary commit and its trees, but avoids storing the boundary blobs. This special form of a shallow clone is able to represent just the difference between two trees.
Pack objects change suggested by Nicolas Pitre.
---
Quoted 3 lines · Nicolas Pitre
On Tue, 12 Aug 2008, Shawn O. Pearce wrote:
I'm not talking about the last command but the "test $(git rev-parse HEAD) = $B" line.
Oh, right. Good catch.
builtin-pack-objects.c | 15 ++++++-- t/t5306-pack-nobase.sh | 80 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 4 deletions(-) create mode 100755 t/t5306-pack-nobase.sh
builtin-pack-objects.c +11 -4
@@ -1096,9 +1096,12 @@ static void check_object(struct object_entry *entry) } entry->type =sha1_object_info (entry->idx.sha1, &entry->size); -if (entry->type <0 ) -die ("unable to get type of object %s" , -sha1_to_hex (entry->idx.sha1)); +/* +* The error condition is checked in prepare_pack(). This is +* to permit a missing preferred base object to be ignored +* as a preferred base. Doing so can result in a larger +* pack file, but the transfer will still take place. +*/ }static int pack_offset_sort (const void *_a,const void *_b)
@@ -1722,8 +1725,12 @@ static void prepare_pack(int window, int depth)if (entry->no_try_delta)continue ; -if (!entry->preferred_base) +if (!entry->preferred_base) { nr_deltas++; +if (entry->type <0 ) +die ("unable to get type of object %s" , +sha1_to_hex (entry->idx.sha1)); + } delta_list[n++] = entry; }
t/t5306-pack-nobase.sh new file +80 -0
80 lines not on this page · raw message
Signature · 5 lines
1.6.0.rc2.22.g71b99 -- Shawn.
Re: [PATCH v2] pack-objects: Allow missing base objects when creating thin packs
Quoted 13 lines · Shawn O. Pearce
If we are building a thin pack and one of the base objects we would consider for deltification is missing its OK, the other side already has that base object. We may be able to get a delta from another object, or we can simply send the new object whole (no delta).
This change allows a shallow clone to store only the objects which are unique to it, as well as the boundary commit and its trees, but avoids storing the boundary blobs. This special form of a shallow clone is able to represent just the difference between two trees.
Pack objects change suggested by Nicolas Pitre.
Signed-off-by Shawn O. Pearce <[email protected]>
Quoted 8 lines
---
Quoted 3 lines · Nicolas Pitre
On Tue, 12 Aug 2008, Shawn O. Pearce wrote:
I'm not talking about the last command but the "test $(git rev-parse HEAD) = $B" line.Oh, right. Good catch.
t/t5306-pack-nobase.sh line 3
Quoted 5 lines
+++
b/t/t5306-pack-nobase.sht/t5306-pack-nobase.sh new file +3
-0@@ -0,0 +1,80 @@ +#!/bin/sh +# +# Copyright (c) 2008 Google Inc.
Heh. ;-)
Nicolas