11 #include "csum-file.h"
12 #include "tree-walk.h"
15 #include "list-objects.h"
18 static const char pack_usage[] = "\
19 git-pack-objects [{ -q | --progress | --all-progress }] \n\
20 [--max-pack-size=N] [--local] [--incremental] \n\
21 [--window=N] [--window-memory=N] [--depth=N] \n\
22 [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\
23 [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\
24 [--stdout | base-name] [<ref-list | <object-list]";
27 struct pack_idx_entry idx;
28 unsigned long size; /* uncompressed size */
29 struct packed_git *in_pack; /* already in pack */
31 struct object_entry *delta; /* delta base object */
32 struct object_entry *delta_child; /* deltified objects who bases me */
33 struct object_entry *delta_sibling; /* other deltified objects who
34 * uses the same base as me
36 void *delta_data; /* cached delta (uncompressed) */
37 unsigned long delta_size; /* delta data size (uncompressed) */
38 unsigned int hash; /* name hint hash */
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 object to delta
46 unsigned char no_try_delta;
50 * Objects we are going to pack are collected in objects array (dynamically
51 * expanded). nr_objects & nr_alloc controls this array. They are stored
52 * in the order we see -- typically rev-list --objects order that gives us
53 * nice "minimum seek" order.
55 static struct object_entry *objects;
56 static struct object_entry **written_list;
57 static uint32_t nr_objects, nr_alloc, nr_result, nr_written;
60 static int no_reuse_delta, no_reuse_object;
62 static int incremental;
63 static int allow_ofs_delta;
64 static const char *pack_tmp_name, *idx_tmp_name;
65 static char tmpname[PATH_MAX];
66 static const char *base_name;
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;
77 static unsigned long delta_cache_size = 0;
78 static unsigned long max_delta_cache_size = 0;
79 static unsigned long cache_max_small_delta_size = 1000;
81 static unsigned long window_memory_limit = 0;
84 * The object names in objects array are hashed with this hashtable,
85 * to help looking up the entry by object name.
86 * This hashtable is built after all the objects are seen.
88 static int *object_ix;
89 static int object_ix_hashsz;
92 * Pack index for existing packs give us easy access to the offsets into
93 * corresponding pack file where each object's data starts, but the entries
94 * do not store the size of the compressed representation (uncompressed
95 * size is easily available by examining the pack entry header). It is
96 * also rather expensive to find the sha1 for an object given its offset.
98 * We build a hashtable of existing packs (pack_revindex), and keep reverse
99 * index here -- pack index file is sorted by object name mapping to offset;
100 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
101 * ordered by offset, so if you know the offset of an object, next offset
102 * is where its packed representation ends and the index_nr can be used to
103 * get the object sha1 from the main index.
105 struct revindex_entry {
109 struct pack_revindex {
110 struct packed_git *p;
111 struct revindex_entry *revindex;
113 static struct pack_revindex *pack_revindex;
114 static int pack_revindex_hashsz;
119 static uint32_t written, written_delta;
120 static uint32_t reused, reused_delta;
122 static int pack_revindex_ix(struct packed_git *p)
124 unsigned long ui = (unsigned long)p;
127 ui = ui ^ (ui >> 16); /* defeat structure alignment */
128 i = (int)(ui % pack_revindex_hashsz);
129 while (pack_revindex[i].p) {
130 if (pack_revindex[i].p == p)
132 if (++i == pack_revindex_hashsz)
138 static void prepare_pack_ix(void)
141 struct packed_git *p;
142 for (num = 0, p = packed_git; p; p = p->next)
146 pack_revindex_hashsz = num * 11;
147 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
148 for (p = packed_git; p; p = p->next) {
149 num = pack_revindex_ix(p);
151 pack_revindex[num].p = p;
153 /* revindex elements are lazily initialized */
156 static int cmp_offset(const void *a_, const void *b_)
158 const struct revindex_entry *a = a_;
159 const struct revindex_entry *b = b_;
160 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
164 * Ordered list of offsets of objects in the pack.
166 static void prepare_pack_revindex(struct pack_revindex *rix)
168 struct packed_git *p = rix->p;
169 int num_ent = p->num_objects;
171 const char *index = p->index_data;
173 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
176 if (p->index_version > 1) {
177 const uint32_t *off_32 =
178 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
179 const uint32_t *off_64 = off_32 + p->num_objects;
180 for (i = 0; i < num_ent; i++) {
181 uint32_t off = ntohl(*off_32++);
182 if (!(off & 0x80000000)) {
183 rix->revindex[i].offset = off;
185 rix->revindex[i].offset =
186 ((uint64_t)ntohl(*off_64++)) << 32;
187 rix->revindex[i].offset |=
190 rix->revindex[i].nr = i;
193 for (i = 0; i < num_ent; i++) {
194 uint32_t hl = *((uint32_t *)(index + 24 * i));
195 rix->revindex[i].offset = ntohl(hl);
196 rix->revindex[i].nr = i;
200 /* This knows the pack format -- the 20-byte trailer
201 * follows immediately after the last object data.
203 rix->revindex[num_ent].offset = p->pack_size - 20;
204 rix->revindex[num_ent].nr = -1;
205 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
208 static struct revindex_entry * find_packed_object(struct packed_git *p,
213 struct pack_revindex *rix;
214 struct revindex_entry *revindex;
215 num = pack_revindex_ix(p);
217 die("internal error: pack revindex uninitialized");
218 rix = &pack_revindex[num];
220 prepare_pack_revindex(rix);
221 revindex = rix->revindex;
223 hi = p->num_objects + 1;
225 int mi = (lo + hi) / 2;
226 if (revindex[mi].offset == ofs) {
227 return revindex + mi;
229 else if (ofs < revindex[mi].offset)
234 die("internal error: pack revindex corrupt");
237 static const unsigned char *find_packed_object_name(struct packed_git *p,
240 struct revindex_entry *entry = find_packed_object(p, ofs);
241 return nth_packed_object_sha1(p, entry->nr);
244 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
246 unsigned long othersize, delta_size;
247 enum object_type type;
248 void *otherbuf = read_sha1_file(entry->delta->idx.sha1, &type, &othersize);
252 die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1));
253 delta_buf = diff_delta(otherbuf, othersize,
254 buf, size, &delta_size, 0);
255 if (!delta_buf || delta_size != entry->delta_size)
256 die("delta size changed");
263 * The per-object header is a pretty dense thing, which is
264 * - first byte: low four bits are "size", then three bits of "type",
265 * and the high bit is "size continues".
266 * - each byte afterwards: low seven bits are size continuation,
267 * with the high bit being "size continues"
269 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
274 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
275 die("bad type %d", type);
277 c = (type << 4) | (size & 15);
290 * we are going to reuse the existing object data as is. make
291 * sure it is not corrupt.
293 static int check_pack_inflate(struct packed_git *p,
294 struct pack_window **w_curs,
297 unsigned long expect)
300 unsigned char fakebuf[4096], *in;
303 memset(&stream, 0, sizeof(stream));
304 inflateInit(&stream);
306 in = use_pack(p, w_curs, offset, &stream.avail_in);
308 stream.next_out = fakebuf;
309 stream.avail_out = sizeof(fakebuf);
310 st = inflate(&stream, Z_FINISH);
311 offset += stream.next_in - in;
312 } while (st == Z_OK || st == Z_BUF_ERROR);
314 return (st == Z_STREAM_END &&
315 stream.total_out == expect &&
316 stream.total_in == len) ? 0 : -1;
319 static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
320 off_t offset, off_t len, unsigned int nr)
322 const uint32_t *index_crc;
323 uint32_t data_crc = crc32(0, Z_NULL, 0);
327 void *data = use_pack(p, w_curs, offset, &avail);
330 data_crc = crc32(data_crc, data, avail);
335 index_crc = p->index_data;
336 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
338 return data_crc != ntohl(*index_crc);
341 static void copy_pack_data(struct sha1file *f,
342 struct packed_git *p,
343 struct pack_window **w_curs,
351 in = use_pack(p, w_curs, offset, &avail);
353 avail = (unsigned int)len;
354 sha1write(f, in, avail);
360 static unsigned long write_object(struct sha1file *f,
361 struct object_entry *entry,
365 enum object_type type;
367 unsigned char header[10];
368 unsigned char dheader[10];
371 enum object_type obj_type;
373 /* write limit if limited packsize and not first object */
374 unsigned long limit = pack_size_limit && nr_written ?
375 pack_size_limit - write_offset : 0;
377 int usable_delta = !entry->delta ? 0 :
378 /* yes if unlimited packfile */
379 !pack_size_limit ? 1 :
380 /* no if base written to previous pack */
381 entry->delta->idx.offset == (off_t)-1 ? 0 :
382 /* otherwise double-check written to this
383 * pack, like we do below
385 entry->delta->idx.offset ? 1 : 0;
390 obj_type = entry->type;
392 to_reuse = 0; /* explicit */
393 else if (!entry->in_pack)
394 to_reuse = 0; /* can't reuse what we don't have */
395 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
396 /* check_object() decided it for us ... */
397 to_reuse = usable_delta;
398 /* ... but pack split may override that */
399 else if (obj_type != entry->in_pack_type)
400 to_reuse = 0; /* pack has delta which is unusable */
401 else if (entry->delta)
402 to_reuse = 0; /* we want to pack afresh */
404 to_reuse = 1; /* we have it in-pack undeltified,
405 * and we do not need to deltify it.
410 unsigned long maxsize;
413 buf = read_sha1_file(entry->idx.sha1, &obj_type, &size);
415 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
416 } else if (entry->delta_data) {
417 size = entry->delta_size;
418 buf = entry->delta_data;
419 entry->delta_data = NULL;
420 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
421 OBJ_OFS_DELTA : OBJ_REF_DELTA;
423 buf = read_sha1_file(entry->idx.sha1, &type, &size);
425 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
426 buf = delta_against(buf, size, entry);
427 size = entry->delta_size;
428 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
429 OBJ_OFS_DELTA : OBJ_REF_DELTA;
431 /* compress the data to store and put compressed length in datalen */
432 memset(&stream, 0, sizeof(stream));
433 deflateInit(&stream, pack_compression_level);
434 maxsize = deflateBound(&stream, size);
435 out = xmalloc(maxsize);
437 stream.next_in = buf;
438 stream.avail_in = size;
439 stream.next_out = out;
440 stream.avail_out = maxsize;
441 while (deflate(&stream, Z_FINISH) == Z_OK)
444 datalen = stream.total_out;
447 * The object header is a byte of 'type' followed by zero or
448 * more bytes of length.
450 hdrlen = encode_header(obj_type, size, header);
452 if (obj_type == OBJ_OFS_DELTA) {
454 * Deltas with relative base contain an additional
455 * encoding of the relative offset for the delta
456 * base from this object's position in the pack.
458 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
459 unsigned pos = sizeof(dheader) - 1;
460 dheader[pos] = ofs & 127;
462 dheader[--pos] = 128 | (--ofs & 127);
463 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
468 sha1write(f, header, hdrlen);
469 sha1write(f, dheader + pos, sizeof(dheader) - pos);
470 hdrlen += sizeof(dheader) - pos;
471 } else if (obj_type == OBJ_REF_DELTA) {
473 * Deltas with a base reference contain
474 * an additional 20 bytes for the base sha1.
476 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
481 sha1write(f, header, hdrlen);
482 sha1write(f, entry->delta->idx.sha1, 20);
485 if (limit && hdrlen + datalen + 20 >= limit) {
490 sha1write(f, header, hdrlen);
492 sha1write(f, out, datalen);
497 struct packed_git *p = entry->in_pack;
498 struct pack_window *w_curs = NULL;
499 struct revindex_entry *revidx;
503 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
504 OBJ_OFS_DELTA : OBJ_REF_DELTA;
507 hdrlen = encode_header(obj_type, entry->size, header);
508 offset = entry->in_pack_offset;
509 revidx = find_packed_object(p, offset);
510 datalen = revidx[1].offset - offset;
511 if (!pack_to_stdout && p->index_version > 1 &&
512 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
513 die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
514 offset += entry->in_pack_header_size;
515 datalen -= entry->in_pack_header_size;
516 if (obj_type == OBJ_OFS_DELTA) {
517 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
518 unsigned pos = sizeof(dheader) - 1;
519 dheader[pos] = ofs & 127;
521 dheader[--pos] = 128 | (--ofs & 127);
522 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit)
524 sha1write(f, header, hdrlen);
525 sha1write(f, dheader + pos, sizeof(dheader) - pos);
526 hdrlen += sizeof(dheader) - pos;
527 } else if (obj_type == OBJ_REF_DELTA) {
528 if (limit && hdrlen + 20 + datalen + 20 >= limit)
530 sha1write(f, header, hdrlen);
531 sha1write(f, entry->delta->idx.sha1, 20);
534 if (limit && hdrlen + datalen + 20 >= limit)
536 sha1write(f, header, hdrlen);
539 if (!pack_to_stdout && p->index_version == 1 &&
540 check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
541 die("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1));
542 copy_pack_data(f, p, &w_curs, offset, datalen);
550 entry->idx.crc32 = crc32_end(f);
551 return hdrlen + datalen;
554 static off_t write_one(struct sha1file *f,
555 struct object_entry *e,
560 /* offset is non zero if object is written already. */
561 if (e->idx.offset || e->preferred_base)
564 /* if we are deltified, write out base object first. */
566 offset = write_one(f, e->delta, offset);
571 e->idx.offset = offset;
572 size = write_object(f, e, offset);
577 written_list[nr_written++] = e;
579 /* make sure off_t is sufficiently large not to wrap */
580 if (offset > offset + size)
581 die("pack too large for current definition of off_t");
582 return offset + size;
585 static int open_object_dir_tmp(const char *path)
587 snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
588 return xmkstemp(tmpname);
591 /* forward declaration for write_pack_file */
592 static int adjust_perm(const char *path, mode_t mode);
594 static void write_pack_file(void)
598 off_t offset, offset_one, last_obj_offset = 0;
599 struct pack_header hdr;
600 int do_progress = progress >> pack_to_stdout;
601 uint32_t nr_remaining = nr_result;
604 start_progress(&progress_state, "Writing %u objects...", "", nr_result);
605 written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
608 unsigned char sha1[20];
610 if (pack_to_stdout) {
611 f = sha1fd(1, "<stdout>");
613 int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
614 pack_tmp_name = xstrdup(tmpname);
615 f = sha1fd(fd, pack_tmp_name);
618 hdr.hdr_signature = htonl(PACK_SIGNATURE);
619 hdr.hdr_version = htonl(PACK_VERSION);
620 hdr.hdr_entries = htonl(nr_remaining);
621 sha1write(f, &hdr, sizeof(hdr));
622 offset = sizeof(hdr);
624 for (; i < nr_objects; i++) {
625 last_obj_offset = offset;
626 offset_one = write_one(f, objects + i, offset);
631 display_progress(&progress_state, written);
635 * Did we write the wrong # entries in the header?
636 * If so, rewrite it like in fast-import
638 if (pack_to_stdout || nr_written == nr_remaining) {
639 sha1close(f, sha1, 1);
641 sha1close(f, sha1, 0);
642 fixup_pack_header_footer(f->fd, sha1, pack_tmp_name, nr_written);
646 if (!pack_to_stdout) {
647 mode_t mode = umask(0);
652 idx_tmp_name = write_idx_file(NULL,
653 (struct pack_idx_entry **) written_list, nr_written, sha1);
654 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
655 base_name, sha1_to_hex(sha1));
656 if (adjust_perm(pack_tmp_name, mode))
657 die("unable to make temporary pack file readable: %s",
659 if (rename(pack_tmp_name, tmpname))
660 die("unable to rename temporary pack file: %s",
662 snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
663 base_name, sha1_to_hex(sha1));
664 if (adjust_perm(idx_tmp_name, mode))
665 die("unable to make temporary index file readable: %s",
667 if (rename(idx_tmp_name, tmpname))
668 die("unable to rename temporary index file: %s",
670 puts(sha1_to_hex(sha1));
673 /* mark written objects as written to previous pack */
674 for (j = 0; j < nr_written; j++) {
675 written_list[j]->idx.offset = (off_t)-1;
677 nr_remaining -= nr_written;
678 } while (nr_remaining && i < nr_objects);
682 stop_progress(&progress_state);
683 if (written != nr_result)
684 die("wrote %u objects while expecting %u", written, nr_result);
686 * We have scanned through [0 ... i). Since we have written
687 * the correct number of objects, the remaining [i ... nr_objects)
688 * items must be either already written (due to out-of-order delta base)
689 * or a preferred base. Count those which are neither and complain if any.
691 for (j = 0; i < nr_objects; i++) {
692 struct object_entry *e = objects + i;
693 j += !e->idx.offset && !e->preferred_base;
696 die("wrote %u objects as expected but %u unwritten", written, j);
699 static int locate_object_entry_hash(const unsigned char *sha1)
703 memcpy(&ui, sha1, sizeof(unsigned int));
704 i = ui % object_ix_hashsz;
705 while (0 < object_ix[i]) {
706 if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1))
708 if (++i == object_ix_hashsz)
714 static struct object_entry *locate_object_entry(const unsigned char *sha1)
718 if (!object_ix_hashsz)
721 i = locate_object_entry_hash(sha1);
723 return &objects[object_ix[i]-1];
727 static void rehash_objects(void)
730 struct object_entry *oe;
732 object_ix_hashsz = nr_objects * 3;
733 if (object_ix_hashsz < 1024)
734 object_ix_hashsz = 1024;
735 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
736 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
737 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
738 int ix = locate_object_entry_hash(oe->idx.sha1);
742 object_ix[ix] = i + 1;
746 static unsigned name_hash(const char *name)
755 * This effectively just creates a sortable number from the
756 * last sixteen non-whitespace characters. Last characters
757 * count "most", so things that end in ".c" sort together.
759 while ((c = *name++) != 0) {
762 hash = (hash >> 2) + (c << 24);
767 static void setup_delta_attr_check(struct git_attr_check *check)
769 static struct git_attr *attr_delta;
772 attr_delta = git_attr("delta", 5);
774 check[0].attr = attr_delta;
777 static int no_try_delta(const char *path)
779 struct git_attr_check check[1];
781 setup_delta_attr_check(check);
782 if (git_checkattr(path, ARRAY_SIZE(check), check))
784 if (ATTR_FALSE(check->value))
789 static int add_object_entry(const unsigned char *sha1, enum object_type type,
790 const char *name, int exclude)
792 struct object_entry *entry;
793 struct packed_git *p, *found_pack = NULL;
794 off_t found_offset = 0;
796 unsigned hash = name_hash(name);
798 ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
801 entry = objects + object_ix[ix] - 1;
802 if (!entry->preferred_base)
804 entry->preferred_base = 1;
809 for (p = packed_git; p; p = p->next) {
810 off_t offset = find_pack_entry_one(sha1, p);
813 found_offset = offset;
820 if (local && !p->pack_local)
825 if (nr_objects >= nr_alloc) {
826 nr_alloc = (nr_alloc + 1024) * 3 / 2;
827 objects = xrealloc(objects, nr_alloc * sizeof(*entry));
830 entry = objects + nr_objects++;
831 memset(entry, 0, sizeof(*entry));
832 hashcpy(entry->idx.sha1, sha1);
837 entry->preferred_base = 1;
841 entry->in_pack = found_pack;
842 entry->in_pack_offset = found_offset;
845 if (object_ix_hashsz * 3 <= nr_objects * 4)
848 object_ix[-1 - ix] = nr_objects;
851 display_progress(&progress_state, nr_objects);
853 if (name && no_try_delta(name))
854 entry->no_try_delta = 1;
859 struct pbase_tree_cache {
860 unsigned char sha1[20];
864 unsigned long tree_size;
867 static struct pbase_tree_cache *(pbase_tree_cache[256]);
868 static int pbase_tree_cache_ix(const unsigned char *sha1)
870 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
872 static int pbase_tree_cache_ix_incr(int ix)
874 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
877 static struct pbase_tree {
878 struct pbase_tree *next;
879 /* This is a phony "cache" entry; we are not
880 * going to evict it nor find it through _get()
881 * mechanism -- this is for the toplevel node that
882 * would almost always change with any commit.
884 struct pbase_tree_cache pcache;
887 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
889 struct pbase_tree_cache *ent, *nent;
892 enum object_type type;
894 int my_ix = pbase_tree_cache_ix(sha1);
895 int available_ix = -1;
897 /* pbase-tree-cache acts as a limited hashtable.
898 * your object will be found at your index or within a few
899 * slots after that slot if it is cached.
901 for (neigh = 0; neigh < 8; neigh++) {
902 ent = pbase_tree_cache[my_ix];
903 if (ent && !hashcmp(ent->sha1, sha1)) {
907 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
908 ((0 <= available_ix) &&
909 (!ent && pbase_tree_cache[available_ix])))
910 available_ix = my_ix;
913 my_ix = pbase_tree_cache_ix_incr(my_ix);
916 /* Did not find one. Either we got a bogus request or
917 * we need to read and perhaps cache.
919 data = read_sha1_file(sha1, &type, &size);
922 if (type != OBJ_TREE) {
927 /* We need to either cache or return a throwaway copy */
929 if (available_ix < 0)
932 ent = pbase_tree_cache[available_ix];
933 my_ix = available_ix;
937 nent = xmalloc(sizeof(*nent));
938 nent->temporary = (available_ix < 0);
941 /* evict and reuse */
942 free(ent->tree_data);
945 hashcpy(nent->sha1, sha1);
946 nent->tree_data = data;
947 nent->tree_size = size;
949 if (!nent->temporary)
950 pbase_tree_cache[my_ix] = nent;
954 static void pbase_tree_put(struct pbase_tree_cache *cache)
956 if (!cache->temporary) {
960 free(cache->tree_data);
964 static int name_cmp_len(const char *name)
967 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
972 static void add_pbase_object(struct tree_desc *tree,
975 const char *fullname)
977 struct name_entry entry;
980 while (tree_entry(tree,&entry)) {
981 if (S_ISGITLINK(entry.mode))
983 cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
984 memcmp(name, entry.path, cmplen);
989 if (name[cmplen] != '/') {
990 add_object_entry(entry.sha1,
991 S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB,
995 if (S_ISDIR(entry.mode)) {
996 struct tree_desc sub;
997 struct pbase_tree_cache *tree;
998 const char *down = name+cmplen+1;
999 int downlen = name_cmp_len(down);
1001 tree = pbase_tree_get(entry.sha1);
1004 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1006 add_pbase_object(&sub, down, downlen, fullname);
1007 pbase_tree_put(tree);
1012 static unsigned *done_pbase_paths;
1013 static int done_pbase_paths_num;
1014 static int done_pbase_paths_alloc;
1015 static int done_pbase_path_pos(unsigned hash)
1018 int hi = done_pbase_paths_num;
1020 int mi = (hi + lo) / 2;
1021 if (done_pbase_paths[mi] == hash)
1023 if (done_pbase_paths[mi] < hash)
1031 static int check_pbase_path(unsigned hash)
1033 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
1037 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
1038 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
1039 done_pbase_paths = xrealloc(done_pbase_paths,
1040 done_pbase_paths_alloc *
1043 done_pbase_paths_num++;
1044 if (pos < done_pbase_paths_num)
1045 memmove(done_pbase_paths + pos + 1,
1046 done_pbase_paths + pos,
1047 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1048 done_pbase_paths[pos] = hash;
1052 static void add_preferred_base_object(const char *name)
1054 struct pbase_tree *it;
1056 unsigned hash = name_hash(name);
1058 if (!num_preferred_base || check_pbase_path(hash))
1061 cmplen = name_cmp_len(name);
1062 for (it = pbase_tree; it; it = it->next) {
1064 add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1);
1067 struct tree_desc tree;
1068 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1069 add_pbase_object(&tree, name, cmplen, name);
1074 static void add_preferred_base(unsigned char *sha1)
1076 struct pbase_tree *it;
1079 unsigned char tree_sha1[20];
1081 if (window <= num_preferred_base++)
1084 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1088 for (it = pbase_tree; it; it = it->next) {
1089 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
1095 it = xcalloc(1, sizeof(*it));
1096 it->next = pbase_tree;
1099 hashcpy(it->pcache.sha1, tree_sha1);
1100 it->pcache.tree_data = data;
1101 it->pcache.tree_size = size;
1104 static void check_object(struct object_entry *entry)
1106 if (entry->in_pack) {
1107 struct packed_git *p = entry->in_pack;
1108 struct pack_window *w_curs = NULL;
1109 const unsigned char *base_ref = NULL;
1110 struct object_entry *base_entry;
1111 unsigned long used, used_0;
1114 unsigned char *buf, c;
1116 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1119 * We want in_pack_type even if we do not reuse delta
1120 * since non-delta representations could still be reused.
1122 used = unpack_object_header_gently(buf, avail,
1123 &entry->in_pack_type,
1127 * Determine if this is a delta and if so whether we can
1128 * reuse it or not. Otherwise let's find out as cheaply as
1129 * possible what the actual type and size for this object is.
1131 switch (entry->in_pack_type) {
1133 /* Not a delta hence we've already got all we need. */
1134 entry->type = entry->in_pack_type;
1135 entry->in_pack_header_size = used;
1136 unuse_pack(&w_curs);
1139 if (!no_reuse_delta && !entry->preferred_base)
1140 base_ref = use_pack(p, &w_curs,
1141 entry->in_pack_offset + used, NULL);
1142 entry->in_pack_header_size = used + 20;
1145 buf = use_pack(p, &w_curs,
1146 entry->in_pack_offset + used, NULL);
1152 if (!ofs || MSB(ofs, 7))
1153 die("delta base offset overflow in pack for %s",
1154 sha1_to_hex(entry->idx.sha1));
1156 ofs = (ofs << 7) + (c & 127);
1158 if (ofs >= entry->in_pack_offset)
1159 die("delta base offset out of bound for %s",
1160 sha1_to_hex(entry->idx.sha1));
1161 ofs = entry->in_pack_offset - ofs;
1162 if (!no_reuse_delta && !entry->preferred_base)
1163 base_ref = find_packed_object_name(p, ofs);
1164 entry->in_pack_header_size = used + used_0;
1168 if (base_ref && (base_entry = locate_object_entry(base_ref))) {
1170 * If base_ref was set above that means we wish to
1171 * reuse delta data, and we even found that base
1172 * in the list of objects we want to pack. Goodie!
1174 * Depth value does not matter - find_deltas() will
1175 * never consider reused delta as the base object to
1176 * deltify other objects against, in order to avoid
1179 entry->type = entry->in_pack_type;
1180 entry->delta = base_entry;
1181 entry->delta_sibling = base_entry->delta_child;
1182 base_entry->delta_child = entry;
1183 unuse_pack(&w_curs);
1189 * This must be a delta and we already know what the
1190 * final object type is. Let's extract the actual
1191 * object size from the delta header.
1193 entry->size = get_size_from_delta(p, &w_curs,
1194 entry->in_pack_offset + entry->in_pack_header_size);
1195 unuse_pack(&w_curs);
1200 * No choice but to fall back to the recursive delta walk
1201 * with sha1_object_info() to find about the object type
1204 unuse_pack(&w_curs);
1207 entry->type = sha1_object_info(entry->idx.sha1, &entry->size);
1208 if (entry->type < 0)
1209 die("unable to get type of object %s",
1210 sha1_to_hex(entry->idx.sha1));
1213 static int pack_offset_sort(const void *_a, const void *_b)
1215 const struct object_entry *a = *(struct object_entry **)_a;
1216 const struct object_entry *b = *(struct object_entry **)_b;
1218 /* avoid filesystem trashing with loose objects */
1219 if (!a->in_pack && !b->in_pack)
1220 return hashcmp(a->idx.sha1, b->idx.sha1);
1222 if (a->in_pack < b->in_pack)
1224 if (a->in_pack > b->in_pack)
1226 return a->in_pack_offset < b->in_pack_offset ? -1 :
1227 (a->in_pack_offset > b->in_pack_offset);
1230 static void get_object_details(void)
1233 struct object_entry **sorted_by_offset;
1235 sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));
1236 for (i = 0; i < nr_objects; i++)
1237 sorted_by_offset[i] = objects + i;
1238 qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
1241 for (i = 0; i < nr_objects; i++)
1242 check_object(sorted_by_offset[i]);
1243 free(sorted_by_offset);
1246 static int type_size_sort(const void *_a, const void *_b)
1248 const struct object_entry *a = *(struct object_entry **)_a;
1249 const struct object_entry *b = *(struct object_entry **)_b;
1251 if (a->type < b->type)
1253 if (a->type > b->type)
1255 if (a->hash < b->hash)
1257 if (a->hash > b->hash)
1259 if (a->preferred_base < b->preferred_base)
1261 if (a->preferred_base > b->preferred_base)
1263 if (a->size < b->size)
1265 if (a->size > b->size)
1267 return a > b ? -1 : (a < b); /* newest last */
1271 struct object_entry *entry;
1273 struct delta_index *index;
1277 static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1278 unsigned long delta_size)
1280 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
1283 if (delta_size < cache_max_small_delta_size)
1286 /* cache delta, if objects are large enough compared to delta size */
1287 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
1294 * We search for deltas _backwards_ in a list sorted by type and
1295 * by size, so that we see progressively smaller and smaller files.
1296 * That's because we prefer deltas to be from the bigger file
1297 * to the smaller - deletes are potentially cheaper, but perhaps
1298 * more importantly, the bigger file is likely the more recent
1301 static int try_delta(struct unpacked *trg, struct unpacked *src,
1302 unsigned max_depth, unsigned long *mem_usage)
1304 struct object_entry *trg_entry = trg->entry;
1305 struct object_entry *src_entry = src->entry;
1306 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1308 enum object_type type;
1311 /* Don't bother doing diffs between different types */
1312 if (trg_entry->type != src_entry->type)
1316 * We do not bother to try a delta that we discarded
1317 * on an earlier try, but only when reusing delta data.
1319 if (!no_reuse_delta && trg_entry->in_pack &&
1320 trg_entry->in_pack == src_entry->in_pack &&
1321 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1322 trg_entry->in_pack_type != OBJ_OFS_DELTA)
1325 /* Let's not bust the allowed depth. */
1326 if (src->depth >= max_depth)
1329 /* Now some size filtering heuristics. */
1330 trg_size = trg_entry->size;
1331 if (!trg_entry->delta) {
1332 max_size = trg_size/2 - 20;
1335 max_size = trg_entry->delta_size;
1336 ref_depth = trg->depth;
1338 max_size = max_size * (max_depth - src->depth) /
1339 (max_depth - ref_depth + 1);
1342 src_size = src_entry->size;
1343 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1344 if (sizediff >= max_size)
1346 if (trg_size < src_size / 32)
1349 /* Load data if not already done */
1351 trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz);
1353 die("object %s cannot be read",
1354 sha1_to_hex(trg_entry->idx.sha1));
1356 die("object %s inconsistent object length (%lu vs %lu)",
1357 sha1_to_hex(trg_entry->idx.sha1), sz, trg_size);
1361 src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz);
1363 die("object %s cannot be read",
1364 sha1_to_hex(src_entry->idx.sha1));
1366 die("object %s inconsistent object length (%lu vs %lu)",
1367 sha1_to_hex(src_entry->idx.sha1), sz, src_size);
1371 src->index = create_delta_index(src->data, src_size);
1373 static int warned = 0;
1375 warning("suboptimal pack - out of memory");
1378 *mem_usage += sizeof_delta_index(src->index);
1381 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1385 if (trg_entry->delta) {
1386 /* Prefer only shallower same-sized deltas. */
1387 if (delta_size == trg_entry->delta_size &&
1388 src->depth + 1 >= trg->depth) {
1394 trg_entry->delta = src_entry;
1395 trg_entry->delta_size = delta_size;
1396 trg->depth = src->depth + 1;
1398 if (trg_entry->delta_data) {
1399 delta_cache_size -= trg_entry->delta_size;
1400 free(trg_entry->delta_data);
1401 trg_entry->delta_data = NULL;
1404 if (delta_cacheable(src_size, trg_size, delta_size)) {
1405 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1406 delta_cache_size += trg_entry->delta_size;
1412 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1414 struct object_entry *child = me->delta_child;
1417 unsigned int c = check_delta_limit(child, n + 1);
1420 child = child->delta_sibling;
1425 static unsigned long free_unpacked(struct unpacked *n)
1427 unsigned long freed_mem = sizeof_delta_index(n->index);
1428 free_delta_index(n->index);
1431 freed_mem += n->entry->size;
1440 static void find_deltas(struct object_entry **list, unsigned list_size,
1441 unsigned nr_deltas, int window, int depth)
1443 uint32_t i = list_size, idx = 0, count = 0, processed = 0;
1444 unsigned int array_size = window * sizeof(struct unpacked);
1445 struct unpacked *array;
1446 unsigned long mem_usage = 0;
1448 array = xmalloc(array_size);
1449 memset(array, 0, array_size);
1451 start_progress(&progress_state, "Deltifying %u objects...", "", nr_deltas);
1454 struct object_entry *entry = list[--i];
1455 struct unpacked *n = array + idx;
1456 int j, max_depth, best_base = -1;
1458 mem_usage -= free_unpacked(n);
1461 while (window_memory_limit &&
1462 mem_usage > window_memory_limit &&
1464 uint32_t tail = (idx + window - count) % window;
1465 mem_usage -= free_unpacked(array + tail);
1469 /* We do not compute delta to *create* objects we are not
1472 if (entry->preferred_base)
1476 display_progress(&progress_state, ++processed);
1479 * If the current object is at pack edge, take the depth the
1480 * objects that depend on the current object into account
1481 * otherwise they would become too deep.
1484 if (entry->delta_child) {
1485 max_depth -= check_delta_limit(entry, 0);
1493 uint32_t other_idx = idx + j;
1495 if (other_idx >= window)
1496 other_idx -= window;
1497 m = array + other_idx;
1500 ret = try_delta(n, m, max_depth, &mem_usage);
1504 best_base = other_idx;
1507 /* if we made n a delta, and if n is already at max
1508 * depth, leaving it in the window is pointless. we
1509 * should evict it first.
1511 if (entry->delta && depth <= n->depth)
1515 * Move the best delta base up in the window, after the
1516 * currently deltified object, to keep it longer. It will
1517 * be the first base object to be attempted next.
1520 struct unpacked swap = array[best_base];
1521 int dist = (window + idx - best_base) % window;
1522 int dst = best_base;
1524 int src = (dst + 1) % window;
1525 array[dst] = array[src];
1533 if (count + 1 < window)
1540 stop_progress(&progress_state);
1542 for (i = 0; i < window; ++i) {
1543 free_delta_index(array[i].index);
1544 free(array[i].data);
1549 static void prepare_pack(int window, int depth)
1551 struct object_entry **delta_list;
1552 uint32_t i, n, nr_deltas;
1554 get_object_details();
1556 if (!nr_objects || !window || !depth)
1559 delta_list = xmalloc(nr_objects * sizeof(*delta_list));
1562 for (i = 0; i < nr_objects; i++) {
1563 struct object_entry *entry = objects + i;
1566 /* This happens if we decided to reuse existing
1567 * delta from a pack. "!no_reuse_delta &&" is implied.
1571 if (entry->size < 50)
1574 if (entry->no_try_delta)
1577 if (!entry->preferred_base)
1580 delta_list[n++] = entry;
1584 qsort(delta_list, n, sizeof(*delta_list), type_size_sort);
1585 find_deltas(delta_list, n, nr_deltas, window+1, depth);
1590 static int git_pack_config(const char *k, const char *v)
1592 if(!strcmp(k, "pack.window")) {
1593 window = git_config_int(k, v);
1596 if (!strcmp(k, "pack.windowmemory")) {
1597 window_memory_limit = git_config_ulong(k, v);
1600 if (!strcmp(k, "pack.depth")) {
1601 depth = git_config_int(k, v);
1604 if (!strcmp(k, "pack.compression")) {
1605 int level = git_config_int(k, v);
1607 level = Z_DEFAULT_COMPRESSION;
1608 else if (level < 0 || level > Z_BEST_COMPRESSION)
1609 die("bad pack compression level %d", level);
1610 pack_compression_level = level;
1611 pack_compression_seen = 1;
1614 if (!strcmp(k, "pack.deltacachesize")) {
1615 max_delta_cache_size = git_config_int(k, v);
1618 if (!strcmp(k, "pack.deltacachelimit")) {
1619 cache_max_small_delta_size = git_config_int(k, v);
1622 return git_default_config(k, v);
1625 static void read_object_list_from_stdin(void)
1627 char line[40 + 1 + PATH_MAX + 2];
1628 unsigned char sha1[20];
1631 if (!fgets(line, sizeof(line), stdin)) {
1635 die("fgets returned NULL, not EOF, not error!");
1637 die("fgets: %s", strerror(errno));
1641 if (line[0] == '-') {
1642 if (get_sha1_hex(line+1, sha1))
1643 die("expected edge sha1, got garbage:\n %s",
1645 add_preferred_base(sha1);
1648 if (get_sha1_hex(line, sha1))
1649 die("expected sha1, got garbage:\n %s", line);
1651 add_preferred_base_object(line+41);
1652 add_object_entry(sha1, 0, line+41, 0);
1656 static void show_commit(struct commit *commit)
1658 add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0);
1661 static void show_object(struct object_array_entry *p)
1663 add_preferred_base_object(p->name);
1664 add_object_entry(p->item->sha1, p->item->type, p->name, 0);
1667 static void show_edge(struct commit *commit)
1669 add_preferred_base(commit->object.sha1);
1672 static void get_object_list(int ac, const char **av)
1674 struct rev_info revs;
1678 init_revisions(&revs, NULL);
1679 save_commit_buffer = 0;
1680 track_object_refs = 0;
1681 setup_revisions(ac, av, &revs, NULL);
1683 while (fgets(line, sizeof(line), stdin) != NULL) {
1684 int len = strlen(line);
1685 if (line[len - 1] == '\n')
1690 if (!strcmp(line, "--not")) {
1691 flags ^= UNINTERESTING;
1694 die("not a rev '%s'", line);
1696 if (handle_revision_arg(line, &revs, flags, 1))
1697 die("bad revision '%s'", line);
1700 prepare_revision_walk(&revs);
1701 mark_edges_uninteresting(revs.commits, &revs, show_edge);
1702 traverse_commit_list(&revs, show_commit, show_object);
1705 static int adjust_perm(const char *path, mode_t mode)
1707 if (chmod(path, mode))
1709 return adjust_shared_perm(path);
1712 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1714 int use_internal_rev_list = 0;
1718 int rp_ac_alloc = 64;
1721 rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
1723 rp_av[0] = "pack-objects";
1724 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1727 git_config(git_pack_config);
1728 if (!pack_compression_seen && core_compression_seen)
1729 pack_compression_level = core_compression_level;
1731 progress = isatty(2);
1732 for (i = 1; i < argc; i++) {
1733 const char *arg = argv[i];
1738 if (!strcmp("--non-empty", arg)) {
1742 if (!strcmp("--local", arg)) {
1746 if (!strcmp("--incremental", arg)) {
1750 if (!prefixcmp(arg, "--compression=")) {
1752 int level = strtoul(arg+14, &end, 0);
1753 if (!arg[14] || *end)
1756 level = Z_DEFAULT_COMPRESSION;
1757 else if (level < 0 || level > Z_BEST_COMPRESSION)
1758 die("bad pack compression level %d", level);
1759 pack_compression_level = level;
1762 if (!prefixcmp(arg, "--max-pack-size=")) {
1764 pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024;
1765 if (!arg[16] || *end)
1769 if (!prefixcmp(arg, "--window=")) {
1771 window = strtoul(arg+9, &end, 0);
1772 if (!arg[9] || *end)
1776 if (!prefixcmp(arg, "--window-memory=")) {
1777 if (!git_parse_ulong(arg+16, &window_memory_limit))
1781 if (!prefixcmp(arg, "--depth=")) {
1783 depth = strtoul(arg+8, &end, 0);
1784 if (!arg[8] || *end)
1788 if (!strcmp("--progress", arg)) {
1792 if (!strcmp("--all-progress", arg)) {
1796 if (!strcmp("-q", arg)) {
1800 if (!strcmp("--no-reuse-delta", arg)) {
1804 if (!strcmp("--no-reuse-object", arg)) {
1805 no_reuse_object = no_reuse_delta = 1;
1808 if (!strcmp("--delta-base-offset", arg)) {
1809 allow_ofs_delta = 1;
1812 if (!strcmp("--stdout", arg)) {
1816 if (!strcmp("--revs", arg)) {
1817 use_internal_rev_list = 1;
1820 if (!strcmp("--unpacked", arg) ||
1821 !prefixcmp(arg, "--unpacked=") ||
1822 !strcmp("--reflog", arg) ||
1823 !strcmp("--all", arg)) {
1824 use_internal_rev_list = 1;
1825 if (rp_ac >= rp_ac_alloc - 1) {
1826 rp_ac_alloc = alloc_nr(rp_ac_alloc);
1827 rp_av = xrealloc(rp_av,
1828 rp_ac_alloc * sizeof(*rp_av));
1830 rp_av[rp_ac++] = arg;
1833 if (!strcmp("--thin", arg)) {
1834 use_internal_rev_list = 1;
1836 rp_av[1] = "--objects-edge";
1839 if (!prefixcmp(arg, "--index-version=")) {
1841 pack_idx_default_version = strtoul(arg + 16, &c, 10);
1842 if (pack_idx_default_version > 2)
1845 pack_idx_off32_limit = strtoul(c+1, &c, 0);
1846 if (*c || pack_idx_off32_limit & 0x80000000)
1853 /* Traditionally "pack-objects [options] base extra" failed;
1854 * we would however want to take refs parameter that would
1855 * have been given to upstream rev-list ourselves, which means
1856 * we somehow want to say what the base name is. So the
1859 * pack-objects [options] base <refs...>
1861 * in other words, we would treat the first non-option as the
1862 * base_name and send everything else to the internal revision
1866 if (!pack_to_stdout)
1867 base_name = argv[i++];
1869 if (pack_to_stdout != !base_name)
1872 if (pack_to_stdout && pack_size_limit)
1873 die("--max-pack-size cannot be used to build a pack for transfer.");
1875 if (!pack_to_stdout && thin)
1876 die("--thin cannot be used to build an indexable pack.");
1878 prepare_packed_git();
1881 start_progress(&progress_state, "Generating pack...",
1882 "Counting objects: ", 0);
1883 if (!use_internal_rev_list)
1884 read_object_list_from_stdin();
1886 rp_av[rp_ac] = NULL;
1887 get_object_list(rp_ac, rp_av);
1890 stop_progress(&progress_state);
1891 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1894 if (non_empty && !nr_result)
1896 if (progress && (nr_objects != nr_result))
1897 fprintf(stderr, "Result has %u objects.\n", nr_result);
1899 prepare_pack(window, depth);
1902 fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
1903 written, written_delta, reused, reused_delta);