Add handlers for fetch-side configuration of remotes.
[git/gitweb.git] / builtin-pack-objects.c
blobd165f10288558d55fee5e8a7ae88ac9fccfbc3d7
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 }] \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 uint32_t nr_objects, nr_alloc, nr_result;
57 static int non_empty;
58 static int no_reuse_delta, no_reuse_object;
59 static int local;
60 static int incremental;
61 static int allow_ofs_delta;
62 static const char *pack_tmp_name, *idx_tmp_name;
63 static char tmpname[PATH_MAX];
64 static unsigned char pack_file_sha1[20];
65 static int progress = 1;
66 static int window = 10;
67 static int depth = 50;
68 static int pack_to_stdout;
69 static int num_preferred_base;
70 static struct progress progress_state;
71 static int pack_compression_level = Z_DEFAULT_COMPRESSION;
72 static int pack_compression_seen;
75 * The object names in objects array are hashed with this hashtable,
76 * to help looking up the entry by object name.
77 * This hashtable is built after all the objects are seen.
79 static int *object_ix;
80 static int object_ix_hashsz;
83 * Pack index for existing packs give us easy access to the offsets into
84 * corresponding pack file where each object's data starts, but the entries
85 * do not store the size of the compressed representation (uncompressed
86 * size is easily available by examining the pack entry header). It is
87 * also rather expensive to find the sha1 for an object given its offset.
89 * We build a hashtable of existing packs (pack_revindex), and keep reverse
90 * index here -- pack index file is sorted by object name mapping to offset;
91 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
92 * ordered by offset, so if you know the offset of an object, next offset
93 * is where its packed representation ends and the index_nr can be used to
94 * get the object sha1 from the main index.
96 struct revindex_entry {
97 off_t offset;
98 unsigned int nr;
100 struct pack_revindex {
101 struct packed_git *p;
102 struct revindex_entry *revindex;
104 static struct pack_revindex *pack_revindex;
105 static int pack_revindex_hashsz;
108 * stats
110 static uint32_t written, written_delta;
111 static uint32_t reused, reused_delta;
113 static int pack_revindex_ix(struct packed_git *p)
115 unsigned long ui = (unsigned long)p;
116 int i;
118 ui = ui ^ (ui >> 16); /* defeat structure alignment */
119 i = (int)(ui % pack_revindex_hashsz);
120 while (pack_revindex[i].p) {
121 if (pack_revindex[i].p == p)
122 return i;
123 if (++i == pack_revindex_hashsz)
124 i = 0;
126 return -1 - i;
129 static void prepare_pack_ix(void)
131 int num;
132 struct packed_git *p;
133 for (num = 0, p = packed_git; p; p = p->next)
134 num++;
135 if (!num)
136 return;
137 pack_revindex_hashsz = num * 11;
138 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
139 for (p = packed_git; p; p = p->next) {
140 num = pack_revindex_ix(p);
141 num = - 1 - num;
142 pack_revindex[num].p = p;
144 /* revindex elements are lazily initialized */
147 static int cmp_offset(const void *a_, const void *b_)
149 const struct revindex_entry *a = a_;
150 const struct revindex_entry *b = b_;
151 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
155 * Ordered list of offsets of objects in the pack.
157 static void prepare_pack_revindex(struct pack_revindex *rix)
159 struct packed_git *p = rix->p;
160 int num_ent = p->num_objects;
161 int i;
162 const char *index = p->index_data;
164 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
165 index += 4 * 256;
167 if (p->index_version > 1) {
168 const uint32_t *off_32 =
169 (uint32_t *)(index + 8 + p->num_objects * (20 + 4));
170 const uint32_t *off_64 = off_32 + p->num_objects;
171 for (i = 0; i < num_ent; i++) {
172 uint32_t off = ntohl(*off_32++);
173 if (!(off & 0x80000000)) {
174 rix->revindex[i].offset = off;
175 } else {
176 rix->revindex[i].offset =
177 ((uint64_t)ntohl(*off_64++)) << 32;
178 rix->revindex[i].offset |=
179 ntohl(*off_64++);
181 rix->revindex[i].nr = i;
183 } else {
184 for (i = 0; i < num_ent; i++) {
185 uint32_t hl = *((uint32_t *)(index + 24 * i));
186 rix->revindex[i].offset = ntohl(hl);
187 rix->revindex[i].nr = i;
191 /* This knows the pack format -- the 20-byte trailer
192 * follows immediately after the last object data.
194 rix->revindex[num_ent].offset = p->pack_size - 20;
195 rix->revindex[num_ent].nr = -1;
196 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
199 static struct revindex_entry * find_packed_object(struct packed_git *p,
200 off_t ofs)
202 int num;
203 int lo, hi;
204 struct pack_revindex *rix;
205 struct revindex_entry *revindex;
206 num = pack_revindex_ix(p);
207 if (num < 0)
208 die("internal error: pack revindex uninitialized");
209 rix = &pack_revindex[num];
210 if (!rix->revindex)
211 prepare_pack_revindex(rix);
212 revindex = rix->revindex;
213 lo = 0;
214 hi = p->num_objects + 1;
215 do {
216 int mi = (lo + hi) / 2;
217 if (revindex[mi].offset == ofs) {
218 return revindex + mi;
220 else if (ofs < revindex[mi].offset)
221 hi = mi;
222 else
223 lo = mi + 1;
224 } while (lo < hi);
225 die("internal error: pack revindex corrupt");
228 static const unsigned char *find_packed_object_name(struct packed_git *p,
229 off_t ofs)
231 struct revindex_entry *entry = find_packed_object(p, ofs);
232 return nth_packed_object_sha1(p, entry->nr);
235 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
237 unsigned long othersize, delta_size;
238 enum object_type type;
239 void *otherbuf = read_sha1_file(entry->delta->sha1, &type, &othersize);
240 void *delta_buf;
242 if (!otherbuf)
243 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
244 delta_buf = diff_delta(otherbuf, othersize,
245 buf, size, &delta_size, 0);
246 if (!delta_buf || delta_size != entry->delta_size)
247 die("delta size changed");
248 free(buf);
249 free(otherbuf);
250 return delta_buf;
254 * The per-object header is a pretty dense thing, which is
255 * - first byte: low four bits are "size", then three bits of "type",
256 * and the high bit is "size continues".
257 * - each byte afterwards: low seven bits are size continuation,
258 * with the high bit being "size continues"
260 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
262 int n = 1;
263 unsigned char c;
265 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
266 die("bad type %d", type);
268 c = (type << 4) | (size & 15);
269 size >>= 4;
270 while (size) {
271 *hdr++ = c | 0x80;
272 c = size & 0x7f;
273 size >>= 7;
274 n++;
276 *hdr = c;
277 return n;
281 * we are going to reuse the existing object data as is. make
282 * sure it is not corrupt.
284 static int check_pack_inflate(struct packed_git *p,
285 struct pack_window **w_curs,
286 off_t offset,
287 off_t len,
288 unsigned long expect)
290 z_stream stream;
291 unsigned char fakebuf[4096], *in;
292 int st;
294 memset(&stream, 0, sizeof(stream));
295 inflateInit(&stream);
296 do {
297 in = use_pack(p, w_curs, offset, &stream.avail_in);
298 stream.next_in = in;
299 stream.next_out = fakebuf;
300 stream.avail_out = sizeof(fakebuf);
301 st = inflate(&stream, Z_FINISH);
302 offset += stream.next_in - in;
303 } while (st == Z_OK || st == Z_BUF_ERROR);
304 inflateEnd(&stream);
305 return (st == Z_STREAM_END &&
306 stream.total_out == expect &&
307 stream.total_in == len) ? 0 : -1;
310 static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
311 off_t offset, off_t len, unsigned int nr)
313 const uint32_t *index_crc;
314 uint32_t data_crc = crc32(0, Z_NULL, 0);
316 do {
317 unsigned int avail;
318 void *data = use_pack(p, w_curs, offset, &avail);
319 if (avail > len)
320 avail = len;
321 data_crc = crc32(data_crc, data, avail);
322 offset += avail;
323 len -= avail;
324 } while (len);
326 index_crc = p->index_data;
327 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
329 return data_crc != ntohl(*index_crc);
332 static void copy_pack_data(struct sha1file *f,
333 struct packed_git *p,
334 struct pack_window **w_curs,
335 off_t offset,
336 off_t len)
338 unsigned char *in;
339 unsigned int avail;
341 while (len) {
342 in = use_pack(p, w_curs, offset, &avail);
343 if (avail > len)
344 avail = (unsigned int)len;
345 sha1write(f, in, avail);
346 offset += avail;
347 len -= avail;
351 static unsigned long write_object(struct sha1file *f,
352 struct object_entry *entry)
354 unsigned long size;
355 enum object_type type;
356 void *buf;
357 unsigned char header[10];
358 unsigned hdrlen;
359 off_t datalen;
360 enum object_type obj_type;
361 int to_reuse = 0;
363 if (!pack_to_stdout)
364 crc32_begin(f);
366 obj_type = entry->type;
367 if (no_reuse_object)
368 to_reuse = 0; /* explicit */
369 else if (!entry->in_pack)
370 to_reuse = 0; /* can't reuse what we don't have */
371 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
372 to_reuse = 1; /* check_object() decided it for us */
373 else if (obj_type != entry->in_pack_type)
374 to_reuse = 0; /* pack has delta which is unusable */
375 else if (entry->delta)
376 to_reuse = 0; /* we want to pack afresh */
377 else
378 to_reuse = 1; /* we have it in-pack undeltified,
379 * and we do not need to deltify it.
382 if (!to_reuse) {
383 buf = read_sha1_file(entry->sha1, &type, &size);
384 if (!buf)
385 die("unable to read %s", sha1_to_hex(entry->sha1));
386 if (size != entry->size)
387 die("object %s size inconsistency (%lu vs %lu)",
388 sha1_to_hex(entry->sha1), size, entry->size);
389 if (entry->delta) {
390 buf = delta_against(buf, size, entry);
391 size = entry->delta_size;
392 obj_type = (allow_ofs_delta && entry->delta->offset) ?
393 OBJ_OFS_DELTA : OBJ_REF_DELTA;
396 * The object header is a byte of 'type' followed by zero or
397 * more bytes of length.
399 hdrlen = encode_header(obj_type, size, header);
400 sha1write(f, header, hdrlen);
402 if (obj_type == OBJ_OFS_DELTA) {
404 * Deltas with relative base contain an additional
405 * encoding of the relative offset for the delta
406 * base from this object's position in the pack.
408 off_t ofs = entry->offset - entry->delta->offset;
409 unsigned pos = sizeof(header) - 1;
410 header[pos] = ofs & 127;
411 while (ofs >>= 7)
412 header[--pos] = 128 | (--ofs & 127);
413 sha1write(f, header + pos, sizeof(header) - pos);
414 hdrlen += sizeof(header) - pos;
415 } else if (obj_type == OBJ_REF_DELTA) {
417 * Deltas with a base reference contain
418 * an additional 20 bytes for the base sha1.
420 sha1write(f, entry->delta->sha1, 20);
421 hdrlen += 20;
423 datalen = sha1write_compressed(f, buf, size, pack_compression_level);
424 free(buf);
426 else {
427 struct packed_git *p = entry->in_pack;
428 struct pack_window *w_curs = NULL;
429 struct revindex_entry *revidx;
430 off_t offset;
432 if (entry->delta) {
433 obj_type = (allow_ofs_delta && entry->delta->offset) ?
434 OBJ_OFS_DELTA : OBJ_REF_DELTA;
435 reused_delta++;
437 hdrlen = encode_header(obj_type, entry->size, header);
438 sha1write(f, header, hdrlen);
439 if (obj_type == OBJ_OFS_DELTA) {
440 off_t ofs = entry->offset - entry->delta->offset;
441 unsigned pos = sizeof(header) - 1;
442 header[pos] = ofs & 127;
443 while (ofs >>= 7)
444 header[--pos] = 128 | (--ofs & 127);
445 sha1write(f, header + pos, sizeof(header) - pos);
446 hdrlen += sizeof(header) - pos;
447 } else if (obj_type == OBJ_REF_DELTA) {
448 sha1write(f, entry->delta->sha1, 20);
449 hdrlen += 20;
452 offset = entry->in_pack_offset;
453 revidx = find_packed_object(p, offset);
454 datalen = revidx[1].offset - offset;
455 if (!pack_to_stdout && p->index_version > 1 &&
456 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
457 die("bad packed object CRC for %s", sha1_to_hex(entry->sha1));
458 offset += entry->in_pack_header_size;
459 datalen -= entry->in_pack_header_size;
460 if (!pack_to_stdout && p->index_version == 1 &&
461 check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
462 die("corrupt packed object for %s", sha1_to_hex(entry->sha1));
463 copy_pack_data(f, p, &w_curs, offset, datalen);
464 unuse_pack(&w_curs);
465 reused++;
467 if (entry->delta)
468 written_delta++;
469 written++;
470 if (!pack_to_stdout)
471 entry->crc32 = crc32_end(f);
472 return hdrlen + datalen;
475 static off_t write_one(struct sha1file *f,
476 struct object_entry *e,
477 off_t offset)
479 unsigned long size;
481 /* offset is non zero if object is written already. */
482 if (e->offset || e->preferred_base)
483 return offset;
485 /* if we are deltified, write out base object first. */
486 if (e->delta)
487 offset = write_one(f, e->delta, offset);
489 e->offset = offset;
490 size = write_object(f, e);
492 /* make sure off_t is sufficiently large not to wrap */
493 if (offset > offset + size)
494 die("pack too large for current definition of off_t");
495 return offset + size;
498 static int open_object_dir_tmp(const char *path)
500 snprintf(tmpname, sizeof(tmpname), "%s/%s", get_object_directory(), path);
501 return mkstemp(tmpname);
504 static off_t write_pack_file(void)
506 uint32_t i;
507 struct sha1file *f;
508 off_t offset, last_obj_offset = 0;
509 struct pack_header hdr;
510 int do_progress = progress;
512 if (pack_to_stdout) {
513 f = sha1fd(1, "<stdout>");
514 do_progress >>= 1;
515 } else {
516 int fd = open_object_dir_tmp("tmp_pack_XXXXXX");
517 if (fd < 0)
518 die("unable to create %s: %s\n", tmpname, strerror(errno));
519 pack_tmp_name = xstrdup(tmpname);
520 f = sha1fd(fd, pack_tmp_name);
523 if (do_progress)
524 start_progress(&progress_state, "Writing %u objects...", "", nr_result);
526 hdr.hdr_signature = htonl(PACK_SIGNATURE);
527 hdr.hdr_version = htonl(PACK_VERSION);
528 hdr.hdr_entries = htonl(nr_result);
529 sha1write(f, &hdr, sizeof(hdr));
530 offset = sizeof(hdr);
531 if (!nr_result)
532 goto done;
533 for (i = 0; i < nr_objects; i++) {
534 last_obj_offset = offset;
535 offset = write_one(f, objects + i, offset);
536 if (do_progress)
537 display_progress(&progress_state, written);
539 if (do_progress)
540 stop_progress(&progress_state);
541 done:
542 if (written != nr_result)
543 die("wrote %u objects while expecting %u", written, nr_result);
544 sha1close(f, pack_file_sha1, 1);
546 return last_obj_offset;
549 static int sha1_sort(const void *_a, const void *_b)
551 const struct object_entry *a = *(struct object_entry **)_a;
552 const struct object_entry *b = *(struct object_entry **)_b;
553 return hashcmp(a->sha1, b->sha1);
556 static uint32_t index_default_version = 1;
557 static uint32_t index_off32_limit = 0x7fffffff;
559 static void write_index_file(off_t last_obj_offset, unsigned char *sha1)
561 struct sha1file *f;
562 struct object_entry **sorted_by_sha, **list, **last;
563 uint32_t array[256];
564 uint32_t i, index_version;
565 SHA_CTX ctx;
567 int fd = open_object_dir_tmp("tmp_idx_XXXXXX");
568 if (fd < 0)
569 die("unable to create %s: %s\n", tmpname, strerror(errno));
570 idx_tmp_name = xstrdup(tmpname);
571 f = sha1fd(fd, idx_tmp_name);
573 if (nr_result) {
574 uint32_t j = 0;
575 sorted_by_sha =
576 xcalloc(nr_result, sizeof(struct object_entry *));
577 for (i = 0; i < nr_objects; i++)
578 if (!objects[i].preferred_base)
579 sorted_by_sha[j++] = objects + i;
580 if (j != nr_result)
581 die("listed %u objects while expecting %u", j, nr_result);
582 qsort(sorted_by_sha, nr_result, sizeof(*sorted_by_sha), sha1_sort);
583 list = sorted_by_sha;
584 last = sorted_by_sha + nr_result;
585 } else
586 sorted_by_sha = list = last = NULL;
588 /* if last object's offset is >= 2^31 we should use index V2 */
589 index_version = (last_obj_offset >> 31) ? 2 : index_default_version;
591 /* index versions 2 and above need a header */
592 if (index_version >= 2) {
593 struct pack_idx_header hdr;
594 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
595 hdr.idx_version = htonl(index_version);
596 sha1write(f, &hdr, sizeof(hdr));
600 * Write the first-level table (the list is sorted,
601 * but we use a 256-entry lookup to be able to avoid
602 * having to do eight extra binary search iterations).
604 for (i = 0; i < 256; i++) {
605 struct object_entry **next = list;
606 while (next < last) {
607 struct object_entry *entry = *next;
608 if (entry->sha1[0] != i)
609 break;
610 next++;
612 array[i] = htonl(next - sorted_by_sha);
613 list = next;
615 sha1write(f, array, 256 * 4);
617 /* Compute the SHA1 hash of sorted object names. */
618 SHA1_Init(&ctx);
620 /* Write the actual SHA1 entries. */
621 list = sorted_by_sha;
622 for (i = 0; i < nr_result; i++) {
623 struct object_entry *entry = *list++;
624 if (index_version < 2) {
625 uint32_t offset = htonl(entry->offset);
626 sha1write(f, &offset, 4);
628 sha1write(f, entry->sha1, 20);
629 SHA1_Update(&ctx, entry->sha1, 20);
632 if (index_version >= 2) {
633 unsigned int nr_large_offset = 0;
635 /* write the crc32 table */
636 list = sorted_by_sha;
637 for (i = 0; i < nr_objects; i++) {
638 struct object_entry *entry = *list++;
639 uint32_t crc32_val = htonl(entry->crc32);
640 sha1write(f, &crc32_val, 4);
643 /* write the 32-bit offset table */
644 list = sorted_by_sha;
645 for (i = 0; i < nr_objects; i++) {
646 struct object_entry *entry = *list++;
647 uint32_t offset = (entry->offset <= index_off32_limit) ?
648 entry->offset : (0x80000000 | nr_large_offset++);
649 offset = htonl(offset);
650 sha1write(f, &offset, 4);
653 /* write the large offset table */
654 list = sorted_by_sha;
655 while (nr_large_offset) {
656 struct object_entry *entry = *list++;
657 uint64_t offset = entry->offset;
658 if (offset > index_off32_limit) {
659 uint32_t split[2];
660 split[0] = htonl(offset >> 32);
661 split[1] = htonl(offset & 0xffffffff);
662 sha1write(f, split, 8);
663 nr_large_offset--;
668 sha1write(f, pack_file_sha1, 20);
669 sha1close(f, NULL, 1);
670 free(sorted_by_sha);
671 SHA1_Final(sha1, &ctx);
674 static int locate_object_entry_hash(const unsigned char *sha1)
676 int i;
677 unsigned int ui;
678 memcpy(&ui, sha1, sizeof(unsigned int));
679 i = ui % object_ix_hashsz;
680 while (0 < object_ix[i]) {
681 if (!hashcmp(sha1, objects[object_ix[i] - 1].sha1))
682 return i;
683 if (++i == object_ix_hashsz)
684 i = 0;
686 return -1 - i;
689 static struct object_entry *locate_object_entry(const unsigned char *sha1)
691 int i;
693 if (!object_ix_hashsz)
694 return NULL;
696 i = locate_object_entry_hash(sha1);
697 if (0 <= i)
698 return &objects[object_ix[i]-1];
699 return NULL;
702 static void rehash_objects(void)
704 uint32_t i;
705 struct object_entry *oe;
707 object_ix_hashsz = nr_objects * 3;
708 if (object_ix_hashsz < 1024)
709 object_ix_hashsz = 1024;
710 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
711 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
712 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
713 int ix = locate_object_entry_hash(oe->sha1);
714 if (0 <= ix)
715 continue;
716 ix = -1 - ix;
717 object_ix[ix] = i + 1;
721 static unsigned name_hash(const char *name)
723 unsigned char c;
724 unsigned hash = 0;
727 * This effectively just creates a sortable number from the
728 * last sixteen non-whitespace characters. Last characters
729 * count "most", so things that end in ".c" sort together.
731 while ((c = *name++) != 0) {
732 if (isspace(c))
733 continue;
734 hash = (hash >> 2) + (c << 24);
736 return hash;
739 static int add_object_entry(const unsigned char *sha1, enum object_type type,
740 unsigned hash, int exclude)
742 struct object_entry *entry;
743 struct packed_git *p, *found_pack = NULL;
744 off_t found_offset = 0;
745 int ix;
747 ix = nr_objects ? locate_object_entry_hash(sha1) : -1;
748 if (ix >= 0) {
749 if (exclude) {
750 entry = objects + object_ix[ix] - 1;
751 if (!entry->preferred_base)
752 nr_result--;
753 entry->preferred_base = 1;
755 return 0;
758 for (p = packed_git; p; p = p->next) {
759 off_t offset = find_pack_entry_one(sha1, p);
760 if (offset) {
761 if (!found_pack) {
762 found_offset = offset;
763 found_pack = p;
765 if (exclude)
766 break;
767 if (incremental)
768 return 0;
769 if (local && !p->pack_local)
770 return 0;
774 if (nr_objects >= nr_alloc) {
775 nr_alloc = (nr_alloc + 1024) * 3 / 2;
776 objects = xrealloc(objects, nr_alloc * sizeof(*entry));
779 entry = objects + nr_objects++;
780 memset(entry, 0, sizeof(*entry));
781 hashcpy(entry->sha1, sha1);
782 entry->hash = hash;
783 if (type)
784 entry->type = type;
785 if (exclude)
786 entry->preferred_base = 1;
787 else
788 nr_result++;
789 if (found_pack) {
790 entry->in_pack = found_pack;
791 entry->in_pack_offset = found_offset;
794 if (object_ix_hashsz * 3 <= nr_objects * 4)
795 rehash_objects();
796 else
797 object_ix[-1 - ix] = nr_objects;
799 if (progress)
800 display_progress(&progress_state, nr_objects);
802 return 1;
805 struct pbase_tree_cache {
806 unsigned char sha1[20];
807 int ref;
808 int temporary;
809 void *tree_data;
810 unsigned long tree_size;
813 static struct pbase_tree_cache *(pbase_tree_cache[256]);
814 static int pbase_tree_cache_ix(const unsigned char *sha1)
816 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
818 static int pbase_tree_cache_ix_incr(int ix)
820 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
823 static struct pbase_tree {
824 struct pbase_tree *next;
825 /* This is a phony "cache" entry; we are not
826 * going to evict it nor find it through _get()
827 * mechanism -- this is for the toplevel node that
828 * would almost always change with any commit.
830 struct pbase_tree_cache pcache;
831 } *pbase_tree;
833 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
835 struct pbase_tree_cache *ent, *nent;
836 void *data;
837 unsigned long size;
838 enum object_type type;
839 int neigh;
840 int my_ix = pbase_tree_cache_ix(sha1);
841 int available_ix = -1;
843 /* pbase-tree-cache acts as a limited hashtable.
844 * your object will be found at your index or within a few
845 * slots after that slot if it is cached.
847 for (neigh = 0; neigh < 8; neigh++) {
848 ent = pbase_tree_cache[my_ix];
849 if (ent && !hashcmp(ent->sha1, sha1)) {
850 ent->ref++;
851 return ent;
853 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
854 ((0 <= available_ix) &&
855 (!ent && pbase_tree_cache[available_ix])))
856 available_ix = my_ix;
857 if (!ent)
858 break;
859 my_ix = pbase_tree_cache_ix_incr(my_ix);
862 /* Did not find one. Either we got a bogus request or
863 * we need to read and perhaps cache.
865 data = read_sha1_file(sha1, &type, &size);
866 if (!data)
867 return NULL;
868 if (type != OBJ_TREE) {
869 free(data);
870 return NULL;
873 /* We need to either cache or return a throwaway copy */
875 if (available_ix < 0)
876 ent = NULL;
877 else {
878 ent = pbase_tree_cache[available_ix];
879 my_ix = available_ix;
882 if (!ent) {
883 nent = xmalloc(sizeof(*nent));
884 nent->temporary = (available_ix < 0);
886 else {
887 /* evict and reuse */
888 free(ent->tree_data);
889 nent = ent;
891 hashcpy(nent->sha1, sha1);
892 nent->tree_data = data;
893 nent->tree_size = size;
894 nent->ref = 1;
895 if (!nent->temporary)
896 pbase_tree_cache[my_ix] = nent;
897 return nent;
900 static void pbase_tree_put(struct pbase_tree_cache *cache)
902 if (!cache->temporary) {
903 cache->ref--;
904 return;
906 free(cache->tree_data);
907 free(cache);
910 static int name_cmp_len(const char *name)
912 int i;
913 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
915 return i;
918 static void add_pbase_object(struct tree_desc *tree,
919 const char *name,
920 int cmplen,
921 const char *fullname)
923 struct name_entry entry;
924 int cmp;
926 while (tree_entry(tree,&entry)) {
927 cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
928 memcmp(name, entry.path, cmplen);
929 if (cmp > 0)
930 continue;
931 if (cmp < 0)
932 return;
933 if (name[cmplen] != '/') {
934 unsigned hash = name_hash(fullname);
935 add_object_entry(entry.sha1,
936 S_ISDIR(entry.mode) ? OBJ_TREE : OBJ_BLOB,
937 hash, 1);
938 return;
940 if (S_ISDIR(entry.mode)) {
941 struct tree_desc sub;
942 struct pbase_tree_cache *tree;
943 const char *down = name+cmplen+1;
944 int downlen = name_cmp_len(down);
946 tree = pbase_tree_get(entry.sha1);
947 if (!tree)
948 return;
949 init_tree_desc(&sub, tree->tree_data, tree->tree_size);
951 add_pbase_object(&sub, down, downlen, fullname);
952 pbase_tree_put(tree);
957 static unsigned *done_pbase_paths;
958 static int done_pbase_paths_num;
959 static int done_pbase_paths_alloc;
960 static int done_pbase_path_pos(unsigned hash)
962 int lo = 0;
963 int hi = done_pbase_paths_num;
964 while (lo < hi) {
965 int mi = (hi + lo) / 2;
966 if (done_pbase_paths[mi] == hash)
967 return mi;
968 if (done_pbase_paths[mi] < hash)
969 hi = mi;
970 else
971 lo = mi + 1;
973 return -lo-1;
976 static int check_pbase_path(unsigned hash)
978 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
979 if (0 <= pos)
980 return 1;
981 pos = -pos - 1;
982 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
983 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
984 done_pbase_paths = xrealloc(done_pbase_paths,
985 done_pbase_paths_alloc *
986 sizeof(unsigned));
988 done_pbase_paths_num++;
989 if (pos < done_pbase_paths_num)
990 memmove(done_pbase_paths + pos + 1,
991 done_pbase_paths + pos,
992 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
993 done_pbase_paths[pos] = hash;
994 return 0;
997 static void add_preferred_base_object(const char *name, unsigned hash)
999 struct pbase_tree *it;
1000 int cmplen;
1002 if (!num_preferred_base || check_pbase_path(hash))
1003 return;
1005 cmplen = name_cmp_len(name);
1006 for (it = pbase_tree; it; it = it->next) {
1007 if (cmplen == 0) {
1008 add_object_entry(it->pcache.sha1, OBJ_TREE, 0, 1);
1010 else {
1011 struct tree_desc tree;
1012 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1013 add_pbase_object(&tree, name, cmplen, name);
1018 static void add_preferred_base(unsigned char *sha1)
1020 struct pbase_tree *it;
1021 void *data;
1022 unsigned long size;
1023 unsigned char tree_sha1[20];
1025 if (window <= num_preferred_base++)
1026 return;
1028 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1029 if (!data)
1030 return;
1032 for (it = pbase_tree; it; it = it->next) {
1033 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
1034 free(data);
1035 return;
1039 it = xcalloc(1, sizeof(*it));
1040 it->next = pbase_tree;
1041 pbase_tree = it;
1043 hashcpy(it->pcache.sha1, tree_sha1);
1044 it->pcache.tree_data = data;
1045 it->pcache.tree_size = size;
1048 static void check_object(struct object_entry *entry)
1050 if (entry->in_pack) {
1051 struct packed_git *p = entry->in_pack;
1052 struct pack_window *w_curs = NULL;
1053 const unsigned char *base_ref = NULL;
1054 struct object_entry *base_entry;
1055 unsigned long used, used_0;
1056 unsigned int avail;
1057 off_t ofs;
1058 unsigned char *buf, c;
1060 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1063 * We want in_pack_type even if we do not reuse delta
1064 * since non-delta representations could still be reused.
1066 used = unpack_object_header_gently(buf, avail,
1067 &entry->in_pack_type,
1068 &entry->size);
1071 * Determine if this is a delta and if so whether we can
1072 * reuse it or not. Otherwise let's find out as cheaply as
1073 * possible what the actual type and size for this object is.
1075 switch (entry->in_pack_type) {
1076 default:
1077 /* Not a delta hence we've already got all we need. */
1078 entry->type = entry->in_pack_type;
1079 entry->in_pack_header_size = used;
1080 unuse_pack(&w_curs);
1081 return;
1082 case OBJ_REF_DELTA:
1083 if (!no_reuse_delta && !entry->preferred_base)
1084 base_ref = use_pack(p, &w_curs,
1085 entry->in_pack_offset + used, NULL);
1086 entry->in_pack_header_size = used + 20;
1087 break;
1088 case OBJ_OFS_DELTA:
1089 buf = use_pack(p, &w_curs,
1090 entry->in_pack_offset + used, NULL);
1091 used_0 = 0;
1092 c = buf[used_0++];
1093 ofs = c & 127;
1094 while (c & 128) {
1095 ofs += 1;
1096 if (!ofs || MSB(ofs, 7))
1097 die("delta base offset overflow in pack for %s",
1098 sha1_to_hex(entry->sha1));
1099 c = buf[used_0++];
1100 ofs = (ofs << 7) + (c & 127);
1102 if (ofs >= entry->in_pack_offset)
1103 die("delta base offset out of bound for %s",
1104 sha1_to_hex(entry->sha1));
1105 ofs = entry->in_pack_offset - ofs;
1106 if (!no_reuse_delta && !entry->preferred_base)
1107 base_ref = find_packed_object_name(p, ofs);
1108 entry->in_pack_header_size = used + used_0;
1109 break;
1112 if (base_ref && (base_entry = locate_object_entry(base_ref))) {
1114 * If base_ref was set above that means we wish to
1115 * reuse delta data, and we even found that base
1116 * in the list of objects we want to pack. Goodie!
1118 * Depth value does not matter - find_deltas() will
1119 * never consider reused delta as the base object to
1120 * deltify other objects against, in order to avoid
1121 * circular deltas.
1123 entry->type = entry->in_pack_type;
1124 entry->delta = base_entry;
1125 entry->delta_sibling = base_entry->delta_child;
1126 base_entry->delta_child = entry;
1127 unuse_pack(&w_curs);
1128 return;
1131 if (entry->type) {
1133 * This must be a delta and we already know what the
1134 * final object type is. Let's extract the actual
1135 * object size from the delta header.
1137 entry->size = get_size_from_delta(p, &w_curs,
1138 entry->in_pack_offset + entry->in_pack_header_size);
1139 unuse_pack(&w_curs);
1140 return;
1144 * No choice but to fall back to the recursive delta walk
1145 * with sha1_object_info() to find about the object type
1146 * at this point...
1148 unuse_pack(&w_curs);
1151 entry->type = sha1_object_info(entry->sha1, &entry->size);
1152 if (entry->type < 0)
1153 die("unable to get type of object %s",
1154 sha1_to_hex(entry->sha1));
1157 static int pack_offset_sort(const void *_a, const void *_b)
1159 const struct object_entry *a = *(struct object_entry **)_a;
1160 const struct object_entry *b = *(struct object_entry **)_b;
1162 /* avoid filesystem trashing with loose objects */
1163 if (!a->in_pack && !b->in_pack)
1164 return hashcmp(a->sha1, b->sha1);
1166 if (a->in_pack < b->in_pack)
1167 return -1;
1168 if (a->in_pack > b->in_pack)
1169 return 1;
1170 return a->in_pack_offset < b->in_pack_offset ? -1 :
1171 (a->in_pack_offset > b->in_pack_offset);
1174 static void get_object_details(void)
1176 uint32_t i;
1177 struct object_entry **sorted_by_offset;
1179 sorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));
1180 for (i = 0; i < nr_objects; i++)
1181 sorted_by_offset[i] = objects + i;
1182 qsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
1184 prepare_pack_ix();
1185 for (i = 0; i < nr_objects; i++)
1186 check_object(sorted_by_offset[i]);
1187 free(sorted_by_offset);
1190 static int type_size_sort(const void *_a, const void *_b)
1192 const struct object_entry *a = *(struct object_entry **)_a;
1193 const struct object_entry *b = *(struct object_entry **)_b;
1195 if (a->type < b->type)
1196 return -1;
1197 if (a->type > b->type)
1198 return 1;
1199 if (a->hash < b->hash)
1200 return -1;
1201 if (a->hash > b->hash)
1202 return 1;
1203 if (a->preferred_base < b->preferred_base)
1204 return -1;
1205 if (a->preferred_base > b->preferred_base)
1206 return 1;
1207 if (a->size < b->size)
1208 return -1;
1209 if (a->size > b->size)
1210 return 1;
1211 return a > b ? -1 : (a < b); /* newest last */
1214 struct unpacked {
1215 struct object_entry *entry;
1216 void *data;
1217 struct delta_index *index;
1221 * We search for deltas _backwards_ in a list sorted by type and
1222 * by size, so that we see progressively smaller and smaller files.
1223 * That's because we prefer deltas to be from the bigger file
1224 * to the smaller - deletes are potentially cheaper, but perhaps
1225 * more importantly, the bigger file is likely the more recent
1226 * one.
1228 static int try_delta(struct unpacked *trg, struct unpacked *src,
1229 unsigned max_depth)
1231 struct object_entry *trg_entry = trg->entry;
1232 struct object_entry *src_entry = src->entry;
1233 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1234 enum object_type type;
1235 void *delta_buf;
1237 /* Don't bother doing diffs between different types */
1238 if (trg_entry->type != src_entry->type)
1239 return -1;
1241 /* We do not compute delta to *create* objects we are not
1242 * going to pack.
1244 if (trg_entry->preferred_base)
1245 return -1;
1248 * We do not bother to try a delta that we discarded
1249 * on an earlier try, but only when reusing delta data.
1251 if (!no_reuse_delta && trg_entry->in_pack &&
1252 trg_entry->in_pack == src_entry->in_pack &&
1253 trg_entry->in_pack_type != OBJ_REF_DELTA &&
1254 trg_entry->in_pack_type != OBJ_OFS_DELTA)
1255 return 0;
1257 /* Let's not bust the allowed depth. */
1258 if (src_entry->depth >= max_depth)
1259 return 0;
1261 /* Now some size filtering heuristics. */
1262 trg_size = trg_entry->size;
1263 max_size = trg_size/2 - 20;
1264 max_size = max_size * (max_depth - src_entry->depth) / max_depth;
1265 if (max_size == 0)
1266 return 0;
1267 if (trg_entry->delta && trg_entry->delta_size <= max_size)
1268 max_size = trg_entry->delta_size-1;
1269 src_size = src_entry->size;
1270 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1271 if (sizediff >= max_size)
1272 return 0;
1274 /* Load data if not already done */
1275 if (!trg->data) {
1276 trg->data = read_sha1_file(trg_entry->sha1, &type, &sz);
1277 if (sz != trg_size)
1278 die("object %s inconsistent object length (%lu vs %lu)",
1279 sha1_to_hex(trg_entry->sha1), sz, trg_size);
1281 if (!src->data) {
1282 src->data = read_sha1_file(src_entry->sha1, &type, &sz);
1283 if (sz != src_size)
1284 die("object %s inconsistent object length (%lu vs %lu)",
1285 sha1_to_hex(src_entry->sha1), sz, src_size);
1287 if (!src->index) {
1288 src->index = create_delta_index(src->data, src_size);
1289 if (!src->index)
1290 die("out of memory");
1293 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1294 if (!delta_buf)
1295 return 0;
1297 trg_entry->delta = src_entry;
1298 trg_entry->delta_size = delta_size;
1299 trg_entry->depth = src_entry->depth + 1;
1300 free(delta_buf);
1301 return 1;
1304 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1306 struct object_entry *child = me->delta_child;
1307 unsigned int m = n;
1308 while (child) {
1309 unsigned int c = check_delta_limit(child, n + 1);
1310 if (m < c)
1311 m = c;
1312 child = child->delta_sibling;
1314 return m;
1317 static void find_deltas(struct object_entry **list, int window, int depth)
1319 uint32_t i = nr_objects, idx = 0, processed = 0;
1320 unsigned int array_size = window * sizeof(struct unpacked);
1321 struct unpacked *array;
1322 int max_depth;
1324 if (!nr_objects)
1325 return;
1326 array = xmalloc(array_size);
1327 memset(array, 0, array_size);
1328 if (progress)
1329 start_progress(&progress_state, "Deltifying %u objects...", "", nr_result);
1331 do {
1332 struct object_entry *entry = list[--i];
1333 struct unpacked *n = array + idx;
1334 int j;
1336 if (!entry->preferred_base)
1337 processed++;
1339 if (progress)
1340 display_progress(&progress_state, processed);
1342 if (entry->delta)
1343 /* This happens if we decided to reuse existing
1344 * delta from a pack. "!no_reuse_delta &&" is implied.
1346 continue;
1348 if (entry->size < 50)
1349 continue;
1350 free_delta_index(n->index);
1351 n->index = NULL;
1352 free(n->data);
1353 n->data = NULL;
1354 n->entry = entry;
1357 * If the current object is at pack edge, take the depth the
1358 * objects that depend on the current object into account
1359 * otherwise they would become too deep.
1361 max_depth = depth;
1362 if (entry->delta_child) {
1363 max_depth -= check_delta_limit(entry, 0);
1364 if (max_depth <= 0)
1365 goto next;
1368 j = window;
1369 while (--j > 0) {
1370 uint32_t other_idx = idx + j;
1371 struct unpacked *m;
1372 if (other_idx >= window)
1373 other_idx -= window;
1374 m = array + other_idx;
1375 if (!m->entry)
1376 break;
1377 if (try_delta(n, m, max_depth) < 0)
1378 break;
1381 /* if we made n a delta, and if n is already at max
1382 * depth, leaving it in the window is pointless. we
1383 * should evict it first.
1385 if (entry->delta && depth <= entry->depth)
1386 continue;
1388 next:
1389 idx++;
1390 if (idx >= window)
1391 idx = 0;
1392 } while (i > 0);
1394 if (progress)
1395 stop_progress(&progress_state);
1397 for (i = 0; i < window; ++i) {
1398 free_delta_index(array[i].index);
1399 free(array[i].data);
1401 free(array);
1404 static void prepare_pack(int window, int depth)
1406 struct object_entry **delta_list;
1407 uint32_t i;
1409 get_object_details();
1411 if (!window || !depth)
1412 return;
1414 delta_list = xmalloc(nr_objects * sizeof(*delta_list));
1415 for (i = 0; i < nr_objects; i++)
1416 delta_list[i] = objects + i;
1417 qsort(delta_list, nr_objects, sizeof(*delta_list), type_size_sort);
1418 find_deltas(delta_list, window+1, depth);
1419 free(delta_list);
1422 static int git_pack_config(const char *k, const char *v)
1424 if(!strcmp(k, "pack.window")) {
1425 window = git_config_int(k, v);
1426 return 0;
1428 if(!strcmp(k, "pack.depth")) {
1429 depth = git_config_int(k, v);
1430 return 0;
1432 if (!strcmp(k, "pack.compression")) {
1433 int level = git_config_int(k, v);
1434 if (level == -1)
1435 level = Z_DEFAULT_COMPRESSION;
1436 else if (level < 0 || level > Z_BEST_COMPRESSION)
1437 die("bad pack compression level %d", level);
1438 pack_compression_level = level;
1439 pack_compression_seen = 1;
1440 return 0;
1442 return git_default_config(k, v);
1445 static void read_object_list_from_stdin(void)
1447 char line[40 + 1 + PATH_MAX + 2];
1448 unsigned char sha1[20];
1449 unsigned hash;
1451 for (;;) {
1452 if (!fgets(line, sizeof(line), stdin)) {
1453 if (feof(stdin))
1454 break;
1455 if (!ferror(stdin))
1456 die("fgets returned NULL, not EOF, not error!");
1457 if (errno != EINTR)
1458 die("fgets: %s", strerror(errno));
1459 clearerr(stdin);
1460 continue;
1462 if (line[0] == '-') {
1463 if (get_sha1_hex(line+1, sha1))
1464 die("expected edge sha1, got garbage:\n %s",
1465 line);
1466 add_preferred_base(sha1);
1467 continue;
1469 if (get_sha1_hex(line, sha1))
1470 die("expected sha1, got garbage:\n %s", line);
1472 hash = name_hash(line+41);
1473 add_preferred_base_object(line+41, hash);
1474 add_object_entry(sha1, 0, hash, 0);
1478 static void show_commit(struct commit *commit)
1480 add_object_entry(commit->object.sha1, OBJ_COMMIT, 0, 0);
1483 static void show_object(struct object_array_entry *p)
1485 unsigned hash = name_hash(p->name);
1486 add_preferred_base_object(p->name, hash);
1487 add_object_entry(p->item->sha1, p->item->type, hash, 0);
1490 static void show_edge(struct commit *commit)
1492 add_preferred_base(commit->object.sha1);
1495 static void get_object_list(int ac, const char **av)
1497 struct rev_info revs;
1498 char line[1000];
1499 int flags = 0;
1501 init_revisions(&revs, NULL);
1502 save_commit_buffer = 0;
1503 track_object_refs = 0;
1504 setup_revisions(ac, av, &revs, NULL);
1506 while (fgets(line, sizeof(line), stdin) != NULL) {
1507 int len = strlen(line);
1508 if (line[len - 1] == '\n')
1509 line[--len] = 0;
1510 if (!len)
1511 break;
1512 if (*line == '-') {
1513 if (!strcmp(line, "--not")) {
1514 flags ^= UNINTERESTING;
1515 continue;
1517 die("not a rev '%s'", line);
1519 if (handle_revision_arg(line, &revs, flags, 1))
1520 die("bad revision '%s'", line);
1523 prepare_revision_walk(&revs);
1524 mark_edges_uninteresting(revs.commits, &revs, show_edge);
1525 traverse_commit_list(&revs, show_commit, show_object);
1528 static int adjust_perm(const char *path, mode_t mode)
1530 if (chmod(path, mode))
1531 return -1;
1532 return adjust_shared_perm(path);
1535 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1537 int use_internal_rev_list = 0;
1538 int thin = 0;
1539 uint32_t i;
1540 off_t last_obj_offset;
1541 const char *base_name = NULL;
1542 const char **rp_av;
1543 int rp_ac_alloc = 64;
1544 int rp_ac;
1546 rp_av = xcalloc(rp_ac_alloc, sizeof(*rp_av));
1548 rp_av[0] = "pack-objects";
1549 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1550 rp_ac = 2;
1552 git_config(git_pack_config);
1553 if (!pack_compression_seen && core_compression_seen)
1554 pack_compression_level = core_compression_level;
1556 progress = isatty(2);
1557 for (i = 1; i < argc; i++) {
1558 const char *arg = argv[i];
1560 if (*arg != '-')
1561 break;
1563 if (!strcmp("--non-empty", arg)) {
1564 non_empty = 1;
1565 continue;
1567 if (!strcmp("--local", arg)) {
1568 local = 1;
1569 continue;
1571 if (!strcmp("--incremental", arg)) {
1572 incremental = 1;
1573 continue;
1575 if (!prefixcmp(arg, "--compression=")) {
1576 char *end;
1577 int level = strtoul(arg+14, &end, 0);
1578 if (!arg[14] || *end)
1579 usage(pack_usage);
1580 if (level == -1)
1581 level = Z_DEFAULT_COMPRESSION;
1582 else if (level < 0 || level > Z_BEST_COMPRESSION)
1583 die("bad pack compression level %d", level);
1584 pack_compression_level = level;
1585 continue;
1587 if (!prefixcmp(arg, "--window=")) {
1588 char *end;
1589 window = strtoul(arg+9, &end, 0);
1590 if (!arg[9] || *end)
1591 usage(pack_usage);
1592 continue;
1594 if (!prefixcmp(arg, "--depth=")) {
1595 char *end;
1596 depth = strtoul(arg+8, &end, 0);
1597 if (!arg[8] || *end)
1598 usage(pack_usage);
1599 continue;
1601 if (!strcmp("--progress", arg)) {
1602 progress = 1;
1603 continue;
1605 if (!strcmp("--all-progress", arg)) {
1606 progress = 2;
1607 continue;
1609 if (!strcmp("-q", arg)) {
1610 progress = 0;
1611 continue;
1613 if (!strcmp("--no-reuse-delta", arg)) {
1614 no_reuse_delta = 1;
1615 continue;
1617 if (!strcmp("--no-reuse-object", arg)) {
1618 no_reuse_object = no_reuse_delta = 1;
1619 continue;
1621 if (!strcmp("--delta-base-offset", arg)) {
1622 allow_ofs_delta = 1;
1623 continue;
1625 if (!strcmp("--stdout", arg)) {
1626 pack_to_stdout = 1;
1627 continue;
1629 if (!strcmp("--revs", arg)) {
1630 use_internal_rev_list = 1;
1631 continue;
1633 if (!strcmp("--unpacked", arg) ||
1634 !prefixcmp(arg, "--unpacked=") ||
1635 !strcmp("--reflog", arg) ||
1636 !strcmp("--all", arg)) {
1637 use_internal_rev_list = 1;
1638 if (rp_ac >= rp_ac_alloc - 1) {
1639 rp_ac_alloc = alloc_nr(rp_ac_alloc);
1640 rp_av = xrealloc(rp_av,
1641 rp_ac_alloc * sizeof(*rp_av));
1643 rp_av[rp_ac++] = arg;
1644 continue;
1646 if (!strcmp("--thin", arg)) {
1647 use_internal_rev_list = 1;
1648 thin = 1;
1649 rp_av[1] = "--objects-edge";
1650 continue;
1652 if (!prefixcmp(arg, "--index-version=")) {
1653 char *c;
1654 index_default_version = strtoul(arg + 16, &c, 10);
1655 if (index_default_version > 2)
1656 die("bad %s", arg);
1657 if (*c == ',')
1658 index_off32_limit = strtoul(c+1, &c, 0);
1659 if (*c || index_off32_limit & 0x80000000)
1660 die("bad %s", arg);
1661 continue;
1663 usage(pack_usage);
1666 /* Traditionally "pack-objects [options] base extra" failed;
1667 * we would however want to take refs parameter that would
1668 * have been given to upstream rev-list ourselves, which means
1669 * we somehow want to say what the base name is. So the
1670 * syntax would be:
1672 * pack-objects [options] base <refs...>
1674 * in other words, we would treat the first non-option as the
1675 * base_name and send everything else to the internal revision
1676 * walker.
1679 if (!pack_to_stdout)
1680 base_name = argv[i++];
1682 if (pack_to_stdout != !base_name)
1683 usage(pack_usage);
1685 if (!pack_to_stdout && thin)
1686 die("--thin cannot be used to build an indexable pack.");
1688 prepare_packed_git();
1690 if (progress)
1691 start_progress(&progress_state, "Generating pack...",
1692 "Counting objects: ", 0);
1693 if (!use_internal_rev_list)
1694 read_object_list_from_stdin();
1695 else {
1696 rp_av[rp_ac] = NULL;
1697 get_object_list(rp_ac, rp_av);
1699 if (progress) {
1700 stop_progress(&progress_state);
1701 fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1704 if (non_empty && !nr_result)
1705 return 0;
1706 if (progress && (nr_objects != nr_result))
1707 fprintf(stderr, "Result has %u objects.\n", nr_result);
1708 if (nr_result)
1709 prepare_pack(window, depth);
1710 last_obj_offset = write_pack_file();
1711 if (!pack_to_stdout) {
1712 unsigned char object_list_sha1[20];
1713 mode_t mode = umask(0);
1715 umask(mode);
1716 mode = 0444 & ~mode;
1718 write_index_file(last_obj_offset, object_list_sha1);
1719 snprintf(tmpname, sizeof(tmpname), "%s-%s.pack",
1720 base_name, sha1_to_hex(object_list_sha1));
1721 if (adjust_perm(pack_tmp_name, mode))
1722 die("unable to make temporary pack file readable: %s",
1723 strerror(errno));
1724 if (rename(pack_tmp_name, tmpname))
1725 die("unable to rename temporary pack file: %s",
1726 strerror(errno));
1727 snprintf(tmpname, sizeof(tmpname), "%s-%s.idx",
1728 base_name, sha1_to_hex(object_list_sha1));
1729 if (adjust_perm(idx_tmp_name, mode))
1730 die("unable to make temporary index file readable: %s",
1731 strerror(errno));
1732 if (rename(idx_tmp_name, tmpname))
1733 die("unable to rename temporary index file: %s",
1734 strerror(errno));
1735 puts(sha1_to_hex(object_list_sha1));
1737 if (progress)
1738 fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
1739 written, written_delta, reused, reused_delta);
1740 return 0;