@matheusbsbs / matheus-bagliano / commits / 636171cb80
Fork 0

make index-pack able to complete thin packs.

A new flag, --fix-thin, instructs git-index-pack to append any missing objects to a thin pack to make it self contained and indexable. Of course objects missing from the pack must be present elsewhere in the local repository. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Oct 25, 2006 at 23:28 UTC 636171cb80255682bdfc9bf5a98c9e66d4c0444a
2 files changed +224 -44
Documentation/git-index-pack.txt
+13 -1
@@ -8,7 +8,8 @@ git-index-pack - Build pack index file for an existing packed archive
8
9 SYNOPSIS
10 --------
11 -'git-index-pack' [-o <index-file>] { <pack-file> | --stdin [<pack-file>] }
11 +'git-index-pack' [-o <index-file>] <pack-file>
12 +'git-index-pack' --stdin [--fix-thin] [-o <index-file>] [<pack-file>]
13
14
15 DESCRIPTION
@@ -36,6 +37,17 @@ OPTIONS
37 objects/pack/ directory of the current git repository with
38 a default name determined from the pack content.
39
40 +--fix-thin::
41 + It is possible for gitlink:git-pack-objects[1] to build
42 + "thin" pack, which records objects in deltified form based on
43 + objects not included in the pack to reduce network traffic.
44 + Those objects are expected to be present on the receiving end
45 + and they must be included in the pack for that pack to be self
46 + contained and indexable. Without this option any attempt to
47 + index a thin pack will fail. This option only makes sense in
48 + conjonction with --stdin.
49 +
50 +
51 Author
52 ------
53 Written by Sergey Vlasov <vsu@altlinux.ru>
index-pack.c
+211 -43
@@ -8,7 +8,7 @@
8 #include "tree.h"
9
10 static const char index_pack_usage[] =
11 -"git-index-pack [-o <index-file>] { <pack-file> | --stdin [<pack-file>] }";
11 +"git-index-pack [-o <index-file>] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
12
13 struct object_entry
14 {
@@ -33,14 +33,15 @@ union delta_base {
33
34 struct delta_entry
35 {
36 - struct object_entry *obj;
36 union delta_base base;
37 + int obj_no;
38 };
39
40 static struct object_entry *objects;
41 static struct delta_entry *deltas;
42 static int nr_objects;
43 static int nr_deltas;
44 +static int nr_resolved_deltas;
45
46 static int from_stdin;
47
@@ -50,6 +51,18 @@ static unsigned long input_offset, input_len, consumed_bytes;
51 static SHA_CTX input_ctx;
52 static int input_fd, output_fd, mmap_fd;
53
54 +/* Discard current buffer used content. */
55 +static void flush()
56 +{
57 + if (input_offset) {
58 + if (output_fd >= 0)
59 + write_or_die(output_fd, input_buffer, input_offset);
60 + SHA1_Update(&input_ctx, input_buffer, input_offset);
61 + memcpy(input_buffer, input_buffer + input_offset, input_len);
62 + input_offset = 0;
63 + }
64 +}
65 +
66 /*
67 * Make sure at least "min" bytes are available in the buffer, and
68 * return the pointer to the buffer.
@@ -60,13 +73,7 @@ static void * fill(int min)
73 return input_buffer + input_offset;
74 if (min > sizeof(input_buffer))
75 die("cannot fill %d bytes", min);
63 - if (input_offset) {
64 - if (output_fd >= 0)
65 - write_or_die(output_fd, input_buffer, input_offset);
66 - SHA1_Update(&input_ctx, input_buffer, input_offset);
67 - memcpy(input_buffer, input_buffer + input_offset, input_len);
68 - input_offset = 0;
69 - }
76 + flush();
77 do {
78 int ret = xread(input_fd, input_buffer + input_len,
79 sizeof(input_buffer) - input_len);
@@ -323,10 +330,9 @@ static void sha1_object(const void *data, unsigned long size,
330 SHA1_Final(sha1, &ctx);
331 }
332
326 -static void resolve_delta(struct delta_entry *delta, void *base_data,
333 +static void resolve_delta(struct object_entry *delta_obj, void *base_data,
334 unsigned long base_size, enum object_type type)
335 {
329 - struct object_entry *obj = delta->obj;
336 void *delta_data;
337 unsigned long delta_size;
338 void *result;
@@ -334,29 +340,34 @@ static void resolve_delta(struct delta_entry *delta, void *base_data,
340 union delta_base delta_base;
341 int j, first, last;
342
337 - obj->real_type = type;
338 - delta_data = get_data_from_pack(obj);
339 - delta_size = obj->size;
343 + delta_obj->real_type = type;
344 + delta_data = get_data_from_pack(delta_obj);
345 + delta_size = delta_obj->size;
346 result = patch_delta(base_data, base_size, delta_data, delta_size,
347 &result_size);
348 free(delta_data);
349 if (!result)
344 - bad_object(obj->offset, "failed to apply delta");
345 - sha1_object(result, result_size, type, obj->sha1);
350 + bad_object(delta_obj->offset, "failed to apply delta");
351 + sha1_object(result, result_size, type, delta_obj->sha1);
352 + nr_resolved_deltas++;
353
347 - hashcpy(delta_base.sha1, obj->sha1);
354 + hashcpy(delta_base.sha1, delta_obj->sha1);
355 if (!find_delta_childs(&delta_base, &first, &last)) {
349 - for (j = first; j <= last; j++)
350 - if (deltas[j].obj->type == OBJ_REF_DELTA)
351 - resolve_delta(&deltas[j], result, result_size, type);
356 + for (j = first; j <= last; j++) {
357 + struct object_entry *child = objects + deltas[j].obj_no;
358 + if (child->real_type == OBJ_REF_DELTA)
359 + resolve_delta(child, result, result_size, type);
360 + }
361 }
362
363 memset(&delta_base, 0, sizeof(delta_base));
355 - delta_base.offset = obj->offset;
364 + delta_base.offset = delta_obj->offset;
365 if (!find_delta_childs(&delta_base, &first, &last)) {
357 - for (j = first; j <= last; j++)
358 - if (deltas[j].obj->type == OBJ_OFS_DELTA)
359 - resolve_delta(&deltas[j], result, result_size, type);
366 + for (j = first; j <= last; j++) {
367 + struct object_entry *child = objects + deltas[j].obj_no;
368 + if (child->real_type == OBJ_OFS_DELTA)
369 + resolve_delta(child, result, result_size, type);
370 + }
371 }
372
373 free(result);
@@ -389,7 +400,7 @@ static void parse_pack_objects(unsigned char *sha1)
400 obj->real_type = obj->type;
401 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
402 nr_deltas++;
392 - delta->obj = obj;
403 + delta->obj_no = i;
404 delta++;
405 } else
406 sha1_object(data, obj->size, obj->type, obj->sha1);
@@ -398,18 +409,15 @@ static void parse_pack_objects(unsigned char *sha1)
409 objects[i].offset = consumed_bytes;
410
411 /* Check pack integrity */
401 - SHA1_Update(&input_ctx, input_buffer, input_offset);
412 + flush();
413 SHA1_Final(sha1, &input_ctx);
414 if (hashcmp(fill(20), sha1))
415 die("pack is corrupted (SHA1 mismatch)");
405 - use(20);
406 - if (output_fd >= 0)
407 - write_or_die(output_fd, input_buffer, input_offset);
416
417 /* If input_fd is a file, we should have reached its end now. */
418 if (fstat(input_fd, &st))
419 die("cannot fstat packfile: %s", strerror(errno));
412 - if (S_ISREG(st.st_mode) && st.st_size != consumed_bytes)
420 + if (S_ISREG(st.st_mode) && st.st_size != consumed_bytes + 20)
421 die("pack has junk at the end");
422
423 /* Sort deltas by base SHA1/offset for fast searching */
@@ -440,24 +448,161 @@ static void parse_pack_objects(unsigned char *sha1)
448 continue;
449 data = get_data_from_pack(obj);
450 if (ref)
443 - for (j = ref_first; j <= ref_last; j++)
444 - if (deltas[j].obj->type == OBJ_REF_DELTA)
445 - resolve_delta(&deltas[j], data,
451 + for (j = ref_first; j <= ref_last; j++) {
452 + struct object_entry *child = objects + deltas[j].obj_no;
453 + if (child->real_type == OBJ_REF_DELTA)
454 + resolve_delta(child, data,
455 obj->size, obj->type);
456 + }
457 if (ofs)
448 - for (j = ofs_first; j <= ofs_last; j++)
449 - if (deltas[j].obj->type == OBJ_OFS_DELTA)
450 - resolve_delta(&deltas[j], data,
458 + for (j = ofs_first; j <= ofs_last; j++) {
459 + struct object_entry *child = objects + deltas[j].obj_no;
460 + if (child->real_type == OBJ_OFS_DELTA)
461 + resolve_delta(child, data,
462 obj->size, obj->type);
463 + }
464 free(data);
465 }
466 +}
467 +
468 +static int write_compressed(int fd, void *in, unsigned int size)
469 +{
470 + z_stream stream;
471 + unsigned long maxsize;
472 + void *out;
473 +
474 + memset(&stream, 0, sizeof(stream));
475 + deflateInit(&stream, zlib_compression_level);
476 + maxsize = deflateBound(&stream, size);
477 + out = xmalloc(maxsize);
478 +
479 + /* Compress it */
480 + stream.next_in = in;
481 + stream.avail_in = size;
482 + stream.next_out = out;
483 + stream.avail_out = maxsize;
484 + while (deflate(&stream, Z_FINISH) == Z_OK);
485 + deflateEnd(&stream);
486 +
487 + size = stream.total_out;
488 + write_or_die(fd, out, size);
489 + free(out);
490 + return size;
491 +}
492 +
493 +static void append_obj_to_pack(void *buf,
494 + unsigned long size, enum object_type type)
495 +{
496 + struct object_entry *obj = &objects[nr_objects++];
497 + unsigned char header[10];
498 + unsigned long s = size;
499 + int n = 0;
500 + unsigned char c = (type << 4) | (s & 15);
501 + s >>= 4;
502 + while (s) {
503 + header[n++] = c | 0x80;
504 + c = s & 0x7f;
505 + s >>= 7;
506 + }
507 + header[n++] = c;
508 + write_or_die(output_fd, header, n);
509 + obj[1].offset = obj[0].offset + n;
510 + obj[1].offset += write_compressed(output_fd, buf, size);
511 + sha1_object(buf, size, type, obj->sha1);
512 +}
513 +
514 +static int delta_pos_compare(const void *_a, const void *_b)
515 +{
516 + struct delta_entry *a = *(struct delta_entry **)_a;
517 + struct delta_entry *b = *(struct delta_entry **)_b;
518 + return a->obj_no - b->obj_no;
519 +}
520
455 - /* Check for unresolved deltas */
521 +static void fix_unresolved_deltas(int nr_unresolved)
522 +{
523 + struct delta_entry **sorted_by_pos;
524 + int i, n = 0;
525 +
526 + /*
527 + * Since many unresolved deltas may well be themselves base objects
528 + * for more unresolved deltas, we really want to include the
529 + * smallest number of base objects that would cover as much delta
530 + * as possible by picking the
531 + * trunc deltas first, allowing for other deltas to resolve without
532 + * additional base objects. Since most base objects are to be found
533 + * before deltas depending on them, a good heuristic is to start
534 + * resolving deltas in the same order as their position in the pack.
535 + */
536 + sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
537 for (i = 0; i < nr_deltas; i++) {
457 - if (deltas[i].obj->real_type == OBJ_REF_DELTA ||
458 - deltas[i].obj->real_type == OBJ_OFS_DELTA)
459 - die("pack has unresolved deltas");
538 + if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
539 + continue;
540 + sorted_by_pos[n++] = &deltas[i];
541 }
542 + qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
543 +
544 + for (i = 0; i < n; i++) {
545 + struct delta_entry *d = sorted_by_pos[i];
546 + void *data;
547 + unsigned long size;
548 + char type[10];
549 + enum object_type obj_type;
550 + int j, first, last;
551 +
552 + if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
553 + continue;
554 + data = read_sha1_file(d->base.sha1, type, &size);
555 + if (!data)
556 + continue;
557 + if (!strcmp(type, blob_type)) obj_type = OBJ_BLOB;
558 + else if (!strcmp(type, tree_type)) obj_type = OBJ_TREE;
559 + else if (!strcmp(type, commit_type)) obj_type = OBJ_COMMIT;
560 + else if (!strcmp(type, tag_type)) obj_type = OBJ_TAG;
561 + else die("base object %s is of type '%s'",
562 + sha1_to_hex(d->base.sha1), type);
563 +
564 + find_delta_childs(&d->base, &first, &last);
565 + for (j = first; j <= last; j++) {
566 + struct object_entry *child = objects + deltas[j].obj_no;
567 + if (child->real_type == OBJ_REF_DELTA)
568 + resolve_delta(child, data, size, obj_type);
569 + }
570 +
571 + append_obj_to_pack(data, size, obj_type);
572 + free(data);
573 + }
574 + free(sorted_by_pos);
575 +}
576 +
577 +static void readjust_pack_header_and_sha1(unsigned char *sha1)
578 +{
579 + struct pack_header hdr;
580 + SHA_CTX ctx;
581 + int size;
582 +
583 + /* Rewrite pack header with updated object number */
584 + if (lseek(output_fd, 0, SEEK_SET) != 0)
585 + die("cannot seek back: %s", strerror(errno));
586 + if (xread(output_fd, &hdr, sizeof(hdr)) != sizeof(hdr))
587 + die("cannot read pack header back: %s", strerror(errno));
588 + hdr.hdr_entries = htonl(nr_objects);
589 + if (lseek(output_fd, 0, SEEK_SET) != 0)
590 + die("cannot seek back: %s", strerror(errno));
591 + write_or_die(output_fd, &hdr, sizeof(hdr));
592 + if (lseek(output_fd, 0, SEEK_SET) != 0)
593 + die("cannot seek back: %s", strerror(errno));
594 +
595 + /* Recompute and store the new pack's SHA1 */
596 + SHA1_Init(&ctx);
597 + do {
598 + unsigned char *buf[4096];
599 + size = xread(output_fd, buf, sizeof(buf));
600 + if (size < 0)
601 + die("cannot read pack data back: %s", strerror(errno));
602 + SHA1_Update(&ctx, buf, size);
603 + } while (size > 0);
604 + SHA1_Final(sha1, &ctx);
605 + write_or_die(output_fd, sha1, 20);
606 }
607
608 static int sha1_compare(const void *_a, const void *_b)
@@ -588,7 +733,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
733
734 int main(int argc, char **argv)
735 {
591 - int i;
736 + int i, fix_thin_pack = 0;
737 const char *curr_pack, *pack_name = NULL;
738 const char *curr_index, *index_name = NULL;
739 char *index_name_buf = NULL;
@@ -600,6 +745,8 @@ int main(int argc, char **argv)
745 if (*arg == '-') {
746 if (!strcmp(arg, "--stdin")) {
747 from_stdin = 1;
748 + } else if (!strcmp(arg, "--fix-thin")) {
749 + fix_thin_pack = 1;
750 } else if (!strcmp(arg, "-o")) {
751 if (index_name || (i+1) >= argc)
752 usage(index_pack_usage);
@@ -616,6 +763,8 @@ int main(int argc, char **argv)
763
764 if (!pack_name && !from_stdin)
765 usage(index_pack_usage);
766 + if (fix_thin_pack && !from_stdin)
767 + die("--fix-thin cannot be used without --stdin");
768 if (!index_name && pack_name) {
769 int len = strlen(pack_name);
770 if (!has_extension(pack_name, ".pack"))
@@ -629,9 +778,28 @@ int main(int argc, char **argv)
778
779 curr_pack = open_pack_file(pack_name);
780 parse_pack_header();
632 - objects = xcalloc(nr_objects + 1, sizeof(struct object_entry));
633 - deltas = xcalloc(nr_objects, sizeof(struct delta_entry));
781 + objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
782 + deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
783 parse_pack_objects(sha1);
784 + if (nr_deltas != nr_resolved_deltas) {
785 + if (fix_thin_pack) {
786 + int nr_unresolved = nr_deltas - nr_resolved_deltas;
787 + if (nr_unresolved <= 0)
788 + die("confusion beyond insanity");
789 + objects = xrealloc(objects,
790 + (nr_objects + nr_unresolved + 1)
791 + * sizeof(*objects));
792 + fix_unresolved_deltas(nr_unresolved);
793 + readjust_pack_header_and_sha1(sha1);
794 + }
795 + if (nr_deltas != nr_resolved_deltas)
796 + die("pack has %d unresolved deltas",
797 + nr_deltas - nr_resolved_deltas);
798 + } else {
799 + /* Flush remaining pack final 20-byte SHA1. */
800 + use(20);
801 + flush();
802 + }
803 free(deltas);
804 curr_index = write_index_file(index_name, sha1);
805 final(pack_name, curr_pack, index_name, curr_index, sha1);