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