packv4: Kill duplicated code in nth_packed_object_offset()
[git/packv4.git] / builtin-pack-objects.c
blob475f0b56aef7793ebd3b5ff3141b86a7ec84f0f8
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 > 2) {
179 const uint32_t *off_32 =
180 (uint32_t *)(index + 8 + p->num_objects * 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 if (p->index_version > 1) {
195 const uint32_t *off_32 =
196 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
197 const uint32_t *off_64 = off_32 + p->num_objects;
198 for (i = 0; i < num_ent; i++) {
199 uint32_t off = ntohl(*off_32++);
200 if (!(off & 0x80000000)) {
201 rix->revindex[i].offset = off;
202 } else {
203 rix->revindex[i].offset =
204 ((uint64_t)ntohl(*off_64++)) << 32;
205 rix->revindex[i].offset |=
206 ntohl(*off_64++);
208 rix->revindex[i].nr = i;
210 } else {
211 for (i = 0; i < num_ent; i++) {
212 uint32_t hl = *((uint32_t *)(index + 24 * i));
213 rix->revindex[i].offset = ntohl(hl);
214 rix->revindex[i].nr = i;
218 /* This knows the pack format -- the 20-byte trailer
219 * follows immediately after the last object data.
221 rix->revindex[num_ent].offset = p->pack_size - 20;
222 rix->revindex[num_ent].nr = -1;
223 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
226 static struct revindex_entry * find_packed_object(struct packed_git *p,
227 off_t ofs)
229 int num;
230 int lo, hi;
231 struct pack_revindex *rix;
232 struct revindex_entry *revindex;
233 num = pack_revindex_ix(p);
234 if (num < 0)
235 die("internal error: pack revindex uninitialized");
236 rix = &pack_revindex[num];
237 if (!rix->revindex)
238 prepare_pack_revindex(rix);
239 revindex = rix->revindex;
240 lo = 0;
241 hi = p->num_objects + 1;
242 do {
243 int mi = (lo + hi) / 2;
244 if (revindex[mi].offset == ofs) {
245 return revindex + mi;
247 else if (ofs < revindex[mi].offset)
248 hi = mi;
249 else
250 lo = mi + 1;
251 } while (lo < hi);
252 die("internal error: pack revindex corrupt");
255 static const unsigned char *find_packed_object_name(struct packed_git *p,
256 off_t ofs)
258 struct revindex_entry *entry = find_packed_object(p, ofs);
259 return nth_packed_object_sha1(p, entry->nr);
262 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
264 unsigned long othersize, delta_size;
265 enum object_type type;
266 void *otherbuf = read_sha1_file(entry->delta->idx.sha1, &type, &othersize);
267 void *delta_buf;
269 if (!otherbuf)
270 die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1));
271 delta_buf = diff_delta(otherbuf, othersize,
272 buf, size, &delta_size, 0);
273 if (!delta_buf || delta_size != entry->delta_size)
274 die("delta size changed");
275 free(buf);
276 free(otherbuf);
277 return delta_buf;
281 * The per-object header is a pretty dense thing, which is
282 * - first byte: low four bits are "size", then three bits of "type",
283 * and the high bit is "size continues".
284 * - each byte afterwards: low seven bits are size continuation,
285 * with the high bit being "size continues"
287 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
289 int n = 1;
290 unsigned char c;
292 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
293 die("bad type %d", type);
295 c = (type << 4) | (size & 15);
296 size >>= 4;
297 while (size) {
298 *hdr++ = c | 0x80;
299 c = size & 0x7f;
300 size >>= 7;
301 n++;
303 *hdr = c;
304 return n;
308 * we are going to reuse the existing object data as is. make
309 * sure it is not corrupt.
311 static int check_pack_inflate(struct packed_git *p,
312 struct pack_window **w_curs,
313 off_t offset,
314 off_t len,
315 unsigned long expect)
317 z_stream stream;
318 unsigned char fakebuf[4096], *in;
319 int st;
321 memset(&stream, 0, sizeof(stream));
322 inflateInit(&stream);
323 do {
324 in = use_pack(p, w_curs, offset, &stream.avail_in);
325 stream.next_in = in;
326 stream.next_out = fakebuf;
327 stream.avail_out = sizeof(fakebuf);
328 st = inflate(&stream, Z_FINISH);
329 offset += stream.next_in - in;
330 } while (st == Z_OK || st == Z_BUF_ERROR);
331 inflateEnd(&stream);
332 return (st == Z_STREAM_END &&
333 stream.total_out == expect &&
334 stream.total_in == len) ? 0 : -1;
337 static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
338 off_t offset, off_t len, unsigned int nr)
340 const uint32_t *index_crc;
341 uint32_t data_crc = crc32(0, Z_NULL, 0);
342 uint32_t obj_offset = 0;
344 do {
345 unsigned int avail;
346 void *data = use_pack(p, w_curs, offset, &avail);
347 if (avail > len)
348 avail = len;
349 data_crc = crc32(data_crc, data, avail);
350 offset += avail;
351 len -= avail;
352 } while (len);
354 if (p->index_version < 3)
355 obj_offset = p->num_objects * (20/4);
357 index_crc = p->index_data;
358 index_crc += 2 + 256 + obj_offset + nr;
360 return data_crc != ntohl(*index_crc);
363 static void copy_pack_data(struct sha1file *f,
364 struct packed_git *p,
365 struct pack_window **w_curs,
366 off_t offset,
367 off_t len)
369 unsigned char *in;
370 unsigned int avail;
372 while (len) {
373 in = use_pack(p, w_curs, offset, &avail);
374 if (avail > len)
375 avail = (unsigned int)len;
376 sha1write(f, in, avail);
377 offset += avail;
378 len -= avail;
382 static unsigned long write_object(struct sha1file *f,
383 struct object_entry *entry,
384 off_t write_offset)
386 unsigned long size;
387 enum object_type type;
388 void *buf;
389 unsigned char header[10];
390 unsigned char dheader[10];
391 unsigned hdrlen;
392 off_t datalen;
393 enum object_type obj_type;
394 int to_reuse = 0;
395 /* write limit if limited packsize and not first object */
396 unsigned long limit = pack_size_limit && nr_written ?
397 pack_size_limit - write_offset : 0;
398 /* no if no delta */
399 int usable_delta = !entry->delta ? 0 :
400 /* yes if unlimited packfile */
401 !pack_size_limit ? 1 :
402 /* no if base written to previous pack */
403 entry->delta->idx.offset == (off_t)-1 ? 0 :
404 /* otherwise double-check written to this
405 * pack, like we do below
407 entry->delta->idx.offset ? 1 : 0;
409 if (!pack_to_stdout)
410 crc32_begin(f);
412 obj_type = entry->type;
413 if (no_reuse_object)
414 to_reuse = 0; /* explicit */
415 else if (!entry->in_pack)
416 to_reuse = 0; /* can't reuse what we don't have */
417 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
418 /* check_object() decided it for us ... */
419 to_reuse = usable_delta;
420 /* ... but pack split may override that */
421 else if (obj_type != entry->in_pack_type)
422 to_reuse = 0; /* pack has delta which is unusable */
423 else if (entry->delta)
424 to_reuse = 0; /* we want to pack afresh */
425 else
426 to_reuse = 1; /* we have it in-pack undeltified,
427 * and we do not need to deltify it.
430 if (!to_reuse) {
431 z_stream stream;
432 unsigned long maxsize;
433 void *out;
434 if (!usable_delta) {
435 buf = read_sha1_file(entry->idx.sha1, &obj_type, &size);
436 if (!buf)
437 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
438 } else if (entry->delta_data) {
439 size = entry->delta_size;
440 buf = entry->delta_data;
441 entry->delta_data = NULL;
442 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
443 OBJ_OFS_DELTA : OBJ_REF_DELTA;
444 } else {
445 buf = read_sha1_file(entry->idx.sha1, &type, &size);
446 if (!buf)
447 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
448 buf = delta_against(buf, size, entry);
449 size = entry->delta_size;
450 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
451 OBJ_OFS_DELTA : OBJ_REF_DELTA;
453 /* compress the data to store and put compressed length in datalen */
454 memset(&stream, 0, sizeof(stream));
455 deflateInit(&stream, pack_compression_level);
456 maxsize = deflateBound(&stream, size);
457 out = xmalloc(maxsize);
458 /* Compress it */
459 stream.next_in = buf;
460 stream.avail_in = size;
461 stream.next_out = out;
462 stream.avail_out = maxsize;
463 while (deflate(&stream, Z_FINISH) == Z_OK)
464 /* nothing */;
465 deflateEnd(&stream);
466 datalen = stream.total_out;
467 deflateEnd(&stream);
469 * The object header is a byte of 'type' followed by zero or
470 * more bytes of length.
472 hdrlen = encode_header(obj_type, size, header);
474 if (obj_type == OBJ_OFS_DELTA) {
476 * Deltas with relative base contain an additional
477 * encoding of the relative offset for the delta
478 * base from this object's position in the pack.
480 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
481 unsigned pos = sizeof(dheader) - 1;
482 dheader[pos] = ofs & 127;
483 while (ofs >>= 7)
484 dheader[--pos] = 128 | (--ofs & 127);
485 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
486 free(out);
487 free(buf);
488 return 0;
490 sha1write(f, header, hdrlen);
491 sha1write(f, dheader + pos, sizeof(dheader) - pos);
492 hdrlen += sizeof(dheader) - pos;
493 } else if (obj_type == OBJ_REF_DELTA) {
495 * Deltas with a base reference contain
496 * an additional 20 bytes for the base sha1.
498 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
499 free(out);
500 free(buf);
501 return 0;
503 sha1write(f, header, hdrlen);
504 sha1write(f, entry->delta->idx.sha1, 20);
505 hdrlen += 20;
506 } else {
507 if (limit && hdrlen + datalen + 20 >= limit) {
508 free(out);
509 free(buf);
510 return 0;
512 sha1write(f, header, hdrlen);
514 sha1write(f, out, datalen);
515 free(out);
516 free(buf);
518 else {
519 struct packed_git *p = entry->in_pack;
520 struct pack_window *w_curs = NULL;
521 struct revindex_entry *revidx;
522 off_t offset;
524 if (entry->delta) {
525 obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
526 OBJ_OFS_DELTA : OBJ_REF_DELTA;
527 reused_delta++;
529 hdrlen = encode_header(obj_type, entry->size, header);
530 offset = entry->in_pack_offset;
531 revidx = find_packed_object(p, offset);
532 datalen = revidx[1].offset - offset;
533 if (!pack_to_stdout && p->index_version > 1 &&
534 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
535 die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
536 offset += entry->in_pack_header_size;
537 datalen -= entry->in_pack_header_size;
538 if (obj_type == OBJ_OFS_DELTA) {
539 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
540 unsigned pos = sizeof(dheader) - 1;
541 dheader[pos] = ofs & 127;
542 while (ofs >>= 7)
543 dheader[--pos] = 128 | (--ofs & 127);
544 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit)
545 return 0;
546 sha1write(f, header, hdrlen);
547 sha1write(f, dheader + pos, sizeof(dheader) - pos);
548 hdrlen += sizeof(dheader) - pos;
549 } else if (obj_type == OBJ_REF_DELTA) {
550 if (limit && hdrlen + 20 + datalen + 20 >= limit)
551 return 0;
552 sha1write(f, header, hdrlen);
553 sha1write(f, entry->delta->idx.sha1, 20);
554 hdrlen += 20;
555 } else {
556 if (limit && hdrlen + datalen + 20 >= limit)
557 return 0;
558 sha1write(f, header, hdrlen);
561 if (!pack_to_stdout && p->index_version == 1 &&
562 check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
563 die("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1));
564 copy_pack_data(f, p, &w_curs, offset, datalen);
565 unuse_pack(&w_curs);
566 reused++;
568 if (usable_delta)
569 written_delta++;
570 written++;
571 if (!pack_to_stdout)
572 entry->idx.crc32 = crc32_end(f);
573 return hdrlen + datalen;
576 static off_t write_one(struct sha1file *f,
577 struct object_entry *e,
578 off_t offset)
580 unsigned long size;
582 /* offset is non zero if object is written already. */
583 if (e->idx.offset || e->preferred_base)
584 return offset;
586 /* if we are deltified, write out base object first. */
587 if (e->delta) {
588 offset = write_one(f, e->delta, offset);
589 if (!offset)
590 return 0;
593 e->idx.offset = offset;
594 size = write_object(f, e, offset);
595 if (!size) {
596 e->idx.offset = 0;
597 return 0;
599 written_list[nr_written++] = e;
601 /* make sure off_t is sufficiently large not to wrap */
602 if (offset > offset + size)
603 die("pack too large for current definition of off_t");
604 return offset + size;
607 static int open_object_dir_tmp(const char *path)
609 snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
610 return xmkstemp(tmpname);
614 * Brute force when in doubt, that's the rule dude!
616 * I've done this ugly (and probably non-sense) hack because
617 * I'm not sure whether I can play with objects order _before_
618 * writing them into the pack file.
620 * So, instead of changing objects order I'm creating a new SHA1
621 * list and sorting it.
623 * Yeah, this is slow and if not needed all this code can be
624 * reduced to one to three lines.
626 struct sha1_list {
627 unsigned char sha1[20];
630 static int sha1_compare(const void *_a, const void *_b)
632 struct sha1_list *a = (struct sha1_list *) _a;
633 struct sha1_list *b = (struct sha1_list *) _b;
634 return hashcmp(a->sha1, b->sha1);
637 static off_t write_sha1_dict(struct sha1file *f)
639 int i;
640 struct sha1_list *sha1_list;
642 sha1_list = xmalloc(sizeof(*sha1_list) * nr_objects);
643 for (i = 0; i < nr_objects; i++)
644 hashcpy(sha1_list[i].sha1, objects[i].idx.sha1);
646 qsort(sha1_list, nr_objects, sizeof(*sha1_list), sha1_compare);
648 for (i = 0; i < nr_objects; i++)
649 sha1write(f, sha1_list[i].sha1, 20);
651 free(sha1_list);
652 return nr_objects * 20;
655 /* forward declaration for write_pack_file */
656 static int adjust_perm(const char *path, mode_t mode);
658 static void write_pack_file(void)
660 uint32_t i = 0, j;
661 struct sha1file *f;
662 off_t offset, offset_one, last_obj_offset = 0;
663 struct pack_header hdr;
664 int do_progress = progress >> pack_to_stdout;
665 uint32_t nr_remaining = nr_result;
667 if (do_progress)
668 start_progress(&progress_state, "Writing %u objects...", "", nr_result);
669 written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
671 do {
672 unsigned char sha1[20];
674 if (pack_to_stdout) {
675 f = sha1fd(1, "<stdout>");
676 } else {
677 int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
678 pack_tmp_name = xstrdup(tmpname);
679 f = sha1fd(fd, pack_tmp_name);
682 hdr.hdr_signature = htonl(PACK_SIGNATURE);
683 hdr.hdr_version = htonl(pack_version);
684 hdr.hdr_entries = htonl(nr_remaining);
685 sha1write(f, &hdr, sizeof(hdr));
686 offset = sizeof(hdr);
688 if (pack_version == 4)
689 offset += write_sha1_dict(f);
691 nr_written = 0;
692 for (; i < nr_objects; i++) {
693 last_obj_offset = offset;
694 offset_one = write_one(f, objects + i, offset);
695 if (!offset_one)
696 break;
697 offset = offset_one;
698 if (do_progress)
699 display_progress(&progress_state, written);
703 * Did we write the wrong # entries in the header?
704 * If so, rewrite it like in fast-import
706 if (pack_to_stdout || nr_written == nr_remaining) {
707 sha1close(f, sha1, 1);
708 } else {
709 sha1close(f, sha1, 0);
710 fixup_pack_header_footer(f->fd, sha1, pack_tmp_name, nr_written);
711 close(f->fd);
714 if (!pack_to_stdout) {
715 mode_t mode = umask(0);
717 umask(mode);
718 mode = 0444 & ~mode;
720 idx_tmp_name = write_idx_file(NULL,
721 (struct pack_idx_entry **) written_list, nr_written, sha1);
722 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
723 base_name, sha1_to_hex(sha1));
724 if (adjust_perm(pack_tmp_name, mode))
725 die("unable to make temporary pack file readable: %s",
726 strerror(errno));
727 if (rename(pack_tmp_name, tmpname))
728 die("unable to rename temporary pack file: %s",
729 strerror(errno));
730 snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
731 base_name, sha1_to_hex(sha1));
732 if (adjust_perm(idx_tmp_name, mode))
733 die("unable to make temporary index file readable: %s",
734 strerror(errno));
735 if (rename(idx_tmp_name, tmpname))
736 die("unable to rename temporary index file: %s",
737 strerror(errno));
738 puts(sha1_to_hex(sha1));
741 /* mark written objects as written to previous pack */
742 for (j = 0; j < nr_written; j++) {
743 written_list[j]->idx.offset = (off_t)-1;
745 nr_remaining -= nr_written;
746 } while (nr_remaining && i < nr_objects);
748 free(written_list);
749 if (do_progress)
750 stop_progress(&progress_state);
751 if (written != nr_result)
752 die("wrote %u objects while expecting %u", written, nr_result);
754 * We have scanned through [0 ... i). Since we have written
755 * the correct number of objects, the remaining [i ... nr_objects)
756 * items must be either already written (due to out-of-order delta base)
757 * or a preferred base. Count those which are neither and complain if any.
759 for (j = 0; i < nr_objects; i++) {
760 struct object_entry *e = objects + i;
761 j += !e->idx.offset && !e->preferred_base;
763 if (j)
764 die("wrote %u objects as expected but %u unwritten", written, j);
767 static int locate_object_entry_hash(const unsigned char *sha1)
769 int i;
770 unsigned int ui;
771 memcpy(&ui, sha1, sizeof(unsigned int));
772 i = ui % object_ix_hashsz;
773 while (0 < object_ix[i]) {
774 if (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1))
775 return i;
776 if (++i == object_ix_hashsz)
777 i = 0;
779 return -1 - i;
782 static struct object_entry *locate_object_entry(const unsigned char *sha1)
784 int i;
786 if (!object_ix_hashsz)
787 return NULL;
789 i = locate_object_entry_hash(sha1);
790 if (0 <= i)
791 return &objects[object_ix[i]-1];
792 return NULL;
795 static void rehash_objects(void)
797 uint32_t i;
798 struct object_entry *oe;
800 object_ix_hashsz = nr_objects * 3;
801 if (object_ix_hashsz < 1024)
802 object_ix_hashsz = 1024;
803 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
804 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
805 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
806 int ix = locate_object_entry_hash(oe->idx.sha1);
807 if (0 <= ix)
808 continue;
809 ix = -1 - ix;
810 object_ix[ix] = i + 1;
814 static unsigned name_hash(const char *name)
816 unsigned char c;
817 unsigned hash = 0;
819 if (!name)
820 return 0;
823 * This effectively just creates a sortable number from the
824 * last sixteen non-whitespace characters. Last characters
825 * count "most", so things that end in ".c" sort together.
827 while ((c = *name++) != 0) {
828 if (isspace(c))
829 continue;
830 hash = (hash >> 2) + (c << 24);
832 return hash;
835 static void setup_delta_attr_check(struct git_attr_check *check)
837 static struct git_attr *attr_delta;
839 if (!attr_delta)
840 attr_delta = git_attr("delta", 5);
842 check[0].attr = attr_delta;
845 static int no_try_delta(const char *path)
847 struct git_attr_check check[1];
849 setup_delta_attr_check(check);
850 if (git_checkattr(path, ARRAY_SIZE(check), check))
851 return 0;
852 if (ATTR_FALSE(check->value))
853 return 1;
854 return 0;
857 static int add_object_entry(const unsigned char *sha1, enum object_type type,
858 const char *name, int exclude)
860 struct object_entry *entry;
861 struct packed_git *p, *found_pack = NULL;
862 off_t found_offset = 0;
863 int ix;
864 unsigned hash = name_hash(name);
866 ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
867 if (ix >= 0) {
868 if (exclude) {
869 entry = objects + object_ix[ix] - 1;
870 if (!entry->preferred_base)
871 nr_result--;
872 entry->preferred_base = 1;
874 return 0;
877 for (p = packed_git; p; p = p->next) {
878 off_t offset = find_pack_entry_one(sha1, p);
879 if (offset) {
880 if (!found_pack) {
881 found_offset = offset;
882 found_pack = p;
884 if (exclude)
885 break;
886 if (incremental)
887 return 0;
888 if (local && !p->pack_local)
889 return 0;
893 if (nr_objects >= nr_alloc) {
894 nr_alloc = (nr_alloc + 1024) * 3 / 2;
895 objects = xrealloc(objects, nr_alloc * sizeof(*entry));
898 entry = objects + nr_objects++;
899 memset(entry, 0, sizeof(*entry));
900 hashcpy(entry->idx.sha1, sha1);
901 entry->hash = hash;
902 if (type)
903 entry->type = type;
904 if (exclude)
905 entry->preferred_base = 1;
906 else
907 nr_result++;
908 if (found_pack) {
909 entry->in_pack = found_pack;
910 entry->in_pack_offset = found_offset;
913 if (object_ix_hashsz * 3 <= nr_objects * 4)
914 rehash_objects();
915 else
916 object_ix[-1 - ix] = nr_objects;
918 if (progress)
919 display_progress(&progress_state, nr_objects);
921 if (name && no_try_delta(name))
922 entry->no_try_delta = 1;
924 return 1;
927 struct pbase_tree_cache {
928 unsigned char sha1[20];
929 int ref;
930 int temporary;
931 void *tree_data;
932 unsigned long tree_size;
935 static struct pbase_tree_cache *(pbase_tree_cache[256]);
936 static int pbase_tree_cache_ix(const unsigned char *sha1)
938 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
940 static int pbase_tree_cache_ix_incr(int ix)
942 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
945 static struct pbase_tree {
946 struct pbase_tree *next;
947 /* This is a phony "cache" entry; we are not
948 * going to evict it nor find it through _get()
949 * mechanism -- this is for the toplevel node that
950 * would almost always change with any commit.
952 struct pbase_tree_cache pcache;
953 } *pbase_tree;
955 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
957 struct pbase_tree_cache *ent, *nent;
958 void *data;
959 unsigned long size;
960 enum object_type type;
961 int neigh;
962 int my_ix = pbase_tree_cache_ix(sha1);
963 int available_ix = -1;
965 /* pbase-tree-cache acts as a limited hashtable.
966 * your object will be found at your index or within a few
967 * slots after that slot if it is cached.
969 for (neigh = 0; neigh < 8; neigh++) {
970 ent = pbase_tree_cache[my_ix];
971 if (ent && !hashcmp(ent->sha1, sha1)) {
972 ent->ref++;
973 return ent;
975 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
976 ((0 <= available_ix) &&
977 (!ent && pbase_tree_cache[available_ix])))
978 available_ix = my_ix;
979 if (!ent)
980 break;
981 my_ix = pbase_tree_cache_ix_incr(my_ix);
984 /* Did not find one. Either we got a bogus request or
985 * we need to read and perhaps cache.
987 data = read_sha1_file(sha1, &type, &size);
988 if (!data)
989 return NULL;
990 if (type != OBJ_TREE) {
991 free(data);
992 return NULL;
995 /* We need to either cache or return a throwaway copy */
997 if (available_ix < 0)
998 ent = NULL;
999 else {
1000 ent = pbase_tree_cache[available_ix];
1001 my_ix = available_ix;
1004 if (!ent) {
1005 nent = xmalloc(sizeof(*nent));
1006 nent->temporary = (available_ix < 0);
1008 else {
1009 /* evict and reuse */
1010 free(ent->tree_data);
1011 nent = ent;
1013 hashcpy(nent->sha1, sha1);
1014 nent->tree_data = data;
1015 nent->tree_size = size;
1016 nent->ref = 1;
1017 if (!nent->temporary)
1018 pbase_tree_cache[my_ix] = nent;
1019 return nent;
1022 static void pbase_tree_put(struct pbase_tree_cache *cache)
1024 if (!cache->temporary) {
1025 cache->ref--;
1026 return;
1028 free(cache->tree_data);
1029 free(cache);
1032 static int name_cmp_len(const char *name)
1034 int i;
1035 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1037 return i;
1040 static void add_pbase_object(struct tree_desc *tree,
1041 const char *name,
1042 int cmplen,
1043 const char *fullname)
1045 struct name_entry entry;
1046 int cmp;
1048 while (tree_entry(tree,&entry)) {
1049 if (S_ISGITLINK(entry.mode))
1050 continue;
1051 cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
1052 memcmp(name, entry.path, cmplen);
1053 if (cmp > 0)
1054 continue;
1055 if (cmp < 0)
1056 return;
1057 if (name[cmplen] != '/') {
1058 add_object_entry(entry.sha1,
1059 S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB,
1060 fullname, 1);
1061 return;
1063 if (S_ISDIR(entry.mode)) {
1064 struct tree_desc sub;
1065 struct pbase_tree_cache *tree;
1066 const char *down = name+cmplen+1;
1067 int downlen = name_cmp_len(down);
1069 tree = pbase_tree_get(entry.sha1);
1070 if (!tree)
1071 return;
1072 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1074 add_pbase_object(&sub, down, downlen, fullname);
1075 pbase_tree_put(tree);
1080 static unsigned *done_pbase_paths;
1081 static int done_pbase_paths_num;
1082 static int done_pbase_paths_alloc;
1083 static int done_pbase_path_pos(unsigned hash)
1085 int lo = 0;
1086 int hi = done_pbase_paths_num;
1087 while (lo < hi) {
1088 int mi = (hi + lo) / 2;
1089 if (done_pbase_paths[mi] == hash)
1090 return mi;
1091 if (done_pbase_paths[mi] < hash)
1092 hi = mi;
1093 else
1094 lo = mi + 1;
1096 return -lo-1;
1099 static int check_pbase_path(unsigned hash)
1101 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
1102 if (0 <= pos)
1103 return 1;
1104 pos = -pos - 1;
1105 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
1106 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
1107 done_pbase_paths = xrealloc(done_pbase_paths,
1108 done_pbase_paths_alloc *
1109 sizeof(unsigned));
1111 done_pbase_paths_num++;
1112 if (pos < done_pbase_paths_num)
1113 memmove(done_pbase_paths + pos + 1,
1114 done_pbase_paths + pos,
1115 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1116 done_pbase_paths[pos] = hash;
1117 return 0;
1120 static void add_preferred_base_object(const char *name)
1122 struct pbase_tree *it;
1123 int cmplen;
1124 unsigned hash = name_hash(name);
1126 if (!num_preferred_base || check_pbase_path(hash))
1127 return;
1129 cmplen = name_cmp_len(name);
1130 for (it = pbase_tree; it; it = it->next) {
1131 if (cmplen == 0) {
1132 add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1);
1134 else {
1135 struct tree_desc tree;
1136 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1137 add_pbase_object(&tree, name, cmplen, name);
1142 static void add_preferred_base(unsigned char *sha1)
1144 struct pbase_tree *it;
1145 void *data;
1146 unsigned long size;
1147 unsigned char tree_sha1[20];
1149 if (window <= num_preferred_base++)
1150 return;
1152 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1153 if (!data)
1154 return;
1156 for (it = pbase_tree; it; it = it->next) {
1157 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
1158 free(data);
1159 return;
1163 it = xcalloc(1, sizeof(*it));
1164 it->next = pbase_tree;
1165 pbase_tree = it;
1167 hashcpy(it->pcache.sha1, tree_sha1);
1168 it->pcache.tree_data = data;
1169 it->pcache.tree_size = size;
1172 static void check_object(struct object_entry *entry)
1174 if (entry->in_pack) {
1175 struct packed_git *p = entry->in_pack;
1176 struct pack_window *w_curs = NULL;
1177 const unsigned char *base_ref = NULL;
1178 struct object_entry *base_entry;
1179 unsigned long used, used_0;
1180 unsigned int avail;
1181 off_t ofs;
1182 unsigned char *buf, c;
1184 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1187 * We want in_pack_type even if we do not reuse delta
1188 * since non-delta representations could still be reused.
1190 used = unpack_object_header_gently(buf, avail,
1191 &entry->in_pack_type,
1192 &entry->size);
1195 * Determine if this is a delta and if so whether we can
1196 * reuse it or not. Otherwise let's find out as cheaply as
1197 * possible what the actual type and size for this object is.
1199 switch (entry->in_pack_type) {
1200 default:
1201 /* Not a delta hence we've already got all we need. */
1202 entry->type = entry->in_pack_type;
1203 entry->in_pack_header_size = used;
1204 unuse_pack(&w_curs);
1205 return;
1206 case OBJ_REF_DELTA:
1207 if (!no_reuse_delta && !entry->preferred_base)
1208 base_ref = use_pack(p, &w_curs,
1209 entry->in_pack_offset + used, NULL);
1210 entry->in_pack_header_size = used + 20;
1211 break;
1212 case OBJ_OFS_DELTA:
1213 buf = use_pack(p, &w_curs,
1214 entry->in_pack_offset + used, NULL);
1215 used_0 = 0;
1216 c = buf[used_0++];
1217 ofs = c & 127;
1218 while (c & 128) {
1219 ofs += 1;
1220 if (!ofs || MSB(ofs, 7))
1221 die("delta base offset overflow in pack for %s",
1222 sha1_to_hex(entry->idx.sha1));
1223 c = buf[used_0++];
1224 ofs = (ofs << 7) + (c & 127);
1226 if (ofs >= entry->in_pack_offset)
1227 die("delta base offset out of bound for %s",
1228 sha1_to_hex(entry->idx.sha1));
1229 ofs = entry->in_pack_offset - ofs;
1230 if (!no_reuse_delta && !entry->preferred_base)
1231 base_ref = find_packed_object_name(p, ofs);
1232 entry->in_pack_header_size = used + used_0;
1233 break;
1236 if (base_ref && (base_entry = locate_object_entry(base_ref))) {
1238 * If base_ref was set above that means we wish to
1239 * reuse delta data, and we even found that base
1240 * in the list of objects we want to pack. Goodie!
1242 * Depth value does not matter - find_deltas() will
1243 * never consider reused delta as the base object to
1244 * deltify other objects against, in order to avoid
1245 * circular deltas.
1247 entry->type = entry->in_pack_type;
1248 entry->delta = base_entry;
1249 entry->delta_sibling = base_entry->delta_child;
1250 base_entry->delta_child = entry;
1251 unuse_pack(&w_curs);
1252 return;
1255 if (entry->type) {
1257 * This must be a delta and we already know what the
1258 * final object type is. Let's extract the actual
1259 * object size from the delta header.
1261 entry->size = get_size_from_delta(p, &w_curs,
1262 entry->in_pack_offset + entry->in_pack_header_size);
1263 unuse_pack(&w_curs);
1264 return;
1268 * No choice but to fall back to the recursive delta walk
1269 * with sha1_object_info() to find about the object type
1270 * at this point...
1272 unuse_pack(&w_curs);
1275 entry->type = sha1_object_info(entry->idx.sha1, &entry->size);
1276 if (entry->type < 0)
1277 die("unable to get type of object %s",
1278 sha1_to_hex(entry->idx.sha1));
1281 static int pack_offset_sort(const void *_a, const void *_b)
1283 const struct object_entry *a = *(struct object_entry **)_a;
1284 const struct object_entry *b = *(struct object_entry **)_b;
1286 /* avoid filesystem trashing with loose objects */
1287 if (!a->in_pack && !b->in_pack)
1288 return hashcmp(a->idx.sha1, b->idx.sha1);
1290 if (a->in_pack < b->in_pack)
1291 return -1;
1292 if (a->in_pack > b->in_pack)
1293 return 1;
1294 return a->in_pack_offset < b->in_pack_offset ? -1 :
1295 (a->in_pack_offset > b->in_pack_offset);
1298 static void get_object_details(void)
1300 uint32_t i;
1301 struct object_entry **sorted_by_offset;
1303 sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));
1304 for (i = 0; i < nr_objects; i++)
1305 sorted_by_offset[i] = objects + i;
1306 qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
1308 prepare_pack_ix();
1309 for (i = 0; i < nr_objects; i++)
1310 check_object(sorted_by_offset[i]);
1311 free(sorted_by_offset);
1314 static int type_size_sort(const void *_a, const void *_b)
1316 const struct object_entry *a = *(struct object_entry **)_a;
1317 const struct object_entry *b = *(struct object_entry **)_b;
1319 if (a->type < b->type)
1320 return -1;
1321 if (a->type > b->type)
1322 return 1;
1323 if (a->hash < b->hash)
1324 return -1;
1325 if (a->hash > b->hash)
1326 return 1;
1327 if (a->preferred_base < b->preferred_base)
1328 return -1;
1329 if (a->preferred_base > b->preferred_base)
1330 return 1;
1331 if (a->size < b->size)
1332 return -1;
1333 if (a->size > b->size)
1334 return 1;
1335 return a > b ? -1 : (a < b); /* newest last */
1338 struct unpacked {
1339 struct object_entry *entry;
1340 void *data;
1341 struct delta_index *index;
1342 unsigned depth;
1345 static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1346 unsigned long delta_size)
1348 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
1349 return 0;
1351 if (delta_size < cache_max_small_delta_size)
1352 return 1;
1354 /* cache delta, if objects are large enough compared to delta size */
1355 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
1356 return 1;
1358 return 0;
1362 * We search for deltas _backwards_ in a list sorted by type and
1363 * by size, so that we see progressively smaller and smaller files.
1364 * That's because we prefer deltas to be from the bigger file
1365 * to the smaller - deletes are potentially cheaper, but perhaps
1366 * more importantly, the bigger file is likely the more recent
1367 * one.
1369 static int try_delta(struct unpacked *trg, struct unpacked *src,
1370 unsigned max_depth)
1372 struct object_entry *trg_entry = trg->entry;
1373 struct object_entry *src_entry = src->entry;
1374 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1375 unsigned ref_depth;
1376 enum object_type type;
1377 void *delta_buf;
1379 /* Don't bother doing diffs between different types */
1380 if (trg_entry->type != src_entry->type)
1381 return -1;
1383 /* We do not compute delta to *create* objects we are not
1384 * going to pack.
1386 if (trg_entry->preferred_base)
1387 return -1;
1390 * We do not bother to try a delta that we discarded
1391 * on an earlier try, but only when reusing delta data.
1393 if (!no_reuse_delta && trg_entry->in_pack &&
1394 trg_entry->in_pack == src_entry->in_pack &&
1395 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1396 trg_entry->in_pack_type != OBJ_OFS_DELTA)
1397 return 0;
1399 /* Let's not bust the allowed depth. */
1400 if (src->depth >= max_depth)
1401 return 0;
1403 /* Now some size filtering heuristics. */
1404 trg_size = trg_entry->size;
1405 if (!trg_entry->delta) {
1406 max_size = trg_size/2 - 20;
1407 ref_depth = 1;
1408 } else {
1409 max_size = trg_entry->delta_size;
1410 ref_depth = trg->depth;
1412 max_size = max_size * (max_depth - src->depth) /
1413 (max_depth - ref_depth + 1);
1414 if (max_size == 0)
1415 return 0;
1416 src_size = src_entry->size;
1417 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1418 if (sizediff >= max_size)
1419 return 0;
1420 if (trg_size < src_size / 32)
1421 return 0;
1423 /* Load data if not already done */
1424 if (!trg->data) {
1425 trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz);
1426 if (!trg->data)
1427 die("object %s cannot be read",
1428 sha1_to_hex(trg_entry->idx.sha1));
1429 if (sz != trg_size)
1430 die("object %s inconsistent object length (%lu vs %lu)",
1431 sha1_to_hex(trg_entry->idx.sha1), sz, trg_size);
1432 window_memory_usage += sz;
1434 if (!src->data) {
1435 src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz);
1436 if (!src->data)
1437 die("object %s cannot be read",
1438 sha1_to_hex(src_entry->idx.sha1));
1439 if (sz != src_size)
1440 die("object %s inconsistent object length (%lu vs %lu)",
1441 sha1_to_hex(src_entry->idx.sha1), sz, src_size);
1442 window_memory_usage += sz;
1444 if (!src->index) {
1445 src->index = create_delta_index(src->data, src_size);
1446 if (!src->index) {
1447 static int warned = 0;
1448 if (!warned++)
1449 warning("suboptimal pack - out of memory");
1450 return 0;
1452 window_memory_usage += sizeof_delta_index(src->index);
1455 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1456 if (!delta_buf)
1457 return 0;
1459 if (trg_entry->delta_data) {
1460 /* Prefer only shallower same-sized deltas. */
1461 if (delta_size == trg_entry->delta_size &&
1462 src->depth + 1 >= trg->depth) {
1463 free(delta_buf);
1464 return 0;
1466 delta_cache_size -= trg_entry->delta_size;
1467 free(trg_entry->delta_data);
1468 trg_entry->delta_data = NULL;
1470 trg_entry->delta = src_entry;
1471 trg_entry->delta_size = delta_size;
1472 trg->depth = src->depth + 1;
1474 if (delta_cacheable(src_size, trg_size, delta_size)) {
1475 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1476 delta_cache_size += trg_entry->delta_size;
1477 } else
1478 free(delta_buf);
1479 return 1;
1482 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1484 struct object_entry *child = me->delta_child;
1485 unsigned int m = n;
1486 while (child) {
1487 unsigned int c = check_delta_limit(child, n + 1);
1488 if (m < c)
1489 m = c;
1490 child = child->delta_sibling;
1492 return m;
1495 static void free_unpacked(struct unpacked *n)
1497 window_memory_usage -= sizeof_delta_index(n->index);
1498 free_delta_index(n->index);
1499 n->index = NULL;
1500 if (n->data) {
1501 free(n->data);
1502 n->data = NULL;
1503 window_memory_usage -= n->entry->size;
1505 n->entry = NULL;
1506 n->depth = 0;
1509 static void find_deltas(struct object_entry **list, int window, int depth)
1511 uint32_t i = nr_objects, idx = 0, count = 0, processed = 0;
1512 unsigned int array_size = window * sizeof(struct unpacked);
1513 struct unpacked *array;
1514 int max_depth;
1516 if (!nr_objects)
1517 return;
1518 array = xmalloc(array_size);
1519 memset(array, 0, array_size);
1520 if (progress)
1521 start_progress(&progress_state, "Deltifying %u objects...", "", nr_result);
1523 do {
1524 struct object_entry *entry = list[--i];
1525 struct unpacked *n = array + idx;
1526 int j;
1528 if (!entry->preferred_base)
1529 processed++;
1531 if (progress)
1532 display_progress(&progress_state, processed);
1534 if (entry->delta)
1535 /* This happens if we decided to reuse existing
1536 * delta from a pack. "!no_reuse_delta &&" is implied.
1538 continue;
1540 if (entry->size < 50)
1541 continue;
1543 if (entry->no_try_delta)
1544 continue;
1546 free_unpacked(n);
1547 n->entry = entry;
1549 while (window_memory_limit &&
1550 window_memory_usage > window_memory_limit &&
1551 count > 1) {
1552 uint32_t tail = (idx + window - count) % window;
1553 free_unpacked(array + tail);
1554 count--;
1558 * If the current object is at pack edge, take the depth the
1559 * objects that depend on the current object into account
1560 * otherwise they would become too deep.
1562 max_depth = depth;
1563 if (entry->delta_child) {
1564 max_depth -= check_delta_limit(entry, 0);
1565 if (max_depth <= 0)
1566 goto next;
1569 j = window;
1570 while (--j > 0) {
1571 uint32_t other_idx = idx + j;
1572 struct unpacked *m;
1573 if (other_idx >= window)
1574 other_idx -= window;
1575 m = array + other_idx;
1576 if (!m->entry)
1577 break;
1578 if (try_delta(n, m, max_depth) < 0)
1579 break;
1582 /* if we made n a delta, and if n is already at max
1583 * depth, leaving it in the window is pointless. we
1584 * should evict it first.
1586 if (entry->delta && depth <= n->depth)
1587 continue;
1589 next:
1590 idx++;
1591 if (count + 1 < window)
1592 count++;
1593 if (idx >= window)
1594 idx = 0;
1595 } while (i > 0);
1597 if (progress)
1598 stop_progress(&progress_state);
1600 for (i = 0; i < window; ++i) {
1601 free_delta_index(array[i].index);
1602 free(array[i].data);
1604 free(array);
1607 static void prepare_pack(int window, int depth)
1609 struct object_entry **delta_list;
1610 uint32_t i;
1612 get_object_details();
1614 if (!window || !depth)
1615 return;
1617 delta_list = xmalloc(nr_objects * sizeof(*delta_list));
1618 for (i = 0; i < nr_objects; i++)
1619 delta_list[i] = objects + i;
1620 qsort(delta_list, nr_objects, sizeof(*delta_list), type_size_sort);
1621 find_deltas(delta_list, window+1, depth);
1622 free(delta_list);
1625 static int git_pack_config(const char *k, const char *v)
1627 if(!strcmp(k, "pack.window")) {
1628 window = git_config_int(k, v);
1629 return 0;
1631 if (!strcmp(k, "pack.windowmemory")) {
1632 window_memory_limit = git_config_ulong(k, v);
1633 return 0;
1635 if (!strcmp(k, "pack.depth")) {
1636 depth = git_config_int(k, v);
1637 return 0;
1639 if (!strcmp(k, "pack.compression")) {
1640 int level = git_config_int(k, v);
1641 if (level == -1)
1642 level = Z_DEFAULT_COMPRESSION;
1643 else if (level < 0 || level > Z_BEST_COMPRESSION)
1644 die("bad pack compression level %d", level);
1645 pack_compression_level = level;
1646 pack_compression_seen = 1;
1647 return 0;
1649 if (!strcmp(k, "pack.deltacachesize")) {
1650 max_delta_cache_size = git_config_int(k, v);
1651 return 0;
1653 if (!strcmp(k, "pack.deltacachelimit")) {
1654 cache_max_small_delta_size = git_config_int(k, v);
1655 return 0;
1657 return git_default_config(k, v);
1660 static void read_object_list_from_stdin(void)
1662 char line[40 + 1 + PATH_MAX + 2];
1663 unsigned char sha1[20];
1665 for (;;) {
1666 if (!fgets(line, sizeof(line), stdin)) {
1667 if (feof(stdin))
1668 break;
1669 if (!ferror(stdin))
1670 die("fgets returned NULL, not EOF, not error!");
1671 if (errno != EINTR)
1672 die("fgets: %s", strerror(errno));
1673 clearerr(stdin);
1674 continue;
1676 if (line[0] == '-') {
1677 if (get_sha1_hex(line+1, sha1))
1678 die("expected edge sha1, got garbage:\n %s",
1679 line);
1680 add_preferred_base(sha1);
1681 continue;
1683 if (get_sha1_hex(line, sha1))
1684 die("expected sha1, got garbage:\n %s", line);
1686 add_preferred_base_object(line+41);
1687 add_object_entry(sha1, 0, line+41, 0);
1691 static void show_commit(struct commit *commit)
1693 add_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0);
1696 static void show_object(struct object_array_entry *p)
1698 add_preferred_base_object(p->name);
1699 add_object_entry(p->item->sha1, p->item->type, p->name, 0);
1702 static void show_edge(struct commit *commit)
1704 add_preferred_base(commit->object.sha1);
1707 static void get_object_list(int ac, const char **av)
1709 struct rev_info revs;
1710 char line[1000];
1711 int flags = 0;
1713 init_revisions(&revs, NULL);
1714 save_commit_buffer = 0;
1715 track_object_refs = 0;
1716 setup_revisions(ac, av, &revs, NULL);
1718 while (fgets(line, sizeof(line), stdin) != NULL) {
1719 int len = strlen(line);
1720 if (line[len - 1] == '\n')
1721 line[--len] = 0;
1722 if (!len)
1723 break;
1724 if (*line == '-') {
1725 if (!strcmp(line, "--not")) {
1726 flags ^= UNINTERESTING;
1727 continue;
1729 die("not a rev '%s'", line);
1731 if (handle_revision_arg(line, &revs, flags, 1))
1732 die("bad revision '%s'", line);
1735 prepare_revision_walk(&revs);
1736 mark_edges_uninteresting(revs.commits, &revs, show_edge);
1737 traverse_commit_list(&revs, show_commit, show_object);
1740 static int adjust_perm(const char *path, mode_t mode)
1742 if (chmod(path, mode))
1743 return -1;
1744 return adjust_shared_perm(path);
1747 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1749 int use_internal_rev_list = 0;
1750 int thin = 0;
1751 uint32_t i;
1752 const char **rp_av;
1753 int rp_ac_alloc = 64;
1754 int rp_ac;
1756 rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
1758 rp_av[0] = "pack-objects";
1759 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1760 rp_ac = 2;
1762 git_config(git_pack_config);
1763 if (!pack_compression_seen && core_compression_seen)
1764 pack_compression_level = core_compression_level;
1766 progress = isatty(2);
1767 for (i = 1; i < argc; i++) {
1768 const char *arg = argv[i];
1770 if (*arg != '-')
1771 break;
1773 if (!strcmp("--non-empty", arg)) {
1774 non_empty = 1;
1775 continue;
1777 if (!strcmp("--local", arg)) {
1778 local = 1;
1779 continue;
1781 if (!strcmp("--incremental", arg)) {
1782 incremental = 1;
1783 continue;
1785 if (!prefixcmp(arg, "--compression=")) {
1786 char *end;
1787 int level = strtoul(arg+14, &end, 0);
1788 if (!arg[14] || *end)
1789 usage(pack_usage);
1790 if (level == -1)
1791 level = Z_DEFAULT_COMPRESSION;
1792 else if (level < 0 || level > Z_BEST_COMPRESSION)
1793 die("bad pack compression level %d", level);
1794 pack_compression_level = level;
1795 continue;
1797 if (!prefixcmp(arg, "--max-pack-size=")) {
1798 char *end;
1799 pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024;
1800 if (!arg[16] || *end)
1801 usage(pack_usage);
1802 continue;
1804 if (!prefixcmp(arg, "--window=")) {
1805 char *end;
1806 window = strtoul(arg+9, &end, 0);
1807 if (!arg[9] || *end)
1808 usage(pack_usage);
1809 continue;
1811 if (!prefixcmp(arg, "--window-memory=")) {
1812 if (!git_parse_ulong(arg+16, &window_memory_limit))
1813 usage(pack_usage);
1814 continue;
1816 if (!prefixcmp(arg, "--depth=")) {
1817 char *end;
1818 depth = strtoul(arg+8, &end, 0);
1819 if (!arg[8] || *end)
1820 usage(pack_usage);
1821 continue;
1823 if (!strcmp("--progress", arg)) {
1824 progress = 1;
1825 continue;
1827 if (!strcmp("--all-progress", arg)) {
1828 progress = 2;
1829 continue;
1831 if (!strcmp("-q", arg)) {
1832 progress = 0;
1833 continue;
1835 if (!strcmp("--no-reuse-delta", arg)) {
1836 no_reuse_delta = 1;
1837 continue;
1839 if (!strcmp("--no-reuse-object", arg)) {
1840 no_reuse_object = no_reuse_delta = 1;
1841 continue;
1843 if (!strcmp("--delta-base-offset", arg)) {
1844 allow_ofs_delta = 1;
1845 continue;
1847 if (!strncmp("--pack-version=", arg, 15)) {
1848 pack_version = strtol(arg + 15, NULL, 10);
1849 if (!pack_version_ok(htonl(pack_version)))
1850 die("unsupported pack version %d", pack_version);
1851 // packv4 and later requires index v3 (at least)
1852 if (pack_version > 2)
1853 pack_idx_default_version = 3;
1854 continue;
1856 if (!strcmp("--stdout", arg)) {
1857 pack_to_stdout = 1;
1858 continue;
1860 if (!strcmp("--revs", arg)) {
1861 use_internal_rev_list = 1;
1862 continue;
1864 if (!strcmp("--unpacked", arg) ||
1865 !prefixcmp(arg, "--unpacked=") ||
1866 !strcmp("--reflog", arg) ||
1867 !strcmp("--all", arg)) {
1868 use_internal_rev_list = 1;
1869 if (rp_ac >= rp_ac_alloc - 1) {
1870 rp_ac_alloc = alloc_nr(rp_ac_alloc);
1871 rp_av = xrealloc(rp_av,
1872 rp_ac_alloc * sizeof(*rp_av));
1874 rp_av[rp_ac++] = arg;
1875 continue;
1877 if (!strcmp("--thin", arg)) {
1878 use_internal_rev_list = 1;
1879 thin = 1;
1880 rp_av[1] = "--objects-edge";
1881 continue;
1883 if (!prefixcmp(arg, "--index-version=")) {
1884 char *c;
1885 pack_idx_default_version = strtoul(arg + 16, &c, 10);
1886 if (pack_idx_default_version > 3)
1887 die("bad %s", arg);
1888 if (*c == ',')
1889 pack_idx_off32_limit = strtoul(c+1, &c, 0);
1890 if (*c || pack_idx_off32_limit & 0x80000000)
1891 die("bad %s", arg);
1892 continue;
1894 usage(pack_usage);
1897 /* Traditionally "pack-objects [options] base extra" failed;
1898 * we would however want to take refs parameter that would
1899 * have been given to upstream rev-list ourselves, which means
1900 * we somehow want to say what the base name is. So the
1901 * syntax would be:
1903 * pack-objects [options] base <refs...>
1905 * in other words, we would treat the first non-option as the
1906 * base_name and send everything else to the internal revision
1907 * walker.
1910 if (!pack_to_stdout)
1911 base_name = argv[i++];
1913 if (pack_to_stdout != !base_name)
1914 usage(pack_usage);
1916 if (pack_to_stdout && pack_size_limit)
1917 die("--max-pack-size cannot be used to build a pack for transfer.");
1919 if (!pack_to_stdout && thin)
1920 die("--thin cannot be used to build an indexable pack.");
1922 prepare_packed_git();
1924 if (progress)
1925 start_progress(&progress_state, "Generating pack...",
1926 "Counting objects: ", 0);
1927 if (!use_internal_rev_list)
1928 read_object_list_from_stdin();
1929 else {
1930 rp_av[rp_ac] = NULL;
1931 get_object_list(rp_ac, rp_av);
1933 if (progress) {
1934 stop_progress(&progress_state);
1935 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1938 if (non_empty && !nr_result)
1939 return 0;
1940 if (progress && (nr_objects != nr_result))
1941 fprintf(stderr, "Result has %u objects.\n", nr_result);
1942 if (nr_result)
1943 prepare_pack(window, depth);
1944 write_pack_file();
1945 if (progress)
1946 fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
1947 written, written_delta, reused, reused_delta);
1948 return 0;