pack-revindex: make `load_pack_revindex` take a repository
[git/debian.git] / pack-bitmap.c
blob38b35c48237c9976bac816d49eb290741d2dcdb8
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 repository *r, 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(r, bitmap_git->midx->packs[i]);
481 if (ret)
482 return ret;
484 return 0;
486 return load_pack_revindex(r, bitmap_git->pack);
489 static int load_bitmap(struct repository *r, 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(r, 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(r, 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 repository *r = the_repository;
593 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
595 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
596 return bitmap_git;
598 free_bitmap_index(bitmap_git);
599 return NULL;
602 struct include_data {
603 struct bitmap_index *bitmap_git;
604 struct bitmap *base;
605 struct bitmap *seen;
608 struct bitmap_lookup_table_triplet {
609 uint32_t commit_pos;
610 uint64_t offset;
611 uint32_t xor_row;
614 struct bitmap_lookup_table_xor_item {
615 struct object_id oid;
616 uint64_t offset;
620 * Given a `triplet` struct pointer and pointer `p`, this
621 * function reads the triplet beginning at `p` into the struct.
622 * Note that this function assumes that there is enough memory
623 * left for filling the `triplet` struct from `p`.
625 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
626 const unsigned char *p)
628 if (!triplet)
629 return -1;
631 triplet->commit_pos = get_be32(p);
632 p += sizeof(uint32_t);
633 triplet->offset = get_be64(p);
634 p += sizeof(uint64_t);
635 triplet->xor_row = get_be32(p);
636 return 0;
640 * This function gets the raw triplet from `row`'th row in the
641 * lookup table and fills that data to the `triplet`.
643 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
644 uint32_t pos,
645 struct bitmap_lookup_table_triplet *triplet)
647 unsigned char *p = NULL;
648 if (pos >= bitmap_git->entry_count)
649 return error(_("corrupt bitmap lookup table: triplet position out of index"));
651 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
653 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
657 * Searches for a matching triplet. `commit_pos` is a pointer
658 * to the wanted commit position value. `table_entry` points to
659 * a triplet in lookup table. The first 4 bytes of each
660 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
662 static int triplet_cmp(const void *commit_pos, const void *table_entry)
665 uint32_t a = *(uint32_t *)commit_pos;
666 uint32_t b = get_be32(table_entry);
667 if (a > b)
668 return 1;
669 else if (a < b)
670 return -1;
672 return 0;
675 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
676 struct object_id *oid,
677 uint32_t *result)
679 int found;
681 if (bitmap_is_midx(bitmap_git))
682 found = bsearch_midx(oid, bitmap_git->midx, result);
683 else
684 found = bsearch_pack(oid, bitmap_git->pack, result);
686 return found;
690 * `bsearch_triplet_by_pos` function searches for the raw triplet
691 * having commit position same as `commit_pos` and fills `triplet`
692 * object from the raw triplet. Returns 1 on success and 0 on
693 * failure.
695 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
696 struct bitmap_index *bitmap_git,
697 struct bitmap_lookup_table_triplet *triplet)
699 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
700 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
702 if (!p)
703 return -1;
705 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
708 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
709 struct commit *commit)
711 uint32_t commit_pos, xor_row;
712 uint64_t offset;
713 int flags;
714 struct bitmap_lookup_table_triplet triplet;
715 struct object_id *oid = &commit->object.oid;
716 struct ewah_bitmap *bitmap;
717 struct stored_bitmap *xor_bitmap = NULL;
718 const int bitmap_header_size = 6;
719 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
720 static size_t xor_items_nr = 0, xor_items_alloc = 0;
721 static int is_corrupt = 0;
722 int xor_flags;
723 khiter_t hash_pos;
724 struct bitmap_lookup_table_xor_item *xor_item;
726 if (is_corrupt)
727 return NULL;
729 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
730 return NULL;
732 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
733 return NULL;
735 xor_items_nr = 0;
736 offset = triplet.offset;
737 xor_row = triplet.xor_row;
739 while (xor_row != 0xffffffff) {
740 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
742 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
743 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
744 goto corrupt;
747 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
748 goto corrupt;
750 xor_item = &xor_items[xor_items_nr];
751 xor_item->offset = triplet.offset;
753 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
754 error(_("corrupt bitmap lookup table: commit index %u out of range"),
755 triplet.commit_pos);
756 goto corrupt;
759 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
762 * If desired bitmap is already stored, we don't need
763 * to iterate further. Because we know that bitmaps
764 * that are needed to be parsed to parse this bitmap
765 * has already been stored. So, assign this stored bitmap
766 * to the xor_bitmap.
768 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
769 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
770 break;
771 xor_items_nr++;
772 xor_row = triplet.xor_row;
775 while (xor_items_nr) {
776 xor_item = &xor_items[xor_items_nr - 1];
777 bitmap_git->map_pos = xor_item->offset;
778 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
779 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
780 oid_to_hex(&xor_item->oid));
781 goto corrupt;
784 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
785 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
786 bitmap = read_bitmap_1(bitmap_git);
788 if (!bitmap)
789 goto corrupt;
791 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
792 xor_items_nr--;
795 bitmap_git->map_pos = offset;
796 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
797 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
798 oid_to_hex(oid));
799 goto corrupt;
803 * Don't bother reading the commit's index position or its xor
804 * offset:
806 * - The commit's index position is irrelevant to us, since
807 * load_bitmap_entries_v1 only uses it to learn the object
808 * id which is used to compute the hashmap's key. We already
809 * have an object id, so no need to look it up again.
811 * - The xor_offset is unusable for us, since it specifies how
812 * many entries previous to ours we should look at. This
813 * makes sense when reading the bitmaps sequentially (as in
814 * load_bitmap_entries_v1()), since we can keep track of
815 * each bitmap as we read them.
817 * But it can't work for us, since the bitmap's don't have a
818 * fixed size. So we learn the position of the xor'd bitmap
819 * from the commit table (and resolve it to a bitmap in the
820 * above if-statement).
822 * Instead, we can skip ahead and immediately read the flags and
823 * ewah bitmap.
825 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
826 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
827 bitmap = read_bitmap_1(bitmap_git);
829 if (!bitmap)
830 goto corrupt;
832 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
834 corrupt:
835 free(xor_items);
836 is_corrupt = 1;
837 return NULL;
840 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
841 struct commit *commit)
843 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
844 commit->object.oid);
845 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
846 struct stored_bitmap *bitmap = NULL;
847 if (!bitmap_git->table_lookup)
848 return NULL;
850 /* this is a fairly hot codepath - no trace2_region please */
851 /* NEEDSWORK: cache misses aren't recorded */
852 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
853 if (!bitmap)
854 return NULL;
855 return lookup_stored_bitmap(bitmap);
857 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
860 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
861 const struct object_id *oid)
863 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
864 khiter_t pos = kh_get_oid_pos(positions, *oid);
866 if (pos < kh_end(positions)) {
867 int bitmap_pos = kh_value(positions, pos);
868 return bitmap_pos + bitmap_num_objects(bitmap_git);
871 return -1;
874 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
875 const struct object_id *oid)
877 uint32_t pos;
878 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
879 if (!offset)
880 return -1;
882 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
883 return -1;
884 return pos;
887 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
888 const struct object_id *oid)
890 uint32_t want, got;
891 if (!bsearch_midx(oid, bitmap_git->midx, &want))
892 return -1;
894 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
895 return -1;
896 return got;
899 static int bitmap_position(struct bitmap_index *bitmap_git,
900 const struct object_id *oid)
902 int pos;
903 if (bitmap_is_midx(bitmap_git))
904 pos = bitmap_position_midx(bitmap_git, oid);
905 else
906 pos = bitmap_position_packfile(bitmap_git, oid);
907 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
910 static int ext_index_add_object(struct bitmap_index *bitmap_git,
911 struct object *object, const char *name)
913 struct eindex *eindex = &bitmap_git->ext_index;
915 khiter_t hash_pos;
916 int hash_ret;
917 int bitmap_pos;
919 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
920 if (hash_ret > 0) {
921 if (eindex->count >= eindex->alloc) {
922 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
923 REALLOC_ARRAY(eindex->objects, eindex->alloc);
924 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
927 bitmap_pos = eindex->count;
928 eindex->objects[eindex->count] = object;
929 eindex->hashes[eindex->count] = pack_name_hash(name);
930 kh_value(eindex->positions, hash_pos) = bitmap_pos;
931 eindex->count++;
932 } else {
933 bitmap_pos = kh_value(eindex->positions, hash_pos);
936 return bitmap_pos + bitmap_num_objects(bitmap_git);
939 struct bitmap_show_data {
940 struct bitmap_index *bitmap_git;
941 struct bitmap *base;
944 static void show_object(struct object *object, const char *name, void *data_)
946 struct bitmap_show_data *data = data_;
947 int bitmap_pos;
949 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
951 if (bitmap_pos < 0)
952 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
953 name);
955 bitmap_set(data->base, bitmap_pos);
958 static void show_commit(struct commit *commit UNUSED,
959 void *data UNUSED)
963 static int add_to_include_set(struct bitmap_index *bitmap_git,
964 struct include_data *data,
965 struct commit *commit,
966 int bitmap_pos)
968 struct ewah_bitmap *partial;
970 if (data->seen && bitmap_get(data->seen, bitmap_pos))
971 return 0;
973 if (bitmap_get(data->base, bitmap_pos))
974 return 0;
976 partial = bitmap_for_commit(bitmap_git, commit);
977 if (partial) {
978 bitmap_or_ewah(data->base, partial);
979 return 0;
982 bitmap_set(data->base, bitmap_pos);
983 return 1;
986 static int should_include(struct commit *commit, void *_data)
988 struct include_data *data = _data;
989 int bitmap_pos;
991 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
992 if (bitmap_pos < 0)
993 bitmap_pos = ext_index_add_object(data->bitmap_git,
994 (struct object *)commit,
995 NULL);
997 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
998 struct commit_list *parent = commit->parents;
1000 while (parent) {
1001 parent->item->object.flags |= SEEN;
1002 parent = parent->next;
1005 return 0;
1008 return 1;
1011 static int should_include_obj(struct object *obj, void *_data)
1013 struct include_data *data = _data;
1014 int bitmap_pos;
1016 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1017 if (bitmap_pos < 0)
1018 return 1;
1019 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1020 bitmap_get(data->base, bitmap_pos)) {
1021 obj->flags |= SEEN;
1022 return 0;
1024 return 1;
1027 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1028 struct bitmap **base,
1029 struct commit *commit)
1031 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1033 if (!or_with)
1034 return 0;
1036 if (!*base)
1037 *base = ewah_to_bitmap(or_with);
1038 else
1039 bitmap_or_ewah(*base, or_with);
1041 return 1;
1044 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1045 struct rev_info *revs,
1046 struct object_list *roots,
1047 struct bitmap *seen)
1049 struct bitmap *base = NULL;
1050 int needs_walk = 0;
1052 struct object_list *not_mapped = NULL;
1055 * Go through all the roots for the walk. The ones that have bitmaps
1056 * on the bitmap index will be `or`ed together to form an initial
1057 * global reachability analysis.
1059 * The ones without bitmaps in the index will be stored in the
1060 * `not_mapped_list` for further processing.
1062 while (roots) {
1063 struct object *object = roots->item;
1064 roots = roots->next;
1066 if (object->type == OBJ_COMMIT &&
1067 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1068 object->flags |= SEEN;
1069 continue;
1072 object_list_insert(object, &not_mapped);
1076 * Best case scenario: We found bitmaps for all the roots,
1077 * so the resulting `or` bitmap has the full reachability analysis
1079 if (!not_mapped)
1080 return base;
1082 roots = not_mapped;
1085 * Let's iterate through all the roots that don't have bitmaps to
1086 * check if we can determine them to be reachable from the existing
1087 * global bitmap.
1089 * If we cannot find them in the existing global bitmap, we'll need
1090 * to push them to an actual walk and run it until we can confirm
1091 * they are reachable
1093 while (roots) {
1094 struct object *object = roots->item;
1095 int pos;
1097 roots = roots->next;
1098 pos = bitmap_position(bitmap_git, &object->oid);
1100 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1101 object->flags &= ~UNINTERESTING;
1102 add_pending_object(revs, object, "");
1103 needs_walk = 1;
1104 } else {
1105 object->flags |= SEEN;
1109 if (needs_walk) {
1110 struct include_data incdata;
1111 struct bitmap_show_data show_data;
1113 if (!base)
1114 base = bitmap_new();
1116 incdata.bitmap_git = bitmap_git;
1117 incdata.base = base;
1118 incdata.seen = seen;
1120 revs->include_check = should_include;
1121 revs->include_check_obj = should_include_obj;
1122 revs->include_check_data = &incdata;
1124 if (prepare_revision_walk(revs))
1125 die(_("revision walk setup failed"));
1127 show_data.bitmap_git = bitmap_git;
1128 show_data.base = base;
1130 traverse_commit_list(revs,
1131 show_commit, show_object,
1132 &show_data);
1134 revs->include_check = NULL;
1135 revs->include_check_obj = NULL;
1136 revs->include_check_data = NULL;
1139 return base;
1142 static void show_extended_objects(struct bitmap_index *bitmap_git,
1143 struct rev_info *revs,
1144 show_reachable_fn show_reach)
1146 struct bitmap *objects = bitmap_git->result;
1147 struct eindex *eindex = &bitmap_git->ext_index;
1148 uint32_t i;
1150 for (i = 0; i < eindex->count; ++i) {
1151 struct object *obj;
1153 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1154 continue;
1156 obj = eindex->objects[i];
1157 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1158 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1159 (obj->type == OBJ_TAG && !revs->tag_objects))
1160 continue;
1162 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1166 static void init_type_iterator(struct ewah_iterator *it,
1167 struct bitmap_index *bitmap_git,
1168 enum object_type type)
1170 switch (type) {
1171 case OBJ_COMMIT:
1172 ewah_iterator_init(it, bitmap_git->commits);
1173 break;
1175 case OBJ_TREE:
1176 ewah_iterator_init(it, bitmap_git->trees);
1177 break;
1179 case OBJ_BLOB:
1180 ewah_iterator_init(it, bitmap_git->blobs);
1181 break;
1183 case OBJ_TAG:
1184 ewah_iterator_init(it, bitmap_git->tags);
1185 break;
1187 default:
1188 BUG("object type %d not stored by bitmap type index", type);
1189 break;
1193 static void show_objects_for_type(
1194 struct bitmap_index *bitmap_git,
1195 enum object_type object_type,
1196 show_reachable_fn show_reach)
1198 size_t i = 0;
1199 uint32_t offset;
1201 struct ewah_iterator it;
1202 eword_t filter;
1204 struct bitmap *objects = bitmap_git->result;
1206 init_type_iterator(&it, bitmap_git, object_type);
1208 for (i = 0; i < objects->word_alloc &&
1209 ewah_iterator_next(&filter, &it); i++) {
1210 eword_t word = objects->words[i] & filter;
1211 size_t pos = (i * BITS_IN_EWORD);
1213 if (!word)
1214 continue;
1216 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1217 struct packed_git *pack;
1218 struct object_id oid;
1219 uint32_t hash = 0, index_pos;
1220 off_t ofs;
1222 if ((word >> offset) == 0)
1223 break;
1225 offset += ewah_bit_ctz64(word >> offset);
1227 if (bitmap_is_midx(bitmap_git)) {
1228 struct multi_pack_index *m = bitmap_git->midx;
1229 uint32_t pack_id;
1231 index_pos = pack_pos_to_midx(m, pos + offset);
1232 ofs = nth_midxed_offset(m, index_pos);
1233 nth_midxed_object_oid(&oid, m, index_pos);
1235 pack_id = nth_midxed_pack_int_id(m, index_pos);
1236 pack = bitmap_git->midx->packs[pack_id];
1237 } else {
1238 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1239 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1240 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1242 pack = bitmap_git->pack;
1245 if (bitmap_git->hashes)
1246 hash = get_be32(bitmap_git->hashes + index_pos);
1248 show_reach(&oid, object_type, 0, hash, pack, ofs);
1253 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1254 struct object_list *roots)
1256 while (roots) {
1257 struct object *object = roots->item;
1258 roots = roots->next;
1260 if (bitmap_is_midx(bitmap_git)) {
1261 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1262 return 1;
1263 } else {
1264 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1265 return 1;
1269 return 0;
1272 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1273 struct object_list *tip_objects,
1274 enum object_type type)
1276 struct bitmap *result = bitmap_new();
1277 struct object_list *p;
1279 for (p = tip_objects; p; p = p->next) {
1280 int pos;
1282 if (p->item->type != type)
1283 continue;
1285 pos = bitmap_position(bitmap_git, &p->item->oid);
1286 if (pos < 0)
1287 continue;
1289 bitmap_set(result, pos);
1292 return result;
1295 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1296 struct object_list *tip_objects,
1297 struct bitmap *to_filter,
1298 enum object_type type)
1300 struct eindex *eindex = &bitmap_git->ext_index;
1301 struct bitmap *tips;
1302 struct ewah_iterator it;
1303 eword_t mask;
1304 uint32_t i;
1307 * The non-bitmap version of this filter never removes
1308 * objects which the other side specifically asked for,
1309 * so we must match that behavior.
1311 tips = find_tip_objects(bitmap_git, tip_objects, type);
1314 * We can use the type-level bitmap for 'type' to work in whole
1315 * words for the objects that are actually in the bitmapped
1316 * packfile.
1318 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1319 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1320 i++) {
1321 if (i < tips->word_alloc)
1322 mask &= ~tips->words[i];
1323 to_filter->words[i] &= ~mask;
1327 * Clear any objects that weren't in the packfile (and so would
1328 * not have been caught by the loop above. We'll have to check
1329 * them individually.
1331 for (i = 0; i < eindex->count; i++) {
1332 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1333 if (eindex->objects[i]->type == type &&
1334 bitmap_get(to_filter, pos) &&
1335 !bitmap_get(tips, pos))
1336 bitmap_unset(to_filter, pos);
1339 bitmap_free(tips);
1342 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1343 struct object_list *tip_objects,
1344 struct bitmap *to_filter)
1346 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1347 OBJ_BLOB);
1350 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1351 uint32_t pos)
1353 unsigned long size;
1354 struct object_info oi = OBJECT_INFO_INIT;
1356 oi.sizep = &size;
1358 if (pos < bitmap_num_objects(bitmap_git)) {
1359 struct packed_git *pack;
1360 off_t ofs;
1362 if (bitmap_is_midx(bitmap_git)) {
1363 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1364 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1366 pack = bitmap_git->midx->packs[pack_id];
1367 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1368 } else {
1369 pack = bitmap_git->pack;
1370 ofs = pack_pos_to_offset(pack, pos);
1373 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1374 struct object_id oid;
1375 nth_bitmap_object_oid(bitmap_git, &oid,
1376 pack_pos_to_index(pack, pos));
1377 die(_("unable to get size of %s"), oid_to_hex(&oid));
1379 } else {
1380 struct eindex *eindex = &bitmap_git->ext_index;
1381 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1382 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1383 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1386 return size;
1389 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1390 struct object_list *tip_objects,
1391 struct bitmap *to_filter,
1392 unsigned long limit)
1394 struct eindex *eindex = &bitmap_git->ext_index;
1395 struct bitmap *tips;
1396 struct ewah_iterator it;
1397 eword_t mask;
1398 uint32_t i;
1400 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1402 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1403 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1404 i++) {
1405 eword_t word = to_filter->words[i] & mask;
1406 unsigned offset;
1408 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1409 uint32_t pos;
1411 if ((word >> offset) == 0)
1412 break;
1413 offset += ewah_bit_ctz64(word >> offset);
1414 pos = i * BITS_IN_EWORD + offset;
1416 if (!bitmap_get(tips, pos) &&
1417 get_size_by_pos(bitmap_git, pos) >= limit)
1418 bitmap_unset(to_filter, pos);
1422 for (i = 0; i < eindex->count; i++) {
1423 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1424 if (eindex->objects[i]->type == OBJ_BLOB &&
1425 bitmap_get(to_filter, pos) &&
1426 !bitmap_get(tips, pos) &&
1427 get_size_by_pos(bitmap_git, pos) >= limit)
1428 bitmap_unset(to_filter, pos);
1431 bitmap_free(tips);
1434 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1435 struct object_list *tip_objects,
1436 struct bitmap *to_filter,
1437 unsigned long limit)
1439 if (limit)
1440 BUG("filter_bitmap_tree_depth given non-zero limit");
1442 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1443 OBJ_TREE);
1444 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1445 OBJ_BLOB);
1448 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1449 struct object_list *tip_objects,
1450 struct bitmap *to_filter,
1451 enum object_type object_type)
1453 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1454 BUG("filter_bitmap_object_type given invalid object");
1456 if (object_type != OBJ_TAG)
1457 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1458 if (object_type != OBJ_COMMIT)
1459 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1460 if (object_type != OBJ_TREE)
1461 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1462 if (object_type != OBJ_BLOB)
1463 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1466 static int filter_bitmap(struct bitmap_index *bitmap_git,
1467 struct object_list *tip_objects,
1468 struct bitmap *to_filter,
1469 struct list_objects_filter_options *filter)
1471 if (!filter || filter->choice == LOFC_DISABLED)
1472 return 0;
1474 if (filter->choice == LOFC_BLOB_NONE) {
1475 if (bitmap_git)
1476 filter_bitmap_blob_none(bitmap_git, tip_objects,
1477 to_filter);
1478 return 0;
1481 if (filter->choice == LOFC_BLOB_LIMIT) {
1482 if (bitmap_git)
1483 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1484 to_filter,
1485 filter->blob_limit_value);
1486 return 0;
1489 if (filter->choice == LOFC_TREE_DEPTH &&
1490 filter->tree_exclude_depth == 0) {
1491 if (bitmap_git)
1492 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1493 to_filter,
1494 filter->tree_exclude_depth);
1495 return 0;
1498 if (filter->choice == LOFC_OBJECT_TYPE) {
1499 if (bitmap_git)
1500 filter_bitmap_object_type(bitmap_git, tip_objects,
1501 to_filter,
1502 filter->object_type);
1503 return 0;
1506 if (filter->choice == LOFC_COMBINE) {
1507 int i;
1508 for (i = 0; i < filter->sub_nr; i++) {
1509 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1510 &filter->sub[i]) < 0)
1511 return -1;
1513 return 0;
1516 /* filter choice not handled */
1517 return -1;
1520 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1522 return !filter_bitmap(NULL, NULL, NULL, filter);
1525 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1526 int filter_provided_objects)
1528 unsigned int i;
1530 struct object_list *wants = NULL;
1531 struct object_list *haves = NULL;
1533 struct bitmap *wants_bitmap = NULL;
1534 struct bitmap *haves_bitmap = NULL;
1536 struct bitmap_index *bitmap_git;
1539 * We can't do pathspec limiting with bitmaps, because we don't know
1540 * which commits are associated with which object changes (let alone
1541 * even which objects are associated with which paths).
1543 if (revs->prune)
1544 return NULL;
1546 if (!can_filter_bitmap(&revs->filter))
1547 return NULL;
1549 /* try to open a bitmapped pack, but don't parse it yet
1550 * because we may not need to use it */
1551 CALLOC_ARRAY(bitmap_git, 1);
1552 if (open_bitmap(revs->repo, bitmap_git) < 0)
1553 goto cleanup;
1555 for (i = 0; i < revs->pending.nr; ++i) {
1556 struct object *object = revs->pending.objects[i].item;
1558 if (object->type == OBJ_NONE)
1559 parse_object_or_die(&object->oid, NULL);
1561 while (object->type == OBJ_TAG) {
1562 struct tag *tag = (struct tag *) object;
1564 if (object->flags & UNINTERESTING)
1565 object_list_insert(object, &haves);
1566 else
1567 object_list_insert(object, &wants);
1569 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1570 object->flags |= (tag->object.flags & UNINTERESTING);
1573 if (object->flags & UNINTERESTING)
1574 object_list_insert(object, &haves);
1575 else
1576 object_list_insert(object, &wants);
1580 * if we have a HAVES list, but none of those haves is contained
1581 * in the packfile that has a bitmap, we don't have anything to
1582 * optimize here
1584 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1585 goto cleanup;
1587 /* if we don't want anything, we're done here */
1588 if (!wants)
1589 goto cleanup;
1592 * now we're going to use bitmaps, so load the actual bitmap entries
1593 * from disk. this is the point of no return; after this the rev_list
1594 * becomes invalidated and we must perform the revwalk through bitmaps
1596 if (load_bitmap(revs->repo, bitmap_git) < 0)
1597 goto cleanup;
1599 object_array_clear(&revs->pending);
1601 if (haves) {
1602 revs->ignore_missing_links = 1;
1603 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1604 reset_revision_walk();
1605 revs->ignore_missing_links = 0;
1607 if (!haves_bitmap)
1608 BUG("failed to perform bitmap walk");
1611 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1613 if (!wants_bitmap)
1614 BUG("failed to perform bitmap walk");
1616 if (haves_bitmap)
1617 bitmap_and_not(wants_bitmap, haves_bitmap);
1619 filter_bitmap(bitmap_git,
1620 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1621 wants_bitmap,
1622 &revs->filter);
1624 bitmap_git->result = wants_bitmap;
1625 bitmap_git->haves = haves_bitmap;
1627 object_list_free(&wants);
1628 object_list_free(&haves);
1630 return bitmap_git;
1632 cleanup:
1633 free_bitmap_index(bitmap_git);
1634 object_list_free(&wants);
1635 object_list_free(&haves);
1636 return NULL;
1640 * -1 means "stop trying further objects"; 0 means we may or may not have
1641 * reused, but you can keep feeding bits.
1643 static int try_partial_reuse(struct packed_git *pack,
1644 size_t pos,
1645 struct bitmap *reuse,
1646 struct pack_window **w_curs)
1648 off_t offset, delta_obj_offset;
1649 enum object_type type;
1650 unsigned long size;
1653 * try_partial_reuse() is called either on (a) objects in the
1654 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1655 * objects in the preferred pack of a multi-pack bitmap.
1656 * Importantly, the latter can pretend as if only a single pack
1657 * exists because:
1659 * - The first pack->num_objects bits of a MIDX bitmap are
1660 * reserved for the preferred pack, and
1662 * - Ties due to duplicate objects are always resolved in
1663 * favor of the preferred pack.
1665 * Therefore we do not need to ever ask the MIDX for its copy of
1666 * an object by OID, since it will always select it from the
1667 * preferred pack. Likewise, the selected copy of the base
1668 * object for any deltas will reside in the same pack.
1670 * This means that we can reuse pos when looking up the bit in
1671 * the reuse bitmap, too, since bits corresponding to the
1672 * preferred pack precede all bits from other packs.
1675 if (pos >= pack->num_objects)
1676 return -1; /* not actually in the pack or MIDX preferred pack */
1678 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1679 type = unpack_object_header(pack, w_curs, &offset, &size);
1680 if (type < 0)
1681 return -1; /* broken packfile, punt */
1683 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1684 off_t base_offset;
1685 uint32_t base_pos;
1688 * Find the position of the base object so we can look it up
1689 * in our bitmaps. If we can't come up with an offset, or if
1690 * that offset is not in the revidx, the pack is corrupt.
1691 * There's nothing we can do, so just punt on this object,
1692 * and the normal slow path will complain about it in
1693 * more detail.
1695 base_offset = get_delta_base(pack, w_curs, &offset, type,
1696 delta_obj_offset);
1697 if (!base_offset)
1698 return 0;
1699 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1700 return 0;
1703 * We assume delta dependencies always point backwards. This
1704 * lets us do a single pass, and is basically always true
1705 * due to the way OFS_DELTAs work. You would not typically
1706 * find REF_DELTA in a bitmapped pack, since we only bitmap
1707 * packs we write fresh, and OFS_DELTA is the default). But
1708 * let's double check to make sure the pack wasn't written with
1709 * odd parameters.
1711 if (base_pos >= pos)
1712 return 0;
1715 * And finally, if we're not sending the base as part of our
1716 * reuse chunk, then don't send this object either. The base
1717 * would come after us, along with other objects not
1718 * necessarily in the pack, which means we'd need to convert
1719 * to REF_DELTA on the fly. Better to just let the normal
1720 * object_entry code path handle it.
1722 if (!bitmap_get(reuse, base_pos))
1723 return 0;
1727 * If we got here, then the object is OK to reuse. Mark it.
1729 bitmap_set(reuse, pos);
1730 return 0;
1733 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1735 struct multi_pack_index *m = bitmap_git->midx;
1736 if (!m)
1737 BUG("midx_preferred_pack: requires non-empty MIDX");
1738 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1741 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1742 struct packed_git **packfile_out,
1743 uint32_t *entries,
1744 struct bitmap **reuse_out)
1746 struct repository *r = the_repository;
1747 struct packed_git *pack;
1748 struct bitmap *result = bitmap_git->result;
1749 struct bitmap *reuse;
1750 struct pack_window *w_curs = NULL;
1751 size_t i = 0;
1752 uint32_t offset;
1753 uint32_t objects_nr;
1755 assert(result);
1757 load_reverse_index(r, bitmap_git);
1759 if (bitmap_is_midx(bitmap_git))
1760 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1761 else
1762 pack = bitmap_git->pack;
1763 objects_nr = pack->num_objects;
1765 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1766 i++;
1769 * Don't mark objects not in the packfile or preferred pack. This bitmap
1770 * marks objects eligible for reuse, but the pack-reuse code only
1771 * understands how to reuse a single pack. Since the preferred pack is
1772 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1773 * we use it instead of another pack. In single-pack bitmaps, the choice
1774 * is made for us.
1776 if (i > objects_nr / BITS_IN_EWORD)
1777 i = objects_nr / BITS_IN_EWORD;
1779 reuse = bitmap_word_alloc(i);
1780 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1782 for (; i < result->word_alloc; ++i) {
1783 eword_t word = result->words[i];
1784 size_t pos = (i * BITS_IN_EWORD);
1786 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1787 if ((word >> offset) == 0)
1788 break;
1790 offset += ewah_bit_ctz64(word >> offset);
1791 if (try_partial_reuse(pack, pos + offset,
1792 reuse, &w_curs) < 0) {
1794 * try_partial_reuse indicated we couldn't reuse
1795 * any bits, so there is no point in trying more
1796 * bits in the current word, or any other words
1797 * in result.
1799 * Jump out of both loops to avoid future
1800 * unnecessary calls to try_partial_reuse.
1802 goto done;
1807 done:
1808 unuse_pack(&w_curs);
1810 *entries = bitmap_popcount(reuse);
1811 if (!*entries) {
1812 bitmap_free(reuse);
1813 return -1;
1817 * Drop any reused objects from the result, since they will not
1818 * need to be handled separately.
1820 bitmap_and_not(result, reuse);
1821 *packfile_out = pack;
1822 *reuse_out = reuse;
1823 return 0;
1826 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1827 struct bitmap *bitmap, const struct object_id *oid)
1829 int idx;
1831 if (!bitmap)
1832 return 0;
1834 idx = bitmap_position(bitmap_git, oid);
1835 return idx >= 0 && bitmap_get(bitmap, idx);
1838 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1839 struct rev_info *revs,
1840 show_reachable_fn show_reachable)
1842 assert(bitmap_git->result);
1844 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1845 if (revs->tree_objects)
1846 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1847 if (revs->blob_objects)
1848 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1849 if (revs->tag_objects)
1850 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1852 show_extended_objects(bitmap_git, revs, show_reachable);
1855 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1856 enum object_type type)
1858 struct bitmap *objects = bitmap_git->result;
1859 struct eindex *eindex = &bitmap_git->ext_index;
1861 uint32_t i = 0, count = 0;
1862 struct ewah_iterator it;
1863 eword_t filter;
1865 init_type_iterator(&it, bitmap_git, type);
1867 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1868 eword_t word = objects->words[i++] & filter;
1869 count += ewah_bit_popcount64(word);
1872 for (i = 0; i < eindex->count; ++i) {
1873 if (eindex->objects[i]->type == type &&
1874 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1875 count++;
1878 return count;
1881 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1882 uint32_t *commits, uint32_t *trees,
1883 uint32_t *blobs, uint32_t *tags)
1885 assert(bitmap_git->result);
1887 if (commits)
1888 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1890 if (trees)
1891 *trees = count_object_type(bitmap_git, OBJ_TREE);
1893 if (blobs)
1894 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1896 if (tags)
1897 *tags = count_object_type(bitmap_git, OBJ_TAG);
1900 struct bitmap_test_data {
1901 struct bitmap_index *bitmap_git;
1902 struct bitmap *base;
1903 struct bitmap *commits;
1904 struct bitmap *trees;
1905 struct bitmap *blobs;
1906 struct bitmap *tags;
1907 struct progress *prg;
1908 size_t seen;
1911 static void test_bitmap_type(struct bitmap_test_data *tdata,
1912 struct object *obj, int pos)
1914 enum object_type bitmap_type = OBJ_NONE;
1915 int bitmaps_nr = 0;
1917 if (bitmap_get(tdata->commits, pos)) {
1918 bitmap_type = OBJ_COMMIT;
1919 bitmaps_nr++;
1921 if (bitmap_get(tdata->trees, pos)) {
1922 bitmap_type = OBJ_TREE;
1923 bitmaps_nr++;
1925 if (bitmap_get(tdata->blobs, pos)) {
1926 bitmap_type = OBJ_BLOB;
1927 bitmaps_nr++;
1929 if (bitmap_get(tdata->tags, pos)) {
1930 bitmap_type = OBJ_TAG;
1931 bitmaps_nr++;
1934 if (bitmap_type == OBJ_NONE)
1935 die(_("object '%s' not found in type bitmaps"),
1936 oid_to_hex(&obj->oid));
1938 if (bitmaps_nr > 1)
1939 die(_("object '%s' does not have a unique type"),
1940 oid_to_hex(&obj->oid));
1942 if (bitmap_type != obj->type)
1943 die(_("object '%s': real type '%s', expected: '%s'"),
1944 oid_to_hex(&obj->oid),
1945 type_name(obj->type),
1946 type_name(bitmap_type));
1949 static void test_show_object(struct object *object,
1950 const char *name UNUSED,
1951 void *data)
1953 struct bitmap_test_data *tdata = data;
1954 int bitmap_pos;
1956 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1957 if (bitmap_pos < 0)
1958 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
1959 test_bitmap_type(tdata, object, bitmap_pos);
1961 bitmap_set(tdata->base, bitmap_pos);
1962 display_progress(tdata->prg, ++tdata->seen);
1965 static void test_show_commit(struct commit *commit, void *data)
1967 struct bitmap_test_data *tdata = data;
1968 int bitmap_pos;
1970 bitmap_pos = bitmap_position(tdata->bitmap_git,
1971 &commit->object.oid);
1972 if (bitmap_pos < 0)
1973 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
1974 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1976 bitmap_set(tdata->base, bitmap_pos);
1977 display_progress(tdata->prg, ++tdata->seen);
1980 void test_bitmap_walk(struct rev_info *revs)
1982 struct object *root;
1983 struct bitmap *result = NULL;
1984 size_t result_popcnt;
1985 struct bitmap_test_data tdata;
1986 struct bitmap_index *bitmap_git;
1987 struct ewah_bitmap *bm;
1989 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1990 die(_("failed to load bitmap indexes"));
1992 if (revs->pending.nr != 1)
1993 die(_("you must specify exactly one commit to test"));
1995 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
1996 bitmap_git->version,
1997 bitmap_git->entry_count,
1998 bitmap_git->table_lookup ? "" : " loaded");
2000 root = revs->pending.objects[0].item;
2001 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2003 if (bm) {
2004 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2005 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2007 result = ewah_to_bitmap(bm);
2010 if (!result)
2011 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2013 revs->tag_objects = 1;
2014 revs->tree_objects = 1;
2015 revs->blob_objects = 1;
2017 result_popcnt = bitmap_popcount(result);
2019 if (prepare_revision_walk(revs))
2020 die(_("revision walk setup failed"));
2022 tdata.bitmap_git = bitmap_git;
2023 tdata.base = bitmap_new();
2024 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2025 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2026 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2027 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2028 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2029 tdata.seen = 0;
2031 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2033 stop_progress(&tdata.prg);
2035 if (bitmap_equals(result, tdata.base))
2036 fprintf_ln(stderr, "OK!");
2037 else
2038 die(_("mismatch in bitmap results"));
2040 bitmap_free(result);
2041 bitmap_free(tdata.base);
2042 bitmap_free(tdata.commits);
2043 bitmap_free(tdata.trees);
2044 bitmap_free(tdata.blobs);
2045 bitmap_free(tdata.tags);
2046 free_bitmap_index(bitmap_git);
2049 int test_bitmap_commits(struct repository *r)
2051 struct object_id oid;
2052 MAYBE_UNUSED void *value;
2053 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2055 if (!bitmap_git)
2056 die(_("failed to load bitmap indexes"));
2059 * As this function is only used to print bitmap selected
2060 * commits, we don't have to read the commit table.
2062 if (bitmap_git->table_lookup) {
2063 if (load_bitmap_entries_v1(bitmap_git) < 0)
2064 die(_("failed to load bitmap indexes"));
2067 kh_foreach(bitmap_git->bitmaps, oid, value, {
2068 printf_ln("%s", oid_to_hex(&oid));
2071 free_bitmap_index(bitmap_git);
2073 return 0;
2076 int test_bitmap_hashes(struct repository *r)
2078 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2079 struct object_id oid;
2080 uint32_t i, index_pos;
2082 if (!bitmap_git || !bitmap_git->hashes)
2083 goto cleanup;
2085 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2086 if (bitmap_is_midx(bitmap_git))
2087 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2088 else
2089 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2091 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2093 printf_ln("%s %"PRIu32"",
2094 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2097 cleanup:
2098 free_bitmap_index(bitmap_git);
2100 return 0;
2103 int rebuild_bitmap(const uint32_t *reposition,
2104 struct ewah_bitmap *source,
2105 struct bitmap *dest)
2107 uint32_t pos = 0;
2108 struct ewah_iterator it;
2109 eword_t word;
2111 ewah_iterator_init(&it, source);
2113 while (ewah_iterator_next(&word, &it)) {
2114 uint32_t offset, bit_pos;
2116 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2117 if ((word >> offset) == 0)
2118 break;
2120 offset += ewah_bit_ctz64(word >> offset);
2122 bit_pos = reposition[pos + offset];
2123 if (bit_pos > 0)
2124 bitmap_set(dest, bit_pos - 1);
2125 else /* can't reuse, we don't have the object */
2126 return -1;
2129 pos += BITS_IN_EWORD;
2131 return 0;
2134 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2135 struct packing_data *mapping)
2137 struct repository *r = the_repository;
2138 uint32_t i, num_objects;
2139 uint32_t *reposition;
2141 if (!bitmap_is_midx(bitmap_git))
2142 load_reverse_index(r, bitmap_git);
2143 else if (load_midx_revindex(bitmap_git->midx) < 0)
2144 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2145 "extension");
2147 num_objects = bitmap_num_objects(bitmap_git);
2148 CALLOC_ARRAY(reposition, num_objects);
2150 for (i = 0; i < num_objects; ++i) {
2151 struct object_id oid;
2152 struct object_entry *oe;
2153 uint32_t index_pos;
2155 if (bitmap_is_midx(bitmap_git))
2156 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2157 else
2158 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2159 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2160 oe = packlist_find(mapping, &oid);
2162 if (oe) {
2163 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2164 if (bitmap_git->hashes && !oe->hash)
2165 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2169 return reposition;
2172 void free_bitmap_index(struct bitmap_index *b)
2174 if (!b)
2175 return;
2177 if (b->map)
2178 munmap(b->map, b->map_size);
2179 ewah_pool_free(b->commits);
2180 ewah_pool_free(b->trees);
2181 ewah_pool_free(b->blobs);
2182 ewah_pool_free(b->tags);
2183 if (b->bitmaps) {
2184 struct stored_bitmap *sb;
2185 kh_foreach_value(b->bitmaps, sb, {
2186 ewah_pool_free(sb->root);
2187 free(sb);
2190 kh_destroy_oid_map(b->bitmaps);
2191 free(b->ext_index.objects);
2192 free(b->ext_index.hashes);
2193 kh_destroy_oid_pos(b->ext_index.positions);
2194 bitmap_free(b->result);
2195 bitmap_free(b->haves);
2196 if (bitmap_is_midx(b)) {
2198 * Multi-pack bitmaps need to have resources associated with
2199 * their on-disk reverse indexes unmapped so that stale .rev and
2200 * .bitmap files can be removed.
2202 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2203 * written in the same 'git multi-pack-index write --bitmap'
2204 * process. Close resources so they can be removed safely on
2205 * platforms like Windows.
2207 close_midx_revindex(b->midx);
2209 free(b);
2212 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2213 const struct object_id *oid)
2215 return bitmap_git &&
2216 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2219 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2220 enum object_type object_type)
2222 struct bitmap *result = bitmap_git->result;
2223 off_t total = 0;
2224 struct ewah_iterator it;
2225 eword_t filter;
2226 size_t i;
2228 init_type_iterator(&it, bitmap_git, object_type);
2229 for (i = 0; i < result->word_alloc &&
2230 ewah_iterator_next(&filter, &it); i++) {
2231 eword_t word = result->words[i] & filter;
2232 size_t base = (i * BITS_IN_EWORD);
2233 unsigned offset;
2235 if (!word)
2236 continue;
2238 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2239 if ((word >> offset) == 0)
2240 break;
2242 offset += ewah_bit_ctz64(word >> offset);
2244 if (bitmap_is_midx(bitmap_git)) {
2245 uint32_t pack_pos;
2246 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2247 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2249 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2250 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2252 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2253 struct object_id oid;
2254 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2256 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2257 oid_to_hex(&oid),
2258 pack->pack_name,
2259 (uintmax_t)offset);
2262 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2263 } else {
2264 size_t pos = base + offset;
2265 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2266 pack_pos_to_offset(bitmap_git->pack, pos);
2271 return total;
2274 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2276 struct bitmap *result = bitmap_git->result;
2277 struct eindex *eindex = &bitmap_git->ext_index;
2278 off_t total = 0;
2279 struct object_info oi = OBJECT_INFO_INIT;
2280 off_t object_size;
2281 size_t i;
2283 oi.disk_sizep = &object_size;
2285 for (i = 0; i < eindex->count; i++) {
2286 struct object *obj = eindex->objects[i];
2288 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2289 continue;
2291 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2292 die(_("unable to get disk usage of '%s'"),
2293 oid_to_hex(&obj->oid));
2295 total += object_size;
2297 return total;
2300 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2301 struct rev_info *revs)
2303 off_t total = 0;
2305 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2306 if (revs->tree_objects)
2307 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2308 if (revs->blob_objects)
2309 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2310 if (revs->tag_objects)
2311 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2313 total += get_disk_usage_for_extended(bitmap_git);
2315 return total;
2318 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2320 return !!bitmap_git->midx;
2323 const struct string_list *bitmap_preferred_tips(struct repository *r)
2325 const struct string_list *dest;
2327 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2328 return dest;
2329 return NULL;
2332 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2334 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2335 struct string_list_item *item;
2337 if (!preferred_tips)
2338 return 0;
2340 for_each_string_list_item(item, preferred_tips) {
2341 if (starts_with(refname, item->string))
2342 return 1;
2345 return 0;