Code

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