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