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