Merge branch 'fixes/2.45.1/2.43' into fixes/2.45.1/2.44
[git.git] / pack-bitmap.c
blob2baeabacee13d4ec5311261499fe10d8f0a882e5
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, preferred_pack;
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 if (midx_preferred_pack(bitmap_git->midx, &preferred_pack) < 0) {
390 warning(_("could not determine MIDX preferred pack"));
391 goto cleanup;
394 preferred = bitmap_git->midx->packs[preferred_pack];
395 if (!is_pack_valid(preferred)) {
396 warning(_("preferred pack (%s) is invalid"),
397 preferred->pack_name);
398 goto cleanup;
401 return 0;
403 cleanup:
404 munmap(bitmap_git->map, bitmap_git->map_size);
405 bitmap_git->map_size = 0;
406 bitmap_git->map_pos = 0;
407 bitmap_git->map = NULL;
408 bitmap_git->midx = NULL;
409 return -1;
412 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
414 int fd;
415 struct stat st;
416 char *bitmap_name;
418 bitmap_name = pack_bitmap_filename(packfile);
419 fd = git_open(bitmap_name);
421 if (fd < 0) {
422 if (errno != ENOENT)
423 warning_errno("cannot open '%s'", bitmap_name);
424 free(bitmap_name);
425 return -1;
427 free(bitmap_name);
429 if (fstat(fd, &st)) {
430 error_errno(_("cannot fstat bitmap file"));
431 close(fd);
432 return -1;
435 if (bitmap_git->pack || bitmap_git->midx) {
436 trace2_data_string("bitmap", the_repository,
437 "ignoring extra bitmap file", packfile->pack_name);
438 close(fd);
439 return -1;
442 if (!is_pack_valid(packfile)) {
443 close(fd);
444 return -1;
447 bitmap_git->pack = packfile;
448 bitmap_git->map_size = xsize_t(st.st_size);
449 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
450 bitmap_git->map_pos = 0;
451 close(fd);
453 if (load_bitmap_header(bitmap_git) < 0) {
454 munmap(bitmap_git->map, bitmap_git->map_size);
455 bitmap_git->map = NULL;
456 bitmap_git->map_size = 0;
457 bitmap_git->map_pos = 0;
458 bitmap_git->pack = NULL;
459 return -1;
462 trace2_data_string("bitmap", the_repository, "opened bitmap file",
463 packfile->pack_name);
464 return 0;
467 static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
469 if (bitmap_is_midx(bitmap_git)) {
470 uint32_t i;
471 int ret;
474 * The multi-pack-index's .rev file is already loaded via
475 * open_pack_bitmap_1().
477 * But we still need to open the individual pack .rev files,
478 * since we will need to make use of them in pack-objects.
480 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
481 ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
482 if (ret)
483 return ret;
485 return 0;
487 return load_pack_revindex(r, bitmap_git->pack);
490 static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
492 assert(bitmap_git->map);
494 bitmap_git->bitmaps = kh_init_oid_map();
495 bitmap_git->ext_index.positions = kh_init_oid_pos();
497 if (load_reverse_index(r, bitmap_git))
498 goto failed;
500 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
501 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
502 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
503 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
504 goto failed;
506 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
507 goto failed;
509 return 0;
511 failed:
512 munmap(bitmap_git->map, bitmap_git->map_size);
513 bitmap_git->map = NULL;
514 bitmap_git->map_size = 0;
516 kh_destroy_oid_map(bitmap_git->bitmaps);
517 bitmap_git->bitmaps = NULL;
519 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
520 bitmap_git->ext_index.positions = NULL;
522 return -1;
525 static int open_pack_bitmap(struct repository *r,
526 struct bitmap_index *bitmap_git)
528 struct packed_git *p;
529 int ret = -1;
531 for (p = get_all_packs(r); p; p = p->next) {
532 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
533 ret = 0;
535 * The only reason to keep looking is to report
536 * duplicates.
538 if (!trace2_is_enabled())
539 break;
543 return ret;
546 static int open_midx_bitmap(struct repository *r,
547 struct bitmap_index *bitmap_git)
549 int ret = -1;
550 struct multi_pack_index *midx;
552 assert(!bitmap_git->map);
554 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
555 if (!open_midx_bitmap_1(bitmap_git, midx))
556 ret = 0;
558 return ret;
561 static int open_bitmap(struct repository *r,
562 struct bitmap_index *bitmap_git)
564 int found;
566 assert(!bitmap_git->map);
568 found = !open_midx_bitmap(r, bitmap_git);
571 * these will all be skipped if we opened a midx bitmap; but run it
572 * anyway if tracing is enabled to report the duplicates
574 if (!found || trace2_is_enabled())
575 found |= !open_pack_bitmap(r, bitmap_git);
577 return found ? 0 : -1;
580 struct bitmap_index *prepare_bitmap_git(struct repository *r)
582 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
584 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
585 return bitmap_git;
587 free_bitmap_index(bitmap_git);
588 return NULL;
591 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
593 struct repository *r = the_repository;
594 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
596 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
597 return bitmap_git;
599 free_bitmap_index(bitmap_git);
600 return NULL;
603 struct include_data {
604 struct bitmap_index *bitmap_git;
605 struct bitmap *base;
606 struct bitmap *seen;
609 struct bitmap_lookup_table_triplet {
610 uint32_t commit_pos;
611 uint64_t offset;
612 uint32_t xor_row;
615 struct bitmap_lookup_table_xor_item {
616 struct object_id oid;
617 uint64_t offset;
621 * Given a `triplet` struct pointer and pointer `p`, this
622 * function reads the triplet beginning at `p` into the struct.
623 * Note that this function assumes that there is enough memory
624 * left for filling the `triplet` struct from `p`.
626 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
627 const unsigned char *p)
629 if (!triplet)
630 return -1;
632 triplet->commit_pos = get_be32(p);
633 p += sizeof(uint32_t);
634 triplet->offset = get_be64(p);
635 p += sizeof(uint64_t);
636 triplet->xor_row = get_be32(p);
637 return 0;
641 * This function gets the raw triplet from `row`'th row in the
642 * lookup table and fills that data to the `triplet`.
644 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
645 uint32_t pos,
646 struct bitmap_lookup_table_triplet *triplet)
648 unsigned char *p = NULL;
649 if (pos >= bitmap_git->entry_count)
650 return error(_("corrupt bitmap lookup table: triplet position out of index"));
652 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
654 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
658 * Searches for a matching triplet. `commit_pos` is a pointer
659 * to the wanted commit position value. `table_entry` points to
660 * a triplet in lookup table. The first 4 bytes of each
661 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
663 static int triplet_cmp(const void *commit_pos, const void *table_entry)
666 uint32_t a = *(uint32_t *)commit_pos;
667 uint32_t b = get_be32(table_entry);
668 if (a > b)
669 return 1;
670 else if (a < b)
671 return -1;
673 return 0;
676 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
677 struct object_id *oid,
678 uint32_t *result)
680 int found;
682 if (bitmap_is_midx(bitmap_git))
683 found = bsearch_midx(oid, bitmap_git->midx, result);
684 else
685 found = bsearch_pack(oid, bitmap_git->pack, result);
687 return found;
691 * `bsearch_triplet_by_pos` function searches for the raw triplet
692 * having commit position same as `commit_pos` and fills `triplet`
693 * object from the raw triplet. Returns 1 on success and 0 on
694 * failure.
696 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
697 struct bitmap_index *bitmap_git,
698 struct bitmap_lookup_table_triplet *triplet)
700 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
701 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
703 if (!p)
704 return -1;
706 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
709 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
710 struct commit *commit)
712 uint32_t commit_pos, xor_row;
713 uint64_t offset;
714 int flags;
715 struct bitmap_lookup_table_triplet triplet;
716 struct object_id *oid = &commit->object.oid;
717 struct ewah_bitmap *bitmap;
718 struct stored_bitmap *xor_bitmap = NULL;
719 const int bitmap_header_size = 6;
720 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
721 static size_t xor_items_nr = 0, xor_items_alloc = 0;
722 static int is_corrupt = 0;
723 int xor_flags;
724 khiter_t hash_pos;
725 struct bitmap_lookup_table_xor_item *xor_item;
727 if (is_corrupt)
728 return NULL;
730 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
731 return NULL;
733 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
734 return NULL;
736 xor_items_nr = 0;
737 offset = triplet.offset;
738 xor_row = triplet.xor_row;
740 while (xor_row != 0xffffffff) {
741 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
743 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
744 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
745 goto corrupt;
748 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
749 goto corrupt;
751 xor_item = &xor_items[xor_items_nr];
752 xor_item->offset = triplet.offset;
754 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
755 error(_("corrupt bitmap lookup table: commit index %u out of range"),
756 triplet.commit_pos);
757 goto corrupt;
760 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
763 * If desired bitmap is already stored, we don't need
764 * to iterate further. Because we know that bitmaps
765 * that are needed to be parsed to parse this bitmap
766 * has already been stored. So, assign this stored bitmap
767 * to the xor_bitmap.
769 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
770 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
771 break;
772 xor_items_nr++;
773 xor_row = triplet.xor_row;
776 while (xor_items_nr) {
777 xor_item = &xor_items[xor_items_nr - 1];
778 bitmap_git->map_pos = xor_item->offset;
779 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
780 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
781 oid_to_hex(&xor_item->oid));
782 goto corrupt;
785 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
786 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
787 bitmap = read_bitmap_1(bitmap_git);
789 if (!bitmap)
790 goto corrupt;
792 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
793 xor_items_nr--;
796 bitmap_git->map_pos = offset;
797 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
798 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
799 oid_to_hex(oid));
800 goto corrupt;
804 * Don't bother reading the commit's index position or its xor
805 * offset:
807 * - The commit's index position is irrelevant to us, since
808 * load_bitmap_entries_v1 only uses it to learn the object
809 * id which is used to compute the hashmap's key. We already
810 * have an object id, so no need to look it up again.
812 * - The xor_offset is unusable for us, since it specifies how
813 * many entries previous to ours we should look at. This
814 * makes sense when reading the bitmaps sequentially (as in
815 * load_bitmap_entries_v1()), since we can keep track of
816 * each bitmap as we read them.
818 * But it can't work for us, since the bitmap's don't have a
819 * fixed size. So we learn the position of the xor'd bitmap
820 * from the commit table (and resolve it to a bitmap in the
821 * above if-statement).
823 * Instead, we can skip ahead and immediately read the flags and
824 * ewah bitmap.
826 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
827 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
828 bitmap = read_bitmap_1(bitmap_git);
830 if (!bitmap)
831 goto corrupt;
833 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
835 corrupt:
836 free(xor_items);
837 is_corrupt = 1;
838 return NULL;
841 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
842 struct commit *commit)
844 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
845 commit->object.oid);
846 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
847 struct stored_bitmap *bitmap = NULL;
848 if (!bitmap_git->table_lookup)
849 return NULL;
851 /* this is a fairly hot codepath - no trace2_region please */
852 /* NEEDSWORK: cache misses aren't recorded */
853 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
854 if (!bitmap)
855 return NULL;
856 return lookup_stored_bitmap(bitmap);
858 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
861 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
862 const struct object_id *oid)
864 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
865 khiter_t pos = kh_get_oid_pos(positions, *oid);
867 if (pos < kh_end(positions)) {
868 int bitmap_pos = kh_value(positions, pos);
869 return bitmap_pos + bitmap_num_objects(bitmap_git);
872 return -1;
875 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
876 const struct object_id *oid)
878 uint32_t pos;
879 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
880 if (!offset)
881 return -1;
883 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
884 return -1;
885 return pos;
888 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
889 const struct object_id *oid)
891 uint32_t want, got;
892 if (!bsearch_midx(oid, bitmap_git->midx, &want))
893 return -1;
895 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
896 return -1;
897 return got;
900 static int bitmap_position(struct bitmap_index *bitmap_git,
901 const struct object_id *oid)
903 int pos;
904 if (bitmap_is_midx(bitmap_git))
905 pos = bitmap_position_midx(bitmap_git, oid);
906 else
907 pos = bitmap_position_packfile(bitmap_git, oid);
908 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
911 static int ext_index_add_object(struct bitmap_index *bitmap_git,
912 struct object *object, const char *name)
914 struct eindex *eindex = &bitmap_git->ext_index;
916 khiter_t hash_pos;
917 int hash_ret;
918 int bitmap_pos;
920 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
921 if (hash_ret > 0) {
922 if (eindex->count >= eindex->alloc) {
923 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
924 REALLOC_ARRAY(eindex->objects, eindex->alloc);
925 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
928 bitmap_pos = eindex->count;
929 eindex->objects[eindex->count] = object;
930 eindex->hashes[eindex->count] = pack_name_hash(name);
931 kh_value(eindex->positions, hash_pos) = bitmap_pos;
932 eindex->count++;
933 } else {
934 bitmap_pos = kh_value(eindex->positions, hash_pos);
937 return bitmap_pos + bitmap_num_objects(bitmap_git);
940 struct bitmap_show_data {
941 struct bitmap_index *bitmap_git;
942 struct bitmap *base;
945 static void show_object(struct object *object, const char *name, void *data_)
947 struct bitmap_show_data *data = data_;
948 int bitmap_pos;
950 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
952 if (bitmap_pos < 0)
953 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
954 name);
956 bitmap_set(data->base, bitmap_pos);
959 static void show_commit(struct commit *commit UNUSED,
960 void *data UNUSED)
964 static int add_to_include_set(struct bitmap_index *bitmap_git,
965 struct include_data *data,
966 struct commit *commit,
967 int bitmap_pos)
969 struct ewah_bitmap *partial;
971 if (data->seen && bitmap_get(data->seen, bitmap_pos))
972 return 0;
974 if (bitmap_get(data->base, bitmap_pos))
975 return 0;
977 partial = bitmap_for_commit(bitmap_git, commit);
978 if (partial) {
979 bitmap_or_ewah(data->base, partial);
980 return 0;
983 bitmap_set(data->base, bitmap_pos);
984 return 1;
987 static int should_include(struct commit *commit, void *_data)
989 struct include_data *data = _data;
990 int bitmap_pos;
992 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
993 if (bitmap_pos < 0)
994 bitmap_pos = ext_index_add_object(data->bitmap_git,
995 (struct object *)commit,
996 NULL);
998 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
999 struct commit_list *parent = commit->parents;
1001 while (parent) {
1002 parent->item->object.flags |= SEEN;
1003 parent = parent->next;
1006 return 0;
1009 return 1;
1012 static int should_include_obj(struct object *obj, void *_data)
1014 struct include_data *data = _data;
1015 int bitmap_pos;
1017 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1018 if (bitmap_pos < 0)
1019 return 1;
1020 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1021 bitmap_get(data->base, bitmap_pos)) {
1022 obj->flags |= SEEN;
1023 return 0;
1025 return 1;
1028 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1029 struct bitmap **base,
1030 struct commit *commit)
1032 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1034 if (!or_with)
1035 return 0;
1037 if (!*base)
1038 *base = ewah_to_bitmap(or_with);
1039 else
1040 bitmap_or_ewah(*base, or_with);
1042 return 1;
1045 static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1046 struct rev_info *revs,
1047 struct bitmap *base,
1048 struct bitmap *seen)
1050 struct include_data incdata;
1051 struct bitmap_show_data show_data;
1053 if (!base)
1054 base = bitmap_new();
1056 incdata.bitmap_git = bitmap_git;
1057 incdata.base = base;
1058 incdata.seen = seen;
1060 revs->include_check = should_include;
1061 revs->include_check_obj = should_include_obj;
1062 revs->include_check_data = &incdata;
1064 if (prepare_revision_walk(revs))
1065 die(_("revision walk setup failed"));
1067 show_data.bitmap_git = bitmap_git;
1068 show_data.base = base;
1070 traverse_commit_list(revs, show_commit, show_object, &show_data);
1072 revs->include_check = NULL;
1073 revs->include_check_obj = NULL;
1074 revs->include_check_data = NULL;
1076 return base;
1079 struct bitmap_boundary_cb {
1080 struct bitmap_index *bitmap_git;
1081 struct bitmap *base;
1083 struct object_array boundary;
1086 static void show_boundary_commit(struct commit *commit, void *_data)
1088 struct bitmap_boundary_cb *data = _data;
1090 if (commit->object.flags & BOUNDARY)
1091 add_object_array(&commit->object, "", &data->boundary);
1093 if (commit->object.flags & UNINTERESTING) {
1094 if (bitmap_walk_contains(data->bitmap_git, data->base,
1095 &commit->object.oid))
1096 return;
1098 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1102 static void show_boundary_object(struct object *object UNUSED,
1103 const char *name UNUSED,
1104 void *data UNUSED)
1106 BUG("should not be called");
1109 static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1110 struct rev_info *revs,
1111 struct object_list *roots)
1113 struct bitmap_boundary_cb cb;
1114 struct object_list *root;
1115 unsigned int i;
1116 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1117 int any_missing = 0;
1119 cb.bitmap_git = bitmap_git;
1120 cb.base = bitmap_new();
1121 object_array_init(&cb.boundary);
1123 revs->ignore_missing_links = 1;
1126 * OR in any existing reachability bitmaps among `roots` into
1127 * `cb.base`.
1129 for (root = roots; root; root = root->next) {
1130 struct object *object = root->item;
1131 if (object->type != OBJ_COMMIT ||
1132 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1133 continue;
1135 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1136 (struct commit *)object))
1137 continue;
1139 any_missing = 1;
1142 if (!any_missing)
1143 goto cleanup;
1145 tmp_blobs = revs->blob_objects;
1146 tmp_trees = revs->tree_objects;
1147 tmp_tags = revs->blob_objects;
1148 revs->blob_objects = 0;
1149 revs->tree_objects = 0;
1150 revs->tag_objects = 0;
1153 * We didn't have complete coverage of the roots. First setup a
1154 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1155 * between the tips and boundary, and (b) record the boundary.
1157 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1158 if (prepare_revision_walk(revs))
1159 die("revision walk setup failed");
1160 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1162 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1163 revs->boundary = 1;
1164 traverse_commit_list_filtered(revs,
1165 show_boundary_commit,
1166 show_boundary_object,
1167 &cb, NULL);
1168 revs->boundary = 0;
1169 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1171 revs->blob_objects = tmp_blobs;
1172 revs->tree_objects = tmp_trees;
1173 revs->tag_objects = tmp_tags;
1175 reset_revision_walk();
1176 clear_object_flags(UNINTERESTING);
1179 * Then add the boundary commit(s) as fill-in traversal tips.
1181 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1182 for (i = 0; i < cb.boundary.nr; i++) {
1183 struct object *obj = cb.boundary.objects[i].item;
1184 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1185 obj->flags |= SEEN;
1186 else
1187 add_pending_object(revs, obj, "");
1189 if (revs->pending.nr)
1190 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1191 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1193 cleanup:
1194 object_array_clear(&cb.boundary);
1195 revs->ignore_missing_links = 0;
1197 return cb.base;
1200 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1201 struct rev_info *revs,
1202 struct object_list *roots,
1203 struct bitmap *seen)
1205 struct bitmap *base = NULL;
1206 int needs_walk = 0;
1208 struct object_list *not_mapped = NULL;
1211 * Go through all the roots for the walk. The ones that have bitmaps
1212 * on the bitmap index will be `or`ed together to form an initial
1213 * global reachability analysis.
1215 * The ones without bitmaps in the index will be stored in the
1216 * `not_mapped_list` for further processing.
1218 while (roots) {
1219 struct object *object = roots->item;
1220 roots = roots->next;
1222 if (object->type == OBJ_COMMIT &&
1223 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1224 object->flags |= SEEN;
1225 continue;
1228 object_list_insert(object, &not_mapped);
1232 * Best case scenario: We found bitmaps for all the roots,
1233 * so the resulting `or` bitmap has the full reachability analysis
1235 if (!not_mapped)
1236 return base;
1238 roots = not_mapped;
1241 * Let's iterate through all the roots that don't have bitmaps to
1242 * check if we can determine them to be reachable from the existing
1243 * global bitmap.
1245 * If we cannot find them in the existing global bitmap, we'll need
1246 * to push them to an actual walk and run it until we can confirm
1247 * they are reachable
1249 while (roots) {
1250 struct object *object = roots->item;
1251 int pos;
1253 roots = roots->next;
1254 pos = bitmap_position(bitmap_git, &object->oid);
1256 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1257 object->flags &= ~UNINTERESTING;
1258 add_pending_object(revs, object, "");
1259 needs_walk = 1;
1260 } else {
1261 object->flags |= SEEN;
1265 if (needs_walk) {
1267 * This fill-in traversal may walk over some objects
1268 * again, since we have already traversed in order to
1269 * find the boundary.
1271 * But this extra walk should be extremely cheap, since
1272 * all commit objects are loaded into memory, and
1273 * because we skip walking to parents that are
1274 * UNINTERESTING, since it will be marked in the haves
1275 * bitmap already (or it has an on-disk bitmap, since
1276 * OR-ing it in covers all of its ancestors).
1278 base = fill_in_bitmap(bitmap_git, revs, base, seen);
1281 object_list_free(&not_mapped);
1283 return base;
1286 static void show_extended_objects(struct bitmap_index *bitmap_git,
1287 struct rev_info *revs,
1288 show_reachable_fn show_reach)
1290 struct bitmap *objects = bitmap_git->result;
1291 struct eindex *eindex = &bitmap_git->ext_index;
1292 uint32_t i;
1294 for (i = 0; i < eindex->count; ++i) {
1295 struct object *obj;
1297 if (!bitmap_get(objects, st_add(bitmap_num_objects(bitmap_git), i)))
1298 continue;
1300 obj = eindex->objects[i];
1301 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1302 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1303 (obj->type == OBJ_TAG && !revs->tag_objects))
1304 continue;
1306 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1310 static void init_type_iterator(struct ewah_iterator *it,
1311 struct bitmap_index *bitmap_git,
1312 enum object_type type)
1314 switch (type) {
1315 case OBJ_COMMIT:
1316 ewah_iterator_init(it, bitmap_git->commits);
1317 break;
1319 case OBJ_TREE:
1320 ewah_iterator_init(it, bitmap_git->trees);
1321 break;
1323 case OBJ_BLOB:
1324 ewah_iterator_init(it, bitmap_git->blobs);
1325 break;
1327 case OBJ_TAG:
1328 ewah_iterator_init(it, bitmap_git->tags);
1329 break;
1331 default:
1332 BUG("object type %d not stored by bitmap type index", type);
1333 break;
1337 static void show_objects_for_type(
1338 struct bitmap_index *bitmap_git,
1339 enum object_type object_type,
1340 show_reachable_fn show_reach)
1342 size_t i = 0;
1343 uint32_t offset;
1345 struct ewah_iterator it;
1346 eword_t filter;
1348 struct bitmap *objects = bitmap_git->result;
1350 init_type_iterator(&it, bitmap_git, object_type);
1352 for (i = 0; i < objects->word_alloc &&
1353 ewah_iterator_next(&filter, &it); i++) {
1354 eword_t word = objects->words[i] & filter;
1355 size_t pos = (i * BITS_IN_EWORD);
1357 if (!word)
1358 continue;
1360 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1361 struct packed_git *pack;
1362 struct object_id oid;
1363 uint32_t hash = 0, index_pos;
1364 off_t ofs;
1366 if ((word >> offset) == 0)
1367 break;
1369 offset += ewah_bit_ctz64(word >> offset);
1371 if (bitmap_is_midx(bitmap_git)) {
1372 struct multi_pack_index *m = bitmap_git->midx;
1373 uint32_t pack_id;
1375 index_pos = pack_pos_to_midx(m, pos + offset);
1376 ofs = nth_midxed_offset(m, index_pos);
1377 nth_midxed_object_oid(&oid, m, index_pos);
1379 pack_id = nth_midxed_pack_int_id(m, index_pos);
1380 pack = bitmap_git->midx->packs[pack_id];
1381 } else {
1382 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1383 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1384 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1386 pack = bitmap_git->pack;
1389 if (bitmap_git->hashes)
1390 hash = get_be32(bitmap_git->hashes + index_pos);
1392 show_reach(&oid, object_type, 0, hash, pack, ofs);
1397 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1398 struct object_list *roots)
1400 while (roots) {
1401 struct object *object = roots->item;
1402 roots = roots->next;
1404 if (bitmap_is_midx(bitmap_git)) {
1405 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1406 return 1;
1407 } else {
1408 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1409 return 1;
1413 return 0;
1416 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1417 struct object_list *tip_objects,
1418 enum object_type type)
1420 struct bitmap *result = bitmap_new();
1421 struct object_list *p;
1423 for (p = tip_objects; p; p = p->next) {
1424 int pos;
1426 if (p->item->type != type)
1427 continue;
1429 pos = bitmap_position(bitmap_git, &p->item->oid);
1430 if (pos < 0)
1431 continue;
1433 bitmap_set(result, pos);
1436 return result;
1439 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1440 struct object_list *tip_objects,
1441 struct bitmap *to_filter,
1442 enum object_type type)
1444 struct eindex *eindex = &bitmap_git->ext_index;
1445 struct bitmap *tips;
1446 struct ewah_iterator it;
1447 eword_t mask;
1448 uint32_t i;
1451 * The non-bitmap version of this filter never removes
1452 * objects which the other side specifically asked for,
1453 * so we must match that behavior.
1455 tips = find_tip_objects(bitmap_git, tip_objects, type);
1458 * We can use the type-level bitmap for 'type' to work in whole
1459 * words for the objects that are actually in the bitmapped
1460 * packfile.
1462 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1463 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1464 i++) {
1465 if (i < tips->word_alloc)
1466 mask &= ~tips->words[i];
1467 to_filter->words[i] &= ~mask;
1471 * Clear any objects that weren't in the packfile (and so would
1472 * not have been caught by the loop above. We'll have to check
1473 * them individually.
1475 for (i = 0; i < eindex->count; i++) {
1476 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1477 if (eindex->objects[i]->type == type &&
1478 bitmap_get(to_filter, pos) &&
1479 !bitmap_get(tips, pos))
1480 bitmap_unset(to_filter, pos);
1483 bitmap_free(tips);
1486 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1487 struct object_list *tip_objects,
1488 struct bitmap *to_filter)
1490 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1491 OBJ_BLOB);
1494 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1495 uint32_t pos)
1497 unsigned long size;
1498 struct object_info oi = OBJECT_INFO_INIT;
1500 oi.sizep = &size;
1502 if (pos < bitmap_num_objects(bitmap_git)) {
1503 struct packed_git *pack;
1504 off_t ofs;
1506 if (bitmap_is_midx(bitmap_git)) {
1507 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1508 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1510 pack = bitmap_git->midx->packs[pack_id];
1511 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1512 } else {
1513 pack = bitmap_git->pack;
1514 ofs = pack_pos_to_offset(pack, pos);
1517 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1518 struct object_id oid;
1519 nth_bitmap_object_oid(bitmap_git, &oid,
1520 pack_pos_to_index(pack, pos));
1521 die(_("unable to get size of %s"), oid_to_hex(&oid));
1523 } else {
1524 struct eindex *eindex = &bitmap_git->ext_index;
1525 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1526 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1527 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1530 return size;
1533 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1534 struct object_list *tip_objects,
1535 struct bitmap *to_filter,
1536 unsigned long limit)
1538 struct eindex *eindex = &bitmap_git->ext_index;
1539 struct bitmap *tips;
1540 struct ewah_iterator it;
1541 eword_t mask;
1542 uint32_t i;
1544 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1546 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1547 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1548 i++) {
1549 eword_t word = to_filter->words[i] & mask;
1550 unsigned offset;
1552 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1553 uint32_t pos;
1555 if ((word >> offset) == 0)
1556 break;
1557 offset += ewah_bit_ctz64(word >> offset);
1558 pos = i * BITS_IN_EWORD + offset;
1560 if (!bitmap_get(tips, pos) &&
1561 get_size_by_pos(bitmap_git, pos) >= limit)
1562 bitmap_unset(to_filter, pos);
1566 for (i = 0; i < eindex->count; i++) {
1567 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1568 if (eindex->objects[i]->type == OBJ_BLOB &&
1569 bitmap_get(to_filter, pos) &&
1570 !bitmap_get(tips, pos) &&
1571 get_size_by_pos(bitmap_git, pos) >= limit)
1572 bitmap_unset(to_filter, pos);
1575 bitmap_free(tips);
1578 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1579 struct object_list *tip_objects,
1580 struct bitmap *to_filter,
1581 unsigned long limit)
1583 if (limit)
1584 BUG("filter_bitmap_tree_depth given non-zero limit");
1586 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1587 OBJ_TREE);
1588 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1589 OBJ_BLOB);
1592 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1593 struct object_list *tip_objects,
1594 struct bitmap *to_filter,
1595 enum object_type object_type)
1597 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1598 BUG("filter_bitmap_object_type given invalid object");
1600 if (object_type != OBJ_TAG)
1601 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1602 if (object_type != OBJ_COMMIT)
1603 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1604 if (object_type != OBJ_TREE)
1605 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1606 if (object_type != OBJ_BLOB)
1607 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1610 static int filter_bitmap(struct bitmap_index *bitmap_git,
1611 struct object_list *tip_objects,
1612 struct bitmap *to_filter,
1613 struct list_objects_filter_options *filter)
1615 if (!filter || filter->choice == LOFC_DISABLED)
1616 return 0;
1618 if (filter->choice == LOFC_BLOB_NONE) {
1619 if (bitmap_git)
1620 filter_bitmap_blob_none(bitmap_git, tip_objects,
1621 to_filter);
1622 return 0;
1625 if (filter->choice == LOFC_BLOB_LIMIT) {
1626 if (bitmap_git)
1627 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1628 to_filter,
1629 filter->blob_limit_value);
1630 return 0;
1633 if (filter->choice == LOFC_TREE_DEPTH &&
1634 filter->tree_exclude_depth == 0) {
1635 if (bitmap_git)
1636 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1637 to_filter,
1638 filter->tree_exclude_depth);
1639 return 0;
1642 if (filter->choice == LOFC_OBJECT_TYPE) {
1643 if (bitmap_git)
1644 filter_bitmap_object_type(bitmap_git, tip_objects,
1645 to_filter,
1646 filter->object_type);
1647 return 0;
1650 if (filter->choice == LOFC_COMBINE) {
1651 int i;
1652 for (i = 0; i < filter->sub_nr; i++) {
1653 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1654 &filter->sub[i]) < 0)
1655 return -1;
1657 return 0;
1660 /* filter choice not handled */
1661 return -1;
1664 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1666 return !filter_bitmap(NULL, NULL, NULL, filter);
1670 static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
1671 struct bitmap *result)
1673 struct eindex *eindex = &bitmap_git->ext_index;
1674 uint32_t objects_nr;
1675 size_t i, pos;
1677 objects_nr = bitmap_num_objects(bitmap_git);
1678 pos = objects_nr / BITS_IN_EWORD;
1680 if (pos > result->word_alloc)
1681 pos = result->word_alloc;
1683 memset(result->words, 0x00, sizeof(eword_t) * pos);
1684 for (i = pos * BITS_IN_EWORD; i < objects_nr; i++)
1685 bitmap_unset(result, i);
1687 for (i = 0; i < eindex->count; ++i) {
1688 if (has_object_pack(&eindex->objects[i]->oid))
1689 bitmap_unset(result, objects_nr + i);
1693 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1694 int filter_provided_objects)
1696 unsigned int i;
1697 int use_boundary_traversal;
1699 struct object_list *wants = NULL;
1700 struct object_list *haves = NULL;
1702 struct bitmap *wants_bitmap = NULL;
1703 struct bitmap *haves_bitmap = NULL;
1705 struct bitmap_index *bitmap_git;
1708 * We can't do pathspec limiting with bitmaps, because we don't know
1709 * which commits are associated with which object changes (let alone
1710 * even which objects are associated with which paths).
1712 if (revs->prune)
1713 return NULL;
1715 if (!can_filter_bitmap(&revs->filter))
1716 return NULL;
1718 /* try to open a bitmapped pack, but don't parse it yet
1719 * because we may not need to use it */
1720 CALLOC_ARRAY(bitmap_git, 1);
1721 if (open_bitmap(revs->repo, bitmap_git) < 0)
1722 goto cleanup;
1724 for (i = 0; i < revs->pending.nr; ++i) {
1725 struct object *object = revs->pending.objects[i].item;
1727 if (object->type == OBJ_NONE)
1728 parse_object_or_die(&object->oid, NULL);
1730 while (object->type == OBJ_TAG) {
1731 struct tag *tag = (struct tag *) object;
1733 if (object->flags & UNINTERESTING)
1734 object_list_insert(object, &haves);
1735 else
1736 object_list_insert(object, &wants);
1738 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1739 object->flags |= (tag->object.flags & UNINTERESTING);
1742 if (object->flags & UNINTERESTING)
1743 object_list_insert(object, &haves);
1744 else
1745 object_list_insert(object, &wants);
1748 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1749 if (use_boundary_traversal < 0) {
1750 prepare_repo_settings(revs->repo);
1751 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1754 if (!use_boundary_traversal) {
1756 * if we have a HAVES list, but none of those haves is contained
1757 * in the packfile that has a bitmap, we don't have anything to
1758 * optimize here
1760 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1761 goto cleanup;
1764 /* if we don't want anything, we're done here */
1765 if (!wants)
1766 goto cleanup;
1769 * now we're going to use bitmaps, so load the actual bitmap entries
1770 * from disk. this is the point of no return; after this the rev_list
1771 * becomes invalidated and we must perform the revwalk through bitmaps
1773 if (load_bitmap(revs->repo, bitmap_git) < 0)
1774 goto cleanup;
1776 if (!use_boundary_traversal)
1777 object_array_clear(&revs->pending);
1779 if (haves) {
1780 if (use_boundary_traversal) {
1781 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1782 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1783 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1784 } else {
1785 trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1786 revs->ignore_missing_links = 1;
1787 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1788 reset_revision_walk();
1789 revs->ignore_missing_links = 0;
1790 trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1793 if (!haves_bitmap)
1794 BUG("failed to perform bitmap walk");
1797 if (use_boundary_traversal) {
1798 object_array_clear(&revs->pending);
1799 reset_revision_walk();
1802 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1804 if (!wants_bitmap)
1805 BUG("failed to perform bitmap walk");
1807 if (haves_bitmap)
1808 bitmap_and_not(wants_bitmap, haves_bitmap);
1810 filter_bitmap(bitmap_git,
1811 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1812 wants_bitmap,
1813 &revs->filter);
1815 if (revs->unpacked)
1816 filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap);
1818 bitmap_git->result = wants_bitmap;
1819 bitmap_git->haves = haves_bitmap;
1821 object_list_free(&wants);
1822 object_list_free(&haves);
1824 return bitmap_git;
1826 cleanup:
1827 free_bitmap_index(bitmap_git);
1828 object_list_free(&wants);
1829 object_list_free(&haves);
1830 return NULL;
1834 * -1 means "stop trying further objects"; 0 means we may or may not have
1835 * reused, but you can keep feeding bits.
1837 static int try_partial_reuse(struct bitmap_index *bitmap_git,
1838 struct bitmapped_pack *pack,
1839 size_t bitmap_pos,
1840 uint32_t pack_pos,
1841 struct bitmap *reuse,
1842 struct pack_window **w_curs)
1844 off_t offset, delta_obj_offset;
1845 enum object_type type;
1846 unsigned long size;
1848 if (pack_pos >= pack->p->num_objects)
1849 return -1; /* not actually in the pack */
1851 offset = delta_obj_offset = pack_pos_to_offset(pack->p, pack_pos);
1852 type = unpack_object_header(pack->p, w_curs, &offset, &size);
1853 if (type < 0)
1854 return -1; /* broken packfile, punt */
1856 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1857 off_t base_offset;
1858 uint32_t base_pos;
1859 uint32_t base_bitmap_pos;
1862 * Find the position of the base object so we can look it up
1863 * in our bitmaps. If we can't come up with an offset, or if
1864 * that offset is not in the revidx, the pack is corrupt.
1865 * There's nothing we can do, so just punt on this object,
1866 * and the normal slow path will complain about it in
1867 * more detail.
1869 base_offset = get_delta_base(pack->p, w_curs, &offset, type,
1870 delta_obj_offset);
1871 if (!base_offset)
1872 return 0;
1874 offset_to_pack_pos(pack->p, base_offset, &base_pos);
1876 if (bitmap_is_midx(bitmap_git)) {
1878 * Cross-pack deltas are rejected for now, but could
1879 * theoretically be supported in the future.
1881 * We would need to ensure that we're sending both
1882 * halves of the delta/base pair, regardless of whether
1883 * or not the two cross a pack boundary. If they do,
1884 * then we must convert the delta to an REF_DELTA to
1885 * refer back to the base in the other pack.
1886 * */
1887 if (midx_pair_to_pack_pos(bitmap_git->midx,
1888 pack->pack_int_id,
1889 base_offset,
1890 &base_bitmap_pos) < 0) {
1891 return 0;
1893 } else {
1894 if (offset_to_pack_pos(pack->p, base_offset,
1895 &base_pos) < 0)
1896 return 0;
1898 * We assume delta dependencies always point backwards.
1899 * This lets us do a single pass, and is basically
1900 * always true due to the way OFS_DELTAs work. You would
1901 * not typically find REF_DELTA in a bitmapped pack,
1902 * since we only bitmap packs we write fresh, and
1903 * OFS_DELTA is the default). But let's double check to
1904 * make sure the pack wasn't written with odd
1905 * parameters.
1907 if (base_pos >= pack_pos)
1908 return 0;
1909 base_bitmap_pos = pack->bitmap_pos + base_pos;
1913 * And finally, if we're not sending the base as part of our
1914 * reuse chunk, then don't send this object either. The base
1915 * would come after us, along with other objects not
1916 * necessarily in the pack, which means we'd need to convert
1917 * to REF_DELTA on the fly. Better to just let the normal
1918 * object_entry code path handle it.
1920 if (!bitmap_get(reuse, base_bitmap_pos))
1921 return 0;
1925 * If we got here, then the object is OK to reuse. Mark it.
1927 bitmap_set(reuse, bitmap_pos);
1928 return 0;
1931 static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
1932 struct bitmapped_pack *pack,
1933 struct bitmap *reuse)
1935 struct bitmap *result = bitmap_git->result;
1936 struct pack_window *w_curs = NULL;
1937 size_t pos = pack->bitmap_pos / BITS_IN_EWORD;
1939 if (!pack->bitmap_pos) {
1941 * If we're processing the first (in the case of a MIDX, the
1942 * preferred pack) or the only (in the case of single-pack
1943 * bitmaps) pack, then we can reuse whole words at a time.
1945 * This is because we know that any deltas in this range *must*
1946 * have their bases chosen from the same pack, since:
1948 * - In the single pack case, there is no other pack to choose
1949 * them from.
1951 * - In the MIDX case, the first pack is the preferred pack, so
1952 * all ties are broken in favor of that pack (i.e. the one
1953 * we're currently processing). So any duplicate bases will be
1954 * resolved in favor of the pack we're processing.
1956 while (pos < result->word_alloc &&
1957 pos < pack->bitmap_nr / BITS_IN_EWORD &&
1958 result->words[pos] == (eword_t)~0)
1959 pos++;
1960 memset(reuse->words, 0xFF, pos * sizeof(eword_t));
1963 for (; pos < result->word_alloc; pos++) {
1964 eword_t word = result->words[pos];
1965 size_t offset;
1967 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1968 size_t bit_pos;
1969 uint32_t pack_pos;
1971 if (word >> offset == 0)
1972 break;
1974 offset += ewah_bit_ctz64(word >> offset);
1976 bit_pos = pos * BITS_IN_EWORD + offset;
1977 if (bit_pos < pack->bitmap_pos)
1978 continue;
1979 if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
1980 goto done;
1982 if (bitmap_is_midx(bitmap_git)) {
1983 uint32_t midx_pos;
1984 off_t ofs;
1986 midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
1987 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1989 if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
1990 BUG("could not find object in pack %s "
1991 "at offset %"PRIuMAX" in MIDX",
1992 pack_basename(pack->p), (uintmax_t)ofs);
1993 } else {
1994 pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
1995 if (pack_pos >= pack->p->num_objects)
1996 BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
1997 pack_basename(pack->p), (uintmax_t)pack_pos,
1998 pack->p->num_objects);
2001 if (try_partial_reuse(bitmap_git, pack, bit_pos,
2002 pack_pos, reuse, &w_curs) < 0) {
2004 * try_partial_reuse indicated we couldn't reuse
2005 * any bits, so there is no point in trying more
2006 * bits in the current word, or any other words
2007 * in result.
2009 * Jump out of both loops to avoid future
2010 * unnecessary calls to try_partial_reuse.
2012 goto done;
2017 done:
2018 unuse_pack(&w_curs);
2021 static int bitmapped_pack_cmp(const void *va, const void *vb)
2023 const struct bitmapped_pack *a = va;
2024 const struct bitmapped_pack *b = vb;
2026 if (a->bitmap_pos < b->bitmap_pos)
2027 return -1;
2028 if (a->bitmap_pos > b->bitmap_pos)
2029 return 1;
2030 return 0;
2033 void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2034 struct bitmapped_pack **packs_out,
2035 size_t *packs_nr_out,
2036 struct bitmap **reuse_out,
2037 int multi_pack_reuse)
2039 struct repository *r = the_repository;
2040 struct bitmapped_pack *packs = NULL;
2041 struct bitmap *result = bitmap_git->result;
2042 struct bitmap *reuse;
2043 size_t i;
2044 size_t packs_nr = 0, packs_alloc = 0;
2045 size_t word_alloc;
2046 uint32_t objects_nr = 0;
2048 assert(result);
2050 load_reverse_index(r, bitmap_git);
2052 if (bitmap_is_midx(bitmap_git)) {
2053 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
2054 struct bitmapped_pack pack;
2055 if (nth_bitmapped_pack(r, bitmap_git->midx, &pack, i) < 0) {
2056 warning(_("unable to load pack: '%s', disabling pack-reuse"),
2057 bitmap_git->midx->pack_names[i]);
2058 free(packs);
2059 return;
2062 if (!pack.bitmap_nr)
2063 continue;
2065 if (!multi_pack_reuse && pack.bitmap_pos) {
2067 * If we're only reusing a single pack, skip
2068 * over any packs which are not positioned at
2069 * the beginning of the MIDX bitmap.
2071 * This is consistent with the existing
2072 * single-pack reuse behavior, which only reuses
2073 * parts of the MIDX's preferred pack.
2075 continue;
2078 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2079 memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2081 objects_nr += pack.p->num_objects;
2083 if (!multi_pack_reuse)
2084 break;
2087 QSORT(packs, packs_nr, bitmapped_pack_cmp);
2088 } else {
2089 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2091 packs[packs_nr].p = bitmap_git->pack;
2092 packs[packs_nr].bitmap_nr = bitmap_git->pack->num_objects;
2093 packs[packs_nr].bitmap_pos = 0;
2095 objects_nr = packs[packs_nr++].bitmap_nr;
2098 word_alloc = objects_nr / BITS_IN_EWORD;
2099 if (objects_nr % BITS_IN_EWORD)
2100 word_alloc++;
2101 reuse = bitmap_word_alloc(word_alloc);
2103 for (i = 0; i < packs_nr; i++)
2104 reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse);
2106 if (bitmap_is_empty(reuse)) {
2107 free(packs);
2108 bitmap_free(reuse);
2109 return;
2113 * Drop any reused objects from the result, since they will not
2114 * need to be handled separately.
2116 bitmap_and_not(result, reuse);
2117 *packs_out = packs;
2118 *packs_nr_out = packs_nr;
2119 *reuse_out = reuse;
2122 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2123 struct bitmap *bitmap, const struct object_id *oid)
2125 int idx;
2127 if (!bitmap)
2128 return 0;
2130 idx = bitmap_position(bitmap_git, oid);
2131 return idx >= 0 && bitmap_get(bitmap, idx);
2134 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2135 struct rev_info *revs,
2136 show_reachable_fn show_reachable)
2138 assert(bitmap_git->result);
2140 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2141 if (revs->tree_objects)
2142 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2143 if (revs->blob_objects)
2144 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2145 if (revs->tag_objects)
2146 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2148 show_extended_objects(bitmap_git, revs, show_reachable);
2151 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2152 enum object_type type)
2154 struct bitmap *objects = bitmap_git->result;
2155 struct eindex *eindex = &bitmap_git->ext_index;
2157 uint32_t i = 0, count = 0;
2158 struct ewah_iterator it;
2159 eword_t filter;
2161 init_type_iterator(&it, bitmap_git, type);
2163 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2164 eword_t word = objects->words[i++] & filter;
2165 count += ewah_bit_popcount64(word);
2168 for (i = 0; i < eindex->count; ++i) {
2169 if (eindex->objects[i]->type == type &&
2170 bitmap_get(objects,
2171 st_add(bitmap_num_objects(bitmap_git), i)))
2172 count++;
2175 return count;
2178 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2179 uint32_t *commits, uint32_t *trees,
2180 uint32_t *blobs, uint32_t *tags)
2182 assert(bitmap_git->result);
2184 if (commits)
2185 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2187 if (trees)
2188 *trees = count_object_type(bitmap_git, OBJ_TREE);
2190 if (blobs)
2191 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2193 if (tags)
2194 *tags = count_object_type(bitmap_git, OBJ_TAG);
2197 struct bitmap_test_data {
2198 struct bitmap_index *bitmap_git;
2199 struct bitmap *base;
2200 struct bitmap *commits;
2201 struct bitmap *trees;
2202 struct bitmap *blobs;
2203 struct bitmap *tags;
2204 struct progress *prg;
2205 size_t seen;
2208 static void test_bitmap_type(struct bitmap_test_data *tdata,
2209 struct object *obj, int pos)
2211 enum object_type bitmap_type = OBJ_NONE;
2212 int bitmaps_nr = 0;
2214 if (bitmap_get(tdata->commits, pos)) {
2215 bitmap_type = OBJ_COMMIT;
2216 bitmaps_nr++;
2218 if (bitmap_get(tdata->trees, pos)) {
2219 bitmap_type = OBJ_TREE;
2220 bitmaps_nr++;
2222 if (bitmap_get(tdata->blobs, pos)) {
2223 bitmap_type = OBJ_BLOB;
2224 bitmaps_nr++;
2226 if (bitmap_get(tdata->tags, pos)) {
2227 bitmap_type = OBJ_TAG;
2228 bitmaps_nr++;
2231 if (bitmap_type == OBJ_NONE)
2232 die(_("object '%s' not found in type bitmaps"),
2233 oid_to_hex(&obj->oid));
2235 if (bitmaps_nr > 1)
2236 die(_("object '%s' does not have a unique type"),
2237 oid_to_hex(&obj->oid));
2239 if (bitmap_type != obj->type)
2240 die(_("object '%s': real type '%s', expected: '%s'"),
2241 oid_to_hex(&obj->oid),
2242 type_name(obj->type),
2243 type_name(bitmap_type));
2246 static void test_show_object(struct object *object,
2247 const char *name UNUSED,
2248 void *data)
2250 struct bitmap_test_data *tdata = data;
2251 int bitmap_pos;
2253 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2254 if (bitmap_pos < 0)
2255 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2256 test_bitmap_type(tdata, object, bitmap_pos);
2258 bitmap_set(tdata->base, bitmap_pos);
2259 display_progress(tdata->prg, ++tdata->seen);
2262 static void test_show_commit(struct commit *commit, void *data)
2264 struct bitmap_test_data *tdata = data;
2265 int bitmap_pos;
2267 bitmap_pos = bitmap_position(tdata->bitmap_git,
2268 &commit->object.oid);
2269 if (bitmap_pos < 0)
2270 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2271 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2273 bitmap_set(tdata->base, bitmap_pos);
2274 display_progress(tdata->prg, ++tdata->seen);
2277 void test_bitmap_walk(struct rev_info *revs)
2279 struct object *root;
2280 struct bitmap *result = NULL;
2281 size_t result_popcnt;
2282 struct bitmap_test_data tdata;
2283 struct bitmap_index *bitmap_git;
2284 struct ewah_bitmap *bm;
2286 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2287 die(_("failed to load bitmap indexes"));
2289 if (revs->pending.nr != 1)
2290 die(_("you must specify exactly one commit to test"));
2292 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2293 bitmap_git->version,
2294 bitmap_git->entry_count,
2295 bitmap_git->table_lookup ? "" : " loaded");
2297 root = revs->pending.objects[0].item;
2298 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2300 if (bm) {
2301 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2302 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2304 result = ewah_to_bitmap(bm);
2307 if (!result)
2308 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2310 revs->tag_objects = 1;
2311 revs->tree_objects = 1;
2312 revs->blob_objects = 1;
2314 result_popcnt = bitmap_popcount(result);
2316 if (prepare_revision_walk(revs))
2317 die(_("revision walk setup failed"));
2319 tdata.bitmap_git = bitmap_git;
2320 tdata.base = bitmap_new();
2321 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2322 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2323 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2324 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2325 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2326 tdata.seen = 0;
2328 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2330 stop_progress(&tdata.prg);
2332 if (bitmap_equals(result, tdata.base))
2333 fprintf_ln(stderr, "OK!");
2334 else
2335 die(_("mismatch in bitmap results"));
2337 bitmap_free(result);
2338 bitmap_free(tdata.base);
2339 bitmap_free(tdata.commits);
2340 bitmap_free(tdata.trees);
2341 bitmap_free(tdata.blobs);
2342 bitmap_free(tdata.tags);
2343 free_bitmap_index(bitmap_git);
2346 int test_bitmap_commits(struct repository *r)
2348 struct object_id oid;
2349 MAYBE_UNUSED void *value;
2350 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2352 if (!bitmap_git)
2353 die(_("failed to load bitmap indexes"));
2356 * As this function is only used to print bitmap selected
2357 * commits, we don't have to read the commit table.
2359 if (bitmap_git->table_lookup) {
2360 if (load_bitmap_entries_v1(bitmap_git) < 0)
2361 die(_("failed to load bitmap indexes"));
2364 kh_foreach(bitmap_git->bitmaps, oid, value, {
2365 printf_ln("%s", oid_to_hex(&oid));
2368 free_bitmap_index(bitmap_git);
2370 return 0;
2373 int test_bitmap_hashes(struct repository *r)
2375 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2376 struct object_id oid;
2377 uint32_t i, index_pos;
2379 if (!bitmap_git || !bitmap_git->hashes)
2380 goto cleanup;
2382 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2383 if (bitmap_is_midx(bitmap_git))
2384 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2385 else
2386 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2388 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2390 printf_ln("%s %"PRIu32"",
2391 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2394 cleanup:
2395 free_bitmap_index(bitmap_git);
2397 return 0;
2400 int rebuild_bitmap(const uint32_t *reposition,
2401 struct ewah_bitmap *source,
2402 struct bitmap *dest)
2404 uint32_t pos = 0;
2405 struct ewah_iterator it;
2406 eword_t word;
2408 ewah_iterator_init(&it, source);
2410 while (ewah_iterator_next(&word, &it)) {
2411 uint32_t offset, bit_pos;
2413 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2414 if ((word >> offset) == 0)
2415 break;
2417 offset += ewah_bit_ctz64(word >> offset);
2419 bit_pos = reposition[pos + offset];
2420 if (bit_pos > 0)
2421 bitmap_set(dest, bit_pos - 1);
2422 else /* can't reuse, we don't have the object */
2423 return -1;
2426 pos += BITS_IN_EWORD;
2428 return 0;
2431 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2432 struct packing_data *mapping)
2434 struct repository *r = the_repository;
2435 uint32_t i, num_objects;
2436 uint32_t *reposition;
2438 if (!bitmap_is_midx(bitmap_git))
2439 load_reverse_index(r, bitmap_git);
2440 else if (load_midx_revindex(bitmap_git->midx))
2441 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2442 "extension");
2444 num_objects = bitmap_num_objects(bitmap_git);
2445 CALLOC_ARRAY(reposition, num_objects);
2447 for (i = 0; i < num_objects; ++i) {
2448 struct object_id oid;
2449 struct object_entry *oe;
2450 uint32_t index_pos;
2452 if (bitmap_is_midx(bitmap_git))
2453 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2454 else
2455 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2456 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2457 oe = packlist_find(mapping, &oid);
2459 if (oe) {
2460 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2461 if (bitmap_git->hashes && !oe->hash)
2462 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2466 return reposition;
2469 void free_bitmap_index(struct bitmap_index *b)
2471 if (!b)
2472 return;
2474 if (b->map)
2475 munmap(b->map, b->map_size);
2476 ewah_pool_free(b->commits);
2477 ewah_pool_free(b->trees);
2478 ewah_pool_free(b->blobs);
2479 ewah_pool_free(b->tags);
2480 if (b->bitmaps) {
2481 struct stored_bitmap *sb;
2482 kh_foreach_value(b->bitmaps, sb, {
2483 ewah_pool_free(sb->root);
2484 free(sb);
2487 kh_destroy_oid_map(b->bitmaps);
2488 free(b->ext_index.objects);
2489 free(b->ext_index.hashes);
2490 kh_destroy_oid_pos(b->ext_index.positions);
2491 bitmap_free(b->result);
2492 bitmap_free(b->haves);
2493 if (bitmap_is_midx(b)) {
2495 * Multi-pack bitmaps need to have resources associated with
2496 * their on-disk reverse indexes unmapped so that stale .rev and
2497 * .bitmap files can be removed.
2499 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2500 * written in the same 'git multi-pack-index write --bitmap'
2501 * process. Close resources so they can be removed safely on
2502 * platforms like Windows.
2504 close_midx_revindex(b->midx);
2506 free(b);
2509 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2510 const struct object_id *oid)
2512 return bitmap_git &&
2513 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2516 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2517 enum object_type object_type)
2519 struct bitmap *result = bitmap_git->result;
2520 off_t total = 0;
2521 struct ewah_iterator it;
2522 eword_t filter;
2523 size_t i;
2525 init_type_iterator(&it, bitmap_git, object_type);
2526 for (i = 0; i < result->word_alloc &&
2527 ewah_iterator_next(&filter, &it); i++) {
2528 eword_t word = result->words[i] & filter;
2529 size_t base = (i * BITS_IN_EWORD);
2530 unsigned offset;
2532 if (!word)
2533 continue;
2535 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2536 if ((word >> offset) == 0)
2537 break;
2539 offset += ewah_bit_ctz64(word >> offset);
2541 if (bitmap_is_midx(bitmap_git)) {
2542 uint32_t pack_pos;
2543 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2544 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2546 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2547 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2549 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2550 struct object_id oid;
2551 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2553 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2554 oid_to_hex(&oid),
2555 pack->pack_name,
2556 (uintmax_t)offset);
2559 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2560 } else {
2561 size_t pos = base + offset;
2562 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2563 pack_pos_to_offset(bitmap_git->pack, pos);
2568 return total;
2571 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2573 struct bitmap *result = bitmap_git->result;
2574 struct eindex *eindex = &bitmap_git->ext_index;
2575 off_t total = 0;
2576 struct object_info oi = OBJECT_INFO_INIT;
2577 off_t object_size;
2578 size_t i;
2580 oi.disk_sizep = &object_size;
2582 for (i = 0; i < eindex->count; i++) {
2583 struct object *obj = eindex->objects[i];
2585 if (!bitmap_get(result,
2586 st_add(bitmap_num_objects(bitmap_git), i)))
2587 continue;
2589 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2590 die(_("unable to get disk usage of '%s'"),
2591 oid_to_hex(&obj->oid));
2593 total += object_size;
2595 return total;
2598 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2599 struct rev_info *revs)
2601 off_t total = 0;
2603 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2604 if (revs->tree_objects)
2605 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2606 if (revs->blob_objects)
2607 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2608 if (revs->tag_objects)
2609 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2611 total += get_disk_usage_for_extended(bitmap_git);
2613 return total;
2616 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2618 return !!bitmap_git->midx;
2621 const struct string_list *bitmap_preferred_tips(struct repository *r)
2623 const struct string_list *dest;
2625 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2626 return dest;
2627 return NULL;
2630 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2632 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2633 struct string_list_item *item;
2635 if (!preferred_tips)
2636 return 0;
2638 for_each_string_list_item(item, preferred_tips) {
2639 if (starts_with(refname, item->string))
2640 return 1;
2643 return 0;
2646 static int verify_bitmap_file(const char *name)
2648 struct stat st;
2649 unsigned char *data;
2650 int fd = git_open(name);
2651 int res = 0;
2653 /* It is OK to not have the file. */
2654 if (fd < 0 || fstat(fd, &st)) {
2655 if (fd >= 0)
2656 close(fd);
2657 return 0;
2660 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2661 close(fd);
2662 if (!hashfile_checksum_valid(data, st.st_size))
2663 res = error(_("bitmap file '%s' has invalid checksum"),
2664 name);
2666 munmap(data, st.st_size);
2667 return res;
2670 int verify_bitmap_files(struct repository *r)
2672 int res = 0;
2674 for (struct multi_pack_index *m = get_multi_pack_index(r);
2675 m; m = m->next) {
2676 char *midx_bitmap_name = midx_bitmap_filename(m);
2677 res |= verify_bitmap_file(midx_bitmap_name);
2678 free(midx_bitmap_name);
2681 for (struct packed_git *p = get_all_packs(r);
2682 p; p = p->next) {
2683 char *pack_bitmap_name = pack_bitmap_filename(p);
2684 res |= verify_bitmap_file(pack_bitmap_name);
2685 free(pack_bitmap_name);
2688 return res;