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