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