pack-objects: clarification & option checks for --max-pack-size
[git/jrn.git] / builtin-pack-objects.c
bloba4370bbee175ecb7ae95d6e8d93dea3c677c1b8c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "object.h"
4 #include "blob.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "tree.h"
8 #include "delta.h"
9 #include "pack.h"
10 #include "csum-file.h"
11 #include "tree-walk.h"
12 #include "diff.h"
13 #include "revision.h"
14 #include "list-objects.h"
15 #include "progress.h"
17 static const char pack_usage[] = "\
18 git-pack-objects [{ -q | --progress | --all-progress }] [--max-pack-size=N] \n\
19 [--local] [--incremental] [--window=N] [--depth=N] \n\
20 [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\
21 [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\
22 [--stdout | base-name] [<ref-list | <object-list]";
24 struct object_entry {
25 unsigned char sha1[20];
26 uint32_t crc32; /* crc of raw pack data for this object */
27 off_t offset; /* offset into the final pack file */
28 unsigned long size; /* uncompressed size */
29 unsigned int hash; /* name hint hash */
30 unsigned int depth; /* delta depth */
31 struct packed_git *in_pack; /* already in pack */
32 off_t in_pack_offset;
33 struct object_entry *delta; /* delta base object */
34 struct object_entry *delta_child; /* deltified objects who bases me */
35 struct object_entry *delta_sibling; /* other deltified objects who
36 * uses the same base as me
38 unsigned long delta_size; /* delta data size (uncompressed) */
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 objectto delta
44 * objects against.
49 * Objects we are going to pack are collected in objects array (dynamically
50 * expanded). nr_objects & nr_alloc controls this array. They are stored
51 * in the order we see -- typically rev-list --objects order that gives us
52 * nice "minimum seek" order.
54 static struct object_entry *objects;
55 static struct object_entry **written_list;
56 static uint32_t nr_objects, nr_alloc, nr_result, nr_written;
58 static int non_empty;
59 static int no_reuse_delta, no_reuse_object;
60 static int local;
61 static int incremental;
62 static int allow_ofs_delta;
63 static const char *pack_tmp_name, *idx_tmp_name;
64 static char tmpname[PATH_MAX];
65 static const char *base_name;
66 static unsigned char pack_file_sha1[20];
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;
78 * The object names in objects array are hashed with this hashtable,
79 * to help looking up the entry by object name.
80 * This hashtable is built after all the objects are seen.
82 static int *object_ix;
83 static int object_ix_hashsz;
86 * Pack index for existing packs give us easy access to the offsets into
87 * corresponding pack file where each object's data starts, but the entries
88 * do not store the size of the compressed representation (uncompressed
89 * size is easily available by examining the pack entry header). It is
90 * also rather expensive to find the sha1 for an object given its offset.
92 * We build a hashtable of existing packs (pack_revindex), and keep reverse
93 * index here -- pack index file is sorted by object name mapping to offset;
94 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
95 * ordered by offset, so if you know the offset of an object, next offset
96 * is where its packed representation ends and the index_nr can be used to
97 * get the object sha1 from the main index.
99 struct revindex_entry {
100 off_t offset;
101 unsigned int nr;
103 struct pack_revindex {
104 struct packed_git *p;
105 struct revindex_entry *revindex;
107 static struct pack_revindex *pack_revindex;
108 static int pack_revindex_hashsz;
111 * stats
113 static uint32_t written, written_delta;
114 static uint32_t reused, reused_delta;
116 static int pack_revindex_ix(struct packed_git *p)
118 unsigned long ui = (unsigned long)p;
119 int i;
121 ui = ui ^ (ui >> 16); /* defeat structure alignment */
122 i = (int)(ui % pack_revindex_hashsz);
123 while (pack_revindex[i].p) {
124 if (pack_revindex[i].p == p)
125 return i;
126 if (++i == pack_revindex_hashsz)
127 i = 0;
129 return -1 - i;
132 static void prepare_pack_ix(void)
134 int num;
135 struct packed_git *p;
136 for (num = 0, p = packed_git; p; p = p->next)
137 num++;
138 if (!num)
139 return;
140 pack_revindex_hashsz = num * 11;
141 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
142 for (p = packed_git; p; p = p->next) {
143 num = pack_revindex_ix(p);
144 num = - 1 - num;
145 pack_revindex[num].p = p;
147 /* revindex elements are lazily initialized */
150 static int cmp_offset(const void *a_, const void *b_)
152 const struct revindex_entry *a = a_;
153 const struct revindex_entry *b = b_;
154 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
158 * Ordered list of offsets of objects in the pack.
160 static void prepare_pack_revindex(struct pack_revindex *rix)
162 struct packed_git *p = rix->p;
163 int num_ent = p->num_objects;
164 int i;
165 const char *index = p->index_data;
167 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
168 index += 4 * 256;
170 if (p->index_version > 1) {
171 const uint32_t *off_32 =
172 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
173 const uint32_t *off_64 = off_32 + p->num_objects;
174 for (i = 0; i < num_ent; i++) {
175 uint32_t off = ntohl(*off_32++);
176 if (!(off & 0x80000000)) {
177 rix->revindex[i].offset = off;
178 } else {
179 rix->revindex[i].offset =
180 ((uint64_t)ntohl(*off_64++)) << 32;
181 rix->revindex[i].offset |=
182 ntohl(*off_64++);
184 rix->revindex[i].nr = i;
186 } else {
187 for (i = 0; i < num_ent; i++) {
188 uint32_t hl = *((uint32_t *)(index + 24 * i));
189 rix->revindex[i].offset = ntohl(hl);
190 rix->revindex[i].nr = i;
194 /* This knows the pack format -- the 20-byte trailer
195 * follows immediately after the last object data.
197 rix->revindex[num_ent].offset = p->pack_size - 20;
198 rix->revindex[num_ent].nr = -1;
199 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
202 static struct revindex_entry * find_packed_object(struct packed_git *p,
203 off_t ofs)
205 int num;
206 int lo, hi;
207 struct pack_revindex *rix;
208 struct revindex_entry *revindex;
209 num = pack_revindex_ix(p);
210 if (num < 0)
211 die("internal error: pack revindex uninitialized");
212 rix = &pack_revindex[num];
213 if (!rix->revindex)
214 prepare_pack_revindex(rix);
215 revindex = rix->revindex;
216 lo = 0;
217 hi = p->num_objects + 1;
218 do {
219 int mi = (lo + hi) / 2;
220 if (revindex[mi].offset == ofs) {
221 return revindex + mi;
223 else if (ofs < revindex[mi].offset)
224 hi = mi;
225 else
226 lo = mi + 1;
227 } while (lo < hi);
228 die("internal error: pack revindex corrupt");
231 static const unsigned char *find_packed_object_name(struct packed_git *p,
232 off_t ofs)
234 struct revindex_entry *entry = find_packed_object(p, ofs);
235 return nth_packed_object_sha1(p, entry->nr);
238 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
240 unsigned long othersize, delta_size;
241 enum object_type type;
242 void *otherbuf = read_sha1_file(entry->delta->sha1, &type, &othersize);
243 void *delta_buf;
245 if (!otherbuf)
246 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
247 delta_buf = diff_delta(otherbuf, othersize,
248 buf, size, &delta_size, 0);
249 if (!delta_buf || delta_size != entry->delta_size)
250 die("delta size changed");
251 free(buf);
252 free(otherbuf);
253 return delta_buf;
257 * The per-object header is a pretty dense thing, which is
258 * - first byte: low four bits are "size", then three bits of "type",
259 * and the high bit is "size continues".
260 * - each byte afterwards: low seven bits are size continuation,
261 * with the high bit being "size continues"
263 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
265 int n = 1;
266 unsigned char c;
268 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
269 die("bad type %d", type);
271 c = (type << 4) | (size & 15);
272 size >>= 4;
273 while (size) {
274 *hdr++ = c | 0x80;
275 c = size & 0x7f;
276 size >>= 7;
277 n++;
279 *hdr = c;
280 return n;
284 * we are going to reuse the existing object data as is. make
285 * sure it is not corrupt.
287 static int check_pack_inflate(struct packed_git *p,
288 struct pack_window **w_curs,
289 off_t offset,
290 off_t len,
291 unsigned long expect)
293 z_stream stream;
294 unsigned char fakebuf[4096], *in;
295 int st;
297 memset(&stream, 0, sizeof(stream));
298 inflateInit(&stream);
299 do {
300 in = use_pack(p, w_curs, offset, &stream.avail_in);
301 stream.next_in = in;
302 stream.next_out = fakebuf;
303 stream.avail_out = sizeof(fakebuf);
304 st = inflate(&stream, Z_FINISH);
305 offset += stream.next_in - in;
306 } while (st == Z_OK || st == Z_BUF_ERROR);
307 inflateEnd(&stream);
308 return (st == Z_STREAM_END &&
309 stream.total_out == expect &&
310 stream.total_in == len) ? 0 : -1;
313 static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
314 off_t offset, off_t len, unsigned int nr)
316 const uint32_t *index_crc;
317 uint32_t data_crc = crc32(0, Z_NULL, 0);
319 do {
320 unsigned int avail;
321 void *data = use_pack(p, w_curs, offset, &avail);
322 if (avail > len)
323 avail = len;
324 data_crc = crc32(data_crc, data, avail);
325 offset += avail;
326 len -= avail;
327 } while (len);
329 index_crc = p->index_data;
330 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
332 return data_crc != ntohl(*index_crc);
335 static void copy_pack_data(struct sha1file *f,
336 struct packed_git *p,
337 struct pack_window **w_curs,
338 off_t offset,
339 off_t len)
341 unsigned char *in;
342 unsigned int avail;
344 while (len) {
345 in = use_pack(p, w_curs, offset, &avail);
346 if (avail > len)
347 avail = (unsigned int)len;
348 sha1write(f, in, avail);
349 offset += avail;
350 len -= avail;
354 static unsigned long write_object(struct sha1file *f,
355 struct object_entry *entry,
356 off_t write_offset)
358 unsigned long size;
359 enum object_type type;
360 void *buf;
361 unsigned char header[10];
362 unsigned char dheader[10];
363 unsigned hdrlen;
364 off_t datalen;
365 enum object_type obj_type;
366 int to_reuse = 0;
367 /* write limit if limited packsize and not first object */
368 unsigned long limit = pack_size_limit && nr_written ?
369 pack_size_limit - write_offset : 0;
370 /* no if no delta */
371 int usable_delta = !entry->delta ? 0 :
372 /* yes if unlimited packfile */
373 !pack_size_limit ? 1 :
374 /* no if base written to previous pack */
375 entry->delta->offset == (off_t)-1 ? 0 :
376 /* otherwise double-check written to this
377 * pack, like we do below
379 entry->delta->offset ? 1 : 0;
381 if (!pack_to_stdout)
382 crc32_begin(f);
384 obj_type = entry->type;
385 if (no_reuse_object)
386 to_reuse = 0; /* explicit */
387 else if (!entry->in_pack)
388 to_reuse = 0; /* can't reuse what we don't have */
389 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
390 /* check_object() decided it for us ... */
391 to_reuse = usable_delta;
392 /* ... but pack split may override that */
393 else if (obj_type != entry->in_pack_type)
394 to_reuse = 0; /* pack has delta which is unusable */
395 else if (entry->delta)
396 to_reuse = 0; /* we want to pack afresh */
397 else
398 to_reuse = 1; /* we have it in-pack undeltified,
399 * and we do not need to deltify it.
402 if (!to_reuse) {
403 z_stream stream;
404 unsigned long maxsize;
405 void *out;
406 buf = read_sha1_file(entry->sha1, &type, &size);
407 if (!buf)
408 die("unable to read %s", sha1_to_hex(entry->sha1));
409 if (size != entry->size)
410 die("object %s size inconsistency (%lu vs %lu)",
411 sha1_to_hex(entry->sha1), size, entry->size);
412 if (usable_delta) {
413 buf = delta_against(buf, size, entry);
414 size = entry->delta_size;
415 obj_type = (allow_ofs_delta && entry->delta->offset) ?
416 OBJ_OFS_DELTA : OBJ_REF_DELTA;
417 } else {
419 * recover real object type in case
420 * check_object() wanted to re-use a delta,
421 * but we couldn't since base was in previous split pack
423 obj_type = type;
425 /* compress the data to store and put compressed length in datalen */
426 memset(&stream, 0, sizeof(stream));
427 deflateInit(&stream, pack_compression_level);
428 maxsize = deflateBound(&stream, size);
429 out = xmalloc(maxsize);
430 /* Compress it */
431 stream.next_in = buf;
432 stream.avail_in = size;
433 stream.next_out = out;
434 stream.avail_out = maxsize;
435 while (deflate(&stream, Z_FINISH) == Z_OK)
436 /* nothing */;
437 deflateEnd(&stream);
438 datalen = stream.total_out;
439 deflateEnd(&stream);
441 * The object header is a byte of 'type' followed by zero or
442 * more bytes of length.
444 hdrlen = encode_header(obj_type, size, header);
446 if (obj_type == OBJ_OFS_DELTA) {
448 * Deltas with relative base contain an additional
449 * encoding of the relative offset for the delta
450 * base from this object's position in the pack.
452 off_t ofs = entry->offset - entry->delta->offset;
453 unsigned pos = sizeof(dheader) - 1;
454 dheader[pos] = ofs & 127;
455 while (ofs >>= 7)
456 dheader[--pos] = 128 | (--ofs & 127);
457 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
458 free(out);
459 free(buf);
460 return 0;
462 sha1write(f, header, hdrlen);
463 sha1write(f, dheader + pos, sizeof(dheader) - pos);
464 hdrlen += sizeof(dheader) - pos;
465 } else if (obj_type == OBJ_REF_DELTA) {
467 * Deltas with a base reference contain
468 * an additional 20 bytes for the base sha1.
470 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
471 free(out);
472 free(buf);
473 return 0;
475 sha1write(f, header, hdrlen);
476 sha1write(f, entry->delta->sha1, 20);
477 hdrlen += 20;
478 } else {
479 if (limit && hdrlen + datalen + 20 >= limit) {
480 free(out);
481 free(buf);
482 return 0;
484 sha1write(f, header, hdrlen);
486 sha1write(f, out, datalen);
487 free(out);
488 free(buf);
490 else {
491 struct packed_git *p = entry->in_pack;
492 struct pack_window *w_curs = NULL;
493 struct revindex_entry *revidx;
494 off_t offset;
496 if (entry->delta) {
497 obj_type = (allow_ofs_delta && entry->delta->offset) ?
498 OBJ_OFS_DELTA : OBJ_REF_DELTA;
499 reused_delta++;
501 hdrlen = encode_header(obj_type, entry->size, header);
502 offset = entry->in_pack_offset;
503 revidx = find_packed_object(p, offset);
504 datalen = revidx[1].offset - offset;
505 if (!pack_to_stdout && p->index_version > 1 &&
506 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
507 die("bad packed object CRC for %s", sha1_to_hex(entry->sha1));
508 offset += entry->in_pack_header_size;
509 datalen -= entry->in_pack_header_size;
510 if (obj_type == OBJ_OFS_DELTA) {
511 off_t ofs = entry->offset - entry->delta->offset;
512 unsigned pos = sizeof(dheader) - 1;
513 dheader[pos] = ofs & 127;
514 while (ofs >>= 7)
515 dheader[--pos] = 128 | (--ofs & 127);
516 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit)
517 return 0;
518 sha1write(f, header, hdrlen);
519 sha1write(f, dheader + pos, sizeof(dheader) - pos);
520 hdrlen += sizeof(dheader) - pos;
521 } else if (obj_type == OBJ_REF_DELTA) {
522 if (limit && hdrlen + 20 + datalen + 20 >= limit)
523 return 0;
524 sha1write(f, header, hdrlen);
525 sha1write(f, entry->delta->sha1, 20);
526 hdrlen += 20;
527 } else {
528 if (limit && hdrlen + datalen + 20 >= limit)
529 return 0;
530 sha1write(f, header, hdrlen);
533 if (!pack_to_stdout && p->index_version == 1 &&
534 check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
535 die("corrupt packed object for %s", sha1_to_hex(entry->sha1));
536 copy_pack_data(f, p, &w_curs, offset, datalen);
537 unuse_pack(&w_curs);
538 reused++;
540 if (usable_delta)
541 written_delta++;
542 written++;
543 if (!pack_to_stdout)
544 entry->crc32 = crc32_end(f);
545 return hdrlen + datalen;
548 static off_t write_one(struct sha1file *f,
549 struct object_entry *e,
550 off_t offset)
552 unsigned long size;
554 /* offset is non zero if object is written already. */
555 if (e->offset || e->preferred_base)
556 return offset;
558 /* if we are deltified, write out base object first. */
559 if (e->delta) {
560 offset = write_one(f, e->delta, offset);
561 if (!offset)
562 return 0;
565 e->offset = offset;
566 size = write_object(f, e, offset);
567 if (!size) {
568 e->offset = 0;
569 return 0;
571 written_list[nr_written++] = e;
573 /* make sure off_t is sufficiently large not to wrap */
574 if (offset > offset + size)
575 die("pack too large for current definition of off_t");
576 return offset + size;
579 static int open_object_dir_tmp(const char *path)
581 snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
582 return mkstemp(tmpname);
585 /* forward declarations for write_pack_file */
586 static void write_index_file(off_t last_obj_offset, unsigned char *sha1);
587 static int adjust_perm(const char *path, mode_t mode);
589 static void write_pack_file(void)
591 uint32_t i = 0, j;
592 struct sha1file *f;
593 off_t offset, offset_one, last_obj_offset = 0;
594 struct pack_header hdr;
595 int do_progress = progress >> pack_to_stdout;
596 uint32_t nr_remaining = nr_result;
598 if (do_progress)
599 start_progress(&progress_state, "Writing %u objects...", "", nr_result);
600 written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
602 do {
603 if (pack_to_stdout) {
604 f = sha1fd(1, "<stdout>");
605 } else {
606 int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
607 if (fd < 0)
608 die("unable to create %s: %s\n", tmpname, strerror(errno));
609 pack_tmp_name = xstrdup(tmpname);
610 f = sha1fd(fd, pack_tmp_name);
613 hdr.hdr_signature = htonl(PACK_SIGNATURE);
614 hdr.hdr_version = htonl(PACK_VERSION);
615 hdr.hdr_entries = htonl(nr_remaining);
616 sha1write(f, &hdr, sizeof(hdr));
617 offset = sizeof(hdr);
618 nr_written = 0;
619 for (; i < nr_objects; i++) {
620 last_obj_offset = offset;
621 offset_one = write_one(f, objects + i, offset);
622 if (!offset_one)
623 break;
624 offset = offset_one;
625 if (do_progress)
626 display_progress(&progress_state, written);
630 * Did we write the wrong # entries in the header?
631 * If so, rewrite it like in fast-import
633 if (pack_to_stdout || nr_written == nr_remaining) {
634 sha1close(f, pack_file_sha1, 1);
635 } else {
636 sha1close(f, pack_file_sha1, 0);
637 fixup_pack_header_footer(f->fd, pack_file_sha1, pack_tmp_name, nr_written);
638 close(f->fd);
641 if (!pack_to_stdout) {
642 unsigned char object_list_sha1[20];
643 mode_t mode = umask(0);
645 umask(mode);
646 mode = 0444 & ~mode;
648 write_index_file(last_obj_offset, object_list_sha1);
649 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
650 base_name, sha1_to_hex(object_list_sha1));
651 if (adjust_perm(pack_tmp_name, mode))
652 die("unable to make temporary pack file readable: %s",
653 strerror(errno));
654 if (rename(pack_tmp_name, tmpname))
655 die("unable to rename temporary pack file: %s",
656 strerror(errno));
657 snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
658 base_name, sha1_to_hex(object_list_sha1));
659 if (adjust_perm(idx_tmp_name, mode))
660 die("unable to make temporary index file readable: %s",
661 strerror(errno));
662 if (rename(idx_tmp_name, tmpname))
663 die("unable to rename temporary index file: %s",
664 strerror(errno));
665 puts(sha1_to_hex(object_list_sha1));
668 /* mark written objects as written to previous pack */
669 for (j = 0; j < nr_written; j++) {
670 written_list[j]->offset = (off_t)-1;
672 nr_remaining -= nr_written;
673 } while (nr_remaining && i < nr_objects);
675 free(written_list);
676 if (do_progress)
677 stop_progress(&progress_state);
678 if (written != nr_result)
679 die("wrote %u objects while expecting %u", written, nr_result);
681 * We have scanned through [0 ... i). Since we have written
682 * the correct number of objects, the remaining [i ... nr_objects)
683 * items must be either already written (due to out-of-order delta base)
684 * or a preferred base. Count those which are neither and complain if any.
686 for (j = 0; i < nr_objects; i++) {
687 struct object_entry *e = objects + i;
688 j += !e->offset && !e->preferred_base;
690 if (j)
691 die("wrote %u objects as expected but %u unwritten", written, j);
694 static int sha1_sort(const void *_a, const void *_b)
696 const struct object_entry *a = *(struct object_entry **)_a;
697 const struct object_entry *b = *(struct object_entry **)_b;
698 return hashcmp(a->sha1, b->sha1);
701 static uint32_t index_default_version = 1;
702 static uint32_t index_off32_limit = 0x7fffffff;
704 static void write_index_file(off_t last_obj_offset, unsigned char *sha1)
706 struct sha1file *f;
707 struct object_entry **sorted_by_sha, **list, **last;
708 uint32_t array[256];
709 uint32_t i, index_version;
710 SHA_CTX ctx;
712 int fd = open_object_dir_tmp("tmp_idx_XXXXXX");
713 if (fd < 0)
714 die("unable to create %s: %s\n", tmpname, strerror(errno));
715 idx_tmp_name = xstrdup(tmpname);
716 f = sha1fd(fd, idx_tmp_name);
718 if (nr_written) {
719 sorted_by_sha = written_list;
720 qsort(sorted_by_sha, nr_written, sizeof(*sorted_by_sha), sha1_sort);
721 list = sorted_by_sha;
722 last = sorted_by_sha + nr_written;
723 } else
724 sorted_by_sha = list = last = NULL;
726 /* if last object's offset is >= 2^31 we should use index V2 */
727 index_version = (last_obj_offset >> 31) ? 2 : index_default_version;
729 /* index versions 2 and above need a header */
730 if (index_version >= 2) {
731 struct pack_idx_header hdr;
732 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
733 hdr.idx_version = htonl(index_version);
734 sha1write(f, &hdr, sizeof(hdr));
738 * Write the first-level table (the list is sorted,
739 * but we use a 256-entry lookup to be able to avoid
740 * having to do eight extra binary search iterations).
742 for (i = 0; i < 256; i++) {
743 struct object_entry **next = list;
744 while (next < last) {
745 struct object_entry *entry = *next;
746 if (entry->sha1[0] != i)
747 break;
748 next++;
750 array[i] = htonl(next - sorted_by_sha);
751 list = next;
753 sha1write(f, array, 256 * 4);
755 /* Compute the SHA1 hash of sorted object names. */
756 SHA1_Init(&ctx);
758 /* Write the actual SHA1 entries. */
759 list = sorted_by_sha;
760 for (i = 0; i < nr_written; i++) {
761 struct object_entry *entry = *list++;
762 if (index_version < 2) {
763 uint32_t offset = htonl(entry->offset);
764 sha1write(f, &offset, 4);
766 sha1write(f, entry->sha1, 20);
767 SHA1_Update(&ctx, entry->sha1, 20);
770 if (index_version >= 2) {
771 unsigned int nr_large_offset = 0;
773 /* write the crc32 table */
774 list = sorted_by_sha;
775 for (i = 0; i < nr_written; i++) {
776 struct object_entry *entry = *list++;
777 uint32_t crc32_val = htonl(entry->crc32);
778 sha1write(f, &crc32_val, 4);
781 /* write the 32-bit offset table */
782 list = sorted_by_sha;
783 for (i = 0; i < nr_written; i++) {
784 struct object_entry *entry = *list++;
785 uint32_t offset = (entry->offset <= index_off32_limit) ?
786 entry->offset : (0x80000000 | nr_large_offset++);
787 offset = htonl(offset);
788 sha1write(f, &offset, 4);
791 /* write the large offset table */
792 list = sorted_by_sha;
793 while (nr_large_offset) {
794 struct object_entry *entry = *list++;
795 uint64_t offset = entry->offset;
796 if (offset > index_off32_limit) {
797 uint32_t split[2];
798 split[0] = htonl(offset >> 32);
799 split[1] = htonl(offset & 0xffffffff);
800 sha1write(f, split, 8);
801 nr_large_offset--;
806 sha1write(f, pack_file_sha1, 20);
807 sha1close(f, NULL, 1);
808 SHA1_Final(sha1, &ctx);
811 static int locate_object_entry_hash(const unsigned char *sha1)
813 int i;
814 unsigned int ui;
815 memcpy(&ui, sha1, sizeof(unsigned int));
816 i = ui % object_ix_hashsz;
817 while (0 < object_ix[i]) {
818 if (!hashcmp(sha1, objects[object_ix[i] - 1].sha1))
819 return i;
820 if (++i == object_ix_hashsz)
821 i = 0;
823 return -1 - i;
826 static struct object_entry *locate_object_entry(const unsigned char *sha1)
828 int i;
830 if (!object_ix_hashsz)
831 return NULL;
833 i = locate_object_entry_hash(sha1);
834 if (0 <= i)
835 return &objects[object_ix[i]-1];
836 return NULL;
839 static void rehash_objects(void)
841 uint32_t i;
842 struct object_entry *oe;
844 object_ix_hashsz = nr_objects * 3;
845 if (object_ix_hashsz < 1024)
846 object_ix_hashsz = 1024;
847 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
848 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
849 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
850 int ix = locate_object_entry_hash(oe->sha1);
851 if (0 <= ix)
852 continue;
853 ix = -1 - ix;
854 object_ix[ix] = i + 1;
858 static unsigned name_hash(const char *name)
860 unsigned char c;
861 unsigned hash = 0;
864 * This effectively just creates a sortable number from the
865 * last sixteen non-whitespace characters. Last characters
866 * count "most", so things that end in ".c" sort together.
868 while ((c = *name++) != 0) {
869 if (isspace(c))
870 continue;
871 hash = (hash >> 2) + (c << 24);
873 return hash;
876 static int add_object_entry(const unsigned char *sha1, enum object_type type,
877 unsigned hash, int exclude)
879 struct object_entry *entry;
880 struct packed_git *p, *found_pack = NULL;
881 off_t found_offset = 0;
882 int ix;
884 ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
885 if (ix >= 0) {
886 if (exclude) {
887 entry = objects + object_ix[ix] - 1;
888 if (!entry->preferred_base)
889 nr_result--;
890 entry->preferred_base = 1;
892 return 0;
895 for (p = packed_git; p; p = p->next) {
896 off_t offset = find_pack_entry_one(sha1, p);
897 if (offset) {
898 if (!found_pack) {
899 found_offset = offset;
900 found_pack = p;
902 if (exclude)
903 break;
904 if (incremental)
905 return 0;
906 if (local && !p->pack_local)
907 return 0;
911 if (nr_objects >= nr_alloc) {
912 nr_alloc = (nr_alloc + 1024) * 3 / 2;
913 objects = xrealloc(objects, nr_alloc * sizeof(*entry));
916 entry = objects + nr_objects++;
917 memset(entry, 0, sizeof(*entry));
918 hashcpy(entry->sha1, sha1);
919 entry->hash = hash;
920 if (type)
921 entry->type = type;
922 if (exclude)
923 entry->preferred_base = 1;
924 else
925 nr_result++;
926 if (found_pack) {
927 entry->in_pack = found_pack;
928 entry->in_pack_offset = found_offset;
931 if (object_ix_hashsz * 3 <= nr_objects * 4)
932 rehash_objects();
933 else
934 object_ix[-1 - ix] = nr_objects;
936 if (progress)
937 display_progress(&progress_state, nr_objects);
939 return 1;
942 struct pbase_tree_cache {
943 unsigned char sha1[20];
944 int ref;
945 int temporary;
946 void *tree_data;
947 unsigned long tree_size;
950 static struct pbase_tree_cache *(pbase_tree_cache[256]);
951 static int pbase_tree_cache_ix(const unsigned char *sha1)
953 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
955 static int pbase_tree_cache_ix_incr(int ix)
957 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
960 static struct pbase_tree {
961 struct pbase_tree *next;
962 /* This is a phony "cache" entry; we are not
963 * going to evict it nor find it through _get()
964 * mechanism -- this is for the toplevel node that
965 * would almost always change with any commit.
967 struct pbase_tree_cache pcache;
968 } *pbase_tree;
970 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
972 struct pbase_tree_cache *ent, *nent;
973 void *data;
974 unsigned long size;
975 enum object_type type;
976 int neigh;
977 int my_ix = pbase_tree_cache_ix(sha1);
978 int available_ix = -1;
980 /* pbase-tree-cache acts as a limited hashtable.
981 * your object will be found at your index or within a few
982 * slots after that slot if it is cached.
984 for (neigh = 0; neigh < 8; neigh++) {
985 ent = pbase_tree_cache[my_ix];
986 if (ent && !hashcmp(ent->sha1, sha1)) {
987 ent->ref++;
988 return ent;
990 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
991 ((0 <= available_ix) &&
992 (!ent && pbase_tree_cache[available_ix])))
993 available_ix = my_ix;
994 if (!ent)
995 break;
996 my_ix = pbase_tree_cache_ix_incr(my_ix);
999 /* Did not find one. Either we got a bogus request or
1000 * we need to read and perhaps cache.
1002 data = read_sha1_file(sha1, &type, &size);
1003 if (!data)
1004 return NULL;
1005 if (type != OBJ_TREE) {
1006 free(data);
1007 return NULL;
1010 /* We need to either cache or return a throwaway copy */
1012 if (available_ix < 0)
1013 ent = NULL;
1014 else {
1015 ent = pbase_tree_cache[available_ix];
1016 my_ix = available_ix;
1019 if (!ent) {
1020 nent = xmalloc(sizeof(*nent));
1021 nent->temporary = (available_ix < 0);
1023 else {
1024 /* evict and reuse */
1025 free(ent->tree_data);
1026 nent = ent;
1028 hashcpy(nent->sha1, sha1);
1029 nent->tree_data = data;
1030 nent->tree_size = size;
1031 nent->ref = 1;
1032 if (!nent->temporary)
1033 pbase_tree_cache[my_ix] = nent;
1034 return nent;
1037 static void pbase_tree_put(struct pbase_tree_cache *cache)
1039 if (!cache->temporary) {
1040 cache->ref--;
1041 return;
1043 free(cache->tree_data);
1044 free(cache);
1047 static int name_cmp_len(const char *name)
1049 int i;
1050 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1052 return i;
1055 static void add_pbase_object(struct tree_desc *tree,
1056 const char *name,
1057 int cmplen,
1058 const char *fullname)
1060 struct name_entry entry;
1061 int cmp;
1063 while (tree_entry(tree,&entry)) {
1064 cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
1065 memcmp(name, entry.path, cmplen);
1066 if (cmp > 0)
1067 continue;
1068 if (cmp < 0)
1069 return;
1070 if (name[cmplen] != '/') {
1071 unsigned hash = name_hash(fullname);
1072 add_object_entry(entry.sha1,
1073 S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB,
1074 hash, 1);
1075 return;
1077 if (S_ISDIR(entry.mode)) {
1078 struct tree_desc sub;
1079 struct pbase_tree_cache *tree;
1080 const char *down = name+cmplen+1;
1081 int downlen = name_cmp_len(down);
1083 tree = pbase_tree_get(entry.sha1);
1084 if (!tree)
1085 return;
1086 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1088 add_pbase_object(&sub, down, downlen, fullname);
1089 pbase_tree_put(tree);
1094 static unsigned *done_pbase_paths;
1095 static int done_pbase_paths_num;
1096 static int done_pbase_paths_alloc;
1097 static int done_pbase_path_pos(unsigned hash)
1099 int lo = 0;
1100 int hi = done_pbase_paths_num;
1101 while (lo < hi) {
1102 int mi = (hi + lo) / 2;
1103 if (done_pbase_paths[mi] == hash)
1104 return mi;
1105 if (done_pbase_paths[mi] < hash)
1106 hi = mi;
1107 else
1108 lo = mi + 1;
1110 return -lo-1;
1113 static int check_pbase_path(unsigned hash)
1115 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
1116 if (0 <= pos)
1117 return 1;
1118 pos = -pos - 1;
1119 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
1120 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
1121 done_pbase_paths = xrealloc(done_pbase_paths,
1122 done_pbase_paths_alloc *
1123 sizeof(unsigned));
1125 done_pbase_paths_num++;
1126 if (pos < done_pbase_paths_num)
1127 memmove(done_pbase_paths + pos + 1,
1128 done_pbase_paths + pos,
1129 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1130 done_pbase_paths[pos] = hash;
1131 return 0;
1134 static void add_preferred_base_object(const char *name, unsigned hash)
1136 struct pbase_tree *it;
1137 int cmplen;
1139 if (!num_preferred_base || check_pbase_path(hash))
1140 return;
1142 cmplen = name_cmp_len(name);
1143 for (it = pbase_tree; it; it = it->next) {
1144 if (cmplen == 0) {
1145 add_object_entry(it->pcache.sha1, OBJ_TREE, 0, 1);
1147 else {
1148 struct tree_desc tree;
1149 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1150 add_pbase_object(&tree, name, cmplen, name);
1155 static void add_preferred_base(unsigned char *sha1)
1157 struct pbase_tree *it;
1158 void *data;
1159 unsigned long size;
1160 unsigned char tree_sha1[20];
1162 if (window <= num_preferred_base++)
1163 return;
1165 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1166 if (!data)
1167 return;
1169 for (it = pbase_tree; it; it = it->next) {
1170 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
1171 free(data);
1172 return;
1176 it = xcalloc(1, sizeof(*it));
1177 it->next = pbase_tree;
1178 pbase_tree = it;
1180 hashcpy(it->pcache.sha1, tree_sha1);
1181 it->pcache.tree_data = data;
1182 it->pcache.tree_size = size;
1185 static void check_object(struct object_entry *entry)
1187 if (entry->in_pack) {
1188 struct packed_git *p = entry->in_pack;
1189 struct pack_window *w_curs = NULL;
1190 const unsigned char *base_ref = NULL;
1191 struct object_entry *base_entry;
1192 unsigned long used, used_0;
1193 unsigned int avail;
1194 off_t ofs;
1195 unsigned char *buf, c;
1197 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1200 * We want in_pack_type even if we do not reuse delta
1201 * since non-delta representations could still be reused.
1203 used = unpack_object_header_gently(buf, avail,
1204 &entry->in_pack_type,
1205 &entry->size);
1208 * Determine if this is a delta and if so whether we can
1209 * reuse it or not. Otherwise let's find out as cheaply as
1210 * possible what the actual type and size for this object is.
1212 switch (entry->in_pack_type) {
1213 default:
1214 /* Not a delta hence we've already got all we need. */
1215 entry->type = entry->in_pack_type;
1216 entry->in_pack_header_size = used;
1217 unuse_pack(&w_curs);
1218 return;
1219 case OBJ_REF_DELTA:
1220 if (!no_reuse_delta && !entry->preferred_base)
1221 base_ref = use_pack(p, &w_curs,
1222 entry->in_pack_offset + used, NULL);
1223 entry->in_pack_header_size = used + 20;
1224 break;
1225 case OBJ_OFS_DELTA:
1226 buf = use_pack(p, &w_curs,
1227 entry->in_pack_offset + used, NULL);
1228 used_0 = 0;
1229 c = buf[used_0++];
1230 ofs = c & 127;
1231 while (c & 128) {
1232 ofs += 1;
1233 if (!ofs || MSB(ofs, 7))
1234 die("delta base offset overflow in pack for %s",
1235 sha1_to_hex(entry->sha1));
1236 c = buf[used_0++];
1237 ofs = (ofs << 7) + (c & 127);
1239 if (ofs >= entry->in_pack_offset)
1240 die("delta base offset out of bound for %s",
1241 sha1_to_hex(entry->sha1));
1242 ofs = entry->in_pack_offset - ofs;
1243 if (!no_reuse_delta && !entry->preferred_base)
1244 base_ref = find_packed_object_name(p, ofs);
1245 entry->in_pack_header_size = used + used_0;
1246 break;
1249 if (base_ref && (base_entry = locate_object_entry(base_ref))) {
1251 * If base_ref was set above that means we wish to
1252 * reuse delta data, and we even found that base
1253 * in the list of objects we want to pack. Goodie!
1255 * Depth value does not matter - find_deltas() will
1256 * never consider reused delta as the base object to
1257 * deltify other objects against, in order to avoid
1258 * circular deltas.
1260 entry->type = entry->in_pack_type;
1261 entry->delta = base_entry;
1262 entry->delta_sibling = base_entry->delta_child;
1263 base_entry->delta_child = entry;
1264 unuse_pack(&w_curs);
1265 return;
1268 if (entry->type) {
1270 * This must be a delta and we already know what the
1271 * final object type is. Let's extract the actual
1272 * object size from the delta header.
1274 entry->size = get_size_from_delta(p, &w_curs,
1275 entry->in_pack_offset + entry->in_pack_header_size);
1276 unuse_pack(&w_curs);
1277 return;
1281 * No choice but to fall back to the recursive delta walk
1282 * with sha1_object_info() to find about the object type
1283 * at this point...
1285 unuse_pack(&w_curs);
1288 entry->type = sha1_object_info(entry->sha1, &entry->size);
1289 if (entry->type < 0)
1290 die("unable to get type of object %s",
1291 sha1_to_hex(entry->sha1));
1294 static int pack_offset_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 /* avoid filesystem trashing with loose objects */
1300 if (!a->in_pack && !b->in_pack)
1301 return hashcmp(a->sha1, b->sha1);
1303 if (a->in_pack < b->in_pack)
1304 return -1;
1305 if (a->in_pack > b->in_pack)
1306 return 1;
1307 return a->in_pack_offset < b->in_pack_offset ? -1 :
1308 (a->in_pack_offset > b->in_pack_offset);
1311 static void get_object_details(void)
1313 uint32_t i;
1314 struct object_entry **sorted_by_offset;
1316 sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));
1317 for (i = 0; i < nr_objects; i++)
1318 sorted_by_offset[i] = objects + i;
1319 qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
1321 prepare_pack_ix();
1322 for (i = 0; i < nr_objects; i++)
1323 check_object(sorted_by_offset[i]);
1324 free(sorted_by_offset);
1327 static int type_size_sort(const void *_a, const void *_b)
1329 const struct object_entry *a = *(struct object_entry **)_a;
1330 const struct object_entry *b = *(struct object_entry **)_b;
1332 if (a->type < b->type)
1333 return -1;
1334 if (a->type > b->type)
1335 return 1;
1336 if (a->hash < b->hash)
1337 return -1;
1338 if (a->hash > b->hash)
1339 return 1;
1340 if (a->preferred_base < b->preferred_base)
1341 return -1;
1342 if (a->preferred_base > b->preferred_base)
1343 return 1;
1344 if (a->size < b->size)
1345 return -1;
1346 if (a->size > b->size)
1347 return 1;
1348 return a > b ? -1 : (a < b); /* newest last */
1351 struct unpacked {
1352 struct object_entry *entry;
1353 void *data;
1354 struct delta_index *index;
1358 * We search for deltas _backwards_ in a list sorted by type and
1359 * by size, so that we see progressively smaller and smaller files.
1360 * That's because we prefer deltas to be from the bigger file
1361 * to the smaller - deletes are potentially cheaper, but perhaps
1362 * more importantly, the bigger file is likely the more recent
1363 * one.
1365 static int try_delta(struct unpacked *trg, struct unpacked *src,
1366 unsigned max_depth)
1368 struct object_entry *trg_entry = trg->entry;
1369 struct object_entry *src_entry = src->entry;
1370 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1371 enum object_type type;
1372 void *delta_buf;
1374 /* Don't bother doing diffs between different types */
1375 if (trg_entry->type != src_entry->type)
1376 return -1;
1378 /* We do not compute delta to *create* objects we are not
1379 * going to pack.
1381 if (trg_entry->preferred_base)
1382 return -1;
1385 * We do not bother to try a delta that we discarded
1386 * on an earlier try, but only when reusing delta data.
1388 if (!no_reuse_delta && trg_entry->in_pack &&
1389 trg_entry->in_pack == src_entry->in_pack &&
1390 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1391 trg_entry->in_pack_type != OBJ_OFS_DELTA)
1392 return 0;
1394 /* Let's not bust the allowed depth. */
1395 if (src_entry->depth >= max_depth)
1396 return 0;
1398 /* Now some size filtering heuristics. */
1399 trg_size = trg_entry->size;
1400 max_size = trg_size/2 - 20;
1401 max_size = max_size * (max_depth - src_entry->depth) / max_depth;
1402 if (max_size == 0)
1403 return 0;
1404 if (trg_entry->delta && trg_entry->delta_size <= max_size)
1405 max_size = trg_entry->delta_size-1;
1406 src_size = src_entry->size;
1407 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1408 if (sizediff >= max_size)
1409 return 0;
1411 /* Load data if not already done */
1412 if (!trg->data) {
1413 trg->data = read_sha1_file(trg_entry->sha1, &type, &sz);
1414 if (sz != trg_size)
1415 die("object %s inconsistent object length (%lu vs %lu)",
1416 sha1_to_hex(trg_entry->sha1), sz, trg_size);
1418 if (!src->data) {
1419 src->data = read_sha1_file(src_entry->sha1, &type, &sz);
1420 if (sz != src_size)
1421 die("object %s inconsistent object length (%lu vs %lu)",
1422 sha1_to_hex(src_entry->sha1), sz, src_size);
1424 if (!src->index) {
1425 src->index = create_delta_index(src->data, src_size);
1426 if (!src->index)
1427 die("out of memory");
1430 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1431 if (!delta_buf)
1432 return 0;
1434 trg_entry->delta = src_entry;
1435 trg_entry->delta_size = delta_size;
1436 trg_entry->depth = src_entry->depth + 1;
1437 free(delta_buf);
1438 return 1;
1441 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1443 struct object_entry *child = me->delta_child;
1444 unsigned int m = n;
1445 while (child) {
1446 unsigned int c = check_delta_limit(child, n + 1);
1447 if (m < c)
1448 m = c;
1449 child = child->delta_sibling;
1451 return m;
1454 static void find_deltas(struct object_entry **list, int window, int depth)
1456 uint32_t i = nr_objects, idx = 0, processed = 0;
1457 unsigned int array_size = window * sizeof(struct unpacked);
1458 struct unpacked *array;
1459 int max_depth;
1461 if (!nr_objects)
1462 return;
1463 array = xmalloc(array_size);
1464 memset(array, 0, array_size);
1465 if (progress)
1466 start_progress(&progress_state, "Deltifying %u objects...", "", nr_result);
1468 do {
1469 struct object_entry *entry = list[--i];
1470 struct unpacked *n = array + idx;
1471 int j;
1473 if (!entry->preferred_base)
1474 processed++;
1476 if (progress)
1477 display_progress(&progress_state, processed);
1479 if (entry->delta)
1480 /* This happens if we decided to reuse existing
1481 * delta from a pack. "!no_reuse_delta &&" is implied.
1483 continue;
1485 if (entry->size < 50)
1486 continue;
1487 free_delta_index(n->index);
1488 n->index = NULL;
1489 free(n->data);
1490 n->data = NULL;
1491 n->entry = entry;
1494 * If the current object is at pack edge, take the depth the
1495 * objects that depend on the current object into account
1496 * otherwise they would become too deep.
1498 max_depth = depth;
1499 if (entry->delta_child) {
1500 max_depth -= check_delta_limit(entry, 0);
1501 if (max_depth <= 0)
1502 goto next;
1505 j = window;
1506 while (--j > 0) {
1507 uint32_t other_idx = idx + j;
1508 struct unpacked *m;
1509 if (other_idx >= window)
1510 other_idx -= window;
1511 m = array + other_idx;
1512 if (!m->entry)
1513 break;
1514 if (try_delta(n, m, max_depth) < 0)
1515 break;
1518 /* if we made n a delta, and if n is already at max
1519 * depth, leaving it in the window is pointless. we
1520 * should evict it first.
1522 if (entry->delta && depth <= entry->depth)
1523 continue;
1525 next:
1526 idx++;
1527 if (idx >= window)
1528 idx = 0;
1529 } while (i > 0);
1531 if (progress)
1532 stop_progress(&progress_state);
1534 for (i = 0; i < window; ++i) {
1535 free_delta_index(array[i].index);
1536 free(array[i].data);
1538 free(array);
1541 static void prepare_pack(int window, int depth)
1543 struct object_entry **delta_list;
1544 uint32_t i;
1546 get_object_details();
1548 if (!window || !depth)
1549 return;
1551 delta_list = xmalloc(nr_objects * sizeof(*delta_list));
1552 for (i = 0; i < nr_objects; i++)
1553 delta_list[i] = objects + i;
1554 qsort(delta_list, nr_objects, sizeof(*delta_list), type_size_sort);
1555 find_deltas(delta_list, window+1, depth);
1556 free(delta_list);
1559 static int git_pack_config(const char *k, const char *v)
1561 if(!strcmp(k, "pack.window")) {
1562 window = git_config_int(k, v);
1563 return 0;
1565 if(!strcmp(k, "pack.depth")) {
1566 depth = git_config_int(k, v);
1567 return 0;
1569 if (!strcmp(k, "pack.compression")) {
1570 int level = git_config_int(k, v);
1571 if (level == -1)
1572 level = Z_DEFAULT_COMPRESSION;
1573 else if (level < 0 || level > Z_BEST_COMPRESSION)
1574 die("bad pack compression level %d", level);
1575 pack_compression_level = level;
1576 pack_compression_seen = 1;
1577 return 0;
1579 return git_default_config(k, v);
1582 static void read_object_list_from_stdin(void)
1584 char line[40 + 1 + PATH_MAX + 2];
1585 unsigned char sha1[20];
1586 unsigned hash;
1588 for (;;) {
1589 if (!fgets(line, sizeof(line), stdin)) {
1590 if (feof(stdin))
1591 break;
1592 if (!ferror(stdin))
1593 die("fgets returned NULL, not EOF, not error!");
1594 if (errno != EINTR)
1595 die("fgets: %s", strerror(errno));
1596 clearerr(stdin);
1597 continue;
1599 if (line[0] == '-') {
1600 if (get_sha1_hex(line+1, sha1))
1601 die("expected edge sha1, got garbage:\n %s",
1602 line);
1603 add_preferred_base(sha1);
1604 continue;
1606 if (get_sha1_hex(line, sha1))
1607 die("expected sha1, got garbage:\n %s", line);
1609 hash = name_hash(line+41);
1610 add_preferred_base_object(line+41, hash);
1611 add_object_entry(sha1, 0, hash, 0);
1615 static void show_commit(struct commit *commit)
1617 add_object_entry(commit->object.sha1, OBJ_COMMIT, 0, 0);
1620 static void show_object(struct object_array_entry *p)
1622 unsigned hash = name_hash(p->name);
1623 add_preferred_base_object(p->name, hash);
1624 add_object_entry(p->item->sha1, p->item->type, hash, 0);
1627 static void show_edge(struct commit *commit)
1629 add_preferred_base(commit->object.sha1);
1632 static void get_object_list(int ac, const char **av)
1634 struct rev_info revs;
1635 char line[1000];
1636 int flags = 0;
1638 init_revisions(&revs, NULL);
1639 save_commit_buffer = 0;
1640 track_object_refs = 0;
1641 setup_revisions(ac, av, &revs, NULL);
1643 while (fgets(line, sizeof(line), stdin) != NULL) {
1644 int len = strlen(line);
1645 if (line[len - 1] == '\n')
1646 line[--len] = 0;
1647 if (!len)
1648 break;
1649 if (*line == '-') {
1650 if (!strcmp(line, "--not")) {
1651 flags ^= UNINTERESTING;
1652 continue;
1654 die("not a rev '%s'", line);
1656 if (handle_revision_arg(line, &revs, flags, 1))
1657 die("bad revision '%s'", line);
1660 prepare_revision_walk(&revs);
1661 mark_edges_uninteresting(revs.commits, &revs, show_edge);
1662 traverse_commit_list(&revs, show_commit, show_object);
1665 static int adjust_perm(const char *path, mode_t mode)
1667 if (chmod(path, mode))
1668 return -1;
1669 return adjust_shared_perm(path);
1672 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1674 int use_internal_rev_list = 0;
1675 int thin = 0;
1676 uint32_t i;
1677 const char **rp_av;
1678 int rp_ac_alloc = 64;
1679 int rp_ac;
1681 rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
1683 rp_av[0] = "pack-objects";
1684 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1685 rp_ac = 2;
1687 git_config(git_pack_config);
1688 if (!pack_compression_seen && core_compression_seen)
1689 pack_compression_level = core_compression_level;
1691 progress = isatty(2);
1692 for (i = 1; i < argc; i++) {
1693 const char *arg = argv[i];
1695 if (*arg != '-')
1696 break;
1698 if (!strcmp("--non-empty", arg)) {
1699 non_empty = 1;
1700 continue;
1702 if (!strcmp("--local", arg)) {
1703 local = 1;
1704 continue;
1706 if (!strcmp("--incremental", arg)) {
1707 incremental = 1;
1708 continue;
1710 if (!prefixcmp(arg, "--compression=")) {
1711 char *end;
1712 int level = strtoul(arg+14, &end, 0);
1713 if (!arg[14] || *end)
1714 usage(pack_usage);
1715 if (level == -1)
1716 level = Z_DEFAULT_COMPRESSION;
1717 else if (level < 0 || level > Z_BEST_COMPRESSION)
1718 die("bad pack compression level %d", level);
1719 pack_compression_level = level;
1720 continue;
1722 if (!prefixcmp(arg, "--max-pack-size=")) {
1723 char *end;
1724 pack_size_limit = strtoul(arg+16, &end, 0) * 1024 * 1024;
1725 if (!arg[16] || *end)
1726 usage(pack_usage);
1727 continue;
1729 if (!prefixcmp(arg, "--window=")) {
1730 char *end;
1731 window = strtoul(arg+9, &end, 0);
1732 if (!arg[9] || *end)
1733 usage(pack_usage);
1734 continue;
1736 if (!prefixcmp(arg, "--depth=")) {
1737 char *end;
1738 depth = strtoul(arg+8, &end, 0);
1739 if (!arg[8] || *end)
1740 usage(pack_usage);
1741 continue;
1743 if (!strcmp("--progress", arg)) {
1744 progress = 1;
1745 continue;
1747 if (!strcmp("--all-progress", arg)) {
1748 progress = 2;
1749 continue;
1751 if (!strcmp("-q", arg)) {
1752 progress = 0;
1753 continue;
1755 if (!strcmp("--no-reuse-delta", arg)) {
1756 no_reuse_delta = 1;
1757 continue;
1759 if (!strcmp("--no-reuse-object", arg)) {
1760 no_reuse_object = no_reuse_delta = 1;
1761 continue;
1763 if (!strcmp("--delta-base-offset", arg)) {
1764 allow_ofs_delta = 1;
1765 continue;
1767 if (!strcmp("--stdout", arg)) {
1768 pack_to_stdout = 1;
1769 continue;
1771 if (!strcmp("--revs", arg)) {
1772 use_internal_rev_list = 1;
1773 continue;
1775 if (!strcmp("--unpacked", arg) ||
1776 !prefixcmp(arg, "--unpacked=") ||
1777 !strcmp("--reflog", arg) ||
1778 !strcmp("--all", arg)) {
1779 use_internal_rev_list = 1;
1780 if (rp_ac >= rp_ac_alloc - 1) {
1781 rp_ac_alloc = alloc_nr(rp_ac_alloc);
1782 rp_av = xrealloc(rp_av,
1783 rp_ac_alloc * sizeof(*rp_av));
1785 rp_av[rp_ac++] = arg;
1786 continue;
1788 if (!strcmp("--thin", arg)) {
1789 use_internal_rev_list = 1;
1790 thin = 1;
1791 rp_av[1] = "--objects-edge";
1792 continue;
1794 if (!prefixcmp(arg, "--index-version=")) {
1795 char *c;
1796 index_default_version = strtoul(arg + 16, &c, 10);
1797 if (index_default_version > 2)
1798 die("bad %s", arg);
1799 if (*c == ',')
1800 index_off32_limit = strtoul(c+1, &c, 0);
1801 if (*c || index_off32_limit & 0x80000000)
1802 die("bad %s", arg);
1803 continue;
1805 usage(pack_usage);
1808 /* Traditionally "pack-objects [options] base extra" failed;
1809 * we would however want to take refs parameter that would
1810 * have been given to upstream rev-list ourselves, which means
1811 * we somehow want to say what the base name is. So the
1812 * syntax would be:
1814 * pack-objects [options] base <refs...>
1816 * in other words, we would treat the first non-option as the
1817 * base_name and send everything else to the internal revision
1818 * walker.
1821 if (!pack_to_stdout)
1822 base_name = argv[i++];
1824 if (pack_to_stdout != !base_name)
1825 usage(pack_usage);
1827 if (pack_to_stdout && pack_size_limit)
1828 die("--max-pack-size cannot be used to build a pack for transfer.");
1830 if (!pack_to_stdout && thin)
1831 die("--thin cannot be used to build an indexable pack.");
1833 prepare_packed_git();
1835 if (progress)
1836 start_progress(&progress_state, "Generating pack...",
1837 "Counting objects: ", 0);
1838 if (!use_internal_rev_list)
1839 read_object_list_from_stdin();
1840 else {
1841 rp_av[rp_ac] = NULL;
1842 get_object_list(rp_ac, rp_av);
1844 if (progress) {
1845 stop_progress(&progress_state);
1846 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1849 if (non_empty && !nr_result)
1850 return 0;
1851 if (progress && (nr_objects != nr_result))
1852 fprintf(stderr, "Result has %u objects.\n", nr_result);
1853 if (nr_result)
1854 prepare_pack(window, depth);
1855 write_pack_file();
1856 if (progress)
1857 fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
1858 written, written_delta, reused, reused_delta);
1859 return 0;