9 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";
12 unsigned char sha1[20];
13 unsigned long size; /* uncompressed size */
14 unsigned long offset; /* offset into the final pack file;
15 * nonzero if already written.
17 unsigned int depth; /* delta depth */
18 unsigned int delta_limit; /* base adjustment for in-pack delta */
19 unsigned int hash; /* name hint hash */
20 enum object_type type;
21 enum object_type in_pack_type; /* could be delta */
22 unsigned long delta_size; /* delta data size (uncompressed) */
23 struct object_entry *delta; /* delta base object */
24 struct packed_git *in_pack; /* already in pack */
25 unsigned int in_pack_offset;
26 struct object_entry *delta_child; /* delitified objects who bases me */
27 struct object_entry *delta_sibling; /* other deltified objects who
28 * uses the same base as me
30 int preferred_base; /* we do not pack this, but is encouraged to
31 * be used as the base objectto delta huge
34 int based_on_preferred; /* current delta candidate is a preferred
35 * one, or delta against a preferred one.
40 * Objects we are going to pack are colected in objects array (dynamically
41 * expanded). nr_objects & nr_alloc controls this array. They are stored
42 * in the order we see -- typically rev-list --objects order that gives us
43 * nice "minimum seek" order.
45 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
46 * elements in the objects array. The former is used to build the pack
47 * index (lists object names in the ascending order to help offset lookup),
48 * and the latter is used to group similar things together by try_delta()
52 static unsigned char object_list_sha1[20];
53 static int non_empty = 0;
54 static int no_reuse_delta = 0;
56 static int incremental = 0;
57 static struct object_entry **sorted_by_sha, **sorted_by_type;
58 static struct object_entry *objects = NULL;
59 static int nr_objects = 0, nr_alloc = 0, nr_result = 0;
60 static const char *base_name;
61 static unsigned char pack_file_sha1[20];
62 static int progress = 1;
65 * The object names in objects array are hashed with this hashtable,
66 * to help looking up the entry by object name. Binary search from
67 * sorted_by_sha is also possible but this was easier to code and faster.
68 * This hashtable is built after all the objects are seen.
70 static int *object_ix = NULL;
71 static int object_ix_hashsz = 0;
74 * Pack index for existing packs give us easy access to the offsets into
75 * corresponding pack file where each object's data starts, but the entries
76 * do not store the size of the compressed representation (uncompressed
77 * size is easily available by examining the pack entry header). We build
78 * a hashtable of existing packs (pack_revindex), and keep reverse index
79 * here -- pack index file is sorted by object name mapping to offset; this
80 * pack_revindex[].revindex array is an ordered list of offsets, so if you
81 * know the offset of an object, next offset is where its packed
82 * representation ends.
84 struct pack_revindex {
86 unsigned long *revindex;
87 } *pack_revindex = NULL;
88 static int pack_revindex_hashsz = 0;
93 static int written = 0;
94 static int written_delta = 0;
95 static int reused = 0;
96 static int reused_delta = 0;
98 static int pack_revindex_ix(struct packed_git *p)
100 unsigned int ui = (unsigned int) p;
103 ui = ui ^ (ui >> 16); /* defeat structure alignment */
104 i = (int)(ui % pack_revindex_hashsz);
105 while (pack_revindex[i].p) {
106 if (pack_revindex[i].p == p)
108 if (++i == pack_revindex_hashsz)
114 static void prepare_pack_ix(void)
117 struct packed_git *p;
118 for (num = 0, p = packed_git; p; p = p->next)
122 pack_revindex_hashsz = num * 11;
123 pack_revindex = xcalloc(sizeof(*pack_revindex), pack_revindex_hashsz);
124 for (p = packed_git; p; p = p->next) {
125 num = pack_revindex_ix(p);
127 pack_revindex[num].p = p;
129 /* revindex elements are lazily initialized */
132 static int cmp_offset(const void *a_, const void *b_)
134 unsigned long a = *(unsigned long *) a_;
135 unsigned long b = *(unsigned long *) b_;
145 * Ordered list of offsets of objects in the pack.
147 static void prepare_pack_revindex(struct pack_revindex *rix)
149 struct packed_git *p = rix->p;
150 int num_ent = num_packed_objects(p);
152 void *index = p->index_base + 256;
154 rix->revindex = xmalloc(sizeof(unsigned long) * (num_ent + 1));
155 for (i = 0; i < num_ent; i++) {
156 long hl = *((long *)(index + 24 * i));
157 rix->revindex[i] = ntohl(hl);
159 /* This knows the pack format -- the 20-byte trailer
160 * follows immediately after the last object data.
162 rix->revindex[num_ent] = p->pack_size - 20;
163 qsort(rix->revindex, num_ent, sizeof(unsigned long), cmp_offset);
166 static unsigned long find_packed_object_size(struct packed_git *p,
171 struct pack_revindex *rix;
172 unsigned long *revindex;
173 num = pack_revindex_ix(p);
175 die("internal error: pack revindex uninitialized");
176 rix = &pack_revindex[num];
178 prepare_pack_revindex(rix);
179 revindex = rix->revindex;
181 hi = num_packed_objects(p) + 1;
183 int mi = (lo + hi) / 2;
184 if (revindex[mi] == ofs) {
185 return revindex[mi+1] - ofs;
187 else if (ofs < revindex[mi])
192 die("internal error: pack revindex corrupt");
195 static void *delta_against(void *buf, unsigned long size, struct object_entry *entry)
197 unsigned long othersize, delta_size;
199 void *otherbuf = read_sha1_file(entry->delta->sha1, type, &othersize);
203 die("unable to read %s", sha1_to_hex(entry->delta->sha1));
204 delta_buf = diff_delta(otherbuf, othersize,
205 buf, size, &delta_size, 0);
206 if (!delta_buf || delta_size != entry->delta_size)
207 die("delta size changed");
214 * The per-object header is a pretty dense thing, which is
215 * - first byte: low four bits are "size", then three bits of "type",
216 * and the high bit is "size continues".
217 * - each byte afterwards: low seven bits are size continuation,
218 * with the high bit being "size continues"
220 static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
225 if (type < OBJ_COMMIT || type > OBJ_DELTA)
226 die("bad type %d", type);
228 c = (type << 4) | (size & 15);
240 static unsigned long write_object(struct sha1file *f,
241 struct object_entry *entry)
246 unsigned char header[10];
247 unsigned hdrlen, datalen;
248 enum object_type obj_type;
251 if (entry->preferred_base)
254 obj_type = entry->type;
255 if (! entry->in_pack)
256 to_reuse = 0; /* can't reuse what we don't have */
257 else if (obj_type == OBJ_DELTA)
258 to_reuse = 1; /* check_object() decided it for us */
259 else if (obj_type != entry->in_pack_type)
260 to_reuse = 0; /* pack has delta which is unusable */
261 else if (entry->delta)
262 to_reuse = 0; /* we want to pack afresh */
264 to_reuse = 1; /* we have it in-pack undeltified,
265 * and we do not need to deltify it.
269 buf = read_sha1_file(entry->sha1, type, &size);
271 die("unable to read %s", sha1_to_hex(entry->sha1));
272 if (size != entry->size)
273 die("object %s size inconsistency (%lu vs %lu)",
274 sha1_to_hex(entry->sha1), size, entry->size);
276 buf = delta_against(buf, size, entry);
277 size = entry->delta_size;
278 obj_type = OBJ_DELTA;
281 * The object header is a byte of 'type' followed by zero or
282 * more bytes of length. For deltas, the 20 bytes of delta
285 hdrlen = encode_header(obj_type, size, header);
286 sha1write(f, header, hdrlen);
289 sha1write(f, entry->delta, 20);
292 datalen = sha1write_compressed(f, buf, size);
296 struct packed_git *p = entry->in_pack;
299 datalen = find_packed_object_size(p, entry->in_pack_offset);
300 buf = p->pack_base + entry->in_pack_offset;
301 sha1write(f, buf, datalen);
303 hdrlen = 0; /* not really */
304 if (obj_type == OBJ_DELTA)
308 if (obj_type == OBJ_DELTA)
311 return hdrlen + datalen;
314 static unsigned long write_one(struct sha1file *f,
315 struct object_entry *e,
316 unsigned long offset)
319 /* offset starts from header size and cannot be zero
320 * if it is written already.
324 offset += write_object(f, e);
325 /* if we are deltified, write out its base object. */
327 offset = write_one(f, e->delta, offset);
331 static void write_pack_file(void)
335 unsigned long offset;
336 struct pack_header hdr;
339 f = sha1fd(1, "<stdout>");
341 f = sha1create("%s-%s.%s", base_name,
342 sha1_to_hex(object_list_sha1), "pack");
343 hdr.hdr_signature = htonl(PACK_SIGNATURE);
344 hdr.hdr_version = htonl(PACK_VERSION);
345 hdr.hdr_entries = htonl(nr_result);
346 sha1write(f, &hdr, sizeof(hdr));
347 offset = sizeof(hdr);
348 for (i = 0; i < nr_objects; i++)
349 offset = write_one(f, objects + i, offset);
351 sha1close(f, pack_file_sha1, 1);
354 static void write_index_file(void)
357 struct sha1file *f = sha1create("%s-%s.%s", base_name,
358 sha1_to_hex(object_list_sha1), "idx");
359 struct object_entry **list = sorted_by_sha;
360 struct object_entry **last = list + nr_result;
361 unsigned int array[256];
364 * Write the first-level table (the list is sorted,
365 * but we use a 256-entry lookup to be able to avoid
366 * having to do eight extra binary search iterations).
368 for (i = 0; i < 256; i++) {
369 struct object_entry **next = list;
370 while (next < last) {
371 struct object_entry *entry = *next;
372 if (entry->sha1[0] != i)
376 array[i] = htonl(next - sorted_by_sha);
379 sha1write(f, array, 256 * sizeof(int));
382 * Write the actual SHA1 entries..
384 list = sorted_by_sha;
385 for (i = 0; i < nr_result; i++) {
386 struct object_entry *entry = *list++;
387 unsigned int offset = htonl(entry->offset);
388 sha1write(f, &offset, 4);
389 sha1write(f, entry->sha1, 20);
391 sha1write(f, pack_file_sha1, 20);
392 sha1close(f, NULL, 1);
395 static int locate_object_entry_hash(const unsigned char *sha1)
399 memcpy(&ui, sha1, sizeof(unsigned int));
400 i = ui % object_ix_hashsz;
401 while (0 < object_ix[i]) {
402 if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
404 if (++i == object_ix_hashsz)
410 static struct object_entry *locate_object_entry(const unsigned char *sha1)
414 if (!object_ix_hashsz)
417 i = locate_object_entry_hash(sha1);
419 return &objects[object_ix[i]-1];
423 static void rehash_objects(void)
426 struct object_entry *oe;
428 object_ix_hashsz = nr_objects * 3;
429 if (object_ix_hashsz < 1024)
430 object_ix_hashsz = 1024;
431 object_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);
432 object_ix = memset(object_ix, 0, sizeof(int) * object_ix_hashsz);
433 for (i = 0, oe = objects; i < nr_objects; i++, oe++) {
434 int ix = locate_object_entry_hash(oe->sha1);
438 object_ix[ix] = i + 1;
443 struct name_path *up;
450 static unsigned name_hash(struct name_path *path, const char *name)
452 struct name_path *p = path;
453 const char *n = name + strlen(name);
454 unsigned hash = 0, name_hash = 0, name_done = 0;
456 if (n != name && n[-1] == '\n')
458 while (name <= --n) {
459 unsigned char c = *n;
460 if (c == '/' && !name_done) {
465 hash = hash * 11 + c;
471 for (p = path; p; p = p->up) {
472 hash = hash * 11 + '/';
473 n = p->elem + p->len;
474 while (p->elem <= --n) {
475 unsigned char c = *n;
476 hash = hash * 11 + c;
480 * Make sure "Makefile" and "t/Makefile" are hashed separately
483 hash = (name_hash<<DIRBITS) | (hash & ((1U<<DIRBITS )-1));
486 n = name + strlen(name);
487 if (n != name && n[-1] == '\n')
491 for (p = path; p; p = p->up) {
493 n = p->elem + p->len;
494 while (p->elem <= --n)
497 fprintf(stderr, "\t%08x\n", hash);
502 static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
504 unsigned int idx = nr_objects;
505 struct object_entry *entry;
506 struct packed_git *p;
507 unsigned int found_offset = 0;
508 struct packed_git *found_pack = NULL;
512 for (p = packed_git; p; p = p->next) {
514 if (find_pack_entry_one(sha1, &e, p)) {
517 if (local && !p->pack_local)
520 found_offset = e.offset;
526 if ((entry = locate_object_entry(sha1)) != NULL)
529 if (idx >= nr_alloc) {
530 unsigned int needed = (idx + 1024) * 3 / 2;
531 objects = xrealloc(objects, needed * sizeof(*entry));
534 entry = objects + idx;
535 nr_objects = idx + 1;
536 memset(entry, 0, sizeof(*entry));
537 memcpy(entry->sha1, sha1, 20);
540 if (object_ix_hashsz * 3 <= nr_objects * 4)
543 ix = locate_object_entry_hash(entry->sha1);
545 die("internal error in object hashing.");
546 object_ix[-1 - ix] = idx + 1;
552 entry->preferred_base = 1;
555 entry->in_pack = found_pack;
556 entry->in_pack_offset = found_offset;
562 static void add_pbase_tree(struct tree_desc *tree, struct name_path *up)
565 const unsigned char *sha1;
571 sha1 = tree_entry_extract(tree, &name, &mode);
572 update_tree_entry(tree);
573 if (!has_sha1_file(sha1))
575 if (sha1_object_info(sha1, type, &size))
578 hash = name_hash(up, name);
579 if (!add_object_entry(sha1, hash, 1))
582 if (!strcmp(type, "tree")) {
583 struct tree_desc sub;
587 elem = read_sha1_file(sha1, type, &sub.size);
592 me.len = strlen(name);
593 add_pbase_tree(&sub, &me);
600 static void add_preferred_base(unsigned char *sha1)
602 struct tree_desc tree;
605 elem = read_object_with_reference(sha1, "tree", &tree.size, NULL);
609 if (add_object_entry(sha1, name_hash(NULL, ""), 1))
610 add_pbase_tree(&tree, NULL);
614 static void check_object(struct object_entry *entry)
618 if (entry->in_pack && !entry->preferred_base) {
619 unsigned char base[20];
621 struct object_entry *base_entry;
623 /* We want in_pack_type even if we do not reuse delta.
624 * There is no point not reusing non-delta representations.
626 check_reuse_pack_delta(entry->in_pack,
627 entry->in_pack_offset,
629 &entry->in_pack_type);
631 /* Check if it is delta, and the base is also an object
632 * we are going to pack. If so we will reuse the existing
635 if (!no_reuse_delta &&
636 entry->in_pack_type == OBJ_DELTA &&
637 (base_entry = locate_object_entry(base)) &&
638 (!base_entry->preferred_base)) {
640 /* Depth value does not matter - find_deltas()
641 * will never consider reused delta as the
642 * base object to deltify other objects
643 * against, in order to avoid circular deltas.
646 /* uncompressed size of the delta data */
647 entry->size = entry->delta_size = size;
648 entry->delta = base_entry;
649 entry->type = OBJ_DELTA;
651 entry->delta_sibling = base_entry->delta_child;
652 base_entry->delta_child = entry;
656 /* Otherwise we would do the usual */
659 if (sha1_object_info(entry->sha1, type, &entry->size))
660 die("unable to get type of object %s",
661 sha1_to_hex(entry->sha1));
663 if (!strcmp(type, "commit")) {
664 entry->type = OBJ_COMMIT;
665 } else if (!strcmp(type, "tree")) {
666 entry->type = OBJ_TREE;
667 } else if (!strcmp(type, "blob")) {
668 entry->type = OBJ_BLOB;
669 } else if (!strcmp(type, "tag")) {
670 entry->type = OBJ_TAG;
672 die("unable to pack object %s of type %s",
673 sha1_to_hex(entry->sha1), type);
676 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
678 struct object_entry *child = me->delta_child;
681 unsigned int c = check_delta_limit(child, n + 1);
684 child = child->delta_sibling;
689 static void get_object_details(void)
692 struct object_entry *entry;
695 for (i = 0, entry = objects; i < nr_objects; i++, entry++)
698 if (nr_objects == nr_result) {
700 * Depth of objects that depend on the entry -- this
701 * is subtracted from depth-max to break too deep
702 * delta chain because of delta data reusing.
703 * However, we loosen this restriction when we know we
704 * are creating a thin pack -- it will have to be
705 * expanded on the other end anyway, so do not
706 * artificially cut the delta chain and let it go as
709 for (i = 0, entry = objects; i < nr_objects; i++, entry++)
710 if (!entry->delta && entry->delta_child)
712 check_delta_limit(entry, 1);
716 typedef int (*entry_sort_t)(const struct object_entry *, const struct object_entry *);
718 static entry_sort_t current_sort;
720 static int sort_comparator(const void *_a, const void *_b)
722 struct object_entry *a = *(struct object_entry **)_a;
723 struct object_entry *b = *(struct object_entry **)_b;
724 return current_sort(a,b);
727 static struct object_entry **create_sorted_list(entry_sort_t sort)
729 struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
732 for (i = 0; i < nr_objects; i++)
733 list[i] = objects + i;
735 qsort(list, nr_objects, sizeof(struct object_entry *), sort_comparator);
739 static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
741 return memcmp(a->sha1, b->sha1, 20);
744 static struct object_entry **create_final_object_list()
746 struct object_entry **list;
749 for (i = nr_result = 0; i < nr_objects; i++)
750 if (!objects[i].preferred_base)
752 list = xmalloc(nr_result * sizeof(struct object_entry *));
753 for (i = j = 0; i < nr_objects; i++) {
754 if (!objects[i].preferred_base)
755 list[j++] = objects + i;
757 current_sort = sha1_sort;
758 qsort(list, nr_result, sizeof(struct object_entry *), sort_comparator);
762 static int type_size_sort(const struct object_entry *a, const struct object_entry *b)
764 if (a->type < b->type)
766 if (a->type > b->type)
768 if (a->hash < b->hash)
770 if (a->hash > b->hash)
772 if (a->preferred_base < b->preferred_base)
774 if (a->preferred_base > b->preferred_base)
776 if (a->size < b->size)
778 if (a->size > b->size)
780 return a < b ? -1 : (a > b);
784 struct object_entry *entry;
789 * We search for deltas _backwards_ in a list sorted by type and
790 * by size, so that we see progressively smaller and smaller files.
791 * That's because we prefer deltas to be from the bigger file
792 * to the smaller - deletes are potentially cheaper, but perhaps
793 * more importantly, the bigger file is likely the more recent
796 static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_depth)
798 struct object_entry *cur_entry = cur->entry;
799 struct object_entry *old_entry = old->entry;
800 int old_preferred = (old_entry->preferred_base ||
801 old_entry->based_on_preferred);
802 unsigned long size, oldsize, delta_size, sizediff;
806 /* Don't bother doing diffs between different types */
807 if (cur_entry->type != old_entry->type)
810 /* We do not compute delta to *create* objects we are not
813 if (cur_entry->preferred_base)
816 /* If the current object is at pack edge, take the depth the
817 * objects that depend on the current object into account --
818 * otherwise they would become too deep.
820 if (cur_entry->delta_child) {
821 if (max_depth <= cur_entry->delta_limit)
823 max_depth -= cur_entry->delta_limit;
826 size = cur_entry->size;
829 oldsize = old_entry->size;
830 sizediff = oldsize > size ? oldsize - size : size - oldsize;
831 if (sizediff > size / 8)
833 if (old_entry->depth >= max_depth)
839 * We always delta from the bigger to the smaller, since that's
840 * more space-efficient (deletes don't have to say _what_ they
843 max_size = size / 2 - 20;
844 if (cur_entry->delta) {
845 if (cur_entry->based_on_preferred) {
847 max_size = cur_entry->delta_size-1;
849 /* trying with non-preferred one when we
850 * already have a delta based on preferred
855 else if (!old_preferred)
856 max_size = cur_entry->delta_size-1;
858 /* otherwise... even if delta with a
859 * preferred one produces a bigger result than
860 * what we currently have, which is based on a
861 * non-preferred one, it is OK.
865 if (sizediff >= max_size)
867 delta_buf = diff_delta(old->data, oldsize,
868 cur->data, size, &delta_size, max_size);
871 cur_entry->delta = old_entry;
872 cur_entry->delta_size = delta_size;
873 cur_entry->depth = old_entry->depth + 1;
874 cur_entry->based_on_preferred = old_preferred;
879 static void find_deltas(struct object_entry **list, int window, int depth)
882 unsigned int array_size = window * sizeof(struct unpacked);
883 struct unpacked *array = xmalloc(array_size);
886 memset(array, 0, array_size);
889 eye_candy = i - (nr_objects / 20);
892 struct object_entry *entry = list[i];
893 struct unpacked *n = array + idx;
898 if (progress && i <= eye_candy) {
899 eye_candy -= nr_objects / 20;
904 /* This happens if we decided to reuse existing
905 * delta from a pack. "!no_reuse_delta &&" is implied.
911 n->data = read_sha1_file(entry->sha1, type, &size);
912 if (size != entry->size)
913 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry->sha1), size, entry->size);
917 unsigned int other_idx = idx + j;
919 if (other_idx >= window)
921 m = array + other_idx;
924 if (try_delta(n, m, depth) < 0)
932 for (i = 0; i < window; ++i)
937 static void prepare_pack(int window, int depth)
940 fprintf(stderr, "Packing %d objects", nr_result);
941 get_object_details();
945 sorted_by_type = create_sorted_list(type_size_sort);
947 find_deltas(sorted_by_type, window+1, depth);
953 static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
955 static const char cache[] = "pack-cache/pack-%s.%s";
956 char *cached_pack, *cached_idx;
957 int ifd, ofd, ifd_ix = -1;
959 cached_pack = git_path(cache, sha1_to_hex(sha1), "pack");
960 ifd = open(cached_pack, O_RDONLY);
964 if (!pack_to_stdout) {
965 cached_idx = git_path(cache, sha1_to_hex(sha1), "idx");
966 ifd_ix = open(cached_idx, O_RDONLY);
974 fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
977 if (pack_to_stdout) {
984 snprintf(name, sizeof(name),
985 "%s-%s.%s", base_name, sha1_to_hex(sha1), "pack");
986 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
988 die("unable to open %s (%s)", name, strerror(errno));
989 if (copy_fd(ifd, ofd))
993 snprintf(name, sizeof(name),
994 "%s-%s.%s", base_name, sha1_to_hex(sha1), "idx");
995 ofd = open(name, O_CREAT | O_EXCL | O_WRONLY, 0666);
997 die("unable to open %s (%s)", name, strerror(errno));
998 if (copy_fd(ifd_ix, ofd))
1001 puts(sha1_to_hex(sha1));
1007 int main(int argc, char **argv)
1010 char line[PATH_MAX + 20];
1011 int window = 10, depth = 10, pack_to_stdout = 0;
1012 struct object_entry **list;
1014 struct timeval prev_tv;
1016 int eye_candy_incr = 500;
1019 setup_git_directory();
1021 for (i = 1; i < argc; i++) {
1022 const char *arg = argv[i];
1025 if (!strcmp("--non-empty", arg)) {
1029 if (!strcmp("--local", arg)) {
1033 if (!strcmp("--incremental", arg)) {
1037 if (!strncmp("--window=", arg, 9)) {
1039 window = strtoul(arg+9, &end, 0);
1040 if (!arg[9] || *end)
1044 if (!strncmp("--depth=", arg, 8)) {
1046 depth = strtoul(arg+8, &end, 0);
1047 if (!arg[8] || *end)
1051 if (!strcmp("-q", arg)) {
1055 if (!strcmp("--no-reuse-delta", arg)) {
1059 if (!strcmp("--stdout", arg)) {
1070 if (pack_to_stdout != !base_name)
1073 prepare_packed_git();
1075 fprintf(stderr, "Generating pack...\n");
1076 gettimeofday(&prev_tv, NULL);
1078 while (fgets(line, sizeof(line), stdin) != NULL) {
1079 unsigned char sha1[20];
1081 if (progress && (eye_candy <= nr_objects)) {
1082 fprintf(stderr, "Counting objects...%d\r", nr_objects);
1083 if (eye_candy && (50 <= eye_candy_incr)) {
1086 gettimeofday(&tv, NULL);
1087 time_diff = (tv.tv_sec - prev_tv.tv_sec);
1089 time_diff += (tv.tv_usec - prev_tv.tv_usec);
1090 if ((1 << 9) < time_diff)
1091 eye_candy_incr += 50;
1092 else if (50 < eye_candy_incr)
1093 eye_candy_incr -= 50;
1095 eye_candy += eye_candy_incr;
1097 if (line[0] == '-') {
1098 if (get_sha1_hex(line+1, sha1))
1099 die("expected edge sha1, got garbage:\n %s",
1101 add_preferred_base(sha1);
1104 if (get_sha1_hex(line, sha1))
1105 die("expected sha1, got garbage:\n %s", line);
1106 add_object_entry(sha1, name_hash(NULL, line+41), 0);
1109 fprintf(stderr, "Done counting %d objects.\n", nr_objects);
1110 if (non_empty && !nr_objects)
1113 sorted_by_sha = create_final_object_list();
1115 list = sorted_by_sha;
1116 for (i = 0; i < nr_result; i++) {
1117 struct object_entry *entry = *list++;
1118 SHA1_Update(&ctx, entry->sha1, 20);
1120 SHA1_Final(object_list_sha1, &ctx);
1121 if (progress && (nr_objects != nr_result))
1122 fprintf(stderr, "Result has %d objects.\n", nr_result);
1124 if (reuse_cached_pack(object_list_sha1, pack_to_stdout))
1127 prepare_pack(window, depth);
1128 if (!pack_to_stdout) {
1130 puts(sha1_to_hex(object_list_sha1));
1134 fprintf(stderr, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
1135 nr_result, written, written_delta, reused, reused_delta);