make git-pack-objects able to create deltas with offset to base
This is enabled with --delta-base-offset only, and doesn't work with pack data reuse yet. The idea is to allow for the fetch protocol to use an extension flag to notify the remote end that --delta-base-offset can be used with git-pack-objects. Eventually git-repack will always provide this flag. With this, all delta base objects are now pushed before deltas that depend on them. This is a requirements for OBJ_OFS_DELTA. This is not a requirement for OBJ_REF_DELTA but always doing so makes the code simpler. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Sep 21, 2006 at 00:09 UTC
be6b19145f64f62790049c06320c35011f7312a7
1 file changed
+33
-14
builtin-pack-objects.c
+33
-14
@@ -60,6 +60,8 @@ static int non_empty;
60
static int no_reuse_delta;
61
static int local;
62
static int incremental;
63
+static int allow_ofs_delta;
64
+
65
static struct object_entry **sorted_by_sha, **sorted_by_type;
66
static struct object_entry *objects;
67
static int nr_objects, nr_alloc, nr_result;
@@ -334,9 +336,6 @@ static unsigned long write_object(struct sha1file *f,
336
enum object_type obj_type;
337
int to_reuse = 0;
338
337
- if (entry->preferred_base)
338
- return 0;
339
-
339
obj_type = entry->type;
340
if (! entry->in_pack)
341
to_reuse = 0; /* can't reuse what we don't have */
@@ -380,18 +379,35 @@ static unsigned long write_object(struct sha1file *f,
379
if (entry->delta) {
380
buf = delta_against(buf, size, entry);
381
size = entry->delta_size;
383
- obj_type = OBJ_REF_DELTA;
382
+ obj_type = (allow_ofs_delta && entry->delta->offset) ?
383
+ OBJ_OFS_DELTA : OBJ_REF_DELTA;
384
}
385
/*
386
* The object header is a byte of 'type' followed by zero or
387
- * more bytes of length. For deltas, the 20 bytes of delta
388
- * sha1 follows that.
387
+ * more bytes of length.
388
*/
389
hdrlen = encode_header(obj_type, size, header);
390
sha1write(f, header, hdrlen);
391
393
- if (entry->delta) {
394
- sha1write(f, entry->delta, 20);
392
+ if (obj_type == OBJ_OFS_DELTA) {
393
+ /*
394
+ * Deltas with relative base contain an additional
395
+ * encoding of the relative offset for the delta
396
+ * base from this object's position in the pack.
397
+ */
398
+ unsigned long ofs = entry->offset - entry->delta->offset;
399
+ unsigned pos = sizeof(header) - 1;
400
+ header[pos] = ofs & 127;
401
+ while (ofs >>= 7)
402
+ header[--pos] = 128 | (--ofs & 127);
403
+ sha1write(f, header + pos, sizeof(header) - pos);
404
+ hdrlen += sizeof(header) - pos;
405
+ } else if (obj_type == OBJ_REF_DELTA) {
406
+ /*
407
+ * Deltas with a base reference contain
408
+ * an additional 20 bytes for the base sha1.
409
+ */
410
+ sha1write(f, entry->delta->sha1, 20);
411
hdrlen += 20;
412
}
413
datalen = sha1write_compressed(f, buf, size);
@@ -413,7 +429,7 @@ static unsigned long write_object(struct sha1file *f,
429
reused_delta++;
430
reused++;
431
}
416
- if (obj_type == OBJ_REF_DELTA)
432
+ if (entry->delta)
433
written_delta++;
434
written++;
435
return hdrlen + datalen;
@@ -423,17 +439,16 @@ static unsigned long write_one(struct sha1file *f,
439
struct object_entry *e,
440
unsigned long offset)
441
{
426
- if (e->offset)
442
+ if (e->offset || e->preferred_base)
443
/* offset starts from header size and cannot be zero
444
* if it is written already.
445
*/
446
return offset;
431
- e->offset = offset;
432
- offset += write_object(f, e);
433
- /* if we are deltified, write out its base object. */
447
+ /* if we are deltified, write out its base object first. */
448
if (e->delta)
449
offset = write_one(f, e->delta, offset);
436
- return offset;
450
+ e->offset = offset;
451
+ return offset + write_object(f, e);
452
}
453
454
static void write_pack_file(void)
@@ -1484,6 +1499,10 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1499
no_reuse_delta = 1;
1500
continue;
1501
}
1502
+ if (!strcmp("--delta-base-offset", arg)) {
1503
+ allow_ofs_delta = no_reuse_delta = 1;
1504
+ continue;
1505
+ }
1506
if (!strcmp("--stdout", arg)) {
1507
pack_to_stdout = 1;
1508
continue;