pack-objects: clarify the use of object_entry::size
[git/git.git] / builtin / pack-objects.c
CommitLineData
5d4a6003 1#include "builtin.h"
c323ac7d 2#include "cache.h"
a80d72db 3#include "repository.h"
b2141fc1 4#include "config.h"
a74db82e 5#include "attr.h"
c323ac7d 6#include "object.h"
8e440259
PE
7#include "blob.h"
8#include "commit.h"
9#include "tag.h"
10#include "tree.h"
c323ac7d 11#include "delta.h"
a733cb60 12#include "pack.h"
3449f8c4 13#include "pack-revindex.h"
c38138cd 14#include "csum-file.h"
1b0c7174 15#include "tree-walk.h"
b5d97e6b
JH
16#include "diff.h"
17#include "revision.h"
18#include "list-objects.h"
9535ce73
JH
19#include "list-objects-filter.h"
20#include "list-objects-filter-options.h"
2834bc27 21#include "pack-objects.h"
96a02f8f 22#include "progress.h"
f0a24aa5 23#include "refs.h"
cf2ba13a 24#include "streaming.h"
93749194 25#include "thread-utils.h"
6b8fda2d 26#include "pack-bitmap.h"
abcb8655
JK
27#include "reachable.h"
28#include "sha1-array.h"
edfbb2aa 29#include "argv-array.h"
ec2dd32c 30#include "list.h"
0317f455 31#include "packfile.h"
a80d72db 32#include "object-store.h"
8ecce684 33
43fa44fa 34#define IN_PACK(obj) oe_in_pack(&to_pack, obj)
898eba5e
NTND
35#define DELTA(obj) oe_delta(&to_pack, obj)
36#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
37#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
38#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
39#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
40#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
43fa44fa 41
99fb6e04 42static const char *pack_usage[] = {
b8c1d275
AH
43 N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
44 N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"),
99fb6e04
NTND
45 NULL
46};
c323ac7d 47
3f9ac8d2 48/*
2834bc27
VM
49 * Objects we are going to pack are collected in the `to_pack` structure.
50 * It contains an array (dynamically expanded) of the object data, and a map
51 * that can resolve SHA1s to their position in the array.
3f9ac8d2 52 */
2834bc27
VM
53static struct packing_data to_pack;
54
79814f42 55static struct pack_idx_entry **written_list;
2834bc27 56static uint32_t nr_result, nr_written;
3f9ac8d2 57
96f1e58f 58static int non_empty;
a7de7130 59static int reuse_delta = 1, reuse_object = 1;
e5e9714a 60static int keep_unreachable, unpack_unreachable, include_tag;
dddbad72 61static timestamp_t unpack_unreachable_expiration;
e26a8c47 62static int pack_loose_unreachable;
96f1e58f 63static int local;
56dfeb62 64static int have_non_local_packs;
96f1e58f 65static int incremental;
e96fb9b8 66static int ignore_packed_keep;
be6b1914 67static int allow_ofs_delta;
ebcfb379 68static struct pack_idx_option pack_idx_opts;
d01fb92f 69static const char *base_name;
024701f1 70static int progress = 1;
4812a93a 71static int window = 10;
568508e7 72static unsigned long pack_size_limit;
618e613a 73static int depth = 50;
43cc2b42 74static int delta_search_threads;
df6d6101 75static int pack_to_stdout;
8d1d8f83 76static int num_preferred_base;
dc6a0757 77static struct progress *progress_state;
c323ac7d 78
6b8fda2d
VM
79static struct packed_git *reuse_packfile;
80static uint32_t reuse_packfile_objects;
81static off_t reuse_packfile_offset;
82
645c432d
KS
83static int use_bitmap_index_default = 1;
84static int use_bitmap_index = -1;
7cc8f971 85static int write_bitmap_index;
ae4f07fb 86static uint16_t write_bitmap_options;
6b8fda2d 87
0c16cd49
JT
88static int exclude_promisor_objects;
89
074b2eea 90static unsigned long delta_cache_size = 0;
5749b0b2 91static unsigned long max_delta_cache_size = 256 * 1024 * 1024;
e3dfddb3 92static unsigned long cache_max_small_delta_size = 1000;
074b2eea 93
a97773ce
BD
94static unsigned long window_memory_limit = 0;
95
9535ce73
JH
96static struct list_objects_filter_options filter_options;
97
98enum missing_action {
0c16cd49
JT
99 MA_ERROR = 0, /* fail if any missing objects are encountered */
100 MA_ALLOW_ANY, /* silently allow ALL missing objects */
101 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
9535ce73
JH
102};
103static enum missing_action arg_missing_action;
104static show_object_fn fn_show_object;
105
3f9ac8d2
JH
106/*
107 * stats
108 */
7cadf491
SP
109static uint32_t written, written_delta;
110static uint32_t reused, reused_delta;
3f9ac8d2 111
7cc8f971
VM
112/*
113 * Indexed commits
114 */
115static struct commit **indexed_commits;
116static unsigned int indexed_commits_nr;
117static unsigned int indexed_commits_alloc;
118
119static void index_commit_for_bitmap(struct commit *commit)
120{
121 if (indexed_commits_nr >= indexed_commits_alloc) {
122 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
2756ca43 123 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
7cc8f971
VM
124 }
125
126 indexed_commits[indexed_commits_nr++] = commit;
127}
780e6e73 128
3613f9b4 129static void *get_delta(struct object_entry *entry)
c323ac7d 130{
3613f9b4
NP
131 unsigned long size, base_size, delta_size;
132 void *buf, *base_buf, *delta_buf;
21666f1a 133 enum object_type type;
c323ac7d 134
b4f5aca4 135 buf = read_object_file(&entry->idx.oid, &type, &size);
3613f9b4 136 if (!buf)
e6a492b7 137 die("unable to read %s", oid_to_hex(&entry->idx.oid));
898eba5e
NTND
138 base_buf = read_object_file(&DELTA(entry)->idx.oid, &type,
139 &base_size);
3613f9b4 140 if (!base_buf)
e6a492b7 141 die("unable to read %s",
898eba5e 142 oid_to_hex(&DELTA(entry)->idx.oid));
3613f9b4 143 delta_buf = diff_delta(base_buf, base_size,
dcde55bc 144 buf, size, &delta_size, 0);
3613f9b4 145 if (!delta_buf || delta_size != entry->delta_size)
a6080a0a 146 die("delta size changed");
3613f9b4
NP
147 free(buf);
148 free(base_buf);
c323ac7d
LT
149 return delta_buf;
150}
151
30ebb40a
NP
152static unsigned long do_compress(void **pptr, unsigned long size)
153{
ef49a7a0 154 git_zstream stream;
30ebb40a
NP
155 void *in, *out;
156 unsigned long maxsize;
157
55bb5c91 158 git_deflate_init(&stream, pack_compression_level);
225a6f10 159 maxsize = git_deflate_bound(&stream, size);
30ebb40a
NP
160
161 in = *pptr;
162 out = xmalloc(maxsize);
163 *pptr = out;
164
165 stream.next_in = in;
166 stream.avail_in = size;
167 stream.next_out = out;
168 stream.avail_out = maxsize;
55bb5c91 169 while (git_deflate(&stream, Z_FINISH) == Z_OK)
30ebb40a 170 ; /* nothing */
55bb5c91 171 git_deflate_end(&stream);
30ebb40a
NP
172
173 free(in);
174 return stream.total_out;
175}
176
98a3beab 177static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f,
188960b4 178 const struct object_id *oid)
cf2ba13a
NTND
179{
180 git_zstream stream;
181 unsigned char ibuf[1024 * 16];
182 unsigned char obuf[1024 * 16];
183 unsigned long olen = 0;
184
cf2ba13a
NTND
185 git_deflate_init(&stream, pack_compression_level);
186
187 for (;;) {
188 ssize_t readlen;
189 int zret = Z_OK;
190 readlen = read_istream(st, ibuf, sizeof(ibuf));
191 if (readlen == -1)
188960b4 192 die(_("unable to read %s"), oid_to_hex(oid));
cf2ba13a
NTND
193
194 stream.next_in = ibuf;
195 stream.avail_in = readlen;
196 while ((stream.avail_in || readlen == 0) &&
197 (zret == Z_OK || zret == Z_BUF_ERROR)) {
198 stream.next_out = obuf;
199 stream.avail_out = sizeof(obuf);
200 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
98a3beab 201 hashwrite(f, obuf, stream.next_out - obuf);
cf2ba13a
NTND
202 olen += stream.next_out - obuf;
203 }
204 if (stream.avail_in)
205 die(_("deflate error (%d)"), zret);
206 if (readlen == 0) {
207 if (zret != Z_STREAM_END)
208 die(_("deflate error (%d)"), zret);
209 break;
210 }
211 }
212 git_deflate_end(&stream);
213 return olen;
214}
215
780e6e73
NP
216/*
217 * we are going to reuse the existing object data as is. make
218 * sure it is not corrupt.
219 */
079afb18
SP
220static int check_pack_inflate(struct packed_git *p,
221 struct pack_window **w_curs,
6777a59f
SP
222 off_t offset,
223 off_t len,
079afb18
SP
224 unsigned long expect)
225{
ef49a7a0 226 git_zstream stream;
079afb18
SP
227 unsigned char fakebuf[4096], *in;
228 int st;
229
230 memset(&stream, 0, sizeof(stream));
39c68542 231 git_inflate_init(&stream);
079afb18
SP
232 do {
233 in = use_pack(p, w_curs, offset, &stream.avail_in);
234 stream.next_in = in;
235 stream.next_out = fakebuf;
236 stream.avail_out = sizeof(fakebuf);
39c68542 237 st = git_inflate(&stream, Z_FINISH);
079afb18
SP
238 offset += stream.next_in - in;
239 } while (st == Z_OK || st == Z_BUF_ERROR);
39c68542 240 git_inflate_end(&stream);
079afb18
SP
241 return (st == Z_STREAM_END &&
242 stream.total_out == expect &&
243 stream.total_in == len) ? 0 : -1;
244}
245
98a3beab 246static void copy_pack_data(struct hashfile *f,
079afb18
SP
247 struct packed_git *p,
248 struct pack_window **w_curs,
6777a59f
SP
249 off_t offset,
250 off_t len)
079afb18
SP
251{
252 unsigned char *in;
ef49a7a0 253 unsigned long avail;
079afb18
SP
254
255 while (len) {
256 in = use_pack(p, w_curs, offset, &avail);
257 if (avail > len)
ef49a7a0 258 avail = (unsigned long)len;
98a3beab 259 hashwrite(f, in, avail);
079afb18
SP
260 offset += avail;
261 len -= avail;
262 }
263}
264
1b4bb16b 265/* Return 0 if we will bust the pack-size limit */
98a3beab 266static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry,
c9018b03 267 unsigned long limit, int usable_delta)
c323ac7d 268{
c9018b03 269 unsigned long size, datalen;
2c5e2865
JK
270 unsigned char header[MAX_PACK_OBJECT_HEADER],
271 dheader[MAX_PACK_OBJECT_HEADER];
6777a59f 272 unsigned hdrlen;
2c5ef824 273 enum object_type type;
c9018b03 274 void *buf;
cf2ba13a 275 struct git_istream *st = NULL;
c9018b03
NTND
276
277 if (!usable_delta) {
fd9b1bae 278 if (oe_type(entry) == OBJ_BLOB &&
cf2ba13a 279 entry->size > big_file_threshold &&
ef7b5195 280 (st = open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL)
cf2ba13a
NTND
281 buf = NULL;
282 else {
b4f5aca4 283 buf = read_object_file(&entry->idx.oid, &type, &size);
cf2ba13a 284 if (!buf)
e6a492b7 285 die(_("unable to read %s"),
286 oid_to_hex(&entry->idx.oid));
cf2ba13a 287 }
c9018b03
NTND
288 /*
289 * make sure no cached delta data remains from a
290 * previous attempt before a pack split occurred.
291 */
6a83d902 292 FREE_AND_NULL(entry->delta_data);
c9018b03
NTND
293 entry->z_delta_size = 0;
294 } else if (entry->delta_data) {
295 size = entry->delta_size;
296 buf = entry->delta_data;
297 entry->delta_data = NULL;
898eba5e 298 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
c9018b03
NTND
299 OBJ_OFS_DELTA : OBJ_REF_DELTA;
300 } else {
301 buf = get_delta(entry);
302 size = entry->delta_size;
898eba5e 303 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
c9018b03
NTND
304 OBJ_OFS_DELTA : OBJ_REF_DELTA;
305 }
306
cf2ba13a
NTND
307 if (st) /* large blob case, just assume we don't compress well */
308 datalen = size;
309 else if (entry->z_delta_size)
c9018b03
NTND
310 datalen = entry->z_delta_size;
311 else
312 datalen = do_compress(&buf, size);
313
314 /*
315 * The object header is a byte of 'type' followed by zero or
316 * more bytes of length.
317 */
7202a6fa
JK
318 hdrlen = encode_in_pack_object_header(header, sizeof(header),
319 type, size);
c9018b03
NTND
320
321 if (type == OBJ_OFS_DELTA) {
322 /*
323 * Deltas with relative base contain an additional
324 * encoding of the relative offset for the delta
325 * base from this object's position in the pack.
326 */
898eba5e 327 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
c9018b03
NTND
328 unsigned pos = sizeof(dheader) - 1;
329 dheader[pos] = ofs & 127;
330 while (ofs >>= 7)
331 dheader[--pos] = 128 | (--ofs & 127);
332 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
cf2ba13a
NTND
333 if (st)
334 close_istream(st);
c9018b03
NTND
335 free(buf);
336 return 0;
337 }
98a3beab 338 hashwrite(f, header, hdrlen);
339 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
c9018b03
NTND
340 hdrlen += sizeof(dheader) - pos;
341 } else if (type == OBJ_REF_DELTA) {
342 /*
343 * Deltas with a base reference contain
344 * an additional 20 bytes for the base sha1.
345 */
346 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
cf2ba13a
NTND
347 if (st)
348 close_istream(st);
c9018b03
NTND
349 free(buf);
350 return 0;
351 }
98a3beab 352 hashwrite(f, header, hdrlen);
898eba5e 353 hashwrite(f, DELTA(entry)->idx.oid.hash, 20);
c9018b03
NTND
354 hdrlen += 20;
355 } else {
356 if (limit && hdrlen + datalen + 20 >= limit) {
cf2ba13a
NTND
357 if (st)
358 close_istream(st);
c9018b03
NTND
359 free(buf);
360 return 0;
361 }
98a3beab 362 hashwrite(f, header, hdrlen);
c9018b03 363 }
cf2ba13a 364 if (st) {
188960b4 365 datalen = write_large_blob_data(st, f, &entry->idx.oid);
cf2ba13a
NTND
366 close_istream(st);
367 } else {
98a3beab 368 hashwrite(f, buf, datalen);
cf2ba13a
NTND
369 free(buf);
370 }
c9018b03
NTND
371
372 return hdrlen + datalen;
373}
374
375/* Return 0 if we will bust the pack-size limit */
98a3beab 376static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry,
af92a645 377 unsigned long limit, int usable_delta)
c9018b03 378{
43fa44fa 379 struct packed_git *p = IN_PACK(entry);
c9018b03
NTND
380 struct pack_window *w_curs = NULL;
381 struct revindex_entry *revidx;
382 off_t offset;
fd9b1bae 383 enum object_type type = oe_type(entry);
211c61c6 384 off_t datalen;
2c5e2865
JK
385 unsigned char header[MAX_PACK_OBJECT_HEADER],
386 dheader[MAX_PACK_OBJECT_HEADER];
c9018b03
NTND
387 unsigned hdrlen;
388
898eba5e
NTND
389 if (DELTA(entry))
390 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ?
c9018b03 391 OBJ_OFS_DELTA : OBJ_REF_DELTA;
7202a6fa
JK
392 hdrlen = encode_in_pack_object_header(header, sizeof(header),
393 type, entry->size);
c9018b03
NTND
394
395 offset = entry->in_pack_offset;
396 revidx = find_pack_revindex(p, offset);
397 datalen = revidx[1].offset - offset;
398 if (!pack_to_stdout && p->index_version > 1 &&
399 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) {
e6a492b7 400 error("bad packed object CRC for %s",
401 oid_to_hex(&entry->idx.oid));
c9018b03
NTND
402 unuse_pack(&w_curs);
403 return write_no_reuse_object(f, entry, limit, usable_delta);
404 }
405
406 offset += entry->in_pack_header_size;
407 datalen -= entry->in_pack_header_size;
408
409 if (!pack_to_stdout && p->index_version == 1 &&
410 check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) {
e6a492b7 411 error("corrupt packed object for %s",
412 oid_to_hex(&entry->idx.oid));
c9018b03
NTND
413 unuse_pack(&w_curs);
414 return write_no_reuse_object(f, entry, limit, usable_delta);
415 }
416
417 if (type == OBJ_OFS_DELTA) {
898eba5e 418 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset;
c9018b03
NTND
419 unsigned pos = sizeof(dheader) - 1;
420 dheader[pos] = ofs & 127;
421 while (ofs >>= 7)
422 dheader[--pos] = 128 | (--ofs & 127);
423 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
424 unuse_pack(&w_curs);
425 return 0;
426 }
98a3beab 427 hashwrite(f, header, hdrlen);
428 hashwrite(f, dheader + pos, sizeof(dheader) - pos);
c9018b03
NTND
429 hdrlen += sizeof(dheader) - pos;
430 reused_delta++;
431 } else if (type == OBJ_REF_DELTA) {
432 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
433 unuse_pack(&w_curs);
434 return 0;
435 }
98a3beab 436 hashwrite(f, header, hdrlen);
898eba5e 437 hashwrite(f, DELTA(entry)->idx.oid.hash, 20);
c9018b03
NTND
438 hdrlen += 20;
439 reused_delta++;
440 } else {
441 if (limit && hdrlen + datalen + 20 >= limit) {
442 unuse_pack(&w_curs);
443 return 0;
444 }
98a3beab 445 hashwrite(f, header, hdrlen);
c9018b03
NTND
446 }
447 copy_pack_data(f, p, &w_curs, offset, datalen);
448 unuse_pack(&w_curs);
449 reused++;
450 return hdrlen + datalen;
451}
452
453/* Return 0 if we will bust the pack-size limit */
98a3beab 454static off_t write_object(struct hashfile *f,
af92a645
NTND
455 struct object_entry *entry,
456 off_t write_offset)
c9018b03 457{
af92a645
NTND
458 unsigned long limit;
459 off_t len;
2c5ef824 460 int usable_delta, to_reuse;
c323ac7d 461
78d1e84f
NP
462 if (!pack_to_stdout)
463 crc32_begin(f);
464
a2430dde 465 /* apply size limit if limited packsize and not first object */
a1e4760f
NP
466 if (!pack_size_limit || !nr_written)
467 limit = 0;
468 else if (pack_size_limit <= write_offset)
469 /*
470 * the earlier object did not fit the limit; avoid
471 * mistaking this with unlimited (i.e. limit = 0).
472 */
473 limit = 1;
474 else
475 limit = pack_size_limit - write_offset;
2c5ef824 476
898eba5e 477 if (!DELTA(entry))
2c5ef824
NP
478 usable_delta = 0; /* no delta */
479 else if (!pack_size_limit)
480 usable_delta = 1; /* unlimited packfile */
898eba5e 481 else if (DELTA(entry)->idx.offset == (off_t)-1)
2c5ef824 482 usable_delta = 0; /* base was written to another pack */
898eba5e 483 else if (DELTA(entry)->idx.offset)
2c5ef824
NP
484 usable_delta = 1; /* base already exists in this pack */
485 else
486 usable_delta = 0; /* base could end up in another pack */
487
a7de7130 488 if (!reuse_object)
fa736f72 489 to_reuse = 0; /* explicit */
43fa44fa 490 else if (!IN_PACK(entry))
ab7cd7bb 491 to_reuse = 0; /* can't reuse what we don't have */
fd9b1bae
NTND
492 else if (oe_type(entry) == OBJ_REF_DELTA ||
493 oe_type(entry) == OBJ_OFS_DELTA)
17b08f2c
DH
494 /* check_object() decided it for us ... */
495 to_reuse = usable_delta;
496 /* ... but pack split may override that */
fd9b1bae 497 else if (oe_type(entry) != entry->in_pack_type)
ab7cd7bb 498 to_reuse = 0; /* pack has delta which is unusable */
898eba5e 499 else if (DELTA(entry))
ab7cd7bb
JH
500 to_reuse = 0; /* we want to pack afresh */
501 else
502 to_reuse = 1; /* we have it in-pack undeltified,
503 * and we do not need to deltify it.
504 */
505
c9018b03
NTND
506 if (!to_reuse)
507 len = write_no_reuse_object(f, entry, limit, usable_delta);
508 else
509 len = write_reuse_object(f, entry, limit, usable_delta);
510 if (!len)
511 return 0;
64bd76b1 512
17b08f2c 513 if (usable_delta)
ab7cd7bb 514 written_delta++;
3f9ac8d2 515 written++;
78d1e84f 516 if (!pack_to_stdout)
aa7e44bf 517 entry->idx.crc32 = crc32_end(f);
c9018b03 518 return len;
c323ac7d
LT
519}
520
f63c79db
JH
521enum write_one_status {
522 WRITE_ONE_SKIP = -1, /* already written */
523 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
524 WRITE_ONE_WRITTEN = 1, /* normal */
525 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
526};
527
98a3beab 528static enum write_one_status write_one(struct hashfile *f,
f63c79db
JH
529 struct object_entry *e,
530 off_t *offset)
9d5ab962 531{
af92a645 532 off_t size;
f63c79db 533 int recursing;
d7dd0223 534
f63c79db
JH
535 /*
536 * we set offset to 1 (which is an impossible value) to mark
537 * the fact that this object is involved in "write its base
538 * first before writing a deltified object" recursion.
539 */
540 recursing = (e->idx.offset == 1);
541 if (recursing) {
542 warning("recursive delta detected for object %s",
e6a492b7 543 oid_to_hex(&e->idx.oid));
f63c79db
JH
544 return WRITE_ONE_RECURSIVE;
545 } else if (e->idx.offset || e->preferred_base) {
546 /* offset is non zero if object is written already. */
547 return WRITE_ONE_SKIP;
548 }
d7dd0223 549
720c9f7b 550 /* if we are deltified, write out base object first. */
898eba5e 551 if (DELTA(e)) {
f63c79db 552 e->idx.offset = 1; /* now recurse */
898eba5e 553 switch (write_one(f, DELTA(e), offset)) {
f63c79db
JH
554 case WRITE_ONE_RECURSIVE:
555 /* we cannot depend on this one */
898eba5e 556 SET_DELTA(e, NULL);
f63c79db
JH
557 break;
558 default:
559 break;
560 case WRITE_ONE_BREAK:
561 e->idx.offset = recursing;
562 return WRITE_ONE_BREAK;
563 }
564 }
d7dd0223 565
6ed7f25e
NP
566 e->idx.offset = *offset;
567 size = write_object(f, e, *offset);
17b08f2c 568 if (!size) {
f63c79db
JH
569 e->idx.offset = recursing;
570 return WRITE_ONE_BREAK;
17b08f2c 571 }
79814f42 572 written_list[nr_written++] = &e->idx;
d7dd0223
NP
573
574 /* make sure off_t is sufficiently large not to wrap */
c03c8315 575 if (signed_add_overflows(*offset, size))
d7dd0223 576 die("pack too large for current definition of off_t");
6ed7f25e 577 *offset += size;
f63c79db 578 return WRITE_ONE_WRITTEN;
9d5ab962
JH
579}
580
d155254c 581static int mark_tagged(const char *path, const struct object_id *oid, int flag,
1b4bb16b
JH
582 void *cb_data)
583{
188960b4 584 struct object_id peeled;
d155254c 585 struct object_entry *entry = packlist_find(&to_pack, oid->hash, NULL);
1b4bb16b
JH
586
587 if (entry)
588 entry->tagged = 1;
b420d909 589 if (!peel_ref(path, &peeled)) {
188960b4 590 entry = packlist_find(&to_pack, peeled.hash, NULL);
1b4bb16b
JH
591 if (entry)
592 entry->tagged = 1;
593 }
594 return 0;
595}
596
be126818 597static inline void add_to_write_order(struct object_entry **wo,
92bef1a1 598 unsigned int *endp,
1b4bb16b
JH
599 struct object_entry *e)
600{
601 if (e->filled)
602 return;
603 wo[(*endp)++] = e;
604 e->filled = 1;
605}
606
607static void add_descendants_to_write_order(struct object_entry **wo,
92bef1a1 608 unsigned int *endp,
1b4bb16b
JH
609 struct object_entry *e)
610{
f380872f
DM
611 int add_to_order = 1;
612 while (e) {
613 if (add_to_order) {
614 struct object_entry *s;
615 /* add this node... */
616 add_to_write_order(wo, endp, e);
617 /* all its siblings... */
898eba5e 618 for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) {
f380872f
DM
619 add_to_write_order(wo, endp, s);
620 }
621 }
622 /* drop down a level to add left subtree nodes if possible */
898eba5e 623 if (DELTA_CHILD(e)) {
f380872f 624 add_to_order = 1;
898eba5e 625 e = DELTA_CHILD(e);
f380872f
DM
626 } else {
627 add_to_order = 0;
628 /* our sibling might have some children, it is next */
898eba5e
NTND
629 if (DELTA_SIBLING(e)) {
630 e = DELTA_SIBLING(e);
f380872f
DM
631 continue;
632 }
633 /* go back to our parent node */
898eba5e
NTND
634 e = DELTA(e);
635 while (e && !DELTA_SIBLING(e)) {
f380872f
DM
636 /* we're on the right side of a subtree, keep
637 * going up until we can go right again */
898eba5e 638 e = DELTA(e);
f380872f
DM
639 }
640 if (!e) {
641 /* done- we hit our original root node */
642 return;
643 }
644 /* pass it off to sibling at this level */
898eba5e 645 e = DELTA_SIBLING(e);
f380872f
DM
646 }
647 };
1b4bb16b
JH
648}
649
650static void add_family_to_write_order(struct object_entry **wo,
92bef1a1 651 unsigned int *endp,
1b4bb16b
JH
652 struct object_entry *e)
653{
654 struct object_entry *root;
655
898eba5e 656 for (root = e; DELTA(root); root = DELTA(root))
1b4bb16b 657 ; /* nothing */
1b4bb16b
JH
658 add_descendants_to_write_order(wo, endp, root);
659}
660
661static struct object_entry **compute_write_order(void)
662{
38d4debb 663 unsigned int i, wo_end, last_untagged;
1b4bb16b 664
b32fa95f 665 struct object_entry **wo;
2834bc27 666 struct object_entry *objects = to_pack.objects;
1b4bb16b 667
2834bc27 668 for (i = 0; i < to_pack.nr_objects; i++) {
1b4bb16b
JH
669 objects[i].tagged = 0;
670 objects[i].filled = 0;
898eba5e
NTND
671 SET_DELTA_CHILD(&objects[i], NULL);
672 SET_DELTA_SIBLING(&objects[i], NULL);
1b4bb16b
JH
673 }
674
675 /*
676 * Fully connect delta_child/delta_sibling network.
677 * Make sure delta_sibling is sorted in the original
678 * recency order.
679 */
2834bc27 680 for (i = to_pack.nr_objects; i > 0;) {
92bef1a1 681 struct object_entry *e = &objects[--i];
898eba5e 682 if (!DELTA(e))
1b4bb16b
JH
683 continue;
684 /* Mark me as the first child */
898eba5e
NTND
685 e->delta_sibling_idx = DELTA(e)->delta_child_idx;
686 SET_DELTA_CHILD(DELTA(e), e);
1b4bb16b
JH
687 }
688
689 /*
690 * Mark objects that are at the tip of tags.
691 */
d155254c 692 for_each_tag_ref(mark_tagged, NULL);
1b4bb16b
JH
693
694 /*
38d4debb 695 * Give the objects in the original recency order until
1b4bb16b
JH
696 * we see a tagged tip.
697 */
b32fa95f 698 ALLOC_ARRAY(wo, to_pack.nr_objects);
2834bc27 699 for (i = wo_end = 0; i < to_pack.nr_objects; i++) {
1b4bb16b
JH
700 if (objects[i].tagged)
701 break;
702 add_to_write_order(wo, &wo_end, &objects[i]);
703 }
38d4debb 704 last_untagged = i;
1b4bb16b
JH
705
706 /*
707 * Then fill all the tagged tips.
708 */
2834bc27 709 for (; i < to_pack.nr_objects; i++) {
1b4bb16b
JH
710 if (objects[i].tagged)
711 add_to_write_order(wo, &wo_end, &objects[i]);
712 }
713
714 /*
715 * And then all remaining commits and tags.
716 */
2834bc27 717 for (i = last_untagged; i < to_pack.nr_objects; i++) {
fd9b1bae
NTND
718 if (oe_type(&objects[i]) != OBJ_COMMIT &&
719 oe_type(&objects[i]) != OBJ_TAG)
1b4bb16b
JH
720 continue;
721 add_to_write_order(wo, &wo_end, &objects[i]);
722 }
723
724 /*
725 * And then all the trees.
726 */
2834bc27 727 for (i = last_untagged; i < to_pack.nr_objects; i++) {
fd9b1bae 728 if (oe_type(&objects[i]) != OBJ_TREE)
1b4bb16b
JH
729 continue;
730 add_to_write_order(wo, &wo_end, &objects[i]);
731 }
732
733 /*
734 * Finally all the rest in really tight order
735 */
2834bc27 736 for (i = last_untagged; i < to_pack.nr_objects; i++) {
38d4debb
DM
737 if (!objects[i].filled)
738 add_family_to_write_order(wo, &wo_end, &objects[i]);
739 }
740
2834bc27
VM
741 if (wo_end != to_pack.nr_objects)
742 die("ordered %u objects, expected %"PRIu32, wo_end, to_pack.nr_objects);
1b4bb16b
JH
743
744 return wo;
745}
746
98a3beab 747static off_t write_reused_pack(struct hashfile *f)
6b8fda2d
VM
748{
749 unsigned char buffer[8192];
657673f1 750 off_t to_write, total;
6b8fda2d
VM
751 int fd;
752
753 if (!is_pack_valid(reuse_packfile))
754 die("packfile is invalid: %s", reuse_packfile->pack_name);
755
a5436b57 756 fd = git_open(reuse_packfile->pack_name);
6b8fda2d
VM
757 if (fd < 0)
758 die_errno("unable to open packfile for reuse: %s",
759 reuse_packfile->pack_name);
760
761 if (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1)
762 die_errno("unable to seek in reused packfile");
763
764 if (reuse_packfile_offset < 0)
765 reuse_packfile_offset = reuse_packfile->pack_size - 20;
766
657673f1 767 total = to_write = reuse_packfile_offset - sizeof(struct pack_header);
6b8fda2d
VM
768
769 while (to_write) {
770 int read_pack = xread(fd, buffer, sizeof(buffer));
771
772 if (read_pack <= 0)
773 die_errno("unable to read from reused packfile");
774
775 if (read_pack > to_write)
776 read_pack = to_write;
777
98a3beab 778 hashwrite(f, buffer, read_pack);
6b8fda2d 779 to_write -= read_pack;
657673f1
JK
780
781 /*
782 * We don't know the actual number of objects written,
783 * only how many bytes written, how many bytes total, and
784 * how many objects total. So we can fake it by pretending all
785 * objects we are writing are the same size. This gives us a
786 * smooth progress meter, and at the end it matches the true
787 * answer.
788 */
789 written = reuse_packfile_objects *
790 (((double)(total - to_write)) / total);
791 display_progress(progress_state, written);
6b8fda2d
VM
792 }
793
794 close(fd);
657673f1
JK
795 written = reuse_packfile_objects;
796 display_progress(progress_state, written);
6b8fda2d
VM
797 return reuse_packfile_offset - sizeof(struct pack_header);
798}
799
9cea46cd
EW
800static const char no_split_warning[] = N_(
801"disabling bitmap writing, packs are split due to pack.packSizeLimit"
802);
803
d01fb92f 804static void write_pack_file(void)
c323ac7d 805{
ebe27b13 806 uint32_t i = 0, j;
98a3beab 807 struct hashfile *f;
6ed7f25e 808 off_t offset;
ebe27b13 809 uint32_t nr_remaining = nr_result;
f746bae8 810 time_t last_mtime = 0;
1b4bb16b 811 struct object_entry **write_order;
81a216a5 812
bcd7954e 813 if (progress > pack_to_stdout)
754dbc43 814 progress_state = start_progress(_("Writing objects"), nr_result);
b32fa95f 815 ALLOC_ARRAY(written_list, to_pack.nr_objects);
1b4bb16b 816 write_order = compute_write_order();
183bdb2c 817
ebe27b13 818 do {
188960b4 819 struct object_id oid;
7ba502c4 820 char *pack_tmp_name = NULL;
aa7e44bf 821
cdf9db3c 822 if (pack_to_stdout)
98a3beab 823 f = hashfd_throughput(1, "<stdout>", progress_state);
cdf9db3c
JH
824 else
825 f = create_tmp_packfile(&pack_tmp_name);
ebe27b13 826
c0ad4657 827 offset = write_pack_header(f, nr_remaining);
6b8fda2d
VM
828
829 if (reuse_packfile) {
830 off_t packfile_size;
831 assert(pack_to_stdout);
832
833 packfile_size = write_reused_pack(f);
834 offset += packfile_size;
835 }
836
ebe27b13 837 nr_written = 0;
2834bc27 838 for (; i < to_pack.nr_objects; i++) {
1b4bb16b 839 struct object_entry *e = write_order[i];
cddec4f8 840 if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
720c9f7b
NP
841 break;
842 display_progress(progress_state, written);
843 }
c553ca25 844
ebe27b13
DH
845 /*
846 * Did we write the wrong # entries in the header?
847 * If so, rewrite it like in fast-import
848 */
54352bb2 849 if (pack_to_stdout) {
98a3beab 850 hashclose(f, oid.hash, CSUM_CLOSE);
54352bb2 851 } else if (nr_written == nr_remaining) {
98a3beab 852 hashclose(f, oid.hash, CSUM_FSYNC);
ebe27b13 853 } else {
98a3beab 854 int fd = hashclose(f, oid.hash, 0);
188960b4 855 fixup_pack_header_footer(fd, oid.hash, pack_tmp_name,
856 nr_written, oid.hash, offset);
7ba502c4 857 close(fd);
9cea46cd
EW
858 if (write_bitmap_index) {
859 warning(_(no_split_warning));
860 write_bitmap_index = 0;
861 }
ebe27b13
DH
862 }
863
864 if (!pack_to_stdout) {
f746bae8 865 struct stat st;
58892711 866 struct strbuf tmpname = STRBUF_INIT;
d01fb92f 867
f746bae8
NP
868 /*
869 * Packs are runtime accessed in their mtime
870 * order since newer packs are more likely to contain
871 * younger objects. So if we are creating multiple
872 * packs then we should modify the mtime of later ones
873 * to preserve this property.
874 */
0e990530 875 if (stat(pack_tmp_name, &st) < 0) {
54d47394 876 warning_errno("failed to stat %s", pack_tmp_name);
f746bae8
NP
877 } else if (!last_mtime) {
878 last_mtime = st.st_mtime;
879 } else {
880 struct utimbuf utb;
881 utb.actime = st.st_atime;
882 utb.modtime = --last_mtime;
0e990530 883 if (utime(pack_tmp_name, &utb) < 0)
54d47394 884 warning_errno("failed utime() on %s", pack_tmp_name);
f746bae8
NP
885 }
886
58892711 887 strbuf_addf(&tmpname, "%s-", base_name);
7cc8f971
VM
888
889 if (write_bitmap_index) {
188960b4 890 bitmap_writer_set_checksum(oid.hash);
06af3bba
NTND
891 bitmap_writer_build_type_index(
892 &to_pack, written_list, nr_written);
7cc8f971
VM
893 }
894
58892711 895 finish_tmp_packfile(&tmpname, pack_tmp_name,
0e990530 896 written_list, nr_written,
188960b4 897 &pack_idx_opts, oid.hash);
7cc8f971
VM
898
899 if (write_bitmap_index) {
188960b4 900 strbuf_addf(&tmpname, "%s.bitmap", oid_to_hex(&oid));
7cc8f971
VM
901
902 stop_progress(&progress_state);
903
904 bitmap_writer_show_progress(progress);
905 bitmap_writer_reuse_bitmaps(&to_pack);
906 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
907 bitmap_writer_build(&to_pack);
ae4f07fb 908 bitmap_writer_finish(written_list, nr_written,
58892711 909 tmpname.buf, write_bitmap_options);
7cc8f971
VM
910 write_bitmap_index = 0;
911 }
912
58892711 913 strbuf_release(&tmpname);
7ba502c4 914 free(pack_tmp_name);
188960b4 915 puts(oid_to_hex(&oid));
ebe27b13
DH
916 }
917
918 /* mark written objects as written to previous pack */
919 for (j = 0; j < nr_written; j++) {
79814f42 920 written_list[j]->offset = (off_t)-1;
ebe27b13
DH
921 }
922 nr_remaining -= nr_written;
2834bc27 923 } while (nr_remaining && i < to_pack.nr_objects);
ebe27b13
DH
924
925 free(written_list);
1b4bb16b 926 free(write_order);
4d4fcc54 927 stop_progress(&progress_state);
67c08ce1 928 if (written != nr_result)
6e1c2344
RJ
929 die("wrote %"PRIu32" objects while expecting %"PRIu32,
930 written, nr_result);
c323ac7d
LT
931}
932
a74db82e
JH
933static int no_try_delta(const char *path)
934{
2aef63d3 935 static struct attr_check *check;
a74db82e 936
2aef63d3
JH
937 if (!check)
938 check = attr_check_initl("delta", NULL);
939 if (git_check_attr(path, check))
a74db82e 940 return 0;
2aef63d3 941 if (ATTR_FALSE(check->items[0].value))
a74db82e
JH
942 return 1;
943 return 0;
944}
945
ce2bc424
JK
946/*
947 * When adding an object, check whether we have already added it
948 * to our packing list. If so, we can skip. However, if we are
949 * being asked to excludei t, but the previous mention was to include
950 * it, make sure to adjust its flags and tweak our numbers accordingly.
951 *
952 * As an optimization, we pass out the index position where we would have
953 * found the item, since that saves us from having to look it up again a
954 * few lines later when we want to add the new entry.
955 */
188960b4 956static int have_duplicate_entry(const struct object_id *oid,
ce2bc424
JK
957 int exclude,
958 uint32_t *index_pos)
c323ac7d 959{
c323ac7d 960 struct object_entry *entry;
29b734e4 961
188960b4 962 entry = packlist_find(&to_pack, oid->hash, index_pos);
ce2bc424 963 if (!entry)
29b734e4 964 return 0;
ce2bc424
JK
965
966 if (exclude) {
967 if (!entry->preferred_base)
968 nr_result--;
969 entry->preferred_base = 1;
29b734e4 970 }
c323ac7d 971
ce2bc424
JK
972 return 1;
973}
974
702d1b95
KS
975static int want_found_object(int exclude, struct packed_git *p)
976{
977 if (exclude)
978 return 1;
979 if (incremental)
980 return 0;
981
982 /*
983 * When asked to do --local (do not include an object that appears in a
984 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
985 * an object that appears in a pack marked with .keep), finding a pack
986 * that matches the criteria is sufficient for us to decide to omit it.
987 * However, even if this pack does not satisfy the criteria, we need to
988 * make sure no copy of this object appears in _any_ pack that makes us
989 * to omit the object, so we need to check all the packs.
990 *
991 * We can however first check whether these options can possible matter;
992 * if they do not matter we know we want the object in generated pack.
993 * Otherwise, we signal "-1" at the end to tell the caller that we do
994 * not know either way, and it needs to check more packs.
995 */
996 if (!ignore_packed_keep &&
997 (!local || !have_non_local_packs))
998 return 1;
999
1000 if (local && !p->pack_local)
1001 return 0;
1002 if (ignore_packed_keep && p->pack_local && p->pack_keep)
1003 return 0;
1004
1005 /* we don't know yet; keep looking for more packs */
1006 return -1;
1007}
1008
ce2bc424
JK
1009/*
1010 * Check whether we want the object in the pack (e.g., we do not want
1011 * objects found in non-local stores if the "--local" option was used).
1012 *
702d1b95
KS
1013 * If the caller already knows an existing pack it wants to take the object
1014 * from, that is passed in *found_pack and *found_offset; otherwise this
1015 * function finds if there is any pack that has the object and returns the pack
1016 * and its offset in these variables.
ce2bc424 1017 */
188960b4 1018static int want_object_in_pack(const struct object_id *oid,
ce2bc424
JK
1019 int exclude,
1020 struct packed_git **found_pack,
1021 off_t *found_offset)
1022{
702d1b95 1023 int want;
8865859d 1024 struct list_head *pos;
ce2bc424 1025
188960b4 1026 if (!exclude && local && has_loose_object_nonlocal(oid->hash))
daae0625
BC
1027 return 0;
1028
702d1b95
KS
1029 /*
1030 * If we already know the pack object lives in, start checks from that
1031 * pack - in the usual case when neither --local was given nor .keep files
1032 * are present we will determine the answer right now.
1033 */
1034 if (*found_pack) {
1035 want = want_found_object(exclude, *found_pack);
1036 if (want != -1)
1037 return want;
1038 }
a80d72db 1039 list_for_each(pos, get_packed_git_mru(the_repository)) {
ec2dd32c 1040 struct packed_git *p = list_entry(pos, struct packed_git, mru);
702d1b95
KS
1041 off_t offset;
1042
1043 if (p == *found_pack)
1044 offset = *found_offset;
1045 else
188960b4 1046 offset = find_pack_entry_one(oid->hash, p);
702d1b95 1047
5c49c116 1048 if (offset) {
ce2bc424 1049 if (!*found_pack) {
319b678a 1050 if (!is_pack_valid(p))
4c080182 1051 continue;
ce2bc424
JK
1052 *found_offset = offset;
1053 *found_pack = p;
64560374 1054 }
702d1b95 1055 want = want_found_object(exclude, p);
e6e24c94 1056 if (!exclude && want > 0)
a80d72db
SB
1057 list_move(&p->mru,
1058 get_packed_git_mru(the_repository));
702d1b95
KS
1059 if (want != -1)
1060 return want;
64560374
LT
1061 }
1062 }
eb019375 1063
ce2bc424
JK
1064 return 1;
1065}
1066
188960b4 1067static void create_object_entry(const struct object_id *oid,
ce2bc424
JK
1068 enum object_type type,
1069 uint32_t hash,
1070 int exclude,
1071 int no_try_delta,
1072 uint32_t index_pos,
1073 struct packed_git *found_pack,
1074 off_t found_offset)
1075{
1076 struct object_entry *entry;
29b734e4 1077
188960b4 1078 entry = packlist_alloc(&to_pack, oid->hash, index_pos);
27225f2e 1079 entry->hash = hash;
fd9b1bae 1080 oe_set_type(entry, type);
29b734e4
NP
1081 if (exclude)
1082 entry->preferred_base = 1;
81a216a5
NP
1083 else
1084 nr_result++;
29b734e4 1085 if (found_pack) {
43fa44fa 1086 oe_set_in_pack(&to_pack, entry, found_pack);
29b734e4
NP
1087 entry->in_pack_offset = found_offset;
1088 }
7a979d99 1089
ce2bc424
JK
1090 entry->no_try_delta = no_try_delta;
1091}
29b734e4 1092
373c67da
JK
1093static const char no_closure_warning[] = N_(
1094"disabling bitmap writing, as some objects are not being packed"
1095);
1096
188960b4 1097static int add_object_entry(const struct object_id *oid, enum object_type type,
ce2bc424
JK
1098 const char *name, int exclude)
1099{
702d1b95
KS
1100 struct packed_git *found_pack = NULL;
1101 off_t found_offset = 0;
ce2bc424 1102 uint32_t index_pos;
7a979d99 1103
188960b4 1104 if (have_duplicate_entry(oid, exclude, &index_pos))
ce2bc424 1105 return 0;
a74db82e 1106
188960b4 1107 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {
373c67da
JK
1108 /* The pack is missing an object, so it will not have closure */
1109 if (write_bitmap_index) {
1110 warning(_(no_closure_warning));
1111 write_bitmap_index = 0;
1112 }
ce2bc424 1113 return 0;
373c67da 1114 }
29b734e4 1115
188960b4 1116 create_object_entry(oid, type, pack_name_hash(name),
ce2bc424
JK
1117 exclude, name && no_try_delta(name),
1118 index_pos, found_pack, found_offset);
a74db82e 1119
78d2214e 1120 display_progress(progress_state, nr_result);
29b734e4 1121 return 1;
c323ac7d
LT
1122}
1123
20664967 1124static int add_object_entry_from_bitmap(const struct object_id *oid,
6b8fda2d
VM
1125 enum object_type type,
1126 int flags, uint32_t name_hash,
1127 struct packed_git *pack, off_t offset)
1128{
1129 uint32_t index_pos;
1130
188960b4 1131 if (have_duplicate_entry(oid, 0, &index_pos))
6b8fda2d
VM
1132 return 0;
1133
188960b4 1134 if (!want_object_in_pack(oid, 0, &pack, &offset))
702d1b95
KS
1135 return 0;
1136
188960b4 1137 create_object_entry(oid, type, name_hash, 0, 0, index_pos, pack, offset);
6b8fda2d 1138
78d2214e 1139 display_progress(progress_state, nr_result);
29b734e4 1140 return 1;
c323ac7d
LT
1141}
1142
5379a5c5 1143struct pbase_tree_cache {
188960b4 1144 struct object_id oid;
5379a5c5
JH
1145 int ref;
1146 int temporary;
1147 void *tree_data;
1148 unsigned long tree_size;
1149};
1150
1151static struct pbase_tree_cache *(pbase_tree_cache[256]);
188960b4 1152static int pbase_tree_cache_ix(const struct object_id *oid)
5379a5c5 1153{
188960b4 1154 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);
5379a5c5
JH
1155}
1156static int pbase_tree_cache_ix_incr(int ix)
1157{
1158 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1159}
1160
1161static struct pbase_tree {
1162 struct pbase_tree *next;
1163 /* This is a phony "cache" entry; we are not
01689909 1164 * going to evict it or find it through _get()
5379a5c5
JH
1165 * mechanism -- this is for the toplevel node that
1166 * would almost always change with any commit.
1167 */
1168 struct pbase_tree_cache pcache;
1169} *pbase_tree;
1170
188960b4 1171static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)
5379a5c5
JH
1172{
1173 struct pbase_tree_cache *ent, *nent;
1174 void *data;
1175 unsigned long size;
21666f1a 1176 enum object_type type;
5379a5c5 1177 int neigh;
188960b4 1178 int my_ix = pbase_tree_cache_ix(oid);
5379a5c5
JH
1179 int available_ix = -1;
1180
1181 /* pbase-tree-cache acts as a limited hashtable.
1182 * your object will be found at your index or within a few
1183 * slots after that slot if it is cached.
1184 */
1185 for (neigh = 0; neigh < 8; neigh++) {
1186 ent = pbase_tree_cache[my_ix];
188960b4 1187 if (ent && !oidcmp(&ent->oid, oid)) {
5379a5c5
JH
1188 ent->ref++;
1189 return ent;
1190 }
1191 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1192 ((0 <= available_ix) &&
1193 (!ent && pbase_tree_cache[available_ix])))
1194 available_ix = my_ix;
1195 if (!ent)
1196 break;
1197 my_ix = pbase_tree_cache_ix_incr(my_ix);
1198 }
1199
1200 /* Did not find one. Either we got a bogus request or
1201 * we need to read and perhaps cache.
1202 */
b4f5aca4 1203 data = read_object_file(oid, &type, &size);
5379a5c5
JH
1204 if (!data)
1205 return NULL;
21666f1a 1206 if (type != OBJ_TREE) {
5379a5c5
JH
1207 free(data);
1208 return NULL;
1209 }
1210
1211 /* We need to either cache or return a throwaway copy */
1212
1213 if (available_ix < 0)
1214 ent = NULL;
1215 else {
1216 ent = pbase_tree_cache[available_ix];
1217 my_ix = available_ix;
1218 }
1219
1220 if (!ent) {
1221 nent = xmalloc(sizeof(*nent));
1222 nent->temporary = (available_ix < 0);
1223 }
1224 else {
1225 /* evict and reuse */
1226 free(ent->tree_data);
1227 nent = ent;
1228 }
188960b4 1229 oidcpy(&nent->oid, oid);
5379a5c5
JH
1230 nent->tree_data = data;
1231 nent->tree_size = size;
1232 nent->ref = 1;
1233 if (!nent->temporary)
1234 pbase_tree_cache[my_ix] = nent;
1235 return nent;
1236}
1237
1238static void pbase_tree_put(struct pbase_tree_cache *cache)
1239{
1240 if (!cache->temporary) {
1241 cache->ref--;
1242 return;
1243 }
1244 free(cache->tree_data);
1245 free(cache);
1246}
1247
1248static int name_cmp_len(const char *name)
1249{
1250 int i;
1251 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1252 ;
1253 return i;
1254}
1255
1256static void add_pbase_object(struct tree_desc *tree,
5379a5c5 1257 const char *name,
ce0bd642
LT
1258 int cmplen,
1259 const char *fullname)
3f9ac8d2 1260{
4c068a98 1261 struct name_entry entry;
8a5a8d6c 1262 int cmp;
4c068a98
LT
1263
1264 while (tree_entry(tree,&entry)) {
1211be6b
LT
1265 if (S_ISGITLINK(entry.mode))
1266 continue;
0de16337 1267 cmp = tree_entry_len(&entry) != cmplen ? 1 :
8a5a8d6c
NP
1268 memcmp(name, entry.path, cmplen);
1269 if (cmp > 0)
7a979d99 1270 continue;
8a5a8d6c
NP
1271 if (cmp < 0)
1272 return;
5379a5c5 1273 if (name[cmplen] != '/') {
188960b4 1274 add_object_entry(entry.oid,
4d1012c3 1275 object_type(entry.mode),
bc32fed5 1276 fullname, 1);
5379a5c5
JH
1277 return;
1278 }
8a5a8d6c 1279 if (S_ISDIR(entry.mode)) {
7a979d99 1280 struct tree_desc sub;
5379a5c5
JH
1281 struct pbase_tree_cache *tree;
1282 const char *down = name+cmplen+1;
1283 int downlen = name_cmp_len(down);
1284
188960b4 1285 tree = pbase_tree_get(entry.oid);
5379a5c5
JH
1286 if (!tree)
1287 return;
6fda5e51 1288 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
5379a5c5 1289
ce0bd642 1290 add_pbase_object(&sub, down, downlen, fullname);
5379a5c5
JH
1291 pbase_tree_put(tree);
1292 }
1293 }
1294}
1d6b38cc 1295
5379a5c5
JH
1296static unsigned *done_pbase_paths;
1297static int done_pbase_paths_num;
1298static int done_pbase_paths_alloc;
1299static int done_pbase_path_pos(unsigned hash)
1300{
1301 int lo = 0;
1302 int hi = done_pbase_paths_num;
1303 while (lo < hi) {
19716b21 1304 int mi = lo + (hi - lo) / 2;
5379a5c5
JH
1305 if (done_pbase_paths[mi] == hash)
1306 return mi;
1307 if (done_pbase_paths[mi] < hash)
1308 hi = mi;
1309 else
1310 lo = mi + 1;
1311 }
1312 return -lo-1;
1313}
1314
1315static int check_pbase_path(unsigned hash)
1316{
c7b07805 1317 int pos = done_pbase_path_pos(hash);
5379a5c5
JH
1318 if (0 <= pos)
1319 return 1;
1320 pos = -pos - 1;
25e19407
DD
1321 ALLOC_GROW(done_pbase_paths,
1322 done_pbase_paths_num + 1,
1323 done_pbase_paths_alloc);
5379a5c5
JH
1324 done_pbase_paths_num++;
1325 if (pos < done_pbase_paths_num)
f331ab9d
RS
1326 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,
1327 done_pbase_paths_num - pos - 1);
5379a5c5
JH
1328 done_pbase_paths[pos] = hash;
1329 return 0;
1330}
1331
bc32fed5 1332static void add_preferred_base_object(const char *name)
5379a5c5
JH
1333{
1334 struct pbase_tree *it;
8a5a8d6c 1335 int cmplen;
68fb36eb 1336 unsigned hash = pack_name_hash(name);
5379a5c5 1337
8a5a8d6c 1338 if (!num_preferred_base || check_pbase_path(hash))
5379a5c5
JH
1339 return;
1340
8a5a8d6c 1341 cmplen = name_cmp_len(name);
5379a5c5
JH
1342 for (it = pbase_tree; it; it = it->next) {
1343 if (cmplen == 0) {
188960b4 1344 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);
5379a5c5
JH
1345 }
1346 else {
1347 struct tree_desc tree;
6fda5e51 1348 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
ce0bd642 1349 add_pbase_object(&tree, name, cmplen, name);
7a979d99 1350 }
3f9ac8d2 1351 }
3f9ac8d2
JH
1352}
1353
188960b4 1354static void add_preferred_base(struct object_id *oid)
3f9ac8d2 1355{
5379a5c5
JH
1356 struct pbase_tree *it;
1357 void *data;
1358 unsigned long size;
188960b4 1359 struct object_id tree_oid;
1d6b38cc 1360
8d1d8f83
JH
1361 if (window <= num_preferred_base++)
1362 return;
1363
02f0547e 1364 data = read_object_with_reference(oid, tree_type, &size, &tree_oid);
5379a5c5 1365 if (!data)
7a979d99 1366 return;
5379a5c5
JH
1367
1368 for (it = pbase_tree; it; it = it->next) {
188960b4 1369 if (!oidcmp(&it->pcache.oid, &tree_oid)) {
5379a5c5
JH
1370 free(data);
1371 return;
1372 }
1373 }
1374
1375 it = xcalloc(1, sizeof(*it));
1376 it->next = pbase_tree;
1377 pbase_tree = it;
1378
188960b4 1379 oidcpy(&it->pcache.oid, &tree_oid);
5379a5c5
JH
1380 it->pcache.tree_data = data;
1381 it->pcache.tree_size = size;
3f9ac8d2
JH
1382}
1383
0ef95f72
NP
1384static void cleanup_preferred_base(void)
1385{
1386 struct pbase_tree *it;
1387 unsigned i;
1388
1389 it = pbase_tree;
1390 pbase_tree = NULL;
1391 while (it) {
095b3b2c
BW
1392 struct pbase_tree *tmp = it;
1393 it = tmp->next;
1394 free(tmp->pcache.tree_data);
1395 free(tmp);
0ef95f72
NP
1396 }
1397
1398 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
1399 if (!pbase_tree_cache[i])
1400 continue;
1401 free(pbase_tree_cache[i]->tree_data);
6a83d902 1402 FREE_AND_NULL(pbase_tree_cache[i]);
0ef95f72
NP
1403 }
1404
6a83d902 1405 FREE_AND_NULL(done_pbase_paths);
0ef95f72
NP
1406 done_pbase_paths_num = done_pbase_paths_alloc = 0;
1407}
1408
c323ac7d
LT
1409static void check_object(struct object_entry *entry)
1410{
43fa44fa
NTND
1411 if (IN_PACK(entry)) {
1412 struct packed_git *p = IN_PACK(entry);
03e79c88 1413 struct pack_window *w_curs = NULL;
5c49c116
NP
1414 const unsigned char *base_ref = NULL;
1415 struct object_entry *base_entry;
1416 unsigned long used, used_0;
ef49a7a0 1417 unsigned long avail;
5c49c116
NP
1418 off_t ofs;
1419 unsigned char *buf, c;
fd9b1bae 1420 enum object_type type;
27a7d067 1421 unsigned long in_pack_size;
780e6e73 1422
6777a59f 1423 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
ab7cd7bb 1424
5c49c116 1425 /*
fa736f72
NP
1426 * We want in_pack_type even if we do not reuse delta
1427 * since non-delta representations could still be reused.
ab7cd7bb 1428 */
09ded04b 1429 used = unpack_object_header_buffer(buf, avail,
fd9b1bae 1430 &type,
27a7d067 1431 &in_pack_size);
03d66015
NP
1432 if (used == 0)
1433 goto give_up;
ab7cd7bb 1434
fd9b1bae
NTND
1435 if (type < 0)
1436 BUG("invalid type %d", type);
1437 entry->in_pack_type = type;
1438
5c49c116
NP
1439 /*
1440 * Determine if this is a delta and if so whether we can
1441 * reuse it or not. Otherwise let's find out as cheaply as
1442 * possible what the actual type and size for this object is.
3f9ac8d2 1443 */
5c49c116
NP
1444 switch (entry->in_pack_type) {
1445 default:
1446 /* Not a delta hence we've already got all we need. */
fd9b1bae 1447 oe_set_type(entry, entry->in_pack_type);
27a7d067 1448 entry->size = in_pack_size;
5c49c116 1449 entry->in_pack_header_size = used;
fd9b1bae 1450 if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)
03d66015 1451 goto give_up;
5c49c116
NP
1452 unuse_pack(&w_curs);
1453 return;
1454 case OBJ_REF_DELTA:
a7de7130 1455 if (reuse_delta && !entry->preferred_base)
5c49c116
NP
1456 base_ref = use_pack(p, &w_curs,
1457 entry->in_pack_offset + used, NULL);
1458 entry->in_pack_header_size = used + 20;
1459 break;
1460 case OBJ_OFS_DELTA:
1461 buf = use_pack(p, &w_curs,
1462 entry->in_pack_offset + used, NULL);
1463 used_0 = 0;
1464 c = buf[used_0++];
1465 ofs = c & 127;
1466 while (c & 128) {
1467 ofs += 1;
03d66015
NP
1468 if (!ofs || MSB(ofs, 7)) {
1469 error("delta base offset overflow in pack for %s",
e6a492b7 1470 oid_to_hex(&entry->idx.oid));
03d66015
NP
1471 goto give_up;
1472 }
5c49c116
NP
1473 c = buf[used_0++];
1474 ofs = (ofs << 7) + (c & 127);
780e6e73 1475 }
5c49c116 1476 ofs = entry->in_pack_offset - ofs;
03d66015
NP
1477 if (ofs <= 0 || ofs >= entry->in_pack_offset) {
1478 error("delta base offset out of bound for %s",
e6a492b7 1479 oid_to_hex(&entry->idx.oid));
03d66015
NP
1480 goto give_up;
1481 }
a7de7130 1482 if (reuse_delta && !entry->preferred_base) {
3449f8c4
NP
1483 struct revindex_entry *revidx;
1484 revidx = find_pack_revindex(p, ofs);
08698b1e
NP
1485 if (!revidx)
1486 goto give_up;
3449f8c4
NP
1487 base_ref = nth_packed_object_sha1(p, revidx->nr);
1488 }
5c49c116
NP
1489 entry->in_pack_header_size = used + used_0;
1490 break;
780e6e73 1491 }
780e6e73 1492
2834bc27 1493 if (base_ref && (base_entry = packlist_find(&to_pack, base_ref, NULL))) {
5c49c116
NP
1494 /*
1495 * If base_ref was set above that means we wish to
1496 * reuse delta data, and we even found that base
1497 * in the list of objects we want to pack. Goodie!
1498 *
1499 * Depth value does not matter - find_deltas() will
1500 * never consider reused delta as the base object to
1501 * deltify other objects against, in order to avoid
1502 * circular deltas.
3f9ac8d2 1503 */
fd9b1bae 1504 oe_set_type(entry, entry->in_pack_type);
27a7d067 1505 entry->size = in_pack_size; /* delta size */
898eba5e 1506 SET_DELTA(entry, base_entry);
64bd76b1 1507 entry->delta_size = entry->size;
898eba5e
NTND
1508 entry->delta_sibling_idx = base_entry->delta_child_idx;
1509 SET_DELTA_CHILD(base_entry, entry);
5c49c116
NP
1510 unuse_pack(&w_curs);
1511 return;
1512 }
ab7cd7bb 1513
fd9b1bae 1514 if (oe_type(entry)) {
27a7d067
NTND
1515 off_t delta_pos;
1516
5c49c116
NP
1517 /*
1518 * This must be a delta and we already know what the
1519 * final object type is. Let's extract the actual
1520 * object size from the delta header.
1521 */
27a7d067
NTND
1522 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;
1523 entry->size = get_size_from_delta(p, &w_curs, delta_pos);
03d66015
NP
1524 if (entry->size == 0)
1525 goto give_up;
5c49c116 1526 unuse_pack(&w_curs);
3f9ac8d2
JH
1527 return;
1528 }
5c49c116
NP
1529
1530 /*
1531 * No choice but to fall back to the recursive delta walk
1532 * with sha1_object_info() to find about the object type
1533 * at this point...
1534 */
03d66015 1535 give_up:
5c49c116 1536 unuse_pack(&w_curs);
36e4d74a 1537 }
3f9ac8d2 1538
fd9b1bae 1539 oe_set_type(entry, oid_object_info(&entry->idx.oid, &entry->size));
6d6f9cdd
SP
1540 /*
1541 * The error condition is checked in prepare_pack(). This is
1542 * to permit a missing preferred base object to be ignored
1543 * as a preferred base. Doing so can result in a larger
1544 * pack file, but the transfer will still take place.
1545 */
3f9ac8d2
JH
1546}
1547
5c49c116
NP
1548static int pack_offset_sort(const void *_a, const void *_b)
1549{
1550 const struct object_entry *a = *(struct object_entry **)_a;
1551 const struct object_entry *b = *(struct object_entry **)_b;
43fa44fa
NTND
1552 const struct packed_git *a_in_pack = IN_PACK(a);
1553 const struct packed_git *b_in_pack = IN_PACK(b);
5c49c116
NP
1554
1555 /* avoid filesystem trashing with loose objects */
43fa44fa 1556 if (!a_in_pack && !b_in_pack)
e6a492b7 1557 return oidcmp(&a->idx.oid, &b->idx.oid);
5c49c116 1558
43fa44fa 1559 if (a_in_pack < b_in_pack)
5c49c116 1560 return -1;
43fa44fa 1561 if (a_in_pack > b_in_pack)
5c49c116
NP
1562 return 1;
1563 return a->in_pack_offset < b->in_pack_offset ? -1 :
1564 (a->in_pack_offset > b->in_pack_offset);
1565}
1566
4cf2143e
JK
1567/*
1568 * Drop an on-disk delta we were planning to reuse. Naively, this would
1569 * just involve blanking out the "delta" field, but we have to deal
1570 * with some extra book-keeping:
1571 *
1572 * 1. Removing ourselves from the delta_sibling linked list.
1573 *
1574 * 2. Updating our size/type to the non-delta representation. These were
1575 * either not recorded initially (size) or overwritten with the delta type
1576 * (type) when check_object() decided to reuse the delta.
7dbabbbe
JK
1577 *
1578 * 3. Resetting our delta depth, as we are now a base object.
4cf2143e
JK
1579 */
1580static void drop_reused_delta(struct object_entry *entry)
1581{
898eba5e 1582 unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;
4cf2143e 1583 struct object_info oi = OBJECT_INFO_INIT;
fd9b1bae 1584 enum object_type type;
4cf2143e 1585
898eba5e
NTND
1586 while (*idx) {
1587 struct object_entry *oe = &to_pack.objects[*idx - 1];
1588
1589 if (oe == entry)
1590 *idx = oe->delta_sibling_idx;
4cf2143e 1591 else
898eba5e 1592 idx = &oe->delta_sibling_idx;
4cf2143e 1593 }
898eba5e 1594 SET_DELTA(entry, NULL);
7dbabbbe 1595 entry->depth = 0;
4cf2143e
JK
1596
1597 oi.sizep = &entry->size;
fd9b1bae 1598 oi.typep = &type;
43fa44fa 1599 if (packed_object_info(IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {
4cf2143e
JK
1600 /*
1601 * We failed to get the info from this pack for some reason;
1602 * fall back to sha1_object_info, which may find another copy.
fd9b1bae 1603 * And if that fails, the error will be recorded in oe_type(entry)
4cf2143e
JK
1604 * and dealt with in prepare_pack().
1605 */
fd9b1bae
NTND
1606 oe_set_type(entry, oid_object_info(&entry->idx.oid,
1607 &entry->size));
1608 } else {
1609 oe_set_type(entry, type);
4cf2143e
JK
1610 }
1611}
1612
1613/*
1614 * Follow the chain of deltas from this entry onward, throwing away any links
1615 * that cause us to hit a cycle (as determined by the DFS state flags in
1616 * the entries).
7dbabbbe
JK
1617 *
1618 * We also detect too-long reused chains that would violate our --depth
1619 * limit.
4cf2143e
JK
1620 */
1621static void break_delta_chains(struct object_entry *entry)
1622{
42b766d7
JK
1623 /*
1624 * The actual depth of each object we will write is stored as an int,
1625 * as it cannot exceed our int "depth" limit. But before we break
1626 * changes based no that limit, we may potentially go as deep as the
1627 * number of objects, which is elsewhere bounded to a uint32_t.
1628 */
1629 uint32_t total_depth;
1630 struct object_entry *cur, *next;
1631
1632 for (cur = entry, total_depth = 0;
1633 cur;
898eba5e 1634 cur = DELTA(cur), total_depth++) {
42b766d7
JK
1635 if (cur->dfs_state == DFS_DONE) {
1636 /*
1637 * We've already seen this object and know it isn't
1638 * part of a cycle. We do need to append its depth
1639 * to our count.
1640 */
1641 total_depth += cur->depth;
1642 break;
1643 }
4cf2143e 1644
4cf2143e 1645 /*
42b766d7
JK
1646 * We break cycles before looping, so an ACTIVE state (or any
1647 * other cruft which made its way into the state variable)
1648 * is a bug.
4cf2143e 1649 */
42b766d7
JK
1650 if (cur->dfs_state != DFS_NONE)
1651 die("BUG: confusing delta dfs state in first pass: %d",
1652 cur->dfs_state);
4cf2143e 1653
4cf2143e 1654 /*
42b766d7
JK
1655 * Now we know this is the first time we've seen the object. If
1656 * it's not a delta, we're done traversing, but we'll mark it
1657 * done to save time on future traversals.
4cf2143e 1658 */
898eba5e 1659 if (!DELTA(cur)) {
42b766d7
JK
1660 cur->dfs_state = DFS_DONE;
1661 break;
1662 }
4cf2143e 1663
4cf2143e 1664 /*
42b766d7
JK
1665 * Mark ourselves as active and see if the next step causes
1666 * us to cycle to another active object. It's important to do
1667 * this _before_ we loop, because it impacts where we make the
1668 * cut, and thus how our total_depth counter works.
1669 * E.g., We may see a partial loop like:
1670 *
1671 * A -> B -> C -> D -> B
1672 *
1673 * Cutting B->C breaks the cycle. But now the depth of A is
1674 * only 1, and our total_depth counter is at 3. The size of the
1675 * error is always one less than the size of the cycle we
1676 * broke. Commits C and D were "lost" from A's chain.
1677 *
1678 * If we instead cut D->B, then the depth of A is correct at 3.
1679 * We keep all commits in the chain that we examined.
4cf2143e 1680 */
42b766d7 1681 cur->dfs_state = DFS_ACTIVE;
898eba5e 1682 if (DELTA(cur)->dfs_state == DFS_ACTIVE) {
42b766d7
JK
1683 drop_reused_delta(cur);
1684 cur->dfs_state = DFS_DONE;
1685 break;
7dbabbbe 1686 }
42b766d7 1687 }
7dbabbbe 1688
42b766d7
JK
1689 /*
1690 * And now that we've gone all the way to the bottom of the chain, we
1691 * need to clear the active flags and set the depth fields as
1692 * appropriate. Unlike the loop above, which can quit when it drops a
1693 * delta, we need to keep going to look for more depth cuts. So we need
1694 * an extra "next" pointer to keep going after we reset cur->delta.
1695 */
1696 for (cur = entry; cur; cur = next) {
898eba5e 1697 next = DELTA(cur);
4cf2143e 1698
42b766d7
JK
1699 /*
1700 * We should have a chain of zero or more ACTIVE states down to
1701 * a final DONE. We can quit after the DONE, because either it
1702 * has no bases, or we've already handled them in a previous
1703 * call.
1704 */
1705 if (cur->dfs_state == DFS_DONE)
1706 break;
1707 else if (cur->dfs_state != DFS_ACTIVE)
1708 die("BUG: confusing delta dfs state in second pass: %d",
1709 cur->dfs_state);
4cf2143e 1710
4cf2143e 1711 /*
42b766d7
JK
1712 * If the total_depth is more than depth, then we need to snip
1713 * the chain into two or more smaller chains that don't exceed
1714 * the maximum depth. Most of the resulting chains will contain
1715 * (depth + 1) entries (i.e., depth deltas plus one base), and
1716 * the last chain (i.e., the one containing entry) will contain
1717 * whatever entries are left over, namely
1718 * (total_depth % (depth + 1)) of them.
1719 *
1720 * Since we are iterating towards decreasing depth, we need to
1721 * decrement total_depth as we go, and we need to write to the
1722 * entry what its final depth will be after all of the
1723 * snipping. Since we're snipping into chains of length (depth
1724 * + 1) entries, the final depth of an entry will be its
1725 * original depth modulo (depth + 1). Any time we encounter an
1726 * entry whose final depth is supposed to be zero, we snip it
1727 * from its delta base, thereby making it so.
4cf2143e 1728 */
42b766d7
JK
1729 cur->depth = (total_depth--) % (depth + 1);
1730 if (!cur->depth)
1731 drop_reused_delta(cur);
1732
1733 cur->dfs_state = DFS_DONE;
4cf2143e
JK
1734 }
1735}
1736
c323ac7d
LT
1737static void get_object_details(void)
1738{
7cadf491 1739 uint32_t i;
5c49c116
NP
1740 struct object_entry **sorted_by_offset;
1741
2834bc27
VM
1742 sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));
1743 for (i = 0; i < to_pack.nr_objects; i++)
1744 sorted_by_offset[i] = to_pack.objects + i;
9ed0d8d6 1745 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
c323ac7d 1746
2834bc27 1747 for (i = 0; i < to_pack.nr_objects; i++) {
15366280
JH
1748 struct object_entry *entry = sorted_by_offset[i];
1749 check_object(entry);
660b3735 1750 if (entry->type_valid && big_file_threshold < entry->size)
15366280
JH
1751 entry->no_try_delta = 1;
1752 }
3449f8c4 1753
4cf2143e
JK
1754 /*
1755 * This must happen in a second pass, since we rely on the delta
1756 * information for the whole list being completed.
1757 */
1758 for (i = 0; i < to_pack.nr_objects; i++)
1759 break_delta_chains(&to_pack.objects[i]);
1760
5c49c116 1761 free(sorted_by_offset);
c323ac7d
LT
1762}
1763
b904166c
NP
1764/*
1765 * We search for deltas in a list sorted by type, by filename hash, and then
1766 * by size, so that we see progressively smaller and smaller files.
1767 * That's because we prefer deltas to be from the bigger file
1768 * to the smaller -- deletes are potentially cheaper, but perhaps
1769 * more importantly, the bigger file is likely the more recent
1770 * one. The deepest deltas are therefore the oldest objects which are
1771 * less susceptible to be accessed often.
1772 */
9668cf59 1773static int type_size_sort(const void *_a, const void *_b)
c323ac7d 1774{
9668cf59
NP
1775 const struct object_entry *a = *(struct object_entry **)_a;
1776 const struct object_entry *b = *(struct object_entry **)_b;
fd9b1bae
NTND
1777 enum object_type a_type = oe_type(a);
1778 enum object_type b_type = oe_type(b);
9668cf59 1779
fd9b1bae 1780 if (a_type > b_type)
27225f2e 1781 return -1;
fd9b1bae 1782 if (a_type < b_type)
27225f2e 1783 return 1;
b904166c 1784 if (a->hash > b->hash)
7a979d99 1785 return -1;
b904166c 1786 if (a->hash < b->hash)
7a979d99 1787 return 1;
b904166c 1788 if (a->preferred_base > b->preferred_base)
c323ac7d 1789 return -1;
b904166c
NP
1790 if (a->preferred_base < b->preferred_base)
1791 return 1;
c323ac7d 1792 if (a->size > b->size)
b904166c
NP
1793 return -1;
1794 if (a->size < b->size)
c323ac7d 1795 return 1;
b904166c 1796 return a < b ? -1 : (a > b); /* newest first */
c323ac7d
LT
1797}
1798
1799struct unpacked {
1800 struct object_entry *entry;
1801 void *data;
f6c7081a 1802 struct delta_index *index;
5a235b5e 1803 unsigned depth;
c323ac7d
LT
1804};
1805
d250626c
NP
1806static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1807 unsigned long delta_size)
074b2eea
MK
1808{
1809 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
1810 return 0;
1811
e3dfddb3
MK
1812 if (delta_size < cache_max_small_delta_size)
1813 return 1;
1814
074b2eea
MK
1815 /* cache delta, if objects are large enough compared to delta size */
1816 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
1817 return 1;
1818
1819 return 0;
1820}
1821
7eb151d6 1822#ifndef NO_PTHREADS
8ecce684 1823
44626dc7 1824static pthread_mutex_t read_mutex;
8ecce684
NP
1825#define read_lock() pthread_mutex_lock(&read_mutex)
1826#define read_unlock() pthread_mutex_unlock(&read_mutex)
1827
44626dc7 1828static pthread_mutex_t cache_mutex;
3c701839
NP
1829#define cache_lock() pthread_mutex_lock(&cache_mutex)
1830#define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1831
44626dc7 1832static pthread_mutex_t progress_mutex;
8ecce684
NP
1833#define progress_lock() pthread_mutex_lock(&progress_mutex)
1834#define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1835
1836#else
1837
e1ef8673
JH
1838#define read_lock() (void)0
1839#define read_unlock() (void)0
1840#define cache_lock() (void)0
1841#define cache_unlock() (void)0
1842#define progress_lock() (void)0
1843#define progress_unlock() (void)0
8ecce684
NP
1844
1845#endif
1846
f6c7081a 1847static int try_delta(struct unpacked *trg, struct unpacked *src,
ef0316fc 1848 unsigned max_depth, unsigned long *mem_usage)
c323ac7d 1849{
f6c7081a
NP
1850 struct object_entry *trg_entry = trg->entry;
1851 struct object_entry *src_entry = src->entry;
560b25a8 1852 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
c83f032e 1853 unsigned ref_depth;
21666f1a 1854 enum object_type type;
c323ac7d
LT
1855 void *delta_buf;
1856
1857 /* Don't bother doing diffs between different types */
fd9b1bae 1858 if (oe_type(trg_entry) != oe_type(src_entry))
c323ac7d
LT
1859 return -1;
1860
51d1e83f 1861 /*
15f07e06
JK
1862 * We do not bother to try a delta that we discarded on an
1863 * earlier try, but only when reusing delta data. Note that
1864 * src_entry that is marked as the preferred_base should always
1865 * be considered, as even if we produce a suboptimal delta against
1866 * it, we will still save the transfer cost, as we already know
1867 * the other side has it and we won't send src_entry at all.
51d1e83f 1868 */
43fa44fa
NTND
1869 if (reuse_delta && IN_PACK(trg_entry) &&
1870 IN_PACK(trg_entry) == IN_PACK(src_entry) &&
15f07e06 1871 !src_entry->preferred_base &&
e9195b58
JH
1872 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1873 trg_entry->in_pack_type != OBJ_OFS_DELTA)
51d1e83f
LT
1874 return 0;
1875
898b14ce 1876 /* Let's not bust the allowed depth. */
5a235b5e 1877 if (src->depth >= max_depth)
d116a45a 1878 return 0;
c323ac7d 1879
c3b06a69 1880 /* Now some size filtering heuristics. */
560b25a8 1881 trg_size = trg_entry->size;
898eba5e 1882 if (!DELTA(trg_entry)) {
c83f032e
NP
1883 max_size = trg_size/2 - 20;
1884 ref_depth = 1;
1885 } else {
1886 max_size = trg_entry->delta_size;
5a235b5e 1887 ref_depth = trg->depth;
c83f032e 1888 }
720fe22d 1889 max_size = (uint64_t)max_size * (max_depth - src->depth) /
c83f032e 1890 (max_depth - ref_depth + 1);
c3b06a69
NP
1891 if (max_size == 0)
1892 return 0;
f6c7081a 1893 src_size = src_entry->size;
560b25a8 1894 sizediff = src_size < trg_size ? trg_size - src_size : 0;
27225f2e 1895 if (sizediff >= max_size)
f527cb8c 1896 return 0;
a1dab41a
BD
1897 if (trg_size < src_size / 32)
1898 return 0;
f6c7081a 1899
560b25a8
NP
1900 /* Load data if not already done */
1901 if (!trg->data) {
8ecce684 1902 read_lock();
b4f5aca4 1903 trg->data = read_object_file(&trg_entry->idx.oid, &type, &sz);
8ecce684 1904 read_unlock();
2e3404c3
JH
1905 if (!trg->data)
1906 die("object %s cannot be read",
e6a492b7 1907 oid_to_hex(&trg_entry->idx.oid));
560b25a8
NP
1908 if (sz != trg_size)
1909 die("object %s inconsistent object length (%lu vs %lu)",
e6a492b7 1910 oid_to_hex(&trg_entry->idx.oid), sz,
1911 trg_size);
ef0316fc 1912 *mem_usage += sz;
560b25a8
NP
1913 }
1914 if (!src->data) {
8ecce684 1915 read_lock();
b4f5aca4 1916 src->data = read_object_file(&src_entry->idx.oid, &type, &sz);
8ecce684 1917 read_unlock();
71064a95
NP
1918 if (!src->data) {
1919 if (src_entry->preferred_base) {
1920 static int warned = 0;
1921 if (!warned++)
1922 warning("object %s cannot be read",
e6a492b7 1923 oid_to_hex(&src_entry->idx.oid));
71064a95
NP
1924 /*
1925 * Those objects are not included in the
1926 * resulting pack. Be resilient and ignore
1927 * them if they can't be read, in case the
1928 * pack could be created nevertheless.
1929 */
1930 return 0;
1931 }
2e3404c3 1932 die("object %s cannot be read",
e6a492b7 1933 oid_to_hex(&src_entry->idx.oid));
71064a95 1934 }
560b25a8
NP
1935 if (sz != src_size)
1936 die("object %s inconsistent object length (%lu vs %lu)",
e6a492b7 1937 oid_to_hex(&src_entry->idx.oid), sz,
1938 src_size);
ef0316fc 1939 *mem_usage += sz;
560b25a8
NP
1940 }
1941 if (!src->index) {
1942 src->index = create_delta_index(src->data, src_size);
a588d88a
MK
1943 if (!src->index) {
1944 static int warned = 0;
1945 if (!warned++)
1946 warning("suboptimal pack - out of memory");
1947 return 0;
1948 }
ef0316fc 1949 *mem_usage += sizeof_delta_index(src->index);
560b25a8
NP
1950 }
1951
1952 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
c323ac7d 1953 if (!delta_buf)
75c42d8c 1954 return 0;
f6c7081a 1955
898eba5e 1956 if (DELTA(trg_entry)) {
848d732c
BD
1957 /* Prefer only shallower same-sized deltas. */
1958 if (delta_size == trg_entry->delta_size &&
5a235b5e 1959 src->depth + 1 >= trg->depth) {
848d732c
BD
1960 free(delta_buf);
1961 return 0;
1962 }
074b2eea 1963 }
9e2d57a0 1964
3c701839
NP
1965 /*
1966 * Handle memory allocation outside of the cache
1967 * accounting lock. Compiler will optimize the strangeness
7eb151d6 1968 * away when NO_PTHREADS is defined.
3c701839 1969 */
8e0f7003 1970 free(trg_entry->delta_data);
3c701839 1971 cache_lock();
9e2d57a0
NP
1972 if (trg_entry->delta_data) {
1973 delta_cache_size -= trg_entry->delta_size;
9e2d57a0
NP
1974 trg_entry->delta_data = NULL;
1975 }
d250626c 1976 if (delta_cacheable(src_size, trg_size, delta_size)) {
b7a28f78 1977 delta_cache_size += delta_size;
3c701839
NP
1978 cache_unlock();
1979 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1980 } else {
1981 cache_unlock();
074b2eea 1982 free(delta_buf);
3c701839
NP
1983 }
1984
898eba5e 1985 SET_DELTA(trg_entry, src_entry);
b7a28f78
NP
1986 trg_entry->delta_size = delta_size;
1987 trg->depth = src->depth + 1;
1988
f6c7081a 1989 return 1;
c323ac7d
LT
1990}
1991
898b14ce 1992static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
b2504a0d 1993{
898eba5e 1994 struct object_entry *child = DELTA_CHILD(me);
898b14ce
NP
1995 unsigned int m = n;
1996 while (child) {
1997 unsigned int c = check_delta_limit(child, n + 1);
1998 if (m < c)
1999 m = c;
898eba5e 2000 child = DELTA_SIBLING(child);
898b14ce
NP
2001 }
2002 return m;
b2504a0d
NP
2003}
2004
75ad235c 2005static unsigned long free_unpacked(struct unpacked *n)
a97773ce 2006{
ef0316fc 2007 unsigned long freed_mem = sizeof_delta_index(n->index);
a97773ce
BD
2008 free_delta_index(n->index);
2009 n->index = NULL;
2010 if (n->data) {
ef0316fc 2011 freed_mem += n->entry->size;
6a83d902 2012 FREE_AND_NULL(n->data);
a97773ce
BD
2013 }
2014 n->entry = NULL;
7d7baa5e 2015 n->depth = 0;
ef0316fc 2016 return freed_mem;
a97773ce
BD
2017}
2018
384b32c0 2019static void find_deltas(struct object_entry **list, unsigned *list_size,
e334977d 2020 int window, int depth, unsigned *processed)
c323ac7d 2021{
384b32c0 2022 uint32_t i, idx = 0, count = 0;
7cadf491 2023 struct unpacked *array;
ef0316fc 2024 unsigned long mem_usage = 0;
c323ac7d 2025
19d4b416 2026 array = xcalloc(window, sizeof(struct unpacked));
21fcd1bd 2027
384b32c0 2028 for (;;) {
421b488a 2029 struct object_entry *entry;
c323ac7d 2030 struct unpacked *n = array + idx;
ef0316fc 2031 int j, max_depth, best_base = -1;
c323ac7d 2032
384b32c0
NP
2033 progress_lock();
2034 if (!*list_size) {
2035 progress_unlock();
2036 break;
2037 }
421b488a 2038 entry = *list++;
384b32c0
NP
2039 (*list_size)--;
2040 if (!entry->preferred_base) {
2041 (*processed)++;
2042 display_progress(progress_state, *processed);
2043 }
2044 progress_unlock();
2045
ef0316fc 2046 mem_usage -= free_unpacked(n);
c323ac7d 2047 n->entry = entry;
ab7cd7bb 2048
a97773ce 2049 while (window_memory_limit &&
ef0316fc 2050 mem_usage > window_memory_limit &&
a97773ce
BD
2051 count > 1) {
2052 uint32_t tail = (idx + window - count) % window;
75ad235c 2053 mem_usage -= free_unpacked(array + tail);
a97773ce
BD
2054 count--;
2055 }
2056
75d39853
NP
2057 /* We do not compute delta to *create* objects we are not
2058 * going to pack.
2059 */
2060 if (entry->preferred_base)
2061 goto next;
2062
898b14ce
NP
2063 /*
2064 * If the current object is at pack edge, take the depth the
2065 * objects that depend on the current object into account
2066 * otherwise they would become too deep.
2067 */
2068 max_depth = depth;
898eba5e 2069 if (DELTA_CHILD(entry)) {
898b14ce
NP
2070 max_depth -= check_delta_limit(entry, 0);
2071 if (max_depth <= 0)
2072 goto next;
2073 }
2074
78817c15
LT
2075 j = window;
2076 while (--j > 0) {
77639870 2077 int ret;
7cadf491 2078 uint32_t other_idx = idx + j;
c323ac7d 2079 struct unpacked *m;
78817c15
LT
2080 if (other_idx >= window)
2081 other_idx -= window;
c323ac7d
LT
2082 m = array + other_idx;
2083 if (!m->entry)
2084 break;
ef0316fc 2085 ret = try_delta(n, m, max_depth, &mem_usage);
77639870 2086 if (ret < 0)
c323ac7d 2087 break;
77639870
JH
2088 else if (ret > 0)
2089 best_base = other_idx;
c323ac7d 2090 }
898b14ce 2091
ed4a9031
NP
2092 /*
2093 * If we decided to cache the delta data, then it is best
2094 * to compress it right away. First because we have to do
2095 * it anyway, and doing it here while we're threaded will
2096 * save a lot of time in the non threaded write phase,
2097 * as well as allow for caching more deltas within
2098 * the same cache size limit.
2099 * ...
2100 * But only if not writing to stdout, since in that case
2101 * the network is most likely throttling writes anyway,
2102 * and therefore it is best to go to the write phase ASAP
2103 * instead, as we can afford spending more time compressing
2104 * between writes at that moment.
2105 */
2106 if (entry->delta_data && !pack_to_stdout) {
0cb3c142
NTND
2107 unsigned long size;
2108
2109 size = do_compress(&entry->delta_data, entry->delta_size);
2110 if (size < (1U << OE_Z_DELTA_BITS)) {
2111 entry->z_delta_size = size;
2112 cache_lock();
2113 delta_cache_size -= entry->delta_size;
2114 delta_cache_size += entry->z_delta_size;
2115 cache_unlock();
2116 } else {
2117 FREE_AND_NULL(entry->delta_data);
2118 entry->z_delta_size = 0;
2119 }
ed4a9031
NP
2120 }
2121
70ca1a3f
JH
2122 /* if we made n a delta, and if n is already at max
2123 * depth, leaving it in the window is pointless. we
2124 * should evict it first.
70ca1a3f 2125 */
898eba5e 2126 if (DELTA(entry) && max_depth <= n->depth)
70ca1a3f 2127 continue;
ff45715c 2128
77639870
JH
2129 /*
2130 * Move the best delta base up in the window, after the
2131 * currently deltified object, to keep it longer. It will
2132 * be the first base object to be attempted next.
2133 */
898eba5e 2134 if (DELTA(entry)) {
77639870
JH
2135 struct unpacked swap = array[best_base];
2136 int dist = (window + idx - best_base) % window;
2137 int dst = best_base;
2138 while (dist--) {
2139 int src = (dst + 1) % window;
2140 array[dst] = array[src];
2141 dst = src;
2142 }
2143 array[dst] = swap;
2144 }
2145
898b14ce 2146 next:
521a4f4c 2147 idx++;
a97773ce
BD
2148 if (count + 1 < window)
2149 count++;
521a4f4c
LT
2150 if (idx >= window)
2151 idx = 0;
384b32c0 2152 }
adee7bdf 2153
f6c7081a 2154 for (i = 0; i < window; ++i) {
ff45715c 2155 free_delta_index(array[i].index);
adee7bdf 2156 free(array[i].data);
f6c7081a 2157 }
adee7bdf 2158 free(array);
c323ac7d
LT
2159}
2160
7eb151d6 2161#ifndef NO_PTHREADS
8ecce684 2162
a9a74636
NP
2163static void try_to_free_from_threads(size_t size)
2164{
2165 read_lock();
7c3ecb32 2166 release_pack_memory(size);
a9a74636
NP
2167 read_unlock();
2168}
2169
bc9b2175 2170static try_to_free_t old_try_to_free_routine;
851c34b0 2171
50f22ada
JS
2172/*
2173 * The main thread waits on the condition that (at least) one of the workers
2174 * has stopped working (which is indicated in the .working member of
2175 * struct thread_params).
2176 * When a work thread has completed its work, it sets .working to 0 and
2177 * signals the main thread and waits on the condition that .data_ready
2178 * becomes 1.
2179 */
2180
8ecce684
NP
2181struct thread_params {
2182 pthread_t thread;
2183 struct object_entry **list;
2184 unsigned list_size;
384b32c0 2185 unsigned remaining;
8ecce684
NP
2186 int window;
2187 int depth;
50f22ada
JS
2188 int working;
2189 int data_ready;
2190 pthread_mutex_t mutex;
2191 pthread_cond_t cond;
8ecce684
NP
2192 unsigned *processed;
2193};
2194
44626dc7
AH
2195static pthread_cond_t progress_cond;
2196
2197/*
2198 * Mutex and conditional variable can't be statically-initialized on Windows.
2199 */
2200static void init_threaded_search(void)
2201{
93749194 2202 init_recursive_mutex(&read_mutex);
44626dc7
AH
2203 pthread_mutex_init(&cache_mutex, NULL);
2204 pthread_mutex_init(&progress_mutex, NULL);
2205 pthread_cond_init(&progress_cond, NULL);
851c34b0 2206 old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads);
44626dc7
AH
2207}
2208
2209static void cleanup_threaded_search(void)
2210{
851c34b0 2211 set_try_to_free_routine(old_try_to_free_routine);
44626dc7
AH
2212 pthread_cond_destroy(&progress_cond);
2213 pthread_mutex_destroy(&read_mutex);
2214 pthread_mutex_destroy(&cache_mutex);
2215 pthread_mutex_destroy(&progress_mutex);
2216}
c2a33679 2217
8ecce684
NP
2218static void *threaded_find_deltas(void *arg)
2219{
c2a33679
NP
2220 struct thread_params *me = arg;
2221
0c2ad00b 2222 progress_lock();
50f22ada 2223 while (me->remaining) {
0c2ad00b
2224 progress_unlock();
2225
384b32c0 2226 find_deltas(me->list, &me->remaining,
c2a33679 2227 me->window, me->depth, me->processed);
50f22ada
JS
2228
2229 progress_lock();
2230 me->working = 0;
2231 pthread_cond_signal(&progress_cond);
2232 progress_unlock();
2233
2234 /*
2235 * We must not set ->data_ready before we wait on the
2236 * condition because the main thread may have set it to 1
2237 * before we get here. In order to be sure that new
2238 * work is available if we see 1 in ->data_ready, it
2239 * was initialized to 0 before this thread was spawned
2240 * and we reset it to 0 right away.
2241 */
2242 pthread_mutex_lock(&me->mutex);
2243 while (!me->data_ready)
2244 pthread_cond_wait(&me->cond, &me->mutex);
2245 me->data_ready = 0;
2246 pthread_mutex_unlock(&me->mutex);
0c2ad00b
2247
2248 progress_lock();
c2a33679 2249 }
0c2ad00b 2250 progress_unlock();
50f22ada
JS
2251 /* leave ->working 1 so that this doesn't get more work assigned */
2252 return NULL;
8ecce684
NP
2253}
2254
8ecce684
NP
2255static void ll_find_deltas(struct object_entry **list, unsigned list_size,
2256 int window, int depth, unsigned *processed)
2257{
dcda3614 2258 struct thread_params *p;
384b32c0 2259 int i, ret, active_threads = 0;
c2a33679 2260
44626dc7
AH
2261 init_threaded_search();
2262
367f4a43 2263 if (delta_search_threads <= 1) {
384b32c0 2264 find_deltas(list, &list_size, window, depth, processed);
44626dc7 2265 cleanup_threaded_search();
367f4a43
NP
2266 return;
2267 }
43cc2b42 2268 if (progress > pack_to_stdout)
b6c29915 2269 fprintf(stderr, "Delta compression using up to %d threads.\n",
43cc2b42 2270 delta_search_threads);
dcda3614 2271 p = xcalloc(delta_search_threads, sizeof(*p));
367f4a43 2272
50f22ada 2273 /* Partition the work amongst work threads. */
367f4a43 2274 for (i = 0; i < delta_search_threads; i++) {
50f22ada
JS
2275 unsigned sub_size = list_size / (delta_search_threads - i);
2276
bf874896
NP
2277 /* don't use too small segments or no deltas will be found */
2278 if (sub_size < 2*window && i+1 < delta_search_threads)
2279 sub_size = 0;
2280
8ecce684
NP
2281 p[i].window = window;
2282 p[i].depth = depth;
2283 p[i].processed = processed;
50f22ada
JS
2284 p[i].working = 1;
2285 p[i].data_ready = 0;
c2a33679 2286
59921b4b 2287 /* try to split chunks on "path" boundaries */
6fc74703
NP
2288 while (sub_size && sub_size < list_size &&
2289 list[sub_size]->hash &&
384b32c0
NP
2290 list[sub_size]->hash == list[sub_size-1]->hash)
2291 sub_size++;
2292
50f22ada
JS
2293 p[i].list = list;
2294 p[i].list_size = sub_size;
2295 p[i].remaining = sub_size;
59921b4b 2296
384b32c0
NP
2297 list += sub_size;
2298 list_size -= sub_size;
2299 }
2300
50f22ada
JS
2301 /* Start work threads. */
2302 for (i = 0; i < delta_search_threads; i++) {
2303 if (!p[i].list_size)
2304 continue;
68e6a4f8
JS
2305 pthread_mutex_init(&p[i].mutex, NULL);
2306 pthread_cond_init(&p[i].cond, NULL);
50f22ada
JS
2307 ret = pthread_create(&p[i].thread, NULL,
2308 threaded_find_deltas, &p[i]);
2309 if (ret)
2310 die("unable to create thread: %s", strerror(ret));
2311 active_threads++;
2312 }
2313
384b32c0
NP
2314 /*
2315 * Now let's wait for work completion. Each time a thread is done
2316 * with its work, we steal half of the remaining work from the
2317 * thread with the largest number of unprocessed objects and give
2318 * it to that newly idle thread. This ensure good load balancing
2319 * until the remaining object list segments are simply too short
2320 * to be worth splitting anymore.
2321 */
50f22ada
JS
2322 while (active_threads) {
2323 struct thread_params *target = NULL;
384b32c0
NP
2324 struct thread_params *victim = NULL;
2325 unsigned sub_size = 0;
384b32c0
NP
2326
2327 progress_lock();
50f22ada
JS
2328 for (;;) {
2329 for (i = 0; !target && i < delta_search_threads; i++)
2330 if (!p[i].working)
2331 target = &p[i];
2332 if (target)
2333 break;
2334 pthread_cond_wait(&progress_cond, &progress_mutex);
2335 }
2336
384b32c0
NP
2337 for (i = 0; i < delta_search_threads; i++)
2338 if (p[i].remaining > 2*window &&
2339 (!victim || victim->remaining < p[i].remaining))
2340 victim = &p[i];
2341 if (victim) {
2342 sub_size = victim->remaining / 2;
2343 list = victim->list + victim->list_size - sub_size;
2344 while (sub_size && list[0]->hash &&
2345 list[0]->hash == list[-1]->hash) {
2346 list++;
2347 sub_size--;
2348 }
eb9688ff
NP
2349 if (!sub_size) {
2350 /*
2351 * It is possible for some "paths" to have
2352 * so many objects that no hash boundary
2353 * might be found. Let's just steal the
2354 * exact half in that case.
2355 */
2356 sub_size = victim->remaining / 2;
2357 list -= sub_size;
2358 }
384b32c0
NP
2359 target->list = list;
2360 victim->list_size -= sub_size;
2361 victim->remaining -= sub_size;
2362 }
384b32c0
NP
2363 target->list_size = sub_size;
2364 target->remaining = sub_size;
50f22ada
JS
2365 target->working = 1;
2366 progress_unlock();
2367
2368 pthread_mutex_lock(&target->mutex);
2369 target->data_ready = 1;
2370 pthread_cond_signal(&target->cond);
2371 pthread_mutex_unlock(&target->mutex);
c2a33679 2372
384b32c0 2373 if (!sub_size) {
b81d9af7 2374 pthread_join(target->thread, NULL);
50f22ada
JS
2375 pthread_cond_destroy(&target->cond);
2376 pthread_mutex_destroy(&target->mutex);
384b32c0 2377 active_threads--;
c2a33679 2378 }
50f22ada 2379 }
44626dc7 2380 cleanup_threaded_search();
dcda3614 2381 free(p);
8ecce684
NP
2382}
2383
2384#else
384b32c0 2385#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)
8ecce684
NP
2386#endif
2387
b773ddea
JK
2388static void add_tag_chain(const struct object_id *oid)
2389{
2390 struct tag *tag;
2391
2392 /*
2393 * We catch duplicates already in add_object_entry(), but we'd
2394 * prefer to do this extra check to avoid having to parse the
2395 * tag at all if we already know that it's being packed (e.g., if
2396 * it was included via bitmaps, we would not have parsed it
2397 * previously).
2398 */
2399 if (packlist_find(&to_pack, oid->hash, NULL))
2400 return;
2401
d3101b53 2402 tag = lookup_tag(oid);
b773ddea
JK
2403 while (1) {
2404 if (!tag || parse_tag(tag) || !tag->tagged)
2405 die("unable to pack objects reachable from tag %s",
2406 oid_to_hex(oid));
2407
188960b4 2408 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);
b773ddea
JK
2409
2410 if (tag->tagged->type != OBJ_TAG)
2411 return;
2412
2413 tag = (struct tag *)tag->tagged;
2414 }
2415}
2416
d155254c 2417static int add_ref_tag(const char *path, const struct object_id *oid, int flag, void *cb_data)
f0a24aa5 2418{
d155254c 2419 struct object_id peeled;
f0a24aa5 2420
59556548 2421 if (starts_with(path, "refs/tags/") && /* is a tag? */
b420d909 2422 !peel_ref(path, &peeled) && /* peelable? */
d155254c 2423 packlist_find(&to_pack, peeled.hash, NULL)) /* object packed? */
b773ddea 2424 add_tag_chain(oid);
f0a24aa5
SP
2425 return 0;
2426}
2427
f3123c4a
JH
2428static void prepare_pack(int window, int depth)
2429{
9668cf59 2430 struct object_entry **delta_list;
6e1c2344
RJ
2431 uint32_t i, nr_deltas;
2432 unsigned n;
9668cf59 2433
3f9ac8d2 2434 get_object_details();
9668cf59 2435
0e8189e2
NP
2436 /*
2437 * If we're locally repacking then we need to be doubly careful
2438 * from now on in order to make sure no stealth corruption gets
2439 * propagated to the new pack. Clients receiving streamed packs
2440 * should validate everything they get anyway so no need to incur
2441 * the additional cost here in that case.
2442 */
2443 if (!pack_to_stdout)
2444 do_check_packed_object_crc = 1;
2445
2834bc27 2446 if (!to_pack.nr_objects || !window || !depth)
9668cf59
NP
2447 return;
2448
b32fa95f 2449 ALLOC_ARRAY(delta_list, to_pack.nr_objects);
75d39853
NP
2450 nr_deltas = n = 0;
2451
2834bc27
VM
2452 for (i = 0; i < to_pack.nr_objects; i++) {
2453 struct object_entry *entry = to_pack.objects + i;
75d39853 2454
898eba5e 2455 if (DELTA(entry))
75d39853 2456 /* This happens if we decided to reuse existing
a7de7130 2457 * delta from a pack. "reuse_delta &&" is implied.
75d39853
NP
2458 */
2459 continue;
2460
660b3735 2461 if (!entry->type_valid || entry->size < 50)
75d39853
NP
2462 continue;
2463
2464 if (entry->no_try_delta)
2465 continue;
2466
6d6f9cdd 2467 if (!entry->preferred_base) {
75d39853 2468 nr_deltas++;
fd9b1bae 2469 if (oe_type(entry) < 0)
6d6f9cdd 2470 die("unable to get type of object %s",
e6a492b7 2471 oid_to_hex(&entry->idx.oid));
eede9f42 2472 } else {
fd9b1bae 2473 if (oe_type(entry) < 0) {
eede9f42
NP
2474 /*
2475 * This object is not found, but we
2476 * don't have to include it anyway.
2477 */
2478 continue;
2479 }
6d6f9cdd 2480 }
75d39853
NP
2481
2482 delta_list[n++] = entry;
2483 }
2484
2f8b8947 2485 if (nr_deltas && n > 1) {
e334977d
NP
2486 unsigned nr_done = 0;
2487 if (progress)
754dbc43 2488 progress_state = start_progress(_("Compressing objects"),
dc6a0757 2489 nr_deltas);
9ed0d8d6 2490 QSORT(delta_list, n, type_size_sort);
8ecce684 2491 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
4d4fcc54 2492 stop_progress(&progress_state);
e334977d
NP
2493 if (nr_done != nr_deltas)
2494 die("inconsistency with delta count");
75d39853 2495 }
9668cf59 2496 free(delta_list);
f3123c4a
JH
2497}
2498
ef90d6d4 2499static int git_pack_config(const char *k, const char *v, void *cb)
4812a93a 2500{
eeefa7c9 2501 if (!strcmp(k, "pack.window")) {
4812a93a
JK
2502 window = git_config_int(k, v);
2503 return 0;
2504 }
a97773ce
BD
2505 if (!strcmp(k, "pack.windowmemory")) {
2506 window_memory_limit = git_config_ulong(k, v);
2507 return 0;
2508 }
2509 if (!strcmp(k, "pack.depth")) {
842aaf93
TT
2510 depth = git_config_int(k, v);
2511 return 0;
2512 }
074b2eea
MK
2513 if (!strcmp(k, "pack.deltacachesize")) {
2514 max_delta_cache_size = git_config_int(k, v);
2515 return 0;
2516 }
e3dfddb3
MK
2517 if (!strcmp(k, "pack.deltacachelimit")) {
2518 cache_max_small_delta_size = git_config_int(k, v);
2519 return 0;
2520 }
ae4f07fb
VM
2521 if (!strcmp(k, "pack.writebitmaphashcache")) {
2522 if (git_config_bool(k, v))
2523 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
2524 else
2525 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
2526 }
6b8fda2d 2527 if (!strcmp(k, "pack.usebitmaps")) {
645c432d 2528 use_bitmap_index_default = git_config_bool(k, v);
6b8fda2d
VM
2529 return 0;
2530 }
693b86ff
NP
2531 if (!strcmp(k, "pack.threads")) {
2532 delta_search_threads = git_config_int(k, v);
833e3df1 2533 if (delta_search_threads < 0)
693b86ff
NP
2534 die("invalid number of threads specified (%d)",
2535 delta_search_threads);
7eb151d6 2536#ifdef NO_PTHREADS
2e96d815 2537 if (delta_search_threads != 1) {
693b86ff 2538 warning("no threads support, ignoring %s", k);
2e96d815
ÆAB
2539 delta_search_threads = 0;
2540 }
693b86ff
NP
2541#endif
2542 return 0;
2543 }
4d00bda2 2544 if (!strcmp(k, "pack.indexversion")) {
ebcfb379
JH
2545 pack_idx_opts.version = git_config_int(k, v);
2546 if (pack_idx_opts.version > 2)
6e1c2344 2547 die("bad pack.indexversion=%"PRIu32,
ebcfb379 2548 pack_idx_opts.version);
4d00bda2
NP
2549 return 0;
2550 }
ef90d6d4 2551 return git_default_config(k, v, cb);
4812a93a
JK
2552}
2553
b5d97e6b 2554static void read_object_list_from_stdin(void)
c323ac7d 2555{
188960b4 2556 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];
2557 struct object_id oid;
2558 const char *p;
b2504a0d 2559
da93d12b 2560 for (;;) {
da93d12b
LT
2561 if (!fgets(line, sizeof(line), stdin)) {
2562 if (feof(stdin))
2563 break;
2564 if (!ferror(stdin))
2565 die("fgets returned NULL, not EOF, not error!");
687dd75c 2566 if (errno != EINTR)
d824cbba 2567 die_errno("fgets");
687dd75c
JH
2568 clearerr(stdin);
2569 continue;
da93d12b 2570 }
7a979d99 2571 if (line[0] == '-') {
188960b4 2572 if (get_oid_hex(line+1, &oid))
2573 die("expected edge object ID, got garbage:\n %s",
b5d97e6b 2574 line);
188960b4 2575 add_preferred_base(&oid);
7a979d99 2576 continue;
21fcd1bd 2577 }
188960b4 2578 if (parse_oid_hex(line, &oid, &p))
2579 die("expected object ID, got garbage:\n %s", line);
b5d97e6b 2580
188960b4 2581 add_preferred_base_object(p + 1);
fd9b1bae 2582 add_object_entry(&oid, OBJ_NONE, p + 1, 0);
c323ac7d 2583 }
b5d97e6b
JH
2584}
2585
95308d64 2586/* Remember to update object flag allocation in object.h */
08cdfb13
JH
2587#define OBJECT_ADDED (1u<<20)
2588
11c211fa 2589static void show_commit(struct commit *commit, void *data)
b5d97e6b 2590{
188960b4 2591 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);
08cdfb13 2592 commit->object.flags |= OBJECT_ADDED;
7cc8f971
VM
2593
2594 if (write_bitmap_index)
2595 index_commit_for_bitmap(commit);
b5d97e6b
JH
2596}
2597
de1e67d0 2598static void show_object(struct object *obj, const char *name, void *data)
b5d97e6b 2599{
8d2dfc49 2600 add_preferred_base_object(name);
188960b4 2601 add_object_entry(&obj->oid, obj->type, name, 0);
8d2dfc49 2602 obj->flags |= OBJECT_ADDED;
b5d97e6b
JH
2603}
2604
9535ce73
JH
2605static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)
2606{
2607 assert(arg_missing_action == MA_ALLOW_ANY);
2608
2609 /*
2610 * Quietly ignore ALL missing objects. This avoids problems with
2611 * staging them now and getting an odd error later.
2612 */
2613 if (!has_object_file(&obj->oid))
2614 return;
2615
2616 show_object(obj, name, data);
2617}
2618
0c16cd49
JT
2619static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)
2620{
2621 assert(arg_missing_action == MA_ALLOW_PROMISOR);
2622
2623 /*
2624 * Quietly ignore EXPECTED missing objects. This avoids problems with
2625 * staging them now and getting an odd error later.
2626 */
2627 if (!has_object_file(&obj->oid) && is_promisor_object(&obj->oid))
2628 return;
2629
2630 show_object(obj, name, data);
2631}
2632
9535ce73
JH
2633static int option_parse_missing_action(const struct option *opt,
2634 const char *arg, int unset)
2635{
2636 assert(arg);
2637 assert(!unset);
2638
2639 if (!strcmp(arg, "error")) {
2640 arg_missing_action = MA_ERROR;
2641 fn_show_object = show_object;
2642 return 0;
2643 }
2644
2645 if (!strcmp(arg, "allow-any")) {
2646 arg_missing_action = MA_ALLOW_ANY;
0c16cd49 2647 fetch_if_missing = 0;
9535ce73
JH
2648 fn_show_object = show_object__ma_allow_any;
2649 return 0;
2650 }
2651
0c16cd49
JT
2652 if (!strcmp(arg, "allow-promisor")) {
2653 arg_missing_action = MA_ALLOW_PROMISOR;
2654 fetch_if_missing = 0;
2655 fn_show_object = show_object__ma_allow_promisor;
2656 return 0;
2657 }
2658
9535ce73
JH
2659 die(_("invalid value for --missing"));
2660 return 0;
2661}
2662
8d1d8f83
JH
2663static void show_edge(struct commit *commit)
2664{
188960b4 2665 add_preferred_base(&commit->object.oid);
8d1d8f83
JH
2666}
2667
08cdfb13
JH
2668struct in_pack_object {
2669 off_t offset;
2670 struct object *object;
2671};
2672
2673struct in_pack {
071bcaab
RJ
2674 unsigned int alloc;
2675 unsigned int nr;
08cdfb13
JH
2676 struct in_pack_object *array;
2677};
2678
2679static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack)
2680{
ed1c9977 2681 in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p);
08cdfb13
JH
2682 in_pack->array[in_pack->nr].object = object;
2683 in_pack->nr++;
2684}
2685
2686/*
2687 * Compare the objects in the offset order, in order to emulate the
f18d244a 2688 * "git rev-list --objects" output that produced the pack originally.
08cdfb13
JH
2689 */
2690static int ofscmp(const void *a_, const void *b_)
2691{
2692 struct in_pack_object *a = (struct in_pack_object *)a_;
2693 struct in_pack_object *b = (struct in_pack_object *)b_;
2694
2695 if (a->offset < b->offset)
2696 return -1;
2697 else if (a->offset > b->offset)
2698 return 1;
2699 else
f2fd0760 2700 return oidcmp(&a->object->oid, &b->object->oid);
08cdfb13
JH
2701}
2702
2703static void add_objects_in_unpacked_packs(struct rev_info *revs)
2704{
2705 struct packed_git *p;
2706 struct in_pack in_pack;
2707 uint32_t i;
2708
2709 memset(&in_pack, 0, sizeof(in_pack));
2710
a80d72db 2711 for (p = get_packed_git(the_repository); p; p = p->next) {
188960b4 2712 struct object_id oid;
08cdfb13
JH
2713 struct object *o;
2714
79bc4c71 2715 if (!p->pack_local || p->pack_keep)
08cdfb13
JH
2716 continue;
2717 if (open_pack_index(p))
2718 die("cannot open pack index");
2719
2720 ALLOC_GROW(in_pack.array,
2721 in_pack.nr + p->num_objects,
2722 in_pack.alloc);
2723
2724 for (i = 0; i < p->num_objects; i++) {
188960b4 2725 nth_packed_object_oid(&oid, p, i);
2726 o = lookup_unknown_object(oid.hash);
08cdfb13
JH
2727 if (!(o->flags & OBJECT_ADDED))
2728 mark_in_pack_object(o, p, &in_pack);
2729 o->flags |= OBJECT_ADDED;
2730 }
2731 }
2732
2733 if (in_pack.nr) {
9ed0d8d6 2734 QSORT(in_pack.array, in_pack.nr, ofscmp);
08cdfb13
JH
2735 for (i = 0; i < in_pack.nr; i++) {
2736 struct object *o = in_pack.array[i].object;
188960b4 2737 add_object_entry(&o->oid, o->type, "", 0);
08cdfb13
JH
2738 }
2739 }
2740 free(in_pack.array);
2741}
2742
76c1d9a0 2743static int add_loose_object(const struct object_id *oid, const char *path,
e26a8c47
JK
2744 void *data)
2745{
abef9020 2746 enum object_type type = oid_object_info(oid, NULL);
e26a8c47
JK
2747
2748 if (type < 0) {
2749 warning("loose object at %s could not be examined", path);
2750 return 0;
2751 }
2752
188960b4 2753 add_object_entry(oid, type, "", 0);
e26a8c47
JK
2754 return 0;
2755}
2756
2757/*
2758 * We actually don't even have to worry about reachability here.
2759 * add_object_entry will weed out duplicates, so we just add every
2760 * loose object we find.
2761 */
2762static void add_unreachable_loose_objects(void)
2763{
2764 for_each_loose_file_in_objdir(get_object_directory(),
2765 add_loose_object,
2766 NULL, NULL, NULL);
2767}
2768
188960b4 2769static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)
094085e3
BC
2770{
2771 static struct packed_git *last_found = (void *)1;
2772 struct packed_git *p;
2773
a80d72db
SB
2774 p = (last_found != (void *)1) ? last_found :
2775 get_packed_git(the_repository);
094085e3
BC
2776
2777 while (p) {
2778 if ((!p->pack_local || p->pack_keep) &&
188960b4 2779 find_pack_entry_one(oid->hash, p)) {
094085e3
BC
2780 last_found = p;
2781 return 1;
2782 }
2783 if (p == last_found)
a80d72db 2784 p = get_packed_git(the_repository);
094085e3
BC
2785 else
2786 p = p->next;
2787 if (p == last_found)
2788 p = p->next;
2789 }
2790 return 0;
2791}
2792
abcb8655
JK
2793/*
2794 * Store a list of sha1s that are should not be discarded
2795 * because they are either written too recently, or are
2796 * reachable from another object that was.
2797 *
2798 * This is filled by get_object_list.
2799 */
910650d2 2800static struct oid_array recent_objects;
abcb8655 2801
4ce3621a 2802static int loosened_object_can_be_discarded(const struct object_id *oid,
dddbad72 2803 timestamp_t mtime)
d0d46abc
JK
2804{
2805 if (!unpack_unreachable_expiration)
2806 return 0;
2807 if (mtime > unpack_unreachable_expiration)
2808 return 0;
910650d2 2809 if (oid_array_lookup(&recent_objects, oid) >= 0)
abcb8655 2810 return 0;
d0d46abc
JK
2811 return 1;
2812}
2813
ca11b212
NP
2814static void loosen_unused_packed_objects(struct rev_info *revs)
2815{
2816 struct packed_git *p;
2817 uint32_t i;
4ce3621a 2818 struct object_id oid;
ca11b212 2819
a80d72db 2820 for (p = get_packed_git(the_repository); p; p = p->next) {
79bc4c71 2821 if (!p->pack_local || p->pack_keep)
ca11b212
NP
2822 continue;
2823
2824 if (open_pack_index(p))
2825 die("cannot open pack index");
2826
2827 for (i = 0; i < p->num_objects; i++) {
4ce3621a 2828 nth_packed_object_oid(&oid, p, i);
2829 if (!packlist_find(&to_pack, oid.hash, NULL) &&
188960b4 2830 !has_sha1_pack_kept_or_nonlocal(&oid) &&
4ce3621a 2831 !loosened_object_can_be_discarded(&oid, p->mtime))
4bdb70a4 2832 if (force_object_loose(&oid, p->mtime))
ca11b212
NP
2833 die("unable to force loose object");
2834 }
2835 }
2836}
2837
69e4b342 2838/*
645c432d
KS
2839 * This tracks any options which pack-reuse code expects to be on, or which a
2840 * reader of the pack might not understand, and which would therefore prevent
2841 * blind reuse of what we have on disk.
69e4b342
JK
2842 */
2843static int pack_options_allow_reuse(void)
2844{
9df4a607
JK
2845 return pack_to_stdout &&
2846 allow_ofs_delta &&
2847 !ignore_packed_keep &&
2848 (!local || !have_non_local_packs) &&
2849 !incremental;
69e4b342
JK
2850}
2851
6b8fda2d
VM
2852static int get_object_list_from_bitmap(struct rev_info *revs)
2853{
2854 if (prepare_bitmap_walk(revs) < 0)
2855 return -1;
2856
69e4b342
JK
2857 if (pack_options_allow_reuse() &&
2858 !reuse_partial_packfile_from_bitmap(
6b8fda2d
VM
2859 &reuse_packfile,
2860 &reuse_packfile_objects,
2861 &reuse_packfile_offset)) {
2862 assert(reuse_packfile_objects);
2863 nr_result += reuse_packfile_objects;
78d2214e 2864 display_progress(progress_state, nr_result);
6b8fda2d
VM
2865 }
2866
2867 traverse_bitmap_commit_list(&add_object_entry_from_bitmap);
2868 return 0;
2869}
2870
abcb8655 2871static void record_recent_object(struct object *obj,
de1e67d0 2872 const char *name,
abcb8655
JK
2873 void *data)
2874{
910650d2 2875 oid_array_append(&recent_objects, &obj->oid);
abcb8655
JK
2876}
2877
2878static void record_recent_commit(struct commit *commit, void *data)
2879{
910650d2 2880 oid_array_append(&recent_objects, &commit->object.oid);
abcb8655
JK
2881}
2882
8d1d8f83 2883static void get_object_list(int ac, const char **av)
b5d97e6b
JH
2884{
2885 struct rev_info revs;
2886 char line[1000];
b5d97e6b
JH
2887 int flags = 0;
2888
b5d97e6b
JH
2889 init_revisions(&revs, NULL);
2890 save_commit_buffer = 0;
b5d97e6b
JH
2891 setup_revisions(ac, av, &revs, NULL);
2892
b790e0f6
NTND
2893 /* make sure shallows are read */
2894 is_repository_shallow();
2895
b5d97e6b
JH
2896 while (fgets(line, sizeof(line), stdin) != NULL) {
2897 int len = strlen(line);
872c930d 2898 if (len && line[len - 1] == '\n')
b5d97e6b
JH
2899 line[--len] = 0;
2900 if (!len)
2901 break;
2902 if (*line == '-') {
2903 if (!strcmp(line, "--not")) {
2904 flags ^= UNINTERESTING;
7cc8f971 2905 write_bitmap_index = 0;
b5d97e6b
JH
2906 continue;
2907 }
b790e0f6 2908 if (starts_with(line, "--shallow ")) {
e92b848c 2909 struct object_id oid;
2910 if (get_oid_hex(line + 10, &oid))
b790e0f6 2911 die("not an SHA-1 '%s'", line + 10);
e92b848c 2912 register_shallow(&oid);
f7f91086 2913 use_bitmap_index = 0;
b790e0f6
NTND
2914 continue;
2915 }
b5d97e6b
JH
2916 die("not a rev '%s'", line);
2917 }
8e676e8b 2918 if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))
b5d97e6b
JH
2919 die("bad revision '%s'", line);
2920 }
2921
6b8fda2d
VM
2922 if (use_bitmap_index && !get_object_list_from_bitmap(&revs))
2923 return;
2924
3d51e1b5
MK
2925 if (prepare_revision_walk(&revs))
2926 die("revision walk setup failed");
e76a5fb4 2927 mark_edges_uninteresting(&revs, show_edge);
9535ce73
JH
2928
2929 if (!fn_show_object)
2930 fn_show_object = show_object;
2931 traverse_commit_list_filtered(&filter_options, &revs,
2932 show_commit, fn_show_object, NULL,
2933 NULL);
08cdfb13 2934
abcb8655
JK
2935 if (unpack_unreachable_expiration) {
2936 revs.ignore_missing_links = 1;
2937 if (add_unseen_recent_objects_to_traversal(&revs,
2938 unpack_unreachable_expiration))
2939 die("unable to add recent objects");
2940 if (prepare_revision_walk(&revs))
2941 die("revision walk setup failed");
2942 traverse_commit_list(&revs, record_recent_commit,
2943 record_recent_object, NULL);
2944 }
2945
08cdfb13
JH
2946 if (keep_unreachable)
2947 add_objects_in_unpacked_packs(&revs);
e26a8c47
JK
2948 if (pack_loose_unreachable)
2949 add_unreachable_loose_objects();
ca11b212
NP
2950 if (unpack_unreachable)
2951 loosen_unused_packed_objects(&revs);
abcb8655 2952
910650d2 2953 oid_array_clear(&recent_objects);
b5d97e6b
JH
2954}
2955
99fb6e04
NTND
2956static int option_parse_index_version(const struct option *opt,
2957 const char *arg, int unset)
2958{
2959 char *c;
2960 const char *val = arg;
2961 pack_idx_opts.version = strtoul(val, &c, 10);
2962 if (pack_idx_opts.version > 2)
2963 die(_("unsupported index version %s"), val);
2964 if (*c == ',' && c[1])
2965 pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
2966 if (*c || pack_idx_opts.off32_limit & 0x80000000)
2967 die(_("bad index version '%s'"), val);
2968 return 0;
2969}
2970
7e52f566
JK
2971static int option_parse_unpack_unreachable(const struct option *opt,
2972 const char *arg, int unset)
2973{
2974 if (unset) {
2975 unpack_unreachable = 0;
2976 unpack_unreachable_expiration = 0;
2977 }
2978 else {
2979 unpack_unreachable = 1;
2980 if (arg)
2981 unpack_unreachable_expiration = approxidate(arg);
2982 }
2983 return 0;
2984}
2985
b5d97e6b
JH
2986int cmd_pack_objects(int argc, const char **argv, const char *prefix)
2987{
b5d97e6b 2988 int use_internal_rev_list = 0;
8d1d8f83 2989 int thin = 0;
2dacf26d 2990 int shallow = 0;
4f366275 2991 int all_progress_implied = 0;
edfbb2aa 2992 struct argv_array rp = ARGV_ARRAY_INIT;
99fb6e04 2993 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
c90f9e13 2994 int rev_list_index = 0;
99fb6e04
NTND
2995 struct option pack_objects_options[] = {
2996 OPT_SET_INT('q', "quiet", &progress,
4c688120 2997 N_("do not show progress meter"), 0),
99fb6e04 2998 OPT_SET_INT(0, "progress", &progress,
4c688120 2999 N_("show progress meter"), 1),
99fb6e04 3000 OPT_SET_INT(0, "all-progress", &progress,
4c688120 3001 N_("show progress meter during object writing phase"), 2),
99fb6e04
NTND
3002 OPT_BOOL(0, "all-progress-implied",
3003 &all_progress_implied,
4c688120
NTND
3004 N_("similar to --all-progress when progress meter is shown")),
3005 { OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"),
3006 N_("write the pack index file in the specified idx format version"),
99fb6e04 3007 0, option_parse_index_version },
2a514ed8
CB
3008 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
3009 N_("maximum size of each output pack file")),
99fb6e04 3010 OPT_BOOL(0, "local", &local,
4c688120 3011 N_("ignore borrowed objects from alternate object store")),
99fb6e04 3012 OPT_BOOL(0, "incremental", &incremental,
4c688120 3013 N_("ignore packed objects")),
99fb6e04 3014 OPT_INTEGER(0, "window", &window,
4c688120 3015 N_("limit pack window by objects")),
2a514ed8
CB
3016 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
3017 N_("limit pack window by memory in addition to object limit")),
99fb6e04 3018 OPT_INTEGER(0, "depth", &depth,
4c688120 3019 N_("maximum length of delta chain allowed in the resulting pack")),
99fb6e04 3020 OPT_BOOL(0, "reuse-delta", &reuse_delta,
4c688120 3021 N_("reuse existing deltas")),
99fb6e04 3022 OPT_BOOL(0, "reuse-object", &reuse_object,
4c688120 3023 N_("reuse existing objects")),
99fb6e04 3024 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
4c688120 3025 N_("use OFS_DELTA objects")),
99fb6e04 3026 OPT_INTEGER(0, "threads", &delta_search_threads,
4c688120 3027 N_("use threads when searching for best delta matches")),
99fb6e04 3028 OPT_BOOL(0, "non-empty", &non_empty,
4c688120 3029 N_("do not create an empty pack output")),
99fb6e04 3030 OPT_BOOL(0, "revs", &use_internal_rev_list,
4c688120 3031 N_("read revision arguments from standard input")),
99fb6e04 3032 { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL,
4c688120 3033 N_("limit the objects to those that are not yet packed"),
99fb6e04
NTND
3034 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
3035 { OPTION_SET_INT, 0, "all", &rev_list_all, NULL,
4c688120 3036 N_("include objects reachable from any reference"),
99fb6e04
NTND
3037 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
3038 { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL,
4c688120 3039 N_("include objects referred by reflog entries"),
99fb6e04 3040 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
c90f9e13
JK
3041 { OPTION_SET_INT, 0, "indexed-objects", &rev_list_index, NULL,
3042 N_("include objects referred to by the index"),
3043 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
99fb6e04 3044 OPT_BOOL(0, "stdout", &pack_to_stdout,
4c688120 3045 N_("output pack to stdout")),
99fb6e04 3046 OPT_BOOL(0, "include-tag", &include_tag,
4c688120 3047 N_("include tag objects that refer to objects to be packed")),
99fb6e04 3048 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
4c688120 3049 N_("keep unreachable objects")),
e26a8c47
JK
3050 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
3051 N_("pack loose unreachable objects")),
4c688120
NTND
3052 { OPTION_CALLBACK, 0, "unpack-unreachable", NULL, N_("time"),
3053 N_("unpack unreachable objects newer than <time>"),
7e52f566 3054 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },
99fb6e04 3055 OPT_BOOL(0, "thin", &thin,
4c688120 3056 N_("create thin packs")),
2dacf26d 3057 OPT_BOOL(0, "shallow", &shallow,
3058 N_("create packs suitable for shallow fetches")),
99fb6e04 3059 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep,
4c688120 3060 N_("ignore packs that have companion .keep file")),
99fb6e04 3061 OPT_INTEGER(0, "compression", &pack_compression_level,
4c688120 3062 N_("pack compression level")),
99fb6e04 3063 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents,
4c688120 3064 N_("do not hide commits by grafts"), 0),
6b8fda2d
VM
3065 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
3066 N_("use a bitmap index if available to speed up counting objects")),
7cc8f971
VM
3067 OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index,
3068 N_("write a bitmap index together with the pack index")),
9535ce73
JH
3069 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
3070 { OPTION_CALLBACK, 0, "missing", NULL, N_("action"),
3071 N_("handling for missing objects"), PARSE_OPT_NONEG,
3072 option_parse_missing_action },
0c16cd49
JT
3073 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,
3074 N_("do not pack objects in promisor packfiles")),
99fb6e04
NTND
3075 OPT_END(),
3076 };
8d1d8f83 3077
0c6804ab
NTND
3078 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))
3079 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
3080
afc711b8 3081 check_replace_refs = 0;
dae556bd 3082
ebcfb379 3083 reset_pack_idx_option(&pack_idx_opts);
ef90d6d4 3084 git_config(git_pack_config, NULL);
b5d97e6b
JH
3085
3086 progress = isatty(2);
99fb6e04
NTND
3087 argc = parse_options(argc, argv, prefix, pack_objects_options,
3088 pack_usage, 0);
b5d97e6b 3089
99fb6e04
NTND
3090 if (argc) {
3091 base_name = argv[0];
3092 argc--;
b5d97e6b 3093 }
99fb6e04
NTND
3094 if (pack_to_stdout != !base_name || argc)
3095 usage_with_options(pack_usage, pack_objects_options);
b5d97e6b 3096
b5c0cbd8
NTND
3097 if (depth >= (1 << OE_DEPTH_BITS)) {
3098 warning(_("delta chain depth %d is too deep, forcing %d"),
3099 depth, (1 << OE_DEPTH_BITS) - 1);
3100 depth = (1 << OE_DEPTH_BITS) - 1;
3101 }
0cb3c142
NTND
3102 if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {
3103 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
3104 (1U << OE_Z_DELTA_BITS) - 1);
3105 cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;
3106 }
b5c0cbd8 3107
edfbb2aa 3108 argv_array_push(&rp, "pack-objects");
99fb6e04
NTND
3109 if (thin) {
3110 use_internal_rev_list = 1;
2dacf26d 3111 argv_array_push(&rp, shallow
3112 ? "--objects-edge-aggressive"
3113 : "--objects-edge");
99fb6e04 3114 } else
edfbb2aa 3115 argv_array_push(&rp, "--objects");
b5d97e6b 3116
99fb6e04
NTND
3117 if (rev_list_all) {
3118 use_internal_rev_list = 1;
edfbb2aa 3119 argv_array_push(&rp, "--all");
99fb6e04
NTND
3120 }
3121 if (rev_list_reflog) {
3122 use_internal_rev_list = 1;
edfbb2aa 3123 argv_array_push(&rp, "--reflog");
99fb6e04 3124 }
c90f9e13
JK
3125 if (rev_list_index) {
3126 use_internal_rev_list = 1;
3127 argv_array_push(&rp, "--indexed-objects");
99fb6e04
NTND
3128 }
3129 if (rev_list_unpacked) {
3130 use_internal_rev_list = 1;
edfbb2aa 3131 argv_array_push(&rp, "--unpacked");
99fb6e04 3132 }
b5d97e6b 3133
0c16cd49
JT
3134 if (exclude_promisor_objects) {
3135 use_internal_rev_list = 1;
3136 fetch_if_missing = 0;
3137 argv_array_push(&rp, "--exclude-promisor-objects");
3138 }
3139
99fb6e04
NTND
3140 if (!reuse_object)
3141 reuse_delta = 0;
3142 if (pack_compression_level == -1)
3143 pack_compression_level = Z_DEFAULT_COMPRESSION;
3144 else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
3145 die("bad pack compression level %d", pack_compression_level);
0c45d258
JH
3146
3147 if (!delta_search_threads) /* --threads=0 means autodetect */
3148 delta_search_threads = online_cpus();
3149
99fb6e04
NTND
3150#ifdef NO_PTHREADS
3151 if (delta_search_threads != 1)
2b34e486 3152 warning("no threads support, ignoring --threads");
99fb6e04 3153#endif
2b84b5a8
JS
3154 if (!pack_to_stdout && !pack_size_limit)
3155 pack_size_limit = pack_size_limit_cfg;
01c12a23
DH
3156 if (pack_to_stdout && pack_size_limit)
3157 die("--max-pack-size cannot be used to build a pack for transfer.");
07cf0f24
NP
3158 if (pack_size_limit && pack_size_limit < 1024*1024) {
3159 warning("minimum pack size limit is 1 MiB");
3160 pack_size_limit = 1024*1024;
3161 }
01c12a23 3162
8d1d8f83
JH
3163 if (!pack_to_stdout && thin)
3164 die("--thin cannot be used to build an indexable pack.");
b5d97e6b 3165
ca11b212
NP
3166 if (keep_unreachable && unpack_unreachable)
3167 die("--keep-unreachable and --unpack-unreachable are incompatible.");
b1e757f3
JK
3168 if (!rev_list_all || !rev_list_reflog || !rev_list_index)
3169 unpack_unreachable_expiration = 0;
ca11b212 3170
9535ce73
JH
3171 if (filter_options.choice) {
3172 if (!pack_to_stdout)
3173 die("cannot use --filter without --stdout.");
3174 use_bitmap_index = 0;
3175 }
3176
645c432d
KS
3177 /*
3178 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
3179 *
3180 * - to produce good pack (with bitmap index not-yet-packed objects are
3181 * packed in suboptimal order).
3182 *
3183 * - to use more robust pack-generation codepath (avoiding possible
3184 * bugs in bitmap code and possible bitmap index corruption).
3185 */
3186 if (!pack_to_stdout)
3187 use_bitmap_index_default = 0;
3188
3189 if (use_bitmap_index < 0)
3190 use_bitmap_index = use_bitmap_index_default;
3191
3192 /* "hard" reasons not to use bitmaps; these just won't work at all */
3193 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow())
6b8fda2d
VM
3194 use_bitmap_index = 0;
3195
7cc8f971
VM
3196 if (pack_to_stdout || !rev_list_all)
3197 write_bitmap_index = 0;
3198
4f366275
NP
3199 if (progress && all_progress_implied)
3200 progress = 2;
3201
56dfeb62
JK
3202 if (ignore_packed_keep) {
3203 struct packed_git *p;
a80d72db 3204 for (p = get_packed_git(the_repository); p; p = p->next)
56dfeb62
JK
3205 if (p->pack_local && p->pack_keep)
3206 break;
3207 if (!p) /* no keep-able packs found */
3208 ignore_packed_keep = 0;
3209 }
3210 if (local) {
3211 /*
3212 * unlike ignore_packed_keep above, we do not want to
3213 * unset "local" based on looking at packs, as it
3214 * also covers non-local objects
3215 */
3216 struct packed_git *p;
a80d72db 3217 for (p = get_packed_git(the_repository); p; p = p->next) {
56dfeb62
JK