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