Merge branch 'ab/config-h-remove-unused'
[git/debian.git] / pack-bitmap.c
blobd2a42abf28cc7246b3d076b1c855a350edcded70
1 #include "cache.h"
2 #include "commit.h"
3 #include "strbuf.h"
4 #include "tag.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "progress.h"
8 #include "list-objects.h"
9 #include "pack.h"
10 #include "pack-bitmap.h"
11 #include "pack-revindex.h"
12 #include "pack-objects.h"
13 #include "packfile.h"
14 #include "repository.h"
15 #include "object-store.h"
16 #include "list-objects-filter-options.h"
17 #include "midx.h"
18 #include "config.h"
21 * An entry on the bitmap index, representing the bitmap for a given
22 * commit.
24 struct stored_bitmap {
25 struct object_id oid;
26 struct ewah_bitmap *root;
27 struct stored_bitmap *xor;
28 int flags;
32 * The active bitmap index for a repository. By design, repositories only have
33 * a single bitmap index available (the index for the biggest packfile in
34 * the repository), since bitmap indexes need full closure.
36 * If there is more than one bitmap index available (e.g. because of alternates),
37 * the active bitmap index is the largest one.
39 struct bitmap_index {
41 * The pack or multi-pack index (MIDX) that this bitmap index belongs
42 * to.
44 * Exactly one of these must be non-NULL; this specifies the object
45 * order used to interpret this bitmap.
47 struct packed_git *pack;
48 struct multi_pack_index *midx;
51 * Mark the first `reuse_objects` in the packfile as reused:
52 * they will be sent as-is without using them for repacking
53 * calculations
55 uint32_t reuse_objects;
57 /* mmapped buffer of the whole bitmap index */
58 unsigned char *map;
59 size_t map_size; /* size of the mmaped buffer */
60 size_t map_pos; /* current position when loading the index */
63 * Type indexes.
65 * Each bitmap marks which objects in the packfile are of the given
66 * type. This provides type information when yielding the objects from
67 * the packfile during a walk, which allows for better delta bases.
69 struct ewah_bitmap *commits;
70 struct ewah_bitmap *trees;
71 struct ewah_bitmap *blobs;
72 struct ewah_bitmap *tags;
74 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
75 kh_oid_map_t *bitmaps;
77 /* Number of bitmapped commits */
78 uint32_t entry_count;
80 /* If not NULL, this is a name-hash cache pointing into map. */
81 uint32_t *hashes;
83 /* The checksum of the packfile or MIDX; points into map. */
84 const unsigned char *checksum;
87 * If not NULL, this point into the commit table extension
88 * (within the memory mapped region `map`).
90 unsigned char *table_lookup;
93 * Extended index.
95 * When trying to perform bitmap operations with objects that are not
96 * packed in `pack`, these objects are added to this "fake index" and
97 * are assumed to appear at the end of the packfile for all operations
99 struct eindex {
100 struct object **objects;
101 uint32_t *hashes;
102 uint32_t count, alloc;
103 kh_oid_pos_t *positions;
104 } ext_index;
106 /* Bitmap result of the last performed walk */
107 struct bitmap *result;
109 /* "have" bitmap from the last performed walk */
110 struct bitmap *haves;
112 /* Version of the bitmap index */
113 unsigned int version;
116 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
118 struct ewah_bitmap *parent;
119 struct ewah_bitmap *composed;
121 if (!st->xor)
122 return st->root;
124 composed = ewah_pool_new();
125 parent = lookup_stored_bitmap(st->xor);
126 ewah_xor(st->root, parent, composed);
128 ewah_pool_free(st->root);
129 st->root = composed;
130 st->xor = NULL;
132 return composed;
136 * Read a bitmap from the current read position on the mmaped
137 * index, and increase the read position accordingly
139 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
141 struct ewah_bitmap *b = ewah_pool_new();
143 ssize_t bitmap_size = ewah_read_mmap(b,
144 index->map + index->map_pos,
145 index->map_size - index->map_pos);
147 if (bitmap_size < 0) {
148 error(_("failed to load bitmap index (corrupted?)"));
149 ewah_pool_free(b);
150 return NULL;
153 index->map_pos += bitmap_size;
154 return b;
157 static uint32_t bitmap_num_objects(struct bitmap_index *index)
159 if (index->midx)
160 return index->midx->num_objects;
161 return index->pack->num_objects;
164 static int load_bitmap_header(struct bitmap_index *index)
166 struct bitmap_disk_header *header = (void *)index->map;
167 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
169 if (index->map_size < header_size + the_hash_algo->rawsz)
170 return error(_("corrupted bitmap index (too small)"));
172 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
173 return error(_("corrupted bitmap index file (wrong header)"));
175 index->version = ntohs(header->version);
176 if (index->version != 1)
177 return error(_("unsupported version '%d' for bitmap index file"), index->version);
179 /* Parse known bitmap format options */
181 uint32_t flags = ntohs(header->options);
182 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
183 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
185 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
186 BUG("unsupported options for bitmap index file "
187 "(Git requires BITMAP_OPT_FULL_DAG)");
189 if (flags & BITMAP_OPT_HASH_CACHE) {
190 if (cache_size > index_end - index->map - header_size)
191 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
192 index->hashes = (void *)(index_end - cache_size);
193 index_end -= cache_size;
196 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
197 size_t table_size = st_mult(ntohl(header->entry_count),
198 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
199 if (table_size > index_end - index->map - header_size)
200 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
201 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
202 index->table_lookup = (void *)(index_end - table_size);
203 index_end -= table_size;
207 index->entry_count = ntohl(header->entry_count);
208 index->checksum = header->checksum;
209 index->map_pos += header_size;
210 return 0;
213 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
214 struct ewah_bitmap *root,
215 const struct object_id *oid,
216 struct stored_bitmap *xor_with,
217 int flags)
219 struct stored_bitmap *stored;
220 khiter_t hash_pos;
221 int ret;
223 stored = xmalloc(sizeof(struct stored_bitmap));
224 stored->root = root;
225 stored->xor = xor_with;
226 stored->flags = flags;
227 oidcpy(&stored->oid, oid);
229 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
232 * A 0 return code means the insertion succeeded with no changes,
233 * because the SHA1 already existed on the map. This is bad, there
234 * shouldn't be duplicated commits in the index.
236 if (ret == 0) {
237 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
238 return NULL;
241 kh_value(index->bitmaps, hash_pos) = stored;
242 return stored;
245 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
247 uint32_t result = get_be32(buffer + *pos);
248 (*pos) += sizeof(result);
249 return result;
252 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
254 return buffer[(*pos)++];
257 #define MAX_XOR_OFFSET 160
259 static int nth_bitmap_object_oid(struct bitmap_index *index,
260 struct object_id *oid,
261 uint32_t n)
263 if (index->midx)
264 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
265 return nth_packed_object_id(oid, index->pack, n);
268 static int load_bitmap_entries_v1(struct bitmap_index *index)
270 uint32_t i;
271 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
273 for (i = 0; i < index->entry_count; ++i) {
274 int xor_offset, flags;
275 struct ewah_bitmap *bitmap = NULL;
276 struct stored_bitmap *xor_bitmap = NULL;
277 uint32_t commit_idx_pos;
278 struct object_id oid;
280 if (index->map_size - index->map_pos < 6)
281 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
283 commit_idx_pos = read_be32(index->map, &index->map_pos);
284 xor_offset = read_u8(index->map, &index->map_pos);
285 flags = read_u8(index->map, &index->map_pos);
287 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
288 return error(_("corrupt ewah bitmap: commit index %u out of range"),
289 (unsigned)commit_idx_pos);
291 bitmap = read_bitmap_1(index);
292 if (!bitmap)
293 return -1;
295 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
296 return error(_("corrupted bitmap pack index"));
298 if (xor_offset > 0) {
299 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
301 if (!xor_bitmap)
302 return error(_("invalid XOR offset in bitmap pack index"));
305 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
306 index, bitmap, &oid, xor_bitmap, flags);
309 return 0;
312 char *midx_bitmap_filename(struct multi_pack_index *midx)
314 struct strbuf buf = STRBUF_INIT;
316 get_midx_filename(&buf, midx->object_dir);
317 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
319 return strbuf_detach(&buf, NULL);
322 char *pack_bitmap_filename(struct packed_git *p)
324 size_t len;
326 if (!strip_suffix(p->pack_name, ".pack", &len))
327 BUG("pack_name does not end in .pack");
328 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
331 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
332 struct multi_pack_index *midx)
334 struct stat st;
335 char *bitmap_name = midx_bitmap_filename(midx);
336 int fd = git_open(bitmap_name);
337 uint32_t i;
338 struct packed_git *preferred;
340 if (fd < 0) {
341 if (errno != ENOENT)
342 warning_errno("cannot open '%s'", bitmap_name);
343 free(bitmap_name);
344 return -1;
346 free(bitmap_name);
348 if (fstat(fd, &st)) {
349 error_errno(_("cannot fstat bitmap file"));
350 close(fd);
351 return -1;
354 if (bitmap_git->pack || bitmap_git->midx) {
355 struct strbuf buf = STRBUF_INIT;
356 get_midx_filename(&buf, midx->object_dir);
357 trace2_data_string("bitmap", the_repository,
358 "ignoring extra midx bitmap file", buf.buf);
359 close(fd);
360 strbuf_release(&buf);
361 return -1;
364 bitmap_git->midx = midx;
365 bitmap_git->map_size = xsize_t(st.st_size);
366 bitmap_git->map_pos = 0;
367 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
368 MAP_PRIVATE, fd, 0);
369 close(fd);
371 if (load_bitmap_header(bitmap_git) < 0)
372 goto cleanup;
374 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
375 error(_("checksum doesn't match in MIDX and bitmap"));
376 goto cleanup;
379 if (load_midx_revindex(bitmap_git->midx) < 0) {
380 warning(_("multi-pack bitmap is missing required reverse index"));
381 goto cleanup;
384 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
385 if (prepare_midx_pack(the_repository, bitmap_git->midx, i))
386 die(_("could not open pack %s"),
387 bitmap_git->midx->pack_names[i]);
390 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
391 if (!is_pack_valid(preferred)) {
392 warning(_("preferred pack (%s) is invalid"),
393 preferred->pack_name);
394 goto cleanup;
397 return 0;
399 cleanup:
400 munmap(bitmap_git->map, bitmap_git->map_size);
401 bitmap_git->map_size = 0;
402 bitmap_git->map_pos = 0;
403 bitmap_git->map = NULL;
404 bitmap_git->midx = NULL;
405 return -1;
408 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
410 int fd;
411 struct stat st;
412 char *bitmap_name;
414 bitmap_name = pack_bitmap_filename(packfile);
415 fd = git_open(bitmap_name);
417 if (fd < 0) {
418 if (errno != ENOENT)
419 warning_errno("cannot open '%s'", bitmap_name);
420 free(bitmap_name);
421 return -1;
423 free(bitmap_name);
425 if (fstat(fd, &st)) {
426 error_errno(_("cannot fstat bitmap file"));
427 close(fd);
428 return -1;
431 if (bitmap_git->pack || bitmap_git->midx) {
432 trace2_data_string("bitmap", the_repository,
433 "ignoring extra bitmap file", packfile->pack_name);
434 close(fd);
435 return -1;
438 if (!is_pack_valid(packfile)) {
439 close(fd);
440 return -1;
443 bitmap_git->pack = packfile;
444 bitmap_git->map_size = xsize_t(st.st_size);
445 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
446 bitmap_git->map_pos = 0;
447 close(fd);
449 if (load_bitmap_header(bitmap_git) < 0) {
450 munmap(bitmap_git->map, bitmap_git->map_size);
451 bitmap_git->map = NULL;
452 bitmap_git->map_size = 0;
453 bitmap_git->map_pos = 0;
454 bitmap_git->pack = NULL;
455 return -1;
458 trace2_data_string("bitmap", the_repository, "opened bitmap file",
459 packfile->pack_name);
460 return 0;
463 static int load_reverse_index(struct bitmap_index *bitmap_git)
465 if (bitmap_is_midx(bitmap_git)) {
466 uint32_t i;
467 int ret;
470 * The multi-pack-index's .rev file is already loaded via
471 * open_pack_bitmap_1().
473 * But we still need to open the individual pack .rev files,
474 * since we will need to make use of them in pack-objects.
476 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
477 ret = load_pack_revindex(bitmap_git->midx->packs[i]);
478 if (ret)
479 return ret;
481 return 0;
483 return load_pack_revindex(bitmap_git->pack);
486 static int load_bitmap(struct bitmap_index *bitmap_git)
488 assert(bitmap_git->map);
490 bitmap_git->bitmaps = kh_init_oid_map();
491 bitmap_git->ext_index.positions = kh_init_oid_pos();
493 if (load_reverse_index(bitmap_git))
494 goto failed;
496 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
497 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
498 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
499 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
500 goto failed;
502 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
503 goto failed;
505 return 0;
507 failed:
508 munmap(bitmap_git->map, bitmap_git->map_size);
509 bitmap_git->map = NULL;
510 bitmap_git->map_size = 0;
512 kh_destroy_oid_map(bitmap_git->bitmaps);
513 bitmap_git->bitmaps = NULL;
515 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
516 bitmap_git->ext_index.positions = NULL;
518 return -1;
521 static int open_pack_bitmap(struct repository *r,
522 struct bitmap_index *bitmap_git)
524 struct packed_git *p;
525 int ret = -1;
527 for (p = get_all_packs(r); p; p = p->next) {
528 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
529 ret = 0;
531 * The only reason to keep looking is to report
532 * duplicates.
534 if (!trace2_is_enabled())
535 break;
539 return ret;
542 static int open_midx_bitmap(struct repository *r,
543 struct bitmap_index *bitmap_git)
545 int ret = -1;
546 struct multi_pack_index *midx;
548 assert(!bitmap_git->map);
550 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
551 if (!open_midx_bitmap_1(bitmap_git, midx))
552 ret = 0;
554 return ret;
557 static int open_bitmap(struct repository *r,
558 struct bitmap_index *bitmap_git)
560 int found;
562 assert(!bitmap_git->map);
564 found = !open_midx_bitmap(r, bitmap_git);
567 * these will all be skipped if we opened a midx bitmap; but run it
568 * anyway if tracing is enabled to report the duplicates
570 if (!found || trace2_is_enabled())
571 found |= !open_pack_bitmap(r, bitmap_git);
573 return found ? 0 : -1;
576 struct bitmap_index *prepare_bitmap_git(struct repository *r)
578 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
580 if (!open_bitmap(r, bitmap_git) && !load_bitmap(bitmap_git))
581 return bitmap_git;
583 free_bitmap_index(bitmap_git);
584 return NULL;
587 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
589 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
591 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(bitmap_git))
592 return bitmap_git;
594 free_bitmap_index(bitmap_git);
595 return NULL;
598 struct include_data {
599 struct bitmap_index *bitmap_git;
600 struct bitmap *base;
601 struct bitmap *seen;
604 struct bitmap_lookup_table_triplet {
605 uint32_t commit_pos;
606 uint64_t offset;
607 uint32_t xor_row;
610 struct bitmap_lookup_table_xor_item {
611 struct object_id oid;
612 uint64_t offset;
616 * Given a `triplet` struct pointer and pointer `p`, this
617 * function reads the triplet beginning at `p` into the struct.
618 * Note that this function assumes that there is enough memory
619 * left for filling the `triplet` struct from `p`.
621 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
622 const unsigned char *p)
624 if (!triplet)
625 return -1;
627 triplet->commit_pos = get_be32(p);
628 p += sizeof(uint32_t);
629 triplet->offset = get_be64(p);
630 p += sizeof(uint64_t);
631 triplet->xor_row = get_be32(p);
632 return 0;
636 * This function gets the raw triplet from `row`'th row in the
637 * lookup table and fills that data to the `triplet`.
639 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
640 uint32_t pos,
641 struct bitmap_lookup_table_triplet *triplet)
643 unsigned char *p = NULL;
644 if (pos >= bitmap_git->entry_count)
645 return error(_("corrupt bitmap lookup table: triplet position out of index"));
647 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
649 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
653 * Searches for a matching triplet. `commit_pos` is a pointer
654 * to the wanted commit position value. `table_entry` points to
655 * a triplet in lookup table. The first 4 bytes of each
656 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
658 static int triplet_cmp(const void *commit_pos, const void *table_entry)
661 uint32_t a = *(uint32_t *)commit_pos;
662 uint32_t b = get_be32(table_entry);
663 if (a > b)
664 return 1;
665 else if (a < b)
666 return -1;
668 return 0;
671 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
672 struct object_id *oid,
673 uint32_t *result)
675 int found;
677 if (bitmap_is_midx(bitmap_git))
678 found = bsearch_midx(oid, bitmap_git->midx, result);
679 else
680 found = bsearch_pack(oid, bitmap_git->pack, result);
682 return found;
686 * `bsearch_triplet_by_pos` function searches for the raw triplet
687 * having commit position same as `commit_pos` and fills `triplet`
688 * object from the raw triplet. Returns 1 on success and 0 on
689 * failure.
691 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
692 struct bitmap_index *bitmap_git,
693 struct bitmap_lookup_table_triplet *triplet)
695 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
696 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
698 if (!p)
699 return -1;
701 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
704 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
705 struct commit *commit)
707 uint32_t commit_pos, xor_row;
708 uint64_t offset;
709 int flags;
710 struct bitmap_lookup_table_triplet triplet;
711 struct object_id *oid = &commit->object.oid;
712 struct ewah_bitmap *bitmap;
713 struct stored_bitmap *xor_bitmap = NULL;
714 const int bitmap_header_size = 6;
715 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
716 static size_t xor_items_nr = 0, xor_items_alloc = 0;
717 static int is_corrupt = 0;
718 int xor_flags;
719 khiter_t hash_pos;
720 struct bitmap_lookup_table_xor_item *xor_item;
722 if (is_corrupt)
723 return NULL;
725 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
726 return NULL;
728 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
729 return NULL;
731 xor_items_nr = 0;
732 offset = triplet.offset;
733 xor_row = triplet.xor_row;
735 while (xor_row != 0xffffffff) {
736 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
738 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
739 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
740 goto corrupt;
743 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
744 goto corrupt;
746 xor_item = &xor_items[xor_items_nr];
747 xor_item->offset = triplet.offset;
749 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
750 error(_("corrupt bitmap lookup table: commit index %u out of range"),
751 triplet.commit_pos);
752 goto corrupt;
755 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
758 * If desired bitmap is already stored, we don't need
759 * to iterate further. Because we know that bitmaps
760 * that are needed to be parsed to parse this bitmap
761 * has already been stored. So, assign this stored bitmap
762 * to the xor_bitmap.
764 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
765 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
766 break;
767 xor_items_nr++;
768 xor_row = triplet.xor_row;
771 while (xor_items_nr) {
772 xor_item = &xor_items[xor_items_nr - 1];
773 bitmap_git->map_pos = xor_item->offset;
774 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
775 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
776 oid_to_hex(&xor_item->oid));
777 goto corrupt;
780 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
781 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
782 bitmap = read_bitmap_1(bitmap_git);
784 if (!bitmap)
785 goto corrupt;
787 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
788 xor_items_nr--;
791 bitmap_git->map_pos = offset;
792 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
793 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
794 oid_to_hex(oid));
795 goto corrupt;
799 * Don't bother reading the commit's index position or its xor
800 * offset:
802 * - The commit's index position is irrelevant to us, since
803 * load_bitmap_entries_v1 only uses it to learn the object
804 * id which is used to compute the hashmap's key. We already
805 * have an object id, so no need to look it up again.
807 * - The xor_offset is unusable for us, since it specifies how
808 * many entries previous to ours we should look at. This
809 * makes sense when reading the bitmaps sequentially (as in
810 * load_bitmap_entries_v1()), since we can keep track of
811 * each bitmap as we read them.
813 * But it can't work for us, since the bitmap's don't have a
814 * fixed size. So we learn the position of the xor'd bitmap
815 * from the commit table (and resolve it to a bitmap in the
816 * above if-statement).
818 * Instead, we can skip ahead and immediately read the flags and
819 * ewah bitmap.
821 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
822 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
823 bitmap = read_bitmap_1(bitmap_git);
825 if (!bitmap)
826 goto corrupt;
828 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
830 corrupt:
831 free(xor_items);
832 is_corrupt = 1;
833 return NULL;
836 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
837 struct commit *commit)
839 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
840 commit->object.oid);
841 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
842 struct stored_bitmap *bitmap = NULL;
843 if (!bitmap_git->table_lookup)
844 return NULL;
846 /* this is a fairly hot codepath - no trace2_region please */
847 /* NEEDSWORK: cache misses aren't recorded */
848 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
849 if (!bitmap)
850 return NULL;
851 return lookup_stored_bitmap(bitmap);
853 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
856 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
857 const struct object_id *oid)
859 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
860 khiter_t pos = kh_get_oid_pos(positions, *oid);
862 if (pos < kh_end(positions)) {
863 int bitmap_pos = kh_value(positions, pos);
864 return bitmap_pos + bitmap_num_objects(bitmap_git);
867 return -1;
870 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
871 const struct object_id *oid)
873 uint32_t pos;
874 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
875 if (!offset)
876 return -1;
878 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
879 return -1;
880 return pos;
883 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
884 const struct object_id *oid)
886 uint32_t want, got;
887 if (!bsearch_midx(oid, bitmap_git->midx, &want))
888 return -1;
890 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
891 return -1;
892 return got;
895 static int bitmap_position(struct bitmap_index *bitmap_git,
896 const struct object_id *oid)
898 int pos;
899 if (bitmap_is_midx(bitmap_git))
900 pos = bitmap_position_midx(bitmap_git, oid);
901 else
902 pos = bitmap_position_packfile(bitmap_git, oid);
903 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
906 static int ext_index_add_object(struct bitmap_index *bitmap_git,
907 struct object *object, const char *name)
909 struct eindex *eindex = &bitmap_git->ext_index;
911 khiter_t hash_pos;
912 int hash_ret;
913 int bitmap_pos;
915 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
916 if (hash_ret > 0) {
917 if (eindex->count >= eindex->alloc) {
918 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
919 REALLOC_ARRAY(eindex->objects, eindex->alloc);
920 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
923 bitmap_pos = eindex->count;
924 eindex->objects[eindex->count] = object;
925 eindex->hashes[eindex->count] = pack_name_hash(name);
926 kh_value(eindex->positions, hash_pos) = bitmap_pos;
927 eindex->count++;
928 } else {
929 bitmap_pos = kh_value(eindex->positions, hash_pos);
932 return bitmap_pos + bitmap_num_objects(bitmap_git);
935 struct bitmap_show_data {
936 struct bitmap_index *bitmap_git;
937 struct bitmap *base;
940 static void show_object(struct object *object, const char *name, void *data_)
942 struct bitmap_show_data *data = data_;
943 int bitmap_pos;
945 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
947 if (bitmap_pos < 0)
948 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
949 name);
951 bitmap_set(data->base, bitmap_pos);
954 static void show_commit(struct commit *commit, void *data)
958 static int add_to_include_set(struct bitmap_index *bitmap_git,
959 struct include_data *data,
960 struct commit *commit,
961 int bitmap_pos)
963 struct ewah_bitmap *partial;
965 if (data->seen && bitmap_get(data->seen, bitmap_pos))
966 return 0;
968 if (bitmap_get(data->base, bitmap_pos))
969 return 0;
971 partial = bitmap_for_commit(bitmap_git, commit);
972 if (partial) {
973 bitmap_or_ewah(data->base, partial);
974 return 0;
977 bitmap_set(data->base, bitmap_pos);
978 return 1;
981 static int should_include(struct commit *commit, void *_data)
983 struct include_data *data = _data;
984 int bitmap_pos;
986 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
987 if (bitmap_pos < 0)
988 bitmap_pos = ext_index_add_object(data->bitmap_git,
989 (struct object *)commit,
990 NULL);
992 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
993 struct commit_list *parent = commit->parents;
995 while (parent) {
996 parent->item->object.flags |= SEEN;
997 parent = parent->next;
1000 return 0;
1003 return 1;
1006 static int should_include_obj(struct object *obj, void *_data)
1008 struct include_data *data = _data;
1009 int bitmap_pos;
1011 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1012 if (bitmap_pos < 0)
1013 return 1;
1014 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1015 bitmap_get(data->base, bitmap_pos)) {
1016 obj->flags |= SEEN;
1017 return 0;
1019 return 1;
1022 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1023 struct bitmap **base,
1024 struct commit *commit)
1026 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1028 if (!or_with)
1029 return 0;
1031 if (!*base)
1032 *base = ewah_to_bitmap(or_with);
1033 else
1034 bitmap_or_ewah(*base, or_with);
1036 return 1;
1039 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1040 struct rev_info *revs,
1041 struct object_list *roots,
1042 struct bitmap *seen)
1044 struct bitmap *base = NULL;
1045 int needs_walk = 0;
1047 struct object_list *not_mapped = NULL;
1050 * Go through all the roots for the walk. The ones that have bitmaps
1051 * on the bitmap index will be `or`ed together to form an initial
1052 * global reachability analysis.
1054 * The ones without bitmaps in the index will be stored in the
1055 * `not_mapped_list` for further processing.
1057 while (roots) {
1058 struct object *object = roots->item;
1059 roots = roots->next;
1061 if (object->type == OBJ_COMMIT &&
1062 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1063 object->flags |= SEEN;
1064 continue;
1067 object_list_insert(object, &not_mapped);
1071 * Best case scenario: We found bitmaps for all the roots,
1072 * so the resulting `or` bitmap has the full reachability analysis
1074 if (!not_mapped)
1075 return base;
1077 roots = not_mapped;
1080 * Let's iterate through all the roots that don't have bitmaps to
1081 * check if we can determine them to be reachable from the existing
1082 * global bitmap.
1084 * If we cannot find them in the existing global bitmap, we'll need
1085 * to push them to an actual walk and run it until we can confirm
1086 * they are reachable
1088 while (roots) {
1089 struct object *object = roots->item;
1090 int pos;
1092 roots = roots->next;
1093 pos = bitmap_position(bitmap_git, &object->oid);
1095 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1096 object->flags &= ~UNINTERESTING;
1097 add_pending_object(revs, object, "");
1098 needs_walk = 1;
1099 } else {
1100 object->flags |= SEEN;
1104 if (needs_walk) {
1105 struct include_data incdata;
1106 struct bitmap_show_data show_data;
1108 if (!base)
1109 base = bitmap_new();
1111 incdata.bitmap_git = bitmap_git;
1112 incdata.base = base;
1113 incdata.seen = seen;
1115 revs->include_check = should_include;
1116 revs->include_check_obj = should_include_obj;
1117 revs->include_check_data = &incdata;
1119 if (prepare_revision_walk(revs))
1120 die(_("revision walk setup failed"));
1122 show_data.bitmap_git = bitmap_git;
1123 show_data.base = base;
1125 traverse_commit_list(revs,
1126 show_commit, show_object,
1127 &show_data);
1129 revs->include_check = NULL;
1130 revs->include_check_obj = NULL;
1131 revs->include_check_data = NULL;
1134 return base;
1137 static void show_extended_objects(struct bitmap_index *bitmap_git,
1138 struct rev_info *revs,
1139 show_reachable_fn show_reach)
1141 struct bitmap *objects = bitmap_git->result;
1142 struct eindex *eindex = &bitmap_git->ext_index;
1143 uint32_t i;
1145 for (i = 0; i < eindex->count; ++i) {
1146 struct object *obj;
1148 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1149 continue;
1151 obj = eindex->objects[i];
1152 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1153 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1154 (obj->type == OBJ_TAG && !revs->tag_objects))
1155 continue;
1157 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1161 static void init_type_iterator(struct ewah_iterator *it,
1162 struct bitmap_index *bitmap_git,
1163 enum object_type type)
1165 switch (type) {
1166 case OBJ_COMMIT:
1167 ewah_iterator_init(it, bitmap_git->commits);
1168 break;
1170 case OBJ_TREE:
1171 ewah_iterator_init(it, bitmap_git->trees);
1172 break;
1174 case OBJ_BLOB:
1175 ewah_iterator_init(it, bitmap_git->blobs);
1176 break;
1178 case OBJ_TAG:
1179 ewah_iterator_init(it, bitmap_git->tags);
1180 break;
1182 default:
1183 BUG("object type %d not stored by bitmap type index", type);
1184 break;
1188 static void show_objects_for_type(
1189 struct bitmap_index *bitmap_git,
1190 enum object_type object_type,
1191 show_reachable_fn show_reach)
1193 size_t i = 0;
1194 uint32_t offset;
1196 struct ewah_iterator it;
1197 eword_t filter;
1199 struct bitmap *objects = bitmap_git->result;
1201 init_type_iterator(&it, bitmap_git, object_type);
1203 for (i = 0; i < objects->word_alloc &&
1204 ewah_iterator_next(&filter, &it); i++) {
1205 eword_t word = objects->words[i] & filter;
1206 size_t pos = (i * BITS_IN_EWORD);
1208 if (!word)
1209 continue;
1211 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1212 struct packed_git *pack;
1213 struct object_id oid;
1214 uint32_t hash = 0, index_pos;
1215 off_t ofs;
1217 if ((word >> offset) == 0)
1218 break;
1220 offset += ewah_bit_ctz64(word >> offset);
1222 if (bitmap_is_midx(bitmap_git)) {
1223 struct multi_pack_index *m = bitmap_git->midx;
1224 uint32_t pack_id;
1226 index_pos = pack_pos_to_midx(m, pos + offset);
1227 ofs = nth_midxed_offset(m, index_pos);
1228 nth_midxed_object_oid(&oid, m, index_pos);
1230 pack_id = nth_midxed_pack_int_id(m, index_pos);
1231 pack = bitmap_git->midx->packs[pack_id];
1232 } else {
1233 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1234 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1235 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1237 pack = bitmap_git->pack;
1240 if (bitmap_git->hashes)
1241 hash = get_be32(bitmap_git->hashes + index_pos);
1243 show_reach(&oid, object_type, 0, hash, pack, ofs);
1248 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1249 struct object_list *roots)
1251 while (roots) {
1252 struct object *object = roots->item;
1253 roots = roots->next;
1255 if (bitmap_is_midx(bitmap_git)) {
1256 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1257 return 1;
1258 } else {
1259 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1260 return 1;
1264 return 0;
1267 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1268 struct object_list *tip_objects,
1269 enum object_type type)
1271 struct bitmap *result = bitmap_new();
1272 struct object_list *p;
1274 for (p = tip_objects; p; p = p->next) {
1275 int pos;
1277 if (p->item->type != type)
1278 continue;
1280 pos = bitmap_position(bitmap_git, &p->item->oid);
1281 if (pos < 0)
1282 continue;
1284 bitmap_set(result, pos);
1287 return result;
1290 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1291 struct object_list *tip_objects,
1292 struct bitmap *to_filter,
1293 enum object_type type)
1295 struct eindex *eindex = &bitmap_git->ext_index;
1296 struct bitmap *tips;
1297 struct ewah_iterator it;
1298 eword_t mask;
1299 uint32_t i;
1302 * The non-bitmap version of this filter never removes
1303 * objects which the other side specifically asked for,
1304 * so we must match that behavior.
1306 tips = find_tip_objects(bitmap_git, tip_objects, type);
1309 * We can use the type-level bitmap for 'type' to work in whole
1310 * words for the objects that are actually in the bitmapped
1311 * packfile.
1313 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1314 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1315 i++) {
1316 if (i < tips->word_alloc)
1317 mask &= ~tips->words[i];
1318 to_filter->words[i] &= ~mask;
1322 * Clear any objects that weren't in the packfile (and so would
1323 * not have been caught by the loop above. We'll have to check
1324 * them individually.
1326 for (i = 0; i < eindex->count; i++) {
1327 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1328 if (eindex->objects[i]->type == type &&
1329 bitmap_get(to_filter, pos) &&
1330 !bitmap_get(tips, pos))
1331 bitmap_unset(to_filter, pos);
1334 bitmap_free(tips);
1337 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1338 struct object_list *tip_objects,
1339 struct bitmap *to_filter)
1341 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1342 OBJ_BLOB);
1345 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1346 uint32_t pos)
1348 unsigned long size;
1349 struct object_info oi = OBJECT_INFO_INIT;
1351 oi.sizep = &size;
1353 if (pos < bitmap_num_objects(bitmap_git)) {
1354 struct packed_git *pack;
1355 off_t ofs;
1357 if (bitmap_is_midx(bitmap_git)) {
1358 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1359 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1361 pack = bitmap_git->midx->packs[pack_id];
1362 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1363 } else {
1364 pack = bitmap_git->pack;
1365 ofs = pack_pos_to_offset(pack, pos);
1368 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1369 struct object_id oid;
1370 nth_bitmap_object_oid(bitmap_git, &oid,
1371 pack_pos_to_index(pack, pos));
1372 die(_("unable to get size of %s"), oid_to_hex(&oid));
1374 } else {
1375 struct eindex *eindex = &bitmap_git->ext_index;
1376 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1377 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1378 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1381 return size;
1384 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1385 struct object_list *tip_objects,
1386 struct bitmap *to_filter,
1387 unsigned long limit)
1389 struct eindex *eindex = &bitmap_git->ext_index;
1390 struct bitmap *tips;
1391 struct ewah_iterator it;
1392 eword_t mask;
1393 uint32_t i;
1395 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1397 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1398 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1399 i++) {
1400 eword_t word = to_filter->words[i] & mask;
1401 unsigned offset;
1403 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1404 uint32_t pos;
1406 if ((word >> offset) == 0)
1407 break;
1408 offset += ewah_bit_ctz64(word >> offset);
1409 pos = i * BITS_IN_EWORD + offset;
1411 if (!bitmap_get(tips, pos) &&
1412 get_size_by_pos(bitmap_git, pos) >= limit)
1413 bitmap_unset(to_filter, pos);
1417 for (i = 0; i < eindex->count; i++) {
1418 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1419 if (eindex->objects[i]->type == OBJ_BLOB &&
1420 bitmap_get(to_filter, pos) &&
1421 !bitmap_get(tips, pos) &&
1422 get_size_by_pos(bitmap_git, pos) >= limit)
1423 bitmap_unset(to_filter, pos);
1426 bitmap_free(tips);
1429 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1430 struct object_list *tip_objects,
1431 struct bitmap *to_filter,
1432 unsigned long limit)
1434 if (limit)
1435 BUG("filter_bitmap_tree_depth given non-zero limit");
1437 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1438 OBJ_TREE);
1439 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1440 OBJ_BLOB);
1443 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1444 struct object_list *tip_objects,
1445 struct bitmap *to_filter,
1446 enum object_type object_type)
1448 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1449 BUG("filter_bitmap_object_type given invalid object");
1451 if (object_type != OBJ_TAG)
1452 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1453 if (object_type != OBJ_COMMIT)
1454 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1455 if (object_type != OBJ_TREE)
1456 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1457 if (object_type != OBJ_BLOB)
1458 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1461 static int filter_bitmap(struct bitmap_index *bitmap_git,
1462 struct object_list *tip_objects,
1463 struct bitmap *to_filter,
1464 struct list_objects_filter_options *filter)
1466 if (!filter || filter->choice == LOFC_DISABLED)
1467 return 0;
1469 if (filter->choice == LOFC_BLOB_NONE) {
1470 if (bitmap_git)
1471 filter_bitmap_blob_none(bitmap_git, tip_objects,
1472 to_filter);
1473 return 0;
1476 if (filter->choice == LOFC_BLOB_LIMIT) {
1477 if (bitmap_git)
1478 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1479 to_filter,
1480 filter->blob_limit_value);
1481 return 0;
1484 if (filter->choice == LOFC_TREE_DEPTH &&
1485 filter->tree_exclude_depth == 0) {
1486 if (bitmap_git)
1487 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1488 to_filter,
1489 filter->tree_exclude_depth);
1490 return 0;
1493 if (filter->choice == LOFC_OBJECT_TYPE) {
1494 if (bitmap_git)
1495 filter_bitmap_object_type(bitmap_git, tip_objects,
1496 to_filter,
1497 filter->object_type);
1498 return 0;
1501 if (filter->choice == LOFC_COMBINE) {
1502 int i;
1503 for (i = 0; i < filter->sub_nr; i++) {
1504 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1505 &filter->sub[i]) < 0)
1506 return -1;
1508 return 0;
1511 /* filter choice not handled */
1512 return -1;
1515 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1517 return !filter_bitmap(NULL, NULL, NULL, filter);
1520 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1521 int filter_provided_objects)
1523 unsigned int i;
1525 struct object_list *wants = NULL;
1526 struct object_list *haves = NULL;
1528 struct bitmap *wants_bitmap = NULL;
1529 struct bitmap *haves_bitmap = NULL;
1531 struct bitmap_index *bitmap_git;
1534 * We can't do pathspec limiting with bitmaps, because we don't know
1535 * which commits are associated with which object changes (let alone
1536 * even which objects are associated with which paths).
1538 if (revs->prune)
1539 return NULL;
1541 if (!can_filter_bitmap(&revs->filter))
1542 return NULL;
1544 /* try to open a bitmapped pack, but don't parse it yet
1545 * because we may not need to use it */
1546 CALLOC_ARRAY(bitmap_git, 1);
1547 if (open_bitmap(revs->repo, bitmap_git) < 0)
1548 goto cleanup;
1550 for (i = 0; i < revs->pending.nr; ++i) {
1551 struct object *object = revs->pending.objects[i].item;
1553 if (object->type == OBJ_NONE)
1554 parse_object_or_die(&object->oid, NULL);
1556 while (object->type == OBJ_TAG) {
1557 struct tag *tag = (struct tag *) object;
1559 if (object->flags & UNINTERESTING)
1560 object_list_insert(object, &haves);
1561 else
1562 object_list_insert(object, &wants);
1564 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1565 object->flags |= (tag->object.flags & UNINTERESTING);
1568 if (object->flags & UNINTERESTING)
1569 object_list_insert(object, &haves);
1570 else
1571 object_list_insert(object, &wants);
1575 * if we have a HAVES list, but none of those haves is contained
1576 * in the packfile that has a bitmap, we don't have anything to
1577 * optimize here
1579 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1580 goto cleanup;
1582 /* if we don't want anything, we're done here */
1583 if (!wants)
1584 goto cleanup;
1587 * now we're going to use bitmaps, so load the actual bitmap entries
1588 * from disk. this is the point of no return; after this the rev_list
1589 * becomes invalidated and we must perform the revwalk through bitmaps
1591 if (load_bitmap(bitmap_git) < 0)
1592 goto cleanup;
1594 object_array_clear(&revs->pending);
1596 if (haves) {
1597 revs->ignore_missing_links = 1;
1598 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1599 reset_revision_walk();
1600 revs->ignore_missing_links = 0;
1602 if (!haves_bitmap)
1603 BUG("failed to perform bitmap walk");
1606 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1608 if (!wants_bitmap)
1609 BUG("failed to perform bitmap walk");
1611 if (haves_bitmap)
1612 bitmap_and_not(wants_bitmap, haves_bitmap);
1614 filter_bitmap(bitmap_git,
1615 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1616 wants_bitmap,
1617 &revs->filter);
1619 bitmap_git->result = wants_bitmap;
1620 bitmap_git->haves = haves_bitmap;
1622 object_list_free(&wants);
1623 object_list_free(&haves);
1625 return bitmap_git;
1627 cleanup:
1628 free_bitmap_index(bitmap_git);
1629 object_list_free(&wants);
1630 object_list_free(&haves);
1631 return NULL;
1635 * -1 means "stop trying further objects"; 0 means we may or may not have
1636 * reused, but you can keep feeding bits.
1638 static int try_partial_reuse(struct packed_git *pack,
1639 size_t pos,
1640 struct bitmap *reuse,
1641 struct pack_window **w_curs)
1643 off_t offset, delta_obj_offset;
1644 enum object_type type;
1645 unsigned long size;
1648 * try_partial_reuse() is called either on (a) objects in the
1649 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1650 * objects in the preferred pack of a multi-pack bitmap.
1651 * Importantly, the latter can pretend as if only a single pack
1652 * exists because:
1654 * - The first pack->num_objects bits of a MIDX bitmap are
1655 * reserved for the preferred pack, and
1657 * - Ties due to duplicate objects are always resolved in
1658 * favor of the preferred pack.
1660 * Therefore we do not need to ever ask the MIDX for its copy of
1661 * an object by OID, since it will always select it from the
1662 * preferred pack. Likewise, the selected copy of the base
1663 * object for any deltas will reside in the same pack.
1665 * This means that we can reuse pos when looking up the bit in
1666 * the reuse bitmap, too, since bits corresponding to the
1667 * preferred pack precede all bits from other packs.
1670 if (pos >= pack->num_objects)
1671 return -1; /* not actually in the pack or MIDX preferred pack */
1673 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1674 type = unpack_object_header(pack, w_curs, &offset, &size);
1675 if (type < 0)
1676 return -1; /* broken packfile, punt */
1678 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1679 off_t base_offset;
1680 uint32_t base_pos;
1683 * Find the position of the base object so we can look it up
1684 * in our bitmaps. If we can't come up with an offset, or if
1685 * that offset is not in the revidx, the pack is corrupt.
1686 * There's nothing we can do, so just punt on this object,
1687 * and the normal slow path will complain about it in
1688 * more detail.
1690 base_offset = get_delta_base(pack, w_curs, &offset, type,
1691 delta_obj_offset);
1692 if (!base_offset)
1693 return 0;
1694 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1695 return 0;
1698 * We assume delta dependencies always point backwards. This
1699 * lets us do a single pass, and is basically always true
1700 * due to the way OFS_DELTAs work. You would not typically
1701 * find REF_DELTA in a bitmapped pack, since we only bitmap
1702 * packs we write fresh, and OFS_DELTA is the default). But
1703 * let's double check to make sure the pack wasn't written with
1704 * odd parameters.
1706 if (base_pos >= pos)
1707 return 0;
1710 * And finally, if we're not sending the base as part of our
1711 * reuse chunk, then don't send this object either. The base
1712 * would come after us, along with other objects not
1713 * necessarily in the pack, which means we'd need to convert
1714 * to REF_DELTA on the fly. Better to just let the normal
1715 * object_entry code path handle it.
1717 if (!bitmap_get(reuse, base_pos))
1718 return 0;
1722 * If we got here, then the object is OK to reuse. Mark it.
1724 bitmap_set(reuse, pos);
1725 return 0;
1728 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1730 struct multi_pack_index *m = bitmap_git->midx;
1731 if (!m)
1732 BUG("midx_preferred_pack: requires non-empty MIDX");
1733 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1736 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1737 struct packed_git **packfile_out,
1738 uint32_t *entries,
1739 struct bitmap **reuse_out)
1741 struct packed_git *pack;
1742 struct bitmap *result = bitmap_git->result;
1743 struct bitmap *reuse;
1744 struct pack_window *w_curs = NULL;
1745 size_t i = 0;
1746 uint32_t offset;
1747 uint32_t objects_nr;
1749 assert(result);
1751 load_reverse_index(bitmap_git);
1753 if (bitmap_is_midx(bitmap_git))
1754 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1755 else
1756 pack = bitmap_git->pack;
1757 objects_nr = pack->num_objects;
1759 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1760 i++;
1763 * Don't mark objects not in the packfile or preferred pack. This bitmap
1764 * marks objects eligible for reuse, but the pack-reuse code only
1765 * understands how to reuse a single pack. Since the preferred pack is
1766 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1767 * we use it instead of another pack. In single-pack bitmaps, the choice
1768 * is made for us.
1770 if (i > objects_nr / BITS_IN_EWORD)
1771 i = objects_nr / BITS_IN_EWORD;
1773 reuse = bitmap_word_alloc(i);
1774 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1776 for (; i < result->word_alloc; ++i) {
1777 eword_t word = result->words[i];
1778 size_t pos = (i * BITS_IN_EWORD);
1780 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1781 if ((word >> offset) == 0)
1782 break;
1784 offset += ewah_bit_ctz64(word >> offset);
1785 if (try_partial_reuse(pack, pos + offset,
1786 reuse, &w_curs) < 0) {
1788 * try_partial_reuse indicated we couldn't reuse
1789 * any bits, so there is no point in trying more
1790 * bits in the current word, or any other words
1791 * in result.
1793 * Jump out of both loops to avoid future
1794 * unnecessary calls to try_partial_reuse.
1796 goto done;
1801 done:
1802 unuse_pack(&w_curs);
1804 *entries = bitmap_popcount(reuse);
1805 if (!*entries) {
1806 bitmap_free(reuse);
1807 return -1;
1811 * Drop any reused objects from the result, since they will not
1812 * need to be handled separately.
1814 bitmap_and_not(result, reuse);
1815 *packfile_out = pack;
1816 *reuse_out = reuse;
1817 return 0;
1820 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1821 struct bitmap *bitmap, const struct object_id *oid)
1823 int idx;
1825 if (!bitmap)
1826 return 0;
1828 idx = bitmap_position(bitmap_git, oid);
1829 return idx >= 0 && bitmap_get(bitmap, idx);
1832 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1833 struct rev_info *revs,
1834 show_reachable_fn show_reachable)
1836 assert(bitmap_git->result);
1838 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1839 if (revs->tree_objects)
1840 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1841 if (revs->blob_objects)
1842 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1843 if (revs->tag_objects)
1844 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1846 show_extended_objects(bitmap_git, revs, show_reachable);
1849 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1850 enum object_type type)
1852 struct bitmap *objects = bitmap_git->result;
1853 struct eindex *eindex = &bitmap_git->ext_index;
1855 uint32_t i = 0, count = 0;
1856 struct ewah_iterator it;
1857 eword_t filter;
1859 init_type_iterator(&it, bitmap_git, type);
1861 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1862 eword_t word = objects->words[i++] & filter;
1863 count += ewah_bit_popcount64(word);
1866 for (i = 0; i < eindex->count; ++i) {
1867 if (eindex->objects[i]->type == type &&
1868 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1869 count++;
1872 return count;
1875 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1876 uint32_t *commits, uint32_t *trees,
1877 uint32_t *blobs, uint32_t *tags)
1879 assert(bitmap_git->result);
1881 if (commits)
1882 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1884 if (trees)
1885 *trees = count_object_type(bitmap_git, OBJ_TREE);
1887 if (blobs)
1888 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1890 if (tags)
1891 *tags = count_object_type(bitmap_git, OBJ_TAG);
1894 struct bitmap_test_data {
1895 struct bitmap_index *bitmap_git;
1896 struct bitmap *base;
1897 struct bitmap *commits;
1898 struct bitmap *trees;
1899 struct bitmap *blobs;
1900 struct bitmap *tags;
1901 struct progress *prg;
1902 size_t seen;
1905 static void test_bitmap_type(struct bitmap_test_data *tdata,
1906 struct object *obj, int pos)
1908 enum object_type bitmap_type = OBJ_NONE;
1909 int bitmaps_nr = 0;
1911 if (bitmap_get(tdata->commits, pos)) {
1912 bitmap_type = OBJ_COMMIT;
1913 bitmaps_nr++;
1915 if (bitmap_get(tdata->trees, pos)) {
1916 bitmap_type = OBJ_TREE;
1917 bitmaps_nr++;
1919 if (bitmap_get(tdata->blobs, pos)) {
1920 bitmap_type = OBJ_BLOB;
1921 bitmaps_nr++;
1923 if (bitmap_get(tdata->tags, pos)) {
1924 bitmap_type = OBJ_TAG;
1925 bitmaps_nr++;
1928 if (bitmap_type == OBJ_NONE)
1929 die(_("object '%s' not found in type bitmaps"),
1930 oid_to_hex(&obj->oid));
1932 if (bitmaps_nr > 1)
1933 die(_("object '%s' does not have a unique type"),
1934 oid_to_hex(&obj->oid));
1936 if (bitmap_type != obj->type)
1937 die(_("object '%s': real type '%s', expected: '%s'"),
1938 oid_to_hex(&obj->oid),
1939 type_name(obj->type),
1940 type_name(bitmap_type));
1943 static void test_show_object(struct object *object, const char *name,
1944 void *data)
1946 struct bitmap_test_data *tdata = data;
1947 int bitmap_pos;
1949 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1950 if (bitmap_pos < 0)
1951 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
1952 test_bitmap_type(tdata, object, bitmap_pos);
1954 bitmap_set(tdata->base, bitmap_pos);
1955 display_progress(tdata->prg, ++tdata->seen);
1958 static void test_show_commit(struct commit *commit, void *data)
1960 struct bitmap_test_data *tdata = data;
1961 int bitmap_pos;
1963 bitmap_pos = bitmap_position(tdata->bitmap_git,
1964 &commit->object.oid);
1965 if (bitmap_pos < 0)
1966 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
1967 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1969 bitmap_set(tdata->base, bitmap_pos);
1970 display_progress(tdata->prg, ++tdata->seen);
1973 void test_bitmap_walk(struct rev_info *revs)
1975 struct object *root;
1976 struct bitmap *result = NULL;
1977 size_t result_popcnt;
1978 struct bitmap_test_data tdata;
1979 struct bitmap_index *bitmap_git;
1980 struct ewah_bitmap *bm;
1982 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1983 die(_("failed to load bitmap indexes"));
1985 if (revs->pending.nr != 1)
1986 die(_("you must specify exactly one commit to test"));
1988 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
1989 bitmap_git->version,
1990 bitmap_git->entry_count,
1991 bitmap_git->table_lookup ? "" : " loaded");
1993 root = revs->pending.objects[0].item;
1994 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
1996 if (bm) {
1997 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
1998 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2000 result = ewah_to_bitmap(bm);
2003 if (!result)
2004 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2006 revs->tag_objects = 1;
2007 revs->tree_objects = 1;
2008 revs->blob_objects = 1;
2010 result_popcnt = bitmap_popcount(result);
2012 if (prepare_revision_walk(revs))
2013 die(_("revision walk setup failed"));
2015 tdata.bitmap_git = bitmap_git;
2016 tdata.base = bitmap_new();
2017 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2018 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2019 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2020 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2021 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2022 tdata.seen = 0;
2024 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2026 stop_progress(&tdata.prg);
2028 if (bitmap_equals(result, tdata.base))
2029 fprintf_ln(stderr, "OK!");
2030 else
2031 die(_("mismatch in bitmap results"));
2033 bitmap_free(result);
2034 bitmap_free(tdata.base);
2035 bitmap_free(tdata.commits);
2036 bitmap_free(tdata.trees);
2037 bitmap_free(tdata.blobs);
2038 bitmap_free(tdata.tags);
2039 free_bitmap_index(bitmap_git);
2042 int test_bitmap_commits(struct repository *r)
2044 struct object_id oid;
2045 MAYBE_UNUSED void *value;
2046 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2048 if (!bitmap_git)
2049 die(_("failed to load bitmap indexes"));
2052 * As this function is only used to print bitmap selected
2053 * commits, we don't have to read the commit table.
2055 if (bitmap_git->table_lookup) {
2056 if (load_bitmap_entries_v1(bitmap_git) < 0)
2057 die(_("failed to load bitmap indexes"));
2060 kh_foreach(bitmap_git->bitmaps, oid, value, {
2061 printf_ln("%s", oid_to_hex(&oid));
2064 free_bitmap_index(bitmap_git);
2066 return 0;
2069 int test_bitmap_hashes(struct repository *r)
2071 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2072 struct object_id oid;
2073 uint32_t i, index_pos;
2075 if (!bitmap_git || !bitmap_git->hashes)
2076 goto cleanup;
2078 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2079 if (bitmap_is_midx(bitmap_git))
2080 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2081 else
2082 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2084 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2086 printf_ln("%s %"PRIu32"",
2087 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2090 cleanup:
2091 free_bitmap_index(bitmap_git);
2093 return 0;
2096 int rebuild_bitmap(const uint32_t *reposition,
2097 struct ewah_bitmap *source,
2098 struct bitmap *dest)
2100 uint32_t pos = 0;
2101 struct ewah_iterator it;
2102 eword_t word;
2104 ewah_iterator_init(&it, source);
2106 while (ewah_iterator_next(&word, &it)) {
2107 uint32_t offset, bit_pos;
2109 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2110 if ((word >> offset) == 0)
2111 break;
2113 offset += ewah_bit_ctz64(word >> offset);
2115 bit_pos = reposition[pos + offset];
2116 if (bit_pos > 0)
2117 bitmap_set(dest, bit_pos - 1);
2118 else /* can't reuse, we don't have the object */
2119 return -1;
2122 pos += BITS_IN_EWORD;
2124 return 0;
2127 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2128 struct packing_data *mapping)
2130 uint32_t i, num_objects;
2131 uint32_t *reposition;
2133 if (!bitmap_is_midx(bitmap_git))
2134 load_reverse_index(bitmap_git);
2135 else if (load_midx_revindex(bitmap_git->midx) < 0)
2136 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2137 "extension");
2139 num_objects = bitmap_num_objects(bitmap_git);
2140 CALLOC_ARRAY(reposition, num_objects);
2142 for (i = 0; i < num_objects; ++i) {
2143 struct object_id oid;
2144 struct object_entry *oe;
2145 uint32_t index_pos;
2147 if (bitmap_is_midx(bitmap_git))
2148 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2149 else
2150 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2151 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2152 oe = packlist_find(mapping, &oid);
2154 if (oe) {
2155 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2156 if (bitmap_git->hashes && !oe->hash)
2157 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2161 return reposition;
2164 void free_bitmap_index(struct bitmap_index *b)
2166 if (!b)
2167 return;
2169 if (b->map)
2170 munmap(b->map, b->map_size);
2171 ewah_pool_free(b->commits);
2172 ewah_pool_free(b->trees);
2173 ewah_pool_free(b->blobs);
2174 ewah_pool_free(b->tags);
2175 if (b->bitmaps) {
2176 struct stored_bitmap *sb;
2177 kh_foreach_value(b->bitmaps, sb, {
2178 ewah_pool_free(sb->root);
2179 free(sb);
2182 kh_destroy_oid_map(b->bitmaps);
2183 free(b->ext_index.objects);
2184 free(b->ext_index.hashes);
2185 kh_destroy_oid_pos(b->ext_index.positions);
2186 bitmap_free(b->result);
2187 bitmap_free(b->haves);
2188 if (bitmap_is_midx(b)) {
2190 * Multi-pack bitmaps need to have resources associated with
2191 * their on-disk reverse indexes unmapped so that stale .rev and
2192 * .bitmap files can be removed.
2194 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2195 * written in the same 'git multi-pack-index write --bitmap'
2196 * process. Close resources so they can be removed safely on
2197 * platforms like Windows.
2199 close_midx_revindex(b->midx);
2201 free(b);
2204 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2205 const struct object_id *oid)
2207 return bitmap_git &&
2208 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2211 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2212 enum object_type object_type)
2214 struct bitmap *result = bitmap_git->result;
2215 off_t total = 0;
2216 struct ewah_iterator it;
2217 eword_t filter;
2218 size_t i;
2220 init_type_iterator(&it, bitmap_git, object_type);
2221 for (i = 0; i < result->word_alloc &&
2222 ewah_iterator_next(&filter, &it); i++) {
2223 eword_t word = result->words[i] & filter;
2224 size_t base = (i * BITS_IN_EWORD);
2225 unsigned offset;
2227 if (!word)
2228 continue;
2230 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2231 if ((word >> offset) == 0)
2232 break;
2234 offset += ewah_bit_ctz64(word >> offset);
2236 if (bitmap_is_midx(bitmap_git)) {
2237 uint32_t pack_pos;
2238 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2239 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2241 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2242 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2244 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2245 struct object_id oid;
2246 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2248 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2249 oid_to_hex(&oid),
2250 pack->pack_name,
2251 (uintmax_t)offset);
2254 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2255 } else {
2256 size_t pos = base + offset;
2257 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2258 pack_pos_to_offset(bitmap_git->pack, pos);
2263 return total;
2266 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2268 struct bitmap *result = bitmap_git->result;
2269 struct eindex *eindex = &bitmap_git->ext_index;
2270 off_t total = 0;
2271 struct object_info oi = OBJECT_INFO_INIT;
2272 off_t object_size;
2273 size_t i;
2275 oi.disk_sizep = &object_size;
2277 for (i = 0; i < eindex->count; i++) {
2278 struct object *obj = eindex->objects[i];
2280 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2281 continue;
2283 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2284 die(_("unable to get disk usage of '%s'"),
2285 oid_to_hex(&obj->oid));
2287 total += object_size;
2289 return total;
2292 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2293 struct rev_info *revs)
2295 off_t total = 0;
2297 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2298 if (revs->tree_objects)
2299 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2300 if (revs->blob_objects)
2301 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2302 if (revs->tag_objects)
2303 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2305 total += get_disk_usage_for_extended(bitmap_git);
2307 return total;
2310 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2312 return !!bitmap_git->midx;
2315 const struct string_list *bitmap_preferred_tips(struct repository *r)
2317 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
2320 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2322 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2323 struct string_list_item *item;
2325 if (!preferred_tips)
2326 return 0;
2328 for_each_string_list_item(item, preferred_tips) {
2329 if (starts_with(refname, item->string))
2330 return 1;
2333 return 0;