Merge branch 'rs/doc-ls-tree-hex-literal'
[git/debian.git] / pack-bitmap.c
blob894bff02c5cc11bb30794bf4a6e83a1575e36a5c
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 *fill_in_bitmap(struct bitmap_index *bitmap_git,
1047 struct rev_info *revs,
1048 struct bitmap *base,
1049 struct bitmap *seen)
1051 struct include_data incdata;
1052 struct bitmap_show_data show_data;
1054 if (!base)
1055 base = bitmap_new();
1057 incdata.bitmap_git = bitmap_git;
1058 incdata.base = base;
1059 incdata.seen = seen;
1061 revs->include_check = should_include;
1062 revs->include_check_obj = should_include_obj;
1063 revs->include_check_data = &incdata;
1065 if (prepare_revision_walk(revs))
1066 die(_("revision walk setup failed"));
1068 show_data.bitmap_git = bitmap_git;
1069 show_data.base = base;
1071 traverse_commit_list(revs, show_commit, show_object, &show_data);
1073 revs->include_check = NULL;
1074 revs->include_check_obj = NULL;
1075 revs->include_check_data = NULL;
1077 return base;
1080 struct bitmap_boundary_cb {
1081 struct bitmap_index *bitmap_git;
1082 struct bitmap *base;
1084 struct object_array boundary;
1087 static void show_boundary_commit(struct commit *commit, void *_data)
1089 struct bitmap_boundary_cb *data = _data;
1091 if (commit->object.flags & BOUNDARY)
1092 add_object_array(&commit->object, "", &data->boundary);
1094 if (commit->object.flags & UNINTERESTING) {
1095 if (bitmap_walk_contains(data->bitmap_git, data->base,
1096 &commit->object.oid))
1097 return;
1099 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1103 static void show_boundary_object(struct object *object,
1104 const char *name, void *data)
1106 BUG("should not be called");
1109 static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1110 struct rev_info *revs,
1111 struct object_list *roots)
1113 struct bitmap_boundary_cb cb;
1114 struct object_list *root;
1115 unsigned int i;
1116 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1117 int any_missing = 0;
1119 cb.bitmap_git = bitmap_git;
1120 cb.base = bitmap_new();
1121 object_array_init(&cb.boundary);
1123 revs->ignore_missing_links = 1;
1126 * OR in any existing reachability bitmaps among `roots` into
1127 * `cb.base`.
1129 for (root = roots; root; root = root->next) {
1130 struct object *object = root->item;
1131 if (object->type != OBJ_COMMIT ||
1132 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1133 continue;
1135 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1136 (struct commit *)object))
1137 continue;
1139 any_missing = 1;
1142 if (!any_missing)
1143 goto cleanup;
1145 tmp_blobs = revs->blob_objects;
1146 tmp_trees = revs->tree_objects;
1147 tmp_tags = revs->blob_objects;
1148 revs->blob_objects = 0;
1149 revs->tree_objects = 0;
1150 revs->tag_objects = 0;
1153 * We didn't have complete coverage of the roots. First setup a
1154 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1155 * between the tips and boundary, and (b) record the boundary.
1157 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1158 if (prepare_revision_walk(revs))
1159 die("revision walk setup failed");
1160 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1162 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1163 revs->boundary = 1;
1164 traverse_commit_list_filtered(revs,
1165 show_boundary_commit,
1166 show_boundary_object,
1167 &cb, NULL);
1168 revs->boundary = 0;
1169 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1171 revs->blob_objects = tmp_blobs;
1172 revs->tree_objects = tmp_trees;
1173 revs->tag_objects = tmp_tags;
1175 reset_revision_walk();
1176 clear_object_flags(UNINTERESTING);
1179 * Then add the boundary commit(s) as fill-in traversal tips.
1181 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1182 for (i = 0; i < cb.boundary.nr; i++) {
1183 struct object *obj = cb.boundary.objects[i].item;
1184 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1185 obj->flags |= SEEN;
1186 else
1187 add_pending_object(revs, obj, "");
1189 if (revs->pending.nr)
1190 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1191 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1193 cleanup:
1194 object_array_clear(&cb.boundary);
1195 revs->ignore_missing_links = 0;
1197 return cb.base;
1200 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1201 struct rev_info *revs,
1202 struct object_list *roots,
1203 struct bitmap *seen)
1205 struct bitmap *base = NULL;
1206 int needs_walk = 0;
1208 struct object_list *not_mapped = NULL;
1211 * Go through all the roots for the walk. The ones that have bitmaps
1212 * on the bitmap index will be `or`ed together to form an initial
1213 * global reachability analysis.
1215 * The ones without bitmaps in the index will be stored in the
1216 * `not_mapped_list` for further processing.
1218 while (roots) {
1219 struct object *object = roots->item;
1220 roots = roots->next;
1222 if (object->type == OBJ_COMMIT &&
1223 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1224 object->flags |= SEEN;
1225 continue;
1228 object_list_insert(object, &not_mapped);
1232 * Best case scenario: We found bitmaps for all the roots,
1233 * so the resulting `or` bitmap has the full reachability analysis
1235 if (!not_mapped)
1236 return base;
1238 roots = not_mapped;
1241 * Let's iterate through all the roots that don't have bitmaps to
1242 * check if we can determine them to be reachable from the existing
1243 * global bitmap.
1245 * If we cannot find them in the existing global bitmap, we'll need
1246 * to push them to an actual walk and run it until we can confirm
1247 * they are reachable
1249 while (roots) {
1250 struct object *object = roots->item;
1251 int pos;
1253 roots = roots->next;
1254 pos = bitmap_position(bitmap_git, &object->oid);
1256 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1257 object->flags &= ~UNINTERESTING;
1258 add_pending_object(revs, object, "");
1259 needs_walk = 1;
1260 } else {
1261 object->flags |= SEEN;
1265 if (needs_walk) {
1267 * This fill-in traversal may walk over some objects
1268 * again, since we have already traversed in order to
1269 * find the boundary.
1271 * But this extra walk should be extremely cheap, since
1272 * all commit objects are loaded into memory, and
1273 * because we skip walking to parents that are
1274 * UNINTERESTING, since it will be marked in the haves
1275 * bitmap already (or it has an on-disk bitmap, since
1276 * OR-ing it in covers all of its ancestors).
1278 base = fill_in_bitmap(bitmap_git, revs, base, seen);
1281 return base;
1284 static void show_extended_objects(struct bitmap_index *bitmap_git,
1285 struct rev_info *revs,
1286 show_reachable_fn show_reach)
1288 struct bitmap *objects = bitmap_git->result;
1289 struct eindex *eindex = &bitmap_git->ext_index;
1290 uint32_t i;
1292 for (i = 0; i < eindex->count; ++i) {
1293 struct object *obj;
1295 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1296 continue;
1298 obj = eindex->objects[i];
1299 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1300 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1301 (obj->type == OBJ_TAG && !revs->tag_objects))
1302 continue;
1304 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1308 static void init_type_iterator(struct ewah_iterator *it,
1309 struct bitmap_index *bitmap_git,
1310 enum object_type type)
1312 switch (type) {
1313 case OBJ_COMMIT:
1314 ewah_iterator_init(it, bitmap_git->commits);
1315 break;
1317 case OBJ_TREE:
1318 ewah_iterator_init(it, bitmap_git->trees);
1319 break;
1321 case OBJ_BLOB:
1322 ewah_iterator_init(it, bitmap_git->blobs);
1323 break;
1325 case OBJ_TAG:
1326 ewah_iterator_init(it, bitmap_git->tags);
1327 break;
1329 default:
1330 BUG("object type %d not stored by bitmap type index", type);
1331 break;
1335 static void show_objects_for_type(
1336 struct bitmap_index *bitmap_git,
1337 enum object_type object_type,
1338 show_reachable_fn show_reach)
1340 size_t i = 0;
1341 uint32_t offset;
1343 struct ewah_iterator it;
1344 eword_t filter;
1346 struct bitmap *objects = bitmap_git->result;
1348 init_type_iterator(&it, bitmap_git, object_type);
1350 for (i = 0; i < objects->word_alloc &&
1351 ewah_iterator_next(&filter, &it); i++) {
1352 eword_t word = objects->words[i] & filter;
1353 size_t pos = (i * BITS_IN_EWORD);
1355 if (!word)
1356 continue;
1358 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1359 struct packed_git *pack;
1360 struct object_id oid;
1361 uint32_t hash = 0, index_pos;
1362 off_t ofs;
1364 if ((word >> offset) == 0)
1365 break;
1367 offset += ewah_bit_ctz64(word >> offset);
1369 if (bitmap_is_midx(bitmap_git)) {
1370 struct multi_pack_index *m = bitmap_git->midx;
1371 uint32_t pack_id;
1373 index_pos = pack_pos_to_midx(m, pos + offset);
1374 ofs = nth_midxed_offset(m, index_pos);
1375 nth_midxed_object_oid(&oid, m, index_pos);
1377 pack_id = nth_midxed_pack_int_id(m, index_pos);
1378 pack = bitmap_git->midx->packs[pack_id];
1379 } else {
1380 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1381 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1382 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1384 pack = bitmap_git->pack;
1387 if (bitmap_git->hashes)
1388 hash = get_be32(bitmap_git->hashes + index_pos);
1390 show_reach(&oid, object_type, 0, hash, pack, ofs);
1395 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1396 struct object_list *roots)
1398 while (roots) {
1399 struct object *object = roots->item;
1400 roots = roots->next;
1402 if (bitmap_is_midx(bitmap_git)) {
1403 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1404 return 1;
1405 } else {
1406 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1407 return 1;
1411 return 0;
1414 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1415 struct object_list *tip_objects,
1416 enum object_type type)
1418 struct bitmap *result = bitmap_new();
1419 struct object_list *p;
1421 for (p = tip_objects; p; p = p->next) {
1422 int pos;
1424 if (p->item->type != type)
1425 continue;
1427 pos = bitmap_position(bitmap_git, &p->item->oid);
1428 if (pos < 0)
1429 continue;
1431 bitmap_set(result, pos);
1434 return result;
1437 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1438 struct object_list *tip_objects,
1439 struct bitmap *to_filter,
1440 enum object_type type)
1442 struct eindex *eindex = &bitmap_git->ext_index;
1443 struct bitmap *tips;
1444 struct ewah_iterator it;
1445 eword_t mask;
1446 uint32_t i;
1449 * The non-bitmap version of this filter never removes
1450 * objects which the other side specifically asked for,
1451 * so we must match that behavior.
1453 tips = find_tip_objects(bitmap_git, tip_objects, type);
1456 * We can use the type-level bitmap for 'type' to work in whole
1457 * words for the objects that are actually in the bitmapped
1458 * packfile.
1460 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1461 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1462 i++) {
1463 if (i < tips->word_alloc)
1464 mask &= ~tips->words[i];
1465 to_filter->words[i] &= ~mask;
1469 * Clear any objects that weren't in the packfile (and so would
1470 * not have been caught by the loop above. We'll have to check
1471 * them individually.
1473 for (i = 0; i < eindex->count; i++) {
1474 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1475 if (eindex->objects[i]->type == type &&
1476 bitmap_get(to_filter, pos) &&
1477 !bitmap_get(tips, pos))
1478 bitmap_unset(to_filter, pos);
1481 bitmap_free(tips);
1484 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1485 struct object_list *tip_objects,
1486 struct bitmap *to_filter)
1488 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1489 OBJ_BLOB);
1492 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1493 uint32_t pos)
1495 unsigned long size;
1496 struct object_info oi = OBJECT_INFO_INIT;
1498 oi.sizep = &size;
1500 if (pos < bitmap_num_objects(bitmap_git)) {
1501 struct packed_git *pack;
1502 off_t ofs;
1504 if (bitmap_is_midx(bitmap_git)) {
1505 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1506 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1508 pack = bitmap_git->midx->packs[pack_id];
1509 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1510 } else {
1511 pack = bitmap_git->pack;
1512 ofs = pack_pos_to_offset(pack, pos);
1515 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1516 struct object_id oid;
1517 nth_bitmap_object_oid(bitmap_git, &oid,
1518 pack_pos_to_index(pack, pos));
1519 die(_("unable to get size of %s"), oid_to_hex(&oid));
1521 } else {
1522 struct eindex *eindex = &bitmap_git->ext_index;
1523 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1524 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1525 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1528 return size;
1531 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1532 struct object_list *tip_objects,
1533 struct bitmap *to_filter,
1534 unsigned long limit)
1536 struct eindex *eindex = &bitmap_git->ext_index;
1537 struct bitmap *tips;
1538 struct ewah_iterator it;
1539 eword_t mask;
1540 uint32_t i;
1542 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1544 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1545 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1546 i++) {
1547 eword_t word = to_filter->words[i] & mask;
1548 unsigned offset;
1550 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1551 uint32_t pos;
1553 if ((word >> offset) == 0)
1554 break;
1555 offset += ewah_bit_ctz64(word >> offset);
1556 pos = i * BITS_IN_EWORD + offset;
1558 if (!bitmap_get(tips, pos) &&
1559 get_size_by_pos(bitmap_git, pos) >= limit)
1560 bitmap_unset(to_filter, pos);
1564 for (i = 0; i < eindex->count; i++) {
1565 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1566 if (eindex->objects[i]->type == OBJ_BLOB &&
1567 bitmap_get(to_filter, pos) &&
1568 !bitmap_get(tips, pos) &&
1569 get_size_by_pos(bitmap_git, pos) >= limit)
1570 bitmap_unset(to_filter, pos);
1573 bitmap_free(tips);
1576 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1577 struct object_list *tip_objects,
1578 struct bitmap *to_filter,
1579 unsigned long limit)
1581 if (limit)
1582 BUG("filter_bitmap_tree_depth given non-zero limit");
1584 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1585 OBJ_TREE);
1586 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1587 OBJ_BLOB);
1590 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1591 struct object_list *tip_objects,
1592 struct bitmap *to_filter,
1593 enum object_type object_type)
1595 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1596 BUG("filter_bitmap_object_type given invalid object");
1598 if (object_type != OBJ_TAG)
1599 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1600 if (object_type != OBJ_COMMIT)
1601 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1602 if (object_type != OBJ_TREE)
1603 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1604 if (object_type != OBJ_BLOB)
1605 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1608 static int filter_bitmap(struct bitmap_index *bitmap_git,
1609 struct object_list *tip_objects,
1610 struct bitmap *to_filter,
1611 struct list_objects_filter_options *filter)
1613 if (!filter || filter->choice == LOFC_DISABLED)
1614 return 0;
1616 if (filter->choice == LOFC_BLOB_NONE) {
1617 if (bitmap_git)
1618 filter_bitmap_blob_none(bitmap_git, tip_objects,
1619 to_filter);
1620 return 0;
1623 if (filter->choice == LOFC_BLOB_LIMIT) {
1624 if (bitmap_git)
1625 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1626 to_filter,
1627 filter->blob_limit_value);
1628 return 0;
1631 if (filter->choice == LOFC_TREE_DEPTH &&
1632 filter->tree_exclude_depth == 0) {
1633 if (bitmap_git)
1634 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1635 to_filter,
1636 filter->tree_exclude_depth);
1637 return 0;
1640 if (filter->choice == LOFC_OBJECT_TYPE) {
1641 if (bitmap_git)
1642 filter_bitmap_object_type(bitmap_git, tip_objects,
1643 to_filter,
1644 filter->object_type);
1645 return 0;
1648 if (filter->choice == LOFC_COMBINE) {
1649 int i;
1650 for (i = 0; i < filter->sub_nr; i++) {
1651 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1652 &filter->sub[i]) < 0)
1653 return -1;
1655 return 0;
1658 /* filter choice not handled */
1659 return -1;
1662 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1664 return !filter_bitmap(NULL, NULL, NULL, filter);
1667 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1668 int filter_provided_objects)
1670 unsigned int i;
1671 int use_boundary_traversal;
1673 struct object_list *wants = NULL;
1674 struct object_list *haves = NULL;
1676 struct bitmap *wants_bitmap = NULL;
1677 struct bitmap *haves_bitmap = NULL;
1679 struct bitmap_index *bitmap_git;
1682 * We can't do pathspec limiting with bitmaps, because we don't know
1683 * which commits are associated with which object changes (let alone
1684 * even which objects are associated with which paths).
1686 if (revs->prune)
1687 return NULL;
1689 if (!can_filter_bitmap(&revs->filter))
1690 return NULL;
1692 /* try to open a bitmapped pack, but don't parse it yet
1693 * because we may not need to use it */
1694 CALLOC_ARRAY(bitmap_git, 1);
1695 if (open_bitmap(revs->repo, bitmap_git) < 0)
1696 goto cleanup;
1698 for (i = 0; i < revs->pending.nr; ++i) {
1699 struct object *object = revs->pending.objects[i].item;
1701 if (object->type == OBJ_NONE)
1702 parse_object_or_die(&object->oid, NULL);
1704 while (object->type == OBJ_TAG) {
1705 struct tag *tag = (struct tag *) object;
1707 if (object->flags & UNINTERESTING)
1708 object_list_insert(object, &haves);
1709 else
1710 object_list_insert(object, &wants);
1712 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1713 object->flags |= (tag->object.flags & UNINTERESTING);
1716 if (object->flags & UNINTERESTING)
1717 object_list_insert(object, &haves);
1718 else
1719 object_list_insert(object, &wants);
1722 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1723 if (use_boundary_traversal < 0) {
1724 prepare_repo_settings(revs->repo);
1725 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1728 if (!use_boundary_traversal) {
1730 * if we have a HAVES list, but none of those haves is contained
1731 * in the packfile that has a bitmap, we don't have anything to
1732 * optimize here
1734 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1735 goto cleanup;
1738 /* if we don't want anything, we're done here */
1739 if (!wants)
1740 goto cleanup;
1743 * now we're going to use bitmaps, so load the actual bitmap entries
1744 * from disk. this is the point of no return; after this the rev_list
1745 * becomes invalidated and we must perform the revwalk through bitmaps
1747 if (load_bitmap(revs->repo, bitmap_git) < 0)
1748 goto cleanup;
1750 if (!use_boundary_traversal)
1751 object_array_clear(&revs->pending);
1753 if (haves) {
1754 if (use_boundary_traversal) {
1755 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1756 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1757 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1758 } else {
1759 trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1760 revs->ignore_missing_links = 1;
1761 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1762 reset_revision_walk();
1763 revs->ignore_missing_links = 0;
1764 trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1767 if (!haves_bitmap)
1768 BUG("failed to perform bitmap walk");
1771 if (use_boundary_traversal) {
1772 object_array_clear(&revs->pending);
1773 reset_revision_walk();
1776 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1778 if (!wants_bitmap)
1779 BUG("failed to perform bitmap walk");
1781 if (haves_bitmap)
1782 bitmap_and_not(wants_bitmap, haves_bitmap);
1784 filter_bitmap(bitmap_git,
1785 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1786 wants_bitmap,
1787 &revs->filter);
1789 bitmap_git->result = wants_bitmap;
1790 bitmap_git->haves = haves_bitmap;
1792 object_list_free(&wants);
1793 object_list_free(&haves);
1795 return bitmap_git;
1797 cleanup:
1798 free_bitmap_index(bitmap_git);
1799 object_list_free(&wants);
1800 object_list_free(&haves);
1801 return NULL;
1805 * -1 means "stop trying further objects"; 0 means we may or may not have
1806 * reused, but you can keep feeding bits.
1808 static int try_partial_reuse(struct packed_git *pack,
1809 size_t pos,
1810 struct bitmap *reuse,
1811 struct pack_window **w_curs)
1813 off_t offset, delta_obj_offset;
1814 enum object_type type;
1815 unsigned long size;
1818 * try_partial_reuse() is called either on (a) objects in the
1819 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1820 * objects in the preferred pack of a multi-pack bitmap.
1821 * Importantly, the latter can pretend as if only a single pack
1822 * exists because:
1824 * - The first pack->num_objects bits of a MIDX bitmap are
1825 * reserved for the preferred pack, and
1827 * - Ties due to duplicate objects are always resolved in
1828 * favor of the preferred pack.
1830 * Therefore we do not need to ever ask the MIDX for its copy of
1831 * an object by OID, since it will always select it from the
1832 * preferred pack. Likewise, the selected copy of the base
1833 * object for any deltas will reside in the same pack.
1835 * This means that we can reuse pos when looking up the bit in
1836 * the reuse bitmap, too, since bits corresponding to the
1837 * preferred pack precede all bits from other packs.
1840 if (pos >= pack->num_objects)
1841 return -1; /* not actually in the pack or MIDX preferred pack */
1843 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1844 type = unpack_object_header(pack, w_curs, &offset, &size);
1845 if (type < 0)
1846 return -1; /* broken packfile, punt */
1848 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1849 off_t base_offset;
1850 uint32_t base_pos;
1853 * Find the position of the base object so we can look it up
1854 * in our bitmaps. If we can't come up with an offset, or if
1855 * that offset is not in the revidx, the pack is corrupt.
1856 * There's nothing we can do, so just punt on this object,
1857 * and the normal slow path will complain about it in
1858 * more detail.
1860 base_offset = get_delta_base(pack, w_curs, &offset, type,
1861 delta_obj_offset);
1862 if (!base_offset)
1863 return 0;
1864 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1865 return 0;
1868 * We assume delta dependencies always point backwards. This
1869 * lets us do a single pass, and is basically always true
1870 * due to the way OFS_DELTAs work. You would not typically
1871 * find REF_DELTA in a bitmapped pack, since we only bitmap
1872 * packs we write fresh, and OFS_DELTA is the default). But
1873 * let's double check to make sure the pack wasn't written with
1874 * odd parameters.
1876 if (base_pos >= pos)
1877 return 0;
1880 * And finally, if we're not sending the base as part of our
1881 * reuse chunk, then don't send this object either. The base
1882 * would come after us, along with other objects not
1883 * necessarily in the pack, which means we'd need to convert
1884 * to REF_DELTA on the fly. Better to just let the normal
1885 * object_entry code path handle it.
1887 if (!bitmap_get(reuse, base_pos))
1888 return 0;
1892 * If we got here, then the object is OK to reuse. Mark it.
1894 bitmap_set(reuse, pos);
1895 return 0;
1898 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1900 struct multi_pack_index *m = bitmap_git->midx;
1901 if (!m)
1902 BUG("midx_preferred_pack: requires non-empty MIDX");
1903 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1906 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1907 struct packed_git **packfile_out,
1908 uint32_t *entries,
1909 struct bitmap **reuse_out)
1911 struct repository *r = the_repository;
1912 struct packed_git *pack;
1913 struct bitmap *result = bitmap_git->result;
1914 struct bitmap *reuse;
1915 struct pack_window *w_curs = NULL;
1916 size_t i = 0;
1917 uint32_t offset;
1918 uint32_t objects_nr;
1920 assert(result);
1922 load_reverse_index(r, bitmap_git);
1924 if (bitmap_is_midx(bitmap_git))
1925 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1926 else
1927 pack = bitmap_git->pack;
1928 objects_nr = pack->num_objects;
1930 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1931 i++;
1934 * Don't mark objects not in the packfile or preferred pack. This bitmap
1935 * marks objects eligible for reuse, but the pack-reuse code only
1936 * understands how to reuse a single pack. Since the preferred pack is
1937 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1938 * we use it instead of another pack. In single-pack bitmaps, the choice
1939 * is made for us.
1941 if (i > objects_nr / BITS_IN_EWORD)
1942 i = objects_nr / BITS_IN_EWORD;
1944 reuse = bitmap_word_alloc(i);
1945 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1947 for (; i < result->word_alloc; ++i) {
1948 eword_t word = result->words[i];
1949 size_t pos = (i * BITS_IN_EWORD);
1951 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1952 if ((word >> offset) == 0)
1953 break;
1955 offset += ewah_bit_ctz64(word >> offset);
1956 if (try_partial_reuse(pack, pos + offset,
1957 reuse, &w_curs) < 0) {
1959 * try_partial_reuse indicated we couldn't reuse
1960 * any bits, so there is no point in trying more
1961 * bits in the current word, or any other words
1962 * in result.
1964 * Jump out of both loops to avoid future
1965 * unnecessary calls to try_partial_reuse.
1967 goto done;
1972 done:
1973 unuse_pack(&w_curs);
1975 *entries = bitmap_popcount(reuse);
1976 if (!*entries) {
1977 bitmap_free(reuse);
1978 return -1;
1982 * Drop any reused objects from the result, since they will not
1983 * need to be handled separately.
1985 bitmap_and_not(result, reuse);
1986 *packfile_out = pack;
1987 *reuse_out = reuse;
1988 return 0;
1991 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1992 struct bitmap *bitmap, const struct object_id *oid)
1994 int idx;
1996 if (!bitmap)
1997 return 0;
1999 idx = bitmap_position(bitmap_git, oid);
2000 return idx >= 0 && bitmap_get(bitmap, idx);
2003 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2004 struct rev_info *revs,
2005 show_reachable_fn show_reachable)
2007 assert(bitmap_git->result);
2009 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2010 if (revs->tree_objects)
2011 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2012 if (revs->blob_objects)
2013 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2014 if (revs->tag_objects)
2015 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2017 show_extended_objects(bitmap_git, revs, show_reachable);
2020 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2021 enum object_type type)
2023 struct bitmap *objects = bitmap_git->result;
2024 struct eindex *eindex = &bitmap_git->ext_index;
2026 uint32_t i = 0, count = 0;
2027 struct ewah_iterator it;
2028 eword_t filter;
2030 init_type_iterator(&it, bitmap_git, type);
2032 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2033 eword_t word = objects->words[i++] & filter;
2034 count += ewah_bit_popcount64(word);
2037 for (i = 0; i < eindex->count; ++i) {
2038 if (eindex->objects[i]->type == type &&
2039 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
2040 count++;
2043 return count;
2046 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2047 uint32_t *commits, uint32_t *trees,
2048 uint32_t *blobs, uint32_t *tags)
2050 assert(bitmap_git->result);
2052 if (commits)
2053 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2055 if (trees)
2056 *trees = count_object_type(bitmap_git, OBJ_TREE);
2058 if (blobs)
2059 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2061 if (tags)
2062 *tags = count_object_type(bitmap_git, OBJ_TAG);
2065 struct bitmap_test_data {
2066 struct bitmap_index *bitmap_git;
2067 struct bitmap *base;
2068 struct bitmap *commits;
2069 struct bitmap *trees;
2070 struct bitmap *blobs;
2071 struct bitmap *tags;
2072 struct progress *prg;
2073 size_t seen;
2076 static void test_bitmap_type(struct bitmap_test_data *tdata,
2077 struct object *obj, int pos)
2079 enum object_type bitmap_type = OBJ_NONE;
2080 int bitmaps_nr = 0;
2082 if (bitmap_get(tdata->commits, pos)) {
2083 bitmap_type = OBJ_COMMIT;
2084 bitmaps_nr++;
2086 if (bitmap_get(tdata->trees, pos)) {
2087 bitmap_type = OBJ_TREE;
2088 bitmaps_nr++;
2090 if (bitmap_get(tdata->blobs, pos)) {
2091 bitmap_type = OBJ_BLOB;
2092 bitmaps_nr++;
2094 if (bitmap_get(tdata->tags, pos)) {
2095 bitmap_type = OBJ_TAG;
2096 bitmaps_nr++;
2099 if (bitmap_type == OBJ_NONE)
2100 die(_("object '%s' not found in type bitmaps"),
2101 oid_to_hex(&obj->oid));
2103 if (bitmaps_nr > 1)
2104 die(_("object '%s' does not have a unique type"),
2105 oid_to_hex(&obj->oid));
2107 if (bitmap_type != obj->type)
2108 die(_("object '%s': real type '%s', expected: '%s'"),
2109 oid_to_hex(&obj->oid),
2110 type_name(obj->type),
2111 type_name(bitmap_type));
2114 static void test_show_object(struct object *object,
2115 const char *name UNUSED,
2116 void *data)
2118 struct bitmap_test_data *tdata = data;
2119 int bitmap_pos;
2121 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2122 if (bitmap_pos < 0)
2123 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2124 test_bitmap_type(tdata, object, bitmap_pos);
2126 bitmap_set(tdata->base, bitmap_pos);
2127 display_progress(tdata->prg, ++tdata->seen);
2130 static void test_show_commit(struct commit *commit, void *data)
2132 struct bitmap_test_data *tdata = data;
2133 int bitmap_pos;
2135 bitmap_pos = bitmap_position(tdata->bitmap_git,
2136 &commit->object.oid);
2137 if (bitmap_pos < 0)
2138 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2139 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2141 bitmap_set(tdata->base, bitmap_pos);
2142 display_progress(tdata->prg, ++tdata->seen);
2145 void test_bitmap_walk(struct rev_info *revs)
2147 struct object *root;
2148 struct bitmap *result = NULL;
2149 size_t result_popcnt;
2150 struct bitmap_test_data tdata;
2151 struct bitmap_index *bitmap_git;
2152 struct ewah_bitmap *bm;
2154 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2155 die(_("failed to load bitmap indexes"));
2157 if (revs->pending.nr != 1)
2158 die(_("you must specify exactly one commit to test"));
2160 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2161 bitmap_git->version,
2162 bitmap_git->entry_count,
2163 bitmap_git->table_lookup ? "" : " loaded");
2165 root = revs->pending.objects[0].item;
2166 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2168 if (bm) {
2169 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2170 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2172 result = ewah_to_bitmap(bm);
2175 if (!result)
2176 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2178 revs->tag_objects = 1;
2179 revs->tree_objects = 1;
2180 revs->blob_objects = 1;
2182 result_popcnt = bitmap_popcount(result);
2184 if (prepare_revision_walk(revs))
2185 die(_("revision walk setup failed"));
2187 tdata.bitmap_git = bitmap_git;
2188 tdata.base = bitmap_new();
2189 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2190 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2191 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2192 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2193 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2194 tdata.seen = 0;
2196 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2198 stop_progress(&tdata.prg);
2200 if (bitmap_equals(result, tdata.base))
2201 fprintf_ln(stderr, "OK!");
2202 else
2203 die(_("mismatch in bitmap results"));
2205 bitmap_free(result);
2206 bitmap_free(tdata.base);
2207 bitmap_free(tdata.commits);
2208 bitmap_free(tdata.trees);
2209 bitmap_free(tdata.blobs);
2210 bitmap_free(tdata.tags);
2211 free_bitmap_index(bitmap_git);
2214 int test_bitmap_commits(struct repository *r)
2216 struct object_id oid;
2217 MAYBE_UNUSED void *value;
2218 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2220 if (!bitmap_git)
2221 die(_("failed to load bitmap indexes"));
2224 * As this function is only used to print bitmap selected
2225 * commits, we don't have to read the commit table.
2227 if (bitmap_git->table_lookup) {
2228 if (load_bitmap_entries_v1(bitmap_git) < 0)
2229 die(_("failed to load bitmap indexes"));
2232 kh_foreach(bitmap_git->bitmaps, oid, value, {
2233 printf_ln("%s", oid_to_hex(&oid));
2236 free_bitmap_index(bitmap_git);
2238 return 0;
2241 int test_bitmap_hashes(struct repository *r)
2243 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2244 struct object_id oid;
2245 uint32_t i, index_pos;
2247 if (!bitmap_git || !bitmap_git->hashes)
2248 goto cleanup;
2250 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2251 if (bitmap_is_midx(bitmap_git))
2252 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2253 else
2254 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2256 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2258 printf_ln("%s %"PRIu32"",
2259 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2262 cleanup:
2263 free_bitmap_index(bitmap_git);
2265 return 0;
2268 int rebuild_bitmap(const uint32_t *reposition,
2269 struct ewah_bitmap *source,
2270 struct bitmap *dest)
2272 uint32_t pos = 0;
2273 struct ewah_iterator it;
2274 eword_t word;
2276 ewah_iterator_init(&it, source);
2278 while (ewah_iterator_next(&word, &it)) {
2279 uint32_t offset, bit_pos;
2281 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2282 if ((word >> offset) == 0)
2283 break;
2285 offset += ewah_bit_ctz64(word >> offset);
2287 bit_pos = reposition[pos + offset];
2288 if (bit_pos > 0)
2289 bitmap_set(dest, bit_pos - 1);
2290 else /* can't reuse, we don't have the object */
2291 return -1;
2294 pos += BITS_IN_EWORD;
2296 return 0;
2299 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2300 struct packing_data *mapping)
2302 struct repository *r = the_repository;
2303 uint32_t i, num_objects;
2304 uint32_t *reposition;
2306 if (!bitmap_is_midx(bitmap_git))
2307 load_reverse_index(r, bitmap_git);
2308 else if (load_midx_revindex(bitmap_git->midx))
2309 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2310 "extension");
2312 num_objects = bitmap_num_objects(bitmap_git);
2313 CALLOC_ARRAY(reposition, num_objects);
2315 for (i = 0; i < num_objects; ++i) {
2316 struct object_id oid;
2317 struct object_entry *oe;
2318 uint32_t index_pos;
2320 if (bitmap_is_midx(bitmap_git))
2321 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2322 else
2323 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2324 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2325 oe = packlist_find(mapping, &oid);
2327 if (oe) {
2328 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2329 if (bitmap_git->hashes && !oe->hash)
2330 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2334 return reposition;
2337 void free_bitmap_index(struct bitmap_index *b)
2339 if (!b)
2340 return;
2342 if (b->map)
2343 munmap(b->map, b->map_size);
2344 ewah_pool_free(b->commits);
2345 ewah_pool_free(b->trees);
2346 ewah_pool_free(b->blobs);
2347 ewah_pool_free(b->tags);
2348 if (b->bitmaps) {
2349 struct stored_bitmap *sb;
2350 kh_foreach_value(b->bitmaps, sb, {
2351 ewah_pool_free(sb->root);
2352 free(sb);
2355 kh_destroy_oid_map(b->bitmaps);
2356 free(b->ext_index.objects);
2357 free(b->ext_index.hashes);
2358 kh_destroy_oid_pos(b->ext_index.positions);
2359 bitmap_free(b->result);
2360 bitmap_free(b->haves);
2361 if (bitmap_is_midx(b)) {
2363 * Multi-pack bitmaps need to have resources associated with
2364 * their on-disk reverse indexes unmapped so that stale .rev and
2365 * .bitmap files can be removed.
2367 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2368 * written in the same 'git multi-pack-index write --bitmap'
2369 * process. Close resources so they can be removed safely on
2370 * platforms like Windows.
2372 close_midx_revindex(b->midx);
2374 free(b);
2377 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2378 const struct object_id *oid)
2380 return bitmap_git &&
2381 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2384 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2385 enum object_type object_type)
2387 struct bitmap *result = bitmap_git->result;
2388 off_t total = 0;
2389 struct ewah_iterator it;
2390 eword_t filter;
2391 size_t i;
2393 init_type_iterator(&it, bitmap_git, object_type);
2394 for (i = 0; i < result->word_alloc &&
2395 ewah_iterator_next(&filter, &it); i++) {
2396 eword_t word = result->words[i] & filter;
2397 size_t base = (i * BITS_IN_EWORD);
2398 unsigned offset;
2400 if (!word)
2401 continue;
2403 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2404 if ((word >> offset) == 0)
2405 break;
2407 offset += ewah_bit_ctz64(word >> offset);
2409 if (bitmap_is_midx(bitmap_git)) {
2410 uint32_t pack_pos;
2411 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2412 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2414 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2415 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2417 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2418 struct object_id oid;
2419 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2421 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2422 oid_to_hex(&oid),
2423 pack->pack_name,
2424 (uintmax_t)offset);
2427 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2428 } else {
2429 size_t pos = base + offset;
2430 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2431 pack_pos_to_offset(bitmap_git->pack, pos);
2436 return total;
2439 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2441 struct bitmap *result = bitmap_git->result;
2442 struct eindex *eindex = &bitmap_git->ext_index;
2443 off_t total = 0;
2444 struct object_info oi = OBJECT_INFO_INIT;
2445 off_t object_size;
2446 size_t i;
2448 oi.disk_sizep = &object_size;
2450 for (i = 0; i < eindex->count; i++) {
2451 struct object *obj = eindex->objects[i];
2453 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2454 continue;
2456 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2457 die(_("unable to get disk usage of '%s'"),
2458 oid_to_hex(&obj->oid));
2460 total += object_size;
2462 return total;
2465 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2466 struct rev_info *revs)
2468 off_t total = 0;
2470 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2471 if (revs->tree_objects)
2472 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2473 if (revs->blob_objects)
2474 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2475 if (revs->tag_objects)
2476 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2478 total += get_disk_usage_for_extended(bitmap_git);
2480 return total;
2483 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2485 return !!bitmap_git->midx;
2488 const struct string_list *bitmap_preferred_tips(struct repository *r)
2490 const struct string_list *dest;
2492 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2493 return dest;
2494 return NULL;
2497 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2499 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2500 struct string_list_item *item;
2502 if (!preferred_tips)
2503 return 0;
2505 for_each_string_list_item(item, preferred_tips) {
2506 if (starts_with(refname, item->string))
2507 return 1;
2510 return 0;
2513 static int verify_bitmap_file(const char *name)
2515 struct stat st;
2516 unsigned char *data;
2517 int fd = git_open(name);
2518 int res = 0;
2520 /* It is OK to not have the file. */
2521 if (fd < 0 || fstat(fd, &st)) {
2522 if (fd >= 0)
2523 close(fd);
2524 return 0;
2527 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2528 close(fd);
2529 if (!hashfile_checksum_valid(data, st.st_size))
2530 res = error(_("bitmap file '%s' has invalid checksum"),
2531 name);
2533 munmap(data, st.st_size);
2534 return res;
2537 int verify_bitmap_files(struct repository *r)
2539 int res = 0;
2541 for (struct multi_pack_index *m = get_multi_pack_index(r);
2542 m; m = m->next) {
2543 char *midx_bitmap_name = midx_bitmap_filename(m);
2544 res |= verify_bitmap_file(midx_bitmap_name);
2545 free(midx_bitmap_name);
2548 for (struct packed_git *p = get_all_packs(r);
2549 p; p = p->next) {
2550 char *pack_bitmap_name = pack_bitmap_filename(p);
2551 res |= verify_bitmap_file(pack_bitmap_name);
2552 free(pack_bitmap_name);
2555 return res;