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