cocci: remove 'unused.cocci'
[git.git] / pack-bitmap.c
blobb2e7d06d604ece888ea3ca0d7139f189d8134d2f
1 #include "cache.h"
2 #include "alloc.h"
3 #include "commit.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "strbuf.h"
7 #include "tag.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "progress.h"
11 #include "list-objects.h"
12 #include "pack.h"
13 #include "pack-bitmap.h"
14 #include "pack-revindex.h"
15 #include "pack-objects.h"
16 #include "packfile.h"
17 #include "repository.h"
18 #include "object-store.h"
19 #include "list-objects-filter-options.h"
20 #include "midx.h"
21 #include "config.h"
24 * An entry on the bitmap index, representing the bitmap for a given
25 * commit.
27 struct stored_bitmap {
28 struct object_id oid;
29 struct ewah_bitmap *root;
30 struct stored_bitmap *xor;
31 int flags;
35 * The active bitmap index for a repository. By design, repositories only have
36 * a single bitmap index available (the index for the biggest packfile in
37 * the repository), since bitmap indexes need full closure.
39 * If there is more than one bitmap index available (e.g. because of alternates),
40 * the active bitmap index is the largest one.
42 struct bitmap_index {
44 * The pack or multi-pack index (MIDX) that this bitmap index belongs
45 * to.
47 * Exactly one of these must be non-NULL; this specifies the object
48 * order used to interpret this bitmap.
50 struct packed_git *pack;
51 struct multi_pack_index *midx;
54 * Mark the first `reuse_objects` in the packfile as reused:
55 * they will be sent as-is without using them for repacking
56 * calculations
58 uint32_t reuse_objects;
60 /* mmapped buffer of the whole bitmap index */
61 unsigned char *map;
62 size_t map_size; /* size of the mmaped buffer */
63 size_t map_pos; /* current position when loading the index */
66 * Type indexes.
68 * Each bitmap marks which objects in the packfile are of the given
69 * type. This provides type information when yielding the objects from
70 * the packfile during a walk, which allows for better delta bases.
72 struct ewah_bitmap *commits;
73 struct ewah_bitmap *trees;
74 struct ewah_bitmap *blobs;
75 struct ewah_bitmap *tags;
77 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
78 kh_oid_map_t *bitmaps;
80 /* Number of bitmapped commits */
81 uint32_t entry_count;
83 /* If not NULL, this is a name-hash cache pointing into map. */
84 uint32_t *hashes;
86 /* The checksum of the packfile or MIDX; points into map. */
87 const unsigned char *checksum;
90 * If not NULL, this point into the commit table extension
91 * (within the memory mapped region `map`).
93 unsigned char *table_lookup;
96 * Extended index.
98 * When trying to perform bitmap operations with objects that are not
99 * packed in `pack`, these objects are added to this "fake index" and
100 * are assumed to appear at the end of the packfile for all operations
102 struct eindex {
103 struct object **objects;
104 uint32_t *hashes;
105 uint32_t count, alloc;
106 kh_oid_pos_t *positions;
107 } ext_index;
109 /* Bitmap result of the last performed walk */
110 struct bitmap *result;
112 /* "have" bitmap from the last performed walk */
113 struct bitmap *haves;
115 /* Version of the bitmap index */
116 unsigned int version;
119 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
121 struct ewah_bitmap *parent;
122 struct ewah_bitmap *composed;
124 if (!st->xor)
125 return st->root;
127 composed = ewah_pool_new();
128 parent = lookup_stored_bitmap(st->xor);
129 ewah_xor(st->root, parent, composed);
131 ewah_pool_free(st->root);
132 st->root = composed;
133 st->xor = NULL;
135 return composed;
139 * Read a bitmap from the current read position on the mmaped
140 * index, and increase the read position accordingly
142 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
144 struct ewah_bitmap *b = ewah_pool_new();
146 ssize_t bitmap_size = ewah_read_mmap(b,
147 index->map + index->map_pos,
148 index->map_size - index->map_pos);
150 if (bitmap_size < 0) {
151 error(_("failed to load bitmap index (corrupted?)"));
152 ewah_pool_free(b);
153 return NULL;
156 index->map_pos += bitmap_size;
157 return b;
160 static uint32_t bitmap_num_objects(struct bitmap_index *index)
162 if (index->midx)
163 return index->midx->num_objects;
164 return index->pack->num_objects;
167 static int load_bitmap_header(struct bitmap_index *index)
169 struct bitmap_disk_header *header = (void *)index->map;
170 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
172 if (index->map_size < header_size + the_hash_algo->rawsz)
173 return error(_("corrupted bitmap index (too small)"));
175 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
176 return error(_("corrupted bitmap index file (wrong header)"));
178 index->version = ntohs(header->version);
179 if (index->version != 1)
180 return error(_("unsupported version '%d' for bitmap index file"), index->version);
182 /* Parse known bitmap format options */
184 uint32_t flags = ntohs(header->options);
185 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
186 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
188 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
189 BUG("unsupported options for bitmap index file "
190 "(Git requires BITMAP_OPT_FULL_DAG)");
192 if (flags & BITMAP_OPT_HASH_CACHE) {
193 if (cache_size > index_end - index->map - header_size)
194 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
195 index->hashes = (void *)(index_end - cache_size);
196 index_end -= cache_size;
199 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
200 size_t table_size = st_mult(ntohl(header->entry_count),
201 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
202 if (table_size > index_end - index->map - header_size)
203 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
204 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
205 index->table_lookup = (void *)(index_end - table_size);
206 index_end -= table_size;
210 index->entry_count = ntohl(header->entry_count);
211 index->checksum = header->checksum;
212 index->map_pos += header_size;
213 return 0;
216 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
217 struct ewah_bitmap *root,
218 const struct object_id *oid,
219 struct stored_bitmap *xor_with,
220 int flags)
222 struct stored_bitmap *stored;
223 khiter_t hash_pos;
224 int ret;
226 stored = xmalloc(sizeof(struct stored_bitmap));
227 stored->root = root;
228 stored->xor = xor_with;
229 stored->flags = flags;
230 oidcpy(&stored->oid, oid);
232 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
235 * A 0 return code means the insertion succeeded with no changes,
236 * because the SHA1 already existed on the map. This is bad, there
237 * shouldn't be duplicated commits in the index.
239 if (ret == 0) {
240 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
241 return NULL;
244 kh_value(index->bitmaps, hash_pos) = stored;
245 return stored;
248 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
250 uint32_t result = get_be32(buffer + *pos);
251 (*pos) += sizeof(result);
252 return result;
255 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
257 return buffer[(*pos)++];
260 #define MAX_XOR_OFFSET 160
262 static int nth_bitmap_object_oid(struct bitmap_index *index,
263 struct object_id *oid,
264 uint32_t n)
266 if (index->midx)
267 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
268 return nth_packed_object_id(oid, index->pack, n);
271 static int load_bitmap_entries_v1(struct bitmap_index *index)
273 uint32_t i;
274 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
276 for (i = 0; i < index->entry_count; ++i) {
277 int xor_offset, flags;
278 struct ewah_bitmap *bitmap = NULL;
279 struct stored_bitmap *xor_bitmap = NULL;
280 uint32_t commit_idx_pos;
281 struct object_id oid;
283 if (index->map_size - index->map_pos < 6)
284 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
286 commit_idx_pos = read_be32(index->map, &index->map_pos);
287 xor_offset = read_u8(index->map, &index->map_pos);
288 flags = read_u8(index->map, &index->map_pos);
290 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
291 return error(_("corrupt ewah bitmap: commit index %u out of range"),
292 (unsigned)commit_idx_pos);
294 bitmap = read_bitmap_1(index);
295 if (!bitmap)
296 return -1;
298 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
299 return error(_("corrupted bitmap pack index"));
301 if (xor_offset > 0) {
302 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
304 if (!xor_bitmap)
305 return error(_("invalid XOR offset in bitmap pack index"));
308 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
309 index, bitmap, &oid, xor_bitmap, flags);
312 return 0;
315 char *midx_bitmap_filename(struct multi_pack_index *midx)
317 struct strbuf buf = STRBUF_INIT;
319 get_midx_filename(&buf, midx->object_dir);
320 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
322 return strbuf_detach(&buf, NULL);
325 char *pack_bitmap_filename(struct packed_git *p)
327 size_t len;
329 if (!strip_suffix(p->pack_name, ".pack", &len))
330 BUG("pack_name does not end in .pack");
331 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
334 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
335 struct multi_pack_index *midx)
337 struct stat st;
338 char *bitmap_name = midx_bitmap_filename(midx);
339 int fd = git_open(bitmap_name);
340 uint32_t i;
341 struct packed_git *preferred;
343 if (fd < 0) {
344 if (errno != ENOENT)
345 warning_errno("cannot open '%s'", bitmap_name);
346 free(bitmap_name);
347 return -1;
349 free(bitmap_name);
351 if (fstat(fd, &st)) {
352 error_errno(_("cannot fstat bitmap file"));
353 close(fd);
354 return -1;
357 if (bitmap_git->pack || bitmap_git->midx) {
358 struct strbuf buf = STRBUF_INIT;
359 get_midx_filename(&buf, midx->object_dir);
360 trace2_data_string("bitmap", the_repository,
361 "ignoring extra midx bitmap file", buf.buf);
362 close(fd);
363 strbuf_release(&buf);
364 return -1;
367 bitmap_git->midx = midx;
368 bitmap_git->map_size = xsize_t(st.st_size);
369 bitmap_git->map_pos = 0;
370 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
371 MAP_PRIVATE, fd, 0);
372 close(fd);
374 if (load_bitmap_header(bitmap_git) < 0)
375 goto cleanup;
377 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
378 error(_("checksum doesn't match in MIDX and bitmap"));
379 goto cleanup;
382 if (load_midx_revindex(bitmap_git->midx) < 0) {
383 warning(_("multi-pack bitmap is missing required reverse index"));
384 goto cleanup;
387 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
388 if (prepare_midx_pack(the_repository, bitmap_git->midx, i))
389 die(_("could not open pack %s"),
390 bitmap_git->midx->pack_names[i]);
393 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
394 if (!is_pack_valid(preferred)) {
395 warning(_("preferred pack (%s) is invalid"),
396 preferred->pack_name);
397 goto cleanup;
400 return 0;
402 cleanup:
403 munmap(bitmap_git->map, bitmap_git->map_size);
404 bitmap_git->map_size = 0;
405 bitmap_git->map_pos = 0;
406 bitmap_git->map = NULL;
407 bitmap_git->midx = NULL;
408 return -1;
411 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
413 int fd;
414 struct stat st;
415 char *bitmap_name;
417 bitmap_name = pack_bitmap_filename(packfile);
418 fd = git_open(bitmap_name);
420 if (fd < 0) {
421 if (errno != ENOENT)
422 warning_errno("cannot open '%s'", bitmap_name);
423 free(bitmap_name);
424 return -1;
426 free(bitmap_name);
428 if (fstat(fd, &st)) {
429 error_errno(_("cannot fstat bitmap file"));
430 close(fd);
431 return -1;
434 if (bitmap_git->pack || bitmap_git->midx) {
435 trace2_data_string("bitmap", the_repository,
436 "ignoring extra bitmap file", packfile->pack_name);
437 close(fd);
438 return -1;
441 if (!is_pack_valid(packfile)) {
442 close(fd);
443 return -1;
446 bitmap_git->pack = packfile;
447 bitmap_git->map_size = xsize_t(st.st_size);
448 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
449 bitmap_git->map_pos = 0;
450 close(fd);
452 if (load_bitmap_header(bitmap_git) < 0) {
453 munmap(bitmap_git->map, bitmap_git->map_size);
454 bitmap_git->map = NULL;
455 bitmap_git->map_size = 0;
456 bitmap_git->map_pos = 0;
457 bitmap_git->pack = NULL;
458 return -1;
461 trace2_data_string("bitmap", the_repository, "opened bitmap file",
462 packfile->pack_name);
463 return 0;
466 static int load_reverse_index(struct bitmap_index *bitmap_git)
468 if (bitmap_is_midx(bitmap_git)) {
469 uint32_t i;
470 int ret;
473 * The multi-pack-index's .rev file is already loaded via
474 * open_pack_bitmap_1().
476 * But we still need to open the individual pack .rev files,
477 * since we will need to make use of them in pack-objects.
479 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
480 ret = load_pack_revindex(bitmap_git->midx->packs[i]);
481 if (ret)
482 return ret;
484 return 0;
486 return load_pack_revindex(bitmap_git->pack);
489 static int load_bitmap(struct bitmap_index *bitmap_git)
491 assert(bitmap_git->map);
493 bitmap_git->bitmaps = kh_init_oid_map();
494 bitmap_git->ext_index.positions = kh_init_oid_pos();
496 if (load_reverse_index(bitmap_git))
497 goto failed;
499 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
500 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
501 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
502 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
503 goto failed;
505 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
506 goto failed;
508 return 0;
510 failed:
511 munmap(bitmap_git->map, bitmap_git->map_size);
512 bitmap_git->map = NULL;
513 bitmap_git->map_size = 0;
515 kh_destroy_oid_map(bitmap_git->bitmaps);
516 bitmap_git->bitmaps = NULL;
518 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
519 bitmap_git->ext_index.positions = NULL;
521 return -1;
524 static int open_pack_bitmap(struct repository *r,
525 struct bitmap_index *bitmap_git)
527 struct packed_git *p;
528 int ret = -1;
530 for (p = get_all_packs(r); p; p = p->next) {
531 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
532 ret = 0;
534 * The only reason to keep looking is to report
535 * duplicates.
537 if (!trace2_is_enabled())
538 break;
542 return ret;
545 static int open_midx_bitmap(struct repository *r,
546 struct bitmap_index *bitmap_git)
548 int ret = -1;
549 struct multi_pack_index *midx;
551 assert(!bitmap_git->map);
553 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
554 if (!open_midx_bitmap_1(bitmap_git, midx))
555 ret = 0;
557 return ret;
560 static int open_bitmap(struct repository *r,
561 struct bitmap_index *bitmap_git)
563 int found;
565 assert(!bitmap_git->map);
567 found = !open_midx_bitmap(r, bitmap_git);
570 * these will all be skipped if we opened a midx bitmap; but run it
571 * anyway if tracing is enabled to report the duplicates
573 if (!found || trace2_is_enabled())
574 found |= !open_pack_bitmap(r, bitmap_git);
576 return found ? 0 : -1;
579 struct bitmap_index *prepare_bitmap_git(struct repository *r)
581 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
583 if (!open_bitmap(r, bitmap_git) && !load_bitmap(bitmap_git))
584 return bitmap_git;
586 free_bitmap_index(bitmap_git);
587 return NULL;
590 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
592 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
594 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(bitmap_git))
595 return bitmap_git;
597 free_bitmap_index(bitmap_git);
598 return NULL;
601 struct include_data {
602 struct bitmap_index *bitmap_git;
603 struct bitmap *base;
604 struct bitmap *seen;
607 struct bitmap_lookup_table_triplet {
608 uint32_t commit_pos;
609 uint64_t offset;
610 uint32_t xor_row;
613 struct bitmap_lookup_table_xor_item {
614 struct object_id oid;
615 uint64_t offset;
619 * Given a `triplet` struct pointer and pointer `p`, this
620 * function reads the triplet beginning at `p` into the struct.
621 * Note that this function assumes that there is enough memory
622 * left for filling the `triplet` struct from `p`.
624 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
625 const unsigned char *p)
627 if (!triplet)
628 return -1;
630 triplet->commit_pos = get_be32(p);
631 p += sizeof(uint32_t);
632 triplet->offset = get_be64(p);
633 p += sizeof(uint64_t);
634 triplet->xor_row = get_be32(p);
635 return 0;
639 * This function gets the raw triplet from `row`'th row in the
640 * lookup table and fills that data to the `triplet`.
642 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
643 uint32_t pos,
644 struct bitmap_lookup_table_triplet *triplet)
646 unsigned char *p = NULL;
647 if (pos >= bitmap_git->entry_count)
648 return error(_("corrupt bitmap lookup table: triplet position out of index"));
650 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
652 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
656 * Searches for a matching triplet. `commit_pos` is a pointer
657 * to the wanted commit position value. `table_entry` points to
658 * a triplet in lookup table. The first 4 bytes of each
659 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
661 static int triplet_cmp(const void *commit_pos, const void *table_entry)
664 uint32_t a = *(uint32_t *)commit_pos;
665 uint32_t b = get_be32(table_entry);
666 if (a > b)
667 return 1;
668 else if (a < b)
669 return -1;
671 return 0;
674 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
675 struct object_id *oid,
676 uint32_t *result)
678 int found;
680 if (bitmap_is_midx(bitmap_git))
681 found = bsearch_midx(oid, bitmap_git->midx, result);
682 else
683 found = bsearch_pack(oid, bitmap_git->pack, result);
685 return found;
689 * `bsearch_triplet_by_pos` function searches for the raw triplet
690 * having commit position same as `commit_pos` and fills `triplet`
691 * object from the raw triplet. Returns 1 on success and 0 on
692 * failure.
694 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
695 struct bitmap_index *bitmap_git,
696 struct bitmap_lookup_table_triplet *triplet)
698 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
699 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
701 if (!p)
702 return -1;
704 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
707 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
708 struct commit *commit)
710 uint32_t commit_pos, xor_row;
711 uint64_t offset;
712 int flags;
713 struct bitmap_lookup_table_triplet triplet;
714 struct object_id *oid = &commit->object.oid;
715 struct ewah_bitmap *bitmap;
716 struct stored_bitmap *xor_bitmap = NULL;
717 const int bitmap_header_size = 6;
718 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
719 static size_t xor_items_nr = 0, xor_items_alloc = 0;
720 static int is_corrupt = 0;
721 int xor_flags;
722 khiter_t hash_pos;
723 struct bitmap_lookup_table_xor_item *xor_item;
725 if (is_corrupt)
726 return NULL;
728 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
729 return NULL;
731 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
732 return NULL;
734 xor_items_nr = 0;
735 offset = triplet.offset;
736 xor_row = triplet.xor_row;
738 while (xor_row != 0xffffffff) {
739 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
741 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
742 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
743 goto corrupt;
746 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
747 goto corrupt;
749 xor_item = &xor_items[xor_items_nr];
750 xor_item->offset = triplet.offset;
752 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
753 error(_("corrupt bitmap lookup table: commit index %u out of range"),
754 triplet.commit_pos);
755 goto corrupt;
758 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
761 * If desired bitmap is already stored, we don't need
762 * to iterate further. Because we know that bitmaps
763 * that are needed to be parsed to parse this bitmap
764 * has already been stored. So, assign this stored bitmap
765 * to the xor_bitmap.
767 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
768 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
769 break;
770 xor_items_nr++;
771 xor_row = triplet.xor_row;
774 while (xor_items_nr) {
775 xor_item = &xor_items[xor_items_nr - 1];
776 bitmap_git->map_pos = xor_item->offset;
777 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
778 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
779 oid_to_hex(&xor_item->oid));
780 goto corrupt;
783 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
784 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
785 bitmap = read_bitmap_1(bitmap_git);
787 if (!bitmap)
788 goto corrupt;
790 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
791 xor_items_nr--;
794 bitmap_git->map_pos = offset;
795 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
796 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
797 oid_to_hex(oid));
798 goto corrupt;
802 * Don't bother reading the commit's index position or its xor
803 * offset:
805 * - The commit's index position is irrelevant to us, since
806 * load_bitmap_entries_v1 only uses it to learn the object
807 * id which is used to compute the hashmap's key. We already
808 * have an object id, so no need to look it up again.
810 * - The xor_offset is unusable for us, since it specifies how
811 * many entries previous to ours we should look at. This
812 * makes sense when reading the bitmaps sequentially (as in
813 * load_bitmap_entries_v1()), since we can keep track of
814 * each bitmap as we read them.
816 * But it can't work for us, since the bitmap's don't have a
817 * fixed size. So we learn the position of the xor'd bitmap
818 * from the commit table (and resolve it to a bitmap in the
819 * above if-statement).
821 * Instead, we can skip ahead and immediately read the flags and
822 * ewah bitmap.
824 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
825 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
826 bitmap = read_bitmap_1(bitmap_git);
828 if (!bitmap)
829 goto corrupt;
831 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
833 corrupt:
834 free(xor_items);
835 is_corrupt = 1;
836 return NULL;
839 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
840 struct commit *commit)
842 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
843 commit->object.oid);
844 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
845 struct stored_bitmap *bitmap = NULL;
846 if (!bitmap_git->table_lookup)
847 return NULL;
849 /* this is a fairly hot codepath - no trace2_region please */
850 /* NEEDSWORK: cache misses aren't recorded */
851 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
852 if (!bitmap)
853 return NULL;
854 return lookup_stored_bitmap(bitmap);
856 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
859 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
860 const struct object_id *oid)
862 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
863 khiter_t pos = kh_get_oid_pos(positions, *oid);
865 if (pos < kh_end(positions)) {
866 int bitmap_pos = kh_value(positions, pos);
867 return bitmap_pos + bitmap_num_objects(bitmap_git);
870 return -1;
873 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
874 const struct object_id *oid)
876 uint32_t pos;
877 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
878 if (!offset)
879 return -1;
881 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
882 return -1;
883 return pos;
886 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
887 const struct object_id *oid)
889 uint32_t want, got;
890 if (!bsearch_midx(oid, bitmap_git->midx, &want))
891 return -1;
893 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
894 return -1;
895 return got;
898 static int bitmap_position(struct bitmap_index *bitmap_git,
899 const struct object_id *oid)
901 int pos;
902 if (bitmap_is_midx(bitmap_git))
903 pos = bitmap_position_midx(bitmap_git, oid);
904 else
905 pos = bitmap_position_packfile(bitmap_git, oid);
906 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
909 static int ext_index_add_object(struct bitmap_index *bitmap_git,
910 struct object *object, const char *name)
912 struct eindex *eindex = &bitmap_git->ext_index;
914 khiter_t hash_pos;
915 int hash_ret;
916 int bitmap_pos;
918 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
919 if (hash_ret > 0) {
920 if (eindex->count >= eindex->alloc) {
921 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
922 REALLOC_ARRAY(eindex->objects, eindex->alloc);
923 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
926 bitmap_pos = eindex->count;
927 eindex->objects[eindex->count] = object;
928 eindex->hashes[eindex->count] = pack_name_hash(name);
929 kh_value(eindex->positions, hash_pos) = bitmap_pos;
930 eindex->count++;
931 } else {
932 bitmap_pos = kh_value(eindex->positions, hash_pos);
935 return bitmap_pos + bitmap_num_objects(bitmap_git);
938 struct bitmap_show_data {
939 struct bitmap_index *bitmap_git;
940 struct bitmap *base;
943 static void show_object(struct object *object, const char *name, void *data_)
945 struct bitmap_show_data *data = data_;
946 int bitmap_pos;
948 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
950 if (bitmap_pos < 0)
951 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
952 name);
954 bitmap_set(data->base, bitmap_pos);
957 static void show_commit(struct commit *commit UNUSED,
958 void *data UNUSED)
962 static int add_to_include_set(struct bitmap_index *bitmap_git,
963 struct include_data *data,
964 struct commit *commit,
965 int bitmap_pos)
967 struct ewah_bitmap *partial;
969 if (data->seen && bitmap_get(data->seen, bitmap_pos))
970 return 0;
972 if (bitmap_get(data->base, bitmap_pos))
973 return 0;
975 partial = bitmap_for_commit(bitmap_git, commit);
976 if (partial) {
977 bitmap_or_ewah(data->base, partial);
978 return 0;
981 bitmap_set(data->base, bitmap_pos);
982 return 1;
985 static int should_include(struct commit *commit, void *_data)
987 struct include_data *data = _data;
988 int bitmap_pos;
990 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
991 if (bitmap_pos < 0)
992 bitmap_pos = ext_index_add_object(data->bitmap_git,
993 (struct object *)commit,
994 NULL);
996 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
997 struct commit_list *parent = commit->parents;
999 while (parent) {
1000 parent->item->object.flags |= SEEN;
1001 parent = parent->next;
1004 return 0;
1007 return 1;
1010 static int should_include_obj(struct object *obj, void *_data)
1012 struct include_data *data = _data;
1013 int bitmap_pos;
1015 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1016 if (bitmap_pos < 0)
1017 return 1;
1018 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1019 bitmap_get(data->base, bitmap_pos)) {
1020 obj->flags |= SEEN;
1021 return 0;
1023 return 1;
1026 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1027 struct bitmap **base,
1028 struct commit *commit)
1030 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1032 if (!or_with)
1033 return 0;
1035 if (!*base)
1036 *base = ewah_to_bitmap(or_with);
1037 else
1038 bitmap_or_ewah(*base, or_with);
1040 return 1;
1043 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1044 struct rev_info *revs,
1045 struct object_list *roots,
1046 struct bitmap *seen)
1048 struct bitmap *base = NULL;
1049 int needs_walk = 0;
1051 struct object_list *not_mapped = NULL;
1054 * Go through all the roots for the walk. The ones that have bitmaps
1055 * on the bitmap index will be `or`ed together to form an initial
1056 * global reachability analysis.
1058 * The ones without bitmaps in the index will be stored in the
1059 * `not_mapped_list` for further processing.
1061 while (roots) {
1062 struct object *object = roots->item;
1063 roots = roots->next;
1065 if (object->type == OBJ_COMMIT &&
1066 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1067 object->flags |= SEEN;
1068 continue;
1071 object_list_insert(object, &not_mapped);
1075 * Best case scenario: We found bitmaps for all the roots,
1076 * so the resulting `or` bitmap has the full reachability analysis
1078 if (!not_mapped)
1079 return base;
1081 roots = not_mapped;
1084 * Let's iterate through all the roots that don't have bitmaps to
1085 * check if we can determine them to be reachable from the existing
1086 * global bitmap.
1088 * If we cannot find them in the existing global bitmap, we'll need
1089 * to push them to an actual walk and run it until we can confirm
1090 * they are reachable
1092 while (roots) {
1093 struct object *object = roots->item;
1094 int pos;
1096 roots = roots->next;
1097 pos = bitmap_position(bitmap_git, &object->oid);
1099 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1100 object->flags &= ~UNINTERESTING;
1101 add_pending_object(revs, object, "");
1102 needs_walk = 1;
1103 } else {
1104 object->flags |= SEEN;
1108 if (needs_walk) {
1109 struct include_data incdata;
1110 struct bitmap_show_data show_data;
1112 if (!base)
1113 base = bitmap_new();
1115 incdata.bitmap_git = bitmap_git;
1116 incdata.base = base;
1117 incdata.seen = seen;
1119 revs->include_check = should_include;
1120 revs->include_check_obj = should_include_obj;
1121 revs->include_check_data = &incdata;
1123 if (prepare_revision_walk(revs))
1124 die(_("revision walk setup failed"));
1126 show_data.bitmap_git = bitmap_git;
1127 show_data.base = base;
1129 traverse_commit_list(revs,
1130 show_commit, show_object,
1131 &show_data);
1133 revs->include_check = NULL;
1134 revs->include_check_obj = NULL;
1135 revs->include_check_data = NULL;
1138 return base;
1141 static void show_extended_objects(struct bitmap_index *bitmap_git,
1142 struct rev_info *revs,
1143 show_reachable_fn show_reach)
1145 struct bitmap *objects = bitmap_git->result;
1146 struct eindex *eindex = &bitmap_git->ext_index;
1147 uint32_t i;
1149 for (i = 0; i < eindex->count; ++i) {
1150 struct object *obj;
1152 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1153 continue;
1155 obj = eindex->objects[i];
1156 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1157 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1158 (obj->type == OBJ_TAG && !revs->tag_objects))
1159 continue;
1161 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1165 static void init_type_iterator(struct ewah_iterator *it,
1166 struct bitmap_index *bitmap_git,
1167 enum object_type type)
1169 switch (type) {
1170 case OBJ_COMMIT:
1171 ewah_iterator_init(it, bitmap_git->commits);
1172 break;
1174 case OBJ_TREE:
1175 ewah_iterator_init(it, bitmap_git->trees);
1176 break;
1178 case OBJ_BLOB:
1179 ewah_iterator_init(it, bitmap_git->blobs);
1180 break;
1182 case OBJ_TAG:
1183 ewah_iterator_init(it, bitmap_git->tags);
1184 break;
1186 default:
1187 BUG("object type %d not stored by bitmap type index", type);
1188 break;
1192 static void show_objects_for_type(
1193 struct bitmap_index *bitmap_git,
1194 enum object_type object_type,
1195 show_reachable_fn show_reach)
1197 size_t i = 0;
1198 uint32_t offset;
1200 struct ewah_iterator it;
1201 eword_t filter;
1203 struct bitmap *objects = bitmap_git->result;
1205 init_type_iterator(&it, bitmap_git, object_type);
1207 for (i = 0; i < objects->word_alloc &&
1208 ewah_iterator_next(&filter, &it); i++) {
1209 eword_t word = objects->words[i] & filter;
1210 size_t pos = (i * BITS_IN_EWORD);
1212 if (!word)
1213 continue;
1215 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1216 struct packed_git *pack;
1217 struct object_id oid;
1218 uint32_t hash = 0, index_pos;
1219 off_t ofs;
1221 if ((word >> offset) == 0)
1222 break;
1224 offset += ewah_bit_ctz64(word >> offset);
1226 if (bitmap_is_midx(bitmap_git)) {
1227 struct multi_pack_index *m = bitmap_git->midx;
1228 uint32_t pack_id;
1230 index_pos = pack_pos_to_midx(m, pos + offset);
1231 ofs = nth_midxed_offset(m, index_pos);
1232 nth_midxed_object_oid(&oid, m, index_pos);
1234 pack_id = nth_midxed_pack_int_id(m, index_pos);
1235 pack = bitmap_git->midx->packs[pack_id];
1236 } else {
1237 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1238 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1239 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1241 pack = bitmap_git->pack;
1244 if (bitmap_git->hashes)
1245 hash = get_be32(bitmap_git->hashes + index_pos);
1247 show_reach(&oid, object_type, 0, hash, pack, ofs);
1252 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1253 struct object_list *roots)
1255 while (roots) {
1256 struct object *object = roots->item;
1257 roots = roots->next;
1259 if (bitmap_is_midx(bitmap_git)) {
1260 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1261 return 1;
1262 } else {
1263 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1264 return 1;
1268 return 0;
1271 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1272 struct object_list *tip_objects,
1273 enum object_type type)
1275 struct bitmap *result = bitmap_new();
1276 struct object_list *p;
1278 for (p = tip_objects; p; p = p->next) {
1279 int pos;
1281 if (p->item->type != type)
1282 continue;
1284 pos = bitmap_position(bitmap_git, &p->item->oid);
1285 if (pos < 0)
1286 continue;
1288 bitmap_set(result, pos);
1291 return result;
1294 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1295 struct object_list *tip_objects,
1296 struct bitmap *to_filter,
1297 enum object_type type)
1299 struct eindex *eindex = &bitmap_git->ext_index;
1300 struct bitmap *tips;
1301 struct ewah_iterator it;
1302 eword_t mask;
1303 uint32_t i;
1306 * The non-bitmap version of this filter never removes
1307 * objects which the other side specifically asked for,
1308 * so we must match that behavior.
1310 tips = find_tip_objects(bitmap_git, tip_objects, type);
1313 * We can use the type-level bitmap for 'type' to work in whole
1314 * words for the objects that are actually in the bitmapped
1315 * packfile.
1317 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1318 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1319 i++) {
1320 if (i < tips->word_alloc)
1321 mask &= ~tips->words[i];
1322 to_filter->words[i] &= ~mask;
1326 * Clear any objects that weren't in the packfile (and so would
1327 * not have been caught by the loop above. We'll have to check
1328 * them individually.
1330 for (i = 0; i < eindex->count; i++) {
1331 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1332 if (eindex->objects[i]->type == type &&
1333 bitmap_get(to_filter, pos) &&
1334 !bitmap_get(tips, pos))
1335 bitmap_unset(to_filter, pos);
1338 bitmap_free(tips);
1341 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1342 struct object_list *tip_objects,
1343 struct bitmap *to_filter)
1345 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1346 OBJ_BLOB);
1349 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1350 uint32_t pos)
1352 unsigned long size;
1353 struct object_info oi = OBJECT_INFO_INIT;
1355 oi.sizep = &size;
1357 if (pos < bitmap_num_objects(bitmap_git)) {
1358 struct packed_git *pack;
1359 off_t ofs;
1361 if (bitmap_is_midx(bitmap_git)) {
1362 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1363 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1365 pack = bitmap_git->midx->packs[pack_id];
1366 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1367 } else {
1368 pack = bitmap_git->pack;
1369 ofs = pack_pos_to_offset(pack, pos);
1372 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1373 struct object_id oid;
1374 nth_bitmap_object_oid(bitmap_git, &oid,
1375 pack_pos_to_index(pack, pos));
1376 die(_("unable to get size of %s"), oid_to_hex(&oid));
1378 } else {
1379 struct eindex *eindex = &bitmap_git->ext_index;
1380 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1381 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1382 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1385 return size;
1388 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1389 struct object_list *tip_objects,
1390 struct bitmap *to_filter,
1391 unsigned long limit)
1393 struct eindex *eindex = &bitmap_git->ext_index;
1394 struct bitmap *tips;
1395 struct ewah_iterator it;
1396 eword_t mask;
1397 uint32_t i;
1399 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1401 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1402 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1403 i++) {
1404 eword_t word = to_filter->words[i] & mask;
1405 unsigned offset;
1407 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1408 uint32_t pos;
1410 if ((word >> offset) == 0)
1411 break;
1412 offset += ewah_bit_ctz64(word >> offset);
1413 pos = i * BITS_IN_EWORD + offset;
1415 if (!bitmap_get(tips, pos) &&
1416 get_size_by_pos(bitmap_git, pos) >= limit)
1417 bitmap_unset(to_filter, pos);
1421 for (i = 0; i < eindex->count; i++) {
1422 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1423 if (eindex->objects[i]->type == OBJ_BLOB &&
1424 bitmap_get(to_filter, pos) &&
1425 !bitmap_get(tips, pos) &&
1426 get_size_by_pos(bitmap_git, pos) >= limit)
1427 bitmap_unset(to_filter, pos);
1430 bitmap_free(tips);
1433 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1434 struct object_list *tip_objects,
1435 struct bitmap *to_filter,
1436 unsigned long limit)
1438 if (limit)
1439 BUG("filter_bitmap_tree_depth given non-zero limit");
1441 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1442 OBJ_TREE);
1443 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1444 OBJ_BLOB);
1447 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1448 struct object_list *tip_objects,
1449 struct bitmap *to_filter,
1450 enum object_type object_type)
1452 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1453 BUG("filter_bitmap_object_type given invalid object");
1455 if (object_type != OBJ_TAG)
1456 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1457 if (object_type != OBJ_COMMIT)
1458 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1459 if (object_type != OBJ_TREE)
1460 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1461 if (object_type != OBJ_BLOB)
1462 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1465 static int filter_bitmap(struct bitmap_index *bitmap_git,
1466 struct object_list *tip_objects,
1467 struct bitmap *to_filter,
1468 struct list_objects_filter_options *filter)
1470 if (!filter || filter->choice == LOFC_DISABLED)
1471 return 0;
1473 if (filter->choice == LOFC_BLOB_NONE) {
1474 if (bitmap_git)
1475 filter_bitmap_blob_none(bitmap_git, tip_objects,
1476 to_filter);
1477 return 0;
1480 if (filter->choice == LOFC_BLOB_LIMIT) {
1481 if (bitmap_git)
1482 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1483 to_filter,
1484 filter->blob_limit_value);
1485 return 0;
1488 if (filter->choice == LOFC_TREE_DEPTH &&
1489 filter->tree_exclude_depth == 0) {
1490 if (bitmap_git)
1491 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1492 to_filter,
1493 filter->tree_exclude_depth);
1494 return 0;
1497 if (filter->choice == LOFC_OBJECT_TYPE) {
1498 if (bitmap_git)
1499 filter_bitmap_object_type(bitmap_git, tip_objects,
1500 to_filter,
1501 filter->object_type);
1502 return 0;
1505 if (filter->choice == LOFC_COMBINE) {
1506 int i;
1507 for (i = 0; i < filter->sub_nr; i++) {
1508 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1509 &filter->sub[i]) < 0)
1510 return -1;
1512 return 0;
1515 /* filter choice not handled */
1516 return -1;
1519 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1521 return !filter_bitmap(NULL, NULL, NULL, filter);
1524 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1525 int filter_provided_objects)
1527 unsigned int i;
1529 struct object_list *wants = NULL;
1530 struct object_list *haves = NULL;
1532 struct bitmap *wants_bitmap = NULL;
1533 struct bitmap *haves_bitmap = NULL;
1535 struct bitmap_index *bitmap_git;
1538 * We can't do pathspec limiting with bitmaps, because we don't know
1539 * which commits are associated with which object changes (let alone
1540 * even which objects are associated with which paths).
1542 if (revs->prune)
1543 return NULL;
1545 if (!can_filter_bitmap(&revs->filter))
1546 return NULL;
1548 /* try to open a bitmapped pack, but don't parse it yet
1549 * because we may not need to use it */
1550 CALLOC_ARRAY(bitmap_git, 1);
1551 if (open_bitmap(revs->repo, bitmap_git) < 0)
1552 goto cleanup;
1554 for (i = 0; i < revs->pending.nr; ++i) {
1555 struct object *object = revs->pending.objects[i].item;
1557 if (object->type == OBJ_NONE)
1558 parse_object_or_die(&object->oid, NULL);
1560 while (object->type == OBJ_TAG) {
1561 struct tag *tag = (struct tag *) object;
1563 if (object->flags & UNINTERESTING)
1564 object_list_insert(object, &haves);
1565 else
1566 object_list_insert(object, &wants);
1568 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1569 object->flags |= (tag->object.flags & UNINTERESTING);
1572 if (object->flags & UNINTERESTING)
1573 object_list_insert(object, &haves);
1574 else
1575 object_list_insert(object, &wants);
1579 * if we have a HAVES list, but none of those haves is contained
1580 * in the packfile that has a bitmap, we don't have anything to
1581 * optimize here
1583 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1584 goto cleanup;
1586 /* if we don't want anything, we're done here */
1587 if (!wants)
1588 goto cleanup;
1591 * now we're going to use bitmaps, so load the actual bitmap entries
1592 * from disk. this is the point of no return; after this the rev_list
1593 * becomes invalidated and we must perform the revwalk through bitmaps
1595 if (load_bitmap(bitmap_git) < 0)
1596 goto cleanup;
1598 object_array_clear(&revs->pending);
1600 if (haves) {
1601 revs->ignore_missing_links = 1;
1602 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1603 reset_revision_walk();
1604 revs->ignore_missing_links = 0;
1606 if (!haves_bitmap)
1607 BUG("failed to perform bitmap walk");
1610 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1612 if (!wants_bitmap)
1613 BUG("failed to perform bitmap walk");
1615 if (haves_bitmap)
1616 bitmap_and_not(wants_bitmap, haves_bitmap);
1618 filter_bitmap(bitmap_git,
1619 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1620 wants_bitmap,
1621 &revs->filter);
1623 bitmap_git->result = wants_bitmap;
1624 bitmap_git->haves = haves_bitmap;
1626 object_list_free(&wants);
1627 object_list_free(&haves);
1629 return bitmap_git;
1631 cleanup:
1632 free_bitmap_index(bitmap_git);
1633 object_list_free(&wants);
1634 object_list_free(&haves);
1635 return NULL;
1639 * -1 means "stop trying further objects"; 0 means we may or may not have
1640 * reused, but you can keep feeding bits.
1642 static int try_partial_reuse(struct packed_git *pack,
1643 size_t pos,
1644 struct bitmap *reuse,
1645 struct pack_window **w_curs)
1647 off_t offset, delta_obj_offset;
1648 enum object_type type;
1649 unsigned long size;
1652 * try_partial_reuse() is called either on (a) objects in the
1653 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1654 * objects in the preferred pack of a multi-pack bitmap.
1655 * Importantly, the latter can pretend as if only a single pack
1656 * exists because:
1658 * - The first pack->num_objects bits of a MIDX bitmap are
1659 * reserved for the preferred pack, and
1661 * - Ties due to duplicate objects are always resolved in
1662 * favor of the preferred pack.
1664 * Therefore we do not need to ever ask the MIDX for its copy of
1665 * an object by OID, since it will always select it from the
1666 * preferred pack. Likewise, the selected copy of the base
1667 * object for any deltas will reside in the same pack.
1669 * This means that we can reuse pos when looking up the bit in
1670 * the reuse bitmap, too, since bits corresponding to the
1671 * preferred pack precede all bits from other packs.
1674 if (pos >= pack->num_objects)
1675 return -1; /* not actually in the pack or MIDX preferred pack */
1677 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1678 type = unpack_object_header(pack, w_curs, &offset, &size);
1679 if (type < 0)
1680 return -1; /* broken packfile, punt */
1682 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1683 off_t base_offset;
1684 uint32_t base_pos;
1687 * Find the position of the base object so we can look it up
1688 * in our bitmaps. If we can't come up with an offset, or if
1689 * that offset is not in the revidx, the pack is corrupt.
1690 * There's nothing we can do, so just punt on this object,
1691 * and the normal slow path will complain about it in
1692 * more detail.
1694 base_offset = get_delta_base(pack, w_curs, &offset, type,
1695 delta_obj_offset);
1696 if (!base_offset)
1697 return 0;
1698 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1699 return 0;
1702 * We assume delta dependencies always point backwards. This
1703 * lets us do a single pass, and is basically always true
1704 * due to the way OFS_DELTAs work. You would not typically
1705 * find REF_DELTA in a bitmapped pack, since we only bitmap
1706 * packs we write fresh, and OFS_DELTA is the default). But
1707 * let's double check to make sure the pack wasn't written with
1708 * odd parameters.
1710 if (base_pos >= pos)
1711 return 0;
1714 * And finally, if we're not sending the base as part of our
1715 * reuse chunk, then don't send this object either. The base
1716 * would come after us, along with other objects not
1717 * necessarily in the pack, which means we'd need to convert
1718 * to REF_DELTA on the fly. Better to just let the normal
1719 * object_entry code path handle it.
1721 if (!bitmap_get(reuse, base_pos))
1722 return 0;
1726 * If we got here, then the object is OK to reuse. Mark it.
1728 bitmap_set(reuse, pos);
1729 return 0;
1732 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1734 struct multi_pack_index *m = bitmap_git->midx;
1735 if (!m)
1736 BUG("midx_preferred_pack: requires non-empty MIDX");
1737 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1740 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1741 struct packed_git **packfile_out,
1742 uint32_t *entries,
1743 struct bitmap **reuse_out)
1745 struct packed_git *pack;
1746 struct bitmap *result = bitmap_git->result;
1747 struct bitmap *reuse;
1748 struct pack_window *w_curs = NULL;
1749 size_t i = 0;
1750 uint32_t offset;
1751 uint32_t objects_nr;
1753 assert(result);
1755 load_reverse_index(bitmap_git);
1757 if (bitmap_is_midx(bitmap_git))
1758 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1759 else
1760 pack = bitmap_git->pack;
1761 objects_nr = pack->num_objects;
1763 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1764 i++;
1767 * Don't mark objects not in the packfile or preferred pack. This bitmap
1768 * marks objects eligible for reuse, but the pack-reuse code only
1769 * understands how to reuse a single pack. Since the preferred pack is
1770 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1771 * we use it instead of another pack. In single-pack bitmaps, the choice
1772 * is made for us.
1774 if (i > objects_nr / BITS_IN_EWORD)
1775 i = objects_nr / BITS_IN_EWORD;
1777 reuse = bitmap_word_alloc(i);
1778 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1780 for (; i < result->word_alloc; ++i) {
1781 eword_t word = result->words[i];
1782 size_t pos = (i * BITS_IN_EWORD);
1784 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1785 if ((word >> offset) == 0)
1786 break;
1788 offset += ewah_bit_ctz64(word >> offset);
1789 if (try_partial_reuse(pack, pos + offset,
1790 reuse, &w_curs) < 0) {
1792 * try_partial_reuse indicated we couldn't reuse
1793 * any bits, so there is no point in trying more
1794 * bits in the current word, or any other words
1795 * in result.
1797 * Jump out of both loops to avoid future
1798 * unnecessary calls to try_partial_reuse.
1800 goto done;
1805 done:
1806 unuse_pack(&w_curs);
1808 *entries = bitmap_popcount(reuse);
1809 if (!*entries) {
1810 bitmap_free(reuse);
1811 return -1;
1815 * Drop any reused objects from the result, since they will not
1816 * need to be handled separately.
1818 bitmap_and_not(result, reuse);
1819 *packfile_out = pack;
1820 *reuse_out = reuse;
1821 return 0;
1824 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1825 struct bitmap *bitmap, const struct object_id *oid)
1827 int idx;
1829 if (!bitmap)
1830 return 0;
1832 idx = bitmap_position(bitmap_git, oid);
1833 return idx >= 0 && bitmap_get(bitmap, idx);
1836 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1837 struct rev_info *revs,
1838 show_reachable_fn show_reachable)
1840 assert(bitmap_git->result);
1842 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1843 if (revs->tree_objects)
1844 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1845 if (revs->blob_objects)
1846 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1847 if (revs->tag_objects)
1848 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1850 show_extended_objects(bitmap_git, revs, show_reachable);
1853 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1854 enum object_type type)
1856 struct bitmap *objects = bitmap_git->result;
1857 struct eindex *eindex = &bitmap_git->ext_index;
1859 uint32_t i = 0, count = 0;
1860 struct ewah_iterator it;
1861 eword_t filter;
1863 init_type_iterator(&it, bitmap_git, type);
1865 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1866 eword_t word = objects->words[i++] & filter;
1867 count += ewah_bit_popcount64(word);
1870 for (i = 0; i < eindex->count; ++i) {
1871 if (eindex->objects[i]->type == type &&
1872 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1873 count++;
1876 return count;
1879 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1880 uint32_t *commits, uint32_t *trees,
1881 uint32_t *blobs, uint32_t *tags)
1883 assert(bitmap_git->result);
1885 if (commits)
1886 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1888 if (trees)
1889 *trees = count_object_type(bitmap_git, OBJ_TREE);
1891 if (blobs)
1892 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1894 if (tags)
1895 *tags = count_object_type(bitmap_git, OBJ_TAG);
1898 struct bitmap_test_data {
1899 struct bitmap_index *bitmap_git;
1900 struct bitmap *base;
1901 struct bitmap *commits;
1902 struct bitmap *trees;
1903 struct bitmap *blobs;
1904 struct bitmap *tags;
1905 struct progress *prg;
1906 size_t seen;
1909 static void test_bitmap_type(struct bitmap_test_data *tdata,
1910 struct object *obj, int pos)
1912 enum object_type bitmap_type = OBJ_NONE;
1913 int bitmaps_nr = 0;
1915 if (bitmap_get(tdata->commits, pos)) {
1916 bitmap_type = OBJ_COMMIT;
1917 bitmaps_nr++;
1919 if (bitmap_get(tdata->trees, pos)) {
1920 bitmap_type = OBJ_TREE;
1921 bitmaps_nr++;
1923 if (bitmap_get(tdata->blobs, pos)) {
1924 bitmap_type = OBJ_BLOB;
1925 bitmaps_nr++;
1927 if (bitmap_get(tdata->tags, pos)) {
1928 bitmap_type = OBJ_TAG;
1929 bitmaps_nr++;
1932 if (bitmap_type == OBJ_NONE)
1933 die(_("object '%s' not found in type bitmaps"),
1934 oid_to_hex(&obj->oid));
1936 if (bitmaps_nr > 1)
1937 die(_("object '%s' does not have a unique type"),
1938 oid_to_hex(&obj->oid));
1940 if (bitmap_type != obj->type)
1941 die(_("object '%s': real type '%s', expected: '%s'"),
1942 oid_to_hex(&obj->oid),
1943 type_name(obj->type),
1944 type_name(bitmap_type));
1947 static void test_show_object(struct object *object,
1948 const char *name UNUSED,
1949 void *data)
1951 struct bitmap_test_data *tdata = data;
1952 int bitmap_pos;
1954 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1955 if (bitmap_pos < 0)
1956 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
1957 test_bitmap_type(tdata, object, bitmap_pos);
1959 bitmap_set(tdata->base, bitmap_pos);
1960 display_progress(tdata->prg, ++tdata->seen);
1963 static void test_show_commit(struct commit *commit, void *data)
1965 struct bitmap_test_data *tdata = data;
1966 int bitmap_pos;
1968 bitmap_pos = bitmap_position(tdata->bitmap_git,
1969 &commit->object.oid);
1970 if (bitmap_pos < 0)
1971 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
1972 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1974 bitmap_set(tdata->base, bitmap_pos);
1975 display_progress(tdata->prg, ++tdata->seen);
1978 void test_bitmap_walk(struct rev_info *revs)
1980 struct object *root;
1981 struct bitmap *result = NULL;
1982 size_t result_popcnt;
1983 struct bitmap_test_data tdata;
1984 struct bitmap_index *bitmap_git;
1985 struct ewah_bitmap *bm;
1987 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1988 die(_("failed to load bitmap indexes"));
1990 if (revs->pending.nr != 1)
1991 die(_("you must specify exactly one commit to test"));
1993 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
1994 bitmap_git->version,
1995 bitmap_git->entry_count,
1996 bitmap_git->table_lookup ? "" : " loaded");
1998 root = revs->pending.objects[0].item;
1999 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2001 if (bm) {
2002 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2003 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2005 result = ewah_to_bitmap(bm);
2008 if (!result)
2009 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2011 revs->tag_objects = 1;
2012 revs->tree_objects = 1;
2013 revs->blob_objects = 1;
2015 result_popcnt = bitmap_popcount(result);
2017 if (prepare_revision_walk(revs))
2018 die(_("revision walk setup failed"));
2020 tdata.bitmap_git = bitmap_git;
2021 tdata.base = bitmap_new();
2022 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2023 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2024 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2025 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2026 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2027 tdata.seen = 0;
2029 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2031 stop_progress(&tdata.prg);
2033 if (bitmap_equals(result, tdata.base))
2034 fprintf_ln(stderr, "OK!");
2035 else
2036 die(_("mismatch in bitmap results"));
2038 bitmap_free(result);
2039 bitmap_free(tdata.base);
2040 bitmap_free(tdata.commits);
2041 bitmap_free(tdata.trees);
2042 bitmap_free(tdata.blobs);
2043 bitmap_free(tdata.tags);
2044 free_bitmap_index(bitmap_git);
2047 int test_bitmap_commits(struct repository *r)
2049 struct object_id oid;
2050 MAYBE_UNUSED void *value;
2051 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2053 if (!bitmap_git)
2054 die(_("failed to load bitmap indexes"));
2057 * As this function is only used to print bitmap selected
2058 * commits, we don't have to read the commit table.
2060 if (bitmap_git->table_lookup) {
2061 if (load_bitmap_entries_v1(bitmap_git) < 0)
2062 die(_("failed to load bitmap indexes"));
2065 kh_foreach(bitmap_git->bitmaps, oid, value, {
2066 printf_ln("%s", oid_to_hex(&oid));
2069 free_bitmap_index(bitmap_git);
2071 return 0;
2074 int test_bitmap_hashes(struct repository *r)
2076 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2077 struct object_id oid;
2078 uint32_t i, index_pos;
2080 if (!bitmap_git || !bitmap_git->hashes)
2081 goto cleanup;
2083 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2084 if (bitmap_is_midx(bitmap_git))
2085 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2086 else
2087 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2089 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2091 printf_ln("%s %"PRIu32"",
2092 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2095 cleanup:
2096 free_bitmap_index(bitmap_git);
2098 return 0;
2101 int rebuild_bitmap(const uint32_t *reposition,
2102 struct ewah_bitmap *source,
2103 struct bitmap *dest)
2105 uint32_t pos = 0;
2106 struct ewah_iterator it;
2107 eword_t word;
2109 ewah_iterator_init(&it, source);
2111 while (ewah_iterator_next(&word, &it)) {
2112 uint32_t offset, bit_pos;
2114 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2115 if ((word >> offset) == 0)
2116 break;
2118 offset += ewah_bit_ctz64(word >> offset);
2120 bit_pos = reposition[pos + offset];
2121 if (bit_pos > 0)
2122 bitmap_set(dest, bit_pos - 1);
2123 else /* can't reuse, we don't have the object */
2124 return -1;
2127 pos += BITS_IN_EWORD;
2129 return 0;
2132 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2133 struct packing_data *mapping)
2135 uint32_t i, num_objects;
2136 uint32_t *reposition;
2138 if (!bitmap_is_midx(bitmap_git))
2139 load_reverse_index(bitmap_git);
2140 else if (load_midx_revindex(bitmap_git->midx) < 0)
2141 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2142 "extension");
2144 num_objects = bitmap_num_objects(bitmap_git);
2145 CALLOC_ARRAY(reposition, num_objects);
2147 for (i = 0; i < num_objects; ++i) {
2148 struct object_id oid;
2149 struct object_entry *oe;
2150 uint32_t index_pos;
2152 if (bitmap_is_midx(bitmap_git))
2153 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2154 else
2155 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2156 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2157 oe = packlist_find(mapping, &oid);
2159 if (oe) {
2160 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2161 if (bitmap_git->hashes && !oe->hash)
2162 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2166 return reposition;
2169 void free_bitmap_index(struct bitmap_index *b)
2171 if (!b)
2172 return;
2174 if (b->map)
2175 munmap(b->map, b->map_size);
2176 ewah_pool_free(b->commits);
2177 ewah_pool_free(b->trees);
2178 ewah_pool_free(b->blobs);
2179 ewah_pool_free(b->tags);
2180 if (b->bitmaps) {
2181 struct stored_bitmap *sb;
2182 kh_foreach_value(b->bitmaps, sb, {
2183 ewah_pool_free(sb->root);
2184 free(sb);
2187 kh_destroy_oid_map(b->bitmaps);
2188 free(b->ext_index.objects);
2189 free(b->ext_index.hashes);
2190 kh_destroy_oid_pos(b->ext_index.positions);
2191 bitmap_free(b->result);
2192 bitmap_free(b->haves);
2193 if (bitmap_is_midx(b)) {
2195 * Multi-pack bitmaps need to have resources associated with
2196 * their on-disk reverse indexes unmapped so that stale .rev and
2197 * .bitmap files can be removed.
2199 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2200 * written in the same 'git multi-pack-index write --bitmap'
2201 * process. Close resources so they can be removed safely on
2202 * platforms like Windows.
2204 close_midx_revindex(b->midx);
2206 free(b);
2209 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2210 const struct object_id *oid)
2212 return bitmap_git &&
2213 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2216 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2217 enum object_type object_type)
2219 struct bitmap *result = bitmap_git->result;
2220 off_t total = 0;
2221 struct ewah_iterator it;
2222 eword_t filter;
2223 size_t i;
2225 init_type_iterator(&it, bitmap_git, object_type);
2226 for (i = 0; i < result->word_alloc &&
2227 ewah_iterator_next(&filter, &it); i++) {
2228 eword_t word = result->words[i] & filter;
2229 size_t base = (i * BITS_IN_EWORD);
2230 unsigned offset;
2232 if (!word)
2233 continue;
2235 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2236 if ((word >> offset) == 0)
2237 break;
2239 offset += ewah_bit_ctz64(word >> offset);
2241 if (bitmap_is_midx(bitmap_git)) {
2242 uint32_t pack_pos;
2243 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2244 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2246 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2247 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2249 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2250 struct object_id oid;
2251 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2253 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2254 oid_to_hex(&oid),
2255 pack->pack_name,
2256 (uintmax_t)offset);
2259 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2260 } else {
2261 size_t pos = base + offset;
2262 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2263 pack_pos_to_offset(bitmap_git->pack, pos);
2268 return total;
2271 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2273 struct bitmap *result = bitmap_git->result;
2274 struct eindex *eindex = &bitmap_git->ext_index;
2275 off_t total = 0;
2276 struct object_info oi = OBJECT_INFO_INIT;
2277 off_t object_size;
2278 size_t i;
2280 oi.disk_sizep = &object_size;
2282 for (i = 0; i < eindex->count; i++) {
2283 struct object *obj = eindex->objects[i];
2285 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2286 continue;
2288 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2289 die(_("unable to get disk usage of '%s'"),
2290 oid_to_hex(&obj->oid));
2292 total += object_size;
2294 return total;
2297 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2298 struct rev_info *revs)
2300 off_t total = 0;
2302 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2303 if (revs->tree_objects)
2304 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2305 if (revs->blob_objects)
2306 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2307 if (revs->tag_objects)
2308 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2310 total += get_disk_usage_for_extended(bitmap_git);
2312 return total;
2315 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2317 return !!bitmap_git->midx;
2320 const struct string_list *bitmap_preferred_tips(struct repository *r)
2322 const struct string_list *dest;
2324 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2325 return dest;
2326 return NULL;
2329 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2331 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2332 struct string_list_item *item;
2334 if (!preferred_tips)
2335 return 0;
2337 for_each_string_list_item(item, preferred_tips) {
2338 if (starts_with(refname, item->string))
2339 return 1;
2342 return 0;