pack-bitmap.c: introduce 'bitmap_num_objects()'
[git/debian.git] / pack-bitmap.c
blob65356f965763da20747975400119405c2d0188a5
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"
19 * An entry on the bitmap index, representing the bitmap for a given
20 * commit.
22 struct stored_bitmap {
23 struct object_id oid;
24 struct ewah_bitmap *root;
25 struct stored_bitmap *xor;
26 int flags;
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.
34 * If there is more than one bitmap index available (e.g. because of alternates),
35 * the active bitmap index is the largest one.
37 struct bitmap_index {
38 /* Packfile to which this bitmap index belongs to */
39 struct packed_git *pack;
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
46 uint32_t reuse_objects;
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 */
54 * Type indexes.
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.
60 struct ewah_bitmap *commits;
61 struct ewah_bitmap *trees;
62 struct ewah_bitmap *blobs;
63 struct ewah_bitmap *tags;
65 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
66 kh_oid_map_t *bitmaps;
68 /* Number of bitmapped commits */
69 uint32_t entry_count;
71 /* If not NULL, this is a name-hash cache pointing into map. */
72 uint32_t *hashes;
75 * Extended index.
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
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;
88 /* Bitmap result of the last performed walk */
89 struct bitmap *result;
91 /* "have" bitmap from the last performed walk */
92 struct bitmap *haves;
94 /* Version of the bitmap index */
95 unsigned int version;
98 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
100 struct ewah_bitmap *parent;
101 struct ewah_bitmap *composed;
103 if (st->xor == NULL)
104 return st->root;
106 composed = ewah_pool_new();
107 parent = lookup_stored_bitmap(st->xor);
108 ewah_xor(st->root, parent, composed);
110 ewah_pool_free(st->root);
111 st->root = composed;
112 st->xor = NULL;
114 return composed;
118 * Read a bitmap from the current read position on the mmaped
119 * index, and increase the read position accordingly
121 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
123 struct ewah_bitmap *b = ewah_pool_new();
125 ssize_t bitmap_size = ewah_read_mmap(b,
126 index->map + index->map_pos,
127 index->map_size - index->map_pos);
129 if (bitmap_size < 0) {
130 error("Failed to load bitmap index (corrupted?)");
131 ewah_pool_free(b);
132 return NULL;
135 index->map_pos += bitmap_size;
136 return b;
139 static uint32_t bitmap_num_objects(struct bitmap_index *index)
141 return index->pack->num_objects;
144 static int load_bitmap_header(struct bitmap_index *index)
146 struct bitmap_disk_header *header = (void *)index->map;
147 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
149 if (index->map_size < header_size + the_hash_algo->rawsz)
150 return error("Corrupted bitmap index (too small)");
152 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
153 return error("Corrupted bitmap index file (wrong header)");
155 index->version = ntohs(header->version);
156 if (index->version != 1)
157 return error("Unsupported version for bitmap index file (%d)", index->version);
159 /* Parse known bitmap format options */
161 uint32_t flags = ntohs(header->options);
162 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
163 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
165 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
166 return error("Unsupported options for bitmap index file "
167 "(Git requires BITMAP_OPT_FULL_DAG)");
169 if (flags & BITMAP_OPT_HASH_CACHE) {
170 if (cache_size > index_end - index->map - header_size)
171 return error("corrupted bitmap index file (too short to fit hash cache)");
172 index->hashes = (void *)(index_end - cache_size);
173 index_end -= cache_size;
177 index->entry_count = ntohl(header->entry_count);
178 index->map_pos += header_size;
179 return 0;
182 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
183 struct ewah_bitmap *root,
184 const struct object_id *oid,
185 struct stored_bitmap *xor_with,
186 int flags)
188 struct stored_bitmap *stored;
189 khiter_t hash_pos;
190 int ret;
192 stored = xmalloc(sizeof(struct stored_bitmap));
193 stored->root = root;
194 stored->xor = xor_with;
195 stored->flags = flags;
196 oidcpy(&stored->oid, oid);
198 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
200 /* a 0 return code means the insertion succeeded with no changes,
201 * because the SHA1 already existed on the map. this is bad, there
202 * shouldn't be duplicated commits in the index */
203 if (ret == 0) {
204 error("Duplicate entry in bitmap index: %s", oid_to_hex(oid));
205 return NULL;
208 kh_value(index->bitmaps, hash_pos) = stored;
209 return stored;
212 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
214 uint32_t result = get_be32(buffer + *pos);
215 (*pos) += sizeof(result);
216 return result;
219 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
221 return buffer[(*pos)++];
224 #define MAX_XOR_OFFSET 160
226 static int load_bitmap_entries_v1(struct bitmap_index *index)
228 uint32_t i;
229 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
231 for (i = 0; i < index->entry_count; ++i) {
232 int xor_offset, flags;
233 struct ewah_bitmap *bitmap = NULL;
234 struct stored_bitmap *xor_bitmap = NULL;
235 uint32_t commit_idx_pos;
236 struct object_id oid;
238 if (index->map_size - index->map_pos < 6)
239 return error("corrupt ewah bitmap: truncated header for entry %d", i);
241 commit_idx_pos = read_be32(index->map, &index->map_pos);
242 xor_offset = read_u8(index->map, &index->map_pos);
243 flags = read_u8(index->map, &index->map_pos);
245 if (nth_packed_object_id(&oid, index->pack, commit_idx_pos) < 0)
246 return error("corrupt ewah bitmap: commit index %u out of range",
247 (unsigned)commit_idx_pos);
249 bitmap = read_bitmap_1(index);
250 if (!bitmap)
251 return -1;
253 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
254 return error("Corrupted bitmap pack index");
256 if (xor_offset > 0) {
257 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
259 if (xor_bitmap == NULL)
260 return error("Invalid XOR offset in bitmap pack index");
263 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
264 index, bitmap, &oid, xor_bitmap, flags);
267 return 0;
270 static char *pack_bitmap_filename(struct packed_git *p)
272 size_t len;
274 if (!strip_suffix(p->pack_name, ".pack", &len))
275 BUG("pack_name does not end in .pack");
276 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
279 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
281 int fd;
282 struct stat st;
283 char *idx_name;
285 if (open_pack_index(packfile))
286 return -1;
288 idx_name = pack_bitmap_filename(packfile);
289 fd = git_open(idx_name);
290 free(idx_name);
292 if (fd < 0)
293 return -1;
295 if (fstat(fd, &st)) {
296 close(fd);
297 return -1;
300 if (bitmap_git->pack) {
301 warning("ignoring extra bitmap file: %s", packfile->pack_name);
302 close(fd);
303 return -1;
306 if (!is_pack_valid(packfile)) {
307 close(fd);
308 return -1;
311 bitmap_git->pack = packfile;
312 bitmap_git->map_size = xsize_t(st.st_size);
313 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
314 bitmap_git->map_pos = 0;
315 close(fd);
317 if (load_bitmap_header(bitmap_git) < 0) {
318 munmap(bitmap_git->map, bitmap_git->map_size);
319 bitmap_git->map = NULL;
320 bitmap_git->map_size = 0;
321 return -1;
324 return 0;
327 static int load_pack_bitmap(struct bitmap_index *bitmap_git)
329 assert(bitmap_git->map);
331 bitmap_git->bitmaps = kh_init_oid_map();
332 bitmap_git->ext_index.positions = kh_init_oid_pos();
333 if (load_pack_revindex(bitmap_git->pack))
334 goto failed;
336 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
337 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
338 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
339 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
340 goto failed;
342 if (load_bitmap_entries_v1(bitmap_git) < 0)
343 goto failed;
345 return 0;
347 failed:
348 munmap(bitmap_git->map, bitmap_git->map_size);
349 bitmap_git->map = NULL;
350 bitmap_git->map_size = 0;
352 kh_destroy_oid_map(bitmap_git->bitmaps);
353 bitmap_git->bitmaps = NULL;
355 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
356 bitmap_git->ext_index.positions = NULL;
358 return -1;
361 static int open_pack_bitmap(struct repository *r,
362 struct bitmap_index *bitmap_git)
364 struct packed_git *p;
365 int ret = -1;
367 assert(!bitmap_git->map);
369 for (p = get_all_packs(r); p; p = p->next) {
370 if (open_pack_bitmap_1(bitmap_git, p) == 0)
371 ret = 0;
374 return ret;
377 struct bitmap_index *prepare_bitmap_git(struct repository *r)
379 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
381 if (!open_pack_bitmap(r, bitmap_git) && !load_pack_bitmap(bitmap_git))
382 return bitmap_git;
384 free_bitmap_index(bitmap_git);
385 return NULL;
388 struct include_data {
389 struct bitmap_index *bitmap_git;
390 struct bitmap *base;
391 struct bitmap *seen;
394 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
395 struct commit *commit)
397 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
398 commit->object.oid);
399 if (hash_pos >= kh_end(bitmap_git->bitmaps))
400 return NULL;
401 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
404 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
405 const struct object_id *oid)
407 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
408 khiter_t pos = kh_get_oid_pos(positions, *oid);
410 if (pos < kh_end(positions)) {
411 int bitmap_pos = kh_value(positions, pos);
412 return bitmap_pos + bitmap_num_objects(bitmap_git);
415 return -1;
418 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
419 const struct object_id *oid)
421 uint32_t pos;
422 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
423 if (!offset)
424 return -1;
426 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
427 return -1;
428 return pos;
431 static int bitmap_position(struct bitmap_index *bitmap_git,
432 const struct object_id *oid)
434 int pos = bitmap_position_packfile(bitmap_git, oid);
435 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
438 static int ext_index_add_object(struct bitmap_index *bitmap_git,
439 struct object *object, const char *name)
441 struct eindex *eindex = &bitmap_git->ext_index;
443 khiter_t hash_pos;
444 int hash_ret;
445 int bitmap_pos;
447 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
448 if (hash_ret > 0) {
449 if (eindex->count >= eindex->alloc) {
450 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
451 REALLOC_ARRAY(eindex->objects, eindex->alloc);
452 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
455 bitmap_pos = eindex->count;
456 eindex->objects[eindex->count] = object;
457 eindex->hashes[eindex->count] = pack_name_hash(name);
458 kh_value(eindex->positions, hash_pos) = bitmap_pos;
459 eindex->count++;
460 } else {
461 bitmap_pos = kh_value(eindex->positions, hash_pos);
464 return bitmap_pos + bitmap_num_objects(bitmap_git);
467 struct bitmap_show_data {
468 struct bitmap_index *bitmap_git;
469 struct bitmap *base;
472 static void show_object(struct object *object, const char *name, void *data_)
474 struct bitmap_show_data *data = data_;
475 int bitmap_pos;
477 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
479 if (bitmap_pos < 0)
480 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
481 name);
483 bitmap_set(data->base, bitmap_pos);
486 static void show_commit(struct commit *commit, void *data)
490 static int add_to_include_set(struct bitmap_index *bitmap_git,
491 struct include_data *data,
492 struct commit *commit,
493 int bitmap_pos)
495 struct ewah_bitmap *partial;
497 if (data->seen && bitmap_get(data->seen, bitmap_pos))
498 return 0;
500 if (bitmap_get(data->base, bitmap_pos))
501 return 0;
503 partial = bitmap_for_commit(bitmap_git, commit);
504 if (partial) {
505 bitmap_or_ewah(data->base, partial);
506 return 0;
509 bitmap_set(data->base, bitmap_pos);
510 return 1;
513 static int should_include(struct commit *commit, void *_data)
515 struct include_data *data = _data;
516 int bitmap_pos;
518 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
519 if (bitmap_pos < 0)
520 bitmap_pos = ext_index_add_object(data->bitmap_git,
521 (struct object *)commit,
522 NULL);
524 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
525 struct commit_list *parent = commit->parents;
527 while (parent) {
528 parent->item->object.flags |= SEEN;
529 parent = parent->next;
532 return 0;
535 return 1;
538 static int should_include_obj(struct object *obj, void *_data)
540 struct include_data *data = _data;
541 int bitmap_pos;
543 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
544 if (bitmap_pos < 0)
545 return 1;
546 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
547 bitmap_get(data->base, bitmap_pos)) {
548 obj->flags |= SEEN;
549 return 0;
551 return 1;
554 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
555 struct bitmap **base,
556 struct commit *commit)
558 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
560 if (!or_with)
561 return 0;
563 if (*base == NULL)
564 *base = ewah_to_bitmap(or_with);
565 else
566 bitmap_or_ewah(*base, or_with);
568 return 1;
571 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
572 struct rev_info *revs,
573 struct object_list *roots,
574 struct bitmap *seen,
575 struct list_objects_filter_options *filter)
577 struct bitmap *base = NULL;
578 int needs_walk = 0;
580 struct object_list *not_mapped = NULL;
583 * Go through all the roots for the walk. The ones that have bitmaps
584 * on the bitmap index will be `or`ed together to form an initial
585 * global reachability analysis.
587 * The ones without bitmaps in the index will be stored in the
588 * `not_mapped_list` for further processing.
590 while (roots) {
591 struct object *object = roots->item;
592 roots = roots->next;
594 if (object->type == OBJ_COMMIT &&
595 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
596 object->flags |= SEEN;
597 continue;
600 object_list_insert(object, &not_mapped);
604 * Best case scenario: We found bitmaps for all the roots,
605 * so the resulting `or` bitmap has the full reachability analysis
607 if (not_mapped == NULL)
608 return base;
610 roots = not_mapped;
613 * Let's iterate through all the roots that don't have bitmaps to
614 * check if we can determine them to be reachable from the existing
615 * global bitmap.
617 * If we cannot find them in the existing global bitmap, we'll need
618 * to push them to an actual walk and run it until we can confirm
619 * they are reachable
621 while (roots) {
622 struct object *object = roots->item;
623 int pos;
625 roots = roots->next;
626 pos = bitmap_position(bitmap_git, &object->oid);
628 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
629 object->flags &= ~UNINTERESTING;
630 add_pending_object(revs, object, "");
631 needs_walk = 1;
632 } else {
633 object->flags |= SEEN;
637 if (needs_walk) {
638 struct include_data incdata;
639 struct bitmap_show_data show_data;
641 if (base == NULL)
642 base = bitmap_new();
644 incdata.bitmap_git = bitmap_git;
645 incdata.base = base;
646 incdata.seen = seen;
648 revs->include_check = should_include;
649 revs->include_check_obj = should_include_obj;
650 revs->include_check_data = &incdata;
652 if (prepare_revision_walk(revs))
653 die("revision walk setup failed");
655 show_data.bitmap_git = bitmap_git;
656 show_data.base = base;
658 traverse_commit_list_filtered(filter, revs,
659 show_commit, show_object,
660 &show_data, NULL);
662 revs->include_check = NULL;
663 revs->include_check_obj = NULL;
664 revs->include_check_data = NULL;
667 return base;
670 static void show_extended_objects(struct bitmap_index *bitmap_git,
671 struct rev_info *revs,
672 show_reachable_fn show_reach)
674 struct bitmap *objects = bitmap_git->result;
675 struct eindex *eindex = &bitmap_git->ext_index;
676 uint32_t i;
678 for (i = 0; i < eindex->count; ++i) {
679 struct object *obj;
681 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
682 continue;
684 obj = eindex->objects[i];
685 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
686 (obj->type == OBJ_TREE && !revs->tree_objects) ||
687 (obj->type == OBJ_TAG && !revs->tag_objects))
688 continue;
690 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
694 static void init_type_iterator(struct ewah_iterator *it,
695 struct bitmap_index *bitmap_git,
696 enum object_type type)
698 switch (type) {
699 case OBJ_COMMIT:
700 ewah_iterator_init(it, bitmap_git->commits);
701 break;
703 case OBJ_TREE:
704 ewah_iterator_init(it, bitmap_git->trees);
705 break;
707 case OBJ_BLOB:
708 ewah_iterator_init(it, bitmap_git->blobs);
709 break;
711 case OBJ_TAG:
712 ewah_iterator_init(it, bitmap_git->tags);
713 break;
715 default:
716 BUG("object type %d not stored by bitmap type index", type);
717 break;
721 static void show_objects_for_type(
722 struct bitmap_index *bitmap_git,
723 enum object_type object_type,
724 show_reachable_fn show_reach)
726 size_t i = 0;
727 uint32_t offset;
729 struct ewah_iterator it;
730 eword_t filter;
732 struct bitmap *objects = bitmap_git->result;
734 init_type_iterator(&it, bitmap_git, object_type);
736 for (i = 0; i < objects->word_alloc &&
737 ewah_iterator_next(&filter, &it); i++) {
738 eword_t word = objects->words[i] & filter;
739 size_t pos = (i * BITS_IN_EWORD);
741 if (!word)
742 continue;
744 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
745 struct object_id oid;
746 uint32_t hash = 0, index_pos;
747 off_t ofs;
749 if ((word >> offset) == 0)
750 break;
752 offset += ewah_bit_ctz64(word >> offset);
754 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
755 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
756 nth_packed_object_id(&oid, bitmap_git->pack, index_pos);
758 if (bitmap_git->hashes)
759 hash = get_be32(bitmap_git->hashes + index_pos);
761 show_reach(&oid, object_type, 0, hash, bitmap_git->pack, ofs);
766 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
767 struct object_list *roots)
769 while (roots) {
770 struct object *object = roots->item;
771 roots = roots->next;
773 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
774 return 1;
777 return 0;
780 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
781 struct object_list *tip_objects,
782 enum object_type type)
784 struct bitmap *result = bitmap_new();
785 struct object_list *p;
787 for (p = tip_objects; p; p = p->next) {
788 int pos;
790 if (p->item->type != type)
791 continue;
793 pos = bitmap_position(bitmap_git, &p->item->oid);
794 if (pos < 0)
795 continue;
797 bitmap_set(result, pos);
800 return result;
803 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
804 struct object_list *tip_objects,
805 struct bitmap *to_filter,
806 enum object_type type)
808 struct eindex *eindex = &bitmap_git->ext_index;
809 struct bitmap *tips;
810 struct ewah_iterator it;
811 eword_t mask;
812 uint32_t i;
815 * The non-bitmap version of this filter never removes
816 * objects which the other side specifically asked for,
817 * so we must match that behavior.
819 tips = find_tip_objects(bitmap_git, tip_objects, type);
822 * We can use the type-level bitmap for 'type' to work in whole
823 * words for the objects that are actually in the bitmapped
824 * packfile.
826 for (i = 0, init_type_iterator(&it, bitmap_git, type);
827 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
828 i++) {
829 if (i < tips->word_alloc)
830 mask &= ~tips->words[i];
831 to_filter->words[i] &= ~mask;
835 * Clear any objects that weren't in the packfile (and so would
836 * not have been caught by the loop above. We'll have to check
837 * them individually.
839 for (i = 0; i < eindex->count; i++) {
840 uint32_t pos = i + bitmap_num_objects(bitmap_git);
841 if (eindex->objects[i]->type == type &&
842 bitmap_get(to_filter, pos) &&
843 !bitmap_get(tips, pos))
844 bitmap_unset(to_filter, pos);
847 bitmap_free(tips);
850 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
851 struct object_list *tip_objects,
852 struct bitmap *to_filter)
854 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
855 OBJ_BLOB);
858 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
859 uint32_t pos)
861 struct packed_git *pack = bitmap_git->pack;
862 unsigned long size;
863 struct object_info oi = OBJECT_INFO_INIT;
865 oi.sizep = &size;
867 if (pos < bitmap_num_objects(bitmap_git)) {
868 off_t ofs = pack_pos_to_offset(pack, pos);
869 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
870 struct object_id oid;
871 nth_packed_object_id(&oid, pack,
872 pack_pos_to_index(pack, pos));
873 die(_("unable to get size of %s"), oid_to_hex(&oid));
875 } else {
876 struct eindex *eindex = &bitmap_git->ext_index;
877 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
878 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
879 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
882 return size;
885 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
886 struct object_list *tip_objects,
887 struct bitmap *to_filter,
888 unsigned long limit)
890 struct eindex *eindex = &bitmap_git->ext_index;
891 struct bitmap *tips;
892 struct ewah_iterator it;
893 eword_t mask;
894 uint32_t i;
896 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
898 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
899 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
900 i++) {
901 eword_t word = to_filter->words[i] & mask;
902 unsigned offset;
904 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
905 uint32_t pos;
907 if ((word >> offset) == 0)
908 break;
909 offset += ewah_bit_ctz64(word >> offset);
910 pos = i * BITS_IN_EWORD + offset;
912 if (!bitmap_get(tips, pos) &&
913 get_size_by_pos(bitmap_git, pos) >= limit)
914 bitmap_unset(to_filter, pos);
918 for (i = 0; i < eindex->count; i++) {
919 uint32_t pos = i + bitmap_num_objects(bitmap_git);
920 if (eindex->objects[i]->type == OBJ_BLOB &&
921 bitmap_get(to_filter, pos) &&
922 !bitmap_get(tips, pos) &&
923 get_size_by_pos(bitmap_git, pos) >= limit)
924 bitmap_unset(to_filter, pos);
927 bitmap_free(tips);
930 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
931 struct object_list *tip_objects,
932 struct bitmap *to_filter,
933 unsigned long limit)
935 if (limit)
936 BUG("filter_bitmap_tree_depth given non-zero limit");
938 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
939 OBJ_TREE);
940 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
941 OBJ_BLOB);
944 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
945 struct object_list *tip_objects,
946 struct bitmap *to_filter,
947 enum object_type object_type)
949 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
950 BUG("filter_bitmap_object_type given invalid object");
952 if (object_type != OBJ_TAG)
953 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
954 if (object_type != OBJ_COMMIT)
955 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
956 if (object_type != OBJ_TREE)
957 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
958 if (object_type != OBJ_BLOB)
959 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
962 static int filter_bitmap(struct bitmap_index *bitmap_git,
963 struct object_list *tip_objects,
964 struct bitmap *to_filter,
965 struct list_objects_filter_options *filter)
967 if (!filter || filter->choice == LOFC_DISABLED)
968 return 0;
970 if (filter->choice == LOFC_BLOB_NONE) {
971 if (bitmap_git)
972 filter_bitmap_blob_none(bitmap_git, tip_objects,
973 to_filter);
974 return 0;
977 if (filter->choice == LOFC_BLOB_LIMIT) {
978 if (bitmap_git)
979 filter_bitmap_blob_limit(bitmap_git, tip_objects,
980 to_filter,
981 filter->blob_limit_value);
982 return 0;
985 if (filter->choice == LOFC_TREE_DEPTH &&
986 filter->tree_exclude_depth == 0) {
987 if (bitmap_git)
988 filter_bitmap_tree_depth(bitmap_git, tip_objects,
989 to_filter,
990 filter->tree_exclude_depth);
991 return 0;
994 if (filter->choice == LOFC_OBJECT_TYPE) {
995 if (bitmap_git)
996 filter_bitmap_object_type(bitmap_git, tip_objects,
997 to_filter,
998 filter->object_type);
999 return 0;
1002 if (filter->choice == LOFC_COMBINE) {
1003 int i;
1004 for (i = 0; i < filter->sub_nr; i++) {
1005 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1006 &filter->sub[i]) < 0)
1007 return -1;
1009 return 0;
1012 /* filter choice not handled */
1013 return -1;
1016 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1018 return !filter_bitmap(NULL, NULL, NULL, filter);
1021 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1022 struct list_objects_filter_options *filter,
1023 int filter_provided_objects)
1025 unsigned int i;
1027 struct object_list *wants = NULL;
1028 struct object_list *haves = NULL;
1030 struct bitmap *wants_bitmap = NULL;
1031 struct bitmap *haves_bitmap = NULL;
1033 struct bitmap_index *bitmap_git;
1036 * We can't do pathspec limiting with bitmaps, because we don't know
1037 * which commits are associated with which object changes (let alone
1038 * even which objects are associated with which paths).
1040 if (revs->prune)
1041 return NULL;
1043 if (!can_filter_bitmap(filter))
1044 return NULL;
1046 /* try to open a bitmapped pack, but don't parse it yet
1047 * because we may not need to use it */
1048 CALLOC_ARRAY(bitmap_git, 1);
1049 if (open_pack_bitmap(revs->repo, bitmap_git) < 0)
1050 goto cleanup;
1052 for (i = 0; i < revs->pending.nr; ++i) {
1053 struct object *object = revs->pending.objects[i].item;
1055 if (object->type == OBJ_NONE)
1056 parse_object_or_die(&object->oid, NULL);
1058 while (object->type == OBJ_TAG) {
1059 struct tag *tag = (struct tag *) object;
1061 if (object->flags & UNINTERESTING)
1062 object_list_insert(object, &haves);
1063 else
1064 object_list_insert(object, &wants);
1066 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1067 object->flags |= (tag->object.flags & UNINTERESTING);
1070 if (object->flags & UNINTERESTING)
1071 object_list_insert(object, &haves);
1072 else
1073 object_list_insert(object, &wants);
1077 * if we have a HAVES list, but none of those haves is contained
1078 * in the packfile that has a bitmap, we don't have anything to
1079 * optimize here
1081 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1082 goto cleanup;
1084 /* if we don't want anything, we're done here */
1085 if (!wants)
1086 goto cleanup;
1089 * now we're going to use bitmaps, so load the actual bitmap entries
1090 * from disk. this is the point of no return; after this the rev_list
1091 * becomes invalidated and we must perform the revwalk through bitmaps
1093 if (load_pack_bitmap(bitmap_git) < 0)
1094 goto cleanup;
1096 object_array_clear(&revs->pending);
1098 if (haves) {
1099 revs->ignore_missing_links = 1;
1100 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
1101 filter);
1102 reset_revision_walk();
1103 revs->ignore_missing_links = 0;
1105 if (haves_bitmap == NULL)
1106 BUG("failed to perform bitmap walk");
1109 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
1110 filter);
1112 if (!wants_bitmap)
1113 BUG("failed to perform bitmap walk");
1115 if (haves_bitmap)
1116 bitmap_and_not(wants_bitmap, haves_bitmap);
1118 filter_bitmap(bitmap_git, (filter && filter_provided_objects) ? NULL : wants,
1119 wants_bitmap, filter);
1121 bitmap_git->result = wants_bitmap;
1122 bitmap_git->haves = haves_bitmap;
1124 object_list_free(&wants);
1125 object_list_free(&haves);
1127 return bitmap_git;
1129 cleanup:
1130 free_bitmap_index(bitmap_git);
1131 object_list_free(&wants);
1132 object_list_free(&haves);
1133 return NULL;
1136 static void try_partial_reuse(struct bitmap_index *bitmap_git,
1137 size_t pos,
1138 struct bitmap *reuse,
1139 struct pack_window **w_curs)
1141 off_t offset, header;
1142 enum object_type type;
1143 unsigned long size;
1145 if (pos >= bitmap_num_objects(bitmap_git))
1146 return; /* not actually in the pack or MIDX */
1148 offset = header = pack_pos_to_offset(bitmap_git->pack, pos);
1149 type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
1150 if (type < 0)
1151 return; /* broken packfile, punt */
1153 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1154 off_t base_offset;
1155 uint32_t base_pos;
1158 * Find the position of the base object so we can look it up
1159 * in our bitmaps. If we can't come up with an offset, or if
1160 * that offset is not in the revidx, the pack is corrupt.
1161 * There's nothing we can do, so just punt on this object,
1162 * and the normal slow path will complain about it in
1163 * more detail.
1165 base_offset = get_delta_base(bitmap_git->pack, w_curs,
1166 &offset, type, header);
1167 if (!base_offset)
1168 return;
1169 if (offset_to_pack_pos(bitmap_git->pack, base_offset, &base_pos) < 0)
1170 return;
1173 * We assume delta dependencies always point backwards. This
1174 * lets us do a single pass, and is basically always true
1175 * due to the way OFS_DELTAs work. You would not typically
1176 * find REF_DELTA in a bitmapped pack, since we only bitmap
1177 * packs we write fresh, and OFS_DELTA is the default). But
1178 * let's double check to make sure the pack wasn't written with
1179 * odd parameters.
1181 if (base_pos >= pos)
1182 return;
1185 * And finally, if we're not sending the base as part of our
1186 * reuse chunk, then don't send this object either. The base
1187 * would come after us, along with other objects not
1188 * necessarily in the pack, which means we'd need to convert
1189 * to REF_DELTA on the fly. Better to just let the normal
1190 * object_entry code path handle it.
1192 if (!bitmap_get(reuse, base_pos))
1193 return;
1197 * If we got here, then the object is OK to reuse. Mark it.
1199 bitmap_set(reuse, pos);
1202 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1203 struct packed_git **packfile_out,
1204 uint32_t *entries,
1205 struct bitmap **reuse_out)
1207 struct bitmap *result = bitmap_git->result;
1208 struct bitmap *reuse;
1209 struct pack_window *w_curs = NULL;
1210 size_t i = 0;
1211 uint32_t offset;
1212 uint32_t objects_nr = bitmap_num_objects(bitmap_git);
1214 assert(result);
1216 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1217 i++;
1219 /* Don't mark objects not in the packfile */
1220 if (i > objects_nr / BITS_IN_EWORD)
1221 i = objects_nr / BITS_IN_EWORD;
1223 reuse = bitmap_word_alloc(i);
1224 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1226 for (; i < result->word_alloc; ++i) {
1227 eword_t word = result->words[i];
1228 size_t pos = (i * BITS_IN_EWORD);
1230 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1231 if ((word >> offset) == 0)
1232 break;
1234 offset += ewah_bit_ctz64(word >> offset);
1235 try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs);
1239 unuse_pack(&w_curs);
1241 *entries = bitmap_popcount(reuse);
1242 if (!*entries) {
1243 bitmap_free(reuse);
1244 return -1;
1248 * Drop any reused objects from the result, since they will not
1249 * need to be handled separately.
1251 bitmap_and_not(result, reuse);
1252 *packfile_out = bitmap_git->pack;
1253 *reuse_out = reuse;
1254 return 0;
1257 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1258 struct bitmap *bitmap, const struct object_id *oid)
1260 int idx;
1262 if (!bitmap)
1263 return 0;
1265 idx = bitmap_position(bitmap_git, oid);
1266 return idx >= 0 && bitmap_get(bitmap, idx);
1269 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1270 struct rev_info *revs,
1271 show_reachable_fn show_reachable)
1273 assert(bitmap_git->result);
1275 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1276 if (revs->tree_objects)
1277 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1278 if (revs->blob_objects)
1279 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1280 if (revs->tag_objects)
1281 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1283 show_extended_objects(bitmap_git, revs, show_reachable);
1286 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1287 enum object_type type)
1289 struct bitmap *objects = bitmap_git->result;
1290 struct eindex *eindex = &bitmap_git->ext_index;
1292 uint32_t i = 0, count = 0;
1293 struct ewah_iterator it;
1294 eword_t filter;
1296 init_type_iterator(&it, bitmap_git, type);
1298 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1299 eword_t word = objects->words[i++] & filter;
1300 count += ewah_bit_popcount64(word);
1303 for (i = 0; i < eindex->count; ++i) {
1304 if (eindex->objects[i]->type == type &&
1305 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1306 count++;
1309 return count;
1312 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1313 uint32_t *commits, uint32_t *trees,
1314 uint32_t *blobs, uint32_t *tags)
1316 assert(bitmap_git->result);
1318 if (commits)
1319 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1321 if (trees)
1322 *trees = count_object_type(bitmap_git, OBJ_TREE);
1324 if (blobs)
1325 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1327 if (tags)
1328 *tags = count_object_type(bitmap_git, OBJ_TAG);
1331 struct bitmap_test_data {
1332 struct bitmap_index *bitmap_git;
1333 struct bitmap *base;
1334 struct bitmap *commits;
1335 struct bitmap *trees;
1336 struct bitmap *blobs;
1337 struct bitmap *tags;
1338 struct progress *prg;
1339 size_t seen;
1342 static void test_bitmap_type(struct bitmap_test_data *tdata,
1343 struct object *obj, int pos)
1345 enum object_type bitmap_type = OBJ_NONE;
1346 int bitmaps_nr = 0;
1348 if (bitmap_get(tdata->commits, pos)) {
1349 bitmap_type = OBJ_COMMIT;
1350 bitmaps_nr++;
1352 if (bitmap_get(tdata->trees, pos)) {
1353 bitmap_type = OBJ_TREE;
1354 bitmaps_nr++;
1356 if (bitmap_get(tdata->blobs, pos)) {
1357 bitmap_type = OBJ_BLOB;
1358 bitmaps_nr++;
1360 if (bitmap_get(tdata->tags, pos)) {
1361 bitmap_type = OBJ_TAG;
1362 bitmaps_nr++;
1365 if (bitmap_type == OBJ_NONE)
1366 die("object %s not found in type bitmaps",
1367 oid_to_hex(&obj->oid));
1369 if (bitmaps_nr > 1)
1370 die("object %s does not have a unique type",
1371 oid_to_hex(&obj->oid));
1373 if (bitmap_type != obj->type)
1374 die("object %s: real type %s, expected: %s",
1375 oid_to_hex(&obj->oid),
1376 type_name(obj->type),
1377 type_name(bitmap_type));
1380 static void test_show_object(struct object *object, const char *name,
1381 void *data)
1383 struct bitmap_test_data *tdata = data;
1384 int bitmap_pos;
1386 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1387 if (bitmap_pos < 0)
1388 die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
1389 test_bitmap_type(tdata, object, bitmap_pos);
1391 bitmap_set(tdata->base, bitmap_pos);
1392 display_progress(tdata->prg, ++tdata->seen);
1395 static void test_show_commit(struct commit *commit, void *data)
1397 struct bitmap_test_data *tdata = data;
1398 int bitmap_pos;
1400 bitmap_pos = bitmap_position(tdata->bitmap_git,
1401 &commit->object.oid);
1402 if (bitmap_pos < 0)
1403 die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
1404 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1406 bitmap_set(tdata->base, bitmap_pos);
1407 display_progress(tdata->prg, ++tdata->seen);
1410 void test_bitmap_walk(struct rev_info *revs)
1412 struct object *root;
1413 struct bitmap *result = NULL;
1414 size_t result_popcnt;
1415 struct bitmap_test_data tdata;
1416 struct bitmap_index *bitmap_git;
1417 struct ewah_bitmap *bm;
1419 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1420 die("failed to load bitmap indexes");
1422 if (revs->pending.nr != 1)
1423 die("you must specify exactly one commit to test");
1425 fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
1426 bitmap_git->version, bitmap_git->entry_count);
1428 root = revs->pending.objects[0].item;
1429 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
1431 if (bm) {
1432 fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
1433 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
1435 result = ewah_to_bitmap(bm);
1438 if (result == NULL)
1439 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
1441 revs->tag_objects = 1;
1442 revs->tree_objects = 1;
1443 revs->blob_objects = 1;
1445 result_popcnt = bitmap_popcount(result);
1447 if (prepare_revision_walk(revs))
1448 die("revision walk setup failed");
1450 tdata.bitmap_git = bitmap_git;
1451 tdata.base = bitmap_new();
1452 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
1453 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
1454 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
1455 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
1456 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1457 tdata.seen = 0;
1459 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1461 stop_progress(&tdata.prg);
1463 if (bitmap_equals(result, tdata.base))
1464 fprintf(stderr, "OK!\n");
1465 else
1466 die("mismatch in bitmap results");
1468 free_bitmap_index(bitmap_git);
1471 int test_bitmap_commits(struct repository *r)
1473 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1474 struct object_id oid;
1475 MAYBE_UNUSED void *value;
1477 if (!bitmap_git)
1478 die("failed to load bitmap indexes");
1480 kh_foreach(bitmap_git->bitmaps, oid, value, {
1481 printf("%s\n", oid_to_hex(&oid));
1484 free_bitmap_index(bitmap_git);
1486 return 0;
1489 int rebuild_bitmap(const uint32_t *reposition,
1490 struct ewah_bitmap *source,
1491 struct bitmap *dest)
1493 uint32_t pos = 0;
1494 struct ewah_iterator it;
1495 eword_t word;
1497 ewah_iterator_init(&it, source);
1499 while (ewah_iterator_next(&word, &it)) {
1500 uint32_t offset, bit_pos;
1502 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1503 if ((word >> offset) == 0)
1504 break;
1506 offset += ewah_bit_ctz64(word >> offset);
1508 bit_pos = reposition[pos + offset];
1509 if (bit_pos > 0)
1510 bitmap_set(dest, bit_pos - 1);
1511 else /* can't reuse, we don't have the object */
1512 return -1;
1515 pos += BITS_IN_EWORD;
1517 return 0;
1520 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1521 struct packing_data *mapping)
1523 uint32_t i, num_objects;
1524 uint32_t *reposition;
1526 num_objects = bitmap_num_objects(bitmap_git);
1527 CALLOC_ARRAY(reposition, num_objects);
1529 for (i = 0; i < num_objects; ++i) {
1530 struct object_id oid;
1531 struct object_entry *oe;
1533 nth_packed_object_id(&oid, bitmap_git->pack,
1534 pack_pos_to_index(bitmap_git->pack, i));
1535 oe = packlist_find(mapping, &oid);
1537 if (oe)
1538 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
1541 return reposition;
1544 void free_bitmap_index(struct bitmap_index *b)
1546 if (!b)
1547 return;
1549 if (b->map)
1550 munmap(b->map, b->map_size);
1551 ewah_pool_free(b->commits);
1552 ewah_pool_free(b->trees);
1553 ewah_pool_free(b->blobs);
1554 ewah_pool_free(b->tags);
1555 kh_destroy_oid_map(b->bitmaps);
1556 free(b->ext_index.objects);
1557 free(b->ext_index.hashes);
1558 bitmap_free(b->result);
1559 bitmap_free(b->haves);
1560 free(b);
1563 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1564 const struct object_id *oid)
1566 return bitmap_git &&
1567 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
1570 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1571 enum object_type object_type)
1573 struct bitmap *result = bitmap_git->result;
1574 struct packed_git *pack = bitmap_git->pack;
1575 off_t total = 0;
1576 struct ewah_iterator it;
1577 eword_t filter;
1578 size_t i;
1580 init_type_iterator(&it, bitmap_git, object_type);
1581 for (i = 0; i < result->word_alloc &&
1582 ewah_iterator_next(&filter, &it); i++) {
1583 eword_t word = result->words[i] & filter;
1584 size_t base = (i * BITS_IN_EWORD);
1585 unsigned offset;
1587 if (!word)
1588 continue;
1590 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1591 size_t pos;
1593 if ((word >> offset) == 0)
1594 break;
1596 offset += ewah_bit_ctz64(word >> offset);
1597 pos = base + offset;
1598 total += pack_pos_to_offset(pack, pos + 1) -
1599 pack_pos_to_offset(pack, pos);
1603 return total;
1606 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1608 struct bitmap *result = bitmap_git->result;
1609 struct eindex *eindex = &bitmap_git->ext_index;
1610 off_t total = 0;
1611 struct object_info oi = OBJECT_INFO_INIT;
1612 off_t object_size;
1613 size_t i;
1615 oi.disk_sizep = &object_size;
1617 for (i = 0; i < eindex->count; i++) {
1618 struct object *obj = eindex->objects[i];
1620 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
1621 continue;
1623 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1624 die(_("unable to get disk usage of %s"),
1625 oid_to_hex(&obj->oid));
1627 total += object_size;
1629 return total;
1632 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
1633 struct rev_info *revs)
1635 off_t total = 0;
1637 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
1638 if (revs->tree_objects)
1639 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
1640 if (revs->blob_objects)
1641 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
1642 if (revs->tag_objects)
1643 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
1645 total += get_disk_usage_for_extended(bitmap_git);
1647 return total;
1650 const struct string_list *bitmap_preferred_tips(struct repository *r)
1652 return repo_config_get_value_multi(r, "pack.preferbitmaptips");