10 #include "csum-file.h"
11 #include "tree-walk.h"
14 #include "list-objects.h"
17 static const char pack_usage[] = "\
18 git-pack-objects [{ -q | --progress | --all-progress }] \n\
19 [--local] [--incremental] [--window=N] [--depth=N] \n\
20 [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\
21 [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\
22 [--stdout | base-name] [<ref-list | <object-list]";
25 unsigned char sha1[20];
26 uint32_t crc32; /* crc of raw pack data for this object */
27 off_t offset; /* offset into the final pack file */
28 unsigned long size; /* uncompressed size */
29 unsigned int hash; /* name hint hash */
30 unsigned int depth; /* delta depth */
31 struct packed_git *in_pack; /* already in pack */
33 struct object_entry *delta; /* delta base object */
34 struct object_entry *delta_child; /* deltified objects who bases me */
35 struct object_entry *delta_sibling; /* other deltified objects who
36 * uses the same base as me
38 unsigned long delta_size; /* delta data size (uncompressed) */
39 enum object_type type;
40 enum object_type in_pack_type; /* could be delta */
41 unsigned char in_pack_header_size;
42 unsigned char preferred_base; /* we do not pack this, but is available
43 * to be used as the base objectto delta
49 * Objects we are going to pack are collected in objects array (dynamically
50 * expanded). nr_objects & nr_alloc controls this array. They are stored
51 * in the order we see -- typically rev-list --objects order that gives us
52 * nice "minimum seek" order.
54 static struct object_entry *objects;
55 static struct object_entry **written_list;
56 static uint32_t nr_objects, nr_alloc, nr_result, nr_written;
59 static int no_reuse_delta, no_reuse_object;
61 static int incremental;
62 static int allow_ofs_delta;
63 static const char *pack_tmp_name, *idx_tmp_name;
64 static char tmpname[PATH_MAX];
65 static const char *base_name;
66 static unsigned char pack_file_sha1[20];
67 static int progress = 1;
68 static int window = 10;
69 static uint32_t pack_size_limit;
70 static int depth = 50;
71 static int pack_to_stdout;
72 static int num_preferred_base;
73 static struct progress progress_state;
74 static int pack_compression_level = Z_DEFAULT_COMPRESSION;
75 static int pack_compression_seen;
78 * The object names in objects array are hashed with this hashtable,
79 * to help looking up the entry by object name.
80 * This hashtable is built after all the objects are seen.
82 static int *object_ix;
83 static int object_ix_hashsz;
86 * Pack index for existing packs give us easy access to the offsets into
87 * corresponding pack file where each object's data starts, but the entries
88 * do not store the size of the compressed representation (uncompressed
89 * size is easily available by examining the pack entry header). It is
90 * also rather expensive to find the sha1 for an object given its offset.
92 * We build a hashtable of existing packs (pack_revindex), and keep reverse
93 * index here -- pack index file is sorted by object name mapping to offset;
94 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
95 * ordered by offset, so if you know the offset of an object, next offset
96 * is where its packed representation ends and the index_nr can be used to
97 * get the object sha1 from the main index.
99 struct revindex_entry {
103 struct pack_revindex {
104 struct packed_git *p;
105 struct revindex_entry *revindex;
107 static struct pack_revindex *pack_revindex;
108 static int pack_revindex_hashsz;
113 static uint32_t written, written_delta;
114 static uint32_t reused, reused_delta;
116 static int pack_revindex_ix(struct packed_git *p)
118 unsigned long ui = (unsigned long)p;
121 ui = ui ^ (ui >> 16); /* defeat structure alignment */
122 i = (int)(ui % pack_revindex_hashsz);
123 while (pack_revindex[i].p) {
124 if (pack_revindex[i].p == p)
126 if (++i == pack_revindex_hashsz)
132 static void prepare_pack_ix(void)
135 struct packed_git *p;
136 for (num = 0, p = packed_git; p; p = p->next)
140 pack_revindex_hashsz = num * 11;
141 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
142 for (p = packed_git; p; p = p->next) {
143 num = pack_revindex_ix(p);
145 pack_revindex[num].p = p;
147 /* revindex elements are lazily initialized */
150 static int cmp_offset(const void *a_, const void *b_)
152 const struct revindex_entry *a = a_;
153 const struct revindex_entry *b = b_;
154 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
158 * Ordered list of offsets of objects in the pack.
160 static void prepare_pack_revindex(struct pack_revindex *rix)
162 struct packed_git *p = rix->p;
163 int num_ent = p->num_objects;
165 const char *index = p->index_data;
167 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
170 if (p->index_version > 1) {
171 const uint32_t *off_32 =
172 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
173 const uint32_t *off_64 = off_32 + p->num_objects;
174 for (i = 0; i < num_ent; i++) {
175 uint32_t off = ntohl(*off_32++);
176 if (!(off & 0x80000000)) {
177 rix->revindex[i].offset = off;
179 rix->revindex[i].offset =
180 ((uint64_t)ntohl(*off_64++)) << 32;
181 rix->revindex[i].offset |=
184 rix->revindex[i].nr = i;
187 for (i = 0; i < num_ent; i++) {
188 uint32_t hl = *((uint32_t *)(index + 24 * i));
189 rix->revindex[i].offset = ntohl(hl);
190 rix->revindex[i].nr = i;
194 /* This knows the pack format -- the 20-byte trailer
195 * follows immediately after the last object data.
197 rix->revindex[num_ent].offset = p->pack_size - 20;
198 rix->revindex[num_ent].nr = -1;
199 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
202 static struct revindex_entry * find_packed_object(struct packed_git *p,
207 struct pack_revindex *rix;
208 struct revindex_entry *revindex;
209 num = pack_revindex_ix(p);
211 die("internal error: pack revindex uninitialized");
212 rix = &pack_revindex[num];
214 prepare_pack_revindex(rix);
215 revindex = rix->revindex;
217 hi = p->num_objects + 1;
219 int mi = (lo + hi) / 2;
220 if (revindex[mi].offset == ofs) {
221 return revindex + mi;
223 else if (ofs < revindex[mi].offset)
228 die("internal error: pack revindex corrupt");
231 static const unsigned char *find_packed_object_name(struct packed_git *p,
234 struct revindex_entry *entry = find_packed_object(p, ofs);
235 return nth_packed_object_sha1(p, entry->nr);
238 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
240 unsigned long othersize, delta_size;
241 enum object_type type;
242 void *otherbuf = read_sha1_file(entry->delta->sha1, &type, &othersize);
246 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
247 delta_buf = diff_delta(otherbuf, othersize,
248 buf, size, &delta_size, 0);
249 if (!delta_buf || delta_size != entry->delta_size)
250 die("delta size changed");
257 * The per-object header is a pretty dense thing, which is
258 * - first byte: low four bits are "size", then three bits of "type",
259 * and the high bit is "size continues".
260 * - each byte afterwards: low seven bits are size continuation,
261 * with the high bit being "size continues"
263 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
268 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
269 die("bad type %d", type);
271 c = (type << 4) | (size & 15);
284 * we are going to reuse the existing object data as is. make
285 * sure it is not corrupt.
287 static int check_pack_inflate(struct packed_git *p,
288 struct pack_window **w_curs,
291 unsigned long expect)
294 unsigned char fakebuf[4096], *in;
297 memset(&stream, 0, sizeof(stream));
298 inflateInit(&stream);
300 in = use_pack(p, w_curs, offset, &stream.avail_in);
302 stream.next_out = fakebuf;
303 stream.avail_out = sizeof(fakebuf);
304 st = inflate(&stream, Z_FINISH);
305 offset += stream.next_in - in;
306 } while (st == Z_OK || st == Z_BUF_ERROR);
308 return (st == Z_STREAM_END &&
309 stream.total_out == expect &&
310 stream.total_in == len) ? 0 : -1;
313 static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
314 off_t offset, off_t len, unsigned int nr)
316 const uint32_t *index_crc;
317 uint32_t data_crc = crc32(0, Z_NULL, 0);
321 void *data = use_pack(p, w_curs, offset, &avail);
324 data_crc = crc32(data_crc, data, avail);
329 index_crc = p->index_data;
330 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
332 return data_crc != ntohl(*index_crc);
335 static void copy_pack_data(struct sha1file *f,
336 struct packed_git *p,
337 struct pack_window **w_curs,
345 in = use_pack(p, w_curs, offset, &avail);
347 avail = (unsigned int)len;
348 sha1write(f, in, avail);
354 static unsigned long write_object(struct sha1file *f,
355 struct object_entry *entry,
359 enum object_type type;
361 unsigned char header[10];
362 unsigned char dheader[10];
365 enum object_type obj_type;
367 /* write limit if limited packsize and not first object */
368 unsigned long limit = pack_size_limit && nr_written ?
369 pack_size_limit - write_offset : 0;
371 int usable_delta = !entry->delta ? 0 :
372 /* yes if unlimited packfile */
373 !pack_size_limit ? 1 :
374 /* no if base written to previous pack */
375 entry->delta->offset == (off_t)-1 ? 0 :
376 /* otherwise double-check written to this
377 * pack, like we do below
379 entry->delta->offset ? 1 : 0;
384 obj_type = entry->type;
386 to_reuse = 0; /* explicit */
387 else if (!entry->in_pack)
388 to_reuse = 0; /* can't reuse what we don't have */
389 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
390 /* check_object() decided it for us ... */
391 to_reuse = usable_delta;
392 /* ... but pack split may override that */
393 else if (obj_type != entry->in_pack_type)
394 to_reuse = 0; /* pack has delta which is unusable */
395 else if (entry->delta)
396 to_reuse = 0; /* we want to pack afresh */
398 to_reuse = 1; /* we have it in-pack undeltified,
399 * and we do not need to deltify it.
404 unsigned long maxsize;
406 buf = read_sha1_file(entry->sha1, &type, &size);
408 die("unable to read %s", sha1_to_hex(entry->sha1));
409 if (size != entry->size)
410 die("object %s size inconsistency (%lu vs %lu)",
411 sha1_to_hex(entry->sha1), size, entry->size);
413 buf = delta_against(buf, size, entry);
414 size = entry->delta_size;
415 obj_type = (allow_ofs_delta && entry->delta->offset) ?
416 OBJ_OFS_DELTA : OBJ_REF_DELTA;
419 * recover real object type in case
420 * check_object() wanted to re-use a delta,
421 * but we couldn't since base was in previous split pack
425 /* compress the data to store and put compressed length in datalen */
426 memset(&stream, 0, sizeof(stream));
427 deflateInit(&stream, pack_compression_level);
428 maxsize = deflateBound(&stream, size);
429 out = xmalloc(maxsize);
431 stream.next_in = buf;
432 stream.avail_in = size;
433 stream.next_out = out;
434 stream.avail_out = maxsize;
435 while (deflate(&stream, Z_FINISH) == Z_OK)
438 datalen = stream.total_out;
441 * The object header is a byte of 'type' followed by zero or
442 * more bytes of length.
444 hdrlen = encode_header(obj_type, size, header);
446 if (obj_type == OBJ_OFS_DELTA) {
448 * Deltas with relative base contain an additional
449 * encoding of the relative offset for the delta
450 * base from this object's position in the pack.
452 off_t ofs = entry->offset - entry->delta->offset;
453 unsigned pos = sizeof(dheader) - 1;
454 dheader[pos] = ofs & 127;
456 dheader[--pos] = 128 | (--ofs & 127);
457 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
462 sha1write(f, header, hdrlen);
463 sha1write(f, dheader + pos, sizeof(dheader) - pos);
464 hdrlen += sizeof(dheader) - pos;
465 } else if (obj_type == OBJ_REF_DELTA) {
467 * Deltas with a base reference contain
468 * an additional 20 bytes for the base sha1.
470 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
475 sha1write(f, header, hdrlen);
476 sha1write(f, entry->delta->sha1, 20);
479 if (limit && hdrlen + datalen + 20 >= limit) {
484 sha1write(f, header, hdrlen);
486 sha1write(f, out, datalen);
491 struct packed_git *p = entry->in_pack;
492 struct pack_window *w_curs = NULL;
493 struct revindex_entry *revidx;
497 obj_type = (allow_ofs_delta && entry->delta->offset) ?
498 OBJ_OFS_DELTA : OBJ_REF_DELTA;
501 hdrlen = encode_header(obj_type, entry->size, header);
502 offset = entry->in_pack_offset;
503 revidx = find_packed_object(p, offset);
504 datalen = revidx[1].offset - offset;
505 if (!pack_to_stdout && p->index_version > 1 &&
506 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
507 die("bad packed object CRC for %s", sha1_to_hex(entry->sha1));
508 offset += entry->in_pack_header_size;
509 datalen -= entry->in_pack_header_size;
510 if (obj_type == OBJ_OFS_DELTA) {
511 off_t ofs = entry->offset - entry->delta->offset;
512 unsigned pos = sizeof(dheader) - 1;
513 dheader[pos] = ofs & 127;
515 dheader[--pos] = 128 | (--ofs & 127);
516 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit)
518 sha1write(f, header, hdrlen);
519 sha1write(f, dheader + pos, sizeof(dheader) - pos);
520 hdrlen += sizeof(dheader) - pos;
521 } else if (obj_type == OBJ_REF_DELTA) {
522 if (limit && hdrlen + 20 + datalen + 20 >= limit)
524 sha1write(f, header, hdrlen);
525 sha1write(f, entry->delta->sha1, 20);
528 if (limit && hdrlen + datalen + 20 >= limit)
530 sha1write(f, header, hdrlen);
533 if (!pack_to_stdout && p->index_version == 1 &&
534 check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
535 die("corrupt packed object for %s", sha1_to_hex(entry->sha1));
536 copy_pack_data(f, p, &w_curs, offset, datalen);
544 entry->crc32 = crc32_end(f);
545 return hdrlen + datalen;
548 static off_t write_one(struct sha1file *f,
549 struct object_entry *e,
554 /* offset is non zero if object is written already. */
555 if (e->offset || e->preferred_base)
558 /* if we are deltified, write out base object first. */
560 offset = write_one(f, e->delta, offset);
566 size = write_object(f, e, offset);
572 /* make sure off_t is sufficiently large not to wrap */
573 if (offset > offset + size)
574 die("pack too large for current definition of off_t");
575 return offset + size;
578 static int open_object_dir_tmp(const char *path)
580 snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
581 return mkstemp(tmpname);
584 /* forward declarations for write_pack_file */
585 static void write_index_file(off_t last_obj_offset, unsigned char *sha1);
586 static int adjust_perm(const char *path, mode_t mode);
588 static void write_pack_file(void)
592 off_t offset, last_obj_offset = 0;
593 struct pack_header hdr;
594 int do_progress = progress;
596 if (pack_to_stdout) {
597 f = sha1fd(1, "<stdout>");
600 int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
602 die("unable to create %s: %s\n", tmpname, strerror(errno));
603 pack_tmp_name = xstrdup(tmpname);
604 f = sha1fd(fd, pack_tmp_name);
608 start_progress(&progress_state, "Writing %u objects...", "", nr_result);
610 hdr.hdr_signature = htonl(PACK_SIGNATURE);
611 hdr.hdr_version = htonl(PACK_VERSION);
612 hdr.hdr_entries = htonl(nr_result);
613 sha1write(f, &hdr, sizeof(hdr));
614 offset = sizeof(hdr);
617 for (i = 0; i < nr_objects; i++) {
618 last_obj_offset = offset;
619 offset = write_one(f, objects + i, offset);
621 display_progress(&progress_state, written);
624 stop_progress(&progress_state);
626 if (written != nr_result)
627 die("wrote %u objects while expecting %u", written, nr_result);
628 sha1close(f, pack_file_sha1, 1);
630 if (!pack_to_stdout) {
631 unsigned char object_list_sha1[20];
632 mode_t mode = umask(0);
637 write_index_file(last_obj_offset, object_list_sha1);
638 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
639 base_name, sha1_to_hex(object_list_sha1));
640 if (adjust_perm(pack_tmp_name, mode))
641 die("unable to make temporary pack file readable: %s",
643 if (rename(pack_tmp_name, tmpname))
644 die("unable to rename temporary pack file: %s",
646 snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
647 base_name, sha1_to_hex(object_list_sha1));
648 if (adjust_perm(idx_tmp_name, mode))
649 die("unable to make temporary index file readable: %s",
651 if (rename(idx_tmp_name, tmpname))
652 die("unable to rename temporary index file: %s",
654 puts(sha1_to_hex(object_list_sha1));
658 static int sha1_sort(const void *_a, const void *_b)
660 const struct object_entry *a = *(struct object_entry **)_a;
661 const struct object_entry *b = *(struct object_entry **)_b;
662 return hashcmp(a->sha1, b->sha1);
665 static uint32_t index_default_version = 1;
666 static uint32_t index_off32_limit = 0x7fffffff;
668 static void write_index_file(off_t last_obj_offset, unsigned char *sha1)
671 struct object_entry **sorted_by_sha, **list, **last;
673 uint32_t i, index_version;
676 int fd = open_object_dir_tmp("tmp_idx_XXXXXX");
678 die("unable to create %s: %s\n", tmpname, strerror(errno));
679 idx_tmp_name = xstrdup(tmpname);
680 f = sha1fd(fd, idx_tmp_name);
685 xcalloc(nr_result, sizeof(struct object_entry *));
686 for (i = 0; i < nr_objects; i++)
687 if (!objects[i].preferred_base)
688 sorted_by_sha[j++] = objects + i;
690 die("listed %u objects while expecting %u", j, nr_result);
691 qsort(sorted_by_sha, nr_result, sizeof(*sorted_by_sha), sha1_sort);
692 list = sorted_by_sha;
693 last = sorted_by_sha + nr_result;
695 sorted_by_sha = list = last = NULL;
697 /* if last object's offset is >= 2^31 we should use index V2 */
698 index_version = (last_obj_offset >> 31) ? 2 : index_default_version;
700 /* index versions 2 and above need a header */
701 if (index_version >= 2) {
702 struct pack_idx_header hdr;
703 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
704 hdr.idx_version = htonl(index_version);
705 sha1write(f, &hdr, sizeof(hdr));
709 * Write the first-level table (the list is sorted,
710 * but we use a 256-entry lookup to be able to avoid
711 * having to do eight extra binary search iterations).
713 for (i = 0; i < 256; i++) {
714 struct object_entry **next = list;
715 while (next < last) {
716 struct object_entry *entry = *next;
717 if (entry->sha1[0] != i)
721 array[i] = htonl(next - sorted_by_sha);
724 sha1write(f, array, 256 * 4);
726 /* Compute the SHA1 hash of sorted object names. */
729 /* Write the actual SHA1 entries. */
730 list = sorted_by_sha;
731 for (i = 0; i < nr_result; i++) {
732 struct object_entry *entry = *list++;
733 if (index_version < 2) {
734 uint32_t offset = htonl(entry->offset);
735 sha1write(f, &offset, 4);
737 sha1write(f, entry->sha1, 20);
738 SHA1_Update(&ctx, entry->sha1, 20);
741 if (index_version >= 2) {
742 unsigned int nr_large_offset = 0;
744 /* write the crc32 table */
745 list = sorted_by_sha;
746 for (i = 0; i < nr_objects; i++) {
747 struct object_entry *entry = *list++;
748 uint32_t crc32_val = htonl(entry->crc32);
749 sha1write(f, &crc32_val, 4);
752 /* write the 32-bit offset table */
753 list = sorted_by_sha;
754 for (i = 0; i < nr_objects; i++) {
755 struct object_entry *entry = *list++;
756 uint32_t offset = (entry->offset <= index_off32_limit) ?
757 entry->offset : (0x80000000 | nr_large_offset++);
758 offset = htonl(offset);
759 sha1write(f, &offset, 4);
762 /* write the large offset table */
763 list = sorted_by_sha;
764 while (nr_large_offset) {
765 struct object_entry *entry = *list++;
766 uint64_t offset = entry->offset;
767 if (offset > index_off32_limit) {
769 split[0] = htonl(offset >> 32);
770 split[1] = htonl(offset & 0xffffffff);
771 sha1write(f, split, 8);
777 sha1write(f, pack_file_sha1, 20);
778 sha1close(f, NULL, 1);
780 SHA1_Final(sha1, &ctx);
783 static int locate_object_entry_hash(const unsigned char *sha1)
787 memcpy(&ui, sha1, sizeof(unsigned int));
788 i = ui % object_ix_hashsz;
789 while (0 < object_ix[i]) {
790 if (!hashcmp(sha1, objects[object_ix[i] - 1].sha1))
792 if (++i == object_ix_hashsz)
798 static struct object_entry *locate_object_entry(const unsigned char *sha1)
802 if (!object_ix_hashsz)
805 i = locate_object_entry_hash(sha1);
807 return &objects[object_ix[i]-1];
811 static void rehash_objects(void)
814 struct object_entry *oe;
816 object_ix_hashsz = nr_objects * 3;
817 if (object_ix_hashsz < 1024)
818 object_ix_hashsz = 1024;
819 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
820 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
821 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
822 int ix = locate_object_entry_hash(oe->sha1);
826 object_ix[ix] = i + 1;
830 static unsigned name_hash(const char *name)
836 * This effectively just creates a sortable number from the
837 * last sixteen non-whitespace characters. Last characters
838 * count "most", so things that end in ".c" sort together.
840 while ((c = *name++) != 0) {
843 hash = (hash >> 2) + (c << 24);
848 static int add_object_entry(const unsigned char *sha1, enum object_type type,
849 unsigned hash, int exclude)
851 struct object_entry *entry;
852 struct packed_git *p, *found_pack = NULL;
853 off_t found_offset = 0;
856 ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
859 entry = objects + object_ix[ix] - 1;
860 if (!entry->preferred_base)
862 entry->preferred_base = 1;
867 for (p = packed_git; p; p = p->next) {
868 off_t offset = find_pack_entry_one(sha1, p);
871 found_offset = offset;
878 if (local && !p->pack_local)
883 if (nr_objects >= nr_alloc) {
884 nr_alloc = (nr_alloc + 1024) * 3 / 2;
885 objects = xrealloc(objects, nr_alloc * sizeof(*entry));
888 entry = objects + nr_objects++;
889 memset(entry, 0, sizeof(*entry));
890 hashcpy(entry->sha1, sha1);
895 entry->preferred_base = 1;
899 entry->in_pack = found_pack;
900 entry->in_pack_offset = found_offset;
903 if (object_ix_hashsz * 3 <= nr_objects * 4)
906 object_ix[-1 - ix] = nr_objects;
909 display_progress(&progress_state, nr_objects);
914 struct pbase_tree_cache {
915 unsigned char sha1[20];
919 unsigned long tree_size;
922 static struct pbase_tree_cache *(pbase_tree_cache[256]);
923 static int pbase_tree_cache_ix(const unsigned char *sha1)
925 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
927 static int pbase_tree_cache_ix_incr(int ix)
929 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
932 static struct pbase_tree {
933 struct pbase_tree *next;
934 /* This is a phony "cache" entry; we are not
935 * going to evict it nor find it through _get()
936 * mechanism -- this is for the toplevel node that
937 * would almost always change with any commit.
939 struct pbase_tree_cache pcache;
942 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
944 struct pbase_tree_cache *ent, *nent;
947 enum object_type type;
949 int my_ix = pbase_tree_cache_ix(sha1);
950 int available_ix = -1;
952 /* pbase-tree-cache acts as a limited hashtable.
953 * your object will be found at your index or within a few
954 * slots after that slot if it is cached.
956 for (neigh = 0; neigh < 8; neigh++) {
957 ent = pbase_tree_cache[my_ix];
958 if (ent && !hashcmp(ent->sha1, sha1)) {
962 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
963 ((0 <= available_ix) &&
964 (!ent && pbase_tree_cache[available_ix])))
965 available_ix = my_ix;
968 my_ix = pbase_tree_cache_ix_incr(my_ix);
971 /* Did not find one. Either we got a bogus request or
972 * we need to read and perhaps cache.
974 data = read_sha1_file(sha1, &type, &size);
977 if (type != OBJ_TREE) {
982 /* We need to either cache or return a throwaway copy */
984 if (available_ix < 0)
987 ent = pbase_tree_cache[available_ix];
988 my_ix = available_ix;
992 nent = xmalloc(sizeof(*nent));
993 nent->temporary = (available_ix < 0);
996 /* evict and reuse */
997 free(ent->tree_data);
1000 hashcpy(nent->sha1, sha1);
1001 nent->tree_data = data;
1002 nent->tree_size = size;
1004 if (!nent->temporary)
1005 pbase_tree_cache[my_ix] = nent;
1009 static void pbase_tree_put(struct pbase_tree_cache *cache)
1011 if (!cache->temporary) {
1015 free(cache->tree_data);
1019 static int name_cmp_len(const char *name)
1022 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1027 static void add_pbase_object(struct tree_desc *tree,
1030 const char *fullname)
1032 struct name_entry entry;
1035 while (tree_entry(tree,&entry)) {
1036 cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
1037 memcmp(name, entry.path, cmplen);
1042 if (name[cmplen] != '/') {
1043 unsigned hash = name_hash(fullname);
1044 add_object_entry(entry.sha1,
1045 S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB,
1049 if (S_ISDIR(entry.mode)) {
1050 struct tree_desc sub;
1051 struct pbase_tree_cache *tree;
1052 const char *down = name+cmplen+1;
1053 int downlen = name_cmp_len(down);
1055 tree = pbase_tree_get(entry.sha1);
1058 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1060 add_pbase_object(&sub, down, downlen, fullname);
1061 pbase_tree_put(tree);
1066 static unsigned *done_pbase_paths;
1067 static int done_pbase_paths_num;
1068 static int done_pbase_paths_alloc;
1069 static int done_pbase_path_pos(unsigned hash)
1072 int hi = done_pbase_paths_num;
1074 int mi = (hi + lo) / 2;
1075 if (done_pbase_paths[mi] == hash)
1077 if (done_pbase_paths[mi] < hash)
1085 static int check_pbase_path(unsigned hash)
1087 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
1091 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
1092 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
1093 done_pbase_paths = xrealloc(done_pbase_paths,
1094 done_pbase_paths_alloc *
1097 done_pbase_paths_num++;
1098 if (pos < done_pbase_paths_num)
1099 memmove(done_pbase_paths + pos + 1,
1100 done_pbase_paths + pos,
1101 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1102 done_pbase_paths[pos] = hash;
1106 static void add_preferred_base_object(const char *name, unsigned hash)
1108 struct pbase_tree *it;
1111 if (!num_preferred_base || check_pbase_path(hash))
1114 cmplen = name_cmp_len(name);
1115 for (it = pbase_tree; it; it = it->next) {
1117 add_object_entry(it->pcache.sha1, OBJ_TREE, 0, 1);
1120 struct tree_desc tree;
1121 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1122 add_pbase_object(&tree, name, cmplen, name);
1127 static void add_preferred_base(unsigned char *sha1)
1129 struct pbase_tree *it;
1132 unsigned char tree_sha1[20];
1134 if (window <= num_preferred_base++)
1137 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1141 for (it = pbase_tree; it; it = it->next) {
1142 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
1148 it = xcalloc(1, sizeof(*it));
1149 it->next = pbase_tree;
1152 hashcpy(it->pcache.sha1, tree_sha1);
1153 it->pcache.tree_data = data;
1154 it->pcache.tree_size = size;
1157 static void check_object(struct object_entry *entry)
1159 if (entry->in_pack) {
1160 struct packed_git *p = entry->in_pack;
1161 struct pack_window *w_curs = NULL;
1162 const unsigned char *base_ref = NULL;
1163 struct object_entry *base_entry;
1164 unsigned long used, used_0;
1167 unsigned char *buf, c;
1169 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1172 * We want in_pack_type even if we do not reuse delta
1173 * since non-delta representations could still be reused.
1175 used = unpack_object_header_gently(buf, avail,
1176 &entry->in_pack_type,
1180 * Determine if this is a delta and if so whether we can
1181 * reuse it or not. Otherwise let's find out as cheaply as
1182 * possible what the actual type and size for this object is.
1184 switch (entry->in_pack_type) {
1186 /* Not a delta hence we've already got all we need. */
1187 entry->type = entry->in_pack_type;
1188 entry->in_pack_header_size = used;
1189 unuse_pack(&w_curs);
1192 if (!no_reuse_delta && !entry->preferred_base)
1193 base_ref = use_pack(p, &w_curs,
1194 entry->in_pack_offset + used, NULL);
1195 entry->in_pack_header_size = used + 20;
1198 buf = use_pack(p, &w_curs,
1199 entry->in_pack_offset + used, NULL);
1205 if (!ofs || MSB(ofs, 7))
1206 die("delta base offset overflow in pack for %s",
1207 sha1_to_hex(entry->sha1));
1209 ofs = (ofs << 7) + (c & 127);
1211 if (ofs >= entry->in_pack_offset)
1212 die("delta base offset out of bound for %s",
1213 sha1_to_hex(entry->sha1));
1214 ofs = entry->in_pack_offset - ofs;
1215 if (!no_reuse_delta && !entry->preferred_base)
1216 base_ref = find_packed_object_name(p, ofs);
1217 entry->in_pack_header_size = used + used_0;
1221 if (base_ref && (base_entry = locate_object_entry(base_ref))) {
1223 * If base_ref was set above that means we wish to
1224 * reuse delta data, and we even found that base
1225 * in the list of objects we want to pack. Goodie!
1227 * Depth value does not matter - find_deltas() will
1228 * never consider reused delta as the base object to
1229 * deltify other objects against, in order to avoid
1232 entry->type = entry->in_pack_type;
1233 entry->delta = base_entry;
1234 entry->delta_sibling = base_entry->delta_child;
1235 base_entry->delta_child = entry;
1236 unuse_pack(&w_curs);
1242 * This must be a delta and we already know what the
1243 * final object type is. Let's extract the actual
1244 * object size from the delta header.
1246 entry->size = get_size_from_delta(p, &w_curs,
1247 entry->in_pack_offset + entry->in_pack_header_size);
1248 unuse_pack(&w_curs);
1253 * No choice but to fall back to the recursive delta walk
1254 * with sha1_object_info() to find about the object type
1257 unuse_pack(&w_curs);
1260 entry->type = sha1_object_info(entry->sha1, &entry->size);
1261 if (entry->type < 0)
1262 die("unable to get type of object %s",
1263 sha1_to_hex(entry->sha1));
1266 static int pack_offset_sort(const void *_a, const void *_b)
1268 const struct object_entry *a = *(struct object_entry **)_a;
1269 const struct object_entry *b = *(struct object_entry **)_b;
1271 /* avoid filesystem trashing with loose objects */
1272 if (!a->in_pack && !b->in_pack)
1273 return hashcmp(a->sha1, b->sha1);
1275 if (a->in_pack < b->in_pack)
1277 if (a->in_pack > b->in_pack)
1279 return a->in_pack_offset < b->in_pack_offset ? -1 :
1280 (a->in_pack_offset > b->in_pack_offset);
1283 static void get_object_details(void)
1286 struct object_entry **sorted_by_offset;
1288 sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));
1289 for (i = 0; i < nr_objects; i++)
1290 sorted_by_offset[i] = objects + i;
1291 qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
1294 for (i = 0; i < nr_objects; i++)
1295 check_object(sorted_by_offset[i]);
1296 free(sorted_by_offset);
1299 static int type_size_sort(const void *_a, const void *_b)
1301 const struct object_entry *a = *(struct object_entry **)_a;
1302 const struct object_entry *b = *(struct object_entry **)_b;
1304 if (a->type < b->type)
1306 if (a->type > b->type)
1308 if (a->hash < b->hash)
1310 if (a->hash > b->hash)
1312 if (a->preferred_base < b->preferred_base)
1314 if (a->preferred_base > b->preferred_base)
1316 if (a->size < b->size)
1318 if (a->size > b->size)
1320 return a > b ? -1 : (a < b); /* newest last */
1324 struct object_entry *entry;
1326 struct delta_index *index;
1330 * We search for deltas _backwards_ in a list sorted by type and
1331 * by size, so that we see progressively smaller and smaller files.
1332 * That's because we prefer deltas to be from the bigger file
1333 * to the smaller - deletes are potentially cheaper, but perhaps
1334 * more importantly, the bigger file is likely the more recent
1337 static int try_delta(struct unpacked *trg, struct unpacked *src,
1340 struct object_entry *trg_entry = trg->entry;
1341 struct object_entry *src_entry = src->entry;
1342 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1343 enum object_type type;
1346 /* Don't bother doing diffs between different types */
1347 if (trg_entry->type != src_entry->type)
1350 /* We do not compute delta to *create* objects we are not
1353 if (trg_entry->preferred_base)
1357 * We do not bother to try a delta that we discarded
1358 * on an earlier try, but only when reusing delta data.
1360 if (!no_reuse_delta && trg_entry->in_pack &&
1361 trg_entry->in_pack == src_entry->in_pack &&
1362 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1363 trg_entry->in_pack_type != OBJ_OFS_DELTA)
1366 /* Let's not bust the allowed depth. */
1367 if (src_entry->depth >= max_depth)
1370 /* Now some size filtering heuristics. */
1371 trg_size = trg_entry->size;
1372 max_size = trg_size/2 - 20;
1373 max_size = max_size * (max_depth - src_entry->depth) / max_depth;
1376 if (trg_entry->delta && trg_entry->delta_size <= max_size)
1377 max_size = trg_entry->delta_size-1;
1378 src_size = src_entry->size;
1379 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1380 if (sizediff >= max_size)
1383 /* Load data if not already done */
1385 trg->data = read_sha1_file(trg_entry->sha1, &type, &sz);
1387 die("object %s inconsistent object length (%lu vs %lu)",
1388 sha1_to_hex(trg_entry->sha1), sz, trg_size);
1391 src->data = read_sha1_file(src_entry->sha1, &type, &sz);
1393 die("object %s inconsistent object length (%lu vs %lu)",
1394 sha1_to_hex(src_entry->sha1), sz, src_size);
1397 src->index = create_delta_index(src->data, src_size);
1399 die("out of memory");
1402 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1406 trg_entry->delta = src_entry;
1407 trg_entry->delta_size = delta_size;
1408 trg_entry->depth = src_entry->depth + 1;
1413 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1415 struct object_entry *child = me->delta_child;
1418 unsigned int c = check_delta_limit(child, n + 1);
1421 child = child->delta_sibling;
1426 static void find_deltas(struct object_entry **list, int window, int depth)
1428 uint32_t i = nr_objects, idx = 0, processed = 0;
1429 unsigned int array_size = window * sizeof(struct unpacked);
1430 struct unpacked *array;
1435 array = xmalloc(array_size);
1436 memset(array, 0, array_size);
1438 start_progress(&progress_state, "Deltifying %u objects...", "", nr_result);
1441 struct object_entry *entry = list[--i];
1442 struct unpacked *n = array + idx;
1445 if (!entry->preferred_base)
1449 display_progress(&progress_state, processed);
1452 /* This happens if we decided to reuse existing
1453 * delta from a pack. "!no_reuse_delta &&" is implied.
1457 if (entry->size < 50)
1459 free_delta_index(n->index);
1466 * If the current object is at pack edge, take the depth the
1467 * objects that depend on the current object into account
1468 * otherwise they would become too deep.
1471 if (entry->delta_child) {
1472 max_depth -= check_delta_limit(entry, 0);
1479 uint32_t other_idx = idx + j;
1481 if (other_idx >= window)
1482 other_idx -= window;
1483 m = array + other_idx;
1486 if (try_delta(n, m, max_depth) < 0)
1490 /* if we made n a delta, and if n is already at max
1491 * depth, leaving it in the window is pointless. we
1492 * should evict it first.
1494 if (entry->delta && depth <= entry->depth)
1504 stop_progress(&progress_state);
1506 for (i = 0; i < window; ++i) {
1507 free_delta_index(array[i].index);
1508 free(array[i].data);
1513 static void prepare_pack(int window, int depth)
1515 struct object_entry **delta_list;
1518 get_object_details();
1520 if (!window || !depth)
1523 delta_list = xmalloc(nr_objects * sizeof(*delta_list));
1524 for (i = 0; i < nr_objects; i++)
1525 delta_list[i] = objects + i;
1526 qsort(delta_list, nr_objects, sizeof(*delta_list), type_size_sort);
1527 find_deltas(delta_list, window+1, depth);
1531 static int git_pack_config(const char *k, const char *v)
1533 if(!strcmp(k, "pack.window")) {
1534 window = git_config_int(k, v);
1537 if(!strcmp(k, "pack.depth")) {
1538 depth = git_config_int(k, v);
1541 if (!strcmp(k, "pack.compression")) {
1542 int level = git_config_int(k, v);
1544 level = Z_DEFAULT_COMPRESSION;
1545 else if (level < 0 || level > Z_BEST_COMPRESSION)
1546 die("bad pack compression level %d", level);
1547 pack_compression_level = level;
1548 pack_compression_seen = 1;
1551 return git_default_config(k, v);
1554 static void read_object_list_from_stdin(void)
1556 char line[40 + 1 + PATH_MAX + 2];
1557 unsigned char sha1[20];
1561 if (!fgets(line, sizeof(line), stdin)) {
1565 die("fgets returned NULL, not EOF, not error!");
1567 die("fgets: %s", strerror(errno));
1571 if (line[0] == '-') {
1572 if (get_sha1_hex(line+1, sha1))
1573 die("expected edge sha1, got garbage:\n %s",
1575 add_preferred_base(sha1);
1578 if (get_sha1_hex(line, sha1))
1579 die("expected sha1, got garbage:\n %s", line);
1581 hash = name_hash(line+41);
1582 add_preferred_base_object(line+41, hash);
1583 add_object_entry(sha1, 0, hash, 0);
1587 static void show_commit(struct commit *commit)
1589 add_object_entry(commit->object.sha1, OBJ_COMMIT, 0, 0);
1592 static void show_object(struct object_array_entry *p)
1594 unsigned hash = name_hash(p->name);
1595 add_preferred_base_object(p->name, hash);
1596 add_object_entry(p->item->sha1, p->item->type, hash, 0);
1599 static void show_edge(struct commit *commit)
1601 add_preferred_base(commit->object.sha1);
1604 static void get_object_list(int ac, const char **av)
1606 struct rev_info revs;
1610 init_revisions(&revs, NULL);
1611 save_commit_buffer = 0;
1612 track_object_refs = 0;
1613 setup_revisions(ac, av, &revs, NULL);
1615 while (fgets(line, sizeof(line), stdin) != NULL) {
1616 int len = strlen(line);
1617 if (line[len - 1] == '\n')
1622 if (!strcmp(line, "--not")) {
1623 flags ^= UNINTERESTING;
1626 die("not a rev '%s'", line);
1628 if (handle_revision_arg(line, &revs, flags, 1))
1629 die("bad revision '%s'", line);
1632 prepare_revision_walk(&revs);
1633 mark_edges_uninteresting(revs.commits, &revs, show_edge);
1634 traverse_commit_list(&revs, show_commit, show_object);
1637 static int adjust_perm(const char *path, mode_t mode)
1639 if (chmod(path, mode))
1641 return adjust_shared_perm(path);
1644 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1646 int use_internal_rev_list = 0;
1650 int rp_ac_alloc = 64;
1653 rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
1655 rp_av[0] = "pack-objects";
1656 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1659 git_config(git_pack_config);
1660 if (!pack_compression_seen && core_compression_seen)
1661 pack_compression_level = core_compression_level;
1663 progress = isatty(2);
1664 for (i = 1; i < argc; i++) {
1665 const char *arg = argv[i];
1670 if (!strcmp("--non-empty", arg)) {
1674 if (!strcmp("--local", arg)) {
1678 if (!strcmp("--incremental", arg)) {
1682 if (!prefixcmp(arg, "--compression=")) {
1684 int level = strtoul(arg+14, &end, 0);
1685 if (!arg[14] || *end)
1688 level = Z_DEFAULT_COMPRESSION;
1689 else if (level < 0 || level > Z_BEST_COMPRESSION)
1690 die("bad pack compression level %d", level);
1691 pack_compression_level = level;
1694 if (!prefixcmp(arg, "--window=")) {
1696 window = strtoul(arg+9, &end, 0);
1697 if (!arg[9] || *end)
1701 if (!prefixcmp(arg, "--depth=")) {
1703 depth = strtoul(arg+8, &end, 0);
1704 if (!arg[8] || *end)
1708 if (!strcmp("--progress", arg)) {
1712 if (!strcmp("--all-progress", arg)) {
1716 if (!strcmp("-q", arg)) {
1720 if (!strcmp("--no-reuse-delta", arg)) {
1724 if (!strcmp("--no-reuse-object", arg)) {
1725 no_reuse_object = no_reuse_delta = 1;
1728 if (!strcmp("--delta-base-offset", arg)) {
1729 allow_ofs_delta = 1;
1732 if (!strcmp("--stdout", arg)) {
1736 if (!strcmp("--revs", arg)) {
1737 use_internal_rev_list = 1;
1740 if (!strcmp("--unpacked", arg) ||
1741 !prefixcmp(arg, "--unpacked=") ||
1742 !strcmp("--reflog", arg) ||
1743 !strcmp("--all", arg)) {
1744 use_internal_rev_list = 1;
1745 if (rp_ac >= rp_ac_alloc - 1) {
1746 rp_ac_alloc = alloc_nr(rp_ac_alloc);
1747 rp_av = xrealloc(rp_av,
1748 rp_ac_alloc * sizeof(*rp_av));
1750 rp_av[rp_ac++] = arg;
1753 if (!strcmp("--thin", arg)) {
1754 use_internal_rev_list = 1;
1756 rp_av[1] = "--objects-edge";
1759 if (!prefixcmp(arg, "--index-version=")) {
1761 index_default_version = strtoul(arg + 16, &c, 10);
1762 if (index_default_version > 2)
1765 index_off32_limit = strtoul(c+1, &c, 0);
1766 if (*c || index_off32_limit & 0x80000000)
1773 /* Traditionally "pack-objects [options] base extra" failed;
1774 * we would however want to take refs parameter that would
1775 * have been given to upstream rev-list ourselves, which means
1776 * we somehow want to say what the base name is. So the
1779 * pack-objects [options] base <refs...>
1781 * in other words, we would treat the first non-option as the
1782 * base_name and send everything else to the internal revision
1786 if (!pack_to_stdout)
1787 base_name = argv[i++];
1789 if (pack_to_stdout != !base_name)
1792 if (!pack_to_stdout && thin)
1793 die("--thin cannot be used to build an indexable pack.");
1795 prepare_packed_git();
1798 start_progress(&progress_state, "Generating pack...",
1799 "Counting objects: ", 0);
1800 if (!use_internal_rev_list)
1801 read_object_list_from_stdin();
1803 rp_av[rp_ac] = NULL;
1804 get_object_list(rp_ac, rp_av);
1807 stop_progress(&progress_state);
1808 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1811 if (non_empty && !nr_result)
1813 if (progress && (nr_objects != nr_result))
1814 fprintf(stderr, "Result has %u objects.\n", nr_result);
1816 prepare_pack(window, depth);
1819 fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
1820 written, written_delta, reused, reused_delta);