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