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