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