verify-pack -v: show delta-chain histogram.
[git/git.git] / pack-objects.c
CommitLineData
c323ac7d
LT
1#include "cache.h"
2#include "object.h"
3#include "delta.h"
a733cb60 4#include "pack.h"
c38138cd 5#include "csum-file.h"
7a979d99 6#include "diff.h"
21fcd1bd 7#include <sys/time.h>
b2504a0d 8#include <signal.h>
c323ac7d 9
ab7cd7bb 10static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
c323ac7d 11
c323ac7d
LT
12struct object_entry {
13 unsigned char sha1[20];
3f9ac8d2 14 unsigned long size; /* uncompressed size */
15b4d577
JH
15 unsigned long offset; /* offset into the final pack file;
16 * nonzero if already written.
17 */
3f9ac8d2 18 unsigned int depth; /* delta depth */
15b4d577 19 unsigned int delta_limit; /* base adjustment for in-pack delta */
3f9ac8d2 20 unsigned int hash; /* name hint hash */
a733cb60 21 enum object_type type;
ab7cd7bb 22 enum object_type in_pack_type; /* could be delta */
3f9ac8d2
JH
23 unsigned long delta_size; /* delta data size (uncompressed) */
24 struct object_entry *delta; /* delta base object */
25 struct packed_git *in_pack; /* already in pack */
3f9ac8d2 26 unsigned int in_pack_offset;
15b4d577
JH
27 struct object_entry *delta_child; /* delitified objects who bases me */
28 struct object_entry *delta_sibling; /* other deltified objects who
29 * uses the same base as me
30 */
7a979d99
JH
31 int preferred_base; /* we do not pack this, but is encouraged to
32 * be used as the base objectto delta huge
33 * objects against.
34 */
35 int based_on_preferred; /* current delta candidate is a preferred
36 * one, or delta against a preferred one.
37 */
c323ac7d
LT
38};
39
3f9ac8d2
JH
40/*
41 * Objects we are going to pack are colected in objects array (dynamically
42 * expanded). nr_objects & nr_alloc controls this array. They are stored
43 * in the order we see -- typically rev-list --objects order that gives us
44 * nice "minimum seek" order.
45 *
46 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
47 * elements in the objects array. The former is used to build the pack
48 * index (lists object names in the ascending order to help offset lookup),
49 * and the latter is used to group similar things together by try_delta()
50 * heuristics.
51 */
52
5f3de58f 53static unsigned char object_list_sha1[20];
1c4a2912 54static int non_empty = 0;
ab7cd7bb 55static int no_reuse_delta = 0;
64560374 56static int local = 0;
eb019375 57static int incremental = 0;
c323ac7d
LT
58static struct object_entry **sorted_by_sha, **sorted_by_type;
59static struct object_entry *objects = NULL;
7a979d99 60static int nr_objects = 0, nr_alloc = 0, nr_result = 0;
c323ac7d 61static const char *base_name;
e1808845 62static unsigned char pack_file_sha1[20];
024701f1 63static int progress = 1;
5e8dc750 64static volatile int progress_update = 0;
c323ac7d 65
3f9ac8d2
JH
66/*
67 * The object names in objects array are hashed with this hashtable,
68 * to help looking up the entry by object name. Binary search from
69 * sorted_by_sha is also possible but this was easier to code and faster.
70 * This hashtable is built after all the objects are seen.
71 */
72static int *object_ix = NULL;
73static int object_ix_hashsz = 0;
74
75/*
76 * Pack index for existing packs give us easy access to the offsets into
77 * corresponding pack file where each object's data starts, but the entries
78 * do not store the size of the compressed representation (uncompressed
79 * size is easily available by examining the pack entry header). We build
80 * a hashtable of existing packs (pack_revindex), and keep reverse index
81 * here -- pack index file is sorted by object name mapping to offset; this
82 * pack_revindex[].revindex array is an ordered list of offsets, so if you
83 * know the offset of an object, next offset is where its packed
84 * representation ends.
85 */
86struct pack_revindex {
87 struct packed_git *p;
88 unsigned long *revindex;
89} *pack_revindex = NULL;
90static int pack_revindex_hashsz = 0;
91
92/*
93 * stats
94 */
95static int written = 0;
ab7cd7bb 96static int written_delta = 0;
3f9ac8d2 97static int reused = 0;
ab7cd7bb 98static int reused_delta = 0;
3f9ac8d2
JH
99
100static int pack_revindex_ix(struct packed_git *p)
101{
2b74cffa 102 unsigned long ui = (unsigned long)p;
3f9ac8d2
JH
103 int i;
104
105 ui = ui ^ (ui >> 16); /* defeat structure alignment */
106 i = (int)(ui % pack_revindex_hashsz);
107 while (pack_revindex[i].p) {
108 if (pack_revindex[i].p == p)
109 return i;
110 if (++i == pack_revindex_hashsz)
111 i = 0;
112 }
113 return -1 - i;
114}
115
116static void prepare_pack_ix(void)
117{
118 int num;
119 struct packed_git *p;
120 for (num = 0, p = packed_git; p; p = p->next)
121 num++;
122 if (!num)
123 return;
124 pack_revindex_hashsz = num * 11;
125 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
126 for (p = packed_git; p; p = p->next) {
127 num = pack_revindex_ix(p);
128 num = - 1 - num;
129 pack_revindex[num].p = p;
130 }
131 /* revindex elements are lazily initialized */
132}
133
134static int cmp_offset(const void *a_, const void *b_)
135{
136 unsigned long a = *(unsigned long *) a_;
137 unsigned long b = *(unsigned long *) b_;
138 if (a < b)
139 return -1;
140 else if (a == b)
141 return 0;
142 else
143 return 1;
144}
145
146/*
147 * Ordered list of offsets of objects in the pack.
148 */
149static void prepare_pack_revindex(struct pack_revindex *rix)
150{
151 struct packed_git *p = rix->p;
152 int num_ent = num_packed_objects(p);
153 int i;
154 void *index = p->index_base + 256;
155
156 rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
157 for (i = 0; i < num_ent; i++) {
158 long hl = *((long *)(index + 24 * i));
159 rix->revindex[i] = ntohl(hl);
160 }
161 /* This knows the pack format -- the 20-byte trailer
162 * follows immediately after the last object data.
163 */
164 rix->revindex[num_ent] = p->pack_size - 20;
165 qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset);
166}
167
168static unsigned long find_packed_object_size(struct packed_git *p,
169 unsigned long ofs)
170{
171 int num;
172 int lo, hi;
173 struct pack_revindex *rix;
174 unsigned long *revindex;
175 num = pack_revindex_ix(p);
176 if (num < 0)
177 die("internal error: pack revindex uninitialized");
178 rix = &pack_revindex[num];
179 if (!rix->revindex)
180 prepare_pack_revindex(rix);
181 revindex = rix->revindex;
182 lo = 0;
183 hi = num_packed_objects(p) + 1;
184 do {
185 int mi = (lo + hi) / 2;
186 if (revindex[mi] == ofs) {
187 return revindex[mi+1] - ofs;
188 }
189 else if (ofs < revindex[mi])
190 hi = mi;
191 else
192 lo = mi + 1;
193 } while (lo < hi);
194 die("internal error: pack revindex corrupt");
195}
196
c323ac7d
LT
197static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
198{
199 unsigned long othersize, delta_size;
200 char type[10];
201 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
202 void *delta_buf;
203
204 if (!otherbuf)
205 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
8ee378a0 206 delta_buf = diff_delta(otherbuf, othersize,
dcde55bc 207 buf, size, &delta_size, 0);
75c42d8c 208 if (!delta_buf || delta_size != entry->delta_size)
c323ac7d
LT
209 die("delta size changed");
210 free(buf);
211 free(otherbuf);
212 return delta_buf;
213}
214
a733cb60
LT
215/*
216 * The per-object header is a pretty dense thing, which is
217 * - first byte: low four bits are "size", then three bits of "type",
218 * and the high bit is "size continues".
219 * - each byte afterwards: low seven bits are size continuation,
220 * with the high bit being "size continues"
221 */
222static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
223{
01247d87 224 int n = 1;
a733cb60
LT
225 unsigned char c;
226
227 if (type < OBJ_COMMIT || type > OBJ_DELTA)
228 die("bad type %d", type);
229
01247d87
LT
230 c = (type << 4) | (size & 15);
231 size >>= 4;
232 while (size) {
a733cb60 233 *hdr++ = c | 0x80;
01247d87
LT
234 c = size & 0x7f;
235 size >>= 7;
236 n++;
a733cb60
LT
237 }
238 *hdr = c;
239 return n;
240}
241
7a979d99
JH
242static unsigned long write_object(struct sha1file *f,
243 struct object_entry *entry)
c323ac7d
LT
244{
245 unsigned long size;
246 char type[10];
3f9ac8d2 247 void *buf;
a733cb60 248 unsigned char header[10];
c323ac7d 249 unsigned hdrlen, datalen;
a733cb60 250 enum object_type obj_type;
ab7cd7bb 251 int to_reuse = 0;
c323ac7d 252
7a979d99
JH
253 if (entry->preferred_base)
254 return 0;
c323ac7d 255
a733cb60 256 obj_type = entry->type;
ab7cd7bb
JH
257 if (! entry->in_pack)
258 to_reuse = 0; /* can't reuse what we don't have */
259 else if (obj_type == OBJ_DELTA)
260 to_reuse = 1; /* check_object() decided it for us */
261 else if (obj_type != entry->in_pack_type)
262 to_reuse = 0; /* pack has delta which is unusable */
263 else if (entry->delta)
264 to_reuse = 0; /* we want to pack afresh */
265 else
266 to_reuse = 1; /* we have it in-pack undeltified,
267 * and we do not need to deltify it.
268 */
269
270 if (! to_reuse) {
3f9ac8d2
JH
271 buf = read_sha1_file(entry->sha1, type, &size);
272 if (!buf)
273 die("unable to read %s", sha1_to_hex(entry->sha1));
274 if (size != entry->size)
275 die("object %s size inconsistency (%lu vs %lu)",
276 sha1_to_hex(entry->sha1), size, entry->size);
277 if (entry->delta) {
278 buf = delta_against(buf, size, entry);
279 size = entry->delta_size;
280 obj_type = OBJ_DELTA;
281 }
282 /*
283 * The object header is a byte of 'type' followed by zero or
284 * more bytes of length. For deltas, the 20 bytes of delta
285 * sha1 follows that.
286 */
287 hdrlen = encode_header(obj_type, size, header);
288 sha1write(f, header, hdrlen);
289
290 if (entry->delta) {
291 sha1write(f, entry->delta, 20);
292 hdrlen += 20;
293 }
294 datalen = sha1write_compressed(f, buf, size);
295 free(buf);
c323ac7d 296 }
3f9ac8d2
JH
297 else {
298 struct packed_git *p = entry->in_pack;
299 use_packed_git(p);
300
301 datalen = find_packed_object_size(p, entry->in_pack_offset);
302 buf = p->pack_base + entry->in_pack_offset;
303 sha1write(f, buf, datalen);
304 unuse_packed_git(p);
305 hdrlen = 0; /* not really */
ab7cd7bb
JH
306 if (obj_type == OBJ_DELTA)
307 reused_delta++;
3f9ac8d2 308 reused++;
a733cb60 309 }
ab7cd7bb
JH
310 if (obj_type == OBJ_DELTA)
311 written_delta++;
3f9ac8d2 312 written++;
c323ac7d
LT
313 return hdrlen + datalen;
314}
315
9d5ab962
JH
316static unsigned long write_one(struct sha1file *f,
317 struct object_entry *e,
318 unsigned long offset)
319{
320 if (e->offset)
321 /* offset starts from header size and cannot be zero
322 * if it is written already.
323 */
324 return offset;
325 e->offset = offset;
326 offset += write_object(f, e);
82f9d58a 327 /* if we are deltified, write out its base object. */
9d5ab962
JH
328 if (e->delta)
329 offset = write_one(f, e->delta, offset);
330 return offset;
331}
332
c323ac7d
LT
333static void write_pack_file(void)
334{
335 int i;
d22b9290 336 struct sha1file *f;
a733cb60 337 unsigned long offset;
a733cb60 338 struct pack_header hdr;
183bdb2c
JH
339 unsigned last_percent = 999;
340 int do_progress = 0;
c323ac7d 341
d22b9290
LT
342 if (!base_name)
343 f = sha1fd(1, "<stdout>");
183bdb2c
JH
344 else {
345 f = sha1create("%s-%s.%s", base_name,
346 sha1_to_hex(object_list_sha1), "pack");
347 do_progress = progress;
348 }
349 if (do_progress)
f0b0af1b 350 fprintf(stderr, "Writing %d objects.\n", nr_result);
183bdb2c 351
a733cb60 352 hdr.hdr_signature = htonl(PACK_SIGNATURE);
01247d87 353 hdr.hdr_version = htonl(PACK_VERSION);
7a979d99 354 hdr.hdr_entries = htonl(nr_result);
a733cb60
LT
355 sha1write(f, &hdr, sizeof(hdr));
356 offset = sizeof(hdr);
f0b0af1b
JH
357 if (!nr_result)
358 goto done;
5e8dc750 359 for (i = 0; i < nr_objects; i++) {
9d5ab962 360 offset = write_one(f, objects + i, offset);
183bdb2c 361 if (do_progress) {
f0b0af1b 362 unsigned percent = written * 100 / nr_result;
183bdb2c
JH
363 if (progress_update || percent != last_percent) {
364 fprintf(stderr, "%4u%% (%u/%u) done\r",
f0b0af1b 365 percent, written, nr_result);
183bdb2c
JH
366 progress_update = 0;
367 last_percent = percent;
368 }
5e8dc750
NP
369 }
370 }
183bdb2c
JH
371 if (do_progress)
372 fputc('\n', stderr);
f0b0af1b 373 done:
e1808845 374 sha1close(f, pack_file_sha1, 1);
c323ac7d
LT
375}
376
377static void write_index_file(void)
378{
379 int i;
7a979d99
JH
380 struct sha1file *f = sha1create("%s-%s.%s", base_name,
381 sha1_to_hex(object_list_sha1), "idx");
c323ac7d 382 struct object_entry **list = sorted_by_sha;
7a979d99 383 struct object_entry **last = list + nr_result;
c323ac7d
LT
384 unsigned int array[256];
385
386 /*
387 * Write the first-level table (the list is sorted,
388 * but we use a 256-entry lookup to be able to avoid
e1808845 389 * having to do eight extra binary search iterations).
c323ac7d
LT
390 */
391 for (i = 0; i < 256; i++) {
392 struct object_entry **next = list;
393 while (next < last) {
394 struct object_entry *entry = *next;
395 if (entry->sha1[0] != i)
396 break;
397 next++;
398 }
399 array[i] = htonl(next - sorted_by_sha);
400 list = next;
401 }
c38138cd 402 sha1write(f, array, 256 * sizeof(int));
c323ac7d
LT
403
404 /*
405 * Write the actual SHA1 entries..
406 */
407 list = sorted_by_sha;
7a979d99 408 for (i = 0; i < nr_result; i++) {
c323ac7d
LT
409 struct object_entry *entry = *list++;
410 unsigned int offset = htonl(entry->offset);
c38138cd
LT
411 sha1write(f, &offset, 4);
412 sha1write(f, entry->sha1, 20);
c323ac7d 413 }
e1808845
LT
414 sha1write(f, pack_file_sha1, 20);
415 sha1close(f, NULL, 1);
c323ac7d
LT
416}
417
7a979d99
JH
418static int locate_object_entry_hash(const unsigned char *sha1)
419{
420 int i;
421 unsigned int ui;
422 memcpy(&ui, sha1, sizeof(unsigned int));
423 i = ui % object_ix_hashsz;
424 while (0 < object_ix[i]) {
425 if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
426 return i;
427 if (++i == object_ix_hashsz)
428 i = 0;
429 }
430 return -1 - i;
431}
432
433static struct object_entry *locate_object_entry(const unsigned char *sha1)
434{
435 int i;
436
437 if (!object_ix_hashsz)
438 return NULL;
439
440 i = locate_object_entry_hash(sha1);
441 if (0 <= i)
442 return &objects[object_ix[i]-1];
443 return NULL;
444}
445
446static void rehash_objects(void)
c323ac7d 447{
7a979d99
JH
448 int i;
449 struct object_entry *oe;
450
451 object_ix_hashsz = nr_objects * 3;
452 if (object_ix_hashsz < 1024)
453 object_ix_hashsz = 1024;
454 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
455 object_ix = memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
456 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
457 int ix = locate_object_entry_hash(oe->sha1);
458 if (0 <= ix)
459 continue;
460 ix = -1 - ix;
461 object_ix[ix] = i + 1;
462 }
463}
464
1d6b38cc
JH
465struct name_path {
466 struct name_path *up;
467 const char *elem;
468 int len;
469};
470
eeef7135
JH
471#define DIRBITS 12
472
1d6b38cc
JH
473static unsigned name_hash(struct name_path *path, const char *name)
474{
475 struct name_path *p = path;
476 const char *n = name + strlen(name);
eeef7135 477 unsigned hash = 0, name_hash = 0, name_done = 0;
1d6b38cc
JH
478
479 if (n != name && n[-1] == '\n')
480 n--;
481 while (name <= --n) {
482 unsigned char c = *n;
eeef7135
JH
483 if (c == '/' && !name_done) {
484 name_hash = hash;
485 name_done = 1;
486 hash = 0;
487 }
1d6b38cc
JH
488 hash = hash * 11 + c;
489 }
eeef7135
JH
490 if (!name_done) {
491 name_hash = hash;
492 hash = 0;
493 }
1d6b38cc
JH
494 for (p = path; p; p = p->up) {
495 hash = hash * 11 + '/';
496 n = p->elem + p->len;
497 while (p->elem <= --n) {
498 unsigned char c = *n;
499 hash = hash * 11 + c;
500 }
501 }
eeef7135
JH
502 /*
503 * Make sure "Makefile" and "t/Makefile" are hashed separately
504 * but close enough.
505 */
506 hash = (name_hash<<DIRBITS) | (hash & ((1U<<DIRBITS )-1));
507
508 if (0) { /* debug */
509 n = name + strlen(name);
510 if (n != name && n[-1] == '\n')
511 n--;
512 while (name <= --n)
513 fputc(*n, stderr);
514 for (p = path; p; p = p->up) {
515 fputc('/', stderr);
516 n = p->elem + p->len;
517 while (p->elem <= --n)
518 fputc(*n, stderr);
519 }
520 fprintf(stderr, "\t%08x\n", hash);
521 }
1d6b38cc
JH
522 return hash;
523}
524
525static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
c323ac7d
LT
526{
527 unsigned int idx = nr_objects;
528 struct object_entry *entry;
3f9ac8d2 529 struct packed_git *p;
ab7cd7bb
JH
530 unsigned int found_offset = 0;
531 struct packed_git *found_pack = NULL;
b925410d 532 int ix, status = 0;
c323ac7d 533
7a979d99 534 if (!exclude) {
64560374
LT
535 for (p = packed_git; p; p = p->next) {
536 struct pack_entry e;
64560374
LT
537 if (find_pack_entry_one(sha1, &e, p)) {
538 if (incremental)
539 return 0;
540 if (local && !p->pack_local)
541 return 0;
7a979d99
JH
542 if (!found_pack) {
543 found_offset = e.offset;
544 found_pack = e.p;
545 }
64560374
LT
546 }
547 }
548 }
7a979d99
JH
549 if ((entry = locate_object_entry(sha1)) != NULL)
550 goto already_added;
eb019375 551
c323ac7d
LT
552 if (idx >= nr_alloc) {
553 unsigned int needed = (idx + 1024) * 3 / 2;
554 objects = xrealloc(objects, needed * sizeof(*entry));
555 nr_alloc = needed;
556 }
557 entry = objects + idx;
7a979d99 558 nr_objects = idx + 1;
c323ac7d
LT
559 memset(entry, 0, sizeof(*entry));
560 memcpy(entry->sha1, sha1, 20);
27225f2e 561 entry->hash = hash;
7a979d99
JH
562
563 if (object_ix_hashsz * 3 <= nr_objects * 4)
564 rehash_objects();
565 else {
566 ix = locate_object_entry_hash(entry->sha1);
567 if (0 <= ix)
568 die("internal error in object hashing.");
569 object_ix[-1 - ix] = idx + 1;
3f9ac8d2 570 }
b925410d 571 status = 1;
7a979d99
JH
572
573 already_added:
f0b0af1b
JH
574 if (progress_update) {
575 fprintf(stderr, "Counting objects...%d\r", nr_objects);
576 progress_update = 0;
577 }
7a979d99
JH
578 if (exclude)
579 entry->preferred_base = 1;
580 else {
581 if (found_pack) {
582 entry->in_pack = found_pack;
583 entry->in_pack_offset = found_offset;
584 }
a49dd05f 585 }
b925410d 586 return status;
c323ac7d
LT
587}
588
1d6b38cc 589static void add_pbase_tree(struct tree_desc *tree, struct name_path *up)
3f9ac8d2 590{
7a979d99
JH
591 while (tree->size) {
592 const unsigned char *sha1;
593 const char *name;
1d6b38cc 594 unsigned mode, hash;
7a979d99
JH
595 unsigned long size;
596 char type[20];
597
598 sha1 = tree_entry_extract(tree, &name, &mode);
599 update_tree_entry(tree);
600 if (!has_sha1_file(sha1))
601 continue;
602 if (sha1_object_info(sha1, type, &size))
603 continue;
b925410d 604
1d6b38cc
JH
605 hash = name_hash(up, name);
606 if (!add_object_entry(sha1, hash, 1))
b925410d
JH
607 continue;
608
7a979d99
JH
609 if (!strcmp(type, "tree")) {
610 struct tree_desc sub;
611 void *elem;
1d6b38cc
JH
612 struct name_path me;
613
7a979d99
JH
614 elem = read_sha1_file(sha1, type, &sub.size);
615 sub.buf = elem;
616 if (sub.buf) {
1d6b38cc
JH
617 me.up = up;
618 me.elem = name;
619 me.len = strlen(name);
620 add_pbase_tree(&sub, &me);
7a979d99
JH
621 free(elem);
622 }
623 }
3f9ac8d2 624 }
3f9ac8d2
JH
625}
626
7a979d99 627static void add_preferred_base(unsigned char *sha1)
3f9ac8d2 628{
7a979d99
JH
629 struct tree_desc tree;
630 void *elem;
1d6b38cc 631
7a979d99
JH
632 elem = read_object_with_reference(sha1, "tree", &tree.size, NULL);
633 tree.buf = elem;
634 if (!tree.buf)
635 return;
1d6b38cc
JH
636 if (add_object_entry(sha1, name_hash(NULL, ""), 1))
637 add_pbase_tree(&tree, NULL);
7a979d99 638 free(elem);
3f9ac8d2
JH
639}
640
c323ac7d
LT
641static void check_object(struct object_entry *entry)
642{
36e4d74a
JH
643 char type[20];
644
7a979d99 645 if (entry->in_pack && !entry->preferred_base) {
ab7cd7bb
JH
646 unsigned char base[20];
647 unsigned long size;
648 struct object_entry *base_entry;
649
650 /* We want in_pack_type even if we do not reuse delta.
651 * There is no point not reusing non-delta representations.
652 */
653 check_reuse_pack_delta(entry->in_pack,
654 entry->in_pack_offset,
655 base, &size,
656 &entry->in_pack_type);
657
3f9ac8d2
JH
658 /* Check if it is delta, and the base is also an object
659 * we are going to pack. If so we will reuse the existing
660 * delta.
661 */
ab7cd7bb
JH
662 if (!no_reuse_delta &&
663 entry->in_pack_type == OBJ_DELTA &&
7a979d99
JH
664 (base_entry = locate_object_entry(base)) &&
665 (!base_entry->preferred_base)) {
ab7cd7bb
JH
666
667 /* Depth value does not matter - find_deltas()
668 * will never consider reused delta as the
669 * base object to deltify other objects
670 * against, in order to avoid circular deltas.
3f9ac8d2 671 */
ab7cd7bb
JH
672
673 /* uncompressed size of the delta data */
3f9ac8d2
JH
674 entry->size = entry->delta_size = size;
675 entry->delta = base_entry;
676 entry->type = OBJ_DELTA;
ab7cd7bb 677
15b4d577
JH
678 entry->delta_sibling = base_entry->delta_child;
679 base_entry->delta_child = entry;
ab7cd7bb 680
3f9ac8d2
JH
681 return;
682 }
683 /* Otherwise we would do the usual */
36e4d74a 684 }
3f9ac8d2
JH
685
686 if (sha1_object_info(entry->sha1, type, &entry->size))
36e4d74a
JH
687 die("unable to get type of object %s",
688 sha1_to_hex(entry->sha1));
3f9ac8d2
JH
689
690 if (!strcmp(type, "commit")) {
691 entry->type = OBJ_COMMIT;
692 } else if (!strcmp(type, "tree")) {
693 entry->type = OBJ_TREE;
694 } else if (!strcmp(type, "blob")) {
695 entry->type = OBJ_BLOB;
696 } else if (!strcmp(type, "tag")) {
697 entry->type = OBJ_TAG;
698 } else
699 die("unable to pack object %s of type %s",
700 sha1_to_hex(entry->sha1), type);
701}
702
15b4d577
JH
703static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
704{
705 struct object_entry *child = me->delta_child;
706 unsigned int m = n;
707 while (child) {
708 unsigned int c = check_delta_limit(child, n + 1);
709 if (m < c)
710 m = c;
711 child = child->delta_sibling;
712 }
713 return m;
714}
715
c323ac7d
LT
716static void get_object_details(void)
717{
718 int i;
15b4d577 719 struct object_entry *entry;
c323ac7d 720
3f9ac8d2 721 prepare_pack_ix();
15b4d577
JH
722 for (i = 0, entry = objects; i < nr_objects; i++, entry++)
723 check_object(entry);
b76f6b62
JH
724
725 if (nr_objects == nr_result) {
726 /*
727 * Depth of objects that depend on the entry -- this
728 * is subtracted from depth-max to break too deep
729 * delta chain because of delta data reusing.
730 * However, we loosen this restriction when we know we
731 * are creating a thin pack -- it will have to be
732 * expanded on the other end anyway, so do not
733 * artificially cut the delta chain and let it go as
734 * deep as it wants.
735 */
736 for (i = 0, entry = objects; i < nr_objects; i++, entry++)
737 if (!entry->delta && entry->delta_child)
738 entry->delta_limit =
739 check_delta_limit(entry, 1);
740 }
c323ac7d
LT
741}
742
743typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
744
745static entry_sort_t current_sort;
746
747static int sort_comparator(const void *_a, const void *_b)
748{
749 struct object_entry *a = *(struct object_entry **)_a;
750 struct object_entry *b = *(struct object_entry **)_b;
751 return current_sort(a,b);
752}
753
754static struct object_entry **create_sorted_list(entry_sort_t sort)
755{
756 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
757 int i;
758
759 for (i = 0; i < nr_objects; i++)
760 list[i] = objects + i;
761 current_sort = sort;
762 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
763 return list;
764}
765
766static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
767{
768 return memcmp(a->sha1, b->sha1, 20);
769}
770
962554c6 771static struct object_entry **create_final_object_list(void)
7a979d99
JH
772{
773 struct object_entry **list;
774 int i, j;
775
776 for (i = nr_result = 0; i < nr_objects; i++)
777 if (!objects[i].preferred_base)
778 nr_result++;
779 list = xmalloc(nr_result * sizeof(struct object_entry *));
780 for (i = j = 0; i < nr_objects; i++) {
781 if (!objects[i].preferred_base)
782 list[j++] = objects + i;
783 }
784 current_sort = sha1_sort;
785 qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);
786 return list;
787}
788
c323ac7d
LT
789static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
790{
791 if (a->type < b->type)
792 return -1;
793 if (a->type > b->type)
794 return 1;
27225f2e
LT
795 if (a->hash < b->hash)
796 return -1;
797 if (a->hash > b->hash)
798 return 1;
7a979d99
JH
799 if (a->preferred_base < b->preferred_base)
800 return -1;
801 if (a->preferred_base > b->preferred_base)
802 return 1;
c323ac7d
LT
803 if (a->size < b->size)
804 return -1;
805 if (a->size > b->size)
806 return 1;
807 return a < b ? -1 : (a > b);
808}
809
810struct unpacked {
811 struct object_entry *entry;
812 void *data;
813};
814
815/*
521a4f4c
LT
816 * We search for deltas _backwards_ in a list sorted by type and
817 * by size, so that we see progressively smaller and smaller files.
818 * That's because we prefer deltas to be from the bigger file
819 * to the smaller - deletes are potentially cheaper, but perhaps
820 * more importantly, the bigger file is likely the more recent
821 * one.
c323ac7d 822 */
d116a45a 823static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
c323ac7d
LT
824{
825 struct object_entry *cur_entry = cur->entry;
826 struct object_entry *old_entry = old->entry;
7a979d99
JH
827 int old_preferred = (old_entry->preferred_base ||
828 old_entry->based_on_preferred);
521a4f4c 829 unsigned long size, oldsize, delta_size, sizediff;
75c42d8c 830 long max_size;
c323ac7d
LT
831 void *delta_buf;
832
833 /* Don't bother doing diffs between different types */
834 if (cur_entry->type != old_entry->type)
835 return -1;
836
7a979d99
JH
837 /* We do not compute delta to *create* objects we are not
838 * going to pack.
839 */
840 if (cur_entry->preferred_base)
75c42d8c 841 return -1;
7a979d99
JH
842
843 /* If the current object is at pack edge, take the depth the
844 * objects that depend on the current object into account --
845 * otherwise they would become too deep.
ab7cd7bb 846 */
15b4d577
JH
847 if (cur_entry->delta_child) {
848 if (max_depth <= cur_entry->delta_limit)
849 return 0;
850 max_depth -= cur_entry->delta_limit;
851 }
ab7cd7bb 852
c323ac7d
LT
853 size = cur_entry->size;
854 oldsize = old_entry->size;
521a4f4c 855 sizediff = oldsize > size ? oldsize - size : size - oldsize;
f0b0af1b
JH
856
857 if (size < 50)
c323ac7d 858 return -1;
d116a45a
LT
859 if (old_entry->depth >= max_depth)
860 return 0;
c323ac7d
LT
861
862 /*
863 * NOTE!
864 *
865 * We always delta from the bigger to the smaller, since that's
866 * more space-efficient (deletes don't have to say _what_ they
867 * delete).
868 */
75c42d8c 869 max_size = size / 2 - 20;
7a979d99
JH
870 if (cur_entry->delta) {
871 if (cur_entry->based_on_preferred) {
872 if (old_preferred)
873 max_size = cur_entry->delta_size-1;
874 else
875 /* trying with non-preferred one when we
876 * already have a delta based on preferred
877 * one is pointless.
878 */
b925410d 879 return -1;
7a979d99
JH
880 }
881 else if (!old_preferred)
882 max_size = cur_entry->delta_size-1;
883 else
884 /* otherwise... even if delta with a
885 * preferred one produces a bigger result than
886 * what we currently have, which is based on a
887 * non-preferred one, it is OK.
888 */
889 ;
890 }
27225f2e
LT
891 if (sizediff >= max_size)
892 return -1;
8ee378a0
JH
893 delta_buf = diff_delta(old->data, oldsize,
894 cur->data, size, &delta_size, max_size);
c323ac7d 895 if (!delta_buf)
75c42d8c
LT
896 return 0;
897 cur_entry->delta = old_entry;
898 cur_entry->delta_size = delta_size;
d116a45a 899 cur_entry->depth = old_entry->depth + 1;
7a979d99 900 cur_entry->based_on_preferred = old_preferred;
c323ac7d 901 free(delta_buf);
eb41ab11 902 return 0;
c323ac7d
LT
903}
904
b2504a0d
NP
905static void progress_interval(int signum)
906{
907 signal(SIGALRM, progress_interval);
908 progress_update = 1;
909}
910
d116a45a 911static void find_deltas(struct object_entry **list, int window, int depth)
c323ac7d 912{
521a4f4c 913 int i, idx;
c323ac7d
LT
914 unsigned int array_size = window * sizeof(struct unpacked);
915 struct unpacked *array = xmalloc(array_size);
183bdb2c
JH
916 unsigned processed = 0;
917 unsigned last_percent = 999;
c323ac7d
LT
918
919 memset(array, 0, array_size);
521a4f4c
LT
920 i = nr_objects;
921 idx = 0;
183bdb2c 922 if (progress)
f0b0af1b 923 fprintf(stderr, "Deltifying %d objects.\n", nr_result);
21fcd1bd 924
521a4f4c 925 while (--i >= 0) {
c323ac7d
LT
926 struct object_entry *entry = list[i];
927 struct unpacked *n = array + idx;
928 unsigned long size;
929 char type[10];
930 int j;
931
f0b0af1b
JH
932 if (!entry->preferred_base)
933 processed++;
934
183bdb2c 935 if (progress) {
f0b0af1b 936 unsigned percent = processed * 100 / nr_result;
183bdb2c
JH
937 if (percent != last_percent || progress_update) {
938 fprintf(stderr, "%4u%% (%u/%u) done\r",
f0b0af1b 939 percent, processed, nr_result);
183bdb2c
JH
940 progress_update = 0;
941 last_percent = percent;
942 }
21fcd1bd 943 }
3f9ac8d2
JH
944
945 if (entry->delta)
946 /* This happens if we decided to reuse existing
ab7cd7bb 947 * delta from a pack. "!no_reuse_delta &&" is implied.
3f9ac8d2
JH
948 */
949 continue;
950
c323ac7d
LT
951 free(n->data);
952 n->entry = entry;
953 n->data = read_sha1_file(entry->sha1, type, &size);
954 if (size != entry->size)
955 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
ab7cd7bb 956
78817c15
LT
957 j = window;
958 while (--j > 0) {
959 unsigned int other_idx = idx + j;
c323ac7d 960 struct unpacked *m;
78817c15
LT
961 if (other_idx >= window)
962 other_idx -= window;
c323ac7d
LT
963 m = array + other_idx;
964 if (!m->entry)
965 break;
d116a45a 966 if (try_delta(n, m, depth) < 0)
c323ac7d
LT
967 break;
968 }
521a4f4c
LT
969 idx++;
970 if (idx >= window)
971 idx = 0;
c323ac7d 972 }
adee7bdf 973
b2504a0d
NP
974 if (progress)
975 fputc('\n', stderr);
976
adee7bdf
SV
977 for (i = 0; i < window; ++i)
978 free(array[i].data);
979 free(array);
c323ac7d
LT
980}
981
f3123c4a
JH
982static void prepare_pack(int window, int depth)
983{
3f9ac8d2 984 get_object_details();
f3123c4a
JH
985 sorted_by_type = create_sorted_list(type_size_sort);
986 if (window && depth)
987 find_deltas(sorted_by_type, window+1, depth);
f3123c4a
JH
988}
989
990static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
991{
992 static const char cache[] = "pack-cache/pack-%s.%s";
993 char *cached_pack, *cached_idx;
994 int ifd, ofd, ifd_ix = -1;
995
996 cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
997 ifd = open(cached_pack, O_RDONLY);
998 if (ifd < 0)
999 return 0;
1000
1001 if (!pack_to_stdout) {
1002 cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
1003 ifd_ix = open(cached_idx, O_RDONLY);
1004 if (ifd_ix < 0) {
1005 close(ifd);
1006 return 0;
1007 }
1008 }
1009
ab7cd7bb
JH
1010 if (progress)
1011 fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
1012 sha1_to_hex(sha1));
f3123c4a
JH
1013
1014 if (pack_to_stdout) {
1015 if (copy_fd(ifd, 1))
1016 exit(1);
1017 close(ifd);
1018 }
1019 else {
1020 char name[PATH_MAX];
1021 snprintf(name, sizeof(name),
1022 "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
1023 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
1024 if (ofd < 0)
1025 die("unable to open %s (%s)", name, strerror(errno));
1026 if (copy_fd(ifd, ofd))
1027 exit(1);
1028 close(ifd);
1029
1030 snprintf(name, sizeof(name),
1031 "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
1032 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
1033 if (ofd < 0)
1034 die("unable to open %s (%s)", name, strerror(errno));
1035 if (copy_fd(ifd_ix, ofd))
1036 exit(1);
1037 close(ifd_ix);
1038 puts(sha1_to_hex(sha1));
1039 }
1040
1041 return 1;
1042}
1043
c323ac7d
LT
1044int main(int argc, char **argv)
1045{
5f3de58f 1046 SHA_CTX ctx;
27225f2e 1047 char line[PATH_MAX + 20];
d22b9290 1048 int window = 10, depth = 10, pack_to_stdout = 0;
84c8d8ae 1049 struct object_entry **list;
c323ac7d
LT
1050 int i;
1051
53228a5f
JH
1052 setup_git_directory();
1053
c323ac7d
LT
1054 for (i = 1; i < argc; i++) {
1055 const char *arg = argv[i];
1056
1057 if (*arg == '-') {
1c4a2912
LT
1058 if (!strcmp("--non-empty", arg)) {
1059 non_empty = 1;
1060 continue;
1061 }
64560374
LT
1062 if (!strcmp("--local", arg)) {
1063 local = 1;
1064 continue;
1065 }
eb019375
LT
1066 if (!strcmp("--incremental", arg)) {
1067 incremental = 1;
1068 continue;
1069 }
c323ac7d
LT
1070 if (!strncmp("--window=", arg, 9)) {
1071 char *end;
f846bbff 1072 window = strtoul(arg+9, &end, 0);
c323ac7d
LT
1073 if (!arg[9] || *end)
1074 usage(pack_usage);
1075 continue;
1076 }
d116a45a
LT
1077 if (!strncmp("--depth=", arg, 8)) {
1078 char *end;
1079 depth = strtoul(arg+8, &end, 0);
1080 if (!arg[8] || *end)
1081 usage(pack_usage);
1082 continue;
1083 }
024701f1
JH
1084 if (!strcmp("-q", arg)) {
1085 progress = 0;
1086 continue;
1087 }
ab7cd7bb
JH
1088 if (!strcmp("--no-reuse-delta", arg)) {
1089 no_reuse_delta = 1;
1090 continue;
1091 }
d22b9290
LT
1092 if (!strcmp("--stdout", arg)) {
1093 pack_to_stdout = 1;
1094 continue;
1095 }
c323ac7d
LT
1096 usage(pack_usage);
1097 }
1098 if (base_name)
1099 usage(pack_usage);
1100 base_name = arg;
1101 }
1102
d22b9290 1103 if (pack_to_stdout != !base_name)
c323ac7d
LT
1104 usage(pack_usage);
1105
64560374 1106 prepare_packed_git();
b2504a0d 1107
21fcd1bd 1108 if (progress) {
b2504a0d
NP
1109 struct itimerval v;
1110 v.it_interval.tv_sec = 1;
1111 v.it_interval.tv_usec = 0;
1112 v.it_value = v.it_interval;
1113 signal(SIGALRM, progress_interval);
1114 setitimer(ITIMER_REAL, &v, NULL);
21fcd1bd 1115 fprintf(stderr, "Generating pack...\n");
21fcd1bd 1116 }
b2504a0d 1117
c323ac7d
LT
1118 while (fgets(line, sizeof(line), stdin) != NULL) {
1119 unsigned char sha1[20];
27225f2e 1120
7a979d99
JH
1121 if (line[0] == '-') {
1122 if (get_sha1_hex(line+1, sha1))
1123 die("expected edge sha1, got garbage:\n %s",
1124 line+1);
1125 add_preferred_base(sha1);
1126 continue;
21fcd1bd 1127 }
c323ac7d 1128 if (get_sha1_hex(line, sha1))
ef07618f 1129 die("expected sha1, got garbage:\n %s", line);
1d6b38cc 1130 add_object_entry(sha1, name_hash(NULL, line+41), 0);
c323ac7d 1131 }
21fcd1bd
JH
1132 if (progress)
1133 fprintf(stderr, "Done counting %d objects.\n", nr_objects);
f0b0af1b
JH
1134 sorted_by_sha = create_final_object_list();
1135 if (non_empty && !nr_result)
1c4a2912 1136 return 0;
c323ac7d 1137
84c8d8ae
JH
1138 SHA1_Init(&ctx);
1139 list = sorted_by_sha;
7a979d99 1140 for (i = 0; i < nr_result; i++) {
84c8d8ae
JH
1141 struct object_entry *entry = *list++;
1142 SHA1_Update(&ctx, entry->sha1, 20);
1143 }
1144 SHA1_Final(object_list_sha1, &ctx);
7a979d99
JH
1145 if (progress && (nr_objects != nr_result))
1146 fprintf(stderr, "Result has %d objects.\n", nr_result);
84c8d8ae 1147
f3123c4a
JH
1148 if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
1149 ;
1150 else {
f0b0af1b
JH
1151 if (nr_result)
1152 prepare_pack(window, depth);
5e8dc750
NP
1153 if (progress && pack_to_stdout) {
1154 /* the other end usually displays progress itself */
1155 struct itimerval v = {{0,},};
1156 setitimer(ITIMER_REAL, &v, NULL);
1157 signal(SIGALRM, SIG_IGN );
1158 progress_update = 0;
1159 }
1160 write_pack_file();
f3123c4a
JH
1161 if (!pack_to_stdout) {
1162 write_index_file();
1163 puts(sha1_to_hex(object_list_sha1));
1164 }
5f3de58f 1165 }
ab7cd7bb
JH
1166 if (progress)
1167 fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
7a979d99 1168 nr_result, written, written_delta, reused, reused_delta);
c323ac7d
LT
1169 return 0;
1170}