Merge branch 'jc/bs-t-is-not-a-tab-for-sed'
[git] / builtin / pack-objects.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "attr.h"
4 #include "object.h"
5 #include "blob.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "tree.h"
9 #include "delta.h"
10 #include "pack.h"
11 #include "pack-revindex.h"
12 #include "csum-file.h"
13 #include "tree-walk.h"
14 #include "diff.h"
15 #include "revision.h"
16 #include "list-objects.h"
17 #include "pack-objects.h"
18 #include "progress.h"
19 #include "refs.h"
20 #include "streaming.h"
21 #include "thread-utils.h"
22 #include "pack-bitmap.h"
23 #include "reachable.h"
24 #include "sha1-array.h"
25 #include "argv-array.h"
26 #include "mru.h"
27
28 static const char *pack_usage[] = {
29         N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
30         N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"),
31         NULL
32 };
33
34 /*
35  * Objects we are going to pack are collected in the `to_pack` structure.
36  * It contains an array (dynamically expanded) of the object data, and a map
37  * that can resolve SHA1s to their position in the array.
38  */
39 static struct packing_data to_pack;
40
41 static struct pack_idx_entry **written_list;
42 static uint32_t nr_result, nr_written;
43
44 static int non_empty;
45 static int reuse_delta = 1, reuse_object = 1;
46 static int keep_unreachable, unpack_unreachable, include_tag;
47 static unsigned long unpack_unreachable_expiration;
48 static int pack_loose_unreachable;
49 static int local;
50 static int have_non_local_packs;
51 static int incremental;
52 static int ignore_packed_keep;
53 static int allow_ofs_delta;
54 static struct pack_idx_option pack_idx_opts;
55 static const char *base_name;
56 static int progress = 1;
57 static int window = 10;
58 static unsigned long pack_size_limit;
59 static int depth = 50;
60 static int delta_search_threads;
61 static int pack_to_stdout;
62 static int num_preferred_base;
63 static struct progress *progress_state;
64
65 static struct packed_git *reuse_packfile;
66 static uint32_t reuse_packfile_objects;
67 static off_t reuse_packfile_offset;
68
69 static int use_bitmap_index_default = 1;
70 static int use_bitmap_index = -1;
71 static int write_bitmap_index;
72 static uint16_t write_bitmap_options;
73
74 static unsigned long delta_cache_size = 0;
75 static unsigned long max_delta_cache_size = 256 * 1024 * 1024;
76 static unsigned long cache_max_small_delta_size = 1000;
77
78 static unsigned long window_memory_limit = 0;
79
80 /*
81  * stats
82  */
83 static uint32_t written, written_delta;
84 static uint32_t reused, reused_delta;
85
86 /*
87  * Indexed commits
88  */
89 static struct commit **indexed_commits;
90 static unsigned int indexed_commits_nr;
91 static unsigned int indexed_commits_alloc;
92
93 static void index_commit_for_bitmap(struct commit *commit)
94 {
95         if (indexed_commits_nr >= indexed_commits_alloc) {
96                 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2;
97                 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc);
98         }
99
100         indexed_commits[indexed_commits_nr++] = commit;
101 }
102
103 static void *get_delta(struct object_entry *entry)
104 {
105         unsigned long size, base_size, delta_size;
106         void *buf, *base_buf, *delta_buf;
107         enum object_type type;
108
109         buf = read_sha1_file(entry->idx.sha1, &type, &size);
110         if (!buf)
111                 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
112         base_buf = read_sha1_file(entry->delta->idx.sha1, &type, &base_size);
113         if (!base_buf)
114                 die("unable to read %s", sha1_to_hex(entry->delta->idx.sha1));
115         delta_buf = diff_delta(base_buf, base_size,
116                                buf, size, &delta_size, 0);
117         if (!delta_buf || delta_size != entry->delta_size)
118                 die("delta size changed");
119         free(buf);
120         free(base_buf);
121         return delta_buf;
122 }
123
124 static unsigned long do_compress(void **pptr, unsigned long size)
125 {
126         git_zstream stream;
127         void *in, *out;
128         unsigned long maxsize;
129
130         git_deflate_init(&stream, pack_compression_level);
131         maxsize = git_deflate_bound(&stream, size);
132
133         in = *pptr;
134         out = xmalloc(maxsize);
135         *pptr = out;
136
137         stream.next_in = in;
138         stream.avail_in = size;
139         stream.next_out = out;
140         stream.avail_out = maxsize;
141         while (git_deflate(&stream, Z_FINISH) == Z_OK)
142                 ; /* nothing */
143         git_deflate_end(&stream);
144
145         free(in);
146         return stream.total_out;
147 }
148
149 static unsigned long write_large_blob_data(struct git_istream *st, struct sha1file *f,
150                                            const unsigned char *sha1)
151 {
152         git_zstream stream;
153         unsigned char ibuf[1024 * 16];
154         unsigned char obuf[1024 * 16];
155         unsigned long olen = 0;
156
157         git_deflate_init(&stream, pack_compression_level);
158
159         for (;;) {
160                 ssize_t readlen;
161                 int zret = Z_OK;
162                 readlen = read_istream(st, ibuf, sizeof(ibuf));
163                 if (readlen == -1)
164                         die(_("unable to read %s"), sha1_to_hex(sha1));
165
166                 stream.next_in = ibuf;
167                 stream.avail_in = readlen;
168                 while ((stream.avail_in || readlen == 0) &&
169                        (zret == Z_OK || zret == Z_BUF_ERROR)) {
170                         stream.next_out = obuf;
171                         stream.avail_out = sizeof(obuf);
172                         zret = git_deflate(&stream, readlen ? 0 : Z_FINISH);
173                         sha1write(f, obuf, stream.next_out - obuf);
174                         olen += stream.next_out - obuf;
175                 }
176                 if (stream.avail_in)
177                         die(_("deflate error (%d)"), zret);
178                 if (readlen == 0) {
179                         if (zret != Z_STREAM_END)
180                                 die(_("deflate error (%d)"), zret);
181                         break;
182                 }
183         }
184         git_deflate_end(&stream);
185         return olen;
186 }
187
188 /*
189  * we are going to reuse the existing object data as is.  make
190  * sure it is not corrupt.
191  */
192 static int check_pack_inflate(struct packed_git *p,
193                 struct pack_window **w_curs,
194                 off_t offset,
195                 off_t len,
196                 unsigned long expect)
197 {
198         git_zstream stream;
199         unsigned char fakebuf[4096], *in;
200         int st;
201
202         memset(&stream, 0, sizeof(stream));
203         git_inflate_init(&stream);
204         do {
205                 in = use_pack(p, w_curs, offset, &stream.avail_in);
206                 stream.next_in = in;
207                 stream.next_out = fakebuf;
208                 stream.avail_out = sizeof(fakebuf);
209                 st = git_inflate(&stream, Z_FINISH);
210                 offset += stream.next_in - in;
211         } while (st == Z_OK || st == Z_BUF_ERROR);
212         git_inflate_end(&stream);
213         return (st == Z_STREAM_END &&
214                 stream.total_out == expect &&
215                 stream.total_in == len) ? 0 : -1;
216 }
217
218 static void copy_pack_data(struct sha1file *f,
219                 struct packed_git *p,
220                 struct pack_window **w_curs,
221                 off_t offset,
222                 off_t len)
223 {
224         unsigned char *in;
225         unsigned long avail;
226
227         while (len) {
228                 in = use_pack(p, w_curs, offset, &avail);
229                 if (avail > len)
230                         avail = (unsigned long)len;
231                 sha1write(f, in, avail);
232                 offset += avail;
233                 len -= avail;
234         }
235 }
236
237 /* Return 0 if we will bust the pack-size limit */
238 static unsigned long write_no_reuse_object(struct sha1file *f, struct object_entry *entry,
239                                            unsigned long limit, int usable_delta)
240 {
241         unsigned long size, datalen;
242         unsigned char header[MAX_PACK_OBJECT_HEADER],
243                       dheader[MAX_PACK_OBJECT_HEADER];
244         unsigned hdrlen;
245         enum object_type type;
246         void *buf;
247         struct git_istream *st = NULL;
248
249         if (!usable_delta) {
250                 if (entry->type == OBJ_BLOB &&
251                     entry->size > big_file_threshold &&
252                     (st = open_istream(entry->idx.sha1, &type, &size, NULL)) != NULL)
253                         buf = NULL;
254                 else {
255                         buf = read_sha1_file(entry->idx.sha1, &type, &size);
256                         if (!buf)
257                                 die(_("unable to read %s"), sha1_to_hex(entry->idx.sha1));
258                 }
259                 /*
260                  * make sure no cached delta data remains from a
261                  * previous attempt before a pack split occurred.
262                  */
263                 free(entry->delta_data);
264                 entry->delta_data = NULL;
265                 entry->z_delta_size = 0;
266         } else if (entry->delta_data) {
267                 size = entry->delta_size;
268                 buf = entry->delta_data;
269                 entry->delta_data = NULL;
270                 type = (allow_ofs_delta && entry->delta->idx.offset) ?
271                         OBJ_OFS_DELTA : OBJ_REF_DELTA;
272         } else {
273                 buf = get_delta(entry);
274                 size = entry->delta_size;
275                 type = (allow_ofs_delta && entry->delta->idx.offset) ?
276                         OBJ_OFS_DELTA : OBJ_REF_DELTA;
277         }
278
279         if (st) /* large blob case, just assume we don't compress well */
280                 datalen = size;
281         else if (entry->z_delta_size)
282                 datalen = entry->z_delta_size;
283         else
284                 datalen = do_compress(&buf, size);
285
286         /*
287          * The object header is a byte of 'type' followed by zero or
288          * more bytes of length.
289          */
290         hdrlen = encode_in_pack_object_header(header, sizeof(header),
291                                               type, size);
292
293         if (type == OBJ_OFS_DELTA) {
294                 /*
295                  * Deltas with relative base contain an additional
296                  * encoding of the relative offset for the delta
297                  * base from this object's position in the pack.
298                  */
299                 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
300                 unsigned pos = sizeof(dheader) - 1;
301                 dheader[pos] = ofs & 127;
302                 while (ofs >>= 7)
303                         dheader[--pos] = 128 | (--ofs & 127);
304                 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
305                         if (st)
306                                 close_istream(st);
307                         free(buf);
308                         return 0;
309                 }
310                 sha1write(f, header, hdrlen);
311                 sha1write(f, dheader + pos, sizeof(dheader) - pos);
312                 hdrlen += sizeof(dheader) - pos;
313         } else if (type == OBJ_REF_DELTA) {
314                 /*
315                  * Deltas with a base reference contain
316                  * an additional 20 bytes for the base sha1.
317                  */
318                 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
319                         if (st)
320                                 close_istream(st);
321                         free(buf);
322                         return 0;
323                 }
324                 sha1write(f, header, hdrlen);
325                 sha1write(f, entry->delta->idx.sha1, 20);
326                 hdrlen += 20;
327         } else {
328                 if (limit && hdrlen + datalen + 20 >= limit) {
329                         if (st)
330                                 close_istream(st);
331                         free(buf);
332                         return 0;
333                 }
334                 sha1write(f, header, hdrlen);
335         }
336         if (st) {
337                 datalen = write_large_blob_data(st, f, entry->idx.sha1);
338                 close_istream(st);
339         } else {
340                 sha1write(f, buf, datalen);
341                 free(buf);
342         }
343
344         return hdrlen + datalen;
345 }
346
347 /* Return 0 if we will bust the pack-size limit */
348 static off_t write_reuse_object(struct sha1file *f, struct object_entry *entry,
349                                 unsigned long limit, int usable_delta)
350 {
351         struct packed_git *p = entry->in_pack;
352         struct pack_window *w_curs = NULL;
353         struct revindex_entry *revidx;
354         off_t offset;
355         enum object_type type = entry->type;
356         off_t datalen;
357         unsigned char header[MAX_PACK_OBJECT_HEADER],
358                       dheader[MAX_PACK_OBJECT_HEADER];
359         unsigned hdrlen;
360
361         if (entry->delta)
362                 type = (allow_ofs_delta && entry->delta->idx.offset) ?
363                         OBJ_OFS_DELTA : OBJ_REF_DELTA;
364         hdrlen = encode_in_pack_object_header(header, sizeof(header),
365                                               type, entry->size);
366
367         offset = entry->in_pack_offset;
368         revidx = find_pack_revindex(p, offset);
369         datalen = revidx[1].offset - offset;
370         if (!pack_to_stdout && p->index_version > 1 &&
371             check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) {
372                 error("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
373                 unuse_pack(&w_curs);
374                 return write_no_reuse_object(f, entry, limit, usable_delta);
375         }
376
377         offset += entry->in_pack_header_size;
378         datalen -= entry->in_pack_header_size;
379
380         if (!pack_to_stdout && p->index_version == 1 &&
381             check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) {
382                 error("corrupt packed object for %s", sha1_to_hex(entry->idx.sha1));
383                 unuse_pack(&w_curs);
384                 return write_no_reuse_object(f, entry, limit, usable_delta);
385         }
386
387         if (type == OBJ_OFS_DELTA) {
388                 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
389                 unsigned pos = sizeof(dheader) - 1;
390                 dheader[pos] = ofs & 127;
391                 while (ofs >>= 7)
392                         dheader[--pos] = 128 | (--ofs & 127);
393                 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
394                         unuse_pack(&w_curs);
395                         return 0;
396                 }
397                 sha1write(f, header, hdrlen);
398                 sha1write(f, dheader + pos, sizeof(dheader) - pos);
399                 hdrlen += sizeof(dheader) - pos;
400                 reused_delta++;
401         } else if (type == OBJ_REF_DELTA) {
402                 if (limit && hdrlen + 20 + datalen + 20 >= limit) {
403                         unuse_pack(&w_curs);
404                         return 0;
405                 }
406                 sha1write(f, header, hdrlen);
407                 sha1write(f, entry->delta->idx.sha1, 20);
408                 hdrlen += 20;
409                 reused_delta++;
410         } else {
411                 if (limit && hdrlen + datalen + 20 >= limit) {
412                         unuse_pack(&w_curs);
413                         return 0;
414                 }
415                 sha1write(f, header, hdrlen);
416         }
417         copy_pack_data(f, p, &w_curs, offset, datalen);
418         unuse_pack(&w_curs);
419         reused++;
420         return hdrlen + datalen;
421 }
422
423 /* Return 0 if we will bust the pack-size limit */
424 static off_t write_object(struct sha1file *f,
425                           struct object_entry *entry,
426                           off_t write_offset)
427 {
428         unsigned long limit;
429         off_t len;
430         int usable_delta, to_reuse;
431
432         if (!pack_to_stdout)
433                 crc32_begin(f);
434
435         /* apply size limit if limited packsize and not first object */
436         if (!pack_size_limit || !nr_written)
437                 limit = 0;
438         else if (pack_size_limit <= write_offset)
439                 /*
440                  * the earlier object did not fit the limit; avoid
441                  * mistaking this with unlimited (i.e. limit = 0).
442                  */
443                 limit = 1;
444         else
445                 limit = pack_size_limit - write_offset;
446
447         if (!entry->delta)
448                 usable_delta = 0;       /* no delta */
449         else if (!pack_size_limit)
450                usable_delta = 1;        /* unlimited packfile */
451         else if (entry->delta->idx.offset == (off_t)-1)
452                 usable_delta = 0;       /* base was written to another pack */
453         else if (entry->delta->idx.offset)
454                 usable_delta = 1;       /* base already exists in this pack */
455         else
456                 usable_delta = 0;       /* base could end up in another pack */
457
458         if (!reuse_object)
459                 to_reuse = 0;   /* explicit */
460         else if (!entry->in_pack)
461                 to_reuse = 0;   /* can't reuse what we don't have */
462         else if (entry->type == OBJ_REF_DELTA || entry->type == OBJ_OFS_DELTA)
463                                 /* check_object() decided it for us ... */
464                 to_reuse = usable_delta;
465                                 /* ... but pack split may override that */
466         else if (entry->type != entry->in_pack_type)
467                 to_reuse = 0;   /* pack has delta which is unusable */
468         else if (entry->delta)
469                 to_reuse = 0;   /* we want to pack afresh */
470         else
471                 to_reuse = 1;   /* we have it in-pack undeltified,
472                                  * and we do not need to deltify it.
473                                  */
474
475         if (!to_reuse)
476                 len = write_no_reuse_object(f, entry, limit, usable_delta);
477         else
478                 len = write_reuse_object(f, entry, limit, usable_delta);
479         if (!len)
480                 return 0;
481
482         if (usable_delta)
483                 written_delta++;
484         written++;
485         if (!pack_to_stdout)
486                 entry->idx.crc32 = crc32_end(f);
487         return len;
488 }
489
490 enum write_one_status {
491         WRITE_ONE_SKIP = -1, /* already written */
492         WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */
493         WRITE_ONE_WRITTEN = 1, /* normal */
494         WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */
495 };
496
497 static enum write_one_status write_one(struct sha1file *f,
498                                        struct object_entry *e,
499                                        off_t *offset)
500 {
501         off_t size;
502         int recursing;
503
504         /*
505          * we set offset to 1 (which is an impossible value) to mark
506          * the fact that this object is involved in "write its base
507          * first before writing a deltified object" recursion.
508          */
509         recursing = (e->idx.offset == 1);
510         if (recursing) {
511                 warning("recursive delta detected for object %s",
512                         sha1_to_hex(e->idx.sha1));
513                 return WRITE_ONE_RECURSIVE;
514         } else if (e->idx.offset || e->preferred_base) {
515                 /* offset is non zero if object is written already. */
516                 return WRITE_ONE_SKIP;
517         }
518
519         /* if we are deltified, write out base object first. */
520         if (e->delta) {
521                 e->idx.offset = 1; /* now recurse */
522                 switch (write_one(f, e->delta, offset)) {
523                 case WRITE_ONE_RECURSIVE:
524                         /* we cannot depend on this one */
525                         e->delta = NULL;
526                         break;
527                 default:
528                         break;
529                 case WRITE_ONE_BREAK:
530                         e->idx.offset = recursing;
531                         return WRITE_ONE_BREAK;
532                 }
533         }
534
535         e->idx.offset = *offset;
536         size = write_object(f, e, *offset);
537         if (!size) {
538                 e->idx.offset = recursing;
539                 return WRITE_ONE_BREAK;
540         }
541         written_list[nr_written++] = &e->idx;
542
543         /* make sure off_t is sufficiently large not to wrap */
544         if (signed_add_overflows(*offset, size))
545                 die("pack too large for current definition of off_t");
546         *offset += size;
547         return WRITE_ONE_WRITTEN;
548 }
549
550 static int mark_tagged(const char *path, const struct object_id *oid, int flag,
551                        void *cb_data)
552 {
553         unsigned char peeled[20];
554         struct object_entry *entry = packlist_find(&to_pack, oid->hash, NULL);
555
556         if (entry)
557                 entry->tagged = 1;
558         if (!peel_ref(path, peeled)) {
559                 entry = packlist_find(&to_pack, peeled, NULL);
560                 if (entry)
561                         entry->tagged = 1;
562         }
563         return 0;
564 }
565
566 static inline void add_to_write_order(struct object_entry **wo,
567                                unsigned int *endp,
568                                struct object_entry *e)
569 {
570         if (e->filled)
571                 return;
572         wo[(*endp)++] = e;
573         e->filled = 1;
574 }
575
576 static void add_descendants_to_write_order(struct object_entry **wo,
577                                            unsigned int *endp,
578                                            struct object_entry *e)
579 {
580         int add_to_order = 1;
581         while (e) {
582                 if (add_to_order) {
583                         struct object_entry *s;
584                         /* add this node... */
585                         add_to_write_order(wo, endp, e);
586                         /* all its siblings... */
587                         for (s = e->delta_sibling; s; s = s->delta_sibling) {
588                                 add_to_write_order(wo, endp, s);
589                         }
590                 }
591                 /* drop down a level to add left subtree nodes if possible */
592                 if (e->delta_child) {
593                         add_to_order = 1;
594                         e = e->delta_child;
595                 } else {
596                         add_to_order = 0;
597                         /* our sibling might have some children, it is next */
598                         if (e->delta_sibling) {
599                                 e = e->delta_sibling;
600                                 continue;
601                         }
602                         /* go back to our parent node */
603                         e = e->delta;
604                         while (e && !e->delta_sibling) {
605                                 /* we're on the right side of a subtree, keep
606                                  * going up until we can go right again */
607                                 e = e->delta;
608                         }
609                         if (!e) {
610                                 /* done- we hit our original root node */
611                                 return;
612                         }
613                         /* pass it off to sibling at this level */
614                         e = e->delta_sibling;
615                 }
616         };
617 }
618
619 static void add_family_to_write_order(struct object_entry **wo,
620                                       unsigned int *endp,
621                                       struct object_entry *e)
622 {
623         struct object_entry *root;
624
625         for (root = e; root->delta; root = root->delta)
626                 ; /* nothing */
627         add_descendants_to_write_order(wo, endp, root);
628 }
629
630 static struct object_entry **compute_write_order(void)
631 {
632         unsigned int i, wo_end, last_untagged;
633
634         struct object_entry **wo;
635         struct object_entry *objects = to_pack.objects;
636
637         for (i = 0; i < to_pack.nr_objects; i++) {
638                 objects[i].tagged = 0;
639                 objects[i].filled = 0;
640                 objects[i].delta_child = NULL;
641                 objects[i].delta_sibling = NULL;
642         }
643
644         /*
645          * Fully connect delta_child/delta_sibling network.
646          * Make sure delta_sibling is sorted in the original
647          * recency order.
648          */
649         for (i = to_pack.nr_objects; i > 0;) {
650                 struct object_entry *e = &objects[--i];
651                 if (!e->delta)
652                         continue;
653                 /* Mark me as the first child */
654                 e->delta_sibling = e->delta->delta_child;
655                 e->delta->delta_child = e;
656         }
657
658         /*
659          * Mark objects that are at the tip of tags.
660          */
661         for_each_tag_ref(mark_tagged, NULL);
662
663         /*
664          * Give the objects in the original recency order until
665          * we see a tagged tip.
666          */
667         ALLOC_ARRAY(wo, to_pack.nr_objects);
668         for (i = wo_end = 0; i < to_pack.nr_objects; i++) {
669                 if (objects[i].tagged)
670                         break;
671                 add_to_write_order(wo, &wo_end, &objects[i]);
672         }
673         last_untagged = i;
674
675         /*
676          * Then fill all the tagged tips.
677          */
678         for (; i < to_pack.nr_objects; i++) {
679                 if (objects[i].tagged)
680                         add_to_write_order(wo, &wo_end, &objects[i]);
681         }
682
683         /*
684          * And then all remaining commits and tags.
685          */
686         for (i = last_untagged; i < to_pack.nr_objects; i++) {
687                 if (objects[i].type != OBJ_COMMIT &&
688                     objects[i].type != OBJ_TAG)
689                         continue;
690                 add_to_write_order(wo, &wo_end, &objects[i]);
691         }
692
693         /*
694          * And then all the trees.
695          */
696         for (i = last_untagged; i < to_pack.nr_objects; i++) {
697                 if (objects[i].type != OBJ_TREE)
698                         continue;
699                 add_to_write_order(wo, &wo_end, &objects[i]);
700         }
701
702         /*
703          * Finally all the rest in really tight order
704          */
705         for (i = last_untagged; i < to_pack.nr_objects; i++) {
706                 if (!objects[i].filled)
707                         add_family_to_write_order(wo, &wo_end, &objects[i]);
708         }
709
710         if (wo_end != to_pack.nr_objects)
711                 die("ordered %u objects, expected %"PRIu32, wo_end, to_pack.nr_objects);
712
713         return wo;
714 }
715
716 static off_t write_reused_pack(struct sha1file *f)
717 {
718         unsigned char buffer[8192];
719         off_t to_write, total;
720         int fd;
721
722         if (!is_pack_valid(reuse_packfile))
723                 die("packfile is invalid: %s", reuse_packfile->pack_name);
724
725         fd = git_open(reuse_packfile->pack_name);
726         if (fd < 0)
727                 die_errno("unable to open packfile for reuse: %s",
728                           reuse_packfile->pack_name);
729
730         if (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1)
731                 die_errno("unable to seek in reused packfile");
732
733         if (reuse_packfile_offset < 0)
734                 reuse_packfile_offset = reuse_packfile->pack_size - 20;
735
736         total = to_write = reuse_packfile_offset - sizeof(struct pack_header);
737
738         while (to_write) {
739                 int read_pack = xread(fd, buffer, sizeof(buffer));
740
741                 if (read_pack <= 0)
742                         die_errno("unable to read from reused packfile");
743
744                 if (read_pack > to_write)
745                         read_pack = to_write;
746
747                 sha1write(f, buffer, read_pack);
748                 to_write -= read_pack;
749
750                 /*
751                  * We don't know the actual number of objects written,
752                  * only how many bytes written, how many bytes total, and
753                  * how many objects total. So we can fake it by pretending all
754                  * objects we are writing are the same size. This gives us a
755                  * smooth progress meter, and at the end it matches the true
756                  * answer.
757                  */
758                 written = reuse_packfile_objects *
759                                 (((double)(total - to_write)) / total);
760                 display_progress(progress_state, written);
761         }
762
763         close(fd);
764         written = reuse_packfile_objects;
765         display_progress(progress_state, written);
766         return reuse_packfile_offset - sizeof(struct pack_header);
767 }
768
769 static const char no_split_warning[] = N_(
770 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
771 );
772
773 static void write_pack_file(void)
774 {
775         uint32_t i = 0, j;
776         struct sha1file *f;
777         off_t offset;
778         uint32_t nr_remaining = nr_result;
779         time_t last_mtime = 0;
780         struct object_entry **write_order;
781
782         if (progress > pack_to_stdout)
783                 progress_state = start_progress(_("Writing objects"), nr_result);
784         ALLOC_ARRAY(written_list, to_pack.nr_objects);
785         write_order = compute_write_order();
786
787         do {
788                 unsigned char sha1[20];
789                 char *pack_tmp_name = NULL;
790
791                 if (pack_to_stdout)
792                         f = sha1fd_throughput(1, "<stdout>", progress_state);
793                 else
794                         f = create_tmp_packfile(&pack_tmp_name);
795
796                 offset = write_pack_header(f, nr_remaining);
797
798                 if (reuse_packfile) {
799                         off_t packfile_size;
800                         assert(pack_to_stdout);
801
802                         packfile_size = write_reused_pack(f);
803                         offset += packfile_size;
804                 }
805
806                 nr_written = 0;
807                 for (; i < to_pack.nr_objects; i++) {
808                         struct object_entry *e = write_order[i];
809                         if (write_one(f, e, &offset) == WRITE_ONE_BREAK)
810                                 break;
811                         display_progress(progress_state, written);
812                 }
813
814                 /*
815                  * Did we write the wrong # entries in the header?
816                  * If so, rewrite it like in fast-import
817                  */
818                 if (pack_to_stdout) {
819                         sha1close(f, sha1, CSUM_CLOSE);
820                 } else if (nr_written == nr_remaining) {
821                         sha1close(f, sha1, CSUM_FSYNC);
822                 } else {
823                         int fd = sha1close(f, sha1, 0);
824                         fixup_pack_header_footer(fd, sha1, pack_tmp_name,
825                                                  nr_written, sha1, offset);
826                         close(fd);
827                         if (write_bitmap_index) {
828                                 warning(_(no_split_warning));
829                                 write_bitmap_index = 0;
830                         }
831                 }
832
833                 if (!pack_to_stdout) {
834                         struct stat st;
835                         struct strbuf tmpname = STRBUF_INIT;
836
837                         /*
838                          * Packs are runtime accessed in their mtime
839                          * order since newer packs are more likely to contain
840                          * younger objects.  So if we are creating multiple
841                          * packs then we should modify the mtime of later ones
842                          * to preserve this property.
843                          */
844                         if (stat(pack_tmp_name, &st) < 0) {
845                                 warning_errno("failed to stat %s", pack_tmp_name);
846                         } else if (!last_mtime) {
847                                 last_mtime = st.st_mtime;
848                         } else {
849                                 struct utimbuf utb;
850                                 utb.actime = st.st_atime;
851                                 utb.modtime = --last_mtime;
852                                 if (utime(pack_tmp_name, &utb) < 0)
853                                         warning_errno("failed utime() on %s", pack_tmp_name);
854                         }
855
856                         strbuf_addf(&tmpname, "%s-", base_name);
857
858                         if (write_bitmap_index) {
859                                 bitmap_writer_set_checksum(sha1);
860                                 bitmap_writer_build_type_index(written_list, nr_written);
861                         }
862
863                         finish_tmp_packfile(&tmpname, pack_tmp_name,
864                                             written_list, nr_written,
865                                             &pack_idx_opts, sha1);
866
867                         if (write_bitmap_index) {
868                                 strbuf_addf(&tmpname, "%s.bitmap", sha1_to_hex(sha1));
869
870                                 stop_progress(&progress_state);
871
872                                 bitmap_writer_show_progress(progress);
873                                 bitmap_writer_reuse_bitmaps(&to_pack);
874                                 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
875                                 bitmap_writer_build(&to_pack);
876                                 bitmap_writer_finish(written_list, nr_written,
877                                                      tmpname.buf, write_bitmap_options);
878                                 write_bitmap_index = 0;
879                         }
880
881                         strbuf_release(&tmpname);
882                         free(pack_tmp_name);
883                         puts(sha1_to_hex(sha1));
884                 }
885
886                 /* mark written objects as written to previous pack */
887                 for (j = 0; j < nr_written; j++) {
888                         written_list[j]->offset = (off_t)-1;
889                 }
890                 nr_remaining -= nr_written;
891         } while (nr_remaining && i < to_pack.nr_objects);
892
893         free(written_list);
894         free(write_order);
895         stop_progress(&progress_state);
896         if (written != nr_result)
897                 die("wrote %"PRIu32" objects while expecting %"PRIu32,
898                         written, nr_result);
899 }
900
901 static int no_try_delta(const char *path)
902 {
903         static struct attr_check *check;
904
905         if (!check)
906                 check = attr_check_initl("delta", NULL);
907         if (git_check_attr(path, check))
908                 return 0;
909         if (ATTR_FALSE(check->items[0].value))
910                 return 1;
911         return 0;
912 }
913
914 /*
915  * When adding an object, check whether we have already added it
916  * to our packing list. If so, we can skip. However, if we are
917  * being asked to excludei t, but the previous mention was to include
918  * it, make sure to adjust its flags and tweak our numbers accordingly.
919  *
920  * As an optimization, we pass out the index position where we would have
921  * found the item, since that saves us from having to look it up again a
922  * few lines later when we want to add the new entry.
923  */
924 static int have_duplicate_entry(const unsigned char *sha1,
925                                 int exclude,
926                                 uint32_t *index_pos)
927 {
928         struct object_entry *entry;
929
930         entry = packlist_find(&to_pack, sha1, index_pos);
931         if (!entry)
932                 return 0;
933
934         if (exclude) {
935                 if (!entry->preferred_base)
936                         nr_result--;
937                 entry->preferred_base = 1;
938         }
939
940         return 1;
941 }
942
943 static int want_found_object(int exclude, struct packed_git *p)
944 {
945         if (exclude)
946                 return 1;
947         if (incremental)
948                 return 0;
949
950         /*
951          * When asked to do --local (do not include an object that appears in a
952          * pack we borrow from elsewhere) or --honor-pack-keep (do not include
953          * an object that appears in a pack marked with .keep), finding a pack
954          * that matches the criteria is sufficient for us to decide to omit it.
955          * However, even if this pack does not satisfy the criteria, we need to
956          * make sure no copy of this object appears in _any_ pack that makes us
957          * to omit the object, so we need to check all the packs.
958          *
959          * We can however first check whether these options can possible matter;
960          * if they do not matter we know we want the object in generated pack.
961          * Otherwise, we signal "-1" at the end to tell the caller that we do
962          * not know either way, and it needs to check more packs.
963          */
964         if (!ignore_packed_keep &&
965             (!local || !have_non_local_packs))
966                 return 1;
967
968         if (local && !p->pack_local)
969                 return 0;
970         if (ignore_packed_keep && p->pack_local && p->pack_keep)
971                 return 0;
972
973         /* we don't know yet; keep looking for more packs */
974         return -1;
975 }
976
977 /*
978  * Check whether we want the object in the pack (e.g., we do not want
979  * objects found in non-local stores if the "--local" option was used).
980  *
981  * If the caller already knows an existing pack it wants to take the object
982  * from, that is passed in *found_pack and *found_offset; otherwise this
983  * function finds if there is any pack that has the object and returns the pack
984  * and its offset in these variables.
985  */
986 static int want_object_in_pack(const unsigned char *sha1,
987                                int exclude,
988                                struct packed_git **found_pack,
989                                off_t *found_offset)
990 {
991         struct mru_entry *entry;
992         int want;
993
994         if (!exclude && local && has_loose_object_nonlocal(sha1))
995                 return 0;
996
997         /*
998          * If we already know the pack object lives in, start checks from that
999          * pack - in the usual case when neither --local was given nor .keep files
1000          * are present we will determine the answer right now.
1001          */
1002         if (*found_pack) {
1003                 want = want_found_object(exclude, *found_pack);
1004                 if (want != -1)
1005                         return want;
1006         }
1007
1008         for (entry = packed_git_mru->head; entry; entry = entry->next) {
1009                 struct packed_git *p = entry->item;
1010                 off_t offset;
1011
1012                 if (p == *found_pack)
1013                         offset = *found_offset;
1014                 else
1015                         offset = find_pack_entry_one(sha1, p);
1016
1017                 if (offset) {
1018                         if (!*found_pack) {
1019                                 if (!is_pack_valid(p))
1020                                         continue;
1021                                 *found_offset = offset;
1022                                 *found_pack = p;
1023                         }
1024                         want = want_found_object(exclude, p);
1025                         if (!exclude && want > 0)
1026                                 mru_mark(packed_git_mru, entry);
1027                         if (want != -1)
1028                                 return want;
1029                 }
1030         }
1031
1032         return 1;
1033 }
1034
1035 static void create_object_entry(const unsigned char *sha1,
1036                                 enum object_type type,
1037                                 uint32_t hash,
1038                                 int exclude,
1039                                 int no_try_delta,
1040                                 uint32_t index_pos,
1041                                 struct packed_git *found_pack,
1042                                 off_t found_offset)
1043 {
1044         struct object_entry *entry;
1045
1046         entry = packlist_alloc(&to_pack, sha1, index_pos);
1047         entry->hash = hash;
1048         if (type)
1049                 entry->type = type;
1050         if (exclude)
1051                 entry->preferred_base = 1;
1052         else
1053                 nr_result++;
1054         if (found_pack) {
1055                 entry->in_pack = found_pack;
1056                 entry->in_pack_offset = found_offset;
1057         }
1058
1059         entry->no_try_delta = no_try_delta;
1060 }
1061
1062 static const char no_closure_warning[] = N_(
1063 "disabling bitmap writing, as some objects are not being packed"
1064 );
1065
1066 static int add_object_entry(const unsigned char *sha1, enum object_type type,
1067                             const char *name, int exclude)
1068 {
1069         struct packed_git *found_pack = NULL;
1070         off_t found_offset = 0;
1071         uint32_t index_pos;
1072
1073         if (have_duplicate_entry(sha1, exclude, &index_pos))
1074                 return 0;
1075
1076         if (!want_object_in_pack(sha1, exclude, &found_pack, &found_offset)) {
1077                 /* The pack is missing an object, so it will not have closure */
1078                 if (write_bitmap_index) {
1079                         warning(_(no_closure_warning));
1080                         write_bitmap_index = 0;
1081                 }
1082                 return 0;
1083         }
1084
1085         create_object_entry(sha1, type, pack_name_hash(name),
1086                             exclude, name && no_try_delta(name),
1087                             index_pos, found_pack, found_offset);
1088
1089         display_progress(progress_state, nr_result);
1090         return 1;
1091 }
1092
1093 static int add_object_entry_from_bitmap(const unsigned char *sha1,
1094                                         enum object_type type,
1095                                         int flags, uint32_t name_hash,
1096                                         struct packed_git *pack, off_t offset)
1097 {
1098         uint32_t index_pos;
1099
1100         if (have_duplicate_entry(sha1, 0, &index_pos))
1101                 return 0;
1102
1103         if (!want_object_in_pack(sha1, 0, &pack, &offset))
1104                 return 0;
1105
1106         create_object_entry(sha1, type, name_hash, 0, 0, index_pos, pack, offset);
1107
1108         display_progress(progress_state, nr_result);
1109         return 1;
1110 }
1111
1112 struct pbase_tree_cache {
1113         unsigned char sha1[20];
1114         int ref;
1115         int temporary;
1116         void *tree_data;
1117         unsigned long tree_size;
1118 };
1119
1120 static struct pbase_tree_cache *(pbase_tree_cache[256]);
1121 static int pbase_tree_cache_ix(const unsigned char *sha1)
1122 {
1123         return sha1[0] % ARRAY_SIZE(pbase_tree_cache);
1124 }
1125 static int pbase_tree_cache_ix_incr(int ix)
1126 {
1127         return (ix+1) % ARRAY_SIZE(pbase_tree_cache);
1128 }
1129
1130 static struct pbase_tree {
1131         struct pbase_tree *next;
1132         /* This is a phony "cache" entry; we are not
1133          * going to evict it or find it through _get()
1134          * mechanism -- this is for the toplevel node that
1135          * would almost always change with any commit.
1136          */
1137         struct pbase_tree_cache pcache;
1138 } *pbase_tree;
1139
1140 static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
1141 {
1142         struct pbase_tree_cache *ent, *nent;
1143         void *data;
1144         unsigned long size;
1145         enum object_type type;
1146         int neigh;
1147         int my_ix = pbase_tree_cache_ix(sha1);
1148         int available_ix = -1;
1149
1150         /* pbase-tree-cache acts as a limited hashtable.
1151          * your object will be found at your index or within a few
1152          * slots after that slot if it is cached.
1153          */
1154         for (neigh = 0; neigh < 8; neigh++) {
1155                 ent = pbase_tree_cache[my_ix];
1156                 if (ent && !hashcmp(ent->sha1, sha1)) {
1157                         ent->ref++;
1158                         return ent;
1159                 }
1160                 else if (((available_ix < 0) && (!ent || !ent->ref)) ||
1161                          ((0 <= available_ix) &&
1162                           (!ent && pbase_tree_cache[available_ix])))
1163                         available_ix = my_ix;
1164                 if (!ent)
1165                         break;
1166                 my_ix = pbase_tree_cache_ix_incr(my_ix);
1167         }
1168
1169         /* Did not find one.  Either we got a bogus request or
1170          * we need to read and perhaps cache.
1171          */
1172         data = read_sha1_file(sha1, &type, &size);
1173         if (!data)
1174                 return NULL;
1175         if (type != OBJ_TREE) {
1176                 free(data);
1177                 return NULL;
1178         }
1179
1180         /* We need to either cache or return a throwaway copy */
1181
1182         if (available_ix < 0)
1183                 ent = NULL;
1184         else {
1185                 ent = pbase_tree_cache[available_ix];
1186                 my_ix = available_ix;
1187         }
1188
1189         if (!ent) {
1190                 nent = xmalloc(sizeof(*nent));
1191                 nent->temporary = (available_ix < 0);
1192         }
1193         else {
1194                 /* evict and reuse */
1195                 free(ent->tree_data);
1196                 nent = ent;
1197         }
1198         hashcpy(nent->sha1, sha1);
1199         nent->tree_data = data;
1200         nent->tree_size = size;
1201         nent->ref = 1;
1202         if (!nent->temporary)
1203                 pbase_tree_cache[my_ix] = nent;
1204         return nent;
1205 }
1206
1207 static void pbase_tree_put(struct pbase_tree_cache *cache)
1208 {
1209         if (!cache->temporary) {
1210                 cache->ref--;
1211                 return;
1212         }
1213         free(cache->tree_data);
1214         free(cache);
1215 }
1216
1217 static int name_cmp_len(const char *name)
1218 {
1219         int i;
1220         for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)
1221                 ;
1222         return i;
1223 }
1224
1225 static void add_pbase_object(struct tree_desc *tree,
1226                              const char *name,
1227                              int cmplen,
1228                              const char *fullname)
1229 {
1230         struct name_entry entry;
1231         int cmp;
1232
1233         while (tree_entry(tree,&entry)) {
1234                 if (S_ISGITLINK(entry.mode))
1235                         continue;
1236                 cmp = tree_entry_len(&entry) != cmplen ? 1 :
1237                       memcmp(name, entry.path, cmplen);
1238                 if (cmp > 0)
1239                         continue;
1240                 if (cmp < 0)
1241                         return;
1242                 if (name[cmplen] != '/') {
1243                         add_object_entry(entry.oid->hash,
1244                                          object_type(entry.mode),
1245                                          fullname, 1);
1246                         return;
1247                 }
1248                 if (S_ISDIR(entry.mode)) {
1249                         struct tree_desc sub;
1250                         struct pbase_tree_cache *tree;
1251                         const char *down = name+cmplen+1;
1252                         int downlen = name_cmp_len(down);
1253
1254                         tree = pbase_tree_get(entry.oid->hash);
1255                         if (!tree)
1256                                 return;
1257                         init_tree_desc(&sub, tree->tree_data, tree->tree_size);
1258
1259                         add_pbase_object(&sub, down, downlen, fullname);
1260                         pbase_tree_put(tree);
1261                 }
1262         }
1263 }
1264
1265 static unsigned *done_pbase_paths;
1266 static int done_pbase_paths_num;
1267 static int done_pbase_paths_alloc;
1268 static int done_pbase_path_pos(unsigned hash)
1269 {
1270         int lo = 0;
1271         int hi = done_pbase_paths_num;
1272         while (lo < hi) {
1273                 int mi = (hi + lo) / 2;
1274                 if (done_pbase_paths[mi] == hash)
1275                         return mi;
1276                 if (done_pbase_paths[mi] < hash)
1277                         hi = mi;
1278                 else
1279                         lo = mi + 1;
1280         }
1281         return -lo-1;
1282 }
1283
1284 static int check_pbase_path(unsigned hash)
1285 {
1286         int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
1287         if (0 <= pos)
1288                 return 1;
1289         pos = -pos - 1;
1290         ALLOC_GROW(done_pbase_paths,
1291                    done_pbase_paths_num + 1,
1292                    done_pbase_paths_alloc);
1293         done_pbase_paths_num++;
1294         if (pos < done_pbase_paths_num)
1295                 memmove(done_pbase_paths + pos + 1,
1296                         done_pbase_paths + pos,
1297                         (done_pbase_paths_num - pos - 1) * sizeof(unsigned));
1298         done_pbase_paths[pos] = hash;
1299         return 0;
1300 }
1301
1302 static void add_preferred_base_object(const char *name)
1303 {
1304         struct pbase_tree *it;
1305         int cmplen;
1306         unsigned hash = pack_name_hash(name);
1307
1308         if (!num_preferred_base || check_pbase_path(hash))
1309                 return;
1310
1311         cmplen = name_cmp_len(name);
1312         for (it = pbase_tree; it; it = it->next) {
1313                 if (cmplen == 0) {
1314                         add_object_entry(it->pcache.sha1, OBJ_TREE, NULL, 1);
1315                 }
1316                 else {
1317                         struct tree_desc tree;
1318                         init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);
1319                         add_pbase_object(&tree, name, cmplen, name);
1320                 }
1321         }
1322 }
1323
1324 static void add_preferred_base(unsigned char *sha1)
1325 {
1326         struct pbase_tree *it;
1327         void *data;
1328         unsigned long size;
1329         unsigned char tree_sha1[20];
1330
1331         if (window <= num_preferred_base++)
1332                 return;
1333
1334         data = read_object_with_reference(sha1, tree_type, &size, tree_sha1);
1335         if (!data)
1336                 return;
1337
1338         for (it = pbase_tree; it; it = it->next) {
1339                 if (!hashcmp(it->pcache.sha1, tree_sha1)) {
1340                         free(data);
1341                         return;
1342                 }
1343         }
1344
1345         it = xcalloc(1, sizeof(*it));
1346         it->next = pbase_tree;
1347         pbase_tree = it;
1348
1349         hashcpy(it->pcache.sha1, tree_sha1);
1350         it->pcache.tree_data = data;
1351         it->pcache.tree_size = size;
1352 }
1353
1354 static void cleanup_preferred_base(void)
1355 {
1356         struct pbase_tree *it;
1357         unsigned i;
1358
1359         it = pbase_tree;
1360         pbase_tree = NULL;
1361         while (it) {
1362                 struct pbase_tree *this = it;
1363                 it = this->next;
1364                 free(this->pcache.tree_data);
1365                 free(this);
1366         }
1367
1368         for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {
1369                 if (!pbase_tree_cache[i])
1370                         continue;
1371                 free(pbase_tree_cache[i]->tree_data);
1372                 free(pbase_tree_cache[i]);
1373                 pbase_tree_cache[i] = NULL;
1374         }
1375
1376         free(done_pbase_paths);
1377         done_pbase_paths = NULL;
1378         done_pbase_paths_num = done_pbase_paths_alloc = 0;
1379 }
1380
1381 static void check_object(struct object_entry *entry)
1382 {
1383         if (entry->in_pack) {
1384                 struct packed_git *p = entry->in_pack;
1385                 struct pack_window *w_curs = NULL;
1386                 const unsigned char *base_ref = NULL;
1387                 struct object_entry *base_entry;
1388                 unsigned long used, used_0;
1389                 unsigned long avail;
1390                 off_t ofs;
1391                 unsigned char *buf, c;
1392
1393                 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);
1394
1395                 /*
1396                  * We want in_pack_type even if we do not reuse delta
1397                  * since non-delta representations could still be reused.
1398                  */
1399                 used = unpack_object_header_buffer(buf, avail,
1400                                                    &entry->in_pack_type,
1401                                                    &entry->size);
1402                 if (used == 0)
1403                         goto give_up;
1404
1405                 /*
1406                  * Determine if this is a delta and if so whether we can
1407                  * reuse it or not.  Otherwise let's find out as cheaply as
1408                  * possible what the actual type and size for this object is.
1409                  */
1410                 switch (entry->in_pack_type) {
1411                 default:
1412                         /* Not a delta hence we've already got all we need. */
1413                         entry->type = entry->in_pack_type;
1414                         entry->in_pack_header_size = used;
1415                         if (entry->type < OBJ_COMMIT || entry->type > OBJ_BLOB)
1416                                 goto give_up;
1417                         unuse_pack(&w_curs);
1418                         return;
1419                 case OBJ_REF_DELTA:
1420                         if (reuse_delta && !entry->preferred_base)
1421                                 base_ref = use_pack(p, &w_curs,
1422                                                 entry->in_pack_offset + used, NULL);
1423                         entry->in_pack_header_size = used + 20;
1424                         break;
1425                 case OBJ_OFS_DELTA:
1426                         buf = use_pack(p, &w_curs,
1427                                        entry->in_pack_offset + used, NULL);
1428                         used_0 = 0;
1429                         c = buf[used_0++];
1430                         ofs = c & 127;
1431                         while (c & 128) {
1432                                 ofs += 1;
1433                                 if (!ofs || MSB(ofs, 7)) {
1434                                         error("delta base offset overflow in pack for %s",
1435                                               sha1_to_hex(entry->idx.sha1));
1436                                         goto give_up;
1437                                 }
1438                                 c = buf[used_0++];
1439                                 ofs = (ofs << 7) + (c & 127);
1440                         }
1441                         ofs = entry->in_pack_offset - ofs;
1442                         if (ofs <= 0 || ofs >= entry->in_pack_offset) {
1443                                 error("delta base offset out of bound for %s",
1444                                       sha1_to_hex(entry->idx.sha1));
1445                                 goto give_up;
1446                         }
1447                         if (reuse_delta && !entry->preferred_base) {
1448                                 struct revindex_entry *revidx;
1449                                 revidx = find_pack_revindex(p, ofs);
1450                                 if (!revidx)
1451                                         goto give_up;
1452                                 base_ref = nth_packed_object_sha1(p, revidx->nr);
1453                         }
1454                         entry->in_pack_header_size = used + used_0;
1455                         break;
1456                 }
1457
1458                 if (base_ref && (base_entry = packlist_find(&to_pack, base_ref, NULL))) {
1459                         /*
1460                          * If base_ref was set above that means we wish to
1461                          * reuse delta data, and we even found that base
1462                          * in the list of objects we want to pack. Goodie!
1463                          *
1464                          * Depth value does not matter - find_deltas() will
1465                          * never consider reused delta as the base object to
1466                          * deltify other objects against, in order to avoid
1467                          * circular deltas.
1468                          */
1469                         entry->type = entry->in_pack_type;
1470                         entry->delta = base_entry;
1471                         entry->delta_size = entry->size;
1472                         entry->delta_sibling = base_entry->delta_child;
1473                         base_entry->delta_child = entry;
1474                         unuse_pack(&w_curs);
1475                         return;
1476                 }
1477
1478                 if (entry->type) {
1479                         /*
1480                          * This must be a delta and we already know what the
1481                          * final object type is.  Let's extract the actual
1482                          * object size from the delta header.
1483                          */
1484                         entry->size = get_size_from_delta(p, &w_curs,
1485                                         entry->in_pack_offset + entry->in_pack_header_size);
1486                         if (entry->size == 0)
1487                                 goto give_up;
1488                         unuse_pack(&w_curs);
1489                         return;
1490                 }
1491
1492                 /*
1493                  * No choice but to fall back to the recursive delta walk
1494                  * with sha1_object_info() to find about the object type
1495                  * at this point...
1496                  */
1497                 give_up:
1498                 unuse_pack(&w_curs);
1499         }
1500
1501         entry->type = sha1_object_info(entry->idx.sha1, &entry->size);
1502         /*
1503          * The error condition is checked in prepare_pack().  This is
1504          * to permit a missing preferred base object to be ignored
1505          * as a preferred base.  Doing so can result in a larger
1506          * pack file, but the transfer will still take place.
1507          */
1508 }
1509
1510 static int pack_offset_sort(const void *_a, const void *_b)
1511 {
1512         const struct object_entry *a = *(struct object_entry **)_a;
1513         const struct object_entry *b = *(struct object_entry **)_b;
1514
1515         /* avoid filesystem trashing with loose objects */
1516         if (!a->in_pack && !b->in_pack)
1517                 return hashcmp(a->idx.sha1, b->idx.sha1);
1518
1519         if (a->in_pack < b->in_pack)
1520                 return -1;
1521         if (a->in_pack > b->in_pack)
1522                 return 1;
1523         return a->in_pack_offset < b->in_pack_offset ? -1 :
1524                         (a->in_pack_offset > b->in_pack_offset);
1525 }
1526
1527 /*
1528  * Drop an on-disk delta we were planning to reuse. Naively, this would
1529  * just involve blanking out the "delta" field, but we have to deal
1530  * with some extra book-keeping:
1531  *
1532  *   1. Removing ourselves from the delta_sibling linked list.
1533  *
1534  *   2. Updating our size/type to the non-delta representation. These were
1535  *      either not recorded initially (size) or overwritten with the delta type
1536  *      (type) when check_object() decided to reuse the delta.
1537  *
1538  *   3. Resetting our delta depth, as we are now a base object.
1539  */
1540 static void drop_reused_delta(struct object_entry *entry)
1541 {
1542         struct object_entry **p = &entry->delta->delta_child;
1543         struct object_info oi = OBJECT_INFO_INIT;
1544
1545         while (*p) {
1546                 if (*p == entry)
1547                         *p = (*p)->delta_sibling;
1548                 else
1549                         p = &(*p)->delta_sibling;
1550         }
1551         entry->delta = NULL;
1552         entry->depth = 0;
1553
1554         oi.sizep = &entry->size;
1555         oi.typep = &entry->type;
1556         if (packed_object_info(entry->in_pack, entry->in_pack_offset, &oi) < 0) {
1557                 /*
1558                  * We failed to get the info from this pack for some reason;
1559                  * fall back to sha1_object_info, which may find another copy.
1560                  * And if that fails, the error will be recorded in entry->type
1561                  * and dealt with in prepare_pack().
1562                  */
1563                 entry->type = sha1_object_info(entry->idx.sha1, &entry->size);
1564         }
1565 }
1566
1567 /*
1568  * Follow the chain of deltas from this entry onward, throwing away any links
1569  * that cause us to hit a cycle (as determined by the DFS state flags in
1570  * the entries).
1571  *
1572  * We also detect too-long reused chains that would violate our --depth
1573  * limit.
1574  */
1575 static void break_delta_chains(struct object_entry *entry)
1576 {
1577         /*
1578          * The actual depth of each object we will write is stored as an int,
1579          * as it cannot exceed our int "depth" limit. But before we break
1580          * changes based no that limit, we may potentially go as deep as the
1581          * number of objects, which is elsewhere bounded to a uint32_t.
1582          */
1583         uint32_t total_depth;
1584         struct object_entry *cur, *next;
1585
1586         for (cur = entry, total_depth = 0;
1587              cur;
1588              cur = cur->delta, total_depth++) {
1589                 if (cur->dfs_state == DFS_DONE) {
1590                         /*
1591                          * We've already seen this object and know it isn't
1592                          * part of a cycle. We do need to append its depth
1593                          * to our count.
1594                          */
1595                         total_depth += cur->depth;
1596                         break;
1597                 }
1598
1599                 /*
1600                  * We break cycles before looping, so an ACTIVE state (or any
1601                  * other cruft which made its way into the state variable)
1602                  * is a bug.
1603                  */
1604                 if (cur->dfs_state != DFS_NONE)
1605                         die("BUG: confusing delta dfs state in first pass: %d",
1606                             cur->dfs_state);
1607
1608                 /*
1609                  * Now we know this is the first time we've seen the object. If
1610                  * it's not a delta, we're done traversing, but we'll mark it
1611                  * done to save time on future traversals.
1612                  */
1613                 if (!cur->delta) {
1614                         cur->dfs_state = DFS_DONE;
1615                         break;
1616                 }
1617
1618                 /*
1619                  * Mark ourselves as active and see if the next step causes
1620                  * us to cycle to another active object. It's important to do
1621                  * this _before_ we loop, because it impacts where we make the
1622                  * cut, and thus how our total_depth counter works.
1623                  * E.g., We may see a partial loop like:
1624                  *
1625                  *   A -> B -> C -> D -> B
1626                  *
1627                  * Cutting B->C breaks the cycle. But now the depth of A is
1628                  * only 1, and our total_depth counter is at 3. The size of the
1629                  * error is always one less than the size of the cycle we
1630                  * broke. Commits C and D were "lost" from A's chain.
1631                  *
1632                  * If we instead cut D->B, then the depth of A is correct at 3.
1633                  * We keep all commits in the chain that we examined.
1634                  */
1635                 cur->dfs_state = DFS_ACTIVE;
1636                 if (cur->delta->dfs_state == DFS_ACTIVE) {
1637                         drop_reused_delta(cur);
1638                         cur->dfs_state = DFS_DONE;
1639                         break;
1640                 }
1641         }
1642
1643         /*
1644          * And now that we've gone all the way to the bottom of the chain, we
1645          * need to clear the active flags and set the depth fields as
1646          * appropriate. Unlike the loop above, which can quit when it drops a
1647          * delta, we need to keep going to look for more depth cuts. So we need
1648          * an extra "next" pointer to keep going after we reset cur->delta.
1649          */
1650         for (cur = entry; cur; cur = next) {
1651                 next = cur->delta;
1652
1653                 /*
1654                  * We should have a chain of zero or more ACTIVE states down to
1655                  * a final DONE. We can quit after the DONE, because either it
1656                  * has no bases, or we've already handled them in a previous
1657                  * call.
1658                  */
1659                 if (cur->dfs_state == DFS_DONE)
1660                         break;
1661                 else if (cur->dfs_state != DFS_ACTIVE)
1662                         die("BUG: confusing delta dfs state in second pass: %d",
1663                             cur->dfs_state);
1664
1665                 /*
1666                  * If the total_depth is more than depth, then we need to snip
1667                  * the chain into two or more smaller chains that don't exceed
1668                  * the maximum depth. Most of the resulting chains will contain
1669                  * (depth + 1) entries (i.e., depth deltas plus one base), and
1670                  * the last chain (i.e., the one containing entry) will contain
1671                  * whatever entries are left over, namely
1672                  * (total_depth % (depth + 1)) of them.
1673                  *
1674                  * Since we are iterating towards decreasing depth, we need to
1675                  * decrement total_depth as we go, and we need to write to the
1676                  * entry what its final depth will be after all of the
1677                  * snipping. Since we're snipping into chains of length (depth
1678                  * + 1) entries, the final depth of an entry will be its
1679                  * original depth modulo (depth + 1). Any time we encounter an
1680                  * entry whose final depth is supposed to be zero, we snip it
1681                  * from its delta base, thereby making it so.
1682                  */
1683                 cur->depth = (total_depth--) % (depth + 1);
1684                 if (!cur->depth)
1685                         drop_reused_delta(cur);
1686
1687                 cur->dfs_state = DFS_DONE;
1688         }
1689 }
1690
1691 static void get_object_details(void)
1692 {
1693         uint32_t i;
1694         struct object_entry **sorted_by_offset;
1695
1696         sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));
1697         for (i = 0; i < to_pack.nr_objects; i++)
1698                 sorted_by_offset[i] = to_pack.objects + i;
1699         QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
1700
1701         for (i = 0; i < to_pack.nr_objects; i++) {
1702                 struct object_entry *entry = sorted_by_offset[i];
1703                 check_object(entry);
1704                 if (big_file_threshold < entry->size)
1705                         entry->no_try_delta = 1;
1706         }
1707
1708         /*
1709          * This must happen in a second pass, since we rely on the delta
1710          * information for the whole list being completed.
1711          */
1712         for (i = 0; i < to_pack.nr_objects; i++)
1713                 break_delta_chains(&to_pack.objects[i]);
1714
1715         free(sorted_by_offset);
1716 }
1717
1718 /*
1719  * We search for deltas in a list sorted by type, by filename hash, and then
1720  * by size, so that we see progressively smaller and smaller files.
1721  * That's because we prefer deltas to be from the bigger file
1722  * to the smaller -- deletes are potentially cheaper, but perhaps
1723  * more importantly, the bigger file is likely the more recent
1724  * one.  The deepest deltas are therefore the oldest objects which are
1725  * less susceptible to be accessed often.
1726  */
1727 static int type_size_sort(const void *_a, const void *_b)
1728 {
1729         const struct object_entry *a = *(struct object_entry **)_a;
1730         const struct object_entry *b = *(struct object_entry **)_b;
1731
1732         if (a->type > b->type)
1733                 return -1;
1734         if (a->type < b->type)
1735                 return 1;
1736         if (a->hash > b->hash)
1737                 return -1;
1738         if (a->hash < b->hash)
1739                 return 1;
1740         if (a->preferred_base > b->preferred_base)
1741                 return -1;
1742         if (a->preferred_base < b->preferred_base)
1743                 return 1;
1744         if (a->size > b->size)
1745                 return -1;
1746         if (a->size < b->size)
1747                 return 1;
1748         return a < b ? -1 : (a > b);  /* newest first */
1749 }
1750
1751 struct unpacked {
1752         struct object_entry *entry;
1753         void *data;
1754         struct delta_index *index;
1755         unsigned depth;
1756 };
1757
1758 static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
1759                            unsigned long delta_size)
1760 {
1761         if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
1762                 return 0;
1763
1764         if (delta_size < cache_max_small_delta_size)
1765                 return 1;
1766
1767         /* cache delta, if objects are large enough compared to delta size */
1768         if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))
1769                 return 1;
1770
1771         return 0;
1772 }
1773
1774 #ifndef NO_PTHREADS
1775
1776 static pthread_mutex_t read_mutex;
1777 #define read_lock()             pthread_mutex_lock(&read_mutex)
1778 #define read_unlock()           pthread_mutex_unlock(&read_mutex)
1779
1780 static pthread_mutex_t cache_mutex;
1781 #define cache_lock()            pthread_mutex_lock(&cache_mutex)
1782 #define cache_unlock()          pthread_mutex_unlock(&cache_mutex)
1783
1784 static pthread_mutex_t progress_mutex;
1785 #define progress_lock()         pthread_mutex_lock(&progress_mutex)
1786 #define progress_unlock()       pthread_mutex_unlock(&progress_mutex)
1787
1788 #else
1789
1790 #define read_lock()             (void)0
1791 #define read_unlock()           (void)0
1792 #define cache_lock()            (void)0
1793 #define cache_unlock()          (void)0
1794 #define progress_lock()         (void)0
1795 #define progress_unlock()       (void)0
1796
1797 #endif
1798
1799 static int try_delta(struct unpacked *trg, struct unpacked *src,
1800                      unsigned max_depth, unsigned long *mem_usage)
1801 {
1802         struct object_entry *trg_entry = trg->entry;
1803         struct object_entry *src_entry = src->entry;
1804         unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
1805         unsigned ref_depth;
1806         enum object_type type;
1807         void *delta_buf;
1808
1809         /* Don't bother doing diffs between different types */
1810         if (trg_entry->type != src_entry->type)
1811                 return -1;
1812
1813         /*
1814          * We do not bother to try a delta that we discarded on an
1815          * earlier try, but only when reusing delta data.  Note that
1816          * src_entry that is marked as the preferred_base should always
1817          * be considered, as even if we produce a suboptimal delta against
1818          * it, we will still save the transfer cost, as we already know
1819          * the other side has it and we won't send src_entry at all.
1820          */
1821         if (reuse_delta && trg_entry->in_pack &&
1822             trg_entry->in_pack == src_entry->in_pack &&
1823             !src_entry->preferred_base &&
1824             trg_entry->in_pack_type != OBJ_REF_DELTA &&
1825             trg_entry->in_pack_type != OBJ_OFS_DELTA)
1826                 return 0;
1827
1828         /* Let's not bust the allowed depth. */
1829         if (src->depth >= max_depth)
1830                 return 0;
1831
1832         /* Now some size filtering heuristics. */
1833         trg_size = trg_entry->size;
1834         if (!trg_entry->delta) {
1835                 max_size = trg_size/2 - 20;
1836                 ref_depth = 1;
1837         } else {
1838                 max_size = trg_entry->delta_size;
1839                 ref_depth = trg->depth;
1840         }
1841         max_size = (uint64_t)max_size * (max_depth - src->depth) /
1842                                                 (max_depth - ref_depth + 1);
1843         if (max_size == 0)
1844                 return 0;
1845         src_size = src_entry->size;
1846         sizediff = src_size < trg_size ? trg_size - src_size : 0;
1847         if (sizediff >= max_size)
1848                 return 0;
1849         if (trg_size < src_size / 32)
1850                 return 0;
1851
1852         /* Load data if not already done */
1853         if (!trg->data) {
1854                 read_lock();
1855                 trg->data = read_sha1_file(trg_entry->idx.sha1, &type, &sz);
1856                 read_unlock();
1857                 if (!trg->data)
1858                         die("object %s cannot be read",
1859                             sha1_to_hex(trg_entry->idx.sha1));
1860                 if (sz != trg_size)
1861                         die("object %s inconsistent object length (%lu vs %lu)",
1862                             sha1_to_hex(trg_entry->idx.sha1), sz, trg_size);
1863                 *mem_usage += sz;
1864         }
1865         if (!src->data) {
1866                 read_lock();
1867                 src->data = read_sha1_file(src_entry->idx.sha1, &type, &sz);
1868                 read_unlock();
1869                 if (!src->data) {
1870                         if (src_entry->preferred_base) {
1871                                 static int warned = 0;
1872                                 if (!warned++)
1873                                         warning("object %s cannot be read",
1874                                                 sha1_to_hex(src_entry->idx.sha1));
1875                                 /*
1876                                  * Those objects are not included in the
1877                                  * resulting pack.  Be resilient and ignore
1878                                  * them if they can't be read, in case the
1879                                  * pack could be created nevertheless.
1880                                  */
1881                                 return 0;
1882                         }
1883                         die("object %s cannot be read",
1884                             sha1_to_hex(src_entry->idx.sha1));
1885                 }
1886                 if (sz != src_size)
1887                         die("object %s inconsistent object length (%lu vs %lu)",
1888                             sha1_to_hex(src_entry->idx.sha1), sz, src_size);
1889                 *mem_usage += sz;
1890         }
1891         if (!src->index) {
1892                 src->index = create_delta_index(src->data, src_size);
1893                 if (!src->index) {
1894                         static int warned = 0;
1895                         if (!warned++)
1896                                 warning("suboptimal pack - out of memory");
1897                         return 0;
1898                 }
1899                 *mem_usage += sizeof_delta_index(src->index);
1900         }
1901
1902         delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);
1903         if (!delta_buf)
1904                 return 0;
1905
1906         if (trg_entry->delta) {
1907                 /* Prefer only shallower same-sized deltas. */
1908                 if (delta_size == trg_entry->delta_size &&
1909                     src->depth + 1 >= trg->depth) {
1910                         free(delta_buf);
1911                         return 0;
1912                 }
1913         }
1914
1915         /*
1916          * Handle memory allocation outside of the cache
1917          * accounting lock.  Compiler will optimize the strangeness
1918          * away when NO_PTHREADS is defined.
1919          */
1920         free(trg_entry->delta_data);
1921         cache_lock();
1922         if (trg_entry->delta_data) {
1923                 delta_cache_size -= trg_entry->delta_size;
1924                 trg_entry->delta_data = NULL;
1925         }
1926         if (delta_cacheable(src_size, trg_size, delta_size)) {
1927                 delta_cache_size += delta_size;
1928                 cache_unlock();
1929                 trg_entry->delta_data = xrealloc(delta_buf, delta_size);
1930         } else {
1931                 cache_unlock();
1932                 free(delta_buf);
1933         }
1934
1935         trg_entry->delta = src_entry;
1936         trg_entry->delta_size = delta_size;
1937         trg->depth = src->depth + 1;
1938
1939         return 1;
1940 }
1941
1942 static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1943 {
1944         struct object_entry *child = me->delta_child;
1945         unsigned int m = n;
1946         while (child) {
1947                 unsigned int c = check_delta_limit(child, n + 1);
1948                 if (m < c)
1949                         m = c;
1950                 child = child->delta_sibling;
1951         }
1952         return m;
1953 }
1954
1955 static unsigned long free_unpacked(struct unpacked *n)
1956 {
1957         unsigned long freed_mem = sizeof_delta_index(n->index);
1958         free_delta_index(n->index);
1959         n->index = NULL;
1960         if (n->data) {
1961                 freed_mem += n->entry->size;
1962                 free(n->data);
1963                 n->data = NULL;
1964         }
1965         n->entry = NULL;
1966         n->depth = 0;
1967         return freed_mem;
1968 }
1969
1970 static void find_deltas(struct object_entry **list, unsigned *list_size,
1971                         int window, int depth, unsigned *processed)
1972 {
1973         uint32_t i, idx = 0, count = 0;
1974         struct unpacked *array;
1975         unsigned long mem_usage = 0;
1976
1977         array = xcalloc(window, sizeof(struct unpacked));
1978
1979         for (;;) {
1980                 struct object_entry *entry;
1981                 struct unpacked *n = array + idx;
1982                 int j, max_depth, best_base = -1;
1983
1984                 progress_lock();
1985                 if (!*list_size) {
1986                         progress_unlock();
1987                         break;
1988                 }
1989                 entry = *list++;
1990                 (*list_size)--;
1991                 if (!entry->preferred_base) {
1992                         (*processed)++;
1993                         display_progress(progress_state, *processed);
1994                 }
1995                 progress_unlock();
1996
1997                 mem_usage -= free_unpacked(n);
1998                 n->entry = entry;
1999
2000                 while (window_memory_limit &&
2001                        mem_usage > window_memory_limit &&
2002                        count > 1) {
2003                         uint32_t tail = (idx + window - count) % window;
2004                         mem_usage -= free_unpacked(array + tail);
2005                         count--;
2006                 }
2007
2008                 /* We do not compute delta to *create* objects we are not
2009                  * going to pack.
2010                  */
2011                 if (entry->preferred_base)
2012                         goto next;
2013
2014                 /*
2015                  * If the current object is at pack edge, take the depth the
2016                  * objects that depend on the current object into account
2017                  * otherwise they would become too deep.
2018                  */
2019                 max_depth = depth;
2020                 if (entry->delta_child) {
2021                         max_depth -= check_delta_limit(entry, 0);
2022                         if (max_depth <= 0)
2023                                 goto next;
2024                 }
2025
2026                 j = window;
2027                 while (--j > 0) {
2028                         int ret;
2029                         uint32_t other_idx = idx + j;
2030                         struct unpacked *m;
2031                         if (other_idx >= window)
2032                                 other_idx -= window;
2033                         m = array + other_idx;
2034                         if (!m->entry)
2035                                 break;
2036                         ret = try_delta(n, m, max_depth, &mem_usage);
2037                         if (ret < 0)
2038                                 break;
2039                         else if (ret > 0)
2040                                 best_base = other_idx;
2041                 }
2042
2043                 /*
2044                  * If we decided to cache the delta data, then it is best
2045                  * to compress it right away.  First because we have to do
2046                  * it anyway, and doing it here while we're threaded will
2047                  * save a lot of time in the non threaded write phase,
2048                  * as well as allow for caching more deltas within
2049                  * the same cache size limit.
2050                  * ...
2051                  * But only if not writing to stdout, since in that case
2052                  * the network is most likely throttling writes anyway,
2053                  * and therefore it is best to go to the write phase ASAP
2054                  * instead, as we can afford spending more time compressing
2055                  * between writes at that moment.
2056                  */
2057                 if (entry->delta_data && !pack_to_stdout) {
2058                         entry->z_delta_size = do_compress(&entry->delta_data,
2059                                                           entry->delta_size);
2060                         cache_lock();
2061                         delta_cache_size -= entry->delta_size;
2062                         delta_cache_size += entry->z_delta_size;
2063                         cache_unlock();
2064                 }
2065
2066                 /* if we made n a delta, and if n is already at max
2067                  * depth, leaving it in the window is pointless.  we
2068                  * should evict it first.
2069                  */
2070                 if (entry->delta && max_depth <= n->depth)
2071                         continue;
2072
2073                 /*
2074                  * Move the best delta base up in the window, after the
2075                  * currently deltified object, to keep it longer.  It will
2076                  * be the first base object to be attempted next.
2077                  */
2078                 if (entry->delta) {
2079                         struct unpacked swap = array[best_base];
2080                         int dist = (window + idx - best_base) % window;
2081                         int dst = best_base;
2082                         while (dist--) {
2083                                 int src = (dst + 1) % window;
2084                                 array[dst] = array[src];
2085                                 dst = src;
2086                         }
2087                         array[dst] = swap;
2088                 }
2089
2090                 next:
2091                 idx++;
2092                 if (count + 1 < window)
2093                         count++;
2094                 if (idx >= window)
2095                         idx = 0;
2096         }
2097
2098         for (i = 0; i < window; ++i) {
2099                 free_delta_index(array[i].index);
2100                 free(array[i].data);
2101         }
2102         free(array);
2103 }
2104
2105 #ifndef NO_PTHREADS
2106
2107 static void try_to_free_from_threads(size_t size)
2108 {
2109         read_lock();
2110         release_pack_memory(size);
2111         read_unlock();
2112 }
2113
2114 static try_to_free_t old_try_to_free_routine;
2115
2116 /*
2117  * The main thread waits on the condition that (at least) one of the workers
2118  * has stopped working (which is indicated in the .working member of
2119  * struct thread_params).
2120  * When a work thread has completed its work, it sets .working to 0 and
2121  * signals the main thread and waits on the condition that .data_ready
2122  * becomes 1.
2123  */
2124
2125 struct thread_params {
2126         pthread_t thread;
2127         struct object_entry **list;
2128         unsigned list_size;
2129         unsigned remaining;
2130         int window;
2131         int depth;
2132         int working;
2133         int data_ready;
2134         pthread_mutex_t mutex;
2135         pthread_cond_t cond;
2136         unsigned *processed;
2137 };
2138
2139 static pthread_cond_t progress_cond;
2140
2141 /*
2142  * Mutex and conditional variable can't be statically-initialized on Windows.
2143  */
2144 static void init_threaded_search(void)
2145 {
2146         init_recursive_mutex(&read_mutex);
2147         pthread_mutex_init(&cache_mutex, NULL);
2148         pthread_mutex_init(&progress_mutex, NULL);
2149         pthread_cond_init(&progress_cond, NULL);
2150         old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads);
2151 }
2152
2153 static void cleanup_threaded_search(void)
2154 {
2155         set_try_to_free_routine(old_try_to_free_routine);
2156         pthread_cond_destroy(&progress_cond);
2157         pthread_mutex_destroy(&read_mutex);
2158         pthread_mutex_destroy(&cache_mutex);
2159         pthread_mutex_destroy(&progress_mutex);
2160 }
2161
2162 static void *threaded_find_deltas(void *arg)
2163 {
2164         struct thread_params *me = arg;
2165
2166         while (me->remaining) {
2167                 find_deltas(me->list, &me->remaining,
2168                             me->window, me->depth, me->processed);
2169
2170                 progress_lock();
2171                 me->working = 0;
2172                 pthread_cond_signal(&progress_cond);
2173                 progress_unlock();
2174
2175                 /*
2176                  * We must not set ->data_ready before we wait on the
2177                  * condition because the main thread may have set it to 1
2178                  * before we get here. In order to be sure that new
2179                  * work is available if we see 1 in ->data_ready, it
2180                  * was initialized to 0 before this thread was spawned
2181                  * and we reset it to 0 right away.
2182                  */
2183                 pthread_mutex_lock(&me->mutex);
2184                 while (!me->data_ready)
2185                         pthread_cond_wait(&me->cond, &me->mutex);
2186                 me->data_ready = 0;
2187                 pthread_mutex_unlock(&me->mutex);
2188         }
2189         /* leave ->working 1 so that this doesn't get more work assigned */
2190         return NULL;
2191 }
2192
2193 static void ll_find_deltas(struct object_entry **list, unsigned list_size,
2194                            int window, int depth, unsigned *processed)
2195 {
2196         struct thread_params *p;
2197         int i, ret, active_threads = 0;
2198
2199         init_threaded_search();
2200
2201         if (delta_search_threads <= 1) {
2202                 find_deltas(list, &list_size, window, depth, processed);
2203                 cleanup_threaded_search();
2204                 return;
2205         }
2206         if (progress > pack_to_stdout)
2207                 fprintf(stderr, "Delta compression using up to %d threads.\n",
2208                                 delta_search_threads);
2209         p = xcalloc(delta_search_threads, sizeof(*p));
2210
2211         /* Partition the work amongst work threads. */
2212         for (i = 0; i < delta_search_threads; i++) {
2213                 unsigned sub_size = list_size / (delta_search_threads - i);
2214
2215                 /* don't use too small segments or no deltas will be found */
2216                 if (sub_size < 2*window && i+1 < delta_search_threads)
2217                         sub_size = 0;
2218
2219                 p[i].window = window;
2220                 p[i].depth = depth;
2221                 p[i].processed = processed;
2222                 p[i].working = 1;
2223                 p[i].data_ready = 0;
2224
2225                 /* try to split chunks on "path" boundaries */
2226                 while (sub_size && sub_size < list_size &&
2227                        list[sub_size]->hash &&
2228                        list[sub_size]->hash == list[sub_size-1]->hash)
2229                         sub_size++;
2230
2231                 p[i].list = list;
2232                 p[i].list_size = sub_size;
2233                 p[i].remaining = sub_size;
2234
2235                 list += sub_size;
2236                 list_size -= sub_size;
2237         }
2238
2239         /* Start work threads. */
2240         for (i = 0; i < delta_search_threads; i++) {
2241                 if (!p[i].list_size)
2242                         continue;
2243                 pthread_mutex_init(&p[i].mutex, NULL);
2244                 pthread_cond_init(&p[i].cond, NULL);
2245                 ret = pthread_create(&p[i].thread, NULL,
2246                                      threaded_find_deltas, &p[i]);
2247                 if (ret)
2248                         die("unable to create thread: %s", strerror(ret));
2249                 active_threads++;
2250         }
2251
2252         /*
2253          * Now let's wait for work completion.  Each time a thread is done
2254          * with its work, we steal half of the remaining work from the
2255          * thread with the largest number of unprocessed objects and give
2256          * it to that newly idle thread.  This ensure good load balancing
2257          * until the remaining object list segments are simply too short
2258          * to be worth splitting anymore.
2259          */
2260         while (active_threads) {
2261                 struct thread_params *target = NULL;
2262                 struct thread_params *victim = NULL;
2263                 unsigned sub_size = 0;
2264
2265                 progress_lock();
2266                 for (;;) {
2267                         for (i = 0; !target && i < delta_search_threads; i++)
2268                                 if (!p[i].working)
2269                                         target = &p[i];
2270                         if (target)
2271                                 break;
2272                         pthread_cond_wait(&progress_cond, &progress_mutex);
2273                 }
2274
2275                 for (i = 0; i < delta_search_threads; i++)
2276                         if (p[i].remaining > 2*window &&
2277                             (!victim || victim->remaining < p[i].remaining))
2278                                 victim = &p[i];
2279                 if (victim) {
2280                         sub_size = victim->remaining / 2;
2281                         list = victim->list + victim->list_size - sub_size;
2282                         while (sub_size && list[0]->hash &&
2283                                list[0]->hash == list[-1]->hash) {
2284                                 list++;
2285                                 sub_size--;
2286                         }
2287                         if (!sub_size) {
2288                                 /*
2289                                  * It is possible for some "paths" to have
2290                                  * so many objects that no hash boundary
2291                                  * might be found.  Let's just steal the
2292                                  * exact half in that case.
2293                                  */
2294                                 sub_size = victim->remaining / 2;
2295                                 list -= sub_size;
2296                         }
2297                         target->list = list;
2298                         victim->list_size -= sub_size;
2299                         victim->remaining -= sub_size;
2300                 }
2301                 target->list_size = sub_size;
2302                 target->remaining = sub_size;
2303                 target->working = 1;
2304                 progress_unlock();
2305
2306                 pthread_mutex_lock(&target->mutex);
2307                 target->data_ready = 1;
2308                 pthread_cond_signal(&target->cond);
2309                 pthread_mutex_unlock(&target->mutex);
2310
2311                 if (!sub_size) {
2312                         pthread_join(target->thread, NULL);
2313                         pthread_cond_destroy(&target->cond);
2314                         pthread_mutex_destroy(&target->mutex);
2315                         active_threads--;
2316                 }
2317         }
2318         cleanup_threaded_search();
2319         free(p);
2320 }
2321
2322 #else
2323 #define ll_find_deltas(l, s, w, d, p)   find_deltas(l, &s, w, d, p)
2324 #endif
2325
2326 static void add_tag_chain(const struct object_id *oid)
2327 {
2328         struct tag *tag;
2329
2330         /*
2331          * We catch duplicates already in add_object_entry(), but we'd
2332          * prefer to do this extra check to avoid having to parse the
2333          * tag at all if we already know that it's being packed (e.g., if
2334          * it was included via bitmaps, we would not have parsed it
2335          * previously).
2336          */
2337         if (packlist_find(&to_pack, oid->hash, NULL))
2338                 return;
2339
2340         tag = lookup_tag(oid->hash);
2341         while (1) {
2342                 if (!tag || parse_tag(tag) || !tag->tagged)
2343                         die("unable to pack objects reachable from tag %s",
2344                             oid_to_hex(oid));
2345
2346                 add_object_entry(tag->object.oid.hash, OBJ_TAG, NULL, 0);
2347
2348                 if (tag->tagged->type != OBJ_TAG)
2349                         return;
2350
2351                 tag = (struct tag *)tag->tagged;
2352         }
2353 }
2354
2355 static int add_ref_tag(const char *path, const struct object_id *oid, int flag, void *cb_data)
2356 {
2357         struct object_id peeled;
2358
2359         if (starts_with(path, "refs/tags/") && /* is a tag? */
2360             !peel_ref(path, peeled.hash)    && /* peelable? */
2361             packlist_find(&to_pack, peeled.hash, NULL))      /* object packed? */
2362                 add_tag_chain(oid);
2363         return 0;
2364 }
2365
2366 static void prepare_pack(int window, int depth)
2367 {
2368         struct object_entry **delta_list;
2369         uint32_t i, nr_deltas;
2370         unsigned n;
2371
2372         get_object_details();
2373
2374         /*
2375          * If we're locally repacking then we need to be doubly careful
2376          * from now on in order to make sure no stealth corruption gets
2377          * propagated to the new pack.  Clients receiving streamed packs
2378          * should validate everything they get anyway so no need to incur
2379          * the additional cost here in that case.
2380          */
2381         if (!pack_to_stdout)
2382                 do_check_packed_object_crc = 1;
2383
2384         if (!to_pack.nr_objects || !window || !depth)
2385                 return;
2386
2387         ALLOC_ARRAY(delta_list, to_pack.nr_objects);
2388         nr_deltas = n = 0;
2389
2390         for (i = 0; i < to_pack.nr_objects; i++) {
2391                 struct object_entry *entry = to_pack.objects + i;
2392
2393                 if (entry->delta)
2394                         /* This happens if we decided to reuse existing
2395                          * delta from a pack.  "reuse_delta &&" is implied.
2396                          */
2397                         continue;
2398
2399                 if (entry->size < 50)
2400                         continue;
2401
2402                 if (entry->no_try_delta)
2403                         continue;
2404
2405                 if (!entry->preferred_base) {
2406                         nr_deltas++;
2407                         if (entry->type < 0)
2408                                 die("unable to get type of object %s",
2409                                     sha1_to_hex(entry->idx.sha1));
2410                 } else {
2411                         if (entry->type < 0) {
2412                                 /*
2413                                  * This object is not found, but we
2414                                  * don't have to include it anyway.
2415                                  */
2416                                 continue;
2417                         }
2418                 }
2419
2420                 delta_list[n++] = entry;
2421         }
2422
2423         if (nr_deltas && n > 1) {
2424                 unsigned nr_done = 0;
2425                 if (progress)
2426                         progress_state = start_progress(_("Compressing objects"),
2427                                                         nr_deltas);
2428                 QSORT(delta_list, n, type_size_sort);
2429                 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
2430                 stop_progress(&progress_state);
2431                 if (nr_done != nr_deltas)
2432                         die("inconsistency with delta count");
2433         }
2434         free(delta_list);
2435 }
2436
2437 static int git_pack_config(const char *k, const char *v, void *cb)
2438 {
2439         if (!strcmp(k, "pack.window")) {
2440                 window = git_config_int(k, v);
2441                 return 0;
2442         }
2443         if (!strcmp(k, "pack.windowmemory")) {
2444                 window_memory_limit = git_config_ulong(k, v);
2445                 return 0;
2446         }
2447         if (!strcmp(k, "pack.depth")) {
2448                 depth = git_config_int(k, v);
2449                 return 0;
2450         }
2451         if (!strcmp(k, "pack.deltacachesize")) {
2452                 max_delta_cache_size = git_config_int(k, v);
2453                 return 0;
2454         }
2455         if (!strcmp(k, "pack.deltacachelimit")) {
2456                 cache_max_small_delta_size = git_config_int(k, v);
2457                 return 0;
2458         }
2459         if (!strcmp(k, "pack.writebitmaphashcache")) {
2460                 if (git_config_bool(k, v))
2461                         write_bitmap_options |= BITMAP_OPT_HASH_CACHE;
2462                 else
2463                         write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;
2464         }
2465         if (!strcmp(k, "pack.usebitmaps")) {
2466                 use_bitmap_index_default = git_config_bool(k, v);
2467                 return 0;
2468         }
2469         if (!strcmp(k, "pack.threads")) {
2470                 delta_search_threads = git_config_int(k, v);
2471                 if (delta_search_threads < 0)
2472                         die("invalid number of threads specified (%d)",
2473                             delta_search_threads);
2474 #ifdef NO_PTHREADS
2475                 if (delta_search_threads != 1)
2476                         warning("no threads support, ignoring %s", k);
2477 #endif
2478                 return 0;
2479         }
2480         if (!strcmp(k, "pack.indexversion")) {
2481                 pack_idx_opts.version = git_config_int(k, v);
2482                 if (pack_idx_opts.version > 2)
2483                         die("bad pack.indexversion=%"PRIu32,
2484                             pack_idx_opts.version);
2485                 return 0;
2486         }
2487         return git_default_config(k, v, cb);
2488 }
2489
2490 static void read_object_list_from_stdin(void)
2491 {
2492         char line[40 + 1 + PATH_MAX + 2];
2493         unsigned char sha1[20];
2494
2495         for (;;) {
2496                 if (!fgets(line, sizeof(line), stdin)) {
2497                         if (feof(stdin))
2498                                 break;
2499                         if (!ferror(stdin))
2500                                 die("fgets returned NULL, not EOF, not error!");
2501                         if (errno != EINTR)
2502                                 die_errno("fgets");
2503                         clearerr(stdin);
2504                         continue;
2505                 }
2506                 if (line[0] == '-') {
2507                         if (get_sha1_hex(line+1, sha1))
2508                                 die("expected edge sha1, got garbage:\n %s",
2509                                     line);
2510                         add_preferred_base(sha1);
2511                         continue;
2512                 }
2513                 if (get_sha1_hex(line, sha1))
2514                         die("expected sha1, got garbage:\n %s", line);
2515
2516                 add_preferred_base_object(line+41);
2517                 add_object_entry(sha1, 0, line+41, 0);
2518         }
2519 }
2520
2521 #define OBJECT_ADDED (1u<<20)
2522
2523 static void show_commit(struct commit *commit, void *data)
2524 {
2525         add_object_entry(commit->object.oid.hash, OBJ_COMMIT, NULL, 0);
2526         commit->object.flags |= OBJECT_ADDED;
2527
2528         if (write_bitmap_index)
2529                 index_commit_for_bitmap(commit);
2530 }
2531
2532 static void show_object(struct object *obj, const char *name, void *data)
2533 {
2534         add_preferred_base_object(name);
2535         add_object_entry(obj->oid.hash, obj->type, name, 0);
2536         obj->flags |= OBJECT_ADDED;
2537 }
2538
2539 static void show_edge(struct commit *commit)
2540 {
2541         add_preferred_base(commit->object.oid.hash);
2542 }
2543
2544 struct in_pack_object {
2545         off_t offset;
2546         struct object *object;
2547 };
2548
2549 struct in_pack {
2550         int alloc;
2551         int nr;
2552         struct in_pack_object *array;
2553 };
2554
2555 static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack)
2556 {
2557         in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p);
2558         in_pack->array[in_pack->nr].object = object;
2559         in_pack->nr++;
2560 }
2561
2562 /*
2563  * Compare the objects in the offset order, in order to emulate the
2564  * "git rev-list --objects" output that produced the pack originally.
2565  */
2566 static int ofscmp(const void *a_, const void *b_)
2567 {
2568         struct in_pack_object *a = (struct in_pack_object *)a_;
2569         struct in_pack_object *b = (struct in_pack_object *)b_;
2570
2571         if (a->offset < b->offset)
2572                 return -1;
2573         else if (a->offset > b->offset)
2574                 return 1;
2575         else
2576                 return oidcmp(&a->object->oid, &b->object->oid);
2577 }
2578
2579 static void add_objects_in_unpacked_packs(struct rev_info *revs)
2580 {
2581         struct packed_git *p;
2582         struct in_pack in_pack;
2583         uint32_t i;
2584
2585         memset(&in_pack, 0, sizeof(in_pack));
2586
2587         for (p = packed_git; p; p = p->next) {
2588                 const unsigned char *sha1;
2589                 struct object *o;
2590
2591                 if (!p->pack_local || p->pack_keep)
2592                         continue;
2593                 if (open_pack_index(p))
2594                         die("cannot open pack index");
2595
2596                 ALLOC_GROW(in_pack.array,
2597                            in_pack.nr + p->num_objects,
2598                            in_pack.alloc);
2599
2600                 for (i = 0; i < p->num_objects; i++) {
2601                         sha1 = nth_packed_object_sha1(p, i);
2602                         o = lookup_unknown_object(sha1);
2603                         if (!(o->flags & OBJECT_ADDED))
2604                                 mark_in_pack_object(o, p, &in_pack);
2605                         o->flags |= OBJECT_ADDED;
2606                 }
2607         }
2608
2609         if (in_pack.nr) {
2610                 QSORT(in_pack.array, in_pack.nr, ofscmp);
2611                 for (i = 0; i < in_pack.nr; i++) {
2612                         struct object *o = in_pack.array[i].object;
2613                         add_object_entry(o->oid.hash, o->type, "", 0);
2614                 }
2615         }
2616         free(in_pack.array);
2617 }
2618
2619 static int add_loose_object(const struct object_id *oid, const char *path,
2620                             void *data)
2621 {
2622         enum object_type type = sha1_object_info(oid->hash, NULL);
2623
2624         if (type < 0) {
2625                 warning("loose object at %s could not be examined", path);
2626                 return 0;
2627         }
2628
2629         add_object_entry(oid->hash, type, "", 0);
2630         return 0;
2631 }
2632
2633 /*
2634  * We actually don't even have to worry about reachability here.
2635  * add_object_entry will weed out duplicates, so we just add every
2636  * loose object we find.
2637  */
2638 static void add_unreachable_loose_objects(void)
2639 {
2640         for_each_loose_file_in_objdir(get_object_directory(),
2641                                       add_loose_object,
2642                                       NULL, NULL, NULL);
2643 }
2644
2645 static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1)
2646 {
2647         static struct packed_git *last_found = (void *)1;
2648         struct packed_git *p;
2649
2650         p = (last_found != (void *)1) ? last_found : packed_git;
2651
2652         while (p) {
2653                 if ((!p->pack_local || p->pack_keep) &&
2654                         find_pack_entry_one(sha1, p)) {
2655                         last_found = p;
2656                         return 1;
2657                 }
2658                 if (p == last_found)
2659                         p = packed_git;
2660                 else
2661                         p = p->next;
2662                 if (p == last_found)
2663                         p = p->next;
2664         }
2665         return 0;
2666 }
2667
2668 /*
2669  * Store a list of sha1s that are should not be discarded
2670  * because they are either written too recently, or are
2671  * reachable from another object that was.
2672  *
2673  * This is filled by get_object_list.
2674  */
2675 static struct sha1_array recent_objects;
2676
2677 static int loosened_object_can_be_discarded(const unsigned char *sha1,
2678                                             unsigned long mtime)
2679 {
2680         if (!unpack_unreachable_expiration)
2681                 return 0;
2682         if (mtime > unpack_unreachable_expiration)
2683                 return 0;
2684         if (sha1_array_lookup(&recent_objects, sha1) >= 0)
2685                 return 0;
2686         return 1;
2687 }
2688
2689 static void loosen_unused_packed_objects(struct rev_info *revs)
2690 {
2691         struct packed_git *p;
2692         uint32_t i;
2693         const unsigned char *sha1;
2694
2695         for (p = packed_git; p; p = p->next) {
2696                 if (!p->pack_local || p->pack_keep)
2697                         continue;
2698
2699                 if (open_pack_index(p))
2700                         die("cannot open pack index");
2701
2702                 for (i = 0; i < p->num_objects; i++) {
2703                         sha1 = nth_packed_object_sha1(p, i);
2704                         if (!packlist_find(&to_pack, sha1, NULL) &&
2705                             !has_sha1_pack_kept_or_nonlocal(sha1) &&
2706                             !loosened_object_can_be_discarded(sha1, p->mtime))
2707                                 if (force_object_loose(sha1, p->mtime))
2708                                         die("unable to force loose object");
2709                 }
2710         }
2711 }
2712
2713 /*
2714  * This tracks any options which pack-reuse code expects to be on, or which a
2715  * reader of the pack might not understand, and which would therefore prevent
2716  * blind reuse of what we have on disk.
2717  */
2718 static int pack_options_allow_reuse(void)
2719 {
2720         return pack_to_stdout && allow_ofs_delta;
2721 }
2722
2723 static int get_object_list_from_bitmap(struct rev_info *revs)
2724 {
2725         if (prepare_bitmap_walk(revs) < 0)
2726                 return -1;
2727
2728         if (pack_options_allow_reuse() &&
2729             !reuse_partial_packfile_from_bitmap(
2730                         &reuse_packfile,
2731                         &reuse_packfile_objects,
2732                         &reuse_packfile_offset)) {
2733                 assert(reuse_packfile_objects);
2734                 nr_result += reuse_packfile_objects;
2735                 display_progress(progress_state, nr_result);
2736         }
2737
2738         traverse_bitmap_commit_list(&add_object_entry_from_bitmap);
2739         return 0;
2740 }
2741
2742 static void record_recent_object(struct object *obj,
2743                                  const char *name,
2744                                  void *data)
2745 {
2746         sha1_array_append(&recent_objects, obj->oid.hash);
2747 }
2748
2749 static void record_recent_commit(struct commit *commit, void *data)
2750 {
2751         sha1_array_append(&recent_objects, commit->object.oid.hash);
2752 }
2753
2754 static void get_object_list(int ac, const char **av)
2755 {
2756         struct rev_info revs;
2757         char line[1000];
2758         int flags = 0;
2759
2760         init_revisions(&revs, NULL);
2761         save_commit_buffer = 0;
2762         setup_revisions(ac, av, &revs, NULL);
2763
2764         /* make sure shallows are read */
2765         is_repository_shallow();
2766
2767         while (fgets(line, sizeof(line), stdin) != NULL) {
2768                 int len = strlen(line);
2769                 if (len && line[len - 1] == '\n')
2770                         line[--len] = 0;
2771                 if (!len)
2772                         break;
2773                 if (*line == '-') {
2774                         if (!strcmp(line, "--not")) {
2775                                 flags ^= UNINTERESTING;
2776                                 write_bitmap_index = 0;
2777                                 continue;
2778                         }
2779                         if (starts_with(line, "--shallow ")) {
2780                                 unsigned char sha1[20];
2781                                 if (get_sha1_hex(line + 10, sha1))
2782                                         die("not an SHA-1 '%s'", line + 10);
2783                                 register_shallow(sha1);
2784                                 use_bitmap_index = 0;
2785                                 continue;
2786                         }
2787                         die("not a rev '%s'", line);
2788                 }
2789                 if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))
2790                         die("bad revision '%s'", line);
2791         }
2792
2793         if (use_bitmap_index && !get_object_list_from_bitmap(&revs))
2794                 return;
2795
2796         if (prepare_revision_walk(&revs))
2797                 die("revision walk setup failed");
2798         mark_edges_uninteresting(&revs, show_edge);
2799         traverse_commit_list(&revs, show_commit, show_object, NULL);
2800
2801         if (unpack_unreachable_expiration) {
2802                 revs.ignore_missing_links = 1;
2803                 if (add_unseen_recent_objects_to_traversal(&revs,
2804                                 unpack_unreachable_expiration))
2805                         die("unable to add recent objects");
2806                 if (prepare_revision_walk(&revs))
2807                         die("revision walk setup failed");
2808                 traverse_commit_list(&revs, record_recent_commit,
2809                                      record_recent_object, NULL);
2810         }
2811
2812         if (keep_unreachable)
2813                 add_objects_in_unpacked_packs(&revs);
2814         if (pack_loose_unreachable)
2815                 add_unreachable_loose_objects();
2816         if (unpack_unreachable)
2817                 loosen_unused_packed_objects(&revs);
2818
2819         sha1_array_clear(&recent_objects);
2820 }
2821
2822 static int option_parse_index_version(const struct option *opt,
2823                                       const char *arg, int unset)
2824 {
2825         char *c;
2826         const char *val = arg;
2827         pack_idx_opts.version = strtoul(val, &c, 10);
2828         if (pack_idx_opts.version > 2)
2829                 die(_("unsupported index version %s"), val);
2830         if (*c == ',' && c[1])
2831                 pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);
2832         if (*c || pack_idx_opts.off32_limit & 0x80000000)
2833                 die(_("bad index version '%s'"), val);
2834         return 0;
2835 }
2836
2837 static int option_parse_unpack_unreachable(const struct option *opt,
2838                                            const char *arg, int unset)
2839 {
2840         if (unset) {
2841                 unpack_unreachable = 0;
2842                 unpack_unreachable_expiration = 0;
2843         }
2844         else {
2845                 unpack_unreachable = 1;
2846                 if (arg)
2847                         unpack_unreachable_expiration = approxidate(arg);
2848         }
2849         return 0;
2850 }
2851
2852 int cmd_pack_objects(int argc, const char **argv, const char *prefix)
2853 {
2854         int use_internal_rev_list = 0;
2855         int thin = 0;
2856         int shallow = 0;
2857         int all_progress_implied = 0;
2858         struct argv_array rp = ARGV_ARRAY_INIT;
2859         int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;
2860         int rev_list_index = 0;
2861         struct option pack_objects_options[] = {
2862                 OPT_SET_INT('q', "quiet", &progress,
2863                             N_("do not show progress meter"), 0),
2864                 OPT_SET_INT(0, "progress", &progress,
2865                             N_("show progress meter"), 1),
2866                 OPT_SET_INT(0, "all-progress", &progress,
2867                             N_("show progress meter during object writing phase"), 2),
2868                 OPT_BOOL(0, "all-progress-implied",
2869                          &all_progress_implied,
2870                          N_("similar to --all-progress when progress meter is shown")),
2871                 { OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"),
2872                   N_("write the pack index file in the specified idx format version"),
2873                   0, option_parse_index_version },
2874                 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,
2875                               N_("maximum size of each output pack file")),
2876                 OPT_BOOL(0, "local", &local,
2877                          N_("ignore borrowed objects from alternate object store")),
2878                 OPT_BOOL(0, "incremental", &incremental,
2879                          N_("ignore packed objects")),
2880                 OPT_INTEGER(0, "window", &window,
2881                             N_("limit pack window by objects")),
2882                 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,
2883                               N_("limit pack window by memory in addition to object limit")),
2884                 OPT_INTEGER(0, "depth", &depth,
2885                             N_("maximum length of delta chain allowed in the resulting pack")),
2886                 OPT_BOOL(0, "reuse-delta", &reuse_delta,
2887                          N_("reuse existing deltas")),
2888                 OPT_BOOL(0, "reuse-object", &reuse_object,
2889                          N_("reuse existing objects")),
2890                 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,
2891                          N_("use OFS_DELTA objects")),
2892                 OPT_INTEGER(0, "threads", &delta_search_threads,
2893                             N_("use threads when searching for best delta matches")),
2894                 OPT_BOOL(0, "non-empty", &non_empty,
2895                          N_("do not create an empty pack output")),
2896                 OPT_BOOL(0, "revs", &use_internal_rev_list,
2897                          N_("read revision arguments from standard input")),
2898                 { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL,
2899                   N_("limit the objects to those that are not yet packed"),
2900                   PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
2901                 { OPTION_SET_INT, 0, "all", &rev_list_all, NULL,
2902                   N_("include objects reachable from any reference"),
2903                   PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
2904                 { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL,
2905                   N_("include objects referred by reflog entries"),
2906                   PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
2907                 { OPTION_SET_INT, 0, "indexed-objects", &rev_list_index, NULL,
2908                   N_("include objects referred to by the index"),
2909                   PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
2910                 OPT_BOOL(0, "stdout", &pack_to_stdout,
2911                          N_("output pack to stdout")),
2912                 OPT_BOOL(0, "include-tag", &include_tag,
2913                          N_("include tag objects that refer to objects to be packed")),
2914                 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,
2915                          N_("keep unreachable objects")),
2916                 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,
2917                          N_("pack loose unreachable objects")),
2918                 { OPTION_CALLBACK, 0, "unpack-unreachable", NULL, N_("time"),
2919                   N_("unpack unreachable objects newer than <time>"),
2920                   PARSE_OPT_OPTARG, option_parse_unpack_unreachable },
2921                 OPT_BOOL(0, "thin", &thin,
2922                          N_("create thin packs")),
2923                 OPT_BOOL(0, "shallow", &shallow,
2924                          N_("create packs suitable for shallow fetches")),
2925                 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep,
2926                          N_("ignore packs that have companion .keep file")),
2927                 OPT_INTEGER(0, "compression", &pack_compression_level,
2928                             N_("pack compression level")),
2929                 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents,
2930                             N_("do not hide commits by grafts"), 0),
2931                 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,
2932                          N_("use a bitmap index if available to speed up counting objects")),
2933                 OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index,
2934                          N_("write a bitmap index together with the pack index")),
2935                 OPT_END(),
2936         };
2937
2938         check_replace_refs = 0;
2939
2940         reset_pack_idx_option(&pack_idx_opts);
2941         git_config(git_pack_config, NULL);
2942
2943         progress = isatty(2);
2944         argc = parse_options(argc, argv, prefix, pack_objects_options,
2945                              pack_usage, 0);
2946
2947         if (argc) {
2948                 base_name = argv[0];
2949                 argc--;
2950         }
2951         if (pack_to_stdout != !base_name || argc)
2952                 usage_with_options(pack_usage, pack_objects_options);
2953
2954         argv_array_push(&rp, "pack-objects");
2955         if (thin) {
2956                 use_internal_rev_list = 1;
2957                 argv_array_push(&rp, shallow
2958                                 ? "--objects-edge-aggressive"
2959                                 : "--objects-edge");
2960         } else
2961                 argv_array_push(&rp, "--objects");
2962
2963         if (rev_list_all) {
2964                 use_internal_rev_list = 1;
2965                 argv_array_push(&rp, "--all");
2966         }
2967         if (rev_list_reflog) {
2968                 use_internal_rev_list = 1;
2969                 argv_array_push(&rp, "--reflog");
2970         }
2971         if (rev_list_index) {
2972                 use_internal_rev_list = 1;
2973                 argv_array_push(&rp, "--indexed-objects");
2974         }
2975         if (rev_list_unpacked) {
2976                 use_internal_rev_list = 1;
2977                 argv_array_push(&rp, "--unpacked");
2978         }
2979
2980         if (!reuse_object)
2981                 reuse_delta = 0;
2982         if (pack_compression_level == -1)
2983                 pack_compression_level = Z_DEFAULT_COMPRESSION;
2984         else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)
2985                 die("bad pack compression level %d", pack_compression_level);
2986
2987         if (!delta_search_threads)      /* --threads=0 means autodetect */
2988                 delta_search_threads = online_cpus();
2989
2990 #ifdef NO_PTHREADS
2991         if (delta_search_threads != 1)
2992                 warning("no threads support, ignoring --threads");
2993 #endif
2994         if (!pack_to_stdout && !pack_size_limit)
2995                 pack_size_limit = pack_size_limit_cfg;
2996         if (pack_to_stdout && pack_size_limit)
2997                 die("--max-pack-size cannot be used to build a pack for transfer.");
2998         if (pack_size_limit && pack_size_limit < 1024*1024) {
2999                 warning("minimum pack size limit is 1 MiB");
3000                 pack_size_limit = 1024*1024;
3001         }
3002
3003         if (!pack_to_stdout && thin)
3004                 die("--thin cannot be used to build an indexable pack.");
3005
3006         if (keep_unreachable && unpack_unreachable)
3007                 die("--keep-unreachable and --unpack-unreachable are incompatible.");
3008         if (!rev_list_all || !rev_list_reflog || !rev_list_index)
3009                 unpack_unreachable_expiration = 0;
3010
3011         /*
3012          * "soft" reasons not to use bitmaps - for on-disk repack by default we want
3013          *
3014          * - to produce good pack (with bitmap index not-yet-packed objects are
3015          *   packed in suboptimal order).
3016          *
3017          * - to use more robust pack-generation codepath (avoiding possible
3018          *   bugs in bitmap code and possible bitmap index corruption).
3019          */
3020         if (!pack_to_stdout)
3021                 use_bitmap_index_default = 0;
3022
3023         if (use_bitmap_index < 0)
3024                 use_bitmap_index = use_bitmap_index_default;
3025
3026         /* "hard" reasons not to use bitmaps; these just won't work at all */
3027         if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow())
3028                 use_bitmap_index = 0;
3029
3030         if (pack_to_stdout || !rev_list_all)
3031                 write_bitmap_index = 0;
3032
3033         if (progress && all_progress_implied)
3034                 progress = 2;
3035
3036         prepare_packed_git();
3037         if (ignore_packed_keep) {
3038                 struct packed_git *p;
3039                 for (p = packed_git; p; p = p->next)
3040                         if (p->pack_local && p->pack_keep)
3041                                 break;
3042                 if (!p) /* no keep-able packs found */
3043                         ignore_packed_keep = 0;
3044         }
3045         if (local) {
3046                 /*
3047                  * unlike ignore_packed_keep above, we do not want to
3048                  * unset "local" based on looking at packs, as it
3049                  * also covers non-local objects
3050                  */
3051                 struct packed_git *p;
3052                 for (p = packed_git; p; p = p->next) {
3053                         if (!p->pack_local) {
3054                                 have_non_local_packs = 1;
3055                                 break;
3056                         }
3057                 }
3058         }
3059
3060         if (progress)
3061                 progress_state = start_progress(_("Counting objects"), 0);
3062         if (!use_internal_rev_list)
3063                 read_object_list_from_stdin();
3064         else {
3065                 get_object_list(rp.argc, rp.argv);
3066                 argv_array_clear(&rp);
3067         }
3068         cleanup_preferred_base();
3069         if (include_tag && nr_result)
3070                 for_each_ref(add_ref_tag, NULL);
3071         stop_progress(&progress_state);
3072
3073         if (non_empty && !nr_result)
3074                 return 0;
3075         if (nr_result)
3076                 prepare_pack(window, depth);
3077         write_pack_file();
3078         if (progress)
3079                 fprintf(stderr, "Total %"PRIu32" (delta %"PRIu32"),"
3080                         " reused %"PRIu32" (delta %"PRIu32")\n",
3081                         written, written_delta, reused, reused_delta);
3082         return 0;
3083 }