Merge branch 'jc/tree-walk-drop-base-offset'
[alt-git.git] / pack-bitmap.c
blob6afc03d1e4c39e53c1c48deb51c16d65667979e8
1 #include "git-compat-util.h"
2 #include "commit.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "strbuf.h"
6 #include "tag.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "progress.h"
10 #include "list-objects.h"
11 #include "pack.h"
12 #include "pack-bitmap.h"
13 #include "pack-revindex.h"
14 #include "pack-objects.h"
15 #include "packfile.h"
16 #include "repository.h"
17 #include "trace2.h"
18 #include "object-file.h"
19 #include "object-store-ll.h"
20 #include "list-objects-filter-options.h"
21 #include "midx.h"
22 #include "config.h"
25 * An entry on the bitmap index, representing the bitmap for a given
26 * commit.
28 struct stored_bitmap {
29 struct object_id oid;
30 struct ewah_bitmap *root;
31 struct stored_bitmap *xor;
32 int flags;
36 * The active bitmap index for a repository. By design, repositories only have
37 * a single bitmap index available (the index for the biggest packfile in
38 * the repository), since bitmap indexes need full closure.
40 * If there is more than one bitmap index available (e.g. because of alternates),
41 * the active bitmap index is the largest one.
43 struct bitmap_index {
45 * The pack or multi-pack index (MIDX) that this bitmap index belongs
46 * to.
48 * Exactly one of these must be non-NULL; this specifies the object
49 * order used to interpret this bitmap.
51 struct packed_git *pack;
52 struct multi_pack_index *midx;
55 * Mark the first `reuse_objects` in the packfile as reused:
56 * they will be sent as-is without using them for repacking
57 * calculations
59 uint32_t reuse_objects;
61 /* mmapped buffer of the whole bitmap index */
62 unsigned char *map;
63 size_t map_size; /* size of the mmaped buffer */
64 size_t map_pos; /* current position when loading the index */
67 * Type indexes.
69 * Each bitmap marks which objects in the packfile are of the given
70 * type. This provides type information when yielding the objects from
71 * the packfile during a walk, which allows for better delta bases.
73 struct ewah_bitmap *commits;
74 struct ewah_bitmap *trees;
75 struct ewah_bitmap *blobs;
76 struct ewah_bitmap *tags;
78 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
79 kh_oid_map_t *bitmaps;
81 /* Number of bitmapped commits */
82 uint32_t entry_count;
84 /* If not NULL, this is a name-hash cache pointing into map. */
85 uint32_t *hashes;
87 /* The checksum of the packfile or MIDX; points into map. */
88 const unsigned char *checksum;
91 * If not NULL, this point into the commit table extension
92 * (within the memory mapped region `map`).
94 unsigned char *table_lookup;
97 * Extended index.
99 * When trying to perform bitmap operations with objects that are not
100 * packed in `pack`, these objects are added to this "fake index" and
101 * are assumed to appear at the end of the packfile for all operations
103 struct eindex {
104 struct object **objects;
105 uint32_t *hashes;
106 uint32_t count, alloc;
107 kh_oid_pos_t *positions;
108 } ext_index;
110 /* Bitmap result of the last performed walk */
111 struct bitmap *result;
113 /* "have" bitmap from the last performed walk */
114 struct bitmap *haves;
116 /* Version of the bitmap index */
117 unsigned int version;
120 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
122 struct ewah_bitmap *parent;
123 struct ewah_bitmap *composed;
125 if (!st->xor)
126 return st->root;
128 composed = ewah_pool_new();
129 parent = lookup_stored_bitmap(st->xor);
130 ewah_xor(st->root, parent, composed);
132 ewah_pool_free(st->root);
133 st->root = composed;
134 st->xor = NULL;
136 return composed;
140 * Read a bitmap from the current read position on the mmaped
141 * index, and increase the read position accordingly
143 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
145 struct ewah_bitmap *b = ewah_pool_new();
147 ssize_t bitmap_size = ewah_read_mmap(b,
148 index->map + index->map_pos,
149 index->map_size - index->map_pos);
151 if (bitmap_size < 0) {
152 error(_("failed to load bitmap index (corrupted?)"));
153 ewah_pool_free(b);
154 return NULL;
157 index->map_pos += bitmap_size;
158 return b;
161 static uint32_t bitmap_num_objects(struct bitmap_index *index)
163 if (index->midx)
164 return index->midx->num_objects;
165 return index->pack->num_objects;
168 static int load_bitmap_header(struct bitmap_index *index)
170 struct bitmap_disk_header *header = (void *)index->map;
171 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
173 if (index->map_size < header_size + the_hash_algo->rawsz)
174 return error(_("corrupted bitmap index (too small)"));
176 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
177 return error(_("corrupted bitmap index file (wrong header)"));
179 index->version = ntohs(header->version);
180 if (index->version != 1)
181 return error(_("unsupported version '%d' for bitmap index file"), index->version);
183 /* Parse known bitmap format options */
185 uint32_t flags = ntohs(header->options);
186 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
187 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
189 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
190 BUG("unsupported options for bitmap index file "
191 "(Git requires BITMAP_OPT_FULL_DAG)");
193 if (flags & BITMAP_OPT_HASH_CACHE) {
194 if (cache_size > index_end - index->map - header_size)
195 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
196 index->hashes = (void *)(index_end - cache_size);
197 index_end -= cache_size;
200 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
201 size_t table_size = st_mult(ntohl(header->entry_count),
202 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
203 if (table_size > index_end - index->map - header_size)
204 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
205 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
206 index->table_lookup = (void *)(index_end - table_size);
207 index_end -= table_size;
211 index->entry_count = ntohl(header->entry_count);
212 index->checksum = header->checksum;
213 index->map_pos += header_size;
214 return 0;
217 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
218 struct ewah_bitmap *root,
219 const struct object_id *oid,
220 struct stored_bitmap *xor_with,
221 int flags)
223 struct stored_bitmap *stored;
224 khiter_t hash_pos;
225 int ret;
227 stored = xmalloc(sizeof(struct stored_bitmap));
228 stored->root = root;
229 stored->xor = xor_with;
230 stored->flags = flags;
231 oidcpy(&stored->oid, oid);
233 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
236 * A 0 return code means the insertion succeeded with no changes,
237 * because the SHA1 already existed on the map. This is bad, there
238 * shouldn't be duplicated commits in the index.
240 if (ret == 0) {
241 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
242 return NULL;
245 kh_value(index->bitmaps, hash_pos) = stored;
246 return stored;
249 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
251 uint32_t result = get_be32(buffer + *pos);
252 (*pos) += sizeof(result);
253 return result;
256 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
258 return buffer[(*pos)++];
261 #define MAX_XOR_OFFSET 160
263 static int nth_bitmap_object_oid(struct bitmap_index *index,
264 struct object_id *oid,
265 uint32_t n)
267 if (index->midx)
268 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
269 return nth_packed_object_id(oid, index->pack, n);
272 static int load_bitmap_entries_v1(struct bitmap_index *index)
274 uint32_t i;
275 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
277 for (i = 0; i < index->entry_count; ++i) {
278 int xor_offset, flags;
279 struct ewah_bitmap *bitmap = NULL;
280 struct stored_bitmap *xor_bitmap = NULL;
281 uint32_t commit_idx_pos;
282 struct object_id oid;
284 if (index->map_size - index->map_pos < 6)
285 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
287 commit_idx_pos = read_be32(index->map, &index->map_pos);
288 xor_offset = read_u8(index->map, &index->map_pos);
289 flags = read_u8(index->map, &index->map_pos);
291 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
292 return error(_("corrupt ewah bitmap: commit index %u out of range"),
293 (unsigned)commit_idx_pos);
295 bitmap = read_bitmap_1(index);
296 if (!bitmap)
297 return -1;
299 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
300 return error(_("corrupted bitmap pack index"));
302 if (xor_offset > 0) {
303 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
305 if (!xor_bitmap)
306 return error(_("invalid XOR offset in bitmap pack index"));
309 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
310 index, bitmap, &oid, xor_bitmap, flags);
313 return 0;
316 char *midx_bitmap_filename(struct multi_pack_index *midx)
318 struct strbuf buf = STRBUF_INIT;
320 get_midx_filename(&buf, midx->object_dir);
321 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
323 return strbuf_detach(&buf, NULL);
326 char *pack_bitmap_filename(struct packed_git *p)
328 size_t len;
330 if (!strip_suffix(p->pack_name, ".pack", &len))
331 BUG("pack_name does not end in .pack");
332 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
335 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
336 struct multi_pack_index *midx)
338 struct stat st;
339 char *bitmap_name = midx_bitmap_filename(midx);
340 int fd = git_open(bitmap_name);
341 uint32_t i;
342 struct packed_git *preferred;
344 if (fd < 0) {
345 if (errno != ENOENT)
346 warning_errno("cannot open '%s'", bitmap_name);
347 free(bitmap_name);
348 return -1;
350 free(bitmap_name);
352 if (fstat(fd, &st)) {
353 error_errno(_("cannot fstat bitmap file"));
354 close(fd);
355 return -1;
358 if (bitmap_git->pack || bitmap_git->midx) {
359 struct strbuf buf = STRBUF_INIT;
360 get_midx_filename(&buf, midx->object_dir);
361 trace2_data_string("bitmap", the_repository,
362 "ignoring extra midx bitmap file", buf.buf);
363 close(fd);
364 strbuf_release(&buf);
365 return -1;
368 bitmap_git->midx = midx;
369 bitmap_git->map_size = xsize_t(st.st_size);
370 bitmap_git->map_pos = 0;
371 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
372 MAP_PRIVATE, fd, 0);
373 close(fd);
375 if (load_bitmap_header(bitmap_git) < 0)
376 goto cleanup;
378 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
379 error(_("checksum doesn't match in MIDX and bitmap"));
380 goto cleanup;
383 if (load_midx_revindex(bitmap_git->midx)) {
384 warning(_("multi-pack bitmap is missing required reverse index"));
385 goto cleanup;
388 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
389 if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
390 warning(_("could not open pack %s"),
391 bitmap_git->midx->pack_names[i]);
392 goto cleanup;
396 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
397 if (!is_pack_valid(preferred)) {
398 warning(_("preferred pack (%s) is invalid"),
399 preferred->pack_name);
400 goto cleanup;
403 return 0;
405 cleanup:
406 munmap(bitmap_git->map, bitmap_git->map_size);
407 bitmap_git->map_size = 0;
408 bitmap_git->map_pos = 0;
409 bitmap_git->map = NULL;
410 bitmap_git->midx = NULL;
411 return -1;
414 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
416 int fd;
417 struct stat st;
418 char *bitmap_name;
420 bitmap_name = pack_bitmap_filename(packfile);
421 fd = git_open(bitmap_name);
423 if (fd < 0) {
424 if (errno != ENOENT)
425 warning_errno("cannot open '%s'", bitmap_name);
426 free(bitmap_name);
427 return -1;
429 free(bitmap_name);
431 if (fstat(fd, &st)) {
432 error_errno(_("cannot fstat bitmap file"));
433 close(fd);
434 return -1;
437 if (bitmap_git->pack || bitmap_git->midx) {
438 trace2_data_string("bitmap", the_repository,
439 "ignoring extra bitmap file", packfile->pack_name);
440 close(fd);
441 return -1;
444 if (!is_pack_valid(packfile)) {
445 close(fd);
446 return -1;
449 bitmap_git->pack = packfile;
450 bitmap_git->map_size = xsize_t(st.st_size);
451 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
452 bitmap_git->map_pos = 0;
453 close(fd);
455 if (load_bitmap_header(bitmap_git) < 0) {
456 munmap(bitmap_git->map, bitmap_git->map_size);
457 bitmap_git->map = NULL;
458 bitmap_git->map_size = 0;
459 bitmap_git->map_pos = 0;
460 bitmap_git->pack = NULL;
461 return -1;
464 trace2_data_string("bitmap", the_repository, "opened bitmap file",
465 packfile->pack_name);
466 return 0;
469 static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
471 if (bitmap_is_midx(bitmap_git)) {
472 uint32_t i;
473 int ret;
476 * The multi-pack-index's .rev file is already loaded via
477 * open_pack_bitmap_1().
479 * But we still need to open the individual pack .rev files,
480 * since we will need to make use of them in pack-objects.
482 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
483 ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
484 if (ret)
485 return ret;
487 return 0;
489 return load_pack_revindex(r, bitmap_git->pack);
492 static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
494 assert(bitmap_git->map);
496 bitmap_git->bitmaps = kh_init_oid_map();
497 bitmap_git->ext_index.positions = kh_init_oid_pos();
499 if (load_reverse_index(r, bitmap_git))
500 goto failed;
502 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
503 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
504 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
505 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
506 goto failed;
508 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
509 goto failed;
511 return 0;
513 failed:
514 munmap(bitmap_git->map, bitmap_git->map_size);
515 bitmap_git->map = NULL;
516 bitmap_git->map_size = 0;
518 kh_destroy_oid_map(bitmap_git->bitmaps);
519 bitmap_git->bitmaps = NULL;
521 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
522 bitmap_git->ext_index.positions = NULL;
524 return -1;
527 static int open_pack_bitmap(struct repository *r,
528 struct bitmap_index *bitmap_git)
530 struct packed_git *p;
531 int ret = -1;
533 for (p = get_all_packs(r); p; p = p->next) {
534 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
535 ret = 0;
537 * The only reason to keep looking is to report
538 * duplicates.
540 if (!trace2_is_enabled())
541 break;
545 return ret;
548 static int open_midx_bitmap(struct repository *r,
549 struct bitmap_index *bitmap_git)
551 int ret = -1;
552 struct multi_pack_index *midx;
554 assert(!bitmap_git->map);
556 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
557 if (!open_midx_bitmap_1(bitmap_git, midx))
558 ret = 0;
560 return ret;
563 static int open_bitmap(struct repository *r,
564 struct bitmap_index *bitmap_git)
566 int found;
568 assert(!bitmap_git->map);
570 found = !open_midx_bitmap(r, bitmap_git);
573 * these will all be skipped if we opened a midx bitmap; but run it
574 * anyway if tracing is enabled to report the duplicates
576 if (!found || trace2_is_enabled())
577 found |= !open_pack_bitmap(r, bitmap_git);
579 return found ? 0 : -1;
582 struct bitmap_index *prepare_bitmap_git(struct repository *r)
584 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
586 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
587 return bitmap_git;
589 free_bitmap_index(bitmap_git);
590 return NULL;
593 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
595 struct repository *r = the_repository;
596 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
598 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
599 return bitmap_git;
601 free_bitmap_index(bitmap_git);
602 return NULL;
605 struct include_data {
606 struct bitmap_index *bitmap_git;
607 struct bitmap *base;
608 struct bitmap *seen;
611 struct bitmap_lookup_table_triplet {
612 uint32_t commit_pos;
613 uint64_t offset;
614 uint32_t xor_row;
617 struct bitmap_lookup_table_xor_item {
618 struct object_id oid;
619 uint64_t offset;
623 * Given a `triplet` struct pointer and pointer `p`, this
624 * function reads the triplet beginning at `p` into the struct.
625 * Note that this function assumes that there is enough memory
626 * left for filling the `triplet` struct from `p`.
628 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
629 const unsigned char *p)
631 if (!triplet)
632 return -1;
634 triplet->commit_pos = get_be32(p);
635 p += sizeof(uint32_t);
636 triplet->offset = get_be64(p);
637 p += sizeof(uint64_t);
638 triplet->xor_row = get_be32(p);
639 return 0;
643 * This function gets the raw triplet from `row`'th row in the
644 * lookup table and fills that data to the `triplet`.
646 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
647 uint32_t pos,
648 struct bitmap_lookup_table_triplet *triplet)
650 unsigned char *p = NULL;
651 if (pos >= bitmap_git->entry_count)
652 return error(_("corrupt bitmap lookup table: triplet position out of index"));
654 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
656 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
660 * Searches for a matching triplet. `commit_pos` is a pointer
661 * to the wanted commit position value. `table_entry` points to
662 * a triplet in lookup table. The first 4 bytes of each
663 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
665 static int triplet_cmp(const void *commit_pos, const void *table_entry)
668 uint32_t a = *(uint32_t *)commit_pos;
669 uint32_t b = get_be32(table_entry);
670 if (a > b)
671 return 1;
672 else if (a < b)
673 return -1;
675 return 0;
678 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
679 struct object_id *oid,
680 uint32_t *result)
682 int found;
684 if (bitmap_is_midx(bitmap_git))
685 found = bsearch_midx(oid, bitmap_git->midx, result);
686 else
687 found = bsearch_pack(oid, bitmap_git->pack, result);
689 return found;
693 * `bsearch_triplet_by_pos` function searches for the raw triplet
694 * having commit position same as `commit_pos` and fills `triplet`
695 * object from the raw triplet. Returns 1 on success and 0 on
696 * failure.
698 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
699 struct bitmap_index *bitmap_git,
700 struct bitmap_lookup_table_triplet *triplet)
702 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
703 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
705 if (!p)
706 return -1;
708 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
711 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
712 struct commit *commit)
714 uint32_t commit_pos, xor_row;
715 uint64_t offset;
716 int flags;
717 struct bitmap_lookup_table_triplet triplet;
718 struct object_id *oid = &commit->object.oid;
719 struct ewah_bitmap *bitmap;
720 struct stored_bitmap *xor_bitmap = NULL;
721 const int bitmap_header_size = 6;
722 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
723 static size_t xor_items_nr = 0, xor_items_alloc = 0;
724 static int is_corrupt = 0;
725 int xor_flags;
726 khiter_t hash_pos;
727 struct bitmap_lookup_table_xor_item *xor_item;
729 if (is_corrupt)
730 return NULL;
732 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
733 return NULL;
735 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
736 return NULL;
738 xor_items_nr = 0;
739 offset = triplet.offset;
740 xor_row = triplet.xor_row;
742 while (xor_row != 0xffffffff) {
743 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
745 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
746 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
747 goto corrupt;
750 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
751 goto corrupt;
753 xor_item = &xor_items[xor_items_nr];
754 xor_item->offset = triplet.offset;
756 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
757 error(_("corrupt bitmap lookup table: commit index %u out of range"),
758 triplet.commit_pos);
759 goto corrupt;
762 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
765 * If desired bitmap is already stored, we don't need
766 * to iterate further. Because we know that bitmaps
767 * that are needed to be parsed to parse this bitmap
768 * has already been stored. So, assign this stored bitmap
769 * to the xor_bitmap.
771 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
772 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
773 break;
774 xor_items_nr++;
775 xor_row = triplet.xor_row;
778 while (xor_items_nr) {
779 xor_item = &xor_items[xor_items_nr - 1];
780 bitmap_git->map_pos = xor_item->offset;
781 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
782 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
783 oid_to_hex(&xor_item->oid));
784 goto corrupt;
787 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
788 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
789 bitmap = read_bitmap_1(bitmap_git);
791 if (!bitmap)
792 goto corrupt;
794 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
795 xor_items_nr--;
798 bitmap_git->map_pos = offset;
799 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
800 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
801 oid_to_hex(oid));
802 goto corrupt;
806 * Don't bother reading the commit's index position or its xor
807 * offset:
809 * - The commit's index position is irrelevant to us, since
810 * load_bitmap_entries_v1 only uses it to learn the object
811 * id which is used to compute the hashmap's key. We already
812 * have an object id, so no need to look it up again.
814 * - The xor_offset is unusable for us, since it specifies how
815 * many entries previous to ours we should look at. This
816 * makes sense when reading the bitmaps sequentially (as in
817 * load_bitmap_entries_v1()), since we can keep track of
818 * each bitmap as we read them.
820 * But it can't work for us, since the bitmap's don't have a
821 * fixed size. So we learn the position of the xor'd bitmap
822 * from the commit table (and resolve it to a bitmap in the
823 * above if-statement).
825 * Instead, we can skip ahead and immediately read the flags and
826 * ewah bitmap.
828 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
829 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
830 bitmap = read_bitmap_1(bitmap_git);
832 if (!bitmap)
833 goto corrupt;
835 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
837 corrupt:
838 free(xor_items);
839 is_corrupt = 1;
840 return NULL;
843 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
844 struct commit *commit)
846 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
847 commit->object.oid);
848 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
849 struct stored_bitmap *bitmap = NULL;
850 if (!bitmap_git->table_lookup)
851 return NULL;
853 /* this is a fairly hot codepath - no trace2_region please */
854 /* NEEDSWORK: cache misses aren't recorded */
855 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
856 if (!bitmap)
857 return NULL;
858 return lookup_stored_bitmap(bitmap);
860 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
863 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
864 const struct object_id *oid)
866 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
867 khiter_t pos = kh_get_oid_pos(positions, *oid);
869 if (pos < kh_end(positions)) {
870 int bitmap_pos = kh_value(positions, pos);
871 return bitmap_pos + bitmap_num_objects(bitmap_git);
874 return -1;
877 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
878 const struct object_id *oid)
880 uint32_t pos;
881 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
882 if (!offset)
883 return -1;
885 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
886 return -1;
887 return pos;
890 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
891 const struct object_id *oid)
893 uint32_t want, got;
894 if (!bsearch_midx(oid, bitmap_git->midx, &want))
895 return -1;
897 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
898 return -1;
899 return got;
902 static int bitmap_position(struct bitmap_index *bitmap_git,
903 const struct object_id *oid)
905 int pos;
906 if (bitmap_is_midx(bitmap_git))
907 pos = bitmap_position_midx(bitmap_git, oid);
908 else
909 pos = bitmap_position_packfile(bitmap_git, oid);
910 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
913 static int ext_index_add_object(struct bitmap_index *bitmap_git,
914 struct object *object, const char *name)
916 struct eindex *eindex = &bitmap_git->ext_index;
918 khiter_t hash_pos;
919 int hash_ret;
920 int bitmap_pos;
922 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
923 if (hash_ret > 0) {
924 if (eindex->count >= eindex->alloc) {
925 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
926 REALLOC_ARRAY(eindex->objects, eindex->alloc);
927 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
930 bitmap_pos = eindex->count;
931 eindex->objects[eindex->count] = object;
932 eindex->hashes[eindex->count] = pack_name_hash(name);
933 kh_value(eindex->positions, hash_pos) = bitmap_pos;
934 eindex->count++;
935 } else {
936 bitmap_pos = kh_value(eindex->positions, hash_pos);
939 return bitmap_pos + bitmap_num_objects(bitmap_git);
942 struct bitmap_show_data {
943 struct bitmap_index *bitmap_git;
944 struct bitmap *base;
947 static void show_object(struct object *object, const char *name, void *data_)
949 struct bitmap_show_data *data = data_;
950 int bitmap_pos;
952 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
954 if (bitmap_pos < 0)
955 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
956 name);
958 bitmap_set(data->base, bitmap_pos);
961 static void show_commit(struct commit *commit UNUSED,
962 void *data UNUSED)
966 static int add_to_include_set(struct bitmap_index *bitmap_git,
967 struct include_data *data,
968 struct commit *commit,
969 int bitmap_pos)
971 struct ewah_bitmap *partial;
973 if (data->seen && bitmap_get(data->seen, bitmap_pos))
974 return 0;
976 if (bitmap_get(data->base, bitmap_pos))
977 return 0;
979 partial = bitmap_for_commit(bitmap_git, commit);
980 if (partial) {
981 bitmap_or_ewah(data->base, partial);
982 return 0;
985 bitmap_set(data->base, bitmap_pos);
986 return 1;
989 static int should_include(struct commit *commit, void *_data)
991 struct include_data *data = _data;
992 int bitmap_pos;
994 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
995 if (bitmap_pos < 0)
996 bitmap_pos = ext_index_add_object(data->bitmap_git,
997 (struct object *)commit,
998 NULL);
1000 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
1001 struct commit_list *parent = commit->parents;
1003 while (parent) {
1004 parent->item->object.flags |= SEEN;
1005 parent = parent->next;
1008 return 0;
1011 return 1;
1014 static int should_include_obj(struct object *obj, void *_data)
1016 struct include_data *data = _data;
1017 int bitmap_pos;
1019 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1020 if (bitmap_pos < 0)
1021 return 1;
1022 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1023 bitmap_get(data->base, bitmap_pos)) {
1024 obj->flags |= SEEN;
1025 return 0;
1027 return 1;
1030 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1031 struct bitmap **base,
1032 struct commit *commit)
1034 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1036 if (!or_with)
1037 return 0;
1039 if (!*base)
1040 *base = ewah_to_bitmap(or_with);
1041 else
1042 bitmap_or_ewah(*base, or_with);
1044 return 1;
1047 static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1048 struct rev_info *revs,
1049 struct bitmap *base,
1050 struct bitmap *seen)
1052 struct include_data incdata;
1053 struct bitmap_show_data show_data;
1055 if (!base)
1056 base = bitmap_new();
1058 incdata.bitmap_git = bitmap_git;
1059 incdata.base = base;
1060 incdata.seen = seen;
1062 revs->include_check = should_include;
1063 revs->include_check_obj = should_include_obj;
1064 revs->include_check_data = &incdata;
1066 if (prepare_revision_walk(revs))
1067 die(_("revision walk setup failed"));
1069 show_data.bitmap_git = bitmap_git;
1070 show_data.base = base;
1072 traverse_commit_list(revs, show_commit, show_object, &show_data);
1074 revs->include_check = NULL;
1075 revs->include_check_obj = NULL;
1076 revs->include_check_data = NULL;
1078 return base;
1081 struct bitmap_boundary_cb {
1082 struct bitmap_index *bitmap_git;
1083 struct bitmap *base;
1085 struct object_array boundary;
1088 static void show_boundary_commit(struct commit *commit, void *_data)
1090 struct bitmap_boundary_cb *data = _data;
1092 if (commit->object.flags & BOUNDARY)
1093 add_object_array(&commit->object, "", &data->boundary);
1095 if (commit->object.flags & UNINTERESTING) {
1096 if (bitmap_walk_contains(data->bitmap_git, data->base,
1097 &commit->object.oid))
1098 return;
1100 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1104 static void show_boundary_object(struct object *object,
1105 const char *name, void *data)
1107 BUG("should not be called");
1110 static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1111 struct rev_info *revs,
1112 struct object_list *roots)
1114 struct bitmap_boundary_cb cb;
1115 struct object_list *root;
1116 unsigned int i;
1117 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1118 int any_missing = 0;
1120 cb.bitmap_git = bitmap_git;
1121 cb.base = bitmap_new();
1122 object_array_init(&cb.boundary);
1124 revs->ignore_missing_links = 1;
1127 * OR in any existing reachability bitmaps among `roots` into
1128 * `cb.base`.
1130 for (root = roots; root; root = root->next) {
1131 struct object *object = root->item;
1132 if (object->type != OBJ_COMMIT ||
1133 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1134 continue;
1136 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1137 (struct commit *)object))
1138 continue;
1140 any_missing = 1;
1143 if (!any_missing)
1144 goto cleanup;
1146 tmp_blobs = revs->blob_objects;
1147 tmp_trees = revs->tree_objects;
1148 tmp_tags = revs->blob_objects;
1149 revs->blob_objects = 0;
1150 revs->tree_objects = 0;
1151 revs->tag_objects = 0;
1154 * We didn't have complete coverage of the roots. First setup a
1155 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1156 * between the tips and boundary, and (b) record the boundary.
1158 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1159 if (prepare_revision_walk(revs))
1160 die("revision walk setup failed");
1161 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1163 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1164 revs->boundary = 1;
1165 traverse_commit_list_filtered(revs,
1166 show_boundary_commit,
1167 show_boundary_object,
1168 &cb, NULL);
1169 revs->boundary = 0;
1170 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1172 revs->blob_objects = tmp_blobs;
1173 revs->tree_objects = tmp_trees;
1174 revs->tag_objects = tmp_tags;
1176 reset_revision_walk();
1177 clear_object_flags(UNINTERESTING);
1180 * Then add the boundary commit(s) as fill-in traversal tips.
1182 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1183 for (i = 0; i < cb.boundary.nr; i++) {
1184 struct object *obj = cb.boundary.objects[i].item;
1185 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1186 obj->flags |= SEEN;
1187 else
1188 add_pending_object(revs, obj, "");
1190 if (revs->pending.nr)
1191 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1192 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1194 cleanup:
1195 object_array_clear(&cb.boundary);
1196 revs->ignore_missing_links = 0;
1198 return cb.base;
1201 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1202 struct rev_info *revs,
1203 struct object_list *roots,
1204 struct bitmap *seen)
1206 struct bitmap *base = NULL;
1207 int needs_walk = 0;
1209 struct object_list *not_mapped = NULL;
1212 * Go through all the roots for the walk. The ones that have bitmaps
1213 * on the bitmap index will be `or`ed together to form an initial
1214 * global reachability analysis.
1216 * The ones without bitmaps in the index will be stored in the
1217 * `not_mapped_list` for further processing.
1219 while (roots) {
1220 struct object *object = roots->item;
1221 roots = roots->next;
1223 if (object->type == OBJ_COMMIT &&
1224 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1225 object->flags |= SEEN;
1226 continue;
1229 object_list_insert(object, &not_mapped);
1233 * Best case scenario: We found bitmaps for all the roots,
1234 * so the resulting `or` bitmap has the full reachability analysis
1236 if (!not_mapped)
1237 return base;
1239 roots = not_mapped;
1242 * Let's iterate through all the roots that don't have bitmaps to
1243 * check if we can determine them to be reachable from the existing
1244 * global bitmap.
1246 * If we cannot find them in the existing global bitmap, we'll need
1247 * to push them to an actual walk and run it until we can confirm
1248 * they are reachable
1250 while (roots) {
1251 struct object *object = roots->item;
1252 int pos;
1254 roots = roots->next;
1255 pos = bitmap_position(bitmap_git, &object->oid);
1257 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1258 object->flags &= ~UNINTERESTING;
1259 add_pending_object(revs, object, "");
1260 needs_walk = 1;
1261 } else {
1262 object->flags |= SEEN;
1266 if (needs_walk) {
1268 * This fill-in traversal may walk over some objects
1269 * again, since we have already traversed in order to
1270 * find the boundary.
1272 * But this extra walk should be extremely cheap, since
1273 * all commit objects are loaded into memory, and
1274 * because we skip walking to parents that are
1275 * UNINTERESTING, since it will be marked in the haves
1276 * bitmap already (or it has an on-disk bitmap, since
1277 * OR-ing it in covers all of its ancestors).
1279 base = fill_in_bitmap(bitmap_git, revs, base, seen);
1282 return base;
1285 static void show_extended_objects(struct bitmap_index *bitmap_git,
1286 struct rev_info *revs,
1287 show_reachable_fn show_reach)
1289 struct bitmap *objects = bitmap_git->result;
1290 struct eindex *eindex = &bitmap_git->ext_index;
1291 uint32_t i;
1293 for (i = 0; i < eindex->count; ++i) {
1294 struct object *obj;
1296 if (!bitmap_get(objects, st_add(bitmap_num_objects(bitmap_git), i)))
1297 continue;
1299 obj = eindex->objects[i];
1300 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1301 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1302 (obj->type == OBJ_TAG && !revs->tag_objects))
1303 continue;
1305 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1309 static void init_type_iterator(struct ewah_iterator *it,
1310 struct bitmap_index *bitmap_git,
1311 enum object_type type)
1313 switch (type) {
1314 case OBJ_COMMIT:
1315 ewah_iterator_init(it, bitmap_git->commits);
1316 break;
1318 case OBJ_TREE:
1319 ewah_iterator_init(it, bitmap_git->trees);
1320 break;
1322 case OBJ_BLOB:
1323 ewah_iterator_init(it, bitmap_git->blobs);
1324 break;
1326 case OBJ_TAG:
1327 ewah_iterator_init(it, bitmap_git->tags);
1328 break;
1330 default:
1331 BUG("object type %d not stored by bitmap type index", type);
1332 break;
1336 static void show_objects_for_type(
1337 struct bitmap_index *bitmap_git,
1338 enum object_type object_type,
1339 show_reachable_fn show_reach)
1341 size_t i = 0;
1342 uint32_t offset;
1344 struct ewah_iterator it;
1345 eword_t filter;
1347 struct bitmap *objects = bitmap_git->result;
1349 init_type_iterator(&it, bitmap_git, object_type);
1351 for (i = 0; i < objects->word_alloc &&
1352 ewah_iterator_next(&filter, &it); i++) {
1353 eword_t word = objects->words[i] & filter;
1354 size_t pos = (i * BITS_IN_EWORD);
1356 if (!word)
1357 continue;
1359 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1360 struct packed_git *pack;
1361 struct object_id oid;
1362 uint32_t hash = 0, index_pos;
1363 off_t ofs;
1365 if ((word >> offset) == 0)
1366 break;
1368 offset += ewah_bit_ctz64(word >> offset);
1370 if (bitmap_is_midx(bitmap_git)) {
1371 struct multi_pack_index *m = bitmap_git->midx;
1372 uint32_t pack_id;
1374 index_pos = pack_pos_to_midx(m, pos + offset);
1375 ofs = nth_midxed_offset(m, index_pos);
1376 nth_midxed_object_oid(&oid, m, index_pos);
1378 pack_id = nth_midxed_pack_int_id(m, index_pos);
1379 pack = bitmap_git->midx->packs[pack_id];
1380 } else {
1381 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1382 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1383 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1385 pack = bitmap_git->pack;
1388 if (bitmap_git->hashes)
1389 hash = get_be32(bitmap_git->hashes + index_pos);
1391 show_reach(&oid, object_type, 0, hash, pack, ofs);
1396 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1397 struct object_list *roots)
1399 while (roots) {
1400 struct object *object = roots->item;
1401 roots = roots->next;
1403 if (bitmap_is_midx(bitmap_git)) {
1404 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1405 return 1;
1406 } else {
1407 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1408 return 1;
1412 return 0;
1415 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1416 struct object_list *tip_objects,
1417 enum object_type type)
1419 struct bitmap *result = bitmap_new();
1420 struct object_list *p;
1422 for (p = tip_objects; p; p = p->next) {
1423 int pos;
1425 if (p->item->type != type)
1426 continue;
1428 pos = bitmap_position(bitmap_git, &p->item->oid);
1429 if (pos < 0)
1430 continue;
1432 bitmap_set(result, pos);
1435 return result;
1438 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1439 struct object_list *tip_objects,
1440 struct bitmap *to_filter,
1441 enum object_type type)
1443 struct eindex *eindex = &bitmap_git->ext_index;
1444 struct bitmap *tips;
1445 struct ewah_iterator it;
1446 eword_t mask;
1447 uint32_t i;
1450 * The non-bitmap version of this filter never removes
1451 * objects which the other side specifically asked for,
1452 * so we must match that behavior.
1454 tips = find_tip_objects(bitmap_git, tip_objects, type);
1457 * We can use the type-level bitmap for 'type' to work in whole
1458 * words for the objects that are actually in the bitmapped
1459 * packfile.
1461 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1462 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1463 i++) {
1464 if (i < tips->word_alloc)
1465 mask &= ~tips->words[i];
1466 to_filter->words[i] &= ~mask;
1470 * Clear any objects that weren't in the packfile (and so would
1471 * not have been caught by the loop above. We'll have to check
1472 * them individually.
1474 for (i = 0; i < eindex->count; i++) {
1475 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1476 if (eindex->objects[i]->type == type &&
1477 bitmap_get(to_filter, pos) &&
1478 !bitmap_get(tips, pos))
1479 bitmap_unset(to_filter, pos);
1482 bitmap_free(tips);
1485 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1486 struct object_list *tip_objects,
1487 struct bitmap *to_filter)
1489 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1490 OBJ_BLOB);
1493 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1494 uint32_t pos)
1496 unsigned long size;
1497 struct object_info oi = OBJECT_INFO_INIT;
1499 oi.sizep = &size;
1501 if (pos < bitmap_num_objects(bitmap_git)) {
1502 struct packed_git *pack;
1503 off_t ofs;
1505 if (bitmap_is_midx(bitmap_git)) {
1506 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1507 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1509 pack = bitmap_git->midx->packs[pack_id];
1510 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1511 } else {
1512 pack = bitmap_git->pack;
1513 ofs = pack_pos_to_offset(pack, pos);
1516 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1517 struct object_id oid;
1518 nth_bitmap_object_oid(bitmap_git, &oid,
1519 pack_pos_to_index(pack, pos));
1520 die(_("unable to get size of %s"), oid_to_hex(&oid));
1522 } else {
1523 struct eindex *eindex = &bitmap_git->ext_index;
1524 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1525 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1526 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1529 return size;
1532 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1533 struct object_list *tip_objects,
1534 struct bitmap *to_filter,
1535 unsigned long limit)
1537 struct eindex *eindex = &bitmap_git->ext_index;
1538 struct bitmap *tips;
1539 struct ewah_iterator it;
1540 eword_t mask;
1541 uint32_t i;
1543 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1545 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1546 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1547 i++) {
1548 eword_t word = to_filter->words[i] & mask;
1549 unsigned offset;
1551 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1552 uint32_t pos;
1554 if ((word >> offset) == 0)
1555 break;
1556 offset += ewah_bit_ctz64(word >> offset);
1557 pos = i * BITS_IN_EWORD + offset;
1559 if (!bitmap_get(tips, pos) &&
1560 get_size_by_pos(bitmap_git, pos) >= limit)
1561 bitmap_unset(to_filter, pos);
1565 for (i = 0; i < eindex->count; i++) {
1566 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1567 if (eindex->objects[i]->type == OBJ_BLOB &&
1568 bitmap_get(to_filter, pos) &&
1569 !bitmap_get(tips, pos) &&
1570 get_size_by_pos(bitmap_git, pos) >= limit)
1571 bitmap_unset(to_filter, pos);
1574 bitmap_free(tips);
1577 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1578 struct object_list *tip_objects,
1579 struct bitmap *to_filter,
1580 unsigned long limit)
1582 if (limit)
1583 BUG("filter_bitmap_tree_depth given non-zero limit");
1585 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1586 OBJ_TREE);
1587 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1588 OBJ_BLOB);
1591 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1592 struct object_list *tip_objects,
1593 struct bitmap *to_filter,
1594 enum object_type object_type)
1596 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1597 BUG("filter_bitmap_object_type given invalid object");
1599 if (object_type != OBJ_TAG)
1600 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1601 if (object_type != OBJ_COMMIT)
1602 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1603 if (object_type != OBJ_TREE)
1604 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1605 if (object_type != OBJ_BLOB)
1606 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1609 static int filter_bitmap(struct bitmap_index *bitmap_git,
1610 struct object_list *tip_objects,
1611 struct bitmap *to_filter,
1612 struct list_objects_filter_options *filter)
1614 if (!filter || filter->choice == LOFC_DISABLED)
1615 return 0;
1617 if (filter->choice == LOFC_BLOB_NONE) {
1618 if (bitmap_git)
1619 filter_bitmap_blob_none(bitmap_git, tip_objects,
1620 to_filter);
1621 return 0;
1624 if (filter->choice == LOFC_BLOB_LIMIT) {
1625 if (bitmap_git)
1626 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1627 to_filter,
1628 filter->blob_limit_value);
1629 return 0;
1632 if (filter->choice == LOFC_TREE_DEPTH &&
1633 filter->tree_exclude_depth == 0) {
1634 if (bitmap_git)
1635 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1636 to_filter,
1637 filter->tree_exclude_depth);
1638 return 0;
1641 if (filter->choice == LOFC_OBJECT_TYPE) {
1642 if (bitmap_git)
1643 filter_bitmap_object_type(bitmap_git, tip_objects,
1644 to_filter,
1645 filter->object_type);
1646 return 0;
1649 if (filter->choice == LOFC_COMBINE) {
1650 int i;
1651 for (i = 0; i < filter->sub_nr; i++) {
1652 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1653 &filter->sub[i]) < 0)
1654 return -1;
1656 return 0;
1659 /* filter choice not handled */
1660 return -1;
1663 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1665 return !filter_bitmap(NULL, NULL, NULL, filter);
1668 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1669 int filter_provided_objects)
1671 unsigned int i;
1672 int use_boundary_traversal;
1674 struct object_list *wants = NULL;
1675 struct object_list *haves = NULL;
1677 struct bitmap *wants_bitmap = NULL;
1678 struct bitmap *haves_bitmap = NULL;
1680 struct bitmap_index *bitmap_git;
1683 * We can't do pathspec limiting with bitmaps, because we don't know
1684 * which commits are associated with which object changes (let alone
1685 * even which objects are associated with which paths).
1687 if (revs->prune)
1688 return NULL;
1690 if (!can_filter_bitmap(&revs->filter))
1691 return NULL;
1693 /* try to open a bitmapped pack, but don't parse it yet
1694 * because we may not need to use it */
1695 CALLOC_ARRAY(bitmap_git, 1);
1696 if (open_bitmap(revs->repo, bitmap_git) < 0)
1697 goto cleanup;
1699 for (i = 0; i < revs->pending.nr; ++i) {
1700 struct object *object = revs->pending.objects[i].item;
1702 if (object->type == OBJ_NONE)
1703 parse_object_or_die(&object->oid, NULL);
1705 while (object->type == OBJ_TAG) {
1706 struct tag *tag = (struct tag *) object;
1708 if (object->flags & UNINTERESTING)
1709 object_list_insert(object, &haves);
1710 else
1711 object_list_insert(object, &wants);
1713 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1714 object->flags |= (tag->object.flags & UNINTERESTING);
1717 if (object->flags & UNINTERESTING)
1718 object_list_insert(object, &haves);
1719 else
1720 object_list_insert(object, &wants);
1723 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1724 if (use_boundary_traversal < 0) {
1725 prepare_repo_settings(revs->repo);
1726 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1729 if (!use_boundary_traversal) {
1731 * if we have a HAVES list, but none of those haves is contained
1732 * in the packfile that has a bitmap, we don't have anything to
1733 * optimize here
1735 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1736 goto cleanup;
1739 /* if we don't want anything, we're done here */
1740 if (!wants)
1741 goto cleanup;
1744 * now we're going to use bitmaps, so load the actual bitmap entries
1745 * from disk. this is the point of no return; after this the rev_list
1746 * becomes invalidated and we must perform the revwalk through bitmaps
1748 if (load_bitmap(revs->repo, bitmap_git) < 0)
1749 goto cleanup;
1751 if (!use_boundary_traversal)
1752 object_array_clear(&revs->pending);
1754 if (haves) {
1755 if (use_boundary_traversal) {
1756 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1757 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1758 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1759 } else {
1760 trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1761 revs->ignore_missing_links = 1;
1762 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1763 reset_revision_walk();
1764 revs->ignore_missing_links = 0;
1765 trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1768 if (!haves_bitmap)
1769 BUG("failed to perform bitmap walk");
1772 if (use_boundary_traversal) {
1773 object_array_clear(&revs->pending);
1774 reset_revision_walk();
1777 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1779 if (!wants_bitmap)
1780 BUG("failed to perform bitmap walk");
1782 if (haves_bitmap)
1783 bitmap_and_not(wants_bitmap, haves_bitmap);
1785 filter_bitmap(bitmap_git,
1786 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1787 wants_bitmap,
1788 &revs->filter);
1790 bitmap_git->result = wants_bitmap;
1791 bitmap_git->haves = haves_bitmap;
1793 object_list_free(&wants);
1794 object_list_free(&haves);
1796 return bitmap_git;
1798 cleanup:
1799 free_bitmap_index(bitmap_git);
1800 object_list_free(&wants);
1801 object_list_free(&haves);
1802 return NULL;
1806 * -1 means "stop trying further objects"; 0 means we may or may not have
1807 * reused, but you can keep feeding bits.
1809 static int try_partial_reuse(struct packed_git *pack,
1810 size_t pos,
1811 struct bitmap *reuse,
1812 struct pack_window **w_curs)
1814 off_t offset, delta_obj_offset;
1815 enum object_type type;
1816 unsigned long size;
1819 * try_partial_reuse() is called either on (a) objects in the
1820 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1821 * objects in the preferred pack of a multi-pack bitmap.
1822 * Importantly, the latter can pretend as if only a single pack
1823 * exists because:
1825 * - The first pack->num_objects bits of a MIDX bitmap are
1826 * reserved for the preferred pack, and
1828 * - Ties due to duplicate objects are always resolved in
1829 * favor of the preferred pack.
1831 * Therefore we do not need to ever ask the MIDX for its copy of
1832 * an object by OID, since it will always select it from the
1833 * preferred pack. Likewise, the selected copy of the base
1834 * object for any deltas will reside in the same pack.
1836 * This means that we can reuse pos when looking up the bit in
1837 * the reuse bitmap, too, since bits corresponding to the
1838 * preferred pack precede all bits from other packs.
1841 if (pos >= pack->num_objects)
1842 return -1; /* not actually in the pack or MIDX preferred pack */
1844 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1845 type = unpack_object_header(pack, w_curs, &offset, &size);
1846 if (type < 0)
1847 return -1; /* broken packfile, punt */
1849 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1850 off_t base_offset;
1851 uint32_t base_pos;
1854 * Find the position of the base object so we can look it up
1855 * in our bitmaps. If we can't come up with an offset, or if
1856 * that offset is not in the revidx, the pack is corrupt.
1857 * There's nothing we can do, so just punt on this object,
1858 * and the normal slow path will complain about it in
1859 * more detail.
1861 base_offset = get_delta_base(pack, w_curs, &offset, type,
1862 delta_obj_offset);
1863 if (!base_offset)
1864 return 0;
1865 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1866 return 0;
1869 * We assume delta dependencies always point backwards. This
1870 * lets us do a single pass, and is basically always true
1871 * due to the way OFS_DELTAs work. You would not typically
1872 * find REF_DELTA in a bitmapped pack, since we only bitmap
1873 * packs we write fresh, and OFS_DELTA is the default). But
1874 * let's double check to make sure the pack wasn't written with
1875 * odd parameters.
1877 if (base_pos >= pos)
1878 return 0;
1881 * And finally, if we're not sending the base as part of our
1882 * reuse chunk, then don't send this object either. The base
1883 * would come after us, along with other objects not
1884 * necessarily in the pack, which means we'd need to convert
1885 * to REF_DELTA on the fly. Better to just let the normal
1886 * object_entry code path handle it.
1888 if (!bitmap_get(reuse, base_pos))
1889 return 0;
1893 * If we got here, then the object is OK to reuse. Mark it.
1895 bitmap_set(reuse, pos);
1896 return 0;
1899 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1901 struct multi_pack_index *m = bitmap_git->midx;
1902 if (!m)
1903 BUG("midx_preferred_pack: requires non-empty MIDX");
1904 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1907 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1908 struct packed_git **packfile_out,
1909 uint32_t *entries,
1910 struct bitmap **reuse_out)
1912 struct repository *r = the_repository;
1913 struct packed_git *pack;
1914 struct bitmap *result = bitmap_git->result;
1915 struct bitmap *reuse;
1916 struct pack_window *w_curs = NULL;
1917 size_t i = 0;
1918 uint32_t offset;
1919 uint32_t objects_nr;
1921 assert(result);
1923 load_reverse_index(r, bitmap_git);
1925 if (bitmap_is_midx(bitmap_git))
1926 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1927 else
1928 pack = bitmap_git->pack;
1929 objects_nr = pack->num_objects;
1931 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1932 i++;
1935 * Don't mark objects not in the packfile or preferred pack. This bitmap
1936 * marks objects eligible for reuse, but the pack-reuse code only
1937 * understands how to reuse a single pack. Since the preferred pack is
1938 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1939 * we use it instead of another pack. In single-pack bitmaps, the choice
1940 * is made for us.
1942 if (i > objects_nr / BITS_IN_EWORD)
1943 i = objects_nr / BITS_IN_EWORD;
1945 reuse = bitmap_word_alloc(i);
1946 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1948 for (; i < result->word_alloc; ++i) {
1949 eword_t word = result->words[i];
1950 size_t pos = (i * BITS_IN_EWORD);
1952 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1953 if ((word >> offset) == 0)
1954 break;
1956 offset += ewah_bit_ctz64(word >> offset);
1957 if (try_partial_reuse(pack, pos + offset,
1958 reuse, &w_curs) < 0) {
1960 * try_partial_reuse indicated we couldn't reuse
1961 * any bits, so there is no point in trying more
1962 * bits in the current word, or any other words
1963 * in result.
1965 * Jump out of both loops to avoid future
1966 * unnecessary calls to try_partial_reuse.
1968 goto done;
1973 done:
1974 unuse_pack(&w_curs);
1976 *entries = bitmap_popcount(reuse);
1977 if (!*entries) {
1978 bitmap_free(reuse);
1979 return -1;
1983 * Drop any reused objects from the result, since they will not
1984 * need to be handled separately.
1986 bitmap_and_not(result, reuse);
1987 *packfile_out = pack;
1988 *reuse_out = reuse;
1989 return 0;
1992 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1993 struct bitmap *bitmap, const struct object_id *oid)
1995 int idx;
1997 if (!bitmap)
1998 return 0;
2000 idx = bitmap_position(bitmap_git, oid);
2001 return idx >= 0 && bitmap_get(bitmap, idx);
2004 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2005 struct rev_info *revs,
2006 show_reachable_fn show_reachable)
2008 assert(bitmap_git->result);
2010 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2011 if (revs->tree_objects)
2012 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2013 if (revs->blob_objects)
2014 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2015 if (revs->tag_objects)
2016 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2018 show_extended_objects(bitmap_git, revs, show_reachable);
2021 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2022 enum object_type type)
2024 struct bitmap *objects = bitmap_git->result;
2025 struct eindex *eindex = &bitmap_git->ext_index;
2027 uint32_t i = 0, count = 0;
2028 struct ewah_iterator it;
2029 eword_t filter;
2031 init_type_iterator(&it, bitmap_git, type);
2033 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2034 eword_t word = objects->words[i++] & filter;
2035 count += ewah_bit_popcount64(word);
2038 for (i = 0; i < eindex->count; ++i) {
2039 if (eindex->objects[i]->type == type &&
2040 bitmap_get(objects,
2041 st_add(bitmap_num_objects(bitmap_git), i)))
2042 count++;
2045 return count;
2048 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2049 uint32_t *commits, uint32_t *trees,
2050 uint32_t *blobs, uint32_t *tags)
2052 assert(bitmap_git->result);
2054 if (commits)
2055 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2057 if (trees)
2058 *trees = count_object_type(bitmap_git, OBJ_TREE);
2060 if (blobs)
2061 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2063 if (tags)
2064 *tags = count_object_type(bitmap_git, OBJ_TAG);
2067 struct bitmap_test_data {
2068 struct bitmap_index *bitmap_git;
2069 struct bitmap *base;
2070 struct bitmap *commits;
2071 struct bitmap *trees;
2072 struct bitmap *blobs;
2073 struct bitmap *tags;
2074 struct progress *prg;
2075 size_t seen;
2078 static void test_bitmap_type(struct bitmap_test_data *tdata,
2079 struct object *obj, int pos)
2081 enum object_type bitmap_type = OBJ_NONE;
2082 int bitmaps_nr = 0;
2084 if (bitmap_get(tdata->commits, pos)) {
2085 bitmap_type = OBJ_COMMIT;
2086 bitmaps_nr++;
2088 if (bitmap_get(tdata->trees, pos)) {
2089 bitmap_type = OBJ_TREE;
2090 bitmaps_nr++;
2092 if (bitmap_get(tdata->blobs, pos)) {
2093 bitmap_type = OBJ_BLOB;
2094 bitmaps_nr++;
2096 if (bitmap_get(tdata->tags, pos)) {
2097 bitmap_type = OBJ_TAG;
2098 bitmaps_nr++;
2101 if (bitmap_type == OBJ_NONE)
2102 die(_("object '%s' not found in type bitmaps"),
2103 oid_to_hex(&obj->oid));
2105 if (bitmaps_nr > 1)
2106 die(_("object '%s' does not have a unique type"),
2107 oid_to_hex(&obj->oid));
2109 if (bitmap_type != obj->type)
2110 die(_("object '%s': real type '%s', expected: '%s'"),
2111 oid_to_hex(&obj->oid),
2112 type_name(obj->type),
2113 type_name(bitmap_type));
2116 static void test_show_object(struct object *object,
2117 const char *name UNUSED,
2118 void *data)
2120 struct bitmap_test_data *tdata = data;
2121 int bitmap_pos;
2123 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2124 if (bitmap_pos < 0)
2125 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2126 test_bitmap_type(tdata, object, bitmap_pos);
2128 bitmap_set(tdata->base, bitmap_pos);
2129 display_progress(tdata->prg, ++tdata->seen);
2132 static void test_show_commit(struct commit *commit, void *data)
2134 struct bitmap_test_data *tdata = data;
2135 int bitmap_pos;
2137 bitmap_pos = bitmap_position(tdata->bitmap_git,
2138 &commit->object.oid);
2139 if (bitmap_pos < 0)
2140 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2141 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2143 bitmap_set(tdata->base, bitmap_pos);
2144 display_progress(tdata->prg, ++tdata->seen);
2147 void test_bitmap_walk(struct rev_info *revs)
2149 struct object *root;
2150 struct bitmap *result = NULL;
2151 size_t result_popcnt;
2152 struct bitmap_test_data tdata;
2153 struct bitmap_index *bitmap_git;
2154 struct ewah_bitmap *bm;
2156 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2157 die(_("failed to load bitmap indexes"));
2159 if (revs->pending.nr != 1)
2160 die(_("you must specify exactly one commit to test"));
2162 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2163 bitmap_git->version,
2164 bitmap_git->entry_count,
2165 bitmap_git->table_lookup ? "" : " loaded");
2167 root = revs->pending.objects[0].item;
2168 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2170 if (bm) {
2171 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2172 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2174 result = ewah_to_bitmap(bm);
2177 if (!result)
2178 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2180 revs->tag_objects = 1;
2181 revs->tree_objects = 1;
2182 revs->blob_objects = 1;
2184 result_popcnt = bitmap_popcount(result);
2186 if (prepare_revision_walk(revs))
2187 die(_("revision walk setup failed"));
2189 tdata.bitmap_git = bitmap_git;
2190 tdata.base = bitmap_new();
2191 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2192 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2193 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2194 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2195 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2196 tdata.seen = 0;
2198 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2200 stop_progress(&tdata.prg);
2202 if (bitmap_equals(result, tdata.base))
2203 fprintf_ln(stderr, "OK!");
2204 else
2205 die(_("mismatch in bitmap results"));
2207 bitmap_free(result);
2208 bitmap_free(tdata.base);
2209 bitmap_free(tdata.commits);
2210 bitmap_free(tdata.trees);
2211 bitmap_free(tdata.blobs);
2212 bitmap_free(tdata.tags);
2213 free_bitmap_index(bitmap_git);
2216 int test_bitmap_commits(struct repository *r)
2218 struct object_id oid;
2219 MAYBE_UNUSED void *value;
2220 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2222 if (!bitmap_git)
2223 die(_("failed to load bitmap indexes"));
2226 * As this function is only used to print bitmap selected
2227 * commits, we don't have to read the commit table.
2229 if (bitmap_git->table_lookup) {
2230 if (load_bitmap_entries_v1(bitmap_git) < 0)
2231 die(_("failed to load bitmap indexes"));
2234 kh_foreach(bitmap_git->bitmaps, oid, value, {
2235 printf_ln("%s", oid_to_hex(&oid));
2238 free_bitmap_index(bitmap_git);
2240 return 0;
2243 int test_bitmap_hashes(struct repository *r)
2245 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2246 struct object_id oid;
2247 uint32_t i, index_pos;
2249 if (!bitmap_git || !bitmap_git->hashes)
2250 goto cleanup;
2252 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2253 if (bitmap_is_midx(bitmap_git))
2254 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2255 else
2256 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2258 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2260 printf_ln("%s %"PRIu32"",
2261 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2264 cleanup:
2265 free_bitmap_index(bitmap_git);
2267 return 0;
2270 int rebuild_bitmap(const uint32_t *reposition,
2271 struct ewah_bitmap *source,
2272 struct bitmap *dest)
2274 uint32_t pos = 0;
2275 struct ewah_iterator it;
2276 eword_t word;
2278 ewah_iterator_init(&it, source);
2280 while (ewah_iterator_next(&word, &it)) {
2281 uint32_t offset, bit_pos;
2283 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2284 if ((word >> offset) == 0)
2285 break;
2287 offset += ewah_bit_ctz64(word >> offset);
2289 bit_pos = reposition[pos + offset];
2290 if (bit_pos > 0)
2291 bitmap_set(dest, bit_pos - 1);
2292 else /* can't reuse, we don't have the object */
2293 return -1;
2296 pos += BITS_IN_EWORD;
2298 return 0;
2301 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2302 struct packing_data *mapping)
2304 struct repository *r = the_repository;
2305 uint32_t i, num_objects;
2306 uint32_t *reposition;
2308 if (!bitmap_is_midx(bitmap_git))
2309 load_reverse_index(r, bitmap_git);
2310 else if (load_midx_revindex(bitmap_git->midx))
2311 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2312 "extension");
2314 num_objects = bitmap_num_objects(bitmap_git);
2315 CALLOC_ARRAY(reposition, num_objects);
2317 for (i = 0; i < num_objects; ++i) {
2318 struct object_id oid;
2319 struct object_entry *oe;
2320 uint32_t index_pos;
2322 if (bitmap_is_midx(bitmap_git))
2323 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2324 else
2325 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2326 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2327 oe = packlist_find(mapping, &oid);
2329 if (oe) {
2330 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2331 if (bitmap_git->hashes && !oe->hash)
2332 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2336 return reposition;
2339 void free_bitmap_index(struct bitmap_index *b)
2341 if (!b)
2342 return;
2344 if (b->map)
2345 munmap(b->map, b->map_size);
2346 ewah_pool_free(b->commits);
2347 ewah_pool_free(b->trees);
2348 ewah_pool_free(b->blobs);
2349 ewah_pool_free(b->tags);
2350 if (b->bitmaps) {
2351 struct stored_bitmap *sb;
2352 kh_foreach_value(b->bitmaps, sb, {
2353 ewah_pool_free(sb->root);
2354 free(sb);
2357 kh_destroy_oid_map(b->bitmaps);
2358 free(b->ext_index.objects);
2359 free(b->ext_index.hashes);
2360 kh_destroy_oid_pos(b->ext_index.positions);
2361 bitmap_free(b->result);
2362 bitmap_free(b->haves);
2363 if (bitmap_is_midx(b)) {
2365 * Multi-pack bitmaps need to have resources associated with
2366 * their on-disk reverse indexes unmapped so that stale .rev and
2367 * .bitmap files can be removed.
2369 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2370 * written in the same 'git multi-pack-index write --bitmap'
2371 * process. Close resources so they can be removed safely on
2372 * platforms like Windows.
2374 close_midx_revindex(b->midx);
2376 free(b);
2379 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2380 const struct object_id *oid)
2382 return bitmap_git &&
2383 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2386 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2387 enum object_type object_type)
2389 struct bitmap *result = bitmap_git->result;
2390 off_t total = 0;
2391 struct ewah_iterator it;
2392 eword_t filter;
2393 size_t i;
2395 init_type_iterator(&it, bitmap_git, object_type);
2396 for (i = 0; i < result->word_alloc &&
2397 ewah_iterator_next(&filter, &it); i++) {
2398 eword_t word = result->words[i] & filter;
2399 size_t base = (i * BITS_IN_EWORD);
2400 unsigned offset;
2402 if (!word)
2403 continue;
2405 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2406 if ((word >> offset) == 0)
2407 break;
2409 offset += ewah_bit_ctz64(word >> offset);
2411 if (bitmap_is_midx(bitmap_git)) {
2412 uint32_t pack_pos;
2413 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2414 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2416 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2417 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2419 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2420 struct object_id oid;
2421 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2423 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2424 oid_to_hex(&oid),
2425 pack->pack_name,
2426 (uintmax_t)offset);
2429 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2430 } else {
2431 size_t pos = base + offset;
2432 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2433 pack_pos_to_offset(bitmap_git->pack, pos);
2438 return total;
2441 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2443 struct bitmap *result = bitmap_git->result;
2444 struct eindex *eindex = &bitmap_git->ext_index;
2445 off_t total = 0;
2446 struct object_info oi = OBJECT_INFO_INIT;
2447 off_t object_size;
2448 size_t i;
2450 oi.disk_sizep = &object_size;
2452 for (i = 0; i < eindex->count; i++) {
2453 struct object *obj = eindex->objects[i];
2455 if (!bitmap_get(result,
2456 st_add(bitmap_num_objects(bitmap_git), i)))
2457 continue;
2459 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2460 die(_("unable to get disk usage of '%s'"),
2461 oid_to_hex(&obj->oid));
2463 total += object_size;
2465 return total;
2468 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2469 struct rev_info *revs)
2471 off_t total = 0;
2473 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2474 if (revs->tree_objects)
2475 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2476 if (revs->blob_objects)
2477 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2478 if (revs->tag_objects)
2479 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2481 total += get_disk_usage_for_extended(bitmap_git);
2483 return total;
2486 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2488 return !!bitmap_git->midx;
2491 const struct string_list *bitmap_preferred_tips(struct repository *r)
2493 const struct string_list *dest;
2495 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2496 return dest;
2497 return NULL;
2500 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2502 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2503 struct string_list_item *item;
2505 if (!preferred_tips)
2506 return 0;
2508 for_each_string_list_item(item, preferred_tips) {
2509 if (starts_with(refname, item->string))
2510 return 1;
2513 return 0;
2516 static int verify_bitmap_file(const char *name)
2518 struct stat st;
2519 unsigned char *data;
2520 int fd = git_open(name);
2521 int res = 0;
2523 /* It is OK to not have the file. */
2524 if (fd < 0 || fstat(fd, &st)) {
2525 if (fd >= 0)
2526 close(fd);
2527 return 0;
2530 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2531 close(fd);
2532 if (!hashfile_checksum_valid(data, st.st_size))
2533 res = error(_("bitmap file '%s' has invalid checksum"),
2534 name);
2536 munmap(data, st.st_size);
2537 return res;
2540 int verify_bitmap_files(struct repository *r)
2542 int res = 0;
2544 for (struct multi_pack_index *m = get_multi_pack_index(r);
2545 m; m = m->next) {
2546 char *midx_bitmap_name = midx_bitmap_filename(m);
2547 res |= verify_bitmap_file(midx_bitmap_name);
2548 free(midx_bitmap_name);
2551 for (struct packed_git *p = get_all_packs(r);
2552 p; p = p->next) {
2553 char *pack_bitmap_name = pack_bitmap_filename(p);
2554 res |= verify_bitmap_file(pack_bitmap_name);
2555 free(pack_bitmap_name);
2558 return res;