interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / builtin / pack-objects.c

5533 lines · 153598 bytes Raw Blame History
1#define USE_THE_REPOSITORY_VARIABLE
2#define DISABLE_SIGN_COMPARE_WARNINGS
3
4#include "builtin.h"
5#include "environment.h"
6#include "gettext.h"
7#include "hex.h"
8#include "config.h"
9#include "attr.h"
10#include "object.h"
11#include "commit.h"
12#include "tag.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-options.h"
22#include "pack-objects.h"
23#include "progress.h"
24#include "refs.h"
25#include "thread-utils.h"
26#include "pack-bitmap.h"
27#include "delta-islands.h"
28#include "reachable.h"
29#include "oid-array.h"
30#include "strvec.h"
31#include "strmap.h"
32#include "list.h"
33#include "packfile.h"
34#include "object-file.h"
35#include "object-file-convert.h"
36#include "odb.h"
37#include "odb/streaming.h"
38#include "replace-object.h"
39#include "dir.h"
40#include "midx.h"
41#include "trace2.h"
42#include "shallow.h"
43#include "promisor-remote.h"
44#include "pack-mtimes.h"
45#include "parse-options.h"
46#include "pkt-line.h"
47#include "blob.h"
48#include "tree.h"
49#include "path-walk.h"
50
51/*
52 * Objects we are going to pack are collected in the `to_pack` structure.
53 * It contains an array (dynamically expanded) of the object data, and a map
54 * that can resolve SHA1s to their position in the array.
55 */
56static struct packing_data to_pack;
57
58static inline struct object_entry *oe_delta(
59 const struct packing_data *pack,
60 const struct object_entry *e)
61{
62 if (!e->delta_idx)
63 return NULL;
64 if (e->ext_base)
65 return &pack->ext_bases[e->delta_idx - 1];
66 else
67 return &pack->objects[e->delta_idx - 1];
68}
69
70static inline size_t oe_delta_size(struct packing_data *pack,
71 const struct object_entry *e)
72{
73 if (e->delta_size_valid)
74 return e->delta_size_;
75
76 /*
77 * pack->delta_size[] can't be NULL because oe_set_delta_size()
78 * must have been called when a new delta is saved with
79 * oe_set_delta().
80 * If oe_delta() returns NULL (i.e. default state, which means
81 * delta_size_valid is also false), then the caller must never
82 * call oe_delta_size().
83 */
84 return pack->delta_size[e - pack->objects];
85}
86
87size_t oe_get_size_slow(struct packing_data *pack,
88 const struct object_entry *e);
89
90static inline size_t oe_size(struct packing_data *pack,
91 const struct object_entry *e)
92{
93 if (e->size_valid)
94 return e->size_;
95
96 return oe_get_size_slow(pack, e);
97}
98
99static inline void oe_set_delta(struct packing_data *pack,
100 struct object_entry *e,
101 struct object_entry *delta)
102{
103 if (delta)
104 e->delta_idx = (delta - pack->objects) + 1;
105 else
106 e->delta_idx = 0;
107}
108
109static inline struct object_entry *oe_delta_sibling(
110 const struct packing_data *pack,
111 const struct object_entry *e)
112{
113 if (e->delta_sibling_idx)
114 return &pack->objects[e->delta_sibling_idx - 1];
115 return NULL;
116}
117
118static inline struct object_entry *oe_delta_child(
119 const struct packing_data *pack,
120 const struct object_entry *e)
121{
122 if (e->delta_child_idx)
123 return &pack->objects[e->delta_child_idx - 1];
124 return NULL;
125}
126
127static inline void oe_set_delta_child(struct packing_data *pack,
128 struct object_entry *e,
129 struct object_entry *delta)
130{
131 if (delta)
132 e->delta_child_idx = (delta - pack->objects) + 1;
133 else
134 e->delta_child_idx = 0;
135}
136
137static inline void oe_set_delta_sibling(struct packing_data *pack,
138 struct object_entry *e,
139 struct object_entry *delta)
140{
141 if (delta)
142 e->delta_sibling_idx = (delta - pack->objects) + 1;
143 else
144 e->delta_sibling_idx = 0;
145}
146
147static inline void oe_set_size(struct packing_data *pack,
148 struct object_entry *e,
149 size_t size)
150{
151 if (size < pack->oe_size_limit) {
152 e->size_ = size;
153 e->size_valid = 1;
154 } else {
155 e->size_valid = 0;
156 if (oe_get_size_slow(pack, e) != size)
157 BUG("'size' is supposed to be the object size!");
158 }
159}
160
161static inline void oe_set_delta_size(struct packing_data *pack,
162 struct object_entry *e,
163 size_t size)
164{
165 if (size < pack->oe_delta_size_limit) {
166 e->delta_size_ = size;
167 e->delta_size_valid = 1;
168 } else {
169 packing_data_lock(pack);
170 if (!pack->delta_size)
171 ALLOC_ARRAY(pack->delta_size, pack->nr_alloc);
172 packing_data_unlock(pack);
173
174 pack->delta_size[e - pack->objects] = size;
175 e->delta_size_valid = 0;
176 }
177}
178
179#define IN_PACK(obj) oe_in_pack(&to_pack, obj)
180#define SIZE(obj) oe_size(&to_pack, obj)
181#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
182#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
183#define DELTA(obj) oe_delta(&to_pack, obj)
184#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
185#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
186#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
187#define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
188#define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
189#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
190#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
191
192static const char *const pack_usage[] = {
193 N_("git pack-objects [-q | --progress | --all-progress] [--all-progress-implied]\n"
194 " [--no-reuse-delta] [--delta-base-offset] [--non-empty]\n"
195 " [--local] [--incremental] [--window=<n>] [--depth=<n>]\n"
196 " [--revs [--unpacked | --all]] [--keep-pack=<pack-name>]\n"
197 " [--cruft] [--cruft-expiration=<time>]\n"
198 " [--stdout [--filter=<filter-spec>] | <base-name>]\n"
199 " [--shallow] [--keep-true-parents] [--[no-]sparse]\n"
200 " [--name-hash-version=<n>] [--path-walk] < <object-list>"),
201 NULL
202};
203
204static struct pack_idx_entry **written_list;
205static uint32_t nr_result, nr_written, nr_seen;
206static struct bitmap_index *bitmap_git;
207static uint32_t write_layer;
208
209static int non_empty;
210static int reuse_delta = 1, reuse_object = 1;
211static int keep_unreachable, unpack_unreachable, include_tag;
212static timestamp_t unpack_unreachable_expiration;
213static int pack_loose_unreachable;
214static int cruft;
215static int shallow = 0;
216static timestamp_t cruft_expiration;
217static int local;
218static int have_non_local_packs;
219static int incremental;
220static int ignore_packed_keep_on_disk;
221static int ignore_packed_keep_in_core;
222static int ignore_packed_keep_in_core_open;
223static int ignore_packed_keep_in_core_has_cruft;
224static int allow_ofs_delta;
225static struct pack_idx_option pack_idx_opts;
226static const char *base_name;
227static int progress = 1;
228static int window = 10;
229static unsigned long pack_size_limit;
230static int depth = 50;
231static int delta_search_threads;
232static int pack_to_stdout;
233static int sparse;
234static int thin;
235static int path_walk = -1;
236static int num_preferred_base;
237static struct progress *progress_state;
238
239static struct bitmapped_pack *reuse_packfiles;
240static size_t reuse_packfiles_nr;
241static size_t reuse_packfiles_used_nr;
242static uint32_t reuse_packfile_objects;
243static struct bitmap *reuse_packfile_bitmap;
244
245static int use_bitmap_index_default = 1;
246static int use_bitmap_index = -1;
247static enum {
248 NO_PACK_REUSE = 0,
249 SINGLE_PACK_REUSE,
250 MULTI_PACK_REUSE,
251} allow_pack_reuse = SINGLE_PACK_REUSE;
252static enum {
253 WRITE_BITMAP_FALSE = 0,
254 WRITE_BITMAP_QUIET,
255 WRITE_BITMAP_TRUE,
256} write_bitmap_index;
257static uint16_t write_bitmap_options = BITMAP_OPT_HASH_CACHE;
258
259static int exclude_promisor_objects;
260static int exclude_promisor_objects_best_effort;
261
262static int use_delta_islands;
263
264static size_t delta_cache_size = 0;
265static size_t max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
266static unsigned long cache_max_small_delta_size = 1000;
267
268static unsigned long window_memory_limit = 0;
269
270static struct string_list uri_protocols = STRING_LIST_INIT_NODUP;
271
272enum missing_action {
273 MA_ERROR = 0, /* fail if any missing objects are encountered */
274 MA_ALLOW_ANY, /* silently allow ALL missing objects */
275 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
276};
277static enum missing_action arg_missing_action;
278static show_object_fn fn_show_object;
279
280struct configured_exclusion {
281 struct oidmap_entry e;
282 char *pack_hash_hex;
283 char *uri;
284};
285static struct oidmap configured_exclusions;
286
287static struct oidset excluded_by_config;
288static int name_hash_version = -1;
289
290enum stdin_packs_mode {
291 STDIN_PACKS_MODE_NONE,
292 STDIN_PACKS_MODE_STANDARD,
293 STDIN_PACKS_MODE_FOLLOW,
294};
295
296/**
297 * Check whether the name_hash_version chosen by user input is appropriate,
298 * and also validate whether it is compatible with other features.
299 */
300static void validate_name_hash_version(void)
301{
302 if (name_hash_version < 1 || name_hash_version > 2)
303 die(_("invalid --name-hash-version option: %d"), name_hash_version);
304 if (write_bitmap_index && name_hash_version != 1) {
305 warning(_("currently, --write-bitmap-index requires --name-hash-version=1"));
306 name_hash_version = 1;
307 }
308}
309
310static inline uint32_t pack_name_hash_fn(const char *name)
311{
312 static int seen_version = -1;
313
314 if (seen_version < 0)
315 seen_version = name_hash_version;
316 else if (seen_version != name_hash_version)
317 BUG("name hash version changed from %d to %d mid-process",
318 seen_version, name_hash_version);
319
320 switch (name_hash_version) {
321 case 1:
322 return pack_name_hash(name);
323
324 case 2:
325 return pack_name_hash_v2((const unsigned char *)name);
326
327 default:
328 BUG("invalid name-hash version: %d", name_hash_version);
329 }
330}
331
332/*
333 * stats
334 */
335static uint32_t written, written_delta;
336static uint32_t reused, reused_delta;
337
338/*
339 * Indexed commits
340 */
341static struct commit **indexed_commits;
342static unsigned int indexed_commits_nr;
343static unsigned int indexed_commits_alloc;
344
345static void index_commit_for_bitmap(struct commit *commit)
346{
347 if (indexed_commits_nr >= indexed_commits_alloc) {
348 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
349 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
350 }
351
352 indexed_commits[indexed_commits_nr++] = commit;
353}
354
355static void *get_delta(struct object_entry *entry)
356{
357 unsigned long size, base_size;
358 size_t delta_size;
359 void *buf, *base_buf, *delta_buf;
360 enum object_type type;
361 size_t size_st = 0, base_size_st = 0;
362
363 buf = odb_read_object(the_repository->objects, &entry->idx.oid,
364 &type, &size_st);
365 size = cast_size_t_to_ulong(size_st);
366 if (!buf)
367 die(_("unable to read %s"), oid_to_hex(&entry->idx.oid));
368 base_buf = odb_read_object(the_repository->objects,
369 &DELTA(entry)->idx.oid, &type,
370 &base_size_st);
371 base_size = cast_size_t_to_ulong(base_size_st);
372 if (!base_buf)
373 die("unable to read %s",
374 oid_to_hex(&DELTA(entry)->idx.oid));
375 delta_buf = diff_delta(base_buf, base_size,
376 buf, size, &delta_size, 0);
377 /*
378 * We successfully computed this delta once but dropped it for
379 * memory reasons. Something is very wrong if this time we
380 * recompute and create a different delta.
381 */
382 if (!delta_buf || delta_size != DELTA_SIZE(entry))
383 BUG("delta size changed");
384 free(buf);
385 free(base_buf);
386 return delta_buf;
387}
388
389static unsigned long do_compress(void **pptr, unsigned long size)
390{
391 git_zstream stream;
392 void *in, *out;
393 unsigned long maxsize;
394 struct repo_config_values *cfg = repo_config_values(the_repository);
395
396 git_deflate_init(&stream, cfg->pack_compression_level);
397 maxsize = git_deflate_bound(&stream, size);
398
399 in = *pptr;
400 out = xmalloc(maxsize);
401 *pptr = out;
402
403 stream.next_in = in;
404 stream.avail_in = size;
405 stream.next_out = out;
406 stream.avail_out = maxsize;
407 while (git_deflate(&stream, Z_FINISH) == Z_OK)
408 ; /* nothing */
409 git_deflate_end(&stream);
410
411 free(in);
412 return stream.total_out;
413}
414
415static unsigned long write_large_blob_data(struct odb_stream *st, struct hashfile *f,
416 const struct object_id *oid)
417{
418 git_zstream stream;
419 unsigned char ibuf[1024 * 16];
420 unsigned char obuf[1024 * 16];
421 unsigned long olen = 0;
422 struct repo_config_values *cfg = repo_config_values(the_repository);
423
424 git_deflate_init(&stream, cfg->pack_compression_level);
425
426 for (;;) {
427 ssize_t readlen;
428 int zret = Z_OK;
429 readlen = odb_stream_read(st, ibuf, sizeof(ibuf));
430 if (readlen == -1)
431 die(_("unable to read %s"), oid_to_hex(oid));
432
433 stream.next_in = ibuf;
434 stream.avail_in = readlen;
435 while ((stream.avail_in || readlen == 0) &&
436 (zret == Z_OK || zret == Z_BUF_ERROR)) {
437 stream.next_out = obuf;
438 stream.avail_out = sizeof(obuf);
439 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
440 hashwrite(f, obuf, stream.next_out - obuf);
441 olen += stream.next_out - obuf;
442 }
443 if (stream.avail_in)
444 die(_("deflate error (%d)"), zret);
445 if (readlen == 0) {
446 if (zret != Z_STREAM_END)
447 die(_("deflate error (%d)"), zret);
448 break;
449 }
450 }
451 git_deflate_end(&stream);
452 return olen;
453}
454
455/*
456 * we are going to reuse the existing object data as is. make
457 * sure it is not corrupt.
458 */
459static int check_pack_inflate(struct packed_git *p,
460 struct pack_window **w_curs,
461 off_t offset,
462 off_t len,
463 size_t expect)
464{
465 git_zstream stream;
466 unsigned char fakebuf[4096], *in;
467 int st;
468
469 memset(&stream, 0, sizeof(stream));
470 git_inflate_init(&stream);
471 do {
472 in = use_pack(p, w_curs, offset, &stream.avail_in);
473 stream.next_in = in;
474 stream.next_out = fakebuf;
475 stream.avail_out = sizeof(fakebuf);
476 st = git_inflate(&stream, Z_FINISH);
477 offset += stream.next_in - in;
478 } while (st == Z_OK || st == Z_BUF_ERROR);
479 git_inflate_end(&stream);
480 return (st == Z_STREAM_END &&
481 stream.total_out == expect &&
482 stream.total_in == len) ? 0 : -1;
483}
484
485static void copy_pack_data(struct hashfile *f,
486 struct packed_git *p,
487 struct pack_window **w_curs,
488 off_t offset,
489 off_t len)
490{
491 unsigned char *in;
492 size_t avail;
493
494 while (len) {
495 in = use_pack(p, w_curs, offset, &avail);
496 if (avail > len)
497 avail = (unsigned long)len;
498 hashwrite(f, in, avail);
499 offset += avail;
500 len -= avail;
501 }
502}
503
504static inline int oe_size_greater_than(struct packing_data *pack,
505 const struct object_entry *lhs,
506 size_t rhs)
507{
508 if (lhs->size_valid)
509 return lhs->size_ > rhs;
510 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
511 return 1;
512 return oe_get_size_slow(pack, lhs) > rhs;
513}
514
515/* Return 0 if we will bust the pack-size limit */
516static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
517 unsigned long limit, int usable_delta)
518{
519 unsigned long size, datalen;
520 unsigned char header[MAX_PACK_OBJECT_HEADER],
521 dheader[MAX_PACK_OBJECT_HEADER];
522 unsigned hdrlen;
523 enum object_type type;
524 void *buf;
525 struct odb_stream *st = NULL;
526 const unsigned hashsz = the_hash_algo->rawsz;
527
528 if (!usable_delta) {
529 if (oe_type(entry) == OBJ_BLOB &&
530 oe_size_greater_than(&to_pack, entry,
531 repo_settings_get_big_file_threshold(the_repository)) &&
532 (st = odb_stream_from_object(the_repository->objects, &entry->idx.oid,
533 NULL)) != NULL) {
534 buf = NULL;
535 type = st->type;
536 size = st->size;
537 } else {
538 size_t size_st = 0;
539 buf = odb_read_object(the_repository->objects,
540 &entry->idx.oid, &type,
541 &size_st);
542 size = cast_size_t_to_ulong(size_st);
543 if (!buf)
544 die(_("unable to read %s"),
545 oid_to_hex(&entry->idx.oid));
546 }
547 /*
548 * make sure no cached delta data remains from a
549 * previous attempt before a pack split occurred.
550 */
551 FREE_AND_NULL(entry->delta_data);
552 entry->z_delta_size = 0;
553 } else if (entry->delta_data) {
554 size = DELTA_SIZE(entry);
555 buf = entry->delta_data;
556 entry->delta_data = NULL;
557 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
558 OBJ_OFS_DELTA : OBJ_REF_DELTA;
559 } else {
560 buf = get_delta(entry);
561 size = DELTA_SIZE(entry);
562 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
563 OBJ_OFS_DELTA : OBJ_REF_DELTA;
564 }
565
566 if (st) /* large blob case, just assume we don't compress well */
567 datalen = size;
568 else if (entry->z_delta_size)
569 datalen = entry->z_delta_size;
570 else
571 datalen = do_compress(&buf, size);
572
573 /*
574 * The object header is a byte of 'type' followed by zero or
575 * more bytes of length.
576 */
577 hdrlen = encode_in_pack_object_header(header, sizeof(header),
578 type, size);
579
580 if (type == OBJ_OFS_DELTA) {
581 /*
582 * Deltas with relative base contain an additional
583 * encoding of the relative offset for the delta
584 * base from this object's position in the pack.
585 */
586 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
587 unsigned pos = sizeof(dheader) - 1;
588 dheader[pos] = ofs & 127;
589 while (ofs >>= 7)
590 dheader[--pos] = 128 | (--ofs & 127);
591 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
592 if (st)
593 odb_stream_close(st);
594 free(buf);
595 return 0;
596 }
597 hashwrite(f, header, hdrlen);
598 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
599 hdrlen += sizeof(dheader) - pos;
600 } else if (type == OBJ_REF_DELTA) {
601 /*
602 * Deltas with a base reference contain
603 * additional bytes for the base object ID.
604 */
605 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
606 if (st)
607 odb_stream_close(st);
608 free(buf);
609 return 0;
610 }
611 hashwrite(f, header, hdrlen);
612 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
613 hdrlen += hashsz;
614 } else {
615 if (limit && hdrlen + datalen + hashsz >= limit) {
616 if (st)
617 odb_stream_close(st);
618 free(buf);
619 return 0;
620 }
621 hashwrite(f, header, hdrlen);
622 }
623 if (st) {
624 datalen = write_large_blob_data(st, f, &entry->idx.oid);
625 odb_stream_close(st);
626 } else {
627 hashwrite(f, buf, datalen);
628 free(buf);
629 }
630
631 return hdrlen + datalen;
632}
633
634/* Return 0 if we will bust the pack-size limit */
635static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
636 unsigned long limit, int usable_delta)
637{
638 struct packed_git *p = IN_PACK(entry);
639 struct pack_window *w_curs = NULL;
640 uint32_t pos;
641 off_t offset, cur;
642 enum object_type type = oe_type(entry);
643 enum object_type in_pack_type;
644 off_t datalen;
645 unsigned char header[MAX_PACK_OBJECT_HEADER],
646 dheader[MAX_PACK_OBJECT_HEADER];
647 unsigned hdrlen;
648 const unsigned hashsz = the_hash_algo->rawsz;
649 size_t entry_size;
650
651 cur = entry->in_pack_offset;
652 in_pack_type = unpack_object_header(p, &w_curs, &cur, &entry_size);
653 if (in_pack_type < 0)
654 die(_("write_reuse_object: unable to parse object header of %s"),
655 oid_to_hex(&entry->idx.oid));
656
657 if (DELTA(entry))
658 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
659 OBJ_OFS_DELTA : OBJ_REF_DELTA;
660 hdrlen = encode_in_pack_object_header(header, sizeof(header),
661 type, entry_size);
662
663 offset = entry->in_pack_offset;
664 if (offset_to_pack_pos(p, offset, &pos) < 0)
665 die(_("write_reuse_object: could not locate %s, expected at "
666 "offset %"PRIuMAX" in pack %s"),
667 oid_to_hex(&entry->idx.oid), (uintmax_t)offset,
668 p->pack_name);
669 datalen = pack_pos_to_offset(p, pos + 1) - offset;
670 if (!pack_to_stdout && p->index_version > 1 &&
671 check_pack_crc(p, &w_curs, offset, datalen,
672 pack_pos_to_index(p, pos))) {
673 error(_("bad packed object CRC for %s"),
674 oid_to_hex(&entry->idx.oid));
675 unuse_pack(&w_curs);
676 return write_no_reuse_object(f, entry, limit, usable_delta);
677 }
678
679 offset += entry->in_pack_header_size;
680 datalen -= entry->in_pack_header_size;
681
682 if (!pack_to_stdout && p->index_version == 1 &&
683 check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) {
684 error(_("corrupt packed object for %s"),
685 oid_to_hex(&entry->idx.oid));
686 unuse_pack(&w_curs);
687 return write_no_reuse_object(f, entry, limit, usable_delta);
688 }
689
690 if (type == OBJ_OFS_DELTA) {
691 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
692 unsigned pos = sizeof(dheader) - 1;
693 dheader[pos] = ofs & 127;
694 while (ofs >>= 7)
695 dheader[--pos] = 128 | (--ofs & 127);
696 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
697 unuse_pack(&w_curs);
698 return 0;
699 }
700 hashwrite(f, header, hdrlen);
701 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
702 hdrlen += sizeof(dheader) - pos;
703 reused_delta++;
704 } else if (type == OBJ_REF_DELTA) {
705 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
706 unuse_pack(&w_curs);
707 return 0;
708 }
709 hashwrite(f, header, hdrlen);
710 hashwrite(f, DELTA(entry)->idx.oid.hash, hashsz);
711 hdrlen += hashsz;
712 reused_delta++;
713 } else {
714 if (limit && hdrlen + datalen + hashsz >= limit) {
715 unuse_pack(&w_curs);
716 return 0;
717 }
718 hashwrite(f, header, hdrlen);
719 }
720 copy_pack_data(f, p, &w_curs, offset, datalen);
721 unuse_pack(&w_curs);
722 reused++;
723 return hdrlen + datalen;
724}
725
726/* Return 0 if we will bust the pack-size limit */
727static off_t write_object(struct hashfile *f,
728 struct object_entry *entry,
729 off_t write_offset)
730{
731 unsigned long limit;
732 off_t len;
733 int usable_delta, to_reuse;
734
735 if (!pack_to_stdout)
736 crc32_begin(f);
737
738 /* apply size limit if limited packsize and not first object */
739 if (!pack_size_limit || !nr_written)
740 limit = 0;
741 else if (pack_size_limit <= write_offset)
742 /*
743 * the earlier object did not fit the limit; avoid
744 * mistaking this with unlimited (i.e. limit = 0).
745 */
746 limit = 1;
747 else
748 limit = pack_size_limit - write_offset;
749
750 if (!DELTA(entry))
751 usable_delta = 0; /* no delta */
752 else if (!pack_size_limit)
753 usable_delta = 1; /* unlimited packfile */
754 else if (DELTA(entry)->idx.offset == (off_t)-1)
755 usable_delta = 0; /* base was written to another pack */
756 else if (DELTA(entry)->idx.offset)
757 usable_delta = 1; /* base already exists in this pack */
758 else
759 usable_delta = 0; /* base could end up in another pack */
760
761 if (!reuse_object)
762 to_reuse = 0; /* explicit */
763 else if (!IN_PACK(entry))
764 to_reuse = 0; /* can't reuse what we don't have */
765 else if (oe_type(entry) == OBJ_REF_DELTA ||
766 oe_type(entry) == OBJ_OFS_DELTA)
767 /* check_object() decided it for us ... */
768 to_reuse = usable_delta;
769 /* ... but pack split may override that */
770 else if (oe_type(entry) != entry->in_pack_type)
771 to_reuse = 0; /* pack has delta which is unusable */
772 else if (DELTA(entry))
773 to_reuse = 0; /* we want to pack afresh */
774 else
775 to_reuse = 1; /* we have it in-pack undeltified,
776 * and we do not need to deltify it.
777 */
778
779 if (!to_reuse)
780 len = write_no_reuse_object(f, entry, limit, usable_delta);
781 else
782 len = write_reuse_object(f, entry, limit, usable_delta);
783 if (!len)
784 return 0;
785
786 if (usable_delta)
787 written_delta++;
788 written++;
789 if (!pack_to_stdout)
790 entry->idx.crc32 = crc32_end(f);
791 return len;
792}
793
794enum write_one_status {
795 WRITE_ONE_SKIP = -1, /* already written */
796 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
797 WRITE_ONE_WRITTEN = 1, /* normal */
798 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
799};
800
801static enum write_one_status write_one(struct hashfile *f,
802 struct object_entry *e,
803 off_t *offset)
804{
805 off_t size;
806 int recursing;
807
808 /*
809 * we set offset to 1 (which is an impossible value) to mark
810 * the fact that this object is involved in "write its base
811 * first before writing a deltified object" recursion.
812 */
813 recursing = (e->idx.offset == 1);
814 if (recursing) {
815 warning(_("recursive delta detected for object %s"),
816 oid_to_hex(&e->idx.oid));
817 return WRITE_ONE_RECURSIVE;
818 } else if (e->idx.offset || e->preferred_base) {
819 /* offset is non zero if object is written already. */
820 return WRITE_ONE_SKIP;
821 }
822
823 /* if we are deltified, write out base object first. */
824 if (DELTA(e)) {
825 e->idx.offset = 1; /* now recurse */
826 switch (write_one(f, DELTA(e), offset)) {
827 case WRITE_ONE_RECURSIVE:
828 /* we cannot depend on this one */
829 SET_DELTA(e, NULL);
830 break;
831 default:
832 break;
833 case WRITE_ONE_BREAK:
834 e->idx.offset = recursing;
835 return WRITE_ONE_BREAK;
836 }
837 }
838
839 e->idx.offset = *offset;
840 size = write_object(f, e, *offset);
841 if (!size) {
842 e->idx.offset = recursing;
843 return WRITE_ONE_BREAK;
844 }
845 written_list[nr_written++] = &e->idx;
846
847 /* make sure off_t is sufficiently large not to wrap */
848 if (signed_add_overflows(*offset, size))
849 die(_("pack too large for current definition of off_t"));
850 *offset += size;
851 return WRITE_ONE_WRITTEN;
852}
853
854static int mark_tagged(const struct reference *ref, void *cb_data UNUSED)
855{
856 struct object_id peeled;
857 struct object_entry *entry = packlist_find(&to_pack, ref->oid);
858
859 if (entry)
860 entry->tagged = 1;
861 if (!reference_get_peeled_oid(the_repository, ref, &peeled)) {
862 entry = packlist_find(&to_pack, &peeled);
863 if (entry)
864 entry->tagged = 1;
865 }
866 return 0;
867}
868
869static inline unsigned char oe_layer(struct packing_data *pack,
870 struct object_entry *e)
871{
872 if (!pack->layer)
873 return 0;
874 return pack->layer[e - pack->objects];
875}
876
877static inline void add_to_write_order(struct object_entry **wo,
878 unsigned int *endp,
879 struct object_entry *e)
880{
881 if (e->filled || oe_layer(&to_pack, e) != write_layer)
882 return;
883 wo[(*endp)++] = e;
884 e->filled = 1;
885}
886
887static void add_descendants_to_write_order(struct object_entry **wo,
888 unsigned int *endp,
889 struct object_entry *e)
890{
891 int add_to_order = 1;
892 while (e) {
893 if (add_to_order) {
894 struct object_entry *s;
895 /* add this node... */
896 add_to_write_order(wo, endp, e);
897 /* all its siblings... */
898 for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
899 add_to_write_order(wo, endp, s);
900 }
901 }
902 /* drop down a level to add left subtree nodes if possible */
903 if (DELTA_CHILD(e)) {
904 add_to_order = 1;
905 e = DELTA_CHILD(e);
906 } else {
907 add_to_order = 0;
908 /* our sibling might have some children, it is next */
909 if (DELTA_SIBLING(e)) {
910 e = DELTA_SIBLING(e);
911 continue;
912 }
913 /* go back to our parent node */
914 e = DELTA(e);
915 while (e && !DELTA_SIBLING(e)) {
916 /* we're on the right side of a subtree, keep
917 * going up until we can go right again */
918 e = DELTA(e);
919 }
920 if (!e) {
921 /* done- we hit our original root node */
922 return;
923 }
924 /* pass it off to sibling at this level */
925 e = DELTA_SIBLING(e);
926 }
927 };
928}
929
930static void add_family_to_write_order(struct object_entry **wo,
931 unsigned int *endp,
932 struct object_entry *e)
933{
934 struct object_entry *root;
935
936 for (root = e; DELTA(root); root = DELTA(root))
937 ; /* nothing */
938 add_descendants_to_write_order(wo, endp, root);
939}
940
941static void compute_layer_order(struct object_entry **wo, unsigned int *wo_end)
942{
943 unsigned int i, last_untagged;
944 struct object_entry *objects = to_pack.objects;
945
946 for (i = 0; i < to_pack.nr_objects; i++) {
947 if (objects[i].tagged)
948 break;
949 add_to_write_order(wo, wo_end, &objects[i]);
950 }
951 last_untagged = i;
952
953 /*
954 * Then fill all the tagged tips.
955 */
956 for (; i < to_pack.nr_objects; i++) {
957 if (objects[i].tagged)
958 add_to_write_order(wo, wo_end, &objects[i]);
959 }
960
961 /*
962 * And then all remaining commits and tags.
963 */
964 for (i = last_untagged; i < to_pack.nr_objects; i++) {
965 if (oe_type(&objects[i]) != OBJ_COMMIT &&
966 oe_type(&objects[i]) != OBJ_TAG)
967 continue;
968 add_to_write_order(wo, wo_end, &objects[i]);
969 }
970
971 /*
972 * And then all the trees.
973 */
974 for (i = last_untagged; i < to_pack.nr_objects; i++) {
975 if (oe_type(&objects[i]) != OBJ_TREE)
976 continue;
977 add_to_write_order(wo, wo_end, &objects[i]);
978 }
979
980 /*
981 * Finally all the rest in really tight order
982 */
983 for (i = last_untagged; i < to_pack.nr_objects; i++) {
984 if (!objects[i].filled && oe_layer(&to_pack, &objects[i]) == write_layer)
985 add_family_to_write_order(wo, wo_end, &objects[i]);
986 }
987}
988
989static struct object_entry **compute_write_order(void)
990{
991 uint32_t max_layers = 1;
992 unsigned int i, wo_end;
993
994 struct object_entry **wo;
995 struct object_entry *objects = to_pack.objects;
996
997 for (i = 0; i < to_pack.nr_objects; i++) {
998 objects[i].tagged = 0;
999 objects[i].filled = 0;
1000 SET_DELTA_CHILD(&objects[i], NULL);
1001 SET_DELTA_SIBLING(&objects[i], NULL);
1002 }
1003
1004 /*
1005 * Fully connect delta_child/delta_sibling network.
1006 * Make sure delta_sibling is sorted in the original
1007 * recency order.
1008 */
1009 for (i = to_pack.nr_objects; i > 0;) {
1010 struct object_entry *e = &objects[--i];
1011 if (!DELTA(e))
1012 continue;
1013 /* Mark me as the first child */
1014 e->delta_sibling_idx = DELTA(e)->delta_child_idx;
1015 SET_DELTA_CHILD(DELTA(e), e);
1016 }
1017
1018 /*
1019 * Mark objects that are at the tip of tags.
1020 */
1021 refs_for_each_tag_ref(get_main_ref_store(the_repository), mark_tagged,
1022 NULL);
1023
1024 if (use_delta_islands) {
1025 max_layers = compute_pack_layers(&to_pack);
1026 free_island_marks();
1027 }
1028
1029 ALLOC_ARRAY(wo, to_pack.nr_objects);
1030 wo_end = 0;
1031
1032 for (; write_layer < max_layers; ++write_layer)
1033 compute_layer_order(wo, &wo_end);
1034
1035 if (wo_end != to_pack.nr_objects)
1036 die(_("ordered %u objects, expected %"PRIu32),
1037 wo_end, to_pack.nr_objects);
1038
1039 return wo;
1040}
1041
1042
1043/*
1044 * A reused set of objects. All objects in a chunk have the same
1045 * relative position in the original packfile and the generated
1046 * packfile.
1047 */
1048
1049static struct reused_chunk {
1050 /* The offset of the first object of this chunk in the original
1051 * packfile. */
1052 off_t original;
1053 /* The difference for "original" minus the offset of the first object of
1054 * this chunk in the generated packfile. */
1055 off_t difference;
1056} *reused_chunks;
1057static int reused_chunks_nr;
1058static int reused_chunks_alloc;
1059
1060static void record_reused_object(off_t where, off_t offset)
1061{
1062 if (reused_chunks_nr && reused_chunks[reused_chunks_nr-1].difference == offset)
1063 return;
1064
1065 ALLOC_GROW(reused_chunks, reused_chunks_nr + 1,
1066 reused_chunks_alloc);
1067 reused_chunks[reused_chunks_nr].original = where;
1068 reused_chunks[reused_chunks_nr].difference = offset;
1069 reused_chunks_nr++;
1070}
1071
1072/*
1073 * Binary search to find the chunk that "where" is in. Note
1074 * that we're not looking for an exact match, just the first
1075 * chunk that contains it (which implicitly ends at the start
1076 * of the next chunk.
1077 */
1078static off_t find_reused_offset(off_t where)
1079{
1080 int lo = 0, hi = reused_chunks_nr;
1081 while (lo < hi) {
1082 int mi = lo + ((hi - lo) / 2);
1083 if (where == reused_chunks[mi].original)
1084 return reused_chunks[mi].difference;
1085 if (where < reused_chunks[mi].original)
1086 hi = mi;
1087 else
1088 lo = mi + 1;
1089 }
1090
1091 /*
1092 * The first chunk starts at zero, so we can't have gone below
1093 * there.
1094 */
1095 assert(lo);
1096 return reused_chunks[lo-1].difference;
1097}
1098
1099static void write_reused_pack_one(struct packed_git *reuse_packfile,
1100 size_t pos, struct hashfile *out,
1101 off_t pack_start,
1102 struct pack_window **w_curs)
1103{
1104 off_t offset, next, cur;
1105 enum object_type type;
1106 size_t size;
1107
1108 offset = pack_pos_to_offset(reuse_packfile, pos);
1109 next = pack_pos_to_offset(reuse_packfile, pos + 1);
1110
1111 record_reused_object(offset,
1112 offset - (hashfile_total(out) - pack_start));
1113
1114 cur = offset;
1115 type = unpack_object_header(reuse_packfile, w_curs, &cur, &size);
1116 assert(type >= 0);
1117
1118 if (type == OBJ_OFS_DELTA) {
1119 off_t base_offset;
1120 off_t fixup;
1121
1122 unsigned char header[MAX_PACK_OBJECT_HEADER];
1123 unsigned len;
1124
1125 base_offset = get_delta_base(reuse_packfile, w_curs, &cur, type, offset);
1126 assert(base_offset != 0);
1127
1128 /* Convert to REF_DELTA if we must... */
1129 if (!allow_ofs_delta) {
1130 uint32_t base_pos;
1131 struct object_id base_oid;
1132
1133 if (offset_to_pack_pos(reuse_packfile, base_offset, &base_pos) < 0)
1134 die(_("expected object at offset %"PRIuMAX" "
1135 "in pack %s"),
1136 (uintmax_t)base_offset,
1137 reuse_packfile->pack_name);
1138
1139 nth_packed_object_id(&base_oid, reuse_packfile,
1140 pack_pos_to_index(reuse_packfile, base_pos));
1141
1142 len = encode_in_pack_object_header(header, sizeof(header),
1143 OBJ_REF_DELTA, size);
1144 hashwrite(out, header, len);
1145 hashwrite(out, base_oid.hash, the_hash_algo->rawsz);
1146 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1147 return;
1148 }
1149
1150 /* Otherwise see if we need to rewrite the offset... */
1151 fixup = find_reused_offset(offset) -
1152 find_reused_offset(base_offset);
1153 if (fixup) {
1154 unsigned char ofs_header[MAX_PACK_OBJECT_HEADER];
1155 unsigned i, ofs_len;
1156 off_t ofs = offset - base_offset - fixup;
1157
1158 len = encode_in_pack_object_header(header, sizeof(header),
1159 OBJ_OFS_DELTA, size);
1160
1161 i = sizeof(ofs_header) - 1;
1162 ofs_header[i] = ofs & 127;
1163 while (ofs >>= 7)
1164 ofs_header[--i] = 128 | (--ofs & 127);
1165
1166 ofs_len = sizeof(ofs_header) - i;
1167
1168 hashwrite(out, header, len);
1169 hashwrite(out, ofs_header + sizeof(ofs_header) - ofs_len, ofs_len);
1170 copy_pack_data(out, reuse_packfile, w_curs, cur, next - cur);
1171 return;
1172 }
1173
1174 /* ...otherwise we have no fixup, and can write it verbatim */
1175 }
1176
1177 copy_pack_data(out, reuse_packfile, w_curs, offset, next - offset);
1178}
1179
1180static size_t write_reused_pack_verbatim(struct bitmapped_pack *reuse_packfile,
1181 struct hashfile *out,
1182 struct pack_window **w_curs)
1183{
1184 size_t pos = 0;
1185 size_t end;
1186
1187 if (reuse_packfile->bitmap_pos) {
1188 /*
1189 * We can't reuse whole chunks verbatim out of
1190 * non-preferred packs since we can't guarantee that
1191 * all duplicate objects were resolved in favor of
1192 * that pack.
1193 *
1194 * Even if we have a whole eword_t worth of bits that
1195 * could be reused, there may be objects between the
1196 * objects corresponding to the first and last bit of
1197 * that word which were selected from a different
1198 * pack, causing us to send duplicate or unwanted
1199 * objects.
1200 *
1201 * Handle non-preferred packs from within
1202 * write_reused_pack(), which inspects and reuses
1203 * individual bits.
1204 */
1205 return reuse_packfile->bitmap_pos / BITS_IN_EWORD;
1206 }
1207
1208 /*
1209 * Only read through the last word whose bits all correspond
1210 * to objects in the given packfile, since we must stop at a
1211 * word boundary.
1212 *
1213 * If there is no whole word to read (i.e. the packfile
1214 * contains fewer than BITS_IN_EWORD objects), then we'll
1215 * inspect bits one-by-one in write_reused_pack().
1216 */
1217 end = reuse_packfile->bitmap_nr / BITS_IN_EWORD;
1218 if (reuse_packfile_bitmap->word_alloc < end)
1219 BUG("fewer words than expected in reuse_packfile_bitmap");
1220
1221 while (pos < end && reuse_packfile_bitmap->words[pos] == (eword_t)~0)
1222 pos++;
1223
1224 if (pos) {
1225 off_t to_write;
1226
1227 written = (pos * BITS_IN_EWORD);
1228 to_write = pack_pos_to_offset(reuse_packfile->p, written)
1229 - sizeof(struct pack_header);
1230
1231 /* We're recording one chunk, not one object. */
1232 record_reused_object(sizeof(struct pack_header), 0);
1233 hashflush(out);
1234 copy_pack_data(out, reuse_packfile->p, w_curs,
1235 sizeof(struct pack_header), to_write);
1236
1237 display_progress(progress_state, written);
1238 }
1239 return pos;
1240}
1241
1242static void write_reused_pack(struct bitmapped_pack *reuse_packfile,
1243 struct hashfile *f)
1244{
1245 size_t i = reuse_packfile->bitmap_pos / BITS_IN_EWORD;
1246 uint32_t offset;
1247 off_t pack_start = hashfile_total(f) - sizeof(struct pack_header);
1248 struct pack_window *w_curs = NULL;
1249
1250 if (allow_ofs_delta)
1251 i = write_reused_pack_verbatim(reuse_packfile, f, &w_curs);
1252
1253 for (; i < reuse_packfile_bitmap->word_alloc; ++i) {
1254 eword_t word = reuse_packfile_bitmap->words[i];
1255 size_t pos = (i * BITS_IN_EWORD);
1256
1257 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1258 uint32_t pack_pos;
1259 if ((word >> offset) == 0)
1260 break;
1261
1262 offset += ewah_bit_ctz64(word >> offset);
1263 if (pos + offset < reuse_packfile->bitmap_pos)
1264 continue;
1265 if (pos + offset >= reuse_packfile->bitmap_pos + reuse_packfile->bitmap_nr)
1266 goto done;
1267
1268 if (reuse_packfile->bitmap_pos) {
1269 /*
1270 * When doing multi-pack reuse on a
1271 * non-preferred pack, translate bit positions
1272 * from the MIDX pseudo-pack order back to their
1273 * pack-relative positions before attempting
1274 * reuse.
1275 */
1276 struct multi_pack_index *m = reuse_packfile->from_midx;
1277 uint32_t midx_pos;
1278 off_t pack_ofs;
1279
1280 if (!m)
1281 BUG("non-zero bitmap position without MIDX");
1282
1283 midx_pos = pack_pos_to_midx(m, pos + offset);
1284 pack_ofs = nth_midxed_offset(m, midx_pos);
1285
1286 if (offset_to_pack_pos(reuse_packfile->p,
1287 pack_ofs, &pack_pos) < 0)
1288 BUG("could not find expected object at offset %"PRIuMAX" in pack %s",
1289 (uintmax_t)pack_ofs,
1290 pack_basename(reuse_packfile->p));
1291 } else {
1292 /*
1293 * Can use bit positions directly, even for MIDX
1294 * bitmaps. See comment in try_partial_reuse()
1295 * for why.
1296 */
1297 pack_pos = pos + offset;
1298 }
1299
1300 write_reused_pack_one(reuse_packfile->p, pack_pos, f,
1301 pack_start, &w_curs);
1302 display_progress(progress_state, ++written);
1303 }
1304 }
1305
1306done:
1307 unuse_pack(&w_curs);
1308}
1309
1310static void write_excluded_by_configs(void)
1311{
1312 struct oidset_iter iter;
1313 const struct object_id *oid;
1314
1315 oidset_iter_init(&excluded_by_config, &iter);
1316 while ((oid = oidset_iter_next(&iter))) {
1317 struct configured_exclusion *ex =
1318 oidmap_get(&configured_exclusions, oid);
1319
1320 if (!ex)
1321 BUG("configured exclusion wasn't configured");
1322 write_in_full(1, ex->pack_hash_hex, strlen(ex->pack_hash_hex));
1323 write_in_full(1, " ", 1);
1324 write_in_full(1, ex->uri, strlen(ex->uri));
1325 write_in_full(1, "\n", 1);
1326 }
1327}
1328
1329static const char no_split_warning[] = N_(
1330"disabling bitmap writing, packs are split due to pack.packSizeLimit"
1331);
1332
1333static void write_pack_file(void)
1334{
1335 uint32_t i = 0, j;
1336 struct hashfile *f;
1337 off_t offset;
1338 uint32_t nr_remaining = nr_result;
1339 time_t last_mtime = 0;
1340 struct object_entry **write_order;
1341 off_t bytes_written = 0;
1342
1343 if (progress > pack_to_stdout)
1344 progress_state = start_progress(the_repository,
1345 _("Writing objects"), nr_result);
1346 ALLOC_ARRAY(written_list, to_pack.nr_objects);
1347 write_order = compute_write_order();
1348
1349 do {
1350 unsigned char hash[GIT_MAX_RAWSZ];
1351 char *pack_tmp_name = NULL;
1352
1353 if (pack_to_stdout) {
1354 /*
1355 * This command is most often invoked via
1356 * git-upload-pack(1), which will typically chunk data
1357 * into pktlines. As such, we use the maximum data
1358 * length of them as buffer length.
1359 *
1360 * Note that we need to subtract one though to
1361 * accommodate for the sideband byte.
1362 */
1363 struct hashfd_options opts = {
1364 .progress = progress_state,
1365 .buffer_len = LARGE_PACKET_DATA_MAX - 1,
1366 };
1367 f = hashfd_ext(the_repository->hash_algo, 1,
1368 "<stdout>", &opts);
1369 } else {
1370 f = create_tmp_packfile(the_repository, &pack_tmp_name);
1371 }
1372
1373 offset = write_pack_header(f, nr_remaining);
1374
1375 if (reuse_packfiles_nr) {
1376 assert(pack_to_stdout);
1377 for (j = 0; j < reuse_packfiles_nr; j++) {
1378 reused_chunks_nr = 0;
1379 write_reused_pack(&reuse_packfiles[j], f);
1380 if (reused_chunks_nr)
1381 reuse_packfiles_used_nr++;
1382 }
1383 offset = hashfile_total(f);
1384 }
1385
1386 nr_written = 0;
1387 for (; i < to_pack.nr_objects; i++) {
1388 struct object_entry *e = write_order[i];
1389 if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
1390 break;
1391 display_progress(progress_state, written);
1392 }
1393
1394 bytes_written += hashfile_total(f) +
1395 the_repository->hash_algo->rawsz;
1396 if (pack_to_stdout) {
1397 /*
1398 * We never fsync when writing to stdout since we may
1399 * not be writing to an actual pack file. For instance,
1400 * the upload-pack code passes a pipe here. Calling
1401 * fsync on a pipe results in unnecessary
1402 * synchronization with the reader on some platforms.
1403 */
1404 finalize_hashfile(f, hash, FSYNC_COMPONENT_NONE,
1405 CSUM_HASH_IN_STREAM | CSUM_CLOSE);
1406 } else if (nr_written == nr_remaining) {
1407 finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK,
1408 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
1409 } else {
1410 /*
1411 * If we wrote the wrong number of entries in the
1412 * header, rewrite it like in fast-import.
1413 */
1414
1415 int fd = finalize_hashfile(f, hash, FSYNC_COMPONENT_PACK, 0);
1416 fixup_pack_header_footer(the_hash_algo, fd, hash,
1417 pack_tmp_name, nr_written,
1418 hash, offset);
1419 close(fd);
1420 if (write_bitmap_index) {
1421 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1422 warning(_(no_split_warning));
1423 write_bitmap_index = 0;
1424 }
1425 }
1426
1427 if (!pack_to_stdout) {
1428 struct stat st;
1429 struct strbuf tmpname = STRBUF_INIT;
1430 struct bitmap_writer bitmap_writer;
1431 char *idx_tmp_name = NULL;
1432
1433 /*
1434 * Packs are runtime accessed in their mtime
1435 * order since newer packs are more likely to contain
1436 * younger objects. So if we are creating multiple
1437 * packs then we should modify the mtime of later ones
1438 * to preserve this property.
1439 */
1440 if (stat(pack_tmp_name, &st) < 0) {
1441 warning_errno(_("failed to stat %s"), pack_tmp_name);
1442 } else if (!last_mtime) {
1443 last_mtime = st.st_mtime;
1444 } else {
1445 struct utimbuf utb;
1446 utb.actime = st.st_atime;
1447 utb.modtime = --last_mtime;
1448 if (utime(pack_tmp_name, &utb) < 0)
1449 warning_errno(_("failed utime() on %s"), pack_tmp_name);
1450 }
1451
1452 strbuf_addf(&tmpname, "%s-%s.", base_name,
1453 hash_to_hex(hash));
1454
1455 if (write_bitmap_index) {
1456 bitmap_writer_init(&bitmap_writer,
1457 the_repository, &to_pack,
1458 NULL);
1459 bitmap_writer_set_checksum(&bitmap_writer, hash);
1460 bitmap_writer_build_type_index(&bitmap_writer,
1461 written_list);
1462 }
1463
1464 if (cruft)
1465 pack_idx_opts.flags |= WRITE_MTIMES;
1466
1467 stage_tmp_packfiles(the_repository, &tmpname,
1468 pack_tmp_name, written_list,
1469 nr_written, &to_pack,
1470 &pack_idx_opts, hash,
1471 &idx_tmp_name);
1472
1473 if (write_bitmap_index) {
1474 size_t tmpname_len = tmpname.len;
1475
1476 strbuf_addstr(&tmpname, "bitmap");
1477 stop_progress(&progress_state);
1478
1479 bitmap_writer_show_progress(&bitmap_writer,
1480 progress);
1481 bitmap_writer_select_commits(&bitmap_writer,
1482 indexed_commits,
1483 indexed_commits_nr);
1484 if (bitmap_writer_build(&bitmap_writer) < 0)
1485 die(_("failed to write bitmap index"));
1486 bitmap_writer_finish(&bitmap_writer,
1487 written_list,
1488 tmpname.buf, write_bitmap_options);
1489 bitmap_writer_free(&bitmap_writer);
1490 write_bitmap_index = 0;
1491 strbuf_setlen(&tmpname, tmpname_len);
1492 }
1493
1494 rename_tmp_packfile_idx(the_repository, &tmpname, &idx_tmp_name);
1495
1496 free(idx_tmp_name);
1497 strbuf_release(&tmpname);
1498 free(pack_tmp_name);
1499 puts(hash_to_hex(hash));
1500 }
1501
1502 /* mark written objects as written to previous pack */
1503 for (j = 0; j < nr_written; j++) {
1504 written_list[j]->offset = (off_t)-1;
1505 }
1506 nr_remaining -= nr_written;
1507 } while (nr_remaining && i < to_pack.nr_objects);
1508
1509 free(written_list);
1510 free(write_order);
1511 stop_progress(&progress_state);
1512 if (written != nr_result)
1513 die(_("wrote %"PRIu32" objects while expecting %"PRIu32),
1514 written, nr_result);
1515 trace2_data_intmax("pack-objects", the_repository,
1516 "write_pack_file/wrote", nr_result);
1517 trace2_data_intmax("pack-objects", the_repository,
1518 "write_pack_file/wrote_bytes", bytes_written);
1519}
1520
1521static int no_try_delta(const char *path)
1522{
1523 static struct attr_check *check;
1524
1525 if (!check)
1526 check = attr_check_initl("delta", NULL);
1527 git_check_attr(the_repository->index, path, check);
1528 if (ATTR_FALSE(check->items[0].value))
1529 return 1;
1530 return 0;
1531}
1532
1533/*
1534 * When adding an object, check whether we have already added it
1535 * to our packing list. If so, we can skip. However, if we are
1536 * being asked to excludei t, but the previous mention was to include
1537 * it, make sure to adjust its flags and tweak our numbers accordingly.
1538 *
1539 * As an optimization, we pass out the index position where we would have
1540 * found the item, since that saves us from having to look it up again a
1541 * few lines later when we want to add the new entry.
1542 */
1543static int have_duplicate_entry(const struct object_id *oid,
1544 int exclude)
1545{
1546 struct object_entry *entry;
1547
1548 if (reuse_packfile_bitmap &&
1549 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid))
1550 return 1;
1551
1552 entry = packlist_find(&to_pack, oid);
1553 if (!entry)
1554 return 0;
1555
1556 if (exclude) {
1557 if (!entry->preferred_base)
1558 nr_result--;
1559 entry->preferred_base = 1;
1560 }
1561
1562 return 1;
1563}
1564
1565static int want_cruft_object_mtime(struct repository *r,
1566 const struct object_id *oid,
1567 unsigned flags, uint32_t mtime)
1568{
1569 struct odb_source_files *files = odb_source_files_downcast(r->objects->source);
1570
1571 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
1572 struct packed_git **cache = packfile_store_get_kept_pack_cache(dir->packed, flags);
1573
1574 for (; *cache; cache++) {
1575 struct packed_git *p = *cache;
1576 off_t ofs;
1577 uint32_t candidate_mtime;
1578
1579 ofs = find_pack_entry_one(oid, p);
1580 if (!ofs)
1581 continue;
1582
1583 /*
1584 * We have a copy of the object 'oid' in a non-cruft
1585 * pack. We can avoid packing an additional copy
1586 * regardless of what the existing copy's mtime is since
1587 * it is outside of a cruft pack.
1588 */
1589 if (!p->is_cruft)
1590 return 0;
1591
1592 /*
1593 * If we have a copy of the object 'oid' in a cruft
1594 * pack, then either read the cruft pack's mtime for
1595 * that object, or, if that can't be loaded, assume the
1596 * pack's mtime itself.
1597 */
1598 if (!load_pack_mtimes(p)) {
1599 uint32_t pos;
1600 if (offset_to_pack_pos(p, ofs, &pos) < 0)
1601 continue;
1602 candidate_mtime = nth_packed_mtime(p, pos);
1603 } else {
1604 candidate_mtime = p->mtime;
1605 }
1606
1607 /*
1608 * We have a surviving copy of the object in a cruft
1609 * pack whose mtime is greater than or equal to the one
1610 * we are considering. We can thus avoid packing an
1611 * additional copy of that object.
1612 */
1613 if (mtime <= candidate_mtime)
1614 return 0;
1615 }
1616 }
1617
1618 return -1;
1619}
1620
1621static int want_found_object(const struct object_id *oid, int exclude,
1622 struct packed_git *p, uint32_t mtime)
1623{
1624 if (exclude)
1625 return 1;
1626 if (incremental)
1627 return 0;
1628
1629 if (!is_pack_valid(p))
1630 return -1;
1631
1632 /*
1633 * When asked to do --local (do not include an object that appears in a
1634 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1635 * an object that appears in a pack marked with .keep), finding a pack
1636 * that matches the criteria is sufficient for us to decide to omit it.
1637 * However, even if this pack does not satisfy the criteria, we need to
1638 * make sure no copy of this object appears in _any_ pack that makes us
1639 * to omit the object, so we need to check all the packs.
1640 *
1641 * We can however first check whether these options can possibly matter;
1642 * if they do not matter we know we want the object in generated pack.
1643 * Otherwise, we signal "-1" at the end to tell the caller that we do
1644 * not know either way, and it needs to check more packs.
1645 */
1646
1647 /*
1648 * Objects in packs borrowed from elsewhere are discarded regardless of
1649 * if they appear in other packs that weren't borrowed.
1650 */
1651 if (local && !p->pack_local)
1652 return 0;
1653
1654 /*
1655 * Then handle .keep first, as we have a fast(er) path there.
1656 */
1657 if (ignore_packed_keep_on_disk || ignore_packed_keep_in_core ||
1658 ignore_packed_keep_in_core_open) {
1659 /*
1660 * Set the flags for the kept-pack cache to be the ones we want
1661 * to ignore.
1662 *
1663 * That is, if we are ignoring objects in on-disk keep packs,
1664 * then we want to search through the on-disk keep and ignore
1665 * the in-core ones.
1666 */
1667 unsigned flags = 0;
1668 if (ignore_packed_keep_on_disk)
1669 flags |= KEPT_PACK_ON_DISK;
1670 if (ignore_packed_keep_in_core)
1671 flags |= KEPT_PACK_IN_CORE;
1672 if (ignore_packed_keep_in_core_open)
1673 flags |= KEPT_PACK_IN_CORE_OPEN;
1674
1675 /*
1676 * If the object is in a pack that we want to ignore, *and* we
1677 * don't have any cruft packs that are being retained, we can
1678 * abort quickly.
1679 */
1680 if (!ignore_packed_keep_in_core_has_cruft) {
1681 if (ignore_packed_keep_on_disk && p->pack_keep)
1682 return 0;
1683 if (ignore_packed_keep_in_core && p->pack_keep_in_core)
1684 return 0;
1685 if (ignore_packed_keep_in_core_open && p->pack_keep_in_core_open)
1686 return 0;
1687 if (has_object_kept_pack(p->repo, oid, flags))
1688 return 0;
1689 } else {
1690 /*
1691 * But if there is at least one cruft pack which
1692 * is being kept, we only want to include the
1693 * provided object if it has a strictly greater
1694 * mtime than any existing cruft copy.
1695 */
1696 if (!want_cruft_object_mtime(p->repo, oid, flags,
1697 mtime))
1698 return 0;
1699 }
1700 }
1701
1702 /*
1703 * At this point we know definitively that either we don't care about
1704 * keep-packs, or the object is not in one. Keep checking other
1705 * conditions...
1706 */
1707 if (!local || !have_non_local_packs)
1708 return 1;
1709
1710 /* we don't know yet; keep looking for more packs */
1711 return -1;
1712}
1713
1714static int want_object_in_pack_one(struct packed_git *p,
1715 const struct object_id *oid,
1716 int exclude,
1717 struct packed_git **found_pack,
1718 off_t *found_offset,
1719 uint32_t found_mtime)
1720{
1721 off_t offset;
1722
1723 if (p == *found_pack)
1724 offset = *found_offset;
1725 else
1726 offset = find_pack_entry_one(oid, p);
1727
1728 if (offset) {
1729 if (!*found_pack) {
1730 if (!is_pack_valid(p))
1731 return -1;
1732 *found_offset = offset;
1733 *found_pack = p;
1734 }
1735 return want_found_object(oid, exclude, p, found_mtime);
1736 }
1737 return -1;
1738}
1739
1740/*
1741 * Check whether we want the object in the pack (e.g., we do not want
1742 * objects found in non-local stores if the "--local" option was used).
1743 *
1744 * If the caller already knows an existing pack it wants to take the object
1745 * from, that is passed in *found_pack and *found_offset; otherwise this
1746 * function finds if there is any pack that has the object and returns the pack
1747 * and its offset in these variables.
1748 */
1749static int want_object_in_pack_mtime(const struct object_id *oid,
1750 int exclude,
1751 struct packed_git **found_pack,
1752 off_t *found_offset,
1753 uint32_t found_mtime)
1754{
1755 struct odb_source_files *files =
1756 odb_source_files_downcast(the_repository->objects->source);
1757 int want;
1758 struct packfile_list_entry *e;
1759
1760 if (!exclude && local) {
1761 /*
1762 * Note that we start iterating at `dirs->next` so that we
1763 * skip the local object directory.
1764 */
1765 for (struct odb_files_dir *dir = files->dirs->next; dir; dir = dir->next)
1766 if (!odb_source_read_object_info(&dir->loose->base, oid, NULL, 0, NULL))
1767 return 0;
1768 }
1769
1770 /*
1771 * If we already know the pack object lives in, start checks from that
1772 * pack - in the usual case when neither --local was given nor .keep files
1773 * are present we will determine the answer right now.
1774 */
1775 if (*found_pack) {
1776 want = want_found_object(oid, exclude, *found_pack,
1777 found_mtime);
1778 if (want != -1)
1779 return want;
1780
1781 *found_pack = NULL;
1782 *found_offset = 0;
1783 }
1784
1785 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
1786 struct multi_pack_index *m = get_multi_pack_index(dir->packed);
1787 struct pack_entry e;
1788
1789 if (m && midx_fill_entry(m, oid, &e, NULL) == MIDX_FILL_HIT) {
1790 want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset, found_mtime);
1791 if (want != -1)
1792 return want;
1793 }
1794 }
1795
1796 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
1797 for (e = dir->packed->packs.head; e; e = e->next) {
1798 struct packed_git *p = e->pack;
1799 want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset, found_mtime);
1800 if (!exclude && want > 0)
1801 packfile_list_prepend(&dir->packed->packs, p);
1802 if (want != -1)
1803 return want;
1804 }
1805 }
1806
1807 if (uri_protocols.nr) {
1808 struct configured_exclusion *ex =
1809 oidmap_get(&configured_exclusions, oid);
1810 int i;
1811 const char *p;
1812
1813 if (ex) {
1814 for (i = 0; i < uri_protocols.nr; i++) {
1815 if (skip_prefix(ex->uri,
1816 uri_protocols.items[i].string,
1817 &p) &&
1818 *p == ':') {
1819 oidset_insert(&excluded_by_config, oid);
1820 return 0;
1821 }
1822 }
1823 }
1824 }
1825
1826 return 1;
1827}
1828
1829static inline int want_object_in_pack(const struct object_id *oid,
1830 int exclude,
1831 struct packed_git **found_pack,
1832 off_t *found_offset)
1833{
1834 return want_object_in_pack_mtime(oid, exclude, found_pack, found_offset,
1835 0);
1836}
1837
1838static struct object_entry *create_object_entry(const struct object_id *oid,
1839 enum object_type type,
1840 uint32_t hash,
1841 int exclude,
1842 int no_try_delta,
1843 struct packed_git *found_pack,
1844 off_t found_offset)
1845{
1846 struct object_entry *entry;
1847
1848 entry = packlist_alloc(&to_pack, oid);
1849 entry->hash = hash;
1850 oe_set_type(entry, type);
1851 if (exclude)
1852 entry->preferred_base = 1;
1853 else
1854 nr_result++;
1855 if (found_pack) {
1856 oe_set_in_pack(&to_pack, entry, found_pack);
1857 entry->in_pack_offset = found_offset;
1858 }
1859
1860 entry->no_try_delta = no_try_delta;
1861
1862 return entry;
1863}
1864
1865static const char no_closure_warning[] = N_(
1866"disabling bitmap writing, as some objects are not being packed"
1867);
1868
1869static void add_object_entry(const struct object_id *oid, enum object_type type,
1870 const char *name, int exclude)
1871{
1872 struct packed_git *found_pack = NULL;
1873 off_t found_offset = 0;
1874
1875 display_progress(progress_state, ++nr_seen);
1876
1877 if (have_duplicate_entry(oid, exclude))
1878 return;
1879
1880 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
1881 /* The pack is missing an object, so it will not have closure */
1882 if (write_bitmap_index) {
1883 if (write_bitmap_index != WRITE_BITMAP_QUIET)
1884 warning(_(no_closure_warning));
1885 write_bitmap_index = 0;
1886 }
1887 return;
1888 }
1889
1890 create_object_entry(oid, type, pack_name_hash_fn(name),
1891 exclude, name && no_try_delta(name),
1892 found_pack, found_offset);
1893}
1894
1895static int add_object_entry_from_bitmap(const struct object_id *oid,
1896 enum object_type type,
1897 int flags UNUSED, uint32_t name_hash,
1898 struct packed_git *pack, off_t offset,
1899 void *payload UNUSED)
1900{
1901 display_progress(progress_state, ++nr_seen);
1902
1903 if (have_duplicate_entry(oid, 0))
1904 return 0;
1905
1906 if (!want_object_in_pack(oid, 0, &pack, &offset))
1907 return 0;
1908
1909 create_object_entry(oid, type, name_hash, 0, 0, pack, offset);
1910 return 0;
1911}
1912
1913struct pbase_tree_cache {
1914 struct object_id oid;
1915 int ref;
1916 int temporary;
1917 void *tree_data;
1918 unsigned long tree_size;
1919};
1920
1921static struct pbase_tree_cache *(pbase_tree_cache[256]);
1922static int pbase_tree_cache_ix(const struct object_id *oid)
1923{
1924 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
1925}
1926static int pbase_tree_cache_ix_incr(int ix)
1927{
1928 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1929}
1930
1931static struct pbase_tree {
1932 struct pbase_tree *next;
1933 /* This is a phony "cache" entry; we are not
1934 * going to evict it or find it through _get()
1935 * mechanism -- this is for the toplevel node that
1936 * would almost always change with any commit.
1937 */
1938 struct pbase_tree_cache pcache;
1939} *pbase_tree;
1940
1941static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
1942{
1943 struct pbase_tree_cache *ent, *nent;
1944 void *data;
1945 unsigned long size;
1946 size_t size_st = 0;
1947 enum object_type type;
1948 int neigh;
1949 int my_ix = pbase_tree_cache_ix(oid);
1950 int available_ix = -1;
1951
1952 /* pbase-tree-cache acts as a limited hashtable.
1953 * your object will be found at your index or within a few
1954 * slots after that slot if it is cached.
1955 */
1956 for (neigh = 0; neigh < 8; neigh++) {
1957 ent = pbase_tree_cache[my_ix];
1958 if (ent && oideq(&ent->oid, oid)) {
1959 ent->ref++;
1960 return ent;
1961 }
1962 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1963 ((0 <= available_ix) &&
1964 (!ent && pbase_tree_cache[available_ix])))
1965 available_ix = my_ix;
1966 if (!ent)
1967 break;
1968 my_ix = pbase_tree_cache_ix_incr(my_ix);
1969 }
1970
1971 /* Did not find one. Either we got a bogus request or
1972 * we need to read and perhaps cache.
1973 */
1974 data = odb_read_object(the_repository->objects, oid, &type, &size_st);
1975 size = cast_size_t_to_ulong(size_st);
1976 if (!data)
1977 return NULL;
1978 if (type != OBJ_TREE) {
1979 free(data);
1980 return NULL;
1981 }
1982
1983 /* We need to either cache or return a throwaway copy */
1984
1985 if (available_ix < 0)
1986 ent = NULL;
1987 else {
1988 ent = pbase_tree_cache[available_ix];
1989 my_ix = available_ix;
1990 }
1991
1992 if (!ent) {
1993 nent = xmalloc(sizeof(*nent));
1994 nent->temporary = (available_ix < 0);
1995 }
1996 else {
1997 /* evict and reuse */
1998 free(ent->tree_data);
1999 nent = ent;
2000 }
2001 oidcpy(&nent->oid, oid);
2002 nent->tree_data = data;
2003 nent->tree_size = size;
2004 nent->ref = 1;
2005 if (!nent->temporary)
2006 pbase_tree_cache[my_ix] = nent;
2007 return nent;
2008}
2009
2010static void pbase_tree_put(struct pbase_tree_cache *cache)
2011{
2012 if (!cache->temporary) {
2013 cache->ref--;
2014 return;
2015 }
2016 free(cache->tree_data);
2017 free(cache);
2018}
2019
2020static size_t name_cmp_len(const char *name)
2021{
2022 return strcspn(name, "\n/");
2023}
2024
2025static void add_pbase_object(struct tree_desc *tree,
2026 const char *name,
2027 size_t cmplen,
2028 const char *fullname)
2029{
2030 struct name_entry entry;
2031 int cmp;
2032
2033 while (tree_entry(tree,&entry)) {
2034 if (S_ISGITLINK(entry.mode))
2035 continue;
2036 cmp = tree_entry_len(&entry) != cmplen ? 1 :
2037 memcmp(name, entry.path, cmplen);
2038 if (cmp > 0)
2039 continue;
2040 if (cmp < 0)
2041 return;
2042 if (name[cmplen] != '/') {
2043 add_object_entry(&entry.oid,
2044 object_type(entry.mode),
2045 fullname, 1);
2046 return;
2047 }
2048 if (S_ISDIR(entry.mode)) {
2049 struct tree_desc sub;
2050 struct pbase_tree_cache *tree;
2051 const char *down = name+cmplen+1;
2052 size_t downlen = name_cmp_len(down);
2053
2054 tree = pbase_tree_get(&entry.oid);
2055 if (!tree)
2056 return;
2057 init_tree_desc(&sub, &tree->oid,
2058 tree->tree_data, tree->tree_size);
2059
2060 add_pbase_object(&sub, down, downlen, fullname);
2061 pbase_tree_put(tree);
2062 }
2063 }
2064}
2065
2066static unsigned *done_pbase_paths;
2067static int done_pbase_paths_num;
2068static int done_pbase_paths_alloc;
2069static int done_pbase_path_pos(unsigned hash)
2070{
2071 int lo = 0;
2072 int hi = done_pbase_paths_num;
2073 while (lo < hi) {
2074 int mi = lo + (hi - lo) / 2;
2075 if (done_pbase_paths[mi] == hash)
2076 return mi;
2077 if (done_pbase_paths[mi] < hash)
2078 hi = mi;
2079 else
2080 lo = mi + 1;
2081 }
2082 return -lo-1;
2083}
2084
2085static int check_pbase_path(unsigned hash)
2086{
2087 int pos = done_pbase_path_pos(hash);
2088 if (0 <= pos)
2089 return 1;
2090 pos = -pos - 1;
2091 ALLOC_GROW(done_pbase_paths,
2092 done_pbase_paths_num + 1,
2093 done_pbase_paths_alloc);
2094 done_pbase_paths_num++;
2095 if (pos < done_pbase_paths_num)
2096 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
2097 done_pbase_paths_num - pos - 1);
2098 done_pbase_paths[pos] = hash;
2099 return 0;
2100}
2101
2102static void add_preferred_base_object(const char *name)
2103{
2104 struct pbase_tree *it;
2105 size_t cmplen;
2106 unsigned hash = pack_name_hash_fn(name);
2107
2108 if (!num_preferred_base || check_pbase_path(hash))
2109 return;
2110
2111 cmplen = name_cmp_len(name);
2112 for (it = pbase_tree; it; it = it->next) {
2113 if (cmplen == 0) {
2114 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
2115 }
2116 else {
2117 struct tree_desc tree;
2118 init_tree_desc(&tree, &it->pcache.oid,
2119 it->pcache.tree_data, it->pcache.tree_size);
2120 add_pbase_object(&tree, name, cmplen, name);
2121 }
2122 }
2123}
2124
2125static void add_preferred_base(struct object_id *oid)
2126{
2127 struct pbase_tree *it;
2128 void *data;
2129 unsigned long size;
2130 size_t size_st = 0;
2131 struct object_id tree_oid;
2132
2133 if (window <= num_preferred_base++)
2134 return;
2135
2136 data = odb_read_object_peeled(the_repository->objects, oid,
2137 OBJ_TREE, &size_st, &tree_oid);
2138 size = cast_size_t_to_ulong(size_st);
2139 if (!data)
2140 return;
2141
2142 for (it = pbase_tree; it; it = it->next) {
2143 if (oideq(&it->pcache.oid, &tree_oid)) {
2144 free(data);
2145 return;
2146 }
2147 }
2148
2149 CALLOC_ARRAY(it, 1);
2150 it->next = pbase_tree;
2151 pbase_tree = it;
2152
2153 oidcpy(&it->pcache.oid, &tree_oid);
2154 it->pcache.tree_data = data;
2155 it->pcache.tree_size = size;
2156}
2157
2158static void cleanup_preferred_base(void)
2159{
2160 struct pbase_tree *it;
2161 unsigned i;
2162
2163 it = pbase_tree;
2164 pbase_tree = NULL;
2165 while (it) {
2166 struct pbase_tree *tmp = it;
2167 it = tmp->next;
2168 free(tmp->pcache.tree_data);
2169 free(tmp);
2170 }
2171
2172 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
2173 if (!pbase_tree_cache[i])
2174 continue;
2175 free(pbase_tree_cache[i]->tree_data);
2176 FREE_AND_NULL(pbase_tree_cache[i]);
2177 }
2178
2179 FREE_AND_NULL(done_pbase_paths);
2180 done_pbase_paths_num = done_pbase_paths_alloc = 0;
2181}
2182
2183/*
2184 * Return 1 iff the object specified by "delta" can be sent
2185 * literally as a delta against the base in "base_sha1". If
2186 * so, then *base_out will point to the entry in our packing
2187 * list, or NULL if we must use the external-base list.
2188 *
2189 * Depth value does not matter - find_deltas() will
2190 * never consider reused delta as the base object to
2191 * deltify other objects against, in order to avoid
2192 * circular deltas.
2193 */
2194static int can_reuse_delta(const struct object_id *base_oid,
2195 struct object_entry *delta,
2196 struct object_entry **base_out)
2197{
2198 struct object_entry *base;
2199
2200 /*
2201 * First see if we're already sending the base (or it's explicitly in
2202 * our "excluded" list).
2203 */
2204 base = packlist_find(&to_pack, base_oid);
2205 if (base) {
2206 if (!in_same_island(&delta->idx.oid, &base->idx.oid))
2207 return 0;
2208 *base_out = base;
2209 return 1;
2210 }
2211
2212 /*
2213 * Otherwise, reachability bitmaps may tell us if the receiver has it,
2214 * even if it was buried too deep in history to make it into the
2215 * packing list.
2216 */
2217 if (thin && bitmap_has_oid_in_uninteresting(bitmap_git, base_oid)) {
2218 if (use_delta_islands) {
2219 if (!in_same_island(&delta->idx.oid, base_oid))
2220 return 0;
2221 }
2222 *base_out = NULL;
2223 return 1;
2224 }
2225
2226 return 0;
2227}
2228
2229static void prefetch_to_pack(uint32_t object_index_start) {
2230 struct oid_array to_fetch = OID_ARRAY_INIT;
2231 uint32_t i;
2232
2233 for (i = object_index_start; i < to_pack.nr_objects; i++) {
2234 struct object_entry *entry = to_pack.objects + i;
2235
2236 if (!odb_read_object_info_extended(the_repository->objects,
2237 &entry->idx.oid,
2238 NULL,
2239 OBJECT_INFO_FOR_PREFETCH))
2240 continue;
2241 oid_array_append(&to_fetch, &entry->idx.oid);
2242 }
2243 promisor_remote_get_direct(the_repository,
2244 to_fetch.oid, to_fetch.nr);
2245 oid_array_clear(&to_fetch);
2246}
2247
2248static void check_object(struct object_entry *entry, uint32_t object_index)
2249{
2250 size_t canonical_size;
2251 enum object_type type;
2252 struct object_info oi = {.typep = &type, .sizep = &canonical_size};
2253
2254 if (IN_PACK(entry)) {
2255 struct packed_git *p = IN_PACK(entry);
2256 struct pack_window *w_curs = NULL;
2257 int have_base = 0;
2258 struct object_id base_ref;
2259 struct object_entry *base_entry;
2260 size_t used, used_0, avail;
2261 off_t ofs;
2262 unsigned char *buf, c;
2263 enum object_type type;
2264 size_t in_pack_size;
2265
2266 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
2267
2268 /*
2269 * We want in_pack_type even if we do not reuse delta
2270 * since non-delta representations could still be reused.
2271 */
2272 used = unpack_object_header_buffer(buf, avail,
2273 &type,
2274 &in_pack_size);
2275 if (used == 0)
2276 goto give_up;
2277
2278 if (type < 0)
2279 BUG("invalid type %d", type);
2280 entry->in_pack_type = type;
2281
2282 /*
2283 * Determine if this is a delta and if so whether we can
2284 * reuse it or not. Otherwise let's find out as cheaply as
2285 * possible what the actual type and size for this object is.
2286 */
2287 switch (entry->in_pack_type) {
2288 default:
2289 /* Not a delta hence we've already got all we need. */
2290 oe_set_type(entry, entry->in_pack_type);
2291 SET_SIZE(entry, in_pack_size);
2292 entry->in_pack_header_size = used;
2293 if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
2294 goto give_up;
2295 unuse_pack(&w_curs);
2296 return;
2297 case OBJ_REF_DELTA:
2298 if (reuse_delta && !entry->preferred_base) {
2299 oidread(&base_ref,
2300 use_pack(p, &w_curs,
2301 entry->in_pack_offset + used,
2302 NULL),
2303 the_repository->hash_algo);
2304 have_base = 1;
2305 }
2306 entry->in_pack_header_size = used + the_hash_algo->rawsz;
2307 break;
2308 case OBJ_OFS_DELTA:
2309 buf = use_pack(p, &w_curs,
2310 entry->in_pack_offset + used, NULL);
2311 used_0 = 0;
2312 c = buf[used_0++];
2313 ofs = c & 127;
2314 while (c & 128) {
2315 ofs += 1;
2316 if (!ofs || MSB(ofs, 7)) {
2317 error(_("delta base offset overflow in pack for %s"),
2318 oid_to_hex(&entry->idx.oid));
2319 goto give_up;
2320 }
2321 c = buf[used_0++];
2322 ofs = (ofs << 7) + (c & 127);
2323 }
2324 ofs = entry->in_pack_offset - ofs;
2325 if (ofs <= 0 || ofs >= entry->in_pack_offset) {
2326 error(_("delta base offset out of bound for %s"),
2327 oid_to_hex(&entry->idx.oid));
2328 goto give_up;
2329 }
2330 if (reuse_delta && !entry->preferred_base) {
2331 uint32_t pos;
2332 if (offset_to_pack_pos(p, ofs, &pos) < 0)
2333 goto give_up;
2334 if (!nth_packed_object_id(&base_ref, p,
2335 pack_pos_to_index(p, pos)))
2336 have_base = 1;
2337 }
2338 entry->in_pack_header_size = used + used_0;
2339 break;
2340 }
2341
2342 if (have_base &&
2343 can_reuse_delta(&base_ref, entry, &base_entry)) {
2344 oe_set_type(entry, entry->in_pack_type);
2345 SET_SIZE(entry, in_pack_size); /* delta size */
2346 SET_DELTA_SIZE(entry, in_pack_size);
2347
2348 if (base_entry) {
2349 SET_DELTA(entry, base_entry);
2350 entry->delta_sibling_idx = base_entry->delta_child_idx;
2351 SET_DELTA_CHILD(base_entry, entry);
2352 } else {
2353 SET_DELTA_EXT(entry, &base_ref);
2354 }
2355
2356 unuse_pack(&w_curs);
2357 return;
2358 }
2359
2360 if (oe_type(entry)) {
2361 off_t delta_pos;
2362
2363 /*
2364 * This must be a delta and we already know what the
2365 * final object type is. Let's extract the actual
2366 * object size from the delta header.
2367 */
2368 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
2369 canonical_size = get_size_from_delta(p, &w_curs,
2370 delta_pos);
2371 if (canonical_size == 0)
2372 goto give_up;
2373 SET_SIZE(entry, canonical_size);
2374 unuse_pack(&w_curs);
2375 return;
2376 }
2377
2378 /*
2379 * No choice but to fall back to the recursive delta walk
2380 * with odb_read_object_info() to find about the object type
2381 * at this point...
2382 */
2383 give_up:
2384 unuse_pack(&w_curs);
2385 }
2386
2387 if (odb_read_object_info_extended(the_repository->objects, &entry->idx.oid, &oi,
2388 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0) {
2389 if (repo_has_promisor_remote(the_repository)) {
2390 prefetch_to_pack(object_index);
2391 if (odb_read_object_info_extended(the_repository->objects, &entry->idx.oid, &oi,
2392 OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_LOOKUP_REPLACE) < 0)
2393 type = -1;
2394 } else {
2395 type = -1;
2396 }
2397 }
2398 oe_set_type(entry, type);
2399 if (entry->type_valid) {
2400 SET_SIZE(entry, canonical_size);
2401 } else {
2402 /*
2403 * Bad object type is checked in prepare_pack(). This is
2404 * to permit a missing preferred base object to be ignored
2405 * as a preferred base. Doing so can result in a larger
2406 * pack file, but the transfer will still take place.
2407 */
2408 }
2409}
2410
2411static int pack_offset_sort(const void *_a, const void *_b)
2412{
2413 const struct object_entry *a = *(struct object_entry **)_a;
2414 const struct object_entry *b = *(struct object_entry **)_b;
2415 const struct packed_git *a_in_pack = IN_PACK(a);
2416 const struct packed_git *b_in_pack = IN_PACK(b);
2417
2418 /* avoid filesystem trashing with loose objects */
2419 if (!a_in_pack && !b_in_pack)
2420 return oidcmp(&a->idx.oid, &b->idx.oid);
2421
2422 if (a_in_pack < b_in_pack)
2423 return -1;
2424 if (a_in_pack > b_in_pack)
2425 return 1;
2426 return a->in_pack_offset < b->in_pack_offset ? -1 :
2427 (a->in_pack_offset > b->in_pack_offset);
2428}
2429
2430/*
2431 * Drop an on-disk delta we were planning to reuse. Naively, this would
2432 * just involve blanking out the "delta" field, but we have to deal
2433 * with some extra book-keeping:
2434 *
2435 * 1. Removing ourselves from the delta_sibling linked list.
2436 *
2437 * 2. Updating our size/type to the non-delta representation. These were
2438 * either not recorded initially (size) or overwritten with the delta type
2439 * (type) when check_object() decided to reuse the delta.
2440 *
2441 * 3. Resetting our delta depth, as we are now a base object.
2442 */
2443static void drop_reused_delta(struct object_entry *entry)
2444{
2445 unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
2446 struct object_info oi = OBJECT_INFO_INIT;
2447 enum object_type type;
2448 size_t size;
2449
2450 while (*idx) {
2451 struct object_entry *oe = &to_pack.objects[*idx - 1];
2452
2453 if (oe == entry)
2454 *idx = oe->delta_sibling_idx;
2455 else
2456 idx = &oe->delta_sibling_idx;
2457 }
2458 SET_DELTA(entry, NULL);
2459 entry->depth = 0;
2460
2461 oi.sizep = &size;
2462 oi.typep = &type;
2463 if (packed_object_info(NULL, IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
2464 /*
2465 * We failed to get the info from this pack for some reason;
2466 * fall back to odb_read_object_info, which may find another copy.
2467 * And if that fails, the error will be recorded in oe_type(entry)
2468 * and dealt with in prepare_pack().
2469 */
2470 oe_set_type(entry,
2471 odb_read_object_info(the_repository->objects,
2472 &entry->idx.oid, &size));
2473 } else {
2474 oe_set_type(entry, type);
2475 }
2476 SET_SIZE(entry, size);
2477}
2478
2479/*
2480 * Follow the chain of deltas from this entry onward, throwing away any links
2481 * that cause us to hit a cycle (as determined by the DFS state flags in
2482 * the entries).
2483 *
2484 * We also detect too-long reused chains that would violate our --depth
2485 * limit.
2486 */
2487static void break_delta_chains(struct object_entry *entry)
2488{
2489 /*
2490 * The actual depth of each object we will write is stored as an int,
2491 * as it cannot exceed our int "depth" limit. But before we break
2492 * changes based no that limit, we may potentially go as deep as the
2493 * number of objects, which is elsewhere bounded to a uint32_t.
2494 */
2495 uint32_t total_depth;
2496 struct object_entry *cur, *next;
2497
2498 for (cur = entry, total_depth = 0;
2499 cur;
2500 cur = DELTA(cur), total_depth++) {
2501 if (cur->dfs_state == DFS_DONE) {
2502 /*
2503 * We've already seen this object and know it isn't
2504 * part of a cycle. We do need to append its depth
2505 * to our count.
2506 */
2507 total_depth += cur->depth;
2508 break;
2509 }
2510
2511 /*
2512 * We break cycles before looping, so an ACTIVE state (or any
2513 * other cruft which made its way into the state variable)
2514 * is a bug.
2515 */
2516 if (cur->dfs_state != DFS_NONE)
2517 BUG("confusing delta dfs state in first pass: %d",
2518 cur->dfs_state);
2519
2520 /*
2521 * Now we know this is the first time we've seen the object. If
2522 * it's not a delta, we're done traversing, but we'll mark it
2523 * done to save time on future traversals.
2524 */
2525 if (!DELTA(cur)) {
2526 cur->dfs_state = DFS_DONE;
2527 break;
2528 }
2529
2530 /*
2531 * Mark ourselves as active and see if the next step causes
2532 * us to cycle to another active object. It's important to do
2533 * this _before_ we loop, because it impacts where we make the
2534 * cut, and thus how our total_depth counter works.
2535 * E.g., We may see a partial loop like:
2536 *
2537 * A -> B -> C -> D -> B
2538 *
2539 * Cutting B->C breaks the cycle. But now the depth of A is
2540 * only 1, and our total_depth counter is at 3. The size of the
2541 * error is always one less than the size of the cycle we
2542 * broke. Commits C and D were "lost" from A's chain.
2543 *
2544 * If we instead cut D->B, then the depth of A is correct at 3.
2545 * We keep all commits in the chain that we examined.
2546 */
2547 cur->dfs_state = DFS_ACTIVE;
2548 if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
2549 drop_reused_delta(cur);
2550 cur->dfs_state = DFS_DONE;
2551 break;
2552 }
2553 }
2554
2555 /*
2556 * And now that we've gone all the way to the bottom of the chain, we
2557 * need to clear the active flags and set the depth fields as
2558 * appropriate. Unlike the loop above, which can quit when it drops a
2559 * delta, we need to keep going to look for more depth cuts. So we need
2560 * an extra "next" pointer to keep going after we reset cur->delta.
2561 */
2562 for (cur = entry; cur; cur = next) {
2563 next = DELTA(cur);
2564
2565 /*
2566 * We should have a chain of zero or more ACTIVE states down to
2567 * a final DONE. We can quit after the DONE, because either it
2568 * has no bases, or we've already handled them in a previous
2569 * call.
2570 */
2571 if (cur->dfs_state == DFS_DONE)
2572 break;
2573 else if (cur->dfs_state != DFS_ACTIVE)
2574 BUG("confusing delta dfs state in second pass: %d",
2575 cur->dfs_state);
2576
2577 /*
2578 * If the total_depth is more than depth, then we need to snip
2579 * the chain into two or more smaller chains that don't exceed
2580 * the maximum depth. Most of the resulting chains will contain
2581 * (depth + 1) entries (i.e., depth deltas plus one base), and
2582 * the last chain (i.e., the one containing entry) will contain
2583 * whatever entries are left over, namely
2584 * (total_depth % (depth + 1)) of them.
2585 *
2586 * Since we are iterating towards decreasing depth, we need to
2587 * decrement total_depth as we go, and we need to write to the
2588 * entry what its final depth will be after all of the
2589 * snipping. Since we're snipping into chains of length (depth
2590 * + 1) entries, the final depth of an entry will be its
2591 * original depth modulo (depth + 1). Any time we encounter an
2592 * entry whose final depth is supposed to be zero, we snip it
2593 * from its delta base, thereby making it so.
2594 */
2595 cur->depth = (total_depth--) % (depth + 1);
2596 if (!cur->depth)
2597 drop_reused_delta(cur);
2598
2599 cur->dfs_state = DFS_DONE;
2600 }
2601}
2602
2603static void get_object_details(void)
2604{
2605 uint32_t i;
2606 struct object_entry **sorted_by_offset;
2607
2608 if (progress)
2609 progress_state = start_progress(the_repository,
2610 _("Counting objects"),
2611 to_pack.nr_objects);
2612
2613 CALLOC_ARRAY(sorted_by_offset, to_pack.nr_objects);
2614 for (i = 0; i < to_pack.nr_objects; i++)
2615 sorted_by_offset[i] = to_pack.objects + i;
2616 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
2617
2618 for (i = 0; i < to_pack.nr_objects; i++) {
2619 struct object_entry *entry = sorted_by_offset[i];
2620 check_object(entry, i);
2621 if (entry->type_valid &&
2622 oe_size_greater_than(&to_pack, entry,
2623 repo_settings_get_big_file_threshold(the_repository)))
2624 entry->no_try_delta = 1;
2625 display_progress(progress_state, i + 1);
2626 }
2627 stop_progress(&progress_state);
2628
2629 /*
2630 * This must happen in a second pass, since we rely on the delta
2631 * information for the whole list being completed.
2632 */
2633 for (i = 0; i < to_pack.nr_objects; i++)
2634 break_delta_chains(&to_pack.objects[i]);
2635
2636 free(sorted_by_offset);
2637}
2638
2639/*
2640 * We search for deltas in a list sorted by type, by filename hash, and then
2641 * by size, so that we see progressively smaller and smaller files.
2642 * That's because we prefer deltas to be from the bigger file
2643 * to the smaller -- deletes are potentially cheaper, but perhaps
2644 * more importantly, the bigger file is likely the more recent
2645 * one. The deepest deltas are therefore the oldest objects which are
2646 * less susceptible to be accessed often.
2647 */
2648static int type_size_sort(const void *_a, const void *_b)
2649{
2650 const struct object_entry *a = *(struct object_entry **)_a;
2651 const struct object_entry *b = *(struct object_entry **)_b;
2652 const enum object_type a_type = oe_type(a);
2653 const enum object_type b_type = oe_type(b);
2654 const unsigned long a_size = SIZE(a);
2655 const unsigned long b_size = SIZE(b);
2656
2657 if (a_type > b_type)
2658 return -1;
2659 if (a_type < b_type)
2660 return 1;
2661 if (a->hash > b->hash)
2662 return -1;
2663 if (a->hash < b->hash)
2664 return 1;
2665 if (a->preferred_base > b->preferred_base)
2666 return -1;
2667 if (a->preferred_base < b->preferred_base)
2668 return 1;
2669 if (use_delta_islands) {
2670 const int island_cmp = island_delta_cmp(&a->idx.oid, &b->idx.oid);
2671 if (island_cmp)
2672 return island_cmp;
2673 }
2674 if (a_size > b_size)
2675 return -1;
2676 if (a_size < b_size)
2677 return 1;
2678 return a < b ? -1 : (a > b); /* newest first */
2679}
2680
2681struct unpacked {
2682 struct object_entry *entry;
2683 void *data;
2684 struct delta_index *index;
2685 unsigned depth;
2686};
2687
2688static int delta_cacheable(size_t src_size, size_t trg_size,
2689 size_t delta_size)
2690{
2691 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
2692 return 0;
2693
2694 if (delta_size < cache_max_small_delta_size)
2695 return 1;
2696
2697 /* cache delta, if objects are large enough compared to delta size */
2698 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
2699 return 1;
2700
2701 return 0;
2702}
2703
2704/* Protect delta_cache_size */
2705static pthread_mutex_t cache_mutex;
2706#define cache_lock() pthread_mutex_lock(&cache_mutex)
2707#define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2708
2709/*
2710 * Protect object list partitioning (e.g. struct thread_param) and
2711 * progress_state
2712 */
2713static pthread_mutex_t progress_mutex;
2714#define progress_lock() pthread_mutex_lock(&progress_mutex)
2715#define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2716
2717/*
2718 * Access to struct object_entry is unprotected since each thread owns
2719 * a portion of the main object list. Just don't access object entries
2720 * ahead in the list because they can be stolen and would need
2721 * progress_mutex for protection.
2722 */
2723
2724static inline int oe_size_less_than(struct packing_data *pack,
2725 const struct object_entry *lhs,
2726 size_t rhs)
2727{
2728 if (lhs->size_valid)
2729 return lhs->size_ < rhs;
2730 if (rhs < pack->oe_size_limit) /* rhs < 2^x <= lhs ? */
2731 return 0;
2732 return oe_get_size_slow(pack, lhs) < rhs;
2733}
2734
2735static inline void oe_set_tree_depth(struct packing_data *pack,
2736 struct object_entry *e,
2737 unsigned int tree_depth)
2738{
2739 if (!pack->tree_depth)
2740 CALLOC_ARRAY(pack->tree_depth, pack->nr_alloc);
2741 pack->tree_depth[e - pack->objects] = tree_depth;
2742}
2743
2744static void record_tree_depth(const struct object_id *oid, const char *name)
2745{
2746 const char *p;
2747 unsigned depth;
2748 struct object_entry *ent;
2749
2750 /* the empty string is a root tree, which is depth 0 */
2751 depth = *name ? 1 : 0;
2752 for (p = strchr(name, '/'); p; p = strchr(p + 1, '/'))
2753 depth++;
2754
2755 ent = packlist_find(&to_pack, oid);
2756 if (ent && depth > oe_tree_depth(&to_pack, ent))
2757 oe_set_tree_depth(&to_pack, ent, depth);
2758}
2759
2760/*
2761 * Return the size of the object without doing any delta
2762 * reconstruction (so non-deltas are true object sizes, but deltas
2763 * return the size of the delta data).
2764 */
2765size_t oe_get_size_slow(struct packing_data *pack,
2766 const struct object_entry *e)
2767{
2768 struct packed_git *p;
2769 struct pack_window *w_curs;
2770 unsigned char *buf;
2771 enum object_type type;
2772 size_t used, avail, size;
2773
2774 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
2775 size_t sz;
2776 packing_data_lock(&to_pack);
2777 if (odb_read_object_info(the_repository->objects,
2778 &e->idx.oid, &sz) < 0)
2779 die(_("unable to get size of %s"),
2780 oid_to_hex(&e->idx.oid));
2781 packing_data_unlock(&to_pack);
2782 return sz;
2783 }
2784
2785 p = oe_in_pack(pack, e);
2786 if (!p)
2787 BUG("when e->type is a delta, it must belong to a pack");
2788
2789 packing_data_lock(&to_pack);
2790 w_curs = NULL;
2791 buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);
2792 used = unpack_object_header_buffer(buf, avail, &type, &size);
2793 if (used == 0)
2794 die(_("unable to parse object header of %s"),
2795 oid_to_hex(&e->idx.oid));
2796
2797 unuse_pack(&w_curs);
2798 packing_data_unlock(&to_pack);
2799 return size;
2800}
2801
2802static int try_delta(struct unpacked *trg, struct unpacked *src,
2803 unsigned max_depth, size_t *mem_usage)
2804{
2805 struct object_entry *trg_entry = trg->entry;
2806 struct object_entry *src_entry = src->entry;
2807 unsigned long trg_size, src_size, sizediff, max_size, sz;
2808 size_t delta_size;
2809 unsigned ref_depth;
2810 enum object_type type;
2811 void *delta_buf;
2812
2813 /* Don't bother doing diffs between different types */
2814 if (oe_type(trg_entry) != oe_type(src_entry))
2815 return -1;
2816
2817 /*
2818 * We do not bother to try a delta that we discarded on an
2819 * earlier try, but only when reusing delta data. Note that
2820 * src_entry that is marked as the preferred_base should always
2821 * be considered, as even if we produce a suboptimal delta against
2822 * it, we will still save the transfer cost, as we already know
2823 * the other side has it and we won't send src_entry at all.
2824 */
2825 if (reuse_delta && IN_PACK(trg_entry) &&
2826 IN_PACK(trg_entry) == IN_PACK(src_entry) &&
2827 !src_entry->preferred_base &&
2828 trg_entry->in_pack_type != OBJ_REF_DELTA &&
2829 trg_entry->in_pack_type != OBJ_OFS_DELTA)
2830 return 0;
2831
2832 /* Let's not bust the allowed depth. */
2833 if (src->depth >= max_depth)
2834 return 0;
2835
2836 /* Now some size filtering heuristics. */
2837 trg_size = SIZE(trg_entry);
2838 if (!DELTA(trg_entry)) {
2839 max_size = trg_size/2 - the_hash_algo->rawsz;
2840 ref_depth = 1;
2841 } else {
2842 max_size = DELTA_SIZE(trg_entry);
2843 ref_depth = trg->depth;
2844 }
2845 max_size = (uint64_t)max_size * (max_depth - src->depth) /
2846 (max_depth - ref_depth + 1);
2847 if (max_size == 0)
2848 return 0;
2849 src_size = SIZE(src_entry);
2850 sizediff = src_size < trg_size ? trg_size - src_size : 0;
2851 if (sizediff >= max_size)
2852 return 0;
2853 if (trg_size < src_size / 32)
2854 return 0;
2855
2856 if (!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))
2857 return 0;
2858
2859 /* Load data if not already done */
2860 if (!trg->data) {
2861 size_t sz_st = 0;
2862 packing_data_lock(&to_pack);
2863 trg->data = odb_read_object(the_repository->objects,
2864 &trg_entry->idx.oid, &type,
2865 &sz_st);
2866 sz = cast_size_t_to_ulong(sz_st);
2867 packing_data_unlock(&to_pack);
2868 if (!trg->data)
2869 die(_("object %s cannot be read"),
2870 oid_to_hex(&trg_entry->idx.oid));
2871 if (sz != trg_size)
2872 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2873 oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,
2874 (uintmax_t)trg_size);
2875 *mem_usage += sz;
2876 }
2877 if (!src->data) {
2878 size_t sz_st = 0;
2879 packing_data_lock(&to_pack);
2880 src->data = odb_read_object(the_repository->objects,
2881 &src_entry->idx.oid, &type,
2882 &sz_st);
2883 sz = cast_size_t_to_ulong(sz_st);
2884 packing_data_unlock(&to_pack);
2885 if (!src->data) {
2886 if (src_entry->preferred_base) {
2887 static int warned = 0;
2888 if (!warned++)
2889 warning(_("object %s cannot be read"),
2890 oid_to_hex(&src_entry->idx.oid));
2891 /*
2892 * Those objects are not included in the
2893 * resulting pack. Be resilient and ignore
2894 * them if they can't be read, in case the
2895 * pack could be created nevertheless.
2896 */
2897 return 0;
2898 }
2899 die(_("object %s cannot be read"),
2900 oid_to_hex(&src_entry->idx.oid));
2901 }
2902 if (sz != src_size)
2903 die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2904 oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,
2905 (uintmax_t)src_size);
2906 *mem_usage += sz;
2907 }
2908 if (!src->index) {
2909 src->index = create_delta_index(src->data, src_size);
2910 if (!src->index) {
2911 static int warned = 0;
2912 if (!warned++)
2913 warning(_("suboptimal pack - out of memory"));
2914 return 0;
2915 }
2916 *mem_usage += sizeof_delta_index(src->index);
2917 }
2918
2919 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
2920 if (!delta_buf)
2921 return 0;
2922
2923 if (DELTA(trg_entry)) {
2924 /* Prefer only shallower same-sized deltas. */
2925 if (delta_size == DELTA_SIZE(trg_entry) &&
2926 src->depth + 1 >= trg->depth) {
2927 free(delta_buf);
2928 return 0;
2929 }
2930 }
2931
2932 /*
2933 * Handle memory allocation outside of the cache
2934 * accounting lock. Compiler will optimize the strangeness
2935 * away when NO_PTHREADS is defined.
2936 */
2937 free(trg_entry->delta_data);
2938 cache_lock();
2939 if (trg_entry->delta_data) {
2940 delta_cache_size -= DELTA_SIZE(trg_entry);
2941 trg_entry->delta_data = NULL;
2942 }
2943 if (delta_cacheable(src_size, trg_size, delta_size)) {
2944 delta_cache_size += delta_size;
2945 cache_unlock();
2946 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
2947 } else {
2948 cache_unlock();
2949 free(delta_buf);
2950 }
2951
2952 SET_DELTA(trg_entry, src_entry);
2953 SET_DELTA_SIZE(trg_entry, delta_size);
2954 trg->depth = src->depth + 1;
2955
2956 return 1;
2957}
2958
2959static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
2960{
2961 struct object_entry *child = DELTA_CHILD(me);
2962 unsigned int m = n;
2963 while (child) {
2964 const unsigned int c = check_delta_limit(child, n + 1);
2965 if (m < c)
2966 m = c;
2967 child = DELTA_SIBLING(child);
2968 }
2969 return m;
2970}
2971
2972static size_t free_unpacked(struct unpacked *n)
2973{
2974 size_t freed_mem = sizeof_delta_index(n->index);
2975 free_delta_index(n->index);
2976 n->index = NULL;
2977 if (n->data) {
2978 freed_mem += SIZE(n->entry);
2979 FREE_AND_NULL(n->data);
2980 }
2981 n->entry = NULL;
2982 n->depth = 0;
2983 return freed_mem;
2984}
2985
2986static void find_deltas(struct object_entry **list, unsigned *list_size,
2987 int window, int depth, unsigned *processed)
2988{
2989 uint32_t i, idx = 0, count = 0;
2990 struct unpacked *array;
2991 size_t mem_usage = 0;
2992
2993 CALLOC_ARRAY(array, window);
2994
2995 for (;;) {
2996 struct object_entry *entry;
2997 struct unpacked *n = array + idx;
2998 int j, max_depth, best_base = -1;
2999
3000 progress_lock();
3001 if (!*list_size) {
3002 progress_unlock();
3003 break;
3004 }
3005 entry = *list++;
3006 (*list_size)--;
3007 if (!entry->preferred_base) {
3008 (*processed)++;
3009 display_progress(progress_state, *processed);
3010 }
3011 progress_unlock();
3012
3013 mem_usage -= free_unpacked(n);
3014 n->entry = entry;
3015
3016 while (window_memory_limit &&
3017 mem_usage > window_memory_limit &&
3018 count > 1) {
3019 const uint32_t tail = (idx + window - count) % window;
3020 mem_usage -= free_unpacked(array + tail);
3021 count--;
3022 }
3023
3024 /* We do not compute delta to *create* objects we are not
3025 * going to pack.
3026 */
3027 if (entry->preferred_base)
3028 goto next;
3029
3030 /*
3031 * If the current object is at pack edge, take the depth the
3032 * objects that depend on the current object into account
3033 * otherwise they would become too deep.
3034 */
3035 max_depth = depth;
3036 if (DELTA_CHILD(entry)) {
3037 max_depth -= check_delta_limit(entry, 0);
3038 if (max_depth <= 0)
3039 goto next;
3040 }
3041
3042 j = window;
3043 while (--j > 0) {
3044 int ret;
3045 uint32_t other_idx = idx + j;
3046 struct unpacked *m;
3047 if (other_idx >= window)
3048 other_idx -= window;
3049 m = array + other_idx;
3050 if (!m->entry)
3051 break;
3052 ret = try_delta(n, m, max_depth, &mem_usage);
3053 if (ret < 0)
3054 break;
3055 else if (ret > 0)
3056 best_base = other_idx;
3057 }
3058
3059 /*
3060 * If we decided to cache the delta data, then it is best
3061 * to compress it right away. First because we have to do
3062 * it anyway, and doing it here while we're threaded will
3063 * save a lot of time in the non threaded write phase,
3064 * as well as allow for caching more deltas within
3065 * the same cache size limit.
3066 * ...
3067 * But only if not writing to stdout, since in that case
3068 * the network is most likely throttling writes anyway,
3069 * and therefore it is best to go to the write phase ASAP
3070 * instead, as we can afford spending more time compressing
3071 * between writes at that moment.
3072 */
3073 if (entry->delta_data && !pack_to_stdout) {
3074 unsigned long size;
3075
3076 size = do_compress(&entry->delta_data, DELTA_SIZE(entry));
3077 if (size < (1U << OE_Z_DELTA_BITS)) {
3078 entry->z_delta_size = size;
3079 cache_lock();
3080 delta_cache_size -= DELTA_SIZE(entry);
3081 delta_cache_size += entry->z_delta_size;
3082 cache_unlock();
3083 } else {
3084 FREE_AND_NULL(entry->delta_data);
3085 entry->z_delta_size = 0;
3086 }
3087 }
3088
3089 /* if we made n a delta, and if n is already at max
3090 * depth, leaving it in the window is pointless. we
3091 * should evict it first.
3092 */
3093 if (DELTA(entry) && max_depth <= n->depth)
3094 continue;
3095
3096 /*
3097 * Move the best delta base up in the window, after the
3098 * currently deltified object, to keep it longer. It will
3099 * be the first base object to be attempted next.
3100 */
3101 if (DELTA(entry)) {
3102 struct unpacked swap = array[best_base];
3103 int dist = (window + idx - best_base) % window;
3104 int dst = best_base;
3105 while (dist--) {
3106 int src = (dst + 1) % window;
3107 array[dst] = array[src];
3108 dst = src;
3109 }
3110 array[dst] = swap;
3111 }
3112
3113 next:
3114 idx++;
3115 if (count + 1 < window)
3116 count++;
3117 if (idx >= window)
3118 idx = 0;
3119 }
3120
3121 for (i = 0; i < window; ++i) {
3122 free_delta_index(array[i].index);
3123 free(array[i].data);
3124 }
3125 free(array);
3126}
3127
3128/*
3129 * The main object list is split into smaller lists, each is handed to
3130 * one worker.
3131 *
3132 * The main thread waits on the condition that (at least) one of the workers
3133 * has stopped working (which is indicated in the .working member of
3134 * struct thread_params).
3135 *
3136 * When a work thread has completed its work, it sets .working to 0 and
3137 * signals the main thread and waits on the condition that .data_ready
3138 * becomes 1.
3139 *
3140 * The main thread steals half of the work from the worker that has
3141 * most work left to hand it to the idle worker.
3142 */
3143
3144struct thread_params {
3145 pthread_t thread;
3146 struct object_entry **list;
3147 struct packing_region *regions;
3148 unsigned list_size;
3149 unsigned remaining;
3150 int window;
3151 int depth;
3152 int working;
3153 int data_ready;
3154 pthread_mutex_t mutex;
3155 pthread_cond_t cond;
3156 unsigned *processed;
3157};
3158
3159static pthread_cond_t progress_cond;
3160
3161/*
3162 * Mutex and conditional variable can't be statically-initialized on Windows.
3163 */
3164static void init_threaded_search(void)
3165{
3166 pthread_mutex_init(&cache_mutex, NULL);
3167 pthread_mutex_init(&progress_mutex, NULL);
3168 pthread_cond_init(&progress_cond, NULL);
3169}
3170
3171static void cleanup_threaded_search(void)
3172{
3173 pthread_cond_destroy(&progress_cond);
3174 pthread_mutex_destroy(&cache_mutex);
3175 pthread_mutex_destroy(&progress_mutex);
3176}
3177
3178static void *threaded_find_deltas(void *arg)
3179{
3180 struct thread_params *me = arg;
3181
3182 progress_lock();
3183 while (me->remaining) {
3184 progress_unlock();
3185
3186 find_deltas(me->list, &me->remaining,
3187 me->window, me->depth, me->processed);
3188
3189 progress_lock();
3190 me->working = 0;
3191 pthread_cond_signal(&progress_cond);
3192 progress_unlock();
3193
3194 /*
3195 * We must not set ->data_ready before we wait on the
3196 * condition because the main thread may have set it to 1
3197 * before we get here. In order to be sure that new
3198 * work is available if we see 1 in ->data_ready, it
3199 * was initialized to 0 before this thread was spawned
3200 * and we reset it to 0 right away.
3201 */
3202 pthread_mutex_lock(&me->mutex);
3203 while (!me->data_ready)
3204 pthread_cond_wait(&me->cond, &me->mutex);
3205 me->data_ready = 0;
3206 pthread_mutex_unlock(&me->mutex);
3207
3208 progress_lock();
3209 }
3210 progress_unlock();
3211 /* leave ->working 1 so that this doesn't get more work assigned */
3212 return NULL;
3213}
3214
3215static void ll_find_deltas(struct object_entry **list, unsigned list_size,
3216 int window, int depth, unsigned *processed)
3217{
3218 struct thread_params *p;
3219 int i, ret, active_threads = 0;
3220
3221 init_threaded_search();
3222
3223 if (delta_search_threads <= 1) {
3224 find_deltas(list, &list_size, window, depth, processed);
3225 cleanup_threaded_search();
3226 return;
3227 }
3228 if (progress > pack_to_stdout)
3229 fprintf_ln(stderr, _("Delta compression using up to %d threads"),
3230 delta_search_threads);
3231 CALLOC_ARRAY(p, delta_search_threads);
3232
3233 /* Partition the work amongst work threads. */
3234 for (i = 0; i < delta_search_threads; i++) {
3235 unsigned sub_size = list_size / (delta_search_threads - i);
3236
3237 /* don't use too small segments or no deltas will be found */
3238 if (sub_size < 2*window && i+1 < delta_search_threads)
3239 sub_size = 0;
3240
3241 p[i].window = window;
3242 p[i].depth = depth;
3243 p[i].processed = processed;
3244 p[i].working = 1;
3245 p[i].data_ready = 0;
3246
3247 /* try to split chunks on "path" boundaries */
3248 while (sub_size && sub_size < list_size &&
3249 list[sub_size]->hash &&
3250 list[sub_size]->hash == list[sub_size-1]->hash)
3251 sub_size++;
3252
3253 p[i].list = list;
3254 p[i].list_size = sub_size;
3255 p[i].remaining = sub_size;
3256
3257 list += sub_size;
3258 list_size -= sub_size;
3259 }
3260
3261 /* Start work threads. */
3262 for (i = 0; i < delta_search_threads; i++) {
3263 if (!p[i].list_size)
3264 continue;
3265 pthread_mutex_init(&p[i].mutex, NULL);
3266 pthread_cond_init(&p[i].cond, NULL);
3267 ret = pthread_create(&p[i].thread, NULL,
3268 threaded_find_deltas, &p[i]);
3269 if (ret)
3270 die(_("unable to create thread: %s"), strerror(ret));
3271 active_threads++;
3272 }
3273
3274 /*
3275 * Now let's wait for work completion. Each time a thread is done
3276 * with its work, we steal half of the remaining work from the
3277 * thread with the largest number of unprocessed objects and give
3278 * it to that newly idle thread. This ensure good load balancing
3279 * until the remaining object list segments are simply too short
3280 * to be worth splitting anymore.
3281 */
3282 while (active_threads) {
3283 struct thread_params *target = NULL;
3284 struct thread_params *victim = NULL;
3285 unsigned sub_size = 0;
3286
3287 progress_lock();
3288 for (;;) {
3289 for (i = 0; !target && i < delta_search_threads; i++)
3290 if (!p[i].working)
3291 target = &p[i];
3292 if (target)
3293 break;
3294 pthread_cond_wait(&progress_cond, &progress_mutex);
3295 }
3296
3297 for (i = 0; i < delta_search_threads; i++)
3298 if (p[i].remaining > 2*window &&
3299 (!victim || victim->remaining < p[i].remaining))
3300 victim = &p[i];
3301 if (victim) {
3302 sub_size = victim->remaining / 2;
3303 list = victim->list + victim->list_size - sub_size;
3304 while (sub_size && list[0]->hash &&
3305 list[0]->hash == list[-1]->hash) {
3306 list++;
3307 sub_size--;
3308 }
3309 if (!sub_size) {
3310 /*
3311 * It is possible for some "paths" to have
3312 * so many objects that no hash boundary
3313 * might be found. Let's just steal the
3314 * exact half in that case.
3315 */
3316 sub_size = victim->remaining / 2;
3317 list -= sub_size;
3318 }
3319 target->list = list;
3320 victim->list_size -= sub_size;
3321 victim->remaining -= sub_size;
3322 }
3323 target->list_size = sub_size;
3324 target->remaining = sub_size;
3325 target->working = 1;
3326 progress_unlock();
3327
3328 pthread_mutex_lock(&target->mutex);
3329 target->data_ready = 1;
3330 pthread_cond_signal(&target->cond);
3331 pthread_mutex_unlock(&target->mutex);
3332
3333 if (!sub_size) {
3334 pthread_join(target->thread, NULL);
3335 pthread_cond_destroy(&target->cond);
3336 pthread_mutex_destroy(&target->mutex);
3337 active_threads--;
3338 }
3339 }
3340 cleanup_threaded_search();
3341 free(p);
3342}
3343
3344static int obj_is_packed(const struct object_id *oid)
3345{
3346 return packlist_find(&to_pack, oid) ||
3347 (reuse_packfile_bitmap &&
3348 bitmap_walk_contains(bitmap_git, reuse_packfile_bitmap, oid));
3349}
3350
3351static void add_tag_chain(const struct object_id *oid)
3352{
3353 struct tag *tag;
3354
3355 /*
3356 * We catch duplicates already in add_object_entry(), but we'd
3357 * prefer to do this extra check to avoid having to parse the
3358 * tag at all if we already know that it's being packed (e.g., if
3359 * it was included via bitmaps, we would not have parsed it
3360 * previously).
3361 */
3362 if (obj_is_packed(oid))
3363 return;
3364
3365 tag = lookup_tag(the_repository, oid);
3366 while (1) {
3367 if (!tag || parse_tag(the_repository, tag) || !tag->tagged)
3368 die(_("unable to pack objects reachable from tag %s"),
3369 oid_to_hex(oid));
3370
3371 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
3372
3373 if (tag->tagged->type != OBJ_TAG)
3374 return;
3375
3376 tag = (struct tag *)tag->tagged;
3377 }
3378}
3379
3380static int add_ref_tag(const struct reference *ref, void *cb_data UNUSED)
3381{
3382 struct object_id peeled;
3383
3384 if (!reference_get_peeled_oid(the_repository, ref, &peeled) &&
3385 obj_is_packed(&peeled))
3386 add_tag_chain(ref->oid);
3387 return 0;
3388}
3389
3390static int should_attempt_deltas(struct object_entry *entry)
3391{
3392 if (DELTA(entry))
3393 /* This happens if we decided to reuse existing
3394 * delta from a pack. "reuse_delta &&" is implied.
3395 */
3396 return 0;
3397
3398 if (!entry->type_valid ||
3399 oe_size_less_than(&to_pack, entry, 50))
3400 return 0;
3401
3402 if (entry->no_try_delta)
3403 return 0;
3404
3405 if (!entry->preferred_base) {
3406 if (oe_type(entry) < 0)
3407 die(_("unable to get type of object %s"),
3408 oid_to_hex(&entry->idx.oid));
3409 } else if (oe_type(entry) < 0) {
3410 /*
3411 * This object is not found, but we
3412 * don't have to include it anyway.
3413 */
3414 return 0;
3415 }
3416
3417 return 1;
3418}
3419
3420static void find_deltas_for_region(struct object_entry *list,
3421 struct packing_region *region,
3422 unsigned int *processed)
3423{
3424 struct object_entry **delta_list;
3425 unsigned int delta_list_nr = 0;
3426
3427 ALLOC_ARRAY(delta_list, region->nr);
3428 for (size_t i = 0; i < region->nr; i++) {
3429 struct object_entry *entry = list + region->start + i;
3430 if (should_attempt_deltas(entry))
3431 delta_list[delta_list_nr++] = entry;
3432 }
3433
3434 QSORT(delta_list, delta_list_nr, type_size_sort);
3435 find_deltas(delta_list, &delta_list_nr, window, depth, processed);
3436 free(delta_list);
3437}
3438
3439static void find_deltas_by_region(struct object_entry *list,
3440 struct packing_region *regions,
3441 size_t start, size_t nr)
3442{
3443 unsigned int processed = 0;
3444 size_t progress_nr;
3445
3446 if (!nr)
3447 return;
3448
3449 progress_nr = regions[nr - 1].start + regions[nr - 1].nr;
3450
3451 if (progress)
3452 progress_state = start_progress(the_repository,
3453 _("Compressing objects by path"),
3454 progress_nr);
3455
3456 while (nr--)
3457 find_deltas_for_region(list,
3458 &regions[start++],
3459 &processed);
3460
3461 display_progress(progress_state, progress_nr);
3462 stop_progress(&progress_state);
3463}
3464
3465static void *threaded_find_deltas_by_path(void *arg)
3466{
3467 struct thread_params *me = arg;
3468
3469 progress_lock();
3470 while (me->remaining) {
3471 while (me->remaining) {
3472 progress_unlock();
3473 find_deltas_for_region(to_pack.objects,
3474 me->regions,
3475 me->processed);
3476 progress_lock();
3477 me->remaining--;
3478 me->regions++;
3479 }
3480
3481 me->working = 0;
3482 pthread_cond_signal(&progress_cond);
3483 progress_unlock();
3484
3485 /*
3486 * We must not set ->data_ready before we wait on the
3487 * condition because the main thread may have set it to 1
3488 * before we get here. In order to be sure that new
3489 * work is available if we see 1 in ->data_ready, it
3490 * was initialized to 0 before this thread was spawned
3491 * and we reset it to 0 right away.
3492 */
3493 pthread_mutex_lock(&me->mutex);
3494 while (!me->data_ready)
3495 pthread_cond_wait(&me->cond, &me->mutex);
3496 me->data_ready = 0;
3497 pthread_mutex_unlock(&me->mutex);
3498
3499 progress_lock();
3500 }
3501 progress_unlock();
3502 /* leave ->working 1 so that this doesn't get more work assigned */
3503 return NULL;
3504}
3505
3506static void ll_find_deltas_by_region(struct object_entry *list,
3507 struct packing_region *regions,
3508 uint32_t start, uint32_t nr)
3509{
3510 struct thread_params *p;
3511 int i, ret, active_threads = 0;
3512 unsigned int processed = 0;
3513 uint32_t progress_nr;
3514 init_threaded_search();
3515
3516 if (!nr)
3517 return;
3518
3519 progress_nr = regions[nr - 1].start + regions[nr - 1].nr;
3520 if (delta_search_threads <= 1) {
3521 find_deltas_by_region(list, regions, start, nr);
3522 cleanup_threaded_search();
3523 return;
3524 }
3525
3526 if (progress > pack_to_stdout)
3527 fprintf_ln(stderr,
3528 Q_("Path-based delta compression using up to %d thread",
3529 "Path-based delta compression using up to %d threads",
3530 delta_search_threads),
3531 delta_search_threads);
3532 CALLOC_ARRAY(p, delta_search_threads);
3533
3534 if (progress)
3535 progress_state = start_progress(the_repository,
3536 _("Compressing objects by path"),
3537 progress_nr);
3538 /* Partition the work amongst work threads. */
3539 for (i = 0; i < delta_search_threads; i++) {
3540 unsigned sub_size = nr / (delta_search_threads - i);
3541
3542 p[i].window = window;
3543 p[i].depth = depth;
3544 p[i].processed = &processed;
3545 p[i].working = 1;
3546 p[i].data_ready = 0;
3547
3548 p[i].regions = regions;
3549 p[i].list_size = sub_size;
3550 p[i].remaining = sub_size;
3551
3552 regions += sub_size;
3553 nr -= sub_size;
3554 }
3555
3556 /* Start work threads. */
3557 for (i = 0; i < delta_search_threads; i++) {
3558 if (!p[i].list_size)
3559 continue;
3560 pthread_mutex_init(&p[i].mutex, NULL);
3561 pthread_cond_init(&p[i].cond, NULL);
3562 ret = pthread_create(&p[i].thread, NULL,
3563 threaded_find_deltas_by_path, &p[i]);
3564 if (ret)
3565 die(_("unable to create thread: %s"), strerror(ret));
3566 active_threads++;
3567 }
3568
3569 /*
3570 * Now let's wait for work completion. Each time a thread is done
3571 * with its work, we steal half of the remaining work from the
3572 * thread with the largest number of unprocessed objects and give
3573 * it to that newly idle thread. This ensure good load balancing
3574 * until the remaining object list segments are simply too short
3575 * to be worth splitting anymore.
3576 */
3577 while (active_threads) {
3578 struct thread_params *target = NULL;
3579 struct thread_params *victim = NULL;
3580 unsigned sub_size = 0;
3581
3582 progress_lock();
3583 for (;;) {
3584 for (i = 0; !target && i < delta_search_threads; i++)
3585 if (!p[i].working)
3586 target = &p[i];
3587 if (target)
3588 break;
3589 pthread_cond_wait(&progress_cond, &progress_mutex);
3590 }
3591
3592 for (i = 0; i < delta_search_threads; i++)
3593 if (p[i].remaining > 2*window &&
3594 (!victim || victim->remaining < p[i].remaining))
3595 victim = &p[i];
3596 if (victim) {
3597 sub_size = victim->remaining / 2;
3598 target->regions = victim->regions + victim->remaining - sub_size;
3599 victim->list_size -= sub_size;
3600 victim->remaining -= sub_size;
3601 }
3602 target->list_size = sub_size;
3603 target->remaining = sub_size;
3604 target->working = 1;
3605 progress_unlock();
3606
3607 pthread_mutex_lock(&target->mutex);
3608 target->data_ready = 1;
3609 pthread_cond_signal(&target->cond);
3610 pthread_mutex_unlock(&target->mutex);
3611
3612 if (!sub_size) {
3613 pthread_join(target->thread, NULL);
3614 pthread_cond_destroy(&target->cond);
3615 pthread_mutex_destroy(&target->mutex);
3616 active_threads--;
3617 }
3618 }
3619 cleanup_threaded_search();
3620 free(p);
3621
3622 display_progress(progress_state, progress_nr);
3623 stop_progress(&progress_state);
3624}
3625
3626static void prepare_pack(int window, int depth)
3627{
3628 struct object_entry **delta_list;
3629 uint32_t i, nr_deltas;
3630 unsigned n;
3631
3632 if (use_delta_islands)
3633 resolve_tree_islands(the_repository, progress, &to_pack);
3634
3635 get_object_details();
3636
3637 /*
3638 * If we're locally repacking then we need to be doubly careful
3639 * from now on in order to make sure no stealth corruption gets
3640 * propagated to the new pack. Clients receiving streamed packs
3641 * should validate everything they get anyway so no need to incur
3642 * the additional cost here in that case.
3643 */
3644 if (!pack_to_stdout)
3645 do_check_packed_object_crc = 1;
3646
3647 if (!to_pack.nr_objects || !window || !depth)
3648 return;
3649
3650 if (path_walk)
3651 ll_find_deltas_by_region(to_pack.objects, to_pack.regions,
3652 0, to_pack.nr_regions);
3653
3654 ALLOC_ARRAY(delta_list, to_pack.nr_objects);
3655 nr_deltas = n = 0;
3656
3657 for (i = 0; i < to_pack.nr_objects; i++) {
3658 struct object_entry *entry = to_pack.objects + i;
3659
3660 if (!should_attempt_deltas(entry))
3661 continue;
3662
3663 if (!entry->preferred_base)
3664 nr_deltas++;
3665
3666 delta_list[n++] = entry;
3667 }
3668
3669 if (nr_deltas && n > 1) {
3670 unsigned nr_done = 0;
3671
3672 if (progress)
3673 progress_state = start_progress(the_repository,
3674 _("Compressing objects"),
3675 nr_deltas);
3676 QSORT(delta_list, n, type_size_sort);
3677 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
3678 stop_progress(&progress_state);
3679 if (nr_done != nr_deltas)
3680 die(_("inconsistency with delta count"));
3681 }
3682 free(delta_list);
3683}
3684
3685static int git_pack_config(const char *k, const char *v,
3686 const struct config_context *ctx, void *cb)
3687{
3688 if (!strcmp(k, "pack.window")) {
3689 window = git_config_int(k, v, ctx->kvi);
3690 return 0;
3691 }
3692 if (!strcmp(k, "pack.windowmemory")) {
3693 window_memory_limit = git_config_ulong(k, v, ctx->kvi);
3694 return 0;
3695 }
3696 if (!strcmp(k, "pack.depth")) {
3697 depth = git_config_int(k, v, ctx->kvi);
3698 return 0;
3699 }
3700 if (!strcmp(k, "pack.deltacachesize")) {
3701 max_delta_cache_size = git_config_size_t(k, v, ctx->kvi);
3702 return 0;
3703 }
3704 if (!strcmp(k, "pack.deltacachelimit")) {
3705 cache_max_small_delta_size = git_config_int(k, v, ctx->kvi);
3706 return 0;
3707 }
3708 if (!strcmp(k, "pack.writebitmaphashcache")) {
3709 if (git_config_bool(k, v))
3710 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
3711 else
3712 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
3713 }
3714
3715 if (!strcmp(k, "pack.writebitmaplookuptable")) {
3716 if (git_config_bool(k, v))
3717 write_bitmap_options |= BITMAP_OPT_LOOKUP_TABLE;
3718 else
3719 write_bitmap_options &= ~BITMAP_OPT_LOOKUP_TABLE;
3720 }
3721
3722 if (!strcmp(k, "pack.usebitmaps")) {
3723 use_bitmap_index_default = git_config_bool(k, v);
3724 return 0;
3725 }
3726 if (!strcmp(k, "pack.allowpackreuse")) {
3727 int res = git_parse_maybe_bool_text(v);
3728 if (res < 0) {
3729 if (!strcasecmp(v, "single"))
3730 allow_pack_reuse = SINGLE_PACK_REUSE;
3731 else if (!strcasecmp(v, "multi"))
3732 allow_pack_reuse = MULTI_PACK_REUSE;
3733 else
3734 die(_("invalid pack.allowPackReuse value: '%s'"), v);
3735 } else if (res) {
3736 allow_pack_reuse = SINGLE_PACK_REUSE;
3737 } else {
3738 allow_pack_reuse = NO_PACK_REUSE;
3739 }
3740 return 0;
3741 }
3742 if (!strcmp(k, "pack.threads")) {
3743 delta_search_threads = git_config_int(k, v, ctx->kvi);
3744 if (delta_search_threads < 0)
3745 die(_("invalid number of threads specified (%d)"),
3746 delta_search_threads);
3747 if (!HAVE_THREADS && delta_search_threads != 1) {
3748 warning(_("no threads support, ignoring %s"), k);
3749 delta_search_threads = 0;
3750 }
3751 return 0;
3752 }
3753 if (!strcmp(k, "pack.indexversion")) {
3754 pack_idx_opts.version = git_config_int(k, v, ctx->kvi);
3755 if (pack_idx_opts.version > 2)
3756 die(_("bad pack.indexVersion=%"PRIu32),
3757 pack_idx_opts.version);
3758 return 0;
3759 }
3760 if (!strcmp(k, "pack.writereverseindex")) {
3761 if (git_config_bool(k, v))
3762 pack_idx_opts.flags |= WRITE_REV;
3763 else
3764 pack_idx_opts.flags &= ~WRITE_REV;
3765 return 0;
3766 }
3767 if (!strcmp(k, "uploadpack.blobpackfileuri")) {
3768 struct configured_exclusion *ex;
3769 const char *oid_end, *pack_end;
3770 /*
3771 * Stores the pack hash. This is not a true object ID, but is
3772 * of the same form.
3773 */
3774 struct object_id pack_hash;
3775
3776 if (!v)
3777 return config_error_nonbool(k);
3778
3779 ex = xmalloc(sizeof(*ex));
3780 if (parse_oid_hex(v, &ex->e.oid, &oid_end) ||
3781 *oid_end != ' ' ||
3782 parse_oid_hex(oid_end + 1, &pack_hash, &pack_end) ||
3783 *pack_end != ' ')
3784 die(_("value of uploadpack.blobpackfileuri must be "
3785 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v);
3786 if (oidmap_get(&configured_exclusions, &ex->e.oid))
3787 die(_("object already configured in another "
3788 "uploadpack.blobpackfileuri (got '%s')"), v);
3789 ex->pack_hash_hex = xcalloc(1, pack_end - oid_end);
3790 memcpy(ex->pack_hash_hex, oid_end + 1, pack_end - oid_end - 1);
3791 ex->uri = xstrdup(pack_end + 1);
3792 oidmap_put(&configured_exclusions, ex);
3793 }
3794 return git_default_config(k, v, ctx, cb);
3795}
3796
3797/* Counters for trace2 output when in --stdin-packs mode. */
3798static int stdin_packs_found_nr;
3799static int stdin_packs_hints_nr;
3800
3801static int add_object_entry_from_pack(const struct object_id *oid,
3802 struct packed_git *p,
3803 uint32_t pos,
3804 void *_data)
3805{
3806 off_t ofs;
3807 struct object_info oi = OBJECT_INFO_INIT;
3808 enum object_type type = OBJ_NONE;
3809
3810 display_progress(progress_state, ++nr_seen);
3811
3812 if (have_duplicate_entry(oid, 0))
3813 return 0;
3814
3815 stdin_packs_found_nr++;
3816
3817 ofs = nth_packed_object_offset(p, pos);
3818
3819 oi.typep = &type;
3820 if (packed_object_info(NULL, p, ofs, &oi) < 0) {
3821 die(_("could not get type of object %s in pack %s"),
3822 oid_to_hex(oid), p->pack_name);
3823 } else if (type == OBJ_COMMIT) {
3824 struct rev_info *revs = _data;
3825 /*
3826 * commits in included packs are used as starting points
3827 * for the subsequent revision walk
3828 *
3829 * Note that we do want to walk through commits that are
3830 * present in excluded-open ('!') packs to pick up any
3831 * objects reachable from them not present in the
3832 * excluded-closed ('^') packs.
3833 *
3834 * However, we'll only add those objects to the packing
3835 * list after checking `want_object_in_pack()` below.
3836 */
3837 add_pending_oid(revs, NULL, oid, 0);
3838 }
3839
3840 if (!want_object_in_pack(oid, 0, &p, &ofs))
3841 return 0;
3842
3843 create_object_entry(oid, type, 0, 0, 0, p, ofs);
3844
3845 return 0;
3846}
3847
3848static void show_object_pack_hint(struct object *object, const char *name,
3849 void *data)
3850{
3851 enum stdin_packs_mode mode = *(enum stdin_packs_mode *)data;
3852 if (mode == STDIN_PACKS_MODE_FOLLOW) {
3853 if (object->type == OBJ_BLOB &&
3854 !odb_has_object(the_repository->objects, &object->oid, 0))
3855 return;
3856 add_object_entry(&object->oid, object->type, name, 0);
3857 } else {
3858 struct object_entry *oe = packlist_find(&to_pack, &object->oid);
3859 if (!oe)
3860 return;
3861
3862 /*
3863 * Our 'to_pack' list was constructed by iterating all
3864 * objects packed in included packs, and so doesn't have
3865 * a non-zero hash field that you would typically pick
3866 * up during a reachability traversal.
3867 *
3868 * Make a best-effort attempt to fill in the ->hash and
3869 * ->no_try_delta fields here in order to perhaps
3870 * improve the delta selection process.
3871 */
3872 oe->hash = pack_name_hash_fn(name);
3873 oe->no_try_delta = name && no_try_delta(name);
3874
3875 stdin_packs_hints_nr++;
3876 }
3877}
3878
3879static void show_commit_pack_hint(struct commit *commit, void *data)
3880{
3881 enum stdin_packs_mode mode = *(enum stdin_packs_mode *)data;
3882
3883 if (mode == STDIN_PACKS_MODE_FOLLOW) {
3884 show_object_pack_hint((struct object *)commit, "", data);
3885 return;
3886 }
3887
3888 /* nothing to do; commits don't have a namehash */
3889
3890}
3891
3892/*
3893 * stdin_pack_info_kind specifies how a pack specified over stdin
3894 * should be treated when pack-objects is invoked with --stdin-packs.
3895 *
3896 * - STDIN_PACK_INCLUDE: objects in any packs with this flag bit set
3897 * should be included in the output pack, unless they appear in an
3898 * excluded pack.
3899 *
3900 * - STDIN_PACK_EXCLUDE_CLOSED: objects in any packs with this flag
3901 * bit set should be excluded from the output pack.
3902 *
3903 * - STDIN_PACK_EXCLUDE_OPEN: objects in any packs with this flag
3904 * bit set should be excluded from the output pack, but are not
3905 * guaranteed to be closed under reachability.
3906 *
3907 * Objects in packs whose 'kind' bits include STDIN_PACK_INCLUDE or
3908 * STDIN_PACK_EXCLUDE_OPEN are used as traversal tips when invoked
3909 * with --stdin-packs=follow.
3910 */
3911enum stdin_pack_info_kind {
3912 STDIN_PACK_INCLUDE = (1<<0),
3913 STDIN_PACK_EXCLUDE_CLOSED = (1<<1),
3914 STDIN_PACK_EXCLUDE_OPEN = (1<<2),
3915};
3916
3917struct stdin_pack_info {
3918 struct packed_git *p;
3919 enum stdin_pack_info_kind kind;
3920};
3921
3922static int pack_mtime_cmp(const void *_a, const void *_b)
3923{
3924 struct stdin_pack_info *a = ((const struct string_list_item*)_a)->util;
3925 struct stdin_pack_info *b = ((const struct string_list_item*)_b)->util;
3926
3927 /*
3928 * order packs by descending mtime so that objects are laid out
3929 * roughly as newest-to-oldest
3930 */
3931 if (a->p->mtime < b->p->mtime)
3932 return 1;
3933 else if (b->p->mtime < a->p->mtime)
3934 return -1;
3935 else
3936 return 0;
3937}
3938
3939static int stdin_packs_include_check_obj(struct object *obj, void *data UNUSED)
3940{
3941 return !has_object_kept_pack(to_pack.repo, &obj->oid,
3942 KEPT_PACK_IN_CORE);
3943}
3944
3945static int stdin_packs_include_check(struct commit *commit, void *data)
3946{
3947 return stdin_packs_include_check_obj((struct object *)commit, data);
3948}
3949
3950static void stdin_packs_add_pack_entries(struct strmap *packs,
3951 struct rev_info *revs)
3952{
3953 struct string_list keys = STRING_LIST_INIT_NODUP;
3954 struct string_list_item *item;
3955 struct hashmap_iter iter;
3956 struct strmap_entry *entry;
3957
3958 strmap_for_each_entry(packs, &iter, entry) {
3959 struct stdin_pack_info *info = entry->value;
3960 if (!info->p)
3961 die(_("could not find pack '%s'"), entry->key);
3962
3963 string_list_append(&keys, entry->key)->util = info;
3964 }
3965
3966 /*
3967 * Order packs by ascending mtime; use QSORT directly to access the
3968 * string_list_item's ->util pointer, which string_list_sort() does not
3969 * provide.
3970 */
3971 QSORT(keys.items, keys.nr, pack_mtime_cmp);
3972
3973 for_each_string_list_item(item, &keys) {
3974 struct stdin_pack_info *info = item->util;
3975
3976 if (info->kind & STDIN_PACK_EXCLUDE_OPEN) {
3977 /*
3978 * When open-excluded packs ("!") are present, stop
3979 * the parent walk at closed-excluded ("^") packs.
3980 * Objects behind a "^" boundary are guaranteed to
3981 * have closure and should not be rescued.
3982 */
3983 revs->include_check = stdin_packs_include_check;
3984 revs->include_check_obj = stdin_packs_include_check_obj;
3985 }
3986
3987 if ((info->kind & STDIN_PACK_INCLUDE) ||
3988 (info->kind & STDIN_PACK_EXCLUDE_OPEN))
3989 for_each_object_in_pack(info->p,
3990 add_object_entry_from_pack,
3991 revs,
3992 ODB_FOR_EACH_OBJECT_PACK_ORDER);
3993 }
3994
3995 string_list_clear(&keys, 0);
3996}
3997
3998static void stdin_packs_read_input(struct rev_info *revs,
3999 enum stdin_packs_mode mode)
4000{
4001 struct strbuf buf = STRBUF_INIT;
4002 struct strmap packs = STRMAP_INIT;
4003 struct packed_git *p;
4004
4005 while (strbuf_getline(&buf, stdin) != EOF) {
4006 struct stdin_pack_info *info;
4007 enum stdin_pack_info_kind kind = STDIN_PACK_INCLUDE;
4008 const char *key = buf.buf;
4009
4010 if (!*key)
4011 continue;
4012 else if (*key == '^')
4013 kind = STDIN_PACK_EXCLUDE_CLOSED;
4014 else if (*key == '!' && mode == STDIN_PACKS_MODE_FOLLOW)
4015 kind = STDIN_PACK_EXCLUDE_OPEN;
4016
4017 if (kind != STDIN_PACK_INCLUDE)
4018 key++;
4019
4020 info = strmap_get(&packs, key);
4021 if (!info) {
4022 CALLOC_ARRAY(info, 1);
4023 strmap_put(&packs, key, info);
4024 }
4025
4026 info->kind |= kind;
4027
4028 strbuf_reset(&buf);
4029 }
4030
4031 repo_for_each_pack(the_repository, p) {
4032 struct stdin_pack_info *info;
4033
4034 info = strmap_get(&packs, pack_basename(p));
4035 if (!info)
4036 continue;
4037
4038 if (info->kind & STDIN_PACK_INCLUDE) {
4039 if (exclude_promisor_objects && p->pack_promisor)
4040 die(_("packfile %s is a promisor but --exclude-promisor-objects was given"), p->pack_name);
4041
4042 /*
4043 * Arguments we got on stdin may not even be
4044 * packs. First check that to avoid segfaulting
4045 * later on in e.g. pack_mtime_cmp(), excluded
4046 * packs are handled below.
4047 */
4048 if (!is_pack_valid(p))
4049 die(_("packfile %s cannot be accessed"), p->pack_name);
4050 }
4051
4052 if (info->kind & STDIN_PACK_EXCLUDE_CLOSED) {
4053 /*
4054 * Marking excluded packs as kept in-core so
4055 * that later calls to add_object_entry()
4056 * discards any objects that are also found in
4057 * excluded packs.
4058 */
4059 p->pack_keep_in_core = 1;
4060 }
4061
4062 if (info->kind & STDIN_PACK_EXCLUDE_OPEN) {
4063 /*
4064 * Marking excluded open packs as kept in-core
4065 * (open) for the same reason as we marked
4066 * exclude closed packs as kept in-core.
4067 *
4068 * Use a separate flag here to ensure we don't
4069 * halt our traversal at these packs, since they
4070 * are not guaranteed to have closure.
4071 *
4072 */
4073 p->pack_keep_in_core_open = 1;
4074 }
4075
4076 info->p = p;
4077 }
4078
4079 stdin_packs_add_pack_entries(&packs, revs);
4080
4081 strbuf_release(&buf);
4082 strmap_clear(&packs, 1);
4083}
4084
4085static void add_unreachable_loose_objects(struct rev_info *revs);
4086
4087static void read_stdin_packs(struct repository *repo,
4088 enum stdin_packs_mode mode, int rev_list_unpacked)
4089{
4090 int prev_fetch_if_missing = repo->fetch_if_missing;
4091 struct rev_info revs;
4092
4093 /*
4094 * The revision walk may hit objects that are promised, only. As the
4095 * walk is best-effort though we don't want to perform backfill fetches
4096 * for them.
4097 */
4098 repo->fetch_if_missing = 0;
4099
4100 repo_init_revisions(repo, &revs, NULL);
4101 /*
4102 * Use a revision walk to fill in the namehash of objects in the include
4103 * packs. To save time, we'll avoid traversing through objects that are
4104 * in excluded packs.
4105 *
4106 * That may cause us to avoid populating all of the namehash fields of
4107 * all included objects, but our goal is best-effort, since this is only
4108 * an optimization during delta selection.
4109 */
4110 revs.no_kept_objects = 1;
4111 revs.keep_pack_cache_flags |= KEPT_PACK_IN_CORE;
4112 revs.blob_objects = 1;
4113 revs.tree_objects = 1;
4114 revs.tag_objects = 1;
4115 revs.ignore_missing_links = 1;
4116 revs.exclude_promisor_objects = exclude_promisor_objects;
4117
4118 /* avoids adding objects in excluded packs */
4119 ignore_packed_keep_in_core = 1;
4120 if (mode == STDIN_PACKS_MODE_FOLLOW) {
4121 /*
4122 * In '--stdin-packs=follow' mode, additionally ignore
4123 * objects in excluded-open packs to prevent them from
4124 * appearing in the resulting pack.
4125 */
4126 ignore_packed_keep_in_core_open = 1;
4127 }
4128 stdin_packs_read_input(&revs, mode);
4129 if (rev_list_unpacked)
4130 add_unreachable_loose_objects(&revs);
4131
4132 if (prepare_revision_walk(&revs))
4133 die(_("revision walk setup failed"));
4134 traverse_commit_list(&revs,
4135 show_commit_pack_hint,
4136 show_object_pack_hint,
4137 &mode);
4138
4139 release_revisions(&revs);
4140
4141 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_found",
4142 stdin_packs_found_nr);
4143 trace2_data_intmax("pack-objects", the_repository, "stdin_packs_hints",
4144 stdin_packs_hints_nr);
4145
4146 repo->fetch_if_missing = prev_fetch_if_missing;
4147}
4148
4149static void add_cruft_object_entry(const struct object_id *oid, enum object_type type,
4150 struct packed_git *pack, off_t offset,
4151 const char *name, uint32_t mtime)
4152{
4153 struct object_entry *entry;
4154
4155 display_progress(progress_state, ++nr_seen);
4156
4157 entry = packlist_find(&to_pack, oid);
4158 if (entry) {
4159 if (name) {
4160 entry->hash = pack_name_hash_fn(name);
4161 entry->no_try_delta = no_try_delta(name);
4162 }
4163 } else {
4164 if (!want_object_in_pack_mtime(oid, 0, &pack, &offset, mtime))
4165 return;
4166 if (!pack && type == OBJ_BLOB) {
4167 struct odb_source_files *files =
4168 odb_source_files_downcast(the_repository->objects->source);
4169 int found = 0;
4170
4171 for (struct odb_files_dir *dir = files->dirs; !found && dir; dir = dir->next)
4172 if (!odb_source_read_object_info(&dir->loose->base, oid, NULL, 0, NULL))
4173 found = 1;
4174
4175 /*
4176 * If a traversed tree has a missing blob then we want
4177 * to avoid adding that missing object to our pack.
4178 *
4179 * This only applies to missing blobs, not trees,
4180 * because the traversal needs to parse sub-trees but
4181 * not blobs.
4182 *
4183 * Note we only perform this check when we couldn't
4184 * already find the object in a pack, so we're really
4185 * limited to "ensure non-tip blobs which don't exist in
4186 * packs do exist via loose objects". Confused?
4187 */
4188 if (!found)
4189 return;
4190 }
4191
4192 entry = create_object_entry(oid, type, pack_name_hash_fn(name),
4193 0, name && no_try_delta(name),
4194 pack, offset);
4195 }
4196
4197 if (mtime > oe_cruft_mtime(&to_pack, entry))
4198 oe_set_cruft_mtime(&to_pack, entry, mtime);
4199 return;
4200}
4201
4202static void show_cruft_object(struct object *obj, const char *name, void *data UNUSED)
4203{
4204 /*
4205 * if we did not record it earlier, it's at least as old as our
4206 * expiration value. Rather than find it exactly, just use that
4207 * value. This may bump it forward from its real mtime, but it
4208 * will still be "too old" next time we run with the same
4209 * expiration.
4210 *
4211 * if obj does appear in the packing list, this call is a noop (or may
4212 * set the namehash).
4213 */
4214 add_cruft_object_entry(&obj->oid, obj->type, NULL, 0, name, cruft_expiration);
4215}
4216
4217static void show_cruft_commit(struct commit *commit, void *data)
4218{
4219 show_cruft_object((struct object*)commit, NULL, data);
4220}
4221
4222static int cruft_include_check_obj(struct object *obj, void *data UNUSED)
4223{
4224 return !has_object_kept_pack(to_pack.repo, &obj->oid, KEPT_PACK_IN_CORE);
4225}
4226
4227static int cruft_include_check(struct commit *commit, void *data)
4228{
4229 return cruft_include_check_obj((struct object*)commit, data);
4230}
4231
4232static void set_cruft_mtime(const struct object *object,
4233 struct packed_git *pack,
4234 off_t offset, time_t mtime)
4235{
4236 add_cruft_object_entry(&object->oid, object->type, pack, offset, NULL,
4237 mtime);
4238}
4239
4240static void mark_pack_kept_in_core(struct string_list *packs, unsigned keep)
4241{
4242 struct string_list_item *item = NULL;
4243 for_each_string_list_item(item, packs) {
4244 struct packed_git *p = item->util;
4245 if (!p)
4246 die(_("could not find pack '%s'"), item->string);
4247 if (p->is_cruft && keep)
4248 ignore_packed_keep_in_core_has_cruft = 1;
4249 p->pack_keep_in_core = keep;
4250 }
4251}
4252
4253static void add_objects_in_unpacked_packs(void);
4254
4255static void enumerate_cruft_objects(void)
4256{
4257 if (progress)
4258 progress_state = start_progress(the_repository,
4259 _("Enumerating cruft objects"), 0);
4260
4261 add_objects_in_unpacked_packs();
4262 add_unreachable_loose_objects(NULL);
4263
4264 stop_progress(&progress_state);
4265}
4266
4267static void enumerate_and_traverse_cruft_objects(struct string_list *fresh_packs)
4268{
4269 struct packed_git *p;
4270 struct rev_info revs;
4271 int ret;
4272
4273 repo_init_revisions(the_repository, &revs, NULL);
4274
4275 revs.tag_objects = 1;
4276 revs.tree_objects = 1;
4277 revs.blob_objects = 1;
4278
4279 revs.include_check = cruft_include_check;
4280 revs.include_check_obj = cruft_include_check_obj;
4281
4282 revs.ignore_missing_links = 1;
4283
4284 if (progress)
4285 progress_state = start_progress(the_repository,
4286 _("Enumerating cruft objects"), 0);
4287 ret = add_unseen_recent_objects_to_traversal(&revs, cruft_expiration,
4288 set_cruft_mtime, 1);
4289 stop_progress(&progress_state);
4290
4291 if (ret)
4292 die(_("unable to add cruft objects"));
4293
4294 /*
4295 * Re-mark only the fresh packs as kept so that objects in
4296 * unknown packs do not halt the reachability traversal early.
4297 */
4298 repo_for_each_pack(the_repository, p)
4299 p->pack_keep_in_core = 0;
4300 mark_pack_kept_in_core(fresh_packs, 1);
4301
4302 if (prepare_revision_walk(&revs))
4303 die(_("revision walk setup failed"));
4304 if (progress)
4305 progress_state = start_progress(the_repository,
4306 _("Traversing cruft objects"), 0);
4307 nr_seen = 0;
4308 traverse_commit_list(&revs, show_cruft_commit, show_cruft_object, NULL);
4309
4310 stop_progress(&progress_state);
4311 release_revisions(&revs);
4312}
4313
4314static void read_cruft_objects(void)
4315{
4316 struct strbuf buf = STRBUF_INIT;
4317 struct string_list discard_packs = STRING_LIST_INIT_DUP;
4318 struct string_list fresh_packs = STRING_LIST_INIT_DUP;
4319 struct packed_git *p;
4320
4321 ignore_packed_keep_in_core = 1;
4322
4323 while (strbuf_getline(&buf, stdin) != EOF) {
4324 if (!buf.len)
4325 continue;
4326
4327 if (*buf.buf == '-')
4328 string_list_append(&discard_packs, buf.buf + 1);
4329 else
4330 string_list_append(&fresh_packs, buf.buf);
4331 }
4332
4333 string_list_sort(&discard_packs);
4334 string_list_sort(&fresh_packs);
4335
4336 repo_for_each_pack(the_repository, p) {
4337 const char *pack_name = pack_basename(p);
4338 struct string_list_item *item;
4339
4340 item = string_list_lookup(&fresh_packs, pack_name);
4341 if (!item)
4342 item = string_list_lookup(&discard_packs, pack_name);
4343
4344 if (item) {
4345 item->util = p;
4346 } else {
4347 /*
4348 * This pack wasn't mentioned in either the "fresh" or
4349 * "discard" list, so the caller didn't know about it.
4350 *
4351 * Mark it as kept so that its objects are ignored by
4352 * add_unseen_recent_objects_to_traversal(). We'll
4353 * unmark it before starting the traversal so it doesn't
4354 * halt the traversal early.
4355 */
4356 p->pack_keep_in_core = 1;
4357 }
4358 }
4359
4360 mark_pack_kept_in_core(&fresh_packs, 1);
4361 mark_pack_kept_in_core(&discard_packs, 0);
4362
4363 if (cruft_expiration)
4364 enumerate_and_traverse_cruft_objects(&fresh_packs);
4365 else
4366 enumerate_cruft_objects();
4367
4368 strbuf_release(&buf);
4369 string_list_clear(&discard_packs, 0);
4370 string_list_clear(&fresh_packs, 0);
4371}
4372
4373static void read_object_list_from_stdin(void)
4374{
4375 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
4376 struct object_id oid;
4377 const char *p;
4378
4379 for (;;) {
4380 if (!fgets(line, sizeof(line), stdin)) {
4381 if (feof(stdin))
4382 break;
4383 if (!ferror(stdin))
4384 BUG("fgets returned NULL, not EOF, not error!");
4385 if (errno != EINTR)
4386 die_errno("fgets");
4387 clearerr(stdin);
4388 continue;
4389 }
4390 if (line[0] == '-') {
4391 if (get_oid_hex(line+1, &oid))
4392 die(_("expected edge object ID, got garbage:\n %s"),
4393 line);
4394 add_preferred_base(&oid);
4395 continue;
4396 }
4397 if (parse_oid_hex(line, &oid, &p))
4398 die(_("expected object ID, got garbage:\n %s"), line);
4399
4400 add_preferred_base_object(p + 1);
4401 add_object_entry(&oid, OBJ_NONE, p + 1, 0);
4402 }
4403}
4404
4405static void show_commit(struct commit *commit, void *data UNUSED)
4406{
4407 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
4408
4409 if (write_bitmap_index)
4410 index_commit_for_bitmap(commit);
4411
4412 if (use_delta_islands)
4413 propagate_island_marks(the_repository, commit);
4414}
4415
4416static void show_object(struct object *obj, const char *name,
4417 void *data UNUSED)
4418{
4419 add_preferred_base_object(name);
4420 add_object_entry(&obj->oid, obj->type, name, 0);
4421
4422 if (use_delta_islands)
4423 record_tree_depth(&obj->oid, name);
4424}
4425
4426static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
4427{
4428 assert(arg_missing_action == MA_ALLOW_ANY);
4429
4430 /*
4431 * Quietly ignore ALL missing objects. This avoids problems with
4432 * staging them now and getting an odd error later.
4433 */
4434 if (!odb_has_object(the_repository->objects, &obj->oid, 0))
4435 return;
4436
4437 show_object(obj, name, data);
4438}
4439
4440static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
4441{
4442 assert(arg_missing_action == MA_ALLOW_PROMISOR);
4443
4444 /*
4445 * Quietly ignore EXPECTED missing objects. This avoids problems with
4446 * staging them now and getting an odd error later.
4447 */
4448 if (!odb_has_object(the_repository->objects, &obj->oid, 0) &&
4449 is_promisor_object(to_pack.repo, &obj->oid))
4450 return;
4451
4452 show_object(obj, name, data);
4453}
4454
4455static int option_parse_missing_action(const struct option *opt,
4456 const char *arg, int unset)
4457{
4458 struct repository *repo = opt->value;
4459
4460 assert(arg);
4461 assert(!unset);
4462
4463 if (!strcmp(arg, "error")) {
4464 arg_missing_action = MA_ERROR;
4465 fn_show_object = show_object;
4466 return 0;
4467 }
4468
4469 if (!strcmp(arg, "allow-any")) {
4470 arg_missing_action = MA_ALLOW_ANY;
4471 repo->fetch_if_missing = 0;
4472 fn_show_object = show_object__ma_allow_any;
4473 return 0;
4474 }
4475
4476 if (!strcmp(arg, "allow-promisor")) {
4477 arg_missing_action = MA_ALLOW_PROMISOR;
4478 repo->fetch_if_missing = 0;
4479 fn_show_object = show_object__ma_allow_promisor;
4480 return 0;
4481 }
4482
4483 die(_("invalid value for '%s': '%s'"), "--missing", arg);
4484 return 0;
4485}
4486
4487static void show_edge(struct commit *commit)
4488{
4489 add_preferred_base(&commit->object.oid);
4490}
4491
4492static int add_object_in_unpacked_pack(const struct object_id *oid,
4493 struct object_info *oi,
4494 void *data UNUSED)
4495{
4496 if (cruft) {
4497 add_cruft_object_entry(oid, OBJ_NONE, oi->source_infop->u.packed.pack,
4498 oi->source_infop->u.packed.offset, NULL,
4499 *oi->mtimep);
4500 } else {
4501 add_object_entry(oid, OBJ_NONE, "", 0);
4502 }
4503 return 0;
4504}
4505
4506static void add_objects_in_unpacked_packs(void)
4507{
4508 struct odb_source_files *files =
4509 odb_source_files_downcast(to_pack.repo->objects->source);
4510 time_t mtime;
4511 struct odb_for_each_object_options opts = {
4512 .flags = ODB_FOR_EACH_OBJECT_PACK_ORDER |
4513 ODB_FOR_EACH_OBJECT_LOCAL_ONLY |
4514 ODB_FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS |
4515 ODB_FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS,
4516 };
4517 struct odb_source_info source_info;
4518 struct object_info oi = {
4519 .mtimep = &mtime,
4520 .source_infop = &source_info,
4521 };
4522
4523 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
4524 if (!dir->local)
4525 continue;
4526
4527 if (odb_source_for_each_object(&dir->packed->base, &oi,
4528 add_object_in_unpacked_pack, NULL, &opts))
4529 die(_("cannot open pack index"));
4530 }
4531}
4532
4533static int add_loose_object(const struct object_id *oid, const char *path,
4534 void *data)
4535{
4536 struct rev_info *revs = data;
4537 enum object_type type = odb_read_object_info(the_repository->objects, oid, NULL);
4538
4539 if (type < 0) {
4540 warning(_("loose object at %s could not be examined"), path);
4541 return 0;
4542 }
4543
4544 if (cruft) {
4545 struct stat st;
4546 if (stat(path, &st) < 0) {
4547 if (errno == ENOENT)
4548 return 0;
4549 return error_errno("unable to stat %s", oid_to_hex(oid));
4550 }
4551
4552 add_cruft_object_entry(oid, type, NULL, 0, NULL,
4553 st.st_mtime);
4554 } else {
4555 add_object_entry(oid, type, "", 0);
4556 }
4557
4558 if (revs && type == OBJ_COMMIT)
4559 add_pending_oid(revs, NULL, oid, 0);
4560
4561 return 0;
4562}
4563
4564/*
4565 * We actually don't even have to worry about reachability here.
4566 * add_object_entry will weed out duplicates, so we just add every
4567 * loose object we find.
4568 */
4569static void add_unreachable_loose_objects(struct rev_info *revs)
4570{
4571 for_each_loose_file_in_source(the_repository->objects->source,
4572 add_loose_object, NULL, NULL, revs);
4573}
4574
4575static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
4576{
4577 static struct packed_git *last_found = NULL;
4578 struct packed_git *p;
4579
4580 if (last_found && find_pack_entry_one(oid, last_found))
4581 return 1;
4582
4583 repo_for_each_pack(the_repository, p) {
4584 /*
4585 * We have already checked `last_found`, so there is no need to
4586 * re-check here.
4587 */
4588 if (p == last_found)
4589 continue;
4590
4591 if ((!p->pack_local || p->pack_keep || p->pack_keep_in_core) &&
4592 find_pack_entry_one(oid, p)) {
4593 last_found = p;
4594 return 1;
4595 }
4596 }
4597
4598 return 0;
4599}
4600
4601/*
4602 * Store a list of sha1s that are should not be discarded
4603 * because they are either written too recently, or are
4604 * reachable from another object that was.
4605 *
4606 * This is filled by get_object_list.
4607 */
4608static struct oid_array recent_objects;
4609
4610static int loosened_object_can_be_discarded(const struct object_id *oid,
4611 timestamp_t mtime)
4612{
4613 if (!unpack_unreachable_expiration)
4614 return 0;
4615 if (mtime > unpack_unreachable_expiration)
4616 return 0;
4617 if (oid_array_lookup(&recent_objects, oid) >= 0)
4618 return 0;
4619 return 1;
4620}
4621
4622static int force_object_loose(struct odb_source *source,
4623 const struct object_id *oid,
4624 const time_t *mtime)
4625{
4626 struct odb_source_files *files = odb_source_files_downcast(source);
4627 const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
4628 struct object_info oi = OBJECT_INFO_INIT;
4629 struct object_id compat_oid, *compat_oid_p = NULL;
4630 enum object_type type;
4631 void *buf = NULL;
4632 size_t len;
4633 int ret;
4634
4635 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next)
4636 if (!odb_source_read_object_info(&dir->loose->base, oid, NULL, 0, NULL))
4637 return 0;
4638
4639 oi.typep = &type;
4640 oi.sizep = &len;
4641 oi.contentp = &buf;
4642 if (odb_read_object_info_extended(source->odb, oid, &oi, 0)) {
4643 ret = error(_("cannot read object for %s"), oid_to_hex(oid));
4644 goto out;
4645 }
4646
4647 if (compat) {
4648 if (repo_oid_to_algop(source->odb->repo, oid, compat, &compat_oid)) {
4649 ret = error(_("cannot map object %s to %s"),
4650 oid_to_hex(oid), compat->name);
4651 goto out;
4652 }
4653
4654 compat_oid_p = &compat_oid;
4655 }
4656
4657 ret = odb_source_write_object(&files->dirs->loose->base, buf, len, type, oid,
4658 compat_oid_p, mtime, 0);
4659
4660out:
4661 free(buf);
4662 return ret;
4663}
4664
4665static void loosen_unused_packed_objects(void)
4666{
4667 struct packed_git *p;
4668 uint32_t i;
4669 uint32_t loosened_objects_nr = 0;
4670 struct object_id oid;
4671
4672 repo_for_each_pack(the_repository, p) {
4673 if (!p->pack_local || p->pack_keep || p->pack_keep_in_core)
4674 continue;
4675
4676 if (open_pack_index(p))
4677 die(_("cannot open pack index"));
4678
4679 for (i = 0; i < p->num_objects; i++) {
4680 nth_packed_object_id(&oid, p, i);
4681 if (!packlist_find(&to_pack, &oid) &&
4682 !has_sha1_pack_kept_or_nonlocal(&oid) &&
4683 !loosened_object_can_be_discarded(&oid, p->mtime)) {
4684 if (force_object_loose(the_repository->objects->source,
4685 &oid, &p->mtime))
4686 die(_("unable to force loose object"));
4687 loosened_objects_nr++;
4688 }
4689 }
4690 }
4691
4692 trace2_data_intmax("pack-objects", the_repository,
4693 "loosen_unused_packed_objects/loosened", loosened_objects_nr);
4694}
4695
4696/*
4697 * This tracks any options which pack-reuse code expects to be on, or which a
4698 * reader of the pack might not understand, and which would therefore prevent
4699 * blind reuse of what we have on disk.
4700 */
4701static int pack_options_allow_reuse(void)
4702{
4703 return allow_pack_reuse != NO_PACK_REUSE &&
4704 pack_to_stdout &&
4705 !ignore_packed_keep_on_disk &&
4706 !ignore_packed_keep_in_core &&
4707 (!local || !have_non_local_packs) &&
4708 !incremental;
4709}
4710
4711static int get_object_list_from_bitmap(struct rev_info *revs)
4712{
4713 if (!(bitmap_git = prepare_bitmap_walk(revs, 0)))
4714 return -1;
4715
4716 /*
4717 * For now, force the name-hash version to be 1 since that
4718 * is the version implied by the bitmap format. Later, the
4719 * format can include this version explicitly in its format,
4720 * allowing readers to know the version that was used during
4721 * the bitmap write.
4722 */
4723 name_hash_version = 1;
4724
4725 if (pack_options_allow_reuse())
4726 reuse_partial_packfile_from_bitmap(bitmap_git,
4727 &reuse_packfiles,
4728 &reuse_packfiles_nr,
4729 &reuse_packfile_bitmap,
4730 allow_pack_reuse == MULTI_PACK_REUSE);
4731
4732 if (reuse_packfiles) {
4733 reuse_packfile_objects = bitmap_popcount(reuse_packfile_bitmap);
4734 if (!reuse_packfile_objects)
4735 BUG("expected non-empty reuse bitmap");
4736
4737 nr_result += reuse_packfile_objects;
4738 nr_seen += reuse_packfile_objects;
4739 display_progress(progress_state, nr_seen);
4740 }
4741
4742 traverse_bitmap_commit_list(bitmap_git, revs,
4743 &add_object_entry_from_bitmap);
4744 return 0;
4745}
4746
4747static void record_recent_object(struct object *obj,
4748 const char *name UNUSED,
4749 void *data UNUSED)
4750{
4751 oid_array_append(&recent_objects, &obj->oid);
4752}
4753
4754static void record_recent_commit(struct commit *commit, void *data UNUSED)
4755{
4756 oid_array_append(&recent_objects, &commit->object.oid);
4757}
4758
4759static int mark_bitmap_preferred_tip(const struct reference *ref, void *data UNUSED)
4760{
4761 const struct object_id *maybe_peeled = ref->oid;
4762 struct object_id peeled;
4763 struct object *object;
4764
4765 if (!reference_get_peeled_oid(the_repository, ref, &peeled))
4766 maybe_peeled = &peeled;
4767
4768 object = parse_object_or_die(the_repository, maybe_peeled, ref->name);
4769 if (object->type == OBJ_COMMIT)
4770 object->flags |= NEEDS_BITMAP;
4771
4772 return 0;
4773}
4774
4775static inline int is_oid_uninteresting(struct repository *repo,
4776 struct object_id *oid)
4777{
4778 struct object *o = lookup_object(repo, oid);
4779 return !o || (o->flags & UNINTERESTING);
4780}
4781
4782static int add_objects_by_path(const char *path,
4783 struct oid_array *oids,
4784 enum object_type type,
4785 void *data)
4786{
4787 size_t oe_start = to_pack.nr_objects;
4788 size_t oe_end;
4789 unsigned int *processed = data;
4790
4791 /*
4792 * First, add all objects to the packing data, including the ones
4793 * marked UNINTERESTING (translated to 'exclude') as they can be
4794 * used as delta bases.
4795 */
4796 for (size_t i = 0; i < oids->nr; i++) {
4797 int exclude;
4798 struct object_info oi = OBJECT_INFO_INIT;
4799 struct object_id *oid = &oids->oid[i];
4800
4801 /* Skip objects that do not exist locally. */
4802 if ((exclude_promisor_objects || arg_missing_action != MA_ERROR) &&
4803 odb_read_object_info_extended(the_repository->objects, oid, &oi,
4804 OBJECT_INFO_FOR_PREFETCH) < 0)
4805 continue;
4806
4807 exclude = is_oid_uninteresting(the_repository, oid);
4808
4809 if (exclude && !thin)
4810 continue;
4811
4812 add_object_entry(oid, type, path, exclude);
4813
4814 if (type == OBJ_COMMIT) {
4815 struct commit *commit;
4816
4817 if (!write_bitmap_index && !use_delta_islands)
4818 continue;
4819
4820 commit = lookup_commit(the_repository, oid);
4821 if (!commit)
4822 die(_("could not find commit %s"), oid_to_hex(oid));
4823 if (write_bitmap_index)
4824 index_commit_for_bitmap(commit);
4825 /*
4826 * Skip island propagation for boundary commits.
4827 * The regular traversal's show_commit() is only
4828 * called for interesting commits; matching that
4829 * here keeps path-walk from doing extra work that
4830 * would only be a no-op anyway (boundary commits
4831 * are not in island_marks).
4832 */
4833 if (use_delta_islands && !exclude)
4834 propagate_island_marks(the_repository, commit);
4835 } else if (type == OBJ_TREE && use_delta_islands) {
4836 record_tree_depth(oid, path);
4837 }
4838 }
4839
4840 oe_end = to_pack.nr_objects;
4841
4842 /* We can skip delta calculations if it is a no-op. */
4843 if (oe_end == oe_start || !window)
4844 return 0;
4845
4846 ALLOC_GROW(to_pack.regions,
4847 to_pack.nr_regions + 1,
4848 to_pack.nr_regions_alloc);
4849
4850 to_pack.regions[to_pack.nr_regions].start = oe_start;
4851 to_pack.regions[to_pack.nr_regions].nr = oe_end - oe_start;
4852 to_pack.nr_regions++;
4853
4854 *processed += oids->nr;
4855 display_progress(progress_state, *processed);
4856
4857 return 0;
4858}
4859
4860static int get_object_list_path_walk(struct rev_info *revs)
4861{
4862 struct path_walk_info info = PATH_WALK_INFO_INIT;
4863 unsigned int processed = 0;
4864 int result;
4865
4866 info.revs = revs;
4867 info.path_fn = add_objects_by_path;
4868 info.path_fn_data = &processed;
4869
4870 /*
4871 * Path-walk needs boundary commits to discover thin-pack bases, but
4872 * bitmap traversal does not understand the boundary state. Set it
4873 * here so any prior bitmap attempt sees the usual non-boundary walk.
4874 */
4875 revs->boundary = 1;
4876
4877 /*
4878 * Allow the --[no-]sparse option to be interesting here, if only
4879 * for testing purposes. Paths with no interesting objects will not
4880 * contribute to the resulting pack, but only create noisy preferred
4881 * base objects.
4882 */
4883 info.prune_all_uninteresting = sparse;
4884 info.edge_aggressive = shallow;
4885
4886 trace2_region_enter("pack-objects", "path-walk", revs->repo);
4887 result = walk_objects_by_path(&info);
4888 trace2_region_leave("pack-objects", "path-walk", revs->repo);
4889
4890 path_walk_info_clear(&info);
4891
4892 return result;
4893}
4894
4895static void get_object_list(struct rev_info *revs, struct strvec *argv)
4896{
4897 struct setup_revision_opt s_r_opt = {
4898 .allow_exclude_promisor_objects = 1,
4899 };
4900 struct repo_config_values *cfg = repo_config_values(the_repository);
4901 char line[1000];
4902 int flags = 0;
4903 int save_warning;
4904
4905 save_commit_buffer = 0;
4906 setup_revisions_from_strvec(argv, revs, &s_r_opt);
4907
4908 /* make sure shallows are read */
4909 is_repository_shallow(the_repository);
4910
4911 save_warning = cfg->warn_on_object_refname_ambiguity;
4912 cfg->warn_on_object_refname_ambiguity = 0;
4913
4914 while (fgets(line, sizeof(line), stdin) != NULL) {
4915 int len = strlen(line);
4916 if (len && line[len - 1] == '\n')
4917 line[--len] = 0;
4918 if (!len)
4919 break;
4920 if (*line == '-') {
4921 if (!strcmp(line, "--not")) {
4922 flags ^= UNINTERESTING;
4923 write_bitmap_index = 0;
4924 continue;
4925 }
4926 if (starts_with(line, "--shallow ")) {
4927 struct object_id oid;
4928 if (get_oid_hex(line + 10, &oid))
4929 die("not an object name '%s'", line + 10);
4930 register_shallow(the_repository, &oid);
4931 use_bitmap_index = 0;
4932 continue;
4933 }
4934 die(_("not a rev '%s'"), line);
4935 }
4936 if (handle_revision_arg(line, revs, flags, REVARG_CANNOT_BE_FILENAME))
4937 die(_("bad revision '%s'"), line);
4938 }
4939
4940 cfg->warn_on_object_refname_ambiguity = save_warning;
4941
4942 if (use_bitmap_index && !get_object_list_from_bitmap(revs))
4943 return;
4944
4945 if (use_delta_islands)
4946 load_delta_islands(the_repository, progress);
4947
4948 if (write_bitmap_index)
4949 for_each_preferred_bitmap_tip(the_repository, mark_bitmap_preferred_tip,
4950 NULL);
4951
4952 if (!fn_show_object)
4953 fn_show_object = show_object;
4954
4955 if (path_walk) {
4956 if (get_object_list_path_walk(revs)) {
4957 warning(_("failed to pack objects via path-walk"));
4958 path_walk = 0;
4959 }
4960 }
4961
4962 if (!path_walk) {
4963 if (prepare_revision_walk(revs))
4964 die(_("revision walk setup failed"));
4965 mark_edges_uninteresting(revs, show_edge, sparse);
4966 traverse_commit_list(revs,
4967 show_commit, fn_show_object,
4968 NULL);
4969 }
4970
4971 if (unpack_unreachable_expiration) {
4972 revs->ignore_missing_links = 1;
4973 if (add_unseen_recent_objects_to_traversal(revs,
4974 unpack_unreachable_expiration, NULL, 0))
4975 die(_("unable to add recent objects"));
4976 if (prepare_revision_walk(revs))
4977 die(_("revision walk setup failed"));
4978 traverse_commit_list(revs, record_recent_commit,
4979 record_recent_object, NULL);
4980 }
4981
4982 if (keep_unreachable)
4983 add_objects_in_unpacked_packs();
4984 if (pack_loose_unreachable)
4985 add_unreachable_loose_objects(NULL);
4986 if (unpack_unreachable)
4987 loosen_unused_packed_objects();
4988
4989 oid_array_clear(&recent_objects);
4990}
4991
4992static void add_extra_kept_packs(const struct string_list *names)
4993{
4994 struct packed_git *p;
4995
4996 if (!names->nr)
4997 return;
4998
4999 repo_for_each_pack(the_repository, p) {
5000 const char *name = basename(p->pack_name);
5001 int i;
5002
5003 if (!p->pack_local)
5004 continue;
5005
5006 for (i = 0; i < names->nr; i++)
5007 if (!fspathcmp(name, names->items[i].string))
5008 break;
5009
5010 if (i < names->nr) {
5011 p->pack_keep_in_core = 1;
5012 ignore_packed_keep_in_core = 1;
5013 continue;
5014 }
5015 }
5016}
5017
5018static int option_parse_quiet(const struct option *opt, const char *arg,
5019 int unset)
5020{
5021 int *val = opt->value;
5022
5023 BUG_ON_OPT_ARG(arg);
5024
5025 if (!unset)
5026 *val = 0;
5027 else if (!*val)
5028 *val = 1;
5029 return 0;
5030}
5031
5032static int option_parse_index_version(const struct option *opt,
5033 const char *arg, int unset)
5034{
5035 struct pack_idx_option *popts = opt->value;
5036 char *c;
5037 const char *val = arg;
5038
5039 BUG_ON_OPT_NEG(unset);
5040
5041 popts->version = strtoul(val, &c, 10);
5042 if (popts->version > 2)
5043 die(_("unsupported index version %s"), val);
5044 if (*c == ',' && c[1])
5045 popts->off32_limit = strtoul(c+1, &c, 0);
5046 if (*c || popts->off32_limit & 0x80000000)
5047 die(_("bad index version '%s'"), val);
5048 return 0;
5049}
5050
5051static int option_parse_unpack_unreachable(const struct option *opt UNUSED,
5052 const char *arg, int unset)
5053{
5054 if (unset) {
5055 unpack_unreachable = 0;
5056 unpack_unreachable_expiration = 0;
5057 }
5058 else {
5059 unpack_unreachable = 1;
5060 if (arg)
5061 unpack_unreachable_expiration = approxidate(arg);
5062 }
5063 return 0;
5064}
5065
5066static int option_parse_cruft_expiration(const struct option *opt UNUSED,
5067 const char *arg, int unset)
5068{
5069 if (unset) {
5070 cruft = 0;
5071 cruft_expiration = 0;
5072 } else {
5073 cruft = 1;
5074 if (arg)
5075 cruft_expiration = approxidate(arg);
5076 }
5077 return 0;
5078}
5079
5080static int is_not_in_promisor_pack_obj(struct object *obj, void *data UNUSED)
5081{
5082 struct odb_source_info source_info;
5083 struct object_info info = {
5084 .source_infop = &source_info,
5085 };
5086
5087 if (odb_read_object_info_extended(the_repository->objects, &obj->oid, &info, 0))
5088 BUG("should_include_obj should only be called on existing objects");
5089 return source_info.source->type != ODB_SOURCE_PACKED || !source_info.u.packed.pack->pack_promisor;
5090}
5091
5092static int is_not_in_promisor_pack(struct commit *commit, void *data) {
5093 return is_not_in_promisor_pack_obj((struct object *) commit, data);
5094}
5095
5096static int parse_stdin_packs_mode(const struct option *opt, const char *arg,
5097 int unset)
5098{
5099 enum stdin_packs_mode *mode = opt->value;
5100
5101 if (unset)
5102 *mode = STDIN_PACKS_MODE_NONE;
5103 else if (!arg || !*arg)
5104 *mode = STDIN_PACKS_MODE_STANDARD;
5105 else if (!strcmp(arg, "follow"))
5106 *mode = STDIN_PACKS_MODE_FOLLOW;
5107 else
5108 die(_("invalid value for '%s': '%s'"), opt->long_name, arg);
5109
5110 return 0;
5111}
5112
5113int cmd_pack_objects(int argc,
5114 const char **argv,
5115 const char *prefix,
5116 struct repository *repo)
5117{
5118 int use_internal_rev_list = 0;
5119 int all_progress_implied = 0;
5120 struct strvec rp = STRVEC_INIT;
5121 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
5122 int rev_list_index = 0;
5123 enum stdin_packs_mode stdin_packs = STDIN_PACKS_MODE_NONE;
5124 struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
5125 struct list_objects_filter_options filter_options =
5126 LIST_OBJECTS_FILTER_INIT;
5127 struct repo_config_values *cfg = repo_config_values(the_repository);
5128
5129 struct option pack_objects_options[] = {
5130 OPT_CALLBACK_F('q', "quiet", &progress, NULL,
5131 N_("do not show progress meter"),
5132 PARSE_OPT_NOARG, option_parse_quiet),
5133 OPT_SET_INT(0, "progress", &progress,
5134 N_("show progress meter"), 1),
5135 OPT_SET_INT(0, "all-progress", &progress,
5136 N_("show progress meter during object writing phase"), 2),
5137 OPT_BOOL(0, "all-progress-implied",
5138 &all_progress_implied,
5139 N_("similar to --all-progress when progress meter is shown")),
5140 OPT_CALLBACK_F(0, "index-version", &pack_idx_opts, N_("<version>[,<offset>]"),
5141 N_("write the pack index file in the specified idx format version"),
5142 PARSE_OPT_NONEG, option_parse_index_version),
5143 OPT_UNSIGNED(0, "max-pack-size", &pack_size_limit,
5144 N_("maximum size of each output pack file")),
5145 OPT_BOOL(0, "local", &local,
5146 N_("ignore borrowed objects from alternate object store")),
5147 OPT_BOOL(0, "incremental", &incremental,
5148 N_("ignore packed objects")),
5149 OPT_INTEGER(0, "window", &window,
5150 N_("limit pack window by objects")),
5151 OPT_UNSIGNED(0, "window-memory", &window_memory_limit,
5152 N_("limit pack window by memory in addition to object limit")),
5153 OPT_INTEGER(0, "depth", &depth,
5154 N_("maximum length of delta chain allowed in the resulting pack")),
5155 OPT_BOOL(0, "reuse-delta", &reuse_delta,
5156 N_("reuse existing deltas")),
5157 OPT_BOOL(0, "reuse-object", &reuse_object,
5158 N_("reuse existing objects")),
5159 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
5160 N_("use OFS_DELTA objects")),
5161 OPT_INTEGER(0, "threads", &delta_search_threads,
5162 N_("use threads when searching for best delta matches")),
5163 OPT_BOOL(0, "non-empty", &non_empty,
5164 N_("do not create an empty pack output")),
5165 OPT_BOOL(0, "revs", &use_internal_rev_list,
5166 N_("read revision arguments from standard input")),
5167 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked,
5168 N_("limit the objects to those that are not yet packed"),
5169 1, PARSE_OPT_NONEG),
5170 OPT_SET_INT_F(0, "all", &rev_list_all,
5171 N_("include objects reachable from any reference"),
5172 1, PARSE_OPT_NONEG),
5173 OPT_SET_INT_F(0, "reflog", &rev_list_reflog,
5174 N_("include objects referred by reflog entries"),
5175 1, PARSE_OPT_NONEG),
5176 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index,
5177 N_("include objects referred to by the index"),
5178 1, PARSE_OPT_NONEG),
5179 OPT_CALLBACK_F(0, "stdin-packs", &stdin_packs, N_("mode"),
5180 N_("read packs from stdin"),
5181 PARSE_OPT_OPTARG, parse_stdin_packs_mode),
5182 OPT_BOOL(0, "stdout", &pack_to_stdout,
5183 N_("output pack to stdout")),
5184 OPT_BOOL(0, "include-tag", &include_tag,
5185 N_("include tag objects that refer to objects to be packed")),
5186 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
5187 N_("keep unreachable objects")),
5188 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
5189 N_("pack loose unreachable objects")),
5190 OPT_CALLBACK_F(0, "unpack-unreachable", NULL, N_("time"),
5191 N_("unpack unreachable objects newer than <time>"),
5192 PARSE_OPT_OPTARG, option_parse_unpack_unreachable),
5193 OPT_BOOL(0, "cruft", &cruft, N_("create a cruft pack")),
5194 OPT_CALLBACK_F(0, "cruft-expiration", NULL, N_("time"),
5195 N_("expire cruft objects older than <time>"),
5196 PARSE_OPT_OPTARG, option_parse_cruft_expiration),
5197 OPT_BOOL(0, "sparse", &sparse,
5198 N_("use the sparse reachability algorithm")),
5199 OPT_BOOL(0, "thin", &thin,
5200 N_("create thin packs")),
5201 OPT_BOOL(0, "path-walk", &path_walk,
5202 N_("use the path-walk API to walk objects when possible")),
5203 OPT_BOOL(0, "shallow", &shallow,
5204 N_("create packs suitable for shallow fetches")),
5205 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk,
5206 N_("ignore packs that have companion .keep file")),
5207 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
5208 N_("ignore this pack")),
5209 OPT_INTEGER(0, "compression", &cfg->pack_compression_level,
5210 N_("pack compression level")),
5211 OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents,
5212 N_("do not hide commits by grafts")),
5213 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
5214 N_("use a bitmap index if available to speed up counting objects")),
5215 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index,
5216 N_("write a bitmap index together with the pack index"),
5217 WRITE_BITMAP_TRUE),
5218 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
5219 &write_bitmap_index,
5220 N_("write a bitmap index if possible"),
5221 WRITE_BITMAP_QUIET, PARSE_OPT_HIDDEN),
5222 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
5223 OPT_CALLBACK_F(0, "missing", repo, N_("action"),
5224 N_("handling for missing objects"), PARSE_OPT_NONEG,
5225 option_parse_missing_action),
5226 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
5227 N_("do not pack objects in promisor packfiles")),
5228 OPT_BOOL(0, "exclude-promisor-objects-best-effort",
5229 &exclude_promisor_objects_best_effort,
5230 N_("implies --missing=allow-any")),
5231 OPT_BOOL(0, "delta-islands", &use_delta_islands,
5232 N_("respect islands during delta compression")),
5233 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols,
5234 N_("protocol"),
5235 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
5236 OPT_INTEGER(0, "name-hash-version", &name_hash_version,
5237 N_("use the specified name-hash function to group similar objects")),
5238 OPT_END(),
5239 };
5240
5241 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
5242 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
5243
5244 disable_replace_refs();
5245
5246 sparse = git_env_bool("GIT_TEST_PACK_SPARSE", -1);
5247 if (the_repository->gitdir) {
5248 prepare_repo_settings(the_repository);
5249 if (sparse < 0)
5250 sparse = the_repository->settings.pack_use_sparse;
5251 if (the_repository->settings.pack_use_multi_pack_reuse)
5252 allow_pack_reuse = MULTI_PACK_REUSE;
5253 }
5254
5255 reset_pack_idx_option(&pack_idx_opts);
5256 pack_idx_opts.flags |= WRITE_REV;
5257 repo_config(the_repository, git_pack_config, NULL);
5258 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX, 0))
5259 pack_idx_opts.flags &= ~WRITE_REV;
5260
5261 progress = isatty(2);
5262 argc = parse_options(argc, argv, prefix, pack_objects_options,
5263 pack_usage, 0);
5264
5265 if (argc) {
5266 base_name = argv[0];
5267 argc--;
5268 }
5269 if (pack_to_stdout != !base_name || argc)
5270 usage_with_options(pack_usage, pack_objects_options);
5271
5272 if (path_walk < 0) {
5273 if (use_bitmap_index > 0 ||
5274 !use_internal_rev_list)
5275 path_walk = 0;
5276 else if (the_repository->gitdir &&
5277 the_repository->settings.pack_use_path_walk)
5278 path_walk = 1;
5279 else
5280 path_walk = git_env_bool("GIT_TEST_PACK_PATH_WALK", 0);
5281 }
5282
5283 if (depth < 0)
5284 depth = 0;
5285 if (depth >= (1 << OE_DEPTH_BITS)) {
5286 warning(_("delta chain depth %d is too deep, forcing %d"),
5287 depth, (1 << OE_DEPTH_BITS) - 1);
5288 depth = (1 << OE_DEPTH_BITS) - 1;
5289 }
5290 if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
5291 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
5292 (1U << OE_Z_DELTA_BITS) - 1);
5293 cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
5294 }
5295 if (window < 0)
5296 window = 0;
5297
5298 strvec_push(&rp, "pack-objects");
5299
5300 if (path_walk) {
5301 const char *option = NULL;
5302 if (!path_walk_filter_compatible(&filter_options))
5303 option = "--filter";
5304
5305 if (option) {
5306 warning(_("cannot use %s with %s"),
5307 option, "--path-walk");
5308 path_walk = 0;
5309 }
5310 }
5311 if (path_walk) {
5312 strvec_push(&rp, "--objects");
5313 } else if (thin) {
5314 use_internal_rev_list = 1;
5315 strvec_push(&rp, shallow
5316 ? "--objects-edge-aggressive"
5317 : "--objects-edge");
5318 } else
5319 strvec_push(&rp, "--objects");
5320
5321 if (rev_list_all) {
5322 use_internal_rev_list = 1;
5323 strvec_push(&rp, "--all");
5324 }
5325 if (rev_list_reflog) {
5326 use_internal_rev_list = 1;
5327 strvec_push(&rp, "--reflog");
5328 }
5329 if (rev_list_index) {
5330 use_internal_rev_list = 1;
5331 strvec_push(&rp, "--indexed-objects");
5332 }
5333 if (rev_list_unpacked && !stdin_packs) {
5334 use_internal_rev_list = 1;
5335 strvec_push(&rp, "--unpacked");
5336 }
5337
5338 die_for_incompatible_opt2(exclude_promisor_objects,
5339 "--exclude-promisor-objects",
5340 exclude_promisor_objects_best_effort,
5341 "--exclude-promisor-objects-best-effort");
5342 if (exclude_promisor_objects) {
5343 repo->fetch_if_missing = 0;
5344
5345 /* --stdin-packs handles promisor objects separately. */
5346 if (!stdin_packs) {
5347 use_internal_rev_list = 1;
5348 strvec_push(&rp, "--exclude-promisor-objects");
5349 }
5350 } else if (exclude_promisor_objects_best_effort) {
5351 use_internal_rev_list = 1;
5352 arg_missing_action = MA_ALLOW_ANY;
5353 repo->fetch_if_missing = 0;
5354 fn_show_object = show_object__ma_allow_any;
5355 /* revs configured below */
5356 }
5357 if (unpack_unreachable || keep_unreachable || pack_loose_unreachable)
5358 use_internal_rev_list = 1;
5359
5360 if (!reuse_object)
5361 reuse_delta = 0;
5362 if (cfg->pack_compression_level == -1)
5363 cfg->pack_compression_level = Z_DEFAULT_COMPRESSION;
5364 else if (cfg->pack_compression_level < 0 || cfg->pack_compression_level > Z_BEST_COMPRESSION)
5365 die(_("bad pack compression level %d"), cfg->pack_compression_level);
5366
5367 if (!delta_search_threads) /* --threads=0 means autodetect */
5368 delta_search_threads = online_cpus();
5369
5370 if (!HAVE_THREADS && delta_search_threads != 1)
5371 warning(_("no threads support, ignoring --threads"));
5372 if (!pack_to_stdout && !pack_size_limit)
5373 pack_size_limit = pack_size_limit_cfg;
5374 if (pack_to_stdout && pack_size_limit)
5375 die(_("--max-pack-size cannot be used to build a pack for transfer"));
5376 if (pack_size_limit && pack_size_limit < 1024*1024) {
5377 warning(_("minimum pack size limit is 1 MiB"));
5378 pack_size_limit = 1024*1024;
5379 }
5380
5381 if (!pack_to_stdout && thin)
5382 die(_("--thin cannot be used to build an indexable pack"));
5383
5384 die_for_incompatible_opt2(keep_unreachable, "--keep-unreachable",
5385 unpack_unreachable, "--unpack-unreachable");
5386 if (!rev_list_all || !rev_list_reflog || !rev_list_index)
5387 unpack_unreachable_expiration = 0;
5388
5389 die_for_incompatible_opt2(stdin_packs, "--stdin-packs",
5390 filter_options.choice, "--filter");
5391
5392
5393 if (stdin_packs && use_internal_rev_list)
5394 die(_("cannot use internal rev list with --stdin-packs"));
5395
5396 if (cruft) {
5397 if (use_internal_rev_list)
5398 die(_("cannot use internal rev list with --cruft"));
5399 die_for_incompatible_opt2(stdin_packs, "--stdin-packs",
5400 cruft, "--cruft");
5401 }
5402
5403 /*
5404 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
5405 *
5406 * - to produce good pack (with bitmap index not-yet-packed objects are
5407 * packed in suboptimal order).
5408 *
5409 * - to use more robust pack-generation codepath (avoiding possible
5410 * bugs in bitmap code and possible bitmap index corruption).
5411 */
5412 if (!pack_to_stdout)
5413 use_bitmap_index_default = 0;
5414
5415 if (use_bitmap_index < 0)
5416 use_bitmap_index = use_bitmap_index_default;
5417
5418 /* "hard" reasons not to use bitmaps; these just won't work at all */
5419 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow(the_repository))
5420 use_bitmap_index = 0;
5421
5422 if (pack_to_stdout || !rev_list_all)
5423 write_bitmap_index = 0;
5424
5425 if (name_hash_version < 0)
5426 name_hash_version = (int)git_env_ulong("GIT_TEST_NAME_HASH_VERSION", 1);
5427
5428 validate_name_hash_version();
5429
5430 if (use_delta_islands)
5431 strvec_push(&rp, "--topo-order");
5432
5433 if (progress && all_progress_implied)
5434 progress = 2;
5435
5436 add_extra_kept_packs(&keep_pack_list);
5437 if (ignore_packed_keep_on_disk) {
5438 struct packed_git *p;
5439
5440 repo_for_each_pack(the_repository, p)
5441 if (p->pack_local && p->pack_keep)
5442 break;
5443 if (!p) /* no keep-able packs found */
5444 ignore_packed_keep_on_disk = 0;
5445 }
5446 if (local) {
5447 /*
5448 * unlike ignore_packed_keep_on_disk above, we do not
5449 * want to unset "local" based on looking at packs, as
5450 * it also covers non-local objects
5451 */
5452 struct packed_git *p;
5453
5454 repo_for_each_pack(the_repository, p) {
5455 if (!p->pack_local) {
5456 have_non_local_packs = 1;
5457 break;
5458 }
5459 }
5460 }
5461
5462 trace2_region_enter("pack-objects", "enumerate-objects",
5463 the_repository);
5464 prepare_packing_data(the_repository, &to_pack);
5465
5466 if (progress && !cruft)
5467 progress_state = start_progress(the_repository,
5468 _("Enumerating objects"), 0);
5469 if (stdin_packs) {
5470 read_stdin_packs(repo, stdin_packs, rev_list_unpacked);
5471 } else if (cruft) {
5472 read_cruft_objects();
5473 } else if (!use_internal_rev_list) {
5474 read_object_list_from_stdin();
5475 } else {
5476 struct rev_info revs;
5477
5478 repo_init_revisions(the_repository, &revs, NULL);
5479 list_objects_filter_copy(&revs.filter, &filter_options);
5480 if (exclude_promisor_objects_best_effort) {
5481 revs.include_check = is_not_in_promisor_pack;
5482 revs.include_check_obj = is_not_in_promisor_pack_obj;
5483 }
5484 get_object_list(&revs, &rp);
5485 release_revisions(&revs);
5486 }
5487 cleanup_preferred_base();
5488 if (include_tag && nr_result)
5489 refs_for_each_tag_ref(get_main_ref_store(the_repository),
5490 add_ref_tag, NULL);
5491 stop_progress(&progress_state);
5492 trace2_region_leave("pack-objects", "enumerate-objects",
5493 the_repository);
5494
5495 if (non_empty && !nr_result)
5496 goto cleanup;
5497 if (nr_result) {
5498 trace2_region_enter("pack-objects", "prepare-pack",
5499 the_repository);
5500 prepare_pack(window, depth);
5501 trace2_region_leave("pack-objects", "prepare-pack",
5502 the_repository);
5503 }
5504
5505 trace2_region_enter("pack-objects", "write-pack-file", the_repository);
5506 write_excluded_by_configs();
5507 write_pack_file();
5508 trace2_region_leave("pack-objects", "write-pack-file", the_repository);
5509
5510 if (progress)
5511 fprintf_ln(stderr,
5512 _("Total %"PRIu32" (delta %"PRIu32"),"
5513 " reused %"PRIu32" (delta %"PRIu32"),"
5514 " pack-reused %"PRIu32" (from %"PRIuMAX")"),
5515 written, written_delta, reused, reused_delta,
5516 reuse_packfile_objects,
5517 (uintmax_t)reuse_packfiles_used_nr);
5518
5519 trace2_data_intmax("pack-objects", the_repository, "written", written);
5520 trace2_data_intmax("pack-objects", the_repository, "written/delta", written_delta);
5521 trace2_data_intmax("pack-objects", the_repository, "reused", reused);
5522 trace2_data_intmax("pack-objects", the_repository, "reused/delta", reused_delta);
5523 trace2_data_intmax("pack-objects", the_repository, "pack-reused", reuse_packfile_objects);
5524 trace2_data_intmax("pack-objects", the_repository, "packs-reused", reuse_packfiles_used_nr);
5525
5526cleanup:
5527 clear_packing_data(&to_pack);
5528 list_objects_filter_release(&filter_options);
5529 string_list_clear(&keep_pack_list, 0);
5530 strvec_clear(&rp);
5531
5532 return 0;
5533}