Merge branch 'tl/zh_CN_2.41.0_rnd1' of github.com:dyrone/git
[git/debian.git] / pack-bitmap.c
blob999f962602da7d1aae2e90cb620c0ffb42a7f510
1 #include "git-compat-util.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 "trace2.h"
19 #include "object-file.h"
20 #include "object-store.h"
21 #include "list-objects-filter-options.h"
22 #include "midx.h"
23 #include "config.h"
26 * An entry on the bitmap index, representing the bitmap for a given
27 * commit.
29 struct stored_bitmap {
30 struct object_id oid;
31 struct ewah_bitmap *root;
32 struct stored_bitmap *xor;
33 int flags;
37 * The active bitmap index for a repository. By design, repositories only have
38 * a single bitmap index available (the index for the biggest packfile in
39 * the repository), since bitmap indexes need full closure.
41 * If there is more than one bitmap index available (e.g. because of alternates),
42 * the active bitmap index is the largest one.
44 struct bitmap_index {
46 * The pack or multi-pack index (MIDX) that this bitmap index belongs
47 * to.
49 * Exactly one of these must be non-NULL; this specifies the object
50 * order used to interpret this bitmap.
52 struct packed_git *pack;
53 struct multi_pack_index *midx;
56 * Mark the first `reuse_objects` in the packfile as reused:
57 * they will be sent as-is without using them for repacking
58 * calculations
60 uint32_t reuse_objects;
62 /* mmapped buffer of the whole bitmap index */
63 unsigned char *map;
64 size_t map_size; /* size of the mmaped buffer */
65 size_t map_pos; /* current position when loading the index */
68 * Type indexes.
70 * Each bitmap marks which objects in the packfile are of the given
71 * type. This provides type information when yielding the objects from
72 * the packfile during a walk, which allows for better delta bases.
74 struct ewah_bitmap *commits;
75 struct ewah_bitmap *trees;
76 struct ewah_bitmap *blobs;
77 struct ewah_bitmap *tags;
79 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
80 kh_oid_map_t *bitmaps;
82 /* Number of bitmapped commits */
83 uint32_t entry_count;
85 /* If not NULL, this is a name-hash cache pointing into map. */
86 uint32_t *hashes;
88 /* The checksum of the packfile or MIDX; points into map. */
89 const unsigned char *checksum;
92 * If not NULL, this point into the commit table extension
93 * (within the memory mapped region `map`).
95 unsigned char *table_lookup;
98 * Extended index.
100 * When trying to perform bitmap operations with objects that are not
101 * packed in `pack`, these objects are added to this "fake index" and
102 * are assumed to appear at the end of the packfile for all operations
104 struct eindex {
105 struct object **objects;
106 uint32_t *hashes;
107 uint32_t count, alloc;
108 kh_oid_pos_t *positions;
109 } ext_index;
111 /* Bitmap result of the last performed walk */
112 struct bitmap *result;
114 /* "have" bitmap from the last performed walk */
115 struct bitmap *haves;
117 /* Version of the bitmap index */
118 unsigned int version;
121 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
123 struct ewah_bitmap *parent;
124 struct ewah_bitmap *composed;
126 if (!st->xor)
127 return st->root;
129 composed = ewah_pool_new();
130 parent = lookup_stored_bitmap(st->xor);
131 ewah_xor(st->root, parent, composed);
133 ewah_pool_free(st->root);
134 st->root = composed;
135 st->xor = NULL;
137 return composed;
141 * Read a bitmap from the current read position on the mmaped
142 * index, and increase the read position accordingly
144 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
146 struct ewah_bitmap *b = ewah_pool_new();
148 ssize_t bitmap_size = ewah_read_mmap(b,
149 index->map + index->map_pos,
150 index->map_size - index->map_pos);
152 if (bitmap_size < 0) {
153 error(_("failed to load bitmap index (corrupted?)"));
154 ewah_pool_free(b);
155 return NULL;
158 index->map_pos += bitmap_size;
159 return b;
162 static uint32_t bitmap_num_objects(struct bitmap_index *index)
164 if (index->midx)
165 return index->midx->num_objects;
166 return index->pack->num_objects;
169 static int load_bitmap_header(struct bitmap_index *index)
171 struct bitmap_disk_header *header = (void *)index->map;
172 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
174 if (index->map_size < header_size + the_hash_algo->rawsz)
175 return error(_("corrupted bitmap index (too small)"));
177 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
178 return error(_("corrupted bitmap index file (wrong header)"));
180 index->version = ntohs(header->version);
181 if (index->version != 1)
182 return error(_("unsupported version '%d' for bitmap index file"), index->version);
184 /* Parse known bitmap format options */
186 uint32_t flags = ntohs(header->options);
187 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
188 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
190 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
191 BUG("unsupported options for bitmap index file "
192 "(Git requires BITMAP_OPT_FULL_DAG)");
194 if (flags & BITMAP_OPT_HASH_CACHE) {
195 if (cache_size > index_end - index->map - header_size)
196 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
197 index->hashes = (void *)(index_end - cache_size);
198 index_end -= cache_size;
201 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
202 size_t table_size = st_mult(ntohl(header->entry_count),
203 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
204 if (table_size > index_end - index->map - header_size)
205 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
206 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
207 index->table_lookup = (void *)(index_end - table_size);
208 index_end -= table_size;
212 index->entry_count = ntohl(header->entry_count);
213 index->checksum = header->checksum;
214 index->map_pos += header_size;
215 return 0;
218 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
219 struct ewah_bitmap *root,
220 const struct object_id *oid,
221 struct stored_bitmap *xor_with,
222 int flags)
224 struct stored_bitmap *stored;
225 khiter_t hash_pos;
226 int ret;
228 stored = xmalloc(sizeof(struct stored_bitmap));
229 stored->root = root;
230 stored->xor = xor_with;
231 stored->flags = flags;
232 oidcpy(&stored->oid, oid);
234 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
237 * A 0 return code means the insertion succeeded with no changes,
238 * because the SHA1 already existed on the map. This is bad, there
239 * shouldn't be duplicated commits in the index.
241 if (ret == 0) {
242 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
243 return NULL;
246 kh_value(index->bitmaps, hash_pos) = stored;
247 return stored;
250 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
252 uint32_t result = get_be32(buffer + *pos);
253 (*pos) += sizeof(result);
254 return result;
257 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
259 return buffer[(*pos)++];
262 #define MAX_XOR_OFFSET 160
264 static int nth_bitmap_object_oid(struct bitmap_index *index,
265 struct object_id *oid,
266 uint32_t n)
268 if (index->midx)
269 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
270 return nth_packed_object_id(oid, index->pack, n);
273 static int load_bitmap_entries_v1(struct bitmap_index *index)
275 uint32_t i;
276 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
278 for (i = 0; i < index->entry_count; ++i) {
279 int xor_offset, flags;
280 struct ewah_bitmap *bitmap = NULL;
281 struct stored_bitmap *xor_bitmap = NULL;
282 uint32_t commit_idx_pos;
283 struct object_id oid;
285 if (index->map_size - index->map_pos < 6)
286 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
288 commit_idx_pos = read_be32(index->map, &index->map_pos);
289 xor_offset = read_u8(index->map, &index->map_pos);
290 flags = read_u8(index->map, &index->map_pos);
292 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
293 return error(_("corrupt ewah bitmap: commit index %u out of range"),
294 (unsigned)commit_idx_pos);
296 bitmap = read_bitmap_1(index);
297 if (!bitmap)
298 return -1;
300 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
301 return error(_("corrupted bitmap pack index"));
303 if (xor_offset > 0) {
304 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
306 if (!xor_bitmap)
307 return error(_("invalid XOR offset in bitmap pack index"));
310 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
311 index, bitmap, &oid, xor_bitmap, flags);
314 return 0;
317 char *midx_bitmap_filename(struct multi_pack_index *midx)
319 struct strbuf buf = STRBUF_INIT;
321 get_midx_filename(&buf, midx->object_dir);
322 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
324 return strbuf_detach(&buf, NULL);
327 char *pack_bitmap_filename(struct packed_git *p)
329 size_t len;
331 if (!strip_suffix(p->pack_name, ".pack", &len))
332 BUG("pack_name does not end in .pack");
333 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
336 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
337 struct multi_pack_index *midx)
339 struct stat st;
340 char *bitmap_name = midx_bitmap_filename(midx);
341 int fd = git_open(bitmap_name);
342 uint32_t i;
343 struct packed_git *preferred;
345 if (fd < 0) {
346 if (errno != ENOENT)
347 warning_errno("cannot open '%s'", bitmap_name);
348 free(bitmap_name);
349 return -1;
351 free(bitmap_name);
353 if (fstat(fd, &st)) {
354 error_errno(_("cannot fstat bitmap file"));
355 close(fd);
356 return -1;
359 if (bitmap_git->pack || bitmap_git->midx) {
360 struct strbuf buf = STRBUF_INIT;
361 get_midx_filename(&buf, midx->object_dir);
362 trace2_data_string("bitmap", the_repository,
363 "ignoring extra midx bitmap file", buf.buf);
364 close(fd);
365 strbuf_release(&buf);
366 return -1;
369 bitmap_git->midx = midx;
370 bitmap_git->map_size = xsize_t(st.st_size);
371 bitmap_git->map_pos = 0;
372 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
373 MAP_PRIVATE, fd, 0);
374 close(fd);
376 if (load_bitmap_header(bitmap_git) < 0)
377 goto cleanup;
379 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
380 error(_("checksum doesn't match in MIDX and bitmap"));
381 goto cleanup;
384 if (load_midx_revindex(bitmap_git->midx)) {
385 warning(_("multi-pack bitmap is missing required reverse index"));
386 goto cleanup;
389 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
390 if (prepare_midx_pack(the_repository, bitmap_git->midx, i))
391 die(_("could not open pack %s"),
392 bitmap_git->midx->pack_names[i]);
395 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
396 if (!is_pack_valid(preferred)) {
397 warning(_("preferred pack (%s) is invalid"),
398 preferred->pack_name);
399 goto cleanup;
402 return 0;
404 cleanup:
405 munmap(bitmap_git->map, bitmap_git->map_size);
406 bitmap_git->map_size = 0;
407 bitmap_git->map_pos = 0;
408 bitmap_git->map = NULL;
409 bitmap_git->midx = NULL;
410 return -1;
413 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
415 int fd;
416 struct stat st;
417 char *bitmap_name;
419 bitmap_name = pack_bitmap_filename(packfile);
420 fd = git_open(bitmap_name);
422 if (fd < 0) {
423 if (errno != ENOENT)
424 warning_errno("cannot open '%s'", bitmap_name);
425 free(bitmap_name);
426 return -1;
428 free(bitmap_name);
430 if (fstat(fd, &st)) {
431 error_errno(_("cannot fstat bitmap file"));
432 close(fd);
433 return -1;
436 if (bitmap_git->pack || bitmap_git->midx) {
437 trace2_data_string("bitmap", the_repository,
438 "ignoring extra bitmap file", packfile->pack_name);
439 close(fd);
440 return -1;
443 if (!is_pack_valid(packfile)) {
444 close(fd);
445 return -1;
448 bitmap_git->pack = packfile;
449 bitmap_git->map_size = xsize_t(st.st_size);
450 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
451 bitmap_git->map_pos = 0;
452 close(fd);
454 if (load_bitmap_header(bitmap_git) < 0) {
455 munmap(bitmap_git->map, bitmap_git->map_size);
456 bitmap_git->map = NULL;
457 bitmap_git->map_size = 0;
458 bitmap_git->map_pos = 0;
459 bitmap_git->pack = NULL;
460 return -1;
463 trace2_data_string("bitmap", the_repository, "opened bitmap file",
464 packfile->pack_name);
465 return 0;
468 static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
470 if (bitmap_is_midx(bitmap_git)) {
471 uint32_t i;
472 int ret;
475 * The multi-pack-index's .rev file is already loaded via
476 * open_pack_bitmap_1().
478 * But we still need to open the individual pack .rev files,
479 * since we will need to make use of them in pack-objects.
481 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
482 ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
483 if (ret)
484 return ret;
486 return 0;
488 return load_pack_revindex(r, bitmap_git->pack);
491 static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
493 assert(bitmap_git->map);
495 bitmap_git->bitmaps = kh_init_oid_map();
496 bitmap_git->ext_index.positions = kh_init_oid_pos();
498 if (load_reverse_index(r, bitmap_git))
499 goto failed;
501 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
502 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
503 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
504 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
505 goto failed;
507 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
508 goto failed;
510 return 0;
512 failed:
513 munmap(bitmap_git->map, bitmap_git->map_size);
514 bitmap_git->map = NULL;
515 bitmap_git->map_size = 0;
517 kh_destroy_oid_map(bitmap_git->bitmaps);
518 bitmap_git->bitmaps = NULL;
520 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
521 bitmap_git->ext_index.positions = NULL;
523 return -1;
526 static int open_pack_bitmap(struct repository *r,
527 struct bitmap_index *bitmap_git)
529 struct packed_git *p;
530 int ret = -1;
532 for (p = get_all_packs(r); p; p = p->next) {
533 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
534 ret = 0;
536 * The only reason to keep looking is to report
537 * duplicates.
539 if (!trace2_is_enabled())
540 break;
544 return ret;
547 static int open_midx_bitmap(struct repository *r,
548 struct bitmap_index *bitmap_git)
550 int ret = -1;
551 struct multi_pack_index *midx;
553 assert(!bitmap_git->map);
555 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
556 if (!open_midx_bitmap_1(bitmap_git, midx))
557 ret = 0;
559 return ret;
562 static int open_bitmap(struct repository *r,
563 struct bitmap_index *bitmap_git)
565 int found;
567 assert(!bitmap_git->map);
569 found = !open_midx_bitmap(r, bitmap_git);
572 * these will all be skipped if we opened a midx bitmap; but run it
573 * anyway if tracing is enabled to report the duplicates
575 if (!found || trace2_is_enabled())
576 found |= !open_pack_bitmap(r, bitmap_git);
578 return found ? 0 : -1;
581 struct bitmap_index *prepare_bitmap_git(struct repository *r)
583 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
585 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
586 return bitmap_git;
588 free_bitmap_index(bitmap_git);
589 return NULL;
592 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
594 struct repository *r = the_repository;
595 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
597 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
598 return bitmap_git;
600 free_bitmap_index(bitmap_git);
601 return NULL;
604 struct include_data {
605 struct bitmap_index *bitmap_git;
606 struct bitmap *base;
607 struct bitmap *seen;
610 struct bitmap_lookup_table_triplet {
611 uint32_t commit_pos;
612 uint64_t offset;
613 uint32_t xor_row;
616 struct bitmap_lookup_table_xor_item {
617 struct object_id oid;
618 uint64_t offset;
622 * Given a `triplet` struct pointer and pointer `p`, this
623 * function reads the triplet beginning at `p` into the struct.
624 * Note that this function assumes that there is enough memory
625 * left for filling the `triplet` struct from `p`.
627 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
628 const unsigned char *p)
630 if (!triplet)
631 return -1;
633 triplet->commit_pos = get_be32(p);
634 p += sizeof(uint32_t);
635 triplet->offset = get_be64(p);
636 p += sizeof(uint64_t);
637 triplet->xor_row = get_be32(p);
638 return 0;
642 * This function gets the raw triplet from `row`'th row in the
643 * lookup table and fills that data to the `triplet`.
645 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
646 uint32_t pos,
647 struct bitmap_lookup_table_triplet *triplet)
649 unsigned char *p = NULL;
650 if (pos >= bitmap_git->entry_count)
651 return error(_("corrupt bitmap lookup table: triplet position out of index"));
653 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
655 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
659 * Searches for a matching triplet. `commit_pos` is a pointer
660 * to the wanted commit position value. `table_entry` points to
661 * a triplet in lookup table. The first 4 bytes of each
662 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
664 static int triplet_cmp(const void *commit_pos, const void *table_entry)
667 uint32_t a = *(uint32_t *)commit_pos;
668 uint32_t b = get_be32(table_entry);
669 if (a > b)
670 return 1;
671 else if (a < b)
672 return -1;
674 return 0;
677 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
678 struct object_id *oid,
679 uint32_t *result)
681 int found;
683 if (bitmap_is_midx(bitmap_git))
684 found = bsearch_midx(oid, bitmap_git->midx, result);
685 else
686 found = bsearch_pack(oid, bitmap_git->pack, result);
688 return found;
692 * `bsearch_triplet_by_pos` function searches for the raw triplet
693 * having commit position same as `commit_pos` and fills `triplet`
694 * object from the raw triplet. Returns 1 on success and 0 on
695 * failure.
697 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
698 struct bitmap_index *bitmap_git,
699 struct bitmap_lookup_table_triplet *triplet)
701 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
702 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
704 if (!p)
705 return -1;
707 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
710 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
711 struct commit *commit)
713 uint32_t commit_pos, xor_row;
714 uint64_t offset;
715 int flags;
716 struct bitmap_lookup_table_triplet triplet;
717 struct object_id *oid = &commit->object.oid;
718 struct ewah_bitmap *bitmap;
719 struct stored_bitmap *xor_bitmap = NULL;
720 const int bitmap_header_size = 6;
721 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
722 static size_t xor_items_nr = 0, xor_items_alloc = 0;
723 static int is_corrupt = 0;
724 int xor_flags;
725 khiter_t hash_pos;
726 struct bitmap_lookup_table_xor_item *xor_item;
728 if (is_corrupt)
729 return NULL;
731 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
732 return NULL;
734 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
735 return NULL;
737 xor_items_nr = 0;
738 offset = triplet.offset;
739 xor_row = triplet.xor_row;
741 while (xor_row != 0xffffffff) {
742 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
744 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
745 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
746 goto corrupt;
749 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
750 goto corrupt;
752 xor_item = &xor_items[xor_items_nr];
753 xor_item->offset = triplet.offset;
755 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
756 error(_("corrupt bitmap lookup table: commit index %u out of range"),
757 triplet.commit_pos);
758 goto corrupt;
761 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
764 * If desired bitmap is already stored, we don't need
765 * to iterate further. Because we know that bitmaps
766 * that are needed to be parsed to parse this bitmap
767 * has already been stored. So, assign this stored bitmap
768 * to the xor_bitmap.
770 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
771 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
772 break;
773 xor_items_nr++;
774 xor_row = triplet.xor_row;
777 while (xor_items_nr) {
778 xor_item = &xor_items[xor_items_nr - 1];
779 bitmap_git->map_pos = xor_item->offset;
780 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
781 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
782 oid_to_hex(&xor_item->oid));
783 goto corrupt;
786 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
787 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
788 bitmap = read_bitmap_1(bitmap_git);
790 if (!bitmap)
791 goto corrupt;
793 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
794 xor_items_nr--;
797 bitmap_git->map_pos = offset;
798 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
799 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
800 oid_to_hex(oid));
801 goto corrupt;
805 * Don't bother reading the commit's index position or its xor
806 * offset:
808 * - The commit's index position is irrelevant to us, since
809 * load_bitmap_entries_v1 only uses it to learn the object
810 * id which is used to compute the hashmap's key. We already
811 * have an object id, so no need to look it up again.
813 * - The xor_offset is unusable for us, since it specifies how
814 * many entries previous to ours we should look at. This
815 * makes sense when reading the bitmaps sequentially (as in
816 * load_bitmap_entries_v1()), since we can keep track of
817 * each bitmap as we read them.
819 * But it can't work for us, since the bitmap's don't have a
820 * fixed size. So we learn the position of the xor'd bitmap
821 * from the commit table (and resolve it to a bitmap in the
822 * above if-statement).
824 * Instead, we can skip ahead and immediately read the flags and
825 * ewah bitmap.
827 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
828 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
829 bitmap = read_bitmap_1(bitmap_git);
831 if (!bitmap)
832 goto corrupt;
834 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
836 corrupt:
837 free(xor_items);
838 is_corrupt = 1;
839 return NULL;
842 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
843 struct commit *commit)
845 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
846 commit->object.oid);
847 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
848 struct stored_bitmap *bitmap = NULL;
849 if (!bitmap_git->table_lookup)
850 return NULL;
852 /* this is a fairly hot codepath - no trace2_region please */
853 /* NEEDSWORK: cache misses aren't recorded */
854 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
855 if (!bitmap)
856 return NULL;
857 return lookup_stored_bitmap(bitmap);
859 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
862 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
863 const struct object_id *oid)
865 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
866 khiter_t pos = kh_get_oid_pos(positions, *oid);
868 if (pos < kh_end(positions)) {
869 int bitmap_pos = kh_value(positions, pos);
870 return bitmap_pos + bitmap_num_objects(bitmap_git);
873 return -1;
876 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
877 const struct object_id *oid)
879 uint32_t pos;
880 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
881 if (!offset)
882 return -1;
884 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
885 return -1;
886 return pos;
889 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
890 const struct object_id *oid)
892 uint32_t want, got;
893 if (!bsearch_midx(oid, bitmap_git->midx, &want))
894 return -1;
896 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
897 return -1;
898 return got;
901 static int bitmap_position(struct bitmap_index *bitmap_git,
902 const struct object_id *oid)
904 int pos;
905 if (bitmap_is_midx(bitmap_git))
906 pos = bitmap_position_midx(bitmap_git, oid);
907 else
908 pos = bitmap_position_packfile(bitmap_git, oid);
909 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
912 static int ext_index_add_object(struct bitmap_index *bitmap_git,
913 struct object *object, const char *name)
915 struct eindex *eindex = &bitmap_git->ext_index;
917 khiter_t hash_pos;
918 int hash_ret;
919 int bitmap_pos;
921 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
922 if (hash_ret > 0) {
923 if (eindex->count >= eindex->alloc) {
924 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
925 REALLOC_ARRAY(eindex->objects, eindex->alloc);
926 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
929 bitmap_pos = eindex->count;
930 eindex->objects[eindex->count] = object;
931 eindex->hashes[eindex->count] = pack_name_hash(name);
932 kh_value(eindex->positions, hash_pos) = bitmap_pos;
933 eindex->count++;
934 } else {
935 bitmap_pos = kh_value(eindex->positions, hash_pos);
938 return bitmap_pos + bitmap_num_objects(bitmap_git);
941 struct bitmap_show_data {
942 struct bitmap_index *bitmap_git;
943 struct bitmap *base;
946 static void show_object(struct object *object, const char *name, void *data_)
948 struct bitmap_show_data *data = data_;
949 int bitmap_pos;
951 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
953 if (bitmap_pos < 0)
954 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
955 name);
957 bitmap_set(data->base, bitmap_pos);
960 static void show_commit(struct commit *commit UNUSED,
961 void *data UNUSED)
965 static int add_to_include_set(struct bitmap_index *bitmap_git,
966 struct include_data *data,
967 struct commit *commit,
968 int bitmap_pos)
970 struct ewah_bitmap *partial;
972 if (data->seen && bitmap_get(data->seen, bitmap_pos))
973 return 0;
975 if (bitmap_get(data->base, bitmap_pos))
976 return 0;
978 partial = bitmap_for_commit(bitmap_git, commit);
979 if (partial) {
980 bitmap_or_ewah(data->base, partial);
981 return 0;
984 bitmap_set(data->base, bitmap_pos);
985 return 1;
988 static int should_include(struct commit *commit, void *_data)
990 struct include_data *data = _data;
991 int bitmap_pos;
993 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
994 if (bitmap_pos < 0)
995 bitmap_pos = ext_index_add_object(data->bitmap_git,
996 (struct object *)commit,
997 NULL);
999 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
1000 struct commit_list *parent = commit->parents;
1002 while (parent) {
1003 parent->item->object.flags |= SEEN;
1004 parent = parent->next;
1007 return 0;
1010 return 1;
1013 static int should_include_obj(struct object *obj, void *_data)
1015 struct include_data *data = _data;
1016 int bitmap_pos;
1018 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1019 if (bitmap_pos < 0)
1020 return 1;
1021 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1022 bitmap_get(data->base, bitmap_pos)) {
1023 obj->flags |= SEEN;
1024 return 0;
1026 return 1;
1029 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1030 struct bitmap **base,
1031 struct commit *commit)
1033 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1035 if (!or_with)
1036 return 0;
1038 if (!*base)
1039 *base = ewah_to_bitmap(or_with);
1040 else
1041 bitmap_or_ewah(*base, or_with);
1043 return 1;
1046 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1047 struct rev_info *revs,
1048 struct object_list *roots,
1049 struct bitmap *seen)
1051 struct bitmap *base = NULL;
1052 int needs_walk = 0;
1054 struct object_list *not_mapped = NULL;
1057 * Go through all the roots for the walk. The ones that have bitmaps
1058 * on the bitmap index will be `or`ed together to form an initial
1059 * global reachability analysis.
1061 * The ones without bitmaps in the index will be stored in the
1062 * `not_mapped_list` for further processing.
1064 while (roots) {
1065 struct object *object = roots->item;
1066 roots = roots->next;
1068 if (object->type == OBJ_COMMIT &&
1069 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1070 object->flags |= SEEN;
1071 continue;
1074 object_list_insert(object, &not_mapped);
1078 * Best case scenario: We found bitmaps for all the roots,
1079 * so the resulting `or` bitmap has the full reachability analysis
1081 if (!not_mapped)
1082 return base;
1084 roots = not_mapped;
1087 * Let's iterate through all the roots that don't have bitmaps to
1088 * check if we can determine them to be reachable from the existing
1089 * global bitmap.
1091 * If we cannot find them in the existing global bitmap, we'll need
1092 * to push them to an actual walk and run it until we can confirm
1093 * they are reachable
1095 while (roots) {
1096 struct object *object = roots->item;
1097 int pos;
1099 roots = roots->next;
1100 pos = bitmap_position(bitmap_git, &object->oid);
1102 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1103 object->flags &= ~UNINTERESTING;
1104 add_pending_object(revs, object, "");
1105 needs_walk = 1;
1106 } else {
1107 object->flags |= SEEN;
1111 if (needs_walk) {
1112 struct include_data incdata;
1113 struct bitmap_show_data show_data;
1115 if (!base)
1116 base = bitmap_new();
1118 incdata.bitmap_git = bitmap_git;
1119 incdata.base = base;
1120 incdata.seen = seen;
1122 revs->include_check = should_include;
1123 revs->include_check_obj = should_include_obj;
1124 revs->include_check_data = &incdata;
1126 if (prepare_revision_walk(revs))
1127 die(_("revision walk setup failed"));
1129 show_data.bitmap_git = bitmap_git;
1130 show_data.base = base;
1132 traverse_commit_list(revs,
1133 show_commit, show_object,
1134 &show_data);
1136 revs->include_check = NULL;
1137 revs->include_check_obj = NULL;
1138 revs->include_check_data = NULL;
1141 return base;
1144 static void show_extended_objects(struct bitmap_index *bitmap_git,
1145 struct rev_info *revs,
1146 show_reachable_fn show_reach)
1148 struct bitmap *objects = bitmap_git->result;
1149 struct eindex *eindex = &bitmap_git->ext_index;
1150 uint32_t i;
1152 for (i = 0; i < eindex->count; ++i) {
1153 struct object *obj;
1155 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1156 continue;
1158 obj = eindex->objects[i];
1159 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1160 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1161 (obj->type == OBJ_TAG && !revs->tag_objects))
1162 continue;
1164 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1168 static void init_type_iterator(struct ewah_iterator *it,
1169 struct bitmap_index *bitmap_git,
1170 enum object_type type)
1172 switch (type) {
1173 case OBJ_COMMIT:
1174 ewah_iterator_init(it, bitmap_git->commits);
1175 break;
1177 case OBJ_TREE:
1178 ewah_iterator_init(it, bitmap_git->trees);
1179 break;
1181 case OBJ_BLOB:
1182 ewah_iterator_init(it, bitmap_git->blobs);
1183 break;
1185 case OBJ_TAG:
1186 ewah_iterator_init(it, bitmap_git->tags);
1187 break;
1189 default:
1190 BUG("object type %d not stored by bitmap type index", type);
1191 break;
1195 static void show_objects_for_type(
1196 struct bitmap_index *bitmap_git,
1197 enum object_type object_type,
1198 show_reachable_fn show_reach)
1200 size_t i = 0;
1201 uint32_t offset;
1203 struct ewah_iterator it;
1204 eword_t filter;
1206 struct bitmap *objects = bitmap_git->result;
1208 init_type_iterator(&it, bitmap_git, object_type);
1210 for (i = 0; i < objects->word_alloc &&
1211 ewah_iterator_next(&filter, &it); i++) {
1212 eword_t word = objects->words[i] & filter;
1213 size_t pos = (i * BITS_IN_EWORD);
1215 if (!word)
1216 continue;
1218 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1219 struct packed_git *pack;
1220 struct object_id oid;
1221 uint32_t hash = 0, index_pos;
1222 off_t ofs;
1224 if ((word >> offset) == 0)
1225 break;
1227 offset += ewah_bit_ctz64(word >> offset);
1229 if (bitmap_is_midx(bitmap_git)) {
1230 struct multi_pack_index *m = bitmap_git->midx;
1231 uint32_t pack_id;
1233 index_pos = pack_pos_to_midx(m, pos + offset);
1234 ofs = nth_midxed_offset(m, index_pos);
1235 nth_midxed_object_oid(&oid, m, index_pos);
1237 pack_id = nth_midxed_pack_int_id(m, index_pos);
1238 pack = bitmap_git->midx->packs[pack_id];
1239 } else {
1240 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1241 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1242 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1244 pack = bitmap_git->pack;
1247 if (bitmap_git->hashes)
1248 hash = get_be32(bitmap_git->hashes + index_pos);
1250 show_reach(&oid, object_type, 0, hash, pack, ofs);
1255 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1256 struct object_list *roots)
1258 while (roots) {
1259 struct object *object = roots->item;
1260 roots = roots->next;
1262 if (bitmap_is_midx(bitmap_git)) {
1263 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1264 return 1;
1265 } else {
1266 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1267 return 1;
1271 return 0;
1274 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1275 struct object_list *tip_objects,
1276 enum object_type type)
1278 struct bitmap *result = bitmap_new();
1279 struct object_list *p;
1281 for (p = tip_objects; p; p = p->next) {
1282 int pos;
1284 if (p->item->type != type)
1285 continue;
1287 pos = bitmap_position(bitmap_git, &p->item->oid);
1288 if (pos < 0)
1289 continue;
1291 bitmap_set(result, pos);
1294 return result;
1297 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1298 struct object_list *tip_objects,
1299 struct bitmap *to_filter,
1300 enum object_type type)
1302 struct eindex *eindex = &bitmap_git->ext_index;
1303 struct bitmap *tips;
1304 struct ewah_iterator it;
1305 eword_t mask;
1306 uint32_t i;
1309 * The non-bitmap version of this filter never removes
1310 * objects which the other side specifically asked for,
1311 * so we must match that behavior.
1313 tips = find_tip_objects(bitmap_git, tip_objects, type);
1316 * We can use the type-level bitmap for 'type' to work in whole
1317 * words for the objects that are actually in the bitmapped
1318 * packfile.
1320 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1321 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1322 i++) {
1323 if (i < tips->word_alloc)
1324 mask &= ~tips->words[i];
1325 to_filter->words[i] &= ~mask;
1329 * Clear any objects that weren't in the packfile (and so would
1330 * not have been caught by the loop above. We'll have to check
1331 * them individually.
1333 for (i = 0; i < eindex->count; i++) {
1334 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1335 if (eindex->objects[i]->type == type &&
1336 bitmap_get(to_filter, pos) &&
1337 !bitmap_get(tips, pos))
1338 bitmap_unset(to_filter, pos);
1341 bitmap_free(tips);
1344 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1345 struct object_list *tip_objects,
1346 struct bitmap *to_filter)
1348 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1349 OBJ_BLOB);
1352 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1353 uint32_t pos)
1355 unsigned long size;
1356 struct object_info oi = OBJECT_INFO_INIT;
1358 oi.sizep = &size;
1360 if (pos < bitmap_num_objects(bitmap_git)) {
1361 struct packed_git *pack;
1362 off_t ofs;
1364 if (bitmap_is_midx(bitmap_git)) {
1365 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1366 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1368 pack = bitmap_git->midx->packs[pack_id];
1369 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1370 } else {
1371 pack = bitmap_git->pack;
1372 ofs = pack_pos_to_offset(pack, pos);
1375 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1376 struct object_id oid;
1377 nth_bitmap_object_oid(bitmap_git, &oid,
1378 pack_pos_to_index(pack, pos));
1379 die(_("unable to get size of %s"), oid_to_hex(&oid));
1381 } else {
1382 struct eindex *eindex = &bitmap_git->ext_index;
1383 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1384 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1385 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1388 return size;
1391 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1392 struct object_list *tip_objects,
1393 struct bitmap *to_filter,
1394 unsigned long limit)
1396 struct eindex *eindex = &bitmap_git->ext_index;
1397 struct bitmap *tips;
1398 struct ewah_iterator it;
1399 eword_t mask;
1400 uint32_t i;
1402 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1404 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1405 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1406 i++) {
1407 eword_t word = to_filter->words[i] & mask;
1408 unsigned offset;
1410 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1411 uint32_t pos;
1413 if ((word >> offset) == 0)
1414 break;
1415 offset += ewah_bit_ctz64(word >> offset);
1416 pos = i * BITS_IN_EWORD + offset;
1418 if (!bitmap_get(tips, pos) &&
1419 get_size_by_pos(bitmap_git, pos) >= limit)
1420 bitmap_unset(to_filter, pos);
1424 for (i = 0; i < eindex->count; i++) {
1425 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1426 if (eindex->objects[i]->type == OBJ_BLOB &&
1427 bitmap_get(to_filter, pos) &&
1428 !bitmap_get(tips, pos) &&
1429 get_size_by_pos(bitmap_git, pos) >= limit)
1430 bitmap_unset(to_filter, pos);
1433 bitmap_free(tips);
1436 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1437 struct object_list *tip_objects,
1438 struct bitmap *to_filter,
1439 unsigned long limit)
1441 if (limit)
1442 BUG("filter_bitmap_tree_depth given non-zero limit");
1444 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1445 OBJ_TREE);
1446 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1447 OBJ_BLOB);
1450 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1451 struct object_list *tip_objects,
1452 struct bitmap *to_filter,
1453 enum object_type object_type)
1455 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1456 BUG("filter_bitmap_object_type given invalid object");
1458 if (object_type != OBJ_TAG)
1459 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1460 if (object_type != OBJ_COMMIT)
1461 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1462 if (object_type != OBJ_TREE)
1463 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1464 if (object_type != OBJ_BLOB)
1465 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1468 static int filter_bitmap(struct bitmap_index *bitmap_git,
1469 struct object_list *tip_objects,
1470 struct bitmap *to_filter,
1471 struct list_objects_filter_options *filter)
1473 if (!filter || filter->choice == LOFC_DISABLED)
1474 return 0;
1476 if (filter->choice == LOFC_BLOB_NONE) {
1477 if (bitmap_git)
1478 filter_bitmap_blob_none(bitmap_git, tip_objects,
1479 to_filter);
1480 return 0;
1483 if (filter->choice == LOFC_BLOB_LIMIT) {
1484 if (bitmap_git)
1485 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1486 to_filter,
1487 filter->blob_limit_value);
1488 return 0;
1491 if (filter->choice == LOFC_TREE_DEPTH &&
1492 filter->tree_exclude_depth == 0) {
1493 if (bitmap_git)
1494 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1495 to_filter,
1496 filter->tree_exclude_depth);
1497 return 0;
1500 if (filter->choice == LOFC_OBJECT_TYPE) {
1501 if (bitmap_git)
1502 filter_bitmap_object_type(bitmap_git, tip_objects,
1503 to_filter,
1504 filter->object_type);
1505 return 0;
1508 if (filter->choice == LOFC_COMBINE) {
1509 int i;
1510 for (i = 0; i < filter->sub_nr; i++) {
1511 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1512 &filter->sub[i]) < 0)
1513 return -1;
1515 return 0;
1518 /* filter choice not handled */
1519 return -1;
1522 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1524 return !filter_bitmap(NULL, NULL, NULL, filter);
1527 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1528 int filter_provided_objects)
1530 unsigned int i;
1532 struct object_list *wants = NULL;
1533 struct object_list *haves = NULL;
1535 struct bitmap *wants_bitmap = NULL;
1536 struct bitmap *haves_bitmap = NULL;
1538 struct bitmap_index *bitmap_git;
1541 * We can't do pathspec limiting with bitmaps, because we don't know
1542 * which commits are associated with which object changes (let alone
1543 * even which objects are associated with which paths).
1545 if (revs->prune)
1546 return NULL;
1548 if (!can_filter_bitmap(&revs->filter))
1549 return NULL;
1551 /* try to open a bitmapped pack, but don't parse it yet
1552 * because we may not need to use it */
1553 CALLOC_ARRAY(bitmap_git, 1);
1554 if (open_bitmap(revs->repo, bitmap_git) < 0)
1555 goto cleanup;
1557 for (i = 0; i < revs->pending.nr; ++i) {
1558 struct object *object = revs->pending.objects[i].item;
1560 if (object->type == OBJ_NONE)
1561 parse_object_or_die(&object->oid, NULL);
1563 while (object->type == OBJ_TAG) {
1564 struct tag *tag = (struct tag *) object;
1566 if (object->flags & UNINTERESTING)
1567 object_list_insert(object, &haves);
1568 else
1569 object_list_insert(object, &wants);
1571 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1572 object->flags |= (tag->object.flags & UNINTERESTING);
1575 if (object->flags & UNINTERESTING)
1576 object_list_insert(object, &haves);
1577 else
1578 object_list_insert(object, &wants);
1582 * if we have a HAVES list, but none of those haves is contained
1583 * in the packfile that has a bitmap, we don't have anything to
1584 * optimize here
1586 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1587 goto cleanup;
1589 /* if we don't want anything, we're done here */
1590 if (!wants)
1591 goto cleanup;
1594 * now we're going to use bitmaps, so load the actual bitmap entries
1595 * from disk. this is the point of no return; after this the rev_list
1596 * becomes invalidated and we must perform the revwalk through bitmaps
1598 if (load_bitmap(revs->repo, bitmap_git) < 0)
1599 goto cleanup;
1601 object_array_clear(&revs->pending);
1603 if (haves) {
1604 revs->ignore_missing_links = 1;
1605 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1606 reset_revision_walk();
1607 revs->ignore_missing_links = 0;
1609 if (!haves_bitmap)
1610 BUG("failed to perform bitmap walk");
1613 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1615 if (!wants_bitmap)
1616 BUG("failed to perform bitmap walk");
1618 if (haves_bitmap)
1619 bitmap_and_not(wants_bitmap, haves_bitmap);
1621 filter_bitmap(bitmap_git,
1622 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1623 wants_bitmap,
1624 &revs->filter);
1626 bitmap_git->result = wants_bitmap;
1627 bitmap_git->haves = haves_bitmap;
1629 object_list_free(&wants);
1630 object_list_free(&haves);
1632 return bitmap_git;
1634 cleanup:
1635 free_bitmap_index(bitmap_git);
1636 object_list_free(&wants);
1637 object_list_free(&haves);
1638 return NULL;
1642 * -1 means "stop trying further objects"; 0 means we may or may not have
1643 * reused, but you can keep feeding bits.
1645 static int try_partial_reuse(struct packed_git *pack,
1646 size_t pos,
1647 struct bitmap *reuse,
1648 struct pack_window **w_curs)
1650 off_t offset, delta_obj_offset;
1651 enum object_type type;
1652 unsigned long size;
1655 * try_partial_reuse() is called either on (a) objects in the
1656 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1657 * objects in the preferred pack of a multi-pack bitmap.
1658 * Importantly, the latter can pretend as if only a single pack
1659 * exists because:
1661 * - The first pack->num_objects bits of a MIDX bitmap are
1662 * reserved for the preferred pack, and
1664 * - Ties due to duplicate objects are always resolved in
1665 * favor of the preferred pack.
1667 * Therefore we do not need to ever ask the MIDX for its copy of
1668 * an object by OID, since it will always select it from the
1669 * preferred pack. Likewise, the selected copy of the base
1670 * object for any deltas will reside in the same pack.
1672 * This means that we can reuse pos when looking up the bit in
1673 * the reuse bitmap, too, since bits corresponding to the
1674 * preferred pack precede all bits from other packs.
1677 if (pos >= pack->num_objects)
1678 return -1; /* not actually in the pack or MIDX preferred pack */
1680 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1681 type = unpack_object_header(pack, w_curs, &offset, &size);
1682 if (type < 0)
1683 return -1; /* broken packfile, punt */
1685 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1686 off_t base_offset;
1687 uint32_t base_pos;
1690 * Find the position of the base object so we can look it up
1691 * in our bitmaps. If we can't come up with an offset, or if
1692 * that offset is not in the revidx, the pack is corrupt.
1693 * There's nothing we can do, so just punt on this object,
1694 * and the normal slow path will complain about it in
1695 * more detail.
1697 base_offset = get_delta_base(pack, w_curs, &offset, type,
1698 delta_obj_offset);
1699 if (!base_offset)
1700 return 0;
1701 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1702 return 0;
1705 * We assume delta dependencies always point backwards. This
1706 * lets us do a single pass, and is basically always true
1707 * due to the way OFS_DELTAs work. You would not typically
1708 * find REF_DELTA in a bitmapped pack, since we only bitmap
1709 * packs we write fresh, and OFS_DELTA is the default). But
1710 * let's double check to make sure the pack wasn't written with
1711 * odd parameters.
1713 if (base_pos >= pos)
1714 return 0;
1717 * And finally, if we're not sending the base as part of our
1718 * reuse chunk, then don't send this object either. The base
1719 * would come after us, along with other objects not
1720 * necessarily in the pack, which means we'd need to convert
1721 * to REF_DELTA on the fly. Better to just let the normal
1722 * object_entry code path handle it.
1724 if (!bitmap_get(reuse, base_pos))
1725 return 0;
1729 * If we got here, then the object is OK to reuse. Mark it.
1731 bitmap_set(reuse, pos);
1732 return 0;
1735 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1737 struct multi_pack_index *m = bitmap_git->midx;
1738 if (!m)
1739 BUG("midx_preferred_pack: requires non-empty MIDX");
1740 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1743 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1744 struct packed_git **packfile_out,
1745 uint32_t *entries,
1746 struct bitmap **reuse_out)
1748 struct repository *r = the_repository;
1749 struct packed_git *pack;
1750 struct bitmap *result = bitmap_git->result;
1751 struct bitmap *reuse;
1752 struct pack_window *w_curs = NULL;
1753 size_t i = 0;
1754 uint32_t offset;
1755 uint32_t objects_nr;
1757 assert(result);
1759 load_reverse_index(r, bitmap_git);
1761 if (bitmap_is_midx(bitmap_git))
1762 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1763 else
1764 pack = bitmap_git->pack;
1765 objects_nr = pack->num_objects;
1767 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1768 i++;
1771 * Don't mark objects not in the packfile or preferred pack. This bitmap
1772 * marks objects eligible for reuse, but the pack-reuse code only
1773 * understands how to reuse a single pack. Since the preferred pack is
1774 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1775 * we use it instead of another pack. In single-pack bitmaps, the choice
1776 * is made for us.
1778 if (i > objects_nr / BITS_IN_EWORD)
1779 i = objects_nr / BITS_IN_EWORD;
1781 reuse = bitmap_word_alloc(i);
1782 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1784 for (; i < result->word_alloc; ++i) {
1785 eword_t word = result->words[i];
1786 size_t pos = (i * BITS_IN_EWORD);
1788 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1789 if ((word >> offset) == 0)
1790 break;
1792 offset += ewah_bit_ctz64(word >> offset);
1793 if (try_partial_reuse(pack, pos + offset,
1794 reuse, &w_curs) < 0) {
1796 * try_partial_reuse indicated we couldn't reuse
1797 * any bits, so there is no point in trying more
1798 * bits in the current word, or any other words
1799 * in result.
1801 * Jump out of both loops to avoid future
1802 * unnecessary calls to try_partial_reuse.
1804 goto done;
1809 done:
1810 unuse_pack(&w_curs);
1812 *entries = bitmap_popcount(reuse);
1813 if (!*entries) {
1814 bitmap_free(reuse);
1815 return -1;
1819 * Drop any reused objects from the result, since they will not
1820 * need to be handled separately.
1822 bitmap_and_not(result, reuse);
1823 *packfile_out = pack;
1824 *reuse_out = reuse;
1825 return 0;
1828 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1829 struct bitmap *bitmap, const struct object_id *oid)
1831 int idx;
1833 if (!bitmap)
1834 return 0;
1836 idx = bitmap_position(bitmap_git, oid);
1837 return idx >= 0 && bitmap_get(bitmap, idx);
1840 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1841 struct rev_info *revs,
1842 show_reachable_fn show_reachable)
1844 assert(bitmap_git->result);
1846 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1847 if (revs->tree_objects)
1848 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1849 if (revs->blob_objects)
1850 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1851 if (revs->tag_objects)
1852 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1854 show_extended_objects(bitmap_git, revs, show_reachable);
1857 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1858 enum object_type type)
1860 struct bitmap *objects = bitmap_git->result;
1861 struct eindex *eindex = &bitmap_git->ext_index;
1863 uint32_t i = 0, count = 0;
1864 struct ewah_iterator it;
1865 eword_t filter;
1867 init_type_iterator(&it, bitmap_git, type);
1869 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1870 eword_t word = objects->words[i++] & filter;
1871 count += ewah_bit_popcount64(word);
1874 for (i = 0; i < eindex->count; ++i) {
1875 if (eindex->objects[i]->type == type &&
1876 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1877 count++;
1880 return count;
1883 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1884 uint32_t *commits, uint32_t *trees,
1885 uint32_t *blobs, uint32_t *tags)
1887 assert(bitmap_git->result);
1889 if (commits)
1890 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1892 if (trees)
1893 *trees = count_object_type(bitmap_git, OBJ_TREE);
1895 if (blobs)
1896 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1898 if (tags)
1899 *tags = count_object_type(bitmap_git, OBJ_TAG);
1902 struct bitmap_test_data {
1903 struct bitmap_index *bitmap_git;
1904 struct bitmap *base;
1905 struct bitmap *commits;
1906 struct bitmap *trees;
1907 struct bitmap *blobs;
1908 struct bitmap *tags;
1909 struct progress *prg;
1910 size_t seen;
1913 static void test_bitmap_type(struct bitmap_test_data *tdata,
1914 struct object *obj, int pos)
1916 enum object_type bitmap_type = OBJ_NONE;
1917 int bitmaps_nr = 0;
1919 if (bitmap_get(tdata->commits, pos)) {
1920 bitmap_type = OBJ_COMMIT;
1921 bitmaps_nr++;
1923 if (bitmap_get(tdata->trees, pos)) {
1924 bitmap_type = OBJ_TREE;
1925 bitmaps_nr++;
1927 if (bitmap_get(tdata->blobs, pos)) {
1928 bitmap_type = OBJ_BLOB;
1929 bitmaps_nr++;
1931 if (bitmap_get(tdata->tags, pos)) {
1932 bitmap_type = OBJ_TAG;
1933 bitmaps_nr++;
1936 if (bitmap_type == OBJ_NONE)
1937 die(_("object '%s' not found in type bitmaps"),
1938 oid_to_hex(&obj->oid));
1940 if (bitmaps_nr > 1)
1941 die(_("object '%s' does not have a unique type"),
1942 oid_to_hex(&obj->oid));
1944 if (bitmap_type != obj->type)
1945 die(_("object '%s': real type '%s', expected: '%s'"),
1946 oid_to_hex(&obj->oid),
1947 type_name(obj->type),
1948 type_name(bitmap_type));
1951 static void test_show_object(struct object *object,
1952 const char *name UNUSED,
1953 void *data)
1955 struct bitmap_test_data *tdata = data;
1956 int bitmap_pos;
1958 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1959 if (bitmap_pos < 0)
1960 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
1961 test_bitmap_type(tdata, object, bitmap_pos);
1963 bitmap_set(tdata->base, bitmap_pos);
1964 display_progress(tdata->prg, ++tdata->seen);
1967 static void test_show_commit(struct commit *commit, void *data)
1969 struct bitmap_test_data *tdata = data;
1970 int bitmap_pos;
1972 bitmap_pos = bitmap_position(tdata->bitmap_git,
1973 &commit->object.oid);
1974 if (bitmap_pos < 0)
1975 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
1976 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1978 bitmap_set(tdata->base, bitmap_pos);
1979 display_progress(tdata->prg, ++tdata->seen);
1982 void test_bitmap_walk(struct rev_info *revs)
1984 struct object *root;
1985 struct bitmap *result = NULL;
1986 size_t result_popcnt;
1987 struct bitmap_test_data tdata;
1988 struct bitmap_index *bitmap_git;
1989 struct ewah_bitmap *bm;
1991 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1992 die(_("failed to load bitmap indexes"));
1994 if (revs->pending.nr != 1)
1995 die(_("you must specify exactly one commit to test"));
1997 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
1998 bitmap_git->version,
1999 bitmap_git->entry_count,
2000 bitmap_git->table_lookup ? "" : " loaded");
2002 root = revs->pending.objects[0].item;
2003 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2005 if (bm) {
2006 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2007 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2009 result = ewah_to_bitmap(bm);
2012 if (!result)
2013 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2015 revs->tag_objects = 1;
2016 revs->tree_objects = 1;
2017 revs->blob_objects = 1;
2019 result_popcnt = bitmap_popcount(result);
2021 if (prepare_revision_walk(revs))
2022 die(_("revision walk setup failed"));
2024 tdata.bitmap_git = bitmap_git;
2025 tdata.base = bitmap_new();
2026 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2027 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2028 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2029 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2030 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2031 tdata.seen = 0;
2033 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2035 stop_progress(&tdata.prg);
2037 if (bitmap_equals(result, tdata.base))
2038 fprintf_ln(stderr, "OK!");
2039 else
2040 die(_("mismatch in bitmap results"));
2042 bitmap_free(result);
2043 bitmap_free(tdata.base);
2044 bitmap_free(tdata.commits);
2045 bitmap_free(tdata.trees);
2046 bitmap_free(tdata.blobs);
2047 bitmap_free(tdata.tags);
2048 free_bitmap_index(bitmap_git);
2051 int test_bitmap_commits(struct repository *r)
2053 struct object_id oid;
2054 MAYBE_UNUSED void *value;
2055 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2057 if (!bitmap_git)
2058 die(_("failed to load bitmap indexes"));
2061 * As this function is only used to print bitmap selected
2062 * commits, we don't have to read the commit table.
2064 if (bitmap_git->table_lookup) {
2065 if (load_bitmap_entries_v1(bitmap_git) < 0)
2066 die(_("failed to load bitmap indexes"));
2069 kh_foreach(bitmap_git->bitmaps, oid, value, {
2070 printf_ln("%s", oid_to_hex(&oid));
2073 free_bitmap_index(bitmap_git);
2075 return 0;
2078 int test_bitmap_hashes(struct repository *r)
2080 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2081 struct object_id oid;
2082 uint32_t i, index_pos;
2084 if (!bitmap_git || !bitmap_git->hashes)
2085 goto cleanup;
2087 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2088 if (bitmap_is_midx(bitmap_git))
2089 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2090 else
2091 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2093 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2095 printf_ln("%s %"PRIu32"",
2096 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2099 cleanup:
2100 free_bitmap_index(bitmap_git);
2102 return 0;
2105 int rebuild_bitmap(const uint32_t *reposition,
2106 struct ewah_bitmap *source,
2107 struct bitmap *dest)
2109 uint32_t pos = 0;
2110 struct ewah_iterator it;
2111 eword_t word;
2113 ewah_iterator_init(&it, source);
2115 while (ewah_iterator_next(&word, &it)) {
2116 uint32_t offset, bit_pos;
2118 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2119 if ((word >> offset) == 0)
2120 break;
2122 offset += ewah_bit_ctz64(word >> offset);
2124 bit_pos = reposition[pos + offset];
2125 if (bit_pos > 0)
2126 bitmap_set(dest, bit_pos - 1);
2127 else /* can't reuse, we don't have the object */
2128 return -1;
2131 pos += BITS_IN_EWORD;
2133 return 0;
2136 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2137 struct packing_data *mapping)
2139 struct repository *r = the_repository;
2140 uint32_t i, num_objects;
2141 uint32_t *reposition;
2143 if (!bitmap_is_midx(bitmap_git))
2144 load_reverse_index(r, bitmap_git);
2145 else if (load_midx_revindex(bitmap_git->midx))
2146 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2147 "extension");
2149 num_objects = bitmap_num_objects(bitmap_git);
2150 CALLOC_ARRAY(reposition, num_objects);
2152 for (i = 0; i < num_objects; ++i) {
2153 struct object_id oid;
2154 struct object_entry *oe;
2155 uint32_t index_pos;
2157 if (bitmap_is_midx(bitmap_git))
2158 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2159 else
2160 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2161 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2162 oe = packlist_find(mapping, &oid);
2164 if (oe) {
2165 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2166 if (bitmap_git->hashes && !oe->hash)
2167 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2171 return reposition;
2174 void free_bitmap_index(struct bitmap_index *b)
2176 if (!b)
2177 return;
2179 if (b->map)
2180 munmap(b->map, b->map_size);
2181 ewah_pool_free(b->commits);
2182 ewah_pool_free(b->trees);
2183 ewah_pool_free(b->blobs);
2184 ewah_pool_free(b->tags);
2185 if (b->bitmaps) {
2186 struct stored_bitmap *sb;
2187 kh_foreach_value(b->bitmaps, sb, {
2188 ewah_pool_free(sb->root);
2189 free(sb);
2192 kh_destroy_oid_map(b->bitmaps);
2193 free(b->ext_index.objects);
2194 free(b->ext_index.hashes);
2195 kh_destroy_oid_pos(b->ext_index.positions);
2196 bitmap_free(b->result);
2197 bitmap_free(b->haves);
2198 if (bitmap_is_midx(b)) {
2200 * Multi-pack bitmaps need to have resources associated with
2201 * their on-disk reverse indexes unmapped so that stale .rev and
2202 * .bitmap files can be removed.
2204 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2205 * written in the same 'git multi-pack-index write --bitmap'
2206 * process. Close resources so they can be removed safely on
2207 * platforms like Windows.
2209 close_midx_revindex(b->midx);
2211 free(b);
2214 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2215 const struct object_id *oid)
2217 return bitmap_git &&
2218 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2221 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2222 enum object_type object_type)
2224 struct bitmap *result = bitmap_git->result;
2225 off_t total = 0;
2226 struct ewah_iterator it;
2227 eword_t filter;
2228 size_t i;
2230 init_type_iterator(&it, bitmap_git, object_type);
2231 for (i = 0; i < result->word_alloc &&
2232 ewah_iterator_next(&filter, &it); i++) {
2233 eword_t word = result->words[i] & filter;
2234 size_t base = (i * BITS_IN_EWORD);
2235 unsigned offset;
2237 if (!word)
2238 continue;
2240 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2241 if ((word >> offset) == 0)
2242 break;
2244 offset += ewah_bit_ctz64(word >> offset);
2246 if (bitmap_is_midx(bitmap_git)) {
2247 uint32_t pack_pos;
2248 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2249 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2251 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2252 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2254 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2255 struct object_id oid;
2256 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2258 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2259 oid_to_hex(&oid),
2260 pack->pack_name,
2261 (uintmax_t)offset);
2264 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2265 } else {
2266 size_t pos = base + offset;
2267 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2268 pack_pos_to_offset(bitmap_git->pack, pos);
2273 return total;
2276 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2278 struct bitmap *result = bitmap_git->result;
2279 struct eindex *eindex = &bitmap_git->ext_index;
2280 off_t total = 0;
2281 struct object_info oi = OBJECT_INFO_INIT;
2282 off_t object_size;
2283 size_t i;
2285 oi.disk_sizep = &object_size;
2287 for (i = 0; i < eindex->count; i++) {
2288 struct object *obj = eindex->objects[i];
2290 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2291 continue;
2293 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2294 die(_("unable to get disk usage of '%s'"),
2295 oid_to_hex(&obj->oid));
2297 total += object_size;
2299 return total;
2302 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2303 struct rev_info *revs)
2305 off_t total = 0;
2307 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2308 if (revs->tree_objects)
2309 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2310 if (revs->blob_objects)
2311 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2312 if (revs->tag_objects)
2313 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2315 total += get_disk_usage_for_extended(bitmap_git);
2317 return total;
2320 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2322 return !!bitmap_git->midx;
2325 const struct string_list *bitmap_preferred_tips(struct repository *r)
2327 const struct string_list *dest;
2329 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2330 return dest;
2331 return NULL;
2334 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2336 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2337 struct string_list_item *item;
2339 if (!preferred_tips)
2340 return 0;
2342 for_each_string_list_item(item, preferred_tips) {
2343 if (starts_with(refname, item->string))
2344 return 1;
2347 return 0;
2350 static int verify_bitmap_file(const char *name)
2352 struct stat st;
2353 unsigned char *data;
2354 int fd = git_open(name);
2355 int res = 0;
2357 /* It is OK to not have the file. */
2358 if (fd < 0 || fstat(fd, &st)) {
2359 if (fd >= 0)
2360 close(fd);
2361 return 0;
2364 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2365 close(fd);
2366 if (!hashfile_checksum_valid(data, st.st_size))
2367 res = error(_("bitmap file '%s' has invalid checksum"),
2368 name);
2370 munmap(data, st.st_size);
2371 return res;
2374 int verify_bitmap_files(struct repository *r)
2376 int res = 0;
2378 for (struct multi_pack_index *m = get_multi_pack_index(r);
2379 m; m = m->next) {
2380 char *midx_bitmap_name = midx_bitmap_filename(m);
2381 res |= verify_bitmap_file(midx_bitmap_name);
2382 free(midx_bitmap_name);
2385 for (struct packed_git *p = get_all_packs(r);
2386 p; p = p->next) {
2387 char *pack_bitmap_name = pack_bitmap_filename(p);
2388 res |= verify_bitmap_file(pack_bitmap_name);
2389 free(pack_bitmap_name);
2392 return res;