packv4: nth_packed_object_sha1() packv4 support
[git/packv4.git] / builtin-pack-objects.c
blob4fe81a723bc8f2eaeafeba9bbea038161d2e8ede
1 #include "builtin.h"
2 #include "cache.h"
3 #include "attr.h"
4 #include "object.h"
5 #include "blob.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "tree.h"
9 #include "delta.h"
10 #include "pack.h"
11 #include "csum-file.h"
12 #include "tree-walk.h"
13 #include "diff.h"
14 #include "revision.h"
15 #include "list-objects.h"
16 #include "progress.h"
18 static const char pack_usage[] = "\
19 git-pack-objects [{ -q | --progress | --all-progress }] \n\
20 [--max-pack-size=N] [--local] [--incremental] \n\
21 [--window=N] [--window-memory=N] [--depth=N] \n\
22 [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\
23 [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\
24 [--stdout | base-name] [<ref-list | <object-list]";
26 struct object_entry {
27 struct pack_idx_entry idx;
28 unsigned long size; /* uncompressed size */
29 struct packed_git *in_pack; /* already in pack */
30 off_t in_pack_offset;
31 struct object_entry *delta; /* delta base object */
32 struct object_entry *delta_child; /* deltified objects who bases me */
33 struct object_entry *delta_sibling; /* other deltified objects who
34 * uses the same base as me
36 void *delta_data; /* cached delta (uncompressed) */
37 unsigned long delta_size; /* delta data size (uncompressed) */
38 unsigned int hash; /* name hint hash */
39 enum object_type type;
40 enum object_type in_pack_type; /* could be delta */
41 unsigned char in_pack_header_size;
42 unsigned char preferred_base; /* we do not pack this, but is available
43 * to be used as the base object to delta
44 * objects against.
46 unsigned char no_try_delta;
50 * Objects we are going to pack are collected in objects array (dynamically
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.
55 static struct object_entry *objects;
56 static struct object_entry **written_list;
57 static uint32_t nr_objects, nr_alloc, nr_result, nr_written;
59 static int non_empty;
60 static int no_reuse_delta, no_reuse_object;
61 static int local;
62 static int incremental;
63 static int allow_ofs_delta;
64 static const char *pack_tmp_name, *idx_tmp_name;
65 static char tmpname[PATH_MAX];
66 static const char *base_name;
67 static int progress = 1;
68 static int window = 10;
69 static uint32_t pack_size_limit;
70 static int depth = 50;
71 static int pack_to_stdout;
72 static int num_preferred_base;
73 static struct progress progress_state;
74 static int pack_compression_level = Z_DEFAULT_COMPRESSION;
75 static int pack_compression_seen;
76 static int pack_version = 2;
78 static unsigned long delta_cache_size = 0;
79 static unsigned long max_delta_cache_size = 0;
80 static unsigned long cache_max_small_delta_size = 1000;
82 static unsigned long window_memory_usage = 0;
83 static unsigned long window_memory_limit = 0;
86 * The object names in objects array are hashed with this hashtable,
87 * to help looking up the entry by object name.
88 * This hashtable is built after all the objects are seen.
90 static int *object_ix;
91 static int object_ix_hashsz;
94 * Pack index for existing packs give us easy access to the offsets into
95 * corresponding pack file where each object's data starts, but the entries
96 * do not store the size of the compressed representation (uncompressed
97 * size is easily available by examining the pack entry header). It is
98 * also rather expensive to find the sha1 for an object given its offset.
100 * We build a hashtable of existing packs (pack_revindex), and keep reverse
101 * index here -- pack index file is sorted by object name mapping to offset;
102 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
103 * ordered by offset, so if you know the offset of an object, next offset
104 * is where its packed representation ends and the index_nr can be used to
105 * get the object sha1 from the main index.
107 struct revindex_entry {
108 off_t offset;
109 unsigned int nr;
111 struct pack_revindex {
112 struct packed_git *p;
113 struct revindex_entry *revindex;
115 static struct pack_revindex *pack_revindex;
116 static int pack_revindex_hashsz;
119 * stats
121 static uint32_t written, written_delta;
122 static uint32_t reused, reused_delta;
124 static int pack_revindex_ix(struct packed_git *p)
126 unsigned long ui = (unsigned long)p;
127 int i;
129 ui = ui ^ (ui >> 16); /* defeat structure alignment */
130 i = (int)(ui % pack_revindex_hashsz);
131 while (pack_revindex[i].p) {
132 if (pack_revindex[i].p == p)
133 return i;
134 if (++i == pack_revindex_hashsz)
135 i = 0;
137 return -1 - i;
140 static void prepare_pack_ix(void)
142 int num;
143 struct packed_git *p;
144 for (num = 0, p = packed_git; p; p = p->next)
145 num++;
146 if (!num)
147 return;
148 pack_revindex_hashsz = num * 11;
149 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
150 for (p = packed_git; p; p = p->next) {
151 num = pack_revindex_ix(p);
152 num = - 1 - num;
153 pack_revindex[num].p = p;
155 /* revindex elements are lazily initialized */
158 static int cmp_offset(const void *a_, const void *b_)
160 const struct revindex_entry *a = a_;
161 const struct revindex_entry *b = b_;
162 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
166 * Ordered list of offsets of objects in the pack.
168 static void prepare_pack_revindex(struct pack_revindex *rix)
170 struct packed_git *p = rix->p;
171 int num_ent = p->num_objects;
172 int i;
173 const char *index = p->index_data;
175 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
176 index += 4 * 256;
178 if (p->index_version > 1) {
179 uint32_t *off_32, *off_64;
180 if (p->index_version > 2) {
181 off_32 = (uint32_t *)(index + 8 + p->num_objects * 4);
182 } else {
183 off_32 =
184 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
186 off_64 = off_32 + p->num_objects;
187 for (i = 0; i < num_ent; i++) {
188 uint32_t off = ntohl(*off_32++);
189 if (!(off & 0x80000000)) {
190 rix->revindex[i].offset = off;
191 } else {
192 rix->revindex[i].offset =
193 ((uint64_t)ntohl(*off_64++)) << 32;
194 rix->revindex[i].offset |=
195 ntohl(*off_64++);
197 rix->revindex[i].nr = i;
199 } else {
200 for (i = 0; i < num_ent; i++) {
201 uint32_t hl = *((uint32_t *)(index + 24 * i));
202 rix->revindex[i].offset = ntohl(hl);
203 rix->revindex[i].nr = i;
207 /* This knows the pack format -- the 20-byte trailer
208 * follows immediately after the last object data.
210 rix->revindex[num_ent].offset = p->pack_size - 20;
211 rix->revindex[num_ent].nr = -1;
212 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
215 static struct revindex_entry * find_packed_object(struct packed_git *p,
216 off_t ofs)
218 int num;
219 int lo, hi;
220 struct pack_revindex *rix;
221 struct revindex_entry *revindex;
222 num = pack_revindex_ix(p);
223 if (num < 0)
224 die("internal error: pack revindex uninitialized");
225 rix = &pack_revindex[num];
226 if (!rix->revindex)
227 prepare_pack_revindex(rix);
228 revindex = rix->revindex;
229 lo = 0;
230 hi = p->num_objects + 1;
231 do {
232 int mi = (lo + hi) / 2;
233 if (revindex[mi].offset == ofs) {
234 return revindex + mi;
236 else if (ofs < revindex[mi].offset)
237 hi = mi;
238 else
239 lo = mi + 1;
240 } while (lo < hi);
241 die("internal error: pack revindex corrupt");
244 static const unsigned char *find_packed_object_name(struct packed_git *p,
245 off_t ofs)
247 struct revindex_entry *entry = find_packed_object(p, ofs);
248 return nth_packed_object_sha1(p, entry->nr);
251 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
253 unsigned long othersize, delta_size;
254 enum object_type type;
255 void *otherbuf = read_sha1_file(entry->delta->idx.sha1, &type, &othersize);
256 void *delta_buf;
258 if (!otherbuf)
259 die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1));
260 delta_buf = diff_delta(otherbuf, othersize,
261 buf, size, &delta_size, 0);
262 if (!delta_buf || delta_size != entry->delta_size)
263 die("delta size changed");
264 free(buf);
265 free(otherbuf);
266 return delta_buf;
270 * The per-object header is a pretty dense thing, which is
271 * - first byte: low four bits are "size", then three bits of "type",
272 * and the high bit is "size continues".
273 * - each byte afterwards: low seven bits are size continuation,
274 * with the high bit being "size continues"
276 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
278 int n = 1;
279 unsigned char c;
281 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
282 die("bad type %d", type);
284 c = (type << 4) | (size & 15);
285 size >>= 4;
286 while (size) {
287 *hdr++ = c | 0x80;
288 c = size & 0x7f;
289 size >>= 7;
290 n++;
292 *hdr = c;
293 return n;
297 * we are going to reuse the existing object data as is. make
298 * sure it is not corrupt.
300 static int check_pack_inflate(struct packed_git *p,
301 struct pack_window **w_curs,
302 off_t offset,
303 off_t len,
304 unsigned long expect)
306 z_stream stream;
307 unsigned char fakebuf[4096], *in;
308 int st;
310 memset(&stream, 0, sizeof(stream));
311 inflateInit(&stream);
312 do {
313 in = use_pack(p, w_curs, offset, &stream.avail_in);
314 stream.next_in = in;
315 stream.next_out = fakebuf;
316 stream.avail_out = sizeof(fakebuf);
317 st = inflate(&stream, Z_FINISH);
318 offset += stream.next_in - in;
319 } while (st == Z_OK || st == Z_BUF_ERROR);
320 inflateEnd(&stream);
321 return (st == Z_STREAM_END &&
322 stream.total_out == expect &&
323 stream.total_in == len) ? 0 : -1;
326 static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
327 off_t offset, off_t len, unsigned int nr)
329 const uint32_t *index_crc;
330 uint32_t data_crc = crc32(0, Z_NULL, 0);
331 uint32_t obj_offset = 0;
333 do {
334 unsigned int avail;
335 void *data = use_pack(p, w_curs, offset, &avail);
336 if (avail > len)
337 avail = len;
338 data_crc = crc32(data_crc, data, avail);
339 offset += avail;
340 len -= avail;
341 } while (len);
343 if (p->index_version < 3)
344 obj_offset = p->num_objects * (20/4);
346 index_crc = p->index_data;
347 index_crc += 2 + 256 + obj_offset + nr;
349 return data_crc != ntohl(*index_crc);
352 static void copy_pack_data(struct sha1file *f,
353 struct packed_git *p,
354 struct pack_window **w_curs,
355 off_t offset,
356 off_t len)
358 unsigned char *in;
359 unsigned int avail;
361 while (len) {
362 in = use_pack(p, w_curs, offset, &avail);
363 if (avail > len)
364 avail = (unsigned int)len;
365 sha1write(f, in, avail);
366 offset += avail;
367 len -= avail;
371 static unsigned long write_object(struct sha1file *f,
372 struct object_entry *entry,
373 off_t write_offset)
375 unsigned long size;
376 enum object_type type;
377 void *buf;
378 unsigned char header[10];
379 unsigned char dheader[10];
380 unsigned hdrlen;
381 off_t datalen;
382 enum object_type obj_type;
383 int to_reuse = 0;
384 /* write limit if limited packsize and not first object */
385 unsigned long limit = pack_size_limit && nr_written ?
386 pack_size_limit - write_offset : 0;
387 /* no if no delta */
388 int usable_delta = !entry->delta ? 0 :
389 /* yes if unlimited packfile */
390 !pack_size_limit ? 1 :
391 /* no if base written to previous pack */
392 entry->delta->idx.offset == (off_t)-1 ? 0 :
393 /* otherwise double-check written to this
394 * pack, like we do below
396 entry->delta->idx.offset ? 1 : 0;
398 if (!pack_to_stdout)
399 crc32_begin(f);
401 obj_type = entry->type;
402 if (no_reuse_object)
403 to_reuse = 0; /* explicit */
404 else if (!entry->in_pack)
405 to_reuse = 0; /* can't reuse what we don't have */
406 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
407 /* check_object() decided it for us ... */
408 to_reuse = usable_delta;
409 /* ... but pack split may override that */
410 else if (obj_type != entry->in_pack_type)
411 to_reuse = 0; /* pack has delta which is unusable */
412 else if (entry->delta)
413 to_reuse = 0; /* we want to pack afresh */
414 else
415 to_reuse = 1; /* we have it in-pack undeltified,
416 * and we do not need to deltify it.
419 if (!to_reuse) {
420 z_stream stream;
421 unsigned long maxsize;
422 void *out;
423 if (!usable_delta) {
424 buf = read_sha1_file(entry->idx.sha1, &obj_type, &size);
425 if (!buf)
426 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
427 } else if (entry->delta_data) {
428 size = entry->delta_size;
429 buf = entry->delta_data;
430 entry->delta_data = NULL;
431 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
432 OBJ_OFS_DELTA : OBJ_REF_DELTA;
433 } else {
434 buf = read_sha1_file(entry->idx.sha1, &type, &size);
435 if (!buf)
436 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
437 buf = delta_against(buf, size, entry);
438 size = entry->delta_size;
439 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
440 OBJ_OFS_DELTA : OBJ_REF_DELTA;
442 /* compress the data to store and put compressed length in datalen */
443 memset(&stream, 0, sizeof(stream));
444 deflateInit(&stream, pack_compression_level);
445 maxsize = deflateBound(&stream, size);
446 out = xmalloc(maxsize);
447 /* Compress it */
448 stream.next_in = buf;
449 stream.avail_in = size;
450 stream.next_out = out;
451 stream.avail_out = maxsize;
452 while (deflate(&stream, Z_FINISH) == Z_OK)
453 /* nothing */;
454 deflateEnd(&stream);
455 datalen = stream.total_out;
456 deflateEnd(&stream);
458 * The object header is a byte of 'type' followed by zero or
459 * more bytes of length.
461 hdrlen = encode_header(obj_type, size, header);
463 if (obj_type == OBJ_OFS_DELTA) {
465 * Deltas with relative base contain an additional
466 * encoding of the relative offset for the delta
467 * base from this object's position in the pack.
469 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
470 unsigned pos = sizeof(dheader) - 1;
471 dheader[pos] = ofs & 127;
472 while (ofs >>= 7)
473 dheader[--pos] = 128 | (--ofs & 127);
474 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
475 free(out);
476 free(buf);
477 return 0;
479 sha1write(f, header, hdrlen);
480 sha1write(f, dheader + pos, sizeof(dheader) - pos);
481 hdrlen += sizeof(dheader) - pos;
482 } else if (obj_type == OBJ_REF_DELTA) {
484 * Deltas with a base reference contain
485 * an additional 20 bytes for the base sha1.
487 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
488 free(out);
489 free(buf);
490 return 0;
492 sha1write(f, header, hdrlen);
493 sha1write(f, entry->delta->idx.sha1, 20);
494 hdrlen += 20;
495 } else {
496 if (limit && hdrlen + datalen + 20 >= limit) {
497 free(out);
498 free(buf);
499 return 0;
501 sha1write(f, header, hdrlen);
503 sha1write(f, out, datalen);
504 free(out);
505 free(buf);
507 else {
508 struct packed_git *p = entry->in_pack;
509 struct pack_window *w_curs = NULL;
510 struct revindex_entry *revidx;
511 off_t offset;
513 if (entry->delta) {
514 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
515 OBJ_OFS_DELTA : OBJ_REF_DELTA;
516 reused_delta++;
518 hdrlen = encode_header(obj_type, entry->size, header);
519 offset = entry->in_pack_offset;
520 revidx = find_packed_object(p, offset);
521 datalen = revidx[1].offset - offset;
522 if (!pack_to_stdout && p->index_version > 1 &&
523 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
524 die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
525 offset += entry->in_pack_header_size;
526 datalen -= entry->in_pack_header_size;
527 if (obj_type == OBJ_OFS_DELTA) {
528 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
529 unsigned pos = sizeof(dheader) - 1;
530 dheader[pos] = ofs & 127;
531 while (ofs >>= 7)
532 dheader[--pos] = 128 | (--ofs & 127);
533 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit)
534 return 0;
535 sha1write(f, header, hdrlen);
536 sha1write(f, dheader + pos, sizeof(dheader) - pos);
537 hdrlen += sizeof(dheader) - pos;
538 } else if (obj_type == OBJ_REF_DELTA) {
539 if (limit && hdrlen + 20 + datalen + 20 >= limit)
540 return 0;
541 sha1write(f, header, hdrlen);
542 sha1write(f, entry->delta->idx.sha1, 20);
543 hdrlen += 20;
544 } else {
545 if (limit && hdrlen + datalen + 20 >= limit)
546 return 0;
547 sha1write(f, header, hdrlen);
550 if (!pack_to_stdout && p->index_version == 1 &&
551 check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
552 die("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1));
553 copy_pack_data(f, p, &w_curs, offset, datalen);
554 unuse_pack(&w_curs);
555 reused++;
557 if (usable_delta)
558 written_delta++;
559 written++;
560 if (!pack_to_stdout)
561 entry->idx.crc32 = crc32_end(f);
562 return hdrlen + datalen;
565 static off_t write_one(struct sha1file *f,
566 struct object_entry *e,
567 off_t offset)
569 unsigned long size;
571 /* offset is non zero if object is written already. */
572 if (e->idx.offset || e->preferred_base)
573 return offset;
575 /* if we are deltified, write out base object first. */
576 if (e->delta) {
577 offset = write_one(f, e->delta, offset);
578 if (!offset)
579 return 0;
582 e->idx.offset = offset;
583 size = write_object(f, e, offset);
584 if (!size) {
585 e->idx.offset = 0;
586 return 0;
588 written_list[nr_written++] = e;
590 /* make sure off_t is sufficiently large not to wrap */
591 if (offset > offset + size)
592 die("pack too large for current definition of off_t");
593 return offset + size;
596 static int open_object_dir_tmp(const char *path)
598 snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
599 return xmkstemp(tmpname);
603 * Brute force when in doubt, that's the rule dude!
605 * I've done this ugly (and probably non-sense) hack because
606 * I'm not sure whether I can play with objects order _before_
607 * writing them into the pack file.
609 * So, instead of changing objects order I'm creating a new SHA1
610 * list and sorting it.
612 * Yeah, this is slow and if not needed all this code can be
613 * reduced to one to three lines.
615 struct sha1_list {
616 unsigned char sha1[20];
619 static int sha1_compare(const void *_a, const void *_b)
621 struct sha1_list *a = (struct sha1_list *) _a;
622 struct sha1_list *b = (struct sha1_list *) _b;
623 return hashcmp(a->sha1, b->sha1);
626 static off_t write_sha1_dict(struct sha1file *f)
628 int i;
629 struct sha1_list *sha1_list;
631 sha1_list = xmalloc(sizeof(*sha1_list) * nr_objects);
632 for (i = 0; i < nr_objects; i++)
633 hashcpy(sha1_list[i].sha1, objects[i].idx.sha1);
635 qsort(sha1_list, nr_objects, sizeof(*sha1_list), sha1_compare);
637 for (i = 0; i < nr_objects; i++)
638 sha1write(f, sha1_list[i].sha1, 20);
640 free(sha1_list);
641 return nr_objects * 20;
644 /* forward declaration for write_pack_file */
645 static int adjust_perm(const char *path, mode_t mode);
647 static void write_pack_file(void)
649 uint32_t i = 0, j;
650 struct sha1file *f;
651 off_t offset, offset_one, last_obj_offset = 0;
652 struct pack_header hdr;
653 int do_progress = progress >> pack_to_stdout;
654 uint32_t nr_remaining = nr_result;
656 if (do_progress)
657 start_progress(&progress_state, "Writing %u objects...", "", nr_result);
658 written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
660 do {
661 unsigned char sha1[20];
663 if (pack_to_stdout) {
664 f = sha1fd(1, "<stdout>");
665 } else {
666 int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
667 pack_tmp_name = xstrdup(tmpname);
668 f = sha1fd(fd, pack_tmp_name);
671 hdr.hdr_signature = htonl(PACK_SIGNATURE);
672 hdr.hdr_version = htonl(pack_version);
673 hdr.hdr_entries = htonl(nr_remaining);
674 sha1write(f, &hdr, sizeof(hdr));
675 offset = sizeof(hdr);
677 if (pack_version == 4)
678 offset += write_sha1_dict(f);
680 nr_written = 0;
681 for (; i < nr_objects; i++) {
682 last_obj_offset = offset;
683 offset_one = write_one(f, objects + i, offset);
684 if (!offset_one)
685 break;
686 offset = offset_one;
687 if (do_progress)
688 display_progress(&progress_state, written);
692 * Did we write the wrong # entries in the header?
693 * If so, rewrite it like in fast-import
695 if (pack_to_stdout || nr_written == nr_remaining) {
696 sha1close(f, sha1, 1);
697 } else {
698 sha1close(f, sha1, 0);
699 fixup_pack_header_footer(f->fd, sha1, pack_tmp_name, nr_written);
700 close(f->fd);
703 if (!pack_to_stdout) {
704 mode_t mode = umask(0);
706 umask(mode);
707 mode = 0444 & ~mode;
709 idx_tmp_name = write_idx_file(NULL,
710 (struct pack_idx_entry **) written_list, nr_written, sha1);
711 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
712 base_name, sha1_to_hex(sha1));
713 if (adjust_perm(pack_tmp_name, mode))
714 die("unable to make temporary pack file readable: %s",
715 strerror(errno));
716 if (rename(pack_tmp_name, tmpname))
717 die("unable to rename temporary pack file: %s",
718 strerror(errno));
719 snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
720 base_name, sha1_to_hex(sha1));
721 if (adjust_perm(idx_tmp_name, mode))
722 die("unable to make temporary index file readable: %s",
723 strerror(errno));
724 if (rename(idx_tmp_name, tmpname))
725 die("unable to rename temporary index file: %s",
726 strerror(errno));
727 puts(sha1_to_hex(sha1));
730 /* mark written objects as written to previous pack */
731 for (j = 0; j < nr_written; j++) {
732 written_list[j]->idx.offset = (off_t)-1;
734 nr_remaining -= nr_written;
735 } while (nr_remaining && i < nr_objects);
737 free(written_list);
738 if (do_progress)
739 stop_progress(&progress_state);
740 if (written != nr_result)
741 die("wrote %u objects while expecting %u", written, nr_result);
743 * We have scanned through [0 ... i). Since we have written
744 * the correct number of objects, the remaining [i ... nr_objects)
745 * items must be either already written (due to out-of-order delta base)
746 * or a preferred base. Count those which are neither and complain if any.
748 for (j = 0; i < nr_objects; i++) {
749 struct object_entry *e = objects + i;
750 j += !e->idx.offset && !e->preferred_base;
752 if (j)
753 die("wrote %u objects as expected but %u unwritten", written, j);
756 static int locate_object_entry_hash(const unsigned char *sha1)
758 int i;
759 unsigned int ui;
760 memcpy(&ui, sha1, sizeof(unsigned int));
761 i = ui % object_ix_hashsz;
762 while (0 < object_ix[i]) {
763 if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1))
764 return i;
765 if (++i == object_ix_hashsz)
766 i = 0;
768 return -1 - i;
771 static struct object_entry *locate_object_entry(const unsigned char *sha1)
773 int i;
775 if (!object_ix_hashsz)
776 return NULL;
778 i = locate_object_entry_hash(sha1);
779 if (0 <= i)
780 return &objects[object_ix[i]-1];
781 return NULL;
784 static void rehash_objects(void)
786 uint32_t i;
787 struct object_entry *oe;
789 object_ix_hashsz = nr_objects * 3;
790 if (object_ix_hashsz < 1024)
791 object_ix_hashsz = 1024;
792 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
793 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
794 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
795 int ix = locate_object_entry_hash(oe->idx.sha1);
796 if (0 <= ix)
797 continue;
798 ix = -1 - ix;
799 object_ix[ix] = i + 1;
803 static unsigned name_hash(const char *name)
805 unsigned char c;
806 unsigned hash = 0;
808 if (!name)
809 return 0;
812 * This effectively just creates a sortable number from the
813 * last sixteen non-whitespace characters. Last characters
814 * count "most", so things that end in ".c" sort together.
816 while ((c = *name++) != 0) {
817 if (isspace(c))
818 continue;
819 hash = (hash >> 2) + (c << 24);
821 return hash;
824 static void setup_delta_attr_check(struct git_attr_check *check)
826 static struct git_attr *attr_delta;
828 if (!attr_delta)
829 attr_delta = git_attr("delta", 5);
831 check[0].attr = attr_delta;
834 static int no_try_delta(const char *path)
836 struct git_attr_check check[1];
838 setup_delta_attr_check(check);
839 if (git_checkattr(path, ARRAY_SIZE(check), check))
840 return 0;
841 if (ATTR_FALSE(check->value))
842 return 1;
843 return 0;
846 static int add_object_entry(const unsigned char *sha1, enum object_type type,
847 const char *name, int exclude)
849 struct object_entry *entry;
850 struct packed_git *p, *found_pack = NULL;
851 off_t found_offset = 0;
852 int ix;
853 unsigned hash = name_hash(name);
855 ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
856 if (ix >= 0) {
857 if (exclude) {
858 entry = objects + object_ix[ix] - 1;
859 if (!entry->preferred_base)
860 nr_result--;
861 entry->preferred_base = 1;
863 return 0;
866 for (p = packed_git; p; p = p->next) {
867 off_t offset = find_pack_entry_one(sha1, p);
868 if (offset) {
869 if (!found_pack) {
870 found_offset = offset;
871 found_pack = p;
873 if (exclude)
874 break;
875 if (incremental)
876 return 0;
877 if (local && !p->pack_local)
878 return 0;
882 if (nr_objects >= nr_alloc) {
883 nr_alloc = (nr_alloc + 1024) * 3 / 2;
884 objects = xrealloc(objects, nr_alloc * sizeof(*entry));
887 entry = objects + nr_objects++;
888 memset(entry, 0, sizeof(*entry));
889 hashcpy(entry->idx.sha1, sha1);
890 entry->hash = hash;
891 if (type)
892 entry->type = type;
893 if (exclude)
894 entry->preferred_base = 1;
895 else
896 nr_result++;
897 if (found_pack) {
898 entry->in_pack = found_pack;
899 entry->in_pack_offset = found_offset;
902 if (object_ix_hashsz * 3 <= nr_objects * 4)
903 rehash_objects();
904 else
905 object_ix[-1 - ix] = nr_objects;
907 if (progress)
908 display_progress(&progress_state, nr_objects);
910 if (name && no_try_delta(name))
911 entry->no_try_delta = 1;
913 return 1;
916 struct pbase_tree_cache {
917 unsigned char sha1[20];
918 int ref;
919 int temporary;
920 void *tree_data;
921 unsigned long tree_size;
924 static struct pbase_tree_cache *(pbase_tree_cache[256]);
925 static int pbase_tree_cache_ix(const unsigned char *sha1)
927 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
929 static int pbase_tree_cache_ix_incr(int ix)
931 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
934 static struct pbase_tree {
935 struct pbase_tree *next;
936 /* This is a phony "cache" entry; we are not
937 * going to evict it nor find it through _get()
938 * mechanism -- this is for the toplevel node that
939 * would almost always change with any commit.
941 struct pbase_tree_cache pcache;
942 } *pbase_tree;
944 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
946 struct pbase_tree_cache *ent, *nent;
947 void *data;
948 unsigned long size;
949 enum object_type type;
950 int neigh;
951 int my_ix = pbase_tree_cache_ix(sha1);
952 int available_ix = -1;
954 /* pbase-tree-cache acts as a limited hashtable.
955 * your object will be found at your index or within a few
956 * slots after that slot if it is cached.
958 for (neigh = 0; neigh < 8; neigh++) {
959 ent = pbase_tree_cache[my_ix];
960 if (ent && !hashcmp(ent->sha1, sha1)) {
961 ent->ref++;
962 return ent;
964 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
965 ((0 <= available_ix) &&
966 (!ent && pbase_tree_cache[available_ix])))
967 available_ix = my_ix;
968 if (!ent)
969 break;
970 my_ix = pbase_tree_cache_ix_incr(my_ix);
973 /* Did not find one. Either we got a bogus request or
974 * we need to read and perhaps cache.
976 data = read_sha1_file(sha1, &type, &size);
977 if (!data)
978 return NULL;
979 if (type != OBJ_TREE) {
980 free(data);
981 return NULL;
984 /* We need to either cache or return a throwaway copy */
986 if (available_ix < 0)
987 ent = NULL;
988 else {
989 ent = pbase_tree_cache[available_ix];
990 my_ix = available_ix;
993 if (!ent) {
994 nent = xmalloc(sizeof(*nent));
995 nent->temporary = (available_ix < 0);
997 else {
998 /* evict and reuse */
999 free(ent->tree_data);
1000 nent = ent;
1002 hashcpy(nent->sha1, sha1);
1003 nent->tree_data = data;
1004 nent->tree_size = size;
1005 nent->ref = 1;
1006 if (!nent->temporary)
1007 pbase_tree_cache[my_ix] = nent;
1008 return nent;
1011 static void pbase_tree_put(struct pbase_tree_cache *cache)
1013 if (!cache->temporary) {
1014 cache->ref--;
1015 return;
1017 free(cache->tree_data);
1018 free(cache);
1021 static int name_cmp_len(const char *name)
1023 int i;
1024 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1026 return i;
1029 static void add_pbase_object(struct tree_desc *tree,
1030 const char *name,
1031 int cmplen,
1032 const char *fullname)
1034 struct name_entry entry;
1035 int cmp;
1037 while (tree_entry(tree,&entry)) {
1038 if (S_ISGITLINK(entry.mode))
1039 continue;
1040 cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
1041 memcmp(name, entry.path, cmplen);
1042 if (cmp > 0)
1043 continue;
1044 if (cmp < 0)
1045 return;
1046 if (name[cmplen] != '/') {
1047 add_object_entry(entry.sha1,
1048 S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB,
1049 fullname, 1);
1050 return;
1052 if (S_ISDIR(entry.mode)) {
1053 struct tree_desc sub;
1054 struct pbase_tree_cache *tree;
1055 const char *down = name+cmplen+1;
1056 int downlen = name_cmp_len(down);
1058 tree = pbase_tree_get(entry.sha1);
1059 if (!tree)
1060 return;
1061 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1063 add_pbase_object(&sub, down, downlen, fullname);
1064 pbase_tree_put(tree);
1069 static unsigned *done_pbase_paths;
1070 static int done_pbase_paths_num;
1071 static int done_pbase_paths_alloc;
1072 static int done_pbase_path_pos(unsigned hash)
1074 int lo = 0;
1075 int hi = done_pbase_paths_num;
1076 while (lo < hi) {
1077 int mi = (hi + lo) / 2;
1078 if (done_pbase_paths[mi] == hash)
1079 return mi;
1080 if (done_pbase_paths[mi] < hash)
1081 hi = mi;
1082 else
1083 lo = mi + 1;
1085 return -lo-1;
1088 static int check_pbase_path(unsigned hash)
1090 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
1091 if (0 <= pos)
1092 return 1;
1093 pos = -pos - 1;
1094 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
1095 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
1096 done_pbase_paths = xrealloc(done_pbase_paths,
1097 done_pbase_paths_alloc *
1098 sizeof(unsigned));
1100 done_pbase_paths_num++;
1101 if (pos < done_pbase_paths_num)
1102 memmove(done_pbase_paths + pos + 1,
1103 done_pbase_paths + pos,
1104 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1105 done_pbase_paths[pos] = hash;
1106 return 0;
1109 static void add_preferred_base_object(const char *name)
1111 struct pbase_tree *it;
1112 int cmplen;
1113 unsigned hash = name_hash(name);
1115 if (!num_preferred_base || check_pbase_path(hash))
1116 return;
1118 cmplen = name_cmp_len(name);
1119 for (it = pbase_tree; it; it = it->next) {
1120 if (cmplen == 0) {
1121 add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1);
1123 else {
1124 struct tree_desc tree;
1125 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1126 add_pbase_object(&tree, name, cmplen, name);
1131 static void add_preferred_base(unsigned char *sha1)
1133 struct pbase_tree *it;
1134 void *data;
1135 unsigned long size;
1136 unsigned char tree_sha1[20];
1138 if (window <= num_preferred_base++)
1139 return;
1141 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1142 if (!data)
1143 return;
1145 for (it = pbase_tree; it; it = it->next) {
1146 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
1147 free(data);
1148 return;
1152 it = xcalloc(1, sizeof(*it));
1153 it->next = pbase_tree;
1154 pbase_tree = it;
1156 hashcpy(it->pcache.sha1, tree_sha1);
1157 it->pcache.tree_data = data;
1158 it->pcache.tree_size = size;
1161 static void check_object(struct object_entry *entry)
1163 if (entry->in_pack) {
1164 struct packed_git *p = entry->in_pack;
1165 struct pack_window *w_curs = NULL;
1166 const unsigned char *base_ref = NULL;
1167 struct object_entry *base_entry;
1168 unsigned long used, used_0;
1169 unsigned int avail;
1170 off_t ofs;
1171 unsigned char *buf, c;
1173 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1176 * We want in_pack_type even if we do not reuse delta
1177 * since non-delta representations could still be reused.
1179 used = unpack_object_header_gently(buf, avail,
1180 &entry->in_pack_type,
1181 &entry->size);
1184 * Determine if this is a delta and if so whether we can
1185 * reuse it or not. Otherwise let's find out as cheaply as
1186 * possible what the actual type and size for this object is.
1188 switch (entry->in_pack_type) {
1189 default:
1190 /* Not a delta hence we've already got all we need. */
1191 entry->type = entry->in_pack_type;
1192 entry->in_pack_header_size = used;
1193 unuse_pack(&w_curs);
1194 return;
1195 case OBJ_REF_DELTA:
1196 if (!no_reuse_delta && !entry->preferred_base)
1197 base_ref = use_pack(p, &w_curs,
1198 entry->in_pack_offset + used, NULL);
1199 entry->in_pack_header_size = used + 20;
1200 break;
1201 case OBJ_OFS_DELTA:
1202 buf = use_pack(p, &w_curs,
1203 entry->in_pack_offset + used, NULL);
1204 used_0 = 0;
1205 c = buf[used_0++];
1206 ofs = c & 127;
1207 while (c & 128) {
1208 ofs += 1;
1209 if (!ofs || MSB(ofs, 7))
1210 die("delta base offset overflow in pack for %s",
1211 sha1_to_hex(entry->idx.sha1));
1212 c = buf[used_0++];
1213 ofs = (ofs << 7) + (c & 127);
1215 if (ofs >= entry->in_pack_offset)
1216 die("delta base offset out of bound for %s",
1217 sha1_to_hex(entry->idx.sha1));
1218 ofs = entry->in_pack_offset - ofs;
1219 if (!no_reuse_delta && !entry->preferred_base)
1220 base_ref = find_packed_object_name(p, ofs);
1221 entry->in_pack_header_size = used + used_0;
1222 break;
1225 if (base_ref && (base_entry = locate_object_entry(base_ref))) {
1227 * If base_ref was set above that means we wish to
1228 * reuse delta data, and we even found that base
1229 * in the list of objects we want to pack. Goodie!
1231 * Depth value does not matter - find_deltas() will
1232 * never consider reused delta as the base object to
1233 * deltify other objects against, in order to avoid
1234 * circular deltas.
1236 entry->type = entry->in_pack_type;
1237 entry->delta = base_entry;
1238 entry->delta_sibling = base_entry->delta_child;
1239 base_entry->delta_child = entry;
1240 unuse_pack(&w_curs);
1241 return;
1244 if (entry->type) {
1246 * This must be a delta and we already know what the
1247 * final object type is. Let's extract the actual
1248 * object size from the delta header.
1250 entry->size = get_size_from_delta(p, &w_curs,
1251 entry->in_pack_offset + entry->in_pack_header_size);
1252 unuse_pack(&w_curs);
1253 return;
1257 * No choice but to fall back to the recursive delta walk
1258 * with sha1_object_info() to find about the object type
1259 * at this point...
1261 unuse_pack(&w_curs);
1264 entry->type = sha1_object_info(entry->idx.sha1, &entry->size);
1265 if (entry->type < 0)
1266 die("unable to get type of object %s",
1267 sha1_to_hex(entry->idx.sha1));
1270 static int pack_offset_sort(const void *_a, const void *_b)
1272 const struct object_entry *a = *(struct object_entry **)_a;
1273 const struct object_entry *b = *(struct object_entry **)_b;
1275 /* avoid filesystem trashing with loose objects */
1276 if (!a->in_pack && !b->in_pack)
1277 return hashcmp(a->idx.sha1, b->idx.sha1);
1279 if (a->in_pack < b->in_pack)
1280 return -1;
1281 if (a->in_pack > b->in_pack)
1282 return 1;
1283 return a->in_pack_offset < b->in_pack_offset ? -1 :
1284 (a->in_pack_offset > b->in_pack_offset);
1287 static void get_object_details(void)
1289 uint32_t i;
1290 struct object_entry **sorted_by_offset;
1292 sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));
1293 for (i = 0; i < nr_objects; i++)
1294 sorted_by_offset[i] = objects + i;
1295 qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
1297 prepare_pack_ix();
1298 for (i = 0; i < nr_objects; i++)
1299 check_object(sorted_by_offset[i]);
1300 free(sorted_by_offset);
1303 static int type_size_sort(const void *_a, const void *_b)
1305 const struct object_entry *a = *(struct object_entry **)_a;
1306 const struct object_entry *b = *(struct object_entry **)_b;
1308 if (a->type < b->type)
1309 return -1;
1310 if (a->type > b->type)
1311 return 1;
1312 if (a->hash < b->hash)
1313 return -1;
1314 if (a->hash > b->hash)
1315 return 1;
1316 if (a->preferred_base < b->preferred_base)
1317 return -1;
1318 if (a->preferred_base > b->preferred_base)
1319 return 1;
1320 if (a->size < b->size)
1321 return -1;
1322 if (a->size > b->size)
1323 return 1;
1324 return a > b ? -1 : (a < b); /* newest last */
1327 struct unpacked {
1328 struct object_entry *entry;
1329 void *data;
1330 struct delta_index *index;
1331 unsigned depth;
1334 static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1335 unsigned long delta_size)
1337 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
1338 return 0;
1340 if (delta_size < cache_max_small_delta_size)
1341 return 1;
1343 /* cache delta, if objects are large enough compared to delta size */
1344 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
1345 return 1;
1347 return 0;
1351 * We search for deltas _backwards_ in a list sorted by type and
1352 * by size, so that we see progressively smaller and smaller files.
1353 * That's because we prefer deltas to be from the bigger file
1354 * to the smaller - deletes are potentially cheaper, but perhaps
1355 * more importantly, the bigger file is likely the more recent
1356 * one.
1358 static int try_delta(struct unpacked *trg, struct unpacked *src,
1359 unsigned max_depth)
1361 struct object_entry *trg_entry = trg->entry;
1362 struct object_entry *src_entry = src->entry;
1363 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1364 unsigned ref_depth;
1365 enum object_type type;
1366 void *delta_buf;
1368 /* Don't bother doing diffs between different types */
1369 if (trg_entry->type != src_entry->type)
1370 return -1;
1372 /* We do not compute delta to *create* objects we are not
1373 * going to pack.
1375 if (trg_entry->preferred_base)
1376 return -1;
1379 * We do not bother to try a delta that we discarded
1380 * on an earlier try, but only when reusing delta data.
1382 if (!no_reuse_delta && trg_entry->in_pack &&
1383 trg_entry->in_pack == src_entry->in_pack &&
1384 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1385 trg_entry->in_pack_type != OBJ_OFS_DELTA)
1386 return 0;
1388 /* Let's not bust the allowed depth. */
1389 if (src->depth >= max_depth)
1390 return 0;
1392 /* Now some size filtering heuristics. */
1393 trg_size = trg_entry->size;
1394 if (!trg_entry->delta) {
1395 max_size = trg_size/2 - 20;
1396 ref_depth = 1;
1397 } else {
1398 max_size = trg_entry->delta_size;
1399 ref_depth = trg->depth;
1401 max_size = max_size * (max_depth - src->depth) /
1402 (max_depth - ref_depth + 1);
1403 if (max_size == 0)
1404 return 0;
1405 src_size = src_entry->size;
1406 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1407 if (sizediff >= max_size)
1408 return 0;
1409 if (trg_size < src_size / 32)
1410 return 0;
1412 /* Load data if not already done */
1413 if (!trg->data) {
1414 trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz);
1415 if (!trg->data)
1416 die("object %s cannot be read",
1417 sha1_to_hex(trg_entry->idx.sha1));
1418 if (sz != trg_size)
1419 die("object %s inconsistent object length (%lu vs %lu)",
1420 sha1_to_hex(trg_entry->idx.sha1), sz, trg_size);
1421 window_memory_usage += sz;
1423 if (!src->data) {
1424 src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz);
1425 if (!src->data)
1426 die("object %s cannot be read",
1427 sha1_to_hex(src_entry->idx.sha1));
1428 if (sz != src_size)
1429 die("object %s inconsistent object length (%lu vs %lu)",
1430 sha1_to_hex(src_entry->idx.sha1), sz, src_size);
1431 window_memory_usage += sz;
1433 if (!src->index) {
1434 src->index = create_delta_index(src->data, src_size);
1435 if (!src->index) {
1436 static int warned = 0;
1437 if (!warned++)
1438 warning("suboptimal pack - out of memory");
1439 return 0;
1441 window_memory_usage += sizeof_delta_index(src->index);
1444 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1445 if (!delta_buf)
1446 return 0;
1448 if (trg_entry->delta_data) {
1449 /* Prefer only shallower same-sized deltas. */
1450 if (delta_size == trg_entry->delta_size &&
1451 src->depth + 1 >= trg->depth) {
1452 free(delta_buf);
1453 return 0;
1455 delta_cache_size -= trg_entry->delta_size;
1456 free(trg_entry->delta_data);
1457 trg_entry->delta_data = NULL;
1459 trg_entry->delta = src_entry;
1460 trg_entry->delta_size = delta_size;
1461 trg->depth = src->depth + 1;
1463 if (delta_cacheable(src_size, trg_size, delta_size)) {
1464 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1465 delta_cache_size += trg_entry->delta_size;
1466 } else
1467 free(delta_buf);
1468 return 1;
1471 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1473 struct object_entry *child = me->delta_child;
1474 unsigned int m = n;
1475 while (child) {
1476 unsigned int c = check_delta_limit(child, n + 1);
1477 if (m < c)
1478 m = c;
1479 child = child->delta_sibling;
1481 return m;
1484 static void free_unpacked(struct unpacked *n)
1486 window_memory_usage -= sizeof_delta_index(n->index);
1487 free_delta_index(n->index);
1488 n->index = NULL;
1489 if (n->data) {
1490 free(n->data);
1491 n->data = NULL;
1492 window_memory_usage -= n->entry->size;
1494 n->entry = NULL;
1495 n->depth = 0;
1498 static void find_deltas(struct object_entry **list, int window, int depth)
1500 uint32_t i = nr_objects, idx = 0, count = 0, processed = 0;
1501 unsigned int array_size = window * sizeof(struct unpacked);
1502 struct unpacked *array;
1503 int max_depth;
1505 if (!nr_objects)
1506 return;
1507 array = xmalloc(array_size);
1508 memset(array, 0, array_size);
1509 if (progress)
1510 start_progress(&progress_state, "Deltifying %u objects...", "", nr_result);
1512 do {
1513 struct object_entry *entry = list[--i];
1514 struct unpacked *n = array + idx;
1515 int j;
1517 if (!entry->preferred_base)
1518 processed++;
1520 if (progress)
1521 display_progress(&progress_state, processed);
1523 if (entry->delta)
1524 /* This happens if we decided to reuse existing
1525 * delta from a pack. "!no_reuse_delta &&" is implied.
1527 continue;
1529 if (entry->size < 50)
1530 continue;
1532 if (entry->no_try_delta)
1533 continue;
1535 free_unpacked(n);
1536 n->entry = entry;
1538 while (window_memory_limit &&
1539 window_memory_usage > window_memory_limit &&
1540 count > 1) {
1541 uint32_t tail = (idx + window - count) % window;
1542 free_unpacked(array + tail);
1543 count--;
1547 * If the current object is at pack edge, take the depth the
1548 * objects that depend on the current object into account
1549 * otherwise they would become too deep.
1551 max_depth = depth;
1552 if (entry->delta_child) {
1553 max_depth -= check_delta_limit(entry, 0);
1554 if (max_depth <= 0)
1555 goto next;
1558 j = window;
1559 while (--j > 0) {
1560 uint32_t other_idx = idx + j;
1561 struct unpacked *m;
1562 if (other_idx >= window)
1563 other_idx -= window;
1564 m = array + other_idx;
1565 if (!m->entry)
1566 break;
1567 if (try_delta(n, m, max_depth) < 0)
1568 break;
1571 /* if we made n a delta, and if n is already at max
1572 * depth, leaving it in the window is pointless. we
1573 * should evict it first.
1575 if (entry->delta && depth <= n->depth)
1576 continue;
1578 next:
1579 idx++;
1580 if (count + 1 < window)
1581 count++;
1582 if (idx >= window)
1583 idx = 0;
1584 } while (i > 0);
1586 if (progress)
1587 stop_progress(&progress_state);
1589 for (i = 0; i < window; ++i) {
1590 free_delta_index(array[i].index);
1591 free(array[i].data);
1593 free(array);
1596 static void prepare_pack(int window, int depth)
1598 struct object_entry **delta_list;
1599 uint32_t i;
1601 get_object_details();
1603 if (!window || !depth)
1604 return;
1606 delta_list = xmalloc(nr_objects * sizeof(*delta_list));
1607 for (i = 0; i < nr_objects; i++)
1608 delta_list[i] = objects + i;
1609 qsort(delta_list, nr_objects, sizeof(*delta_list), type_size_sort);
1610 find_deltas(delta_list, window+1, depth);
1611 free(delta_list);
1614 static int git_pack_config(const char *k, const char *v)
1616 if(!strcmp(k, "pack.window")) {
1617 window = git_config_int(k, v);
1618 return 0;
1620 if (!strcmp(k, "pack.windowmemory")) {
1621 window_memory_limit = git_config_ulong(k, v);
1622 return 0;
1624 if (!strcmp(k, "pack.depth")) {
1625 depth = git_config_int(k, v);
1626 return 0;
1628 if (!strcmp(k, "pack.compression")) {
1629 int level = git_config_int(k, v);
1630 if (level == -1)
1631 level = Z_DEFAULT_COMPRESSION;
1632 else if (level < 0 || level > Z_BEST_COMPRESSION)
1633 die("bad pack compression level %d", level);
1634 pack_compression_level = level;
1635 pack_compression_seen = 1;
1636 return 0;
1638 if (!strcmp(k, "pack.deltacachesize")) {
1639 max_delta_cache_size = git_config_int(k, v);
1640 return 0;
1642 if (!strcmp(k, "pack.deltacachelimit")) {
1643 cache_max_small_delta_size = git_config_int(k, v);
1644 return 0;
1646 return git_default_config(k, v);
1649 static void read_object_list_from_stdin(void)
1651 char line[40 + 1 + PATH_MAX + 2];
1652 unsigned char sha1[20];
1654 for (;;) {
1655 if (!fgets(line, sizeof(line), stdin)) {
1656 if (feof(stdin))
1657 break;
1658 if (!ferror(stdin))
1659 die("fgets returned NULL, not EOF, not error!");
1660 if (errno != EINTR)
1661 die("fgets: %s", strerror(errno));
1662 clearerr(stdin);
1663 continue;
1665 if (line[0] == '-') {
1666 if (get_sha1_hex(line+1, sha1))
1667 die("expected edge sha1, got garbage:\n %s",
1668 line);
1669 add_preferred_base(sha1);
1670 continue;
1672 if (get_sha1_hex(line, sha1))
1673 die("expected sha1, got garbage:\n %s", line);
1675 add_preferred_base_object(line+41);
1676 add_object_entry(sha1, 0, line+41, 0);
1680 static void show_commit(struct commit *commit)
1682 add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0);
1685 static void show_object(struct object_array_entry *p)
1687 add_preferred_base_object(p->name);
1688 add_object_entry(p->item->sha1, p->item->type, p->name, 0);
1691 static void show_edge(struct commit *commit)
1693 add_preferred_base(commit->object.sha1);
1696 static void get_object_list(int ac, const char **av)
1698 struct rev_info revs;
1699 char line[1000];
1700 int flags = 0;
1702 init_revisions(&revs, NULL);
1703 save_commit_buffer = 0;
1704 track_object_refs = 0;
1705 setup_revisions(ac, av, &revs, NULL);
1707 while (fgets(line, sizeof(line), stdin) != NULL) {
1708 int len = strlen(line);
1709 if (line[len - 1] == '\n')
1710 line[--len] = 0;
1711 if (!len)
1712 break;
1713 if (*line == '-') {
1714 if (!strcmp(line, "--not")) {
1715 flags ^= UNINTERESTING;
1716 continue;
1718 die("not a rev '%s'", line);
1720 if (handle_revision_arg(line, &revs, flags, 1))
1721 die("bad revision '%s'", line);
1724 prepare_revision_walk(&revs);
1725 mark_edges_uninteresting(revs.commits, &revs, show_edge);
1726 traverse_commit_list(&revs, show_commit, show_object);
1729 static int adjust_perm(const char *path, mode_t mode)
1731 if (chmod(path, mode))
1732 return -1;
1733 return adjust_shared_perm(path);
1736 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1738 int use_internal_rev_list = 0;
1739 int thin = 0;
1740 uint32_t i;
1741 const char **rp_av;
1742 int rp_ac_alloc = 64;
1743 int rp_ac;
1745 rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
1747 rp_av[0] = "pack-objects";
1748 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1749 rp_ac = 2;
1751 git_config(git_pack_config);
1752 if (!pack_compression_seen && core_compression_seen)
1753 pack_compression_level = core_compression_level;
1755 progress = isatty(2);
1756 for (i = 1; i < argc; i++) {
1757 const char *arg = argv[i];
1759 if (*arg != '-')
1760 break;
1762 if (!strcmp("--non-empty", arg)) {
1763 non_empty = 1;
1764 continue;
1766 if (!strcmp("--local", arg)) {
1767 local = 1;
1768 continue;
1770 if (!strcmp("--incremental", arg)) {
1771 incremental = 1;
1772 continue;
1774 if (!prefixcmp(arg, "--compression=")) {
1775 char *end;
1776 int level = strtoul(arg+14, &end, 0);
1777 if (!arg[14] || *end)
1778 usage(pack_usage);
1779 if (level == -1)
1780 level = Z_DEFAULT_COMPRESSION;
1781 else if (level < 0 || level > Z_BEST_COMPRESSION)
1782 die("bad pack compression level %d", level);
1783 pack_compression_level = level;
1784 continue;
1786 if (!prefixcmp(arg, "--max-pack-size=")) {
1787 char *end;
1788 pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024;
1789 if (!arg[16] || *end)
1790 usage(pack_usage);
1791 continue;
1793 if (!prefixcmp(arg, "--window=")) {
1794 char *end;
1795 window = strtoul(arg+9, &end, 0);
1796 if (!arg[9] || *end)
1797 usage(pack_usage);
1798 continue;
1800 if (!prefixcmp(arg, "--window-memory=")) {
1801 if (!git_parse_ulong(arg+16, &window_memory_limit))
1802 usage(pack_usage);
1803 continue;
1805 if (!prefixcmp(arg, "--depth=")) {
1806 char *end;
1807 depth = strtoul(arg+8, &end, 0);
1808 if (!arg[8] || *end)
1809 usage(pack_usage);
1810 continue;
1812 if (!strcmp("--progress", arg)) {
1813 progress = 1;
1814 continue;
1816 if (!strcmp("--all-progress", arg)) {
1817 progress = 2;
1818 continue;
1820 if (!strcmp("-q", arg)) {
1821 progress = 0;
1822 continue;
1824 if (!strcmp("--no-reuse-delta", arg)) {
1825 no_reuse_delta = 1;
1826 continue;
1828 if (!strcmp("--no-reuse-object", arg)) {
1829 no_reuse_object = no_reuse_delta = 1;
1830 continue;
1832 if (!strcmp("--delta-base-offset", arg)) {
1833 allow_ofs_delta = 1;
1834 continue;
1836 if (!strncmp("--pack-version=", arg, 15)) {
1837 pack_version = strtol(arg + 15, NULL, 10);
1838 if (!pack_version_ok(htonl(pack_version)))
1839 die("unsupported pack version %d", pack_version);
1840 // packv4 and later requires index v3 (at least)
1841 if (pack_version > 2)
1842 pack_idx_default_version = 3;
1843 continue;
1845 if (!strcmp("--stdout", arg)) {
1846 pack_to_stdout = 1;
1847 continue;
1849 if (!strcmp("--revs", arg)) {
1850 use_internal_rev_list = 1;
1851 continue;
1853 if (!strcmp("--unpacked", arg) ||
1854 !prefixcmp(arg, "--unpacked=") ||
1855 !strcmp("--reflog", arg) ||
1856 !strcmp("--all", arg)) {
1857 use_internal_rev_list = 1;
1858 if (rp_ac >= rp_ac_alloc - 1) {
1859 rp_ac_alloc = alloc_nr(rp_ac_alloc);
1860 rp_av = xrealloc(rp_av,
1861 rp_ac_alloc * sizeof(*rp_av));
1863 rp_av[rp_ac++] = arg;
1864 continue;
1866 if (!strcmp("--thin", arg)) {
1867 use_internal_rev_list = 1;
1868 thin = 1;
1869 rp_av[1] = "--objects-edge";
1870 continue;
1872 if (!prefixcmp(arg, "--index-version=")) {
1873 char *c;
1874 pack_idx_default_version = strtoul(arg + 16, &c, 10);
1875 if (pack_idx_default_version > 3)
1876 die("bad %s", arg);
1877 if (*c == ',')
1878 pack_idx_off32_limit = strtoul(c+1, &c, 0);
1879 if (*c || pack_idx_off32_limit & 0x80000000)
1880 die("bad %s", arg);
1881 continue;
1883 usage(pack_usage);
1886 /* Traditionally "pack-objects [options] base extra" failed;
1887 * we would however want to take refs parameter that would
1888 * have been given to upstream rev-list ourselves, which means
1889 * we somehow want to say what the base name is. So the
1890 * syntax would be:
1892 * pack-objects [options] base <refs...>
1894 * in other words, we would treat the first non-option as the
1895 * base_name and send everything else to the internal revision
1896 * walker.
1899 if (!pack_to_stdout)
1900 base_name = argv[i++];
1902 if (pack_to_stdout != !base_name)
1903 usage(pack_usage);
1905 if (pack_to_stdout && pack_size_limit)
1906 die("--max-pack-size cannot be used to build a pack for transfer.");
1908 if (!pack_to_stdout && thin)
1909 die("--thin cannot be used to build an indexable pack.");
1911 prepare_packed_git();
1913 if (progress)
1914 start_progress(&progress_state, "Generating pack...",
1915 "Counting objects: ", 0);
1916 if (!use_internal_rev_list)
1917 read_object_list_from_stdin();
1918 else {
1919 rp_av[rp_ac] = NULL;
1920 get_object_list(rp_ac, rp_av);
1922 if (progress) {
1923 stop_progress(&progress_state);
1924 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1927 if (non_empty && !nr_result)
1928 return 0;
1929 if (progress && (nr_objects != nr_result))
1930 fprintf(stderr, "Result has %u objects.\n", nr_result);
1931 if (nr_result)
1932 prepare_pack(window, depth);
1933 write_pack_file();
1934 if (progress)
1935 fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
1936 written, written_delta, reused, reused_delta);
1937 return 0;