git-repack: allow passing a couple of flags to pack-objects.
[git/mingw.git] / pack-objects.c
blob38e1c9921bfe0caac14bdb6b9b30f958d2ac0b62
1 #include "cache.h"
2 #include "object.h"
3 #include "delta.h"
4 #include "pack.h"
5 #include "csum-file.h"
6 #include <sys/time.h>
8 static const char pack_usage[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
10 struct object_entry {
11 unsigned char sha1[20];
12 unsigned long size; /* uncompressed size */
13 unsigned long offset; /* offset into the final pack file (nonzero if already written) */
14 unsigned int depth; /* delta depth */
15 unsigned int hash; /* name hint hash */
16 enum object_type type;
17 unsigned char edge; /* reused delta chain points at this entry. */
18 enum object_type in_pack_type; /* could be delta */
19 unsigned long delta_size; /* delta data size (uncompressed) */
20 struct object_entry *delta; /* delta base object */
21 struct packed_git *in_pack; /* already in pack */
22 unsigned int in_pack_offset;
26 * Objects we are going to pack are colected in objects array (dynamically
27 * expanded). nr_objects & nr_alloc controls this array. They are stored
28 * in the order we see -- typically rev-list --objects order that gives us
29 * nice "minimum seek" order.
31 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
32 * elements in the objects array. The former is used to build the pack
33 * index (lists object names in the ascending order to help offset lookup),
34 * and the latter is used to group similar things together by try_delta()
35 * heuristics.
38 static unsigned char object_list_sha1[20];
39 static int non_empty = 0;
40 static int no_reuse_delta = 0;
41 static int local = 0;
42 static int incremental = 0;
43 static struct object_entry **sorted_by_sha, **sorted_by_type;
44 static struct object_entry *objects = NULL;
45 static int nr_objects = 0, nr_alloc = 0;
46 static const char *base_name;
47 static unsigned char pack_file_sha1[20];
48 static int progress = 1;
51 * The object names in objects array are hashed with this hashtable,
52 * to help looking up the entry by object name. Binary search from
53 * sorted_by_sha is also possible but this was easier to code and faster.
54 * This hashtable is built after all the objects are seen.
56 static int *object_ix = NULL;
57 static int object_ix_hashsz = 0;
60 * Pack index for existing packs give us easy access to the offsets into
61 * corresponding pack file where each object's data starts, but the entries
62 * do not store the size of the compressed representation (uncompressed
63 * size is easily available by examining the pack entry header). We build
64 * a hashtable of existing packs (pack_revindex), and keep reverse index
65 * here -- pack index file is sorted by object name mapping to offset; this
66 * pack_revindex[].revindex array is an ordered list of offsets, so if you
67 * know the offset of an object, next offset is where its packed
68 * representation ends.
70 struct pack_revindex {
71 struct packed_git *p;
72 unsigned long *revindex;
73 } *pack_revindex = NULL;
74 static int pack_revindex_hashsz = 0;
77 * stats
79 static int written = 0;
80 static int written_delta = 0;
81 static int reused = 0;
82 static int reused_delta = 0;
84 static int pack_revindex_ix(struct packed_git *p)
86 unsigned int ui = (unsigned int) p;
87 int i;
89 ui = ui ^ (ui >> 16); /* defeat structure alignment */
90 i = (int)(ui % pack_revindex_hashsz);
91 while (pack_revindex[i].p) {
92 if (pack_revindex[i].p == p)
93 return i;
94 if (++i == pack_revindex_hashsz)
95 i = 0;
97 return -1 - i;
100 static void prepare_pack_ix(void)
102 int num;
103 struct packed_git *p;
104 for (num = 0, p = packed_git; p; p = p->next)
105 num++;
106 if (!num)
107 return;
108 pack_revindex_hashsz = num * 11;
109 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
110 for (p = packed_git; p; p = p->next) {
111 num = pack_revindex_ix(p);
112 num = - 1 - num;
113 pack_revindex[num].p = p;
115 /* revindex elements are lazily initialized */
118 static int cmp_offset(const void *a_, const void *b_)
120 unsigned long a = *(unsigned long *) a_;
121 unsigned long b = *(unsigned long *) b_;
122 if (a < b)
123 return -1;
124 else if (a == b)
125 return 0;
126 else
127 return 1;
131 * Ordered list of offsets of objects in the pack.
133 static void prepare_pack_revindex(struct pack_revindex *rix)
135 struct packed_git *p = rix->p;
136 int num_ent = num_packed_objects(p);
137 int i;
138 void *index = p->index_base + 256;
140 rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
141 for (i = 0; i < num_ent; i++) {
142 long hl = *((long *)(index + 24 * i));
143 rix->revindex[i] = ntohl(hl);
145 /* This knows the pack format -- the 20-byte trailer
146 * follows immediately after the last object data.
148 rix->revindex[num_ent] = p->pack_size - 20;
149 qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset);
152 static unsigned long find_packed_object_size(struct packed_git *p,
153 unsigned long ofs)
155 int num;
156 int lo, hi;
157 struct pack_revindex *rix;
158 unsigned long *revindex;
159 num = pack_revindex_ix(p);
160 if (num < 0)
161 die("internal error: pack revindex uninitialized");
162 rix = &pack_revindex[num];
163 if (!rix->revindex)
164 prepare_pack_revindex(rix);
165 revindex = rix->revindex;
166 lo = 0;
167 hi = num_packed_objects(p) + 1;
168 do {
169 int mi = (lo + hi) / 2;
170 if (revindex[mi] == ofs) {
171 return revindex[mi+1] - ofs;
173 else if (ofs < revindex[mi])
174 hi = mi;
175 else
176 lo = mi + 1;
177 } while (lo < hi);
178 die("internal error: pack revindex corrupt");
181 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
183 unsigned long othersize, delta_size;
184 char type[10];
185 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
186 void *delta_buf;
188 if (!otherbuf)
189 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
190 delta_buf = diff_delta(otherbuf, othersize,
191 buf, size, &delta_size, 0);
192 if (!delta_buf || delta_size != entry->delta_size)
193 die("delta size changed");
194 free(buf);
195 free(otherbuf);
196 return delta_buf;
200 * The per-object header is a pretty dense thing, which is
201 * - first byte: low four bits are "size", then three bits of "type",
202 * and the high bit is "size continues".
203 * - each byte afterwards: low seven bits are size continuation,
204 * with the high bit being "size continues"
206 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
208 int n = 1;
209 unsigned char c;
211 if (type < OBJ_COMMIT || type > OBJ_DELTA)
212 die("bad type %d", type);
214 c = (type << 4) | (size & 15);
215 size >>= 4;
216 while (size) {
217 *hdr++ = c | 0x80;
218 c = size & 0x7f;
219 size >>= 7;
220 n++;
222 *hdr = c;
223 return n;
226 static unsigned long write_object(struct sha1file *f, struct object_entry *entry)
228 unsigned long size;
229 char type[10];
230 void *buf;
231 unsigned char header[10];
232 unsigned hdrlen, datalen;
233 enum object_type obj_type;
234 int to_reuse = 0;
236 obj_type = entry->type;
237 if (! entry->in_pack)
238 to_reuse = 0; /* can't reuse what we don't have */
239 else if (obj_type == OBJ_DELTA)
240 to_reuse = 1; /* check_object() decided it for us */
241 else if (obj_type != entry->in_pack_type)
242 to_reuse = 0; /* pack has delta which is unusable */
243 else if (entry->delta)
244 to_reuse = 0; /* we want to pack afresh */
245 else
246 to_reuse = 1; /* we have it in-pack undeltified,
247 * and we do not need to deltify it.
250 if (! to_reuse) {
251 buf = read_sha1_file(entry->sha1, type, &size);
252 if (!buf)
253 die("unable to read %s", sha1_to_hex(entry->sha1));
254 if (size != entry->size)
255 die("object %s size inconsistency (%lu vs %lu)",
256 sha1_to_hex(entry->sha1), size, entry->size);
257 if (entry->delta) {
258 buf = delta_against(buf, size, entry);
259 size = entry->delta_size;
260 obj_type = OBJ_DELTA;
263 * The object header is a byte of 'type' followed by zero or
264 * more bytes of length. For deltas, the 20 bytes of delta
265 * sha1 follows that.
267 hdrlen = encode_header(obj_type, size, header);
268 sha1write(f, header, hdrlen);
270 if (entry->delta) {
271 sha1write(f, entry->delta, 20);
272 hdrlen += 20;
274 datalen = sha1write_compressed(f, buf, size);
275 free(buf);
277 else {
278 struct packed_git *p = entry->in_pack;
279 use_packed_git(p);
281 datalen = find_packed_object_size(p, entry->in_pack_offset);
282 buf = p->pack_base + entry->in_pack_offset;
283 sha1write(f, buf, datalen);
284 unuse_packed_git(p);
285 hdrlen = 0; /* not really */
286 if (obj_type == OBJ_DELTA)
287 reused_delta++;
288 reused++;
290 if (obj_type == OBJ_DELTA)
291 written_delta++;
292 written++;
293 return hdrlen + datalen;
296 static unsigned long write_one(struct sha1file *f,
297 struct object_entry *e,
298 unsigned long offset)
300 if (e->offset)
301 /* offset starts from header size and cannot be zero
302 * if it is written already.
304 return offset;
305 e->offset = offset;
306 offset += write_object(f, e);
307 /* if we are deltified, write out its base object. */
308 if (e->delta)
309 offset = write_one(f, e->delta, offset);
310 return offset;
313 static void write_pack_file(void)
315 int i;
316 struct sha1file *f;
317 unsigned long offset;
318 struct pack_header hdr;
320 if (!base_name)
321 f = sha1fd(1, "<stdout>");
322 else
323 f = sha1create("%s-%s.%s", base_name, sha1_to_hex(object_list_sha1), "pack");
324 hdr.hdr_signature = htonl(PACK_SIGNATURE);
325 hdr.hdr_version = htonl(PACK_VERSION);
326 hdr.hdr_entries = htonl(nr_objects);
327 sha1write(f, &hdr, sizeof(hdr));
328 offset = sizeof(hdr);
329 for (i = 0; i < nr_objects; i++)
330 offset = write_one(f, objects + i, offset);
332 sha1close(f, pack_file_sha1, 1);
335 static void write_index_file(void)
337 int i;
338 struct sha1file *f = sha1create("%s-%s.%s", base_name, sha1_to_hex(object_list_sha1), "idx");
339 struct object_entry **list = sorted_by_sha;
340 struct object_entry **last = list + nr_objects;
341 unsigned int array[256];
344 * Write the first-level table (the list is sorted,
345 * but we use a 256-entry lookup to be able to avoid
346 * having to do eight extra binary search iterations).
348 for (i = 0; i < 256; i++) {
349 struct object_entry **next = list;
350 while (next < last) {
351 struct object_entry *entry = *next;
352 if (entry->sha1[0] != i)
353 break;
354 next++;
356 array[i] = htonl(next - sorted_by_sha);
357 list = next;
359 sha1write(f, array, 256 * sizeof(int));
362 * Write the actual SHA1 entries..
364 list = sorted_by_sha;
365 for (i = 0; i < nr_objects; i++) {
366 struct object_entry *entry = *list++;
367 unsigned int offset = htonl(entry->offset);
368 sha1write(f, &offset, 4);
369 sha1write(f, entry->sha1, 20);
371 sha1write(f, pack_file_sha1, 20);
372 sha1close(f, NULL, 1);
375 static int add_object_entry(unsigned char *sha1, unsigned int hash)
377 unsigned int idx = nr_objects;
378 struct object_entry *entry;
379 struct packed_git *p;
380 unsigned int found_offset = 0;
381 struct packed_git *found_pack = NULL;
383 for (p = packed_git; p; p = p->next) {
384 struct pack_entry e;
385 if (find_pack_entry_one(sha1, &e, p)) {
386 if (incremental)
387 return 0;
388 if (local && !p->pack_local)
389 return 0;
390 if (!found_pack) {
391 found_offset = e.offset;
392 found_pack = e.p;
397 if (idx >= nr_alloc) {
398 unsigned int needed = (idx + 1024) * 3 / 2;
399 objects = xrealloc(objects, needed * sizeof(*entry));
400 nr_alloc = needed;
402 entry = objects + idx;
403 memset(entry, 0, sizeof(*entry));
404 memcpy(entry->sha1, sha1, 20);
405 entry->hash = hash;
406 if (found_pack) {
407 entry->in_pack = found_pack;
408 entry->in_pack_offset = found_offset;
410 nr_objects = idx+1;
411 return 1;
414 static int locate_object_entry_hash(unsigned char *sha1)
416 int i;
417 unsigned int ui;
418 memcpy(&ui, sha1, sizeof(unsigned int));
419 i = ui % object_ix_hashsz;
420 while (0 < object_ix[i]) {
421 if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
422 return i;
423 if (++i == object_ix_hashsz)
424 i = 0;
426 return -1 - i;
429 static struct object_entry *locate_object_entry(unsigned char *sha1)
431 int i = locate_object_entry_hash(sha1);
432 if (0 <= i)
433 return &objects[object_ix[i]-1];
434 return NULL;
437 static void check_object(struct object_entry *entry)
439 char type[20];
441 if (entry->in_pack) {
442 unsigned char base[20];
443 unsigned long size;
444 struct object_entry *base_entry;
446 /* We want in_pack_type even if we do not reuse delta.
447 * There is no point not reusing non-delta representations.
449 check_reuse_pack_delta(entry->in_pack,
450 entry->in_pack_offset,
451 base, &size,
452 &entry->in_pack_type);
454 /* Check if it is delta, and the base is also an object
455 * we are going to pack. If so we will reuse the existing
456 * delta.
458 if (!no_reuse_delta &&
459 entry->in_pack_type == OBJ_DELTA &&
460 (base_entry = locate_object_entry(base))) {
462 /* Depth value does not matter - find_deltas()
463 * will never consider reused delta as the
464 * base object to deltify other objects
465 * against, in order to avoid circular deltas.
468 /* uncompressed size of the delta data */
469 entry->size = entry->delta_size = size;
470 entry->delta = base_entry;
471 entry->type = OBJ_DELTA;
473 base_entry->edge = 1;
475 return;
477 /* Otherwise we would do the usual */
480 if (sha1_object_info(entry->sha1, type, &entry->size))
481 die("unable to get type of object %s",
482 sha1_to_hex(entry->sha1));
484 if (!strcmp(type, "commit")) {
485 entry->type = OBJ_COMMIT;
486 } else if (!strcmp(type, "tree")) {
487 entry->type = OBJ_TREE;
488 } else if (!strcmp(type, "blob")) {
489 entry->type = OBJ_BLOB;
490 } else if (!strcmp(type, "tag")) {
491 entry->type = OBJ_TAG;
492 } else
493 die("unable to pack object %s of type %s",
494 sha1_to_hex(entry->sha1), type);
497 static void hash_objects(void)
499 int i;
500 struct object_entry *oe;
502 object_ix_hashsz = nr_objects * 2;
503 object_ix = xcalloc(sizeof(int), object_ix_hashsz);
504 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
505 int ix = locate_object_entry_hash(oe->sha1);
506 if (0 <= ix) {
507 error("the same object '%s' added twice",
508 sha1_to_hex(oe->sha1));
509 continue;
511 ix = -1 - ix;
512 object_ix[ix] = i + 1;
516 static void get_object_details(void)
518 int i;
519 struct object_entry *entry = objects;
521 hash_objects();
522 prepare_pack_ix();
523 for (i = 0; i < nr_objects; i++)
524 check_object(entry++);
527 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
529 static entry_sort_t current_sort;
531 static int sort_comparator(const void *_a, const void *_b)
533 struct object_entry *a = *(struct object_entry **)_a;
534 struct object_entry *b = *(struct object_entry **)_b;
535 return current_sort(a,b);
538 static struct object_entry **create_sorted_list(entry_sort_t sort)
540 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
541 int i;
543 for (i = 0; i < nr_objects; i++)
544 list[i] = objects + i;
545 current_sort = sort;
546 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
547 return list;
550 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
552 return memcmp(a->sha1, b->sha1, 20);
555 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
557 if (a->type < b->type)
558 return -1;
559 if (a->type > b->type)
560 return 1;
561 if (a->hash < b->hash)
562 return -1;
563 if (a->hash > b->hash)
564 return 1;
565 if (a->size < b->size)
566 return -1;
567 if (a->size > b->size)
568 return 1;
569 return a < b ? -1 : (a > b);
572 struct unpacked {
573 struct object_entry *entry;
574 void *data;
578 * We search for deltas _backwards_ in a list sorted by type and
579 * by size, so that we see progressively smaller and smaller files.
580 * That's because we prefer deltas to be from the bigger file
581 * to the smaller - deletes are potentially cheaper, but perhaps
582 * more importantly, the bigger file is likely the more recent
583 * one.
585 static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
587 struct object_entry *cur_entry = cur->entry;
588 struct object_entry *old_entry = old->entry;
589 unsigned long size, oldsize, delta_size, sizediff;
590 long max_size;
591 void *delta_buf;
593 /* Don't bother doing diffs between different types */
594 if (cur_entry->type != old_entry->type)
595 return -1;
597 /* If the current object is at edge, take the depth the objects
598 * that depend on the current object into account -- otherwise
599 * they would become too deep.
601 if (cur_entry->edge)
602 max_depth /= 4;
604 size = cur_entry->size;
605 if (size < 50)
606 return -1;
607 oldsize = old_entry->size;
608 sizediff = oldsize > size ? oldsize - size : size - oldsize;
609 if (sizediff > size / 8)
610 return -1;
611 if (old_entry->depth >= max_depth)
612 return 0;
615 * NOTE!
617 * We always delta from the bigger to the smaller, since that's
618 * more space-efficient (deletes don't have to say _what_ they
619 * delete).
621 max_size = size / 2 - 20;
622 if (cur_entry->delta)
623 max_size = cur_entry->delta_size-1;
624 if (sizediff >= max_size)
625 return -1;
626 delta_buf = diff_delta(old->data, oldsize,
627 cur->data, size, &delta_size, max_size);
628 if (!delta_buf)
629 return 0;
630 cur_entry->delta = old_entry;
631 cur_entry->delta_size = delta_size;
632 cur_entry->depth = old_entry->depth + 1;
633 free(delta_buf);
634 return 0;
637 static void find_deltas(struct object_entry **list, int window, int depth)
639 int i, idx;
640 unsigned int array_size = window * sizeof(struct unpacked);
641 struct unpacked *array = xmalloc(array_size);
642 int eye_candy;
644 memset(array, 0, array_size);
645 i = nr_objects;
646 idx = 0;
647 eye_candy = i - (nr_objects / 20);
649 while (--i >= 0) {
650 struct object_entry *entry = list[i];
651 struct unpacked *n = array + idx;
652 unsigned long size;
653 char type[10];
654 int j;
656 if (progress && i <= eye_candy) {
657 eye_candy -= nr_objects / 20;
658 fputc('.', stderr);
661 if (entry->delta)
662 /* This happens if we decided to reuse existing
663 * delta from a pack. "!no_reuse_delta &&" is implied.
665 continue;
667 free(n->data);
668 n->entry = entry;
669 n->data = read_sha1_file(entry->sha1, type, &size);
670 if (size != entry->size)
671 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
673 j = window;
674 while (--j > 0) {
675 unsigned int other_idx = idx + j;
676 struct unpacked *m;
677 if (other_idx >= window)
678 other_idx -= window;
679 m = array + other_idx;
680 if (!m->entry)
681 break;
682 if (try_delta(n, m, depth) < 0)
683 break;
685 idx++;
686 if (idx >= window)
687 idx = 0;
690 for (i = 0; i < window; ++i)
691 free(array[i].data);
692 free(array);
695 static void prepare_pack(int window, int depth)
697 if (progress)
698 fprintf(stderr, "Packing %d objects", nr_objects);
699 get_object_details();
700 if (progress)
701 fputc('.', stderr);
703 sorted_by_type = create_sorted_list(type_size_sort);
704 if (window && depth)
705 find_deltas(sorted_by_type, window+1, depth);
706 if (progress)
707 fputc('\n', stderr);
708 write_pack_file();
711 static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
713 static const char cache[] = "pack-cache/pack-%s.%s";
714 char *cached_pack, *cached_idx;
715 int ifd, ofd, ifd_ix = -1;
717 cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
718 ifd = open(cached_pack, O_RDONLY);
719 if (ifd < 0)
720 return 0;
722 if (!pack_to_stdout) {
723 cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
724 ifd_ix = open(cached_idx, O_RDONLY);
725 if (ifd_ix < 0) {
726 close(ifd);
727 return 0;
731 if (progress)
732 fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
733 sha1_to_hex(sha1));
735 if (pack_to_stdout) {
736 if (copy_fd(ifd, 1))
737 exit(1);
738 close(ifd);
740 else {
741 char name[PATH_MAX];
742 snprintf(name, sizeof(name),
743 "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
744 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
745 if (ofd < 0)
746 die("unable to open %s (%s)", name, strerror(errno));
747 if (copy_fd(ifd, ofd))
748 exit(1);
749 close(ifd);
751 snprintf(name, sizeof(name),
752 "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
753 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
754 if (ofd < 0)
755 die("unable to open %s (%s)", name, strerror(errno));
756 if (copy_fd(ifd_ix, ofd))
757 exit(1);
758 close(ifd_ix);
759 puts(sha1_to_hex(sha1));
762 return 1;
765 int main(int argc, char **argv)
767 SHA_CTX ctx;
768 char line[PATH_MAX + 20];
769 int window = 10, depth = 10, pack_to_stdout = 0;
770 struct object_entry **list;
771 int i;
772 struct timeval prev_tv;
773 int eye_candy = 0;
774 int eye_candy_incr = 500;
777 setup_git_directory();
779 for (i = 1; i < argc; i++) {
780 const char *arg = argv[i];
782 if (*arg == '-') {
783 if (!strcmp("--non-empty", arg)) {
784 non_empty = 1;
785 continue;
787 if (!strcmp("--local", arg)) {
788 local = 1;
789 continue;
791 if (!strcmp("--incremental", arg)) {
792 incremental = 1;
793 continue;
795 if (!strncmp("--window=", arg, 9)) {
796 char *end;
797 window = strtoul(arg+9, &end, 0);
798 if (!arg[9] || *end)
799 usage(pack_usage);
800 continue;
802 if (!strncmp("--depth=", arg, 8)) {
803 char *end;
804 depth = strtoul(arg+8, &end, 0);
805 if (!arg[8] || *end)
806 usage(pack_usage);
807 continue;
809 if (!strcmp("-q", arg)) {
810 progress = 0;
811 continue;
813 if (!strcmp("--no-reuse-delta", arg)) {
814 no_reuse_delta = 1;
815 continue;
817 if (!strcmp("--stdout", arg)) {
818 pack_to_stdout = 1;
819 continue;
821 usage(pack_usage);
823 if (base_name)
824 usage(pack_usage);
825 base_name = arg;
828 if (pack_to_stdout != !base_name)
829 usage(pack_usage);
831 prepare_packed_git();
832 if (progress) {
833 fprintf(stderr, "Generating pack...\n");
834 gettimeofday(&prev_tv, NULL);
836 while (fgets(line, sizeof(line), stdin) != NULL) {
837 unsigned int hash;
838 char *p;
839 unsigned char sha1[20];
841 if (progress && (eye_candy <= nr_objects)) {
842 fprintf(stderr, "Counting objects...%d\r", nr_objects);
843 if (eye_candy && (50 <= eye_candy_incr)) {
844 struct timeval tv;
845 int time_diff;
846 gettimeofday(&tv, NULL);
847 time_diff = (tv.tv_sec - prev_tv.tv_sec);
848 time_diff <<= 10;
849 time_diff += (tv.tv_usec - prev_tv.tv_usec);
850 if ((1 << 9) < time_diff)
851 eye_candy_incr += 50;
852 else if (50 < eye_candy_incr)
853 eye_candy_incr -= 50;
855 eye_candy += eye_candy_incr;
857 if (get_sha1_hex(line, sha1))
858 die("expected sha1, got garbage:\n %s", line);
859 hash = 0;
860 p = line+40;
861 while (*p) {
862 unsigned char c = *p++;
863 if (isspace(c))
864 continue;
865 hash = hash * 11 + c;
867 add_object_entry(sha1, hash);
869 if (progress)
870 fprintf(stderr, "Done counting %d objects.\n", nr_objects);
871 if (non_empty && !nr_objects)
872 return 0;
874 sorted_by_sha = create_sorted_list(sha1_sort);
875 SHA1_Init(&ctx);
876 list = sorted_by_sha;
877 for (i = 0; i < nr_objects; i++) {
878 struct object_entry *entry = *list++;
879 SHA1_Update(&ctx, entry->sha1, 20);
881 SHA1_Final(object_list_sha1, &ctx);
883 if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
885 else {
886 prepare_pack(window, depth);
887 if (!pack_to_stdout) {
888 write_index_file();
889 puts(sha1_to_hex(object_list_sha1));
892 if (progress)
893 fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
894 nr_objects, written, written_delta, reused, reused_delta);
895 return 0;