12 static const char index_pack_usage[] =
13 "git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] [--strict] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
17 struct pack_idx_entry idx;
19 unsigned int hdr_size;
20 enum object_type type;
21 enum object_type real_type;
25 unsigned char sha1[20];
30 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
31 * to memcmp() only the first 20 bytes.
33 #define UNION_BASE_SZ 20
35 #define FLAG_LINK (1u<<20)
36 #define FLAG_CHECKED (1u<<21)
40 union delta_base base;
44 static struct object_entry *objects;
45 static struct delta_entry *deltas;
46 static int nr_objects;
48 static int nr_resolved_deltas;
50 static int from_stdin;
54 static struct progress *progress;
56 /* We always read in 4kB chunks. */
57 static unsigned char input_buffer[4096];
58 static unsigned int input_offset, input_len;
59 static off_t consumed_bytes;
60 static SHA_CTX input_ctx;
61 static uint32_t input_crc32;
62 static int input_fd, output_fd, pack_fd;
64 static int mark_link(struct object *obj, int type, void *data)
69 if (type != OBJ_ANY && obj->type != type)
70 die("object type mismatch at %s", sha1_to_hex(obj->sha1));
72 obj->flags |= FLAG_LINK;
76 /* The content of each linked object must have been checked
77 or it must be already present in the object database */
78 static void check_object(struct object *obj)
83 if (!(obj->flags & FLAG_LINK))
86 if (!(obj->flags & FLAG_CHECKED)) {
88 int type = sha1_object_info(obj->sha1, &size);
89 if (type != obj->type || type <= 0)
90 die("object of unexpected type");
91 obj->flags |= FLAG_CHECKED;
96 static void check_objects(void)
100 max = get_max_object_index();
101 for (i = 0; i < max; i++)
102 check_object(get_indexed_object(i));
106 /* Discard current buffer used content. */
107 static void flush(void)
111 write_or_die(output_fd, input_buffer, input_offset);
112 SHA1_Update(&input_ctx, input_buffer, input_offset);
113 memmove(input_buffer, input_buffer + input_offset, input_len);
119 * Make sure at least "min" bytes are available in the buffer, and
120 * return the pointer to the buffer.
122 static void *fill(int min)
124 if (min <= input_len)
125 return input_buffer + input_offset;
126 if (min > sizeof(input_buffer))
127 die("cannot fill %d bytes", min);
130 ssize_t ret = xread(input_fd, input_buffer + input_len,
131 sizeof(input_buffer) - input_len);
135 die("read error on input: %s", strerror(errno));
139 display_throughput(progress, consumed_bytes + input_len);
140 } while (input_len < min);
144 static void use(int bytes)
146 if (bytes > input_len)
147 die("used more bytes than were available");
148 input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes);
150 input_offset += bytes;
152 /* make sure off_t is sufficiently large not to wrap */
153 if (consumed_bytes > consumed_bytes + bytes)
154 die("pack too large for current definition of off_t");
155 consumed_bytes += bytes;
158 static char *open_pack_file(char *pack_name)
163 static char tmpfile[PATH_MAX];
164 snprintf(tmpfile, sizeof(tmpfile),
165 "%s/tmp_pack_XXXXXX", get_object_directory());
166 output_fd = xmkstemp(tmpfile);
167 pack_name = xstrdup(tmpfile);
169 output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
171 die("unable to create %s: %s\n", pack_name, strerror(errno));
174 input_fd = open(pack_name, O_RDONLY);
176 die("cannot open packfile '%s': %s",
177 pack_name, strerror(errno));
181 SHA1_Init(&input_ctx);
185 static void parse_pack_header(void)
187 struct pack_header *hdr = fill(sizeof(struct pack_header));
189 /* Header consistency check */
190 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
191 die("pack signature mismatch");
192 if (!pack_version_ok(hdr->hdr_version))
193 die("pack version %"PRIu32" unsupported",
194 ntohl(hdr->hdr_version));
196 nr_objects = ntohl(hdr->hdr_entries);
197 use(sizeof(struct pack_header));
200 static void bad_object(unsigned long offset, const char *format,
201 ...) NORETURN __attribute__((format (printf, 2, 3)));
203 static void bad_object(unsigned long offset, const char *format, ...)
208 va_start(params, format);
209 vsnprintf(buf, sizeof(buf), format, params);
211 die("pack has bad object at offset %lu: %s", offset, buf);
214 static void *unpack_entry_data(unsigned long offset, unsigned long size)
217 void *buf = xmalloc(size);
219 memset(&stream, 0, sizeof(stream));
220 stream.next_out = buf;
221 stream.avail_out = size;
222 stream.next_in = fill(1);
223 stream.avail_in = input_len;
224 inflateInit(&stream);
227 int ret = inflate(&stream, 0);
228 use(input_len - stream.avail_in);
229 if (stream.total_out == size && ret == Z_STREAM_END)
232 bad_object(offset, "inflate returned %d", ret);
233 stream.next_in = fill(1);
234 stream.avail_in = input_len;
240 static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
248 obj->idx.offset = consumed_bytes;
249 input_crc32 = crc32(0, Z_NULL, 0);
254 obj->type = (c >> 4) & 7;
261 size += (c & 0x7fUL) << shift;
268 hashcpy(delta_base->sha1, fill(20));
272 memset(delta_base, 0, sizeof(*delta_base));
276 base_offset = c & 127;
279 if (!base_offset || MSB(base_offset, 7))
280 bad_object(obj->idx.offset, "offset value overflow for delta base object");
284 base_offset = (base_offset << 7) + (c & 127);
286 delta_base->offset = obj->idx.offset - base_offset;
287 if (delta_base->offset >= obj->idx.offset)
288 bad_object(obj->idx.offset, "delta base offset is out of bound");
296 bad_object(obj->idx.offset, "unknown object type %d", obj->type);
298 obj->hdr_size = consumed_bytes - obj->idx.offset;
300 data = unpack_entry_data(obj->idx.offset, obj->size);
301 obj->idx.crc32 = input_crc32;
305 static void *get_data_from_pack(struct object_entry *obj)
307 off_t from = obj[0].idx.offset + obj[0].hdr_size;
308 unsigned long len = obj[1].idx.offset - from;
309 unsigned long rdy = 0;
310 unsigned char *src, *data;
317 ssize_t n = pread(pack_fd, data + rdy, len - rdy, from + rdy);
319 die("cannot pread pack file: %s", strerror(errno));
322 data = xmalloc(obj->size);
323 memset(&stream, 0, sizeof(stream));
324 stream.next_out = data;
325 stream.avail_out = obj->size;
326 stream.next_in = src;
327 stream.avail_in = len;
328 inflateInit(&stream);
329 while ((st = inflate(&stream, Z_FINISH)) == Z_OK);
331 if (st != Z_STREAM_END || stream.total_out != obj->size)
332 die("serious inflate inconsistency");
337 static int find_delta(const union delta_base *base)
339 int first = 0, last = nr_deltas;
341 while (first < last) {
342 int next = (first + last) / 2;
343 struct delta_entry *delta = &deltas[next];
346 cmp = memcmp(base, &delta->base, UNION_BASE_SZ);
358 static int find_delta_children(const union delta_base *base,
359 int *first_index, int *last_index)
361 int first = find_delta(base);
363 int end = nr_deltas - 1;
367 while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ))
369 while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ))
371 *first_index = first;
376 static void sha1_object(const void *data, unsigned long size,
377 enum object_type type, unsigned char *sha1)
379 hash_sha1_file(data, size, typename(type), sha1);
380 if (has_sha1_file(sha1)) {
382 enum object_type has_type;
383 unsigned long has_size;
384 has_data = read_sha1_file(sha1, &has_type, &has_size);
386 die("cannot read existing object %s", sha1_to_hex(sha1));
387 if (size != has_size || type != has_type ||
388 memcmp(data, has_data, size) != 0)
389 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1));
393 if (type == OBJ_BLOB) {
394 struct blob *blob = lookup_blob(sha1);
396 blob->object.flags |= FLAG_CHECKED;
398 die("invalid blob object %s", sha1_to_hex(sha1));
402 void *buf = (void *) data;
405 * we do not need to free the memory here, as the
406 * buf is deleted by the caller.
408 obj = parse_object_buffer(sha1, type, size, buf, &eaten);
410 die("invalid %s", typename(type));
411 if (fsck_object(obj, 1, fsck_error_function))
412 die("Error in object");
413 if (fsck_walk(obj, mark_link, 0))
414 die("Not all child objects of %s are reachable", sha1_to_hex(obj->sha1));
416 if (obj->type == OBJ_TREE) {
417 struct tree *item = (struct tree *) obj;
420 if (obj->type == OBJ_COMMIT) {
421 struct commit *commit = (struct commit *) obj;
422 commit->buffer = NULL;
424 obj->flags |= FLAG_CHECKED;
429 static void resolve_delta(struct object_entry *delta_obj, void *base_data,
430 unsigned long base_size, enum object_type type)
433 unsigned long delta_size;
435 unsigned long result_size;
436 union delta_base delta_base;
439 delta_obj->real_type = type;
440 delta_data = get_data_from_pack(delta_obj);
441 delta_size = delta_obj->size;
442 result = patch_delta(base_data, base_size, delta_data, delta_size,
446 bad_object(delta_obj->idx.offset, "failed to apply delta");
447 sha1_object(result, result_size, type, delta_obj->idx.sha1);
448 nr_resolved_deltas++;
450 hashcpy(delta_base.sha1, delta_obj->idx.sha1);
451 if (!find_delta_children(&delta_base, &first, &last)) {
452 for (j = first; j <= last; j++) {
453 struct object_entry *child = objects + deltas[j].obj_no;
454 if (child->real_type == OBJ_REF_DELTA)
455 resolve_delta(child, result, result_size, type);
459 memset(&delta_base, 0, sizeof(delta_base));
460 delta_base.offset = delta_obj->idx.offset;
461 if (!find_delta_children(&delta_base, &first, &last)) {
462 for (j = first; j <= last; j++) {
463 struct object_entry *child = objects + deltas[j].obj_no;
464 if (child->real_type == OBJ_OFS_DELTA)
465 resolve_delta(child, result, result_size, type);
472 static int compare_delta_entry(const void *a, const void *b)
474 const struct delta_entry *delta_a = a;
475 const struct delta_entry *delta_b = b;
476 return memcmp(&delta_a->base, &delta_b->base, UNION_BASE_SZ);
479 /* Parse all objects and return the pack content SHA1 hash */
480 static void parse_pack_objects(unsigned char *sha1)
483 struct delta_entry *delta = deltas;
489 * - find locations of all objects;
490 * - calculate SHA1 of all non-delta objects;
491 * - remember base (SHA1 or offset) for all deltas.
494 progress = start_progress(
495 from_stdin ? "Receiving objects" : "Indexing objects",
497 for (i = 0; i < nr_objects; i++) {
498 struct object_entry *obj = &objects[i];
499 data = unpack_raw_entry(obj, &delta->base);
500 obj->real_type = obj->type;
501 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA) {
506 sha1_object(data, obj->size, obj->type, obj->idx.sha1);
508 display_progress(progress, i+1);
510 objects[i].idx.offset = consumed_bytes;
511 stop_progress(&progress);
513 /* Check pack integrity */
515 SHA1_Final(sha1, &input_ctx);
516 if (hashcmp(fill(20), sha1))
517 die("pack is corrupted (SHA1 mismatch)");
520 /* If input_fd is a file, we should have reached its end now. */
521 if (fstat(input_fd, &st))
522 die("cannot fstat packfile: %s", strerror(errno));
523 if (S_ISREG(st.st_mode) &&
524 lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size)
525 die("pack has junk at the end");
530 /* Sort deltas by base SHA1/offset for fast searching */
531 qsort(deltas, nr_deltas, sizeof(struct delta_entry),
532 compare_delta_entry);
536 * - for all non-delta objects, look if it is used as a base for
538 * - if used as a base, uncompress the object and apply all deltas,
539 * recursively checking if the resulting object is used as a base
540 * for some more deltas.
543 progress = start_progress("Resolving deltas", nr_deltas);
544 for (i = 0; i < nr_objects; i++) {
545 struct object_entry *obj = &objects[i];
546 union delta_base base;
547 int j, ref, ref_first, ref_last, ofs, ofs_first, ofs_last;
549 if (obj->type == OBJ_REF_DELTA || obj->type == OBJ_OFS_DELTA)
551 hashcpy(base.sha1, obj->idx.sha1);
552 ref = !find_delta_children(&base, &ref_first, &ref_last);
553 memset(&base, 0, sizeof(base));
554 base.offset = obj->idx.offset;
555 ofs = !find_delta_children(&base, &ofs_first, &ofs_last);
558 data = get_data_from_pack(obj);
560 for (j = ref_first; j <= ref_last; j++) {
561 struct object_entry *child = objects + deltas[j].obj_no;
562 if (child->real_type == OBJ_REF_DELTA)
563 resolve_delta(child, data,
564 obj->size, obj->type);
567 for (j = ofs_first; j <= ofs_last; j++) {
568 struct object_entry *child = objects + deltas[j].obj_no;
569 if (child->real_type == OBJ_OFS_DELTA)
570 resolve_delta(child, data,
571 obj->size, obj->type);
574 display_progress(progress, nr_resolved_deltas);
578 static int write_compressed(int fd, void *in, unsigned int size, uint32_t *obj_crc)
581 unsigned long maxsize;
584 memset(&stream, 0, sizeof(stream));
585 deflateInit(&stream, zlib_compression_level);
586 maxsize = deflateBound(&stream, size);
587 out = xmalloc(maxsize);
591 stream.avail_in = size;
592 stream.next_out = out;
593 stream.avail_out = maxsize;
594 while (deflate(&stream, Z_FINISH) == Z_OK);
597 size = stream.total_out;
598 write_or_die(fd, out, size);
599 *obj_crc = crc32(*obj_crc, out, size);
604 static void append_obj_to_pack(const unsigned char *sha1, void *buf,
605 unsigned long size, enum object_type type)
607 struct object_entry *obj = &objects[nr_objects++];
608 unsigned char header[10];
609 unsigned long s = size;
611 unsigned char c = (type << 4) | (s & 15);
614 header[n++] = c | 0x80;
619 write_or_die(output_fd, header, n);
620 obj[0].idx.crc32 = crc32(0, Z_NULL, 0);
621 obj[0].idx.crc32 = crc32(obj[0].idx.crc32, header, n);
622 obj[1].idx.offset = obj[0].idx.offset + n;
623 obj[1].idx.offset += write_compressed(output_fd, buf, size, &obj[0].idx.crc32);
624 hashcpy(obj->idx.sha1, sha1);
627 static int delta_pos_compare(const void *_a, const void *_b)
629 struct delta_entry *a = *(struct delta_entry **)_a;
630 struct delta_entry *b = *(struct delta_entry **)_b;
631 return a->obj_no - b->obj_no;
634 static void fix_unresolved_deltas(int nr_unresolved)
636 struct delta_entry **sorted_by_pos;
640 * Since many unresolved deltas may well be themselves base objects
641 * for more unresolved deltas, we really want to include the
642 * smallest number of base objects that would cover as much delta
643 * as possible by picking the
644 * trunc deltas first, allowing for other deltas to resolve without
645 * additional base objects. Since most base objects are to be found
646 * before deltas depending on them, a good heuristic is to start
647 * resolving deltas in the same order as their position in the pack.
649 sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos));
650 for (i = 0; i < nr_deltas; i++) {
651 if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA)
653 sorted_by_pos[n++] = &deltas[i];
655 qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare);
657 for (i = 0; i < n; i++) {
658 struct delta_entry *d = sorted_by_pos[i];
661 enum object_type type;
664 if (objects[d->obj_no].real_type != OBJ_REF_DELTA)
666 data = read_sha1_file(d->base.sha1, &type, &size);
670 find_delta_children(&d->base, &first, &last);
671 for (j = first; j <= last; j++) {
672 struct object_entry *child = objects + deltas[j].obj_no;
673 if (child->real_type == OBJ_REF_DELTA)
674 resolve_delta(child, data, size, type);
677 if (check_sha1_signature(d->base.sha1, data, size, typename(type)))
678 die("local object %s is corrupt", sha1_to_hex(d->base.sha1));
679 append_obj_to_pack(d->base.sha1, data, size, type);
681 display_progress(progress, nr_resolved_deltas);
686 static void final(const char *final_pack_name, const char *curr_pack_name,
687 const char *final_index_name, const char *curr_index_name,
688 const char *keep_name, const char *keep_msg,
691 const char *report = "pack";
698 fsync_or_die(output_fd, curr_pack_name);
699 err = close(output_fd);
701 die("error while closing pack file: %s", strerror(errno));
702 chmod(curr_pack_name, 0444);
706 int keep_fd, keep_msg_len = strlen(keep_msg);
708 snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
709 get_object_directory(), sha1_to_hex(sha1));
712 keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
715 die("cannot write keep file");
717 if (keep_msg_len > 0) {
718 write_or_die(keep_fd, keep_msg, keep_msg_len);
719 write_or_die(keep_fd, "\n", 1);
721 if (close(keep_fd) != 0)
722 die("cannot write keep file");
727 if (final_pack_name != curr_pack_name) {
728 if (!final_pack_name) {
729 snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
730 get_object_directory(), sha1_to_hex(sha1));
731 final_pack_name = name;
733 if (move_temp_to_file(curr_pack_name, final_pack_name))
734 die("cannot store pack file");
737 chmod(curr_index_name, 0444);
738 if (final_index_name != curr_index_name) {
739 if (!final_index_name) {
740 snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
741 get_object_directory(), sha1_to_hex(sha1));
742 final_index_name = name;
744 if (move_temp_to_file(curr_index_name, final_index_name))
745 die("cannot store index file");
749 printf("%s\n", sha1_to_hex(sha1));
752 int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
753 report, sha1_to_hex(sha1));
754 write_or_die(1, buf, len);
757 * Let's just mimic git-unpack-objects here and write
758 * the last part of the input buffer to stdout.
761 err = xwrite(1, input_buffer + input_offset, input_len);
770 static int git_index_pack_config(const char *k, const char *v, void *cb)
772 if (!strcmp(k, "pack.indexversion")) {
773 pack_idx_default_version = git_config_int(k, v);
774 if (pack_idx_default_version > 2)
775 die("bad pack.indexversion=%"PRIu32,
776 pack_idx_default_version);
779 return git_default_config(k, v, cb);
782 int main(int argc, char **argv)
784 int i, fix_thin_pack = 0;
785 char *curr_pack, *pack_name = NULL;
786 char *curr_index, *index_name = NULL;
787 const char *keep_name = NULL, *keep_msg = NULL;
788 char *index_name_buf = NULL, *keep_name_buf = NULL;
789 struct pack_idx_entry **idx_objects;
790 unsigned char sha1[20];
792 git_config(git_index_pack_config, NULL);
794 for (i = 1; i < argc; i++) {
798 if (!strcmp(arg, "--stdin")) {
800 } else if (!strcmp(arg, "--fix-thin")) {
802 } else if (!strcmp(arg, "--strict")) {
804 } else if (!strcmp(arg, "--keep")) {
806 } else if (!prefixcmp(arg, "--keep=")) {
808 } else if (!prefixcmp(arg, "--pack_header=")) {
809 struct pack_header *hdr;
812 hdr = (struct pack_header *)input_buffer;
813 hdr->hdr_signature = htonl(PACK_SIGNATURE);
814 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
817 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
820 input_len = sizeof(*hdr);
821 } else if (!strcmp(arg, "-v")) {
823 } else if (!strcmp(arg, "-o")) {
824 if (index_name || (i+1) >= argc)
825 usage(index_pack_usage);
826 index_name = argv[++i];
827 } else if (!prefixcmp(arg, "--index-version=")) {
829 pack_idx_default_version = strtoul(arg + 16, &c, 10);
830 if (pack_idx_default_version > 2)
833 pack_idx_off32_limit = strtoul(c+1, &c, 0);
834 if (*c || pack_idx_off32_limit & 0x80000000)
837 usage(index_pack_usage);
842 usage(index_pack_usage);
846 if (!pack_name && !from_stdin)
847 usage(index_pack_usage);
848 if (fix_thin_pack && !from_stdin)
849 die("--fix-thin cannot be used without --stdin");
850 if (!index_name && pack_name) {
851 int len = strlen(pack_name);
852 if (!has_extension(pack_name, ".pack"))
853 die("packfile name '%s' does not end with '.pack'",
855 index_name_buf = xmalloc(len);
856 memcpy(index_name_buf, pack_name, len - 5);
857 strcpy(index_name_buf + len - 5, ".idx");
858 index_name = index_name_buf;
860 if (keep_msg && !keep_name && pack_name) {
861 int len = strlen(pack_name);
862 if (!has_extension(pack_name, ".pack"))
863 die("packfile name '%s' does not end with '.pack'",
865 keep_name_buf = xmalloc(len);
866 memcpy(keep_name_buf, pack_name, len - 5);
867 strcpy(keep_name_buf + len - 5, ".keep");
868 keep_name = keep_name_buf;
871 curr_pack = open_pack_file(pack_name);
873 objects = xmalloc((nr_objects + 1) * sizeof(struct object_entry));
874 deltas = xmalloc(nr_objects * sizeof(struct delta_entry));
875 parse_pack_objects(sha1);
876 if (nr_deltas == nr_resolved_deltas) {
877 stop_progress(&progress);
878 /* Flush remaining pack final 20-byte SHA1. */
883 int nr_unresolved = nr_deltas - nr_resolved_deltas;
884 int nr_objects_initial = nr_objects;
885 if (nr_unresolved <= 0)
886 die("confusion beyond insanity");
887 objects = xrealloc(objects,
888 (nr_objects + nr_unresolved + 1)
890 fix_unresolved_deltas(nr_unresolved);
891 sprintf(msg, "completed with %d local objects",
892 nr_objects - nr_objects_initial);
893 stop_progress_msg(&progress, msg);
894 fixup_pack_header_footer(output_fd, sha1,
895 curr_pack, nr_objects);
897 if (nr_deltas != nr_resolved_deltas)
898 die("pack has %d unresolved deltas",
899 nr_deltas - nr_resolved_deltas);
905 idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *));
906 for (i = 0; i < nr_objects; i++)
907 idx_objects[i] = &objects[i].idx;
908 curr_index = write_idx_file(index_name, idx_objects, nr_objects, sha1);
911 final(pack_name, curr_pack,
912 index_name, curr_index,
916 free(index_name_buf);
918 if (pack_name == NULL)
920 if (index_name == NULL)