Merge branch 'tb/pack-bitmap-drop-unused-struct-member' into maint-2.43
[git.git] / pack-bitmap.c
blobc88dd35bdc57f7cb2dcda13f14849b4c15e159c2
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;
54 /* mmapped buffer of the whole bitmap index */
55 unsigned char *map;
56 size_t map_size; /* size of the mmaped buffer */
57 size_t map_pos; /* current position when loading the index */
60 * Type indexes.
62 * Each bitmap marks which objects in the packfile are of the given
63 * type. This provides type information when yielding the objects from
64 * the packfile during a walk, which allows for better delta bases.
66 struct ewah_bitmap *commits;
67 struct ewah_bitmap *trees;
68 struct ewah_bitmap *blobs;
69 struct ewah_bitmap *tags;
71 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
72 kh_oid_map_t *bitmaps;
74 /* Number of bitmapped commits */
75 uint32_t entry_count;
77 /* If not NULL, this is a name-hash cache pointing into map. */
78 uint32_t *hashes;
80 /* The checksum of the packfile or MIDX; points into map. */
81 const unsigned char *checksum;
84 * If not NULL, this point into the commit table extension
85 * (within the memory mapped region `map`).
87 unsigned char *table_lookup;
90 * Extended index.
92 * When trying to perform bitmap operations with objects that are not
93 * packed in `pack`, these objects are added to this "fake index" and
94 * are assumed to appear at the end of the packfile for all operations
96 struct eindex {
97 struct object **objects;
98 uint32_t *hashes;
99 uint32_t count, alloc;
100 kh_oid_pos_t *positions;
101 } ext_index;
103 /* Bitmap result of the last performed walk */
104 struct bitmap *result;
106 /* "have" bitmap from the last performed walk */
107 struct bitmap *haves;
109 /* Version of the bitmap index */
110 unsigned int version;
113 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
115 struct ewah_bitmap *parent;
116 struct ewah_bitmap *composed;
118 if (!st->xor)
119 return st->root;
121 composed = ewah_pool_new();
122 parent = lookup_stored_bitmap(st->xor);
123 ewah_xor(st->root, parent, composed);
125 ewah_pool_free(st->root);
126 st->root = composed;
127 st->xor = NULL;
129 return composed;
133 * Read a bitmap from the current read position on the mmaped
134 * index, and increase the read position accordingly
136 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
138 struct ewah_bitmap *b = ewah_pool_new();
140 ssize_t bitmap_size = ewah_read_mmap(b,
141 index->map + index->map_pos,
142 index->map_size - index->map_pos);
144 if (bitmap_size < 0) {
145 error(_("failed to load bitmap index (corrupted?)"));
146 ewah_pool_free(b);
147 return NULL;
150 index->map_pos += bitmap_size;
151 return b;
154 static uint32_t bitmap_num_objects(struct bitmap_index *index)
156 if (index->midx)
157 return index->midx->num_objects;
158 return index->pack->num_objects;
161 static int load_bitmap_header(struct bitmap_index *index)
163 struct bitmap_disk_header *header = (void *)index->map;
164 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
166 if (index->map_size < header_size + the_hash_algo->rawsz)
167 return error(_("corrupted bitmap index (too small)"));
169 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
170 return error(_("corrupted bitmap index file (wrong header)"));
172 index->version = ntohs(header->version);
173 if (index->version != 1)
174 return error(_("unsupported version '%d' for bitmap index file"), index->version);
176 /* Parse known bitmap format options */
178 uint32_t flags = ntohs(header->options);
179 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
180 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
182 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
183 BUG("unsupported options for bitmap index file "
184 "(Git requires BITMAP_OPT_FULL_DAG)");
186 if (flags & BITMAP_OPT_HASH_CACHE) {
187 if (cache_size > index_end - index->map - header_size)
188 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
189 index->hashes = (void *)(index_end - cache_size);
190 index_end -= cache_size;
193 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
194 size_t table_size = st_mult(ntohl(header->entry_count),
195 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
196 if (table_size > index_end - index->map - header_size)
197 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
198 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
199 index->table_lookup = (void *)(index_end - table_size);
200 index_end -= table_size;
204 index->entry_count = ntohl(header->entry_count);
205 index->checksum = header->checksum;
206 index->map_pos += header_size;
207 return 0;
210 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
211 struct ewah_bitmap *root,
212 const struct object_id *oid,
213 struct stored_bitmap *xor_with,
214 int flags)
216 struct stored_bitmap *stored;
217 khiter_t hash_pos;
218 int ret;
220 stored = xmalloc(sizeof(struct stored_bitmap));
221 stored->root = root;
222 stored->xor = xor_with;
223 stored->flags = flags;
224 oidcpy(&stored->oid, oid);
226 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
229 * A 0 return code means the insertion succeeded with no changes,
230 * because the SHA1 already existed on the map. This is bad, there
231 * shouldn't be duplicated commits in the index.
233 if (ret == 0) {
234 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
235 return NULL;
238 kh_value(index->bitmaps, hash_pos) = stored;
239 return stored;
242 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
244 uint32_t result = get_be32(buffer + *pos);
245 (*pos) += sizeof(result);
246 return result;
249 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
251 return buffer[(*pos)++];
254 #define MAX_XOR_OFFSET 160
256 static int nth_bitmap_object_oid(struct bitmap_index *index,
257 struct object_id *oid,
258 uint32_t n)
260 if (index->midx)
261 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
262 return nth_packed_object_id(oid, index->pack, n);
265 static int load_bitmap_entries_v1(struct bitmap_index *index)
267 uint32_t i;
268 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
270 for (i = 0; i < index->entry_count; ++i) {
271 int xor_offset, flags;
272 struct ewah_bitmap *bitmap = NULL;
273 struct stored_bitmap *xor_bitmap = NULL;
274 uint32_t commit_idx_pos;
275 struct object_id oid;
277 if (index->map_size - index->map_pos < 6)
278 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
280 commit_idx_pos = read_be32(index->map, &index->map_pos);
281 xor_offset = read_u8(index->map, &index->map_pos);
282 flags = read_u8(index->map, &index->map_pos);
284 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
285 return error(_("corrupt ewah bitmap: commit index %u out of range"),
286 (unsigned)commit_idx_pos);
288 bitmap = read_bitmap_1(index);
289 if (!bitmap)
290 return -1;
292 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
293 return error(_("corrupted bitmap pack index"));
295 if (xor_offset > 0) {
296 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
298 if (!xor_bitmap)
299 return error(_("invalid XOR offset in bitmap pack index"));
302 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
303 index, bitmap, &oid, xor_bitmap, flags);
306 return 0;
309 char *midx_bitmap_filename(struct multi_pack_index *midx)
311 struct strbuf buf = STRBUF_INIT;
313 get_midx_filename(&buf, midx->object_dir);
314 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
316 return strbuf_detach(&buf, NULL);
319 char *pack_bitmap_filename(struct packed_git *p)
321 size_t len;
323 if (!strip_suffix(p->pack_name, ".pack", &len))
324 BUG("pack_name does not end in .pack");
325 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
328 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
329 struct multi_pack_index *midx)
331 struct stat st;
332 char *bitmap_name = midx_bitmap_filename(midx);
333 int fd = git_open(bitmap_name);
334 uint32_t i;
335 struct packed_git *preferred;
337 if (fd < 0) {
338 if (errno != ENOENT)
339 warning_errno("cannot open '%s'", bitmap_name);
340 free(bitmap_name);
341 return -1;
343 free(bitmap_name);
345 if (fstat(fd, &st)) {
346 error_errno(_("cannot fstat bitmap file"));
347 close(fd);
348 return -1;
351 if (bitmap_git->pack || bitmap_git->midx) {
352 struct strbuf buf = STRBUF_INIT;
353 get_midx_filename(&buf, midx->object_dir);
354 trace2_data_string("bitmap", the_repository,
355 "ignoring extra midx bitmap file", buf.buf);
356 close(fd);
357 strbuf_release(&buf);
358 return -1;
361 bitmap_git->midx = midx;
362 bitmap_git->map_size = xsize_t(st.st_size);
363 bitmap_git->map_pos = 0;
364 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
365 MAP_PRIVATE, fd, 0);
366 close(fd);
368 if (load_bitmap_header(bitmap_git) < 0)
369 goto cleanup;
371 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
372 error(_("checksum doesn't match in MIDX and bitmap"));
373 goto cleanup;
376 if (load_midx_revindex(bitmap_git->midx)) {
377 warning(_("multi-pack bitmap is missing required reverse index"));
378 goto cleanup;
381 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
382 if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
383 warning(_("could not open pack %s"),
384 bitmap_git->midx->pack_names[i]);
385 goto cleanup;
389 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
390 if (!is_pack_valid(preferred)) {
391 warning(_("preferred pack (%s) is invalid"),
392 preferred->pack_name);
393 goto cleanup;
396 return 0;
398 cleanup:
399 munmap(bitmap_git->map, bitmap_git->map_size);
400 bitmap_git->map_size = 0;
401 bitmap_git->map_pos = 0;
402 bitmap_git->map = NULL;
403 bitmap_git->midx = NULL;
404 return -1;
407 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
409 int fd;
410 struct stat st;
411 char *bitmap_name;
413 bitmap_name = pack_bitmap_filename(packfile);
414 fd = git_open(bitmap_name);
416 if (fd < 0) {
417 if (errno != ENOENT)
418 warning_errno("cannot open '%s'", bitmap_name);
419 free(bitmap_name);
420 return -1;
422 free(bitmap_name);
424 if (fstat(fd, &st)) {
425 error_errno(_("cannot fstat bitmap file"));
426 close(fd);
427 return -1;
430 if (bitmap_git->pack || bitmap_git->midx) {
431 trace2_data_string("bitmap", the_repository,
432 "ignoring extra bitmap file", packfile->pack_name);
433 close(fd);
434 return -1;
437 if (!is_pack_valid(packfile)) {
438 close(fd);
439 return -1;
442 bitmap_git->pack = packfile;
443 bitmap_git->map_size = xsize_t(st.st_size);
444 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
445 bitmap_git->map_pos = 0;
446 close(fd);
448 if (load_bitmap_header(bitmap_git) < 0) {
449 munmap(bitmap_git->map, bitmap_git->map_size);
450 bitmap_git->map = NULL;
451 bitmap_git->map_size = 0;
452 bitmap_git->map_pos = 0;
453 bitmap_git->pack = NULL;
454 return -1;
457 trace2_data_string("bitmap", the_repository, "opened bitmap file",
458 packfile->pack_name);
459 return 0;
462 static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
464 if (bitmap_is_midx(bitmap_git)) {
465 uint32_t i;
466 int ret;
469 * The multi-pack-index's .rev file is already loaded via
470 * open_pack_bitmap_1().
472 * But we still need to open the individual pack .rev files,
473 * since we will need to make use of them in pack-objects.
475 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
476 ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
477 if (ret)
478 return ret;
480 return 0;
482 return load_pack_revindex(r, bitmap_git->pack);
485 static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
487 assert(bitmap_git->map);
489 bitmap_git->bitmaps = kh_init_oid_map();
490 bitmap_git->ext_index.positions = kh_init_oid_pos();
492 if (load_reverse_index(r, bitmap_git))
493 goto failed;
495 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
496 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
497 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
498 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
499 goto failed;
501 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
502 goto failed;
504 return 0;
506 failed:
507 munmap(bitmap_git->map, bitmap_git->map_size);
508 bitmap_git->map = NULL;
509 bitmap_git->map_size = 0;
511 kh_destroy_oid_map(bitmap_git->bitmaps);
512 bitmap_git->bitmaps = NULL;
514 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
515 bitmap_git->ext_index.positions = NULL;
517 return -1;
520 static int open_pack_bitmap(struct repository *r,
521 struct bitmap_index *bitmap_git)
523 struct packed_git *p;
524 int ret = -1;
526 for (p = get_all_packs(r); p; p = p->next) {
527 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
528 ret = 0;
530 * The only reason to keep looking is to report
531 * duplicates.
533 if (!trace2_is_enabled())
534 break;
538 return ret;
541 static int open_midx_bitmap(struct repository *r,
542 struct bitmap_index *bitmap_git)
544 int ret = -1;
545 struct multi_pack_index *midx;
547 assert(!bitmap_git->map);
549 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
550 if (!open_midx_bitmap_1(bitmap_git, midx))
551 ret = 0;
553 return ret;
556 static int open_bitmap(struct repository *r,
557 struct bitmap_index *bitmap_git)
559 int found;
561 assert(!bitmap_git->map);
563 found = !open_midx_bitmap(r, bitmap_git);
566 * these will all be skipped if we opened a midx bitmap; but run it
567 * anyway if tracing is enabled to report the duplicates
569 if (!found || trace2_is_enabled())
570 found |= !open_pack_bitmap(r, bitmap_git);
572 return found ? 0 : -1;
575 struct bitmap_index *prepare_bitmap_git(struct repository *r)
577 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
579 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
580 return bitmap_git;
582 free_bitmap_index(bitmap_git);
583 return NULL;
586 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
588 struct repository *r = the_repository;
589 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
591 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
592 return bitmap_git;
594 free_bitmap_index(bitmap_git);
595 return NULL;
598 struct include_data {
599 struct bitmap_index *bitmap_git;
600 struct bitmap *base;
601 struct bitmap *seen;
604 struct bitmap_lookup_table_triplet {
605 uint32_t commit_pos;
606 uint64_t offset;
607 uint32_t xor_row;
610 struct bitmap_lookup_table_xor_item {
611 struct object_id oid;
612 uint64_t offset;
616 * Given a `triplet` struct pointer and pointer `p`, this
617 * function reads the triplet beginning at `p` into the struct.
618 * Note that this function assumes that there is enough memory
619 * left for filling the `triplet` struct from `p`.
621 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
622 const unsigned char *p)
624 if (!triplet)
625 return -1;
627 triplet->commit_pos = get_be32(p);
628 p += sizeof(uint32_t);
629 triplet->offset = get_be64(p);
630 p += sizeof(uint64_t);
631 triplet->xor_row = get_be32(p);
632 return 0;
636 * This function gets the raw triplet from `row`'th row in the
637 * lookup table and fills that data to the `triplet`.
639 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
640 uint32_t pos,
641 struct bitmap_lookup_table_triplet *triplet)
643 unsigned char *p = NULL;
644 if (pos >= bitmap_git->entry_count)
645 return error(_("corrupt bitmap lookup table: triplet position out of index"));
647 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
649 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
653 * Searches for a matching triplet. `commit_pos` is a pointer
654 * to the wanted commit position value. `table_entry` points to
655 * a triplet in lookup table. The first 4 bytes of each
656 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
658 static int triplet_cmp(const void *commit_pos, const void *table_entry)
661 uint32_t a = *(uint32_t *)commit_pos;
662 uint32_t b = get_be32(table_entry);
663 if (a > b)
664 return 1;
665 else if (a < b)
666 return -1;
668 return 0;
671 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
672 struct object_id *oid,
673 uint32_t *result)
675 int found;
677 if (bitmap_is_midx(bitmap_git))
678 found = bsearch_midx(oid, bitmap_git->midx, result);
679 else
680 found = bsearch_pack(oid, bitmap_git->pack, result);
682 return found;
686 * `bsearch_triplet_by_pos` function searches for the raw triplet
687 * having commit position same as `commit_pos` and fills `triplet`
688 * object from the raw triplet. Returns 1 on success and 0 on
689 * failure.
691 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
692 struct bitmap_index *bitmap_git,
693 struct bitmap_lookup_table_triplet *triplet)
695 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
696 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
698 if (!p)
699 return -1;
701 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
704 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
705 struct commit *commit)
707 uint32_t commit_pos, xor_row;
708 uint64_t offset;
709 int flags;
710 struct bitmap_lookup_table_triplet triplet;
711 struct object_id *oid = &commit->object.oid;
712 struct ewah_bitmap *bitmap;
713 struct stored_bitmap *xor_bitmap = NULL;
714 const int bitmap_header_size = 6;
715 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
716 static size_t xor_items_nr = 0, xor_items_alloc = 0;
717 static int is_corrupt = 0;
718 int xor_flags;
719 khiter_t hash_pos;
720 struct bitmap_lookup_table_xor_item *xor_item;
722 if (is_corrupt)
723 return NULL;
725 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
726 return NULL;
728 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
729 return NULL;
731 xor_items_nr = 0;
732 offset = triplet.offset;
733 xor_row = triplet.xor_row;
735 while (xor_row != 0xffffffff) {
736 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
738 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
739 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
740 goto corrupt;
743 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
744 goto corrupt;
746 xor_item = &xor_items[xor_items_nr];
747 xor_item->offset = triplet.offset;
749 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
750 error(_("corrupt bitmap lookup table: commit index %u out of range"),
751 triplet.commit_pos);
752 goto corrupt;
755 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
758 * If desired bitmap is already stored, we don't need
759 * to iterate further. Because we know that bitmaps
760 * that are needed to be parsed to parse this bitmap
761 * has already been stored. So, assign this stored bitmap
762 * to the xor_bitmap.
764 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
765 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
766 break;
767 xor_items_nr++;
768 xor_row = triplet.xor_row;
771 while (xor_items_nr) {
772 xor_item = &xor_items[xor_items_nr - 1];
773 bitmap_git->map_pos = xor_item->offset;
774 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
775 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
776 oid_to_hex(&xor_item->oid));
777 goto corrupt;
780 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
781 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
782 bitmap = read_bitmap_1(bitmap_git);
784 if (!bitmap)
785 goto corrupt;
787 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
788 xor_items_nr--;
791 bitmap_git->map_pos = offset;
792 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
793 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
794 oid_to_hex(oid));
795 goto corrupt;
799 * Don't bother reading the commit's index position or its xor
800 * offset:
802 * - The commit's index position is irrelevant to us, since
803 * load_bitmap_entries_v1 only uses it to learn the object
804 * id which is used to compute the hashmap's key. We already
805 * have an object id, so no need to look it up again.
807 * - The xor_offset is unusable for us, since it specifies how
808 * many entries previous to ours we should look at. This
809 * makes sense when reading the bitmaps sequentially (as in
810 * load_bitmap_entries_v1()), since we can keep track of
811 * each bitmap as we read them.
813 * But it can't work for us, since the bitmap's don't have a
814 * fixed size. So we learn the position of the xor'd bitmap
815 * from the commit table (and resolve it to a bitmap in the
816 * above if-statement).
818 * Instead, we can skip ahead and immediately read the flags and
819 * ewah bitmap.
821 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
822 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
823 bitmap = read_bitmap_1(bitmap_git);
825 if (!bitmap)
826 goto corrupt;
828 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
830 corrupt:
831 free(xor_items);
832 is_corrupt = 1;
833 return NULL;
836 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
837 struct commit *commit)
839 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
840 commit->object.oid);
841 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
842 struct stored_bitmap *bitmap = NULL;
843 if (!bitmap_git->table_lookup)
844 return NULL;
846 /* this is a fairly hot codepath - no trace2_region please */
847 /* NEEDSWORK: cache misses aren't recorded */
848 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
849 if (!bitmap)
850 return NULL;
851 return lookup_stored_bitmap(bitmap);
853 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
856 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
857 const struct object_id *oid)
859 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
860 khiter_t pos = kh_get_oid_pos(positions, *oid);
862 if (pos < kh_end(positions)) {
863 int bitmap_pos = kh_value(positions, pos);
864 return bitmap_pos + bitmap_num_objects(bitmap_git);
867 return -1;
870 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
871 const struct object_id *oid)
873 uint32_t pos;
874 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
875 if (!offset)
876 return -1;
878 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
879 return -1;
880 return pos;
883 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
884 const struct object_id *oid)
886 uint32_t want, got;
887 if (!bsearch_midx(oid, bitmap_git->midx, &want))
888 return -1;
890 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
891 return -1;
892 return got;
895 static int bitmap_position(struct bitmap_index *bitmap_git,
896 const struct object_id *oid)
898 int pos;
899 if (bitmap_is_midx(bitmap_git))
900 pos = bitmap_position_midx(bitmap_git, oid);
901 else
902 pos = bitmap_position_packfile(bitmap_git, oid);
903 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
906 static int ext_index_add_object(struct bitmap_index *bitmap_git,
907 struct object *object, const char *name)
909 struct eindex *eindex = &bitmap_git->ext_index;
911 khiter_t hash_pos;
912 int hash_ret;
913 int bitmap_pos;
915 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
916 if (hash_ret > 0) {
917 if (eindex->count >= eindex->alloc) {
918 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
919 REALLOC_ARRAY(eindex->objects, eindex->alloc);
920 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
923 bitmap_pos = eindex->count;
924 eindex->objects[eindex->count] = object;
925 eindex->hashes[eindex->count] = pack_name_hash(name);
926 kh_value(eindex->positions, hash_pos) = bitmap_pos;
927 eindex->count++;
928 } else {
929 bitmap_pos = kh_value(eindex->positions, hash_pos);
932 return bitmap_pos + bitmap_num_objects(bitmap_git);
935 struct bitmap_show_data {
936 struct bitmap_index *bitmap_git;
937 struct bitmap *base;
940 static void show_object(struct object *object, const char *name, void *data_)
942 struct bitmap_show_data *data = data_;
943 int bitmap_pos;
945 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
947 if (bitmap_pos < 0)
948 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
949 name);
951 bitmap_set(data->base, bitmap_pos);
954 static void show_commit(struct commit *commit UNUSED,
955 void *data UNUSED)
959 static int add_to_include_set(struct bitmap_index *bitmap_git,
960 struct include_data *data,
961 struct commit *commit,
962 int bitmap_pos)
964 struct ewah_bitmap *partial;
966 if (data->seen && bitmap_get(data->seen, bitmap_pos))
967 return 0;
969 if (bitmap_get(data->base, bitmap_pos))
970 return 0;
972 partial = bitmap_for_commit(bitmap_git, commit);
973 if (partial) {
974 bitmap_or_ewah(data->base, partial);
975 return 0;
978 bitmap_set(data->base, bitmap_pos);
979 return 1;
982 static int should_include(struct commit *commit, void *_data)
984 struct include_data *data = _data;
985 int bitmap_pos;
987 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
988 if (bitmap_pos < 0)
989 bitmap_pos = ext_index_add_object(data->bitmap_git,
990 (struct object *)commit,
991 NULL);
993 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
994 struct commit_list *parent = commit->parents;
996 while (parent) {
997 parent->item->object.flags |= SEEN;
998 parent = parent->next;
1001 return 0;
1004 return 1;
1007 static int should_include_obj(struct object *obj, void *_data)
1009 struct include_data *data = _data;
1010 int bitmap_pos;
1012 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1013 if (bitmap_pos < 0)
1014 return 1;
1015 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1016 bitmap_get(data->base, bitmap_pos)) {
1017 obj->flags |= SEEN;
1018 return 0;
1020 return 1;
1023 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1024 struct bitmap **base,
1025 struct commit *commit)
1027 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1029 if (!or_with)
1030 return 0;
1032 if (!*base)
1033 *base = ewah_to_bitmap(or_with);
1034 else
1035 bitmap_or_ewah(*base, or_with);
1037 return 1;
1040 static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1041 struct rev_info *revs,
1042 struct bitmap *base,
1043 struct bitmap *seen)
1045 struct include_data incdata;
1046 struct bitmap_show_data show_data;
1048 if (!base)
1049 base = bitmap_new();
1051 incdata.bitmap_git = bitmap_git;
1052 incdata.base = base;
1053 incdata.seen = seen;
1055 revs->include_check = should_include;
1056 revs->include_check_obj = should_include_obj;
1057 revs->include_check_data = &incdata;
1059 if (prepare_revision_walk(revs))
1060 die(_("revision walk setup failed"));
1062 show_data.bitmap_git = bitmap_git;
1063 show_data.base = base;
1065 traverse_commit_list(revs, show_commit, show_object, &show_data);
1067 revs->include_check = NULL;
1068 revs->include_check_obj = NULL;
1069 revs->include_check_data = NULL;
1071 return base;
1074 struct bitmap_boundary_cb {
1075 struct bitmap_index *bitmap_git;
1076 struct bitmap *base;
1078 struct object_array boundary;
1081 static void show_boundary_commit(struct commit *commit, void *_data)
1083 struct bitmap_boundary_cb *data = _data;
1085 if (commit->object.flags & BOUNDARY)
1086 add_object_array(&commit->object, "", &data->boundary);
1088 if (commit->object.flags & UNINTERESTING) {
1089 if (bitmap_walk_contains(data->bitmap_git, data->base,
1090 &commit->object.oid))
1091 return;
1093 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1097 static void show_boundary_object(struct object *object UNUSED,
1098 const char *name UNUSED,
1099 void *data UNUSED)
1101 BUG("should not be called");
1104 static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1105 struct rev_info *revs,
1106 struct object_list *roots)
1108 struct bitmap_boundary_cb cb;
1109 struct object_list *root;
1110 unsigned int i;
1111 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1112 int any_missing = 0;
1114 cb.bitmap_git = bitmap_git;
1115 cb.base = bitmap_new();
1116 object_array_init(&cb.boundary);
1118 revs->ignore_missing_links = 1;
1121 * OR in any existing reachability bitmaps among `roots` into
1122 * `cb.base`.
1124 for (root = roots; root; root = root->next) {
1125 struct object *object = root->item;
1126 if (object->type != OBJ_COMMIT ||
1127 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1128 continue;
1130 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1131 (struct commit *)object))
1132 continue;
1134 any_missing = 1;
1137 if (!any_missing)
1138 goto cleanup;
1140 tmp_blobs = revs->blob_objects;
1141 tmp_trees = revs->tree_objects;
1142 tmp_tags = revs->blob_objects;
1143 revs->blob_objects = 0;
1144 revs->tree_objects = 0;
1145 revs->tag_objects = 0;
1148 * We didn't have complete coverage of the roots. First setup a
1149 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1150 * between the tips and boundary, and (b) record the boundary.
1152 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1153 if (prepare_revision_walk(revs))
1154 die("revision walk setup failed");
1155 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1157 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1158 revs->boundary = 1;
1159 traverse_commit_list_filtered(revs,
1160 show_boundary_commit,
1161 show_boundary_object,
1162 &cb, NULL);
1163 revs->boundary = 0;
1164 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1166 revs->blob_objects = tmp_blobs;
1167 revs->tree_objects = tmp_trees;
1168 revs->tag_objects = tmp_tags;
1170 reset_revision_walk();
1171 clear_object_flags(UNINTERESTING);
1174 * Then add the boundary commit(s) as fill-in traversal tips.
1176 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1177 for (i = 0; i < cb.boundary.nr; i++) {
1178 struct object *obj = cb.boundary.objects[i].item;
1179 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1180 obj->flags |= SEEN;
1181 else
1182 add_pending_object(revs, obj, "");
1184 if (revs->pending.nr)
1185 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1186 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1188 cleanup:
1189 object_array_clear(&cb.boundary);
1190 revs->ignore_missing_links = 0;
1192 return cb.base;
1195 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1196 struct rev_info *revs,
1197 struct object_list *roots,
1198 struct bitmap *seen)
1200 struct bitmap *base = NULL;
1201 int needs_walk = 0;
1203 struct object_list *not_mapped = NULL;
1206 * Go through all the roots for the walk. The ones that have bitmaps
1207 * on the bitmap index will be `or`ed together to form an initial
1208 * global reachability analysis.
1210 * The ones without bitmaps in the index will be stored in the
1211 * `not_mapped_list` for further processing.
1213 while (roots) {
1214 struct object *object = roots->item;
1215 roots = roots->next;
1217 if (object->type == OBJ_COMMIT &&
1218 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1219 object->flags |= SEEN;
1220 continue;
1223 object_list_insert(object, &not_mapped);
1227 * Best case scenario: We found bitmaps for all the roots,
1228 * so the resulting `or` bitmap has the full reachability analysis
1230 if (!not_mapped)
1231 return base;
1233 roots = not_mapped;
1236 * Let's iterate through all the roots that don't have bitmaps to
1237 * check if we can determine them to be reachable from the existing
1238 * global bitmap.
1240 * If we cannot find them in the existing global bitmap, we'll need
1241 * to push them to an actual walk and run it until we can confirm
1242 * they are reachable
1244 while (roots) {
1245 struct object *object = roots->item;
1246 int pos;
1248 roots = roots->next;
1249 pos = bitmap_position(bitmap_git, &object->oid);
1251 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1252 object->flags &= ~UNINTERESTING;
1253 add_pending_object(revs, object, "");
1254 needs_walk = 1;
1255 } else {
1256 object->flags |= SEEN;
1260 if (needs_walk) {
1262 * This fill-in traversal may walk over some objects
1263 * again, since we have already traversed in order to
1264 * find the boundary.
1266 * But this extra walk should be extremely cheap, since
1267 * all commit objects are loaded into memory, and
1268 * because we skip walking to parents that are
1269 * UNINTERESTING, since it will be marked in the haves
1270 * bitmap already (or it has an on-disk bitmap, since
1271 * OR-ing it in covers all of its ancestors).
1273 base = fill_in_bitmap(bitmap_git, revs, base, seen);
1276 return base;
1279 static void show_extended_objects(struct bitmap_index *bitmap_git,
1280 struct rev_info *revs,
1281 show_reachable_fn show_reach)
1283 struct bitmap *objects = bitmap_git->result;
1284 struct eindex *eindex = &bitmap_git->ext_index;
1285 uint32_t i;
1287 for (i = 0; i < eindex->count; ++i) {
1288 struct object *obj;
1290 if (!bitmap_get(objects, st_add(bitmap_num_objects(bitmap_git), i)))
1291 continue;
1293 obj = eindex->objects[i];
1294 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1295 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1296 (obj->type == OBJ_TAG && !revs->tag_objects))
1297 continue;
1299 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1303 static void init_type_iterator(struct ewah_iterator *it,
1304 struct bitmap_index *bitmap_git,
1305 enum object_type type)
1307 switch (type) {
1308 case OBJ_COMMIT:
1309 ewah_iterator_init(it, bitmap_git->commits);
1310 break;
1312 case OBJ_TREE:
1313 ewah_iterator_init(it, bitmap_git->trees);
1314 break;
1316 case OBJ_BLOB:
1317 ewah_iterator_init(it, bitmap_git->blobs);
1318 break;
1320 case OBJ_TAG:
1321 ewah_iterator_init(it, bitmap_git->tags);
1322 break;
1324 default:
1325 BUG("object type %d not stored by bitmap type index", type);
1326 break;
1330 static void show_objects_for_type(
1331 struct bitmap_index *bitmap_git,
1332 enum object_type object_type,
1333 show_reachable_fn show_reach)
1335 size_t i = 0;
1336 uint32_t offset;
1338 struct ewah_iterator it;
1339 eword_t filter;
1341 struct bitmap *objects = bitmap_git->result;
1343 init_type_iterator(&it, bitmap_git, object_type);
1345 for (i = 0; i < objects->word_alloc &&
1346 ewah_iterator_next(&filter, &it); i++) {
1347 eword_t word = objects->words[i] & filter;
1348 size_t pos = (i * BITS_IN_EWORD);
1350 if (!word)
1351 continue;
1353 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1354 struct packed_git *pack;
1355 struct object_id oid;
1356 uint32_t hash = 0, index_pos;
1357 off_t ofs;
1359 if ((word >> offset) == 0)
1360 break;
1362 offset += ewah_bit_ctz64(word >> offset);
1364 if (bitmap_is_midx(bitmap_git)) {
1365 struct multi_pack_index *m = bitmap_git->midx;
1366 uint32_t pack_id;
1368 index_pos = pack_pos_to_midx(m, pos + offset);
1369 ofs = nth_midxed_offset(m, index_pos);
1370 nth_midxed_object_oid(&oid, m, index_pos);
1372 pack_id = nth_midxed_pack_int_id(m, index_pos);
1373 pack = bitmap_git->midx->packs[pack_id];
1374 } else {
1375 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1376 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1377 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1379 pack = bitmap_git->pack;
1382 if (bitmap_git->hashes)
1383 hash = get_be32(bitmap_git->hashes + index_pos);
1385 show_reach(&oid, object_type, 0, hash, pack, ofs);
1390 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1391 struct object_list *roots)
1393 while (roots) {
1394 struct object *object = roots->item;
1395 roots = roots->next;
1397 if (bitmap_is_midx(bitmap_git)) {
1398 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1399 return 1;
1400 } else {
1401 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1402 return 1;
1406 return 0;
1409 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1410 struct object_list *tip_objects,
1411 enum object_type type)
1413 struct bitmap *result = bitmap_new();
1414 struct object_list *p;
1416 for (p = tip_objects; p; p = p->next) {
1417 int pos;
1419 if (p->item->type != type)
1420 continue;
1422 pos = bitmap_position(bitmap_git, &p->item->oid);
1423 if (pos < 0)
1424 continue;
1426 bitmap_set(result, pos);
1429 return result;
1432 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1433 struct object_list *tip_objects,
1434 struct bitmap *to_filter,
1435 enum object_type type)
1437 struct eindex *eindex = &bitmap_git->ext_index;
1438 struct bitmap *tips;
1439 struct ewah_iterator it;
1440 eword_t mask;
1441 uint32_t i;
1444 * The non-bitmap version of this filter never removes
1445 * objects which the other side specifically asked for,
1446 * so we must match that behavior.
1448 tips = find_tip_objects(bitmap_git, tip_objects, type);
1451 * We can use the type-level bitmap for 'type' to work in whole
1452 * words for the objects that are actually in the bitmapped
1453 * packfile.
1455 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1456 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1457 i++) {
1458 if (i < tips->word_alloc)
1459 mask &= ~tips->words[i];
1460 to_filter->words[i] &= ~mask;
1464 * Clear any objects that weren't in the packfile (and so would
1465 * not have been caught by the loop above. We'll have to check
1466 * them individually.
1468 for (i = 0; i < eindex->count; i++) {
1469 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1470 if (eindex->objects[i]->type == type &&
1471 bitmap_get(to_filter, pos) &&
1472 !bitmap_get(tips, pos))
1473 bitmap_unset(to_filter, pos);
1476 bitmap_free(tips);
1479 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1480 struct object_list *tip_objects,
1481 struct bitmap *to_filter)
1483 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1484 OBJ_BLOB);
1487 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1488 uint32_t pos)
1490 unsigned long size;
1491 struct object_info oi = OBJECT_INFO_INIT;
1493 oi.sizep = &size;
1495 if (pos < bitmap_num_objects(bitmap_git)) {
1496 struct packed_git *pack;
1497 off_t ofs;
1499 if (bitmap_is_midx(bitmap_git)) {
1500 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1501 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1503 pack = bitmap_git->midx->packs[pack_id];
1504 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1505 } else {
1506 pack = bitmap_git->pack;
1507 ofs = pack_pos_to_offset(pack, pos);
1510 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1511 struct object_id oid;
1512 nth_bitmap_object_oid(bitmap_git, &oid,
1513 pack_pos_to_index(pack, pos));
1514 die(_("unable to get size of %s"), oid_to_hex(&oid));
1516 } else {
1517 struct eindex *eindex = &bitmap_git->ext_index;
1518 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1519 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1520 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1523 return size;
1526 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1527 struct object_list *tip_objects,
1528 struct bitmap *to_filter,
1529 unsigned long limit)
1531 struct eindex *eindex = &bitmap_git->ext_index;
1532 struct bitmap *tips;
1533 struct ewah_iterator it;
1534 eword_t mask;
1535 uint32_t i;
1537 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1539 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1540 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1541 i++) {
1542 eword_t word = to_filter->words[i] & mask;
1543 unsigned offset;
1545 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1546 uint32_t pos;
1548 if ((word >> offset) == 0)
1549 break;
1550 offset += ewah_bit_ctz64(word >> offset);
1551 pos = i * BITS_IN_EWORD + offset;
1553 if (!bitmap_get(tips, pos) &&
1554 get_size_by_pos(bitmap_git, pos) >= limit)
1555 bitmap_unset(to_filter, pos);
1559 for (i = 0; i < eindex->count; i++) {
1560 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1561 if (eindex->objects[i]->type == OBJ_BLOB &&
1562 bitmap_get(to_filter, pos) &&
1563 !bitmap_get(tips, pos) &&
1564 get_size_by_pos(bitmap_git, pos) >= limit)
1565 bitmap_unset(to_filter, pos);
1568 bitmap_free(tips);
1571 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1572 struct object_list *tip_objects,
1573 struct bitmap *to_filter,
1574 unsigned long limit)
1576 if (limit)
1577 BUG("filter_bitmap_tree_depth given non-zero limit");
1579 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1580 OBJ_TREE);
1581 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1582 OBJ_BLOB);
1585 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1586 struct object_list *tip_objects,
1587 struct bitmap *to_filter,
1588 enum object_type object_type)
1590 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1591 BUG("filter_bitmap_object_type given invalid object");
1593 if (object_type != OBJ_TAG)
1594 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1595 if (object_type != OBJ_COMMIT)
1596 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1597 if (object_type != OBJ_TREE)
1598 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1599 if (object_type != OBJ_BLOB)
1600 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1603 static int filter_bitmap(struct bitmap_index *bitmap_git,
1604 struct object_list *tip_objects,
1605 struct bitmap *to_filter,
1606 struct list_objects_filter_options *filter)
1608 if (!filter || filter->choice == LOFC_DISABLED)
1609 return 0;
1611 if (filter->choice == LOFC_BLOB_NONE) {
1612 if (bitmap_git)
1613 filter_bitmap_blob_none(bitmap_git, tip_objects,
1614 to_filter);
1615 return 0;
1618 if (filter->choice == LOFC_BLOB_LIMIT) {
1619 if (bitmap_git)
1620 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1621 to_filter,
1622 filter->blob_limit_value);
1623 return 0;
1626 if (filter->choice == LOFC_TREE_DEPTH &&
1627 filter->tree_exclude_depth == 0) {
1628 if (bitmap_git)
1629 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1630 to_filter,
1631 filter->tree_exclude_depth);
1632 return 0;
1635 if (filter->choice == LOFC_OBJECT_TYPE) {
1636 if (bitmap_git)
1637 filter_bitmap_object_type(bitmap_git, tip_objects,
1638 to_filter,
1639 filter->object_type);
1640 return 0;
1643 if (filter->choice == LOFC_COMBINE) {
1644 int i;
1645 for (i = 0; i < filter->sub_nr; i++) {
1646 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1647 &filter->sub[i]) < 0)
1648 return -1;
1650 return 0;
1653 /* filter choice not handled */
1654 return -1;
1657 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1659 return !filter_bitmap(NULL, NULL, NULL, filter);
1663 static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
1664 struct bitmap *result)
1666 struct eindex *eindex = &bitmap_git->ext_index;
1667 uint32_t objects_nr;
1668 size_t i, pos;
1670 objects_nr = bitmap_num_objects(bitmap_git);
1671 pos = objects_nr / BITS_IN_EWORD;
1673 if (pos > result->word_alloc)
1674 pos = result->word_alloc;
1676 memset(result->words, 0x00, sizeof(eword_t) * pos);
1677 for (i = pos * BITS_IN_EWORD; i < objects_nr; i++)
1678 bitmap_unset(result, i);
1680 for (i = 0; i < eindex->count; ++i) {
1681 if (has_object_pack(&eindex->objects[i]->oid))
1682 bitmap_unset(result, objects_nr + i);
1686 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1687 int filter_provided_objects)
1689 unsigned int i;
1690 int use_boundary_traversal;
1692 struct object_list *wants = NULL;
1693 struct object_list *haves = NULL;
1695 struct bitmap *wants_bitmap = NULL;
1696 struct bitmap *haves_bitmap = NULL;
1698 struct bitmap_index *bitmap_git;
1701 * We can't do pathspec limiting with bitmaps, because we don't know
1702 * which commits are associated with which object changes (let alone
1703 * even which objects are associated with which paths).
1705 if (revs->prune)
1706 return NULL;
1708 if (!can_filter_bitmap(&revs->filter))
1709 return NULL;
1711 /* try to open a bitmapped pack, but don't parse it yet
1712 * because we may not need to use it */
1713 CALLOC_ARRAY(bitmap_git, 1);
1714 if (open_bitmap(revs->repo, bitmap_git) < 0)
1715 goto cleanup;
1717 for (i = 0; i < revs->pending.nr; ++i) {
1718 struct object *object = revs->pending.objects[i].item;
1720 if (object->type == OBJ_NONE)
1721 parse_object_or_die(&object->oid, NULL);
1723 while (object->type == OBJ_TAG) {
1724 struct tag *tag = (struct tag *) object;
1726 if (object->flags & UNINTERESTING)
1727 object_list_insert(object, &haves);
1728 else
1729 object_list_insert(object, &wants);
1731 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1732 object->flags |= (tag->object.flags & UNINTERESTING);
1735 if (object->flags & UNINTERESTING)
1736 object_list_insert(object, &haves);
1737 else
1738 object_list_insert(object, &wants);
1741 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1742 if (use_boundary_traversal < 0) {
1743 prepare_repo_settings(revs->repo);
1744 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1747 if (!use_boundary_traversal) {
1749 * if we have a HAVES list, but none of those haves is contained
1750 * in the packfile that has a bitmap, we don't have anything to
1751 * optimize here
1753 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1754 goto cleanup;
1757 /* if we don't want anything, we're done here */
1758 if (!wants)
1759 goto cleanup;
1762 * now we're going to use bitmaps, so load the actual bitmap entries
1763 * from disk. this is the point of no return; after this the rev_list
1764 * becomes invalidated and we must perform the revwalk through bitmaps
1766 if (load_bitmap(revs->repo, bitmap_git) < 0)
1767 goto cleanup;
1769 if (!use_boundary_traversal)
1770 object_array_clear(&revs->pending);
1772 if (haves) {
1773 if (use_boundary_traversal) {
1774 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1775 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1776 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1777 } else {
1778 trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1779 revs->ignore_missing_links = 1;
1780 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1781 reset_revision_walk();
1782 revs->ignore_missing_links = 0;
1783 trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1786 if (!haves_bitmap)
1787 BUG("failed to perform bitmap walk");
1790 if (use_boundary_traversal) {
1791 object_array_clear(&revs->pending);
1792 reset_revision_walk();
1795 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1797 if (!wants_bitmap)
1798 BUG("failed to perform bitmap walk");
1800 if (haves_bitmap)
1801 bitmap_and_not(wants_bitmap, haves_bitmap);
1803 filter_bitmap(bitmap_git,
1804 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1805 wants_bitmap,
1806 &revs->filter);
1808 if (revs->unpacked)
1809 filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap);
1811 bitmap_git->result = wants_bitmap;
1812 bitmap_git->haves = haves_bitmap;
1814 object_list_free(&wants);
1815 object_list_free(&haves);
1817 return bitmap_git;
1819 cleanup:
1820 free_bitmap_index(bitmap_git);
1821 object_list_free(&wants);
1822 object_list_free(&haves);
1823 return NULL;
1827 * -1 means "stop trying further objects"; 0 means we may or may not have
1828 * reused, but you can keep feeding bits.
1830 static int try_partial_reuse(struct packed_git *pack,
1831 size_t pos,
1832 struct bitmap *reuse,
1833 struct pack_window **w_curs)
1835 off_t offset, delta_obj_offset;
1836 enum object_type type;
1837 unsigned long size;
1840 * try_partial_reuse() is called either on (a) objects in the
1841 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1842 * objects in the preferred pack of a multi-pack bitmap.
1843 * Importantly, the latter can pretend as if only a single pack
1844 * exists because:
1846 * - The first pack->num_objects bits of a MIDX bitmap are
1847 * reserved for the preferred pack, and
1849 * - Ties due to duplicate objects are always resolved in
1850 * favor of the preferred pack.
1852 * Therefore we do not need to ever ask the MIDX for its copy of
1853 * an object by OID, since it will always select it from the
1854 * preferred pack. Likewise, the selected copy of the base
1855 * object for any deltas will reside in the same pack.
1857 * This means that we can reuse pos when looking up the bit in
1858 * the reuse bitmap, too, since bits corresponding to the
1859 * preferred pack precede all bits from other packs.
1862 if (pos >= pack->num_objects)
1863 return -1; /* not actually in the pack or MIDX preferred pack */
1865 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1866 type = unpack_object_header(pack, w_curs, &offset, &size);
1867 if (type < 0)
1868 return -1; /* broken packfile, punt */
1870 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1871 off_t base_offset;
1872 uint32_t base_pos;
1875 * Find the position of the base object so we can look it up
1876 * in our bitmaps. If we can't come up with an offset, or if
1877 * that offset is not in the revidx, the pack is corrupt.
1878 * There's nothing we can do, so just punt on this object,
1879 * and the normal slow path will complain about it in
1880 * more detail.
1882 base_offset = get_delta_base(pack, w_curs, &offset, type,
1883 delta_obj_offset);
1884 if (!base_offset)
1885 return 0;
1886 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1887 return 0;
1890 * We assume delta dependencies always point backwards. This
1891 * lets us do a single pass, and is basically always true
1892 * due to the way OFS_DELTAs work. You would not typically
1893 * find REF_DELTA in a bitmapped pack, since we only bitmap
1894 * packs we write fresh, and OFS_DELTA is the default). But
1895 * let's double check to make sure the pack wasn't written with
1896 * odd parameters.
1898 if (base_pos >= pos)
1899 return 0;
1902 * And finally, if we're not sending the base as part of our
1903 * reuse chunk, then don't send this object either. The base
1904 * would come after us, along with other objects not
1905 * necessarily in the pack, which means we'd need to convert
1906 * to REF_DELTA on the fly. Better to just let the normal
1907 * object_entry code path handle it.
1909 if (!bitmap_get(reuse, base_pos))
1910 return 0;
1914 * If we got here, then the object is OK to reuse. Mark it.
1916 bitmap_set(reuse, pos);
1917 return 0;
1920 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1922 struct multi_pack_index *m = bitmap_git->midx;
1923 if (!m)
1924 BUG("midx_preferred_pack: requires non-empty MIDX");
1925 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1928 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1929 struct packed_git **packfile_out,
1930 uint32_t *entries,
1931 struct bitmap **reuse_out)
1933 struct repository *r = the_repository;
1934 struct packed_git *pack;
1935 struct bitmap *result = bitmap_git->result;
1936 struct bitmap *reuse;
1937 struct pack_window *w_curs = NULL;
1938 size_t i = 0;
1939 uint32_t offset;
1940 uint32_t objects_nr;
1942 assert(result);
1944 load_reverse_index(r, bitmap_git);
1946 if (bitmap_is_midx(bitmap_git))
1947 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1948 else
1949 pack = bitmap_git->pack;
1950 objects_nr = pack->num_objects;
1952 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1953 i++;
1956 * Don't mark objects not in the packfile or preferred pack. This bitmap
1957 * marks objects eligible for reuse, but the pack-reuse code only
1958 * understands how to reuse a single pack. Since the preferred pack is
1959 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1960 * we use it instead of another pack. In single-pack bitmaps, the choice
1961 * is made for us.
1963 if (i > objects_nr / BITS_IN_EWORD)
1964 i = objects_nr / BITS_IN_EWORD;
1966 reuse = bitmap_word_alloc(i);
1967 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1969 for (; i < result->word_alloc; ++i) {
1970 eword_t word = result->words[i];
1971 size_t pos = (i * BITS_IN_EWORD);
1973 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1974 if ((word >> offset) == 0)
1975 break;
1977 offset += ewah_bit_ctz64(word >> offset);
1978 if (try_partial_reuse(pack, pos + offset,
1979 reuse, &w_curs) < 0) {
1981 * try_partial_reuse indicated we couldn't reuse
1982 * any bits, so there is no point in trying more
1983 * bits in the current word, or any other words
1984 * in result.
1986 * Jump out of both loops to avoid future
1987 * unnecessary calls to try_partial_reuse.
1989 goto done;
1994 done:
1995 unuse_pack(&w_curs);
1997 *entries = bitmap_popcount(reuse);
1998 if (!*entries) {
1999 bitmap_free(reuse);
2000 return -1;
2004 * Drop any reused objects from the result, since they will not
2005 * need to be handled separately.
2007 bitmap_and_not(result, reuse);
2008 *packfile_out = pack;
2009 *reuse_out = reuse;
2010 return 0;
2013 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2014 struct bitmap *bitmap, const struct object_id *oid)
2016 int idx;
2018 if (!bitmap)
2019 return 0;
2021 idx = bitmap_position(bitmap_git, oid);
2022 return idx >= 0 && bitmap_get(bitmap, idx);
2025 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2026 struct rev_info *revs,
2027 show_reachable_fn show_reachable)
2029 assert(bitmap_git->result);
2031 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2032 if (revs->tree_objects)
2033 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2034 if (revs->blob_objects)
2035 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2036 if (revs->tag_objects)
2037 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2039 show_extended_objects(bitmap_git, revs, show_reachable);
2042 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2043 enum object_type type)
2045 struct bitmap *objects = bitmap_git->result;
2046 struct eindex *eindex = &bitmap_git->ext_index;
2048 uint32_t i = 0, count = 0;
2049 struct ewah_iterator it;
2050 eword_t filter;
2052 init_type_iterator(&it, bitmap_git, type);
2054 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2055 eword_t word = objects->words[i++] & filter;
2056 count += ewah_bit_popcount64(word);
2059 for (i = 0; i < eindex->count; ++i) {
2060 if (eindex->objects[i]->type == type &&
2061 bitmap_get(objects,
2062 st_add(bitmap_num_objects(bitmap_git), i)))
2063 count++;
2066 return count;
2069 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2070 uint32_t *commits, uint32_t *trees,
2071 uint32_t *blobs, uint32_t *tags)
2073 assert(bitmap_git->result);
2075 if (commits)
2076 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2078 if (trees)
2079 *trees = count_object_type(bitmap_git, OBJ_TREE);
2081 if (blobs)
2082 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2084 if (tags)
2085 *tags = count_object_type(bitmap_git, OBJ_TAG);
2088 struct bitmap_test_data {
2089 struct bitmap_index *bitmap_git;
2090 struct bitmap *base;
2091 struct bitmap *commits;
2092 struct bitmap *trees;
2093 struct bitmap *blobs;
2094 struct bitmap *tags;
2095 struct progress *prg;
2096 size_t seen;
2099 static void test_bitmap_type(struct bitmap_test_data *tdata,
2100 struct object *obj, int pos)
2102 enum object_type bitmap_type = OBJ_NONE;
2103 int bitmaps_nr = 0;
2105 if (bitmap_get(tdata->commits, pos)) {
2106 bitmap_type = OBJ_COMMIT;
2107 bitmaps_nr++;
2109 if (bitmap_get(tdata->trees, pos)) {
2110 bitmap_type = OBJ_TREE;
2111 bitmaps_nr++;
2113 if (bitmap_get(tdata->blobs, pos)) {
2114 bitmap_type = OBJ_BLOB;
2115 bitmaps_nr++;
2117 if (bitmap_get(tdata->tags, pos)) {
2118 bitmap_type = OBJ_TAG;
2119 bitmaps_nr++;
2122 if (bitmap_type == OBJ_NONE)
2123 die(_("object '%s' not found in type bitmaps"),
2124 oid_to_hex(&obj->oid));
2126 if (bitmaps_nr > 1)
2127 die(_("object '%s' does not have a unique type"),
2128 oid_to_hex(&obj->oid));
2130 if (bitmap_type != obj->type)
2131 die(_("object '%s': real type '%s', expected: '%s'"),
2132 oid_to_hex(&obj->oid),
2133 type_name(obj->type),
2134 type_name(bitmap_type));
2137 static void test_show_object(struct object *object,
2138 const char *name UNUSED,
2139 void *data)
2141 struct bitmap_test_data *tdata = data;
2142 int bitmap_pos;
2144 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2145 if (bitmap_pos < 0)
2146 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2147 test_bitmap_type(tdata, object, bitmap_pos);
2149 bitmap_set(tdata->base, bitmap_pos);
2150 display_progress(tdata->prg, ++tdata->seen);
2153 static void test_show_commit(struct commit *commit, void *data)
2155 struct bitmap_test_data *tdata = data;
2156 int bitmap_pos;
2158 bitmap_pos = bitmap_position(tdata->bitmap_git,
2159 &commit->object.oid);
2160 if (bitmap_pos < 0)
2161 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2162 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2164 bitmap_set(tdata->base, bitmap_pos);
2165 display_progress(tdata->prg, ++tdata->seen);
2168 void test_bitmap_walk(struct rev_info *revs)
2170 struct object *root;
2171 struct bitmap *result = NULL;
2172 size_t result_popcnt;
2173 struct bitmap_test_data tdata;
2174 struct bitmap_index *bitmap_git;
2175 struct ewah_bitmap *bm;
2177 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2178 die(_("failed to load bitmap indexes"));
2180 if (revs->pending.nr != 1)
2181 die(_("you must specify exactly one commit to test"));
2183 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2184 bitmap_git->version,
2185 bitmap_git->entry_count,
2186 bitmap_git->table_lookup ? "" : " loaded");
2188 root = revs->pending.objects[0].item;
2189 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2191 if (bm) {
2192 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2193 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2195 result = ewah_to_bitmap(bm);
2198 if (!result)
2199 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2201 revs->tag_objects = 1;
2202 revs->tree_objects = 1;
2203 revs->blob_objects = 1;
2205 result_popcnt = bitmap_popcount(result);
2207 if (prepare_revision_walk(revs))
2208 die(_("revision walk setup failed"));
2210 tdata.bitmap_git = bitmap_git;
2211 tdata.base = bitmap_new();
2212 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2213 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2214 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2215 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2216 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2217 tdata.seen = 0;
2219 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2221 stop_progress(&tdata.prg);
2223 if (bitmap_equals(result, tdata.base))
2224 fprintf_ln(stderr, "OK!");
2225 else
2226 die(_("mismatch in bitmap results"));
2228 bitmap_free(result);
2229 bitmap_free(tdata.base);
2230 bitmap_free(tdata.commits);
2231 bitmap_free(tdata.trees);
2232 bitmap_free(tdata.blobs);
2233 bitmap_free(tdata.tags);
2234 free_bitmap_index(bitmap_git);
2237 int test_bitmap_commits(struct repository *r)
2239 struct object_id oid;
2240 MAYBE_UNUSED void *value;
2241 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2243 if (!bitmap_git)
2244 die(_("failed to load bitmap indexes"));
2247 * As this function is only used to print bitmap selected
2248 * commits, we don't have to read the commit table.
2250 if (bitmap_git->table_lookup) {
2251 if (load_bitmap_entries_v1(bitmap_git) < 0)
2252 die(_("failed to load bitmap indexes"));
2255 kh_foreach(bitmap_git->bitmaps, oid, value, {
2256 printf_ln("%s", oid_to_hex(&oid));
2259 free_bitmap_index(bitmap_git);
2261 return 0;
2264 int test_bitmap_hashes(struct repository *r)
2266 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2267 struct object_id oid;
2268 uint32_t i, index_pos;
2270 if (!bitmap_git || !bitmap_git->hashes)
2271 goto cleanup;
2273 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2274 if (bitmap_is_midx(bitmap_git))
2275 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2276 else
2277 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2279 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2281 printf_ln("%s %"PRIu32"",
2282 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2285 cleanup:
2286 free_bitmap_index(bitmap_git);
2288 return 0;
2291 int rebuild_bitmap(const uint32_t *reposition,
2292 struct ewah_bitmap *source,
2293 struct bitmap *dest)
2295 uint32_t pos = 0;
2296 struct ewah_iterator it;
2297 eword_t word;
2299 ewah_iterator_init(&it, source);
2301 while (ewah_iterator_next(&word, &it)) {
2302 uint32_t offset, bit_pos;
2304 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2305 if ((word >> offset) == 0)
2306 break;
2308 offset += ewah_bit_ctz64(word >> offset);
2310 bit_pos = reposition[pos + offset];
2311 if (bit_pos > 0)
2312 bitmap_set(dest, bit_pos - 1);
2313 else /* can't reuse, we don't have the object */
2314 return -1;
2317 pos += BITS_IN_EWORD;
2319 return 0;
2322 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2323 struct packing_data *mapping)
2325 struct repository *r = the_repository;
2326 uint32_t i, num_objects;
2327 uint32_t *reposition;
2329 if (!bitmap_is_midx(bitmap_git))
2330 load_reverse_index(r, bitmap_git);
2331 else if (load_midx_revindex(bitmap_git->midx))
2332 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2333 "extension");
2335 num_objects = bitmap_num_objects(bitmap_git);
2336 CALLOC_ARRAY(reposition, num_objects);
2338 for (i = 0; i < num_objects; ++i) {
2339 struct object_id oid;
2340 struct object_entry *oe;
2341 uint32_t index_pos;
2343 if (bitmap_is_midx(bitmap_git))
2344 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2345 else
2346 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2347 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2348 oe = packlist_find(mapping, &oid);
2350 if (oe) {
2351 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2352 if (bitmap_git->hashes && !oe->hash)
2353 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2357 return reposition;
2360 void free_bitmap_index(struct bitmap_index *b)
2362 if (!b)
2363 return;
2365 if (b->map)
2366 munmap(b->map, b->map_size);
2367 ewah_pool_free(b->commits);
2368 ewah_pool_free(b->trees);
2369 ewah_pool_free(b->blobs);
2370 ewah_pool_free(b->tags);
2371 if (b->bitmaps) {
2372 struct stored_bitmap *sb;
2373 kh_foreach_value(b->bitmaps, sb, {
2374 ewah_pool_free(sb->root);
2375 free(sb);
2378 kh_destroy_oid_map(b->bitmaps);
2379 free(b->ext_index.objects);
2380 free(b->ext_index.hashes);
2381 kh_destroy_oid_pos(b->ext_index.positions);
2382 bitmap_free(b->result);
2383 bitmap_free(b->haves);
2384 if (bitmap_is_midx(b)) {
2386 * Multi-pack bitmaps need to have resources associated with
2387 * their on-disk reverse indexes unmapped so that stale .rev and
2388 * .bitmap files can be removed.
2390 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2391 * written in the same 'git multi-pack-index write --bitmap'
2392 * process. Close resources so they can be removed safely on
2393 * platforms like Windows.
2395 close_midx_revindex(b->midx);
2397 free(b);
2400 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2401 const struct object_id *oid)
2403 return bitmap_git &&
2404 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2407 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2408 enum object_type object_type)
2410 struct bitmap *result = bitmap_git->result;
2411 off_t total = 0;
2412 struct ewah_iterator it;
2413 eword_t filter;
2414 size_t i;
2416 init_type_iterator(&it, bitmap_git, object_type);
2417 for (i = 0; i < result->word_alloc &&
2418 ewah_iterator_next(&filter, &it); i++) {
2419 eword_t word = result->words[i] & filter;
2420 size_t base = (i * BITS_IN_EWORD);
2421 unsigned offset;
2423 if (!word)
2424 continue;
2426 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2427 if ((word >> offset) == 0)
2428 break;
2430 offset += ewah_bit_ctz64(word >> offset);
2432 if (bitmap_is_midx(bitmap_git)) {
2433 uint32_t pack_pos;
2434 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2435 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2437 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2438 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2440 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2441 struct object_id oid;
2442 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2444 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2445 oid_to_hex(&oid),
2446 pack->pack_name,
2447 (uintmax_t)offset);
2450 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2451 } else {
2452 size_t pos = base + offset;
2453 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2454 pack_pos_to_offset(bitmap_git->pack, pos);
2459 return total;
2462 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2464 struct bitmap *result = bitmap_git->result;
2465 struct eindex *eindex = &bitmap_git->ext_index;
2466 off_t total = 0;
2467 struct object_info oi = OBJECT_INFO_INIT;
2468 off_t object_size;
2469 size_t i;
2471 oi.disk_sizep = &object_size;
2473 for (i = 0; i < eindex->count; i++) {
2474 struct object *obj = eindex->objects[i];
2476 if (!bitmap_get(result,
2477 st_add(bitmap_num_objects(bitmap_git), i)))
2478 continue;
2480 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2481 die(_("unable to get disk usage of '%s'"),
2482 oid_to_hex(&obj->oid));
2484 total += object_size;
2486 return total;
2489 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2490 struct rev_info *revs)
2492 off_t total = 0;
2494 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2495 if (revs->tree_objects)
2496 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2497 if (revs->blob_objects)
2498 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2499 if (revs->tag_objects)
2500 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2502 total += get_disk_usage_for_extended(bitmap_git);
2504 return total;
2507 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2509 return !!bitmap_git->midx;
2512 const struct string_list *bitmap_preferred_tips(struct repository *r)
2514 const struct string_list *dest;
2516 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2517 return dest;
2518 return NULL;
2521 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2523 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2524 struct string_list_item *item;
2526 if (!preferred_tips)
2527 return 0;
2529 for_each_string_list_item(item, preferred_tips) {
2530 if (starts_with(refname, item->string))
2531 return 1;
2534 return 0;
2537 static int verify_bitmap_file(const char *name)
2539 struct stat st;
2540 unsigned char *data;
2541 int fd = git_open(name);
2542 int res = 0;
2544 /* It is OK to not have the file. */
2545 if (fd < 0 || fstat(fd, &st)) {
2546 if (fd >= 0)
2547 close(fd);
2548 return 0;
2551 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2552 close(fd);
2553 if (!hashfile_checksum_valid(data, st.st_size))
2554 res = error(_("bitmap file '%s' has invalid checksum"),
2555 name);
2557 munmap(data, st.st_size);
2558 return res;
2561 int verify_bitmap_files(struct repository *r)
2563 int res = 0;
2565 for (struct multi_pack_index *m = get_multi_pack_index(r);
2566 m; m = m->next) {
2567 char *midx_bitmap_name = midx_bitmap_filename(m);
2568 res |= verify_bitmap_file(midx_bitmap_name);
2569 free(midx_bitmap_name);
2572 for (struct packed_git *p = get_all_packs(r);
2573 p; p = p->next) {
2574 char *pack_bitmap_name = pack_bitmap_filename(p);
2575 res |= verify_bitmap_file(pack_bitmap_name);
2576 free(pack_bitmap_name);
2579 return res;