Merge branch 'en/header-cleanup' into maint-2.43
[git.git] / builtin / pack-objects.c
blob5c8bfe1035feca3518d2ab6b2515c03adf9d75eb
1 #include "builtin.h"
2 #include "environment.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "repository.h"
6 #include "config.h"
7 #include "attr.h"
8 #include "object.h"
9 #include "commit.h"
10 #include "tag.h"
11 #include "delta.h"
12 #include "pack.h"
13 #include "pack-revindex.h"
14 #include "csum-file.h"
15 #include "tree-walk.h"
16 #include "diff.h"
17 #include "revision.h"
18 #include "list-objects.h"
19 #include "list-objects-filter-options.h"
20 #include "pack-objects.h"
21 #include "progress.h"
22 #include "refs.h"
23 #include "streaming.h"
24 #include "thread-utils.h"
25 #include "pack-bitmap.h"
26 #include "delta-islands.h"
27 #include "reachable.h"
28 #include "oid-array.h"
29 #include "strvec.h"
30 #include "list.h"
31 #include "packfile.h"
32 #include "object-file.h"
33 #include "object-store-ll.h"
34 #include "replace-object.h"
35 #include "dir.h"
36 #include "midx.h"
37 #include "trace2.h"
38 #include "shallow.h"
39 #include "promisor-remote.h"
40 #include "pack-mtimes.h"
41 #include "parse-options.h"
44 * Objects we are going to pack are collected in the `to_pack` structure.
45 * It contains an array (dynamically expanded) of the object data, and a map
46 * that can resolve SHA1s to their position in the array.
48 static struct packing_data to_pack;
50 static inline struct object_entry *oe_delta(
51 const struct packing_data *pack,
52 const struct object_entry *e)
54 if (!e->delta_idx)
55 return NULL;
56 if (e->ext_base)
57 return &pack->ext_bases[e->delta_idx - 1];
58 else
59 return &pack->objects[e->delta_idx - 1];
62 static inline unsigned long oe_delta_size(struct packing_data *pack,
63 const struct object_entry *e)
65 if (e->delta_size_valid)
66 return e->delta_size_;
69 * pack->delta_size[] can't be NULL because oe_set_delta_size()
70 * must have been called when a new delta is saved with
71 * oe_set_delta().
72 * If oe_delta() returns NULL (i.e. default state, which means
73 * delta_size_valid is also false), then the caller must never
74 * call oe_delta_size().
76 return pack->delta_size[e - pack->objects];
79 unsigned long oe_get_size_slow(struct packing_data *pack,
80 const struct object_entry *e);
82 static inline unsigned long oe_size(struct packing_data *pack,
83 const struct object_entry *e)
85 if (e->size_valid)
86 return e->size_;
88 return oe_get_size_slow(pack, e);
91 static inline void oe_set_delta(struct packing_data *pack,
92 struct object_entry *e,
93 struct object_entry *delta)
95 if (delta)
96 e->delta_idx = (delta - pack->objects) + 1;
97 else
98 e->delta_idx = 0;
101 static inline struct object_entry *oe_delta_sibling(
102 const struct packing_data *pack,
103 const struct object_entry *e)
105 if (e->delta_sibling_idx)
106 return &pack->objects[e->delta_sibling_idx - 1];
107 return NULL;
110 static inline struct object_entry *oe_delta_child(
111 const struct packing_data *pack,
112 const struct object_entry *e)
114 if (e->delta_child_idx)
115 return &pack->objects[e->delta_child_idx - 1];
116 return NULL;
119 static inline void oe_set_delta_child(struct packing_data *pack,
120 struct object_entry *e,
121 struct object_entry *delta)
123 if (delta)
124 e->delta_child_idx = (delta - pack->objects) + 1;
125 else
126 e->delta_child_idx = 0;
129 static inline void oe_set_delta_sibling(struct packing_data *pack,
130 struct object_entry *e,
131 struct object_entry *delta)
133 if (delta)
134 e->delta_sibling_idx = (delta - pack->objects) + 1;
135 else
136 e->delta_sibling_idx = 0;
139 static inline void oe_set_size(struct packing_data *pack,
140 struct object_entry *e,
141 unsigned long size)
143 if (size < pack->oe_size_limit) {
144 e->size_ = size;
145 e->size_valid = 1;
146 } else {
147 e->size_valid = 0;
148 if (oe_get_size_slow(pack, e) != size)
149 BUG("'size' is supposed to be the object size!");
153 static inline void oe_set_delta_size(struct packing_data *pack,
154 struct object_entry *e,
155 unsigned long size)
157 if (size < pack->oe_delta_size_limit) {
158 e->delta_size_ = size;
159 e->delta_size_valid = 1;
160 } else {
161 packing_data_lock(pack);
162 if (!pack->delta_size)
163 ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
164 packing_data_unlock(pack);
166 pack->delta_size[e - pack->objects] = size;
167 e->delta_size_valid = 0;
171 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
172 #define SIZE(obj) oe_size(&to_pack, obj)
173 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
174 #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
175 #define DELTA(obj) oe_delta(&to_pack, obj)
176 #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
177 #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
178 #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
179 #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
180 #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
181 #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
182 #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
184 static const char *pack_usage[] = {
185 N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"),
186 N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"),
187 NULL
190 static struct pack_idx_entry **written_list;
191 static uint32_t nr_result, nr_written, nr_seen;
192 static struct bitmap_index *bitmap_git;
193 static uint32_t write_layer;
195 static int non_empty;
196 static int reuse_delta = 1, reuse_object = 1;
197 static int keep_unreachable, unpack_unreachable, include_tag;
198 static timestamp_t unpack_unreachable_expiration;
199 static int pack_loose_unreachable;
200 static int cruft;
201 static timestamp_t cruft_expiration;
202 static int local;
203 static int have_non_local_packs;
204 static int incremental;
205 static int ignore_packed_keep_on_disk;
206 static int ignore_packed_keep_in_core;
207 static int allow_ofs_delta;
208 static struct pack_idx_option pack_idx_opts;
209 static const char *base_name;
210 static int progress = 1;
211 static int window = 10;
212 static unsigned long pack_size_limit;
213 static int depth = 50;
214 static int delta_search_threads;
215 static int pack_to_stdout;
216 static int sparse;
217 static int thin;
218 static int num_preferred_base;
219 static struct progress *progress_state;
221 static struct packed_git *reuse_packfile;
222 static uint32_t reuse_packfile_objects;
223 static struct bitmap *reuse_packfile_bitmap;
225 static int use_bitmap_index_default = 1;
226 static int use_bitmap_index = -1;
227 static int allow_pack_reuse = 1;
228 static enum {
229 WRITE_BITMAP_FALSE = 0,
230 WRITE_BITMAP_QUIET,
231 WRITE_BITMAP_TRUE,
232 } write_bitmap_index;
233 static uint16_t write_bitmap_options = BITMAP_OPT_HASH_CACHE;
235 static int exclude_promisor_objects;
237 static int use_delta_islands;
239 static unsigned long delta_cache_size = 0;
240 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
241 static unsigned long cache_max_small_delta_size = 1000;
243 static unsigned long window_memory_limit = 0;
245 static struct string_list uri_protocols = STRING_LIST_INIT_NODUP;
247 enum missing_action {
248 MA_ERROR = 0, /* fail if any missing objects are encountered */
249 MA_ALLOW_ANY, /* silently allow ALL missing objects */
250 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
252 static enum missing_action arg_missing_action;
253 static show_object_fn fn_show_object;
255 struct configured_exclusion {
256 struct oidmap_entry e;
257 char *pack_hash_hex;
258 char *uri;
260 static struct oidmap configured_exclusions;
262 static struct oidset excluded_by_config;
265 * stats
267 static uint32_t written, written_delta;
268 static uint32_t reused, reused_delta;
271 * Indexed commits
273 static struct commit **indexed_commits;
274 static unsigned int indexed_commits_nr;
275 static unsigned int indexed_commits_alloc;
277 static void index_commit_for_bitmap(struct commit *commit)
279 if (indexed_commits_nr >= indexed_commits_alloc) {
280 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
281 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
284 indexed_commits[indexed_commits_nr++] = commit;
287 static void *get_delta(struct object_entry *entry)
289 unsigned long size, base_size, delta_size;
290 void *buf, *base_buf, *delta_buf;
291 enum object_type type;
293 buf = repo_read_object_file(the_repository, &entry->idx.oid, &type,
294 &size);
295 if (!buf)
296 die(_("unable to read %s"), oid_to_hex(&entry->idx.oid));
297 base_buf = repo_read_object_file(the_repository,
298 &DELTA(entry)->idx.oid, &type,
299 &base_size);
300 if (!base_buf)
301 die("unable to read %s",
302 oid_to_hex(&DELTA(entry)->idx.oid));
303 delta_buf = diff_delta(base_buf, base_size,
304 buf, size, &delta_size, 0);
306 * We successfully computed this delta once but dropped it for
307 * memory reasons. Something is very wrong if this time we
308 * recompute and create a different delta.
310 if (!delta_buf || delta_size != DELTA_SIZE(entry))
311 BUG("delta size changed");
312 free(buf);
313 free(base_buf);
314 return delta_buf;
317 static unsigned long do_compress(void **pptr, unsigned long size)
319 git_zstream stream;
320 void *in, *out;
321 unsigned long maxsize;
323 git_deflate_init(&stream, pack_compression_level);
324 maxsize = git_deflate_bound(&stream, size);
326 in = *pptr;
327 out = xmalloc(maxsize);
328 *pptr = out;
330 stream.next_in = in;
331 stream.avail_in = size;
332 stream.next_out = out;
333 stream.avail_out = maxsize;
334 while (git_deflate(&stream, Z_FINISH) == Z_OK)
335 ; /* nothing */
336 git_deflate_end(&stream);
338 free(in);
339 return stream.total_out;
342 static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f,
343 const struct object_id *oid)
345 git_zstream stream;
346 unsigned char ibuf[1024 * 16];
347 unsigned char obuf[1024 * 16];
348 unsigned long olen = 0;
350 git_deflate_init(&stream, pack_compression_level);
352 for (;;) {
353 ssize_t readlen;
354 int zret = Z_OK;
355 readlen = read_istream(st, ibuf, sizeof(ibuf));
356 if (readlen == -1)
357 die(_("unable to read %s"), oid_to_hex(oid));
359 stream.next_in = ibuf;
360 stream.avail_in = readlen;
361 while ((stream.avail_in || readlen == 0) &&
362 (zret == Z_OK || zret == Z_BUF_ERROR)) {
363 stream.next_out = obuf;
364 stream.avail_out = sizeof(obuf);
365 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
366 hashwrite(f, obuf, stream.next_out - obuf);
367 olen += stream.next_out - obuf;
369 if (stream.avail_in)
370 die(_("deflate error (%d)"), zret);
371 if (readlen == 0) {
372 if (zret != Z_STREAM_END)
373 die(_("deflate error (%d)"), zret);
374 break;
377 git_deflate_end(&stream);
378 return olen;
382 * we are going to reuse the existing object data as is. make
383 * sure it is not corrupt.
385 static int check_pack_inflate(struct packed_git *p,
386 struct pack_window **w_curs,
387 off_t offset,
388 off_t len,
389 unsigned long expect)
391 git_zstream stream;
392 unsigned char fakebuf[4096], *in;
393 int st;
395 memset(&stream, 0, sizeof(stream));
396 git_inflate_init(&stream);
397 do {
398 in = use_pack(p, w_curs, offset, &stream.avail_in);
399 stream.next_in = in;
400 stream.next_out = fakebuf;
401 stream.avail_out = sizeof(fakebuf);
402 st = git_inflate(&stream, Z_FINISH);
403 offset += stream.next_in - in;
404 } while (st == Z_OK || st == Z_BUF_ERROR);
405 git_inflate_end(&stream);
406 return (st == Z_STREAM_END &&
407 stream.total_out == expect &&
408 stream.total_in == len) ? 0 : -1;
411 static void copy_pack_data(struct hashfile *f,
412 struct packed_git *p,
413 struct pack_window **w_curs,
414 off_t offset,
415 off_t len)
417 unsigned char *in;
418 unsigned long avail;
420 while (len) {
421 in = use_pack(p, w_curs, offset, &avail);
422 if (avail > len)
423 avail = (unsigned long)len;
424 hashwrite(f, in, avail);
425 offset += avail;
426 len -= avail;
430 static inline int oe_size_greater_than(struct packing_data *pack,
431 const struct object_entry *lhs,
432 unsigned long rhs)
434 if (lhs->size_valid)
435 return lhs->size_ > rhs;
436 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
437 return 1;
438 return oe_get_size_slow(pack, lhs) > rhs;
441 /* Return 0 if we will bust the pack-size limit */
442 static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
443 unsigned long limit, int usable_delta)
445 unsigned long size, datalen;
446 unsigned char header[MAX_PACK_OBJECT_HEADER],
447 dheader[MAX_PACK_OBJECT_HEADER];
448 unsigned hdrlen;
449 enum object_type type;
450 void *buf;
451 struct git_istream *st = NULL;
452 const unsigned hashsz = the_hash_algo->rawsz;
454 if (!usable_delta) {
455 if (oe_type(entry) == OBJ_BLOB &&
456 oe_size_greater_than(&to_pack, entry, big_file_threshold) &&
457 (st = open_istream(the_repository, &entry->idx.oid, &type,
458 &size, NULL)) != NULL)
459 buf = NULL;
460 else {
461 buf = repo_read_object_file(the_repository,
462 &entry->idx.oid, &type,
463 &size);
464 if (!buf)
465 die(_("unable to read %s"),
466 oid_to_hex(&entry->idx.oid));
469 * make sure no cached delta data remains from a
470 * previous attempt before a pack split occurred.
472 FREE_AND_NULL(entry->delta_data);
473 entry->z_delta_size = 0;
474 } else if (entry->delta_data) {
475 size = DELTA_SIZE(entry);
476 buf = entry->delta_data;
477 entry->delta_data = NULL;
478 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
479 OBJ_OFS_DELTA : OBJ_REF_DELTA;
480 } else {
481 buf = get_delta(entry);
482 size = DELTA_SIZE(entry);
483 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
484 OBJ_OFS_DELTA : OBJ_REF_DELTA;
487 if (st) /* large blob case, just assume we don't compress well */
488 datalen = size;
489 else if (entry->z_delta_size)
490 datalen = entry->z_delta_size;
491 else
492 datalen = do_compress(&buf, size);
495 * The object header is a byte of 'type' followed by zero or
496 * more bytes of length.
498 hdrlen = encode_in_pack_object_header(header, sizeof(header),
499 type, size);
501 if (type == OBJ_OFS_DELTA) {
503 * Deltas with relative base contain an additional
504 * encoding of the relative offset for the delta
505 * base from this object's position in the pack.
507 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
508 unsigned pos = sizeof(dheader) - 1;
509 dheader[pos] = ofs & 127;
510 while (ofs >>= 7)
511 dheader[--pos] = 128 | (--ofs & 127);
512 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
513 if (st)
514 close_istream(st);
515 free(buf);
516 return 0;
518 hashwrite(f, header, hdrlen);
519 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
520 hdrlen += sizeof(dheader) - pos;
521 } else if (type == OBJ_REF_DELTA) {
523 * Deltas with a base reference contain
524 * additional bytes for the base object ID.
526 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
527 if (st)
528 close_istream(st);
529 free(buf);
530 return 0;
532 hashwrite(f, header, hdrlen);
533 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
534 hdrlen += hashsz;
535 } else {
536 if (limit && hdrlen + datalen + hashsz >= limit) {
537 if (st)
538 close_istream(st);
539 free(buf);
540 return 0;
542 hashwrite(f, header, hdrlen);
544 if (st) {
545 datalen = write_large_blob_data(st, f, &entry->idx.oid);
546 close_istream(st);
547 } else {
548 hashwrite(f, buf, datalen);
549 free(buf);
552 return hdrlen + datalen;
555 /* Return 0 if we will bust the pack-size limit */
556 static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
557 unsigned long limit, int usable_delta)
559 struct packed_git *p = IN_PACK(entry);
560 struct pack_window *w_curs = NULL;
561 uint32_t pos;
562 off_t offset;
563 enum object_type type = oe_type(entry);
564 off_t datalen;
565 unsigned char header[MAX_PACK_OBJECT_HEADER],
566 dheader[MAX_PACK_OBJECT_HEADER];
567 unsigned hdrlen;
568 const unsigned hashsz = the_hash_algo->rawsz;
569 unsigned long entry_size = SIZE(entry);
571 if (DELTA(entry))
572 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
573 OBJ_OFS_DELTA : OBJ_REF_DELTA;
574 hdrlen = encode_in_pack_object_header(header, sizeof(header),
575 type, entry_size);
577 offset = entry->in_pack_offset;
578 if (offset_to_pack_pos(p, offset, &pos) < 0)
579 die(_("write_reuse_object: could not locate %s, expected at "
580 "offset %"PRIuMAX" in pack %s"),
581 oid_to_hex(&entry->idx.oid), (uintmax_t)offset,
582 p->pack_name);
583 datalen = pack_pos_to_offset(p, pos + 1) - offset;
584 if (!pack_to_stdout && p->index_version > 1 &&
585 check_pack_crc(p, &w_curs, offset, datalen,
586 pack_pos_to_index(p, pos))) {
587 error(_("bad packed object CRC for %s"),
588 oid_to_hex(&entry->idx.oid));
589 unuse_pack(&w_curs);
590 return write_no_reuse_object(f, entry, limit, usable_delta);
593 offset += entry->in_pack_header_size;
594 datalen -= entry->in_pack_header_size;
596 if (!pack_to_stdout && p->index_version == 1 &&
597 check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) {
598 error(_("corrupt packed object for %s"),
599 oid_to_hex(&entry->idx.oid));
600 unuse_pack(&w_curs);
601 return write_no_reuse_object(f, entry, limit, usable_delta);
604 if (type == OBJ_OFS_DELTA) {
605 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
606 unsigned pos = sizeof(dheader) - 1;
607 dheader[pos] = ofs & 127;
608 while (ofs >>= 7)
609 dheader[--pos] = 128 | (--ofs & 127);
610 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
611 unuse_pack(&w_curs);
612 return 0;
614 hashwrite(f, header, hdrlen);
615 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
616 hdrlen += sizeof(dheader) - pos;
617 reused_delta++;
618 } else if (type == OBJ_REF_DELTA) {
619 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
620 unuse_pack(&w_curs);
621 return 0;
623 hashwrite(f, header, hdrlen);
624 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
625 hdrlen += hashsz;
626 reused_delta++;
627 } else {
628 if (limit && hdrlen + datalen + hashsz >= limit) {
629 unuse_pack(&w_curs);
630 return 0;
632 hashwrite(f, header, hdrlen);
634 copy_pack_data(f, p, &w_curs, offset, datalen);
635 unuse_pack(&w_curs);
636 reused++;
637 return hdrlen + datalen;
640 /* Return 0 if we will bust the pack-size limit */
641 static off_t write_object(struct hashfile *f,
642 struct object_entry *entry,
643 off_t write_offset)
645 unsigned long limit;
646 off_t len;
647 int usable_delta, to_reuse;
649 if (!pack_to_stdout)
650 crc32_begin(f);
652 /* apply size limit if limited packsize and not first object */
653 if (!pack_size_limit || !nr_written)
654 limit = 0;
655 else if (pack_size_limit <= write_offset)
657 * the earlier object did not fit the limit; avoid
658 * mistaking this with unlimited (i.e. limit = 0).
660 limit = 1;
661 else
662 limit = pack_size_limit - write_offset;
664 if (!DELTA(entry))
665 usable_delta = 0; /* no delta */
666 else if (!pack_size_limit)
667 usable_delta = 1; /* unlimited packfile */
668 else if (DELTA(entry)->idx.offset == (off_t)-1)
669 usable_delta = 0; /* base was written to another pack */
670 else if (DELTA(entry)->idx.offset)
671 usable_delta = 1; /* base already exists in this pack */
672 else
673 usable_delta = 0; /* base could end up in another pack */
675 if (!reuse_object)
676 to_reuse = 0; /* explicit */
677 else if (!IN_PACK(entry))
678 to_reuse = 0; /* can't reuse what we don't have */
679 else if (oe_type(entry) == OBJ_REF_DELTA ||
680 oe_type(entry) == OBJ_OFS_DELTA)
681 /* check_object() decided it for us ... */
682 to_reuse = usable_delta;
683 /* ... but pack split may override that */
684 else if (oe_type(entry) != entry->in_pack_type)
685 to_reuse = 0; /* pack has delta which is unusable */
686 else if (DELTA(entry))
687 to_reuse = 0; /* we want to pack afresh */
688 else
689 to_reuse = 1; /* we have it in-pack undeltified,
690 * and we do not need to deltify it.
693 if (!to_reuse)
694 len = write_no_reuse_object(f, entry, limit, usable_delta);
695 else
696 len = write_reuse_object(f, entry, limit, usable_delta);
697 if (!len)
698 return 0;
700 if (usable_delta)
701 written_delta++;
702 written++;
703 if (!pack_to_stdout)
704 entry->idx.crc32 = crc32_end(f);
705 return len;
708 enum write_one_status {
709 WRITE_ONE_SKIP = -1, /* already written */
710 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
711 WRITE_ONE_WRITTEN = 1, /* normal */
712 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
715 static enum write_one_status write_one(struct hashfile *f,
716 struct object_entry *e,
717 off_t *offset)
719 off_t size;
720 int recursing;
723 * we set offset to 1 (which is an impossible value) to mark
724 * the fact that this object is involved in "write its base
725 * first before writing a deltified object" recursion.
727 recursing = (e->idx.offset == 1);
728 if (recursing) {
729 warning(_("recursive delta detected for object %s"),
730 oid_to_hex(&e->idx.oid));
731 return WRITE_ONE_RECURSIVE;
732 } else if (e->idx.offset || e->preferred_base) {
733 /* offset is non zero if object is written already. */
734 return WRITE_ONE_SKIP;
737 /* if we are deltified, write out base object first. */
738 if (DELTA(e)) {
739 e->idx.offset = 1; /* now recurse */
740 switch (write_one(f, DELTA(e), offset)) {
741 case WRITE_ONE_RECURSIVE:
742 /* we cannot depend on this one */
743 SET_DELTA(e, NULL);
744 break;
745 default:
746 break;
747 case WRITE_ONE_BREAK:
748 e->idx.offset = recursing;
749 return WRITE_ONE_BREAK;
753 e->idx.offset = *offset;
754 size = write_object(f, e, *offset);
755 if (!size) {
756 e->idx.offset = recursing;
757 return WRITE_ONE_BREAK;
759 written_list[nr_written++] = &e->idx;
761 /* make sure off_t is sufficiently large not to wrap */
762 if (signed_add_overflows(*offset, size))
763 die(_("pack too large for current definition of off_t"));
764 *offset += size;
765 return WRITE_ONE_WRITTEN;
768 static int mark_tagged(const char *path UNUSED, const struct object_id *oid,
769 int flag UNUSED, void *cb_data UNUSED)
771 struct object_id peeled;
772 struct object_entry *entry = packlist_find(&to_pack, oid);
774 if (entry)
775 entry->tagged = 1;
776 if (!peel_iterated_oid(oid, &peeled)) {
777 entry = packlist_find(&to_pack, &peeled);
778 if (entry)
779 entry->tagged = 1;
781 return 0;
784 static inline unsigned char oe_layer(struct packing_data *pack,
785 struct object_entry *e)
787 if (!pack->layer)
788 return 0;
789 return pack->layer[e - pack->objects];
792 static inline void add_to_write_order(struct object_entry **wo,
793 unsigned int *endp,
794 struct object_entry *e)
796 if (e->filled || oe_layer(&to_pack, e) != write_layer)
797 return;
798 wo[(*endp)++] = e;
799 e->filled = 1;
802 static void add_descendants_to_write_order(struct object_entry **wo,
803 unsigned int *endp,
804 struct object_entry *e)
806 int add_to_order = 1;
807 while (e) {
808 if (add_to_order) {
809 struct object_entry *s;
810 /* add this node... */
811 add_to_write_order(wo, endp, e);
812 /* all its siblings... */
813 for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
814 add_to_write_order(wo, endp, s);
817 /* drop down a level to add left subtree nodes if possible */
818 if (DELTA_CHILD(e)) {
819 add_to_order = 1;
820 e = DELTA_CHILD(e);
821 } else {
822 add_to_order = 0;
823 /* our sibling might have some children, it is next */
824 if (DELTA_SIBLING(e)) {
825 e = DELTA_SIBLING(e);
826 continue;
828 /* go back to our parent node */
829 e = DELTA(e);
830 while (e && !DELTA_SIBLING(e)) {
831 /* we're on the right side of a subtree, keep
832 * going up until we can go right again */
833 e = DELTA(e);
835 if (!e) {
836 /* done- we hit our original root node */
837 return;
839 /* pass it off to sibling at this level */
840 e = DELTA_SIBLING(e);
845 static void add_family_to_write_order(struct object_entry **wo,
846 unsigned int *endp,
847 struct object_entry *e)
849 struct object_entry *root;
851 for (root = e; DELTA(root); root = DELTA(root))
852 ; /* nothing */
853 add_descendants_to_write_order(wo, endp, root);
856 static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end)
858 unsigned int i, last_untagged;
859 struct object_entry *objects = to_pack.objects;
861 for (i = 0; i < to_pack.nr_objects; i++) {
862 if (objects[i].tagged)
863 break;
864 add_to_write_order(wo, wo_end, &objects[i]);
866 last_untagged = i;
869 * Then fill all the tagged tips.
871 for (; i < to_pack.nr_objects; i++) {
872 if (objects[i].tagged)
873 add_to_write_order(wo, wo_end, &objects[i]);
877 * And then all remaining commits and tags.
879 for (i = last_untagged; i < to_pack.nr_objects; i++) {
880 if (oe_type(&objects[i]) != OBJ_COMMIT &&
881 oe_type(&objects[i]) != OBJ_TAG)
882 continue;
883 add_to_write_order(wo, wo_end, &objects[i]);
887 * And then all the trees.
889 for (i = last_untagged; i < to_pack.nr_objects; i++) {
890 if (oe_type(&objects[i]) != OBJ_TREE)
891 continue;
892 add_to_write_order(wo, wo_end, &objects[i]);
896 * Finally all the rest in really tight order
898 for (i = last_untagged; i < to_pack.nr_objects; i++) {
899 if (!objects[i].filled && oe_layer(&to_pack, &objects[i]) == write_layer)
900 add_family_to_write_order(wo, wo_end, &objects[i]);
904 static struct object_entry **compute_write_order(void)
906 uint32_t max_layers = 1;
907 unsigned int i, wo_end;
909 struct object_entry **wo;
910 struct object_entry *objects = to_pack.objects;
912 for (i = 0; i < to_pack.nr_objects; i++) {
913 objects[i].tagged = 0;
914 objects[i].filled = 0;
915 SET_DELTA_CHILD(&objects[i], NULL);
916 SET_DELTA_SIBLING(&objects[i], NULL);
920 * Fully connect delta_child/delta_sibling network.
921 * Make sure delta_sibling is sorted in the original
922 * recency order.
924 for (i = to_pack.nr_objects; i > 0;) {
925 struct object_entry *e = &objects[--i];
926 if (!DELTA(e))
927 continue;
928 /* Mark me as the first child */
929 e->delta_sibling_idx = DELTA(e)->delta_child_idx;
930 SET_DELTA_CHILD(DELTA(e), e);
934 * Mark objects that are at the tip of tags.
936 for_each_tag_ref(mark_tagged, NULL);
938 if (use_delta_islands) {
939 max_layers = compute_pack_layers(&to_pack);
940 free_island_marks();
943 ALLOC_ARRAY(wo, to_pack.nr_objects);
944 wo_end = 0;
946 for (; write_layer < max_layers; ++write_layer)
947 compute_layer_order(wo, &wo_end);
949 if (wo_end != to_pack.nr_objects)
950 die(_("ordered %u objects, expected %"PRIu32),
951 wo_end, to_pack.nr_objects);
953 return wo;
958 * A reused set of objects. All objects in a chunk have the same
959 * relative position in the original packfile and the generated
960 * packfile.
963 static struct reused_chunk {
964 /* The offset of the first object of this chunk in the original
965 * packfile. */
966 off_t original;
967 /* The difference for "original" minus the offset of the first object of
968 * this chunk in the generated packfile. */
969 off_t difference;
970 } *reused_chunks;
971 static int reused_chunks_nr;
972 static int reused_chunks_alloc;
974 static void record_reused_object(off_t where, off_t offset)
976 if (reused_chunks_nr && reused_chunks[reused_chunks_nr-1].difference == offset)
977 return;
979 ALLOC_GROW(reused_chunks, reused_chunks_nr + 1,
980 reused_chunks_alloc);
981 reused_chunks[reused_chunks_nr].original = where;
982 reused_chunks[reused_chunks_nr].difference = offset;
983 reused_chunks_nr++;
987 * Binary search to find the chunk that "where" is in. Note
988 * that we're not looking for an exact match, just the first
989 * chunk that contains it (which implicitly ends at the start
990 * of the next chunk.
992 static off_t find_reused_offset(off_t where)
994 int lo = 0, hi = reused_chunks_nr;
995 while (lo < hi) {
996 int mi = lo + ((hi - lo) / 2);
997 if (where == reused_chunks[mi].original)
998 return reused_chunks[mi].difference;
999 if (where < reused_chunks[mi].original)
1000 hi = mi;
1001 else
1002 lo = mi + 1;
1006 * The first chunk starts at zero, so we can't have gone below
1007 * there.
1009 assert(lo);
1010 return reused_chunks[lo-1].difference;
1013 static void write_reused_pack_one(size_t pos, struct hashfile *out,
1014 struct pack_window **w_curs)
1016 off_t offset, next, cur;
1017 enum object_type type;
1018 unsigned long size;
1020 offset = pack_pos_to_offset(reuse_packfile, pos);
1021 next = pack_pos_to_offset(reuse_packfile, pos + 1);
1023 record_reused_object(offset, offset - hashfile_total(out));
1025 cur = offset;
1026 type = unpack_object_header(reuse_packfile, w_curs, &cur, &size);
1027 assert(type >= 0);
1029 if (type == OBJ_OFS_DELTA) {
1030 off_t base_offset;
1031 off_t fixup;
1033 unsigned char header[MAX_PACK_OBJECT_HEADER];
1034 unsigned len;
1036 base_offset = get_delta_base(reuse_packfile, w_curs, &cur, type, offset);
1037 assert(base_offset != 0);
1039 /* Convert to REF_DELTA if we must... */
1040 if (!allow_ofs_delta) {
1041 uint32_t base_pos;
1042 struct object_id base_oid;
1044 if (offset_to_pack_pos(reuse_packfile, base_offset, &base_pos) < 0)
1045 die(_("expected object at offset %"PRIuMAX" "
1046 "in pack %s"),
1047 (uintmax_t)base_offset,
1048 reuse_packfile->pack_name);
1050 nth_packed_object_id(&base_oid, reuse_packfile,
1051 pack_pos_to_index(reuse_packfile, base_pos));
1053 len = encode_in_pack_object_header(header, sizeof(header),
1054 OBJ_REF_DELTA, size);
1055 hashwrite(out, header, len);
1056 hashwrite(out, base_oid.hash, the_hash_algo->rawsz);
1057 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1058 return;
1061 /* Otherwise see if we need to rewrite the offset... */
1062 fixup = find_reused_offset(offset) -
1063 find_reused_offset(base_offset);
1064 if (fixup) {
1065 unsigned char ofs_header[10];
1066 unsigned i, ofs_len;
1067 off_t ofs = offset - base_offset - fixup;
1069 len = encode_in_pack_object_header(header, sizeof(header),
1070 OBJ_OFS_DELTA, size);
1072 i = sizeof(ofs_header) - 1;
1073 ofs_header[i] = ofs & 127;
1074 while (ofs >>= 7)
1075 ofs_header[--i] = 128 | (--ofs & 127);
1077 ofs_len = sizeof(ofs_header) - i;
1079 hashwrite(out, header, len);
1080 hashwrite(out, ofs_header + sizeof(ofs_header) - ofs_len, ofs_len);
1081 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1082 return;
1085 /* ...otherwise we have no fixup, and can write it verbatim */
1088 copy_pack_data(out, reuse_packfile, w_curs, offset, next - offset);
1091 static size_t write_reused_pack_verbatim(struct hashfile *out,
1092 struct pack_window **w_curs)
1094 size_t pos = 0;
1096 while (pos < reuse_packfile_bitmap->word_alloc &&
1097 reuse_packfile_bitmap->words[pos] == (eword_t)~0)
1098 pos++;
1100 if (pos) {
1101 off_t to_write;
1103 written = (pos * BITS_IN_EWORD);
1104 to_write = pack_pos_to_offset(reuse_packfile, written)
1105 - sizeof(struct pack_header);
1107 /* We're recording one chunk, not one object. */
1108 record_reused_object(sizeof(struct pack_header), 0);
1109 hashflush(out);
1110 copy_pack_data(out, reuse_packfile, w_curs,
1111 sizeof(struct pack_header), to_write);
1113 display_progress(progress_state, written);
1115 return pos;
1118 static void write_reused_pack(struct hashfile *f)
1120 size_t i = 0;
1121 uint32_t offset;
1122 struct pack_window *w_curs = NULL;
1124 if (allow_ofs_delta)
1125 i = write_reused_pack_verbatim(f, &w_curs);
1127 for (; i < reuse_packfile_bitmap->word_alloc; ++i) {
1128 eword_t word = reuse_packfile_bitmap->words[i];
1129 size_t pos = (i * BITS_IN_EWORD);
1131 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1132 if ((word >> offset) == 0)
1133 break;
1135 offset += ewah_bit_ctz64(word >> offset);
1137 * Can use bit positions directly, even for MIDX
1138 * bitmaps. See comment in try_partial_reuse()
1139 * for why.
1141 write_reused_pack_one(pos + offset, f, &w_curs);
1142 display_progress(progress_state, ++written);
1146 unuse_pack(&w_curs);
1149 static void write_excluded_by_configs(void)
1151 struct oidset_iter iter;
1152 const struct object_id *oid;
1154 oidset_iter_init(&excluded_by_config, &iter);
1155 while ((oid = oidset_iter_next(&iter))) {
1156 struct configured_exclusion *ex =
1157 oidmap_get(&configured_exclusions, oid);
1159 if (!ex)
1160 BUG("configured exclusion wasn't configured");
1161 write_in_full(1, ex->pack_hash_hex, strlen(ex->pack_hash_hex));
1162 write_in_full(1, " ", 1);
1163 write_in_full(1, ex->uri, strlen(ex->uri));
1164 write_in_full(1, "\n", 1);
1168 static const char no_split_warning[] = N_(
1169 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
1172 static void write_pack_file(void)
1174 uint32_t i = 0, j;
1175 struct hashfile *f;
1176 off_t offset;
1177 uint32_t nr_remaining = nr_result;
1178 time_t last_mtime = 0;
1179 struct object_entry **write_order;
1181 if (progress > pack_to_stdout)
1182 progress_state = start_progress(_("Writing objects"), nr_result);
1183 ALLOC_ARRAY(written_list, to_pack.nr_objects);
1184 write_order = compute_write_order();
1186 do {
1187 unsigned char hash[GIT_MAX_RAWSZ];
1188 char *pack_tmp_name = NULL;
1190 if (pack_to_stdout)
1191 f = hashfd_throughput(1, "<stdout>", progress_state);
1192 else
1193 f = create_tmp_packfile(&pack_tmp_name);
1195 offset = write_pack_header(f, nr_remaining);
1197 if (reuse_packfile) {
1198 assert(pack_to_stdout);
1199 write_reused_pack(f);
1200 offset = hashfile_total(f);
1203 nr_written = 0;
1204 for (; i < to_pack.nr_objects; i++) {
1205 struct object_entry *e = write_order[i];
1206 if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
1207 break;
1208 display_progress(progress_state, written);
1211 if (pack_to_stdout) {
1213 * We never fsync when writing to stdout since we may
1214 * not be writing to an actual pack file. For instance,
1215 * the upload-pack code passes a pipe here. Calling
1216 * fsync on a pipe results in unnecessary
1217 * synchronization with the reader on some platforms.
1219 finalize_hashfile(f, hash, FSYNC_COMPONENT_NONE,
1220 CSUM_HASH_IN_STREAM | CSUM_CLOSE);
1221 } else if (nr_written == nr_remaining) {
1222 finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK,
1223 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
1224 } else {
1226 * If we wrote the wrong number of entries in the
1227 * header, rewrite it like in fast-import.
1230 int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0);
1231 fixup_pack_header_footer(fd, hash, pack_tmp_name,
1232 nr_written, hash, offset);
1233 close(fd);
1234 if (write_bitmap_index) {
1235 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1236 warning(_(no_split_warning));
1237 write_bitmap_index = 0;
1241 if (!pack_to_stdout) {
1242 struct stat st;
1243 struct strbuf tmpname = STRBUF_INIT;
1244 char *idx_tmp_name = NULL;
1247 * Packs are runtime accessed in their mtime
1248 * order since newer packs are more likely to contain
1249 * younger objects. So if we are creating multiple
1250 * packs then we should modify the mtime of later ones
1251 * to preserve this property.
1253 if (stat(pack_tmp_name, &st) < 0) {
1254 warning_errno(_("failed to stat %s"), pack_tmp_name);
1255 } else if (!last_mtime) {
1256 last_mtime = st.st_mtime;
1257 } else {
1258 struct utimbuf utb;
1259 utb.actime = st.st_atime;
1260 utb.modtime = --last_mtime;
1261 if (utime(pack_tmp_name, &utb) < 0)
1262 warning_errno(_("failed utime() on %s"), pack_tmp_name);
1265 strbuf_addf(&tmpname, "%s-%s.", base_name,
1266 hash_to_hex(hash));
1268 if (write_bitmap_index) {
1269 bitmap_writer_set_checksum(hash);
1270 bitmap_writer_build_type_index(
1271 &to_pack, written_list, nr_written);
1274 if (cruft)
1275 pack_idx_opts.flags |= WRITE_MTIMES;
1277 stage_tmp_packfiles(&tmpname, pack_tmp_name,
1278 written_list, nr_written,
1279 &to_pack, &pack_idx_opts, hash,
1280 &idx_tmp_name);
1282 if (write_bitmap_index) {
1283 size_t tmpname_len = tmpname.len;
1285 strbuf_addstr(&tmpname, "bitmap");
1286 stop_progress(&progress_state);
1288 bitmap_writer_show_progress(progress);
1289 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
1290 if (bitmap_writer_build(&to_pack) < 0)
1291 die(_("failed to write bitmap index"));
1292 bitmap_writer_finish(written_list, nr_written,
1293 tmpname.buf, write_bitmap_options);
1294 write_bitmap_index = 0;
1295 strbuf_setlen(&tmpname, tmpname_len);
1298 rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
1300 free(idx_tmp_name);
1301 strbuf_release(&tmpname);
1302 free(pack_tmp_name);
1303 puts(hash_to_hex(hash));
1306 /* mark written objects as written to previous pack */
1307 for (j = 0; j < nr_written; j++) {
1308 written_list[j]->offset = (off_t)-1;
1310 nr_remaining -= nr_written;
1311 } while (nr_remaining && i < to_pack.nr_objects);
1313 free(written_list);
1314 free(write_order);
1315 stop_progress(&progress_state);
1316 if (written != nr_result)
1317 die(_("wrote %"PRIu32" objects while expecting %"PRIu32),
1318 written, nr_result);
1319 trace2_data_intmax("pack-objects", the_repository,
1320 "write_pack_file/wrote", nr_result);
1323 static int no_try_delta(const char *path)
1325 static struct attr_check *check;
1327 if (!check)
1328 check = attr_check_initl("delta", NULL);
1329 git_check_attr(the_repository->index, path, check);
1330 if (ATTR_FALSE(check->items[0].value))
1331 return 1;
1332 return 0;
1336 * When adding an object, check whether we have already added it
1337 * to our packing list. If so, we can skip. However, if we are
1338 * being asked to excludei t, but the previous mention was to include
1339 * it, make sure to adjust its flags and tweak our numbers accordingly.
1341 * As an optimization, we pass out the index position where we would have
1342 * found the item, since that saves us from having to look it up again a
1343 * few lines later when we want to add the new entry.
1345 static int have_duplicate_entry(const struct object_id *oid,
1346 int exclude)
1348 struct object_entry *entry;
1350 if (reuse_packfile_bitmap &&
1351 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid))
1352 return 1;
1354 entry = packlist_find(&to_pack, oid);
1355 if (!entry)
1356 return 0;
1358 if (exclude) {
1359 if (!entry->preferred_base)
1360 nr_result--;
1361 entry->preferred_base = 1;
1364 return 1;
1367 static int want_found_object(const struct object_id *oid, int exclude,
1368 struct packed_git *p)
1370 if (exclude)
1371 return 1;
1372 if (incremental)
1373 return 0;
1375 if (!is_pack_valid(p))
1376 return -1;
1379 * When asked to do --local (do not include an object that appears in a
1380 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1381 * an object that appears in a pack marked with .keep), finding a pack
1382 * that matches the criteria is sufficient for us to decide to omit it.
1383 * However, even if this pack does not satisfy the criteria, we need to
1384 * make sure no copy of this object appears in _any_ pack that makes us
1385 * to omit the object, so we need to check all the packs.
1387 * We can however first check whether these options can possibly matter;
1388 * if they do not matter we know we want the object in generated pack.
1389 * Otherwise, we signal "-1" at the end to tell the caller that we do
1390 * not know either way, and it needs to check more packs.
1394 * Objects in packs borrowed from elsewhere are discarded regardless of
1395 * if they appear in other packs that weren't borrowed.
1397 if (local && !p->pack_local)
1398 return 0;
1401 * Then handle .keep first, as we have a fast(er) path there.
1403 if (ignore_packed_keep_on_disk || ignore_packed_keep_in_core) {
1405 * Set the flags for the kept-pack cache to be the ones we want
1406 * to ignore.
1408 * That is, if we are ignoring objects in on-disk keep packs,
1409 * then we want to search through the on-disk keep and ignore
1410 * the in-core ones.
1412 unsigned flags = 0;
1413 if (ignore_packed_keep_on_disk)
1414 flags |= ON_DISK_KEEP_PACKS;
1415 if (ignore_packed_keep_in_core)
1416 flags |= IN_CORE_KEEP_PACKS;
1418 if (ignore_packed_keep_on_disk && p->pack_keep)
1419 return 0;
1420 if (ignore_packed_keep_in_core && p->pack_keep_in_core)
1421 return 0;
1422 if (has_object_kept_pack(oid, flags))
1423 return 0;
1427 * At this point we know definitively that either we don't care about
1428 * keep-packs, or the object is not in one. Keep checking other
1429 * conditions...
1431 if (!local || !have_non_local_packs)
1432 return 1;
1434 /* we don't know yet; keep looking for more packs */
1435 return -1;
1438 static int want_object_in_pack_one(struct packed_git *p,
1439 const struct object_id *oid,
1440 int exclude,
1441 struct packed_git **found_pack,
1442 off_t *found_offset)
1444 off_t offset;
1446 if (p == *found_pack)
1447 offset = *found_offset;
1448 else
1449 offset = find_pack_entry_one(oid->hash, p);
1451 if (offset) {
1452 if (!*found_pack) {
1453 if (!is_pack_valid(p))
1454 return -1;
1455 *found_offset = offset;
1456 *found_pack = p;
1458 return want_found_object(oid, exclude, p);
1460 return -1;
1464 * Check whether we want the object in the pack (e.g., we do not want
1465 * objects found in non-local stores if the "--local" option was used).
1467 * If the caller already knows an existing pack it wants to take the object
1468 * from, that is passed in *found_pack and *found_offset; otherwise this
1469 * function finds if there is any pack that has the object and returns the pack
1470 * and its offset in these variables.
1472 static int want_object_in_pack(const struct object_id *oid,
1473 int exclude,
1474 struct packed_git **found_pack,
1475 off_t *found_offset)
1477 int want;
1478 struct list_head *pos;
1479 struct multi_pack_index *m;
1481 if (!exclude && local && has_loose_object_nonlocal(oid))
1482 return 0;
1485 * If we already know the pack object lives in, start checks from that
1486 * pack - in the usual case when neither --local was given nor .keep files
1487 * are present we will determine the answer right now.
1489 if (*found_pack) {
1490 want = want_found_object(oid, exclude, *found_pack);
1491 if (want != -1)
1492 return want;
1494 *found_pack = NULL;
1495 *found_offset = 0;
1498 for (m = get_multi_pack_index(the_repository); m; m = m->next) {
1499 struct pack_entry e;
1500 if (fill_midx_entry(the_repository, oid, &e, m)) {
1501 want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset);
1502 if (want != -1)
1503 return want;
1507 list_for_each(pos, get_packed_git_mru(the_repository)) {
1508 struct packed_git *p = list_entry(pos, struct packed_git, mru);
1509 want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset);
1510 if (!exclude && want > 0)
1511 list_move(&p->mru,
1512 get_packed_git_mru(the_repository));
1513 if (want != -1)
1514 return want;
1517 if (uri_protocols.nr) {
1518 struct configured_exclusion *ex =
1519 oidmap_get(&configured_exclusions, oid);
1520 int i;
1521 const char *p;
1523 if (ex) {
1524 for (i = 0; i < uri_protocols.nr; i++) {
1525 if (skip_prefix(ex->uri,
1526 uri_protocols.items[i].string,
1527 &p) &&
1528 *p == ':') {
1529 oidset_insert(&excluded_by_config, oid);
1530 return 0;
1536 return 1;
1539 static struct object_entry *create_object_entry(const struct object_id *oid,
1540 enum object_type type,
1541 uint32_t hash,
1542 int exclude,
1543 int no_try_delta,
1544 struct packed_git *found_pack,
1545 off_t found_offset)
1547 struct object_entry *entry;
1549 entry = packlist_alloc(&to_pack, oid);
1550 entry->hash = hash;
1551 oe_set_type(entry, type);
1552 if (exclude)
1553 entry->preferred_base = 1;
1554 else
1555 nr_result++;
1556 if (found_pack) {
1557 oe_set_in_pack(&to_pack, entry, found_pack);
1558 entry->in_pack_offset = found_offset;
1561 entry->no_try_delta = no_try_delta;
1563 return entry;
1566 static const char no_closure_warning[] = N_(
1567 "disabling bitmap writing, as some objects are not being packed"
1570 static int add_object_entry(const struct object_id *oid, enum object_type type,
1571 const char *name, int exclude)
1573 struct packed_git *found_pack = NULL;
1574 off_t found_offset = 0;
1576 display_progress(progress_state, ++nr_seen);
1578 if (have_duplicate_entry(oid, exclude))
1579 return 0;
1581 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
1582 /* The pack is missing an object, so it will not have closure */
1583 if (write_bitmap_index) {
1584 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1585 warning(_(no_closure_warning));
1586 write_bitmap_index = 0;
1588 return 0;
1591 create_object_entry(oid, type, pack_name_hash(name),
1592 exclude, name && no_try_delta(name),
1593 found_pack, found_offset);
1594 return 1;
1597 static int add_object_entry_from_bitmap(const struct object_id *oid,
1598 enum object_type type,
1599 int flags UNUSED, uint32_t name_hash,
1600 struct packed_git *pack, off_t offset)
1602 display_progress(progress_state, ++nr_seen);
1604 if (have_duplicate_entry(oid, 0))
1605 return 0;
1607 if (!want_object_in_pack(oid, 0, &pack, &offset))
1608 return 0;
1610 create_object_entry(oid, type, name_hash, 0, 0, pack, offset);
1611 return 1;
1614 struct pbase_tree_cache {
1615 struct object_id oid;
1616 int ref;
1617 int temporary;
1618 void *tree_data;
1619 unsigned long tree_size;
1622 static struct pbase_tree_cache *(pbase_tree_cache[256]);
1623 static int pbase_tree_cache_ix(const struct object_id *oid)
1625 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
1627 static int pbase_tree_cache_ix_incr(int ix)
1629 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1632 static struct pbase_tree {
1633 struct pbase_tree *next;
1634 /* This is a phony "cache" entry; we are not
1635 * going to evict it or find it through _get()
1636 * mechanism -- this is for the toplevel node that
1637 * would almost always change with any commit.
1639 struct pbase_tree_cache pcache;
1640 } *pbase_tree;
1642 static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
1644 struct pbase_tree_cache *ent, *nent;
1645 void *data;
1646 unsigned long size;
1647 enum object_type type;
1648 int neigh;
1649 int my_ix = pbase_tree_cache_ix(oid);
1650 int available_ix = -1;
1652 /* pbase-tree-cache acts as a limited hashtable.
1653 * your object will be found at your index or within a few
1654 * slots after that slot if it is cached.
1656 for (neigh = 0; neigh < 8; neigh++) {
1657 ent = pbase_tree_cache[my_ix];
1658 if (ent && oideq(&ent->oid, oid)) {
1659 ent->ref++;
1660 return ent;
1662 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1663 ((0 <= available_ix) &&
1664 (!ent && pbase_tree_cache[available_ix])))
1665 available_ix = my_ix;
1666 if (!ent)
1667 break;
1668 my_ix = pbase_tree_cache_ix_incr(my_ix);
1671 /* Did not find one. Either we got a bogus request or
1672 * we need to read and perhaps cache.
1674 data = repo_read_object_file(the_repository, oid, &type, &size);
1675 if (!data)
1676 return NULL;
1677 if (type != OBJ_TREE) {
1678 free(data);
1679 return NULL;
1682 /* We need to either cache or return a throwaway copy */
1684 if (available_ix < 0)
1685 ent = NULL;
1686 else {
1687 ent = pbase_tree_cache[available_ix];
1688 my_ix = available_ix;
1691 if (!ent) {
1692 nent = xmalloc(sizeof(*nent));
1693 nent->temporary = (available_ix < 0);
1695 else {
1696 /* evict and reuse */
1697 free(ent->tree_data);
1698 nent = ent;
1700 oidcpy(&nent->oid, oid);
1701 nent->tree_data = data;
1702 nent->tree_size = size;
1703 nent->ref = 1;
1704 if (!nent->temporary)
1705 pbase_tree_cache[my_ix] = nent;
1706 return nent;
1709 static void pbase_tree_put(struct pbase_tree_cache *cache)
1711 if (!cache->temporary) {
1712 cache->ref--;
1713 return;
1715 free(cache->tree_data);
1716 free(cache);
1719 static size_t name_cmp_len(const char *name)
1721 return strcspn(name, "\n/");
1724 static void add_pbase_object(struct tree_desc *tree,
1725 const char *name,
1726 size_t cmplen,
1727 const char *fullname)
1729 struct name_entry entry;
1730 int cmp;
1732 while (tree_entry(tree,&entry)) {
1733 if (S_ISGITLINK(entry.mode))
1734 continue;
1735 cmp = tree_entry_len(&entry) != cmplen ? 1 :
1736 memcmp(name, entry.path, cmplen);
1737 if (cmp > 0)
1738 continue;
1739 if (cmp < 0)
1740 return;
1741 if (name[cmplen] != '/') {
1742 add_object_entry(&entry.oid,
1743 object_type(entry.mode),
1744 fullname, 1);
1745 return;
1747 if (S_ISDIR(entry.mode)) {
1748 struct tree_desc sub;
1749 struct pbase_tree_cache *tree;
1750 const char *down = name+cmplen+1;
1751 size_t downlen = name_cmp_len(down);
1753 tree = pbase_tree_get(&entry.oid);
1754 if (!tree)
1755 return;
1756 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1758 add_pbase_object(&sub, down, downlen, fullname);
1759 pbase_tree_put(tree);
1764 static unsigned *done_pbase_paths;
1765 static int done_pbase_paths_num;
1766 static int done_pbase_paths_alloc;
1767 static int done_pbase_path_pos(unsigned hash)
1769 int lo = 0;
1770 int hi = done_pbase_paths_num;
1771 while (lo < hi) {
1772 int mi = lo + (hi - lo) / 2;
1773 if (done_pbase_paths[mi] == hash)
1774 return mi;
1775 if (done_pbase_paths[mi] < hash)
1776 hi = mi;
1777 else
1778 lo = mi + 1;
1780 return -lo-1;
1783 static int check_pbase_path(unsigned hash)
1785 int pos = done_pbase_path_pos(hash);
1786 if (0 <= pos)
1787 return 1;
1788 pos = -pos - 1;
1789 ALLOC_GROW(done_pbase_paths,
1790 done_pbase_paths_num + 1,
1791 done_pbase_paths_alloc);
1792 done_pbase_paths_num++;
1793 if (pos < done_pbase_paths_num)
1794 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
1795 done_pbase_paths_num - pos - 1);
1796 done_pbase_paths[pos] = hash;
1797 return 0;
1800 static void add_preferred_base_object(const char *name)
1802 struct pbase_tree *it;
1803 size_t cmplen;
1804 unsigned hash = pack_name_hash(name);
1806 if (!num_preferred_base || check_pbase_path(hash))
1807 return;
1809 cmplen = name_cmp_len(name);
1810 for (it = pbase_tree; it; it = it->next) {
1811 if (cmplen == 0) {
1812 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
1814 else {
1815 struct tree_desc tree;
1816 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1817 add_pbase_object(&tree, name, cmplen, name);
1822 static void add_preferred_base(struct object_id *oid)
1824 struct pbase_tree *it;
1825 void *data;
1826 unsigned long size;
1827 struct object_id tree_oid;
1829 if (window <= num_preferred_base++)
1830 return;
1832 data = read_object_with_reference(the_repository, oid,
1833 OBJ_TREE, &size, &tree_oid);
1834 if (!data)
1835 return;
1837 for (it = pbase_tree; it; it = it->next) {
1838 if (oideq(&it->pcache.oid, &tree_oid)) {
1839 free(data);
1840 return;
1844 CALLOC_ARRAY(it, 1);
1845 it->next = pbase_tree;
1846 pbase_tree = it;
1848 oidcpy(&it->pcache.oid, &tree_oid);
1849 it->pcache.tree_data = data;
1850 it->pcache.tree_size = size;
1853 static void cleanup_preferred_base(void)
1855 struct pbase_tree *it;
1856 unsigned i;
1858 it = pbase_tree;
1859 pbase_tree = NULL;
1860 while (it) {
1861 struct pbase_tree *tmp = it;
1862 it = tmp->next;
1863 free(tmp->pcache.tree_data);
1864 free(tmp);
1867 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
1868 if (!pbase_tree_cache[i])
1869 continue;
1870 free(pbase_tree_cache[i]->tree_data);
1871 FREE_AND_NULL(pbase_tree_cache[i]);
1874 FREE_AND_NULL(done_pbase_paths);
1875 done_pbase_paths_num = done_pbase_paths_alloc = 0;
1879 * Return 1 iff the object specified by "delta" can be sent
1880 * literally as a delta against the base in "base_sha1". If
1881 * so, then *base_out will point to the entry in our packing
1882 * list, or NULL if we must use the external-base list.
1884 * Depth value does not matter - find_deltas() will
1885 * never consider reused delta as the base object to
1886 * deltify other objects against, in order to avoid
1887 * circular deltas.
1889 static int can_reuse_delta(const struct object_id *base_oid,
1890 struct object_entry *delta,
1891 struct object_entry **base_out)
1893 struct object_entry *base;
1896 * First see if we're already sending the base (or it's explicitly in
1897 * our "excluded" list).
1899 base = packlist_find(&to_pack, base_oid);
1900 if (base) {
1901 if (!in_same_island(&delta->idx.oid, &base->idx.oid))
1902 return 0;
1903 *base_out = base;
1904 return 1;
1908 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1909 * even if it was buried too deep in history to make it into the
1910 * packing list.
1912 if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, base_oid)) {
1913 if (use_delta_islands) {
1914 if (!in_same_island(&delta->idx.oid, base_oid))
1915 return 0;
1917 *base_out = NULL;
1918 return 1;
1921 return 0;
1924 static void prefetch_to_pack(uint32_t object_index_start) {
1925 struct oid_array to_fetch = OID_ARRAY_INIT;
1926 uint32_t i;
1928 for (i = object_index_start; i < to_pack.nr_objects; i++) {
1929 struct object_entry *entry = to_pack.objects + i;
1931 if (!oid_object_info_extended(the_repository,
1932 &entry->idx.oid,
1933 NULL,
1934 OBJECT_INFO_FOR_PREFETCH))
1935 continue;
1936 oid_array_append(&to_fetch, &entry->idx.oid);
1938 promisor_remote_get_direct(the_repository,
1939 to_fetch.oid, to_fetch.nr);
1940 oid_array_clear(&to_fetch);
1943 static void check_object(struct object_entry *entry, uint32_t object_index)
1945 unsigned long canonical_size;
1946 enum object_type type;
1947 struct object_info oi = {.typep = &type, .sizep = &canonical_size};
1949 if (IN_PACK(entry)) {
1950 struct packed_git *p = IN_PACK(entry);
1951 struct pack_window *w_curs = NULL;
1952 int have_base = 0;
1953 struct object_id base_ref;
1954 struct object_entry *base_entry;
1955 unsigned long used, used_0;
1956 unsigned long avail;
1957 off_t ofs;
1958 unsigned char *buf, c;
1959 enum object_type type;
1960 unsigned long in_pack_size;
1962 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1965 * We want in_pack_type even if we do not reuse delta
1966 * since non-delta representations could still be reused.
1968 used = unpack_object_header_buffer(buf, avail,
1969 &type,
1970 &in_pack_size);
1971 if (used == 0)
1972 goto give_up;
1974 if (type < 0)
1975 BUG("invalid type %d", type);
1976 entry->in_pack_type = type;
1979 * Determine if this is a delta and if so whether we can
1980 * reuse it or not. Otherwise let's find out as cheaply as
1981 * possible what the actual type and size for this object is.
1983 switch (entry->in_pack_type) {
1984 default:
1985 /* Not a delta hence we've already got all we need. */
1986 oe_set_type(entry, entry->in_pack_type);
1987 SET_SIZE(entry, in_pack_size);
1988 entry->in_pack_header_size = used;
1989 if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
1990 goto give_up;
1991 unuse_pack(&w_curs);
1992 return;
1993 case OBJ_REF_DELTA:
1994 if (reuse_delta && !entry->preferred_base) {
1995 oidread(&base_ref,
1996 use_pack(p, &w_curs,
1997 entry->in_pack_offset + used,
1998 NULL));
1999 have_base = 1;
2001 entry->in_pack_header_size = used + the_hash_algo->rawsz;
2002 break;
2003 case OBJ_OFS_DELTA:
2004 buf = use_pack(p, &w_curs,
2005 entry->in_pack_offset + used, NULL);
2006 used_0 = 0;
2007 c = buf[used_0++];
2008 ofs = c & 127;
2009 while (c & 128) {
2010 ofs += 1;
2011 if (!ofs || MSB(ofs, 7)) {
2012 error(_("delta base offset overflow in pack for %s"),
2013 oid_to_hex(&entry->idx.oid));
2014 goto give_up;
2016 c = buf[used_0++];
2017 ofs = (ofs << 7) + (c & 127);
2019 ofs = entry->in_pack_offset - ofs;
2020 if (ofs <= 0 || ofs >= entry->in_pack_offset) {
2021 error(_("delta base offset out of bound for %s"),
2022 oid_to_hex(&entry->idx.oid));
2023 goto give_up;
2025 if (reuse_delta && !entry->preferred_base) {
2026 uint32_t pos;
2027 if (offset_to_pack_pos(p, ofs, &pos) < 0)
2028 goto give_up;
2029 if (!nth_packed_object_id(&base_ref, p,
2030 pack_pos_to_index(p, pos)))
2031 have_base = 1;
2033 entry->in_pack_header_size = used + used_0;
2034 break;
2037 if (have_base &&
2038 can_reuse_delta(&base_ref, entry, &base_entry)) {
2039 oe_set_type(entry, entry->in_pack_type);
2040 SET_SIZE(entry, in_pack_size); /* delta size */
2041 SET_DELTA_SIZE(entry, in_pack_size);
2043 if (base_entry) {
2044 SET_DELTA(entry, base_entry);
2045 entry->delta_sibling_idx = base_entry->delta_child_idx;
2046 SET_DELTA_CHILD(base_entry, entry);
2047 } else {
2048 SET_DELTA_EXT(entry, &base_ref);
2051 unuse_pack(&w_curs);
2052 return;
2055 if (oe_type(entry)) {
2056 off_t delta_pos;
2059 * This must be a delta and we already know what the
2060 * final object type is. Let's extract the actual
2061 * object size from the delta header.
2063 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
2064 canonical_size = get_size_from_delta(p, &w_curs, delta_pos);
2065 if (canonical_size == 0)
2066 goto give_up;
2067 SET_SIZE(entry, canonical_size);
2068 unuse_pack(&w_curs);
2069 return;
2073 * No choice but to fall back to the recursive delta walk
2074 * with oid_object_info() to find about the object type
2075 * at this point...
2077 give_up:
2078 unuse_pack(&w_curs);
2081 if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
2082 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) {
2083 if (repo_has_promisor_remote(the_repository)) {
2084 prefetch_to_pack(object_index);
2085 if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
2086 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0)
2087 type = -1;
2088 } else {
2089 type = -1;
2092 oe_set_type(entry, type);
2093 if (entry->type_valid) {
2094 SET_SIZE(entry, canonical_size);
2095 } else {
2097 * Bad object type is checked in prepare_pack(). This is
2098 * to permit a missing preferred base object to be ignored
2099 * as a preferred base. Doing so can result in a larger
2100 * pack file, but the transfer will still take place.
2105 static int pack_offset_sort(const void *_a, const void *_b)
2107 const struct object_entry *a = *(struct object_entry **)_a;
2108 const struct object_entry *b = *(struct object_entry **)_b;
2109 const struct packed_git *a_in_pack = IN_PACK(a);
2110 const struct packed_git *b_in_pack = IN_PACK(b);
2112 /* avoid filesystem trashing with loose objects */
2113 if (!a_in_pack && !b_in_pack)
2114 return oidcmp(&a->idx.oid, &b->idx.oid);
2116 if (a_in_pack < b_in_pack)
2117 return -1;
2118 if (a_in_pack > b_in_pack)
2119 return 1;
2120 return a->in_pack_offset < b->in_pack_offset ? -1 :
2121 (a->in_pack_offset > b->in_pack_offset);
2125 * Drop an on-disk delta we were planning to reuse. Naively, this would
2126 * just involve blanking out the "delta" field, but we have to deal
2127 * with some extra book-keeping:
2129 * 1. Removing ourselves from the delta_sibling linked list.
2131 * 2. Updating our size/type to the non-delta representation. These were
2132 * either not recorded initially (size) or overwritten with the delta type
2133 * (type) when check_object() decided to reuse the delta.
2135 * 3. Resetting our delta depth, as we are now a base object.
2137 static void drop_reused_delta(struct object_entry *entry)
2139 unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
2140 struct object_info oi = OBJECT_INFO_INIT;
2141 enum object_type type;
2142 unsigned long size;
2144 while (*idx) {
2145 struct object_entry *oe = &to_pack.objects[*idx - 1];
2147 if (oe == entry)
2148 *idx = oe->delta_sibling_idx;
2149 else
2150 idx = &oe->delta_sibling_idx;
2152 SET_DELTA(entry, NULL);
2153 entry->depth = 0;
2155 oi.sizep = &size;
2156 oi.typep = &type;
2157 if (packed_object_info(the_repository, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
2159 * We failed to get the info from this pack for some reason;
2160 * fall back to oid_object_info, which may find another copy.
2161 * And if that fails, the error will be recorded in oe_type(entry)
2162 * and dealt with in prepare_pack().
2164 oe_set_type(entry,
2165 oid_object_info(the_repository, &entry->idx.oid, &size));
2166 } else {
2167 oe_set_type(entry, type);
2169 SET_SIZE(entry, size);
2173 * Follow the chain of deltas from this entry onward, throwing away any links
2174 * that cause us to hit a cycle (as determined by the DFS state flags in
2175 * the entries).
2177 * We also detect too-long reused chains that would violate our --depth
2178 * limit.
2180 static void break_delta_chains(struct object_entry *entry)
2183 * The actual depth of each object we will write is stored as an int,
2184 * as it cannot exceed our int "depth" limit. But before we break
2185 * changes based no that limit, we may potentially go as deep as the
2186 * number of objects, which is elsewhere bounded to a uint32_t.
2188 uint32_t total_depth;
2189 struct object_entry *cur, *next;
2191 for (cur = entry, total_depth = 0;
2192 cur;
2193 cur = DELTA(cur), total_depth++) {
2194 if (cur->dfs_state == DFS_DONE) {
2196 * We've already seen this object and know it isn't
2197 * part of a cycle. We do need to append its depth
2198 * to our count.
2200 total_depth += cur->depth;
2201 break;
2205 * We break cycles before looping, so an ACTIVE state (or any
2206 * other cruft which made its way into the state variable)
2207 * is a bug.
2209 if (cur->dfs_state != DFS_NONE)
2210 BUG("confusing delta dfs state in first pass: %d",
2211 cur->dfs_state);
2214 * Now we know this is the first time we've seen the object. If
2215 * it's not a delta, we're done traversing, but we'll mark it
2216 * done to save time on future traversals.
2218 if (!DELTA(cur)) {
2219 cur->dfs_state = DFS_DONE;
2220 break;
2224 * Mark ourselves as active and see if the next step causes
2225 * us to cycle to another active object. It's important to do
2226 * this _before_ we loop, because it impacts where we make the
2227 * cut, and thus how our total_depth counter works.
2228 * E.g., We may see a partial loop like:
2230 * A -> B -> C -> D -> B
2232 * Cutting B->C breaks the cycle. But now the depth of A is
2233 * only 1, and our total_depth counter is at 3. The size of the
2234 * error is always one less than the size of the cycle we
2235 * broke. Commits C and D were "lost" from A's chain.
2237 * If we instead cut D->B, then the depth of A is correct at 3.
2238 * We keep all commits in the chain that we examined.
2240 cur->dfs_state = DFS_ACTIVE;
2241 if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
2242 drop_reused_delta(cur);
2243 cur->dfs_state = DFS_DONE;
2244 break;
2249 * And now that we've gone all the way to the bottom of the chain, we
2250 * need to clear the active flags and set the depth fields as
2251 * appropriate. Unlike the loop above, which can quit when it drops a
2252 * delta, we need to keep going to look for more depth cuts. So we need
2253 * an extra "next" pointer to keep going after we reset cur->delta.
2255 for (cur = entry; cur; cur = next) {
2256 next = DELTA(cur);
2259 * We should have a chain of zero or more ACTIVE states down to
2260 * a final DONE. We can quit after the DONE, because either it
2261 * has no bases, or we've already handled them in a previous
2262 * call.
2264 if (cur->dfs_state == DFS_DONE)
2265 break;
2266 else if (cur->dfs_state != DFS_ACTIVE)
2267 BUG("confusing delta dfs state in second pass: %d",
2268 cur->dfs_state);
2271 * If the total_depth is more than depth, then we need to snip
2272 * the chain into two or more smaller chains that don't exceed
2273 * the maximum depth. Most of the resulting chains will contain
2274 * (depth + 1) entries (i.e., depth deltas plus one base), and
2275 * the last chain (i.e., the one containing entry) will contain
2276 * whatever entries are left over, namely
2277 * (total_depth % (depth + 1)) of them.
2279 * Since we are iterating towards decreasing depth, we need to
2280 * decrement total_depth as we go, and we need to write to the
2281 * entry what its final depth will be after all of the
2282 * snipping. Since we're snipping into chains of length (depth
2283 * + 1) entries, the final depth of an entry will be its
2284 * original depth modulo (depth + 1). Any time we encounter an
2285 * entry whose final depth is supposed to be zero, we snip it
2286 * from its delta base, thereby making it so.
2288 cur->depth = (total_depth--) % (depth + 1);
2289 if (!cur->depth)
2290 drop_reused_delta(cur);
2292 cur->dfs_state = DFS_DONE;
2296 static void get_object_details(void)
2298 uint32_t i;
2299 struct object_entry **sorted_by_offset;
2301 if (progress)
2302 progress_state = start_progress(_("Counting objects"),
2303 to_pack.nr_objects);
2305 CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects);
2306 for (i = 0; i < to_pack.nr_objects; i++)
2307 sorted_by_offset[i] = to_pack.objects + i;
2308 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
2310 for (i = 0; i < to_pack.nr_objects; i++) {
2311 struct object_entry *entry = sorted_by_offset[i];
2312 check_object(entry, i);
2313 if (entry->type_valid &&
2314 oe_size_greater_than(&to_pack, entry, big_file_threshold))
2315 entry->no_try_delta = 1;
2316 display_progress(progress_state, i + 1);
2318 stop_progress(&progress_state);
2321 * This must happen in a second pass, since we rely on the delta
2322 * information for the whole list being completed.
2324 for (i = 0; i < to_pack.nr_objects; i++)
2325 break_delta_chains(&to_pack.objects[i]);
2327 free(sorted_by_offset);
2331 * We search for deltas in a list sorted by type, by filename hash, and then
2332 * by size, so that we see progressively smaller and smaller files.
2333 * That's because we prefer deltas to be from the bigger file
2334 * to the smaller -- deletes are potentially cheaper, but perhaps
2335 * more importantly, the bigger file is likely the more recent
2336 * one. The deepest deltas are therefore the oldest objects which are
2337 * less susceptible to be accessed often.
2339 static int type_size_sort(const void *_a, const void *_b)
2341 const struct object_entry *a = *(struct object_entry **)_a;
2342 const struct object_entry *b = *(struct object_entry **)_b;
2343 const enum object_type a_type = oe_type(a);
2344 const enum object_type b_type = oe_type(b);
2345 const unsigned long a_size = SIZE(a);
2346 const unsigned long b_size = SIZE(b);
2348 if (a_type > b_type)
2349 return -1;
2350 if (a_type < b_type)
2351 return 1;
2352 if (a->hash > b->hash)
2353 return -1;
2354 if (a->hash < b->hash)
2355 return 1;
2356 if (a->preferred_base > b->preferred_base)
2357 return -1;
2358 if (a->preferred_base < b->preferred_base)
2359 return 1;
2360 if (use_delta_islands) {
2361 const int island_cmp = island_delta_cmp(&a->idx.oid, &b->idx.oid);
2362 if (island_cmp)
2363 return island_cmp;
2365 if (a_size > b_size)
2366 return -1;
2367 if (a_size < b_size)
2368 return 1;
2369 return a < b ? -1 : (a > b); /* newest first */
2372 struct unpacked {
2373 struct object_entry *entry;
2374 void *data;
2375 struct delta_index *index;
2376 unsigned depth;
2379 static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
2380 unsigned long delta_size)
2382 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
2383 return 0;
2385 if (delta_size < cache_max_small_delta_size)
2386 return 1;
2388 /* cache delta, if objects are large enough compared to delta size */
2389 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
2390 return 1;
2392 return 0;
2395 /* Protect delta_cache_size */
2396 static pthread_mutex_t cache_mutex;
2397 #define cache_lock() pthread_mutex_lock(&cache_mutex)
2398 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2401 * Protect object list partitioning (e.g. struct thread_param) and
2402 * progress_state
2404 static pthread_mutex_t progress_mutex;
2405 #define progress_lock() pthread_mutex_lock(&progress_mutex)
2406 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2409 * Access to struct object_entry is unprotected since each thread owns
2410 * a portion of the main object list. Just don't access object entries
2411 * ahead in the list because they can be stolen and would need
2412 * progress_mutex for protection.
2415 static inline int oe_size_less_than(struct packing_data *pack,
2416 const struct object_entry *lhs,
2417 unsigned long rhs)
2419 if (lhs->size_valid)
2420 return lhs->size_ < rhs;
2421 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
2422 return 0;
2423 return oe_get_size_slow(pack, lhs) < rhs;
2426 static inline void oe_set_tree_depth(struct packing_data *pack,
2427 struct object_entry *e,
2428 unsigned int tree_depth)
2430 if (!pack->tree_depth)
2431 CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
2432 pack->tree_depth[e - pack->objects] = tree_depth;
2436 * Return the size of the object without doing any delta
2437 * reconstruction (so non-deltas are true object sizes, but deltas
2438 * return the size of the delta data).
2440 unsigned long oe_get_size_slow(struct packing_data *pack,
2441 const struct object_entry *e)
2443 struct packed_git *p;
2444 struct pack_window *w_curs;
2445 unsigned char *buf;
2446 enum object_type type;
2447 unsigned long used, avail, size;
2449 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
2450 packing_data_lock(&to_pack);
2451 if (oid_object_info(the_repository, &e->idx.oid, &size) < 0)
2452 die(_("unable to get size of %s"),
2453 oid_to_hex(&e->idx.oid));
2454 packing_data_unlock(&to_pack);
2455 return size;
2458 p = oe_in_pack(pack, e);
2459 if (!p)
2460 BUG("when e->type is a delta, it must belong to a pack");
2462 packing_data_lock(&to_pack);
2463 w_curs = NULL;
2464 buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
2465 used = unpack_object_header_buffer(buf, avail, &type, &size);
2466 if (used == 0)
2467 die(_("unable to parse object header of %s"),
2468 oid_to_hex(&e->idx.oid));
2470 unuse_pack(&w_curs);
2471 packing_data_unlock(&to_pack);
2472 return size;
2475 static int try_delta(struct unpacked *trg, struct unpacked *src,
2476 unsigned max_depth, unsigned long *mem_usage)
2478 struct object_entry *trg_entry = trg->entry;
2479 struct object_entry *src_entry = src->entry;
2480 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
2481 unsigned ref_depth;
2482 enum object_type type;
2483 void *delta_buf;
2485 /* Don't bother doing diffs between different types */
2486 if (oe_type(trg_entry) != oe_type(src_entry))
2487 return -1;
2490 * We do not bother to try a delta that we discarded on an
2491 * earlier try, but only when reusing delta data. Note that
2492 * src_entry that is marked as the preferred_base should always
2493 * be considered, as even if we produce a suboptimal delta against
2494 * it, we will still save the transfer cost, as we already know
2495 * the other side has it and we won't send src_entry at all.
2497 if (reuse_delta && IN_PACK(trg_entry) &&
2498 IN_PACK(trg_entry) == IN_PACK(src_entry) &&
2499 !src_entry->preferred_base &&
2500 trg_entry->in_pack_type != OBJ_REF_DELTA &&
2501 trg_entry->in_pack_type != OBJ_OFS_DELTA)
2502 return 0;
2504 /* Let's not bust the allowed depth. */
2505 if (src->depth >= max_depth)
2506 return 0;
2508 /* Now some size filtering heuristics. */
2509 trg_size = SIZE(trg_entry);
2510 if (!DELTA(trg_entry)) {
2511 max_size = trg_size/2 - the_hash_algo->rawsz;
2512 ref_depth = 1;
2513 } else {
2514 max_size = DELTA_SIZE(trg_entry);
2515 ref_depth = trg->depth;
2517 max_size = (uint64_t)max_size * (max_depth - src->depth) /
2518 (max_depth - ref_depth + 1);
2519 if (max_size == 0)
2520 return 0;
2521 src_size = SIZE(src_entry);
2522 sizediff = src_size < trg_size ? trg_size - src_size : 0;
2523 if (sizediff >= max_size)
2524 return 0;
2525 if (trg_size < src_size / 32)
2526 return 0;
2528 if (!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))
2529 return 0;
2531 /* Load data if not already done */
2532 if (!trg->data) {
2533 packing_data_lock(&to_pack);
2534 trg->data = repo_read_object_file(the_repository,
2535 &trg_entry->idx.oid, &type,
2536 &sz);
2537 packing_data_unlock(&to_pack);
2538 if (!trg->data)
2539 die(_("object %s cannot be read"),
2540 oid_to_hex(&trg_entry->idx.oid));
2541 if (sz != trg_size)
2542 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2543 oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,
2544 (uintmax_t)trg_size);
2545 *mem_usage += sz;
2547 if (!src->data) {
2548 packing_data_lock(&to_pack);
2549 src->data = repo_read_object_file(the_repository,
2550 &src_entry->idx.oid, &type,
2551 &sz);
2552 packing_data_unlock(&to_pack);
2553 if (!src->data) {
2554 if (src_entry->preferred_base) {
2555 static int warned = 0;
2556 if (!warned++)
2557 warning(_("object %s cannot be read"),
2558 oid_to_hex(&src_entry->idx.oid));
2560 * Those objects are not included in the
2561 * resulting pack. Be resilient and ignore
2562 * them if they can't be read, in case the
2563 * pack could be created nevertheless.
2565 return 0;
2567 die(_("object %s cannot be read"),
2568 oid_to_hex(&src_entry->idx.oid));
2570 if (sz != src_size)
2571 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2572 oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,
2573 (uintmax_t)src_size);
2574 *mem_usage += sz;
2576 if (!src->index) {
2577 src->index = create_delta_index(src->data, src_size);
2578 if (!src->index) {
2579 static int warned = 0;
2580 if (!warned++)
2581 warning(_("suboptimal pack - out of memory"));
2582 return 0;
2584 *mem_usage += sizeof_delta_index(src->index);
2587 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
2588 if (!delta_buf)
2589 return 0;
2591 if (DELTA(trg_entry)) {
2592 /* Prefer only shallower same-sized deltas. */
2593 if (delta_size == DELTA_SIZE(trg_entry) &&
2594 src->depth + 1 >= trg->depth) {
2595 free(delta_buf);
2596 return 0;
2601 * Handle memory allocation outside of the cache
2602 * accounting lock. Compiler will optimize the strangeness
2603 * away when NO_PTHREADS is defined.
2605 free(trg_entry->delta_data);
2606 cache_lock();
2607 if (trg_entry->delta_data) {
2608 delta_cache_size -= DELTA_SIZE(trg_entry);
2609 trg_entry->delta_data = NULL;
2611 if (delta_cacheable(src_size, trg_size, delta_size)) {
2612 delta_cache_size += delta_size;
2613 cache_unlock();
2614 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
2615 } else {
2616 cache_unlock();
2617 free(delta_buf);
2620 SET_DELTA(trg_entry, src_entry);
2621 SET_DELTA_SIZE(trg_entry, delta_size);
2622 trg->depth = src->depth + 1;
2624 return 1;
2627 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
2629 struct object_entry *child = DELTA_CHILD(me);
2630 unsigned int m = n;
2631 while (child) {
2632 const unsigned int c = check_delta_limit(child, n + 1);
2633 if (m < c)
2634 m = c;
2635 child = DELTA_SIBLING(child);
2637 return m;
2640 static unsigned long free_unpacked(struct unpacked *n)
2642 unsigned long freed_mem = sizeof_delta_index(n->index);
2643 free_delta_index(n->index);
2644 n->index = NULL;
2645 if (n->data) {
2646 freed_mem += SIZE(n->entry);
2647 FREE_AND_NULL(n->data);
2649 n->entry = NULL;
2650 n->depth = 0;
2651 return freed_mem;
2654 static void find_deltas(struct object_entry **list, unsigned *list_size,
2655 int window, int depth, unsigned *processed)
2657 uint32_t i, idx = 0, count = 0;
2658 struct unpacked *array;
2659 unsigned long mem_usage = 0;
2661 CALLOC_ARRAY(array, window);
2663 for (;;) {
2664 struct object_entry *entry;
2665 struct unpacked *n = array + idx;
2666 int j, max_depth, best_base = -1;
2668 progress_lock();
2669 if (!*list_size) {
2670 progress_unlock();
2671 break;
2673 entry = *list++;
2674 (*list_size)--;
2675 if (!entry->preferred_base) {
2676 (*processed)++;
2677 display_progress(progress_state, *processed);
2679 progress_unlock();
2681 mem_usage -= free_unpacked(n);
2682 n->entry = entry;
2684 while (window_memory_limit &&
2685 mem_usage > window_memory_limit &&
2686 count > 1) {
2687 const uint32_t tail = (idx + window - count) % window;
2688 mem_usage -= free_unpacked(array + tail);
2689 count--;
2692 /* We do not compute delta to *create* objects we are not
2693 * going to pack.
2695 if (entry->preferred_base)
2696 goto next;
2699 * If the current object is at pack edge, take the depth the
2700 * objects that depend on the current object into account
2701 * otherwise they would become too deep.
2703 max_depth = depth;
2704 if (DELTA_CHILD(entry)) {
2705 max_depth -= check_delta_limit(entry, 0);
2706 if (max_depth <= 0)
2707 goto next;
2710 j = window;
2711 while (--j > 0) {
2712 int ret;
2713 uint32_t other_idx = idx + j;
2714 struct unpacked *m;
2715 if (other_idx >= window)
2716 other_idx -= window;
2717 m = array + other_idx;
2718 if (!m->entry)
2719 break;
2720 ret = try_delta(n, m, max_depth, &mem_usage);
2721 if (ret < 0)
2722 break;
2723 else if (ret > 0)
2724 best_base = other_idx;
2728 * If we decided to cache the delta data, then it is best
2729 * to compress it right away. First because we have to do
2730 * it anyway, and doing it here while we're threaded will
2731 * save a lot of time in the non threaded write phase,
2732 * as well as allow for caching more deltas within
2733 * the same cache size limit.
2734 * ...
2735 * But only if not writing to stdout, since in that case
2736 * the network is most likely throttling writes anyway,
2737 * and therefore it is best to go to the write phase ASAP
2738 * instead, as we can afford spending more time compressing
2739 * between writes at that moment.
2741 if (entry->delta_data && !pack_to_stdout) {
2742 unsigned long size;
2744 size = do_compress(&entry->delta_data, DELTA_SIZE(entry));
2745 if (size < (1U << OE_Z_DELTA_BITS)) {
2746 entry->z_delta_size = size;
2747 cache_lock();
2748 delta_cache_size -= DELTA_SIZE(entry);
2749 delta_cache_size += entry->z_delta_size;
2750 cache_unlock();
2751 } else {
2752 FREE_AND_NULL(entry->delta_data);
2753 entry->z_delta_size = 0;
2757 /* if we made n a delta, and if n is already at max
2758 * depth, leaving it in the window is pointless. we
2759 * should evict it first.
2761 if (DELTA(entry) && max_depth <= n->depth)
2762 continue;
2765 * Move the best delta base up in the window, after the
2766 * currently deltified object, to keep it longer. It will
2767 * be the first base object to be attempted next.
2769 if (DELTA(entry)) {
2770 struct unpacked swap = array[best_base];
2771 int dist = (window + idx - best_base) % window;
2772 int dst = best_base;
2773 while (dist--) {
2774 int src = (dst + 1) % window;
2775 array[dst] = array[src];
2776 dst = src;
2778 array[dst] = swap;
2781 next:
2782 idx++;
2783 if (count + 1 < window)
2784 count++;
2785 if (idx >= window)
2786 idx = 0;
2789 for (i = 0; i < window; ++i) {
2790 free_delta_index(array[i].index);
2791 free(array[i].data);
2793 free(array);
2797 * The main object list is split into smaller lists, each is handed to
2798 * one worker.
2800 * The main thread waits on the condition that (at least) one of the workers
2801 * has stopped working (which is indicated in the .working member of
2802 * struct thread_params).
2804 * When a work thread has completed its work, it sets .working to 0 and
2805 * signals the main thread and waits on the condition that .data_ready
2806 * becomes 1.
2808 * The main thread steals half of the work from the worker that has
2809 * most work left to hand it to the idle worker.
2812 struct thread_params {
2813 pthread_t thread;
2814 struct object_entry **list;
2815 unsigned list_size;
2816 unsigned remaining;
2817 int window;
2818 int depth;
2819 int working;
2820 int data_ready;
2821 pthread_mutex_t mutex;
2822 pthread_cond_t cond;
2823 unsigned *processed;
2826 static pthread_cond_t progress_cond;
2829 * Mutex and conditional variable can't be statically-initialized on Windows.
2831 static void init_threaded_search(void)
2833 pthread_mutex_init(&cache_mutex, NULL);
2834 pthread_mutex_init(&progress_mutex, NULL);
2835 pthread_cond_init(&progress_cond, NULL);
2838 static void cleanup_threaded_search(void)
2840 pthread_cond_destroy(&progress_cond);
2841 pthread_mutex_destroy(&cache_mutex);
2842 pthread_mutex_destroy(&progress_mutex);
2845 static void *threaded_find_deltas(void *arg)
2847 struct thread_params *me = arg;
2849 progress_lock();
2850 while (me->remaining) {
2851 progress_unlock();
2853 find_deltas(me->list, &me->remaining,
2854 me->window, me->depth, me->processed);
2856 progress_lock();
2857 me->working = 0;
2858 pthread_cond_signal(&progress_cond);
2859 progress_unlock();
2862 * We must not set ->data_ready before we wait on the
2863 * condition because the main thread may have set it to 1
2864 * before we get here. In order to be sure that new
2865 * work is available if we see 1 in ->data_ready, it
2866 * was initialized to 0 before this thread was spawned
2867 * and we reset it to 0 right away.
2869 pthread_mutex_lock(&me->mutex);
2870 while (!me->data_ready)
2871 pthread_cond_wait(&me->cond, &me->mutex);
2872 me->data_ready = 0;
2873 pthread_mutex_unlock(&me->mutex);
2875 progress_lock();
2877 progress_unlock();
2878 /* leave ->working 1 so that this doesn't get more work assigned */
2879 return NULL;
2882 static void ll_find_deltas(struct object_entry **list, unsigned list_size,
2883 int window, int depth, unsigned *processed)
2885 struct thread_params *p;
2886 int i, ret, active_threads = 0;
2888 init_threaded_search();
2890 if (delta_search_threads <= 1) {
2891 find_deltas(list, &list_size, window, depth, processed);
2892 cleanup_threaded_search();
2893 return;
2895 if (progress > pack_to_stdout)
2896 fprintf_ln(stderr, _("Delta compression using up to %d threads"),
2897 delta_search_threads);
2898 CALLOC_ARRAY(p, delta_search_threads);
2900 /* Partition the work amongst work threads. */
2901 for (i = 0; i < delta_search_threads; i++) {
2902 unsigned sub_size = list_size / (delta_search_threads - i);
2904 /* don't use too small segments or no deltas will be found */
2905 if (sub_size < 2*window && i+1 < delta_search_threads)
2906 sub_size = 0;
2908 p[i].window = window;
2909 p[i].depth = depth;
2910 p[i].processed = processed;
2911 p[i].working = 1;
2912 p[i].data_ready = 0;
2914 /* try to split chunks on "path" boundaries */
2915 while (sub_size && sub_size < list_size &&
2916 list[sub_size]->hash &&
2917 list[sub_size]->hash == list[sub_size-1]->hash)
2918 sub_size++;
2920 p[i].list = list;
2921 p[i].list_size = sub_size;
2922 p[i].remaining = sub_size;
2924 list += sub_size;
2925 list_size -= sub_size;
2928 /* Start work threads. */
2929 for (i = 0; i < delta_search_threads; i++) {
2930 if (!p[i].list_size)
2931 continue;
2932 pthread_mutex_init(&p[i].mutex, NULL);
2933 pthread_cond_init(&p[i].cond, NULL);
2934 ret = pthread_create(&p[i].thread, NULL,
2935 threaded_find_deltas, &p[i]);
2936 if (ret)
2937 die(_("unable to create thread: %s"), strerror(ret));
2938 active_threads++;
2942 * Now let's wait for work completion. Each time a thread is done
2943 * with its work, we steal half of the remaining work from the
2944 * thread with the largest number of unprocessed objects and give
2945 * it to that newly idle thread. This ensure good load balancing
2946 * until the remaining object list segments are simply too short
2947 * to be worth splitting anymore.
2949 while (active_threads) {
2950 struct thread_params *target = NULL;
2951 struct thread_params *victim = NULL;
2952 unsigned sub_size = 0;
2954 progress_lock();
2955 for (;;) {
2956 for (i = 0; !target && i < delta_search_threads; i++)
2957 if (!p[i].working)
2958 target = &p[i];
2959 if (target)
2960 break;
2961 pthread_cond_wait(&progress_cond, &progress_mutex);
2964 for (i = 0; i < delta_search_threads; i++)
2965 if (p[i].remaining > 2*window &&
2966 (!victim || victim->remaining < p[i].remaining))
2967 victim = &p[i];
2968 if (victim) {
2969 sub_size = victim->remaining / 2;
2970 list = victim->list + victim->list_size - sub_size;
2971 while (sub_size && list[0]->hash &&
2972 list[0]->hash == list[-1]->hash) {
2973 list++;
2974 sub_size--;
2976 if (!sub_size) {
2978 * It is possible for some "paths" to have
2979 * so many objects that no hash boundary
2980 * might be found. Let's just steal the
2981 * exact half in that case.
2983 sub_size = victim->remaining / 2;
2984 list -= sub_size;
2986 target->list = list;
2987 victim->list_size -= sub_size;
2988 victim->remaining -= sub_size;
2990 target->list_size = sub_size;
2991 target->remaining = sub_size;
2992 target->working = 1;
2993 progress_unlock();
2995 pthread_mutex_lock(&target->mutex);
2996 target->data_ready = 1;
2997 pthread_cond_signal(&target->cond);
2998 pthread_mutex_unlock(&target->mutex);
3000 if (!sub_size) {
3001 pthread_join(target->thread, NULL);
3002 pthread_cond_destroy(&target->cond);
3003 pthread_mutex_destroy(&target->mutex);
3004 active_threads--;
3007 cleanup_threaded_search();
3008 free(p);
3011 static int obj_is_packed(const struct object_id *oid)
3013 return packlist_find(&to_pack, oid) ||
3014 (reuse_packfile_bitmap &&
3015 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid));
3018 static void add_tag_chain(const struct object_id *oid)
3020 struct tag *tag;
3023 * We catch duplicates already in add_object_entry(), but we'd
3024 * prefer to do this extra check to avoid having to parse the
3025 * tag at all if we already know that it's being packed (e.g., if
3026 * it was included via bitmaps, we would not have parsed it
3027 * previously).
3029 if (obj_is_packed(oid))
3030 return;
3032 tag = lookup_tag(the_repository, oid);
3033 while (1) {
3034 if (!tag || parse_tag(tag) || !tag->tagged)
3035 die(_("unable to pack objects reachable from tag %s"),
3036 oid_to_hex(oid));
3038 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
3040 if (tag->tagged->type != OBJ_TAG)
3041 return;
3043 tag = (struct tag *)tag->tagged;
3047 static int add_ref_tag(const char *tag UNUSED, const struct object_id *oid,
3048 int flag UNUSED, void *cb_data UNUSED)
3050 struct object_id peeled;
3052 if (!peel_iterated_oid(oid, &peeled) && obj_is_packed(&peeled))
3053 add_tag_chain(oid);
3054 return 0;
3057 static void prepare_pack(int window, int depth)
3059 struct object_entry **delta_list;
3060 uint32_t i, nr_deltas;
3061 unsigned n;
3063 if (use_delta_islands)
3064 resolve_tree_islands(the_repository, progress, &to_pack);
3066 get_object_details();
3069 * If we're locally repacking then we need to be doubly careful
3070 * from now on in order to make sure no stealth corruption gets
3071 * propagated to the new pack. Clients receiving streamed packs
3072 * should validate everything they get anyway so no need to incur
3073 * the additional cost here in that case.
3075 if (!pack_to_stdout)
3076 do_check_packed_object_crc = 1;
3078 if (!to_pack.nr_objects || !window || !depth)
3079 return;
3081 ALLOC_ARRAY(delta_list, to_pack.nr_objects);
3082 nr_deltas = n = 0;
3084 for (i = 0; i < to_pack.nr_objects; i++) {
3085 struct object_entry *entry = to_pack.objects + i;
3087 if (DELTA(entry))
3088 /* This happens if we decided to reuse existing
3089 * delta from a pack. "reuse_delta &&" is implied.
3091 continue;
3093 if (!entry->type_valid ||
3094 oe_size_less_than(&to_pack, entry, 50))
3095 continue;
3097 if (entry->no_try_delta)
3098 continue;
3100 if (!entry->preferred_base) {
3101 nr_deltas++;
3102 if (oe_type(entry) < 0)
3103 die(_("unable to get type of object %s"),
3104 oid_to_hex(&entry->idx.oid));
3105 } else {
3106 if (oe_type(entry) < 0) {
3108 * This object is not found, but we
3109 * don't have to include it anyway.
3111 continue;
3115 delta_list[n++] = entry;
3118 if (nr_deltas && n > 1) {
3119 unsigned nr_done = 0;
3121 if (progress)
3122 progress_state = start_progress(_("Compressing objects"),
3123 nr_deltas);
3124 QSORT(delta_list, n, type_size_sort);
3125 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
3126 stop_progress(&progress_state);
3127 if (nr_done != nr_deltas)
3128 die(_("inconsistency with delta count"));
3130 free(delta_list);
3133 static int git_pack_config(const char *k, const char *v,
3134 const struct config_context *ctx, void *cb)
3136 if (!strcmp(k, "pack.window")) {
3137 window = git_config_int(k, v, ctx->kvi);
3138 return 0;
3140 if (!strcmp(k, "pack.windowmemory")) {
3141 window_memory_limit = git_config_ulong(k, v, ctx->kvi);
3142 return 0;
3144 if (!strcmp(k, "pack.depth")) {
3145 depth = git_config_int(k, v, ctx->kvi);
3146 return 0;
3148 if (!strcmp(k, "pack.deltacachesize")) {
3149 max_delta_cache_size = git_config_int(k, v, ctx->kvi);
3150 return 0;
3152 if (!strcmp(k, "pack.deltacachelimit")) {
3153 cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
3154 return 0;
3156 if (!strcmp(k, "pack.writebitmaphashcache")) {
3157 if (git_config_bool(k, v))
3158 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
3159 else
3160 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
3163 if (!strcmp(k, "pack.writebitmaplookuptable")) {
3164 if (git_config_bool(k, v))
3165 write_bitmap_options |= BITMAP_OPT_LOOKUP_TABLE;
3166 else
3167 write_bitmap_options &= ~BITMAP_OPT_LOOKUP_TABLE;
3170 if (!strcmp(k, "pack.usebitmaps")) {
3171 use_bitmap_index_default = git_config_bool(k, v);
3172 return 0;
3174 if (!strcmp(k, "pack.allowpackreuse")) {
3175 allow_pack_reuse = git_config_bool(k, v);
3176 return 0;
3178 if (!strcmp(k, "pack.threads")) {
3179 delta_search_threads = git_config_int(k, v, ctx->kvi);
3180 if (delta_search_threads < 0)
3181 die(_("invalid number of threads specified (%d)"),
3182 delta_search_threads);
3183 if (!HAVE_THREADS && delta_search_threads != 1) {
3184 warning(_("no threads support, ignoring %s"), k);
3185 delta_search_threads = 0;
3187 return 0;
3189 if (!strcmp(k, "pack.indexversion")) {
3190 pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
3191 if (pack_idx_opts.version > 2)
3192 die(_("bad pack.indexVersion=%"PRIu32),
3193 pack_idx_opts.version);
3194 return 0;
3196 if (!strcmp(k, "pack.writereverseindex")) {
3197 if (git_config_bool(k, v))
3198 pack_idx_opts.flags |= WRITE_REV;
3199 else
3200 pack_idx_opts.flags &= ~WRITE_REV;
3201 return 0;
3203 if (!strcmp(k, "uploadpack.blobpackfileuri")) {
3204 struct configured_exclusion *ex;
3205 const char *oid_end, *pack_end;
3207 * Stores the pack hash. This is not a true object ID, but is
3208 * of the same form.
3210 struct object_id pack_hash;
3212 if (!v)
3213 return config_error_nonbool(k);
3215 ex = xmalloc(sizeof(*ex));
3216 if (parse_oid_hex(v, &ex->e.oid, &oid_end) ||
3217 *oid_end != ' ' ||
3218 parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) ||
3219 *pack_end != ' ')
3220 die(_("value of uploadpack.blobpackfileuri must be "
3221 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v);
3222 if (oidmap_get(&configured_exclusions, &ex->e.oid))
3223 die(_("object already configured in another "
3224 "uploadpack.blobpackfileuri (got '%s')"), v);
3225 ex->pack_hash_hex = xcalloc(1, pack_end - oid_end);
3226 memcpy(ex->pack_hash_hex, oid_end + 1, pack_end - oid_end - 1);
3227 ex->uri = xstrdup(pack_end + 1);
3228 oidmap_put(&configured_exclusions, ex);
3230 return git_default_config(k, v, ctx, cb);
3233 /* Counters for trace2 output when in --stdin-packs mode. */
3234 static int stdin_packs_found_nr;
3235 static int stdin_packs_hints_nr;
3237 static int add_object_entry_from_pack(const struct object_id *oid,
3238 struct packed_git *p,
3239 uint32_t pos,
3240 void *_data)
3242 off_t ofs;
3243 enum object_type type = OBJ_NONE;
3245 display_progress(progress_state, ++nr_seen);
3247 if (have_duplicate_entry(oid, 0))
3248 return 0;
3250 ofs = nth_packed_object_offset(p, pos);
3251 if (!want_object_in_pack(oid, 0, &p, &ofs))
3252 return 0;
3254 if (p) {
3255 struct rev_info *revs = _data;
3256 struct object_info oi = OBJECT_INFO_INIT;
3258 oi.typep = &type;
3259 if (packed_object_info(the_repository, p, ofs, &oi) < 0) {
3260 die(_("could not get type of object %s in pack %s"),
3261 oid_to_hex(oid), p->pack_name);
3262 } else if (type == OBJ_COMMIT) {
3264 * commits in included packs are used as starting points for the
3265 * subsequent revision walk
3267 add_pending_oid(revs, NULL, oid, 0);
3270 stdin_packs_found_nr++;
3273 create_object_entry(oid, type, 0, 0, 0, p, ofs);
3275 return 0;
3278 static void show_commit_pack_hint(struct commit *commit UNUSED,
3279 void *data UNUSED)
3281 /* nothing to do; commits don't have a namehash */
3284 static void show_object_pack_hint(struct object *object, const char *name,
3285 void *data UNUSED)
3287 struct object_entry *oe = packlist_find(&to_pack, &object->oid);
3288 if (!oe)
3289 return;
3292 * Our 'to_pack' list was constructed by iterating all objects packed in
3293 * included packs, and so doesn't have a non-zero hash field that you
3294 * would typically pick up during a reachability traversal.
3296 * Make a best-effort attempt to fill in the ->hash and ->no_try_delta
3297 * here using a now in order to perhaps improve the delta selection
3298 * process.
3300 oe->hash = pack_name_hash(name);
3301 oe->no_try_delta = name && no_try_delta(name);
3303 stdin_packs_hints_nr++;
3306 static int pack_mtime_cmp(const void *_a, const void *_b)
3308 struct packed_git *a = ((const struct string_list_item*)_a)->util;
3309 struct packed_git *b = ((const struct string_list_item*)_b)->util;
3312 * order packs by descending mtime so that objects are laid out
3313 * roughly as newest-to-oldest
3315 if (a->mtime < b->mtime)
3316 return 1;
3317 else if (b->mtime < a->mtime)
3318 return -1;
3319 else
3320 return 0;
3323 static void read_packs_list_from_stdin(void)
3325 struct strbuf buf = STRBUF_INIT;
3326 struct string_list include_packs = STRING_LIST_INIT_DUP;
3327 struct string_list exclude_packs = STRING_LIST_INIT_DUP;
3328 struct string_list_item *item = NULL;
3330 struct packed_git *p;
3331 struct rev_info revs;
3333 repo_init_revisions(the_repository, &revs, NULL);
3335 * Use a revision walk to fill in the namehash of objects in the include
3336 * packs. To save time, we'll avoid traversing through objects that are
3337 * in excluded packs.
3339 * That may cause us to avoid populating all of the namehash fields of
3340 * all included objects, but our goal is best-effort, since this is only
3341 * an optimization during delta selection.
3343 revs.no_kept_objects = 1;
3344 revs.keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
3345 revs.blob_objects = 1;
3346 revs.tree_objects = 1;
3347 revs.tag_objects = 1;
3348 revs.ignore_missing_links = 1;
3350 while (strbuf_getline(&buf, stdin) != EOF) {
3351 if (!buf.len)
3352 continue;
3354 if (*buf.buf == '^')
3355 string_list_append(&exclude_packs, buf.buf + 1);
3356 else
3357 string_list_append(&include_packs, buf.buf);
3359 strbuf_reset(&buf);
3362 string_list_sort(&include_packs);
3363 string_list_remove_duplicates(&include_packs, 0);
3364 string_list_sort(&exclude_packs);
3365 string_list_remove_duplicates(&exclude_packs, 0);
3367 for (p = get_all_packs(the_repository); p; p = p->next) {
3368 const char *pack_name = pack_basename(p);
3370 if ((item = string_list_lookup(&include_packs, pack_name)))
3371 item->util = p;
3372 if ((item = string_list_lookup(&exclude_packs, pack_name)))
3373 item->util = p;
3377 * Arguments we got on stdin may not even be packs. First
3378 * check that to avoid segfaulting later on in
3379 * e.g. pack_mtime_cmp(), excluded packs are handled below.
3381 * Since we first parsed our STDIN and then sorted the input
3382 * lines the pack we error on will be whatever line happens to
3383 * sort first. This is lazy, it's enough that we report one
3384 * bad case here, we don't need to report the first/last one,
3385 * or all of them.
3387 for_each_string_list_item(item, &include_packs) {
3388 struct packed_git *p = item->util;
3389 if (!p)
3390 die(_("could not find pack '%s'"), item->string);
3391 if (!is_pack_valid(p))
3392 die(_("packfile %s cannot be accessed"), p->pack_name);
3396 * Then, handle all of the excluded packs, marking them as
3397 * kept in-core so that later calls to add_object_entry()
3398 * discards any objects that are also found in excluded packs.
3400 for_each_string_list_item(item, &exclude_packs) {
3401 struct packed_git *p = item->util;
3402 if (!p)
3403 die(_("could not find pack '%s'"), item->string);
3404 p->pack_keep_in_core = 1;
3408 * Order packs by ascending mtime; use QSORT directly to access the
3409 * string_list_item's ->util pointer, which string_list_sort() does not
3410 * provide.
3412 QSORT(include_packs.items, include_packs.nr, pack_mtime_cmp);
3414 for_each_string_list_item(item, &include_packs) {
3415 struct packed_git *p = item->util;
3416 for_each_object_in_pack(p,
3417 add_object_entry_from_pack,
3418 &revs,
3419 FOR_EACH_OBJECT_PACK_ORDER);
3422 if (prepare_revision_walk(&revs))
3423 die(_("revision walk setup failed"));
3424 traverse_commit_list(&revs,
3425 show_commit_pack_hint,
3426 show_object_pack_hint,
3427 NULL);
3429 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_found",
3430 stdin_packs_found_nr);
3431 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_hints",
3432 stdin_packs_hints_nr);
3434 strbuf_release(&buf);
3435 string_list_clear(&include_packs, 0);
3436 string_list_clear(&exclude_packs, 0);
3439 static void add_cruft_object_entry(const struct object_id *oid, enum object_type type,
3440 struct packed_git *pack, off_t offset,
3441 const char *name, uint32_t mtime)
3443 struct object_entry *entry;
3445 display_progress(progress_state, ++nr_seen);
3447 entry = packlist_find(&to_pack, oid);
3448 if (entry) {
3449 if (name) {
3450 entry->hash = pack_name_hash(name);
3451 entry->no_try_delta = no_try_delta(name);
3453 } else {
3454 if (!want_object_in_pack(oid, 0, &pack, &offset))
3455 return;
3456 if (!pack && type == OBJ_BLOB && !has_loose_object(oid)) {
3458 * If a traversed tree has a missing blob then we want
3459 * to avoid adding that missing object to our pack.
3461 * This only applies to missing blobs, not trees,
3462 * because the traversal needs to parse sub-trees but
3463 * not blobs.
3465 * Note we only perform this check when we couldn't
3466 * already find the object in a pack, so we're really
3467 * limited to "ensure non-tip blobs which don't exist in
3468 * packs do exist via loose objects". Confused?
3470 return;
3473 entry = create_object_entry(oid, type, pack_name_hash(name),
3474 0, name && no_try_delta(name),
3475 pack, offset);
3478 if (mtime > oe_cruft_mtime(&to_pack, entry))
3479 oe_set_cruft_mtime(&to_pack, entry, mtime);
3480 return;
3483 static void show_cruft_object(struct object *obj, const char *name, void *data UNUSED)
3486 * if we did not record it earlier, it's at least as old as our
3487 * expiration value. Rather than find it exactly, just use that
3488 * value. This may bump it forward from its real mtime, but it
3489 * will still be "too old" next time we run with the same
3490 * expiration.
3492 * if obj does appear in the packing list, this call is a noop (or may
3493 * set the namehash).
3495 add_cruft_object_entry(&obj->oid, obj->type, NULL, 0, name, cruft_expiration);
3498 static void show_cruft_commit(struct commit *commit, void *data)
3500 show_cruft_object((struct object*)commit, NULL, data);
3503 static int cruft_include_check_obj(struct object *obj, void *data UNUSED)
3505 return !has_object_kept_pack(&obj->oid, IN_CORE_KEEP_PACKS);
3508 static int cruft_include_check(struct commit *commit, void *data)
3510 return cruft_include_check_obj((struct object*)commit, data);
3513 static void set_cruft_mtime(const struct object *object,
3514 struct packed_git *pack,
3515 off_t offset, time_t mtime)
3517 add_cruft_object_entry(&object->oid, object->type, pack, offset, NULL,
3518 mtime);
3521 static void mark_pack_kept_in_core(struct string_list *packs, unsigned keep)
3523 struct string_list_item *item = NULL;
3524 for_each_string_list_item(item, packs) {
3525 struct packed_git *p = item->util;
3526 if (!p)
3527 die(_("could not find pack '%s'"), item->string);
3528 p->pack_keep_in_core = keep;
3532 static void add_unreachable_loose_objects(void);
3533 static void add_objects_in_unpacked_packs(void);
3535 static void enumerate_cruft_objects(void)
3537 if (progress)
3538 progress_state = start_progress(_("Enumerating cruft objects"), 0);
3540 add_objects_in_unpacked_packs();
3541 add_unreachable_loose_objects();
3543 stop_progress(&progress_state);
3546 static void enumerate_and_traverse_cruft_objects(struct string_list *fresh_packs)
3548 struct packed_git *p;
3549 struct rev_info revs;
3550 int ret;
3552 repo_init_revisions(the_repository, &revs, NULL);
3554 revs.tag_objects = 1;
3555 revs.tree_objects = 1;
3556 revs.blob_objects = 1;
3558 revs.include_check = cruft_include_check;
3559 revs.include_check_obj = cruft_include_check_obj;
3561 revs.ignore_missing_links = 1;
3563 if (progress)
3564 progress_state = start_progress(_("Enumerating cruft objects"), 0);
3565 ret = add_unseen_recent_objects_to_traversal(&revs, cruft_expiration,
3566 set_cruft_mtime, 1);
3567 stop_progress(&progress_state);
3569 if (ret)
3570 die(_("unable to add cruft objects"));
3573 * Re-mark only the fresh packs as kept so that objects in
3574 * unknown packs do not halt the reachability traversal early.
3576 for (p = get_all_packs(the_repository); p; p = p->next)
3577 p->pack_keep_in_core = 0;
3578 mark_pack_kept_in_core(fresh_packs, 1);
3580 if (prepare_revision_walk(&revs))
3581 die(_("revision walk setup failed"));
3582 if (progress)
3583 progress_state = start_progress(_("Traversing cruft objects"), 0);
3584 nr_seen = 0;
3585 traverse_commit_list(&revs, show_cruft_commit, show_cruft_object, NULL);
3587 stop_progress(&progress_state);
3590 static void read_cruft_objects(void)
3592 struct strbuf buf = STRBUF_INIT;
3593 struct string_list discard_packs = STRING_LIST_INIT_DUP;
3594 struct string_list fresh_packs = STRING_LIST_INIT_DUP;
3595 struct packed_git *p;
3597 ignore_packed_keep_in_core = 1;
3599 while (strbuf_getline(&buf, stdin) != EOF) {
3600 if (!buf.len)
3601 continue;
3603 if (*buf.buf == '-')
3604 string_list_append(&discard_packs, buf.buf + 1);
3605 else
3606 string_list_append(&fresh_packs, buf.buf);
3609 string_list_sort(&discard_packs);
3610 string_list_sort(&fresh_packs);
3612 for (p = get_all_packs(the_repository); p; p = p->next) {
3613 const char *pack_name = pack_basename(p);
3614 struct string_list_item *item;
3616 item = string_list_lookup(&fresh_packs, pack_name);
3617 if (!item)
3618 item = string_list_lookup(&discard_packs, pack_name);
3620 if (item) {
3621 item->util = p;
3622 } else {
3624 * This pack wasn't mentioned in either the "fresh" or
3625 * "discard" list, so the caller didn't know about it.
3627 * Mark it as kept so that its objects are ignored by
3628 * add_unseen_recent_objects_to_traversal(). We'll
3629 * unmark it before starting the traversal so it doesn't
3630 * halt the traversal early.
3632 p->pack_keep_in_core = 1;
3636 mark_pack_kept_in_core(&fresh_packs, 1);
3637 mark_pack_kept_in_core(&discard_packs, 0);
3639 if (cruft_expiration)
3640 enumerate_and_traverse_cruft_objects(&fresh_packs);
3641 else
3642 enumerate_cruft_objects();
3644 strbuf_release(&buf);
3645 string_list_clear(&discard_packs, 0);
3646 string_list_clear(&fresh_packs, 0);
3649 static void read_object_list_from_stdin(void)
3651 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
3652 struct object_id oid;
3653 const char *p;
3655 for (;;) {
3656 if (!fgets(line, sizeof(line), stdin)) {
3657 if (feof(stdin))
3658 break;
3659 if (!ferror(stdin))
3660 BUG("fgets returned NULL, not EOF, not error!");
3661 if (errno != EINTR)
3662 die_errno("fgets");
3663 clearerr(stdin);
3664 continue;
3666 if (line[0] == '-') {
3667 if (get_oid_hex(line+1, &oid))
3668 die(_("expected edge object ID, got garbage:\n %s"),
3669 line);
3670 add_preferred_base(&oid);
3671 continue;
3673 if (parse_oid_hex(line, &oid, &p))
3674 die(_("expected object ID, got garbage:\n %s"), line);
3676 add_preferred_base_object(p + 1);
3677 add_object_entry(&oid, OBJ_NONE, p + 1, 0);
3681 static void show_commit(struct commit *commit, void *data UNUSED)
3683 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
3685 if (write_bitmap_index)
3686 index_commit_for_bitmap(commit);
3688 if (use_delta_islands)
3689 propagate_island_marks(commit);
3692 static void show_object(struct object *obj, const char *name,
3693 void *data UNUSED)
3695 add_preferred_base_object(name);
3696 add_object_entry(&obj->oid, obj->type, name, 0);
3698 if (use_delta_islands) {
3699 const char *p;
3700 unsigned depth;
3701 struct object_entry *ent;
3703 /* the empty string is a root tree, which is depth 0 */
3704 depth = *name ? 1 : 0;
3705 for (p = strchr(name, '/'); p; p = strchr(p + 1, '/'))
3706 depth++;
3708 ent = packlist_find(&to_pack, &obj->oid);
3709 if (ent && depth > oe_tree_depth(&to_pack, ent))
3710 oe_set_tree_depth(&to_pack, ent, depth);
3714 static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
3716 assert(arg_missing_action == MA_ALLOW_ANY);
3719 * Quietly ignore ALL missing objects. This avoids problems with
3720 * staging them now and getting an odd error later.
3722 if (!has_object(the_repository, &obj->oid, 0))
3723 return;
3725 show_object(obj, name, data);
3728 static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
3730 assert(arg_missing_action == MA_ALLOW_PROMISOR);
3733 * Quietly ignore EXPECTED missing objects. This avoids problems with
3734 * staging them now and getting an odd error later.
3736 if (!has_object(the_repository, &obj->oid, 0) && is_promisor_object(&obj->oid))
3737 return;
3739 show_object(obj, name, data);
3742 static int option_parse_missing_action(const struct option *opt UNUSED,
3743 const char *arg, int unset)
3745 assert(arg);
3746 assert(!unset);
3748 if (!strcmp(arg, "error")) {
3749 arg_missing_action = MA_ERROR;
3750 fn_show_object = show_object;
3751 return 0;
3754 if (!strcmp(arg, "allow-any")) {
3755 arg_missing_action = MA_ALLOW_ANY;
3756 fetch_if_missing = 0;
3757 fn_show_object = show_object__ma_allow_any;
3758 return 0;
3761 if (!strcmp(arg, "allow-promisor")) {
3762 arg_missing_action = MA_ALLOW_PROMISOR;
3763 fetch_if_missing = 0;
3764 fn_show_object = show_object__ma_allow_promisor;
3765 return 0;
3768 die(_("invalid value for '%s': '%s'"), "--missing", arg);
3769 return 0;
3772 static void show_edge(struct commit *commit)
3774 add_preferred_base(&commit->object.oid);
3777 static int add_object_in_unpacked_pack(const struct object_id *oid,
3778 struct packed_git *pack,
3779 uint32_t pos,
3780 void *data UNUSED)
3782 if (cruft) {
3783 off_t offset;
3784 time_t mtime;
3786 if (pack->is_cruft) {
3787 if (load_pack_mtimes(pack) < 0)
3788 die(_("could not load cruft pack .mtimes"));
3789 mtime = nth_packed_mtime(pack, pos);
3790 } else {
3791 mtime = pack->mtime;
3793 offset = nth_packed_object_offset(pack, pos);
3795 add_cruft_object_entry(oid, OBJ_NONE, pack, offset,
3796 NULL, mtime);
3797 } else {
3798 add_object_entry(oid, OBJ_NONE, "", 0);
3800 return 0;
3803 static void add_objects_in_unpacked_packs(void)
3805 if (for_each_packed_object(add_object_in_unpacked_pack, NULL,
3806 FOR_EACH_OBJECT_PACK_ORDER |
3807 FOR_EACH_OBJECT_LOCAL_ONLY |
3808 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
3809 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS))
3810 die(_("cannot open pack index"));
3813 static int add_loose_object(const struct object_id *oid, const char *path,
3814 void *data UNUSED)
3816 enum object_type type = oid_object_info(the_repository, oid, NULL);
3818 if (type < 0) {
3819 warning(_("loose object at %s could not be examined"), path);
3820 return 0;
3823 if (cruft) {
3824 struct stat st;
3825 if (stat(path, &st) < 0) {
3826 if (errno == ENOENT)
3827 return 0;
3828 return error_errno("unable to stat %s", oid_to_hex(oid));
3831 add_cruft_object_entry(oid, type, NULL, 0, NULL,
3832 st.st_mtime);
3833 } else {
3834 add_object_entry(oid, type, "", 0);
3836 return 0;
3840 * We actually don't even have to worry about reachability here.
3841 * add_object_entry will weed out duplicates, so we just add every
3842 * loose object we find.
3844 static void add_unreachable_loose_objects(void)
3846 for_each_loose_file_in_objdir(get_object_directory(),
3847 add_loose_object,
3848 NULL, NULL, NULL);
3851 static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
3853 static struct packed_git *last_found = (void *)1;
3854 struct packed_git *p;
3856 p = (last_found != (void *)1) ? last_found :
3857 get_all_packs(the_repository);
3859 while (p) {
3860 if ((!p->pack_local || p->pack_keep ||
3861 p->pack_keep_in_core) &&
3862 find_pack_entry_one(oid->hash, p)) {
3863 last_found = p;
3864 return 1;
3866 if (p == last_found)
3867 p = get_all_packs(the_repository);
3868 else
3869 p = p->next;
3870 if (p == last_found)
3871 p = p->next;
3873 return 0;
3877 * Store a list of sha1s that are should not be discarded
3878 * because they are either written too recently, or are
3879 * reachable from another object that was.
3881 * This is filled by get_object_list.
3883 static struct oid_array recent_objects;
3885 static int loosened_object_can_be_discarded(const struct object_id *oid,
3886 timestamp_t mtime)
3888 if (!unpack_unreachable_expiration)
3889 return 0;
3890 if (mtime > unpack_unreachable_expiration)
3891 return 0;
3892 if (oid_array_lookup(&recent_objects, oid) >= 0)
3893 return 0;
3894 return 1;
3897 static void loosen_unused_packed_objects(void)
3899 struct packed_git *p;
3900 uint32_t i;
3901 uint32_t loosened_objects_nr = 0;
3902 struct object_id oid;
3904 for (p = get_all_packs(the_repository); p; p = p->next) {
3905 if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
3906 continue;
3908 if (open_pack_index(p))
3909 die(_("cannot open pack index"));
3911 for (i = 0; i < p->num_objects; i++) {
3912 nth_packed_object_id(&oid, p, i);
3913 if (!packlist_find(&to_pack, &oid) &&
3914 !has_sha1_pack_kept_or_nonlocal(&oid) &&
3915 !loosened_object_can_be_discarded(&oid, p->mtime)) {
3916 if (force_object_loose(&oid, p->mtime))
3917 die(_("unable to force loose object"));
3918 loosened_objects_nr++;
3923 trace2_data_intmax("pack-objects", the_repository,
3924 "loosen_unused_packed_objects/loosened", loosened_objects_nr);
3928 * This tracks any options which pack-reuse code expects to be on, or which a
3929 * reader of the pack might not understand, and which would therefore prevent
3930 * blind reuse of what we have on disk.
3932 static int pack_options_allow_reuse(void)
3934 return allow_pack_reuse &&
3935 pack_to_stdout &&
3936 !ignore_packed_keep_on_disk &&
3937 !ignore_packed_keep_in_core &&
3938 (!local || !have_non_local_packs) &&
3939 !incremental;
3942 static int get_object_list_from_bitmap(struct rev_info *revs)
3944 if (!(bitmap_git = prepare_bitmap_walk(revs, 0)))
3945 return -1;
3947 if (pack_options_allow_reuse() &&
3948 !reuse_partial_packfile_from_bitmap(
3949 bitmap_git,
3950 &reuse_packfile,
3951 &reuse_packfile_objects,
3952 &reuse_packfile_bitmap)) {
3953 assert(reuse_packfile_objects);
3954 nr_result += reuse_packfile_objects;
3955 nr_seen += reuse_packfile_objects;
3956 display_progress(progress_state, nr_seen);
3959 traverse_bitmap_commit_list(bitmap_git, revs,
3960 &add_object_entry_from_bitmap);
3961 return 0;
3964 static void record_recent_object(struct object *obj,
3965 const char *name UNUSED,
3966 void *data UNUSED)
3968 oid_array_append(&recent_objects, &obj->oid);
3971 static void record_recent_commit(struct commit *commit, void *data UNUSED)
3973 oid_array_append(&recent_objects, &commit->object.oid);
3976 static int mark_bitmap_preferred_tip(const char *refname,
3977 const struct object_id *oid,
3978 int flags UNUSED,
3979 void *data UNUSED)
3981 struct object_id peeled;
3982 struct object *object;
3984 if (!peel_iterated_oid(oid, &peeled))
3985 oid = &peeled;
3987 object = parse_object_or_die(oid, refname);
3988 if (object->type == OBJ_COMMIT)
3989 object->flags |= NEEDS_BITMAP;
3991 return 0;
3994 static void mark_bitmap_preferred_tips(void)
3996 struct string_list_item *item;
3997 const struct string_list *preferred_tips;
3999 preferred_tips = bitmap_preferred_tips(the_repository);
4000 if (!preferred_tips)
4001 return;
4003 for_each_string_list_item(item, preferred_tips) {
4004 for_each_ref_in(item->string, mark_bitmap_preferred_tip, NULL);
4008 static void get_object_list(struct rev_info *revs, int ac, const char **av)
4010 struct setup_revision_opt s_r_opt = {
4011 .allow_exclude_promisor_objects = 1,
4013 char line[1000];
4014 int flags = 0;
4015 int save_warning;
4017 save_commit_buffer = 0;
4018 setup_revisions(ac, av, revs, &s_r_opt);
4020 /* make sure shallows are read */
4021 is_repository_shallow(the_repository);
4023 save_warning = warn_on_object_refname_ambiguity;
4024 warn_on_object_refname_ambiguity = 0;
4026 while (fgets(line, sizeof(line), stdin) != NULL) {
4027 int len = strlen(line);
4028 if (len && line[len - 1] == '\n')
4029 line[--len] = 0;
4030 if (!len)
4031 break;
4032 if (*line == '-') {
4033 if (!strcmp(line, "--not")) {
4034 flags ^= UNINTERESTING;
4035 write_bitmap_index = 0;
4036 continue;
4038 if (starts_with(line, "--shallow ")) {
4039 struct object_id oid;
4040 if (get_oid_hex(line + 10, &oid))
4041 die("not an object name '%s'", line + 10);
4042 register_shallow(the_repository, &oid);
4043 use_bitmap_index = 0;
4044 continue;
4046 die(_("not a rev '%s'"), line);
4048 if (handle_revision_arg(line, revs, flags, REVARG_CANNOT_BE_FILENAME))
4049 die(_("bad revision '%s'"), line);
4052 warn_on_object_refname_ambiguity = save_warning;
4054 if (use_bitmap_index && !get_object_list_from_bitmap(revs))
4055 return;
4057 if (use_delta_islands)
4058 load_delta_islands(the_repository, progress);
4060 if (write_bitmap_index)
4061 mark_bitmap_preferred_tips();
4063 if (prepare_revision_walk(revs))
4064 die(_("revision walk setup failed"));
4065 mark_edges_uninteresting(revs, show_edge, sparse);
4067 if (!fn_show_object)
4068 fn_show_object = show_object;
4069 traverse_commit_list(revs,
4070 show_commit, fn_show_object,
4071 NULL);
4073 if (unpack_unreachable_expiration) {
4074 revs->ignore_missing_links = 1;
4075 if (add_unseen_recent_objects_to_traversal(revs,
4076 unpack_unreachable_expiration, NULL, 0))
4077 die(_("unable to add recent objects"));
4078 if (prepare_revision_walk(revs))
4079 die(_("revision walk setup failed"));
4080 traverse_commit_list(revs, record_recent_commit,
4081 record_recent_object, NULL);
4084 if (keep_unreachable)
4085 add_objects_in_unpacked_packs();
4086 if (pack_loose_unreachable)
4087 add_unreachable_loose_objects();
4088 if (unpack_unreachable)
4089 loosen_unused_packed_objects();
4091 oid_array_clear(&recent_objects);
4094 static void add_extra_kept_packs(const struct string_list *names)
4096 struct packed_git *p;
4098 if (!names->nr)
4099 return;
4101 for (p = get_all_packs(the_repository); p; p = p->next) {
4102 const char *name = basename(p->pack_name);
4103 int i;
4105 if (!p->pack_local)
4106 continue;
4108 for (i = 0; i < names->nr; i++)
4109 if (!fspathcmp(name, names->items[i].string))
4110 break;
4112 if (i < names->nr) {
4113 p->pack_keep_in_core = 1;
4114 ignore_packed_keep_in_core = 1;
4115 continue;
4120 static int option_parse_quiet(const struct option *opt, const char *arg,
4121 int unset)
4123 int *val = opt->value;
4125 BUG_ON_OPT_ARG(arg);
4127 if (!unset)
4128 *val = 0;
4129 else if (!*val)
4130 *val = 1;
4131 return 0;
4134 static int option_parse_index_version(const struct option *opt,
4135 const char *arg, int unset)
4137 struct pack_idx_option *popts = opt->value;
4138 char *c;
4139 const char *val = arg;
4141 BUG_ON_OPT_NEG(unset);
4143 popts->version = strtoul(val, &c, 10);
4144 if (popts->version > 2)
4145 die(_("unsupported index version %s"), val);
4146 if (*c == ',' && c[1])
4147 popts->off32_limit = strtoul(c+1, &c, 0);
4148 if (*c || popts->off32_limit & 0x80000000)
4149 die(_("bad index version '%s'"), val);
4150 return 0;
4153 static int option_parse_unpack_unreachable(const struct option *opt UNUSED,
4154 const char *arg, int unset)
4156 if (unset) {
4157 unpack_unreachable = 0;
4158 unpack_unreachable_expiration = 0;
4160 else {
4161 unpack_unreachable = 1;
4162 if (arg)
4163 unpack_unreachable_expiration = approxidate(arg);
4165 return 0;
4168 static int option_parse_cruft_expiration(const struct option *opt UNUSED,
4169 const char *arg, int unset)
4171 if (unset) {
4172 cruft = 0;
4173 cruft_expiration = 0;
4174 } else {
4175 cruft = 1;
4176 if (arg)
4177 cruft_expiration = approxidate(arg);
4179 return 0;
4182 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
4184 int use_internal_rev_list = 0;
4185 int shallow = 0;
4186 int all_progress_implied = 0;
4187 struct strvec rp = STRVEC_INIT;
4188 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
4189 int rev_list_index = 0;
4190 int stdin_packs = 0;
4191 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
4192 struct list_objects_filter_options filter_options =
4193 LIST_OBJECTS_FILTER_INIT;
4195 struct option pack_objects_options[] = {
4196 OPT_CALLBACK_F('q', "quiet", &progress, NULL,
4197 N_("do not show progress meter"),
4198 PARSE_OPT_NOARG, option_parse_quiet),
4199 OPT_SET_INT(0, "progress", &progress,
4200 N_("show progress meter"), 1),
4201 OPT_SET_INT(0, "all-progress", &progress,
4202 N_("show progress meter during object writing phase"), 2),
4203 OPT_BOOL(0, "all-progress-implied",
4204 &all_progress_implied,
4205 N_("similar to --all-progress when progress meter is shown")),
4206 OPT_CALLBACK_F(0, "index-version", &pack_idx_opts, N_("<version>[,<offset>]"),
4207 N_("write the pack index file in the specified idx format version"),
4208 PARSE_OPT_NONEG, option_parse_index_version),
4209 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
4210 N_("maximum size of each output pack file")),
4211 OPT_BOOL(0, "local", &local,
4212 N_("ignore borrowed objects from alternate object store")),
4213 OPT_BOOL(0, "incremental", &incremental,
4214 N_("ignore packed objects")),
4215 OPT_INTEGER(0, "window", &window,
4216 N_("limit pack window by objects")),
4217 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
4218 N_("limit pack window by memory in addition to object limit")),
4219 OPT_INTEGER(0, "depth", &depth,
4220 N_("maximum length of delta chain allowed in the resulting pack")),
4221 OPT_BOOL(0, "reuse-delta", &reuse_delta,
4222 N_("reuse existing deltas")),
4223 OPT_BOOL(0, "reuse-object", &reuse_object,
4224 N_("reuse existing objects")),
4225 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
4226 N_("use OFS_DELTA objects")),
4227 OPT_INTEGER(0, "threads", &delta_search_threads,
4228 N_("use threads when searching for best delta matches")),
4229 OPT_BOOL(0, "non-empty", &non_empty,
4230 N_("do not create an empty pack output")),
4231 OPT_BOOL(0, "revs", &use_internal_rev_list,
4232 N_("read revision arguments from standard input")),
4233 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked,
4234 N_("limit the objects to those that are not yet packed"),
4235 1, PARSE_OPT_NONEG),
4236 OPT_SET_INT_F(0, "all", &rev_list_all,
4237 N_("include objects reachable from any reference"),
4238 1, PARSE_OPT_NONEG),
4239 OPT_SET_INT_F(0, "reflog", &rev_list_reflog,
4240 N_("include objects referred by reflog entries"),
4241 1, PARSE_OPT_NONEG),
4242 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index,
4243 N_("include objects referred to by the index"),
4244 1, PARSE_OPT_NONEG),
4245 OPT_BOOL(0, "stdin-packs", &stdin_packs,
4246 N_("read packs from stdin")),
4247 OPT_BOOL(0, "stdout", &pack_to_stdout,
4248 N_("output pack to stdout")),
4249 OPT_BOOL(0, "include-tag", &include_tag,
4250 N_("include tag objects that refer to objects to be packed")),
4251 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
4252 N_("keep unreachable objects")),
4253 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
4254 N_("pack loose unreachable objects")),
4255 OPT_CALLBACK_F(0, "unpack-unreachable", NULL, N_("time"),
4256 N_("unpack unreachable objects newer than <time>"),
4257 PARSE_OPT_OPTARG, option_parse_unpack_unreachable),
4258 OPT_BOOL(0, "cruft", &cruft, N_("create a cruft pack")),
4259 OPT_CALLBACK_F(0, "cruft-expiration", NULL, N_("time"),
4260 N_("expire cruft objects older than <time>"),
4261 PARSE_OPT_OPTARG, option_parse_cruft_expiration),
4262 OPT_BOOL(0, "sparse", &sparse,
4263 N_("use the sparse reachability algorithm")),
4264 OPT_BOOL(0, "thin", &thin,
4265 N_("create thin packs")),
4266 OPT_BOOL(0, "shallow", &shallow,
4267 N_("create packs suitable for shallow fetches")),
4268 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
4269 N_("ignore packs that have companion .keep file")),
4270 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
4271 N_("ignore this pack")),
4272 OPT_INTEGER(0, "compression", &pack_compression_level,
4273 N_("pack compression level")),
4274 OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents,
4275 N_("do not hide commits by grafts")),
4276 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
4277 N_("use a bitmap index if available to speed up counting objects")),
4278 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index,
4279 N_("write a bitmap index together with the pack index"),
4280 WRITE_BITMAP_TRUE),
4281 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4282 &write_bitmap_index,
4283 N_("write a bitmap index if possible"),
4284 WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN),
4285 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
4286 OPT_CALLBACK_F(0, "missing", NULL, N_("action"),
4287 N_("handling for missing objects"), PARSE_OPT_NONEG,
4288 option_parse_missing_action),
4289 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
4290 N_("do not pack objects in promisor packfiles")),
4291 OPT_BOOL(0, "delta-islands", &use_delta_islands,
4292 N_("respect islands during delta compression")),
4293 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols,
4294 N_("protocol"),
4295 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
4296 OPT_END(),
4299 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
4300 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4302 disable_replace_refs();
4304 sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
4305 if (the_repository->gitdir) {
4306 prepare_repo_settings(the_repository);
4307 if (sparse < 0)
4308 sparse = the_repository->settings.pack_use_sparse;
4311 reset_pack_idx_option(&pack_idx_opts);
4312 pack_idx_opts.flags |= WRITE_REV;
4313 git_config(git_pack_config, NULL);
4314 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0))
4315 pack_idx_opts.flags &= ~WRITE_REV;
4317 progress = isatty(2);
4318 argc = parse_options(argc, argv, prefix, pack_objects_options,
4319 pack_usage, 0);
4321 if (argc) {
4322 base_name = argv[0];
4323 argc--;
4325 if (pack_to_stdout != !base_name || argc)
4326 usage_with_options(pack_usage, pack_objects_options);
4328 if (depth < 0)
4329 depth = 0;
4330 if (depth >= (1 << OE_DEPTH_BITS)) {
4331 warning(_("delta chain depth %d is too deep, forcing %d"),
4332 depth, (1 << OE_DEPTH_BITS) - 1);
4333 depth = (1 << OE_DEPTH_BITS) - 1;
4335 if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
4336 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4337 (1U << OE_Z_DELTA_BITS) - 1);
4338 cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
4340 if (window < 0)
4341 window = 0;
4343 strvec_push(&rp, "pack-objects");
4344 if (thin) {
4345 use_internal_rev_list = 1;
4346 strvec_push(&rp, shallow
4347 ? "--objects-edge-aggressive"
4348 : "--objects-edge");
4349 } else
4350 strvec_push(&rp, "--objects");
4352 if (rev_list_all) {
4353 use_internal_rev_list = 1;
4354 strvec_push(&rp, "--all");
4356 if (rev_list_reflog) {
4357 use_internal_rev_list = 1;
4358 strvec_push(&rp, "--reflog");
4360 if (rev_list_index) {
4361 use_internal_rev_list = 1;
4362 strvec_push(&rp, "--indexed-objects");
4364 if (rev_list_unpacked && !stdin_packs) {
4365 use_internal_rev_list = 1;
4366 strvec_push(&rp, "--unpacked");
4369 if (exclude_promisor_objects) {
4370 use_internal_rev_list = 1;
4371 fetch_if_missing = 0;
4372 strvec_push(&rp, "--exclude-promisor-objects");
4374 if (unpack_unreachable || keep_unreachable || pack_loose_unreachable)
4375 use_internal_rev_list = 1;
4377 if (!reuse_object)
4378 reuse_delta = 0;
4379 if (pack_compression_level == -1)
4380 pack_compression_level = Z_DEFAULT_COMPRESSION;
4381 else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
4382 die(_("bad pack compression level %d"), pack_compression_level);
4384 if (!delta_search_threads) /* --threads=0 means autodetect */
4385 delta_search_threads = online_cpus();
4387 if (!HAVE_THREADS && delta_search_threads != 1)
4388 warning(_("no threads support, ignoring --threads"));
4389 if (!pack_to_stdout && !pack_size_limit)
4390 pack_size_limit = pack_size_limit_cfg;
4391 if (pack_to_stdout && pack_size_limit)
4392 die(_("--max-pack-size cannot be used to build a pack for transfer"));
4393 if (pack_size_limit && pack_size_limit < 1024*1024) {
4394 warning(_("minimum pack size limit is 1 MiB"));
4395 pack_size_limit = 1024*1024;
4398 if (!pack_to_stdout && thin)
4399 die(_("--thin cannot be used to build an indexable pack"));
4401 if (keep_unreachable && unpack_unreachable)
4402 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
4403 if (!rev_list_all || !rev_list_reflog || !rev_list_index)
4404 unpack_unreachable_expiration = 0;
4406 if (stdin_packs && filter_options.choice)
4407 die(_("cannot use --filter with --stdin-packs"));
4409 if (stdin_packs && use_internal_rev_list)
4410 die(_("cannot use internal rev list with --stdin-packs"));
4412 if (cruft) {
4413 if (use_internal_rev_list)
4414 die(_("cannot use internal rev list with --cruft"));
4415 if (stdin_packs)
4416 die(_("cannot use --stdin-packs with --cruft"));
4420 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
4422 * - to produce good pack (with bitmap index not-yet-packed objects are
4423 * packed in suboptimal order).
4425 * - to use more robust pack-generation codepath (avoiding possible
4426 * bugs in bitmap code and possible bitmap index corruption).
4428 if (!pack_to_stdout)
4429 use_bitmap_index_default = 0;
4431 if (use_bitmap_index < 0)
4432 use_bitmap_index = use_bitmap_index_default;
4434 /* "hard" reasons not to use bitmaps; these just won't work at all */
4435 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow(the_repository))
4436 use_bitmap_index = 0;
4438 if (pack_to_stdout || !rev_list_all)
4439 write_bitmap_index = 0;
4441 if (use_delta_islands)
4442 strvec_push(&rp, "--topo-order");
4444 if (progress && all_progress_implied)
4445 progress = 2;
4447 add_extra_kept_packs(&keep_pack_list);
4448 if (ignore_packed_keep_on_disk) {
4449 struct packed_git *p;
4450 for (p = get_all_packs(the_repository); p; p = p->next)
4451 if (p->pack_local && p->pack_keep)
4452 break;
4453 if (!p) /* no keep-able packs found */
4454 ignore_packed_keep_on_disk = 0;
4456 if (local) {
4458 * unlike ignore_packed_keep_on_disk above, we do not
4459 * want to unset "local" based on looking at packs, as
4460 * it also covers non-local objects
4462 struct packed_git *p;
4463 for (p = get_all_packs(the_repository); p; p = p->next) {
4464 if (!p->pack_local) {
4465 have_non_local_packs = 1;
4466 break;
4471 trace2_region_enter("pack-objects", "enumerate-objects",
4472 the_repository);
4473 prepare_packing_data(the_repository, &to_pack);
4475 if (progress && !cruft)
4476 progress_state = start_progress(_("Enumerating objects"), 0);
4477 if (stdin_packs) {
4478 /* avoids adding objects in excluded packs */
4479 ignore_packed_keep_in_core = 1;
4480 read_packs_list_from_stdin();
4481 if (rev_list_unpacked)
4482 add_unreachable_loose_objects();
4483 } else if (cruft) {
4484 read_cruft_objects();
4485 } else if (!use_internal_rev_list) {
4486 read_object_list_from_stdin();
4487 } else {
4488 struct rev_info revs;
4490 repo_init_revisions(the_repository, &revs, NULL);
4491 list_objects_filter_copy(&revs.filter, &filter_options);
4492 get_object_list(&revs, rp.nr, rp.v);
4493 release_revisions(&revs);
4495 cleanup_preferred_base();
4496 if (include_tag && nr_result)
4497 for_each_tag_ref(add_ref_tag, NULL);
4498 stop_progress(&progress_state);
4499 trace2_region_leave("pack-objects", "enumerate-objects",
4500 the_repository);
4502 if (non_empty && !nr_result)
4503 goto cleanup;
4504 if (nr_result) {
4505 trace2_region_enter("pack-objects", "prepare-pack",
4506 the_repository);
4507 prepare_pack(window, depth);
4508 trace2_region_leave("pack-objects", "prepare-pack",
4509 the_repository);
4512 trace2_region_enter("pack-objects", "write-pack-file", the_repository);
4513 write_excluded_by_configs();
4514 write_pack_file();
4515 trace2_region_leave("pack-objects", "write-pack-file", the_repository);
4517 if (progress)
4518 fprintf_ln(stderr,
4519 _("Total %"PRIu32" (delta %"PRIu32"),"
4520 " reused %"PRIu32" (delta %"PRIu32"),"
4521 " pack-reused %"PRIu32),
4522 written, written_delta, reused, reused_delta,
4523 reuse_packfile_objects);
4525 cleanup:
4526 list_objects_filter_release(&filter_options);
4527 strvec_clear(&rp);
4529 return 0;