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