Sync with 'maint'
[git/gitster.git] / pack-bitmap.c
blob4fa9dfc771a30545281b8a5e2b7d26dc5682c285
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "commit.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "strbuf.h"
8 #include "tag.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "progress.h"
12 #include "list-objects.h"
13 #include "pack.h"
14 #include "pack-bitmap.h"
15 #include "pack-revindex.h"
16 #include "pack-objects.h"
17 #include "packfile.h"
18 #include "repository.h"
19 #include "trace2.h"
20 #include "object-file.h"
21 #include "object-store-ll.h"
22 #include "list-objects-filter-options.h"
23 #include "midx.h"
24 #include "config.h"
25 #include "pseudo-merge.h"
28 * An entry on the bitmap index, representing the bitmap for a given
29 * commit.
31 struct stored_bitmap {
32 struct object_id oid;
33 struct ewah_bitmap *root;
34 struct stored_bitmap *xor;
35 int flags;
39 * The active bitmap index for a repository. By design, repositories only have
40 * a single bitmap index available (the index for the biggest packfile in
41 * the repository), since bitmap indexes need full closure.
43 * If there is more than one bitmap index available (e.g. because of alternates),
44 * the active bitmap index is the largest one.
46 struct bitmap_index {
48 * The pack or multi-pack index (MIDX) that this bitmap index belongs
49 * to.
51 * Exactly one of these must be non-NULL; this specifies the object
52 * order used to interpret this bitmap.
54 struct packed_git *pack;
55 struct multi_pack_index *midx;
57 /* mmapped buffer of the whole bitmap index */
58 unsigned char *map;
59 size_t map_size; /* size of the mmaped buffer */
60 size_t map_pos; /* current position when loading the index */
63 * Type indexes.
65 * Each bitmap marks which objects in the packfile are of the given
66 * type. This provides type information when yielding the objects from
67 * the packfile during a walk, which allows for better delta bases.
69 struct ewah_bitmap *commits;
70 struct ewah_bitmap *trees;
71 struct ewah_bitmap *blobs;
72 struct ewah_bitmap *tags;
74 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
75 kh_oid_map_t *bitmaps;
77 /* Number of bitmapped commits */
78 uint32_t entry_count;
80 /* If not NULL, this is a name-hash cache pointing into map. */
81 uint32_t *hashes;
83 /* The checksum of the packfile or MIDX; points into map. */
84 const unsigned char *checksum;
87 * If not NULL, this point into the commit table extension
88 * (within the memory mapped region `map`).
90 unsigned char *table_lookup;
92 /* This contains the pseudo-merge cache within 'map' (if found). */
93 struct pseudo_merge_map pseudo_merges;
96 * Extended index.
98 * When trying to perform bitmap operations with objects that are not
99 * packed in `pack`, these objects are added to this "fake index" and
100 * are assumed to appear at the end of the packfile for all operations
102 struct eindex {
103 struct object **objects;
104 uint32_t *hashes;
105 uint32_t count, alloc;
106 kh_oid_pos_t *positions;
107 } ext_index;
109 /* Bitmap result of the last performed walk */
110 struct bitmap *result;
112 /* "have" bitmap from the last performed walk */
113 struct bitmap *haves;
115 /* Version of the bitmap index */
116 unsigned int version;
119 static int pseudo_merges_satisfied_nr;
120 static int pseudo_merges_cascades_nr;
121 static int existing_bitmaps_hits_nr;
122 static int existing_bitmaps_misses_nr;
123 static int roots_with_bitmaps_nr;
124 static int roots_without_bitmaps_nr;
126 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
128 struct ewah_bitmap *parent;
129 struct ewah_bitmap *composed;
131 if (!st->xor)
132 return st->root;
134 composed = ewah_pool_new();
135 parent = lookup_stored_bitmap(st->xor);
136 ewah_xor(st->root, parent, composed);
138 ewah_pool_free(st->root);
139 st->root = composed;
140 st->xor = NULL;
142 return composed;
145 struct ewah_bitmap *read_bitmap(const unsigned char *map,
146 size_t map_size, size_t *map_pos)
148 struct ewah_bitmap *b = ewah_pool_new();
150 ssize_t bitmap_size = ewah_read_mmap(b, map + *map_pos,
151 map_size - *map_pos);
153 if (bitmap_size < 0) {
154 error(_("failed to load bitmap index (corrupted?)"));
155 ewah_pool_free(b);
156 return NULL;
159 *map_pos += bitmap_size;
161 return b;
165 * Read a bitmap from the current read position on the mmaped
166 * index, and increase the read position accordingly
168 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
170 return read_bitmap(index->map, index->map_size, &index->map_pos);
173 static uint32_t bitmap_num_objects(struct bitmap_index *index)
175 if (index->midx)
176 return index->midx->num_objects;
177 return index->pack->num_objects;
180 static int load_bitmap_header(struct bitmap_index *index)
182 struct bitmap_disk_header *header = (void *)index->map;
183 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
185 if (index->map_size < header_size + the_hash_algo->rawsz)
186 return error(_("corrupted bitmap index (too small)"));
188 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
189 return error(_("corrupted bitmap index file (wrong header)"));
191 index->version = ntohs(header->version);
192 if (index->version != 1)
193 return error(_("unsupported version '%d' for bitmap index file"), index->version);
195 /* Parse known bitmap format options */
197 uint32_t flags = ntohs(header->options);
198 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
199 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
201 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
202 BUG("unsupported options for bitmap index file "
203 "(Git requires BITMAP_OPT_FULL_DAG)");
205 if (flags & BITMAP_OPT_HASH_CACHE) {
206 if (cache_size > index_end - index->map - header_size)
207 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
208 index->hashes = (void *)(index_end - cache_size);
209 index_end -= cache_size;
212 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
213 size_t table_size = st_mult(ntohl(header->entry_count),
214 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
215 if (table_size > index_end - index->map - header_size)
216 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
217 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
218 index->table_lookup = (void *)(index_end - table_size);
219 index_end -= table_size;
222 if (flags & BITMAP_OPT_PSEUDO_MERGES) {
223 unsigned char *pseudo_merge_ofs;
224 size_t table_size;
225 uint32_t i;
227 if (sizeof(table_size) > index_end - index->map - header_size)
228 return error(_("corrupted bitmap index file (too short to fit pseudo-merge table header)"));
230 table_size = get_be64(index_end - 8);
231 if (table_size > index_end - index->map - header_size)
232 return error(_("corrupted bitmap index file (too short to fit pseudo-merge table)"));
234 if (git_env_bool("GIT_TEST_USE_PSEUDO_MERGES", 1)) {
235 const unsigned char *ext = (index_end - table_size);
237 index->pseudo_merges.map = index->map;
238 index->pseudo_merges.map_size = index->map_size;
239 index->pseudo_merges.commits = ext + get_be64(index_end - 16);
240 index->pseudo_merges.commits_nr = get_be32(index_end - 20);
241 index->pseudo_merges.nr = get_be32(index_end - 24);
243 if (st_add(st_mult(index->pseudo_merges.nr,
244 sizeof(uint64_t)),
245 24) > table_size)
246 return error(_("corrupted bitmap index file, pseudo-merge table too short"));
248 CALLOC_ARRAY(index->pseudo_merges.v,
249 index->pseudo_merges.nr);
251 pseudo_merge_ofs = index_end - 24 -
252 (index->pseudo_merges.nr * sizeof(uint64_t));
253 for (i = 0; i < index->pseudo_merges.nr; i++) {
254 index->pseudo_merges.v[i].at = get_be64(pseudo_merge_ofs);
255 pseudo_merge_ofs += sizeof(uint64_t);
259 index_end -= table_size;
263 index->entry_count = ntohl(header->entry_count);
264 index->checksum = header->checksum;
265 index->map_pos += header_size;
266 return 0;
269 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
270 struct ewah_bitmap *root,
271 const struct object_id *oid,
272 struct stored_bitmap *xor_with,
273 int flags)
275 struct stored_bitmap *stored;
276 khiter_t hash_pos;
277 int ret;
279 stored = xmalloc(sizeof(struct stored_bitmap));
280 stored->root = root;
281 stored->xor = xor_with;
282 stored->flags = flags;
283 oidcpy(&stored->oid, oid);
285 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
288 * A 0 return code means the insertion succeeded with no changes,
289 * because the SHA1 already existed on the map. This is bad, there
290 * shouldn't be duplicated commits in the index.
292 if (ret == 0) {
293 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
294 return NULL;
297 kh_value(index->bitmaps, hash_pos) = stored;
298 return stored;
301 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
303 uint32_t result = get_be32(buffer + *pos);
304 (*pos) += sizeof(result);
305 return result;
308 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
310 return buffer[(*pos)++];
313 #define MAX_XOR_OFFSET 160
315 static int nth_bitmap_object_oid(struct bitmap_index *index,
316 struct object_id *oid,
317 uint32_t n)
319 if (index->midx)
320 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
321 return nth_packed_object_id(oid, index->pack, n);
324 static int load_bitmap_entries_v1(struct bitmap_index *index)
326 uint32_t i;
327 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
329 for (i = 0; i < index->entry_count; ++i) {
330 int xor_offset, flags;
331 struct ewah_bitmap *bitmap = NULL;
332 struct stored_bitmap *xor_bitmap = NULL;
333 uint32_t commit_idx_pos;
334 struct object_id oid;
336 if (index->map_size - index->map_pos < 6)
337 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
339 commit_idx_pos = read_be32(index->map, &index->map_pos);
340 xor_offset = read_u8(index->map, &index->map_pos);
341 flags = read_u8(index->map, &index->map_pos);
343 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
344 return error(_("corrupt ewah bitmap: commit index %u out of range"),
345 (unsigned)commit_idx_pos);
347 bitmap = read_bitmap_1(index);
348 if (!bitmap)
349 return -1;
351 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
352 return error(_("corrupted bitmap pack index"));
354 if (xor_offset > 0) {
355 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
357 if (!xor_bitmap)
358 return error(_("invalid XOR offset in bitmap pack index"));
361 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
362 index, bitmap, &oid, xor_bitmap, flags);
365 return 0;
368 char *midx_bitmap_filename(struct multi_pack_index *midx)
370 struct strbuf buf = STRBUF_INIT;
371 get_midx_filename_ext(&buf, midx->object_dir, get_midx_checksum(midx),
372 MIDX_EXT_BITMAP);
374 return strbuf_detach(&buf, NULL);
377 char *pack_bitmap_filename(struct packed_git *p)
379 size_t len;
381 if (!strip_suffix(p->pack_name, ".pack", &len))
382 BUG("pack_name does not end in .pack");
383 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
386 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
387 struct multi_pack_index *midx)
389 struct stat st;
390 char *bitmap_name = midx_bitmap_filename(midx);
391 int fd = git_open(bitmap_name);
392 uint32_t i, preferred_pack;
393 struct packed_git *preferred;
395 if (fd < 0) {
396 if (errno != ENOENT)
397 warning_errno("cannot open '%s'", bitmap_name);
398 free(bitmap_name);
399 return -1;
401 free(bitmap_name);
403 if (fstat(fd, &st)) {
404 error_errno(_("cannot fstat bitmap file"));
405 close(fd);
406 return -1;
409 if (bitmap_git->pack || bitmap_git->midx) {
410 struct strbuf buf = STRBUF_INIT;
411 get_midx_filename(&buf, midx->object_dir);
412 trace2_data_string("bitmap", the_repository,
413 "ignoring extra midx bitmap file", buf.buf);
414 close(fd);
415 strbuf_release(&buf);
416 return -1;
419 bitmap_git->midx = midx;
420 bitmap_git->map_size = xsize_t(st.st_size);
421 bitmap_git->map_pos = 0;
422 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
423 MAP_PRIVATE, fd, 0);
424 close(fd);
426 if (load_bitmap_header(bitmap_git) < 0)
427 goto cleanup;
429 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum,
430 the_repository->hash_algo)) {
431 error(_("checksum doesn't match in MIDX and bitmap"));
432 goto cleanup;
435 if (load_midx_revindex(bitmap_git->midx)) {
436 warning(_("multi-pack bitmap is missing required reverse index"));
437 goto cleanup;
440 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
441 if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
442 warning(_("could not open pack %s"),
443 bitmap_git->midx->pack_names[i]);
444 goto cleanup;
448 if (midx_preferred_pack(bitmap_git->midx, &preferred_pack) < 0) {
449 warning(_("could not determine MIDX preferred pack"));
450 goto cleanup;
453 preferred = bitmap_git->midx->packs[preferred_pack];
454 if (!is_pack_valid(preferred)) {
455 warning(_("preferred pack (%s) is invalid"),
456 preferred->pack_name);
457 goto cleanup;
460 return 0;
462 cleanup:
463 munmap(bitmap_git->map, bitmap_git->map_size);
464 bitmap_git->map_size = 0;
465 bitmap_git->map_pos = 0;
466 bitmap_git->map = NULL;
467 bitmap_git->midx = NULL;
468 return -1;
471 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
473 int fd;
474 struct stat st;
475 char *bitmap_name;
477 bitmap_name = pack_bitmap_filename(packfile);
478 fd = git_open(bitmap_name);
480 if (fd < 0) {
481 if (errno != ENOENT)
482 warning_errno("cannot open '%s'", bitmap_name);
483 free(bitmap_name);
484 return -1;
486 free(bitmap_name);
488 if (fstat(fd, &st)) {
489 error_errno(_("cannot fstat bitmap file"));
490 close(fd);
491 return -1;
494 if (bitmap_git->pack || bitmap_git->midx) {
495 trace2_data_string("bitmap", the_repository,
496 "ignoring extra bitmap file", packfile->pack_name);
497 close(fd);
498 return -1;
501 if (!is_pack_valid(packfile)) {
502 close(fd);
503 return -1;
506 bitmap_git->pack = packfile;
507 bitmap_git->map_size = xsize_t(st.st_size);
508 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
509 bitmap_git->map_pos = 0;
510 close(fd);
512 if (load_bitmap_header(bitmap_git) < 0) {
513 munmap(bitmap_git->map, bitmap_git->map_size);
514 bitmap_git->map = NULL;
515 bitmap_git->map_size = 0;
516 bitmap_git->map_pos = 0;
517 bitmap_git->pack = NULL;
518 return -1;
521 trace2_data_string("bitmap", the_repository, "opened bitmap file",
522 packfile->pack_name);
523 return 0;
526 static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
528 if (bitmap_is_midx(bitmap_git)) {
529 uint32_t i;
530 int ret;
533 * The multi-pack-index's .rev file is already loaded via
534 * open_pack_bitmap_1().
536 * But we still need to open the individual pack .rev files,
537 * since we will need to make use of them in pack-objects.
539 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
540 ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
541 if (ret)
542 return ret;
544 return 0;
546 return load_pack_revindex(r, bitmap_git->pack);
549 static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
551 assert(bitmap_git->map);
553 bitmap_git->bitmaps = kh_init_oid_map();
554 bitmap_git->ext_index.positions = kh_init_oid_pos();
556 if (load_reverse_index(r, bitmap_git))
557 goto failed;
559 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
560 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
561 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
562 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
563 goto failed;
565 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
566 goto failed;
568 return 0;
570 failed:
571 munmap(bitmap_git->map, bitmap_git->map_size);
572 bitmap_git->map = NULL;
573 bitmap_git->map_size = 0;
575 kh_destroy_oid_map(bitmap_git->bitmaps);
576 bitmap_git->bitmaps = NULL;
578 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
579 bitmap_git->ext_index.positions = NULL;
581 return -1;
584 static int open_pack_bitmap(struct repository *r,
585 struct bitmap_index *bitmap_git)
587 struct packed_git *p;
588 int ret = -1;
590 for (p = get_all_packs(r); p; p = p->next) {
591 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
592 ret = 0;
594 * The only reason to keep looking is to report
595 * duplicates.
597 if (!trace2_is_enabled())
598 break;
602 return ret;
605 static int open_midx_bitmap(struct repository *r,
606 struct bitmap_index *bitmap_git)
608 int ret = -1;
609 struct multi_pack_index *midx;
611 assert(!bitmap_git->map);
613 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
614 if (!open_midx_bitmap_1(bitmap_git, midx))
615 ret = 0;
617 return ret;
620 static int open_bitmap(struct repository *r,
621 struct bitmap_index *bitmap_git)
623 int found;
625 assert(!bitmap_git->map);
627 found = !open_midx_bitmap(r, bitmap_git);
630 * these will all be skipped if we opened a midx bitmap; but run it
631 * anyway if tracing is enabled to report the duplicates
633 if (!found || trace2_is_enabled())
634 found |= !open_pack_bitmap(r, bitmap_git);
636 return found ? 0 : -1;
639 struct bitmap_index *prepare_bitmap_git(struct repository *r)
641 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
643 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
644 return bitmap_git;
646 free_bitmap_index(bitmap_git);
647 return NULL;
650 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
652 struct repository *r = the_repository;
653 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
655 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
656 return bitmap_git;
658 free_bitmap_index(bitmap_git);
659 return NULL;
662 struct include_data {
663 struct bitmap_index *bitmap_git;
664 struct bitmap *base;
665 struct bitmap *seen;
668 struct bitmap_lookup_table_triplet {
669 uint32_t commit_pos;
670 uint64_t offset;
671 uint32_t xor_row;
674 struct bitmap_lookup_table_xor_item {
675 struct object_id oid;
676 uint64_t offset;
680 * Given a `triplet` struct pointer and pointer `p`, this
681 * function reads the triplet beginning at `p` into the struct.
682 * Note that this function assumes that there is enough memory
683 * left for filling the `triplet` struct from `p`.
685 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
686 const unsigned char *p)
688 if (!triplet)
689 return -1;
691 triplet->commit_pos = get_be32(p);
692 p += sizeof(uint32_t);
693 triplet->offset = get_be64(p);
694 p += sizeof(uint64_t);
695 triplet->xor_row = get_be32(p);
696 return 0;
700 * This function gets the raw triplet from `row`'th row in the
701 * lookup table and fills that data to the `triplet`.
703 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
704 uint32_t pos,
705 struct bitmap_lookup_table_triplet *triplet)
707 unsigned char *p = NULL;
708 if (pos >= bitmap_git->entry_count)
709 return error(_("corrupt bitmap lookup table: triplet position out of index"));
711 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
713 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
717 * Searches for a matching triplet. `commit_pos` is a pointer
718 * to the wanted commit position value. `table_entry` points to
719 * a triplet in lookup table. The first 4 bytes of each
720 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
722 static int triplet_cmp(const void *commit_pos, const void *table_entry)
725 uint32_t a = *(uint32_t *)commit_pos;
726 uint32_t b = get_be32(table_entry);
727 if (a > b)
728 return 1;
729 else if (a < b)
730 return -1;
732 return 0;
735 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
736 struct object_id *oid,
737 uint32_t *result)
739 int found;
741 if (bitmap_is_midx(bitmap_git))
742 found = bsearch_midx(oid, bitmap_git->midx, result);
743 else
744 found = bsearch_pack(oid, bitmap_git->pack, result);
746 return found;
750 * `bsearch_triplet_by_pos` function searches for the raw triplet
751 * having commit position same as `commit_pos` and fills `triplet`
752 * object from the raw triplet. Returns 1 on success and 0 on
753 * failure.
755 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
756 struct bitmap_index *bitmap_git,
757 struct bitmap_lookup_table_triplet *triplet)
759 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
760 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
762 if (!p)
763 return -1;
765 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
768 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
769 struct commit *commit)
771 uint32_t commit_pos, xor_row;
772 uint64_t offset;
773 int flags;
774 struct bitmap_lookup_table_triplet triplet;
775 struct object_id *oid = &commit->object.oid;
776 struct ewah_bitmap *bitmap;
777 struct stored_bitmap *xor_bitmap = NULL;
778 const int bitmap_header_size = 6;
779 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
780 static size_t xor_items_nr = 0, xor_items_alloc = 0;
781 static int is_corrupt = 0;
782 int xor_flags;
783 khiter_t hash_pos;
784 struct bitmap_lookup_table_xor_item *xor_item;
786 if (is_corrupt)
787 return NULL;
789 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
790 return NULL;
792 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
793 return NULL;
795 xor_items_nr = 0;
796 offset = triplet.offset;
797 xor_row = triplet.xor_row;
799 while (xor_row != 0xffffffff) {
800 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
802 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
803 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
804 goto corrupt;
807 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
808 goto corrupt;
810 xor_item = &xor_items[xor_items_nr];
811 xor_item->offset = triplet.offset;
813 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
814 error(_("corrupt bitmap lookup table: commit index %u out of range"),
815 triplet.commit_pos);
816 goto corrupt;
819 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
822 * If desired bitmap is already stored, we don't need
823 * to iterate further. Because we know that bitmaps
824 * that are needed to be parsed to parse this bitmap
825 * has already been stored. So, assign this stored bitmap
826 * to the xor_bitmap.
828 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
829 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
830 break;
831 xor_items_nr++;
832 xor_row = triplet.xor_row;
835 while (xor_items_nr) {
836 xor_item = &xor_items[xor_items_nr - 1];
837 bitmap_git->map_pos = xor_item->offset;
838 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
839 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
840 oid_to_hex(&xor_item->oid));
841 goto corrupt;
844 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
845 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
846 bitmap = read_bitmap_1(bitmap_git);
848 if (!bitmap)
849 goto corrupt;
851 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
852 xor_items_nr--;
855 bitmap_git->map_pos = offset;
856 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
857 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
858 oid_to_hex(oid));
859 goto corrupt;
863 * Don't bother reading the commit's index position or its xor
864 * offset:
866 * - The commit's index position is irrelevant to us, since
867 * load_bitmap_entries_v1 only uses it to learn the object
868 * id which is used to compute the hashmap's key. We already
869 * have an object id, so no need to look it up again.
871 * - The xor_offset is unusable for us, since it specifies how
872 * many entries previous to ours we should look at. This
873 * makes sense when reading the bitmaps sequentially (as in
874 * load_bitmap_entries_v1()), since we can keep track of
875 * each bitmap as we read them.
877 * But it can't work for us, since the bitmap's don't have a
878 * fixed size. So we learn the position of the xor'd bitmap
879 * from the commit table (and resolve it to a bitmap in the
880 * above if-statement).
882 * Instead, we can skip ahead and immediately read the flags and
883 * ewah bitmap.
885 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
886 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
887 bitmap = read_bitmap_1(bitmap_git);
889 if (!bitmap)
890 goto corrupt;
892 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
894 corrupt:
895 free(xor_items);
896 is_corrupt = 1;
897 return NULL;
900 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
901 struct commit *commit)
903 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
904 commit->object.oid);
905 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
906 struct stored_bitmap *bitmap = NULL;
907 if (!bitmap_git->table_lookup)
908 return NULL;
910 /* this is a fairly hot codepath - no trace2_region please */
911 /* NEEDSWORK: cache misses aren't recorded */
912 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
913 if (!bitmap)
914 return NULL;
915 return lookup_stored_bitmap(bitmap);
917 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
920 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
921 const struct object_id *oid)
923 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
924 khiter_t pos = kh_get_oid_pos(positions, *oid);
926 if (pos < kh_end(positions)) {
927 int bitmap_pos = kh_value(positions, pos);
928 return bitmap_pos + bitmap_num_objects(bitmap_git);
931 return -1;
934 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
935 const struct object_id *oid)
937 uint32_t pos;
938 off_t offset = find_pack_entry_one(oid, bitmap_git->pack);
939 if (!offset)
940 return -1;
942 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
943 return -1;
944 return pos;
947 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
948 const struct object_id *oid)
950 uint32_t want, got;
951 if (!bsearch_midx(oid, bitmap_git->midx, &want))
952 return -1;
954 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
955 return -1;
956 return got;
959 static int bitmap_position(struct bitmap_index *bitmap_git,
960 const struct object_id *oid)
962 int pos;
963 if (bitmap_is_midx(bitmap_git))
964 pos = bitmap_position_midx(bitmap_git, oid);
965 else
966 pos = bitmap_position_packfile(bitmap_git, oid);
967 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
970 static int ext_index_add_object(struct bitmap_index *bitmap_git,
971 struct object *object, const char *name)
973 struct eindex *eindex = &bitmap_git->ext_index;
975 khiter_t hash_pos;
976 int hash_ret;
977 int bitmap_pos;
979 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
980 if (hash_ret > 0) {
981 if (eindex->count >= eindex->alloc) {
982 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
983 REALLOC_ARRAY(eindex->objects, eindex->alloc);
984 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
987 bitmap_pos = eindex->count;
988 eindex->objects[eindex->count] = object;
989 eindex->hashes[eindex->count] = pack_name_hash(name);
990 kh_value(eindex->positions, hash_pos) = bitmap_pos;
991 eindex->count++;
992 } else {
993 bitmap_pos = kh_value(eindex->positions, hash_pos);
996 return bitmap_pos + bitmap_num_objects(bitmap_git);
999 struct bitmap_show_data {
1000 struct bitmap_index *bitmap_git;
1001 struct bitmap *base;
1004 static void show_object(struct object *object, const char *name, void *data_)
1006 struct bitmap_show_data *data = data_;
1007 int bitmap_pos;
1009 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
1011 if (bitmap_pos < 0)
1012 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
1013 name);
1015 bitmap_set(data->base, bitmap_pos);
1018 static void show_commit(struct commit *commit UNUSED,
1019 void *data UNUSED)
1023 static unsigned apply_pseudo_merges_for_commit_1(struct bitmap_index *bitmap_git,
1024 struct bitmap *result,
1025 struct commit *commit,
1026 uint32_t commit_pos)
1028 int ret;
1030 ret = apply_pseudo_merges_for_commit(&bitmap_git->pseudo_merges,
1031 result, commit, commit_pos);
1033 if (ret)
1034 pseudo_merges_satisfied_nr += ret;
1036 return ret;
1039 static int add_to_include_set(struct bitmap_index *bitmap_git,
1040 struct include_data *data,
1041 struct commit *commit,
1042 int bitmap_pos)
1044 struct ewah_bitmap *partial;
1046 if (data->seen && bitmap_get(data->seen, bitmap_pos))
1047 return 0;
1049 if (bitmap_get(data->base, bitmap_pos))
1050 return 0;
1052 partial = bitmap_for_commit(bitmap_git, commit);
1053 if (partial) {
1054 existing_bitmaps_hits_nr++;
1056 bitmap_or_ewah(data->base, partial);
1057 return 0;
1060 existing_bitmaps_misses_nr++;
1062 bitmap_set(data->base, bitmap_pos);
1063 if (apply_pseudo_merges_for_commit_1(bitmap_git, data->base, commit,
1064 bitmap_pos))
1065 return 0;
1067 return 1;
1070 static int should_include(struct commit *commit, void *_data)
1072 struct include_data *data = _data;
1073 int bitmap_pos;
1075 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
1076 if (bitmap_pos < 0)
1077 bitmap_pos = ext_index_add_object(data->bitmap_git,
1078 (struct object *)commit,
1079 NULL);
1081 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
1082 struct commit_list *parent = commit->parents;
1084 while (parent) {
1085 parent->item->object.flags |= SEEN;
1086 parent = parent->next;
1089 return 0;
1092 return 1;
1095 static int should_include_obj(struct object *obj, void *_data)
1097 struct include_data *data = _data;
1098 int bitmap_pos;
1100 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1101 if (bitmap_pos < 0)
1102 return 1;
1103 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1104 bitmap_get(data->base, bitmap_pos)) {
1105 obj->flags |= SEEN;
1106 return 0;
1108 return 1;
1111 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1112 struct bitmap **base,
1113 struct commit *commit)
1115 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1117 if (!or_with) {
1118 existing_bitmaps_misses_nr++;
1119 return 0;
1122 existing_bitmaps_hits_nr++;
1124 if (!*base)
1125 *base = ewah_to_bitmap(or_with);
1126 else
1127 bitmap_or_ewah(*base, or_with);
1129 return 1;
1132 static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1133 struct rev_info *revs,
1134 struct bitmap *base,
1135 struct bitmap *seen)
1137 struct include_data incdata;
1138 struct bitmap_show_data show_data;
1140 if (!base)
1141 base = bitmap_new();
1143 incdata.bitmap_git = bitmap_git;
1144 incdata.base = base;
1145 incdata.seen = seen;
1147 revs->include_check = should_include;
1148 revs->include_check_obj = should_include_obj;
1149 revs->include_check_data = &incdata;
1151 if (prepare_revision_walk(revs))
1152 die(_("revision walk setup failed"));
1154 show_data.bitmap_git = bitmap_git;
1155 show_data.base = base;
1157 traverse_commit_list(revs, show_commit, show_object, &show_data);
1159 revs->include_check = NULL;
1160 revs->include_check_obj = NULL;
1161 revs->include_check_data = NULL;
1163 return base;
1166 struct bitmap_boundary_cb {
1167 struct bitmap_index *bitmap_git;
1168 struct bitmap *base;
1170 struct object_array boundary;
1173 static void show_boundary_commit(struct commit *commit, void *_data)
1175 struct bitmap_boundary_cb *data = _data;
1177 if (commit->object.flags & BOUNDARY)
1178 add_object_array(&commit->object, "", &data->boundary);
1180 if (commit->object.flags & UNINTERESTING) {
1181 if (bitmap_walk_contains(data->bitmap_git, data->base,
1182 &commit->object.oid))
1183 return;
1185 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1189 static void show_boundary_object(struct object *object UNUSED,
1190 const char *name UNUSED,
1191 void *data UNUSED)
1193 BUG("should not be called");
1196 static unsigned cascade_pseudo_merges_1(struct bitmap_index *bitmap_git,
1197 struct bitmap *result,
1198 struct bitmap *roots)
1200 int ret = cascade_pseudo_merges(&bitmap_git->pseudo_merges,
1201 result, roots);
1202 if (ret) {
1203 pseudo_merges_cascades_nr++;
1204 pseudo_merges_satisfied_nr += ret;
1207 return ret;
1210 static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1211 struct rev_info *revs,
1212 struct object_list *roots)
1214 struct bitmap_boundary_cb cb;
1215 struct object_list *root;
1216 unsigned int i;
1217 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1218 int any_missing = 0;
1219 int existing_bitmaps = 0;
1221 cb.bitmap_git = bitmap_git;
1222 cb.base = bitmap_new();
1223 object_array_init(&cb.boundary);
1225 revs->ignore_missing_links = 1;
1227 if (bitmap_git->pseudo_merges.nr) {
1228 struct bitmap *roots_bitmap = bitmap_new();
1229 struct object_list *objects = NULL;
1231 for (objects = roots; objects; objects = objects->next) {
1232 struct object *object = objects->item;
1233 int pos;
1235 pos = bitmap_position(bitmap_git, &object->oid);
1236 if (pos < 0)
1237 continue;
1239 bitmap_set(roots_bitmap, pos);
1242 if (!cascade_pseudo_merges_1(bitmap_git, cb.base, roots_bitmap))
1243 bitmap_free(roots_bitmap);
1247 * OR in any existing reachability bitmaps among `roots` into
1248 * `cb.base`.
1250 for (root = roots; root; root = root->next) {
1251 struct object *object = root->item;
1252 if (object->type != OBJ_COMMIT ||
1253 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1254 continue;
1256 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1257 (struct commit *)object)) {
1258 existing_bitmaps = 1;
1259 continue;
1262 any_missing = 1;
1265 if (!any_missing)
1266 goto cleanup;
1268 if (existing_bitmaps)
1269 cascade_pseudo_merges_1(bitmap_git, cb.base, NULL);
1271 tmp_blobs = revs->blob_objects;
1272 tmp_trees = revs->tree_objects;
1273 tmp_tags = revs->blob_objects;
1274 revs->blob_objects = 0;
1275 revs->tree_objects = 0;
1276 revs->tag_objects = 0;
1279 * We didn't have complete coverage of the roots. First setup a
1280 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1281 * between the tips and boundary, and (b) record the boundary.
1283 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1284 if (prepare_revision_walk(revs))
1285 die("revision walk setup failed");
1286 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1288 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1289 revs->boundary = 1;
1290 traverse_commit_list_filtered(revs,
1291 show_boundary_commit,
1292 show_boundary_object,
1293 &cb, NULL);
1294 revs->boundary = 0;
1295 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1297 revs->blob_objects = tmp_blobs;
1298 revs->tree_objects = tmp_trees;
1299 revs->tag_objects = tmp_tags;
1301 reset_revision_walk();
1302 clear_object_flags(UNINTERESTING);
1305 * Then add the boundary commit(s) as fill-in traversal tips.
1307 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1308 for (i = 0; i < cb.boundary.nr; i++) {
1309 struct object *obj = cb.boundary.objects[i].item;
1310 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1311 obj->flags |= SEEN;
1312 else
1313 add_pending_object(revs, obj, "");
1315 if (revs->pending.nr)
1316 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1317 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1319 cleanup:
1320 object_array_clear(&cb.boundary);
1321 revs->ignore_missing_links = 0;
1323 return cb.base;
1326 struct ewah_bitmap *pseudo_merge_bitmap_for_commit(struct bitmap_index *bitmap_git,
1327 struct commit *commit)
1329 struct commit_list *p;
1330 struct bitmap *parents;
1331 struct pseudo_merge *match = NULL;
1333 if (!bitmap_git->pseudo_merges.nr)
1334 return NULL;
1336 parents = bitmap_new();
1338 for (p = commit->parents; p; p = p->next) {
1339 int pos = bitmap_position(bitmap_git, &p->item->object.oid);
1340 if (pos < 0 || pos >= bitmap_num_objects(bitmap_git))
1341 goto done;
1343 bitmap_set(parents, pos);
1346 match = pseudo_merge_for_parents(&bitmap_git->pseudo_merges,
1347 parents);
1349 done:
1350 bitmap_free(parents);
1351 if (match)
1352 return pseudo_merge_bitmap(&bitmap_git->pseudo_merges, match);
1354 return NULL;
1357 static void unsatisfy_all_pseudo_merges(struct bitmap_index *bitmap_git)
1359 uint32_t i;
1360 for (i = 0; i < bitmap_git->pseudo_merges.nr; i++)
1361 bitmap_git->pseudo_merges.v[i].satisfied = 0;
1364 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1365 struct rev_info *revs,
1366 struct object_list *roots,
1367 struct bitmap *seen)
1369 struct bitmap *base = NULL;
1370 int needs_walk = 0;
1371 unsigned existing_bitmaps = 0;
1373 struct object_list *not_mapped = NULL;
1375 unsatisfy_all_pseudo_merges(bitmap_git);
1377 if (bitmap_git->pseudo_merges.nr) {
1378 struct bitmap *roots_bitmap = bitmap_new();
1379 struct object_list *objects = NULL;
1381 for (objects = roots; objects; objects = objects->next) {
1382 struct object *object = objects->item;
1383 int pos;
1385 pos = bitmap_position(bitmap_git, &object->oid);
1386 if (pos < 0)
1387 continue;
1389 bitmap_set(roots_bitmap, pos);
1392 base = bitmap_new();
1393 cascade_pseudo_merges_1(bitmap_git, base, roots_bitmap);
1394 bitmap_free(roots_bitmap);
1398 * Go through all the roots for the walk. The ones that have bitmaps
1399 * on the bitmap index will be `or`ed together to form an initial
1400 * global reachability analysis.
1402 * The ones without bitmaps in the index will be stored in the
1403 * `not_mapped_list` for further processing.
1405 while (roots) {
1406 struct object *object = roots->item;
1408 roots = roots->next;
1410 if (base) {
1411 int pos = bitmap_position(bitmap_git, &object->oid);
1412 if (pos > 0 && bitmap_get(base, pos)) {
1413 object->flags |= SEEN;
1414 continue;
1418 if (object->type == OBJ_COMMIT &&
1419 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1420 object->flags |= SEEN;
1421 existing_bitmaps = 1;
1422 continue;
1425 object_list_insert(object, &not_mapped);
1429 * Best case scenario: We found bitmaps for all the roots,
1430 * so the resulting `or` bitmap has the full reachability analysis
1432 if (!not_mapped)
1433 return base;
1435 roots = not_mapped;
1437 if (existing_bitmaps)
1438 cascade_pseudo_merges_1(bitmap_git, base, NULL);
1441 * Let's iterate through all the roots that don't have bitmaps to
1442 * check if we can determine them to be reachable from the existing
1443 * global bitmap.
1445 * If we cannot find them in the existing global bitmap, we'll need
1446 * to push them to an actual walk and run it until we can confirm
1447 * they are reachable
1449 while (roots) {
1450 struct object *object = roots->item;
1451 int pos;
1453 roots = roots->next;
1454 pos = bitmap_position(bitmap_git, &object->oid);
1456 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1457 object->flags &= ~UNINTERESTING;
1458 add_pending_object(revs, object, "");
1459 needs_walk = 1;
1461 roots_without_bitmaps_nr++;
1462 } else {
1463 object->flags |= SEEN;
1465 roots_with_bitmaps_nr++;
1469 if (needs_walk) {
1471 * This fill-in traversal may walk over some objects
1472 * again, since we have already traversed in order to
1473 * find the boundary.
1475 * But this extra walk should be extremely cheap, since
1476 * all commit objects are loaded into memory, and
1477 * because we skip walking to parents that are
1478 * UNINTERESTING, since it will be marked in the haves
1479 * bitmap already (or it has an on-disk bitmap, since
1480 * OR-ing it in covers all of its ancestors).
1482 base = fill_in_bitmap(bitmap_git, revs, base, seen);
1485 object_list_free(&not_mapped);
1487 return base;
1490 static void show_extended_objects(struct bitmap_index *bitmap_git,
1491 struct rev_info *revs,
1492 show_reachable_fn show_reach)
1494 struct bitmap *objects = bitmap_git->result;
1495 struct eindex *eindex = &bitmap_git->ext_index;
1496 uint32_t i;
1498 for (i = 0; i < eindex->count; ++i) {
1499 struct object *obj;
1501 if (!bitmap_get(objects, st_add(bitmap_num_objects(bitmap_git), i)))
1502 continue;
1504 obj = eindex->objects[i];
1505 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1506 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1507 (obj->type == OBJ_TAG && !revs->tag_objects))
1508 continue;
1510 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1514 static void init_type_iterator(struct ewah_iterator *it,
1515 struct bitmap_index *bitmap_git,
1516 enum object_type type)
1518 switch (type) {
1519 case OBJ_COMMIT:
1520 ewah_iterator_init(it, bitmap_git->commits);
1521 break;
1523 case OBJ_TREE:
1524 ewah_iterator_init(it, bitmap_git->trees);
1525 break;
1527 case OBJ_BLOB:
1528 ewah_iterator_init(it, bitmap_git->blobs);
1529 break;
1531 case OBJ_TAG:
1532 ewah_iterator_init(it, bitmap_git->tags);
1533 break;
1535 default:
1536 BUG("object type %d not stored by bitmap type index", type);
1537 break;
1541 static void show_objects_for_type(
1542 struct bitmap_index *bitmap_git,
1543 enum object_type object_type,
1544 show_reachable_fn show_reach)
1546 size_t i = 0;
1547 uint32_t offset;
1549 struct ewah_iterator it;
1550 eword_t filter;
1552 struct bitmap *objects = bitmap_git->result;
1554 init_type_iterator(&it, bitmap_git, object_type);
1556 for (i = 0; i < objects->word_alloc &&
1557 ewah_iterator_next(&filter, &it); i++) {
1558 eword_t word = objects->words[i] & filter;
1559 size_t pos = (i * BITS_IN_EWORD);
1561 if (!word)
1562 continue;
1564 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1565 struct packed_git *pack;
1566 struct object_id oid;
1567 uint32_t hash = 0, index_pos;
1568 off_t ofs;
1570 if ((word >> offset) == 0)
1571 break;
1573 offset += ewah_bit_ctz64(word >> offset);
1575 if (bitmap_is_midx(bitmap_git)) {
1576 struct multi_pack_index *m = bitmap_git->midx;
1577 uint32_t pack_id;
1579 index_pos = pack_pos_to_midx(m, pos + offset);
1580 ofs = nth_midxed_offset(m, index_pos);
1581 nth_midxed_object_oid(&oid, m, index_pos);
1583 pack_id = nth_midxed_pack_int_id(m, index_pos);
1584 pack = bitmap_git->midx->packs[pack_id];
1585 } else {
1586 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1587 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1588 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1590 pack = bitmap_git->pack;
1593 if (bitmap_git->hashes)
1594 hash = get_be32(bitmap_git->hashes + index_pos);
1596 show_reach(&oid, object_type, 0, hash, pack, ofs);
1601 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1602 struct object_list *roots)
1604 while (roots) {
1605 struct object *object = roots->item;
1606 roots = roots->next;
1608 if (bitmap_is_midx(bitmap_git)) {
1609 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1610 return 1;
1611 } else {
1612 if (find_pack_entry_one(&object->oid, bitmap_git->pack) > 0)
1613 return 1;
1617 return 0;
1620 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1621 struct object_list *tip_objects,
1622 enum object_type type)
1624 struct bitmap *result = bitmap_new();
1625 struct object_list *p;
1627 for (p = tip_objects; p; p = p->next) {
1628 int pos;
1630 if (p->item->type != type)
1631 continue;
1633 pos = bitmap_position(bitmap_git, &p->item->oid);
1634 if (pos < 0)
1635 continue;
1637 bitmap_set(result, pos);
1640 return result;
1643 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1644 struct object_list *tip_objects,
1645 struct bitmap *to_filter,
1646 enum object_type type)
1648 struct eindex *eindex = &bitmap_git->ext_index;
1649 struct bitmap *tips;
1650 struct ewah_iterator it;
1651 eword_t mask;
1652 uint32_t i;
1655 * The non-bitmap version of this filter never removes
1656 * objects which the other side specifically asked for,
1657 * so we must match that behavior.
1659 tips = find_tip_objects(bitmap_git, tip_objects, type);
1662 * We can use the type-level bitmap for 'type' to work in whole
1663 * words for the objects that are actually in the bitmapped
1664 * packfile.
1666 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1667 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1668 i++) {
1669 if (i < tips->word_alloc)
1670 mask &= ~tips->words[i];
1671 to_filter->words[i] &= ~mask;
1675 * Clear any objects that weren't in the packfile (and so would
1676 * not have been caught by the loop above. We'll have to check
1677 * them individually.
1679 for (i = 0; i < eindex->count; i++) {
1680 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1681 if (eindex->objects[i]->type == type &&
1682 bitmap_get(to_filter, pos) &&
1683 !bitmap_get(tips, pos))
1684 bitmap_unset(to_filter, pos);
1687 bitmap_free(tips);
1690 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1691 struct object_list *tip_objects,
1692 struct bitmap *to_filter)
1694 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1695 OBJ_BLOB);
1698 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1699 uint32_t pos)
1701 unsigned long size;
1702 struct object_info oi = OBJECT_INFO_INIT;
1704 oi.sizep = &size;
1706 if (pos < bitmap_num_objects(bitmap_git)) {
1707 struct packed_git *pack;
1708 off_t ofs;
1710 if (bitmap_is_midx(bitmap_git)) {
1711 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1712 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1714 pack = bitmap_git->midx->packs[pack_id];
1715 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1716 } else {
1717 pack = bitmap_git->pack;
1718 ofs = pack_pos_to_offset(pack, pos);
1721 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1722 struct object_id oid;
1723 nth_bitmap_object_oid(bitmap_git, &oid,
1724 pack_pos_to_index(pack, pos));
1725 die(_("unable to get size of %s"), oid_to_hex(&oid));
1727 } else {
1728 struct eindex *eindex = &bitmap_git->ext_index;
1729 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1730 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1731 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1734 return size;
1737 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1738 struct object_list *tip_objects,
1739 struct bitmap *to_filter,
1740 unsigned long limit)
1742 struct eindex *eindex = &bitmap_git->ext_index;
1743 struct bitmap *tips;
1744 struct ewah_iterator it;
1745 eword_t mask;
1746 uint32_t i;
1748 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1750 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1751 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1752 i++) {
1753 eword_t word = to_filter->words[i] & mask;
1754 unsigned offset;
1756 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1757 uint32_t pos;
1759 if ((word >> offset) == 0)
1760 break;
1761 offset += ewah_bit_ctz64(word >> offset);
1762 pos = i * BITS_IN_EWORD + offset;
1764 if (!bitmap_get(tips, pos) &&
1765 get_size_by_pos(bitmap_git, pos) >= limit)
1766 bitmap_unset(to_filter, pos);
1770 for (i = 0; i < eindex->count; i++) {
1771 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1772 if (eindex->objects[i]->type == OBJ_BLOB &&
1773 bitmap_get(to_filter, pos) &&
1774 !bitmap_get(tips, pos) &&
1775 get_size_by_pos(bitmap_git, pos) >= limit)
1776 bitmap_unset(to_filter, pos);
1779 bitmap_free(tips);
1782 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1783 struct object_list *tip_objects,
1784 struct bitmap *to_filter,
1785 unsigned long limit)
1787 if (limit)
1788 BUG("filter_bitmap_tree_depth given non-zero limit");
1790 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1791 OBJ_TREE);
1792 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1793 OBJ_BLOB);
1796 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1797 struct object_list *tip_objects,
1798 struct bitmap *to_filter,
1799 enum object_type object_type)
1801 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1802 BUG("filter_bitmap_object_type given invalid object");
1804 if (object_type != OBJ_TAG)
1805 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1806 if (object_type != OBJ_COMMIT)
1807 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1808 if (object_type != OBJ_TREE)
1809 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1810 if (object_type != OBJ_BLOB)
1811 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1814 static int filter_bitmap(struct bitmap_index *bitmap_git,
1815 struct object_list *tip_objects,
1816 struct bitmap *to_filter,
1817 struct list_objects_filter_options *filter)
1819 if (!filter || filter->choice == LOFC_DISABLED)
1820 return 0;
1822 if (filter->choice == LOFC_BLOB_NONE) {
1823 if (bitmap_git)
1824 filter_bitmap_blob_none(bitmap_git, tip_objects,
1825 to_filter);
1826 return 0;
1829 if (filter->choice == LOFC_BLOB_LIMIT) {
1830 if (bitmap_git)
1831 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1832 to_filter,
1833 filter->blob_limit_value);
1834 return 0;
1837 if (filter->choice == LOFC_TREE_DEPTH &&
1838 filter->tree_exclude_depth == 0) {
1839 if (bitmap_git)
1840 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1841 to_filter,
1842 filter->tree_exclude_depth);
1843 return 0;
1846 if (filter->choice == LOFC_OBJECT_TYPE) {
1847 if (bitmap_git)
1848 filter_bitmap_object_type(bitmap_git, tip_objects,
1849 to_filter,
1850 filter->object_type);
1851 return 0;
1854 if (filter->choice == LOFC_COMBINE) {
1855 int i;
1856 for (i = 0; i < filter->sub_nr; i++) {
1857 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1858 &filter->sub[i]) < 0)
1859 return -1;
1861 return 0;
1864 /* filter choice not handled */
1865 return -1;
1868 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1870 return !filter_bitmap(NULL, NULL, NULL, filter);
1874 static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
1875 struct bitmap *result)
1877 struct eindex *eindex = &bitmap_git->ext_index;
1878 uint32_t objects_nr;
1879 size_t i, pos;
1881 objects_nr = bitmap_num_objects(bitmap_git);
1882 pos = objects_nr / BITS_IN_EWORD;
1884 if (pos > result->word_alloc)
1885 pos = result->word_alloc;
1887 memset(result->words, 0x00, sizeof(eword_t) * pos);
1888 for (i = pos * BITS_IN_EWORD; i < objects_nr; i++)
1889 bitmap_unset(result, i);
1891 for (i = 0; i < eindex->count; ++i) {
1892 if (has_object_pack(&eindex->objects[i]->oid))
1893 bitmap_unset(result, objects_nr + i);
1897 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1898 int filter_provided_objects)
1900 unsigned int i;
1901 int use_boundary_traversal;
1903 struct object_list *wants = NULL;
1904 struct object_list *haves = NULL;
1906 struct bitmap *wants_bitmap = NULL;
1907 struct bitmap *haves_bitmap = NULL;
1909 struct bitmap_index *bitmap_git;
1912 * We can't do pathspec limiting with bitmaps, because we don't know
1913 * which commits are associated with which object changes (let alone
1914 * even which objects are associated with which paths).
1916 if (revs->prune)
1917 return NULL;
1919 if (!can_filter_bitmap(&revs->filter))
1920 return NULL;
1922 /* try to open a bitmapped pack, but don't parse it yet
1923 * because we may not need to use it */
1924 CALLOC_ARRAY(bitmap_git, 1);
1925 if (open_bitmap(revs->repo, bitmap_git) < 0)
1926 goto cleanup;
1928 for (i = 0; i < revs->pending.nr; ++i) {
1929 struct object *object = revs->pending.objects[i].item;
1931 if (object->type == OBJ_NONE)
1932 parse_object_or_die(&object->oid, NULL);
1934 while (object->type == OBJ_TAG) {
1935 struct tag *tag = (struct tag *) object;
1937 if (object->flags & UNINTERESTING)
1938 object_list_insert(object, &haves);
1939 else
1940 object_list_insert(object, &wants);
1942 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1943 object->flags |= (tag->object.flags & UNINTERESTING);
1946 if (object->flags & UNINTERESTING)
1947 object_list_insert(object, &haves);
1948 else
1949 object_list_insert(object, &wants);
1952 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1953 if (use_boundary_traversal < 0) {
1954 prepare_repo_settings(revs->repo);
1955 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1958 if (!use_boundary_traversal) {
1960 * if we have a HAVES list, but none of those haves is contained
1961 * in the packfile that has a bitmap, we don't have anything to
1962 * optimize here
1964 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1965 goto cleanup;
1968 /* if we don't want anything, we're done here */
1969 if (!wants)
1970 goto cleanup;
1973 * now we're going to use bitmaps, so load the actual bitmap entries
1974 * from disk. this is the point of no return; after this the rev_list
1975 * becomes invalidated and we must perform the revwalk through bitmaps
1977 if (load_bitmap(revs->repo, bitmap_git) < 0)
1978 goto cleanup;
1980 if (!use_boundary_traversal)
1981 object_array_clear(&revs->pending);
1983 if (haves) {
1984 if (use_boundary_traversal) {
1985 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1986 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1987 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1988 } else {
1989 trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1990 revs->ignore_missing_links = 1;
1991 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1992 reset_revision_walk();
1993 revs->ignore_missing_links = 0;
1994 trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1997 if (!haves_bitmap)
1998 BUG("failed to perform bitmap walk");
2001 if (use_boundary_traversal) {
2002 object_array_clear(&revs->pending);
2003 reset_revision_walk();
2006 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
2008 if (!wants_bitmap)
2009 BUG("failed to perform bitmap walk");
2011 if (haves_bitmap)
2012 bitmap_and_not(wants_bitmap, haves_bitmap);
2014 filter_bitmap(bitmap_git,
2015 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
2016 wants_bitmap,
2017 &revs->filter);
2019 if (revs->unpacked)
2020 filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap);
2022 bitmap_git->result = wants_bitmap;
2023 bitmap_git->haves = haves_bitmap;
2025 object_list_free(&wants);
2026 object_list_free(&haves);
2028 trace2_data_intmax("bitmap", the_repository, "pseudo_merges_satisfied",
2029 pseudo_merges_satisfied_nr);
2030 trace2_data_intmax("bitmap", the_repository, "pseudo_merges_cascades",
2031 pseudo_merges_cascades_nr);
2032 trace2_data_intmax("bitmap", the_repository, "bitmap/hits",
2033 existing_bitmaps_hits_nr);
2034 trace2_data_intmax("bitmap", the_repository, "bitmap/misses",
2035 existing_bitmaps_misses_nr);
2036 trace2_data_intmax("bitmap", the_repository, "bitmap/roots_with_bitmap",
2037 roots_with_bitmaps_nr);
2038 trace2_data_intmax("bitmap", the_repository, "bitmap/roots_without_bitmap",
2039 roots_without_bitmaps_nr);
2041 return bitmap_git;
2043 cleanup:
2044 free_bitmap_index(bitmap_git);
2045 object_list_free(&wants);
2046 object_list_free(&haves);
2047 return NULL;
2051 * -1 means "stop trying further objects"; 0 means we may or may not have
2052 * reused, but you can keep feeding bits.
2054 static int try_partial_reuse(struct bitmap_index *bitmap_git,
2055 struct bitmapped_pack *pack,
2056 size_t bitmap_pos,
2057 uint32_t pack_pos,
2058 off_t offset,
2059 struct bitmap *reuse,
2060 struct pack_window **w_curs)
2062 off_t delta_obj_offset;
2063 enum object_type type;
2064 unsigned long size;
2066 if (pack_pos >= pack->p->num_objects)
2067 return -1; /* not actually in the pack */
2069 delta_obj_offset = offset;
2070 type = unpack_object_header(pack->p, w_curs, &offset, &size);
2071 if (type < 0)
2072 return -1; /* broken packfile, punt */
2074 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
2075 off_t base_offset;
2076 uint32_t base_pos;
2077 uint32_t base_bitmap_pos;
2080 * Find the position of the base object so we can look it up
2081 * in our bitmaps. If we can't come up with an offset, or if
2082 * that offset is not in the revidx, the pack is corrupt.
2083 * There's nothing we can do, so just punt on this object,
2084 * and the normal slow path will complain about it in
2085 * more detail.
2087 base_offset = get_delta_base(pack->p, w_curs, &offset, type,
2088 delta_obj_offset);
2089 if (!base_offset)
2090 return 0;
2092 offset_to_pack_pos(pack->p, base_offset, &base_pos);
2094 if (bitmap_is_midx(bitmap_git)) {
2096 * Cross-pack deltas are rejected for now, but could
2097 * theoretically be supported in the future.
2099 * We would need to ensure that we're sending both
2100 * halves of the delta/base pair, regardless of whether
2101 * or not the two cross a pack boundary. If they do,
2102 * then we must convert the delta to an REF_DELTA to
2103 * refer back to the base in the other pack.
2104 * */
2105 if (midx_pair_to_pack_pos(bitmap_git->midx,
2106 pack->pack_int_id,
2107 base_offset,
2108 &base_bitmap_pos) < 0) {
2109 return 0;
2111 } else {
2112 if (offset_to_pack_pos(pack->p, base_offset,
2113 &base_pos) < 0)
2114 return 0;
2116 * We assume delta dependencies always point backwards.
2117 * This lets us do a single pass, and is basically
2118 * always true due to the way OFS_DELTAs work. You would
2119 * not typically find REF_DELTA in a bitmapped pack,
2120 * since we only bitmap packs we write fresh, and
2121 * OFS_DELTA is the default). But let's double check to
2122 * make sure the pack wasn't written with odd
2123 * parameters.
2125 if (base_pos >= pack_pos)
2126 return 0;
2127 base_bitmap_pos = pack->bitmap_pos + base_pos;
2131 * And finally, if we're not sending the base as part of our
2132 * reuse chunk, then don't send this object either. The base
2133 * would come after us, along with other objects not
2134 * necessarily in the pack, which means we'd need to convert
2135 * to REF_DELTA on the fly. Better to just let the normal
2136 * object_entry code path handle it.
2138 if (!bitmap_get(reuse, base_bitmap_pos))
2139 return 0;
2143 * If we got here, then the object is OK to reuse. Mark it.
2145 bitmap_set(reuse, bitmap_pos);
2146 return 0;
2149 static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
2150 struct bitmapped_pack *pack,
2151 struct bitmap *reuse)
2153 struct bitmap *result = bitmap_git->result;
2154 struct pack_window *w_curs = NULL;
2155 size_t pos = pack->bitmap_pos / BITS_IN_EWORD;
2157 if (!pack->bitmap_pos) {
2159 * If we're processing the first (in the case of a MIDX, the
2160 * preferred pack) or the only (in the case of single-pack
2161 * bitmaps) pack, then we can reuse whole words at a time.
2163 * This is because we know that any deltas in this range *must*
2164 * have their bases chosen from the same pack, since:
2166 * - In the single pack case, there is no other pack to choose
2167 * them from.
2169 * - In the MIDX case, the first pack is the preferred pack, so
2170 * all ties are broken in favor of that pack (i.e. the one
2171 * we're currently processing). So any duplicate bases will be
2172 * resolved in favor of the pack we're processing.
2174 while (pos < result->word_alloc &&
2175 pos < pack->bitmap_nr / BITS_IN_EWORD &&
2176 result->words[pos] == (eword_t)~0)
2177 pos++;
2178 memset(reuse->words, 0xFF, pos * sizeof(eword_t));
2181 for (; pos < result->word_alloc; pos++) {
2182 eword_t word = result->words[pos];
2183 size_t offset;
2185 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2186 size_t bit_pos;
2187 uint32_t pack_pos;
2188 off_t ofs;
2190 if (word >> offset == 0)
2191 break;
2193 offset += ewah_bit_ctz64(word >> offset);
2195 bit_pos = pos * BITS_IN_EWORD + offset;
2196 if (bit_pos < pack->bitmap_pos)
2197 continue;
2198 if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
2199 goto done;
2201 if (bitmap_is_midx(bitmap_git)) {
2202 uint32_t midx_pos;
2204 midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2205 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
2207 if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
2208 BUG("could not find object in pack %s "
2209 "at offset %"PRIuMAX" in MIDX",
2210 pack_basename(pack->p), (uintmax_t)ofs);
2211 } else {
2212 pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
2213 if (pack_pos >= pack->p->num_objects)
2214 BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
2215 pack_basename(pack->p), (uintmax_t)pack_pos,
2216 pack->p->num_objects);
2218 ofs = pack_pos_to_offset(pack->p, pack_pos);
2221 if (try_partial_reuse(bitmap_git, pack, bit_pos,
2222 pack_pos, ofs, reuse, &w_curs) < 0) {
2224 * try_partial_reuse indicated we couldn't reuse
2225 * any bits, so there is no point in trying more
2226 * bits in the current word, or any other words
2227 * in result.
2229 * Jump out of both loops to avoid future
2230 * unnecessary calls to try_partial_reuse.
2232 goto done;
2237 done:
2238 unuse_pack(&w_curs);
2241 static int bitmapped_pack_cmp(const void *va, const void *vb)
2243 const struct bitmapped_pack *a = va;
2244 const struct bitmapped_pack *b = vb;
2246 if (a->bitmap_pos < b->bitmap_pos)
2247 return -1;
2248 if (a->bitmap_pos > b->bitmap_pos)
2249 return 1;
2250 return 0;
2253 void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2254 struct bitmapped_pack **packs_out,
2255 size_t *packs_nr_out,
2256 struct bitmap **reuse_out,
2257 int multi_pack_reuse)
2259 struct repository *r = the_repository;
2260 struct bitmapped_pack *packs = NULL;
2261 struct bitmap *result = bitmap_git->result;
2262 struct bitmap *reuse;
2263 size_t i;
2264 size_t packs_nr = 0, packs_alloc = 0;
2265 size_t word_alloc;
2266 uint32_t objects_nr = 0;
2268 assert(result);
2270 load_reverse_index(r, bitmap_git);
2272 if (!bitmap_is_midx(bitmap_git) || !bitmap_git->midx->chunk_bitmapped_packs)
2273 multi_pack_reuse = 0;
2275 if (multi_pack_reuse) {
2276 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
2277 struct bitmapped_pack pack;
2278 if (nth_bitmapped_pack(r, bitmap_git->midx, &pack, i) < 0) {
2279 warning(_("unable to load pack: '%s', disabling pack-reuse"),
2280 bitmap_git->midx->pack_names[i]);
2281 free(packs);
2282 return;
2285 if (!pack.bitmap_nr)
2286 continue;
2288 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2289 memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2291 objects_nr += pack.p->num_objects;
2294 QSORT(packs, packs_nr, bitmapped_pack_cmp);
2295 } else {
2296 struct packed_git *pack;
2297 uint32_t pack_int_id;
2299 if (bitmap_is_midx(bitmap_git)) {
2300 uint32_t preferred_pack_pos;
2302 if (midx_preferred_pack(bitmap_git->midx, &preferred_pack_pos) < 0) {
2303 warning(_("unable to compute preferred pack, disabling pack-reuse"));
2304 return;
2307 pack = bitmap_git->midx->packs[preferred_pack_pos];
2308 pack_int_id = preferred_pack_pos;
2309 } else {
2310 pack = bitmap_git->pack;
2312 * Any value for 'pack_int_id' will do here. When we
2313 * process the pack via try_partial_reuse(), we won't
2314 * use the `pack_int_id` field since we have a non-MIDX
2315 * bitmap.
2317 * Use '-1' as a sentinel value to make it clear
2318 * that we do not expect to read this field.
2320 pack_int_id = -1;
2323 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2324 packs[packs_nr].p = pack;
2325 packs[packs_nr].pack_int_id = pack_int_id;
2326 packs[packs_nr].bitmap_nr = pack->num_objects;
2327 packs[packs_nr].bitmap_pos = 0;
2328 packs[packs_nr].from_midx = bitmap_git->midx;
2330 objects_nr = packs[packs_nr++].bitmap_nr;
2333 word_alloc = objects_nr / BITS_IN_EWORD;
2334 if (objects_nr % BITS_IN_EWORD)
2335 word_alloc++;
2336 reuse = bitmap_word_alloc(word_alloc);
2338 for (i = 0; i < packs_nr; i++)
2339 reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse);
2341 if (bitmap_is_empty(reuse)) {
2342 free(packs);
2343 bitmap_free(reuse);
2344 return;
2348 * Drop any reused objects from the result, since they will not
2349 * need to be handled separately.
2351 bitmap_and_not(result, reuse);
2352 *packs_out = packs;
2353 *packs_nr_out = packs_nr;
2354 *reuse_out = reuse;
2357 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2358 struct bitmap *bitmap, const struct object_id *oid)
2360 int idx;
2362 if (!bitmap)
2363 return 0;
2365 idx = bitmap_position(bitmap_git, oid);
2366 return idx >= 0 && bitmap_get(bitmap, idx);
2369 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2370 struct rev_info *revs,
2371 show_reachable_fn show_reachable)
2373 assert(bitmap_git->result);
2375 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2376 if (revs->tree_objects)
2377 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2378 if (revs->blob_objects)
2379 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2380 if (revs->tag_objects)
2381 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2383 show_extended_objects(bitmap_git, revs, show_reachable);
2386 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2387 enum object_type type)
2389 struct bitmap *objects = bitmap_git->result;
2390 struct eindex *eindex = &bitmap_git->ext_index;
2392 uint32_t i = 0, count = 0;
2393 struct ewah_iterator it;
2394 eword_t filter;
2396 init_type_iterator(&it, bitmap_git, type);
2398 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2399 eword_t word = objects->words[i++] & filter;
2400 count += ewah_bit_popcount64(word);
2403 for (i = 0; i < eindex->count; ++i) {
2404 if (eindex->objects[i]->type == type &&
2405 bitmap_get(objects,
2406 st_add(bitmap_num_objects(bitmap_git), i)))
2407 count++;
2410 return count;
2413 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2414 uint32_t *commits, uint32_t *trees,
2415 uint32_t *blobs, uint32_t *tags)
2417 assert(bitmap_git->result);
2419 if (commits)
2420 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2422 if (trees)
2423 *trees = count_object_type(bitmap_git, OBJ_TREE);
2425 if (blobs)
2426 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2428 if (tags)
2429 *tags = count_object_type(bitmap_git, OBJ_TAG);
2432 struct bitmap_test_data {
2433 struct bitmap_index *bitmap_git;
2434 struct bitmap *base;
2435 struct bitmap *commits;
2436 struct bitmap *trees;
2437 struct bitmap *blobs;
2438 struct bitmap *tags;
2439 struct progress *prg;
2440 size_t seen;
2443 static void test_bitmap_type(struct bitmap_test_data *tdata,
2444 struct object *obj, int pos)
2446 enum object_type bitmap_type = OBJ_NONE;
2447 int bitmaps_nr = 0;
2449 if (bitmap_get(tdata->commits, pos)) {
2450 bitmap_type = OBJ_COMMIT;
2451 bitmaps_nr++;
2453 if (bitmap_get(tdata->trees, pos)) {
2454 bitmap_type = OBJ_TREE;
2455 bitmaps_nr++;
2457 if (bitmap_get(tdata->blobs, pos)) {
2458 bitmap_type = OBJ_BLOB;
2459 bitmaps_nr++;
2461 if (bitmap_get(tdata->tags, pos)) {
2462 bitmap_type = OBJ_TAG;
2463 bitmaps_nr++;
2466 if (bitmap_type == OBJ_NONE)
2467 die(_("object '%s' not found in type bitmaps"),
2468 oid_to_hex(&obj->oid));
2470 if (bitmaps_nr > 1)
2471 die(_("object '%s' does not have a unique type"),
2472 oid_to_hex(&obj->oid));
2474 if (bitmap_type != obj->type)
2475 die(_("object '%s': real type '%s', expected: '%s'"),
2476 oid_to_hex(&obj->oid),
2477 type_name(obj->type),
2478 type_name(bitmap_type));
2481 static void test_show_object(struct object *object,
2482 const char *name UNUSED,
2483 void *data)
2485 struct bitmap_test_data *tdata = data;
2486 int bitmap_pos;
2488 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2489 if (bitmap_pos < 0)
2490 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2491 test_bitmap_type(tdata, object, bitmap_pos);
2493 bitmap_set(tdata->base, bitmap_pos);
2494 display_progress(tdata->prg, ++tdata->seen);
2497 static void test_show_commit(struct commit *commit, void *data)
2499 struct bitmap_test_data *tdata = data;
2500 int bitmap_pos;
2502 bitmap_pos = bitmap_position(tdata->bitmap_git,
2503 &commit->object.oid);
2504 if (bitmap_pos < 0)
2505 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2506 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2508 bitmap_set(tdata->base, bitmap_pos);
2509 display_progress(tdata->prg, ++tdata->seen);
2512 void test_bitmap_walk(struct rev_info *revs)
2514 struct object *root;
2515 struct bitmap *result = NULL;
2516 size_t result_popcnt;
2517 struct bitmap_test_data tdata;
2518 struct bitmap_index *bitmap_git;
2519 struct ewah_bitmap *bm;
2521 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2522 die(_("failed to load bitmap indexes"));
2524 if (revs->pending.nr != 1)
2525 die(_("you must specify exactly one commit to test"));
2527 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2528 bitmap_git->version,
2529 bitmap_git->entry_count,
2530 bitmap_git->table_lookup ? "" : " loaded");
2532 root = revs->pending.objects[0].item;
2533 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2535 if (bm) {
2536 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2537 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2539 result = ewah_to_bitmap(bm);
2542 if (!result)
2543 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2545 revs->tag_objects = 1;
2546 revs->tree_objects = 1;
2547 revs->blob_objects = 1;
2549 result_popcnt = bitmap_popcount(result);
2551 if (prepare_revision_walk(revs))
2552 die(_("revision walk setup failed"));
2554 tdata.bitmap_git = bitmap_git;
2555 tdata.base = bitmap_new();
2556 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2557 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2558 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2559 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2560 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2561 tdata.seen = 0;
2563 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2565 stop_progress(&tdata.prg);
2567 if (bitmap_equals(result, tdata.base))
2568 fprintf_ln(stderr, "OK!");
2569 else
2570 die(_("mismatch in bitmap results"));
2572 bitmap_free(result);
2573 bitmap_free(tdata.base);
2574 bitmap_free(tdata.commits);
2575 bitmap_free(tdata.trees);
2576 bitmap_free(tdata.blobs);
2577 bitmap_free(tdata.tags);
2578 free_bitmap_index(bitmap_git);
2581 int test_bitmap_commits(struct repository *r)
2583 struct object_id oid;
2584 MAYBE_UNUSED void *value;
2585 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2587 if (!bitmap_git)
2588 die(_("failed to load bitmap indexes"));
2591 * As this function is only used to print bitmap selected
2592 * commits, we don't have to read the commit table.
2594 if (bitmap_git->table_lookup) {
2595 if (load_bitmap_entries_v1(bitmap_git) < 0)
2596 die(_("failed to load bitmap indexes"));
2599 kh_foreach(bitmap_git->bitmaps, oid, value, {
2600 printf_ln("%s", oid_to_hex(&oid));
2603 free_bitmap_index(bitmap_git);
2605 return 0;
2608 int test_bitmap_hashes(struct repository *r)
2610 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2611 struct object_id oid;
2612 uint32_t i, index_pos;
2614 if (!bitmap_git || !bitmap_git->hashes)
2615 goto cleanup;
2617 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2618 if (bitmap_is_midx(bitmap_git))
2619 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2620 else
2621 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2623 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2625 printf_ln("%s %"PRIu32"",
2626 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2629 cleanup:
2630 free_bitmap_index(bitmap_git);
2632 return 0;
2635 static void bit_pos_to_object_id(struct bitmap_index *bitmap_git,
2636 uint32_t bit_pos,
2637 struct object_id *oid)
2639 uint32_t index_pos;
2641 if (bitmap_is_midx(bitmap_git))
2642 index_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2643 else
2644 index_pos = pack_pos_to_index(bitmap_git->pack, bit_pos);
2646 nth_bitmap_object_oid(bitmap_git, oid, index_pos);
2649 int test_bitmap_pseudo_merges(struct repository *r)
2651 struct bitmap_index *bitmap_git;
2652 uint32_t i;
2654 bitmap_git = prepare_bitmap_git(r);
2655 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2656 goto cleanup;
2658 for (i = 0; i < bitmap_git->pseudo_merges.nr; i++) {
2659 struct pseudo_merge *merge;
2660 struct ewah_bitmap *commits_bitmap, *merge_bitmap;
2662 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2663 &bitmap_git->pseudo_merges.v[i]);
2664 commits_bitmap = merge->commits;
2665 merge_bitmap = pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
2666 merge);
2668 printf("at=%"PRIuMAX", commits=%"PRIuMAX", objects=%"PRIuMAX"\n",
2669 (uintmax_t)merge->at,
2670 (uintmax_t)ewah_bitmap_popcount(commits_bitmap),
2671 (uintmax_t)ewah_bitmap_popcount(merge_bitmap));
2674 cleanup:
2675 free_bitmap_index(bitmap_git);
2676 return 0;
2679 static void dump_ewah_object_ids(struct bitmap_index *bitmap_git,
2680 struct ewah_bitmap *bitmap)
2683 struct ewah_iterator it;
2684 eword_t word;
2685 uint32_t pos = 0;
2687 ewah_iterator_init(&it, bitmap);
2689 while (ewah_iterator_next(&word, &it)) {
2690 struct object_id oid;
2691 uint32_t offset;
2693 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2694 if (!(word >> offset))
2695 break;
2697 offset += ewah_bit_ctz64(word >> offset);
2699 bit_pos_to_object_id(bitmap_git, pos + offset, &oid);
2700 printf("%s\n", oid_to_hex(&oid));
2702 pos += BITS_IN_EWORD;
2706 int test_bitmap_pseudo_merge_commits(struct repository *r, uint32_t n)
2708 struct bitmap_index *bitmap_git;
2709 struct pseudo_merge *merge;
2710 int ret = 0;
2712 bitmap_git = prepare_bitmap_git(r);
2713 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2714 goto cleanup;
2716 if (n >= bitmap_git->pseudo_merges.nr) {
2717 ret = error(_("pseudo-merge index out of range "
2718 "(%"PRIu32" >= %"PRIuMAX")"),
2719 n, (uintmax_t)bitmap_git->pseudo_merges.nr);
2720 goto cleanup;
2723 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2724 &bitmap_git->pseudo_merges.v[n]);
2725 dump_ewah_object_ids(bitmap_git, merge->commits);
2727 cleanup:
2728 free_bitmap_index(bitmap_git);
2729 return ret;
2732 int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n)
2734 struct bitmap_index *bitmap_git;
2735 struct pseudo_merge *merge;
2736 int ret = 0;
2738 bitmap_git = prepare_bitmap_git(r);
2739 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2740 goto cleanup;
2742 if (n >= bitmap_git->pseudo_merges.nr) {
2743 ret = error(_("pseudo-merge index out of range "
2744 "(%"PRIu32" >= %"PRIuMAX")"),
2745 n, (uintmax_t)bitmap_git->pseudo_merges.nr);
2746 goto cleanup;
2749 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2750 &bitmap_git->pseudo_merges.v[n]);
2752 dump_ewah_object_ids(bitmap_git,
2753 pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
2754 merge));
2756 cleanup:
2757 free_bitmap_index(bitmap_git);
2758 return ret;
2761 int rebuild_bitmap(const uint32_t *reposition,
2762 struct ewah_bitmap *source,
2763 struct bitmap *dest)
2765 uint32_t pos = 0;
2766 struct ewah_iterator it;
2767 eword_t word;
2769 ewah_iterator_init(&it, source);
2771 while (ewah_iterator_next(&word, &it)) {
2772 uint32_t offset, bit_pos;
2774 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2775 if ((word >> offset) == 0)
2776 break;
2778 offset += ewah_bit_ctz64(word >> offset);
2780 bit_pos = reposition[pos + offset];
2781 if (bit_pos > 0)
2782 bitmap_set(dest, bit_pos - 1);
2783 else /* can't reuse, we don't have the object */
2784 return -1;
2787 pos += BITS_IN_EWORD;
2789 return 0;
2792 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2793 struct packing_data *mapping)
2795 struct repository *r = the_repository;
2796 uint32_t i, num_objects;
2797 uint32_t *reposition;
2799 if (!bitmap_is_midx(bitmap_git))
2800 load_reverse_index(r, bitmap_git);
2801 else if (load_midx_revindex(bitmap_git->midx))
2802 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2803 "extension");
2805 num_objects = bitmap_num_objects(bitmap_git);
2806 CALLOC_ARRAY(reposition, num_objects);
2808 for (i = 0; i < num_objects; ++i) {
2809 struct object_id oid;
2810 struct object_entry *oe;
2811 uint32_t index_pos;
2813 if (bitmap_is_midx(bitmap_git))
2814 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2815 else
2816 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2817 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2818 oe = packlist_find(mapping, &oid);
2820 if (oe) {
2821 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2822 if (bitmap_git->hashes && !oe->hash)
2823 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2827 return reposition;
2830 void free_bitmap_index(struct bitmap_index *b)
2832 if (!b)
2833 return;
2835 if (b->map)
2836 munmap(b->map, b->map_size);
2837 ewah_pool_free(b->commits);
2838 ewah_pool_free(b->trees);
2839 ewah_pool_free(b->blobs);
2840 ewah_pool_free(b->tags);
2841 if (b->bitmaps) {
2842 struct stored_bitmap *sb;
2843 kh_foreach_value(b->bitmaps, sb, {
2844 ewah_pool_free(sb->root);
2845 free(sb);
2848 kh_destroy_oid_map(b->bitmaps);
2849 free(b->ext_index.objects);
2850 free(b->ext_index.hashes);
2851 kh_destroy_oid_pos(b->ext_index.positions);
2852 bitmap_free(b->result);
2853 bitmap_free(b->haves);
2854 if (bitmap_is_midx(b)) {
2856 * Multi-pack bitmaps need to have resources associated with
2857 * their on-disk reverse indexes unmapped so that stale .rev and
2858 * .bitmap files can be removed.
2860 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2861 * written in the same 'git multi-pack-index write --bitmap'
2862 * process. Close resources so they can be removed safely on
2863 * platforms like Windows.
2865 close_midx_revindex(b->midx);
2867 free_pseudo_merge_map(&b->pseudo_merges);
2868 free(b);
2871 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2872 const struct object_id *oid)
2874 return bitmap_git &&
2875 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2878 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2879 enum object_type object_type)
2881 struct bitmap *result = bitmap_git->result;
2882 off_t total = 0;
2883 struct ewah_iterator it;
2884 eword_t filter;
2885 size_t i;
2887 init_type_iterator(&it, bitmap_git, object_type);
2888 for (i = 0; i < result->word_alloc &&
2889 ewah_iterator_next(&filter, &it); i++) {
2890 eword_t word = result->words[i] & filter;
2891 size_t base = (i * BITS_IN_EWORD);
2892 unsigned offset;
2894 if (!word)
2895 continue;
2897 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2898 if ((word >> offset) == 0)
2899 break;
2901 offset += ewah_bit_ctz64(word >> offset);
2903 if (bitmap_is_midx(bitmap_git)) {
2904 uint32_t pack_pos;
2905 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2906 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2908 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2909 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2911 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2912 struct object_id oid;
2913 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2915 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2916 oid_to_hex(&oid),
2917 pack->pack_name,
2918 (uintmax_t)offset);
2921 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2922 } else {
2923 size_t pos = base + offset;
2924 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2925 pack_pos_to_offset(bitmap_git->pack, pos);
2930 return total;
2933 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2935 struct bitmap *result = bitmap_git->result;
2936 struct eindex *eindex = &bitmap_git->ext_index;
2937 off_t total = 0;
2938 struct object_info oi = OBJECT_INFO_INIT;
2939 off_t object_size;
2940 size_t i;
2942 oi.disk_sizep = &object_size;
2944 for (i = 0; i < eindex->count; i++) {
2945 struct object *obj = eindex->objects[i];
2947 if (!bitmap_get(result,
2948 st_add(bitmap_num_objects(bitmap_git), i)))
2949 continue;
2951 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2952 die(_("unable to get disk usage of '%s'"),
2953 oid_to_hex(&obj->oid));
2955 total += object_size;
2957 return total;
2960 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2961 struct rev_info *revs)
2963 off_t total = 0;
2965 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2966 if (revs->tree_objects)
2967 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2968 if (revs->blob_objects)
2969 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2970 if (revs->tag_objects)
2971 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2973 total += get_disk_usage_for_extended(bitmap_git);
2975 return total;
2978 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2980 return !!bitmap_git->midx;
2983 const struct string_list *bitmap_preferred_tips(struct repository *r)
2985 const struct string_list *dest;
2987 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2988 return dest;
2989 return NULL;
2992 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2994 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2995 struct string_list_item *item;
2997 if (!preferred_tips)
2998 return 0;
3000 for_each_string_list_item(item, preferred_tips) {
3001 if (starts_with(refname, item->string))
3002 return 1;
3005 return 0;
3008 static int verify_bitmap_file(const char *name)
3010 struct stat st;
3011 unsigned char *data;
3012 int fd = git_open(name);
3013 int res = 0;
3015 /* It is OK to not have the file. */
3016 if (fd < 0 || fstat(fd, &st)) {
3017 if (fd >= 0)
3018 close(fd);
3019 return 0;
3022 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3023 close(fd);
3024 if (!hashfile_checksum_valid(data, st.st_size))
3025 res = error(_("bitmap file '%s' has invalid checksum"),
3026 name);
3028 munmap(data, st.st_size);
3029 return res;
3032 int verify_bitmap_files(struct repository *r)
3034 int res = 0;
3036 for (struct multi_pack_index *m = get_multi_pack_index(r);
3037 m; m = m->next) {
3038 char *midx_bitmap_name = midx_bitmap_filename(m);
3039 res |= verify_bitmap_file(midx_bitmap_name);
3040 free(midx_bitmap_name);
3043 for (struct packed_git *p = get_all_packs(r);
3044 p; p = p->next) {
3045 char *pack_bitmap_name = pack_bitmap_filename(p);
3046 res |= verify_bitmap_file(pack_bitmap_name);
3047 free(pack_bitmap_name);
3050 return res;