3 #include "repository.h"
13 #include "pack-revindex.h"
14 #include "csum-file.h"
15 #include "tree-walk.h"
18 #include "list-objects.h"
19 #include "list-objects-filter.h"
20 #include "list-objects-filter-options.h"
21 #include "pack-objects.h"
24 #include "streaming.h"
25 #include "thread-utils.h"
26 #include "pack-bitmap.h"
27 #include "reachable.h"
28 #include "sha1-array.h"
29 #include "argv-array.h"
32 #include "object-store.h"
35 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
36 #define SIZE(obj) oe_size(&to_pack, obj)
37 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
38 #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
39 #define DELTA(obj) oe_delta(&to_pack, obj)
40 #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
41 #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
42 #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
43 #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
44 #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
45 #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
47 static const char *pack_usage[] = {
48 N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
49 N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"),
54 * Objects we are going to pack are collected in the `to_pack` structure.
55 * It contains an array (dynamically expanded) of the object data, and a map
56 * that can resolve SHA1s to their position in the array.
58 static struct packing_data to_pack;
60 static struct pack_idx_entry **written_list;
61 static uint32_t nr_result, nr_written, nr_seen;
64 static int reuse_delta = 1, reuse_object = 1;
65 static int keep_unreachable, unpack_unreachable, include_tag;
66 static timestamp_t unpack_unreachable_expiration;
67 static int pack_loose_unreachable;
69 static int have_non_local_packs;
70 static int incremental;
71 static int ignore_packed_keep_on_disk;
72 static int ignore_packed_keep_in_core;
73 static int allow_ofs_delta;
74 static struct pack_idx_option pack_idx_opts;
75 static const char *base_name;
76 static int progress = 1;
77 static int window = 10;
78 static unsigned long pack_size_limit;
79 static int depth = 50;
80 static int delta_search_threads;
81 static int pack_to_stdout;
82 static int num_preferred_base;
83 static struct progress *progress_state;
85 static struct packed_git *reuse_packfile;
86 static uint32_t reuse_packfile_objects;
87 static off_t reuse_packfile_offset;
89 static int use_bitmap_index_default = 1;
90 static int use_bitmap_index = -1;
91 static int write_bitmap_index;
92 static uint16_t write_bitmap_options;
94 static int exclude_promisor_objects;
96 static unsigned long delta_cache_size = 0;
97 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
98 static unsigned long cache_max_small_delta_size = 1000;
100 static unsigned long window_memory_limit = 0;
102 static struct list_objects_filter_options filter_options;
104 enum missing_action {
105 MA_ERROR = 0, /* fail if any missing objects are encountered */
106 MA_ALLOW_ANY, /* silently allow ALL missing objects */
107 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
109 static enum missing_action arg_missing_action;
110 static show_object_fn fn_show_object;
115 static uint32_t written, written_delta;
116 static uint32_t reused, reused_delta;
121 static struct commit **indexed_commits;
122 static unsigned int indexed_commits_nr;
123 static unsigned int indexed_commits_alloc;
125 static void index_commit_for_bitmap(struct commit *commit)
127 if (indexed_commits_nr >= indexed_commits_alloc) {
128 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
129 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
132 indexed_commits[indexed_commits_nr++] = commit;
135 static void *get_delta(struct object_entry *entry)
137 unsigned long size, base_size, delta_size;
138 void *buf, *base_buf, *delta_buf;
139 enum object_type type;
141 buf = read_object_file(&entry->idx.oid, &type, &size);
143 die("unable to read %s", oid_to_hex(&entry->idx.oid));
144 base_buf = read_object_file(&DELTA(entry)->idx.oid, &type,
147 die("unable to read %s",
148 oid_to_hex(&DELTA(entry)->idx.oid));
149 delta_buf = diff_delta(base_buf, base_size,
150 buf, size, &delta_size, 0);
151 if (!delta_buf || delta_size != DELTA_SIZE(entry))
152 die("delta size changed");
158 static unsigned long do_compress(void **pptr, unsigned long size)
162 unsigned long maxsize;
164 git_deflate_init(&stream, pack_compression_level);
165 maxsize = git_deflate_bound(&stream, size);
168 out = xmalloc(maxsize);
172 stream.avail_in = size;
173 stream.next_out = out;
174 stream.avail_out = maxsize;
175 while (git_deflate(&stream, Z_FINISH) == Z_OK)
177 git_deflate_end(&stream);
180 return stream.total_out;
183 static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f,
184 const struct object_id *oid)
187 unsigned char ibuf[1024 * 16];
188 unsigned char obuf[1024 * 16];
189 unsigned long olen = 0;
191 git_deflate_init(&stream, pack_compression_level);
196 readlen = read_istream(st, ibuf, sizeof(ibuf));
198 die(_("unable to read %s"), oid_to_hex(oid));
200 stream.next_in = ibuf;
201 stream.avail_in = readlen;
202 while ((stream.avail_in || readlen == 0) &&
203 (zret == Z_OK || zret == Z_BUF_ERROR)) {
204 stream.next_out = obuf;
205 stream.avail_out = sizeof(obuf);
206 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
207 hashwrite(f, obuf, stream.next_out - obuf);
208 olen += stream.next_out - obuf;
211 die(_("deflate error (%d)"), zret);
213 if (zret != Z_STREAM_END)
214 die(_("deflate error (%d)"), zret);
218 git_deflate_end(&stream);
223 * we are going to reuse the existing object data as is. make
224 * sure it is not corrupt.
226 static int check_pack_inflate(struct packed_git *p,
227 struct pack_window **w_curs,
230 unsigned long expect)
233 unsigned char fakebuf[4096], *in;
236 memset(&stream, 0, sizeof(stream));
237 git_inflate_init(&stream);
239 in = use_pack(p, w_curs, offset, &stream.avail_in);
241 stream.next_out = fakebuf;
242 stream.avail_out = sizeof(fakebuf);
243 st = git_inflate(&stream, Z_FINISH);
244 offset += stream.next_in - in;
245 } while (st == Z_OK || st == Z_BUF_ERROR);
246 git_inflate_end(&stream);
247 return (st == Z_STREAM_END &&
248 stream.total_out == expect &&
249 stream.total_in == len) ? 0 : -1;
252 static void copy_pack_data(struct hashfile *f,
253 struct packed_git *p,
254 struct pack_window **w_curs,
262 in = use_pack(p, w_curs, offset, &avail);
264 avail = (unsigned long)len;
265 hashwrite(f, in, avail);
271 /* Return 0 if we will bust the pack-size limit */
272 static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
273 unsigned long limit, int usable_delta)
275 unsigned long size, datalen;
276 unsigned char header[MAX_PACK_OBJECT_HEADER],
277 dheader[MAX_PACK_OBJECT_HEADER];
279 enum object_type type;
281 struct git_istream *st = NULL;
284 if (oe_type(entry) == OBJ_BLOB &&
285 oe_size_greater_than(&to_pack, entry, big_file_threshold) &&
286 (st = open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL)
289 buf = read_object_file(&entry->idx.oid, &type, &size);
291 die(_("unable to read %s"),
292 oid_to_hex(&entry->idx.oid));
295 * make sure no cached delta data remains from a
296 * previous attempt before a pack split occurred.
298 FREE_AND_NULL(entry->delta_data);
299 entry->z_delta_size = 0;
300 } else if (entry->delta_data) {
301 size = DELTA_SIZE(entry);
302 buf = entry->delta_data;
303 entry->delta_data = NULL;
304 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
305 OBJ_OFS_DELTA : OBJ_REF_DELTA;
307 buf = get_delta(entry);
308 size = DELTA_SIZE(entry);
309 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
310 OBJ_OFS_DELTA : OBJ_REF_DELTA;
313 if (st) /* large blob case, just assume we don't compress well */
315 else if (entry->z_delta_size)
316 datalen = entry->z_delta_size;
318 datalen = do_compress(&buf, size);
321 * The object header is a byte of 'type' followed by zero or
322 * more bytes of length.
324 hdrlen = encode_in_pack_object_header(header, sizeof(header),
327 if (type == OBJ_OFS_DELTA) {
329 * Deltas with relative base contain an additional
330 * encoding of the relative offset for the delta
331 * base from this object's position in the pack.
333 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
334 unsigned pos = sizeof(dheader) - 1;
335 dheader[pos] = ofs & 127;
337 dheader[--pos] = 128 | (--ofs & 127);
338 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
344 hashwrite(f, header, hdrlen);
345 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
346 hdrlen += sizeof(dheader) - pos;
347 } else if (type == OBJ_REF_DELTA) {
349 * Deltas with a base reference contain
350 * an additional 20 bytes for the base sha1.
352 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
358 hashwrite(f, header, hdrlen);
359 hashwrite(f, DELTA(entry)->idx.oid.hash, 20);
362 if (limit && hdrlen + datalen + 20 >= limit) {
368 hashwrite(f, header, hdrlen);
371 datalen = write_large_blob_data(st, f, &entry->idx.oid);
374 hashwrite(f, buf, datalen);
378 return hdrlen + datalen;
381 /* Return 0 if we will bust the pack-size limit */
382 static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
383 unsigned long limit, int usable_delta)
385 struct packed_git *p = IN_PACK(entry);
386 struct pack_window *w_curs = NULL;
387 struct revindex_entry *revidx;
389 enum object_type type = oe_type(entry);
391 unsigned char header[MAX_PACK_OBJECT_HEADER],
392 dheader[MAX_PACK_OBJECT_HEADER];
394 unsigned long entry_size = SIZE(entry);
397 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
398 OBJ_OFS_DELTA : OBJ_REF_DELTA;
399 hdrlen = encode_in_pack_object_header(header, sizeof(header),
402 offset = entry->in_pack_offset;
403 revidx = find_pack_revindex(p, offset);
404 datalen = revidx[1].offset - offset;
405 if (!pack_to_stdout && p->index_version > 1 &&
406 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) {
407 error("bad packed object CRC for %s",
408 oid_to_hex(&entry->idx.oid));
410 return write_no_reuse_object(f, entry, limit, usable_delta);
413 offset += entry->in_pack_header_size;
414 datalen -= entry->in_pack_header_size;
416 if (!pack_to_stdout && p->index_version == 1 &&
417 check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) {
418 error("corrupt packed object for %s",
419 oid_to_hex(&entry->idx.oid));
421 return write_no_reuse_object(f, entry, limit, usable_delta);
424 if (type == OBJ_OFS_DELTA) {
425 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
426 unsigned pos = sizeof(dheader) - 1;
427 dheader[pos] = ofs & 127;
429 dheader[--pos] = 128 | (--ofs & 127);
430 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
434 hashwrite(f, header, hdrlen);
435 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
436 hdrlen += sizeof(dheader) - pos;
438 } else if (type == OBJ_REF_DELTA) {
439 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
443 hashwrite(f, header, hdrlen);
444 hashwrite(f, DELTA(entry)->idx.oid.hash, 20);
448 if (limit && hdrlen + datalen + 20 >= limit) {
452 hashwrite(f, header, hdrlen);
454 copy_pack_data(f, p, &w_curs, offset, datalen);
457 return hdrlen + datalen;
460 /* Return 0 if we will bust the pack-size limit */
461 static off_t write_object(struct hashfile *f,
462 struct object_entry *entry,
467 int usable_delta, to_reuse;
472 /* apply size limit if limited packsize and not first object */
473 if (!pack_size_limit || !nr_written)
475 else if (pack_size_limit <= write_offset)
477 * the earlier object did not fit the limit; avoid
478 * mistaking this with unlimited (i.e. limit = 0).
482 limit = pack_size_limit - write_offset;
485 usable_delta = 0; /* no delta */
486 else if (!pack_size_limit)
487 usable_delta = 1; /* unlimited packfile */
488 else if (DELTA(entry)->idx.offset == (off_t)-1)
489 usable_delta = 0; /* base was written to another pack */
490 else if (DELTA(entry)->idx.offset)
491 usable_delta = 1; /* base already exists in this pack */
493 usable_delta = 0; /* base could end up in another pack */
496 to_reuse = 0; /* explicit */
497 else if (!IN_PACK(entry))
498 to_reuse = 0; /* can't reuse what we don't have */
499 else if (oe_type(entry) == OBJ_REF_DELTA ||
500 oe_type(entry) == OBJ_OFS_DELTA)
501 /* check_object() decided it for us ... */
502 to_reuse = usable_delta;
503 /* ... but pack split may override that */
504 else if (oe_type(entry) != entry->in_pack_type)
505 to_reuse = 0; /* pack has delta which is unusable */
506 else if (DELTA(entry))
507 to_reuse = 0; /* we want to pack afresh */
509 to_reuse = 1; /* we have it in-pack undeltified,
510 * and we do not need to deltify it.
514 len = write_no_reuse_object(f, entry, limit, usable_delta);
516 len = write_reuse_object(f, entry, limit, usable_delta);
524 entry->idx.crc32 = crc32_end(f);
528 enum write_one_status {
529 WRITE_ONE_SKIP = -1, /* already written */
530 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
531 WRITE_ONE_WRITTEN = 1, /* normal */
532 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
535 static enum write_one_status write_one(struct hashfile *f,
536 struct object_entry *e,
543 * we set offset to 1 (which is an impossible value) to mark
544 * the fact that this object is involved in "write its base
545 * first before writing a deltified object" recursion.
547 recursing = (e->idx.offset == 1);
549 warning("recursive delta detected for object %s",
550 oid_to_hex(&e->idx.oid));
551 return WRITE_ONE_RECURSIVE;
552 } else if (e->idx.offset || e->preferred_base) {
553 /* offset is non zero if object is written already. */
554 return WRITE_ONE_SKIP;
557 /* if we are deltified, write out base object first. */
559 e->idx.offset = 1; /* now recurse */
560 switch (write_one(f, DELTA(e), offset)) {
561 case WRITE_ONE_RECURSIVE:
562 /* we cannot depend on this one */
567 case WRITE_ONE_BREAK:
568 e->idx.offset = recursing;
569 return WRITE_ONE_BREAK;
573 e->idx.offset = *offset;
574 size = write_object(f, e, *offset);
576 e->idx.offset = recursing;
577 return WRITE_ONE_BREAK;
579 written_list[nr_written++] = &e->idx;
581 /* make sure off_t is sufficiently large not to wrap */
582 if (signed_add_overflows(*offset, size))
583 die("pack too large for current definition of off_t");
585 return WRITE_ONE_WRITTEN;
588 static int mark_tagged(const char *path, const struct object_id *oid, int flag,
591 struct object_id peeled;
592 struct object_entry *entry = packlist_find(&to_pack, oid->hash, NULL);
596 if (!peel_ref(path, &peeled)) {
597 entry = packlist_find(&to_pack, peeled.hash, NULL);
604 static inline void add_to_write_order(struct object_entry **wo,
606 struct object_entry *e)
614 static void add_descendants_to_write_order(struct object_entry **wo,
616 struct object_entry *e)
618 int add_to_order = 1;
621 struct object_entry *s;
622 /* add this node... */
623 add_to_write_order(wo, endp, e);
624 /* all its siblings... */
625 for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
626 add_to_write_order(wo, endp, s);
629 /* drop down a level to add left subtree nodes if possible */
630 if (DELTA_CHILD(e)) {
635 /* our sibling might have some children, it is next */
636 if (DELTA_SIBLING(e)) {
637 e = DELTA_SIBLING(e);
640 /* go back to our parent node */
642 while (e && !DELTA_SIBLING(e)) {
643 /* we're on the right side of a subtree, keep
644 * going up until we can go right again */
648 /* done- we hit our original root node */
651 /* pass it off to sibling at this level */
652 e = DELTA_SIBLING(e);
657 static void add_family_to_write_order(struct object_entry **wo,
659 struct object_entry *e)
661 struct object_entry *root;
663 for (root = e; DELTA(root); root = DELTA(root))
665 add_descendants_to_write_order(wo, endp, root);
668 static struct object_entry **compute_write_order(void)
670 unsigned int i, wo_end, last_untagged;
672 struct object_entry **wo;
673 struct object_entry *objects = to_pack.objects;
675 for (i = 0; i < to_pack.nr_objects; i++) {
676 objects[i].tagged = 0;
677 objects[i].filled = 0;
678 SET_DELTA_CHILD(&objects[i], NULL);
679 SET_DELTA_SIBLING(&objects[i], NULL);
683 * Fully connect delta_child/delta_sibling network.
684 * Make sure delta_sibling is sorted in the original
687 for (i = to_pack.nr_objects; i > 0;) {
688 struct object_entry *e = &objects[--i];
691 /* Mark me as the first child */
692 e->delta_sibling_idx = DELTA(e)->delta_child_idx;
693 SET_DELTA_CHILD(DELTA(e), e);
697 * Mark objects that are at the tip of tags.
699 for_each_tag_ref(mark_tagged, NULL);
702 * Give the objects in the original recency order until
703 * we see a tagged tip.
705 ALLOC_ARRAY(wo, to_pack.nr_objects);
706 for (i = wo_end = 0; i < to_pack.nr_objects; i++) {
707 if (objects[i].tagged)
709 add_to_write_order(wo, &wo_end, &objects[i]);
714 * Then fill all the tagged tips.
716 for (; i < to_pack.nr_objects; i++) {
717 if (objects[i].tagged)
718 add_to_write_order(wo, &wo_end, &objects[i]);
722 * And then all remaining commits and tags.
724 for (i = last_untagged; i < to_pack.nr_objects; i++) {
725 if (oe_type(&objects[i]) != OBJ_COMMIT &&
726 oe_type(&objects[i]) != OBJ_TAG)
728 add_to_write_order(wo, &wo_end, &objects[i]);
732 * And then all the trees.
734 for (i = last_untagged; i < to_pack.nr_objects; i++) {
735 if (oe_type(&objects[i]) != OBJ_TREE)
737 add_to_write_order(wo, &wo_end, &objects[i]);
741 * Finally all the rest in really tight order
743 for (i = last_untagged; i < to_pack.nr_objects; i++) {
744 if (!objects[i].filled)
745 add_family_to_write_order(wo, &wo_end, &objects[i]);
748 if (wo_end != to_pack.nr_objects)
749 die("ordered %u objects, expected %"PRIu32, wo_end, to_pack.nr_objects);
754 static off_t write_reused_pack(struct hashfile *f)
756 unsigned char buffer[8192];
757 off_t to_write, total;
760 if (!is_pack_valid(reuse_packfile))
761 die("packfile is invalid: %s", reuse_packfile->pack_name);
763 fd = git_open(reuse_packfile->pack_name);
765 die_errno("unable to open packfile for reuse: %s",
766 reuse_packfile->pack_name);
768 if (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1)
769 die_errno("unable to seek in reused packfile");
771 if (reuse_packfile_offset < 0)
772 reuse_packfile_offset = reuse_packfile->pack_size - 20;
774 total = to_write = reuse_packfile_offset - sizeof(struct pack_header);
777 int read_pack = xread(fd, buffer, sizeof(buffer));
780 die_errno("unable to read from reused packfile");
782 if (read_pack > to_write)
783 read_pack = to_write;
785 hashwrite(f, buffer, read_pack);
786 to_write -= read_pack;
789 * We don't know the actual number of objects written,
790 * only how many bytes written, how many bytes total, and
791 * how many objects total. So we can fake it by pretending all
792 * objects we are writing are the same size. This gives us a
793 * smooth progress meter, and at the end it matches the true
796 written = reuse_packfile_objects *
797 (((double)(total - to_write)) / total);
798 display_progress(progress_state, written);
802 written = reuse_packfile_objects;
803 display_progress(progress_state, written);
804 return reuse_packfile_offset - sizeof(struct pack_header);
807 static const char no_split_warning[] = N_(
808 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
811 static void write_pack_file(void)
816 uint32_t nr_remaining = nr_result;
817 time_t last_mtime = 0;
818 struct object_entry **write_order;
820 if (progress > pack_to_stdout)
821 progress_state = start_progress(_("Writing objects"), nr_result);
822 ALLOC_ARRAY(written_list, to_pack.nr_objects);
823 write_order = compute_write_order();
826 struct object_id oid;
827 char *pack_tmp_name = NULL;
830 f = hashfd_throughput(1, "<stdout>", progress_state);
832 f = create_tmp_packfile(&pack_tmp_name);
834 offset = write_pack_header(f, nr_remaining);
836 if (reuse_packfile) {
838 assert(pack_to_stdout);
840 packfile_size = write_reused_pack(f);
841 offset += packfile_size;
845 for (; i < to_pack.nr_objects; i++) {
846 struct object_entry *e = write_order[i];
847 if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
849 display_progress(progress_state, written);
853 * Did we write the wrong # entries in the header?
854 * If so, rewrite it like in fast-import
856 if (pack_to_stdout) {
857 finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE);
858 } else if (nr_written == nr_remaining) {
859 finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
861 int fd = finalize_hashfile(f, oid.hash, 0);
862 fixup_pack_header_footer(fd, oid.hash, pack_tmp_name,
863 nr_written, oid.hash, offset);
865 if (write_bitmap_index) {
866 warning(_(no_split_warning));
867 write_bitmap_index = 0;
871 if (!pack_to_stdout) {
873 struct strbuf tmpname = STRBUF_INIT;
876 * Packs are runtime accessed in their mtime
877 * order since newer packs are more likely to contain
878 * younger objects. So if we are creating multiple
879 * packs then we should modify the mtime of later ones
880 * to preserve this property.
882 if (stat(pack_tmp_name, &st) < 0) {
883 warning_errno("failed to stat %s", pack_tmp_name);
884 } else if (!last_mtime) {
885 last_mtime = st.st_mtime;
888 utb.actime = st.st_atime;
889 utb.modtime = --last_mtime;
890 if (utime(pack_tmp_name, &utb) < 0)
891 warning_errno("failed utime() on %s", pack_tmp_name);
894 strbuf_addf(&tmpname, "%s-", base_name);
896 if (write_bitmap_index) {
897 bitmap_writer_set_checksum(oid.hash);
898 bitmap_writer_build_type_index(
899 &to_pack, written_list, nr_written);
902 finish_tmp_packfile(&tmpname, pack_tmp_name,
903 written_list, nr_written,
904 &pack_idx_opts, oid.hash);
906 if (write_bitmap_index) {
907 strbuf_addf(&tmpname, "%s.bitmap", oid_to_hex(&oid));
909 stop_progress(&progress_state);
911 bitmap_writer_show_progress(progress);
912 bitmap_writer_reuse_bitmaps(&to_pack);
913 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
914 bitmap_writer_build(&to_pack);
915 bitmap_writer_finish(written_list, nr_written,
916 tmpname.buf, write_bitmap_options);
917 write_bitmap_index = 0;
920 strbuf_release(&tmpname);
922 puts(oid_to_hex(&oid));
925 /* mark written objects as written to previous pack */
926 for (j = 0; j < nr_written; j++) {
927 written_list[j]->offset = (off_t)-1;
929 nr_remaining -= nr_written;
930 } while (nr_remaining && i < to_pack.nr_objects);
934 stop_progress(&progress_state);
935 if (written != nr_result)
936 die("wrote %"PRIu32" objects while expecting %"PRIu32,
940 static int no_try_delta(const char *path)
942 static struct attr_check *check;
945 check = attr_check_initl("delta", NULL);
946 if (git_check_attr(path, check))
948 if (ATTR_FALSE(check->items[0].value))
954 * When adding an object, check whether we have already added it
955 * to our packing list. If so, we can skip. However, if we are
956 * being asked to excludei t, but the previous mention was to include
957 * it, make sure to adjust its flags and tweak our numbers accordingly.
959 * As an optimization, we pass out the index position where we would have
960 * found the item, since that saves us from having to look it up again a
961 * few lines later when we want to add the new entry.
963 static int have_duplicate_entry(const struct object_id *oid,
967 struct object_entry *entry;
969 entry = packlist_find(&to_pack, oid->hash, index_pos);
974 if (!entry->preferred_base)
976 entry->preferred_base = 1;
982 static int want_found_object(int exclude, struct packed_git *p)
990 * When asked to do --local (do not include an object that appears in a
991 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
992 * an object that appears in a pack marked with .keep), finding a pack
993 * that matches the criteria is sufficient for us to decide to omit it.
994 * However, even if this pack does not satisfy the criteria, we need to
995 * make sure no copy of this object appears in _any_ pack that makes us
996 * to omit the object, so we need to check all the packs.
998 * We can however first check whether these options can possible matter;
999 * if they do not matter we know we want the object in generated pack.
1000 * Otherwise, we signal "-1" at the end to tell the caller that we do
1001 * not know either way, and it needs to check more packs.
1003 if (!ignore_packed_keep_on_disk &&
1004 !ignore_packed_keep_in_core &&
1005 (!local || !have_non_local_packs))
1008 if (local && !p->pack_local)
1010 if (p->pack_local &&
1011 ((ignore_packed_keep_on_disk && p->pack_keep) ||
1012 (ignore_packed_keep_in_core && p->pack_keep_in_core)))
1015 /* we don't know yet; keep looking for more packs */
1020 * Check whether we want the object in the pack (e.g., we do not want
1021 * objects found in non-local stores if the "--local" option was used).
1023 * If the caller already knows an existing pack it wants to take the object
1024 * from, that is passed in *found_pack and *found_offset; otherwise this
1025 * function finds if there is any pack that has the object and returns the pack
1026 * and its offset in these variables.
1028 static int want_object_in_pack(const struct object_id *oid,
1030 struct packed_git **found_pack,
1031 off_t *found_offset)
1034 struct list_head *pos;
1036 if (!exclude && local && has_loose_object_nonlocal(oid->hash))
1040 * If we already know the pack object lives in, start checks from that
1041 * pack - in the usual case when neither --local was given nor .keep files
1042 * are present we will determine the answer right now.
1045 want = want_found_object(exclude, *found_pack);
1049 list_for_each(pos, get_packed_git_mru(the_repository)) {
1050 struct packed_git *p = list_entry(pos, struct packed_git, mru);
1053 if (p == *found_pack)
1054 offset = *found_offset;
1056 offset = find_pack_entry_one(oid->hash, p);
1060 if (!is_pack_valid(p))
1062 *found_offset = offset;
1065 want = want_found_object(exclude, p);
1066 if (!exclude && want > 0)
1068 get_packed_git_mru(the_repository));
1077 static void create_object_entry(const struct object_id *oid,
1078 enum object_type type,
1083 struct packed_git *found_pack,
1086 struct object_entry *entry;
1088 entry = packlist_alloc(&to_pack, oid->hash, index_pos);
1090 oe_set_type(entry, type);
1092 entry->preferred_base = 1;
1096 oe_set_in_pack(&to_pack, entry, found_pack);
1097 entry->in_pack_offset = found_offset;
1100 entry->no_try_delta = no_try_delta;
1103 static const char no_closure_warning[] = N_(
1104 "disabling bitmap writing, as some objects are not being packed"
1107 static int add_object_entry(const struct object_id *oid, enum object_type type,
1108 const char *name, int exclude)
1110 struct packed_git *found_pack = NULL;
1111 off_t found_offset = 0;
1114 display_progress(progress_state, ++nr_seen);
1116 if (have_duplicate_entry(oid, exclude, &index_pos))
1119 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
1120 /* The pack is missing an object, so it will not have closure */
1121 if (write_bitmap_index) {
1122 warning(_(no_closure_warning));
1123 write_bitmap_index = 0;
1128 create_object_entry(oid, type, pack_name_hash(name),
1129 exclude, name && no_try_delta(name),
1130 index_pos, found_pack, found_offset);
1134 static int add_object_entry_from_bitmap(const struct object_id *oid,
1135 enum object_type type,
1136 int flags, uint32_t name_hash,
1137 struct packed_git *pack, off_t offset)
1141 display_progress(progress_state, ++nr_seen);
1143 if (have_duplicate_entry(oid, 0, &index_pos))
1146 if (!want_object_in_pack(oid, 0, &pack, &offset))
1149 create_object_entry(oid, type, name_hash, 0, 0, index_pos, pack, offset);
1153 struct pbase_tree_cache {
1154 struct object_id oid;
1158 unsigned long tree_size;
1161 static struct pbase_tree_cache *(pbase_tree_cache[256]);
1162 static int pbase_tree_cache_ix(const struct object_id *oid)
1164 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
1166 static int pbase_tree_cache_ix_incr(int ix)
1168 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1171 static struct pbase_tree {
1172 struct pbase_tree *next;
1173 /* This is a phony "cache" entry; we are not
1174 * going to evict it or find it through _get()
1175 * mechanism -- this is for the toplevel node that
1176 * would almost always change with any commit.
1178 struct pbase_tree_cache pcache;
1181 static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
1183 struct pbase_tree_cache *ent, *nent;
1186 enum object_type type;
1188 int my_ix = pbase_tree_cache_ix(oid);
1189 int available_ix = -1;
1191 /* pbase-tree-cache acts as a limited hashtable.
1192 * your object will be found at your index or within a few
1193 * slots after that slot if it is cached.
1195 for (neigh = 0; neigh < 8; neigh++) {
1196 ent = pbase_tree_cache[my_ix];
1197 if (ent && !oidcmp(&ent->oid, oid)) {
1201 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1202 ((0 <= available_ix) &&
1203 (!ent && pbase_tree_cache[available_ix])))
1204 available_ix = my_ix;
1207 my_ix = pbase_tree_cache_ix_incr(my_ix);
1210 /* Did not find one. Either we got a bogus request or
1211 * we need to read and perhaps cache.
1213 data = read_object_file(oid, &type, &size);
1216 if (type != OBJ_TREE) {
1221 /* We need to either cache or return a throwaway copy */
1223 if (available_ix < 0)
1226 ent = pbase_tree_cache[available_ix];
1227 my_ix = available_ix;
1231 nent = xmalloc(sizeof(*nent));
1232 nent->temporary = (available_ix < 0);
1235 /* evict and reuse */
1236 free(ent->tree_data);
1239 oidcpy(&nent->oid, oid);
1240 nent->tree_data = data;
1241 nent->tree_size = size;
1243 if (!nent->temporary)
1244 pbase_tree_cache[my_ix] = nent;
1248 static void pbase_tree_put(struct pbase_tree_cache *cache)
1250 if (!cache->temporary) {
1254 free(cache->tree_data);
1258 static int name_cmp_len(const char *name)
1261 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1266 static void add_pbase_object(struct tree_desc *tree,
1269 const char *fullname)
1271 struct name_entry entry;
1274 while (tree_entry(tree,&entry)) {
1275 if (S_ISGITLINK(entry.mode))
1277 cmp = tree_entry_len(&entry) != cmplen ? 1 :
1278 memcmp(name, entry.path, cmplen);
1283 if (name[cmplen] != '/') {
1284 add_object_entry(entry.oid,
1285 object_type(entry.mode),
1289 if (S_ISDIR(entry.mode)) {
1290 struct tree_desc sub;
1291 struct pbase_tree_cache *tree;
1292 const char *down = name+cmplen+1;
1293 int downlen = name_cmp_len(down);
1295 tree = pbase_tree_get(entry.oid);
1298 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1300 add_pbase_object(&sub, down, downlen, fullname);
1301 pbase_tree_put(tree);
1306 static unsigned *done_pbase_paths;
1307 static int done_pbase_paths_num;
1308 static int done_pbase_paths_alloc;
1309 static int done_pbase_path_pos(unsigned hash)
1312 int hi = done_pbase_paths_num;
1314 int mi = lo + (hi - lo) / 2;
1315 if (done_pbase_paths[mi] == hash)
1317 if (done_pbase_paths[mi] < hash)
1325 static int check_pbase_path(unsigned hash)
1327 int pos = done_pbase_path_pos(hash);
1331 ALLOC_GROW(done_pbase_paths,
1332 done_pbase_paths_num + 1,
1333 done_pbase_paths_alloc);
1334 done_pbase_paths_num++;
1335 if (pos < done_pbase_paths_num)
1336 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
1337 done_pbase_paths_num - pos - 1);
1338 done_pbase_paths[pos] = hash;
1342 static void add_preferred_base_object(const char *name)
1344 struct pbase_tree *it;
1346 unsigned hash = pack_name_hash(name);
1348 if (!num_preferred_base || check_pbase_path(hash))
1351 cmplen = name_cmp_len(name);
1352 for (it = pbase_tree; it; it = it->next) {
1354 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
1357 struct tree_desc tree;
1358 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1359 add_pbase_object(&tree, name, cmplen, name);
1364 static void add_preferred_base(struct object_id *oid)
1366 struct pbase_tree *it;
1369 struct object_id tree_oid;
1371 if (window <= num_preferred_base++)
1374 data = read_object_with_reference(oid, tree_type, &size, &tree_oid);
1378 for (it = pbase_tree; it; it = it->next) {
1379 if (!oidcmp(&it->pcache.oid, &tree_oid)) {
1385 it = xcalloc(1, sizeof(*it));
1386 it->next = pbase_tree;
1389 oidcpy(&it->pcache.oid, &tree_oid);
1390 it->pcache.tree_data = data;
1391 it->pcache.tree_size = size;
1394 static void cleanup_preferred_base(void)
1396 struct pbase_tree *it;
1402 struct pbase_tree *tmp = it;
1404 free(tmp->pcache.tree_data);
1408 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
1409 if (!pbase_tree_cache[i])
1411 free(pbase_tree_cache[i]->tree_data);
1412 FREE_AND_NULL(pbase_tree_cache[i]);
1415 FREE_AND_NULL(done_pbase_paths);
1416 done_pbase_paths_num = done_pbase_paths_alloc = 0;
1419 static void check_object(struct object_entry *entry)
1421 unsigned long canonical_size;
1423 if (IN_PACK(entry)) {
1424 struct packed_git *p = IN_PACK(entry);
1425 struct pack_window *w_curs = NULL;
1426 const unsigned char *base_ref = NULL;
1427 struct object_entry *base_entry;
1428 unsigned long used, used_0;
1429 unsigned long avail;
1431 unsigned char *buf, c;
1432 enum object_type type;
1433 unsigned long in_pack_size;
1435 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1438 * We want in_pack_type even if we do not reuse delta
1439 * since non-delta representations could still be reused.
1441 used = unpack_object_header_buffer(buf, avail,
1448 BUG("invalid type %d", type);
1449 entry->in_pack_type = type;
1452 * Determine if this is a delta and if so whether we can
1453 * reuse it or not. Otherwise let's find out as cheaply as
1454 * possible what the actual type and size for this object is.
1456 switch (entry->in_pack_type) {
1458 /* Not a delta hence we've already got all we need. */
1459 oe_set_type(entry, entry->in_pack_type);
1460 SET_SIZE(entry, in_pack_size);
1461 entry->in_pack_header_size = used;
1462 if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
1464 unuse_pack(&w_curs);
1467 if (reuse_delta && !entry->preferred_base)
1468 base_ref = use_pack(p, &w_curs,
1469 entry->in_pack_offset + used, NULL);
1470 entry->in_pack_header_size = used + 20;
1473 buf = use_pack(p, &w_curs,
1474 entry->in_pack_offset + used, NULL);
1480 if (!ofs || MSB(ofs, 7)) {
1481 error("delta base offset overflow in pack for %s",
1482 oid_to_hex(&entry->idx.oid));
1486 ofs = (ofs << 7) + (c & 127);
1488 ofs = entry->in_pack_offset - ofs;
1489 if (ofs <= 0 || ofs >= entry->in_pack_offset) {
1490 error("delta base offset out of bound for %s",
1491 oid_to_hex(&entry->idx.oid));
1494 if (reuse_delta && !entry->preferred_base) {
1495 struct revindex_entry *revidx;
1496 revidx = find_pack_revindex(p, ofs);
1499 base_ref = nth_packed_object_sha1(p, revidx->nr);
1501 entry->in_pack_header_size = used + used_0;
1505 if (base_ref && (base_entry = packlist_find(&to_pack, base_ref, NULL))) {
1507 * If base_ref was set above that means we wish to
1508 * reuse delta data, and we even found that base
1509 * in the list of objects we want to pack. Goodie!
1511 * Depth value does not matter - find_deltas() will
1512 * never consider reused delta as the base object to
1513 * deltify other objects against, in order to avoid
1516 oe_set_type(entry, entry->in_pack_type);
1517 SET_SIZE(entry, in_pack_size); /* delta size */
1518 SET_DELTA(entry, base_entry);
1519 SET_DELTA_SIZE(entry, in_pack_size);
1520 entry->delta_sibling_idx = base_entry->delta_child_idx;
1521 SET_DELTA_CHILD(base_entry, entry);
1522 unuse_pack(&w_curs);
1526 if (oe_type(entry)) {
1530 * This must be a delta and we already know what the
1531 * final object type is. Let's extract the actual
1532 * object size from the delta header.
1534 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
1535 canonical_size = get_size_from_delta(p, &w_curs, delta_pos);
1536 if (canonical_size == 0)
1538 SET_SIZE(entry, canonical_size);
1539 unuse_pack(&w_curs);
1544 * No choice but to fall back to the recursive delta walk
1545 * with sha1_object_info() to find about the object type
1549 unuse_pack(&w_curs);
1553 oid_object_info(the_repository, &entry->idx.oid, &canonical_size));
1554 if (entry->type_valid) {
1555 SET_SIZE(entry, canonical_size);
1558 * Bad object type is checked in prepare_pack(). This is
1559 * to permit a missing preferred base object to be ignored
1560 * as a preferred base. Doing so can result in a larger
1561 * pack file, but the transfer will still take place.
1566 static int pack_offset_sort(const void *_a, const void *_b)
1568 const struct object_entry *a = *(struct object_entry **)_a;
1569 const struct object_entry *b = *(struct object_entry **)_b;
1570 const struct packed_git *a_in_pack = IN_PACK(a);
1571 const struct packed_git *b_in_pack = IN_PACK(b);
1573 /* avoid filesystem trashing with loose objects */
1574 if (!a_in_pack && !b_in_pack)
1575 return oidcmp(&a->idx.oid, &b->idx.oid);
1577 if (a_in_pack < b_in_pack)
1579 if (a_in_pack > b_in_pack)
1581 return a->in_pack_offset < b->in_pack_offset ? -1 :
1582 (a->in_pack_offset > b->in_pack_offset);
1586 * Drop an on-disk delta we were planning to reuse. Naively, this would
1587 * just involve blanking out the "delta" field, but we have to deal
1588 * with some extra book-keeping:
1590 * 1. Removing ourselves from the delta_sibling linked list.
1592 * 2. Updating our size/type to the non-delta representation. These were
1593 * either not recorded initially (size) or overwritten with the delta type
1594 * (type) when check_object() decided to reuse the delta.
1596 * 3. Resetting our delta depth, as we are now a base object.
1598 static void drop_reused_delta(struct object_entry *entry)
1600 unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
1601 struct object_info oi = OBJECT_INFO_INIT;
1602 enum object_type type;
1606 struct object_entry *oe = &to_pack.objects[*idx - 1];
1609 *idx = oe->delta_sibling_idx;
1611 idx = &oe->delta_sibling_idx;
1613 SET_DELTA(entry, NULL);
1618 if (packed_object_info(the_repository, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
1620 * We failed to get the info from this pack for some reason;
1621 * fall back to sha1_object_info, which may find another copy.
1622 * And if that fails, the error will be recorded in oe_type(entry)
1623 * and dealt with in prepare_pack().
1626 oid_object_info(the_repository, &entry->idx.oid, &size));
1628 oe_set_type(entry, type);
1630 SET_SIZE(entry, size);
1634 * Follow the chain of deltas from this entry onward, throwing away any links
1635 * that cause us to hit a cycle (as determined by the DFS state flags in
1638 * We also detect too-long reused chains that would violate our --depth
1641 static void break_delta_chains(struct object_entry *entry)
1644 * The actual depth of each object we will write is stored as an int,
1645 * as it cannot exceed our int "depth" limit. But before we break
1646 * changes based no that limit, we may potentially go as deep as the
1647 * number of objects, which is elsewhere bounded to a uint32_t.
1649 uint32_t total_depth;
1650 struct object_entry *cur, *next;
1652 for (cur = entry, total_depth = 0;
1654 cur = DELTA(cur), total_depth++) {
1655 if (cur->dfs_state == DFS_DONE) {
1657 * We've already seen this object and know it isn't
1658 * part of a cycle. We do need to append its depth
1661 total_depth += cur->depth;
1666 * We break cycles before looping, so an ACTIVE state (or any
1667 * other cruft which made its way into the state variable)
1670 if (cur->dfs_state != DFS_NONE)
1671 die("BUG: confusing delta dfs state in first pass: %d",
1675 * Now we know this is the first time we've seen the object. If
1676 * it's not a delta, we're done traversing, but we'll mark it
1677 * done to save time on future traversals.
1680 cur->dfs_state = DFS_DONE;
1685 * Mark ourselves as active and see if the next step causes
1686 * us to cycle to another active object. It's important to do
1687 * this _before_ we loop, because it impacts where we make the
1688 * cut, and thus how our total_depth counter works.
1689 * E.g., We may see a partial loop like:
1691 * A -> B -> C -> D -> B
1693 * Cutting B->C breaks the cycle. But now the depth of A is
1694 * only 1, and our total_depth counter is at 3. The size of the
1695 * error is always one less than the size of the cycle we
1696 * broke. Commits C and D were "lost" from A's chain.
1698 * If we instead cut D->B, then the depth of A is correct at 3.
1699 * We keep all commits in the chain that we examined.
1701 cur->dfs_state = DFS_ACTIVE;
1702 if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
1703 drop_reused_delta(cur);
1704 cur->dfs_state = DFS_DONE;
1710 * And now that we've gone all the way to the bottom of the chain, we
1711 * need to clear the active flags and set the depth fields as
1712 * appropriate. Unlike the loop above, which can quit when it drops a
1713 * delta, we need to keep going to look for more depth cuts. So we need
1714 * an extra "next" pointer to keep going after we reset cur->delta.
1716 for (cur = entry; cur; cur = next) {
1720 * We should have a chain of zero or more ACTIVE states down to
1721 * a final DONE. We can quit after the DONE, because either it
1722 * has no bases, or we've already handled them in a previous
1725 if (cur->dfs_state == DFS_DONE)
1727 else if (cur->dfs_state != DFS_ACTIVE)
1728 die("BUG: confusing delta dfs state in second pass: %d",
1732 * If the total_depth is more than depth, then we need to snip
1733 * the chain into two or more smaller chains that don't exceed
1734 * the maximum depth. Most of the resulting chains will contain
1735 * (depth + 1) entries (i.e., depth deltas plus one base), and
1736 * the last chain (i.e., the one containing entry) will contain
1737 * whatever entries are left over, namely
1738 * (total_depth % (depth + 1)) of them.
1740 * Since we are iterating towards decreasing depth, we need to
1741 * decrement total_depth as we go, and we need to write to the
1742 * entry what its final depth will be after all of the
1743 * snipping. Since we're snipping into chains of length (depth
1744 * + 1) entries, the final depth of an entry will be its
1745 * original depth modulo (depth + 1). Any time we encounter an
1746 * entry whose final depth is supposed to be zero, we snip it
1747 * from its delta base, thereby making it so.
1749 cur->depth = (total_depth--) % (depth + 1);
1751 drop_reused_delta(cur);
1753 cur->dfs_state = DFS_DONE;
1757 static void get_object_details(void)
1760 struct object_entry **sorted_by_offset;
1763 progress_state = start_progress(_("Counting objects"),
1764 to_pack.nr_objects);
1766 sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));
1767 for (i = 0; i < to_pack.nr_objects; i++)
1768 sorted_by_offset[i] = to_pack.objects + i;
1769 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
1771 for (i = 0; i < to_pack.nr_objects; i++) {
1772 struct object_entry *entry = sorted_by_offset[i];
1773 check_object(entry);
1774 if (entry->type_valid &&
1775 oe_size_greater_than(&to_pack, entry, big_file_threshold))
1776 entry->no_try_delta = 1;
1777 display_progress(progress_state, i + 1);
1779 stop_progress(&progress_state);
1782 * This must happen in a second pass, since we rely on the delta
1783 * information for the whole list being completed.
1785 for (i = 0; i < to_pack.nr_objects; i++)
1786 break_delta_chains(&to_pack.objects[i]);
1788 free(sorted_by_offset);
1792 * We search for deltas in a list sorted by type, by filename hash, and then
1793 * by size, so that we see progressively smaller and smaller files.
1794 * That's because we prefer deltas to be from the bigger file
1795 * to the smaller -- deletes are potentially cheaper, but perhaps
1796 * more importantly, the bigger file is likely the more recent
1797 * one. The deepest deltas are therefore the oldest objects which are
1798 * less susceptible to be accessed often.
1800 static int type_size_sort(const void *_a, const void *_b)
1802 const struct object_entry *a = *(struct object_entry **)_a;
1803 const struct object_entry *b = *(struct object_entry **)_b;
1804 enum object_type a_type = oe_type(a);
1805 enum object_type b_type = oe_type(b);
1806 unsigned long a_size = SIZE(a);
1807 unsigned long b_size = SIZE(b);
1809 if (a_type > b_type)
1811 if (a_type < b_type)
1813 if (a->hash > b->hash)
1815 if (a->hash < b->hash)
1817 if (a->preferred_base > b->preferred_base)
1819 if (a->preferred_base < b->preferred_base)
1821 if (a_size > b_size)
1823 if (a_size < b_size)
1825 return a < b ? -1 : (a > b); /* newest first */
1829 struct object_entry *entry;
1831 struct delta_index *index;
1835 static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1836 unsigned long delta_size)
1838 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
1841 if (delta_size < cache_max_small_delta_size)
1844 /* cache delta, if objects are large enough compared to delta size */
1845 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
1853 static pthread_mutex_t read_mutex;
1854 #define read_lock() pthread_mutex_lock(&read_mutex)
1855 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1857 static pthread_mutex_t cache_mutex;
1858 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1859 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1861 static pthread_mutex_t progress_mutex;
1862 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1863 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1867 #define read_lock() (void)0
1868 #define read_unlock() (void)0
1869 #define cache_lock() (void)0
1870 #define cache_unlock() (void)0
1871 #define progress_lock() (void)0
1872 #define progress_unlock() (void)0
1877 * Return the size of the object without doing any delta
1878 * reconstruction (so non-deltas are true object sizes, but deltas
1879 * return the size of the delta data).
1881 unsigned long oe_get_size_slow(struct packing_data *pack,
1882 const struct object_entry *e)
1884 struct packed_git *p;
1885 struct pack_window *w_curs;
1887 enum object_type type;
1888 unsigned long used, avail, size;
1890 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
1892 if (oid_object_info(the_repository, &e->idx.oid, &size) < 0)
1893 die(_("unable to get size of %s"),
1894 oid_to_hex(&e->idx.oid));
1899 p = oe_in_pack(pack, e);
1901 BUG("when e->type is a delta, it must belong to a pack");
1905 buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
1906 used = unpack_object_header_buffer(buf, avail, &type, &size);
1908 die(_("unable to parse object header of %s"),
1909 oid_to_hex(&e->idx.oid));
1911 unuse_pack(&w_curs);
1916 static int try_delta(struct unpacked *trg, struct unpacked *src,
1917 unsigned max_depth, unsigned long *mem_usage)
1919 struct object_entry *trg_entry = trg->entry;
1920 struct object_entry *src_entry = src->entry;
1921 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1923 enum object_type type;
1926 /* Don't bother doing diffs between different types */
1927 if (oe_type(trg_entry) != oe_type(src_entry))
1931 * We do not bother to try a delta that we discarded on an
1932 * earlier try, but only when reusing delta data. Note that
1933 * src_entry that is marked as the preferred_base should always
1934 * be considered, as even if we produce a suboptimal delta against
1935 * it, we will still save the transfer cost, as we already know
1936 * the other side has it and we won't send src_entry at all.
1938 if (reuse_delta && IN_PACK(trg_entry) &&
1939 IN_PACK(trg_entry) == IN_PACK(src_entry) &&
1940 !src_entry->preferred_base &&
1941 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1942 trg_entry->in_pack_type != OBJ_OFS_DELTA)
1945 /* Let's not bust the allowed depth. */
1946 if (src->depth >= max_depth)
1949 /* Now some size filtering heuristics. */
1950 trg_size = SIZE(trg_entry);
1951 if (!DELTA(trg_entry)) {
1952 max_size = trg_size/2 - 20;
1955 max_size = DELTA_SIZE(trg_entry);
1956 ref_depth = trg->depth;
1958 max_size = (uint64_t)max_size * (max_depth - src->depth) /
1959 (max_depth - ref_depth + 1);
1962 src_size = SIZE(src_entry);
1963 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1964 if (sizediff >= max_size)
1966 if (trg_size < src_size / 32)
1969 /* Load data if not already done */
1972 trg->data = read_object_file(&trg_entry->idx.oid, &type, &sz);
1975 die("object %s cannot be read",
1976 oid_to_hex(&trg_entry->idx.oid));
1978 die("object %s inconsistent object length (%lu vs %lu)",
1979 oid_to_hex(&trg_entry->idx.oid), sz,
1985 src->data = read_object_file(&src_entry->idx.oid, &type, &sz);
1988 if (src_entry->preferred_base) {
1989 static int warned = 0;
1991 warning("object %s cannot be read",
1992 oid_to_hex(&src_entry->idx.oid));
1994 * Those objects are not included in the
1995 * resulting pack. Be resilient and ignore
1996 * them if they can't be read, in case the
1997 * pack could be created nevertheless.
2001 die("object %s cannot be read",
2002 oid_to_hex(&src_entry->idx.oid));
2005 die("object %s inconsistent object length (%lu vs %lu)",
2006 oid_to_hex(&src_entry->idx.oid), sz,
2011 src->index = create_delta_index(src->data, src_size);
2013 static int warned = 0;
2015 warning("suboptimal pack - out of memory");
2018 *mem_usage += sizeof_delta_index(src->index);
2021 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
2024 if (delta_size >= (1U << OE_DELTA_SIZE_BITS)) {
2029 if (DELTA(trg_entry)) {
2030 /* Prefer only shallower same-sized deltas. */
2031 if (delta_size == DELTA_SIZE(trg_entry) &&
2032 src->depth + 1 >= trg->depth) {
2039 * Handle memory allocation outside of the cache
2040 * accounting lock. Compiler will optimize the strangeness
2041 * away when NO_PTHREADS is defined.
2043 free(trg_entry->delta_data);
2045 if (trg_entry->delta_data) {
2046 delta_cache_size -= DELTA_SIZE(trg_entry);
2047 trg_entry->delta_data = NULL;
2049 if (delta_cacheable(src_size, trg_size, delta_size)) {
2050 delta_cache_size += delta_size;
2052 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
2058 SET_DELTA(trg_entry, src_entry);
2059 SET_DELTA_SIZE(trg_entry, delta_size);
2060 trg->depth = src->depth + 1;
2065 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
2067 struct object_entry *child = DELTA_CHILD(me);
2070 unsigned int c = check_delta_limit(child, n + 1);
2073 child = DELTA_SIBLING(child);
2078 static unsigned long free_unpacked(struct unpacked *n)
2080 unsigned long freed_mem = sizeof_delta_index(n->index);
2081 free_delta_index(n->index);
2084 freed_mem += SIZE(n->entry);
2085 FREE_AND_NULL(n->data);
2092 static void find_deltas(struct object_entry **list, unsigned *list_size,
2093 int window, int depth, unsigned *processed)
2095 uint32_t i, idx = 0, count = 0;
2096 struct unpacked *array;
2097 unsigned long mem_usage = 0;
2099 array = xcalloc(window, sizeof(struct unpacked));
2102 struct object_entry *entry;
2103 struct unpacked *n = array + idx;
2104 int j, max_depth, best_base = -1;
2113 if (!entry->preferred_base) {
2115 display_progress(progress_state, *processed);
2119 mem_usage -= free_unpacked(n);
2122 while (window_memory_limit &&
2123 mem_usage > window_memory_limit &&
2125 uint32_t tail = (idx + window - count) % window;
2126 mem_usage -= free_unpacked(array + tail);
2130 /* We do not compute delta to *create* objects we are not
2133 if (entry->preferred_base)
2137 * If the current object is at pack edge, take the depth the
2138 * objects that depend on the current object into account
2139 * otherwise they would become too deep.
2142 if (DELTA_CHILD(entry)) {
2143 max_depth -= check_delta_limit(entry, 0);
2151 uint32_t other_idx = idx + j;
2153 if (other_idx >= window)
2154 other_idx -= window;
2155 m = array + other_idx;
2158 ret = try_delta(n, m, max_depth, &mem_usage);
2162 best_base = other_idx;
2166 * If we decided to cache the delta data, then it is best
2167 * to compress it right away. First because we have to do
2168 * it anyway, and doing it here while we're threaded will
2169 * save a lot of time in the non threaded write phase,
2170 * as well as allow for caching more deltas within
2171 * the same cache size limit.
2173 * But only if not writing to stdout, since in that case
2174 * the network is most likely throttling writes anyway,
2175 * and therefore it is best to go to the write phase ASAP
2176 * instead, as we can afford spending more time compressing
2177 * between writes at that moment.
2179 if (entry->delta_data && !pack_to_stdout) {
2182 size = do_compress(&entry->delta_data, DELTA_SIZE(entry));
2183 if (size < (1U << OE_Z_DELTA_BITS)) {
2184 entry->z_delta_size = size;
2186 delta_cache_size -= DELTA_SIZE(entry);
2187 delta_cache_size += entry->z_delta_size;
2190 FREE_AND_NULL(entry->delta_data);
2191 entry->z_delta_size = 0;
2195 /* if we made n a delta, and if n is already at max
2196 * depth, leaving it in the window is pointless. we
2197 * should evict it first.
2199 if (DELTA(entry) && max_depth <= n->depth)
2203 * Move the best delta base up in the window, after the
2204 * currently deltified object, to keep it longer. It will
2205 * be the first base object to be attempted next.
2208 struct unpacked swap = array[best_base];
2209 int dist = (window + idx - best_base) % window;
2210 int dst = best_base;
2212 int src = (dst + 1) % window;
2213 array[dst] = array[src];
2221 if (count + 1 < window)
2227 for (i = 0; i < window; ++i) {
2228 free_delta_index(array[i].index);
2229 free(array[i].data);
2236 static void try_to_free_from_threads(size_t size)
2239 release_pack_memory(size);
2243 static try_to_free_t old_try_to_free_routine;
2246 * The main thread waits on the condition that (at least) one of the workers
2247 * has stopped working (which is indicated in the .working member of
2248 * struct thread_params).
2249 * When a work thread has completed its work, it sets .working to 0 and
2250 * signals the main thread and waits on the condition that .data_ready
2254 struct thread_params {
2256 struct object_entry **list;
2263 pthread_mutex_t mutex;
2264 pthread_cond_t cond;
2265 unsigned *processed;
2268 static pthread_cond_t progress_cond;
2271 * Mutex and conditional variable can't be statically-initialized on Windows.
2273 static void init_threaded_search(void)
2275 init_recursive_mutex(&read_mutex);
2276 pthread_mutex_init(&cache_mutex, NULL);
2277 pthread_mutex_init(&progress_mutex, NULL);
2278 pthread_cond_init(&progress_cond, NULL);
2279 old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads);
2282 static void cleanup_threaded_search(void)
2284 set_try_to_free_routine(old_try_to_free_routine);
2285 pthread_cond_destroy(&progress_cond);
2286 pthread_mutex_destroy(&read_mutex);
2287 pthread_mutex_destroy(&cache_mutex);
2288 pthread_mutex_destroy(&progress_mutex);
2291 static void *threaded_find_deltas(void *arg)
2293 struct thread_params *me = arg;
2296 while (me->remaining) {
2299 find_deltas(me->list, &me->remaining,
2300 me->window, me->depth, me->processed);
2304 pthread_cond_signal(&progress_cond);
2308 * We must not set ->data_ready before we wait on the
2309 * condition because the main thread may have set it to 1
2310 * before we get here. In order to be sure that new
2311 * work is available if we see 1 in ->data_ready, it
2312 * was initialized to 0 before this thread was spawned
2313 * and we reset it to 0 right away.
2315 pthread_mutex_lock(&me->mutex);
2316 while (!me->data_ready)
2317 pthread_cond_wait(&me->cond, &me->mutex);
2319 pthread_mutex_unlock(&me->mutex);
2324 /* leave ->working 1 so that this doesn't get more work assigned */
2328 static void ll_find_deltas(struct object_entry **list, unsigned list_size,
2329 int window, int depth, unsigned *processed)
2331 struct thread_params *p;
2332 int i, ret, active_threads = 0;
2334 init_threaded_search();
2336 if (delta_search_threads <= 1) {
2337 find_deltas(list, &list_size, window, depth, processed);
2338 cleanup_threaded_search();
2341 if (progress > pack_to_stdout)
2342 fprintf(stderr, "Delta compression using up to %d threads.\n",
2343 delta_search_threads);
2344 p = xcalloc(delta_search_threads, sizeof(*p));
2346 /* Partition the work amongst work threads. */
2347 for (i = 0; i < delta_search_threads; i++) {
2348 unsigned sub_size = list_size / (delta_search_threads - i);
2350 /* don't use too small segments or no deltas will be found */
2351 if (sub_size < 2*window && i+1 < delta_search_threads)
2354 p[i].window = window;
2356 p[i].processed = processed;
2358 p[i].data_ready = 0;
2360 /* try to split chunks on "path" boundaries */
2361 while (sub_size && sub_size < list_size &&
2362 list[sub_size]->hash &&
2363 list[sub_size]->hash == list[sub_size-1]->hash)
2367 p[i].list_size = sub_size;
2368 p[i].remaining = sub_size;
2371 list_size -= sub_size;
2374 /* Start work threads. */
2375 for (i = 0; i < delta_search_threads; i++) {
2376 if (!p[i].list_size)
2378 pthread_mutex_init(&p[i].mutex, NULL);
2379 pthread_cond_init(&p[i].cond, NULL);
2380 ret = pthread_create(&p[i].thread, NULL,
2381 threaded_find_deltas, &p[i]);
2383 die("unable to create thread: %s", strerror(ret));
2388 * Now let's wait for work completion. Each time a thread is done
2389 * with its work, we steal half of the remaining work from the
2390 * thread with the largest number of unprocessed objects and give
2391 * it to that newly idle thread. This ensure good load balancing
2392 * until the remaining object list segments are simply too short
2393 * to be worth splitting anymore.
2395 while (active_threads) {
2396 struct thread_params *target = NULL;
2397 struct thread_params *victim = NULL;
2398 unsigned sub_size = 0;
2402 for (i = 0; !target && i < delta_search_threads; i++)
2407 pthread_cond_wait(&progress_cond, &progress_mutex);
2410 for (i = 0; i < delta_search_threads; i++)
2411 if (p[i].remaining > 2*window &&
2412 (!victim || victim->remaining < p[i].remaining))
2415 sub_size = victim->remaining / 2;
2416 list = victim->list + victim->list_size - sub_size;
2417 while (sub_size && list[0]->hash &&
2418 list[0]->hash == list[-1]->hash) {
2424 * It is possible for some "paths" to have
2425 * so many objects that no hash boundary
2426 * might be found. Let's just steal the
2427 * exact half in that case.
2429 sub_size = victim->remaining / 2;
2432 target->list = list;
2433 victim->list_size -= sub_size;
2434 victim->remaining -= sub_size;
2436 target->list_size = sub_size;
2437 target->remaining = sub_size;
2438 target->working = 1;
2441 pthread_mutex_lock(&target->mutex);
2442 target->data_ready = 1;
2443 pthread_cond_signal(&target->cond);
2444 pthread_mutex_unlock(&target->mutex);
2447 pthread_join(target->thread, NULL);
2448 pthread_cond_destroy(&target->cond);
2449 pthread_mutex_destroy(&target->mutex);
2453 cleanup_threaded_search();
2458 #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)
2461 static void add_tag_chain(const struct object_id *oid)
2466 * We catch duplicates already in add_object_entry(), but we'd
2467 * prefer to do this extra check to avoid having to parse the
2468 * tag at all if we already know that it's being packed (e.g., if
2469 * it was included via bitmaps, we would not have parsed it
2472 if (packlist_find(&to_pack, oid->hash, NULL))
2475 tag = lookup_tag(oid);
2477 if (!tag || parse_tag(tag) || !tag->tagged)
2478 die("unable to pack objects reachable from tag %s",
2481 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
2483 if (tag->tagged->type != OBJ_TAG)
2486 tag = (struct tag *)tag->tagged;
2490 static int add_ref_tag(const char *path, const struct object_id *oid, int flag, void *cb_data)
2492 struct object_id peeled;
2494 if (starts_with(path, "refs/tags/") && /* is a tag? */
2495 !peel_ref(path, &peeled) && /* peelable? */
2496 packlist_find(&to_pack, peeled.hash, NULL)) /* object packed? */
2501 static void prepare_pack(int window, int depth)
2503 struct object_entry **delta_list;
2504 uint32_t i, nr_deltas;
2507 get_object_details();
2510 * If we're locally repacking then we need to be doubly careful
2511 * from now on in order to make sure no stealth corruption gets
2512 * propagated to the new pack. Clients receiving streamed packs
2513 * should validate everything they get anyway so no need to incur
2514 * the additional cost here in that case.
2516 if (!pack_to_stdout)
2517 do_check_packed_object_crc = 1;
2519 if (!to_pack.nr_objects || !window || !depth)
2522 ALLOC_ARRAY(delta_list, to_pack.nr_objects);
2525 for (i = 0; i < to_pack.nr_objects; i++) {
2526 struct object_entry *entry = to_pack.objects + i;
2529 /* This happens if we decided to reuse existing
2530 * delta from a pack. "reuse_delta &&" is implied.
2534 if (!entry->type_valid ||
2535 oe_size_less_than(&to_pack, entry, 50))
2538 if (entry->no_try_delta)
2541 if (!entry->preferred_base) {
2543 if (oe_type(entry) < 0)
2544 die("unable to get type of object %s",
2545 oid_to_hex(&entry->idx.oid));
2547 if (oe_type(entry) < 0) {
2549 * This object is not found, but we
2550 * don't have to include it anyway.
2556 delta_list[n++] = entry;
2559 if (nr_deltas && n > 1) {
2560 unsigned nr_done = 0;
2562 progress_state = start_progress(_("Compressing objects"),
2564 QSORT(delta_list, n, type_size_sort);
2565 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
2566 stop_progress(&progress_state);
2567 if (nr_done != nr_deltas)
2568 die("inconsistency with delta count");
2573 static int git_pack_config(const char *k, const char *v, void *cb)
2575 if (!strcmp(k, "pack.window")) {
2576 window = git_config_int(k, v);
2579 if (!strcmp(k, "pack.windowmemory")) {
2580 window_memory_limit = git_config_ulong(k, v);
2583 if (!strcmp(k, "pack.depth")) {
2584 depth = git_config_int(k, v);
2587 if (!strcmp(k, "pack.deltacachesize")) {
2588 max_delta_cache_size = git_config_int(k, v);
2591 if (!strcmp(k, "pack.deltacachelimit")) {
2592 cache_max_small_delta_size = git_config_int(k, v);
2595 if (!strcmp(k, "pack.writebitmaphashcache")) {
2596 if (git_config_bool(k, v))
2597 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
2599 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
2601 if (!strcmp(k, "pack.usebitmaps")) {
2602 use_bitmap_index_default = git_config_bool(k, v);
2605 if (!strcmp(k, "pack.threads")) {
2606 delta_search_threads = git_config_int(k, v);
2607 if (delta_search_threads < 0)
2608 die("invalid number of threads specified (%d)",
2609 delta_search_threads);
2611 if (delta_search_threads != 1) {
2612 warning("no threads support, ignoring %s", k);
2613 delta_search_threads = 0;
2618 if (!strcmp(k, "pack.indexversion")) {
2619 pack_idx_opts.version = git_config_int(k, v);
2620 if (pack_idx_opts.version > 2)
2621 die("bad pack.indexversion=%"PRIu32,
2622 pack_idx_opts.version);
2625 return git_default_config(k, v, cb);
2628 static void read_object_list_from_stdin(void)
2630 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
2631 struct object_id oid;
2635 if (!fgets(line, sizeof(line), stdin)) {
2639 die("fgets returned NULL, not EOF, not error!");
2645 if (line[0] == '-') {
2646 if (get_oid_hex(line+1, &oid))
2647 die("expected edge object ID, got garbage:\n %s",
2649 add_preferred_base(&oid);
2652 if (parse_oid_hex(line, &oid, &p))
2653 die("expected object ID, got garbage:\n %s", line);
2655 add_preferred_base_object(p + 1);
2656 add_object_entry(&oid, OBJ_NONE, p + 1, 0);
2660 /* Remember to update object flag allocation in object.h */
2661 #define OBJECT_ADDED (1u<<20)
2663 static void show_commit(struct commit *commit, void *data)
2665 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
2666 commit->object.flags |= OBJECT_ADDED;
2668 if (write_bitmap_index)
2669 index_commit_for_bitmap(commit);
2672 static void show_object(struct object *obj, const char *name, void *data)
2674 add_preferred_base_object(name);
2675 add_object_entry(&obj->oid, obj->type, name, 0);
2676 obj->flags |= OBJECT_ADDED;
2679 static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
2681 assert(arg_missing_action == MA_ALLOW_ANY);
2684 * Quietly ignore ALL missing objects. This avoids problems with
2685 * staging them now and getting an odd error later.
2687 if (!has_object_file(&obj->oid))
2690 show_object(obj, name, data);
2693 static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
2695 assert(arg_missing_action == MA_ALLOW_PROMISOR);
2698 * Quietly ignore EXPECTED missing objects. This avoids problems with
2699 * staging them now and getting an odd error later.
2701 if (!has_object_file(&obj->oid) && is_promisor_object(&obj->oid))
2704 show_object(obj, name, data);
2707 static int option_parse_missing_action(const struct option *opt,
2708 const char *arg, int unset)
2713 if (!strcmp(arg, "error")) {
2714 arg_missing_action = MA_ERROR;
2715 fn_show_object = show_object;
2719 if (!strcmp(arg, "allow-any")) {
2720 arg_missing_action = MA_ALLOW_ANY;
2721 fetch_if_missing = 0;
2722 fn_show_object = show_object__ma_allow_any;
2726 if (!strcmp(arg, "allow-promisor")) {
2727 arg_missing_action = MA_ALLOW_PROMISOR;
2728 fetch_if_missing = 0;
2729 fn_show_object = show_object__ma_allow_promisor;
2733 die(_("invalid value for --missing"));
2737 static void show_edge(struct commit *commit)
2739 add_preferred_base(&commit->object.oid);
2742 struct in_pack_object {
2744 struct object *object;
2750 struct in_pack_object *array;
2753 static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack)
2755 in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p);
2756 in_pack->array[in_pack->nr].object = object;
2761 * Compare the objects in the offset order, in order to emulate the
2762 * "git rev-list --objects" output that produced the pack originally.
2764 static int ofscmp(const void *a_, const void *b_)
2766 struct in_pack_object *a = (struct in_pack_object *)a_;
2767 struct in_pack_object *b = (struct in_pack_object *)b_;
2769 if (a->offset < b->offset)
2771 else if (a->offset > b->offset)
2774 return oidcmp(&a->object->oid, &b->object->oid);
2777 static void add_objects_in_unpacked_packs(struct rev_info *revs)
2779 struct packed_git *p;
2780 struct in_pack in_pack;
2783 memset(&in_pack, 0, sizeof(in_pack));
2785 for (p = get_packed_git(the_repository); p; p = p->next) {
2786 struct object_id oid;
2789 if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
2791 if (open_pack_index(p))
2792 die("cannot open pack index");
2794 ALLOC_GROW(in_pack.array,
2795 in_pack.nr + p->num_objects,
2798 for (i = 0; i < p->num_objects; i++) {
2799 nth_packed_object_oid(&oid, p, i);
2800 o = lookup_unknown_object(oid.hash);
2801 if (!(o->flags & OBJECT_ADDED))
2802 mark_in_pack_object(o, p, &in_pack);
2803 o->flags |= OBJECT_ADDED;
2808 QSORT(in_pack.array, in_pack.nr, ofscmp);
2809 for (i = 0; i < in_pack.nr; i++) {
2810 struct object *o = in_pack.array[i].object;
2811 add_object_entry(&o->oid, o->type, "", 0);
2814 free(in_pack.array);
2817 static int add_loose_object(const struct object_id *oid, const char *path,
2820 enum object_type type = oid_object_info(the_repository, oid, NULL);
2823 warning("loose object at %s could not be examined", path);
2827 add_object_entry(oid, type, "", 0);
2832 * We actually don't even have to worry about reachability here.
2833 * add_object_entry will weed out duplicates, so we just add every
2834 * loose object we find.
2836 static void add_unreachable_loose_objects(void)
2838 for_each_loose_file_in_objdir(get_object_directory(),
2843 static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
2845 static struct packed_git *last_found = (void *)1;
2846 struct packed_git *p;
2848 p = (last_found != (void *)1) ? last_found :
2849 get_packed_git(the_repository);
2852 if ((!p->pack_local || p->pack_keep ||
2853 p->pack_keep_in_core) &&
2854 find_pack_entry_one(oid->hash, p)) {
2858 if (p == last_found)
2859 p = get_packed_git(the_repository);
2862 if (p == last_found)
2869 * Store a list of sha1s that are should not be discarded
2870 * because they are either written too recently, or are
2871 * reachable from another object that was.
2873 * This is filled by get_object_list.
2875 static struct oid_array recent_objects;
2877 static int loosened_object_can_be_discarded(const struct object_id *oid,
2880 if (!unpack_unreachable_expiration)
2882 if (mtime > unpack_unreachable_expiration)
2884 if (oid_array_lookup(&recent_objects, oid) >= 0)
2889 static void loosen_unused_packed_objects(struct rev_info *revs)
2891 struct packed_git *p;
2893 struct object_id oid;
2895 for (p = get_packed_git(the_repository); p; p = p->next) {
2896 if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
2899 if (open_pack_index(p))
2900 die("cannot open pack index");
2902 for (i = 0; i < p->num_objects; i++) {
2903 nth_packed_object_oid(&oid, p, i);
2904 if (!packlist_find(&to_pack, oid.hash, NULL) &&
2905 !has_sha1_pack_kept_or_nonlocal(&oid) &&
2906 !loosened_object_can_be_discarded(&oid, p->mtime))
2907 if (force_object_loose(&oid, p->mtime))
2908 die("unable to force loose object");
2914 * This tracks any options which pack-reuse code expects to be on, or which a
2915 * reader of the pack might not understand, and which would therefore prevent
2916 * blind reuse of what we have on disk.
2918 static int pack_options_allow_reuse(void)
2920 return pack_to_stdout &&
2922 !ignore_packed_keep_on_disk &&
2923 !ignore_packed_keep_in_core &&
2924 (!local || !have_non_local_packs) &&
2928 static int get_object_list_from_bitmap(struct rev_info *revs)
2930 if (prepare_bitmap_walk(revs) < 0)
2933 if (pack_options_allow_reuse() &&
2934 !reuse_partial_packfile_from_bitmap(
2936 &reuse_packfile_objects,
2937 &reuse_packfile_offset)) {
2938 assert(reuse_packfile_objects);
2939 nr_result += reuse_packfile_objects;
2940 display_progress(progress_state, nr_result);
2943 traverse_bitmap_commit_list(&add_object_entry_from_bitmap);
2947 static void record_recent_object(struct object *obj,
2951 oid_array_append(&recent_objects, &obj->oid);
2954 static void record_recent_commit(struct commit *commit, void *data)
2956 oid_array_append(&recent_objects, &commit->object.oid);
2959 static void get_object_list(int ac, const char **av)
2961 struct rev_info revs;
2965 init_revisions(&revs, NULL);
2966 save_commit_buffer = 0;
2967 setup_revisions(ac, av, &revs, NULL);
2969 /* make sure shallows are read */
2970 is_repository_shallow();
2972 while (fgets(line, sizeof(line), stdin) != NULL) {
2973 int len = strlen(line);
2974 if (len && line[len - 1] == '\n')
2979 if (!strcmp(line, "--not")) {
2980 flags ^= UNINTERESTING;
2981 write_bitmap_index = 0;
2984 if (starts_with(line, "--shallow ")) {
2985 struct object_id oid;
2986 if (get_oid_hex(line + 10, &oid))
2987 die("not an SHA-1 '%s'", line + 10);
2988 register_shallow(&oid);
2989 use_bitmap_index = 0;
2992 die("not a rev '%s'", line);
2994 if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))
2995 die("bad revision '%s'", line);
2998 if (use_bitmap_index && !get_object_list_from_bitmap(&revs))
3001 if (prepare_revision_walk(&revs))
3002 die("revision walk setup failed");
3003 mark_edges_uninteresting(&revs, show_edge);
3005 if (!fn_show_object)
3006 fn_show_object = show_object;
3007 traverse_commit_list_filtered(&filter_options, &revs,
3008 show_commit, fn_show_object, NULL,
3011 if (unpack_unreachable_expiration) {
3012 revs.ignore_missing_links = 1;
3013 if (add_unseen_recent_objects_to_traversal(&revs,
3014 unpack_unreachable_expiration))
3015 die("unable to add recent objects");
3016 if (prepare_revision_walk(&revs))
3017 die("revision walk setup failed");
3018 traverse_commit_list(&revs, record_recent_commit,
3019 record_recent_object, NULL);
3022 if (keep_unreachable)
3023 add_objects_in_unpacked_packs(&revs);
3024 if (pack_loose_unreachable)
3025 add_unreachable_loose_objects();
3026 if (unpack_unreachable)
3027 loosen_unused_packed_objects(&revs);
3029 oid_array_clear(&recent_objects);
3032 static void add_extra_kept_packs(const struct string_list *names)
3034 struct packed_git *p;
3039 for (p = get_packed_git(the_repository); p; p = p->next) {
3040 const char *name = basename(p->pack_name);
3046 for (i = 0; i < names->nr; i++)
3047 if (!fspathcmp(name, names->items[i].string))
3050 if (i < names->nr) {
3051 p->pack_keep_in_core = 1;
3052 ignore_packed_keep_in_core = 1;
3058 static int option_parse_index_version(const struct option *opt,
3059 const char *arg, int unset)
3062 const char *val = arg;
3063 pack_idx_opts.version = strtoul(val, &c, 10);
3064 if (pack_idx_opts.version > 2)
3065 die(_("unsupported index version %s"), val);
3066 if (*c == ',' && c[1])
3067 pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
3068 if (*c || pack_idx_opts.off32_limit & 0x80000000)
3069 die(_("bad index version '%s'"), val);
3073 static int option_parse_unpack_unreachable(const struct option *opt,
3074 const char *arg, int unset)
3077 unpack_unreachable = 0;
3078 unpack_unreachable_expiration = 0;
3081 unpack_unreachable = 1;
3083 unpack_unreachable_expiration = approxidate(arg);
3088 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
3090 int use_internal_rev_list = 0;
3093 int all_progress_implied = 0;
3094 struct argv_array rp = ARGV_ARRAY_INIT;
3095 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
3096 int rev_list_index = 0;
3097 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
3098 struct option pack_objects_options[] = {
3099 OPT_SET_INT('q', "quiet", &progress,
3100 N_("do not show progress meter"), 0),
3101 OPT_SET_INT(0, "progress", &progress,
3102 N_("show progress meter"), 1),
3103 OPT_SET_INT(0, "all-progress", &progress,
3104 N_("show progress meter during object writing phase"), 2),
3105 OPT_BOOL(0, "all-progress-implied",
3106 &all_progress_implied,
3107 N_("similar to --all-progress when progress meter is shown")),
3108 { OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"),
3109 N_("write the pack index file in the specified idx format version"),
3110 0, option_parse_index_version },
3111 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
3112 N_("maximum size of each output pack file")),
3113 OPT_BOOL(0, "local", &local,
3114 N_("ignore borrowed objects from alternate object store")),
3115 OPT_BOOL(0, "incremental", &incremental,
3116 N_("ignore packed objects")),
3117 OPT_INTEGER(0, "window", &window,
3118 N_("limit pack window by objects")),
3119 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
3120 N_("limit pack window by memory in addition to object limit")),
3121 OPT_INTEGER(0, "depth", &depth,
3122 N_("maximum length of delta chain allowed in the resulting pack")),
3123 OPT_BOOL(0, "reuse-delta", &reuse_delta,
3124 N_("reuse existing deltas")),
3125 OPT_BOOL(0, "reuse-object", &reuse_object,
3126 N_("reuse existing objects")),
3127 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
3128 N_("use OFS_DELTA objects")),
3129 OPT_INTEGER(0, "threads", &delta_search_threads,
3130 N_("use threads when searching for best delta matches")),
3131 OPT_BOOL(0, "non-empty", &non_empty,
3132 N_("do not create an empty pack output")),
3133 OPT_BOOL(0, "revs", &use_internal_rev_list,
3134 N_("read revision arguments from standard input")),
3135 { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL,
3136 N_("limit the objects to those that are not yet packed"),
3137 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
3138 { OPTION_SET_INT, 0, "all", &rev_list_all, NULL,
3139 N_("include objects reachable from any reference"),
3140 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
3141 { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL,
3142 N_("include objects referred by reflog entries"),
3143 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
3144 { OPTION_SET_INT, 0, "indexed-objects", &rev_list_index, NULL,
3145 N_("include objects referred to by the index"),
3146 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
3147 OPT_BOOL(0, "stdout", &pack_to_stdout,
3148 N_("output pack to stdout")),
3149 OPT_BOOL(0, "include-tag", &include_tag,
3150 N_("include tag objects that refer to objects to be packed")),
3151 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
3152 N_("keep unreachable objects")),
3153 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
3154 N_("pack loose unreachable objects")),
3155 { OPTION_CALLBACK, 0, "unpack-unreachable", NULL, N_("time"),
3156 N_("unpack unreachable objects newer than <time>"),
3157 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },
3158 OPT_BOOL(0, "thin", &thin,
3159 N_("create thin packs")),
3160 OPT_BOOL(0, "shallow", &shallow,
3161 N_("create packs suitable for shallow fetches")),
3162 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
3163 N_("ignore packs that have companion .keep file")),
3164 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
3165 N_("ignore this pack")),
3166 OPT_INTEGER(0, "compression", &pack_compression_level,
3167 N_("pack compression level")),
3168 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents,
3169 N_("do not hide commits by grafts"), 0),
3170 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
3171 N_("use a bitmap index if available to speed up counting objects")),
3172 OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index,
3173 N_("write a bitmap index together with the pack index")),
3174 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
3175 { OPTION_CALLBACK, 0, "missing", NULL, N_("action"),
3176 N_("handling for missing objects"), PARSE_OPT_NONEG,
3177 option_parse_missing_action },
3178 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
3179 N_("do not pack objects in promisor packfiles")),
3183 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
3184 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
3186 check_replace_refs = 0;
3188 reset_pack_idx_option(&pack_idx_opts);
3189 git_config(git_pack_config, NULL);
3191 progress = isatty(2);
3192 argc = parse_options(argc, argv, prefix, pack_objects_options,
3196 base_name = argv[0];
3199 if (pack_to_stdout != !base_name || argc)
3200 usage_with_options(pack_usage, pack_objects_options);
3202 if (depth >= (1 << OE_DEPTH_BITS)) {
3203 warning(_("delta chain depth %d is too deep, forcing %d"),
3204 depth, (1 << OE_DEPTH_BITS) - 1);
3205 depth = (1 << OE_DEPTH_BITS) - 1;
3207 if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
3208 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
3209 (1U << OE_Z_DELTA_BITS) - 1);
3210 cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
3213 argv_array_push(&rp, "pack-objects");
3215 use_internal_rev_list = 1;
3216 argv_array_push(&rp, shallow
3217 ? "--objects-edge-aggressive"
3218 : "--objects-edge");
3220 argv_array_push(&rp, "--objects");
3223 use_internal_rev_list = 1;
3224 argv_array_push(&rp, "--all");
3226 if (rev_list_reflog) {
3227 use_internal_rev_list = 1;
3228 argv_array_push(&rp, "--reflog");
3230 if (rev_list_index) {
3231 use_internal_rev_list = 1;
3232 argv_array_push(&rp, "--indexed-objects");
3234 if (rev_list_unpacked) {
3235 use_internal_rev_list = 1;
3236 argv_array_push(&rp, "--unpacked");
3239 if (exclude_promisor_objects) {
3240 use_internal_rev_list = 1;
3241 fetch_if_missing = 0;
3242 argv_array_push(&rp, "--exclude-promisor-objects");
3247 if (pack_compression_level == -1)
3248 pack_compression_level = Z_DEFAULT_COMPRESSION;
3249 else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
3250 die("bad pack compression level %d", pack_compression_level);
3252 if (!delta_search_threads) /* --threads=0 means autodetect */
3253 delta_search_threads = online_cpus();
3256 if (delta_search_threads != 1)
3257 warning("no threads support, ignoring --threads");
3259 if (!pack_to_stdout && !pack_size_limit)
3260 pack_size_limit = pack_size_limit_cfg;
3261 if (pack_to_stdout && pack_size_limit)
3262 die("--max-pack-size cannot be used to build a pack for transfer.");
3263 if (pack_size_limit && pack_size_limit < 1024*1024) {
3264 warning("minimum pack size limit is 1 MiB");
3265 pack_size_limit = 1024*1024;
3268 if (!pack_to_stdout && thin)
3269 die("--thin cannot be used to build an indexable pack.");
3271 if (keep_unreachable && unpack_unreachable)
3272 die("--keep-unreachable and --unpack-unreachable are incompatible.");
3273 if (!rev_list_all || !rev_list_reflog || !rev_list_index)
3274 unpack_unreachable_expiration = 0;
3276 if (filter_options.choice) {
3277 if (!pack_to_stdout)
3278 die("cannot use --filter without --stdout.");
3279 use_bitmap_index = 0;
3283 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
3285 * - to produce good pack (with bitmap index not-yet-packed objects are
3286 * packed in suboptimal order).
3288 * - to use more robust pack-generation codepath (avoiding possible
3289 * bugs in bitmap code and possible bitmap index corruption).
3291 if (!pack_to_stdout)
3292 use_bitmap_index_default = 0;
3294 if (use_bitmap_index < 0)
3295 use_bitmap_index = use_bitmap_index_default;
3297 /* "hard" reasons not to use bitmaps; these just won't work at all */
3298 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow())
3299 use_bitmap_index = 0;
3301 if (pack_to_stdout || !rev_list_all)
3302 write_bitmap_index = 0;
3304 if (progress && all_progress_implied)
3307 add_extra_kept_packs(&keep_pack_list);
3308 if (ignore_packed_keep_on_disk) {
3309 struct packed_git *p;
3310 for (p = get_packed_git(the_repository); p; p = p->next)
3311 if (p->pack_local && p->pack_keep)
3313 if (!p) /* no keep-able packs found */
3314 ignore_packed_keep_on_disk = 0;
3318 * unlike ignore_packed_keep_on_disk above, we do not
3319 * want to unset "local" based on looking at packs, as
3320 * it also covers non-local objects
3322 struct packed_git *p;
3323 for (p = get_packed_git(the_repository); p; p = p->next) {
3324 if (!p->pack_local) {
3325 have_non_local_packs = 1;
3331 prepare_packing_data(&to_pack);
3334 progress_state = start_progress(_("Enumerating objects"), 0);
3335 if (!use_internal_rev_list)
3336 read_object_list_from_stdin();
3338 get_object_list(rp.argc, rp.argv);
3339 argv_array_clear(&rp);
3341 cleanup_preferred_base();
3342 if (include_tag && nr_result)
3343 for_each_ref(add_ref_tag, NULL);
3344 stop_progress(&progress_state);
3346 if (non_empty && !nr_result)
3349 prepare_pack(window, depth);
3352 fprintf(stderr, "Total %"PRIu32" (delta %"PRIu32"),"
3353 " reused %"PRIu32" (delta %"PRIu32")\n",
3354 written, written_delta, reused, reused_delta);