Merge branch 'jk/prune-with-bitmap-fix'
[git] / pack-bitmap.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "progress.h"
7 #include "list-objects.h"
8 #include "pack.h"
9 #include "pack-bitmap.h"
10 #include "pack-revindex.h"
11 #include "pack-objects.h"
12 #include "packfile.h"
13 #include "repository.h"
14 #include "object-store.h"
15 #include "list-objects-filter-options.h"
16 #include "config.h"
17
18 /*
19  * An entry on the bitmap index, representing the bitmap for a given
20  * commit.
21  */
22 struct stored_bitmap {
23         struct object_id oid;
24         struct ewah_bitmap *root;
25         struct stored_bitmap *xor;
26         int flags;
27 };
28
29 /*
30  * The active bitmap index for a repository. By design, repositories only have
31  * a single bitmap index available (the index for the biggest packfile in
32  * the repository), since bitmap indexes need full closure.
33  *
34  * If there is more than one bitmap index available (e.g. because of alternates),
35  * the active bitmap index is the largest one.
36  */
37 struct bitmap_index {
38         /* Packfile to which this bitmap index belongs to */
39         struct packed_git *pack;
40
41         /*
42          * Mark the first `reuse_objects` in the packfile as reused:
43          * they will be sent as-is without using them for repacking
44          * calculations
45          */
46         uint32_t reuse_objects;
47
48         /* mmapped buffer of the whole bitmap index */
49         unsigned char *map;
50         size_t map_size; /* size of the mmaped buffer */
51         size_t map_pos; /* current position when loading the index */
52
53         /*
54          * Type indexes.
55          *
56          * Each bitmap marks which objects in the packfile  are of the given
57          * type. This provides type information when yielding the objects from
58          * the packfile during a walk, which allows for better delta bases.
59          */
60         struct ewah_bitmap *commits;
61         struct ewah_bitmap *trees;
62         struct ewah_bitmap *blobs;
63         struct ewah_bitmap *tags;
64
65         /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
66         kh_oid_map_t *bitmaps;
67
68         /* Number of bitmapped commits */
69         uint32_t entry_count;
70
71         /* If not NULL, this is a name-hash cache pointing into map. */
72         uint32_t *hashes;
73
74         /*
75          * Extended index.
76          *
77          * When trying to perform bitmap operations with objects that are not
78          * packed in `pack`, these objects are added to this "fake index" and
79          * are assumed to appear at the end of the packfile for all operations
80          */
81         struct eindex {
82                 struct object **objects;
83                 uint32_t *hashes;
84                 uint32_t count, alloc;
85                 kh_oid_pos_t *positions;
86         } ext_index;
87
88         /* Bitmap result of the last performed walk */
89         struct bitmap *result;
90
91         /* "have" bitmap from the last performed walk */
92         struct bitmap *haves;
93
94         /* Version of the bitmap index */
95         unsigned int version;
96 };
97
98 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
99 {
100         struct ewah_bitmap *parent;
101         struct ewah_bitmap *composed;
102
103         if (st->xor == NULL)
104                 return st->root;
105
106         composed = ewah_pool_new();
107         parent = lookup_stored_bitmap(st->xor);
108         ewah_xor(st->root, parent, composed);
109
110         ewah_pool_free(st->root);
111         st->root = composed;
112         st->xor = NULL;
113
114         return composed;
115 }
116
117 /*
118  * Read a bitmap from the current read position on the mmaped
119  * index, and increase the read position accordingly
120  */
121 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
122 {
123         struct ewah_bitmap *b = ewah_pool_new();
124
125         ssize_t bitmap_size = ewah_read_mmap(b,
126                 index->map + index->map_pos,
127                 index->map_size - index->map_pos);
128
129         if (bitmap_size < 0) {
130                 error("Failed to load bitmap index (corrupted?)");
131                 ewah_pool_free(b);
132                 return NULL;
133         }
134
135         index->map_pos += bitmap_size;
136         return b;
137 }
138
139 static int load_bitmap_header(struct bitmap_index *index)
140 {
141         struct bitmap_disk_header *header = (void *)index->map;
142         size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
143
144         if (index->map_size < header_size + the_hash_algo->rawsz)
145                 return error("Corrupted bitmap index (too small)");
146
147         if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
148                 return error("Corrupted bitmap index file (wrong header)");
149
150         index->version = ntohs(header->version);
151         if (index->version != 1)
152                 return error("Unsupported version for bitmap index file (%d)", index->version);
153
154         /* Parse known bitmap format options */
155         {
156                 uint32_t flags = ntohs(header->options);
157                 size_t cache_size = st_mult(index->pack->num_objects, sizeof(uint32_t));
158                 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
159
160                 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
161                         return error("Unsupported options for bitmap index file "
162                                 "(Git requires BITMAP_OPT_FULL_DAG)");
163
164                 if (flags & BITMAP_OPT_HASH_CACHE) {
165                         if (cache_size > index_end - index->map - header_size)
166                                 return error("corrupted bitmap index file (too short to fit hash cache)");
167                         index->hashes = (void *)(index_end - cache_size);
168                         index_end -= cache_size;
169                 }
170         }
171
172         index->entry_count = ntohl(header->entry_count);
173         index->map_pos += header_size;
174         return 0;
175 }
176
177 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
178                                           struct ewah_bitmap *root,
179                                           const struct object_id *oid,
180                                           struct stored_bitmap *xor_with,
181                                           int flags)
182 {
183         struct stored_bitmap *stored;
184         khiter_t hash_pos;
185         int ret;
186
187         stored = xmalloc(sizeof(struct stored_bitmap));
188         stored->root = root;
189         stored->xor = xor_with;
190         stored->flags = flags;
191         oidcpy(&stored->oid, oid);
192
193         hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
194
195         /* a 0 return code means the insertion succeeded with no changes,
196          * because the SHA1 already existed on the map. this is bad, there
197          * shouldn't be duplicated commits in the index */
198         if (ret == 0) {
199                 error("Duplicate entry in bitmap index: %s", oid_to_hex(oid));
200                 return NULL;
201         }
202
203         kh_value(index->bitmaps, hash_pos) = stored;
204         return stored;
205 }
206
207 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
208 {
209         uint32_t result = get_be32(buffer + *pos);
210         (*pos) += sizeof(result);
211         return result;
212 }
213
214 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
215 {
216         return buffer[(*pos)++];
217 }
218
219 #define MAX_XOR_OFFSET 160
220
221 static int load_bitmap_entries_v1(struct bitmap_index *index)
222 {
223         uint32_t i;
224         struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
225
226         for (i = 0; i < index->entry_count; ++i) {
227                 int xor_offset, flags;
228                 struct ewah_bitmap *bitmap = NULL;
229                 struct stored_bitmap *xor_bitmap = NULL;
230                 uint32_t commit_idx_pos;
231                 struct object_id oid;
232
233                 if (index->map_size - index->map_pos < 6)
234                         return error("corrupt ewah bitmap: truncated header for entry %d", i);
235
236                 commit_idx_pos = read_be32(index->map, &index->map_pos);
237                 xor_offset = read_u8(index->map, &index->map_pos);
238                 flags = read_u8(index->map, &index->map_pos);
239
240                 if (nth_packed_object_id(&oid, index->pack, commit_idx_pos) < 0)
241                         return error("corrupt ewah bitmap: commit index %u out of range",
242                                      (unsigned)commit_idx_pos);
243
244                 bitmap = read_bitmap_1(index);
245                 if (!bitmap)
246                         return -1;
247
248                 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
249                         return error("Corrupted bitmap pack index");
250
251                 if (xor_offset > 0) {
252                         xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
253
254                         if (xor_bitmap == NULL)
255                                 return error("Invalid XOR offset in bitmap pack index");
256                 }
257
258                 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
259                         index, bitmap, &oid, xor_bitmap, flags);
260         }
261
262         return 0;
263 }
264
265 static char *pack_bitmap_filename(struct packed_git *p)
266 {
267         size_t len;
268
269         if (!strip_suffix(p->pack_name, ".pack", &len))
270                 BUG("pack_name does not end in .pack");
271         return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
272 }
273
274 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
275 {
276         int fd;
277         struct stat st;
278         char *idx_name;
279
280         if (open_pack_index(packfile))
281                 return -1;
282
283         idx_name = pack_bitmap_filename(packfile);
284         fd = git_open(idx_name);
285         free(idx_name);
286
287         if (fd < 0)
288                 return -1;
289
290         if (fstat(fd, &st)) {
291                 close(fd);
292                 return -1;
293         }
294
295         if (bitmap_git->pack) {
296                 warning("ignoring extra bitmap file: %s", packfile->pack_name);
297                 close(fd);
298                 return -1;
299         }
300
301         bitmap_git->pack = packfile;
302         bitmap_git->map_size = xsize_t(st.st_size);
303         bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
304         bitmap_git->map_pos = 0;
305         close(fd);
306
307         if (load_bitmap_header(bitmap_git) < 0) {
308                 munmap(bitmap_git->map, bitmap_git->map_size);
309                 bitmap_git->map = NULL;
310                 bitmap_git->map_size = 0;
311                 return -1;
312         }
313
314         return 0;
315 }
316
317 static int load_pack_bitmap(struct bitmap_index *bitmap_git)
318 {
319         assert(bitmap_git->map);
320
321         bitmap_git->bitmaps = kh_init_oid_map();
322         bitmap_git->ext_index.positions = kh_init_oid_pos();
323         if (load_pack_revindex(bitmap_git->pack))
324                 goto failed;
325
326         if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
327                 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
328                 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
329                 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
330                 goto failed;
331
332         if (load_bitmap_entries_v1(bitmap_git) < 0)
333                 goto failed;
334
335         return 0;
336
337 failed:
338         munmap(bitmap_git->map, bitmap_git->map_size);
339         bitmap_git->map = NULL;
340         bitmap_git->map_size = 0;
341
342         kh_destroy_oid_map(bitmap_git->bitmaps);
343         bitmap_git->bitmaps = NULL;
344
345         kh_destroy_oid_pos(bitmap_git->ext_index.positions);
346         bitmap_git->ext_index.positions = NULL;
347
348         return -1;
349 }
350
351 static int open_pack_bitmap(struct repository *r,
352                             struct bitmap_index *bitmap_git)
353 {
354         struct packed_git *p;
355         int ret = -1;
356
357         assert(!bitmap_git->map);
358
359         for (p = get_all_packs(r); p; p = p->next) {
360                 if (open_pack_bitmap_1(bitmap_git, p) == 0)
361                         ret = 0;
362         }
363
364         return ret;
365 }
366
367 struct bitmap_index *prepare_bitmap_git(struct repository *r)
368 {
369         struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
370
371         if (!open_pack_bitmap(r, bitmap_git) && !load_pack_bitmap(bitmap_git))
372                 return bitmap_git;
373
374         free_bitmap_index(bitmap_git);
375         return NULL;
376 }
377
378 struct include_data {
379         struct bitmap_index *bitmap_git;
380         struct bitmap *base;
381         struct bitmap *seen;
382 };
383
384 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
385                                       struct commit *commit)
386 {
387         khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
388                                            commit->object.oid);
389         if (hash_pos >= kh_end(bitmap_git->bitmaps))
390                 return NULL;
391         return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
392 }
393
394 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
395                                            const struct object_id *oid)
396 {
397         kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
398         khiter_t pos = kh_get_oid_pos(positions, *oid);
399
400         if (pos < kh_end(positions)) {
401                 int bitmap_pos = kh_value(positions, pos);
402                 return bitmap_pos + bitmap_git->pack->num_objects;
403         }
404
405         return -1;
406 }
407
408 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
409                                            const struct object_id *oid)
410 {
411         uint32_t pos;
412         off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
413         if (!offset)
414                 return -1;
415
416         if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
417                 return -1;
418         return pos;
419 }
420
421 static int bitmap_position(struct bitmap_index *bitmap_git,
422                            const struct object_id *oid)
423 {
424         int pos = bitmap_position_packfile(bitmap_git, oid);
425         return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
426 }
427
428 static int ext_index_add_object(struct bitmap_index *bitmap_git,
429                                 struct object *object, const char *name)
430 {
431         struct eindex *eindex = &bitmap_git->ext_index;
432
433         khiter_t hash_pos;
434         int hash_ret;
435         int bitmap_pos;
436
437         hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
438         if (hash_ret > 0) {
439                 if (eindex->count >= eindex->alloc) {
440                         eindex->alloc = (eindex->alloc + 16) * 3 / 2;
441                         REALLOC_ARRAY(eindex->objects, eindex->alloc);
442                         REALLOC_ARRAY(eindex->hashes, eindex->alloc);
443                 }
444
445                 bitmap_pos = eindex->count;
446                 eindex->objects[eindex->count] = object;
447                 eindex->hashes[eindex->count] = pack_name_hash(name);
448                 kh_value(eindex->positions, hash_pos) = bitmap_pos;
449                 eindex->count++;
450         } else {
451                 bitmap_pos = kh_value(eindex->positions, hash_pos);
452         }
453
454         return bitmap_pos + bitmap_git->pack->num_objects;
455 }
456
457 struct bitmap_show_data {
458         struct bitmap_index *bitmap_git;
459         struct bitmap *base;
460 };
461
462 static void show_object(struct object *object, const char *name, void *data_)
463 {
464         struct bitmap_show_data *data = data_;
465         int bitmap_pos;
466
467         bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
468
469         if (bitmap_pos < 0)
470                 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
471                                                   name);
472
473         bitmap_set(data->base, bitmap_pos);
474 }
475
476 static void show_commit(struct commit *commit, void *data)
477 {
478 }
479
480 static int add_to_include_set(struct bitmap_index *bitmap_git,
481                               struct include_data *data,
482                               struct commit *commit,
483                               int bitmap_pos)
484 {
485         struct ewah_bitmap *partial;
486
487         if (data->seen && bitmap_get(data->seen, bitmap_pos))
488                 return 0;
489
490         if (bitmap_get(data->base, bitmap_pos))
491                 return 0;
492
493         partial = bitmap_for_commit(bitmap_git, commit);
494         if (partial) {
495                 bitmap_or_ewah(data->base, partial);
496                 return 0;
497         }
498
499         bitmap_set(data->base, bitmap_pos);
500         return 1;
501 }
502
503 static int should_include(struct commit *commit, void *_data)
504 {
505         struct include_data *data = _data;
506         int bitmap_pos;
507
508         bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
509         if (bitmap_pos < 0)
510                 bitmap_pos = ext_index_add_object(data->bitmap_git,
511                                                   (struct object *)commit,
512                                                   NULL);
513
514         if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
515                 struct commit_list *parent = commit->parents;
516
517                 while (parent) {
518                         parent->item->object.flags |= SEEN;
519                         parent = parent->next;
520                 }
521
522                 return 0;
523         }
524
525         return 1;
526 }
527
528 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
529                                 struct bitmap **base,
530                                 struct commit *commit)
531 {
532         struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
533
534         if (!or_with)
535                 return 0;
536
537         if (*base == NULL)
538                 *base = ewah_to_bitmap(or_with);
539         else
540                 bitmap_or_ewah(*base, or_with);
541
542         return 1;
543 }
544
545 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
546                                    struct rev_info *revs,
547                                    struct object_list *roots,
548                                    struct bitmap *seen,
549                                    struct list_objects_filter_options *filter)
550 {
551         struct bitmap *base = NULL;
552         int needs_walk = 0;
553
554         struct object_list *not_mapped = NULL;
555
556         /*
557          * Go through all the roots for the walk. The ones that have bitmaps
558          * on the bitmap index will be `or`ed together to form an initial
559          * global reachability analysis.
560          *
561          * The ones without bitmaps in the index will be stored in the
562          * `not_mapped_list` for further processing.
563          */
564         while (roots) {
565                 struct object *object = roots->item;
566                 roots = roots->next;
567
568                 if (object->type == OBJ_COMMIT &&
569                     add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
570                         object->flags |= SEEN;
571                         continue;
572                 }
573
574                 object_list_insert(object, &not_mapped);
575         }
576
577         /*
578          * Best case scenario: We found bitmaps for all the roots,
579          * so the resulting `or` bitmap has the full reachability analysis
580          */
581         if (not_mapped == NULL)
582                 return base;
583
584         roots = not_mapped;
585
586         /*
587          * Let's iterate through all the roots that don't have bitmaps to
588          * check if we can determine them to be reachable from the existing
589          * global bitmap.
590          *
591          * If we cannot find them in the existing global bitmap, we'll need
592          * to push them to an actual walk and run it until we can confirm
593          * they are reachable
594          */
595         while (roots) {
596                 struct object *object = roots->item;
597                 int pos;
598
599                 roots = roots->next;
600                 pos = bitmap_position(bitmap_git, &object->oid);
601
602                 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
603                         object->flags &= ~UNINTERESTING;
604                         add_pending_object(revs, object, "");
605                         needs_walk = 1;
606                 } else {
607                         object->flags |= SEEN;
608                 }
609         }
610
611         if (needs_walk) {
612                 struct include_data incdata;
613                 struct bitmap_show_data show_data;
614
615                 if (base == NULL)
616                         base = bitmap_new();
617
618                 incdata.bitmap_git = bitmap_git;
619                 incdata.base = base;
620                 incdata.seen = seen;
621
622                 revs->include_check = should_include;
623                 revs->include_check_data = &incdata;
624
625                 if (prepare_revision_walk(revs))
626                         die("revision walk setup failed");
627
628                 show_data.bitmap_git = bitmap_git;
629                 show_data.base = base;
630
631                 traverse_commit_list_filtered(filter, revs,
632                                               show_commit, show_object,
633                                               &show_data, NULL);
634
635                 revs->include_check = NULL;
636                 revs->include_check_data = NULL;
637         }
638
639         return base;
640 }
641
642 static void show_extended_objects(struct bitmap_index *bitmap_git,
643                                   struct rev_info *revs,
644                                   show_reachable_fn show_reach)
645 {
646         struct bitmap *objects = bitmap_git->result;
647         struct eindex *eindex = &bitmap_git->ext_index;
648         uint32_t i;
649
650         for (i = 0; i < eindex->count; ++i) {
651                 struct object *obj;
652
653                 if (!bitmap_get(objects, bitmap_git->pack->num_objects + i))
654                         continue;
655
656                 obj = eindex->objects[i];
657                 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
658                     (obj->type == OBJ_TREE && !revs->tree_objects) ||
659                     (obj->type == OBJ_TAG && !revs->tag_objects))
660                         continue;
661
662                 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
663         }
664 }
665
666 static void init_type_iterator(struct ewah_iterator *it,
667                                struct bitmap_index *bitmap_git,
668                                enum object_type type)
669 {
670         switch (type) {
671         case OBJ_COMMIT:
672                 ewah_iterator_init(it, bitmap_git->commits);
673                 break;
674
675         case OBJ_TREE:
676                 ewah_iterator_init(it, bitmap_git->trees);
677                 break;
678
679         case OBJ_BLOB:
680                 ewah_iterator_init(it, bitmap_git->blobs);
681                 break;
682
683         case OBJ_TAG:
684                 ewah_iterator_init(it, bitmap_git->tags);
685                 break;
686
687         default:
688                 BUG("object type %d not stored by bitmap type index", type);
689                 break;
690         }
691 }
692
693 static void show_objects_for_type(
694         struct bitmap_index *bitmap_git,
695         enum object_type object_type,
696         show_reachable_fn show_reach)
697 {
698         size_t i = 0;
699         uint32_t offset;
700
701         struct ewah_iterator it;
702         eword_t filter;
703
704         struct bitmap *objects = bitmap_git->result;
705
706         init_type_iterator(&it, bitmap_git, object_type);
707
708         for (i = 0; i < objects->word_alloc &&
709                         ewah_iterator_next(&filter, &it); i++) {
710                 eword_t word = objects->words[i] & filter;
711                 size_t pos = (i * BITS_IN_EWORD);
712
713                 if (!word)
714                         continue;
715
716                 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
717                         struct object_id oid;
718                         uint32_t hash = 0, index_pos;
719                         off_t ofs;
720
721                         if ((word >> offset) == 0)
722                                 break;
723
724                         offset += ewah_bit_ctz64(word >> offset);
725
726                         index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
727                         ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
728                         nth_packed_object_id(&oid, bitmap_git->pack, index_pos);
729
730                         if (bitmap_git->hashes)
731                                 hash = get_be32(bitmap_git->hashes + index_pos);
732
733                         show_reach(&oid, object_type, 0, hash, bitmap_git->pack, ofs);
734                 }
735         }
736 }
737
738 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
739                              struct object_list *roots)
740 {
741         while (roots) {
742                 struct object *object = roots->item;
743                 roots = roots->next;
744
745                 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
746                         return 1;
747         }
748
749         return 0;
750 }
751
752 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
753                                        struct object_list *tip_objects,
754                                        enum object_type type)
755 {
756         struct bitmap *result = bitmap_new();
757         struct object_list *p;
758
759         for (p = tip_objects; p; p = p->next) {
760                 int pos;
761
762                 if (p->item->type != type)
763                         continue;
764
765                 pos = bitmap_position(bitmap_git, &p->item->oid);
766                 if (pos < 0)
767                         continue;
768
769                 bitmap_set(result, pos);
770         }
771
772         return result;
773 }
774
775 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
776                                        struct object_list *tip_objects,
777                                        struct bitmap *to_filter,
778                                        enum object_type type)
779 {
780         struct eindex *eindex = &bitmap_git->ext_index;
781         struct bitmap *tips;
782         struct ewah_iterator it;
783         eword_t mask;
784         uint32_t i;
785
786         if (type != OBJ_BLOB && type != OBJ_TREE)
787                 BUG("filter_bitmap_exclude_type: unsupported type '%d'", type);
788
789         /*
790          * The non-bitmap version of this filter never removes
791          * objects which the other side specifically asked for,
792          * so we must match that behavior.
793          */
794         tips = find_tip_objects(bitmap_git, tip_objects, type);
795
796         /*
797          * We can use the blob type-bitmap to work in whole words
798          * for the objects that are actually in the bitmapped packfile.
799          */
800         for (i = 0, init_type_iterator(&it, bitmap_git, type);
801              i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
802              i++) {
803                 if (i < tips->word_alloc)
804                         mask &= ~tips->words[i];
805                 to_filter->words[i] &= ~mask;
806         }
807
808         /*
809          * Clear any blobs that weren't in the packfile (and so would not have
810          * been caught by the loop above. We'll have to check them
811          * individually.
812          */
813         for (i = 0; i < eindex->count; i++) {
814                 uint32_t pos = i + bitmap_git->pack->num_objects;
815                 if (eindex->objects[i]->type == type &&
816                     bitmap_get(to_filter, pos) &&
817                     !bitmap_get(tips, pos))
818                         bitmap_unset(to_filter, pos);
819         }
820
821         bitmap_free(tips);
822 }
823
824 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
825                                     struct object_list *tip_objects,
826                                     struct bitmap *to_filter)
827 {
828         filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
829                                    OBJ_BLOB);
830 }
831
832 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
833                                      uint32_t pos)
834 {
835         struct packed_git *pack = bitmap_git->pack;
836         unsigned long size;
837         struct object_info oi = OBJECT_INFO_INIT;
838
839         oi.sizep = &size;
840
841         if (pos < pack->num_objects) {
842                 off_t ofs = pack_pos_to_offset(pack, pos);
843                 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
844                         struct object_id oid;
845                         nth_packed_object_id(&oid, pack,
846                                              pack_pos_to_index(pack, pos));
847                         die(_("unable to get size of %s"), oid_to_hex(&oid));
848                 }
849         } else {
850                 struct eindex *eindex = &bitmap_git->ext_index;
851                 struct object *obj = eindex->objects[pos - pack->num_objects];
852                 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
853                         die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
854         }
855
856         return size;
857 }
858
859 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
860                                      struct object_list *tip_objects,
861                                      struct bitmap *to_filter,
862                                      unsigned long limit)
863 {
864         struct eindex *eindex = &bitmap_git->ext_index;
865         struct bitmap *tips;
866         struct ewah_iterator it;
867         eword_t mask;
868         uint32_t i;
869
870         tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
871
872         for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
873              i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
874              i++) {
875                 eword_t word = to_filter->words[i] & mask;
876                 unsigned offset;
877
878                 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
879                         uint32_t pos;
880
881                         if ((word >> offset) == 0)
882                                 break;
883                         offset += ewah_bit_ctz64(word >> offset);
884                         pos = i * BITS_IN_EWORD + offset;
885
886                         if (!bitmap_get(tips, pos) &&
887                             get_size_by_pos(bitmap_git, pos) >= limit)
888                                 bitmap_unset(to_filter, pos);
889                 }
890         }
891
892         for (i = 0; i < eindex->count; i++) {
893                 uint32_t pos = i + bitmap_git->pack->num_objects;
894                 if (eindex->objects[i]->type == OBJ_BLOB &&
895                     bitmap_get(to_filter, pos) &&
896                     !bitmap_get(tips, pos) &&
897                     get_size_by_pos(bitmap_git, pos) >= limit)
898                         bitmap_unset(to_filter, pos);
899         }
900
901         bitmap_free(tips);
902 }
903
904 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
905                                      struct object_list *tip_objects,
906                                      struct bitmap *to_filter,
907                                      unsigned long limit)
908 {
909         if (limit)
910                 BUG("filter_bitmap_tree_depth given non-zero limit");
911
912         filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
913                                    OBJ_TREE);
914         filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
915                                    OBJ_BLOB);
916 }
917
918 static int filter_bitmap(struct bitmap_index *bitmap_git,
919                          struct object_list *tip_objects,
920                          struct bitmap *to_filter,
921                          struct list_objects_filter_options *filter)
922 {
923         if (!filter || filter->choice == LOFC_DISABLED)
924                 return 0;
925
926         if (filter->choice == LOFC_BLOB_NONE) {
927                 if (bitmap_git)
928                         filter_bitmap_blob_none(bitmap_git, tip_objects,
929                                                 to_filter);
930                 return 0;
931         }
932
933         if (filter->choice == LOFC_BLOB_LIMIT) {
934                 if (bitmap_git)
935                         filter_bitmap_blob_limit(bitmap_git, tip_objects,
936                                                  to_filter,
937                                                  filter->blob_limit_value);
938                 return 0;
939         }
940
941         if (filter->choice == LOFC_TREE_DEPTH &&
942             filter->tree_exclude_depth == 0) {
943                 if (bitmap_git)
944                         filter_bitmap_tree_depth(bitmap_git, tip_objects,
945                                                  to_filter,
946                                                  filter->tree_exclude_depth);
947                 return 0;
948         }
949
950         /* filter choice not handled */
951         return -1;
952 }
953
954 static int can_filter_bitmap(struct list_objects_filter_options *filter)
955 {
956         return !filter_bitmap(NULL, NULL, NULL, filter);
957 }
958
959 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
960                                          struct list_objects_filter_options *filter)
961 {
962         unsigned int i;
963
964         struct object_list *wants = NULL;
965         struct object_list *haves = NULL;
966
967         struct bitmap *wants_bitmap = NULL;
968         struct bitmap *haves_bitmap = NULL;
969
970         struct bitmap_index *bitmap_git;
971
972         /*
973          * We can't do pathspec limiting with bitmaps, because we don't know
974          * which commits are associated with which object changes (let alone
975          * even which objects are associated with which paths).
976          */
977         if (revs->prune)
978                 return NULL;
979
980         if (!can_filter_bitmap(filter))
981                 return NULL;
982
983         /* try to open a bitmapped pack, but don't parse it yet
984          * because we may not need to use it */
985         CALLOC_ARRAY(bitmap_git, 1);
986         if (open_pack_bitmap(revs->repo, bitmap_git) < 0)
987                 goto cleanup;
988
989         for (i = 0; i < revs->pending.nr; ++i) {
990                 struct object *object = revs->pending.objects[i].item;
991
992                 if (object->type == OBJ_NONE)
993                         parse_object_or_die(&object->oid, NULL);
994
995                 while (object->type == OBJ_TAG) {
996                         struct tag *tag = (struct tag *) object;
997
998                         if (object->flags & UNINTERESTING)
999                                 object_list_insert(object, &haves);
1000                         else
1001                                 object_list_insert(object, &wants);
1002
1003                         object = parse_object_or_die(get_tagged_oid(tag), NULL);
1004                         object->flags |= (tag->object.flags & UNINTERESTING);
1005                 }
1006
1007                 if (object->flags & UNINTERESTING)
1008                         object_list_insert(object, &haves);
1009                 else
1010                         object_list_insert(object, &wants);
1011         }
1012
1013         /*
1014          * if we have a HAVES list, but none of those haves is contained
1015          * in the packfile that has a bitmap, we don't have anything to
1016          * optimize here
1017          */
1018         if (haves && !in_bitmapped_pack(bitmap_git, haves))
1019                 goto cleanup;
1020
1021         /* if we don't want anything, we're done here */
1022         if (!wants)
1023                 goto cleanup;
1024
1025         /*
1026          * now we're going to use bitmaps, so load the actual bitmap entries
1027          * from disk. this is the point of no return; after this the rev_list
1028          * becomes invalidated and we must perform the revwalk through bitmaps
1029          */
1030         if (load_pack_bitmap(bitmap_git) < 0)
1031                 goto cleanup;
1032
1033         object_array_clear(&revs->pending);
1034
1035         if (haves) {
1036                 revs->ignore_missing_links = 1;
1037                 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
1038                                             filter);
1039                 reset_revision_walk();
1040                 revs->ignore_missing_links = 0;
1041
1042                 if (haves_bitmap == NULL)
1043                         BUG("failed to perform bitmap walk");
1044         }
1045
1046         wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
1047                                     filter);
1048
1049         if (!wants_bitmap)
1050                 BUG("failed to perform bitmap walk");
1051
1052         if (haves_bitmap)
1053                 bitmap_and_not(wants_bitmap, haves_bitmap);
1054
1055         filter_bitmap(bitmap_git, wants, wants_bitmap, filter);
1056
1057         bitmap_git->result = wants_bitmap;
1058         bitmap_git->haves = haves_bitmap;
1059
1060         object_list_free(&wants);
1061         object_list_free(&haves);
1062
1063         return bitmap_git;
1064
1065 cleanup:
1066         free_bitmap_index(bitmap_git);
1067         object_list_free(&wants);
1068         object_list_free(&haves);
1069         return NULL;
1070 }
1071
1072 static void try_partial_reuse(struct bitmap_index *bitmap_git,
1073                               size_t pos,
1074                               struct bitmap *reuse,
1075                               struct pack_window **w_curs)
1076 {
1077         off_t offset, header;
1078         enum object_type type;
1079         unsigned long size;
1080
1081         if (pos >= bitmap_git->pack->num_objects)
1082                 return; /* not actually in the pack */
1083
1084         offset = header = pack_pos_to_offset(bitmap_git->pack, pos);
1085         type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
1086         if (type < 0)
1087                 return; /* broken packfile, punt */
1088
1089         if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1090                 off_t base_offset;
1091                 uint32_t base_pos;
1092
1093                 /*
1094                  * Find the position of the base object so we can look it up
1095                  * in our bitmaps. If we can't come up with an offset, or if
1096                  * that offset is not in the revidx, the pack is corrupt.
1097                  * There's nothing we can do, so just punt on this object,
1098                  * and the normal slow path will complain about it in
1099                  * more detail.
1100                  */
1101                 base_offset = get_delta_base(bitmap_git->pack, w_curs,
1102                                              &offset, type, header);
1103                 if (!base_offset)
1104                         return;
1105                 if (offset_to_pack_pos(bitmap_git->pack, base_offset, &base_pos) < 0)
1106                         return;
1107
1108                 /*
1109                  * We assume delta dependencies always point backwards. This
1110                  * lets us do a single pass, and is basically always true
1111                  * due to the way OFS_DELTAs work. You would not typically
1112                  * find REF_DELTA in a bitmapped pack, since we only bitmap
1113                  * packs we write fresh, and OFS_DELTA is the default). But
1114                  * let's double check to make sure the pack wasn't written with
1115                  * odd parameters.
1116                  */
1117                 if (base_pos >= pos)
1118                         return;
1119
1120                 /*
1121                  * And finally, if we're not sending the base as part of our
1122                  * reuse chunk, then don't send this object either. The base
1123                  * would come after us, along with other objects not
1124                  * necessarily in the pack, which means we'd need to convert
1125                  * to REF_DELTA on the fly. Better to just let the normal
1126                  * object_entry code path handle it.
1127                  */
1128                 if (!bitmap_get(reuse, base_pos))
1129                         return;
1130         }
1131
1132         /*
1133          * If we got here, then the object is OK to reuse. Mark it.
1134          */
1135         bitmap_set(reuse, pos);
1136 }
1137
1138 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1139                                        struct packed_git **packfile_out,
1140                                        uint32_t *entries,
1141                                        struct bitmap **reuse_out)
1142 {
1143         struct bitmap *result = bitmap_git->result;
1144         struct bitmap *reuse;
1145         struct pack_window *w_curs = NULL;
1146         size_t i = 0;
1147         uint32_t offset;
1148
1149         assert(result);
1150
1151         while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1152                 i++;
1153
1154         /* Don't mark objects not in the packfile */
1155         if (i > bitmap_git->pack->num_objects / BITS_IN_EWORD)
1156                 i = bitmap_git->pack->num_objects / BITS_IN_EWORD;
1157
1158         reuse = bitmap_word_alloc(i);
1159         memset(reuse->words, 0xFF, i * sizeof(eword_t));
1160
1161         for (; i < result->word_alloc; ++i) {
1162                 eword_t word = result->words[i];
1163                 size_t pos = (i * BITS_IN_EWORD);
1164
1165                 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1166                         if ((word >> offset) == 0)
1167                                 break;
1168
1169                         offset += ewah_bit_ctz64(word >> offset);
1170                         try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs);
1171                 }
1172         }
1173
1174         unuse_pack(&w_curs);
1175
1176         *entries = bitmap_popcount(reuse);
1177         if (!*entries) {
1178                 bitmap_free(reuse);
1179                 return -1;
1180         }
1181
1182         /*
1183          * Drop any reused objects from the result, since they will not
1184          * need to be handled separately.
1185          */
1186         bitmap_and_not(result, reuse);
1187         *packfile_out = bitmap_git->pack;
1188         *reuse_out = reuse;
1189         return 0;
1190 }
1191
1192 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1193                          struct bitmap *bitmap, const struct object_id *oid)
1194 {
1195         int idx;
1196
1197         if (!bitmap)
1198                 return 0;
1199
1200         idx = bitmap_position(bitmap_git, oid);
1201         return idx >= 0 && bitmap_get(bitmap, idx);
1202 }
1203
1204 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1205                                  struct rev_info *revs,
1206                                  show_reachable_fn show_reachable)
1207 {
1208         assert(bitmap_git->result);
1209
1210         show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1211         if (revs->tree_objects)
1212                 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1213         if (revs->blob_objects)
1214                 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1215         if (revs->tag_objects)
1216                 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1217
1218         show_extended_objects(bitmap_git, revs, show_reachable);
1219 }
1220
1221 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1222                                   enum object_type type)
1223 {
1224         struct bitmap *objects = bitmap_git->result;
1225         struct eindex *eindex = &bitmap_git->ext_index;
1226
1227         uint32_t i = 0, count = 0;
1228         struct ewah_iterator it;
1229         eword_t filter;
1230
1231         init_type_iterator(&it, bitmap_git, type);
1232
1233         while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1234                 eword_t word = objects->words[i++] & filter;
1235                 count += ewah_bit_popcount64(word);
1236         }
1237
1238         for (i = 0; i < eindex->count; ++i) {
1239                 if (eindex->objects[i]->type == type &&
1240                         bitmap_get(objects, bitmap_git->pack->num_objects + i))
1241                         count++;
1242         }
1243
1244         return count;
1245 }
1246
1247 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1248                               uint32_t *commits, uint32_t *trees,
1249                               uint32_t *blobs, uint32_t *tags)
1250 {
1251         assert(bitmap_git->result);
1252
1253         if (commits)
1254                 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1255
1256         if (trees)
1257                 *trees = count_object_type(bitmap_git, OBJ_TREE);
1258
1259         if (blobs)
1260                 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1261
1262         if (tags)
1263                 *tags = count_object_type(bitmap_git, OBJ_TAG);
1264 }
1265
1266 struct bitmap_test_data {
1267         struct bitmap_index *bitmap_git;
1268         struct bitmap *base;
1269         struct progress *prg;
1270         size_t seen;
1271 };
1272
1273 static void test_show_object(struct object *object, const char *name,
1274                              void *data)
1275 {
1276         struct bitmap_test_data *tdata = data;
1277         int bitmap_pos;
1278
1279         bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1280         if (bitmap_pos < 0)
1281                 die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
1282
1283         bitmap_set(tdata->base, bitmap_pos);
1284         display_progress(tdata->prg, ++tdata->seen);
1285 }
1286
1287 static void test_show_commit(struct commit *commit, void *data)
1288 {
1289         struct bitmap_test_data *tdata = data;
1290         int bitmap_pos;
1291
1292         bitmap_pos = bitmap_position(tdata->bitmap_git,
1293                                      &commit->object.oid);
1294         if (bitmap_pos < 0)
1295                 die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
1296
1297         bitmap_set(tdata->base, bitmap_pos);
1298         display_progress(tdata->prg, ++tdata->seen);
1299 }
1300
1301 void test_bitmap_walk(struct rev_info *revs)
1302 {
1303         struct object *root;
1304         struct bitmap *result = NULL;
1305         size_t result_popcnt;
1306         struct bitmap_test_data tdata;
1307         struct bitmap_index *bitmap_git;
1308         struct ewah_bitmap *bm;
1309
1310         if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1311                 die("failed to load bitmap indexes");
1312
1313         if (revs->pending.nr != 1)
1314                 die("you must specify exactly one commit to test");
1315
1316         fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
1317                 bitmap_git->version, bitmap_git->entry_count);
1318
1319         root = revs->pending.objects[0].item;
1320         bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
1321
1322         if (bm) {
1323                 fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
1324                         oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
1325
1326                 result = ewah_to_bitmap(bm);
1327         }
1328
1329         if (result == NULL)
1330                 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
1331
1332         revs->tag_objects = 1;
1333         revs->tree_objects = 1;
1334         revs->blob_objects = 1;
1335
1336         result_popcnt = bitmap_popcount(result);
1337
1338         if (prepare_revision_walk(revs))
1339                 die("revision walk setup failed");
1340
1341         tdata.bitmap_git = bitmap_git;
1342         tdata.base = bitmap_new();
1343         tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1344         tdata.seen = 0;
1345
1346         traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1347
1348         stop_progress(&tdata.prg);
1349
1350         if (bitmap_equals(result, tdata.base))
1351                 fprintf(stderr, "OK!\n");
1352         else
1353                 die("mismatch in bitmap results");
1354
1355         free_bitmap_index(bitmap_git);
1356 }
1357
1358 int test_bitmap_commits(struct repository *r)
1359 {
1360         struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1361         struct object_id oid;
1362         MAYBE_UNUSED void *value;
1363
1364         if (!bitmap_git)
1365                 die("failed to load bitmap indexes");
1366
1367         kh_foreach(bitmap_git->bitmaps, oid, value, {
1368                 printf("%s\n", oid_to_hex(&oid));
1369         });
1370
1371         free_bitmap_index(bitmap_git);
1372
1373         return 0;
1374 }
1375
1376 int rebuild_bitmap(const uint32_t *reposition,
1377                    struct ewah_bitmap *source,
1378                    struct bitmap *dest)
1379 {
1380         uint32_t pos = 0;
1381         struct ewah_iterator it;
1382         eword_t word;
1383
1384         ewah_iterator_init(&it, source);
1385
1386         while (ewah_iterator_next(&word, &it)) {
1387                 uint32_t offset, bit_pos;
1388
1389                 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1390                         if ((word >> offset) == 0)
1391                                 break;
1392
1393                         offset += ewah_bit_ctz64(word >> offset);
1394
1395                         bit_pos = reposition[pos + offset];
1396                         if (bit_pos > 0)
1397                                 bitmap_set(dest, bit_pos - 1);
1398                         else /* can't reuse, we don't have the object */
1399                                 return -1;
1400                 }
1401
1402                 pos += BITS_IN_EWORD;
1403         }
1404         return 0;
1405 }
1406
1407 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1408                                 struct packing_data *mapping)
1409 {
1410         uint32_t i, num_objects;
1411         uint32_t *reposition;
1412
1413         num_objects = bitmap_git->pack->num_objects;
1414         CALLOC_ARRAY(reposition, num_objects);
1415
1416         for (i = 0; i < num_objects; ++i) {
1417                 struct object_id oid;
1418                 struct object_entry *oe;
1419
1420                 nth_packed_object_id(&oid, bitmap_git->pack,
1421                                      pack_pos_to_index(bitmap_git->pack, i));
1422                 oe = packlist_find(mapping, &oid);
1423
1424                 if (oe)
1425                         reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
1426         }
1427
1428         return reposition;
1429 }
1430
1431 void free_bitmap_index(struct bitmap_index *b)
1432 {
1433         if (!b)
1434                 return;
1435
1436         if (b->map)
1437                 munmap(b->map, b->map_size);
1438         ewah_pool_free(b->commits);
1439         ewah_pool_free(b->trees);
1440         ewah_pool_free(b->blobs);
1441         ewah_pool_free(b->tags);
1442         kh_destroy_oid_map(b->bitmaps);
1443         free(b->ext_index.objects);
1444         free(b->ext_index.hashes);
1445         bitmap_free(b->result);
1446         bitmap_free(b->haves);
1447         free(b);
1448 }
1449
1450 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1451                                     const struct object_id *oid)
1452 {
1453         return bitmap_git &&
1454                 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
1455 }
1456
1457 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1458                                      enum object_type object_type)
1459 {
1460         struct bitmap *result = bitmap_git->result;
1461         struct packed_git *pack = bitmap_git->pack;
1462         off_t total = 0;
1463         struct ewah_iterator it;
1464         eword_t filter;
1465         size_t i;
1466
1467         init_type_iterator(&it, bitmap_git, object_type);
1468         for (i = 0; i < result->word_alloc &&
1469                         ewah_iterator_next(&filter, &it); i++) {
1470                 eword_t word = result->words[i] & filter;
1471                 size_t base = (i * BITS_IN_EWORD);
1472                 unsigned offset;
1473
1474                 if (!word)
1475                         continue;
1476
1477                 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1478                         size_t pos;
1479
1480                         if ((word >> offset) == 0)
1481                                 break;
1482
1483                         offset += ewah_bit_ctz64(word >> offset);
1484                         pos = base + offset;
1485                         total += pack_pos_to_offset(pack, pos + 1) -
1486                                  pack_pos_to_offset(pack, pos);
1487                 }
1488         }
1489
1490         return total;
1491 }
1492
1493 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1494 {
1495         struct bitmap *result = bitmap_git->result;
1496         struct packed_git *pack = bitmap_git->pack;
1497         struct eindex *eindex = &bitmap_git->ext_index;
1498         off_t total = 0;
1499         struct object_info oi = OBJECT_INFO_INIT;
1500         off_t object_size;
1501         size_t i;
1502
1503         oi.disk_sizep = &object_size;
1504
1505         for (i = 0; i < eindex->count; i++) {
1506                 struct object *obj = eindex->objects[i];
1507
1508                 if (!bitmap_get(result, pack->num_objects + i))
1509                         continue;
1510
1511                 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1512                         die(_("unable to get disk usage of %s"),
1513                             oid_to_hex(&obj->oid));
1514
1515                 total += object_size;
1516         }
1517         return total;
1518 }
1519
1520 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
1521                                  struct rev_info *revs)
1522 {
1523         off_t total = 0;
1524
1525         total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
1526         if (revs->tree_objects)
1527                 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
1528         if (revs->blob_objects)
1529                 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
1530         if (revs->tag_objects)
1531                 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
1532
1533         total += get_disk_usage_for_extended(bitmap_git);
1534
1535         return total;
1536 }
1537
1538 const struct string_list *bitmap_preferred_tips(struct repository *r)
1539 {
1540         return repo_config_get_value_multi(r, "pack.preferbitmaptips");
1541 }