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 <sys/time.h>
16 #include <signal.h>
18 static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] [--revs [--unpacked | --all]*] [--stdout | base-name] <ref-list | <object-list]";
20 struct object_entry {
21 unsigned char sha1[20];
22 unsigned long size; /* uncompressed size */
23 unsigned long offset; /* offset into the final pack file;
24 * nonzero if already written.
25 */
26 unsigned int depth; /* delta depth */
27 unsigned int delta_limit; /* base adjustment for in-pack delta */
28 unsigned int hash; /* name hint hash */
29 enum object_type type;
30 enum object_type in_pack_type; /* could be delta */
31 unsigned long delta_size; /* delta data size (uncompressed) */
32 #define in_pack_header_size delta_size /* only when reusing pack data */
33 struct object_entry *delta; /* delta base object */
34 struct packed_git *in_pack; /* already in pack */
35 unsigned int in_pack_offset;
36 struct object_entry *delta_child; /* deltified objects who bases me */
37 struct object_entry *delta_sibling; /* other deltified objects who
38 * uses the same base as me
39 */
40 int preferred_base; /* we do not pack this, but is encouraged to
41 * be used as the base objectto delta huge
42 * objects against.
43 */
44 };
46 /*
47 * Objects we are going to pack are collected in objects array (dynamically
48 * expanded). nr_objects & nr_alloc controls this array. They are stored
49 * in the order we see -- typically rev-list --objects order that gives us
50 * nice "minimum seek" order.
51 *
52 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
53 * elements in the objects array. The former is used to build the pack
54 * index (lists object names in the ascending order to help offset lookup),
55 * and the latter is used to group similar things together by try_delta()
56 * heuristics.
57 */
59 static unsigned char object_list_sha1[20];
60 static int non_empty;
61 static int no_reuse_delta;
62 static int local;
63 static int incremental;
64 static int allow_ofs_delta;
66 static struct object_entry **sorted_by_sha, **sorted_by_type;
67 static struct object_entry *objects;
68 static int nr_objects, nr_alloc, nr_result;
69 static const char *base_name;
70 static unsigned char pack_file_sha1[20];
71 static int progress = 1;
72 static volatile sig_atomic_t progress_update;
73 static int window = 10;
74 static int pack_to_stdout;
75 static int num_preferred_base;
77 /*
78 * The object names in objects array are hashed with this hashtable,
79 * to help looking up the entry by object name. Binary search from
80 * sorted_by_sha is also possible but this was easier to code and faster.
81 * This hashtable is built after all the objects are seen.
82 */
83 static int *object_ix;
84 static int object_ix_hashsz;
86 /*
87 * Pack index for existing packs give us easy access to the offsets into
88 * corresponding pack file where each object's data starts, but the entries
89 * do not store the size of the compressed representation (uncompressed
90 * size is easily available by examining the pack entry header). It is
91 * also rather expensive to find the sha1 for an object given its offset.
92 *
93 * We build a hashtable of existing packs (pack_revindex), and keep reverse
94 * index here -- pack index file is sorted by object name mapping to offset;
95 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
96 * ordered by offset, so if you know the offset of an object, next offset
97 * is where its packed representation ends and the index_nr can be used to
98 * get the object sha1 from the main index.
99 */
100 struct revindex_entry {
101 unsigned int offset;
102 unsigned int nr;
103 };
104 struct pack_revindex {
105 struct packed_git *p;
106 struct revindex_entry *revindex;
107 };
108 static struct pack_revindex *pack_revindex;
109 static int pack_revindex_hashsz;
111 /*
112 * stats
113 */
114 static int written;
115 static int written_delta;
116 static int reused;
117 static int reused_delta;
119 static int pack_revindex_ix(struct packed_git *p)
120 {
121 unsigned long ui = (unsigned long)p;
122 int i;
124 ui = ui ^ (ui >> 16); /* defeat structure alignment */
125 i = (int)(ui % pack_revindex_hashsz);
126 while (pack_revindex[i].p) {
127 if (pack_revindex[i].p == p)
128 return i;
129 if (++i == pack_revindex_hashsz)
130 i = 0;
131 }
132 return -1 - i;
133 }
135 static void prepare_pack_ix(void)
136 {
137 int num;
138 struct packed_git *p;
139 for (num = 0, p = packed_git; p; p = p->next)
140 num++;
141 if (!num)
142 return;
143 pack_revindex_hashsz = num * 11;
144 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
145 for (p = packed_git; p; p = p->next) {
146 num = pack_revindex_ix(p);
147 num = - 1 - num;
148 pack_revindex[num].p = p;
149 }
150 /* revindex elements are lazily initialized */
151 }
153 static int cmp_offset(const void *a_, const void *b_)
154 {
155 const struct revindex_entry *a = a_;
156 const struct revindex_entry *b = b_;
157 return (a->offset < b->offset) ? -1 : (a->offset > b->offset) ? 1 : 0;
158 }
160 /*
161 * Ordered list of offsets of objects in the pack.
162 */
163 static void prepare_pack_revindex(struct pack_revindex *rix)
164 {
165 struct packed_git *p = rix->p;
166 int num_ent = num_packed_objects(p);
167 int i;
168 void *index = p->index_base + 256;
170 rix->revindex = xmalloc(sizeof(*rix->revindex) * (num_ent + 1));
171 for (i = 0; i < num_ent; i++) {
172 unsigned int hl = *((unsigned int *)((char *) index + 24*i));
173 rix->revindex[i].offset = ntohl(hl);
174 rix->revindex[i].nr = i;
175 }
176 /* This knows the pack format -- the 20-byte trailer
177 * follows immediately after the last object data.
178 */
179 rix->revindex[num_ent].offset = p->pack_size - 20;
180 rix->revindex[num_ent].nr = -1;
181 qsort(rix->revindex, num_ent, sizeof(*rix->revindex), cmp_offset);
182 }
184 static struct revindex_entry * find_packed_object(struct packed_git *p,
185 unsigned int ofs)
186 {
187 int num;
188 int lo, hi;
189 struct pack_revindex *rix;
190 struct revindex_entry *revindex;
191 num = pack_revindex_ix(p);
192 if (num < 0)
193 die("internal error: pack revindex uninitialized");
194 rix = &pack_revindex[num];
195 if (!rix->revindex)
196 prepare_pack_revindex(rix);
197 revindex = rix->revindex;
198 lo = 0;
199 hi = num_packed_objects(p) + 1;
200 do {
201 int mi = (lo + hi) / 2;
202 if (revindex[mi].offset == ofs) {
203 return revindex + mi;
204 }
205 else if (ofs < revindex[mi].offset)
206 hi = mi;
207 else
208 lo = mi + 1;
209 } while (lo < hi);
210 die("internal error: pack revindex corrupt");
211 }
213 static unsigned long find_packed_object_size(struct packed_git *p,
214 unsigned long ofs)
215 {
216 struct revindex_entry *entry = find_packed_object(p, ofs);
217 return entry[1].offset - ofs;
218 }
220 static unsigned char *find_packed_object_name(struct packed_git *p,
221 unsigned long ofs)
222 {
223 struct revindex_entry *entry = find_packed_object(p, ofs);
224 return (unsigned char *)(p->index_base + 256) + 24 * entry->nr + 4;
225 }
227 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
228 {
229 unsigned long othersize, delta_size;
230 char type[10];
231 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
232 void *delta_buf;
234 if (!otherbuf)
235 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
236 delta_buf = diff_delta(otherbuf, othersize,
237 buf, size, &delta_size, 0);
238 if (!delta_buf || delta_size != entry->delta_size)
239 die("delta size changed");
240 free(buf);
241 free(otherbuf);
242 return delta_buf;
243 }
245 /*
246 * The per-object header is a pretty dense thing, which is
247 * - first byte: low four bits are "size", then three bits of "type",
248 * and the high bit is "size continues".
249 * - each byte afterwards: low seven bits are size continuation,
250 * with the high bit being "size continues"
251 */
252 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
253 {
254 int n = 1;
255 unsigned char c;
257 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
258 die("bad type %d", type);
260 c = (type << 4) | (size & 15);
261 size >>= 4;
262 while (size) {
263 *hdr++ = c | 0x80;
264 c = size & 0x7f;
265 size >>= 7;
266 n++;
267 }
268 *hdr = c;
269 return n;
270 }
272 /*
273 * we are going to reuse the existing object data as is. make
274 * sure it is not corrupt.
275 */
276 static int check_inflate(unsigned char *data, unsigned long len, unsigned long expect)
277 {
278 z_stream stream;
279 unsigned char fakebuf[4096];
280 int st;
282 memset(&stream, 0, sizeof(stream));
283 stream.next_in = data;
284 stream.avail_in = len;
285 stream.next_out = fakebuf;
286 stream.avail_out = sizeof(fakebuf);
287 inflateInit(&stream);
289 while (1) {
290 st = inflate(&stream, Z_FINISH);
291 if (st == Z_STREAM_END || st == Z_OK) {
292 st = (stream.total_out == expect &&
293 stream.total_in == len) ? 0 : -1;
294 break;
295 }
296 if (st != Z_BUF_ERROR) {
297 st = -1;
298 break;
299 }
300 stream.next_out = fakebuf;
301 stream.avail_out = sizeof(fakebuf);
302 }
303 inflateEnd(&stream);
304 return st;
305 }
307 static int revalidate_loose_object(struct object_entry *entry,
308 unsigned char *map,
309 unsigned long mapsize)
310 {
311 /* we already know this is a loose object with new type header. */
312 enum object_type type;
313 unsigned long size, used;
315 if (pack_to_stdout)
316 return 0;
318 used = unpack_object_header_gently(map, mapsize, &type, &size);
319 if (!used)
320 return -1;
321 map += used;
322 mapsize -= used;
323 return check_inflate(map, mapsize, size);
324 }
326 static unsigned long write_object(struct sha1file *f,
327 struct object_entry *entry)
328 {
329 unsigned long size;
330 char type[10];
331 void *buf;
332 unsigned char header[10];
333 unsigned hdrlen, datalen;
334 enum object_type obj_type;
335 int to_reuse = 0;
337 obj_type = entry->type;
338 if (! entry->in_pack)
339 to_reuse = 0; /* can't reuse what we don't have */
340 else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
341 to_reuse = 1; /* check_object() decided it for us */
342 else if (obj_type != entry->in_pack_type)
343 to_reuse = 0; /* pack has delta which is unusable */
344 else if (entry->delta)
345 to_reuse = 0; /* we want to pack afresh */
346 else
347 to_reuse = 1; /* we have it in-pack undeltified,
348 * and we do not need to deltify it.
349 */
351 if (!entry->in_pack && !entry->delta) {
352 unsigned char *map;
353 unsigned long mapsize;
354 map = map_sha1_file(entry->sha1, &mapsize);
355 if (map && !legacy_loose_object(map)) {
356 /* We can copy straight into the pack file */
357 if (revalidate_loose_object(entry, map, mapsize))
358 die("corrupt loose object %s",
359 sha1_to_hex(entry->sha1));
360 sha1write(f, map, mapsize);
361 munmap(map, mapsize);
362 written++;
363 reused++;
364 return mapsize;
365 }
366 if (map)
367 munmap(map, mapsize);
368 }
370 if (!to_reuse) {
371 buf = read_sha1_file(entry->sha1, type, &size);
372 if (!buf)
373 die("unable to read %s", sha1_to_hex(entry->sha1));
374 if (size != entry->size)
375 die("object %s size inconsistency (%lu vs %lu)",
376 sha1_to_hex(entry->sha1), size, entry->size);
377 if (entry->delta) {
378 buf = delta_against(buf, size, entry);
379 size = entry->delta_size;
380 obj_type = (allow_ofs_delta && entry->delta->offset) ?
381 OBJ_OFS_DELTA : OBJ_REF_DELTA;
382 }
383 /*
384 * The object header is a byte of 'type' followed by zero or
385 * more bytes of length.
386 */
387 hdrlen = encode_header(obj_type, size, header);
388 sha1write(f, header, hdrlen);
390 if (obj_type == OBJ_OFS_DELTA) {
391 /*
392 * Deltas with relative base contain an additional
393 * encoding of the relative offset for the delta
394 * base from this object's position in the pack.
395 */
396 unsigned long ofs = entry->offset - entry->delta->offset;
397 unsigned pos = sizeof(header) - 1;
398 header[pos] = ofs & 127;
399 while (ofs >>= 7)
400 header[--pos] = 128 | (--ofs & 127);
401 sha1write(f, header + pos, sizeof(header) - pos);
402 hdrlen += sizeof(header) - pos;
403 } else if (obj_type == OBJ_REF_DELTA) {
404 /*
405 * Deltas with a base reference contain
406 * an additional 20 bytes for the base sha1.
407 */
408 sha1write(f, entry->delta->sha1, 20);
409 hdrlen += 20;
410 }
411 datalen = sha1write_compressed(f, buf, size);
412 free(buf);
413 }
414 else {
415 struct packed_git *p = entry->in_pack;
417 if (entry->delta) {
418 obj_type = (allow_ofs_delta && entry->delta->offset) ?
419 OBJ_OFS_DELTA : OBJ_REF_DELTA;
420 reused_delta++;
421 }
422 hdrlen = encode_header(obj_type, entry->size, header);
423 sha1write(f, header, hdrlen);
424 if (obj_type == OBJ_OFS_DELTA) {
425 unsigned long ofs = entry->offset - entry->delta->offset;
426 unsigned pos = sizeof(header) - 1;
427 header[pos] = ofs & 127;
428 while (ofs >>= 7)
429 header[--pos] = 128 | (--ofs & 127);
430 sha1write(f, header + pos, sizeof(header) - pos);
431 hdrlen += sizeof(header) - pos;
432 } else if (obj_type == OBJ_REF_DELTA) {
433 sha1write(f, entry->delta->sha1, 20);
434 hdrlen += 20;
435 }
437 use_packed_git(p);
438 buf = (char *) p->pack_base
439 + entry->in_pack_offset
440 + entry->in_pack_header_size;
441 datalen = find_packed_object_size(p, entry->in_pack_offset)
442 - entry->in_pack_header_size;
443 //fprintf(stderr, "reusing %d at %d header %d size %d\n", obj_type, entry->in_pack_offset, entry->in_pack_header_size, datalen);
444 if (!pack_to_stdout && check_inflate(buf, datalen, entry->size))
445 die("corrupt delta in pack %s", sha1_to_hex(entry->sha1));
446 sha1write(f, buf, datalen);
447 unuse_packed_git(p);
448 reused++;
449 }
450 if (entry->delta)
451 written_delta++;
452 written++;
453 return hdrlen + datalen;
454 }
456 static unsigned long write_one(struct sha1file *f,
457 struct object_entry *e,
458 unsigned long offset)
459 {
460 if (e->offset || e->preferred_base)
461 /* offset starts from header size and cannot be zero
462 * if it is written already.
463 */
464 return offset;
465 /* if we are deltified, write out its base object first. */
466 if (e->delta)
467 offset = write_one(f, e->delta, offset);
468 e->offset = offset;
469 return offset + write_object(f, e);
470 }
472 static void write_pack_file(void)
473 {
474 int i;
475 struct sha1file *f;
476 unsigned long offset;
477 struct pack_header hdr;
478 unsigned last_percent = 999;
479 int do_progress = 0;
481 if (!base_name)
482 f = sha1fd(1, "<stdout>");
483 else {
484 f = sha1create("%s-%s.%s", base_name,
485 sha1_to_hex(object_list_sha1), "pack");
486 do_progress = progress;
487 }
488 if (do_progress)
489 fprintf(stderr, "Writing %d objects.\n", nr_result);
491 hdr.hdr_signature = htonl(PACK_SIGNATURE);
492 hdr.hdr_version = htonl(PACK_VERSION);
493 hdr.hdr_entries = htonl(nr_result);
494 sha1write(f, &hdr, sizeof(hdr));
495 offset = sizeof(hdr);
496 if (!nr_result)
497 goto done;
498 for (i = 0; i < nr_objects; i++) {
499 offset = write_one(f, objects + i, offset);
500 if (do_progress) {
501 unsigned percent = written * 100 / nr_result;
502 if (progress_update || percent != last_percent) {
503 fprintf(stderr, "%4u%% (%u/%u) done\r",
504 percent, written, nr_result);
505 progress_update = 0;
506 last_percent = percent;
507 }
508 }
509 }
510 if (do_progress)
511 fputc('\n', stderr);
512 done:
513 sha1close(f, pack_file_sha1, 1);
514 }
516 static void write_index_file(void)
517 {
518 int i;
519 struct sha1file *f = sha1create("%s-%s.%s", base_name,
520 sha1_to_hex(object_list_sha1), "idx");
521 struct object_entry **list = sorted_by_sha;
522 struct object_entry **last = list + nr_result;
523 unsigned int array[256];
525 /*
526 * Write the first-level table (the list is sorted,
527 * but we use a 256-entry lookup to be able to avoid
528 * having to do eight extra binary search iterations).
529 */
530 for (i = 0; i < 256; i++) {
531 struct object_entry **next = list;
532 while (next < last) {
533 struct object_entry *entry = *next;
534 if (entry->sha1[0] != i)
535 break;
536 next++;
537 }
538 array[i] = htonl(next - sorted_by_sha);
539 list = next;
540 }
541 sha1write(f, array, 256 * sizeof(int));
543 /*
544 * Write the actual SHA1 entries..
545 */
546 list = sorted_by_sha;
547 for (i = 0; i < nr_result; i++) {
548 struct object_entry *entry = *list++;
549 unsigned int offset = htonl(entry->offset);
550 sha1write(f, &offset, 4);
551 sha1write(f, entry->sha1, 20);
552 }
553 sha1write(f, pack_file_sha1, 20);
554 sha1close(f, NULL, 1);
555 }
557 static int locate_object_entry_hash(const unsigned char *sha1)
558 {
559 int i;
560 unsigned int ui;
561 memcpy(&ui, sha1, sizeof(unsigned int));
562 i = ui % object_ix_hashsz;
563 while (0 < object_ix[i]) {
564 if (!hashcmp(sha1, objects[object_ix[i] - 1].sha1))
565 return i;
566 if (++i == object_ix_hashsz)
567 i = 0;
568 }
569 return -1 - i;
570 }
572 static struct object_entry *locate_object_entry(const unsigned char *sha1)
573 {
574 int i;
576 if (!object_ix_hashsz)
577 return NULL;
579 i = locate_object_entry_hash(sha1);
580 if (0 <= i)
581 return &objects[object_ix[i]-1];
582 return NULL;
583 }
585 static void rehash_objects(void)
586 {
587 int i;
588 struct object_entry *oe;
590 object_ix_hashsz = nr_objects * 3;
591 if (object_ix_hashsz < 1024)
592 object_ix_hashsz = 1024;
593 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
594 memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
595 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
596 int ix = locate_object_entry_hash(oe->sha1);
597 if (0 <= ix)
598 continue;
599 ix = -1 - ix;
600 object_ix[ix] = i + 1;
601 }
602 }
604 static unsigned name_hash(const char *name)
605 {
606 unsigned char c;
607 unsigned hash = 0;
609 /*
610 * This effectively just creates a sortable number from the
611 * last sixteen non-whitespace characters. Last characters
612 * count "most", so things that end in ".c" sort together.
613 */
614 while ((c = *name++) != 0) {
615 if (isspace(c))
616 continue;
617 hash = (hash >> 2) + (c << 24);
618 }
619 return hash;
620 }
622 static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
623 {
624 unsigned int idx = nr_objects;
625 struct object_entry *entry;
626 struct packed_git *p;
627 unsigned int found_offset = 0;
628 struct packed_git *found_pack = NULL;
629 int ix, status = 0;
631 if (!exclude) {
632 for (p = packed_git; p; p = p->next) {
633 unsigned long offset = find_pack_entry_one(sha1, p);
634 if (offset) {
635 if (incremental)
636 return 0;
637 if (local && !p->pack_local)
638 return 0;
639 if (!found_pack) {
640 found_offset = offset;
641 found_pack = p;
642 }
643 }
644 }
645 }
646 if ((entry = locate_object_entry(sha1)) != NULL)
647 goto already_added;
649 if (idx >= nr_alloc) {
650 unsigned int needed = (idx + 1024) * 3 / 2;
651 objects = xrealloc(objects, needed * sizeof(*entry));
652 nr_alloc = needed;
653 }
654 entry = objects + idx;
655 nr_objects = idx + 1;
656 memset(entry, 0, sizeof(*entry));
657 hashcpy(entry->sha1, sha1);
658 entry->hash = hash;
660 if (object_ix_hashsz * 3 <= nr_objects * 4)
661 rehash_objects();
662 else {
663 ix = locate_object_entry_hash(entry->sha1);
664 if (0 <= ix)
665 die("internal error in object hashing.");
666 object_ix[-1 - ix] = idx + 1;
667 }
668 status = 1;
670 already_added:
671 if (progress_update) {
672 fprintf(stderr, "Counting objects...%d\r", nr_objects);
673 progress_update = 0;
674 }
675 if (exclude)
676 entry->preferred_base = 1;
677 else {
678 if (found_pack) {
679 entry->in_pack = found_pack;
680 entry->in_pack_offset = found_offset;
681 }
682 }
683 return status;
684 }
686 struct pbase_tree_cache {
687 unsigned char sha1[20];
688 int ref;
689 int temporary;
690 void *tree_data;
691 unsigned long tree_size;
692 };
694 static struct pbase_tree_cache *(pbase_tree_cache[256]);
695 static int pbase_tree_cache_ix(const unsigned char *sha1)
696 {
697 return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
698 }
699 static int pbase_tree_cache_ix_incr(int ix)
700 {
701 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
702 }
704 static struct pbase_tree {
705 struct pbase_tree *next;
706 /* This is a phony "cache" entry; we are not
707 * going to evict it nor find it through _get()
708 * mechanism -- this is for the toplevel node that
709 * would almost always change with any commit.
710 */
711 struct pbase_tree_cache pcache;
712 } *pbase_tree;
714 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
715 {
716 struct pbase_tree_cache *ent, *nent;
717 void *data;
718 unsigned long size;
719 char type[20];
720 int neigh;
721 int my_ix = pbase_tree_cache_ix(sha1);
722 int available_ix = -1;
724 /* pbase-tree-cache acts as a limited hashtable.
725 * your object will be found at your index or within a few
726 * slots after that slot if it is cached.
727 */
728 for (neigh = 0; neigh < 8; neigh++) {
729 ent = pbase_tree_cache[my_ix];
730 if (ent && !hashcmp(ent->sha1, sha1)) {
731 ent->ref++;
732 return ent;
733 }
734 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
735 ((0 <= available_ix) &&
736 (!ent && pbase_tree_cache[available_ix])))
737 available_ix = my_ix;
738 if (!ent)
739 break;
740 my_ix = pbase_tree_cache_ix_incr(my_ix);
741 }
743 /* Did not find one. Either we got a bogus request or
744 * we need to read and perhaps cache.
745 */
746 data = read_sha1_file(sha1, type, &size);
747 if (!data)
748 return NULL;
749 if (strcmp(type, tree_type)) {
750 free(data);
751 return NULL;
752 }
754 /* We need to either cache or return a throwaway copy */
756 if (available_ix < 0)
757 ent = NULL;
758 else {
759 ent = pbase_tree_cache[available_ix];
760 my_ix = available_ix;
761 }
763 if (!ent) {
764 nent = xmalloc(sizeof(*nent));
765 nent->temporary = (available_ix < 0);
766 }
767 else {
768 /* evict and reuse */
769 free(ent->tree_data);
770 nent = ent;
771 }
772 hashcpy(nent->sha1, sha1);
773 nent->tree_data = data;
774 nent->tree_size = size;
775 nent->ref = 1;
776 if (!nent->temporary)
777 pbase_tree_cache[my_ix] = nent;
778 return nent;
779 }
781 static void pbase_tree_put(struct pbase_tree_cache *cache)
782 {
783 if (!cache->temporary) {
784 cache->ref--;
785 return;
786 }
787 free(cache->tree_data);
788 free(cache);
789 }
791 static int name_cmp_len(const char *name)
792 {
793 int i;
794 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
795 ;
796 return i;
797 }
799 static void add_pbase_object(struct tree_desc *tree,
800 const char *name,
801 int cmplen,
802 const char *fullname)
803 {
804 struct name_entry entry;
806 while (tree_entry(tree,&entry)) {
807 unsigned long size;
808 char type[20];
810 if (entry.pathlen != cmplen ||
811 memcmp(entry.path, name, cmplen) ||
812 !has_sha1_file(entry.sha1) ||
813 sha1_object_info(entry.sha1, type, &size))
814 continue;
815 if (name[cmplen] != '/') {
816 unsigned hash = name_hash(fullname);
817 add_object_entry(entry.sha1, hash, 1);
818 return;
819 }
820 if (!strcmp(type, tree_type)) {
821 struct tree_desc sub;
822 struct pbase_tree_cache *tree;
823 const char *down = name+cmplen+1;
824 int downlen = name_cmp_len(down);
826 tree = pbase_tree_get(entry.sha1);
827 if (!tree)
828 return;
829 sub.buf = tree->tree_data;
830 sub.size = tree->tree_size;
832 add_pbase_object(&sub, down, downlen, fullname);
833 pbase_tree_put(tree);
834 }
835 }
836 }
838 static unsigned *done_pbase_paths;
839 static int done_pbase_paths_num;
840 static int done_pbase_paths_alloc;
841 static int done_pbase_path_pos(unsigned hash)
842 {
843 int lo = 0;
844 int hi = done_pbase_paths_num;
845 while (lo < hi) {
846 int mi = (hi + lo) / 2;
847 if (done_pbase_paths[mi] == hash)
848 return mi;
849 if (done_pbase_paths[mi] < hash)
850 hi = mi;
851 else
852 lo = mi + 1;
853 }
854 return -lo-1;
855 }
857 static int check_pbase_path(unsigned hash)
858 {
859 int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
860 if (0 <= pos)
861 return 1;
862 pos = -pos - 1;
863 if (done_pbase_paths_alloc <= done_pbase_paths_num) {
864 done_pbase_paths_alloc = alloc_nr(done_pbase_paths_alloc);
865 done_pbase_paths = xrealloc(done_pbase_paths,
866 done_pbase_paths_alloc *
867 sizeof(unsigned));
868 }
869 done_pbase_paths_num++;
870 if (pos < done_pbase_paths_num)
871 memmove(done_pbase_paths + pos + 1,
872 done_pbase_paths + pos,
873 (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
874 done_pbase_paths[pos] = hash;
875 return 0;
876 }
878 static void add_preferred_base_object(const char *name, unsigned hash)
879 {
880 struct pbase_tree *it;
881 int cmplen = name_cmp_len(name);
883 if (check_pbase_path(hash))
884 return;
886 for (it = pbase_tree; it; it = it->next) {
887 if (cmplen == 0) {
888 hash = name_hash("");
889 add_object_entry(it->pcache.sha1, hash, 1);
890 }
891 else {
892 struct tree_desc tree;
893 tree.buf = it->pcache.tree_data;
894 tree.size = it->pcache.tree_size;
895 add_pbase_object(&tree, name, cmplen, name);
896 }
897 }
898 }
900 static void add_preferred_base(unsigned char *sha1)
901 {
902 struct pbase_tree *it;
903 void *data;
904 unsigned long size;
905 unsigned char tree_sha1[20];
907 if (window <= num_preferred_base++)
908 return;
910 data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
911 if (!data)
912 return;
914 for (it = pbase_tree; it; it = it->next) {
915 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
916 free(data);
917 return;
918 }
919 }
921 it = xcalloc(1, sizeof(*it));
922 it->next = pbase_tree;
923 pbase_tree = it;
925 hashcpy(it->pcache.sha1, tree_sha1);
926 it->pcache.tree_data = data;
927 it->pcache.tree_size = size;
928 }
930 static void check_object(struct object_entry *entry)
931 {
932 char type[20];
934 if (entry->in_pack && !entry->preferred_base) {
935 struct packed_git *p = entry->in_pack;
936 unsigned long left = p->pack_size - entry->in_pack_offset;
937 unsigned long size, used;
938 unsigned char *buf;
939 struct object_entry *base_entry = NULL;
941 use_packed_git(p);
942 buf = p->pack_base;
943 buf += entry->in_pack_offset;
945 /* We want in_pack_type even if we do not reuse delta.
946 * There is no point not reusing non-delta representations.
947 */
948 used = unpack_object_header_gently(buf, left,
949 &entry->in_pack_type, &size);
950 if (!used || left - used <= 20)
951 die("corrupt pack for %s", sha1_to_hex(entry->sha1));
953 /* Check if it is delta, and the base is also an object
954 * we are going to pack. If so we will reuse the existing
955 * delta.
956 */
957 if (!no_reuse_delta) {
958 unsigned char c, *base_name;
959 unsigned long ofs;
960 /* there is at least 20 bytes left in the pack */
961 switch (entry->in_pack_type) {
962 case OBJ_REF_DELTA:
963 base_name = buf + used;
964 used += 20;
965 break;
966 case OBJ_OFS_DELTA:
967 c = buf[used++];
968 ofs = c & 127;
969 while (c & 128) {
970 ofs += 1;
971 if (!ofs || ofs & ~(~0UL >> 7))
972 die("delta base offset overflow in pack for %s",
973 sha1_to_hex(entry->sha1));
974 c = buf[used++];
975 ofs = (ofs << 7) + (c & 127);
976 }
977 if (ofs >= entry->in_pack_offset)
978 die("delta base offset out of bound for %s",
979 sha1_to_hex(entry->sha1));
980 ofs = entry->in_pack_offset - ofs;
981 base_name = find_packed_object_name(p, ofs);
982 break;
983 default:
984 base_name = NULL;
985 }
986 if (base_name)
987 base_entry = locate_object_entry(base_name);
988 }
989 unuse_packed_git(p);
990 entry->in_pack_header_size = used;
992 if (base_entry && !base_entry->preferred_base) {
994 /* Depth value does not matter - find_deltas()
995 * will never consider reused delta as the
996 * base object to deltify other objects
997 * against, in order to avoid circular deltas.
998 */
1000 /* uncompressed size of the delta data */
1001 entry->size = size;
1002 entry->delta = base_entry;
1003 entry->type = entry->in_pack_type;
1005 entry->delta_sibling = base_entry->delta_child;
1006 base_entry->delta_child = entry;
1008 return;
1009 }
1010 /* Otherwise we would do the usual */
1011 }
1013 if (sha1_object_info(entry->sha1, type, &entry->size))
1014 die("unable to get type of object %s",
1015 sha1_to_hex(entry->sha1));
1017 if (!strcmp(type, commit_type)) {
1018 entry->type = OBJ_COMMIT;
1019 } else if (!strcmp(type, tree_type)) {
1020 entry->type = OBJ_TREE;
1021 } else if (!strcmp(type, blob_type)) {
1022 entry->type = OBJ_BLOB;
1023 } else if (!strcmp(type, tag_type)) {
1024 entry->type = OBJ_TAG;
1025 } else
1026 die("unable to pack object %s of type %s",
1027 sha1_to_hex(entry->sha1), type);
1028 }
1030 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1031 {
1032 struct object_entry *child = me->delta_child;
1033 unsigned int m = n;
1034 while (child) {
1035 unsigned int c = check_delta_limit(child, n + 1);
1036 if (m < c)
1037 m = c;
1038 child = child->delta_sibling;
1039 }
1040 return m;
1041 }
1043 static void get_object_details(void)
1044 {
1045 int i;
1046 struct object_entry *entry;
1048 prepare_pack_ix();
1049 for (i = 0, entry = objects; i < nr_objects; i++, entry++)
1050 check_object(entry);
1052 if (nr_objects == nr_result) {
1053 /*
1054 * Depth of objects that depend on the entry -- this
1055 * is subtracted from depth-max to break too deep
1056 * delta chain because of delta data reusing.
1057 * However, we loosen this restriction when we know we
1058 * are creating a thin pack -- it will have to be
1059 * expanded on the other end anyway, so do not
1060 * artificially cut the delta chain and let it go as
1061 * deep as it wants.
1062 */
1063 for (i = 0, entry = objects; i < nr_objects; i++, entry++)
1064 if (!entry->delta && entry->delta_child)
1065 entry->delta_limit =
1066 check_delta_limit(entry, 1);
1067 }
1068 }
1070 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
1072 static entry_sort_t current_sort;
1074 static int sort_comparator(const void *_a, const void *_b)
1075 {
1076 struct object_entry *a = *(struct object_entry **)_a;
1077 struct object_entry *b = *(struct object_entry **)_b;
1078 return current_sort(a,b);
1079 }
1081 static struct object_entry **create_sorted_list(entry_sort_t sort)
1082 {
1083 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
1084 int i;
1086 for (i = 0; i < nr_objects; i++)
1087 list[i] = objects + i;
1088 current_sort = sort;
1089 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
1090 return list;
1091 }
1093 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
1094 {
1095 return hashcmp(a->sha1, b->sha1);
1096 }
1098 static struct object_entry **create_final_object_list(void)
1099 {
1100 struct object_entry **list;
1101 int i, j;
1103 for (i = nr_result = 0; i < nr_objects; i++)
1104 if (!objects[i].preferred_base)
1105 nr_result++;
1106 list = xmalloc(nr_result * sizeof(struct object_entry *));
1107 for (i = j = 0; i < nr_objects; i++) {
1108 if (!objects[i].preferred_base)
1109 list[j++] = objects + i;
1110 }
1111 current_sort = sha1_sort;
1112 qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);
1113 return list;
1114 }
1116 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
1117 {
1118 if (a->type < b->type)
1119 return -1;
1120 if (a->type > b->type)
1121 return 1;
1122 if (a->hash < b->hash)
1123 return -1;
1124 if (a->hash > b->hash)
1125 return 1;
1126 if (a->preferred_base < b->preferred_base)
1127 return -1;
1128 if (a->preferred_base > b->preferred_base)
1129 return 1;
1130 if (a->size < b->size)
1131 return -1;
1132 if (a->size > b->size)
1133 return 1;
1134 return a < b ? -1 : (a > b);
1135 }
1137 struct unpacked {
1138 struct object_entry *entry;
1139 void *data;
1140 struct delta_index *index;
1141 };
1143 /*
1144 * We search for deltas _backwards_ in a list sorted by type and
1145 * by size, so that we see progressively smaller and smaller files.
1146 * That's because we prefer deltas to be from the bigger file
1147 * to the smaller - deletes are potentially cheaper, but perhaps
1148 * more importantly, the bigger file is likely the more recent
1149 * one.
1150 */
1151 static int try_delta(struct unpacked *trg, struct unpacked *src,
1152 unsigned max_depth)
1153 {
1154 struct object_entry *trg_entry = trg->entry;
1155 struct object_entry *src_entry = src->entry;
1156 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1157 char type[10];
1158 void *delta_buf;
1160 /* Don't bother doing diffs between different types */
1161 if (trg_entry->type != src_entry->type)
1162 return -1;
1164 /* We do not compute delta to *create* objects we are not
1165 * going to pack.
1166 */
1167 if (trg_entry->preferred_base)
1168 return -1;
1170 /*
1171 * We do not bother to try a delta that we discarded
1172 * on an earlier try, but only when reusing delta data.
1173 */
1174 if (!no_reuse_delta && trg_entry->in_pack &&
1175 trg_entry->in_pack == src_entry->in_pack)
1176 return 0;
1178 /*
1179 * If the current object is at pack edge, take the depth the
1180 * objects that depend on the current object into account --
1181 * otherwise they would become too deep.
1182 */
1183 if (trg_entry->delta_child) {
1184 if (max_depth <= trg_entry->delta_limit)
1185 return 0;
1186 max_depth -= trg_entry->delta_limit;
1187 }
1188 if (src_entry->depth >= max_depth)
1189 return 0;
1191 /* Now some size filtering heuristics. */
1192 trg_size = trg_entry->size;
1193 max_size = trg_size/2 - 20;
1194 max_size = max_size * (max_depth - src_entry->depth) / max_depth;
1195 if (max_size == 0)
1196 return 0;
1197 if (trg_entry->delta && trg_entry->delta_size <= max_size)
1198 max_size = trg_entry->delta_size-1;
1199 src_size = src_entry->size;
1200 sizediff = src_size < trg_size ? trg_size - src_size : 0;
1201 if (sizediff >= max_size)
1202 return 0;
1204 /* Load data if not already done */
1205 if (!trg->data) {
1206 trg->data = read_sha1_file(trg_entry->sha1, type, &sz);
1207 if (sz != trg_size)
1208 die("object %s inconsistent object length (%lu vs %lu)",
1209 sha1_to_hex(trg_entry->sha1), sz, trg_size);
1210 }
1211 if (!src->data) {
1212 src->data = read_sha1_file(src_entry->sha1, type, &sz);
1213 if (sz != src_size)
1214 die("object %s inconsistent object length (%lu vs %lu)",
1215 sha1_to_hex(src_entry->sha1), sz, src_size);
1216 }
1217 if (!src->index) {
1218 src->index = create_delta_index(src->data, src_size);
1219 if (!src->index)
1220 die("out of memory");
1221 }
1223 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1224 if (!delta_buf)
1225 return 0;
1227 trg_entry->delta = src_entry;
1228 trg_entry->delta_size = delta_size;
1229 trg_entry->depth = src_entry->depth + 1;
1230 free(delta_buf);
1231 return 1;
1232 }
1234 static void progress_interval(int signum)
1235 {
1236 progress_update = 1;
1237 }
1239 static void find_deltas(struct object_entry **list, int window, int depth)
1240 {
1241 int i, idx;
1242 unsigned int array_size = window * sizeof(struct unpacked);
1243 struct unpacked *array = xmalloc(array_size);
1244 unsigned processed = 0;
1245 unsigned last_percent = 999;
1247 memset(array, 0, array_size);
1248 i = nr_objects;
1249 idx = 0;
1250 if (progress)
1251 fprintf(stderr, "Deltifying %d objects.\n", nr_result);
1253 while (--i >= 0) {
1254 struct object_entry *entry = list[i];
1255 struct unpacked *n = array + idx;
1256 int j;
1258 if (!entry->preferred_base)
1259 processed++;
1261 if (progress) {
1262 unsigned percent = processed * 100 / nr_result;
1263 if (percent != last_percent || progress_update) {
1264 fprintf(stderr, "%4u%% (%u/%u) done\r",
1265 percent, processed, nr_result);
1266 progress_update = 0;
1267 last_percent = percent;
1268 }
1269 }
1271 if (entry->delta)
1272 /* This happens if we decided to reuse existing
1273 * delta from a pack. "!no_reuse_delta &&" is implied.
1274 */
1275 continue;
1277 if (entry->size < 50)
1278 continue;
1279 free_delta_index(n->index);
1280 n->index = NULL;
1281 free(n->data);
1282 n->data = NULL;
1283 n->entry = entry;
1285 j = window;
1286 while (--j > 0) {
1287 unsigned int other_idx = idx + j;
1288 struct unpacked *m;
1289 if (other_idx >= window)
1290 other_idx -= window;
1291 m = array + other_idx;
1292 if (!m->entry)
1293 break;
1294 if (try_delta(n, m, depth) < 0)
1295 break;
1296 }
1297 /* if we made n a delta, and if n is already at max
1298 * depth, leaving it in the window is pointless. we
1299 * should evict it first.
1300 */
1301 if (entry->delta && depth <= entry->depth)
1302 continue;
1304 idx++;
1305 if (idx >= window)
1306 idx = 0;
1307 }
1309 if (progress)
1310 fputc('\n', stderr);
1312 for (i = 0; i < window; ++i) {
1313 free_delta_index(array[i].index);
1314 free(array[i].data);
1315 }
1316 free(array);
1317 }
1319 static void prepare_pack(int window, int depth)
1320 {
1321 get_object_details();
1322 sorted_by_type = create_sorted_list(type_size_sort);
1323 if (window && depth)
1324 find_deltas(sorted_by_type, window+1, depth);
1325 }
1327 static int reuse_cached_pack(unsigned char *sha1)
1328 {
1329 static const char cache[] = "pack-cache/pack-%s.%s";
1330 char *cached_pack, *cached_idx;
1331 int ifd, ofd, ifd_ix = -1;
1333 cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
1334 ifd = open(cached_pack, O_RDONLY);
1335 if (ifd < 0)
1336 return 0;
1338 if (!pack_to_stdout) {
1339 cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
1340 ifd_ix = open(cached_idx, O_RDONLY);
1341 if (ifd_ix < 0) {
1342 close(ifd);
1343 return 0;
1344 }
1345 }
1347 if (progress)
1348 fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
1349 sha1_to_hex(sha1));
1351 if (pack_to_stdout) {
1352 if (copy_fd(ifd, 1))
1353 exit(1);
1354 close(ifd);
1355 }
1356 else {
1357 char name[PATH_MAX];
1358 snprintf(name, sizeof(name),
1359 "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
1360 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
1361 if (ofd < 0)
1362 die("unable to open %s (%s)", name, strerror(errno));
1363 if (copy_fd(ifd, ofd))
1364 exit(1);
1365 close(ifd);
1367 snprintf(name, sizeof(name),
1368 "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
1369 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
1370 if (ofd < 0)
1371 die("unable to open %s (%s)", name, strerror(errno));
1372 if (copy_fd(ifd_ix, ofd))
1373 exit(1);
1374 close(ifd_ix);
1375 puts(sha1_to_hex(sha1));
1376 }
1378 return 1;
1379 }
1381 static void setup_progress_signal(void)
1382 {
1383 struct sigaction sa;
1384 struct itimerval v;
1386 memset(&sa, 0, sizeof(sa));
1387 sa.sa_handler = progress_interval;
1388 sigemptyset(&sa.sa_mask);
1389 sa.sa_flags = SA_RESTART;
1390 sigaction(SIGALRM, &sa, NULL);
1392 v.it_interval.tv_sec = 1;
1393 v.it_interval.tv_usec = 0;
1394 v.it_value = v.it_interval;
1395 setitimer(ITIMER_REAL, &v, NULL);
1396 }
1398 static int git_pack_config(const char *k, const char *v)
1399 {
1400 if(!strcmp(k, "pack.window")) {
1401 window = git_config_int(k, v);
1402 return 0;
1403 }
1404 return git_default_config(k, v);
1405 }
1407 static void read_object_list_from_stdin(void)
1408 {
1409 char line[40 + 1 + PATH_MAX + 2];
1410 unsigned char sha1[20];
1411 unsigned hash;
1413 for (;;) {
1414 if (!fgets(line, sizeof(line), stdin)) {
1415 if (feof(stdin))
1416 break;
1417 if (!ferror(stdin))
1418 die("fgets returned NULL, not EOF, not error!");
1419 if (errno != EINTR)
1420 die("fgets: %s", strerror(errno));
1421 clearerr(stdin);
1422 continue;
1423 }
1424 if (line[0] == '-') {
1425 if (get_sha1_hex(line+1, sha1))
1426 die("expected edge sha1, got garbage:\n %s",
1427 line);
1428 add_preferred_base(sha1);
1429 continue;
1430 }
1431 if (get_sha1_hex(line, sha1))
1432 die("expected sha1, got garbage:\n %s", line);
1434 hash = name_hash(line+41);
1435 add_preferred_base_object(line+41, hash);
1436 add_object_entry(sha1, hash, 0);
1437 }
1438 }
1440 static void show_commit(struct commit *commit)
1441 {
1442 unsigned hash = name_hash("");
1443 add_preferred_base_object("", hash);
1444 add_object_entry(commit->object.sha1, hash, 0);
1445 }
1447 static void show_object(struct object_array_entry *p)
1448 {
1449 unsigned hash = name_hash(p->name);
1450 add_preferred_base_object(p->name, hash);
1451 add_object_entry(p->item->sha1, hash, 0);
1452 }
1454 static void show_edge(struct commit *commit)
1455 {
1456 add_preferred_base(commit->object.sha1);
1457 }
1459 static void get_object_list(int ac, const char **av)
1460 {
1461 struct rev_info revs;
1462 char line[1000];
1463 int flags = 0;
1465 init_revisions(&revs, NULL);
1466 save_commit_buffer = 0;
1467 track_object_refs = 0;
1468 setup_revisions(ac, av, &revs, NULL);
1470 while (fgets(line, sizeof(line), stdin) != NULL) {
1471 int len = strlen(line);
1472 if (line[len - 1] == '\n')
1473 line[--len] = 0;
1474 if (!len)
1475 break;
1476 if (*line == '-') {
1477 if (!strcmp(line, "--not")) {
1478 flags ^= UNINTERESTING;
1479 continue;
1480 }
1481 die("not a rev '%s'", line);
1482 }
1483 if (handle_revision_arg(line, &revs, flags, 1))
1484 die("bad revision '%s'", line);
1485 }
1487 prepare_revision_walk(&revs);
1488 mark_edges_uninteresting(revs.commits, &revs, show_edge);
1489 traverse_commit_list(&revs, show_commit, show_object);
1490 }
1492 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1493 {
1494 SHA_CTX ctx;
1495 int depth = 10;
1496 struct object_entry **list;
1497 int use_internal_rev_list = 0;
1498 int thin = 0;
1499 int i;
1500 const char *rp_av[64];
1501 int rp_ac;
1503 rp_av[0] = "pack-objects";
1504 rp_av[1] = "--objects"; /* --thin will make it --objects-edge */
1505 rp_ac = 2;
1507 git_config(git_pack_config);
1509 progress = isatty(2);
1510 for (i = 1; i < argc; i++) {
1511 const char *arg = argv[i];
1513 if (*arg != '-')
1514 break;
1516 if (!strcmp("--non-empty", arg)) {
1517 non_empty = 1;
1518 continue;
1519 }
1520 if (!strcmp("--local", arg)) {
1521 local = 1;
1522 continue;
1523 }
1524 if (!strcmp("--progress", arg)) {
1525 progress = 1;
1526 continue;
1527 }
1528 if (!strcmp("--incremental", arg)) {
1529 incremental = 1;
1530 continue;
1531 }
1532 if (!strncmp("--window=", arg, 9)) {
1533 char *end;
1534 window = strtoul(arg+9, &end, 0);
1535 if (!arg[9] || *end)
1536 usage(pack_usage);
1537 continue;
1538 }
1539 if (!strncmp("--depth=", arg, 8)) {
1540 char *end;
1541 depth = strtoul(arg+8, &end, 0);
1542 if (!arg[8] || *end)
1543 usage(pack_usage);
1544 continue;
1545 }
1546 if (!strcmp("--progress", arg)) {
1547 progress = 1;
1548 continue;
1549 }
1550 if (!strcmp("-q", arg)) {
1551 progress = 0;
1552 continue;
1553 }
1554 if (!strcmp("--no-reuse-delta", arg)) {
1555 no_reuse_delta = 1;
1556 continue;
1557 }
1558 if (!strcmp("--delta-base-offset", arg)) {
1559 allow_ofs_delta = 1;
1560 continue;
1561 }
1562 if (!strcmp("--stdout", arg)) {
1563 pack_to_stdout = 1;
1564 continue;
1565 }
1566 if (!strcmp("--revs", arg)) {
1567 use_internal_rev_list = 1;
1568 continue;
1569 }
1570 if (!strcmp("--unpacked", arg) ||
1571 !strncmp("--unpacked=", arg, 11) ||
1572 !strcmp("--all", arg)) {
1573 use_internal_rev_list = 1;
1574 if (ARRAY_SIZE(rp_av) - 1 <= rp_ac)
1575 die("too many internal rev-list options");
1576 rp_av[rp_ac++] = arg;
1577 continue;
1578 }
1579 if (!strcmp("--thin", arg)) {
1580 use_internal_rev_list = 1;
1581 thin = 1;
1582 rp_av[1] = "--objects-edge";
1583 continue;
1584 }
1585 usage(pack_usage);
1586 }
1588 /* Traditionally "pack-objects [options] base extra" failed;
1589 * we would however want to take refs parameter that would
1590 * have been given to upstream rev-list ourselves, which means
1591 * we somehow want to say what the base name is. So the
1592 * syntax would be:
1593 *
1594 * pack-objects [options] base <refs...>
1595 *
1596 * in other words, we would treat the first non-option as the
1597 * base_name and send everything else to the internal revision
1598 * walker.
1599 */
1601 if (!pack_to_stdout)
1602 base_name = argv[i++];
1604 if (pack_to_stdout != !base_name)
1605 usage(pack_usage);
1607 if (!pack_to_stdout && thin)
1608 die("--thin cannot be used to build an indexable pack.");
1610 prepare_packed_git();
1612 if (progress) {
1613 fprintf(stderr, "Generating pack...\n");
1614 setup_progress_signal();
1615 }
1617 if (!use_internal_rev_list)
1618 read_object_list_from_stdin();
1619 else {
1620 rp_av[rp_ac] = NULL;
1621 get_object_list(rp_ac, rp_av);
1622 }
1624 if (progress)
1625 fprintf(stderr, "Done counting %d objects.\n", nr_objects);
1626 sorted_by_sha = create_final_object_list();
1627 if (non_empty && !nr_result)
1628 return 0;
1630 SHA1_Init(&ctx);
1631 list = sorted_by_sha;
1632 for (i = 0; i < nr_result; i++) {
1633 struct object_entry *entry = *list++;
1634 SHA1_Update(&ctx, entry->sha1, 20);
1635 }
1636 SHA1_Final(object_list_sha1, &ctx);
1637 if (progress && (nr_objects != nr_result))
1638 fprintf(stderr, "Result has %d objects.\n", nr_result);
1640 if (reuse_cached_pack(object_list_sha1))
1641 ;
1642 else {
1643 if (nr_result)
1644 prepare_pack(window, depth);
1645 if (progress && pack_to_stdout) {
1646 /* the other end usually displays progress itself */
1647 struct itimerval v = {{0,},};
1648 setitimer(ITIMER_REAL, &v, NULL);
1649 signal(SIGALRM, SIG_IGN );
1650 progress_update = 0;
1651 }
1652 write_pack_file();
1653 if (!pack_to_stdout) {
1654 write_index_file();
1655 puts(sha1_to_hex(object_list_sha1));
1656 }
1657 }
1658 if (progress)
1659 fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
1660 nr_result, written, written_delta, reused, reused_delta);
1661 return 0;
1662 }