8 static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
11 unsigned char sha1[20];
12 unsigned long size; /* uncompressed size */
13 unsigned long offset; /* offset into the final pack file (nonzero if already written) */
14 unsigned int depth; /* delta depth */
15 unsigned int hash; /* name hint hash */
16 enum object_type type;
17 unsigned char edge; /* reused delta chain points at this entry. */
18 enum object_type in_pack_type; /* could be delta */
19 unsigned long delta_size; /* delta data size (uncompressed) */
20 struct object_entry *delta; /* delta base object */
21 struct packed_git *in_pack; /* already in pack */
22 unsigned int in_pack_offset;
26 * Objects we are going to pack are colected in objects array (dynamically
27 * expanded). nr_objects & nr_alloc controls this array. They are stored
28 * in the order we see -- typically rev-list --objects order that gives us
29 * nice "minimum seek" order.
31 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
32 * elements in the objects array. The former is used to build the pack
33 * index (lists object names in the ascending order to help offset lookup),
34 * and the latter is used to group similar things together by try_delta()
38 static unsigned char object_list_sha1[20];
39 static int non_empty = 0;
40 static int no_reuse_delta = 0;
42 static int incremental = 0;
43 static struct object_entry **sorted_by_sha, **sorted_by_type;
44 static struct object_entry *objects = NULL;
45 static int nr_objects = 0, nr_alloc = 0;
46 static const char *base_name;
47 static unsigned char pack_file_sha1[20];
48 static int progress = 1;
51 * The object names in objects array are hashed with this hashtable,
52 * to help looking up the entry by object name. Binary search from
53 * sorted_by_sha is also possible but this was easier to code and faster.
54 * This hashtable is built after all the objects are seen.
56 static int *object_ix = NULL;
57 static int object_ix_hashsz = 0;
60 * Pack index for existing packs give us easy access to the offsets into
61 * corresponding pack file where each object's data starts, but the entries
62 * do not store the size of the compressed representation (uncompressed
63 * size is easily available by examining the pack entry header). We build
64 * a hashtable of existing packs (pack_revindex), and keep reverse index
65 * here -- pack index file is sorted by object name mapping to offset; this
66 * pack_revindex[].revindex array is an ordered list of offsets, so if you
67 * know the offset of an object, next offset is where its packed
68 * representation ends.
70 struct pack_revindex {
72 unsigned long *revindex;
73 } *pack_revindex = NULL;
74 static int pack_revindex_hashsz = 0;
79 static int written = 0;
80 static int written_delta = 0;
81 static int reused = 0;
82 static int reused_delta = 0;
84 static int pack_revindex_ix(struct packed_git *p)
86 unsigned int ui = (unsigned int) p;
89 ui = ui ^ (ui >> 16); /* defeat structure alignment */
90 i = (int)(ui % pack_revindex_hashsz);
91 while (pack_revindex[i].p) {
92 if (pack_revindex[i].p == p)
94 if (++i == pack_revindex_hashsz)
100 static void prepare_pack_ix(void)
103 struct packed_git *p;
104 for (num = 0, p = packed_git; p; p = p->next)
108 pack_revindex_hashsz = num * 11;
109 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
110 for (p = packed_git; p; p = p->next) {
111 num = pack_revindex_ix(p);
113 pack_revindex[num].p = p;
115 /* revindex elements are lazily initialized */
118 static int cmp_offset(const void *a_, const void *b_)
120 unsigned long a = *(unsigned long *) a_;
121 unsigned long b = *(unsigned long *) b_;
131 * Ordered list of offsets of objects in the pack.
133 static void prepare_pack_revindex(struct pack_revindex *rix)
135 struct packed_git *p = rix->p;
136 int num_ent = num_packed_objects(p);
138 void *index = p->index_base + 256;
140 rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
141 for (i = 0; i < num_ent; i++) {
142 long hl = *((long *)(index + 24 * i));
143 rix->revindex[i] = ntohl(hl);
145 /* This knows the pack format -- the 20-byte trailer
146 * follows immediately after the last object data.
148 rix->revindex[num_ent] = p->pack_size - 20;
149 qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset);
152 static unsigned long find_packed_object_size(struct packed_git *p,
157 struct pack_revindex *rix;
158 unsigned long *revindex;
159 num = pack_revindex_ix(p);
161 die("internal error: pack revindex uninitialized");
162 rix = &pack_revindex[num];
164 prepare_pack_revindex(rix);
165 revindex = rix->revindex;
167 hi = num_packed_objects(p) + 1;
169 int mi = (lo + hi) / 2;
170 if (revindex[mi] == ofs) {
171 return revindex[mi+1] - ofs;
173 else if (ofs < revindex[mi])
178 die("internal error: pack revindex corrupt");
181 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
183 unsigned long othersize, delta_size;
185 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
189 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
190 delta_buf = diff_delta(otherbuf, othersize,
191 buf, size, &delta_size, 0);
192 if (!delta_buf || delta_size != entry->delta_size)
193 die("delta size changed");
200 * The per-object header is a pretty dense thing, which is
201 * - first byte: low four bits are "size", then three bits of "type",
202 * and the high bit is "size continues".
203 * - each byte afterwards: low seven bits are size continuation,
204 * with the high bit being "size continues"
206 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
211 if (type < OBJ_COMMIT || type > OBJ_DELTA)
212 die("bad type %d", type);
214 c = (type << 4) | (size & 15);
226 static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
231 unsigned char header[10];
232 unsigned hdrlen, datalen;
233 enum object_type obj_type;
236 obj_type = entry->type;
237 if (! entry->in_pack)
238 to_reuse = 0; /* can't reuse what we don't have */
239 else if (obj_type == OBJ_DELTA)
240 to_reuse = 1; /* check_object() decided it for us */
241 else if (obj_type != entry->in_pack_type)
242 to_reuse = 0; /* pack has delta which is unusable */
243 else if (entry->delta)
244 to_reuse = 0; /* we want to pack afresh */
246 to_reuse = 1; /* we have it in-pack undeltified,
247 * and we do not need to deltify it.
251 buf = read_sha1_file(entry->sha1, type, &size);
253 die("unable to read %s", sha1_to_hex(entry->sha1));
254 if (size != entry->size)
255 die("object %s size inconsistency (%lu vs %lu)",
256 sha1_to_hex(entry->sha1), size, entry->size);
258 buf = delta_against(buf, size, entry);
259 size = entry->delta_size;
260 obj_type = OBJ_DELTA;
263 * The object header is a byte of 'type' followed by zero or
264 * more bytes of length. For deltas, the 20 bytes of delta
267 hdrlen = encode_header(obj_type, size, header);
268 sha1write(f, header, hdrlen);
271 sha1write(f, entry->delta, 20);
274 datalen = sha1write_compressed(f, buf, size);
278 struct packed_git *p = entry->in_pack;
281 datalen = find_packed_object_size(p, entry->in_pack_offset);
282 buf = p->pack_base + entry->in_pack_offset;
283 sha1write(f, buf, datalen);
285 hdrlen = 0; /* not really */
286 if (obj_type == OBJ_DELTA)
290 if (obj_type == OBJ_DELTA)
293 return hdrlen + datalen;
296 static unsigned long write_one(struct sha1file *f,
297 struct object_entry *e,
298 unsigned long offset)
301 /* offset starts from header size and cannot be zero
302 * if it is written already.
306 offset += write_object(f, e);
307 /* if we are deltified, write out its base object. */
309 offset = write_one(f, e->delta, offset);
313 static void write_pack_file(void)
317 unsigned long offset;
318 struct pack_header hdr;
321 f = sha1fd(1, "<stdout>");
323 f = sha1create("%s-%s.%s", base_name, sha1_to_hex(object_list_sha1), "pack");
324 hdr.hdr_signature = htonl(PACK_SIGNATURE);
325 hdr.hdr_version = htonl(PACK_VERSION);
326 hdr.hdr_entries = htonl(nr_objects);
327 sha1write(f, &hdr, sizeof(hdr));
328 offset = sizeof(hdr);
329 for (i = 0; i < nr_objects; i++)
330 offset = write_one(f, objects + i, offset);
332 sha1close(f, pack_file_sha1, 1);
335 static void write_index_file(void)
338 struct sha1file *f = sha1create("%s-%s.%s", base_name, sha1_to_hex(object_list_sha1), "idx");
339 struct object_entry **list = sorted_by_sha;
340 struct object_entry **last = list + nr_objects;
341 unsigned int array[256];
344 * Write the first-level table (the list is sorted,
345 * but we use a 256-entry lookup to be able to avoid
346 * having to do eight extra binary search iterations).
348 for (i = 0; i < 256; i++) {
349 struct object_entry **next = list;
350 while (next < last) {
351 struct object_entry *entry = *next;
352 if (entry->sha1[0] != i)
356 array[i] = htonl(next - sorted_by_sha);
359 sha1write(f, array, 256 * sizeof(int));
362 * Write the actual SHA1 entries..
364 list = sorted_by_sha;
365 for (i = 0; i < nr_objects; i++) {
366 struct object_entry *entry = *list++;
367 unsigned int offset = htonl(entry->offset);
368 sha1write(f, &offset, 4);
369 sha1write(f, entry->sha1, 20);
371 sha1write(f, pack_file_sha1, 20);
372 sha1close(f, NULL, 1);
375 static int add_object_entry(unsigned char *sha1, unsigned int hash)
377 unsigned int idx = nr_objects;
378 struct object_entry *entry;
379 struct packed_git *p;
380 unsigned int found_offset = 0;
381 struct packed_git *found_pack = NULL;
383 for (p = packed_git; p; p = p->next) {
385 if (find_pack_entry_one(sha1, &e, p)) {
388 if (local && !p->pack_local)
391 found_offset = e.offset;
397 if (idx >= nr_alloc) {
398 unsigned int needed = (idx + 1024) * 3 / 2;
399 objects = xrealloc(objects, needed * sizeof(*entry));
402 entry = objects + idx;
403 memset(entry, 0, sizeof(*entry));
404 memcpy(entry->sha1, sha1, 20);
407 entry->in_pack = found_pack;
408 entry->in_pack_offset = found_offset;
414 static int locate_object_entry_hash(unsigned char *sha1)
418 memcpy(&ui, sha1, sizeof(unsigned int));
419 i = ui % object_ix_hashsz;
420 while (0 < object_ix[i]) {
421 if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
423 if (++i == object_ix_hashsz)
429 static struct object_entry *locate_object_entry(unsigned char *sha1)
431 int i = locate_object_entry_hash(sha1);
433 return &objects[object_ix[i]-1];
437 static void check_object(struct object_entry *entry)
441 if (entry->in_pack) {
442 unsigned char base[20];
444 struct object_entry *base_entry;
446 /* We want in_pack_type even if we do not reuse delta.
447 * There is no point not reusing non-delta representations.
449 check_reuse_pack_delta(entry->in_pack,
450 entry->in_pack_offset,
452 &entry->in_pack_type);
454 /* Check if it is delta, and the base is also an object
455 * we are going to pack. If so we will reuse the existing
458 if (!no_reuse_delta &&
459 entry->in_pack_type == OBJ_DELTA &&
460 (base_entry = locate_object_entry(base))) {
462 /* Depth value does not matter - find_deltas()
463 * will never consider reused delta as the
464 * base object to deltify other objects
465 * against, in order to avoid circular deltas.
468 /* uncompressed size of the delta data */
469 entry->size = entry->delta_size = size;
470 entry->delta = base_entry;
471 entry->type = OBJ_DELTA;
473 base_entry->edge = 1;
477 /* Otherwise we would do the usual */
480 if (sha1_object_info(entry->sha1, type, &entry->size))
481 die("unable to get type of object %s",
482 sha1_to_hex(entry->sha1));
484 if (!strcmp(type, "commit")) {
485 entry->type = OBJ_COMMIT;
486 } else if (!strcmp(type, "tree")) {
487 entry->type = OBJ_TREE;
488 } else if (!strcmp(type, "blob")) {
489 entry->type = OBJ_BLOB;
490 } else if (!strcmp(type, "tag")) {
491 entry->type = OBJ_TAG;
493 die("unable to pack object %s of type %s",
494 sha1_to_hex(entry->sha1), type);
497 static void hash_objects(void)
500 struct object_entry *oe;
502 object_ix_hashsz = nr_objects * 2;
503 object_ix = xcalloc(sizeof(int), object_ix_hashsz);
504 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
505 int ix = locate_object_entry_hash(oe->sha1);
507 error("the same object '%s' added twice",
508 sha1_to_hex(oe->sha1));
512 object_ix[ix] = i + 1;
516 static void get_object_details(void)
519 struct object_entry *entry = objects;
523 for (i = 0; i < nr_objects; i++)
524 check_object(entry++);
527 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
529 static entry_sort_t current_sort;
531 static int sort_comparator(const void *_a, const void *_b)
533 struct object_entry *a = *(struct object_entry **)_a;
534 struct object_entry *b = *(struct object_entry **)_b;
535 return current_sort(a,b);
538 static struct object_entry **create_sorted_list(entry_sort_t sort)
540 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
543 for (i = 0; i < nr_objects; i++)
544 list[i] = objects + i;
546 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
550 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
552 return memcmp(a->sha1, b->sha1, 20);
555 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
557 if (a->type < b->type)
559 if (a->type > b->type)
561 if (a->hash < b->hash)
563 if (a->hash > b->hash)
565 if (a->size < b->size)
567 if (a->size > b->size)
569 return a < b ? -1 : (a > b);
573 struct object_entry *entry;
578 * We search for deltas _backwards_ in a list sorted by type and
579 * by size, so that we see progressively smaller and smaller files.
580 * That's because we prefer deltas to be from the bigger file
581 * to the smaller - deletes are potentially cheaper, but perhaps
582 * more importantly, the bigger file is likely the more recent
585 static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
587 struct object_entry *cur_entry = cur->entry;
588 struct object_entry *old_entry = old->entry;
589 unsigned long size, oldsize, delta_size, sizediff;
593 /* Don't bother doing diffs between different types */
594 if (cur_entry->type != old_entry->type)
597 /* If the current object is at edge, take the depth the objects
598 * that depend on the current object into account -- otherwise
599 * they would become too deep.
604 size = cur_entry->size;
607 oldsize = old_entry->size;
608 sizediff = oldsize > size ? oldsize - size : size - oldsize;
609 if (sizediff > size / 8)
611 if (old_entry->depth >= max_depth)
617 * We always delta from the bigger to the smaller, since that's
618 * more space-efficient (deletes don't have to say _what_ they
621 max_size = size / 2 - 20;
622 if (cur_entry->delta)
623 max_size = cur_entry->delta_size-1;
624 if (sizediff >= max_size)
626 delta_buf = diff_delta(old->data, oldsize,
627 cur->data, size, &delta_size, max_size);
630 cur_entry->delta = old_entry;
631 cur_entry->delta_size = delta_size;
632 cur_entry->depth = old_entry->depth + 1;
637 static void find_deltas(struct object_entry **list, int window, int depth)
640 unsigned int array_size = window * sizeof(struct unpacked);
641 struct unpacked *array = xmalloc(array_size);
644 memset(array, 0, array_size);
647 eye_candy = i - (nr_objects / 20);
650 struct object_entry *entry = list[i];
651 struct unpacked *n = array + idx;
656 if (progress && i <= eye_candy) {
657 eye_candy -= nr_objects / 20;
662 /* This happens if we decided to reuse existing
663 * delta from a pack. "!no_reuse_delta &&" is implied.
669 n->data = read_sha1_file(entry->sha1, type, &size);
670 if (size != entry->size)
671 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
675 unsigned int other_idx = idx + j;
677 if (other_idx >= window)
679 m = array + other_idx;
682 if (try_delta(n, m, depth) < 0)
690 for (i = 0; i < window; ++i)
695 static void prepare_pack(int window, int depth)
698 fprintf(stderr, "Packing %d objects", nr_objects);
699 get_object_details();
703 sorted_by_type = create_sorted_list(type_size_sort);
705 find_deltas(sorted_by_type, window+1, depth);
711 static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
713 static const char cache[] = "pack-cache/pack-%s.%s";
714 char *cached_pack, *cached_idx;
715 int ifd, ofd, ifd_ix = -1;
717 cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
718 ifd = open(cached_pack, O_RDONLY);
722 if (!pack_to_stdout) {
723 cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
724 ifd_ix = open(cached_idx, O_RDONLY);
732 fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
735 if (pack_to_stdout) {
742 snprintf(name, sizeof(name),
743 "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
744 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
746 die("unable to open %s (%s)", name, strerror(errno));
747 if (copy_fd(ifd, ofd))
751 snprintf(name, sizeof(name),
752 "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
753 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
755 die("unable to open %s (%s)", name, strerror(errno));
756 if (copy_fd(ifd_ix, ofd))
759 puts(sha1_to_hex(sha1));
765 int main(int argc, char **argv)
768 char line[PATH_MAX + 20];
769 int window = 10, depth = 10, pack_to_stdout = 0;
770 struct object_entry **list;
772 struct timeval prev_tv;
774 int eye_candy_incr = 500;
777 setup_git_directory();
779 for (i = 1; i < argc; i++) {
780 const char *arg = argv[i];
783 if (!strcmp("--non-empty", arg)) {
787 if (!strcmp("--local", arg)) {
791 if (!strcmp("--incremental", arg)) {
795 if (!strncmp("--window=", arg, 9)) {
797 window = strtoul(arg+9, &end, 0);
802 if (!strncmp("--depth=", arg, 8)) {
804 depth = strtoul(arg+8, &end, 0);
809 if (!strcmp("-q", arg)) {
813 if (!strcmp("--no-reuse-delta", arg)) {
817 if (!strcmp("--stdout", arg)) {
828 if (pack_to_stdout != !base_name)
831 prepare_packed_git();
833 fprintf(stderr, "Generating pack...\n");
834 gettimeofday(&prev_tv, NULL);
836 while (fgets(line, sizeof(line), stdin) != NULL) {
839 unsigned char sha1[20];
841 if (progress && (eye_candy <= nr_objects)) {
842 fprintf(stderr, "Counting objects...%d\r", nr_objects);
843 if (eye_candy && (50 <= eye_candy_incr)) {
846 gettimeofday(&tv, NULL);
847 time_diff = (tv.tv_sec - prev_tv.tv_sec);
849 time_diff += (tv.tv_usec - prev_tv.tv_usec);
850 if ((1 << 9) < time_diff)
851 eye_candy_incr += 50;
852 else if (50 < eye_candy_incr)
853 eye_candy_incr -= 50;
855 eye_candy += eye_candy_incr;
857 if (get_sha1_hex(line, sha1))
858 die("expected sha1, got garbage:\n %s", line);
862 unsigned char c = *p++;
865 hash = hash * 11 + c;
867 add_object_entry(sha1, hash);
870 fprintf(stderr, "Done counting %d objects.\n", nr_objects);
871 if (non_empty && !nr_objects)
874 sorted_by_sha = create_sorted_list(sha1_sort);
876 list = sorted_by_sha;
877 for (i = 0; i < nr_objects; i++) {
878 struct object_entry *entry = *list++;
879 SHA1_Update(&ctx, entry->sha1, 20);
881 SHA1_Final(object_list_sha1, &ctx);
883 if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
886 prepare_pack(window, depth);
887 if (!pack_to_stdout) {
889 puts(sha1_to_hex(object_list_sha1));
893 fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
894 nr_objects, written, written_delta, reused, reused_delta);