nth_packed_object_offset(): index v3 support
[git/packv4.git] / builtin-pack-objects.c
blob26f1e75e90f3e4e485c2a962a9c03fda940ea148
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 const uint32_t *off_32 =
180 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
181 const uint32_t *off_64 = off_32 + p->num_objects;
182 for (i = 0; i < num_ent; i++) {
183 uint32_t off = ntohl(*off_32++);
184 if (!(off & 0x80000000)) {
185 rix->revindex[i].offset = off;
186 } else {
187 rix->revindex[i].offset =
188 ((uint64_t)ntohl(*off_64++)) << 32;
189 rix->revindex[i].offset |=
190 ntohl(*off_64++);
192 rix->revindex[i].nr = i;
194 } else {
195 for (i = 0; i < num_ent; i++) {
196 uint32_t hl = *((uint32_t *)(index + 24 * i));
197 rix->revindex[i].offset = ntohl(hl);
198 rix->revindex[i].nr = i;
202 /* This knows the pack format -- the 20-byte trailer
203 * follows immediately after the last object data.
205 rix->revindex[num_ent].offset = p->pack_size - 20;
206 rix->revindex[num_ent].nr = -1;
207 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
210 static struct revindex_entry * find_packed_object(struct packed_git *p,
211 off_t ofs)
213 int num;
214 int lo, hi;
215 struct pack_revindex *rix;
216 struct revindex_entry *revindex;
217 num = pack_revindex_ix(p);
218 if (num < 0)
219 die("internal error: pack revindex uninitialized");
220 rix = &pack_revindex[num];
221 if (!rix->revindex)
222 prepare_pack_revindex(rix);
223 revindex = rix->revindex;
224 lo = 0;
225 hi = p->num_objects + 1;
226 do {
227 int mi = (lo + hi) / 2;
228 if (revindex[mi].offset == ofs) {
229 return revindex + mi;
231 else if (ofs < revindex[mi].offset)
232 hi = mi;
233 else
234 lo = mi + 1;
235 } while (lo < hi);
236 die("internal error: pack revindex corrupt");
239 static const unsigned char *find_packed_object_name(struct packed_git *p,
240 off_t ofs)
242 struct revindex_entry *entry = find_packed_object(p, ofs);
243 return nth_packed_object_sha1(p, entry->nr);
246 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
248 unsigned long othersize, delta_size;
249 enum object_type type;
250 void *otherbuf = read_sha1_file(entry->delta->idx.sha1, &type, &othersize);
251 void *delta_buf;
253 if (!otherbuf)
254 die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1));
255 delta_buf = diff_delta(otherbuf, othersize,
256 buf, size, &delta_size, 0);
257 if (!delta_buf || delta_size != entry->delta_size)
258 die("delta size changed");
259 free(buf);
260 free(otherbuf);
261 return delta_buf;
265 * The per-object header is a pretty dense thing, which is
266 * - first byte: low four bits are "size", then three bits of "type",
267 * and the high bit is "size continues".
268 * - each byte afterwards: low seven bits are size continuation,
269 * with the high bit being "size continues"
271 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
273 int n = 1;
274 unsigned char c;
276 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
277 die("bad type %d", type);
279 c = (type << 4) | (size & 15);
280 size >>= 4;
281 while (size) {
282 *hdr++ = c | 0x80;
283 c = size & 0x7f;
284 size >>= 7;
285 n++;
287 *hdr = c;
288 return n;
292 * we are going to reuse the existing object data as is. make
293 * sure it is not corrupt.
295 static int check_pack_inflate(struct packed_git *p,
296 struct pack_window **w_curs,
297 off_t offset,
298 off_t len,
299 unsigned long expect)
301 z_stream stream;
302 unsigned char fakebuf[4096], *in;
303 int st;
305 memset(&stream, 0, sizeof(stream));
306 inflateInit(&stream);
307 do {
308 in = use_pack(p, w_curs, offset, &stream.avail_in);
309 stream.next_in = in;
310 stream.next_out = fakebuf;
311 stream.avail_out = sizeof(fakebuf);
312 st = inflate(&stream, Z_FINISH);
313 offset += stream.next_in - in;
314 } while (st == Z_OK || st == Z_BUF_ERROR);
315 inflateEnd(&stream);
316 return (st == Z_STREAM_END &&
317 stream.total_out == expect &&
318 stream.total_in == len) ? 0 : -1;
321 static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
322 off_t offset, off_t len, unsigned int nr)
324 const uint32_t *index_crc;
325 uint32_t data_crc = crc32(0, Z_NULL, 0);
327 do {
328 unsigned int avail;
329 void *data = use_pack(p, w_curs, offset, &avail);
330 if (avail > len)
331 avail = len;
332 data_crc = crc32(data_crc, data, avail);
333 offset += avail;
334 len -= avail;
335 } while (len);
337 index_crc = p->index_data;
338 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
340 return data_crc != ntohl(*index_crc);
343 static void copy_pack_data(struct sha1file *f,
344 struct packed_git *p,
345 struct pack_window **w_curs,
346 off_t offset,
347 off_t len)
349 unsigned char *in;
350 unsigned int avail;
352 while (len) {
353 in = use_pack(p, w_curs, offset, &avail);
354 if (avail > len)
355 avail = (unsigned int)len;
356 sha1write(f, in, avail);
357 offset += avail;
358 len -= avail;
362 static unsigned long write_object(struct sha1file *f,
363 struct object_entry *entry,
364 off_t write_offset)
366 unsigned long size;
367 enum object_type type;
368 void *buf;
369 unsigned char header[10];
370 unsigned char dheader[10];
371 unsigned hdrlen;
372 off_t datalen;
373 enum object_type obj_type;
374 int to_reuse = 0;
375 /* write limit if limited packsize and not first object */
376 unsigned long limit = pack_size_limit && nr_written ?
377 pack_size_limit - write_offset : 0;
378 /* no if no delta */
379 int usable_delta = !entry->delta ? 0 :
380 /* yes if unlimited packfile */
381 !pack_size_limit ? 1 :
382 /* no if base written to previous pack */
383 entry->delta->idx.offset == (off_t)-1 ? 0 :
384 /* otherwise double-check written to this
385 * pack, like we do below
387 entry->delta->idx.offset ? 1 : 0;
389 if (!pack_to_stdout)
390 crc32_begin(f);
392 obj_type = entry->type;
393 if (no_reuse_object)
394 to_reuse = 0; /* explicit */
395 else if (!entry->in_pack)
396 to_reuse = 0; /* can't reuse what we don't have */
397 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
398 /* check_object() decided it for us ... */
399 to_reuse = usable_delta;
400 /* ... but pack split may override that */
401 else if (obj_type != entry->in_pack_type)
402 to_reuse = 0; /* pack has delta which is unusable */
403 else if (entry->delta)
404 to_reuse = 0; /* we want to pack afresh */
405 else
406 to_reuse = 1; /* we have it in-pack undeltified,
407 * and we do not need to deltify it.
410 if (!to_reuse) {
411 z_stream stream;
412 unsigned long maxsize;
413 void *out;
414 if (!usable_delta) {
415 buf = read_sha1_file(entry->idx.sha1, &obj_type, &size);
416 if (!buf)
417 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
418 } else if (entry->delta_data) {
419 size = entry->delta_size;
420 buf = entry->delta_data;
421 entry->delta_data = NULL;
422 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
423 OBJ_OFS_DELTA : OBJ_REF_DELTA;
424 } else {
425 buf = read_sha1_file(entry->idx.sha1, &type, &size);
426 if (!buf)
427 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
428 buf = delta_against(buf, size, entry);
429 size = entry->delta_size;
430 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
431 OBJ_OFS_DELTA : OBJ_REF_DELTA;
433 /* compress the data to store and put compressed length in datalen */
434 memset(&stream, 0, sizeof(stream));
435 deflateInit(&stream, pack_compression_level);
436 maxsize = deflateBound(&stream, size);
437 out = xmalloc(maxsize);
438 /* Compress it */
439 stream.next_in = buf;
440 stream.avail_in = size;
441 stream.next_out = out;
442 stream.avail_out = maxsize;
443 while (deflate(&stream, Z_FINISH) == Z_OK)
444 /* nothing */;
445 deflateEnd(&stream);
446 datalen = stream.total_out;
447 deflateEnd(&stream);
449 * The object header is a byte of 'type' followed by zero or
450 * more bytes of length.
452 hdrlen = encode_header(obj_type, size, header);
454 if (obj_type == OBJ_OFS_DELTA) {
456 * Deltas with relative base contain an additional
457 * encoding of the relative offset for the delta
458 * base from this object's position in the pack.
460 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
461 unsigned pos = sizeof(dheader) - 1;
462 dheader[pos] = ofs & 127;
463 while (ofs >>= 7)
464 dheader[--pos] = 128 | (--ofs & 127);
465 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
466 free(out);
467 free(buf);
468 return 0;
470 sha1write(f, header, hdrlen);
471 sha1write(f, dheader + pos, sizeof(dheader) - pos);
472 hdrlen += sizeof(dheader) - pos;
473 } else if (obj_type == OBJ_REF_DELTA) {
475 * Deltas with a base reference contain
476 * an additional 20 bytes for the base sha1.
478 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
479 free(out);
480 free(buf);
481 return 0;
483 sha1write(f, header, hdrlen);
484 sha1write(f, entry->delta->idx.sha1, 20);
485 hdrlen += 20;
486 } else {
487 if (limit && hdrlen + datalen + 20 >= limit) {
488 free(out);
489 free(buf);
490 return 0;
492 sha1write(f, header, hdrlen);
494 sha1write(f, out, datalen);
495 free(out);
496 free(buf);
498 else {
499 struct packed_git *p = entry->in_pack;
500 struct pack_window *w_curs = NULL;
501 struct revindex_entry *revidx;
502 off_t offset;
504 if (entry->delta) {
505 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
506 OBJ_OFS_DELTA : OBJ_REF_DELTA;
507 reused_delta++;
509 hdrlen = encode_header(obj_type, entry->size, header);
510 offset = entry->in_pack_offset;
511 revidx = find_packed_object(p, offset);
512 datalen = revidx[1].offset - offset;
513 if (!pack_to_stdout && p->index_version > 1 &&
514 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
515 die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
516 offset += entry->in_pack_header_size;
517 datalen -= entry->in_pack_header_size;
518 if (obj_type == OBJ_OFS_DELTA) {
519 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
520 unsigned pos = sizeof(dheader) - 1;
521 dheader[pos] = ofs & 127;
522 while (ofs >>= 7)
523 dheader[--pos] = 128 | (--ofs & 127);
524 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit)
525 return 0;
526 sha1write(f, header, hdrlen);
527 sha1write(f, dheader + pos, sizeof(dheader) - pos);
528 hdrlen += sizeof(dheader) - pos;
529 } else if (obj_type == OBJ_REF_DELTA) {
530 if (limit && hdrlen + 20 + datalen + 20 >= limit)
531 return 0;
532 sha1write(f, header, hdrlen);
533 sha1write(f, entry->delta->idx.sha1, 20);
534 hdrlen += 20;
535 } else {
536 if (limit && hdrlen + datalen + 20 >= limit)
537 return 0;
538 sha1write(f, header, hdrlen);
541 if (!pack_to_stdout && p->index_version == 1 &&
542 check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
543 die("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1));
544 copy_pack_data(f, p, &w_curs, offset, datalen);
545 unuse_pack(&w_curs);
546 reused++;
548 if (usable_delta)
549 written_delta++;
550 written++;
551 if (!pack_to_stdout)
552 entry->idx.crc32 = crc32_end(f);
553 return hdrlen + datalen;
556 static off_t write_one(struct sha1file *f,
557 struct object_entry *e,
558 off_t offset)
560 unsigned long size;
562 /* offset is non zero if object is written already. */
563 if (e->idx.offset || e->preferred_base)
564 return offset;
566 /* if we are deltified, write out base object first. */
567 if (e->delta) {
568 offset = write_one(f, e->delta, offset);
569 if (!offset)
570 return 0;
573 e->idx.offset = offset;
574 size = write_object(f, e, offset);
575 if (!size) {
576 e->idx.offset = 0;
577 return 0;
579 written_list[nr_written++] = e;
581 /* make sure off_t is sufficiently large not to wrap */
582 if (offset > offset + size)
583 die("pack too large for current definition of off_t");
584 return offset + size;
587 static int open_object_dir_tmp(const char *path)
589 snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
590 return xmkstemp(tmpname);
594 * Brute force when in doubt, that's the rule dude!
596 * I've done this ugly (and probably non-sense) hack because
597 * I'm not sure whether I can play with objects order _before_
598 * writing them into the pack file.
600 * So, instead of changing objects order I'm creating a new SHA1
601 * list and sorting it.
603 * Yeah, this is slow and if not needed all this code can be
604 * reduced to one to three lines.
606 struct sha1_list {
607 unsigned char sha1[20];
610 static int sha1_compare(const void *_a, const void *_b)
612 struct sha1_list *a = (struct sha1_list *) _a;
613 struct sha1_list *b = (struct sha1_list *) _b;
614 return hashcmp(a->sha1, b->sha1);
617 static off_t write_sha1_dict(struct sha1file *f)
619 int i;
620 struct sha1_list *sha1_list;
622 sha1_list = xmalloc(sizeof(*sha1_list) * nr_objects);
623 for (i = 0; i < nr_objects; i++)
624 hashcpy(sha1_list[i].sha1, objects[i].idx.sha1);
626 qsort(sha1_list, nr_objects, sizeof(*sha1_list), sha1_compare);
628 for (i = 0; i < nr_objects; i++)
629 sha1write(f, sha1_list[i].sha1, 20);
631 free(sha1_list);
632 return nr_objects * 20;
635 /* forward declaration for write_pack_file */
636 static int adjust_perm(const char *path, mode_t mode);
638 static void write_pack_file(void)
640 uint32_t i = 0, j;
641 struct sha1file *f;
642 off_t offset, offset_one, last_obj_offset = 0;
643 struct pack_header hdr;
644 int do_progress = progress >> pack_to_stdout;
645 uint32_t nr_remaining = nr_result;
647 if (do_progress)
648 start_progress(&progress_state, "Writing %u objects...", "", nr_result);
649 written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
651 do {
652 unsigned char sha1[20];
654 if (pack_to_stdout) {
655 f = sha1fd(1, "<stdout>");
656 } else {
657 int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
658 pack_tmp_name = xstrdup(tmpname);
659 f = sha1fd(fd, pack_tmp_name);
662 hdr.hdr_signature = htonl(PACK_SIGNATURE);
663 hdr.hdr_version = htonl(pack_version);
664 hdr.hdr_entries = htonl(nr_remaining);
665 sha1write(f, &hdr, sizeof(hdr));
666 offset = sizeof(hdr);
668 if (pack_version == 4)
669 offset += write_sha1_dict(f);
671 nr_written = 0;
672 for (; i < nr_objects; i++) {
673 last_obj_offset = offset;
674 offset_one = write_one(f, objects + i, offset);
675 if (!offset_one)
676 break;
677 offset = offset_one;
678 if (do_progress)
679 display_progress(&progress_state, written);
683 * Did we write the wrong # entries in the header?
684 * If so, rewrite it like in fast-import
686 if (pack_to_stdout || nr_written == nr_remaining) {
687 sha1close(f, sha1, 1);
688 } else {
689 sha1close(f, sha1, 0);
690 fixup_pack_header_footer(f->fd, sha1, pack_tmp_name, nr_written);
691 close(f->fd);
694 if (!pack_to_stdout) {
695 mode_t mode = umask(0);
697 umask(mode);
698 mode = 0444 & ~mode;
700 idx_tmp_name = write_idx_file(NULL,
701 (struct pack_idx_entry **) written_list, nr_written, sha1);
702 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
703 base_name, sha1_to_hex(sha1));
704 if (adjust_perm(pack_tmp_name, mode))
705 die("unable to make temporary pack file readable: %s",
706 strerror(errno));
707 if (rename(pack_tmp_name, tmpname))
708 die("unable to rename temporary pack file: %s",
709 strerror(errno));
710 snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
711 base_name, sha1_to_hex(sha1));
712 if (adjust_perm(idx_tmp_name, mode))
713 die("unable to make temporary index file readable: %s",
714 strerror(errno));
715 if (rename(idx_tmp_name, tmpname))
716 die("unable to rename temporary index file: %s",
717 strerror(errno));
718 puts(sha1_to_hex(sha1));
721 /* mark written objects as written to previous pack */
722 for (j = 0; j < nr_written; j++) {
723 written_list[j]->idx.offset = (off_t)-1;
725 nr_remaining -= nr_written;
726 } while (nr_remaining && i < nr_objects);
728 free(written_list);
729 if (do_progress)
730 stop_progress(&progress_state);
731 if (written != nr_result)
732 die("wrote %u objects while expecting %u", written, nr_result);
734 * We have scanned through [0 ... i). Since we have written
735 * the correct number of objects, the remaining [i ... nr_objects)
736 * items must be either already written (due to out-of-order delta base)
737 * or a preferred base. Count those which are neither and complain if any.
739 for (j = 0; i < nr_objects; i++) {
740 struct object_entry *e = objects + i;
741 j += !e->idx.offset && !e->preferred_base;
743 if (j)
744 die("wrote %u objects as expected but %u unwritten", written, j);
747 static int locate_object_entry_hash(const unsigned char *sha1)
749 int i;
750 unsigned int ui;
751 memcpy(&ui, sha1, sizeof(unsigned int));
752 i = ui % object_ix_hashsz;
753 while (0 < object_ix[i]) {
754 if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1))
755 return i;
756 if (++i == object_ix_hashsz)
757 i = 0;
759 return -1 - i;
762 static struct object_entry *locate_object_entry(const unsigned char *sha1)
764 int i;
766 if (!object_ix_hashsz)
767 return NULL;
769 i = locate_object_entry_hash(sha1);
770 if (0 <= i)
771 return &objects[object_ix[i]-1];
772 return NULL;
775 static void rehash_objects(void)
777 uint32_t i;
778 struct object_entry *oe;
780 object_ix_hashsz = nr_objects * 3;
781 if (object_ix_hashsz < 1024)
782 object_ix_hashsz = 1024;
783 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
784 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
785 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
786 int ix = locate_object_entry_hash(oe->idx.sha1);
787 if (0 <= ix)
788 continue;
789 ix = -1 - ix;
790 object_ix[ix] = i + 1;
794 static unsigned name_hash(const char *name)
796 unsigned char c;
797 unsigned hash = 0;
799 if (!name)
800 return 0;
803 * This effectively just creates a sortable number from the
804 * last sixteen non-whitespace characters. Last characters
805 * count "most", so things that end in ".c" sort together.
807 while ((c = *name++) != 0) {
808 if (isspace(c))
809 continue;
810 hash = (hash >> 2) + (c << 24);
812 return hash;
815 static void setup_delta_attr_check(struct git_attr_check *check)
817 static struct git_attr *attr_delta;
819 if (!attr_delta)
820 attr_delta = git_attr("delta", 5);
822 check[0].attr = attr_delta;
825 static int no_try_delta(const char *path)
827 struct git_attr_check check[1];
829 setup_delta_attr_check(check);
830 if (git_checkattr(path, ARRAY_SIZE(check), check))
831 return 0;
832 if (ATTR_FALSE(check->value))
833 return 1;
834 return 0;
837 static int add_object_entry(const unsigned char *sha1, enum object_type type,
838 const char *name, int exclude)
840 struct object_entry *entry;
841 struct packed_git *p, *found_pack = NULL;
842 off_t found_offset = 0;
843 int ix;
844 unsigned hash = name_hash(name);
846 ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
847 if (ix >= 0) {
848 if (exclude) {
849 entry = objects + object_ix[ix] - 1;
850 if (!entry->preferred_base)
851 nr_result--;
852 entry->preferred_base = 1;
854 return 0;
857 for (p = packed_git; p; p = p->next) {
858 off_t offset = find_pack_entry_one(sha1, p);
859 if (offset) {
860 if (!found_pack) {
861 found_offset = offset;
862 found_pack = p;
864 if (exclude)
865 break;
866 if (incremental)
867 return 0;
868 if (local && !p->pack_local)
869 return 0;
873 if (nr_objects >= nr_alloc) {
874 nr_alloc = (nr_alloc + 1024) * 3 / 2;
875 objects = xrealloc(objects, nr_alloc * sizeof(*entry));
878 entry = objects + nr_objects++;
879 memset(entry, 0, sizeof(*entry));
880 hashcpy(entry->idx.sha1, sha1);
881 entry->hash = hash;
882 if (type)
883 entry->type = type;
884 if (exclude)
885 entry->preferred_base = 1;
886 else
887 nr_result++;
888 if (found_pack) {
889 entry->in_pack = found_pack;
890 entry->in_pack_offset = found_offset;
893 if (object_ix_hashsz * 3 <= nr_objects * 4)
894 rehash_objects();
895 else
896 object_ix[-1 - ix] = nr_objects;
898 if (progress)
899 display_progress(&progress_state, nr_objects);
901 if (name && no_try_delta(name))
902 entry->no_try_delta = 1;
904 return 1;
907 struct pbase_tree_cache {
908 unsigned char sha1[20];
909 int ref;
910 int temporary;
911 void *tree_data;
912 unsigned long tree_size;
915 static struct pbase_tree_cache *(pbase_tree_cache[256]);
916 static int pbase_tree_cache_ix(const unsigned char *sha1)
918 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
920 static int pbase_tree_cache_ix_incr(int ix)
922 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
925 static struct pbase_tree {
926 struct pbase_tree *next;
927 /* This is a phony "cache" entry; we are not
928 * going to evict it nor find it through _get()
929 * mechanism -- this is for the toplevel node that
930 * would almost always change with any commit.
932 struct pbase_tree_cache pcache;
933 } *pbase_tree;
935 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
937 struct pbase_tree_cache *ent, *nent;
938 void *data;
939 unsigned long size;
940 enum object_type type;
941 int neigh;
942 int my_ix = pbase_tree_cache_ix(sha1);
943 int available_ix = -1;
945 /* pbase-tree-cache acts as a limited hashtable.
946 * your object will be found at your index or within a few
947 * slots after that slot if it is cached.
949 for (neigh = 0; neigh < 8; neigh++) {
950 ent = pbase_tree_cache[my_ix];
951 if (ent && !hashcmp(ent->sha1, sha1)) {
952 ent->ref++;
953 return ent;
955 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
956 ((0 <= available_ix) &&
957 (!ent && pbase_tree_cache[available_ix])))
958 available_ix = my_ix;
959 if (!ent)
960 break;
961 my_ix = pbase_tree_cache_ix_incr(my_ix);
964 /* Did not find one. Either we got a bogus request or
965 * we need to read and perhaps cache.
967 data = read_sha1_file(sha1, &type, &size);
968 if (!data)
969 return NULL;
970 if (type != OBJ_TREE) {
971 free(data);
972 return NULL;
975 /* We need to either cache or return a throwaway copy */
977 if (available_ix < 0)
978 ent = NULL;
979 else {
980 ent = pbase_tree_cache[available_ix];
981 my_ix = available_ix;
984 if (!ent) {
985 nent = xmalloc(sizeof(*nent));
986 nent->temporary = (available_ix < 0);
988 else {
989 /* evict and reuse */
990 free(ent->tree_data);
991 nent = ent;
993 hashcpy(nent->sha1, sha1);
994 nent->tree_data = data;
995 nent->tree_size = size;
996 nent->ref = 1;
997 if (!nent->temporary)
998 pbase_tree_cache[my_ix] = nent;
999 return nent;
1002 static void pbase_tree_put(struct pbase_tree_cache *cache)
1004 if (!cache->temporary) {
1005 cache->ref--;
1006 return;
1008 free(cache->tree_data);
1009 free(cache);
1012 static int name_cmp_len(const char *name)
1014 int i;
1015 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1017 return i;
1020 static void add_pbase_object(struct tree_desc *tree,
1021 const char *name,
1022 int cmplen,
1023 const char *fullname)
1025 struct name_entry entry;
1026 int cmp;
1028 while (tree_entry(tree,&entry)) {
1029 if (S_ISGITLINK(entry.mode))
1030 continue;
1031 cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
1032 memcmp(name, entry.path, cmplen);
1033 if (cmp > 0)
1034 continue;
1035 if (cmp < 0)
1036 return;
1037 if (name[cmplen] != '/') {
1038 add_object_entry(entry.sha1,
1039 S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB,
1040 fullname, 1);
1041 return;
1043 if (S_ISDIR(entry.mode)) {
1044 struct tree_desc sub;
1045 struct pbase_tree_cache *tree;
1046 const char *down = name+cmplen+1;
1047 int downlen = name_cmp_len(down);
1049 tree = pbase_tree_get(entry.sha1);
1050 if (!tree)
1051 return;
1052 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1054 add_pbase_object(&sub, down, downlen, fullname);
1055 pbase_tree_put(tree);
1060 static unsigned *done_pbase_paths;
1061 static int done_pbase_paths_num;
1062 static int done_pbase_paths_alloc;
1063 static int done_pbase_path_pos(unsigned hash)
1065 int lo = 0;
1066 int hi = done_pbase_paths_num;
1067 while (lo < hi) {
1068 int mi = (hi + lo) / 2;
1069 if (done_pbase_paths[mi] == hash)
1070 return mi;
1071 if (done_pbase_paths[mi] < hash)
1072 hi = mi;
1073 else
1074 lo = mi + 1;
1076 return -lo-1;
1079 static int check_pbase_path(unsigned hash)
1081 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
1082 if (0 <= pos)
1083 return 1;
1084 pos = -pos - 1;
1085 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
1086 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
1087 done_pbase_paths = xrealloc(done_pbase_paths,
1088 done_pbase_paths_alloc *
1089 sizeof(unsigned));
1091 done_pbase_paths_num++;
1092 if (pos < done_pbase_paths_num)
1093 memmove(done_pbase_paths + pos + 1,
1094 done_pbase_paths + pos,
1095 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1096 done_pbase_paths[pos] = hash;
1097 return 0;
1100 static void add_preferred_base_object(const char *name)
1102 struct pbase_tree *it;
1103 int cmplen;
1104 unsigned hash = name_hash(name);
1106 if (!num_preferred_base || check_pbase_path(hash))
1107 return;
1109 cmplen = name_cmp_len(name);
1110 for (it = pbase_tree; it; it = it->next) {
1111 if (cmplen == 0) {
1112 add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1);
1114 else {
1115 struct tree_desc tree;
1116 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1117 add_pbase_object(&tree, name, cmplen, name);
1122 static void add_preferred_base(unsigned char *sha1)
1124 struct pbase_tree *it;
1125 void *data;
1126 unsigned long size;
1127 unsigned char tree_sha1[20];
1129 if (window <= num_preferred_base++)
1130 return;
1132 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1133 if (!data)
1134 return;
1136 for (it = pbase_tree; it; it = it->next) {
1137 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
1138 free(data);
1139 return;
1143 it = xcalloc(1, sizeof(*it));
1144 it->next = pbase_tree;
1145 pbase_tree = it;
1147 hashcpy(it->pcache.sha1, tree_sha1);
1148 it->pcache.tree_data = data;
1149 it->pcache.tree_size = size;
1152 static void check_object(struct object_entry *entry)
1154 if (entry->in_pack) {
1155 struct packed_git *p = entry->in_pack;
1156 struct pack_window *w_curs = NULL;
1157 const unsigned char *base_ref = NULL;
1158 struct object_entry *base_entry;
1159 unsigned long used, used_0;
1160 unsigned int avail;
1161 off_t ofs;
1162 unsigned char *buf, c;
1164 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1167 * We want in_pack_type even if we do not reuse delta
1168 * since non-delta representations could still be reused.
1170 used = unpack_object_header_gently(buf, avail,
1171 &entry->in_pack_type,
1172 &entry->size);
1175 * Determine if this is a delta and if so whether we can
1176 * reuse it or not. Otherwise let's find out as cheaply as
1177 * possible what the actual type and size for this object is.
1179 switch (entry->in_pack_type) {
1180 default:
1181 /* Not a delta hence we've already got all we need. */
1182 entry->type = entry->in_pack_type;
1183 entry->in_pack_header_size = used;
1184 unuse_pack(&w_curs);
1185 return;
1186 case OBJ_REF_DELTA:
1187 if (!no_reuse_delta && !entry->preferred_base)
1188 base_ref = use_pack(p, &w_curs,
1189 entry->in_pack_offset + used, NULL);
1190 entry->in_pack_header_size = used + 20;
1191 break;
1192 case OBJ_OFS_DELTA:
1193 buf = use_pack(p, &w_curs,
1194 entry->in_pack_offset + used, NULL);
1195 used_0 = 0;
1196 c = buf[used_0++];
1197 ofs = c & 127;
1198 while (c & 128) {
1199 ofs += 1;
1200 if (!ofs || MSB(ofs, 7))
1201 die("delta base offset overflow in pack for %s",
1202 sha1_to_hex(entry->idx.sha1));
1203 c = buf[used_0++];
1204 ofs = (ofs << 7) + (c & 127);
1206 if (ofs >= entry->in_pack_offset)
1207 die("delta base offset out of bound for %s",
1208 sha1_to_hex(entry->idx.sha1));
1209 ofs = entry->in_pack_offset - ofs;
1210 if (!no_reuse_delta && !entry->preferred_base)
1211 base_ref = find_packed_object_name(p, ofs);
1212 entry->in_pack_header_size = used + used_0;
1213 break;
1216 if (base_ref && (base_entry = locate_object_entry(base_ref))) {
1218 * If base_ref was set above that means we wish to
1219 * reuse delta data, and we even found that base
1220 * in the list of objects we want to pack. Goodie!
1222 * Depth value does not matter - find_deltas() will
1223 * never consider reused delta as the base object to
1224 * deltify other objects against, in order to avoid
1225 * circular deltas.
1227 entry->type = entry->in_pack_type;
1228 entry->delta = base_entry;
1229 entry->delta_sibling = base_entry->delta_child;
1230 base_entry->delta_child = entry;
1231 unuse_pack(&w_curs);
1232 return;
1235 if (entry->type) {
1237 * This must be a delta and we already know what the
1238 * final object type is. Let's extract the actual
1239 * object size from the delta header.
1241 entry->size = get_size_from_delta(p, &w_curs,
1242 entry->in_pack_offset + entry->in_pack_header_size);
1243 unuse_pack(&w_curs);
1244 return;
1248 * No choice but to fall back to the recursive delta walk
1249 * with sha1_object_info() to find about the object type
1250 * at this point...
1252 unuse_pack(&w_curs);
1255 entry->type = sha1_object_info(entry->idx.sha1, &entry->size);
1256 if (entry->type < 0)
1257 die("unable to get type of object %s",
1258 sha1_to_hex(entry->idx.sha1));
1261 static int pack_offset_sort(const void *_a, const void *_b)
1263 const struct object_entry *a = *(struct object_entry **)_a;
1264 const struct object_entry *b = *(struct object_entry **)_b;
1266 /* avoid filesystem trashing with loose objects */
1267 if (!a->in_pack && !b->in_pack)
1268 return hashcmp(a->idx.sha1, b->idx.sha1);
1270 if (a->in_pack < b->in_pack)
1271 return -1;
1272 if (a->in_pack > b->in_pack)
1273 return 1;
1274 return a->in_pack_offset < b->in_pack_offset ? -1 :
1275 (a->in_pack_offset > b->in_pack_offset);
1278 static void get_object_details(void)
1280 uint32_t i;
1281 struct object_entry **sorted_by_offset;
1283 sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));
1284 for (i = 0; i < nr_objects; i++)
1285 sorted_by_offset[i] = objects + i;
1286 qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
1288 prepare_pack_ix();
1289 for (i = 0; i < nr_objects; i++)
1290 check_object(sorted_by_offset[i]);
1291 free(sorted_by_offset);
1294 static int type_size_sort(const void *_a, const void *_b)
1296 const struct object_entry *a = *(struct object_entry **)_a;
1297 const struct object_entry *b = *(struct object_entry **)_b;
1299 if (a->type < b->type)
1300 return -1;
1301 if (a->type > b->type)
1302 return 1;
1303 if (a->hash < b->hash)
1304 return -1;
1305 if (a->hash > b->hash)
1306 return 1;
1307 if (a->preferred_base < b->preferred_base)
1308 return -1;
1309 if (a->preferred_base > b->preferred_base)
1310 return 1;
1311 if (a->size < b->size)
1312 return -1;
1313 if (a->size > b->size)
1314 return 1;
1315 return a > b ? -1 : (a < b); /* newest last */
1318 struct unpacked {
1319 struct object_entry *entry;
1320 void *data;
1321 struct delta_index *index;
1322 unsigned depth;
1325 static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1326 unsigned long delta_size)
1328 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
1329 return 0;
1331 if (delta_size < cache_max_small_delta_size)
1332 return 1;
1334 /* cache delta, if objects are large enough compared to delta size */
1335 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
1336 return 1;
1338 return 0;
1342 * We search for deltas _backwards_ in a list sorted by type and
1343 * by size, so that we see progressively smaller and smaller files.
1344 * That's because we prefer deltas to be from the bigger file
1345 * to the smaller - deletes are potentially cheaper, but perhaps
1346 * more importantly, the bigger file is likely the more recent
1347 * one.
1349 static int try_delta(struct unpacked *trg, struct unpacked *src,
1350 unsigned max_depth)
1352 struct object_entry *trg_entry = trg->entry;
1353 struct object_entry *src_entry = src->entry;
1354 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1355 unsigned ref_depth;
1356 enum object_type type;
1357 void *delta_buf;
1359 /* Don't bother doing diffs between different types */
1360 if (trg_entry->type != src_entry->type)
1361 return -1;
1363 /* We do not compute delta to *create* objects we are not
1364 * going to pack.
1366 if (trg_entry->preferred_base)
1367 return -1;
1370 * We do not bother to try a delta that we discarded
1371 * on an earlier try, but only when reusing delta data.
1373 if (!no_reuse_delta && trg_entry->in_pack &&
1374 trg_entry->in_pack == src_entry->in_pack &&
1375 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1376 trg_entry->in_pack_type != OBJ_OFS_DELTA)
1377 return 0;
1379 /* Let's not bust the allowed depth. */
1380 if (src->depth >= max_depth)
1381 return 0;
1383 /* Now some size filtering heuristics. */
1384 trg_size = trg_entry->size;
1385 if (!trg_entry->delta) {
1386 max_size = trg_size/2 - 20;
1387 ref_depth = 1;
1388 } else {
1389 max_size = trg_entry->delta_size;
1390 ref_depth = trg->depth;
1392 max_size = max_size * (max_depth - src->depth) /
1393 (max_depth - ref_depth + 1);
1394 if (max_size == 0)
1395 return 0;
1396 src_size = src_entry->size;
1397 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1398 if (sizediff >= max_size)
1399 return 0;
1400 if (trg_size < src_size / 32)
1401 return 0;
1403 /* Load data if not already done */
1404 if (!trg->data) {
1405 trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz);
1406 if (!trg->data)
1407 die("object %s cannot be read",
1408 sha1_to_hex(trg_entry->idx.sha1));
1409 if (sz != trg_size)
1410 die("object %s inconsistent object length (%lu vs %lu)",
1411 sha1_to_hex(trg_entry->idx.sha1), sz, trg_size);
1412 window_memory_usage += sz;
1414 if (!src->data) {
1415 src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz);
1416 if (!src->data)
1417 die("object %s cannot be read",
1418 sha1_to_hex(src_entry->idx.sha1));
1419 if (sz != src_size)
1420 die("object %s inconsistent object length (%lu vs %lu)",
1421 sha1_to_hex(src_entry->idx.sha1), sz, src_size);
1422 window_memory_usage += sz;
1424 if (!src->index) {
1425 src->index = create_delta_index(src->data, src_size);
1426 if (!src->index) {
1427 static int warned = 0;
1428 if (!warned++)
1429 warning("suboptimal pack - out of memory");
1430 return 0;
1432 window_memory_usage += sizeof_delta_index(src->index);
1435 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1436 if (!delta_buf)
1437 return 0;
1439 if (trg_entry->delta_data) {
1440 /* Prefer only shallower same-sized deltas. */
1441 if (delta_size == trg_entry->delta_size &&
1442 src->depth + 1 >= trg->depth) {
1443 free(delta_buf);
1444 return 0;
1446 delta_cache_size -= trg_entry->delta_size;
1447 free(trg_entry->delta_data);
1448 trg_entry->delta_data = NULL;
1450 trg_entry->delta = src_entry;
1451 trg_entry->delta_size = delta_size;
1452 trg->depth = src->depth + 1;
1454 if (delta_cacheable(src_size, trg_size, delta_size)) {
1455 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1456 delta_cache_size += trg_entry->delta_size;
1457 } else
1458 free(delta_buf);
1459 return 1;
1462 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1464 struct object_entry *child = me->delta_child;
1465 unsigned int m = n;
1466 while (child) {
1467 unsigned int c = check_delta_limit(child, n + 1);
1468 if (m < c)
1469 m = c;
1470 child = child->delta_sibling;
1472 return m;
1475 static void free_unpacked(struct unpacked *n)
1477 window_memory_usage -= sizeof_delta_index(n->index);
1478 free_delta_index(n->index);
1479 n->index = NULL;
1480 if (n->data) {
1481 free(n->data);
1482 n->data = NULL;
1483 window_memory_usage -= n->entry->size;
1485 n->entry = NULL;
1486 n->depth = 0;
1489 static void find_deltas(struct object_entry **list, int window, int depth)
1491 uint32_t i = nr_objects, idx = 0, count = 0, processed = 0;
1492 unsigned int array_size = window * sizeof(struct unpacked);
1493 struct unpacked *array;
1494 int max_depth;
1496 if (!nr_objects)
1497 return;
1498 array = xmalloc(array_size);
1499 memset(array, 0, array_size);
1500 if (progress)
1501 start_progress(&progress_state, "Deltifying %u objects...", "", nr_result);
1503 do {
1504 struct object_entry *entry = list[--i];
1505 struct unpacked *n = array + idx;
1506 int j;
1508 if (!entry->preferred_base)
1509 processed++;
1511 if (progress)
1512 display_progress(&progress_state, processed);
1514 if (entry->delta)
1515 /* This happens if we decided to reuse existing
1516 * delta from a pack. "!no_reuse_delta &&" is implied.
1518 continue;
1520 if (entry->size < 50)
1521 continue;
1523 if (entry->no_try_delta)
1524 continue;
1526 free_unpacked(n);
1527 n->entry = entry;
1529 while (window_memory_limit &&
1530 window_memory_usage > window_memory_limit &&
1531 count > 1) {
1532 uint32_t tail = (idx + window - count) % window;
1533 free_unpacked(array + tail);
1534 count--;
1538 * If the current object is at pack edge, take the depth the
1539 * objects that depend on the current object into account
1540 * otherwise they would become too deep.
1542 max_depth = depth;
1543 if (entry->delta_child) {
1544 max_depth -= check_delta_limit(entry, 0);
1545 if (max_depth <= 0)
1546 goto next;
1549 j = window;
1550 while (--j > 0) {
1551 uint32_t other_idx = idx + j;
1552 struct unpacked *m;
1553 if (other_idx >= window)
1554 other_idx -= window;
1555 m = array + other_idx;
1556 if (!m->entry)
1557 break;
1558 if (try_delta(n, m, max_depth) < 0)
1559 break;
1562 /* if we made n a delta, and if n is already at max
1563 * depth, leaving it in the window is pointless. we
1564 * should evict it first.
1566 if (entry->delta && depth <= n->depth)
1567 continue;
1569 next:
1570 idx++;
1571 if (count + 1 < window)
1572 count++;
1573 if (idx >= window)
1574 idx = 0;
1575 } while (i > 0);
1577 if (progress)
1578 stop_progress(&progress_state);
1580 for (i = 0; i < window; ++i) {
1581 free_delta_index(array[i].index);
1582 free(array[i].data);
1584 free(array);
1587 static void prepare_pack(int window, int depth)
1589 struct object_entry **delta_list;
1590 uint32_t i;
1592 get_object_details();
1594 if (!window || !depth)
1595 return;
1597 delta_list = xmalloc(nr_objects * sizeof(*delta_list));
1598 for (i = 0; i < nr_objects; i++)
1599 delta_list[i] = objects + i;
1600 qsort(delta_list, nr_objects, sizeof(*delta_list), type_size_sort);
1601 find_deltas(delta_list, window+1, depth);
1602 free(delta_list);
1605 static int git_pack_config(const char *k, const char *v)
1607 if(!strcmp(k, "pack.window")) {
1608 window = git_config_int(k, v);
1609 return 0;
1611 if (!strcmp(k, "pack.windowmemory")) {
1612 window_memory_limit = git_config_ulong(k, v);
1613 return 0;
1615 if (!strcmp(k, "pack.depth")) {
1616 depth = git_config_int(k, v);
1617 return 0;
1619 if (!strcmp(k, "pack.compression")) {
1620 int level = git_config_int(k, v);
1621 if (level == -1)
1622 level = Z_DEFAULT_COMPRESSION;
1623 else if (level < 0 || level > Z_BEST_COMPRESSION)
1624 die("bad pack compression level %d", level);
1625 pack_compression_level = level;
1626 pack_compression_seen = 1;
1627 return 0;
1629 if (!strcmp(k, "pack.deltacachesize")) {
1630 max_delta_cache_size = git_config_int(k, v);
1631 return 0;
1633 if (!strcmp(k, "pack.deltacachelimit")) {
1634 cache_max_small_delta_size = git_config_int(k, v);
1635 return 0;
1637 return git_default_config(k, v);
1640 static void read_object_list_from_stdin(void)
1642 char line[40 + 1 + PATH_MAX + 2];
1643 unsigned char sha1[20];
1645 for (;;) {
1646 if (!fgets(line, sizeof(line), stdin)) {
1647 if (feof(stdin))
1648 break;
1649 if (!ferror(stdin))
1650 die("fgets returned NULL, not EOF, not error!");
1651 if (errno != EINTR)
1652 die("fgets: %s", strerror(errno));
1653 clearerr(stdin);
1654 continue;
1656 if (line[0] == '-') {
1657 if (get_sha1_hex(line+1, sha1))
1658 die("expected edge sha1, got garbage:\n %s",
1659 line);
1660 add_preferred_base(sha1);
1661 continue;
1663 if (get_sha1_hex(line, sha1))
1664 die("expected sha1, got garbage:\n %s", line);
1666 add_preferred_base_object(line+41);
1667 add_object_entry(sha1, 0, line+41, 0);
1671 static void show_commit(struct commit *commit)
1673 add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0);
1676 static void show_object(struct object_array_entry *p)
1678 add_preferred_base_object(p->name);
1679 add_object_entry(p->item->sha1, p->item->type, p->name, 0);
1682 static void show_edge(struct commit *commit)
1684 add_preferred_base(commit->object.sha1);
1687 static void get_object_list(int ac, const char **av)
1689 struct rev_info revs;
1690 char line[1000];
1691 int flags = 0;
1693 init_revisions(&revs, NULL);
1694 save_commit_buffer = 0;
1695 track_object_refs = 0;
1696 setup_revisions(ac, av, &revs, NULL);
1698 while (fgets(line, sizeof(line), stdin) != NULL) {
1699 int len = strlen(line);
1700 if (line[len - 1] == '\n')
1701 line[--len] = 0;
1702 if (!len)
1703 break;
1704 if (*line == '-') {
1705 if (!strcmp(line, "--not")) {
1706 flags ^= UNINTERESTING;
1707 continue;
1709 die("not a rev '%s'", line);
1711 if (handle_revision_arg(line, &revs, flags, 1))
1712 die("bad revision '%s'", line);
1715 prepare_revision_walk(&revs);
1716 mark_edges_uninteresting(revs.commits, &revs, show_edge);
1717 traverse_commit_list(&revs, show_commit, show_object);
1720 static int adjust_perm(const char *path, mode_t mode)
1722 if (chmod(path, mode))
1723 return -1;
1724 return adjust_shared_perm(path);
1727 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1729 int use_internal_rev_list = 0;
1730 int thin = 0;
1731 uint32_t i;
1732 const char **rp_av;
1733 int rp_ac_alloc = 64;
1734 int rp_ac;
1736 rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
1738 rp_av[0] = "pack-objects";
1739 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1740 rp_ac = 2;
1742 git_config(git_pack_config);
1743 if (!pack_compression_seen && core_compression_seen)
1744 pack_compression_level = core_compression_level;
1746 progress = isatty(2);
1747 for (i = 1; i < argc; i++) {
1748 const char *arg = argv[i];
1750 if (*arg != '-')
1751 break;
1753 if (!strcmp("--non-empty", arg)) {
1754 non_empty = 1;
1755 continue;
1757 if (!strcmp("--local", arg)) {
1758 local = 1;
1759 continue;
1761 if (!strcmp("--incremental", arg)) {
1762 incremental = 1;
1763 continue;
1765 if (!prefixcmp(arg, "--compression=")) {
1766 char *end;
1767 int level = strtoul(arg+14, &end, 0);
1768 if (!arg[14] || *end)
1769 usage(pack_usage);
1770 if (level == -1)
1771 level = Z_DEFAULT_COMPRESSION;
1772 else if (level < 0 || level > Z_BEST_COMPRESSION)
1773 die("bad pack compression level %d", level);
1774 pack_compression_level = level;
1775 continue;
1777 if (!prefixcmp(arg, "--max-pack-size=")) {
1778 char *end;
1779 pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024;
1780 if (!arg[16] || *end)
1781 usage(pack_usage);
1782 continue;
1784 if (!prefixcmp(arg, "--window=")) {
1785 char *end;
1786 window = strtoul(arg+9, &end, 0);
1787 if (!arg[9] || *end)
1788 usage(pack_usage);
1789 continue;
1791 if (!prefixcmp(arg, "--window-memory=")) {
1792 if (!git_parse_ulong(arg+16, &window_memory_limit))
1793 usage(pack_usage);
1794 continue;
1796 if (!prefixcmp(arg, "--depth=")) {
1797 char *end;
1798 depth = strtoul(arg+8, &end, 0);
1799 if (!arg[8] || *end)
1800 usage(pack_usage);
1801 continue;
1803 if (!strcmp("--progress", arg)) {
1804 progress = 1;
1805 continue;
1807 if (!strcmp("--all-progress", arg)) {
1808 progress = 2;
1809 continue;
1811 if (!strcmp("-q", arg)) {
1812 progress = 0;
1813 continue;
1815 if (!strcmp("--no-reuse-delta", arg)) {
1816 no_reuse_delta = 1;
1817 continue;
1819 if (!strcmp("--no-reuse-object", arg)) {
1820 no_reuse_object = no_reuse_delta = 1;
1821 continue;
1823 if (!strcmp("--delta-base-offset", arg)) {
1824 allow_ofs_delta = 1;
1825 continue;
1827 if (!strncmp("--pack-version=", arg, 15)) {
1828 pack_version = strtol(arg + 15, NULL, 10);
1829 if (!pack_version_ok(htonl(pack_version)))
1830 die("unsupported pack version %d", pack_version);
1831 // packv4 and later requires index v3 (at least)
1832 pack_idx_default_version = 3;
1833 continue;
1835 if (!strcmp("--stdout", arg)) {
1836 pack_to_stdout = 1;
1837 continue;
1839 if (!strcmp("--revs", arg)) {
1840 use_internal_rev_list = 1;
1841 continue;
1843 if (!strcmp("--unpacked", arg) ||
1844 !prefixcmp(arg, "--unpacked=") ||
1845 !strcmp("--reflog", arg) ||
1846 !strcmp("--all", arg)) {
1847 use_internal_rev_list = 1;
1848 if (rp_ac >= rp_ac_alloc - 1) {
1849 rp_ac_alloc = alloc_nr(rp_ac_alloc);
1850 rp_av = xrealloc(rp_av,
1851 rp_ac_alloc * sizeof(*rp_av));
1853 rp_av[rp_ac++] = arg;
1854 continue;
1856 if (!strcmp("--thin", arg)) {
1857 use_internal_rev_list = 1;
1858 thin = 1;
1859 rp_av[1] = "--objects-edge";
1860 continue;
1862 if (!prefixcmp(arg, "--index-version=")) {
1863 char *c;
1864 pack_idx_default_version = strtoul(arg + 16, &c, 10);
1865 if (pack_idx_default_version > 2)
1866 die("bad %s", arg);
1867 if (*c == ',')
1868 pack_idx_off32_limit = strtoul(c+1, &c, 0);
1869 if (*c || pack_idx_off32_limit & 0x80000000)
1870 die("bad %s", arg);
1871 continue;
1873 usage(pack_usage);
1876 /* Traditionally "pack-objects [options] base extra" failed;
1877 * we would however want to take refs parameter that would
1878 * have been given to upstream rev-list ourselves, which means
1879 * we somehow want to say what the base name is. So the
1880 * syntax would be:
1882 * pack-objects [options] base <refs...>
1884 * in other words, we would treat the first non-option as the
1885 * base_name and send everything else to the internal revision
1886 * walker.
1889 if (!pack_to_stdout)
1890 base_name = argv[i++];
1892 if (pack_to_stdout != !base_name)
1893 usage(pack_usage);
1895 if (pack_to_stdout && pack_size_limit)
1896 die("--max-pack-size cannot be used to build a pack for transfer.");
1898 if (!pack_to_stdout && thin)
1899 die("--thin cannot be used to build an indexable pack.");
1901 prepare_packed_git();
1903 if (progress)
1904 start_progress(&progress_state, "Generating pack...",
1905 "Counting objects: ", 0);
1906 if (!use_internal_rev_list)
1907 read_object_list_from_stdin();
1908 else {
1909 rp_av[rp_ac] = NULL;
1910 get_object_list(rp_ac, rp_av);
1912 if (progress) {
1913 stop_progress(&progress_state);
1914 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1917 if (non_empty && !nr_result)
1918 return 0;
1919 if (progress && (nr_objects != nr_result))
1920 fprintf(stderr, "Result has %u objects.\n", nr_result);
1921 if (nr_result)
1922 prepare_pack(window, depth);
1923 write_pack_file();
1924 if (progress)
1925 fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
1926 written, written_delta, reused, reused_delta);
1927 return 0;