config: handle NULL value when parsing non-bools
[alt-git.git] / builtin / pack-objects.c
blob62c540b4db3ae4c334cb6709c1b5505e3d50912b
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 "blob.h"
10 #include "commit.h"
11 #include "tag.h"
12 #include "tree.h"
13 #include "delta.h"
14 #include "pack.h"
15 #include "pack-revindex.h"
16 #include "csum-file.h"
17 #include "tree-walk.h"
18 #include "diff.h"
19 #include "revision.h"
20 #include "list-objects.h"
21 #include "list-objects-filter.h"
22 #include "list-objects-filter-options.h"
23 #include "pack-objects.h"
24 #include "progress.h"
25 #include "refs.h"
26 #include "streaming.h"
27 #include "thread-utils.h"
28 #include "pack-bitmap.h"
29 #include "delta-islands.h"
30 #include "reachable.h"
31 #include "oid-array.h"
32 #include "strvec.h"
33 #include "list.h"
34 #include "packfile.h"
35 #include "object-file.h"
36 #include "object-store-ll.h"
37 #include "replace-object.h"
38 #include "dir.h"
39 #include "midx.h"
40 #include "trace2.h"
41 #include "shallow.h"
42 #include "promisor-remote.h"
43 #include "pack-mtimes.h"
44 #include "parse-options.h"
47 * Objects we are going to pack are collected in the `to_pack` structure.
48 * It contains an array (dynamically expanded) of the object data, and a map
49 * that can resolve SHA1s to their position in the array.
51 static struct packing_data to_pack;
53 static inline struct object_entry *oe_delta(
54 const struct packing_data *pack,
55 const struct object_entry *e)
57 if (!e->delta_idx)
58 return NULL;
59 if (e->ext_base)
60 return &pack->ext_bases[e->delta_idx - 1];
61 else
62 return &pack->objects[e->delta_idx - 1];
65 static inline unsigned long oe_delta_size(struct packing_data *pack,
66 const struct object_entry *e)
68 if (e->delta_size_valid)
69 return e->delta_size_;
72 * pack->delta_size[] can't be NULL because oe_set_delta_size()
73 * must have been called when a new delta is saved with
74 * oe_set_delta().
75 * If oe_delta() returns NULL (i.e. default state, which means
76 * delta_size_valid is also false), then the caller must never
77 * call oe_delta_size().
79 return pack->delta_size[e - pack->objects];
82 unsigned long oe_get_size_slow(struct packing_data *pack,
83 const struct object_entry *e);
85 static inline unsigned long oe_size(struct packing_data *pack,
86 const struct object_entry *e)
88 if (e->size_valid)
89 return e->size_;
91 return oe_get_size_slow(pack, e);
94 static inline void oe_set_delta(struct packing_data *pack,
95 struct object_entry *e,
96 struct object_entry *delta)
98 if (delta)
99 e->delta_idx = (delta - pack->objects) + 1;
100 else
101 e->delta_idx = 0;
104 static inline struct object_entry *oe_delta_sibling(
105 const struct packing_data *pack,
106 const struct object_entry *e)
108 if (e->delta_sibling_idx)
109 return &pack->objects[e->delta_sibling_idx - 1];
110 return NULL;
113 static inline struct object_entry *oe_delta_child(
114 const struct packing_data *pack,
115 const struct object_entry *e)
117 if (e->delta_child_idx)
118 return &pack->objects[e->delta_child_idx - 1];
119 return NULL;
122 static inline void oe_set_delta_child(struct packing_data *pack,
123 struct object_entry *e,
124 struct object_entry *delta)
126 if (delta)
127 e->delta_child_idx = (delta - pack->objects) + 1;
128 else
129 e->delta_child_idx = 0;
132 static inline void oe_set_delta_sibling(struct packing_data *pack,
133 struct object_entry *e,
134 struct object_entry *delta)
136 if (delta)
137 e->delta_sibling_idx = (delta - pack->objects) + 1;
138 else
139 e->delta_sibling_idx = 0;
142 static inline void oe_set_size(struct packing_data *pack,
143 struct object_entry *e,
144 unsigned long size)
146 if (size < pack->oe_size_limit) {
147 e->size_ = size;
148 e->size_valid = 1;
149 } else {
150 e->size_valid = 0;
151 if (oe_get_size_slow(pack, e) != size)
152 BUG("'size' is supposed to be the object size!");
156 static inline void oe_set_delta_size(struct packing_data *pack,
157 struct object_entry *e,
158 unsigned long size)
160 if (size < pack->oe_delta_size_limit) {
161 e->delta_size_ = size;
162 e->delta_size_valid = 1;
163 } else {
164 packing_data_lock(pack);
165 if (!pack->delta_size)
166 ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
167 packing_data_unlock(pack);
169 pack->delta_size[e - pack->objects] = size;
170 e->delta_size_valid = 0;
174 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
175 #define SIZE(obj) oe_size(&to_pack, obj)
176 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
177 #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
178 #define DELTA(obj) oe_delta(&to_pack, obj)
179 #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
180 #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
181 #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
182 #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
183 #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
184 #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
185 #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
187 static const char *pack_usage[] = {
188 N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"),
189 N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"),
190 NULL
193 static struct pack_idx_entry **written_list;
194 static uint32_t nr_result, nr_written, nr_seen;
195 static struct bitmap_index *bitmap_git;
196 static uint32_t write_layer;
198 static int non_empty;
199 static int reuse_delta = 1, reuse_object = 1;
200 static int keep_unreachable, unpack_unreachable, include_tag;
201 static timestamp_t unpack_unreachable_expiration;
202 static int pack_loose_unreachable;
203 static int cruft;
204 static timestamp_t cruft_expiration;
205 static int local;
206 static int have_non_local_packs;
207 static int incremental;
208 static int ignore_packed_keep_on_disk;
209 static int ignore_packed_keep_in_core;
210 static int allow_ofs_delta;
211 static struct pack_idx_option pack_idx_opts;
212 static const char *base_name;
213 static int progress = 1;
214 static int window = 10;
215 static unsigned long pack_size_limit;
216 static int depth = 50;
217 static int delta_search_threads;
218 static int pack_to_stdout;
219 static int sparse;
220 static int thin;
221 static int num_preferred_base;
222 static struct progress *progress_state;
224 static struct packed_git *reuse_packfile;
225 static uint32_t reuse_packfile_objects;
226 static struct bitmap *reuse_packfile_bitmap;
228 static int use_bitmap_index_default = 1;
229 static int use_bitmap_index = -1;
230 static int allow_pack_reuse = 1;
231 static enum {
232 WRITE_BITMAP_FALSE = 0,
233 WRITE_BITMAP_QUIET,
234 WRITE_BITMAP_TRUE,
235 } write_bitmap_index;
236 static uint16_t write_bitmap_options = BITMAP_OPT_HASH_CACHE;
238 static int exclude_promisor_objects;
240 static int use_delta_islands;
242 static unsigned long delta_cache_size = 0;
243 static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
244 static unsigned long cache_max_small_delta_size = 1000;
246 static unsigned long window_memory_limit = 0;
248 static struct string_list uri_protocols = STRING_LIST_INIT_NODUP;
250 enum missing_action {
251 MA_ERROR = 0, /* fail if any missing objects are encountered */
252 MA_ALLOW_ANY, /* silently allow ALL missing objects */
253 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
255 static enum missing_action arg_missing_action;
256 static show_object_fn fn_show_object;
258 struct configured_exclusion {
259 struct oidmap_entry e;
260 char *pack_hash_hex;
261 char *uri;
263 static struct oidmap configured_exclusions;
265 static struct oidset excluded_by_config;
268 * stats
270 static uint32_t written, written_delta;
271 static uint32_t reused, reused_delta;
274 * Indexed commits
276 static struct commit **indexed_commits;
277 static unsigned int indexed_commits_nr;
278 static unsigned int indexed_commits_alloc;
280 static void index_commit_for_bitmap(struct commit *commit)
282 if (indexed_commits_nr >= indexed_commits_alloc) {
283 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
284 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
287 indexed_commits[indexed_commits_nr++] = commit;
290 static void *get_delta(struct object_entry *entry)
292 unsigned long size, base_size, delta_size;
293 void *buf, *base_buf, *delta_buf;
294 enum object_type type;
296 buf = repo_read_object_file(the_repository, &entry->idx.oid, &type,
297 &size);
298 if (!buf)
299 die(_("unable to read %s"), oid_to_hex(&entry->idx.oid));
300 base_buf = repo_read_object_file(the_repository,
301 &DELTA(entry)->idx.oid, &type,
302 &base_size);
303 if (!base_buf)
304 die("unable to read %s",
305 oid_to_hex(&DELTA(entry)->idx.oid));
306 delta_buf = diff_delta(base_buf, base_size,
307 buf, size, &delta_size, 0);
309 * We successfully computed this delta once but dropped it for
310 * memory reasons. Something is very wrong if this time we
311 * recompute and create a different delta.
313 if (!delta_buf || delta_size != DELTA_SIZE(entry))
314 BUG("delta size changed");
315 free(buf);
316 free(base_buf);
317 return delta_buf;
320 static unsigned long do_compress(void **pptr, unsigned long size)
322 git_zstream stream;
323 void *in, *out;
324 unsigned long maxsize;
326 git_deflate_init(&stream, pack_compression_level);
327 maxsize = git_deflate_bound(&stream, size);
329 in = *pptr;
330 out = xmalloc(maxsize);
331 *pptr = out;
333 stream.next_in = in;
334 stream.avail_in = size;
335 stream.next_out = out;
336 stream.avail_out = maxsize;
337 while (git_deflate(&stream, Z_FINISH) == Z_OK)
338 ; /* nothing */
339 git_deflate_end(&stream);
341 free(in);
342 return stream.total_out;
345 static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f,
346 const struct object_id *oid)
348 git_zstream stream;
349 unsigned char ibuf[1024 * 16];
350 unsigned char obuf[1024 * 16];
351 unsigned long olen = 0;
353 git_deflate_init(&stream, pack_compression_level);
355 for (;;) {
356 ssize_t readlen;
357 int zret = Z_OK;
358 readlen = read_istream(st, ibuf, sizeof(ibuf));
359 if (readlen == -1)
360 die(_("unable to read %s"), oid_to_hex(oid));
362 stream.next_in = ibuf;
363 stream.avail_in = readlen;
364 while ((stream.avail_in || readlen == 0) &&
365 (zret == Z_OK || zret == Z_BUF_ERROR)) {
366 stream.next_out = obuf;
367 stream.avail_out = sizeof(obuf);
368 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
369 hashwrite(f, obuf, stream.next_out - obuf);
370 olen += stream.next_out - obuf;
372 if (stream.avail_in)
373 die(_("deflate error (%d)"), zret);
374 if (readlen == 0) {
375 if (zret != Z_STREAM_END)
376 die(_("deflate error (%d)"), zret);
377 break;
380 git_deflate_end(&stream);
381 return olen;
385 * we are going to reuse the existing object data as is. make
386 * sure it is not corrupt.
388 static int check_pack_inflate(struct packed_git *p,
389 struct pack_window **w_curs,
390 off_t offset,
391 off_t len,
392 unsigned long expect)
394 git_zstream stream;
395 unsigned char fakebuf[4096], *in;
396 int st;
398 memset(&stream, 0, sizeof(stream));
399 git_inflate_init(&stream);
400 do {
401 in = use_pack(p, w_curs, offset, &stream.avail_in);
402 stream.next_in = in;
403 stream.next_out = fakebuf;
404 stream.avail_out = sizeof(fakebuf);
405 st = git_inflate(&stream, Z_FINISH);
406 offset += stream.next_in - in;
407 } while (st == Z_OK || st == Z_BUF_ERROR);
408 git_inflate_end(&stream);
409 return (st == Z_STREAM_END &&
410 stream.total_out == expect &&
411 stream.total_in == len) ? 0 : -1;
414 static void copy_pack_data(struct hashfile *f,
415 struct packed_git *p,
416 struct pack_window **w_curs,
417 off_t offset,
418 off_t len)
420 unsigned char *in;
421 unsigned long avail;
423 while (len) {
424 in = use_pack(p, w_curs, offset, &avail);
425 if (avail > len)
426 avail = (unsigned long)len;
427 hashwrite(f, in, avail);
428 offset += avail;
429 len -= avail;
433 static inline int oe_size_greater_than(struct packing_data *pack,
434 const struct object_entry *lhs,
435 unsigned long rhs)
437 if (lhs->size_valid)
438 return lhs->size_ > rhs;
439 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
440 return 1;
441 return oe_get_size_slow(pack, lhs) > rhs;
444 /* Return 0 if we will bust the pack-size limit */
445 static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
446 unsigned long limit, int usable_delta)
448 unsigned long size, datalen;
449 unsigned char header[MAX_PACK_OBJECT_HEADER],
450 dheader[MAX_PACK_OBJECT_HEADER];
451 unsigned hdrlen;
452 enum object_type type;
453 void *buf;
454 struct git_istream *st = NULL;
455 const unsigned hashsz = the_hash_algo->rawsz;
457 if (!usable_delta) {
458 if (oe_type(entry) == OBJ_BLOB &&
459 oe_size_greater_than(&to_pack, entry, big_file_threshold) &&
460 (st = open_istream(the_repository, &entry->idx.oid, &type,
461 &size, NULL)) != NULL)
462 buf = NULL;
463 else {
464 buf = repo_read_object_file(the_repository,
465 &entry->idx.oid, &type,
466 &size);
467 if (!buf)
468 die(_("unable to read %s"),
469 oid_to_hex(&entry->idx.oid));
472 * make sure no cached delta data remains from a
473 * previous attempt before a pack split occurred.
475 FREE_AND_NULL(entry->delta_data);
476 entry->z_delta_size = 0;
477 } else if (entry->delta_data) {
478 size = DELTA_SIZE(entry);
479 buf = entry->delta_data;
480 entry->delta_data = NULL;
481 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
482 OBJ_OFS_DELTA : OBJ_REF_DELTA;
483 } else {
484 buf = get_delta(entry);
485 size = DELTA_SIZE(entry);
486 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
487 OBJ_OFS_DELTA : OBJ_REF_DELTA;
490 if (st) /* large blob case, just assume we don't compress well */
491 datalen = size;
492 else if (entry->z_delta_size)
493 datalen = entry->z_delta_size;
494 else
495 datalen = do_compress(&buf, size);
498 * The object header is a byte of 'type' followed by zero or
499 * more bytes of length.
501 hdrlen = encode_in_pack_object_header(header, sizeof(header),
502 type, size);
504 if (type == OBJ_OFS_DELTA) {
506 * Deltas with relative base contain an additional
507 * encoding of the relative offset for the delta
508 * base from this object's position in the pack.
510 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
511 unsigned pos = sizeof(dheader) - 1;
512 dheader[pos] = ofs & 127;
513 while (ofs >>= 7)
514 dheader[--pos] = 128 | (--ofs & 127);
515 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
516 if (st)
517 close_istream(st);
518 free(buf);
519 return 0;
521 hashwrite(f, header, hdrlen);
522 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
523 hdrlen += sizeof(dheader) - pos;
524 } else if (type == OBJ_REF_DELTA) {
526 * Deltas with a base reference contain
527 * additional bytes for the base object ID.
529 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
530 if (st)
531 close_istream(st);
532 free(buf);
533 return 0;
535 hashwrite(f, header, hdrlen);
536 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
537 hdrlen += hashsz;
538 } else {
539 if (limit && hdrlen + datalen + hashsz >= limit) {
540 if (st)
541 close_istream(st);
542 free(buf);
543 return 0;
545 hashwrite(f, header, hdrlen);
547 if (st) {
548 datalen = write_large_blob_data(st, f, &entry->idx.oid);
549 close_istream(st);
550 } else {
551 hashwrite(f, buf, datalen);
552 free(buf);
555 return hdrlen + datalen;
558 /* Return 0 if we will bust the pack-size limit */
559 static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
560 unsigned long limit, int usable_delta)
562 struct packed_git *p = IN_PACK(entry);
563 struct pack_window *w_curs = NULL;
564 uint32_t pos;
565 off_t offset;
566 enum object_type type = oe_type(entry);
567 off_t datalen;
568 unsigned char header[MAX_PACK_OBJECT_HEADER],
569 dheader[MAX_PACK_OBJECT_HEADER];
570 unsigned hdrlen;
571 const unsigned hashsz = the_hash_algo->rawsz;
572 unsigned long entry_size = SIZE(entry);
574 if (DELTA(entry))
575 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
576 OBJ_OFS_DELTA : OBJ_REF_DELTA;
577 hdrlen = encode_in_pack_object_header(header, sizeof(header),
578 type, entry_size);
580 offset = entry->in_pack_offset;
581 if (offset_to_pack_pos(p, offset, &pos) < 0)
582 die(_("write_reuse_object: could not locate %s, expected at "
583 "offset %"PRIuMAX" in pack %s"),
584 oid_to_hex(&entry->idx.oid), (uintmax_t)offset,
585 p->pack_name);
586 datalen = pack_pos_to_offset(p, pos + 1) - offset;
587 if (!pack_to_stdout && p->index_version > 1 &&
588 check_pack_crc(p, &w_curs, offset, datalen,
589 pack_pos_to_index(p, pos))) {
590 error(_("bad packed object CRC for %s"),
591 oid_to_hex(&entry->idx.oid));
592 unuse_pack(&w_curs);
593 return write_no_reuse_object(f, entry, limit, usable_delta);
596 offset += entry->in_pack_header_size;
597 datalen -= entry->in_pack_header_size;
599 if (!pack_to_stdout && p->index_version == 1 &&
600 check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) {
601 error(_("corrupt packed object for %s"),
602 oid_to_hex(&entry->idx.oid));
603 unuse_pack(&w_curs);
604 return write_no_reuse_object(f, entry, limit, usable_delta);
607 if (type == OBJ_OFS_DELTA) {
608 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
609 unsigned pos = sizeof(dheader) - 1;
610 dheader[pos] = ofs & 127;
611 while (ofs >>= 7)
612 dheader[--pos] = 128 | (--ofs & 127);
613 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
614 unuse_pack(&w_curs);
615 return 0;
617 hashwrite(f, header, hdrlen);
618 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
619 hdrlen += sizeof(dheader) - pos;
620 reused_delta++;
621 } else if (type == OBJ_REF_DELTA) {
622 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
623 unuse_pack(&w_curs);
624 return 0;
626 hashwrite(f, header, hdrlen);
627 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
628 hdrlen += hashsz;
629 reused_delta++;
630 } else {
631 if (limit && hdrlen + datalen + hashsz >= limit) {
632 unuse_pack(&w_curs);
633 return 0;
635 hashwrite(f, header, hdrlen);
637 copy_pack_data(f, p, &w_curs, offset, datalen);
638 unuse_pack(&w_curs);
639 reused++;
640 return hdrlen + datalen;
643 /* Return 0 if we will bust the pack-size limit */
644 static off_t write_object(struct hashfile *f,
645 struct object_entry *entry,
646 off_t write_offset)
648 unsigned long limit;
649 off_t len;
650 int usable_delta, to_reuse;
652 if (!pack_to_stdout)
653 crc32_begin(f);
655 /* apply size limit if limited packsize and not first object */
656 if (!pack_size_limit || !nr_written)
657 limit = 0;
658 else if (pack_size_limit <= write_offset)
660 * the earlier object did not fit the limit; avoid
661 * mistaking this with unlimited (i.e. limit = 0).
663 limit = 1;
664 else
665 limit = pack_size_limit - write_offset;
667 if (!DELTA(entry))
668 usable_delta = 0; /* no delta */
669 else if (!pack_size_limit)
670 usable_delta = 1; /* unlimited packfile */
671 else if (DELTA(entry)->idx.offset == (off_t)-1)
672 usable_delta = 0; /* base was written to another pack */
673 else if (DELTA(entry)->idx.offset)
674 usable_delta = 1; /* base already exists in this pack */
675 else
676 usable_delta = 0; /* base could end up in another pack */
678 if (!reuse_object)
679 to_reuse = 0; /* explicit */
680 else if (!IN_PACK(entry))
681 to_reuse = 0; /* can't reuse what we don't have */
682 else if (oe_type(entry) == OBJ_REF_DELTA ||
683 oe_type(entry) == OBJ_OFS_DELTA)
684 /* check_object() decided it for us ... */
685 to_reuse = usable_delta;
686 /* ... but pack split may override that */
687 else if (oe_type(entry) != entry->in_pack_type)
688 to_reuse = 0; /* pack has delta which is unusable */
689 else if (DELTA(entry))
690 to_reuse = 0; /* we want to pack afresh */
691 else
692 to_reuse = 1; /* we have it in-pack undeltified,
693 * and we do not need to deltify it.
696 if (!to_reuse)
697 len = write_no_reuse_object(f, entry, limit, usable_delta);
698 else
699 len = write_reuse_object(f, entry, limit, usable_delta);
700 if (!len)
701 return 0;
703 if (usable_delta)
704 written_delta++;
705 written++;
706 if (!pack_to_stdout)
707 entry->idx.crc32 = crc32_end(f);
708 return len;
711 enum write_one_status {
712 WRITE_ONE_SKIP = -1, /* already written */
713 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
714 WRITE_ONE_WRITTEN = 1, /* normal */
715 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
718 static enum write_one_status write_one(struct hashfile *f,
719 struct object_entry *e,
720 off_t *offset)
722 off_t size;
723 int recursing;
726 * we set offset to 1 (which is an impossible value) to mark
727 * the fact that this object is involved in "write its base
728 * first before writing a deltified object" recursion.
730 recursing = (e->idx.offset == 1);
731 if (recursing) {
732 warning(_("recursive delta detected for object %s"),
733 oid_to_hex(&e->idx.oid));
734 return WRITE_ONE_RECURSIVE;
735 } else if (e->idx.offset || e->preferred_base) {
736 /* offset is non zero if object is written already. */
737 return WRITE_ONE_SKIP;
740 /* if we are deltified, write out base object first. */
741 if (DELTA(e)) {
742 e->idx.offset = 1; /* now recurse */
743 switch (write_one(f, DELTA(e), offset)) {
744 case WRITE_ONE_RECURSIVE:
745 /* we cannot depend on this one */
746 SET_DELTA(e, NULL);
747 break;
748 default:
749 break;
750 case WRITE_ONE_BREAK:
751 e->idx.offset = recursing;
752 return WRITE_ONE_BREAK;
756 e->idx.offset = *offset;
757 size = write_object(f, e, *offset);
758 if (!size) {
759 e->idx.offset = recursing;
760 return WRITE_ONE_BREAK;
762 written_list[nr_written++] = &e->idx;
764 /* make sure off_t is sufficiently large not to wrap */
765 if (signed_add_overflows(*offset, size))
766 die(_("pack too large for current definition of off_t"));
767 *offset += size;
768 return WRITE_ONE_WRITTEN;
771 static int mark_tagged(const char *path UNUSED, const struct object_id *oid,
772 int flag UNUSED, void *cb_data UNUSED)
774 struct object_id peeled;
775 struct object_entry *entry = packlist_find(&to_pack, oid);
777 if (entry)
778 entry->tagged = 1;
779 if (!peel_iterated_oid(oid, &peeled)) {
780 entry = packlist_find(&to_pack, &peeled);
781 if (entry)
782 entry->tagged = 1;
784 return 0;
787 static inline unsigned char oe_layer(struct packing_data *pack,
788 struct object_entry *e)
790 if (!pack->layer)
791 return 0;
792 return pack->layer[e - pack->objects];
795 static inline void add_to_write_order(struct object_entry **wo,
796 unsigned int *endp,
797 struct object_entry *e)
799 if (e->filled || oe_layer(&to_pack, e) != write_layer)
800 return;
801 wo[(*endp)++] = e;
802 e->filled = 1;
805 static void add_descendants_to_write_order(struct object_entry **wo,
806 unsigned int *endp,
807 struct object_entry *e)
809 int add_to_order = 1;
810 while (e) {
811 if (add_to_order) {
812 struct object_entry *s;
813 /* add this node... */
814 add_to_write_order(wo, endp, e);
815 /* all its siblings... */
816 for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
817 add_to_write_order(wo, endp, s);
820 /* drop down a level to add left subtree nodes if possible */
821 if (DELTA_CHILD(e)) {
822 add_to_order = 1;
823 e = DELTA_CHILD(e);
824 } else {
825 add_to_order = 0;
826 /* our sibling might have some children, it is next */
827 if (DELTA_SIBLING(e)) {
828 e = DELTA_SIBLING(e);
829 continue;
831 /* go back to our parent node */
832 e = DELTA(e);
833 while (e && !DELTA_SIBLING(e)) {
834 /* we're on the right side of a subtree, keep
835 * going up until we can go right again */
836 e = DELTA(e);
838 if (!e) {
839 /* done- we hit our original root node */
840 return;
842 /* pass it off to sibling at this level */
843 e = DELTA_SIBLING(e);
848 static void add_family_to_write_order(struct object_entry **wo,
849 unsigned int *endp,
850 struct object_entry *e)
852 struct object_entry *root;
854 for (root = e; DELTA(root); root = DELTA(root))
855 ; /* nothing */
856 add_descendants_to_write_order(wo, endp, root);
859 static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end)
861 unsigned int i, last_untagged;
862 struct object_entry *objects = to_pack.objects;
864 for (i = 0; i < to_pack.nr_objects; i++) {
865 if (objects[i].tagged)
866 break;
867 add_to_write_order(wo, wo_end, &objects[i]);
869 last_untagged = i;
872 * Then fill all the tagged tips.
874 for (; i < to_pack.nr_objects; i++) {
875 if (objects[i].tagged)
876 add_to_write_order(wo, wo_end, &objects[i]);
880 * And then all remaining commits and tags.
882 for (i = last_untagged; i < to_pack.nr_objects; i++) {
883 if (oe_type(&objects[i]) != OBJ_COMMIT &&
884 oe_type(&objects[i]) != OBJ_TAG)
885 continue;
886 add_to_write_order(wo, wo_end, &objects[i]);
890 * And then all the trees.
892 for (i = last_untagged; i < to_pack.nr_objects; i++) {
893 if (oe_type(&objects[i]) != OBJ_TREE)
894 continue;
895 add_to_write_order(wo, wo_end, &objects[i]);
899 * Finally all the rest in really tight order
901 for (i = last_untagged; i < to_pack.nr_objects; i++) {
902 if (!objects[i].filled && oe_layer(&to_pack, &objects[i]) == write_layer)
903 add_family_to_write_order(wo, wo_end, &objects[i]);
907 static struct object_entry **compute_write_order(void)
909 uint32_t max_layers = 1;
910 unsigned int i, wo_end;
912 struct object_entry **wo;
913 struct object_entry *objects = to_pack.objects;
915 for (i = 0; i < to_pack.nr_objects; i++) {
916 objects[i].tagged = 0;
917 objects[i].filled = 0;
918 SET_DELTA_CHILD(&objects[i], NULL);
919 SET_DELTA_SIBLING(&objects[i], NULL);
923 * Fully connect delta_child/delta_sibling network.
924 * Make sure delta_sibling is sorted in the original
925 * recency order.
927 for (i = to_pack.nr_objects; i > 0;) {
928 struct object_entry *e = &objects[--i];
929 if (!DELTA(e))
930 continue;
931 /* Mark me as the first child */
932 e->delta_sibling_idx = DELTA(e)->delta_child_idx;
933 SET_DELTA_CHILD(DELTA(e), e);
937 * Mark objects that are at the tip of tags.
939 for_each_tag_ref(mark_tagged, NULL);
941 if (use_delta_islands) {
942 max_layers = compute_pack_layers(&to_pack);
943 free_island_marks();
946 ALLOC_ARRAY(wo, to_pack.nr_objects);
947 wo_end = 0;
949 for (; write_layer < max_layers; ++write_layer)
950 compute_layer_order(wo, &wo_end);
952 if (wo_end != to_pack.nr_objects)
953 die(_("ordered %u objects, expected %"PRIu32),
954 wo_end, to_pack.nr_objects);
956 return wo;
961 * A reused set of objects. All objects in a chunk have the same
962 * relative position in the original packfile and the generated
963 * packfile.
966 static struct reused_chunk {
967 /* The offset of the first object of this chunk in the original
968 * packfile. */
969 off_t original;
970 /* The difference for "original" minus the offset of the first object of
971 * this chunk in the generated packfile. */
972 off_t difference;
973 } *reused_chunks;
974 static int reused_chunks_nr;
975 static int reused_chunks_alloc;
977 static void record_reused_object(off_t where, off_t offset)
979 if (reused_chunks_nr && reused_chunks[reused_chunks_nr-1].difference == offset)
980 return;
982 ALLOC_GROW(reused_chunks, reused_chunks_nr + 1,
983 reused_chunks_alloc);
984 reused_chunks[reused_chunks_nr].original = where;
985 reused_chunks[reused_chunks_nr].difference = offset;
986 reused_chunks_nr++;
990 * Binary search to find the chunk that "where" is in. Note
991 * that we're not looking for an exact match, just the first
992 * chunk that contains it (which implicitly ends at the start
993 * of the next chunk.
995 static off_t find_reused_offset(off_t where)
997 int lo = 0, hi = reused_chunks_nr;
998 while (lo < hi) {
999 int mi = lo + ((hi - lo) / 2);
1000 if (where == reused_chunks[mi].original)
1001 return reused_chunks[mi].difference;
1002 if (where < reused_chunks[mi].original)
1003 hi = mi;
1004 else
1005 lo = mi + 1;
1009 * The first chunk starts at zero, so we can't have gone below
1010 * there.
1012 assert(lo);
1013 return reused_chunks[lo-1].difference;
1016 static void write_reused_pack_one(size_t pos, struct hashfile *out,
1017 struct pack_window **w_curs)
1019 off_t offset, next, cur;
1020 enum object_type type;
1021 unsigned long size;
1023 offset = pack_pos_to_offset(reuse_packfile, pos);
1024 next = pack_pos_to_offset(reuse_packfile, pos + 1);
1026 record_reused_object(offset, offset - hashfile_total(out));
1028 cur = offset;
1029 type = unpack_object_header(reuse_packfile, w_curs, &cur, &size);
1030 assert(type >= 0);
1032 if (type == OBJ_OFS_DELTA) {
1033 off_t base_offset;
1034 off_t fixup;
1036 unsigned char header[MAX_PACK_OBJECT_HEADER];
1037 unsigned len;
1039 base_offset = get_delta_base(reuse_packfile, w_curs, &cur, type, offset);
1040 assert(base_offset != 0);
1042 /* Convert to REF_DELTA if we must... */
1043 if (!allow_ofs_delta) {
1044 uint32_t base_pos;
1045 struct object_id base_oid;
1047 if (offset_to_pack_pos(reuse_packfile, base_offset, &base_pos) < 0)
1048 die(_("expected object at offset %"PRIuMAX" "
1049 "in pack %s"),
1050 (uintmax_t)base_offset,
1051 reuse_packfile->pack_name);
1053 nth_packed_object_id(&base_oid, reuse_packfile,
1054 pack_pos_to_index(reuse_packfile, base_pos));
1056 len = encode_in_pack_object_header(header, sizeof(header),
1057 OBJ_REF_DELTA, size);
1058 hashwrite(out, header, len);
1059 hashwrite(out, base_oid.hash, the_hash_algo->rawsz);
1060 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1061 return;
1064 /* Otherwise see if we need to rewrite the offset... */
1065 fixup = find_reused_offset(offset) -
1066 find_reused_offset(base_offset);
1067 if (fixup) {
1068 unsigned char ofs_header[10];
1069 unsigned i, ofs_len;
1070 off_t ofs = offset - base_offset - fixup;
1072 len = encode_in_pack_object_header(header, sizeof(header),
1073 OBJ_OFS_DELTA, size);
1075 i = sizeof(ofs_header) - 1;
1076 ofs_header[i] = ofs & 127;
1077 while (ofs >>= 7)
1078 ofs_header[--i] = 128 | (--ofs & 127);
1080 ofs_len = sizeof(ofs_header) - i;
1082 hashwrite(out, header, len);
1083 hashwrite(out, ofs_header + sizeof(ofs_header) - ofs_len, ofs_len);
1084 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1085 return;
1088 /* ...otherwise we have no fixup, and can write it verbatim */
1091 copy_pack_data(out, reuse_packfile, w_curs, offset, next - offset);
1094 static size_t write_reused_pack_verbatim(struct hashfile *out,
1095 struct pack_window **w_curs)
1097 size_t pos = 0;
1099 while (pos < reuse_packfile_bitmap->word_alloc &&
1100 reuse_packfile_bitmap->words[pos] == (eword_t)~0)
1101 pos++;
1103 if (pos) {
1104 off_t to_write;
1106 written = (pos * BITS_IN_EWORD);
1107 to_write = pack_pos_to_offset(reuse_packfile, written)
1108 - sizeof(struct pack_header);
1110 /* We're recording one chunk, not one object. */
1111 record_reused_object(sizeof(struct pack_header), 0);
1112 hashflush(out);
1113 copy_pack_data(out, reuse_packfile, w_curs,
1114 sizeof(struct pack_header), to_write);
1116 display_progress(progress_state, written);
1118 return pos;
1121 static void write_reused_pack(struct hashfile *f)
1123 size_t i = 0;
1124 uint32_t offset;
1125 struct pack_window *w_curs = NULL;
1127 if (allow_ofs_delta)
1128 i = write_reused_pack_verbatim(f, &w_curs);
1130 for (; i < reuse_packfile_bitmap->word_alloc; ++i) {
1131 eword_t word = reuse_packfile_bitmap->words[i];
1132 size_t pos = (i * BITS_IN_EWORD);
1134 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1135 if ((word >> offset) == 0)
1136 break;
1138 offset += ewah_bit_ctz64(word >> offset);
1140 * Can use bit positions directly, even for MIDX
1141 * bitmaps. See comment in try_partial_reuse()
1142 * for why.
1144 write_reused_pack_one(pos + offset, f, &w_curs);
1145 display_progress(progress_state, ++written);
1149 unuse_pack(&w_curs);
1152 static void write_excluded_by_configs(void)
1154 struct oidset_iter iter;
1155 const struct object_id *oid;
1157 oidset_iter_init(&excluded_by_config, &iter);
1158 while ((oid = oidset_iter_next(&iter))) {
1159 struct configured_exclusion *ex =
1160 oidmap_get(&configured_exclusions, oid);
1162 if (!ex)
1163 BUG("configured exclusion wasn't configured");
1164 write_in_full(1, ex->pack_hash_hex, strlen(ex->pack_hash_hex));
1165 write_in_full(1, " ", 1);
1166 write_in_full(1, ex->uri, strlen(ex->uri));
1167 write_in_full(1, "\n", 1);
1171 static const char no_split_warning[] = N_(
1172 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
1175 static void write_pack_file(void)
1177 uint32_t i = 0, j;
1178 struct hashfile *f;
1179 off_t offset;
1180 uint32_t nr_remaining = nr_result;
1181 time_t last_mtime = 0;
1182 struct object_entry **write_order;
1184 if (progress > pack_to_stdout)
1185 progress_state = start_progress(_("Writing objects"), nr_result);
1186 ALLOC_ARRAY(written_list, to_pack.nr_objects);
1187 write_order = compute_write_order();
1189 do {
1190 unsigned char hash[GIT_MAX_RAWSZ];
1191 char *pack_tmp_name = NULL;
1193 if (pack_to_stdout)
1194 f = hashfd_throughput(1, "<stdout>", progress_state);
1195 else
1196 f = create_tmp_packfile(&pack_tmp_name);
1198 offset = write_pack_header(f, nr_remaining);
1200 if (reuse_packfile) {
1201 assert(pack_to_stdout);
1202 write_reused_pack(f);
1203 offset = hashfile_total(f);
1206 nr_written = 0;
1207 for (; i < to_pack.nr_objects; i++) {
1208 struct object_entry *e = write_order[i];
1209 if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
1210 break;
1211 display_progress(progress_state, written);
1214 if (pack_to_stdout) {
1216 * We never fsync when writing to stdout since we may
1217 * not be writing to an actual pack file. For instance,
1218 * the upload-pack code passes a pipe here. Calling
1219 * fsync on a pipe results in unnecessary
1220 * synchronization with the reader on some platforms.
1222 finalize_hashfile(f, hash, FSYNC_COMPONENT_NONE,
1223 CSUM_HASH_IN_STREAM | CSUM_CLOSE);
1224 } else if (nr_written == nr_remaining) {
1225 finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK,
1226 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
1227 } else {
1229 * If we wrote the wrong number of entries in the
1230 * header, rewrite it like in fast-import.
1233 int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0);
1234 fixup_pack_header_footer(fd, hash, pack_tmp_name,
1235 nr_written, hash, offset);
1236 close(fd);
1237 if (write_bitmap_index) {
1238 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1239 warning(_(no_split_warning));
1240 write_bitmap_index = 0;
1244 if (!pack_to_stdout) {
1245 struct stat st;
1246 struct strbuf tmpname = STRBUF_INIT;
1247 char *idx_tmp_name = NULL;
1250 * Packs are runtime accessed in their mtime
1251 * order since newer packs are more likely to contain
1252 * younger objects. So if we are creating multiple
1253 * packs then we should modify the mtime of later ones
1254 * to preserve this property.
1256 if (stat(pack_tmp_name, &st) < 0) {
1257 warning_errno(_("failed to stat %s"), pack_tmp_name);
1258 } else if (!last_mtime) {
1259 last_mtime = st.st_mtime;
1260 } else {
1261 struct utimbuf utb;
1262 utb.actime = st.st_atime;
1263 utb.modtime = --last_mtime;
1264 if (utime(pack_tmp_name, &utb) < 0)
1265 warning_errno(_("failed utime() on %s"), pack_tmp_name);
1268 strbuf_addf(&tmpname, "%s-%s.", base_name,
1269 hash_to_hex(hash));
1271 if (write_bitmap_index) {
1272 bitmap_writer_set_checksum(hash);
1273 bitmap_writer_build_type_index(
1274 &to_pack, written_list, nr_written);
1277 if (cruft)
1278 pack_idx_opts.flags |= WRITE_MTIMES;
1280 stage_tmp_packfiles(&tmpname, pack_tmp_name,
1281 written_list, nr_written,
1282 &to_pack, &pack_idx_opts, hash,
1283 &idx_tmp_name);
1285 if (write_bitmap_index) {
1286 size_t tmpname_len = tmpname.len;
1288 strbuf_addstr(&tmpname, "bitmap");
1289 stop_progress(&progress_state);
1291 bitmap_writer_show_progress(progress);
1292 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
1293 if (bitmap_writer_build(&to_pack) < 0)
1294 die(_("failed to write bitmap index"));
1295 bitmap_writer_finish(written_list, nr_written,
1296 tmpname.buf, write_bitmap_options);
1297 write_bitmap_index = 0;
1298 strbuf_setlen(&tmpname, tmpname_len);
1301 rename_tmp_packfile_idx(&tmpname, &idx_tmp_name);
1303 free(idx_tmp_name);
1304 strbuf_release(&tmpname);
1305 free(pack_tmp_name);
1306 puts(hash_to_hex(hash));
1309 /* mark written objects as written to previous pack */
1310 for (j = 0; j < nr_written; j++) {
1311 written_list[j]->offset = (off_t)-1;
1313 nr_remaining -= nr_written;
1314 } while (nr_remaining && i < to_pack.nr_objects);
1316 free(written_list);
1317 free(write_order);
1318 stop_progress(&progress_state);
1319 if (written != nr_result)
1320 die(_("wrote %"PRIu32" objects while expecting %"PRIu32),
1321 written, nr_result);
1322 trace2_data_intmax("pack-objects", the_repository,
1323 "write_pack_file/wrote", nr_result);
1326 static int no_try_delta(const char *path)
1328 static struct attr_check *check;
1330 if (!check)
1331 check = attr_check_initl("delta", NULL);
1332 git_check_attr(the_repository->index, path, check);
1333 if (ATTR_FALSE(check->items[0].value))
1334 return 1;
1335 return 0;
1339 * When adding an object, check whether we have already added it
1340 * to our packing list. If so, we can skip. However, if we are
1341 * being asked to excludei t, but the previous mention was to include
1342 * it, make sure to adjust its flags and tweak our numbers accordingly.
1344 * As an optimization, we pass out the index position where we would have
1345 * found the item, since that saves us from having to look it up again a
1346 * few lines later when we want to add the new entry.
1348 static int have_duplicate_entry(const struct object_id *oid,
1349 int exclude)
1351 struct object_entry *entry;
1353 if (reuse_packfile_bitmap &&
1354 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid))
1355 return 1;
1357 entry = packlist_find(&to_pack, oid);
1358 if (!entry)
1359 return 0;
1361 if (exclude) {
1362 if (!entry->preferred_base)
1363 nr_result--;
1364 entry->preferred_base = 1;
1367 return 1;
1370 static int want_found_object(const struct object_id *oid, int exclude,
1371 struct packed_git *p)
1373 if (exclude)
1374 return 1;
1375 if (incremental)
1376 return 0;
1378 if (!is_pack_valid(p))
1379 return -1;
1382 * When asked to do --local (do not include an object that appears in a
1383 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1384 * an object that appears in a pack marked with .keep), finding a pack
1385 * that matches the criteria is sufficient for us to decide to omit it.
1386 * However, even if this pack does not satisfy the criteria, we need to
1387 * make sure no copy of this object appears in _any_ pack that makes us
1388 * to omit the object, so we need to check all the packs.
1390 * We can however first check whether these options can possibly matter;
1391 * if they do not matter we know we want the object in generated pack.
1392 * Otherwise, we signal "-1" at the end to tell the caller that we do
1393 * not know either way, and it needs to check more packs.
1397 * Objects in packs borrowed from elsewhere are discarded regardless of
1398 * if they appear in other packs that weren't borrowed.
1400 if (local && !p->pack_local)
1401 return 0;
1404 * Then handle .keep first, as we have a fast(er) path there.
1406 if (ignore_packed_keep_on_disk || ignore_packed_keep_in_core) {
1408 * Set the flags for the kept-pack cache to be the ones we want
1409 * to ignore.
1411 * That is, if we are ignoring objects in on-disk keep packs,
1412 * then we want to search through the on-disk keep and ignore
1413 * the in-core ones.
1415 unsigned flags = 0;
1416 if (ignore_packed_keep_on_disk)
1417 flags |= ON_DISK_KEEP_PACKS;
1418 if (ignore_packed_keep_in_core)
1419 flags |= IN_CORE_KEEP_PACKS;
1421 if (ignore_packed_keep_on_disk && p->pack_keep)
1422 return 0;
1423 if (ignore_packed_keep_in_core && p->pack_keep_in_core)
1424 return 0;
1425 if (has_object_kept_pack(oid, flags))
1426 return 0;
1430 * At this point we know definitively that either we don't care about
1431 * keep-packs, or the object is not in one. Keep checking other
1432 * conditions...
1434 if (!local || !have_non_local_packs)
1435 return 1;
1437 /* we don't know yet; keep looking for more packs */
1438 return -1;
1441 static int want_object_in_pack_one(struct packed_git *p,
1442 const struct object_id *oid,
1443 int exclude,
1444 struct packed_git **found_pack,
1445 off_t *found_offset)
1447 off_t offset;
1449 if (p == *found_pack)
1450 offset = *found_offset;
1451 else
1452 offset = find_pack_entry_one(oid->hash, p);
1454 if (offset) {
1455 if (!*found_pack) {
1456 if (!is_pack_valid(p))
1457 return -1;
1458 *found_offset = offset;
1459 *found_pack = p;
1461 return want_found_object(oid, exclude, p);
1463 return -1;
1467 * Check whether we want the object in the pack (e.g., we do not want
1468 * objects found in non-local stores if the "--local" option was used).
1470 * If the caller already knows an existing pack it wants to take the object
1471 * from, that is passed in *found_pack and *found_offset; otherwise this
1472 * function finds if there is any pack that has the object and returns the pack
1473 * and its offset in these variables.
1475 static int want_object_in_pack(const struct object_id *oid,
1476 int exclude,
1477 struct packed_git **found_pack,
1478 off_t *found_offset)
1480 int want;
1481 struct list_head *pos;
1482 struct multi_pack_index *m;
1484 if (!exclude && local && has_loose_object_nonlocal(oid))
1485 return 0;
1488 * If we already know the pack object lives in, start checks from that
1489 * pack - in the usual case when neither --local was given nor .keep files
1490 * are present we will determine the answer right now.
1492 if (*found_pack) {
1493 want = want_found_object(oid, exclude, *found_pack);
1494 if (want != -1)
1495 return want;
1497 *found_pack = NULL;
1498 *found_offset = 0;
1501 for (m = get_multi_pack_index(the_repository); m; m = m->next) {
1502 struct pack_entry e;
1503 if (fill_midx_entry(the_repository, oid, &e, m)) {
1504 want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset);
1505 if (want != -1)
1506 return want;
1510 list_for_each(pos, get_packed_git_mru(the_repository)) {
1511 struct packed_git *p = list_entry(pos, struct packed_git, mru);
1512 want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset);
1513 if (!exclude && want > 0)
1514 list_move(&p->mru,
1515 get_packed_git_mru(the_repository));
1516 if (want != -1)
1517 return want;
1520 if (uri_protocols.nr) {
1521 struct configured_exclusion *ex =
1522 oidmap_get(&configured_exclusions, oid);
1523 int i;
1524 const char *p;
1526 if (ex) {
1527 for (i = 0; i < uri_protocols.nr; i++) {
1528 if (skip_prefix(ex->uri,
1529 uri_protocols.items[i].string,
1530 &p) &&
1531 *p == ':') {
1532 oidset_insert(&excluded_by_config, oid);
1533 return 0;
1539 return 1;
1542 static struct object_entry *create_object_entry(const struct object_id *oid,
1543 enum object_type type,
1544 uint32_t hash,
1545 int exclude,
1546 int no_try_delta,
1547 struct packed_git *found_pack,
1548 off_t found_offset)
1550 struct object_entry *entry;
1552 entry = packlist_alloc(&to_pack, oid);
1553 entry->hash = hash;
1554 oe_set_type(entry, type);
1555 if (exclude)
1556 entry->preferred_base = 1;
1557 else
1558 nr_result++;
1559 if (found_pack) {
1560 oe_set_in_pack(&to_pack, entry, found_pack);
1561 entry->in_pack_offset = found_offset;
1564 entry->no_try_delta = no_try_delta;
1566 return entry;
1569 static const char no_closure_warning[] = N_(
1570 "disabling bitmap writing, as some objects are not being packed"
1573 static int add_object_entry(const struct object_id *oid, enum object_type type,
1574 const char *name, int exclude)
1576 struct packed_git *found_pack = NULL;
1577 off_t found_offset = 0;
1579 display_progress(progress_state, ++nr_seen);
1581 if (have_duplicate_entry(oid, exclude))
1582 return 0;
1584 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
1585 /* The pack is missing an object, so it will not have closure */
1586 if (write_bitmap_index) {
1587 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1588 warning(_(no_closure_warning));
1589 write_bitmap_index = 0;
1591 return 0;
1594 create_object_entry(oid, type, pack_name_hash(name),
1595 exclude, name && no_try_delta(name),
1596 found_pack, found_offset);
1597 return 1;
1600 static int add_object_entry_from_bitmap(const struct object_id *oid,
1601 enum object_type type,
1602 int flags UNUSED, uint32_t name_hash,
1603 struct packed_git *pack, off_t offset)
1605 display_progress(progress_state, ++nr_seen);
1607 if (have_duplicate_entry(oid, 0))
1608 return 0;
1610 if (!want_object_in_pack(oid, 0, &pack, &offset))
1611 return 0;
1613 create_object_entry(oid, type, name_hash, 0, 0, pack, offset);
1614 return 1;
1617 struct pbase_tree_cache {
1618 struct object_id oid;
1619 int ref;
1620 int temporary;
1621 void *tree_data;
1622 unsigned long tree_size;
1625 static struct pbase_tree_cache *(pbase_tree_cache[256]);
1626 static int pbase_tree_cache_ix(const struct object_id *oid)
1628 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
1630 static int pbase_tree_cache_ix_incr(int ix)
1632 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1635 static struct pbase_tree {
1636 struct pbase_tree *next;
1637 /* This is a phony "cache" entry; we are not
1638 * going to evict it or find it through _get()
1639 * mechanism -- this is for the toplevel node that
1640 * would almost always change with any commit.
1642 struct pbase_tree_cache pcache;
1643 } *pbase_tree;
1645 static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
1647 struct pbase_tree_cache *ent, *nent;
1648 void *data;
1649 unsigned long size;
1650 enum object_type type;
1651 int neigh;
1652 int my_ix = pbase_tree_cache_ix(oid);
1653 int available_ix = -1;
1655 /* pbase-tree-cache acts as a limited hashtable.
1656 * your object will be found at your index or within a few
1657 * slots after that slot if it is cached.
1659 for (neigh = 0; neigh < 8; neigh++) {
1660 ent = pbase_tree_cache[my_ix];
1661 if (ent && oideq(&ent->oid, oid)) {
1662 ent->ref++;
1663 return ent;
1665 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1666 ((0 <= available_ix) &&
1667 (!ent && pbase_tree_cache[available_ix])))
1668 available_ix = my_ix;
1669 if (!ent)
1670 break;
1671 my_ix = pbase_tree_cache_ix_incr(my_ix);
1674 /* Did not find one. Either we got a bogus request or
1675 * we need to read and perhaps cache.
1677 data = repo_read_object_file(the_repository, oid, &type, &size);
1678 if (!data)
1679 return NULL;
1680 if (type != OBJ_TREE) {
1681 free(data);
1682 return NULL;
1685 /* We need to either cache or return a throwaway copy */
1687 if (available_ix < 0)
1688 ent = NULL;
1689 else {
1690 ent = pbase_tree_cache[available_ix];
1691 my_ix = available_ix;
1694 if (!ent) {
1695 nent = xmalloc(sizeof(*nent));
1696 nent->temporary = (available_ix < 0);
1698 else {
1699 /* evict and reuse */
1700 free(ent->tree_data);
1701 nent = ent;
1703 oidcpy(&nent->oid, oid);
1704 nent->tree_data = data;
1705 nent->tree_size = size;
1706 nent->ref = 1;
1707 if (!nent->temporary)
1708 pbase_tree_cache[my_ix] = nent;
1709 return nent;
1712 static void pbase_tree_put(struct pbase_tree_cache *cache)
1714 if (!cache->temporary) {
1715 cache->ref--;
1716 return;
1718 free(cache->tree_data);
1719 free(cache);
1722 static size_t name_cmp_len(const char *name)
1724 return strcspn(name, "\n/");
1727 static void add_pbase_object(struct tree_desc *tree,
1728 const char *name,
1729 size_t cmplen,
1730 const char *fullname)
1732 struct name_entry entry;
1733 int cmp;
1735 while (tree_entry(tree,&entry)) {
1736 if (S_ISGITLINK(entry.mode))
1737 continue;
1738 cmp = tree_entry_len(&entry) != cmplen ? 1 :
1739 memcmp(name, entry.path, cmplen);
1740 if (cmp > 0)
1741 continue;
1742 if (cmp < 0)
1743 return;
1744 if (name[cmplen] != '/') {
1745 add_object_entry(&entry.oid,
1746 object_type(entry.mode),
1747 fullname, 1);
1748 return;
1750 if (S_ISDIR(entry.mode)) {
1751 struct tree_desc sub;
1752 struct pbase_tree_cache *tree;
1753 const char *down = name+cmplen+1;
1754 size_t downlen = name_cmp_len(down);
1756 tree = pbase_tree_get(&entry.oid);
1757 if (!tree)
1758 return;
1759 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1761 add_pbase_object(&sub, down, downlen, fullname);
1762 pbase_tree_put(tree);
1767 static unsigned *done_pbase_paths;
1768 static int done_pbase_paths_num;
1769 static int done_pbase_paths_alloc;
1770 static int done_pbase_path_pos(unsigned hash)
1772 int lo = 0;
1773 int hi = done_pbase_paths_num;
1774 while (lo < hi) {
1775 int mi = lo + (hi - lo) / 2;
1776 if (done_pbase_paths[mi] == hash)
1777 return mi;
1778 if (done_pbase_paths[mi] < hash)
1779 hi = mi;
1780 else
1781 lo = mi + 1;
1783 return -lo-1;
1786 static int check_pbase_path(unsigned hash)
1788 int pos = done_pbase_path_pos(hash);
1789 if (0 <= pos)
1790 return 1;
1791 pos = -pos - 1;
1792 ALLOC_GROW(done_pbase_paths,
1793 done_pbase_paths_num + 1,
1794 done_pbase_paths_alloc);
1795 done_pbase_paths_num++;
1796 if (pos < done_pbase_paths_num)
1797 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
1798 done_pbase_paths_num - pos - 1);
1799 done_pbase_paths[pos] = hash;
1800 return 0;
1803 static void add_preferred_base_object(const char *name)
1805 struct pbase_tree *it;
1806 size_t cmplen;
1807 unsigned hash = pack_name_hash(name);
1809 if (!num_preferred_base || check_pbase_path(hash))
1810 return;
1812 cmplen = name_cmp_len(name);
1813 for (it = pbase_tree; it; it = it->next) {
1814 if (cmplen == 0) {
1815 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
1817 else {
1818 struct tree_desc tree;
1819 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1820 add_pbase_object(&tree, name, cmplen, name);
1825 static void add_preferred_base(struct object_id *oid)
1827 struct pbase_tree *it;
1828 void *data;
1829 unsigned long size;
1830 struct object_id tree_oid;
1832 if (window <= num_preferred_base++)
1833 return;
1835 data = read_object_with_reference(the_repository, oid,
1836 OBJ_TREE, &size, &tree_oid);
1837 if (!data)
1838 return;
1840 for (it = pbase_tree; it; it = it->next) {
1841 if (oideq(&it->pcache.oid, &tree_oid)) {
1842 free(data);
1843 return;
1847 CALLOC_ARRAY(it, 1);
1848 it->next = pbase_tree;
1849 pbase_tree = it;
1851 oidcpy(&it->pcache.oid, &tree_oid);
1852 it->pcache.tree_data = data;
1853 it->pcache.tree_size = size;
1856 static void cleanup_preferred_base(void)
1858 struct pbase_tree *it;
1859 unsigned i;
1861 it = pbase_tree;
1862 pbase_tree = NULL;
1863 while (it) {
1864 struct pbase_tree *tmp = it;
1865 it = tmp->next;
1866 free(tmp->pcache.tree_data);
1867 free(tmp);
1870 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
1871 if (!pbase_tree_cache[i])
1872 continue;
1873 free(pbase_tree_cache[i]->tree_data);
1874 FREE_AND_NULL(pbase_tree_cache[i]);
1877 FREE_AND_NULL(done_pbase_paths);
1878 done_pbase_paths_num = done_pbase_paths_alloc = 0;
1882 * Return 1 iff the object specified by "delta" can be sent
1883 * literally as a delta against the base in "base_sha1". If
1884 * so, then *base_out will point to the entry in our packing
1885 * list, or NULL if we must use the external-base list.
1887 * Depth value does not matter - find_deltas() will
1888 * never consider reused delta as the base object to
1889 * deltify other objects against, in order to avoid
1890 * circular deltas.
1892 static int can_reuse_delta(const struct object_id *base_oid,
1893 struct object_entry *delta,
1894 struct object_entry **base_out)
1896 struct object_entry *base;
1899 * First see if we're already sending the base (or it's explicitly in
1900 * our "excluded" list).
1902 base = packlist_find(&to_pack, base_oid);
1903 if (base) {
1904 if (!in_same_island(&delta->idx.oid, &base->idx.oid))
1905 return 0;
1906 *base_out = base;
1907 return 1;
1911 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1912 * even if it was buried too deep in history to make it into the
1913 * packing list.
1915 if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, base_oid)) {
1916 if (use_delta_islands) {
1917 if (!in_same_island(&delta->idx.oid, base_oid))
1918 return 0;
1920 *base_out = NULL;
1921 return 1;
1924 return 0;
1927 static void prefetch_to_pack(uint32_t object_index_start) {
1928 struct oid_array to_fetch = OID_ARRAY_INIT;
1929 uint32_t i;
1931 for (i = object_index_start; i < to_pack.nr_objects; i++) {
1932 struct object_entry *entry = to_pack.objects + i;
1934 if (!oid_object_info_extended(the_repository,
1935 &entry->idx.oid,
1936 NULL,
1937 OBJECT_INFO_FOR_PREFETCH))
1938 continue;
1939 oid_array_append(&to_fetch, &entry->idx.oid);
1941 promisor_remote_get_direct(the_repository,
1942 to_fetch.oid, to_fetch.nr);
1943 oid_array_clear(&to_fetch);
1946 static void check_object(struct object_entry *entry, uint32_t object_index)
1948 unsigned long canonical_size;
1949 enum object_type type;
1950 struct object_info oi = {.typep = &type, .sizep = &canonical_size};
1952 if (IN_PACK(entry)) {
1953 struct packed_git *p = IN_PACK(entry);
1954 struct pack_window *w_curs = NULL;
1955 int have_base = 0;
1956 struct object_id base_ref;
1957 struct object_entry *base_entry;
1958 unsigned long used, used_0;
1959 unsigned long avail;
1960 off_t ofs;
1961 unsigned char *buf, c;
1962 enum object_type type;
1963 unsigned long in_pack_size;
1965 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1968 * We want in_pack_type even if we do not reuse delta
1969 * since non-delta representations could still be reused.
1971 used = unpack_object_header_buffer(buf, avail,
1972 &type,
1973 &in_pack_size);
1974 if (used == 0)
1975 goto give_up;
1977 if (type < 0)
1978 BUG("invalid type %d", type);
1979 entry->in_pack_type = type;
1982 * Determine if this is a delta and if so whether we can
1983 * reuse it or not. Otherwise let's find out as cheaply as
1984 * possible what the actual type and size for this object is.
1986 switch (entry->in_pack_type) {
1987 default:
1988 /* Not a delta hence we've already got all we need. */
1989 oe_set_type(entry, entry->in_pack_type);
1990 SET_SIZE(entry, in_pack_size);
1991 entry->in_pack_header_size = used;
1992 if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
1993 goto give_up;
1994 unuse_pack(&w_curs);
1995 return;
1996 case OBJ_REF_DELTA:
1997 if (reuse_delta && !entry->preferred_base) {
1998 oidread(&base_ref,
1999 use_pack(p, &w_curs,
2000 entry->in_pack_offset + used,
2001 NULL));
2002 have_base = 1;
2004 entry->in_pack_header_size = used + the_hash_algo->rawsz;
2005 break;
2006 case OBJ_OFS_DELTA:
2007 buf = use_pack(p, &w_curs,
2008 entry->in_pack_offset + used, NULL);
2009 used_0 = 0;
2010 c = buf[used_0++];
2011 ofs = c & 127;
2012 while (c & 128) {
2013 ofs += 1;
2014 if (!ofs || MSB(ofs, 7)) {
2015 error(_("delta base offset overflow in pack for %s"),
2016 oid_to_hex(&entry->idx.oid));
2017 goto give_up;
2019 c = buf[used_0++];
2020 ofs = (ofs << 7) + (c & 127);
2022 ofs = entry->in_pack_offset - ofs;
2023 if (ofs <= 0 || ofs >= entry->in_pack_offset) {
2024 error(_("delta base offset out of bound for %s"),
2025 oid_to_hex(&entry->idx.oid));
2026 goto give_up;
2028 if (reuse_delta && !entry->preferred_base) {
2029 uint32_t pos;
2030 if (offset_to_pack_pos(p, ofs, &pos) < 0)
2031 goto give_up;
2032 if (!nth_packed_object_id(&base_ref, p,
2033 pack_pos_to_index(p, pos)))
2034 have_base = 1;
2036 entry->in_pack_header_size = used + used_0;
2037 break;
2040 if (have_base &&
2041 can_reuse_delta(&base_ref, entry, &base_entry)) {
2042 oe_set_type(entry, entry->in_pack_type);
2043 SET_SIZE(entry, in_pack_size); /* delta size */
2044 SET_DELTA_SIZE(entry, in_pack_size);
2046 if (base_entry) {
2047 SET_DELTA(entry, base_entry);
2048 entry->delta_sibling_idx = base_entry->delta_child_idx;
2049 SET_DELTA_CHILD(base_entry, entry);
2050 } else {
2051 SET_DELTA_EXT(entry, &base_ref);
2054 unuse_pack(&w_curs);
2055 return;
2058 if (oe_type(entry)) {
2059 off_t delta_pos;
2062 * This must be a delta and we already know what the
2063 * final object type is. Let's extract the actual
2064 * object size from the delta header.
2066 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
2067 canonical_size = get_size_from_delta(p, &w_curs, delta_pos);
2068 if (canonical_size == 0)
2069 goto give_up;
2070 SET_SIZE(entry, canonical_size);
2071 unuse_pack(&w_curs);
2072 return;
2076 * No choice but to fall back to the recursive delta walk
2077 * with oid_object_info() to find about the object type
2078 * at this point...
2080 give_up:
2081 unuse_pack(&w_curs);
2084 if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
2085 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) {
2086 if (repo_has_promisor_remote(the_repository)) {
2087 prefetch_to_pack(object_index);
2088 if (oid_object_info_extended(the_repository, &entry->idx.oid, &oi,
2089 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0)
2090 type = -1;
2091 } else {
2092 type = -1;
2095 oe_set_type(entry, type);
2096 if (entry->type_valid) {
2097 SET_SIZE(entry, canonical_size);
2098 } else {
2100 * Bad object type is checked in prepare_pack(). This is
2101 * to permit a missing preferred base object to be ignored
2102 * as a preferred base. Doing so can result in a larger
2103 * pack file, but the transfer will still take place.
2108 static int pack_offset_sort(const void *_a, const void *_b)
2110 const struct object_entry *a = *(struct object_entry **)_a;
2111 const struct object_entry *b = *(struct object_entry **)_b;
2112 const struct packed_git *a_in_pack = IN_PACK(a);
2113 const struct packed_git *b_in_pack = IN_PACK(b);
2115 /* avoid filesystem trashing with loose objects */
2116 if (!a_in_pack && !b_in_pack)
2117 return oidcmp(&a->idx.oid, &b->idx.oid);
2119 if (a_in_pack < b_in_pack)
2120 return -1;
2121 if (a_in_pack > b_in_pack)
2122 return 1;
2123 return a->in_pack_offset < b->in_pack_offset ? -1 :
2124 (a->in_pack_offset > b->in_pack_offset);
2128 * Drop an on-disk delta we were planning to reuse. Naively, this would
2129 * just involve blanking out the "delta" field, but we have to deal
2130 * with some extra book-keeping:
2132 * 1. Removing ourselves from the delta_sibling linked list.
2134 * 2. Updating our size/type to the non-delta representation. These were
2135 * either not recorded initially (size) or overwritten with the delta type
2136 * (type) when check_object() decided to reuse the delta.
2138 * 3. Resetting our delta depth, as we are now a base object.
2140 static void drop_reused_delta(struct object_entry *entry)
2142 unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
2143 struct object_info oi = OBJECT_INFO_INIT;
2144 enum object_type type;
2145 unsigned long size;
2147 while (*idx) {
2148 struct object_entry *oe = &to_pack.objects[*idx - 1];
2150 if (oe == entry)
2151 *idx = oe->delta_sibling_idx;
2152 else
2153 idx = &oe->delta_sibling_idx;
2155 SET_DELTA(entry, NULL);
2156 entry->depth = 0;
2158 oi.sizep = &size;
2159 oi.typep = &type;
2160 if (packed_object_info(the_repository, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
2162 * We failed to get the info from this pack for some reason;
2163 * fall back to oid_object_info, which may find another copy.
2164 * And if that fails, the error will be recorded in oe_type(entry)
2165 * and dealt with in prepare_pack().
2167 oe_set_type(entry,
2168 oid_object_info(the_repository, &entry->idx.oid, &size));
2169 } else {
2170 oe_set_type(entry, type);
2172 SET_SIZE(entry, size);
2176 * Follow the chain of deltas from this entry onward, throwing away any links
2177 * that cause us to hit a cycle (as determined by the DFS state flags in
2178 * the entries).
2180 * We also detect too-long reused chains that would violate our --depth
2181 * limit.
2183 static void break_delta_chains(struct object_entry *entry)
2186 * The actual depth of each object we will write is stored as an int,
2187 * as it cannot exceed our int "depth" limit. But before we break
2188 * changes based no that limit, we may potentially go as deep as the
2189 * number of objects, which is elsewhere bounded to a uint32_t.
2191 uint32_t total_depth;
2192 struct object_entry *cur, *next;
2194 for (cur = entry, total_depth = 0;
2195 cur;
2196 cur = DELTA(cur), total_depth++) {
2197 if (cur->dfs_state == DFS_DONE) {
2199 * We've already seen this object and know it isn't
2200 * part of a cycle. We do need to append its depth
2201 * to our count.
2203 total_depth += cur->depth;
2204 break;
2208 * We break cycles before looping, so an ACTIVE state (or any
2209 * other cruft which made its way into the state variable)
2210 * is a bug.
2212 if (cur->dfs_state != DFS_NONE)
2213 BUG("confusing delta dfs state in first pass: %d",
2214 cur->dfs_state);
2217 * Now we know this is the first time we've seen the object. If
2218 * it's not a delta, we're done traversing, but we'll mark it
2219 * done to save time on future traversals.
2221 if (!DELTA(cur)) {
2222 cur->dfs_state = DFS_DONE;
2223 break;
2227 * Mark ourselves as active and see if the next step causes
2228 * us to cycle to another active object. It's important to do
2229 * this _before_ we loop, because it impacts where we make the
2230 * cut, and thus how our total_depth counter works.
2231 * E.g., We may see a partial loop like:
2233 * A -> B -> C -> D -> B
2235 * Cutting B->C breaks the cycle. But now the depth of A is
2236 * only 1, and our total_depth counter is at 3. The size of the
2237 * error is always one less than the size of the cycle we
2238 * broke. Commits C and D were "lost" from A's chain.
2240 * If we instead cut D->B, then the depth of A is correct at 3.
2241 * We keep all commits in the chain that we examined.
2243 cur->dfs_state = DFS_ACTIVE;
2244 if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
2245 drop_reused_delta(cur);
2246 cur->dfs_state = DFS_DONE;
2247 break;
2252 * And now that we've gone all the way to the bottom of the chain, we
2253 * need to clear the active flags and set the depth fields as
2254 * appropriate. Unlike the loop above, which can quit when it drops a
2255 * delta, we need to keep going to look for more depth cuts. So we need
2256 * an extra "next" pointer to keep going after we reset cur->delta.
2258 for (cur = entry; cur; cur = next) {
2259 next = DELTA(cur);
2262 * We should have a chain of zero or more ACTIVE states down to
2263 * a final DONE. We can quit after the DONE, because either it
2264 * has no bases, or we've already handled them in a previous
2265 * call.
2267 if (cur->dfs_state == DFS_DONE)
2268 break;
2269 else if (cur->dfs_state != DFS_ACTIVE)
2270 BUG("confusing delta dfs state in second pass: %d",
2271 cur->dfs_state);
2274 * If the total_depth is more than depth, then we need to snip
2275 * the chain into two or more smaller chains that don't exceed
2276 * the maximum depth. Most of the resulting chains will contain
2277 * (depth + 1) entries (i.e., depth deltas plus one base), and
2278 * the last chain (i.e., the one containing entry) will contain
2279 * whatever entries are left over, namely
2280 * (total_depth % (depth + 1)) of them.
2282 * Since we are iterating towards decreasing depth, we need to
2283 * decrement total_depth as we go, and we need to write to the
2284 * entry what its final depth will be after all of the
2285 * snipping. Since we're snipping into chains of length (depth
2286 * + 1) entries, the final depth of an entry will be its
2287 * original depth modulo (depth + 1). Any time we encounter an
2288 * entry whose final depth is supposed to be zero, we snip it
2289 * from its delta base, thereby making it so.
2291 cur->depth = (total_depth--) % (depth + 1);
2292 if (!cur->depth)
2293 drop_reused_delta(cur);
2295 cur->dfs_state = DFS_DONE;
2299 static void get_object_details(void)
2301 uint32_t i;
2302 struct object_entry **sorted_by_offset;
2304 if (progress)
2305 progress_state = start_progress(_("Counting objects"),
2306 to_pack.nr_objects);
2308 CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects);
2309 for (i = 0; i < to_pack.nr_objects; i++)
2310 sorted_by_offset[i] = to_pack.objects + i;
2311 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
2313 for (i = 0; i < to_pack.nr_objects; i++) {
2314 struct object_entry *entry = sorted_by_offset[i];
2315 check_object(entry, i);
2316 if (entry->type_valid &&
2317 oe_size_greater_than(&to_pack, entry, big_file_threshold))
2318 entry->no_try_delta = 1;
2319 display_progress(progress_state, i + 1);
2321 stop_progress(&progress_state);
2324 * This must happen in a second pass, since we rely on the delta
2325 * information for the whole list being completed.
2327 for (i = 0; i < to_pack.nr_objects; i++)
2328 break_delta_chains(&to_pack.objects[i]);
2330 free(sorted_by_offset);
2334 * We search for deltas in a list sorted by type, by filename hash, and then
2335 * by size, so that we see progressively smaller and smaller files.
2336 * That's because we prefer deltas to be from the bigger file
2337 * to the smaller -- deletes are potentially cheaper, but perhaps
2338 * more importantly, the bigger file is likely the more recent
2339 * one. The deepest deltas are therefore the oldest objects which are
2340 * less susceptible to be accessed often.
2342 static int type_size_sort(const void *_a, const void *_b)
2344 const struct object_entry *a = *(struct object_entry **)_a;
2345 const struct object_entry *b = *(struct object_entry **)_b;
2346 const enum object_type a_type = oe_type(a);
2347 const enum object_type b_type = oe_type(b);
2348 const unsigned long a_size = SIZE(a);
2349 const unsigned long b_size = SIZE(b);
2351 if (a_type > b_type)
2352 return -1;
2353 if (a_type < b_type)
2354 return 1;
2355 if (a->hash > b->hash)
2356 return -1;
2357 if (a->hash < b->hash)
2358 return 1;
2359 if (a->preferred_base > b->preferred_base)
2360 return -1;
2361 if (a->preferred_base < b->preferred_base)
2362 return 1;
2363 if (use_delta_islands) {
2364 const int island_cmp = island_delta_cmp(&a->idx.oid, &b->idx.oid);
2365 if (island_cmp)
2366 return island_cmp;
2368 if (a_size > b_size)
2369 return -1;
2370 if (a_size < b_size)
2371 return 1;
2372 return a < b ? -1 : (a > b); /* newest first */
2375 struct unpacked {
2376 struct object_entry *entry;
2377 void *data;
2378 struct delta_index *index;
2379 unsigned depth;
2382 static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
2383 unsigned long delta_size)
2385 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
2386 return 0;
2388 if (delta_size < cache_max_small_delta_size)
2389 return 1;
2391 /* cache delta, if objects are large enough compared to delta size */
2392 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
2393 return 1;
2395 return 0;
2398 /* Protect delta_cache_size */
2399 static pthread_mutex_t cache_mutex;
2400 #define cache_lock() pthread_mutex_lock(&cache_mutex)
2401 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2404 * Protect object list partitioning (e.g. struct thread_param) and
2405 * progress_state
2407 static pthread_mutex_t progress_mutex;
2408 #define progress_lock() pthread_mutex_lock(&progress_mutex)
2409 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2412 * Access to struct object_entry is unprotected since each thread owns
2413 * a portion of the main object list. Just don't access object entries
2414 * ahead in the list because they can be stolen and would need
2415 * progress_mutex for protection.
2418 static inline int oe_size_less_than(struct packing_data *pack,
2419 const struct object_entry *lhs,
2420 unsigned long rhs)
2422 if (lhs->size_valid)
2423 return lhs->size_ < rhs;
2424 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
2425 return 0;
2426 return oe_get_size_slow(pack, lhs) < rhs;
2429 static inline void oe_set_tree_depth(struct packing_data *pack,
2430 struct object_entry *e,
2431 unsigned int tree_depth)
2433 if (!pack->tree_depth)
2434 CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
2435 pack->tree_depth[e - pack->objects] = tree_depth;
2439 * Return the size of the object without doing any delta
2440 * reconstruction (so non-deltas are true object sizes, but deltas
2441 * return the size of the delta data).
2443 unsigned long oe_get_size_slow(struct packing_data *pack,
2444 const struct object_entry *e)
2446 struct packed_git *p;
2447 struct pack_window *w_curs;
2448 unsigned char *buf;
2449 enum object_type type;
2450 unsigned long used, avail, size;
2452 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
2453 packing_data_lock(&to_pack);
2454 if (oid_object_info(the_repository, &e->idx.oid, &size) < 0)
2455 die(_("unable to get size of %s"),
2456 oid_to_hex(&e->idx.oid));
2457 packing_data_unlock(&to_pack);
2458 return size;
2461 p = oe_in_pack(pack, e);
2462 if (!p)
2463 BUG("when e->type is a delta, it must belong to a pack");
2465 packing_data_lock(&to_pack);
2466 w_curs = NULL;
2467 buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
2468 used = unpack_object_header_buffer(buf, avail, &type, &size);
2469 if (used == 0)
2470 die(_("unable to parse object header of %s"),
2471 oid_to_hex(&e->idx.oid));
2473 unuse_pack(&w_curs);
2474 packing_data_unlock(&to_pack);
2475 return size;
2478 static int try_delta(struct unpacked *trg, struct unpacked *src,
2479 unsigned max_depth, unsigned long *mem_usage)
2481 struct object_entry *trg_entry = trg->entry;
2482 struct object_entry *src_entry = src->entry;
2483 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
2484 unsigned ref_depth;
2485 enum object_type type;
2486 void *delta_buf;
2488 /* Don't bother doing diffs between different types */
2489 if (oe_type(trg_entry) != oe_type(src_entry))
2490 return -1;
2493 * We do not bother to try a delta that we discarded on an
2494 * earlier try, but only when reusing delta data. Note that
2495 * src_entry that is marked as the preferred_base should always
2496 * be considered, as even if we produce a suboptimal delta against
2497 * it, we will still save the transfer cost, as we already know
2498 * the other side has it and we won't send src_entry at all.
2500 if (reuse_delta && IN_PACK(trg_entry) &&
2501 IN_PACK(trg_entry) == IN_PACK(src_entry) &&
2502 !src_entry->preferred_base &&
2503 trg_entry->in_pack_type != OBJ_REF_DELTA &&
2504 trg_entry->in_pack_type != OBJ_OFS_DELTA)
2505 return 0;
2507 /* Let's not bust the allowed depth. */
2508 if (src->depth >= max_depth)
2509 return 0;
2511 /* Now some size filtering heuristics. */
2512 trg_size = SIZE(trg_entry);
2513 if (!DELTA(trg_entry)) {
2514 max_size = trg_size/2 - the_hash_algo->rawsz;
2515 ref_depth = 1;
2516 } else {
2517 max_size = DELTA_SIZE(trg_entry);
2518 ref_depth = trg->depth;
2520 max_size = (uint64_t)max_size * (max_depth - src->depth) /
2521 (max_depth - ref_depth + 1);
2522 if (max_size == 0)
2523 return 0;
2524 src_size = SIZE(src_entry);
2525 sizediff = src_size < trg_size ? trg_size - src_size : 0;
2526 if (sizediff >= max_size)
2527 return 0;
2528 if (trg_size < src_size / 32)
2529 return 0;
2531 if (!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))
2532 return 0;
2534 /* Load data if not already done */
2535 if (!trg->data) {
2536 packing_data_lock(&to_pack);
2537 trg->data = repo_read_object_file(the_repository,
2538 &trg_entry->idx.oid, &type,
2539 &sz);
2540 packing_data_unlock(&to_pack);
2541 if (!trg->data)
2542 die(_("object %s cannot be read"),
2543 oid_to_hex(&trg_entry->idx.oid));
2544 if (sz != trg_size)
2545 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2546 oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,
2547 (uintmax_t)trg_size);
2548 *mem_usage += sz;
2550 if (!src->data) {
2551 packing_data_lock(&to_pack);
2552 src->data = repo_read_object_file(the_repository,
2553 &src_entry->idx.oid, &type,
2554 &sz);
2555 packing_data_unlock(&to_pack);
2556 if (!src->data) {
2557 if (src_entry->preferred_base) {
2558 static int warned = 0;
2559 if (!warned++)
2560 warning(_("object %s cannot be read"),
2561 oid_to_hex(&src_entry->idx.oid));
2563 * Those objects are not included in the
2564 * resulting pack. Be resilient and ignore
2565 * them if they can't be read, in case the
2566 * pack could be created nevertheless.
2568 return 0;
2570 die(_("object %s cannot be read"),
2571 oid_to_hex(&src_entry->idx.oid));
2573 if (sz != src_size)
2574 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2575 oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,
2576 (uintmax_t)src_size);
2577 *mem_usage += sz;
2579 if (!src->index) {
2580 src->index = create_delta_index(src->data, src_size);
2581 if (!src->index) {
2582 static int warned = 0;
2583 if (!warned++)
2584 warning(_("suboptimal pack - out of memory"));
2585 return 0;
2587 *mem_usage += sizeof_delta_index(src->index);
2590 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
2591 if (!delta_buf)
2592 return 0;
2594 if (DELTA(trg_entry)) {
2595 /* Prefer only shallower same-sized deltas. */
2596 if (delta_size == DELTA_SIZE(trg_entry) &&
2597 src->depth + 1 >= trg->depth) {
2598 free(delta_buf);
2599 return 0;
2604 * Handle memory allocation outside of the cache
2605 * accounting lock. Compiler will optimize the strangeness
2606 * away when NO_PTHREADS is defined.
2608 free(trg_entry->delta_data);
2609 cache_lock();
2610 if (trg_entry->delta_data) {
2611 delta_cache_size -= DELTA_SIZE(trg_entry);
2612 trg_entry->delta_data = NULL;
2614 if (delta_cacheable(src_size, trg_size, delta_size)) {
2615 delta_cache_size += delta_size;
2616 cache_unlock();
2617 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
2618 } else {
2619 cache_unlock();
2620 free(delta_buf);
2623 SET_DELTA(trg_entry, src_entry);
2624 SET_DELTA_SIZE(trg_entry, delta_size);
2625 trg->depth = src->depth + 1;
2627 return 1;
2630 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
2632 struct object_entry *child = DELTA_CHILD(me);
2633 unsigned int m = n;
2634 while (child) {
2635 const unsigned int c = check_delta_limit(child, n + 1);
2636 if (m < c)
2637 m = c;
2638 child = DELTA_SIBLING(child);
2640 return m;
2643 static unsigned long free_unpacked(struct unpacked *n)
2645 unsigned long freed_mem = sizeof_delta_index(n->index);
2646 free_delta_index(n->index);
2647 n->index = NULL;
2648 if (n->data) {
2649 freed_mem += SIZE(n->entry);
2650 FREE_AND_NULL(n->data);
2652 n->entry = NULL;
2653 n->depth = 0;
2654 return freed_mem;
2657 static void find_deltas(struct object_entry **list, unsigned *list_size,
2658 int window, int depth, unsigned *processed)
2660 uint32_t i, idx = 0, count = 0;
2661 struct unpacked *array;
2662 unsigned long mem_usage = 0;
2664 CALLOC_ARRAY(array, window);
2666 for (;;) {
2667 struct object_entry *entry;
2668 struct unpacked *n = array + idx;
2669 int j, max_depth, best_base = -1;
2671 progress_lock();
2672 if (!*list_size) {
2673 progress_unlock();
2674 break;
2676 entry = *list++;
2677 (*list_size)--;
2678 if (!entry->preferred_base) {
2679 (*processed)++;
2680 display_progress(progress_state, *processed);
2682 progress_unlock();
2684 mem_usage -= free_unpacked(n);
2685 n->entry = entry;
2687 while (window_memory_limit &&
2688 mem_usage > window_memory_limit &&
2689 count > 1) {
2690 const uint32_t tail = (idx + window - count) % window;
2691 mem_usage -= free_unpacked(array + tail);
2692 count--;
2695 /* We do not compute delta to *create* objects we are not
2696 * going to pack.
2698 if (entry->preferred_base)
2699 goto next;
2702 * If the current object is at pack edge, take the depth the
2703 * objects that depend on the current object into account
2704 * otherwise they would become too deep.
2706 max_depth = depth;
2707 if (DELTA_CHILD(entry)) {
2708 max_depth -= check_delta_limit(entry, 0);
2709 if (max_depth <= 0)
2710 goto next;
2713 j = window;
2714 while (--j > 0) {
2715 int ret;
2716 uint32_t other_idx = idx + j;
2717 struct unpacked *m;
2718 if (other_idx >= window)
2719 other_idx -= window;
2720 m = array + other_idx;
2721 if (!m->entry)
2722 break;
2723 ret = try_delta(n, m, max_depth, &mem_usage);
2724 if (ret < 0)
2725 break;
2726 else if (ret > 0)
2727 best_base = other_idx;
2731 * If we decided to cache the delta data, then it is best
2732 * to compress it right away. First because we have to do
2733 * it anyway, and doing it here while we're threaded will
2734 * save a lot of time in the non threaded write phase,
2735 * as well as allow for caching more deltas within
2736 * the same cache size limit.
2737 * ...
2738 * But only if not writing to stdout, since in that case
2739 * the network is most likely throttling writes anyway,
2740 * and therefore it is best to go to the write phase ASAP
2741 * instead, as we can afford spending more time compressing
2742 * between writes at that moment.
2744 if (entry->delta_data && !pack_to_stdout) {
2745 unsigned long size;
2747 size = do_compress(&entry->delta_data, DELTA_SIZE(entry));
2748 if (size < (1U << OE_Z_DELTA_BITS)) {
2749 entry->z_delta_size = size;
2750 cache_lock();
2751 delta_cache_size -= DELTA_SIZE(entry);
2752 delta_cache_size += entry->z_delta_size;
2753 cache_unlock();
2754 } else {
2755 FREE_AND_NULL(entry->delta_data);
2756 entry->z_delta_size = 0;
2760 /* if we made n a delta, and if n is already at max
2761 * depth, leaving it in the window is pointless. we
2762 * should evict it first.
2764 if (DELTA(entry) && max_depth <= n->depth)
2765 continue;
2768 * Move the best delta base up in the window, after the
2769 * currently deltified object, to keep it longer. It will
2770 * be the first base object to be attempted next.
2772 if (DELTA(entry)) {
2773 struct unpacked swap = array[best_base];
2774 int dist = (window + idx - best_base) % window;
2775 int dst = best_base;
2776 while (dist--) {
2777 int src = (dst + 1) % window;
2778 array[dst] = array[src];
2779 dst = src;
2781 array[dst] = swap;
2784 next:
2785 idx++;
2786 if (count + 1 < window)
2787 count++;
2788 if (idx >= window)
2789 idx = 0;
2792 for (i = 0; i < window; ++i) {
2793 free_delta_index(array[i].index);
2794 free(array[i].data);
2796 free(array);
2800 * The main object list is split into smaller lists, each is handed to
2801 * one worker.
2803 * The main thread waits on the condition that (at least) one of the workers
2804 * has stopped working (which is indicated in the .working member of
2805 * struct thread_params).
2807 * When a work thread has completed its work, it sets .working to 0 and
2808 * signals the main thread and waits on the condition that .data_ready
2809 * becomes 1.
2811 * The main thread steals half of the work from the worker that has
2812 * most work left to hand it to the idle worker.
2815 struct thread_params {
2816 pthread_t thread;
2817 struct object_entry **list;
2818 unsigned list_size;
2819 unsigned remaining;
2820 int window;
2821 int depth;
2822 int working;
2823 int data_ready;
2824 pthread_mutex_t mutex;
2825 pthread_cond_t cond;
2826 unsigned *processed;
2829 static pthread_cond_t progress_cond;
2832 * Mutex and conditional variable can't be statically-initialized on Windows.
2834 static void init_threaded_search(void)
2836 pthread_mutex_init(&cache_mutex, NULL);
2837 pthread_mutex_init(&progress_mutex, NULL);
2838 pthread_cond_init(&progress_cond, NULL);
2841 static void cleanup_threaded_search(void)
2843 pthread_cond_destroy(&progress_cond);
2844 pthread_mutex_destroy(&cache_mutex);
2845 pthread_mutex_destroy(&progress_mutex);
2848 static void *threaded_find_deltas(void *arg)
2850 struct thread_params *me = arg;
2852 progress_lock();
2853 while (me->remaining) {
2854 progress_unlock();
2856 find_deltas(me->list, &me->remaining,
2857 me->window, me->depth, me->processed);
2859 progress_lock();
2860 me->working = 0;
2861 pthread_cond_signal(&progress_cond);
2862 progress_unlock();
2865 * We must not set ->data_ready before we wait on the
2866 * condition because the main thread may have set it to 1
2867 * before we get here. In order to be sure that new
2868 * work is available if we see 1 in ->data_ready, it
2869 * was initialized to 0 before this thread was spawned
2870 * and we reset it to 0 right away.
2872 pthread_mutex_lock(&me->mutex);
2873 while (!me->data_ready)
2874 pthread_cond_wait(&me->cond, &me->mutex);
2875 me->data_ready = 0;
2876 pthread_mutex_unlock(&me->mutex);
2878 progress_lock();
2880 progress_unlock();
2881 /* leave ->working 1 so that this doesn't get more work assigned */
2882 return NULL;
2885 static void ll_find_deltas(struct object_entry **list, unsigned list_size,
2886 int window, int depth, unsigned *processed)
2888 struct thread_params *p;
2889 int i, ret, active_threads = 0;
2891 init_threaded_search();
2893 if (delta_search_threads <= 1) {
2894 find_deltas(list, &list_size, window, depth, processed);
2895 cleanup_threaded_search();
2896 return;
2898 if (progress > pack_to_stdout)
2899 fprintf_ln(stderr, _("Delta compression using up to %d threads"),
2900 delta_search_threads);
2901 CALLOC_ARRAY(p, delta_search_threads);
2903 /* Partition the work amongst work threads. */
2904 for (i = 0; i < delta_search_threads; i++) {
2905 unsigned sub_size = list_size / (delta_search_threads - i);
2907 /* don't use too small segments or no deltas will be found */
2908 if (sub_size < 2*window && i+1 < delta_search_threads)
2909 sub_size = 0;
2911 p[i].window = window;
2912 p[i].depth = depth;
2913 p[i].processed = processed;
2914 p[i].working = 1;
2915 p[i].data_ready = 0;
2917 /* try to split chunks on "path" boundaries */
2918 while (sub_size && sub_size < list_size &&
2919 list[sub_size]->hash &&
2920 list[sub_size]->hash == list[sub_size-1]->hash)
2921 sub_size++;
2923 p[i].list = list;
2924 p[i].list_size = sub_size;
2925 p[i].remaining = sub_size;
2927 list += sub_size;
2928 list_size -= sub_size;
2931 /* Start work threads. */
2932 for (i = 0; i < delta_search_threads; i++) {
2933 if (!p[i].list_size)
2934 continue;
2935 pthread_mutex_init(&p[i].mutex, NULL);
2936 pthread_cond_init(&p[i].cond, NULL);
2937 ret = pthread_create(&p[i].thread, NULL,
2938 threaded_find_deltas, &p[i]);
2939 if (ret)
2940 die(_("unable to create thread: %s"), strerror(ret));
2941 active_threads++;
2945 * Now let's wait for work completion. Each time a thread is done
2946 * with its work, we steal half of the remaining work from the
2947 * thread with the largest number of unprocessed objects and give
2948 * it to that newly idle thread. This ensure good load balancing
2949 * until the remaining object list segments are simply too short
2950 * to be worth splitting anymore.
2952 while (active_threads) {
2953 struct thread_params *target = NULL;
2954 struct thread_params *victim = NULL;
2955 unsigned sub_size = 0;
2957 progress_lock();
2958 for (;;) {
2959 for (i = 0; !target && i < delta_search_threads; i++)
2960 if (!p[i].working)
2961 target = &p[i];
2962 if (target)
2963 break;
2964 pthread_cond_wait(&progress_cond, &progress_mutex);
2967 for (i = 0; i < delta_search_threads; i++)
2968 if (p[i].remaining > 2*window &&
2969 (!victim || victim->remaining < p[i].remaining))
2970 victim = &p[i];
2971 if (victim) {
2972 sub_size = victim->remaining / 2;
2973 list = victim->list + victim->list_size - sub_size;
2974 while (sub_size && list[0]->hash &&
2975 list[0]->hash == list[-1]->hash) {
2976 list++;
2977 sub_size--;
2979 if (!sub_size) {
2981 * It is possible for some "paths" to have
2982 * so many objects that no hash boundary
2983 * might be found. Let's just steal the
2984 * exact half in that case.
2986 sub_size = victim->remaining / 2;
2987 list -= sub_size;
2989 target->list = list;
2990 victim->list_size -= sub_size;
2991 victim->remaining -= sub_size;
2993 target->list_size = sub_size;
2994 target->remaining = sub_size;
2995 target->working = 1;
2996 progress_unlock();
2998 pthread_mutex_lock(&target->mutex);
2999 target->data_ready = 1;
3000 pthread_cond_signal(&target->cond);
3001 pthread_mutex_unlock(&target->mutex);
3003 if (!sub_size) {
3004 pthread_join(target->thread, NULL);
3005 pthread_cond_destroy(&target->cond);
3006 pthread_mutex_destroy(&target->mutex);
3007 active_threads--;
3010 cleanup_threaded_search();
3011 free(p);
3014 static int obj_is_packed(const struct object_id *oid)
3016 return packlist_find(&to_pack, oid) ||
3017 (reuse_packfile_bitmap &&
3018 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid));
3021 static void add_tag_chain(const struct object_id *oid)
3023 struct tag *tag;
3026 * We catch duplicates already in add_object_entry(), but we'd
3027 * prefer to do this extra check to avoid having to parse the
3028 * tag at all if we already know that it's being packed (e.g., if
3029 * it was included via bitmaps, we would not have parsed it
3030 * previously).
3032 if (obj_is_packed(oid))
3033 return;
3035 tag = lookup_tag(the_repository, oid);
3036 while (1) {
3037 if (!tag || parse_tag(tag) || !tag->tagged)
3038 die(_("unable to pack objects reachable from tag %s"),
3039 oid_to_hex(oid));
3041 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
3043 if (tag->tagged->type != OBJ_TAG)
3044 return;
3046 tag = (struct tag *)tag->tagged;
3050 static int add_ref_tag(const char *tag UNUSED, const struct object_id *oid,
3051 int flag UNUSED, void *cb_data UNUSED)
3053 struct object_id peeled;
3055 if (!peel_iterated_oid(oid, &peeled) && obj_is_packed(&peeled))
3056 add_tag_chain(oid);
3057 return 0;
3060 static void prepare_pack(int window, int depth)
3062 struct object_entry **delta_list;
3063 uint32_t i, nr_deltas;
3064 unsigned n;
3066 if (use_delta_islands)
3067 resolve_tree_islands(the_repository, progress, &to_pack);
3069 get_object_details();
3072 * If we're locally repacking then we need to be doubly careful
3073 * from now on in order to make sure no stealth corruption gets
3074 * propagated to the new pack. Clients receiving streamed packs
3075 * should validate everything they get anyway so no need to incur
3076 * the additional cost here in that case.
3078 if (!pack_to_stdout)
3079 do_check_packed_object_crc = 1;
3081 if (!to_pack.nr_objects || !window || !depth)
3082 return;
3084 ALLOC_ARRAY(delta_list, to_pack.nr_objects);
3085 nr_deltas = n = 0;
3087 for (i = 0; i < to_pack.nr_objects; i++) {
3088 struct object_entry *entry = to_pack.objects + i;
3090 if (DELTA(entry))
3091 /* This happens if we decided to reuse existing
3092 * delta from a pack. "reuse_delta &&" is implied.
3094 continue;
3096 if (!entry->type_valid ||
3097 oe_size_less_than(&to_pack, entry, 50))
3098 continue;
3100 if (entry->no_try_delta)
3101 continue;
3103 if (!entry->preferred_base) {
3104 nr_deltas++;
3105 if (oe_type(entry) < 0)
3106 die(_("unable to get type of object %s"),
3107 oid_to_hex(&entry->idx.oid));
3108 } else {
3109 if (oe_type(entry) < 0) {
3111 * This object is not found, but we
3112 * don't have to include it anyway.
3114 continue;
3118 delta_list[n++] = entry;
3121 if (nr_deltas && n > 1) {
3122 unsigned nr_done = 0;
3124 if (progress)
3125 progress_state = start_progress(_("Compressing objects"),
3126 nr_deltas);
3127 QSORT(delta_list, n, type_size_sort);
3128 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
3129 stop_progress(&progress_state);
3130 if (nr_done != nr_deltas)
3131 die(_("inconsistency with delta count"));
3133 free(delta_list);
3136 static int git_pack_config(const char *k, const char *v,
3137 const struct config_context *ctx, void *cb)
3139 if (!strcmp(k, "pack.window")) {
3140 window = git_config_int(k, v, ctx->kvi);
3141 return 0;
3143 if (!strcmp(k, "pack.windowmemory")) {
3144 window_memory_limit = git_config_ulong(k, v, ctx->kvi);
3145 return 0;
3147 if (!strcmp(k, "pack.depth")) {
3148 depth = git_config_int(k, v, ctx->kvi);
3149 return 0;
3151 if (!strcmp(k, "pack.deltacachesize")) {
3152 max_delta_cache_size = git_config_int(k, v, ctx->kvi);
3153 return 0;
3155 if (!strcmp(k, "pack.deltacachelimit")) {
3156 cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
3157 return 0;
3159 if (!strcmp(k, "pack.writebitmaphashcache")) {
3160 if (git_config_bool(k, v))
3161 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
3162 else
3163 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
3166 if (!strcmp(k, "pack.writebitmaplookuptable")) {
3167 if (git_config_bool(k, v))
3168 write_bitmap_options |= BITMAP_OPT_LOOKUP_TABLE;
3169 else
3170 write_bitmap_options &= ~BITMAP_OPT_LOOKUP_TABLE;
3173 if (!strcmp(k, "pack.usebitmaps")) {
3174 use_bitmap_index_default = git_config_bool(k, v);
3175 return 0;
3177 if (!strcmp(k, "pack.allowpackreuse")) {
3178 allow_pack_reuse = git_config_bool(k, v);
3179 return 0;
3181 if (!strcmp(k, "pack.threads")) {
3182 delta_search_threads = git_config_int(k, v, ctx->kvi);
3183 if (delta_search_threads < 0)
3184 die(_("invalid number of threads specified (%d)"),
3185 delta_search_threads);
3186 if (!HAVE_THREADS && delta_search_threads != 1) {
3187 warning(_("no threads support, ignoring %s"), k);
3188 delta_search_threads = 0;
3190 return 0;
3192 if (!strcmp(k, "pack.indexversion")) {
3193 pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
3194 if (pack_idx_opts.version > 2)
3195 die(_("bad pack.indexVersion=%"PRIu32),
3196 pack_idx_opts.version);
3197 return 0;
3199 if (!strcmp(k, "pack.writereverseindex")) {
3200 if (git_config_bool(k, v))
3201 pack_idx_opts.flags |= WRITE_REV;
3202 else
3203 pack_idx_opts.flags &= ~WRITE_REV;
3204 return 0;
3206 if (!strcmp(k, "uploadpack.blobpackfileuri")) {
3207 struct configured_exclusion *ex;
3208 const char *oid_end, *pack_end;
3210 * Stores the pack hash. This is not a true object ID, but is
3211 * of the same form.
3213 struct object_id pack_hash;
3215 if (!v)
3216 return config_error_nonbool(k);
3218 ex = xmalloc(sizeof(*ex));
3219 if (parse_oid_hex(v, &ex->e.oid, &oid_end) ||
3220 *oid_end != ' ' ||
3221 parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) ||
3222 *pack_end != ' ')
3223 die(_("value of uploadpack.blobpackfileuri must be "
3224 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v);
3225 if (oidmap_get(&configured_exclusions, &ex->e.oid))
3226 die(_("object already configured in another "
3227 "uploadpack.blobpackfileuri (got '%s')"), v);
3228 ex->pack_hash_hex = xcalloc(1, pack_end - oid_end);
3229 memcpy(ex->pack_hash_hex, oid_end + 1, pack_end - oid_end - 1);
3230 ex->uri = xstrdup(pack_end + 1);
3231 oidmap_put(&configured_exclusions, ex);
3233 return git_default_config(k, v, ctx, cb);
3236 /* Counters for trace2 output when in --stdin-packs mode. */
3237 static int stdin_packs_found_nr;
3238 static int stdin_packs_hints_nr;
3240 static int add_object_entry_from_pack(const struct object_id *oid,
3241 struct packed_git *p,
3242 uint32_t pos,
3243 void *_data)
3245 off_t ofs;
3246 enum object_type type = OBJ_NONE;
3248 display_progress(progress_state, ++nr_seen);
3250 if (have_duplicate_entry(oid, 0))
3251 return 0;
3253 ofs = nth_packed_object_offset(p, pos);
3254 if (!want_object_in_pack(oid, 0, &p, &ofs))
3255 return 0;
3257 if (p) {
3258 struct rev_info *revs = _data;
3259 struct object_info oi = OBJECT_INFO_INIT;
3261 oi.typep = &type;
3262 if (packed_object_info(the_repository, p, ofs, &oi) < 0) {
3263 die(_("could not get type of object %s in pack %s"),
3264 oid_to_hex(oid), p->pack_name);
3265 } else if (type == OBJ_COMMIT) {
3267 * commits in included packs are used as starting points for the
3268 * subsequent revision walk
3270 add_pending_oid(revs, NULL, oid, 0);
3273 stdin_packs_found_nr++;
3276 create_object_entry(oid, type, 0, 0, 0, p, ofs);
3278 return 0;
3281 static void show_commit_pack_hint(struct commit *commit UNUSED,
3282 void *data UNUSED)
3284 /* nothing to do; commits don't have a namehash */
3287 static void show_object_pack_hint(struct object *object, const char *name,
3288 void *data UNUSED)
3290 struct object_entry *oe = packlist_find(&to_pack, &object->oid);
3291 if (!oe)
3292 return;
3295 * Our 'to_pack' list was constructed by iterating all objects packed in
3296 * included packs, and so doesn't have a non-zero hash field that you
3297 * would typically pick up during a reachability traversal.
3299 * Make a best-effort attempt to fill in the ->hash and ->no_try_delta
3300 * here using a now in order to perhaps improve the delta selection
3301 * process.
3303 oe->hash = pack_name_hash(name);
3304 oe->no_try_delta = name && no_try_delta(name);
3306 stdin_packs_hints_nr++;
3309 static int pack_mtime_cmp(const void *_a, const void *_b)
3311 struct packed_git *a = ((const struct string_list_item*)_a)->util;
3312 struct packed_git *b = ((const struct string_list_item*)_b)->util;
3315 * order packs by descending mtime so that objects are laid out
3316 * roughly as newest-to-oldest
3318 if (a->mtime < b->mtime)
3319 return 1;
3320 else if (b->mtime < a->mtime)
3321 return -1;
3322 else
3323 return 0;
3326 static void read_packs_list_from_stdin(void)
3328 struct strbuf buf = STRBUF_INIT;
3329 struct string_list include_packs = STRING_LIST_INIT_DUP;
3330 struct string_list exclude_packs = STRING_LIST_INIT_DUP;
3331 struct string_list_item *item = NULL;
3333 struct packed_git *p;
3334 struct rev_info revs;
3336 repo_init_revisions(the_repository, &revs, NULL);
3338 * Use a revision walk to fill in the namehash of objects in the include
3339 * packs. To save time, we'll avoid traversing through objects that are
3340 * in excluded packs.
3342 * That may cause us to avoid populating all of the namehash fields of
3343 * all included objects, but our goal is best-effort, since this is only
3344 * an optimization during delta selection.
3346 revs.no_kept_objects = 1;
3347 revs.keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
3348 revs.blob_objects = 1;
3349 revs.tree_objects = 1;
3350 revs.tag_objects = 1;
3351 revs.ignore_missing_links = 1;
3353 while (strbuf_getline(&buf, stdin) != EOF) {
3354 if (!buf.len)
3355 continue;
3357 if (*buf.buf == '^')
3358 string_list_append(&exclude_packs, buf.buf + 1);
3359 else
3360 string_list_append(&include_packs, buf.buf);
3362 strbuf_reset(&buf);
3365 string_list_sort(&include_packs);
3366 string_list_remove_duplicates(&include_packs, 0);
3367 string_list_sort(&exclude_packs);
3368 string_list_remove_duplicates(&exclude_packs, 0);
3370 for (p = get_all_packs(the_repository); p; p = p->next) {
3371 const char *pack_name = pack_basename(p);
3373 if ((item = string_list_lookup(&include_packs, pack_name)))
3374 item->util = p;
3375 if ((item = string_list_lookup(&exclude_packs, pack_name)))
3376 item->util = p;
3380 * Arguments we got on stdin may not even be packs. First
3381 * check that to avoid segfaulting later on in
3382 * e.g. pack_mtime_cmp(), excluded packs are handled below.
3384 * Since we first parsed our STDIN and then sorted the input
3385 * lines the pack we error on will be whatever line happens to
3386 * sort first. This is lazy, it's enough that we report one
3387 * bad case here, we don't need to report the first/last one,
3388 * or all of them.
3390 for_each_string_list_item(item, &include_packs) {
3391 struct packed_git *p = item->util;
3392 if (!p)
3393 die(_("could not find pack '%s'"), item->string);
3394 if (!is_pack_valid(p))
3395 die(_("packfile %s cannot be accessed"), p->pack_name);
3399 * Then, handle all of the excluded packs, marking them as
3400 * kept in-core so that later calls to add_object_entry()
3401 * discards any objects that are also found in excluded packs.
3403 for_each_string_list_item(item, &exclude_packs) {
3404 struct packed_git *p = item->util;
3405 if (!p)
3406 die(_("could not find pack '%s'"), item->string);
3407 p->pack_keep_in_core = 1;
3411 * Order packs by ascending mtime; use QSORT directly to access the
3412 * string_list_item's ->util pointer, which string_list_sort() does not
3413 * provide.
3415 QSORT(include_packs.items, include_packs.nr, pack_mtime_cmp);
3417 for_each_string_list_item(item, &include_packs) {
3418 struct packed_git *p = item->util;
3419 for_each_object_in_pack(p,
3420 add_object_entry_from_pack,
3421 &revs,
3422 FOR_EACH_OBJECT_PACK_ORDER);
3425 if (prepare_revision_walk(&revs))
3426 die(_("revision walk setup failed"));
3427 traverse_commit_list(&revs,
3428 show_commit_pack_hint,
3429 show_object_pack_hint,
3430 NULL);
3432 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_found",
3433 stdin_packs_found_nr);
3434 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_hints",
3435 stdin_packs_hints_nr);
3437 strbuf_release(&buf);
3438 string_list_clear(&include_packs, 0);
3439 string_list_clear(&exclude_packs, 0);
3442 static void add_cruft_object_entry(const struct object_id *oid, enum object_type type,
3443 struct packed_git *pack, off_t offset,
3444 const char *name, uint32_t mtime)
3446 struct object_entry *entry;
3448 display_progress(progress_state, ++nr_seen);
3450 entry = packlist_find(&to_pack, oid);
3451 if (entry) {
3452 if (name) {
3453 entry->hash = pack_name_hash(name);
3454 entry->no_try_delta = no_try_delta(name);
3456 } else {
3457 if (!want_object_in_pack(oid, 0, &pack, &offset))
3458 return;
3459 if (!pack && type == OBJ_BLOB && !has_loose_object(oid)) {
3461 * If a traversed tree has a missing blob then we want
3462 * to avoid adding that missing object to our pack.
3464 * This only applies to missing blobs, not trees,
3465 * because the traversal needs to parse sub-trees but
3466 * not blobs.
3468 * Note we only perform this check when we couldn't
3469 * already find the object in a pack, so we're really
3470 * limited to "ensure non-tip blobs which don't exist in
3471 * packs do exist via loose objects". Confused?
3473 return;
3476 entry = create_object_entry(oid, type, pack_name_hash(name),
3477 0, name && no_try_delta(name),
3478 pack, offset);
3481 if (mtime > oe_cruft_mtime(&to_pack, entry))
3482 oe_set_cruft_mtime(&to_pack, entry, mtime);
3483 return;
3486 static void show_cruft_object(struct object *obj, const char *name, void *data UNUSED)
3489 * if we did not record it earlier, it's at least as old as our
3490 * expiration value. Rather than find it exactly, just use that
3491 * value. This may bump it forward from its real mtime, but it
3492 * will still be "too old" next time we run with the same
3493 * expiration.
3495 * if obj does appear in the packing list, this call is a noop (or may
3496 * set the namehash).
3498 add_cruft_object_entry(&obj->oid, obj->type, NULL, 0, name, cruft_expiration);
3501 static void show_cruft_commit(struct commit *commit, void *data)
3503 show_cruft_object((struct object*)commit, NULL, data);
3506 static int cruft_include_check_obj(struct object *obj, void *data UNUSED)
3508 return !has_object_kept_pack(&obj->oid, IN_CORE_KEEP_PACKS);
3511 static int cruft_include_check(struct commit *commit, void *data)
3513 return cruft_include_check_obj((struct object*)commit, data);
3516 static void set_cruft_mtime(const struct object *object,
3517 struct packed_git *pack,
3518 off_t offset, time_t mtime)
3520 add_cruft_object_entry(&object->oid, object->type, pack, offset, NULL,
3521 mtime);
3524 static void mark_pack_kept_in_core(struct string_list *packs, unsigned keep)
3526 struct string_list_item *item = NULL;
3527 for_each_string_list_item(item, packs) {
3528 struct packed_git *p = item->util;
3529 if (!p)
3530 die(_("could not find pack '%s'"), item->string);
3531 p->pack_keep_in_core = keep;
3535 static void add_unreachable_loose_objects(void);
3536 static void add_objects_in_unpacked_packs(void);
3538 static void enumerate_cruft_objects(void)
3540 if (progress)
3541 progress_state = start_progress(_("Enumerating cruft objects"), 0);
3543 add_objects_in_unpacked_packs();
3544 add_unreachable_loose_objects();
3546 stop_progress(&progress_state);
3549 static void enumerate_and_traverse_cruft_objects(struct string_list *fresh_packs)
3551 struct packed_git *p;
3552 struct rev_info revs;
3553 int ret;
3555 repo_init_revisions(the_repository, &revs, NULL);
3557 revs.tag_objects = 1;
3558 revs.tree_objects = 1;
3559 revs.blob_objects = 1;
3561 revs.include_check = cruft_include_check;
3562 revs.include_check_obj = cruft_include_check_obj;
3564 revs.ignore_missing_links = 1;
3566 if (progress)
3567 progress_state = start_progress(_("Enumerating cruft objects"), 0);
3568 ret = add_unseen_recent_objects_to_traversal(&revs, cruft_expiration,
3569 set_cruft_mtime, 1);
3570 stop_progress(&progress_state);
3572 if (ret)
3573 die(_("unable to add cruft objects"));
3576 * Re-mark only the fresh packs as kept so that objects in
3577 * unknown packs do not halt the reachability traversal early.
3579 for (p = get_all_packs(the_repository); p; p = p->next)
3580 p->pack_keep_in_core = 0;
3581 mark_pack_kept_in_core(fresh_packs, 1);
3583 if (prepare_revision_walk(&revs))
3584 die(_("revision walk setup failed"));
3585 if (progress)
3586 progress_state = start_progress(_("Traversing cruft objects"), 0);
3587 nr_seen = 0;
3588 traverse_commit_list(&revs, show_cruft_commit, show_cruft_object, NULL);
3590 stop_progress(&progress_state);
3593 static void read_cruft_objects(void)
3595 struct strbuf buf = STRBUF_INIT;
3596 struct string_list discard_packs = STRING_LIST_INIT_DUP;
3597 struct string_list fresh_packs = STRING_LIST_INIT_DUP;
3598 struct packed_git *p;
3600 ignore_packed_keep_in_core = 1;
3602 while (strbuf_getline(&buf, stdin) != EOF) {
3603 if (!buf.len)
3604 continue;
3606 if (*buf.buf == '-')
3607 string_list_append(&discard_packs, buf.buf + 1);
3608 else
3609 string_list_append(&fresh_packs, buf.buf);
3612 string_list_sort(&discard_packs);
3613 string_list_sort(&fresh_packs);
3615 for (p = get_all_packs(the_repository); p; p = p->next) {
3616 const char *pack_name = pack_basename(p);
3617 struct string_list_item *item;
3619 item = string_list_lookup(&fresh_packs, pack_name);
3620 if (!item)
3621 item = string_list_lookup(&discard_packs, pack_name);
3623 if (item) {
3624 item->util = p;
3625 } else {
3627 * This pack wasn't mentioned in either the "fresh" or
3628 * "discard" list, so the caller didn't know about it.
3630 * Mark it as kept so that its objects are ignored by
3631 * add_unseen_recent_objects_to_traversal(). We'll
3632 * unmark it before starting the traversal so it doesn't
3633 * halt the traversal early.
3635 p->pack_keep_in_core = 1;
3639 mark_pack_kept_in_core(&fresh_packs, 1);
3640 mark_pack_kept_in_core(&discard_packs, 0);
3642 if (cruft_expiration)
3643 enumerate_and_traverse_cruft_objects(&fresh_packs);
3644 else
3645 enumerate_cruft_objects();
3647 strbuf_release(&buf);
3648 string_list_clear(&discard_packs, 0);
3649 string_list_clear(&fresh_packs, 0);
3652 static void read_object_list_from_stdin(void)
3654 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
3655 struct object_id oid;
3656 const char *p;
3658 for (;;) {
3659 if (!fgets(line, sizeof(line), stdin)) {
3660 if (feof(stdin))
3661 break;
3662 if (!ferror(stdin))
3663 BUG("fgets returned NULL, not EOF, not error!");
3664 if (errno != EINTR)
3665 die_errno("fgets");
3666 clearerr(stdin);
3667 continue;
3669 if (line[0] == '-') {
3670 if (get_oid_hex(line+1, &oid))
3671 die(_("expected edge object ID, got garbage:\n %s"),
3672 line);
3673 add_preferred_base(&oid);
3674 continue;
3676 if (parse_oid_hex(line, &oid, &p))
3677 die(_("expected object ID, got garbage:\n %s"), line);
3679 add_preferred_base_object(p + 1);
3680 add_object_entry(&oid, OBJ_NONE, p + 1, 0);
3684 static void show_commit(struct commit *commit, void *data UNUSED)
3686 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
3688 if (write_bitmap_index)
3689 index_commit_for_bitmap(commit);
3691 if (use_delta_islands)
3692 propagate_island_marks(commit);
3695 static void show_object(struct object *obj, const char *name,
3696 void *data UNUSED)
3698 add_preferred_base_object(name);
3699 add_object_entry(&obj->oid, obj->type, name, 0);
3701 if (use_delta_islands) {
3702 const char *p;
3703 unsigned depth;
3704 struct object_entry *ent;
3706 /* the empty string is a root tree, which is depth 0 */
3707 depth = *name ? 1 : 0;
3708 for (p = strchr(name, '/'); p; p = strchr(p + 1, '/'))
3709 depth++;
3711 ent = packlist_find(&to_pack, &obj->oid);
3712 if (ent && depth > oe_tree_depth(&to_pack, ent))
3713 oe_set_tree_depth(&to_pack, ent, depth);
3717 static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
3719 assert(arg_missing_action == MA_ALLOW_ANY);
3722 * Quietly ignore ALL missing objects. This avoids problems with
3723 * staging them now and getting an odd error later.
3725 if (!has_object(the_repository, &obj->oid, 0))
3726 return;
3728 show_object(obj, name, data);
3731 static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
3733 assert(arg_missing_action == MA_ALLOW_PROMISOR);
3736 * Quietly ignore EXPECTED missing objects. This avoids problems with
3737 * staging them now and getting an odd error later.
3739 if (!has_object(the_repository, &obj->oid, 0) && is_promisor_object(&obj->oid))
3740 return;
3742 show_object(obj, name, data);
3745 static int option_parse_missing_action(const struct option *opt UNUSED,
3746 const char *arg, int unset)
3748 assert(arg);
3749 assert(!unset);
3751 if (!strcmp(arg, "error")) {
3752 arg_missing_action = MA_ERROR;
3753 fn_show_object = show_object;
3754 return 0;
3757 if (!strcmp(arg, "allow-any")) {
3758 arg_missing_action = MA_ALLOW_ANY;
3759 fetch_if_missing = 0;
3760 fn_show_object = show_object__ma_allow_any;
3761 return 0;
3764 if (!strcmp(arg, "allow-promisor")) {
3765 arg_missing_action = MA_ALLOW_PROMISOR;
3766 fetch_if_missing = 0;
3767 fn_show_object = show_object__ma_allow_promisor;
3768 return 0;
3771 die(_("invalid value for '%s': '%s'"), "--missing", arg);
3772 return 0;
3775 static void show_edge(struct commit *commit)
3777 add_preferred_base(&commit->object.oid);
3780 static int add_object_in_unpacked_pack(const struct object_id *oid,
3781 struct packed_git *pack,
3782 uint32_t pos,
3783 void *data UNUSED)
3785 if (cruft) {
3786 off_t offset;
3787 time_t mtime;
3789 if (pack->is_cruft) {
3790 if (load_pack_mtimes(pack) < 0)
3791 die(_("could not load cruft pack .mtimes"));
3792 mtime = nth_packed_mtime(pack, pos);
3793 } else {
3794 mtime = pack->mtime;
3796 offset = nth_packed_object_offset(pack, pos);
3798 add_cruft_object_entry(oid, OBJ_NONE, pack, offset,
3799 NULL, mtime);
3800 } else {
3801 add_object_entry(oid, OBJ_NONE, "", 0);
3803 return 0;
3806 static void add_objects_in_unpacked_packs(void)
3808 if (for_each_packed_object(add_object_in_unpacked_pack, NULL,
3809 FOR_EACH_OBJECT_PACK_ORDER |
3810 FOR_EACH_OBJECT_LOCAL_ONLY |
3811 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
3812 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS))
3813 die(_("cannot open pack index"));
3816 static int add_loose_object(const struct object_id *oid, const char *path,
3817 void *data UNUSED)
3819 enum object_type type = oid_object_info(the_repository, oid, NULL);
3821 if (type < 0) {
3822 warning(_("loose object at %s could not be examined"), path);
3823 return 0;
3826 if (cruft) {
3827 struct stat st;
3828 if (stat(path, &st) < 0) {
3829 if (errno == ENOENT)
3830 return 0;
3831 return error_errno("unable to stat %s", oid_to_hex(oid));
3834 add_cruft_object_entry(oid, type, NULL, 0, NULL,
3835 st.st_mtime);
3836 } else {
3837 add_object_entry(oid, type, "", 0);
3839 return 0;
3843 * We actually don't even have to worry about reachability here.
3844 * add_object_entry will weed out duplicates, so we just add every
3845 * loose object we find.
3847 static void add_unreachable_loose_objects(void)
3849 for_each_loose_file_in_objdir(get_object_directory(),
3850 add_loose_object,
3851 NULL, NULL, NULL);
3854 static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
3856 static struct packed_git *last_found = (void *)1;
3857 struct packed_git *p;
3859 p = (last_found != (void *)1) ? last_found :
3860 get_all_packs(the_repository);
3862 while (p) {
3863 if ((!p->pack_local || p->pack_keep ||
3864 p->pack_keep_in_core) &&
3865 find_pack_entry_one(oid->hash, p)) {
3866 last_found = p;
3867 return 1;
3869 if (p == last_found)
3870 p = get_all_packs(the_repository);
3871 else
3872 p = p->next;
3873 if (p == last_found)
3874 p = p->next;
3876 return 0;
3880 * Store a list of sha1s that are should not be discarded
3881 * because they are either written too recently, or are
3882 * reachable from another object that was.
3884 * This is filled by get_object_list.
3886 static struct oid_array recent_objects;
3888 static int loosened_object_can_be_discarded(const struct object_id *oid,
3889 timestamp_t mtime)
3891 if (!unpack_unreachable_expiration)
3892 return 0;
3893 if (mtime > unpack_unreachable_expiration)
3894 return 0;
3895 if (oid_array_lookup(&recent_objects, oid) >= 0)
3896 return 0;
3897 return 1;
3900 static void loosen_unused_packed_objects(void)
3902 struct packed_git *p;
3903 uint32_t i;
3904 uint32_t loosened_objects_nr = 0;
3905 struct object_id oid;
3907 for (p = get_all_packs(the_repository); p; p = p->next) {
3908 if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
3909 continue;
3911 if (open_pack_index(p))
3912 die(_("cannot open pack index"));
3914 for (i = 0; i < p->num_objects; i++) {
3915 nth_packed_object_id(&oid, p, i);
3916 if (!packlist_find(&to_pack, &oid) &&
3917 !has_sha1_pack_kept_or_nonlocal(&oid) &&
3918 !loosened_object_can_be_discarded(&oid, p->mtime)) {
3919 if (force_object_loose(&oid, p->mtime))
3920 die(_("unable to force loose object"));
3921 loosened_objects_nr++;
3926 trace2_data_intmax("pack-objects", the_repository,
3927 "loosen_unused_packed_objects/loosened", loosened_objects_nr);
3931 * This tracks any options which pack-reuse code expects to be on, or which a
3932 * reader of the pack might not understand, and which would therefore prevent
3933 * blind reuse of what we have on disk.
3935 static int pack_options_allow_reuse(void)
3937 return allow_pack_reuse &&
3938 pack_to_stdout &&
3939 !ignore_packed_keep_on_disk &&
3940 !ignore_packed_keep_in_core &&
3941 (!local || !have_non_local_packs) &&
3942 !incremental;
3945 static int get_object_list_from_bitmap(struct rev_info *revs)
3947 if (!(bitmap_git = prepare_bitmap_walk(revs, 0)))
3948 return -1;
3950 if (pack_options_allow_reuse() &&
3951 !reuse_partial_packfile_from_bitmap(
3952 bitmap_git,
3953 &reuse_packfile,
3954 &reuse_packfile_objects,
3955 &reuse_packfile_bitmap)) {
3956 assert(reuse_packfile_objects);
3957 nr_result += reuse_packfile_objects;
3958 nr_seen += reuse_packfile_objects;
3959 display_progress(progress_state, nr_seen);
3962 traverse_bitmap_commit_list(bitmap_git, revs,
3963 &add_object_entry_from_bitmap);
3964 return 0;
3967 static void record_recent_object(struct object *obj,
3968 const char *name UNUSED,
3969 void *data UNUSED)
3971 oid_array_append(&recent_objects, &obj->oid);
3974 static void record_recent_commit(struct commit *commit, void *data UNUSED)
3976 oid_array_append(&recent_objects, &commit->object.oid);
3979 static int mark_bitmap_preferred_tip(const char *refname,
3980 const struct object_id *oid,
3981 int flags UNUSED,
3982 void *data UNUSED)
3984 struct object_id peeled;
3985 struct object *object;
3987 if (!peel_iterated_oid(oid, &peeled))
3988 oid = &peeled;
3990 object = parse_object_or_die(oid, refname);
3991 if (object->type == OBJ_COMMIT)
3992 object->flags |= NEEDS_BITMAP;
3994 return 0;
3997 static void mark_bitmap_preferred_tips(void)
3999 struct string_list_item *item;
4000 const struct string_list *preferred_tips;
4002 preferred_tips = bitmap_preferred_tips(the_repository);
4003 if (!preferred_tips)
4004 return;
4006 for_each_string_list_item(item, preferred_tips) {
4007 for_each_ref_in(item->string, mark_bitmap_preferred_tip, NULL);
4011 static void get_object_list(struct rev_info *revs, int ac, const char **av)
4013 struct setup_revision_opt s_r_opt = {
4014 .allow_exclude_promisor_objects = 1,
4016 char line[1000];
4017 int flags = 0;
4018 int save_warning;
4020 save_commit_buffer = 0;
4021 setup_revisions(ac, av, revs, &s_r_opt);
4023 /* make sure shallows are read */
4024 is_repository_shallow(the_repository);
4026 save_warning = warn_on_object_refname_ambiguity;
4027 warn_on_object_refname_ambiguity = 0;
4029 while (fgets(line, sizeof(line), stdin) != NULL) {
4030 int len = strlen(line);
4031 if (len && line[len - 1] == '\n')
4032 line[--len] = 0;
4033 if (!len)
4034 break;
4035 if (*line == '-') {
4036 if (!strcmp(line, "--not")) {
4037 flags ^= UNINTERESTING;
4038 write_bitmap_index = 0;
4039 continue;
4041 if (starts_with(line, "--shallow ")) {
4042 struct object_id oid;
4043 if (get_oid_hex(line + 10, &oid))
4044 die("not an object name '%s'", line + 10);
4045 register_shallow(the_repository, &oid);
4046 use_bitmap_index = 0;
4047 continue;
4049 die(_("not a rev '%s'"), line);
4051 if (handle_revision_arg(line, revs, flags, REVARG_CANNOT_BE_FILENAME))
4052 die(_("bad revision '%s'"), line);
4055 warn_on_object_refname_ambiguity = save_warning;
4057 if (use_bitmap_index && !get_object_list_from_bitmap(revs))
4058 return;
4060 if (use_delta_islands)
4061 load_delta_islands(the_repository, progress);
4063 if (write_bitmap_index)
4064 mark_bitmap_preferred_tips();
4066 if (prepare_revision_walk(revs))
4067 die(_("revision walk setup failed"));
4068 mark_edges_uninteresting(revs, show_edge, sparse);
4070 if (!fn_show_object)
4071 fn_show_object = show_object;
4072 traverse_commit_list(revs,
4073 show_commit, fn_show_object,
4074 NULL);
4076 if (unpack_unreachable_expiration) {
4077 revs->ignore_missing_links = 1;
4078 if (add_unseen_recent_objects_to_traversal(revs,
4079 unpack_unreachable_expiration, NULL, 0))
4080 die(_("unable to add recent objects"));
4081 if (prepare_revision_walk(revs))
4082 die(_("revision walk setup failed"));
4083 traverse_commit_list(revs, record_recent_commit,
4084 record_recent_object, NULL);
4087 if (keep_unreachable)
4088 add_objects_in_unpacked_packs();
4089 if (pack_loose_unreachable)
4090 add_unreachable_loose_objects();
4091 if (unpack_unreachable)
4092 loosen_unused_packed_objects();
4094 oid_array_clear(&recent_objects);
4097 static void add_extra_kept_packs(const struct string_list *names)
4099 struct packed_git *p;
4101 if (!names->nr)
4102 return;
4104 for (p = get_all_packs(the_repository); p; p = p->next) {
4105 const char *name = basename(p->pack_name);
4106 int i;
4108 if (!p->pack_local)
4109 continue;
4111 for (i = 0; i < names->nr; i++)
4112 if (!fspathcmp(name, names->items[i].string))
4113 break;
4115 if (i < names->nr) {
4116 p->pack_keep_in_core = 1;
4117 ignore_packed_keep_in_core = 1;
4118 continue;
4123 static int option_parse_quiet(const struct option *opt, const char *arg,
4124 int unset)
4126 int *val = opt->value;
4128 BUG_ON_OPT_ARG(arg);
4130 if (!unset)
4131 *val = 0;
4132 else if (!*val)
4133 *val = 1;
4134 return 0;
4137 static int option_parse_index_version(const struct option *opt,
4138 const char *arg, int unset)
4140 struct pack_idx_option *popts = opt->value;
4141 char *c;
4142 const char *val = arg;
4144 BUG_ON_OPT_NEG(unset);
4146 popts->version = strtoul(val, &c, 10);
4147 if (popts->version > 2)
4148 die(_("unsupported index version %s"), val);
4149 if (*c == ',' && c[1])
4150 popts->off32_limit = strtoul(c+1, &c, 0);
4151 if (*c || popts->off32_limit & 0x80000000)
4152 die(_("bad index version '%s'"), val);
4153 return 0;
4156 static int option_parse_unpack_unreachable(const struct option *opt UNUSED,
4157 const char *arg, int unset)
4159 if (unset) {
4160 unpack_unreachable = 0;
4161 unpack_unreachable_expiration = 0;
4163 else {
4164 unpack_unreachable = 1;
4165 if (arg)
4166 unpack_unreachable_expiration = approxidate(arg);
4168 return 0;
4171 static int option_parse_cruft_expiration(const struct option *opt UNUSED,
4172 const char *arg, int unset)
4174 if (unset) {
4175 cruft = 0;
4176 cruft_expiration = 0;
4177 } else {
4178 cruft = 1;
4179 if (arg)
4180 cruft_expiration = approxidate(arg);
4182 return 0;
4185 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
4187 int use_internal_rev_list = 0;
4188 int shallow = 0;
4189 int all_progress_implied = 0;
4190 struct strvec rp = STRVEC_INIT;
4191 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
4192 int rev_list_index = 0;
4193 int stdin_packs = 0;
4194 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
4195 struct list_objects_filter_options filter_options =
4196 LIST_OBJECTS_FILTER_INIT;
4198 struct option pack_objects_options[] = {
4199 OPT_CALLBACK_F('q', "quiet", &progress, NULL,
4200 N_("do not show progress meter"),
4201 PARSE_OPT_NOARG, option_parse_quiet),
4202 OPT_SET_INT(0, "progress", &progress,
4203 N_("show progress meter"), 1),
4204 OPT_SET_INT(0, "all-progress", &progress,
4205 N_("show progress meter during object writing phase"), 2),
4206 OPT_BOOL(0, "all-progress-implied",
4207 &all_progress_implied,
4208 N_("similar to --all-progress when progress meter is shown")),
4209 OPT_CALLBACK_F(0, "index-version", &pack_idx_opts, N_("<version>[,<offset>]"),
4210 N_("write the pack index file in the specified idx format version"),
4211 PARSE_OPT_NONEG, option_parse_index_version),
4212 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
4213 N_("maximum size of each output pack file")),
4214 OPT_BOOL(0, "local", &local,
4215 N_("ignore borrowed objects from alternate object store")),
4216 OPT_BOOL(0, "incremental", &incremental,
4217 N_("ignore packed objects")),
4218 OPT_INTEGER(0, "window", &window,
4219 N_("limit pack window by objects")),
4220 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
4221 N_("limit pack window by memory in addition to object limit")),
4222 OPT_INTEGER(0, "depth", &depth,
4223 N_("maximum length of delta chain allowed in the resulting pack")),
4224 OPT_BOOL(0, "reuse-delta", &reuse_delta,
4225 N_("reuse existing deltas")),
4226 OPT_BOOL(0, "reuse-object", &reuse_object,
4227 N_("reuse existing objects")),
4228 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
4229 N_("use OFS_DELTA objects")),
4230 OPT_INTEGER(0, "threads", &delta_search_threads,
4231 N_("use threads when searching for best delta matches")),
4232 OPT_BOOL(0, "non-empty", &non_empty,
4233 N_("do not create an empty pack output")),
4234 OPT_BOOL(0, "revs", &use_internal_rev_list,
4235 N_("read revision arguments from standard input")),
4236 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked,
4237 N_("limit the objects to those that are not yet packed"),
4238 1, PARSE_OPT_NONEG),
4239 OPT_SET_INT_F(0, "all", &rev_list_all,
4240 N_("include objects reachable from any reference"),
4241 1, PARSE_OPT_NONEG),
4242 OPT_SET_INT_F(0, "reflog", &rev_list_reflog,
4243 N_("include objects referred by reflog entries"),
4244 1, PARSE_OPT_NONEG),
4245 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index,
4246 N_("include objects referred to by the index"),
4247 1, PARSE_OPT_NONEG),
4248 OPT_BOOL(0, "stdin-packs", &stdin_packs,
4249 N_("read packs from stdin")),
4250 OPT_BOOL(0, "stdout", &pack_to_stdout,
4251 N_("output pack to stdout")),
4252 OPT_BOOL(0, "include-tag", &include_tag,
4253 N_("include tag objects that refer to objects to be packed")),
4254 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
4255 N_("keep unreachable objects")),
4256 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
4257 N_("pack loose unreachable objects")),
4258 OPT_CALLBACK_F(0, "unpack-unreachable", NULL, N_("time"),
4259 N_("unpack unreachable objects newer than <time>"),
4260 PARSE_OPT_OPTARG, option_parse_unpack_unreachable),
4261 OPT_BOOL(0, "cruft", &cruft, N_("create a cruft pack")),
4262 OPT_CALLBACK_F(0, "cruft-expiration", NULL, N_("time"),
4263 N_("expire cruft objects older than <time>"),
4264 PARSE_OPT_OPTARG, option_parse_cruft_expiration),
4265 OPT_BOOL(0, "sparse", &sparse,
4266 N_("use the sparse reachability algorithm")),
4267 OPT_BOOL(0, "thin", &thin,
4268 N_("create thin packs")),
4269 OPT_BOOL(0, "shallow", &shallow,
4270 N_("create packs suitable for shallow fetches")),
4271 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
4272 N_("ignore packs that have companion .keep file")),
4273 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
4274 N_("ignore this pack")),
4275 OPT_INTEGER(0, "compression", &pack_compression_level,
4276 N_("pack compression level")),
4277 OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents,
4278 N_("do not hide commits by grafts")),
4279 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
4280 N_("use a bitmap index if available to speed up counting objects")),
4281 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index,
4282 N_("write a bitmap index together with the pack index"),
4283 WRITE_BITMAP_TRUE),
4284 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4285 &write_bitmap_index,
4286 N_("write a bitmap index if possible"),
4287 WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN),
4288 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
4289 OPT_CALLBACK_F(0, "missing", NULL, N_("action"),
4290 N_("handling for missing objects"), PARSE_OPT_NONEG,
4291 option_parse_missing_action),
4292 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
4293 N_("do not pack objects in promisor packfiles")),
4294 OPT_BOOL(0, "delta-islands", &use_delta_islands,
4295 N_("respect islands during delta compression")),
4296 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols,
4297 N_("protocol"),
4298 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
4299 OPT_END(),
4302 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
4303 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4305 disable_replace_refs();
4307 sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
4308 if (the_repository->gitdir) {
4309 prepare_repo_settings(the_repository);
4310 if (sparse < 0)
4311 sparse = the_repository->settings.pack_use_sparse;
4314 reset_pack_idx_option(&pack_idx_opts);
4315 pack_idx_opts.flags |= WRITE_REV;
4316 git_config(git_pack_config, NULL);
4317 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0))
4318 pack_idx_opts.flags &= ~WRITE_REV;
4320 progress = isatty(2);
4321 argc = parse_options(argc, argv, prefix, pack_objects_options,
4322 pack_usage, 0);
4324 if (argc) {
4325 base_name = argv[0];
4326 argc--;
4328 if (pack_to_stdout != !base_name || argc)
4329 usage_with_options(pack_usage, pack_objects_options);
4331 if (depth < 0)
4332 depth = 0;
4333 if (depth >= (1 << OE_DEPTH_BITS)) {
4334 warning(_("delta chain depth %d is too deep, forcing %d"),
4335 depth, (1 << OE_DEPTH_BITS) - 1);
4336 depth = (1 << OE_DEPTH_BITS) - 1;
4338 if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
4339 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4340 (1U << OE_Z_DELTA_BITS) - 1);
4341 cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
4343 if (window < 0)
4344 window = 0;
4346 strvec_push(&rp, "pack-objects");
4347 if (thin) {
4348 use_internal_rev_list = 1;
4349 strvec_push(&rp, shallow
4350 ? "--objects-edge-aggressive"
4351 : "--objects-edge");
4352 } else
4353 strvec_push(&rp, "--objects");
4355 if (rev_list_all) {
4356 use_internal_rev_list = 1;
4357 strvec_push(&rp, "--all");
4359 if (rev_list_reflog) {
4360 use_internal_rev_list = 1;
4361 strvec_push(&rp, "--reflog");
4363 if (rev_list_index) {
4364 use_internal_rev_list = 1;
4365 strvec_push(&rp, "--indexed-objects");
4367 if (rev_list_unpacked && !stdin_packs) {
4368 use_internal_rev_list = 1;
4369 strvec_push(&rp, "--unpacked");
4372 if (exclude_promisor_objects) {
4373 use_internal_rev_list = 1;
4374 fetch_if_missing = 0;
4375 strvec_push(&rp, "--exclude-promisor-objects");
4377 if (unpack_unreachable || keep_unreachable || pack_loose_unreachable)
4378 use_internal_rev_list = 1;
4380 if (!reuse_object)
4381 reuse_delta = 0;
4382 if (pack_compression_level == -1)
4383 pack_compression_level = Z_DEFAULT_COMPRESSION;
4384 else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
4385 die(_("bad pack compression level %d"), pack_compression_level);
4387 if (!delta_search_threads) /* --threads=0 means autodetect */
4388 delta_search_threads = online_cpus();
4390 if (!HAVE_THREADS && delta_search_threads != 1)
4391 warning(_("no threads support, ignoring --threads"));
4392 if (!pack_to_stdout && !pack_size_limit)
4393 pack_size_limit = pack_size_limit_cfg;
4394 if (pack_to_stdout && pack_size_limit)
4395 die(_("--max-pack-size cannot be used to build a pack for transfer"));
4396 if (pack_size_limit && pack_size_limit < 1024*1024) {
4397 warning(_("minimum pack size limit is 1 MiB"));
4398 pack_size_limit = 1024*1024;
4401 if (!pack_to_stdout && thin)
4402 die(_("--thin cannot be used to build an indexable pack"));
4404 if (keep_unreachable && unpack_unreachable)
4405 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
4406 if (!rev_list_all || !rev_list_reflog || !rev_list_index)
4407 unpack_unreachable_expiration = 0;
4409 if (stdin_packs && filter_options.choice)
4410 die(_("cannot use --filter with --stdin-packs"));
4412 if (stdin_packs && use_internal_rev_list)
4413 die(_("cannot use internal rev list with --stdin-packs"));
4415 if (cruft) {
4416 if (use_internal_rev_list)
4417 die(_("cannot use internal rev list with --cruft"));
4418 if (stdin_packs)
4419 die(_("cannot use --stdin-packs with --cruft"));
4423 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
4425 * - to produce good pack (with bitmap index not-yet-packed objects are
4426 * packed in suboptimal order).
4428 * - to use more robust pack-generation codepath (avoiding possible
4429 * bugs in bitmap code and possible bitmap index corruption).
4431 if (!pack_to_stdout)
4432 use_bitmap_index_default = 0;
4434 if (use_bitmap_index < 0)
4435 use_bitmap_index = use_bitmap_index_default;
4437 /* "hard" reasons not to use bitmaps; these just won't work at all */
4438 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow(the_repository))
4439 use_bitmap_index = 0;
4441 if (pack_to_stdout || !rev_list_all)
4442 write_bitmap_index = 0;
4444 if (use_delta_islands)
4445 strvec_push(&rp, "--topo-order");
4447 if (progress && all_progress_implied)
4448 progress = 2;
4450 add_extra_kept_packs(&keep_pack_list);
4451 if (ignore_packed_keep_on_disk) {
4452 struct packed_git *p;
4453 for (p = get_all_packs(the_repository); p; p = p->next)
4454 if (p->pack_local && p->pack_keep)
4455 break;
4456 if (!p) /* no keep-able packs found */
4457 ignore_packed_keep_on_disk = 0;
4459 if (local) {
4461 * unlike ignore_packed_keep_on_disk above, we do not
4462 * want to unset "local" based on looking at packs, as
4463 * it also covers non-local objects
4465 struct packed_git *p;
4466 for (p = get_all_packs(the_repository); p; p = p->next) {
4467 if (!p->pack_local) {
4468 have_non_local_packs = 1;
4469 break;
4474 trace2_region_enter("pack-objects", "enumerate-objects",
4475 the_repository);
4476 prepare_packing_data(the_repository, &to_pack);
4478 if (progress && !cruft)
4479 progress_state = start_progress(_("Enumerating objects"), 0);
4480 if (stdin_packs) {
4481 /* avoids adding objects in excluded packs */
4482 ignore_packed_keep_in_core = 1;
4483 read_packs_list_from_stdin();
4484 if (rev_list_unpacked)
4485 add_unreachable_loose_objects();
4486 } else if (cruft) {
4487 read_cruft_objects();
4488 } else if (!use_internal_rev_list) {
4489 read_object_list_from_stdin();
4490 } else {
4491 struct rev_info revs;
4493 repo_init_revisions(the_repository, &revs, NULL);
4494 list_objects_filter_copy(&revs.filter, &filter_options);
4495 get_object_list(&revs, rp.nr, rp.v);
4496 release_revisions(&revs);
4498 cleanup_preferred_base();
4499 if (include_tag && nr_result)
4500 for_each_tag_ref(add_ref_tag, NULL);
4501 stop_progress(&progress_state);
4502 trace2_region_leave("pack-objects", "enumerate-objects",
4503 the_repository);
4505 if (non_empty && !nr_result)
4506 goto cleanup;
4507 if (nr_result) {
4508 trace2_region_enter("pack-objects", "prepare-pack",
4509 the_repository);
4510 prepare_pack(window, depth);
4511 trace2_region_leave("pack-objects", "prepare-pack",
4512 the_repository);
4515 trace2_region_enter("pack-objects", "write-pack-file", the_repository);
4516 write_excluded_by_configs();
4517 write_pack_file();
4518 trace2_region_leave("pack-objects", "write-pack-file", the_repository);
4520 if (progress)
4521 fprintf_ln(stderr,
4522 _("Total %"PRIu32" (delta %"PRIu32"),"
4523 " reused %"PRIu32" (delta %"PRIu32"),"
4524 " pack-reused %"PRIu32),
4525 written, written_delta, reused, reused_delta,
4526 reuse_packfile_objects);
4528 cleanup:
4529 list_objects_filter_release(&filter_options);
4530 strvec_clear(&rp);
4532 return 0;