Start the 2.41 cycle
[git/debian.git] / pack-bitmap.c
blobca7c81b5c9f2770daf79d4fe15b889ac4e8b5251
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "commit.h"
4 #include "hex.h"
5 #include "strbuf.h"
6 #include "tag.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "progress.h"
10 #include "list-objects.h"
11 #include "pack.h"
12 #include "pack-bitmap.h"
13 #include "pack-revindex.h"
14 #include "pack-objects.h"
15 #include "packfile.h"
16 #include "repository.h"
17 #include "object-store.h"
18 #include "list-objects-filter-options.h"
19 #include "midx.h"
20 #include "config.h"
23 * An entry on the bitmap index, representing the bitmap for a given
24 * commit.
26 struct stored_bitmap {
27 struct object_id oid;
28 struct ewah_bitmap *root;
29 struct stored_bitmap *xor;
30 int flags;
34 * The active bitmap index for a repository. By design, repositories only have
35 * a single bitmap index available (the index for the biggest packfile in
36 * the repository), since bitmap indexes need full closure.
38 * If there is more than one bitmap index available (e.g. because of alternates),
39 * the active bitmap index is the largest one.
41 struct bitmap_index {
43 * The pack or multi-pack index (MIDX) that this bitmap index belongs
44 * to.
46 * Exactly one of these must be non-NULL; this specifies the object
47 * order used to interpret this bitmap.
49 struct packed_git *pack;
50 struct multi_pack_index *midx;
53 * Mark the first `reuse_objects` in the packfile as reused:
54 * they will be sent as-is without using them for repacking
55 * calculations
57 uint32_t reuse_objects;
59 /* mmapped buffer of the whole bitmap index */
60 unsigned char *map;
61 size_t map_size; /* size of the mmaped buffer */
62 size_t map_pos; /* current position when loading the index */
65 * Type indexes.
67 * Each bitmap marks which objects in the packfile are of the given
68 * type. This provides type information when yielding the objects from
69 * the packfile during a walk, which allows for better delta bases.
71 struct ewah_bitmap *commits;
72 struct ewah_bitmap *trees;
73 struct ewah_bitmap *blobs;
74 struct ewah_bitmap *tags;
76 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
77 kh_oid_map_t *bitmaps;
79 /* Number of bitmapped commits */
80 uint32_t entry_count;
82 /* If not NULL, this is a name-hash cache pointing into map. */
83 uint32_t *hashes;
85 /* The checksum of the packfile or MIDX; points into map. */
86 const unsigned char *checksum;
89 * If not NULL, this point into the commit table extension
90 * (within the memory mapped region `map`).
92 unsigned char *table_lookup;
95 * Extended index.
97 * When trying to perform bitmap operations with objects that are not
98 * packed in `pack`, these objects are added to this "fake index" and
99 * are assumed to appear at the end of the packfile for all operations
101 struct eindex {
102 struct object **objects;
103 uint32_t *hashes;
104 uint32_t count, alloc;
105 kh_oid_pos_t *positions;
106 } ext_index;
108 /* Bitmap result of the last performed walk */
109 struct bitmap *result;
111 /* "have" bitmap from the last performed walk */
112 struct bitmap *haves;
114 /* Version of the bitmap index */
115 unsigned int version;
118 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
120 struct ewah_bitmap *parent;
121 struct ewah_bitmap *composed;
123 if (!st->xor)
124 return st->root;
126 composed = ewah_pool_new();
127 parent = lookup_stored_bitmap(st->xor);
128 ewah_xor(st->root, parent, composed);
130 ewah_pool_free(st->root);
131 st->root = composed;
132 st->xor = NULL;
134 return composed;
138 * Read a bitmap from the current read position on the mmaped
139 * index, and increase the read position accordingly
141 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
143 struct ewah_bitmap *b = ewah_pool_new();
145 ssize_t bitmap_size = ewah_read_mmap(b,
146 index->map + index->map_pos,
147 index->map_size - index->map_pos);
149 if (bitmap_size < 0) {
150 error(_("failed to load bitmap index (corrupted?)"));
151 ewah_pool_free(b);
152 return NULL;
155 index->map_pos += bitmap_size;
156 return b;
159 static uint32_t bitmap_num_objects(struct bitmap_index *index)
161 if (index->midx)
162 return index->midx->num_objects;
163 return index->pack->num_objects;
166 static int load_bitmap_header(struct bitmap_index *index)
168 struct bitmap_disk_header *header = (void *)index->map;
169 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
171 if (index->map_size < header_size + the_hash_algo->rawsz)
172 return error(_("corrupted bitmap index (too small)"));
174 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
175 return error(_("corrupted bitmap index file (wrong header)"));
177 index->version = ntohs(header->version);
178 if (index->version != 1)
179 return error(_("unsupported version '%d' for bitmap index file"), index->version);
181 /* Parse known bitmap format options */
183 uint32_t flags = ntohs(header->options);
184 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
185 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
187 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
188 BUG("unsupported options for bitmap index file "
189 "(Git requires BITMAP_OPT_FULL_DAG)");
191 if (flags & BITMAP_OPT_HASH_CACHE) {
192 if (cache_size > index_end - index->map - header_size)
193 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
194 index->hashes = (void *)(index_end - cache_size);
195 index_end -= cache_size;
198 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
199 size_t table_size = st_mult(ntohl(header->entry_count),
200 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
201 if (table_size > index_end - index->map - header_size)
202 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
203 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
204 index->table_lookup = (void *)(index_end - table_size);
205 index_end -= table_size;
209 index->entry_count = ntohl(header->entry_count);
210 index->checksum = header->checksum;
211 index->map_pos += header_size;
212 return 0;
215 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
216 struct ewah_bitmap *root,
217 const struct object_id *oid,
218 struct stored_bitmap *xor_with,
219 int flags)
221 struct stored_bitmap *stored;
222 khiter_t hash_pos;
223 int ret;
225 stored = xmalloc(sizeof(struct stored_bitmap));
226 stored->root = root;
227 stored->xor = xor_with;
228 stored->flags = flags;
229 oidcpy(&stored->oid, oid);
231 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
234 * A 0 return code means the insertion succeeded with no changes,
235 * because the SHA1 already existed on the map. This is bad, there
236 * shouldn't be duplicated commits in the index.
238 if (ret == 0) {
239 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
240 return NULL;
243 kh_value(index->bitmaps, hash_pos) = stored;
244 return stored;
247 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
249 uint32_t result = get_be32(buffer + *pos);
250 (*pos) += sizeof(result);
251 return result;
254 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
256 return buffer[(*pos)++];
259 #define MAX_XOR_OFFSET 160
261 static int nth_bitmap_object_oid(struct bitmap_index *index,
262 struct object_id *oid,
263 uint32_t n)
265 if (index->midx)
266 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
267 return nth_packed_object_id(oid, index->pack, n);
270 static int load_bitmap_entries_v1(struct bitmap_index *index)
272 uint32_t i;
273 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
275 for (i = 0; i < index->entry_count; ++i) {
276 int xor_offset, flags;
277 struct ewah_bitmap *bitmap = NULL;
278 struct stored_bitmap *xor_bitmap = NULL;
279 uint32_t commit_idx_pos;
280 struct object_id oid;
282 if (index->map_size - index->map_pos < 6)
283 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
285 commit_idx_pos = read_be32(index->map, &index->map_pos);
286 xor_offset = read_u8(index->map, &index->map_pos);
287 flags = read_u8(index->map, &index->map_pos);
289 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
290 return error(_("corrupt ewah bitmap: commit index %u out of range"),
291 (unsigned)commit_idx_pos);
293 bitmap = read_bitmap_1(index);
294 if (!bitmap)
295 return -1;
297 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
298 return error(_("corrupted bitmap pack index"));
300 if (xor_offset > 0) {
301 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
303 if (!xor_bitmap)
304 return error(_("invalid XOR offset in bitmap pack index"));
307 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
308 index, bitmap, &oid, xor_bitmap, flags);
311 return 0;
314 char *midx_bitmap_filename(struct multi_pack_index *midx)
316 struct strbuf buf = STRBUF_INIT;
318 get_midx_filename(&buf, midx->object_dir);
319 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
321 return strbuf_detach(&buf, NULL);
324 char *pack_bitmap_filename(struct packed_git *p)
326 size_t len;
328 if (!strip_suffix(p->pack_name, ".pack", &len))
329 BUG("pack_name does not end in .pack");
330 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
333 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
334 struct multi_pack_index *midx)
336 struct stat st;
337 char *bitmap_name = midx_bitmap_filename(midx);
338 int fd = git_open(bitmap_name);
339 uint32_t i;
340 struct packed_git *preferred;
342 if (fd < 0) {
343 if (errno != ENOENT)
344 warning_errno("cannot open '%s'", bitmap_name);
345 free(bitmap_name);
346 return -1;
348 free(bitmap_name);
350 if (fstat(fd, &st)) {
351 error_errno(_("cannot fstat bitmap file"));
352 close(fd);
353 return -1;
356 if (bitmap_git->pack || bitmap_git->midx) {
357 struct strbuf buf = STRBUF_INIT;
358 get_midx_filename(&buf, midx->object_dir);
359 trace2_data_string("bitmap", the_repository,
360 "ignoring extra midx bitmap file", buf.buf);
361 close(fd);
362 strbuf_release(&buf);
363 return -1;
366 bitmap_git->midx = midx;
367 bitmap_git->map_size = xsize_t(st.st_size);
368 bitmap_git->map_pos = 0;
369 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
370 MAP_PRIVATE, fd, 0);
371 close(fd);
373 if (load_bitmap_header(bitmap_git) < 0)
374 goto cleanup;
376 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
377 error(_("checksum doesn't match in MIDX and bitmap"));
378 goto cleanup;
381 if (load_midx_revindex(bitmap_git->midx) < 0) {
382 warning(_("multi-pack bitmap is missing required reverse index"));
383 goto cleanup;
386 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
387 if (prepare_midx_pack(the_repository, bitmap_git->midx, i))
388 die(_("could not open pack %s"),
389 bitmap_git->midx->pack_names[i]);
392 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
393 if (!is_pack_valid(preferred)) {
394 warning(_("preferred pack (%s) is invalid"),
395 preferred->pack_name);
396 goto cleanup;
399 return 0;
401 cleanup:
402 munmap(bitmap_git->map, bitmap_git->map_size);
403 bitmap_git->map_size = 0;
404 bitmap_git->map_pos = 0;
405 bitmap_git->map = NULL;
406 bitmap_git->midx = NULL;
407 return -1;
410 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
412 int fd;
413 struct stat st;
414 char *bitmap_name;
416 bitmap_name = pack_bitmap_filename(packfile);
417 fd = git_open(bitmap_name);
419 if (fd < 0) {
420 if (errno != ENOENT)
421 warning_errno("cannot open '%s'", bitmap_name);
422 free(bitmap_name);
423 return -1;
425 free(bitmap_name);
427 if (fstat(fd, &st)) {
428 error_errno(_("cannot fstat bitmap file"));
429 close(fd);
430 return -1;
433 if (bitmap_git->pack || bitmap_git->midx) {
434 trace2_data_string("bitmap", the_repository,
435 "ignoring extra bitmap file", packfile->pack_name);
436 close(fd);
437 return -1;
440 if (!is_pack_valid(packfile)) {
441 close(fd);
442 return -1;
445 bitmap_git->pack = packfile;
446 bitmap_git->map_size = xsize_t(st.st_size);
447 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
448 bitmap_git->map_pos = 0;
449 close(fd);
451 if (load_bitmap_header(bitmap_git) < 0) {
452 munmap(bitmap_git->map, bitmap_git->map_size);
453 bitmap_git->map = NULL;
454 bitmap_git->map_size = 0;
455 bitmap_git->map_pos = 0;
456 bitmap_git->pack = NULL;
457 return -1;
460 trace2_data_string("bitmap", the_repository, "opened bitmap file",
461 packfile->pack_name);
462 return 0;
465 static int load_reverse_index(struct bitmap_index *bitmap_git)
467 if (bitmap_is_midx(bitmap_git)) {
468 uint32_t i;
469 int ret;
472 * The multi-pack-index's .rev file is already loaded via
473 * open_pack_bitmap_1().
475 * But we still need to open the individual pack .rev files,
476 * since we will need to make use of them in pack-objects.
478 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
479 ret = load_pack_revindex(bitmap_git->midx->packs[i]);
480 if (ret)
481 return ret;
483 return 0;
485 return load_pack_revindex(bitmap_git->pack);
488 static int load_bitmap(struct bitmap_index *bitmap_git)
490 assert(bitmap_git->map);
492 bitmap_git->bitmaps = kh_init_oid_map();
493 bitmap_git->ext_index.positions = kh_init_oid_pos();
495 if (load_reverse_index(bitmap_git))
496 goto failed;
498 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
499 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
500 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
501 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
502 goto failed;
504 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
505 goto failed;
507 return 0;
509 failed:
510 munmap(bitmap_git->map, bitmap_git->map_size);
511 bitmap_git->map = NULL;
512 bitmap_git->map_size = 0;
514 kh_destroy_oid_map(bitmap_git->bitmaps);
515 bitmap_git->bitmaps = NULL;
517 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
518 bitmap_git->ext_index.positions = NULL;
520 return -1;
523 static int open_pack_bitmap(struct repository *r,
524 struct bitmap_index *bitmap_git)
526 struct packed_git *p;
527 int ret = -1;
529 for (p = get_all_packs(r); p; p = p->next) {
530 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
531 ret = 0;
533 * The only reason to keep looking is to report
534 * duplicates.
536 if (!trace2_is_enabled())
537 break;
541 return ret;
544 static int open_midx_bitmap(struct repository *r,
545 struct bitmap_index *bitmap_git)
547 int ret = -1;
548 struct multi_pack_index *midx;
550 assert(!bitmap_git->map);
552 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
553 if (!open_midx_bitmap_1(bitmap_git, midx))
554 ret = 0;
556 return ret;
559 static int open_bitmap(struct repository *r,
560 struct bitmap_index *bitmap_git)
562 int found;
564 assert(!bitmap_git->map);
566 found = !open_midx_bitmap(r, bitmap_git);
569 * these will all be skipped if we opened a midx bitmap; but run it
570 * anyway if tracing is enabled to report the duplicates
572 if (!found || trace2_is_enabled())
573 found |= !open_pack_bitmap(r, bitmap_git);
575 return found ? 0 : -1;
578 struct bitmap_index *prepare_bitmap_git(struct repository *r)
580 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
582 if (!open_bitmap(r, bitmap_git) && !load_bitmap(bitmap_git))
583 return bitmap_git;
585 free_bitmap_index(bitmap_git);
586 return NULL;
589 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
591 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
593 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(bitmap_git))
594 return bitmap_git;
596 free_bitmap_index(bitmap_git);
597 return NULL;
600 struct include_data {
601 struct bitmap_index *bitmap_git;
602 struct bitmap *base;
603 struct bitmap *seen;
606 struct bitmap_lookup_table_triplet {
607 uint32_t commit_pos;
608 uint64_t offset;
609 uint32_t xor_row;
612 struct bitmap_lookup_table_xor_item {
613 struct object_id oid;
614 uint64_t offset;
618 * Given a `triplet` struct pointer and pointer `p`, this
619 * function reads the triplet beginning at `p` into the struct.
620 * Note that this function assumes that there is enough memory
621 * left for filling the `triplet` struct from `p`.
623 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
624 const unsigned char *p)
626 if (!triplet)
627 return -1;
629 triplet->commit_pos = get_be32(p);
630 p += sizeof(uint32_t);
631 triplet->offset = get_be64(p);
632 p += sizeof(uint64_t);
633 triplet->xor_row = get_be32(p);
634 return 0;
638 * This function gets the raw triplet from `row`'th row in the
639 * lookup table and fills that data to the `triplet`.
641 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
642 uint32_t pos,
643 struct bitmap_lookup_table_triplet *triplet)
645 unsigned char *p = NULL;
646 if (pos >= bitmap_git->entry_count)
647 return error(_("corrupt bitmap lookup table: triplet position out of index"));
649 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
651 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
655 * Searches for a matching triplet. `commit_pos` is a pointer
656 * to the wanted commit position value. `table_entry` points to
657 * a triplet in lookup table. The first 4 bytes of each
658 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
660 static int triplet_cmp(const void *commit_pos, const void *table_entry)
663 uint32_t a = *(uint32_t *)commit_pos;
664 uint32_t b = get_be32(table_entry);
665 if (a > b)
666 return 1;
667 else if (a < b)
668 return -1;
670 return 0;
673 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
674 struct object_id *oid,
675 uint32_t *result)
677 int found;
679 if (bitmap_is_midx(bitmap_git))
680 found = bsearch_midx(oid, bitmap_git->midx, result);
681 else
682 found = bsearch_pack(oid, bitmap_git->pack, result);
684 return found;
688 * `bsearch_triplet_by_pos` function searches for the raw triplet
689 * having commit position same as `commit_pos` and fills `triplet`
690 * object from the raw triplet. Returns 1 on success and 0 on
691 * failure.
693 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
694 struct bitmap_index *bitmap_git,
695 struct bitmap_lookup_table_triplet *triplet)
697 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
698 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
700 if (!p)
701 return -1;
703 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
706 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
707 struct commit *commit)
709 uint32_t commit_pos, xor_row;
710 uint64_t offset;
711 int flags;
712 struct bitmap_lookup_table_triplet triplet;
713 struct object_id *oid = &commit->object.oid;
714 struct ewah_bitmap *bitmap;
715 struct stored_bitmap *xor_bitmap = NULL;
716 const int bitmap_header_size = 6;
717 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
718 static size_t xor_items_nr = 0, xor_items_alloc = 0;
719 static int is_corrupt = 0;
720 int xor_flags;
721 khiter_t hash_pos;
722 struct bitmap_lookup_table_xor_item *xor_item;
724 if (is_corrupt)
725 return NULL;
727 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
728 return NULL;
730 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
731 return NULL;
733 xor_items_nr = 0;
734 offset = triplet.offset;
735 xor_row = triplet.xor_row;
737 while (xor_row != 0xffffffff) {
738 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
740 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
741 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
742 goto corrupt;
745 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
746 goto corrupt;
748 xor_item = &xor_items[xor_items_nr];
749 xor_item->offset = triplet.offset;
751 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
752 error(_("corrupt bitmap lookup table: commit index %u out of range"),
753 triplet.commit_pos);
754 goto corrupt;
757 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
760 * If desired bitmap is already stored, we don't need
761 * to iterate further. Because we know that bitmaps
762 * that are needed to be parsed to parse this bitmap
763 * has already been stored. So, assign this stored bitmap
764 * to the xor_bitmap.
766 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
767 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
768 break;
769 xor_items_nr++;
770 xor_row = triplet.xor_row;
773 while (xor_items_nr) {
774 xor_item = &xor_items[xor_items_nr - 1];
775 bitmap_git->map_pos = xor_item->offset;
776 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
777 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
778 oid_to_hex(&xor_item->oid));
779 goto corrupt;
782 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
783 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
784 bitmap = read_bitmap_1(bitmap_git);
786 if (!bitmap)
787 goto corrupt;
789 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
790 xor_items_nr--;
793 bitmap_git->map_pos = offset;
794 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
795 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
796 oid_to_hex(oid));
797 goto corrupt;
801 * Don't bother reading the commit's index position or its xor
802 * offset:
804 * - The commit's index position is irrelevant to us, since
805 * load_bitmap_entries_v1 only uses it to learn the object
806 * id which is used to compute the hashmap's key. We already
807 * have an object id, so no need to look it up again.
809 * - The xor_offset is unusable for us, since it specifies how
810 * many entries previous to ours we should look at. This
811 * makes sense when reading the bitmaps sequentially (as in
812 * load_bitmap_entries_v1()), since we can keep track of
813 * each bitmap as we read them.
815 * But it can't work for us, since the bitmap's don't have a
816 * fixed size. So we learn the position of the xor'd bitmap
817 * from the commit table (and resolve it to a bitmap in the
818 * above if-statement).
820 * Instead, we can skip ahead and immediately read the flags and
821 * ewah bitmap.
823 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
824 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
825 bitmap = read_bitmap_1(bitmap_git);
827 if (!bitmap)
828 goto corrupt;
830 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
832 corrupt:
833 free(xor_items);
834 is_corrupt = 1;
835 return NULL;
838 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
839 struct commit *commit)
841 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
842 commit->object.oid);
843 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
844 struct stored_bitmap *bitmap = NULL;
845 if (!bitmap_git->table_lookup)
846 return NULL;
848 /* this is a fairly hot codepath - no trace2_region please */
849 /* NEEDSWORK: cache misses aren't recorded */
850 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
851 if (!bitmap)
852 return NULL;
853 return lookup_stored_bitmap(bitmap);
855 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
858 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
859 const struct object_id *oid)
861 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
862 khiter_t pos = kh_get_oid_pos(positions, *oid);
864 if (pos < kh_end(positions)) {
865 int bitmap_pos = kh_value(positions, pos);
866 return bitmap_pos + bitmap_num_objects(bitmap_git);
869 return -1;
872 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
873 const struct object_id *oid)
875 uint32_t pos;
876 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
877 if (!offset)
878 return -1;
880 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
881 return -1;
882 return pos;
885 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
886 const struct object_id *oid)
888 uint32_t want, got;
889 if (!bsearch_midx(oid, bitmap_git->midx, &want))
890 return -1;
892 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
893 return -1;
894 return got;
897 static int bitmap_position(struct bitmap_index *bitmap_git,
898 const struct object_id *oid)
900 int pos;
901 if (bitmap_is_midx(bitmap_git))
902 pos = bitmap_position_midx(bitmap_git, oid);
903 else
904 pos = bitmap_position_packfile(bitmap_git, oid);
905 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
908 static int ext_index_add_object(struct bitmap_index *bitmap_git,
909 struct object *object, const char *name)
911 struct eindex *eindex = &bitmap_git->ext_index;
913 khiter_t hash_pos;
914 int hash_ret;
915 int bitmap_pos;
917 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
918 if (hash_ret > 0) {
919 if (eindex->count >= eindex->alloc) {
920 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
921 REALLOC_ARRAY(eindex->objects, eindex->alloc);
922 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
925 bitmap_pos = eindex->count;
926 eindex->objects[eindex->count] = object;
927 eindex->hashes[eindex->count] = pack_name_hash(name);
928 kh_value(eindex->positions, hash_pos) = bitmap_pos;
929 eindex->count++;
930 } else {
931 bitmap_pos = kh_value(eindex->positions, hash_pos);
934 return bitmap_pos + bitmap_num_objects(bitmap_git);
937 struct bitmap_show_data {
938 struct bitmap_index *bitmap_git;
939 struct bitmap *base;
942 static void show_object(struct object *object, const char *name, void *data_)
944 struct bitmap_show_data *data = data_;
945 int bitmap_pos;
947 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
949 if (bitmap_pos < 0)
950 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
951 name);
953 bitmap_set(data->base, bitmap_pos);
956 static void show_commit(struct commit *commit UNUSED,
957 void *data UNUSED)
961 static int add_to_include_set(struct bitmap_index *bitmap_git,
962 struct include_data *data,
963 struct commit *commit,
964 int bitmap_pos)
966 struct ewah_bitmap *partial;
968 if (data->seen && bitmap_get(data->seen, bitmap_pos))
969 return 0;
971 if (bitmap_get(data->base, bitmap_pos))
972 return 0;
974 partial = bitmap_for_commit(bitmap_git, commit);
975 if (partial) {
976 bitmap_or_ewah(data->base, partial);
977 return 0;
980 bitmap_set(data->base, bitmap_pos);
981 return 1;
984 static int should_include(struct commit *commit, void *_data)
986 struct include_data *data = _data;
987 int bitmap_pos;
989 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
990 if (bitmap_pos < 0)
991 bitmap_pos = ext_index_add_object(data->bitmap_git,
992 (struct object *)commit,
993 NULL);
995 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
996 struct commit_list *parent = commit->parents;
998 while (parent) {
999 parent->item->object.flags |= SEEN;
1000 parent = parent->next;
1003 return 0;
1006 return 1;
1009 static int should_include_obj(struct object *obj, void *_data)
1011 struct include_data *data = _data;
1012 int bitmap_pos;
1014 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1015 if (bitmap_pos < 0)
1016 return 1;
1017 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1018 bitmap_get(data->base, bitmap_pos)) {
1019 obj->flags |= SEEN;
1020 return 0;
1022 return 1;
1025 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1026 struct bitmap **base,
1027 struct commit *commit)
1029 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1031 if (!or_with)
1032 return 0;
1034 if (!*base)
1035 *base = ewah_to_bitmap(or_with);
1036 else
1037 bitmap_or_ewah(*base, or_with);
1039 return 1;
1042 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1043 struct rev_info *revs,
1044 struct object_list *roots,
1045 struct bitmap *seen)
1047 struct bitmap *base = NULL;
1048 int needs_walk = 0;
1050 struct object_list *not_mapped = NULL;
1053 * Go through all the roots for the walk. The ones that have bitmaps
1054 * on the bitmap index will be `or`ed together to form an initial
1055 * global reachability analysis.
1057 * The ones without bitmaps in the index will be stored in the
1058 * `not_mapped_list` for further processing.
1060 while (roots) {
1061 struct object *object = roots->item;
1062 roots = roots->next;
1064 if (object->type == OBJ_COMMIT &&
1065 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1066 object->flags |= SEEN;
1067 continue;
1070 object_list_insert(object, &not_mapped);
1074 * Best case scenario: We found bitmaps for all the roots,
1075 * so the resulting `or` bitmap has the full reachability analysis
1077 if (!not_mapped)
1078 return base;
1080 roots = not_mapped;
1083 * Let's iterate through all the roots that don't have bitmaps to
1084 * check if we can determine them to be reachable from the existing
1085 * global bitmap.
1087 * If we cannot find them in the existing global bitmap, we'll need
1088 * to push them to an actual walk and run it until we can confirm
1089 * they are reachable
1091 while (roots) {
1092 struct object *object = roots->item;
1093 int pos;
1095 roots = roots->next;
1096 pos = bitmap_position(bitmap_git, &object->oid);
1098 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1099 object->flags &= ~UNINTERESTING;
1100 add_pending_object(revs, object, "");
1101 needs_walk = 1;
1102 } else {
1103 object->flags |= SEEN;
1107 if (needs_walk) {
1108 struct include_data incdata;
1109 struct bitmap_show_data show_data;
1111 if (!base)
1112 base = bitmap_new();
1114 incdata.bitmap_git = bitmap_git;
1115 incdata.base = base;
1116 incdata.seen = seen;
1118 revs->include_check = should_include;
1119 revs->include_check_obj = should_include_obj;
1120 revs->include_check_data = &incdata;
1122 if (prepare_revision_walk(revs))
1123 die(_("revision walk setup failed"));
1125 show_data.bitmap_git = bitmap_git;
1126 show_data.base = base;
1128 traverse_commit_list(revs,
1129 show_commit, show_object,
1130 &show_data);
1132 revs->include_check = NULL;
1133 revs->include_check_obj = NULL;
1134 revs->include_check_data = NULL;
1137 return base;
1140 static void show_extended_objects(struct bitmap_index *bitmap_git,
1141 struct rev_info *revs,
1142 show_reachable_fn show_reach)
1144 struct bitmap *objects = bitmap_git->result;
1145 struct eindex *eindex = &bitmap_git->ext_index;
1146 uint32_t i;
1148 for (i = 0; i < eindex->count; ++i) {
1149 struct object *obj;
1151 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1152 continue;
1154 obj = eindex->objects[i];
1155 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1156 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1157 (obj->type == OBJ_TAG && !revs->tag_objects))
1158 continue;
1160 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1164 static void init_type_iterator(struct ewah_iterator *it,
1165 struct bitmap_index *bitmap_git,
1166 enum object_type type)
1168 switch (type) {
1169 case OBJ_COMMIT:
1170 ewah_iterator_init(it, bitmap_git->commits);
1171 break;
1173 case OBJ_TREE:
1174 ewah_iterator_init(it, bitmap_git->trees);
1175 break;
1177 case OBJ_BLOB:
1178 ewah_iterator_init(it, bitmap_git->blobs);
1179 break;
1181 case OBJ_TAG:
1182 ewah_iterator_init(it, bitmap_git->tags);
1183 break;
1185 default:
1186 BUG("object type %d not stored by bitmap type index", type);
1187 break;
1191 static void show_objects_for_type(
1192 struct bitmap_index *bitmap_git,
1193 enum object_type object_type,
1194 show_reachable_fn show_reach)
1196 size_t i = 0;
1197 uint32_t offset;
1199 struct ewah_iterator it;
1200 eword_t filter;
1202 struct bitmap *objects = bitmap_git->result;
1204 init_type_iterator(&it, bitmap_git, object_type);
1206 for (i = 0; i < objects->word_alloc &&
1207 ewah_iterator_next(&filter, &it); i++) {
1208 eword_t word = objects->words[i] & filter;
1209 size_t pos = (i * BITS_IN_EWORD);
1211 if (!word)
1212 continue;
1214 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1215 struct packed_git *pack;
1216 struct object_id oid;
1217 uint32_t hash = 0, index_pos;
1218 off_t ofs;
1220 if ((word >> offset) == 0)
1221 break;
1223 offset += ewah_bit_ctz64(word >> offset);
1225 if (bitmap_is_midx(bitmap_git)) {
1226 struct multi_pack_index *m = bitmap_git->midx;
1227 uint32_t pack_id;
1229 index_pos = pack_pos_to_midx(m, pos + offset);
1230 ofs = nth_midxed_offset(m, index_pos);
1231 nth_midxed_object_oid(&oid, m, index_pos);
1233 pack_id = nth_midxed_pack_int_id(m, index_pos);
1234 pack = bitmap_git->midx->packs[pack_id];
1235 } else {
1236 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1237 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1238 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1240 pack = bitmap_git->pack;
1243 if (bitmap_git->hashes)
1244 hash = get_be32(bitmap_git->hashes + index_pos);
1246 show_reach(&oid, object_type, 0, hash, pack, ofs);
1251 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1252 struct object_list *roots)
1254 while (roots) {
1255 struct object *object = roots->item;
1256 roots = roots->next;
1258 if (bitmap_is_midx(bitmap_git)) {
1259 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1260 return 1;
1261 } else {
1262 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1263 return 1;
1267 return 0;
1270 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1271 struct object_list *tip_objects,
1272 enum object_type type)
1274 struct bitmap *result = bitmap_new();
1275 struct object_list *p;
1277 for (p = tip_objects; p; p = p->next) {
1278 int pos;
1280 if (p->item->type != type)
1281 continue;
1283 pos = bitmap_position(bitmap_git, &p->item->oid);
1284 if (pos < 0)
1285 continue;
1287 bitmap_set(result, pos);
1290 return result;
1293 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1294 struct object_list *tip_objects,
1295 struct bitmap *to_filter,
1296 enum object_type type)
1298 struct eindex *eindex = &bitmap_git->ext_index;
1299 struct bitmap *tips;
1300 struct ewah_iterator it;
1301 eword_t mask;
1302 uint32_t i;
1305 * The non-bitmap version of this filter never removes
1306 * objects which the other side specifically asked for,
1307 * so we must match that behavior.
1309 tips = find_tip_objects(bitmap_git, tip_objects, type);
1312 * We can use the type-level bitmap for 'type' to work in whole
1313 * words for the objects that are actually in the bitmapped
1314 * packfile.
1316 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1317 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1318 i++) {
1319 if (i < tips->word_alloc)
1320 mask &= ~tips->words[i];
1321 to_filter->words[i] &= ~mask;
1325 * Clear any objects that weren't in the packfile (and so would
1326 * not have been caught by the loop above. We'll have to check
1327 * them individually.
1329 for (i = 0; i < eindex->count; i++) {
1330 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1331 if (eindex->objects[i]->type == type &&
1332 bitmap_get(to_filter, pos) &&
1333 !bitmap_get(tips, pos))
1334 bitmap_unset(to_filter, pos);
1337 bitmap_free(tips);
1340 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1341 struct object_list *tip_objects,
1342 struct bitmap *to_filter)
1344 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1345 OBJ_BLOB);
1348 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1349 uint32_t pos)
1351 unsigned long size;
1352 struct object_info oi = OBJECT_INFO_INIT;
1354 oi.sizep = &size;
1356 if (pos < bitmap_num_objects(bitmap_git)) {
1357 struct packed_git *pack;
1358 off_t ofs;
1360 if (bitmap_is_midx(bitmap_git)) {
1361 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1362 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1364 pack = bitmap_git->midx->packs[pack_id];
1365 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1366 } else {
1367 pack = bitmap_git->pack;
1368 ofs = pack_pos_to_offset(pack, pos);
1371 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1372 struct object_id oid;
1373 nth_bitmap_object_oid(bitmap_git, &oid,
1374 pack_pos_to_index(pack, pos));
1375 die(_("unable to get size of %s"), oid_to_hex(&oid));
1377 } else {
1378 struct eindex *eindex = &bitmap_git->ext_index;
1379 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1380 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1381 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1384 return size;
1387 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1388 struct object_list *tip_objects,
1389 struct bitmap *to_filter,
1390 unsigned long limit)
1392 struct eindex *eindex = &bitmap_git->ext_index;
1393 struct bitmap *tips;
1394 struct ewah_iterator it;
1395 eword_t mask;
1396 uint32_t i;
1398 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1400 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1401 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1402 i++) {
1403 eword_t word = to_filter->words[i] & mask;
1404 unsigned offset;
1406 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1407 uint32_t pos;
1409 if ((word >> offset) == 0)
1410 break;
1411 offset += ewah_bit_ctz64(word >> offset);
1412 pos = i * BITS_IN_EWORD + offset;
1414 if (!bitmap_get(tips, pos) &&
1415 get_size_by_pos(bitmap_git, pos) >= limit)
1416 bitmap_unset(to_filter, pos);
1420 for (i = 0; i < eindex->count; i++) {
1421 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1422 if (eindex->objects[i]->type == OBJ_BLOB &&
1423 bitmap_get(to_filter, pos) &&
1424 !bitmap_get(tips, pos) &&
1425 get_size_by_pos(bitmap_git, pos) >= limit)
1426 bitmap_unset(to_filter, pos);
1429 bitmap_free(tips);
1432 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1433 struct object_list *tip_objects,
1434 struct bitmap *to_filter,
1435 unsigned long limit)
1437 if (limit)
1438 BUG("filter_bitmap_tree_depth given non-zero limit");
1440 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1441 OBJ_TREE);
1442 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1443 OBJ_BLOB);
1446 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1447 struct object_list *tip_objects,
1448 struct bitmap *to_filter,
1449 enum object_type object_type)
1451 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1452 BUG("filter_bitmap_object_type given invalid object");
1454 if (object_type != OBJ_TAG)
1455 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1456 if (object_type != OBJ_COMMIT)
1457 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1458 if (object_type != OBJ_TREE)
1459 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1460 if (object_type != OBJ_BLOB)
1461 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1464 static int filter_bitmap(struct bitmap_index *bitmap_git,
1465 struct object_list *tip_objects,
1466 struct bitmap *to_filter,
1467 struct list_objects_filter_options *filter)
1469 if (!filter || filter->choice == LOFC_DISABLED)
1470 return 0;
1472 if (filter->choice == LOFC_BLOB_NONE) {
1473 if (bitmap_git)
1474 filter_bitmap_blob_none(bitmap_git, tip_objects,
1475 to_filter);
1476 return 0;
1479 if (filter->choice == LOFC_BLOB_LIMIT) {
1480 if (bitmap_git)
1481 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1482 to_filter,
1483 filter->blob_limit_value);
1484 return 0;
1487 if (filter->choice == LOFC_TREE_DEPTH &&
1488 filter->tree_exclude_depth == 0) {
1489 if (bitmap_git)
1490 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1491 to_filter,
1492 filter->tree_exclude_depth);
1493 return 0;
1496 if (filter->choice == LOFC_OBJECT_TYPE) {
1497 if (bitmap_git)
1498 filter_bitmap_object_type(bitmap_git, tip_objects,
1499 to_filter,
1500 filter->object_type);
1501 return 0;
1504 if (filter->choice == LOFC_COMBINE) {
1505 int i;
1506 for (i = 0; i < filter->sub_nr; i++) {
1507 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1508 &filter->sub[i]) < 0)
1509 return -1;
1511 return 0;
1514 /* filter choice not handled */
1515 return -1;
1518 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1520 return !filter_bitmap(NULL, NULL, NULL, filter);
1523 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1524 int filter_provided_objects)
1526 unsigned int i;
1528 struct object_list *wants = NULL;
1529 struct object_list *haves = NULL;
1531 struct bitmap *wants_bitmap = NULL;
1532 struct bitmap *haves_bitmap = NULL;
1534 struct bitmap_index *bitmap_git;
1537 * We can't do pathspec limiting with bitmaps, because we don't know
1538 * which commits are associated with which object changes (let alone
1539 * even which objects are associated with which paths).
1541 if (revs->prune)
1542 return NULL;
1544 if (!can_filter_bitmap(&revs->filter))
1545 return NULL;
1547 /* try to open a bitmapped pack, but don't parse it yet
1548 * because we may not need to use it */
1549 CALLOC_ARRAY(bitmap_git, 1);
1550 if (open_bitmap(revs->repo, bitmap_git) < 0)
1551 goto cleanup;
1553 for (i = 0; i < revs->pending.nr; ++i) {
1554 struct object *object = revs->pending.objects[i].item;
1556 if (object->type == OBJ_NONE)
1557 parse_object_or_die(&object->oid, NULL);
1559 while (object->type == OBJ_TAG) {
1560 struct tag *tag = (struct tag *) object;
1562 if (object->flags & UNINTERESTING)
1563 object_list_insert(object, &haves);
1564 else
1565 object_list_insert(object, &wants);
1567 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1568 object->flags |= (tag->object.flags & UNINTERESTING);
1571 if (object->flags & UNINTERESTING)
1572 object_list_insert(object, &haves);
1573 else
1574 object_list_insert(object, &wants);
1578 * if we have a HAVES list, but none of those haves is contained
1579 * in the packfile that has a bitmap, we don't have anything to
1580 * optimize here
1582 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1583 goto cleanup;
1585 /* if we don't want anything, we're done here */
1586 if (!wants)
1587 goto cleanup;
1590 * now we're going to use bitmaps, so load the actual bitmap entries
1591 * from disk. this is the point of no return; after this the rev_list
1592 * becomes invalidated and we must perform the revwalk through bitmaps
1594 if (load_bitmap(bitmap_git) < 0)
1595 goto cleanup;
1597 object_array_clear(&revs->pending);
1599 if (haves) {
1600 revs->ignore_missing_links = 1;
1601 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1602 reset_revision_walk();
1603 revs->ignore_missing_links = 0;
1605 if (!haves_bitmap)
1606 BUG("failed to perform bitmap walk");
1609 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1611 if (!wants_bitmap)
1612 BUG("failed to perform bitmap walk");
1614 if (haves_bitmap)
1615 bitmap_and_not(wants_bitmap, haves_bitmap);
1617 filter_bitmap(bitmap_git,
1618 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1619 wants_bitmap,
1620 &revs->filter);
1622 bitmap_git->result = wants_bitmap;
1623 bitmap_git->haves = haves_bitmap;
1625 object_list_free(&wants);
1626 object_list_free(&haves);
1628 return bitmap_git;
1630 cleanup:
1631 free_bitmap_index(bitmap_git);
1632 object_list_free(&wants);
1633 object_list_free(&haves);
1634 return NULL;
1638 * -1 means "stop trying further objects"; 0 means we may or may not have
1639 * reused, but you can keep feeding bits.
1641 static int try_partial_reuse(struct packed_git *pack,
1642 size_t pos,
1643 struct bitmap *reuse,
1644 struct pack_window **w_curs)
1646 off_t offset, delta_obj_offset;
1647 enum object_type type;
1648 unsigned long size;
1651 * try_partial_reuse() is called either on (a) objects in the
1652 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1653 * objects in the preferred pack of a multi-pack bitmap.
1654 * Importantly, the latter can pretend as if only a single pack
1655 * exists because:
1657 * - The first pack->num_objects bits of a MIDX bitmap are
1658 * reserved for the preferred pack, and
1660 * - Ties due to duplicate objects are always resolved in
1661 * favor of the preferred pack.
1663 * Therefore we do not need to ever ask the MIDX for its copy of
1664 * an object by OID, since it will always select it from the
1665 * preferred pack. Likewise, the selected copy of the base
1666 * object for any deltas will reside in the same pack.
1668 * This means that we can reuse pos when looking up the bit in
1669 * the reuse bitmap, too, since bits corresponding to the
1670 * preferred pack precede all bits from other packs.
1673 if (pos >= pack->num_objects)
1674 return -1; /* not actually in the pack or MIDX preferred pack */
1676 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1677 type = unpack_object_header(pack, w_curs, &offset, &size);
1678 if (type < 0)
1679 return -1; /* broken packfile, punt */
1681 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1682 off_t base_offset;
1683 uint32_t base_pos;
1686 * Find the position of the base object so we can look it up
1687 * in our bitmaps. If we can't come up with an offset, or if
1688 * that offset is not in the revidx, the pack is corrupt.
1689 * There's nothing we can do, so just punt on this object,
1690 * and the normal slow path will complain about it in
1691 * more detail.
1693 base_offset = get_delta_base(pack, w_curs, &offset, type,
1694 delta_obj_offset);
1695 if (!base_offset)
1696 return 0;
1697 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1698 return 0;
1701 * We assume delta dependencies always point backwards. This
1702 * lets us do a single pass, and is basically always true
1703 * due to the way OFS_DELTAs work. You would not typically
1704 * find REF_DELTA in a bitmapped pack, since we only bitmap
1705 * packs we write fresh, and OFS_DELTA is the default). But
1706 * let's double check to make sure the pack wasn't written with
1707 * odd parameters.
1709 if (base_pos >= pos)
1710 return 0;
1713 * And finally, if we're not sending the base as part of our
1714 * reuse chunk, then don't send this object either. The base
1715 * would come after us, along with other objects not
1716 * necessarily in the pack, which means we'd need to convert
1717 * to REF_DELTA on the fly. Better to just let the normal
1718 * object_entry code path handle it.
1720 if (!bitmap_get(reuse, base_pos))
1721 return 0;
1725 * If we got here, then the object is OK to reuse. Mark it.
1727 bitmap_set(reuse, pos);
1728 return 0;
1731 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1733 struct multi_pack_index *m = bitmap_git->midx;
1734 if (!m)
1735 BUG("midx_preferred_pack: requires non-empty MIDX");
1736 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1739 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1740 struct packed_git **packfile_out,
1741 uint32_t *entries,
1742 struct bitmap **reuse_out)
1744 struct packed_git *pack;
1745 struct bitmap *result = bitmap_git->result;
1746 struct bitmap *reuse;
1747 struct pack_window *w_curs = NULL;
1748 size_t i = 0;
1749 uint32_t offset;
1750 uint32_t objects_nr;
1752 assert(result);
1754 load_reverse_index(bitmap_git);
1756 if (bitmap_is_midx(bitmap_git))
1757 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1758 else
1759 pack = bitmap_git->pack;
1760 objects_nr = pack->num_objects;
1762 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1763 i++;
1766 * Don't mark objects not in the packfile or preferred pack. This bitmap
1767 * marks objects eligible for reuse, but the pack-reuse code only
1768 * understands how to reuse a single pack. Since the preferred pack is
1769 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1770 * we use it instead of another pack. In single-pack bitmaps, the choice
1771 * is made for us.
1773 if (i > objects_nr / BITS_IN_EWORD)
1774 i = objects_nr / BITS_IN_EWORD;
1776 reuse = bitmap_word_alloc(i);
1777 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1779 for (; i < result->word_alloc; ++i) {
1780 eword_t word = result->words[i];
1781 size_t pos = (i * BITS_IN_EWORD);
1783 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1784 if ((word >> offset) == 0)
1785 break;
1787 offset += ewah_bit_ctz64(word >> offset);
1788 if (try_partial_reuse(pack, pos + offset,
1789 reuse, &w_curs) < 0) {
1791 * try_partial_reuse indicated we couldn't reuse
1792 * any bits, so there is no point in trying more
1793 * bits in the current word, or any other words
1794 * in result.
1796 * Jump out of both loops to avoid future
1797 * unnecessary calls to try_partial_reuse.
1799 goto done;
1804 done:
1805 unuse_pack(&w_curs);
1807 *entries = bitmap_popcount(reuse);
1808 if (!*entries) {
1809 bitmap_free(reuse);
1810 return -1;
1814 * Drop any reused objects from the result, since they will not
1815 * need to be handled separately.
1817 bitmap_and_not(result, reuse);
1818 *packfile_out = pack;
1819 *reuse_out = reuse;
1820 return 0;
1823 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1824 struct bitmap *bitmap, const struct object_id *oid)
1826 int idx;
1828 if (!bitmap)
1829 return 0;
1831 idx = bitmap_position(bitmap_git, oid);
1832 return idx >= 0 && bitmap_get(bitmap, idx);
1835 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1836 struct rev_info *revs,
1837 show_reachable_fn show_reachable)
1839 assert(bitmap_git->result);
1841 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1842 if (revs->tree_objects)
1843 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1844 if (revs->blob_objects)
1845 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1846 if (revs->tag_objects)
1847 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1849 show_extended_objects(bitmap_git, revs, show_reachable);
1852 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1853 enum object_type type)
1855 struct bitmap *objects = bitmap_git->result;
1856 struct eindex *eindex = &bitmap_git->ext_index;
1858 uint32_t i = 0, count = 0;
1859 struct ewah_iterator it;
1860 eword_t filter;
1862 init_type_iterator(&it, bitmap_git, type);
1864 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1865 eword_t word = objects->words[i++] & filter;
1866 count += ewah_bit_popcount64(word);
1869 for (i = 0; i < eindex->count; ++i) {
1870 if (eindex->objects[i]->type == type &&
1871 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1872 count++;
1875 return count;
1878 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1879 uint32_t *commits, uint32_t *trees,
1880 uint32_t *blobs, uint32_t *tags)
1882 assert(bitmap_git->result);
1884 if (commits)
1885 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1887 if (trees)
1888 *trees = count_object_type(bitmap_git, OBJ_TREE);
1890 if (blobs)
1891 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1893 if (tags)
1894 *tags = count_object_type(bitmap_git, OBJ_TAG);
1897 struct bitmap_test_data {
1898 struct bitmap_index *bitmap_git;
1899 struct bitmap *base;
1900 struct bitmap *commits;
1901 struct bitmap *trees;
1902 struct bitmap *blobs;
1903 struct bitmap *tags;
1904 struct progress *prg;
1905 size_t seen;
1908 static void test_bitmap_type(struct bitmap_test_data *tdata,
1909 struct object *obj, int pos)
1911 enum object_type bitmap_type = OBJ_NONE;
1912 int bitmaps_nr = 0;
1914 if (bitmap_get(tdata->commits, pos)) {
1915 bitmap_type = OBJ_COMMIT;
1916 bitmaps_nr++;
1918 if (bitmap_get(tdata->trees, pos)) {
1919 bitmap_type = OBJ_TREE;
1920 bitmaps_nr++;
1922 if (bitmap_get(tdata->blobs, pos)) {
1923 bitmap_type = OBJ_BLOB;
1924 bitmaps_nr++;
1926 if (bitmap_get(tdata->tags, pos)) {
1927 bitmap_type = OBJ_TAG;
1928 bitmaps_nr++;
1931 if (bitmap_type == OBJ_NONE)
1932 die(_("object '%s' not found in type bitmaps"),
1933 oid_to_hex(&obj->oid));
1935 if (bitmaps_nr > 1)
1936 die(_("object '%s' does not have a unique type"),
1937 oid_to_hex(&obj->oid));
1939 if (bitmap_type != obj->type)
1940 die(_("object '%s': real type '%s', expected: '%s'"),
1941 oid_to_hex(&obj->oid),
1942 type_name(obj->type),
1943 type_name(bitmap_type));
1946 static void test_show_object(struct object *object,
1947 const char *name UNUSED,
1948 void *data)
1950 struct bitmap_test_data *tdata = data;
1951 int bitmap_pos;
1953 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1954 if (bitmap_pos < 0)
1955 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
1956 test_bitmap_type(tdata, object, bitmap_pos);
1958 bitmap_set(tdata->base, bitmap_pos);
1959 display_progress(tdata->prg, ++tdata->seen);
1962 static void test_show_commit(struct commit *commit, void *data)
1964 struct bitmap_test_data *tdata = data;
1965 int bitmap_pos;
1967 bitmap_pos = bitmap_position(tdata->bitmap_git,
1968 &commit->object.oid);
1969 if (bitmap_pos < 0)
1970 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
1971 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1973 bitmap_set(tdata->base, bitmap_pos);
1974 display_progress(tdata->prg, ++tdata->seen);
1977 void test_bitmap_walk(struct rev_info *revs)
1979 struct object *root;
1980 struct bitmap *result = NULL;
1981 size_t result_popcnt;
1982 struct bitmap_test_data tdata;
1983 struct bitmap_index *bitmap_git;
1984 struct ewah_bitmap *bm;
1986 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1987 die(_("failed to load bitmap indexes"));
1989 if (revs->pending.nr != 1)
1990 die(_("you must specify exactly one commit to test"));
1992 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
1993 bitmap_git->version,
1994 bitmap_git->entry_count,
1995 bitmap_git->table_lookup ? "" : " loaded");
1997 root = revs->pending.objects[0].item;
1998 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2000 if (bm) {
2001 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2002 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2004 result = ewah_to_bitmap(bm);
2007 if (!result)
2008 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2010 revs->tag_objects = 1;
2011 revs->tree_objects = 1;
2012 revs->blob_objects = 1;
2014 result_popcnt = bitmap_popcount(result);
2016 if (prepare_revision_walk(revs))
2017 die(_("revision walk setup failed"));
2019 tdata.bitmap_git = bitmap_git;
2020 tdata.base = bitmap_new();
2021 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2022 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2023 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2024 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2025 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2026 tdata.seen = 0;
2028 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2030 stop_progress(&tdata.prg);
2032 if (bitmap_equals(result, tdata.base))
2033 fprintf_ln(stderr, "OK!");
2034 else
2035 die(_("mismatch in bitmap results"));
2037 bitmap_free(result);
2038 bitmap_free(tdata.base);
2039 bitmap_free(tdata.commits);
2040 bitmap_free(tdata.trees);
2041 bitmap_free(tdata.blobs);
2042 bitmap_free(tdata.tags);
2043 free_bitmap_index(bitmap_git);
2046 int test_bitmap_commits(struct repository *r)
2048 struct object_id oid;
2049 MAYBE_UNUSED void *value;
2050 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2052 if (!bitmap_git)
2053 die(_("failed to load bitmap indexes"));
2056 * As this function is only used to print bitmap selected
2057 * commits, we don't have to read the commit table.
2059 if (bitmap_git->table_lookup) {
2060 if (load_bitmap_entries_v1(bitmap_git) < 0)
2061 die(_("failed to load bitmap indexes"));
2064 kh_foreach(bitmap_git->bitmaps, oid, value, {
2065 printf_ln("%s", oid_to_hex(&oid));
2068 free_bitmap_index(bitmap_git);
2070 return 0;
2073 int test_bitmap_hashes(struct repository *r)
2075 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2076 struct object_id oid;
2077 uint32_t i, index_pos;
2079 if (!bitmap_git || !bitmap_git->hashes)
2080 goto cleanup;
2082 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2083 if (bitmap_is_midx(bitmap_git))
2084 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2085 else
2086 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2088 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2090 printf_ln("%s %"PRIu32"",
2091 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2094 cleanup:
2095 free_bitmap_index(bitmap_git);
2097 return 0;
2100 int rebuild_bitmap(const uint32_t *reposition,
2101 struct ewah_bitmap *source,
2102 struct bitmap *dest)
2104 uint32_t pos = 0;
2105 struct ewah_iterator it;
2106 eword_t word;
2108 ewah_iterator_init(&it, source);
2110 while (ewah_iterator_next(&word, &it)) {
2111 uint32_t offset, bit_pos;
2113 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2114 if ((word >> offset) == 0)
2115 break;
2117 offset += ewah_bit_ctz64(word >> offset);
2119 bit_pos = reposition[pos + offset];
2120 if (bit_pos > 0)
2121 bitmap_set(dest, bit_pos - 1);
2122 else /* can't reuse, we don't have the object */
2123 return -1;
2126 pos += BITS_IN_EWORD;
2128 return 0;
2131 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2132 struct packing_data *mapping)
2134 uint32_t i, num_objects;
2135 uint32_t *reposition;
2137 if (!bitmap_is_midx(bitmap_git))
2138 load_reverse_index(bitmap_git);
2139 else if (load_midx_revindex(bitmap_git->midx) < 0)
2140 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2141 "extension");
2143 num_objects = bitmap_num_objects(bitmap_git);
2144 CALLOC_ARRAY(reposition, num_objects);
2146 for (i = 0; i < num_objects; ++i) {
2147 struct object_id oid;
2148 struct object_entry *oe;
2149 uint32_t index_pos;
2151 if (bitmap_is_midx(bitmap_git))
2152 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2153 else
2154 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2155 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2156 oe = packlist_find(mapping, &oid);
2158 if (oe) {
2159 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2160 if (bitmap_git->hashes && !oe->hash)
2161 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2165 return reposition;
2168 void free_bitmap_index(struct bitmap_index *b)
2170 if (!b)
2171 return;
2173 if (b->map)
2174 munmap(b->map, b->map_size);
2175 ewah_pool_free(b->commits);
2176 ewah_pool_free(b->trees);
2177 ewah_pool_free(b->blobs);
2178 ewah_pool_free(b->tags);
2179 if (b->bitmaps) {
2180 struct stored_bitmap *sb;
2181 kh_foreach_value(b->bitmaps, sb, {
2182 ewah_pool_free(sb->root);
2183 free(sb);
2186 kh_destroy_oid_map(b->bitmaps);
2187 free(b->ext_index.objects);
2188 free(b->ext_index.hashes);
2189 kh_destroy_oid_pos(b->ext_index.positions);
2190 bitmap_free(b->result);
2191 bitmap_free(b->haves);
2192 if (bitmap_is_midx(b)) {
2194 * Multi-pack bitmaps need to have resources associated with
2195 * their on-disk reverse indexes unmapped so that stale .rev and
2196 * .bitmap files can be removed.
2198 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2199 * written in the same 'git multi-pack-index write --bitmap'
2200 * process. Close resources so they can be removed safely on
2201 * platforms like Windows.
2203 close_midx_revindex(b->midx);
2205 free(b);
2208 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2209 const struct object_id *oid)
2211 return bitmap_git &&
2212 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2215 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2216 enum object_type object_type)
2218 struct bitmap *result = bitmap_git->result;
2219 off_t total = 0;
2220 struct ewah_iterator it;
2221 eword_t filter;
2222 size_t i;
2224 init_type_iterator(&it, bitmap_git, object_type);
2225 for (i = 0; i < result->word_alloc &&
2226 ewah_iterator_next(&filter, &it); i++) {
2227 eword_t word = result->words[i] & filter;
2228 size_t base = (i * BITS_IN_EWORD);
2229 unsigned offset;
2231 if (!word)
2232 continue;
2234 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2235 if ((word >> offset) == 0)
2236 break;
2238 offset += ewah_bit_ctz64(word >> offset);
2240 if (bitmap_is_midx(bitmap_git)) {
2241 uint32_t pack_pos;
2242 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2243 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2245 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2246 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2248 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2249 struct object_id oid;
2250 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2252 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2253 oid_to_hex(&oid),
2254 pack->pack_name,
2255 (uintmax_t)offset);
2258 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2259 } else {
2260 size_t pos = base + offset;
2261 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2262 pack_pos_to_offset(bitmap_git->pack, pos);
2267 return total;
2270 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2272 struct bitmap *result = bitmap_git->result;
2273 struct eindex *eindex = &bitmap_git->ext_index;
2274 off_t total = 0;
2275 struct object_info oi = OBJECT_INFO_INIT;
2276 off_t object_size;
2277 size_t i;
2279 oi.disk_sizep = &object_size;
2281 for (i = 0; i < eindex->count; i++) {
2282 struct object *obj = eindex->objects[i];
2284 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2285 continue;
2287 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2288 die(_("unable to get disk usage of '%s'"),
2289 oid_to_hex(&obj->oid));
2291 total += object_size;
2293 return total;
2296 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2297 struct rev_info *revs)
2299 off_t total = 0;
2301 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2302 if (revs->tree_objects)
2303 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2304 if (revs->blob_objects)
2305 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2306 if (revs->tag_objects)
2307 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2309 total += get_disk_usage_for_extended(bitmap_git);
2311 return total;
2314 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2316 return !!bitmap_git->midx;
2319 const struct string_list *bitmap_preferred_tips(struct repository *r)
2321 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
2324 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2326 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2327 struct string_list_item *item;
2329 if (!preferred_tips)
2330 return 0;
2332 for_each_string_list_item(item, preferred_tips) {
2333 if (starts_with(refname, item->string))
2334 return 1;
2337 return 0;