rearrange delta search progress reporting
[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"
c38138cd 11#include "csum-file.h"
1b0c7174 12#include "tree-walk.h"
b5d97e6b
JH
13#include "diff.h"
14#include "revision.h"
15#include "list-objects.h"
96a02f8f 16#include "progress.h"
c323ac7d 17
231f240b 18static const char pack_usage[] = "\
a97773ce
BD
19git-pack-objects [{ -q | --progress | --all-progress }] \n\
20 [--max-pack-size=N] [--local] [--incremental] \n\
21 [--window=N] [--window-memory=N] [--depth=N] \n\
fa736f72
NP
22 [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\
23 [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\
24 [--stdout | base-name] [<ref-list | <object-list]";
c323ac7d 25
c323ac7d 26struct object_entry {
aa7e44bf 27 struct pack_idx_entry idx;
3f9ac8d2 28 unsigned long size; /* uncompressed size */
3f9ac8d2 29 struct packed_git *in_pack; /* already in pack */
6777a59f 30 off_t in_pack_offset;
a3fbf4df 31 struct object_entry *delta; /* delta base object */
82e5a82f 32 struct object_entry *delta_child; /* deltified objects who bases me */
15b4d577
JH
33 struct object_entry *delta_sibling; /* other deltified objects who
34 * uses the same base as me
35 */
074b2eea 36 void *delta_data; /* cached delta (uncompressed) */
a3fbf4df 37 unsigned long delta_size; /* delta data size (uncompressed) */
5a235b5e 38 unsigned int hash; /* name hint hash */
a3fbf4df
NP
39 enum object_type type;
40 enum object_type in_pack_type; /* could be delta */
41 unsigned char in_pack_header_size;
42 unsigned char preferred_base; /* we do not pack this, but is available
a74db82e 43 * to be used as the base object to delta
a3fbf4df
NP
44 * objects against.
45 */
a74db82e 46 unsigned char no_try_delta;
c323ac7d
LT
47};
48
3f9ac8d2 49/*
82e5a82f 50 * Objects we are going to pack are collected in objects array (dynamically
3f9ac8d2
JH
51 * expanded). nr_objects & nr_alloc controls this array. They are stored
52 * in the order we see -- typically rev-list --objects order that gives us
53 * nice "minimum seek" order.
3f9ac8d2 54 */
e4d58311 55static struct object_entry *objects;
d01fb92f
DH
56static struct object_entry **written_list;
57static uint32_t nr_objects, nr_alloc, nr_result, nr_written;
3f9ac8d2 58
96f1e58f 59static int non_empty;
fa736f72 60static int no_reuse_delta, no_reuse_object;
96f1e58f
DR
61static int local;
62static int incremental;
be6b1914 63static int allow_ofs_delta;
81a216a5
NP
64static const char *pack_tmp_name, *idx_tmp_name;
65static char tmpname[PATH_MAX];
d01fb92f 66static const char *base_name;
024701f1 67static int progress = 1;
4812a93a 68static int window = 10;
d01fb92f 69static uint32_t pack_size_limit;
618e613a 70static int depth = 50;
df6d6101 71static int pack_to_stdout;
8d1d8f83 72static int num_preferred_base;
96a02f8f 73static struct progress progress_state;
960ccca6
DH
74static int pack_compression_level = Z_DEFAULT_COMPRESSION;
75static int pack_compression_seen;
c323ac7d 76
074b2eea
MK
77static unsigned long delta_cache_size = 0;
78static unsigned long max_delta_cache_size = 0;
e3dfddb3 79static unsigned long cache_max_small_delta_size = 1000;
074b2eea 80
a97773ce
BD
81static unsigned long window_memory_limit = 0;
82
3f9ac8d2
JH
83/*
84 * The object names in objects array are hashed with this hashtable,
e4d58311 85 * to help looking up the entry by object name.
3f9ac8d2
JH
86 * This hashtable is built after all the objects are seen.
87 */
96f1e58f
DR
88static int *object_ix;
89static int object_ix_hashsz;
3f9ac8d2
JH
90
91/*
92 * Pack index for existing packs give us easy access to the offsets into
93 * corresponding pack file where each object's data starts, but the entries
94 * do not store the size of the compressed representation (uncompressed
780e6e73
NP
95 * size is easily available by examining the pack entry header). It is
96 * also rather expensive to find the sha1 for an object given its offset.
97 *
98 * We build a hashtable of existing packs (pack_revindex), and keep reverse
99 * index here -- pack index file is sorted by object name mapping to offset;
100 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
101 * ordered by offset, so if you know the offset of an object, next offset
102 * is where its packed representation ends and the index_nr can be used to
103 * get the object sha1 from the main index.
3f9ac8d2 104 */
780e6e73 105struct revindex_entry {
6777a59f 106 off_t offset;
780e6e73
NP
107 unsigned int nr;
108};
3f9ac8d2
JH
109struct pack_revindex {
110 struct packed_git *p;
780e6e73
NP
111 struct revindex_entry *revindex;
112};
113static struct pack_revindex *pack_revindex;
96f1e58f 114static int pack_revindex_hashsz;
3f9ac8d2
JH
115
116/*
117 * stats
118 */
7cadf491
SP
119static uint32_t written, written_delta;
120static uint32_t reused, reused_delta;
3f9ac8d2
JH
121
122static int pack_revindex_ix(struct packed_git *p)
123{
2b74cffa 124 unsigned long ui = (unsigned long)p;
3f9ac8d2
JH
125 int i;
126
127 ui = ui ^ (ui >> 16); /* defeat structure alignment */
128 i = (int)(ui % pack_revindex_hashsz);
129 while (pack_revindex[i].p) {
130 if (pack_revindex[i].p == p)
131 return i;
132 if (++i == pack_revindex_hashsz)
133 i = 0;
134 }
135 return -1 - i;
136}
137
138static void prepare_pack_ix(void)
139{
140 int num;
141 struct packed_git *p;
142 for (num = 0, p = packed_git; p; p = p->next)
143 num++;
144 if (!num)
145 return;
146 pack_revindex_hashsz = num * 11;
147 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
148 for (p = packed_git; p; p = p->next) {
149 num = pack_revindex_ix(p);
150 num = - 1 - num;
151 pack_revindex[num].p = p;
152 }
153 /* revindex elements are lazily initialized */
154}
155
156static int cmp_offset(const void *a_, const void *b_)
157{
780e6e73
NP
158 const struct revindex_entry *a = a_;
159 const struct revindex_entry *b = b_;
160 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
3f9ac8d2
JH
161}
162
163/*
164 * Ordered list of offsets of objects in the pack.
165 */
166static void prepare_pack_revindex(struct pack_revindex *rix)
167{
168 struct packed_git *p = rix->p;
57059091 169 int num_ent = p->num_objects;
3f9ac8d2 170 int i;
42873078 171 const char *index = p->index_data;
3f9ac8d2 172
780e6e73 173 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
c553ca25
NP
174 index += 4 * 256;
175
176 if (p->index_version > 1) {
177 const uint32_t *off_32 =
178 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
179 const uint32_t *off_64 = off_32 + p->num_objects;
180 for (i = 0; i < num_ent; i++) {
181 uint32_t off = ntohl(*off_32++);
182 if (!(off & 0x80000000)) {
183 rix->revindex[i].offset = off;
184 } else {
185 rix->revindex[i].offset =
186 ((uint64_t)ntohl(*off_64++)) << 32;
187 rix->revindex[i].offset |=
188 ntohl(*off_64++);
189 }
190 rix->revindex[i].nr = i;
191 }
192 } else {
193 for (i = 0; i < num_ent; i++) {
194 uint32_t hl = *((uint32_t *)(index + 24 * i));
195 rix->revindex[i].offset = ntohl(hl);
196 rix->revindex[i].nr = i;
197 }
3f9ac8d2 198 }
c553ca25 199
3f9ac8d2
JH
200 /* This knows the pack format -- the 20-byte trailer
201 * follows immediately after the last object data.
202 */
780e6e73
NP
203 rix->revindex[num_ent].offset = p->pack_size - 20;
204 rix->revindex[num_ent].nr = -1;
205 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
3f9ac8d2
JH
206}
207
780e6e73 208static struct revindex_entry * find_packed_object(struct packed_git *p,
6777a59f 209 off_t ofs)
3f9ac8d2
JH
210{
211 int num;
212 int lo, hi;
213 struct pack_revindex *rix;
780e6e73 214 struct revindex_entry *revindex;
3f9ac8d2
JH
215 num = pack_revindex_ix(p);
216 if (num < 0)
217 die("internal error: pack revindex uninitialized");
218 rix = &pack_revindex[num];
219 if (!rix->revindex)
220 prepare_pack_revindex(rix);
221 revindex = rix->revindex;
222 lo = 0;
57059091 223 hi = p->num_objects + 1;
3f9ac8d2
JH
224 do {
225 int mi = (lo + hi) / 2;
780e6e73
NP
226 if (revindex[mi].offset == ofs) {
227 return revindex + mi;
3f9ac8d2 228 }
780e6e73 229 else if (ofs < revindex[mi].offset)
3f9ac8d2
JH
230 hi = mi;
231 else
232 lo = mi + 1;
233 } while (lo < hi);
234 die("internal error: pack revindex corrupt");
235}
236
42873078
NP
237static const unsigned char *find_packed_object_name(struct packed_git *p,
238 off_t ofs)
780e6e73
NP
239{
240 struct revindex_entry *entry = find_packed_object(p, ofs);
d72308e0 241 return nth_packed_object_sha1(p, entry->nr);
780e6e73
NP
242}
243
c323ac7d
LT
244static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
245{
246 unsigned long othersize, delta_size;
21666f1a 247 enum object_type type;
aa7e44bf 248 void *otherbuf = read_sha1_file(entry->delta->idx.sha1, &type, &othersize);
c323ac7d
LT
249 void *delta_buf;
250
251 if (!otherbuf)
aa7e44bf 252 die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1));
8ee378a0 253 delta_buf = diff_delta(otherbuf, othersize,
dcde55bc 254 buf, size, &delta_size, 0);
75c42d8c 255 if (!delta_buf || delta_size != entry->delta_size)
a6080a0a 256 die("delta size changed");
c323ac7d
LT
257 free(buf);
258 free(otherbuf);
259 return delta_buf;
260}
261
a733cb60
LT
262/*
263 * The per-object header is a pretty dense thing, which is
264 * - first byte: low four bits are "size", then three bits of "type",
265 * and the high bit is "size continues".
266 * - each byte afterwards: low seven bits are size continuation,
267 * with the high bit being "size continues"
268 */
269static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
270{
01247d87 271 int n = 1;
a733cb60
LT
272 unsigned char c;
273
eb32d236 274 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
a733cb60
LT
275 die("bad type %d", type);
276
01247d87
LT
277 c = (type << 4) | (size & 15);
278 size >>= 4;
279 while (size) {
a733cb60 280 *hdr++ = c | 0x80;
01247d87
LT
281 c = size & 0x7f;
282 size >>= 7;
283 n++;
a733cb60
LT
284 }
285 *hdr = c;
286 return n;
287}
288
780e6e73
NP
289/*
290 * we are going to reuse the existing object data as is. make
291 * sure it is not corrupt.
292 */
079afb18
SP
293static int check_pack_inflate(struct packed_git *p,
294 struct pack_window **w_curs,
6777a59f
SP
295 off_t offset,
296 off_t len,
079afb18
SP
297 unsigned long expect)
298{
299 z_stream stream;
300 unsigned char fakebuf[4096], *in;
301 int st;
302
303 memset(&stream, 0, sizeof(stream));
304 inflateInit(&stream);
305 do {
306 in = use_pack(p, w_curs, offset, &stream.avail_in);
307 stream.next_in = in;
308 stream.next_out = fakebuf;
309 stream.avail_out = sizeof(fakebuf);
310 st = inflate(&stream, Z_FINISH);
311 offset += stream.next_in - in;
312 } while (st == Z_OK || st == Z_BUF_ERROR);
313 inflateEnd(&stream);
314 return (st == Z_STREAM_END &&
315 stream.total_out == expect &&
316 stream.total_in == len) ? 0 : -1;
317}
318
91ecbeca
NP
319static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
320 off_t offset, off_t len, unsigned int nr)
321{
322 const uint32_t *index_crc;
323 uint32_t data_crc = crc32(0, Z_NULL, 0);
324
325 do {
326 unsigned int avail;
327 void *data = use_pack(p, w_curs, offset, &avail);
328 if (avail > len)
329 avail = len;
330 data_crc = crc32(data_crc, data, avail);
331 offset += avail;
332 len -= avail;
333 } while (len);
334
335 index_crc = p->index_data;
336 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
337
338 return data_crc != ntohl(*index_crc);
339}
340
079afb18
SP
341static void copy_pack_data(struct sha1file *f,
342 struct packed_git *p,
343 struct pack_window **w_curs,
6777a59f
SP
344 off_t offset,
345 off_t len)
079afb18
SP
346{
347 unsigned char *in;
348 unsigned int avail;
349
350 while (len) {
351 in = use_pack(p, w_curs, offset, &avail);
352 if (avail > len)
6777a59f 353 avail = (unsigned int)len;
079afb18
SP
354 sha1write(f, in, avail);
355 offset += avail;
356 len -= avail;
357 }
358}
359
d7dd0223 360static unsigned long write_object(struct sha1file *f,
17b08f2c
DH
361 struct object_entry *entry,
362 off_t write_offset)
c323ac7d
LT
363{
364 unsigned long size;
21666f1a 365 enum object_type type;
3f9ac8d2 366 void *buf;
a733cb60 367 unsigned char header[10];
17b08f2c 368 unsigned char dheader[10];
6777a59f
SP
369 unsigned hdrlen;
370 off_t datalen;
a733cb60 371 enum object_type obj_type;
ab7cd7bb 372 int to_reuse = 0;
17b08f2c
DH
373 /* write limit if limited packsize and not first object */
374 unsigned long limit = pack_size_limit && nr_written ?
375 pack_size_limit - write_offset : 0;
376 /* no if no delta */
377 int usable_delta = !entry->delta ? 0 :
378 /* yes if unlimited packfile */
379 !pack_size_limit ? 1 :
380 /* no if base written to previous pack */
aa7e44bf 381 entry->delta->idx.offset == (off_t)-1 ? 0 :
17b08f2c
DH
382 /* otherwise double-check written to this
383 * pack, like we do below
384 */
aa7e44bf 385 entry->delta->idx.offset ? 1 : 0;
c323ac7d 386
78d1e84f
NP
387 if (!pack_to_stdout)
388 crc32_begin(f);
389
a733cb60 390 obj_type = entry->type;
fa736f72
NP
391 if (no_reuse_object)
392 to_reuse = 0; /* explicit */
393 else if (!entry->in_pack)
ab7cd7bb 394 to_reuse = 0; /* can't reuse what we don't have */
780e6e73 395 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
17b08f2c
DH
396 /* check_object() decided it for us ... */
397 to_reuse = usable_delta;
398 /* ... but pack split may override that */
ab7cd7bb
JH
399 else if (obj_type != entry->in_pack_type)
400 to_reuse = 0; /* pack has delta which is unusable */
401 else if (entry->delta)
402 to_reuse = 0; /* we want to pack afresh */
403 else
404 to_reuse = 1; /* we have it in-pack undeltified,
405 * and we do not need to deltify it.
406 */
407
df6d6101 408 if (!to_reuse) {
17b08f2c
DH
409 z_stream stream;
410 unsigned long maxsize;
411 void *out;
5476a8ad 412 if (!usable_delta) {
aa7e44bf 413 buf = read_sha1_file(entry->idx.sha1, &obj_type, &size);
5476a8ad 414 if (!buf)
aa7e44bf 415 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
5476a8ad 416 } else if (entry->delta_data) {
3f9ac8d2 417 size = entry->delta_size;
5476a8ad
NP
418 buf = entry->delta_data;
419 entry->delta_data = NULL;
aa7e44bf 420 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
be6b1914 421 OBJ_OFS_DELTA : OBJ_REF_DELTA;
17b08f2c 422 } else {
aa7e44bf 423 buf = read_sha1_file(entry->idx.sha1, &type, &size);
074b2eea 424 if (!buf)
aa7e44bf 425 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
5476a8ad
NP
426 buf = delta_against(buf, size, entry);
427 size = entry->delta_size;
aa7e44bf 428 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
5476a8ad 429 OBJ_OFS_DELTA : OBJ_REF_DELTA;
3f9ac8d2 430 }
17b08f2c
DH
431 /* compress the data to store and put compressed length in datalen */
432 memset(&stream, 0, sizeof(stream));
433 deflateInit(&stream, pack_compression_level);
434 maxsize = deflateBound(&stream, size);
435 out = xmalloc(maxsize);
436 /* Compress it */
437 stream.next_in = buf;
438 stream.avail_in = size;
439 stream.next_out = out;
440 stream.avail_out = maxsize;
441 while (deflate(&stream, Z_FINISH) == Z_OK)
442 /* nothing */;
443 deflateEnd(&stream);
444 datalen = stream.total_out;
445 deflateEnd(&stream);
3f9ac8d2
JH
446 /*
447 * The object header is a byte of 'type' followed by zero or
be6b1914 448 * more bytes of length.
3f9ac8d2
JH
449 */
450 hdrlen = encode_header(obj_type, size, header);
3f9ac8d2 451
be6b1914
NP
452 if (obj_type == OBJ_OFS_DELTA) {
453 /*
454 * Deltas with relative base contain an additional
455 * encoding of the relative offset for the delta
456 * base from this object's position in the pack.
457 */
aa7e44bf 458 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
17b08f2c
DH
459 unsigned pos = sizeof(dheader) - 1;
460 dheader[pos] = ofs & 127;
be6b1914 461 while (ofs >>= 7)
17b08f2c
DH
462 dheader[--pos] = 128 | (--ofs & 127);
463 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
464 free(out);
465 free(buf);
466 return 0;
467 }
468 sha1write(f, header, hdrlen);
469 sha1write(f, dheader + pos, sizeof(dheader) - pos);
470 hdrlen += sizeof(dheader) - pos;
be6b1914
NP
471 } else if (obj_type == OBJ_REF_DELTA) {
472 /*
473 * Deltas with a base reference contain
474 * an additional 20 bytes for the base sha1.
475 */
17b08f2c
DH
476 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
477 free(out);
478 free(buf);
479 return 0;
480 }
481 sha1write(f, header, hdrlen);
aa7e44bf 482 sha1write(f, entry->delta->idx.sha1, 20);
3f9ac8d2 483 hdrlen += 20;
17b08f2c
DH
484 } else {
485 if (limit && hdrlen + datalen + 20 >= limit) {
486 free(out);
487 free(buf);
488 return 0;
489 }
490 sha1write(f, header, hdrlen);
3f9ac8d2 491 }
17b08f2c
DH
492 sha1write(f, out, datalen);
493 free(out);
3f9ac8d2 494 free(buf);
c323ac7d 495 }
3f9ac8d2
JH
496 else {
497 struct packed_git *p = entry->in_pack;
03e79c88 498 struct pack_window *w_curs = NULL;
91ecbeca 499 struct revindex_entry *revidx;
6777a59f 500 off_t offset;
3f9ac8d2 501
780e6e73 502 if (entry->delta) {
aa7e44bf 503 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
780e6e73
NP
504 OBJ_OFS_DELTA : OBJ_REF_DELTA;
505 reused_delta++;
506 }
507 hdrlen = encode_header(obj_type, entry->size, header);
91ecbeca
NP
508 offset = entry->in_pack_offset;
509 revidx = find_packed_object(p, offset);
510 datalen = revidx[1].offset - offset;
511 if (!pack_to_stdout && p->index_version > 1 &&
512 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
aa7e44bf 513 die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
91ecbeca
NP
514 offset += entry->in_pack_header_size;
515 datalen -= entry->in_pack_header_size;
17b08f2c 516 if (obj_type == OBJ_OFS_DELTA) {
aa7e44bf 517 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
17b08f2c
DH
518 unsigned pos = sizeof(dheader) - 1;
519 dheader[pos] = ofs & 127;
520 while (ofs >>= 7)
521 dheader[--pos] = 128 | (--ofs & 127);
522 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit)
523 return 0;
524 sha1write(f, header, hdrlen);
525 sha1write(f, dheader + pos, sizeof(dheader) - pos);
526 hdrlen += sizeof(dheader) - pos;
527 } else if (obj_type == OBJ_REF_DELTA) {
528 if (limit && hdrlen + 20 + datalen + 20 >= limit)
529 return 0;
530 sha1write(f, header, hdrlen);
aa7e44bf 531 sha1write(f, entry->delta->idx.sha1, 20);
17b08f2c
DH
532 hdrlen += 20;
533 } else {
534 if (limit && hdrlen + datalen + 20 >= limit)
535 return 0;
536 sha1write(f, header, hdrlen);
537 }
538
91ecbeca
NP
539 if (!pack_to_stdout && p->index_version == 1 &&
540 check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
aa7e44bf 541 die("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1));
079afb18 542 copy_pack_data(f, p, &w_curs, offset, datalen);
03e79c88 543 unuse_pack(&w_curs);
3f9ac8d2 544 reused++;
a733cb60 545 }
17b08f2c 546 if (usable_delta)
ab7cd7bb 547 written_delta++;
3f9ac8d2 548 written++;
78d1e84f 549 if (!pack_to_stdout)
aa7e44bf 550 entry->idx.crc32 = crc32_end(f);
c323ac7d
LT
551 return hdrlen + datalen;
552}
553
6777a59f 554static off_t write_one(struct sha1file *f,
9d5ab962 555 struct object_entry *e,
6777a59f 556 off_t offset)
9d5ab962 557{
d7dd0223
NP
558 unsigned long size;
559
560 /* offset is non zero if object is written already. */
aa7e44bf 561 if (e->idx.offset || e->preferred_base)
9d5ab962 562 return offset;
d7dd0223
NP
563
564 /* if we are deltified, write out base object first. */
17b08f2c 565 if (e->delta) {
9d5ab962 566 offset = write_one(f, e->delta, offset);
17b08f2c
DH
567 if (!offset)
568 return 0;
569 }
d7dd0223 570
aa7e44bf 571 e->idx.offset = offset;
17b08f2c
DH
572 size = write_object(f, e, offset);
573 if (!size) {
aa7e44bf 574 e->idx.offset = 0;
17b08f2c
DH
575 return 0;
576 }
ebe27b13 577 written_list[nr_written++] = e;
d7dd0223
NP
578
579 /* make sure off_t is sufficiently large not to wrap */
580 if (offset > offset + size)
581 die("pack too large for current definition of off_t");
582 return offset + size;
9d5ab962
JH
583}
584
3082acfa
AR
585static int open_object_dir_tmp(const char *path)
586{
587 snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
7647b17f 588 return xmkstemp(tmpname);
3082acfa
AR
589}
590
aa7e44bf 591/* forward declaration for write_pack_file */
d01fb92f
DH
592static int adjust_perm(const char *path, mode_t mode);
593
594static void write_pack_file(void)
c323ac7d 595{
ebe27b13 596 uint32_t i = 0, j;
d22b9290 597 struct sha1file *f;
ebe27b13 598 off_t offset, offset_one, last_obj_offset = 0;
a733cb60 599 struct pack_header hdr;
ebe27b13
DH
600 int do_progress = progress >> pack_to_stdout;
601 uint32_t nr_remaining = nr_result;
81a216a5 602
13aaf148
NP
603 if (do_progress)
604 start_progress(&progress_state, "Writing %u objects...", "", nr_result);
ebe27b13 605 written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
183bdb2c 606
ebe27b13 607 do {
aa7e44bf
GB
608 unsigned char sha1[20];
609
ebe27b13
DH
610 if (pack_to_stdout) {
611 f = sha1fd(1, "<stdout>");
612 } else {
613 int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
ebe27b13
DH
614 pack_tmp_name = xstrdup(tmpname);
615 f = sha1fd(fd, pack_tmp_name);
616 }
617
618 hdr.hdr_signature = htonl(PACK_SIGNATURE);
619 hdr.hdr_version = htonl(PACK_VERSION);
620 hdr.hdr_entries = htonl(nr_remaining);
621 sha1write(f, &hdr, sizeof(hdr));
622 offset = sizeof(hdr);
623 nr_written = 0;
624 for (; i < nr_objects; i++) {
625 last_obj_offset = offset;
626 offset_one = write_one(f, objects + i, offset);
627 if (!offset_one)
628 break;
629 offset = offset_one;
630 if (do_progress)
631 display_progress(&progress_state, written);
632 }
c553ca25 633
ebe27b13
DH
634 /*
635 * Did we write the wrong # entries in the header?
636 * If so, rewrite it like in fast-import
637 */
638 if (pack_to_stdout || nr_written == nr_remaining) {
aa7e44bf 639 sha1close(f, sha1, 1);
ebe27b13 640 } else {
aa7e44bf
GB
641 sha1close(f, sha1, 0);
642 fixup_pack_header_footer(f->fd, sha1, pack_tmp_name, nr_written);
ebe27b13
DH
643 close(f->fd);
644 }
645
646 if (!pack_to_stdout) {
d01fb92f
DH
647 mode_t mode = umask(0);
648
649 umask(mode);
650 mode = 0444 & ~mode;
651
aa7e44bf
GB
652 idx_tmp_name = write_idx_file(NULL,
653 (struct pack_idx_entry **) written_list, nr_written, sha1);
d01fb92f 654 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
aa7e44bf 655 base_name, sha1_to_hex(sha1));
d01fb92f
DH
656 if (adjust_perm(pack_tmp_name, mode))
657 die("unable to make temporary pack file readable: %s",
658 strerror(errno));
659 if (rename(pack_tmp_name, tmpname))
660 die("unable to rename temporary pack file: %s",
661 strerror(errno));
662 snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
aa7e44bf 663 base_name, sha1_to_hex(sha1));
d01fb92f
DH
664 if (adjust_perm(idx_tmp_name, mode))
665 die("unable to make temporary index file readable: %s",
666 strerror(errno));
667 if (rename(idx_tmp_name, tmpname))
668 die("unable to rename temporary index file: %s",
669 strerror(errno));
aa7e44bf 670 puts(sha1_to_hex(sha1));
ebe27b13
DH
671 }
672
673 /* mark written objects as written to previous pack */
674 for (j = 0; j < nr_written; j++) {
aa7e44bf 675 written_list[j]->idx.offset = (off_t)-1;
ebe27b13
DH
676 }
677 nr_remaining -= nr_written;
678 } while (nr_remaining && i < nr_objects);
679
680 free(written_list);
183bdb2c 681 if (do_progress)
96a02f8f 682 stop_progress(&progress_state);
67c08ce1 683 if (written != nr_result)
7cadf491 684 die("wrote %u objects while expecting %u", written, nr_result);
01c12a23
DH
685 /*
686 * We have scanned through [0 ... i). Since we have written
687 * the correct number of objects, the remaining [i ... nr_objects)
688 * items must be either already written (due to out-of-order delta base)
689 * or a preferred base. Count those which are neither and complain if any.
690 */
ebe27b13
DH
691 for (j = 0; i < nr_objects; i++) {
692 struct object_entry *e = objects + i;
aa7e44bf 693 j += !e->idx.offset && !e->preferred_base;
d01fb92f 694 }
ebe27b13
DH
695 if (j)
696 die("wrote %u objects as expected but %u unwritten", written, j);
c323ac7d
LT
697}
698
7a979d99
JH
699static int locate_object_entry_hash(const unsigned char *sha1)
700{
701 int i;
702 unsigned int ui;
703 memcpy(&ui, sha1, sizeof(unsigned int));
704 i = ui % object_ix_hashsz;
705 while (0 < object_ix[i]) {
aa7e44bf 706 if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1))
7a979d99
JH
707 return i;
708 if (++i == object_ix_hashsz)
709 i = 0;
710 }
711 return -1 - i;
712}
713
714static struct object_entry *locate_object_entry(const unsigned char *sha1)
715{
716 int i;
717
718 if (!object_ix_hashsz)
719 return NULL;
720
721 i = locate_object_entry_hash(sha1);
722 if (0 <= i)
723 return &objects[object_ix[i]-1];
724 return NULL;
725}
726
727static void rehash_objects(void)
c323ac7d 728{
7cadf491 729 uint32_t i;
7a979d99
JH
730 struct object_entry *oe;
731
732 object_ix_hashsz = nr_objects * 3;
733 if (object_ix_hashsz < 1024)
734 object_ix_hashsz = 1024;
735 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
5379a5c5 736 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
7a979d99 737 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
aa7e44bf 738 int ix = locate_object_entry_hash(oe->idx.sha1);
7a979d99
JH
739 if (0 <= ix)
740 continue;
741 ix = -1 - ix;
742 object_ix[ix] = i + 1;
743 }
744}
745
ce0bd642 746static unsigned name_hash(const char *name)
1d6b38cc 747{
ce0bd642
LT
748 unsigned char c;
749 unsigned hash = 0;
750
bc32fed5
JH
751 if (!name)
752 return 0;
753
eeef7135 754 /*
ce0bd642
LT
755 * This effectively just creates a sortable number from the
756 * last sixteen non-whitespace characters. Last characters
757 * count "most", so things that end in ".c" sort together.
eeef7135 758 */
ce0bd642
LT
759 while ((c = *name++) != 0) {
760 if (isspace(c))
761 continue;
762 hash = (hash >> 2) + (c << 24);
763 }
1d6b38cc
JH
764 return hash;
765}
766
a74db82e
JH
767static void setup_delta_attr_check(struct git_attr_check *check)
768{
769 static struct git_attr *attr_delta;
770
771 if (!attr_delta)
772 attr_delta = git_attr("delta", 5);
773
774 check[0].attr = attr_delta;
775}
776
777static int no_try_delta(const char *path)
778{
779 struct git_attr_check check[1];
780
781 setup_delta_attr_check(check);
782 if (git_checkattr(path, ARRAY_SIZE(check), check))
783 return 0;
784 if (ATTR_FALSE(check->value))
785 return 1;
786 return 0;
787}
788
5c49c116 789static int add_object_entry(const unsigned char *sha1, enum object_type type,
bc32fed5 790 const char *name, int exclude)
c323ac7d 791{
c323ac7d 792 struct object_entry *entry;
29b734e4 793 struct packed_git *p, *found_pack = NULL;
6777a59f 794 off_t found_offset = 0;
29b734e4 795 int ix;
bc32fed5 796 unsigned hash = name_hash(name);
29b734e4
NP
797
798 ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
799 if (ix >= 0) {
800 if (exclude) {
801 entry = objects + object_ix[ix] - 1;
81a216a5
NP
802 if (!entry->preferred_base)
803 nr_result--;
29b734e4
NP
804 entry->preferred_base = 1;
805 }
806 return 0;
807 }
c323ac7d 808
5c49c116
NP
809 for (p = packed_git; p; p = p->next) {
810 off_t offset = find_pack_entry_one(sha1, p);
811 if (offset) {
812 if (!found_pack) {
813 found_offset = offset;
814 found_pack = p;
64560374 815 }
5c49c116
NP
816 if (exclude)
817 break;
818 if (incremental)
819 return 0;
820 if (local && !p->pack_local)
821 return 0;
64560374
LT
822 }
823 }
eb019375 824
29b734e4
NP
825 if (nr_objects >= nr_alloc) {
826 nr_alloc = (nr_alloc + 1024) * 3 / 2;
7cadf491 827 objects = xrealloc(objects, nr_alloc * sizeof(*entry));
c323ac7d 828 }
29b734e4
NP
829
830 entry = objects + nr_objects++;
c323ac7d 831 memset(entry, 0, sizeof(*entry));
aa7e44bf 832 hashcpy(entry->idx.sha1, sha1);
27225f2e 833 entry->hash = hash;
5c49c116
NP
834 if (type)
835 entry->type = type;
29b734e4
NP
836 if (exclude)
837 entry->preferred_base = 1;
81a216a5
NP
838 else
839 nr_result++;
29b734e4
NP
840 if (found_pack) {
841 entry->in_pack = found_pack;
842 entry->in_pack_offset = found_offset;
843 }
7a979d99
JH
844
845 if (object_ix_hashsz * 3 <= nr_objects * 4)
846 rehash_objects();
29b734e4
NP
847 else
848 object_ix[-1 - ix] = nr_objects;
7a979d99 849
96a02f8f
NP
850 if (progress)
851 display_progress(&progress_state, nr_objects);
29b734e4 852
a74db82e
JH
853 if (name && no_try_delta(name))
854 entry->no_try_delta = 1;
855
29b734e4 856 return 1;
c323ac7d
LT
857}
858
5379a5c5
JH
859struct pbase_tree_cache {
860 unsigned char sha1[20];
861 int ref;
862 int temporary;
863 void *tree_data;
864 unsigned long tree_size;
865};
866
867static struct pbase_tree_cache *(pbase_tree_cache[256]);
868static int pbase_tree_cache_ix(const unsigned char *sha1)
869{
870 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
871}
872static int pbase_tree_cache_ix_incr(int ix)
873{
874 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
875}
876
877static struct pbase_tree {
878 struct pbase_tree *next;
879 /* This is a phony "cache" entry; we are not
880 * going to evict it nor find it through _get()
881 * mechanism -- this is for the toplevel node that
882 * would almost always change with any commit.
883 */
884 struct pbase_tree_cache pcache;
885} *pbase_tree;
886
887static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
888{
889 struct pbase_tree_cache *ent, *nent;
890 void *data;
891 unsigned long size;
21666f1a 892 enum object_type type;
5379a5c5
JH
893 int neigh;
894 int my_ix = pbase_tree_cache_ix(sha1);
895 int available_ix = -1;
896
897 /* pbase-tree-cache acts as a limited hashtable.
898 * your object will be found at your index or within a few
899 * slots after that slot if it is cached.
900 */
901 for (neigh = 0; neigh < 8; neigh++) {
902 ent = pbase_tree_cache[my_ix];
a89fccd2 903 if (ent && !hashcmp(ent->sha1, sha1)) {
5379a5c5
JH
904 ent->ref++;
905 return ent;
906 }
907 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
908 ((0 <= available_ix) &&
909 (!ent && pbase_tree_cache[available_ix])))
910 available_ix = my_ix;
911 if (!ent)
912 break;
913 my_ix = pbase_tree_cache_ix_incr(my_ix);
914 }
915
916 /* Did not find one. Either we got a bogus request or
917 * we need to read and perhaps cache.
918 */
21666f1a 919 data = read_sha1_file(sha1, &type, &size);
5379a5c5
JH
920 if (!data)
921 return NULL;
21666f1a 922 if (type != OBJ_TREE) {
5379a5c5
JH
923 free(data);
924 return NULL;
925 }
926
927 /* We need to either cache or return a throwaway copy */
928
929 if (available_ix < 0)
930 ent = NULL;
931 else {
932 ent = pbase_tree_cache[available_ix];
933 my_ix = available_ix;
934 }
935
936 if (!ent) {
937 nent = xmalloc(sizeof(*nent));
938 nent->temporary = (available_ix < 0);
939 }
940 else {
941 /* evict and reuse */
942 free(ent->tree_data);
943 nent = ent;
944 }
e702496e 945 hashcpy(nent->sha1, sha1);
5379a5c5
JH
946 nent->tree_data = data;
947 nent->tree_size = size;
948 nent->ref = 1;
949 if (!nent->temporary)
950 pbase_tree_cache[my_ix] = nent;
951 return nent;
952}
953
954static void pbase_tree_put(struct pbase_tree_cache *cache)
955{
956 if (!cache->temporary) {
957 cache->ref--;
958 return;
959 }
960 free(cache->tree_data);
961 free(cache);
962}
963
964static int name_cmp_len(const char *name)
965{
966 int i;
967 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
968 ;
969 return i;
970}
971
972static void add_pbase_object(struct tree_desc *tree,
5379a5c5 973 const char *name,
ce0bd642
LT
974 int cmplen,
975 const char *fullname)
3f9ac8d2 976{
4c068a98 977 struct name_entry entry;
8a5a8d6c 978 int cmp;
4c068a98
LT
979
980 while (tree_entry(tree,&entry)) {
1211be6b
LT
981 if (S_ISGITLINK(entry.mode))
982 continue;
8a5a8d6c
NP
983 cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
984 memcmp(name, entry.path, cmplen);
985 if (cmp > 0)
7a979d99 986 continue;
8a5a8d6c
NP
987 if (cmp < 0)
988 return;
5379a5c5 989 if (name[cmplen] != '/') {
5c49c116
NP
990 add_object_entry(entry.sha1,
991 S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB,
bc32fed5 992 fullname, 1);
5379a5c5
JH
993 return;
994 }
8a5a8d6c 995 if (S_ISDIR(entry.mode)) {
7a979d99 996 struct tree_desc sub;
5379a5c5
JH
997 struct pbase_tree_cache *tree;
998 const char *down = name+cmplen+1;
999 int downlen = name_cmp_len(down);
1000
4c068a98 1001 tree = pbase_tree_get(entry.sha1);
5379a5c5
JH
1002 if (!tree)
1003 return;
6fda5e51 1004 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
5379a5c5 1005
ce0bd642 1006 add_pbase_object(&sub, down, downlen, fullname);
5379a5c5
JH
1007 pbase_tree_put(tree);
1008 }
1009 }
1010}
1d6b38cc 1011
5379a5c5
JH
1012static unsigned *done_pbase_paths;
1013static int done_pbase_paths_num;
1014static int done_pbase_paths_alloc;
1015static int done_pbase_path_pos(unsigned hash)
1016{
1017 int lo = 0;
1018 int hi = done_pbase_paths_num;
1019 while (lo < hi) {
1020 int mi = (hi + lo) / 2;
1021 if (done_pbase_paths[mi] == hash)
1022 return mi;
1023 if (done_pbase_paths[mi] < hash)
1024 hi = mi;
1025 else
1026 lo = mi + 1;
1027 }
1028 return -lo-1;
1029}
1030
1031static int check_pbase_path(unsigned hash)
1032{
1033 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
1034 if (0 <= pos)
1035 return 1;
1036 pos = -pos - 1;
1037 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
1038 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
1039 done_pbase_paths = xrealloc(done_pbase_paths,
1040 done_pbase_paths_alloc *
1041 sizeof(unsigned));
1042 }
1043 done_pbase_paths_num++;
1044 if (pos < done_pbase_paths_num)
1045 memmove(done_pbase_paths + pos + 1,
1046 done_pbase_paths + pos,
1047 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1048 done_pbase_paths[pos] = hash;
1049 return 0;
1050}
1051
bc32fed5 1052static void add_preferred_base_object(const char *name)
5379a5c5
JH
1053{
1054 struct pbase_tree *it;
8a5a8d6c 1055 int cmplen;
bc32fed5 1056 unsigned hash = name_hash(name);
5379a5c5 1057
8a5a8d6c 1058 if (!num_preferred_base || check_pbase_path(hash))
5379a5c5
JH
1059 return;
1060
8a5a8d6c 1061 cmplen = name_cmp_len(name);
5379a5c5
JH
1062 for (it = pbase_tree; it; it = it->next) {
1063 if (cmplen == 0) {
bc32fed5 1064 add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1);
5379a5c5
JH
1065 }
1066 else {
1067 struct tree_desc tree;
6fda5e51 1068 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
ce0bd642 1069 add_pbase_object(&tree, name, cmplen, name);
7a979d99 1070 }
3f9ac8d2 1071 }
3f9ac8d2
JH
1072}
1073
7a979d99 1074static void add_preferred_base(unsigned char *sha1)
3f9ac8d2 1075{
5379a5c5
JH
1076 struct pbase_tree *it;
1077 void *data;
1078 unsigned long size;
1079 unsigned char tree_sha1[20];
1d6b38cc 1080
8d1d8f83
JH
1081 if (window <= num_preferred_base++)
1082 return;
1083
5379a5c5
JH
1084 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1085 if (!data)
7a979d99 1086 return;
5379a5c5
JH
1087
1088 for (it = pbase_tree; it; it = it->next) {
a89fccd2 1089 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
5379a5c5
JH
1090 free(data);
1091 return;
1092 }
1093 }
1094
1095 it = xcalloc(1, sizeof(*it));
1096 it->next = pbase_tree;
1097 pbase_tree = it;
1098
e702496e 1099 hashcpy(it->pcache.sha1, tree_sha1);
5379a5c5
JH
1100 it->pcache.tree_data = data;
1101 it->pcache.tree_size = size;
3f9ac8d2
JH
1102}
1103
c323ac7d
LT
1104static void check_object(struct object_entry *entry)
1105{
5c49c116 1106 if (entry->in_pack) {
780e6e73 1107 struct packed_git *p = entry->in_pack;
03e79c88 1108 struct pack_window *w_curs = NULL;
5c49c116
NP
1109 const unsigned char *base_ref = NULL;
1110 struct object_entry *base_entry;
1111 unsigned long used, used_0;
6777a59f 1112 unsigned int avail;
5c49c116
NP
1113 off_t ofs;
1114 unsigned char *buf, c;
780e6e73 1115
6777a59f 1116 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
ab7cd7bb 1117
5c49c116 1118 /*
fa736f72
NP
1119 * We want in_pack_type even if we do not reuse delta
1120 * since non-delta representations could still be reused.
ab7cd7bb 1121 */
6777a59f 1122 used = unpack_object_header_gently(buf, avail,
5c49c116
NP
1123 &entry->in_pack_type,
1124 &entry->size);
ab7cd7bb 1125
5c49c116
NP
1126 /*
1127 * Determine if this is a delta and if so whether we can
1128 * reuse it or not. Otherwise let's find out as cheaply as
1129 * possible what the actual type and size for this object is.
3f9ac8d2 1130 */
5c49c116
NP
1131 switch (entry->in_pack_type) {
1132 default:
1133 /* Not a delta hence we've already got all we need. */
1134 entry->type = entry->in_pack_type;
1135 entry->in_pack_header_size = used;
1136 unuse_pack(&w_curs);
1137 return;
1138 case OBJ_REF_DELTA:
1139 if (!no_reuse_delta && !entry->preferred_base)
1140 base_ref = use_pack(p, &w_curs,
1141 entry->in_pack_offset + used, NULL);
1142 entry->in_pack_header_size = used + 20;
1143 break;
1144 case OBJ_OFS_DELTA:
1145 buf = use_pack(p, &w_curs,
1146 entry->in_pack_offset + used, NULL);
1147 used_0 = 0;
1148 c = buf[used_0++];
1149 ofs = c & 127;
1150 while (c & 128) {
1151 ofs += 1;
1152 if (!ofs || MSB(ofs, 7))
1153 die("delta base offset overflow in pack for %s",
aa7e44bf 1154 sha1_to_hex(entry->idx.sha1));
5c49c116
NP
1155 c = buf[used_0++];
1156 ofs = (ofs << 7) + (c & 127);
780e6e73 1157 }
5c49c116
NP
1158 if (ofs >= entry->in_pack_offset)
1159 die("delta base offset out of bound for %s",
aa7e44bf 1160 sha1_to_hex(entry->idx.sha1));
5c49c116
NP
1161 ofs = entry->in_pack_offset - ofs;
1162 if (!no_reuse_delta && !entry->preferred_base)
1163 base_ref = find_packed_object_name(p, ofs);
1164 entry->in_pack_header_size = used + used_0;
1165 break;
780e6e73 1166 }
780e6e73 1167
5c49c116
NP
1168 if (base_ref && (base_entry = locate_object_entry(base_ref))) {
1169 /*
1170 * If base_ref was set above that means we wish to
1171 * reuse delta data, and we even found that base
1172 * in the list of objects we want to pack. Goodie!
1173 *
1174 * Depth value does not matter - find_deltas() will
1175 * never consider reused delta as the base object to
1176 * deltify other objects against, in order to avoid
1177 * circular deltas.
3f9ac8d2 1178 */
780e6e73 1179 entry->type = entry->in_pack_type;
5c49c116 1180 entry->delta = base_entry;
15b4d577
JH
1181 entry->delta_sibling = base_entry->delta_child;
1182 base_entry->delta_child = entry;
5c49c116
NP
1183 unuse_pack(&w_curs);
1184 return;
1185 }
ab7cd7bb 1186
5c49c116
NP
1187 if (entry->type) {
1188 /*
1189 * This must be a delta and we already know what the
1190 * final object type is. Let's extract the actual
1191 * object size from the delta header.
1192 */
1193 entry->size = get_size_from_delta(p, &w_curs,
1194 entry->in_pack_offset + entry->in_pack_header_size);
1195 unuse_pack(&w_curs);
3f9ac8d2
JH
1196 return;
1197 }
5c49c116
NP
1198
1199 /*
1200 * No choice but to fall back to the recursive delta walk
1201 * with sha1_object_info() to find about the object type
1202 * at this point...
1203 */
1204 unuse_pack(&w_curs);
36e4d74a 1205 }
3f9ac8d2 1206
aa7e44bf 1207 entry->type = sha1_object_info(entry->idx.sha1, &entry->size);
21666f1a 1208 if (entry->type < 0)
36e4d74a 1209 die("unable to get type of object %s",
aa7e44bf 1210 sha1_to_hex(entry->idx.sha1));
3f9ac8d2
JH
1211}
1212
5c49c116
NP
1213static int pack_offset_sort(const void *_a, const void *_b)
1214{
1215 const struct object_entry *a = *(struct object_entry **)_a;
1216 const struct object_entry *b = *(struct object_entry **)_b;
1217
1218 /* avoid filesystem trashing with loose objects */
1219 if (!a->in_pack && !b->in_pack)
aa7e44bf 1220 return hashcmp(a->idx.sha1, b->idx.sha1);
5c49c116
NP
1221
1222 if (a->in_pack < b->in_pack)
1223 return -1;
1224 if (a->in_pack > b->in_pack)
1225 return 1;
1226 return a->in_pack_offset < b->in_pack_offset ? -1 :
1227 (a->in_pack_offset > b->in_pack_offset);
1228}
1229
c323ac7d
LT
1230static void get_object_details(void)
1231{
7cadf491 1232 uint32_t i;
5c49c116
NP
1233 struct object_entry **sorted_by_offset;
1234
1235 sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));
1236 for (i = 0; i < nr_objects; i++)
1237 sorted_by_offset[i] = objects + i;
1238 qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
c323ac7d 1239
3f9ac8d2 1240 prepare_pack_ix();
5c49c116
NP
1241 for (i = 0; i < nr_objects; i++)
1242 check_object(sorted_by_offset[i]);
1243 free(sorted_by_offset);
c323ac7d
LT
1244}
1245
9668cf59 1246static int type_size_sort(const void *_a, const void *_b)
c323ac7d 1247{
9668cf59
NP
1248 const struct object_entry *a = *(struct object_entry **)_a;
1249 const struct object_entry *b = *(struct object_entry **)_b;
1250
c323ac7d
LT
1251 if (a->type < b->type)
1252 return -1;
1253 if (a->type > b->type)
1254 return 1;
27225f2e
LT
1255 if (a->hash < b->hash)
1256 return -1;
1257 if (a->hash > b->hash)
1258 return 1;
7a979d99
JH
1259 if (a->preferred_base < b->preferred_base)
1260 return -1;
1261 if (a->preferred_base > b->preferred_base)
1262 return 1;
c323ac7d
LT
1263 if (a->size < b->size)
1264 return -1;
1265 if (a->size > b->size)
1266 return 1;
adcc7095 1267 return a > b ? -1 : (a < b); /* newest last */
c323ac7d
LT
1268}
1269
1270struct unpacked {
1271 struct object_entry *entry;
1272 void *data;
f6c7081a 1273 struct delta_index *index;
5a235b5e 1274 unsigned depth;
c323ac7d
LT
1275};
1276
d250626c
NP
1277static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1278 unsigned long delta_size)
074b2eea
MK
1279{
1280 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
1281 return 0;
1282
e3dfddb3
MK
1283 if (delta_size < cache_max_small_delta_size)
1284 return 1;
1285
074b2eea
MK
1286 /* cache delta, if objects are large enough compared to delta size */
1287 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
1288 return 1;
1289
1290 return 0;
1291}
1292
c323ac7d 1293/*
521a4f4c
LT
1294 * We search for deltas _backwards_ in a list sorted by type and
1295 * by size, so that we see progressively smaller and smaller files.
1296 * That's because we prefer deltas to be from the bigger file
1297 * to the smaller - deletes are potentially cheaper, but perhaps
1298 * more importantly, the bigger file is likely the more recent
1299 * one.
c323ac7d 1300 */
f6c7081a 1301static int try_delta(struct unpacked *trg, struct unpacked *src,
ef0316fc 1302 unsigned max_depth, unsigned long *mem_usage)
c323ac7d 1303{
f6c7081a
NP
1304 struct object_entry *trg_entry = trg->entry;
1305 struct object_entry *src_entry = src->entry;
560b25a8 1306 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
c83f032e 1307 unsigned ref_depth;
21666f1a 1308 enum object_type type;
c323ac7d
LT
1309 void *delta_buf;
1310
1311 /* Don't bother doing diffs between different types */
f6c7081a 1312 if (trg_entry->type != src_entry->type)
c323ac7d
LT
1313 return -1;
1314
51d1e83f
LT
1315 /*
1316 * We do not bother to try a delta that we discarded
8dbbd14e 1317 * on an earlier try, but only when reusing delta data.
51d1e83f 1318 */
8dbbd14e 1319 if (!no_reuse_delta && trg_entry->in_pack &&
e9195b58
JH
1320 trg_entry->in_pack == src_entry->in_pack &&
1321 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1322 trg_entry->in_pack_type != OBJ_OFS_DELTA)
51d1e83f
LT
1323 return 0;
1324
898b14ce 1325 /* Let's not bust the allowed depth. */
5a235b5e 1326 if (src->depth >= max_depth)
d116a45a 1327 return 0;
c323ac7d 1328
c3b06a69 1329 /* Now some size filtering heuristics. */
560b25a8 1330 trg_size = trg_entry->size;
c83f032e
NP
1331 if (!trg_entry->delta) {
1332 max_size = trg_size/2 - 20;
1333 ref_depth = 1;
1334 } else {
1335 max_size = trg_entry->delta_size;
5a235b5e 1336 ref_depth = trg->depth;
c83f032e 1337 }
5a235b5e 1338 max_size = max_size * (max_depth - src->depth) /
c83f032e 1339 (max_depth - ref_depth + 1);
c3b06a69
NP
1340 if (max_size == 0)
1341 return 0;
f6c7081a 1342 src_size = src_entry->size;
560b25a8 1343 sizediff = src_size < trg_size ? trg_size - src_size : 0;
27225f2e 1344 if (sizediff >= max_size)
f527cb8c 1345 return 0;
a1dab41a
BD
1346 if (trg_size < src_size / 32)
1347 return 0;
f6c7081a 1348
560b25a8
NP
1349 /* Load data if not already done */
1350 if (!trg->data) {
aa7e44bf 1351 trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz);
2e3404c3
JH
1352 if (!trg->data)
1353 die("object %s cannot be read",
1354 sha1_to_hex(trg_entry->idx.sha1));
560b25a8
NP
1355 if (sz != trg_size)
1356 die("object %s inconsistent object length (%lu vs %lu)",
aa7e44bf 1357 sha1_to_hex(trg_entry->idx.sha1), sz, trg_size);
ef0316fc 1358 *mem_usage += sz;
560b25a8
NP
1359 }
1360 if (!src->data) {
aa7e44bf 1361 src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz);
2e3404c3
JH
1362 if (!src->data)
1363 die("object %s cannot be read",
1364 sha1_to_hex(src_entry->idx.sha1));
560b25a8
NP
1365 if (sz != src_size)
1366 die("object %s inconsistent object length (%lu vs %lu)",
aa7e44bf 1367 sha1_to_hex(src_entry->idx.sha1), sz, src_size);
ef0316fc 1368 *mem_usage += sz;
560b25a8
NP
1369 }
1370 if (!src->index) {
1371 src->index = create_delta_index(src->data, src_size);
a588d88a
MK
1372 if (!src->index) {
1373 static int warned = 0;
1374 if (!warned++)
1375 warning("suboptimal pack - out of memory");
1376 return 0;
1377 }
ef0316fc 1378 *mem_usage += sizeof_delta_index(src->index);
560b25a8
NP
1379 }
1380
1381 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
c323ac7d 1382 if (!delta_buf)
75c42d8c 1383 return 0;
f6c7081a 1384
9e2d57a0 1385 if (trg_entry->delta) {
848d732c
BD
1386 /* Prefer only shallower same-sized deltas. */
1387 if (delta_size == trg_entry->delta_size &&
5a235b5e 1388 src->depth + 1 >= trg->depth) {
848d732c
BD
1389 free(delta_buf);
1390 return 0;
1391 }
074b2eea 1392 }
9e2d57a0 1393
f6c7081a
NP
1394 trg_entry->delta = src_entry;
1395 trg_entry->delta_size = delta_size;
5a235b5e 1396 trg->depth = src->depth + 1;
074b2eea 1397
9e2d57a0
NP
1398 if (trg_entry->delta_data) {
1399 delta_cache_size -= trg_entry->delta_size;
1400 free(trg_entry->delta_data);
1401 trg_entry->delta_data = NULL;
1402 }
1403
d250626c 1404 if (delta_cacheable(src_size, trg_size, delta_size)) {
074b2eea
MK
1405 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1406 delta_cache_size += trg_entry->delta_size;
1407 } else
1408 free(delta_buf);
f6c7081a 1409 return 1;
c323ac7d
LT
1410}
1411
898b14ce 1412static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
b2504a0d 1413{
898b14ce
NP
1414 struct object_entry *child = me->delta_child;
1415 unsigned int m = n;
1416 while (child) {
1417 unsigned int c = check_delta_limit(child, n + 1);
1418 if (m < c)
1419 m = c;
1420 child = child->delta_sibling;
1421 }
1422 return m;
b2504a0d
NP
1423}
1424
ef0316fc 1425static unsigned long free_unpacked(struct unpacked *n)
a97773ce 1426{
ef0316fc 1427 unsigned long freed_mem = sizeof_delta_index(n->index);
a97773ce
BD
1428 free_delta_index(n->index);
1429 n->index = NULL;
1430 if (n->data) {
ef0316fc 1431 freed_mem += n->entry->size;
a97773ce
BD
1432 free(n->data);
1433 n->data = NULL;
a97773ce
BD
1434 }
1435 n->entry = NULL;
7d7baa5e 1436 n->depth = 0;
ef0316fc 1437 return freed_mem;
a97773ce
BD
1438}
1439
75d39853 1440static void find_deltas(struct object_entry **list, unsigned list_size,
e334977d 1441 int window, int depth, unsigned *processed)
c323ac7d 1442{
e334977d 1443 uint32_t i = list_size, idx = 0, count = 0;
c323ac7d 1444 unsigned int array_size = window * sizeof(struct unpacked);
7cadf491 1445 struct unpacked *array;
ef0316fc 1446 unsigned long mem_usage = 0;
c323ac7d 1447
7cadf491 1448 array = xmalloc(array_size);
c323ac7d 1449 memset(array, 0, array_size);
21fcd1bd 1450
7cadf491
SP
1451 do {
1452 struct object_entry *entry = list[--i];
c323ac7d 1453 struct unpacked *n = array + idx;
ef0316fc 1454 int j, max_depth, best_base = -1;
c323ac7d 1455
ef0316fc 1456 mem_usage -= free_unpacked(n);
c323ac7d 1457 n->entry = entry;
ab7cd7bb 1458
a97773ce 1459 while (window_memory_limit &&
ef0316fc 1460 mem_usage > window_memory_limit &&
a97773ce
BD
1461 count > 1) {
1462 uint32_t tail = (idx + window - count) % window;
ef0316fc 1463 mem_usage -= free_unpacked(array + tail);
a97773ce
BD
1464 count--;
1465 }
1466
75d39853
NP
1467 /* We do not compute delta to *create* objects we are not
1468 * going to pack.
1469 */
1470 if (entry->preferred_base)
1471 goto next;
1472
e334977d 1473 (*processed)++;
75d39853 1474 if (progress)
e334977d 1475 display_progress(&progress_state, *processed);
75d39853 1476
898b14ce
NP
1477 /*
1478 * If the current object is at pack edge, take the depth the
1479 * objects that depend on the current object into account
1480 * otherwise they would become too deep.
1481 */
1482 max_depth = depth;
1483 if (entry->delta_child) {
1484 max_depth -= check_delta_limit(entry, 0);
1485 if (max_depth <= 0)
1486 goto next;
1487 }
1488
78817c15
LT
1489 j = window;
1490 while (--j > 0) {
77639870 1491 int ret;
7cadf491 1492 uint32_t other_idx = idx + j;
c323ac7d 1493 struct unpacked *m;
78817c15
LT
1494 if (other_idx >= window)
1495 other_idx -= window;
c323ac7d
LT
1496 m = array + other_idx;
1497 if (!m->entry)
1498 break;
ef0316fc 1499 ret = try_delta(n, m, max_depth, &mem_usage);
77639870 1500 if (ret < 0)
c323ac7d 1501 break;
77639870
JH
1502 else if (ret > 0)
1503 best_base = other_idx;
c323ac7d 1504 }
898b14ce 1505
70ca1a3f
JH
1506 /* if we made n a delta, and if n is already at max
1507 * depth, leaving it in the window is pointless. we
1508 * should evict it first.
70ca1a3f 1509 */
5a235b5e 1510 if (entry->delta && depth <= n->depth)
70ca1a3f 1511 continue;
ff45715c 1512
77639870
JH
1513 /*
1514 * Move the best delta base up in the window, after the
1515 * currently deltified object, to keep it longer. It will
1516 * be the first base object to be attempted next.
1517 */
1518 if (entry->delta) {
1519 struct unpacked swap = array[best_base];
1520 int dist = (window + idx - best_base) % window;
1521 int dst = best_base;
1522 while (dist--) {
1523 int src = (dst + 1) % window;
1524 array[dst] = array[src];
1525 dst = src;
1526 }
1527 array[dst] = swap;
1528 }
1529
898b14ce 1530 next:
521a4f4c 1531 idx++;
a97773ce
BD
1532 if (count + 1 < window)
1533 count++;
521a4f4c
LT
1534 if (idx >= window)
1535 idx = 0;
7cadf491 1536 } while (i > 0);
adee7bdf 1537
f6c7081a 1538 for (i = 0; i < window; ++i) {
ff45715c 1539 free_delta_index(array[i].index);
adee7bdf 1540 free(array[i].data);
f6c7081a 1541 }
adee7bdf 1542 free(array);
c323ac7d
LT
1543}
1544
f3123c4a
JH
1545static void prepare_pack(int window, int depth)
1546{
9668cf59 1547 struct object_entry **delta_list;
75d39853 1548 uint32_t i, n, nr_deltas;
9668cf59 1549
3f9ac8d2 1550 get_object_details();
9668cf59 1551
75d39853 1552 if (!nr_objects || !window || !depth)
9668cf59
NP
1553 return;
1554
1555 delta_list = xmalloc(nr_objects * sizeof(*delta_list));
75d39853
NP
1556 nr_deltas = n = 0;
1557
1558 for (i = 0; i < nr_objects; i++) {
1559 struct object_entry *entry = objects + i;
1560
1561 if (entry->delta)
1562 /* This happens if we decided to reuse existing
1563 * delta from a pack. "!no_reuse_delta &&" is implied.
1564 */
1565 continue;
1566
1567 if (entry->size < 50)
1568 continue;
1569
1570 if (entry->no_try_delta)
1571 continue;
1572
1573 if (!entry->preferred_base)
1574 nr_deltas++;
1575
1576 delta_list[n++] = entry;
1577 }
1578
1579 if (nr_deltas) {
e334977d
NP
1580 unsigned nr_done = 0;
1581 if (progress)
1582 start_progress(&progress_state,
1583 "Deltifying %u objects...", "",
1584 nr_deltas);
75d39853 1585 qsort(delta_list, n, sizeof(*delta_list), type_size_sort);
e334977d
NP
1586 find_deltas(delta_list, n, window+1, depth, &nr_done);
1587 if (progress)
1588 stop_progress(&progress_state);
1589 if (nr_done != nr_deltas)
1590 die("inconsistency with delta count");
75d39853 1591 }
9668cf59 1592 free(delta_list);
f3123c4a
JH
1593}
1594
4812a93a
JK
1595static int git_pack_config(const char *k, const char *v)
1596{
1597 if(!strcmp(k, "pack.window")) {
1598 window = git_config_int(k, v);
1599 return 0;
1600 }
a97773ce
BD
1601 if (!strcmp(k, "pack.windowmemory")) {
1602 window_memory_limit = git_config_ulong(k, v);
1603 return 0;
1604 }
1605 if (!strcmp(k, "pack.depth")) {
842aaf93
TT
1606 depth = git_config_int(k, v);
1607 return 0;
1608 }
960ccca6
DH
1609 if (!strcmp(k, "pack.compression")) {
1610 int level = git_config_int(k, v);
1611 if (level == -1)
1612 level = Z_DEFAULT_COMPRESSION;
1613 else if (level < 0 || level > Z_BEST_COMPRESSION)
1614 die("bad pack compression level %d", level);
1615 pack_compression_level = level;
1616 pack_compression_seen = 1;
1617 return 0;
1618 }
074b2eea
MK
1619 if (!strcmp(k, "pack.deltacachesize")) {
1620 max_delta_cache_size = git_config_int(k, v);
1621 return 0;
1622 }
e3dfddb3
MK
1623 if (!strcmp(k, "pack.deltacachelimit")) {
1624 cache_max_small_delta_size = git_config_int(k, v);
1625 return 0;
1626 }
4812a93a
JK
1627 return git_default_config(k, v);
1628}
1629
b5d97e6b 1630static void read_object_list_from_stdin(void)
c323ac7d 1631{
b5d97e6b
JH
1632 char line[40 + 1 + PATH_MAX + 2];
1633 unsigned char sha1[20];
b2504a0d 1634
da93d12b 1635 for (;;) {
da93d12b
LT
1636 if (!fgets(line, sizeof(line), stdin)) {
1637 if (feof(stdin))
1638 break;
1639 if (!ferror(stdin))
1640 die("fgets returned NULL, not EOF, not error!");
687dd75c
JH
1641 if (errno != EINTR)
1642 die("fgets: %s", strerror(errno));
1643 clearerr(stdin);
1644 continue;
da93d12b 1645 }
7a979d99
JH
1646 if (line[0] == '-') {
1647 if (get_sha1_hex(line+1, sha1))
1648 die("expected edge sha1, got garbage:\n %s",
b5d97e6b 1649 line);
8d1d8f83 1650 add_preferred_base(sha1);
7a979d99 1651 continue;
21fcd1bd 1652 }
c323ac7d 1653 if (get_sha1_hex(line, sha1))
ef07618f 1654 die("expected sha1, got garbage:\n %s", line);
b5d97e6b 1655
bc32fed5
JH
1656 add_preferred_base_object(line+41);
1657 add_object_entry(sha1, 0, line+41, 0);
c323ac7d 1658 }
b5d97e6b
JH
1659}
1660
b5d97e6b
JH
1661static void show_commit(struct commit *commit)
1662{
bc32fed5 1663 add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0);
b5d97e6b
JH
1664}
1665
1666static void show_object(struct object_array_entry *p)
1667{
bc32fed5
JH
1668 add_preferred_base_object(p->name);
1669 add_object_entry(p->item->sha1, p->item->type, p->name, 0);
b5d97e6b
JH
1670}
1671
8d1d8f83
JH
1672static void show_edge(struct commit *commit)
1673{
1674 add_preferred_base(commit->object.sha1);
1675}
1676
1677static void get_object_list(int ac, const char **av)
b5d97e6b
JH
1678{
1679 struct rev_info revs;
1680 char line[1000];
b5d97e6b
JH
1681 int flags = 0;
1682
b5d97e6b
JH
1683 init_revisions(&revs, NULL);
1684 save_commit_buffer = 0;
1685 track_object_refs = 0;
1686 setup_revisions(ac, av, &revs, NULL);
1687
b5d97e6b
JH
1688 while (fgets(line, sizeof(line), stdin) != NULL) {
1689 int len = strlen(line);
1690 if (line[len - 1] == '\n')
1691 line[--len] = 0;
1692 if (!len)
1693 break;
1694 if (*line == '-') {
1695 if (!strcmp(line, "--not")) {
1696 flags ^= UNINTERESTING;
1697 continue;
1698 }
1699 die("not a rev '%s'", line);
1700 }
1701 if (handle_revision_arg(line, &revs, flags, 1))
1702 die("bad revision '%s'", line);
1703 }
1704
1705 prepare_revision_walk(&revs);
8d1d8f83 1706 mark_edges_uninteresting(revs.commits, &revs, show_edge);
b5d97e6b
JH
1707 traverse_commit_list(&revs, show_commit, show_object);
1708}
1709
b6b32ccb
JH
1710static int adjust_perm(const char *path, mode_t mode)
1711{
1712 if (chmod(path, mode))
1713 return -1;
1714 return adjust_shared_perm(path);
1715}
1716
b5d97e6b
JH
1717int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1718{
b5d97e6b 1719 int use_internal_rev_list = 0;
8d1d8f83 1720 int thin = 0;
7cadf491 1721 uint32_t i;
ffa84ffb
RD
1722 const char **rp_av;
1723 int rp_ac_alloc = 64;
8d1d8f83
JH
1724 int rp_ac;
1725
ffa84ffb
RD
1726 rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
1727
8d1d8f83
JH
1728 rp_av[0] = "pack-objects";
1729 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1730 rp_ac = 2;
b5d97e6b
JH
1731
1732 git_config(git_pack_config);
960ccca6
DH
1733 if (!pack_compression_seen && core_compression_seen)
1734 pack_compression_level = core_compression_level;
b5d97e6b
JH
1735
1736 progress = isatty(2);
1737 for (i = 1; i < argc; i++) {
1738 const char *arg = argv[i];
1739
1740 if (*arg != '-')
1741 break;
1742
1743 if (!strcmp("--non-empty", arg)) {
1744 non_empty = 1;
1745 continue;
1746 }
1747 if (!strcmp("--local", arg)) {
1748 local = 1;
1749 continue;
1750 }
b5d97e6b
JH
1751 if (!strcmp("--incremental", arg)) {
1752 incremental = 1;
1753 continue;
1754 }
960ccca6
DH
1755 if (!prefixcmp(arg, "--compression=")) {
1756 char *end;
1757 int level = strtoul(arg+14, &end, 0);
1758 if (!arg[14] || *end)
1759 usage(pack_usage);
1760 if (level == -1)
1761 level = Z_DEFAULT_COMPRESSION;
1762 else if (level < 0 || level > Z_BEST_COMPRESSION)
1763 die("bad pack compression level %d", level);
1764 pack_compression_level = level;
1765 continue;
1766 }
6b94b1a0
DH
1767 if (!prefixcmp(arg, "--max-pack-size=")) {
1768 char *end;
1769 pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024;
1770 if (!arg[16] || *end)
1771 usage(pack_usage);
1772 continue;
1773 }
599065a3 1774 if (!prefixcmp(arg, "--window=")) {
b5d97e6b
JH
1775 char *end;
1776 window = strtoul(arg+9, &end, 0);
1777 if (!arg[9] || *end)
1778 usage(pack_usage);
1779 continue;
1780 }
a97773ce
BD
1781 if (!prefixcmp(arg, "--window-memory=")) {
1782 if (!git_parse_ulong(arg+16, &window_memory_limit))
1783 usage(pack_usage);
1784 continue;
1785 }
599065a3 1786 if (!prefixcmp(arg, "--depth=")) {
b5d97e6b
JH
1787 char *end;
1788 depth = strtoul(arg+8, &end, 0);
1789 if (!arg[8] || *end)
1790 usage(pack_usage);
1791 continue;
1792 }
1793 if (!strcmp("--progress", arg)) {
1794 progress = 1;
1795 continue;
1796 }
231f240b
NP
1797 if (!strcmp("--all-progress", arg)) {
1798 progress = 2;
1799 continue;
1800 }
b5d97e6b
JH
1801 if (!strcmp("-q", arg)) {
1802 progress = 0;
1803 continue;
1804 }
1805 if (!strcmp("--no-reuse-delta", arg)) {
1806 no_reuse_delta = 1;
1807 continue;
1808 }
fa736f72
NP
1809 if (!strcmp("--no-reuse-object", arg)) {
1810 no_reuse_object = no_reuse_delta = 1;
1811 continue;
1812 }
be6b1914 1813 if (!strcmp("--delta-base-offset", arg)) {
780e6e73 1814 allow_ofs_delta = 1;
be6b1914
NP
1815 continue;
1816 }
b5d97e6b
JH
1817 if (!strcmp("--stdout", arg)) {
1818 pack_to_stdout = 1;
1819 continue;
1820 }
1821 if (!strcmp("--revs", arg)) {
1822 use_internal_rev_list = 1;
1823 continue;
1824 }
8d1d8f83 1825 if (!strcmp("--unpacked", arg) ||
599065a3 1826 !prefixcmp(arg, "--unpacked=") ||
63049292 1827 !strcmp("--reflog", arg) ||
8d1d8f83
JH
1828 !strcmp("--all", arg)) {
1829 use_internal_rev_list = 1;
ffa84ffb
RD
1830 if (rp_ac >= rp_ac_alloc - 1) {
1831 rp_ac_alloc = alloc_nr(rp_ac_alloc);
1832 rp_av = xrealloc(rp_av,
1833 rp_ac_alloc * sizeof(*rp_av));
1834 }
8d1d8f83 1835 rp_av[rp_ac++] = arg;
b5d97e6b
JH
1836 continue;
1837 }
8d1d8f83
JH
1838 if (!strcmp("--thin", arg)) {
1839 use_internal_rev_list = 1;
1840 thin = 1;
1841 rp_av[1] = "--objects-edge";
b5d97e6b 1842 continue;
4ba7d711
NP
1843 }
1844 if (!prefixcmp(arg, "--index-version=")) {
1845 char *c;
aa7e44bf
GB
1846 pack_idx_default_version = strtoul(arg + 16, &c, 10);
1847 if (pack_idx_default_version > 2)
4ba7d711
NP
1848 die("bad %s", arg);
1849 if (*c == ',')
aa7e44bf
GB
1850 pack_idx_off32_limit = strtoul(c+1, &c, 0);
1851 if (*c || pack_idx_off32_limit & 0x80000000)
4ba7d711
NP
1852 die("bad %s", arg);
1853 continue;
b5d97e6b
JH
1854 }
1855 usage(pack_usage);
1856 }
1857
1858 /* Traditionally "pack-objects [options] base extra" failed;
1859 * we would however want to take refs parameter that would
1860 * have been given to upstream rev-list ourselves, which means
1861 * we somehow want to say what the base name is. So the
1862 * syntax would be:
1863 *
1864 * pack-objects [options] base <refs...>
1865 *
1866 * in other words, we would treat the first non-option as the
1867 * base_name and send everything else to the internal revision
1868 * walker.
1869 */
1870
1871 if (!pack_to_stdout)
1872 base_name = argv[i++];
1873
1874 if (pack_to_stdout != !base_name)
1875 usage(pack_usage);
1876
01c12a23
DH
1877 if (pack_to_stdout && pack_size_limit)
1878 die("--max-pack-size cannot be used to build a pack for transfer.");
1879
8d1d8f83
JH
1880 if (!pack_to_stdout && thin)
1881 die("--thin cannot be used to build an indexable pack.");
b5d97e6b
JH
1882
1883 prepare_packed_git();
1884
13aaf148
NP
1885 if (progress)
1886 start_progress(&progress_state, "Generating pack...",
1887 "Counting objects: ", 0);
b5d97e6b
JH
1888 if (!use_internal_rev_list)
1889 read_object_list_from_stdin();
8d1d8f83
JH
1890 else {
1891 rp_av[rp_ac] = NULL;
1892 get_object_list(rp_ac, rp_av);
1893 }
96a02f8f
NP
1894 if (progress) {
1895 stop_progress(&progress_state);
7cadf491 1896 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
96a02f8f
NP
1897 }
1898
f0b0af1b 1899 if (non_empty && !nr_result)
1c4a2912 1900 return 0;
7a979d99 1901 if (progress && (nr_objects != nr_result))
7cadf491 1902 fprintf(stderr, "Result has %u objects.\n", nr_result);
f7ae6a93
NP
1903 if (nr_result)
1904 prepare_pack(window, depth);
d01fb92f 1905 write_pack_file();
ab7cd7bb 1906 if (progress)
7cadf491 1907 fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
67c08ce1 1908 written, written_delta, reused, reused_delta);
c323ac7d
LT
1909 return 0;
1910}