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