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