t-reftable-block: release used block reader
[git/gitster.git] / pack-bitmap.c
blob2e657a2aa4ccdacbbe2fdb113e64696deba722f5
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->hash, 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 if (!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.hash, 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 struct bitmap *reuse,
2059 struct pack_window **w_curs)
2061 off_t offset, delta_obj_offset;
2062 enum object_type type;
2063 unsigned long size;
2065 if (pack_pos >= pack->p->num_objects)
2066 return -1; /* not actually in the pack */
2068 offset = delta_obj_offset = pack_pos_to_offset(pack->p, pack_pos);
2069 type = unpack_object_header(pack->p, w_curs, &offset, &size);
2070 if (type < 0)
2071 return -1; /* broken packfile, punt */
2073 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
2074 off_t base_offset;
2075 uint32_t base_pos;
2076 uint32_t base_bitmap_pos;
2079 * Find the position of the base object so we can look it up
2080 * in our bitmaps. If we can't come up with an offset, or if
2081 * that offset is not in the revidx, the pack is corrupt.
2082 * There's nothing we can do, so just punt on this object,
2083 * and the normal slow path will complain about it in
2084 * more detail.
2086 base_offset = get_delta_base(pack->p, w_curs, &offset, type,
2087 delta_obj_offset);
2088 if (!base_offset)
2089 return 0;
2091 offset_to_pack_pos(pack->p, base_offset, &base_pos);
2093 if (bitmap_is_midx(bitmap_git)) {
2095 * Cross-pack deltas are rejected for now, but could
2096 * theoretically be supported in the future.
2098 * We would need to ensure that we're sending both
2099 * halves of the delta/base pair, regardless of whether
2100 * or not the two cross a pack boundary. If they do,
2101 * then we must convert the delta to an REF_DELTA to
2102 * refer back to the base in the other pack.
2103 * */
2104 if (midx_pair_to_pack_pos(bitmap_git->midx,
2105 pack->pack_int_id,
2106 base_offset,
2107 &base_bitmap_pos) < 0) {
2108 return 0;
2110 } else {
2111 if (offset_to_pack_pos(pack->p, base_offset,
2112 &base_pos) < 0)
2113 return 0;
2115 * We assume delta dependencies always point backwards.
2116 * This lets us do a single pass, and is basically
2117 * always true due to the way OFS_DELTAs work. You would
2118 * not typically find REF_DELTA in a bitmapped pack,
2119 * since we only bitmap packs we write fresh, and
2120 * OFS_DELTA is the default). But let's double check to
2121 * make sure the pack wasn't written with odd
2122 * parameters.
2124 if (base_pos >= pack_pos)
2125 return 0;
2126 base_bitmap_pos = pack->bitmap_pos + base_pos;
2130 * And finally, if we're not sending the base as part of our
2131 * reuse chunk, then don't send this object either. The base
2132 * would come after us, along with other objects not
2133 * necessarily in the pack, which means we'd need to convert
2134 * to REF_DELTA on the fly. Better to just let the normal
2135 * object_entry code path handle it.
2137 if (!bitmap_get(reuse, base_bitmap_pos))
2138 return 0;
2142 * If we got here, then the object is OK to reuse. Mark it.
2144 bitmap_set(reuse, bitmap_pos);
2145 return 0;
2148 static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
2149 struct bitmapped_pack *pack,
2150 struct bitmap *reuse)
2152 struct bitmap *result = bitmap_git->result;
2153 struct pack_window *w_curs = NULL;
2154 size_t pos = pack->bitmap_pos / BITS_IN_EWORD;
2156 if (!pack->bitmap_pos) {
2158 * If we're processing the first (in the case of a MIDX, the
2159 * preferred pack) or the only (in the case of single-pack
2160 * bitmaps) pack, then we can reuse whole words at a time.
2162 * This is because we know that any deltas in this range *must*
2163 * have their bases chosen from the same pack, since:
2165 * - In the single pack case, there is no other pack to choose
2166 * them from.
2168 * - In the MIDX case, the first pack is the preferred pack, so
2169 * all ties are broken in favor of that pack (i.e. the one
2170 * we're currently processing). So any duplicate bases will be
2171 * resolved in favor of the pack we're processing.
2173 while (pos < result->word_alloc &&
2174 pos < pack->bitmap_nr / BITS_IN_EWORD &&
2175 result->words[pos] == (eword_t)~0)
2176 pos++;
2177 memset(reuse->words, 0xFF, pos * sizeof(eword_t));
2180 for (; pos < result->word_alloc; pos++) {
2181 eword_t word = result->words[pos];
2182 size_t offset;
2184 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2185 size_t bit_pos;
2186 uint32_t pack_pos;
2188 if (word >> offset == 0)
2189 break;
2191 offset += ewah_bit_ctz64(word >> offset);
2193 bit_pos = pos * BITS_IN_EWORD + offset;
2194 if (bit_pos < pack->bitmap_pos)
2195 continue;
2196 if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
2197 goto done;
2199 if (bitmap_is_midx(bitmap_git)) {
2200 uint32_t midx_pos;
2201 off_t ofs;
2203 midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2204 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
2206 if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
2207 BUG("could not find object in pack %s "
2208 "at offset %"PRIuMAX" in MIDX",
2209 pack_basename(pack->p), (uintmax_t)ofs);
2210 } else {
2211 pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
2212 if (pack_pos >= pack->p->num_objects)
2213 BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
2214 pack_basename(pack->p), (uintmax_t)pack_pos,
2215 pack->p->num_objects);
2218 if (try_partial_reuse(bitmap_git, pack, bit_pos,
2219 pack_pos, reuse, &w_curs) < 0) {
2221 * try_partial_reuse indicated we couldn't reuse
2222 * any bits, so there is no point in trying more
2223 * bits in the current word, or any other words
2224 * in result.
2226 * Jump out of both loops to avoid future
2227 * unnecessary calls to try_partial_reuse.
2229 goto done;
2234 done:
2235 unuse_pack(&w_curs);
2238 static int bitmapped_pack_cmp(const void *va, const void *vb)
2240 const struct bitmapped_pack *a = va;
2241 const struct bitmapped_pack *b = vb;
2243 if (a->bitmap_pos < b->bitmap_pos)
2244 return -1;
2245 if (a->bitmap_pos > b->bitmap_pos)
2246 return 1;
2247 return 0;
2250 void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2251 struct bitmapped_pack **packs_out,
2252 size_t *packs_nr_out,
2253 struct bitmap **reuse_out,
2254 int multi_pack_reuse)
2256 struct repository *r = the_repository;
2257 struct bitmapped_pack *packs = NULL;
2258 struct bitmap *result = bitmap_git->result;
2259 struct bitmap *reuse;
2260 size_t i;
2261 size_t packs_nr = 0, packs_alloc = 0;
2262 size_t word_alloc;
2263 uint32_t objects_nr = 0;
2265 assert(result);
2267 load_reverse_index(r, bitmap_git);
2269 if (!bitmap_is_midx(bitmap_git) || !bitmap_git->midx->chunk_bitmapped_packs)
2270 multi_pack_reuse = 0;
2272 if (multi_pack_reuse) {
2273 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
2274 struct bitmapped_pack pack;
2275 if (nth_bitmapped_pack(r, bitmap_git->midx, &pack, i) < 0) {
2276 warning(_("unable to load pack: '%s', disabling pack-reuse"),
2277 bitmap_git->midx->pack_names[i]);
2278 free(packs);
2279 return;
2282 if (!pack.bitmap_nr)
2283 continue;
2285 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2286 memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2288 objects_nr += pack.p->num_objects;
2291 QSORT(packs, packs_nr, bitmapped_pack_cmp);
2292 } else {
2293 struct packed_git *pack;
2294 uint32_t pack_int_id;
2296 if (bitmap_is_midx(bitmap_git)) {
2297 uint32_t preferred_pack_pos;
2299 if (midx_preferred_pack(bitmap_git->midx, &preferred_pack_pos) < 0) {
2300 warning(_("unable to compute preferred pack, disabling pack-reuse"));
2301 return;
2304 pack = bitmap_git->midx->packs[preferred_pack_pos];
2305 pack_int_id = preferred_pack_pos;
2306 } else {
2307 pack = bitmap_git->pack;
2309 * Any value for 'pack_int_id' will do here. When we
2310 * process the pack via try_partial_reuse(), we won't
2311 * use the `pack_int_id` field since we have a non-MIDX
2312 * bitmap.
2314 * Use '-1' as a sentinel value to make it clear
2315 * that we do not expect to read this field.
2317 pack_int_id = -1;
2320 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2321 packs[packs_nr].p = pack;
2322 packs[packs_nr].pack_int_id = pack_int_id;
2323 packs[packs_nr].bitmap_nr = pack->num_objects;
2324 packs[packs_nr].bitmap_pos = 0;
2326 objects_nr = packs[packs_nr++].bitmap_nr;
2329 word_alloc = objects_nr / BITS_IN_EWORD;
2330 if (objects_nr % BITS_IN_EWORD)
2331 word_alloc++;
2332 reuse = bitmap_word_alloc(word_alloc);
2334 for (i = 0; i < packs_nr; i++)
2335 reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse);
2337 if (bitmap_is_empty(reuse)) {
2338 free(packs);
2339 bitmap_free(reuse);
2340 return;
2344 * Drop any reused objects from the result, since they will not
2345 * need to be handled separately.
2347 bitmap_and_not(result, reuse);
2348 *packs_out = packs;
2349 *packs_nr_out = packs_nr;
2350 *reuse_out = reuse;
2353 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2354 struct bitmap *bitmap, const struct object_id *oid)
2356 int idx;
2358 if (!bitmap)
2359 return 0;
2361 idx = bitmap_position(bitmap_git, oid);
2362 return idx >= 0 && bitmap_get(bitmap, idx);
2365 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2366 struct rev_info *revs,
2367 show_reachable_fn show_reachable)
2369 assert(bitmap_git->result);
2371 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2372 if (revs->tree_objects)
2373 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2374 if (revs->blob_objects)
2375 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2376 if (revs->tag_objects)
2377 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2379 show_extended_objects(bitmap_git, revs, show_reachable);
2382 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2383 enum object_type type)
2385 struct bitmap *objects = bitmap_git->result;
2386 struct eindex *eindex = &bitmap_git->ext_index;
2388 uint32_t i = 0, count = 0;
2389 struct ewah_iterator it;
2390 eword_t filter;
2392 init_type_iterator(&it, bitmap_git, type);
2394 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2395 eword_t word = objects->words[i++] & filter;
2396 count += ewah_bit_popcount64(word);
2399 for (i = 0; i < eindex->count; ++i) {
2400 if (eindex->objects[i]->type == type &&
2401 bitmap_get(objects,
2402 st_add(bitmap_num_objects(bitmap_git), i)))
2403 count++;
2406 return count;
2409 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2410 uint32_t *commits, uint32_t *trees,
2411 uint32_t *blobs, uint32_t *tags)
2413 assert(bitmap_git->result);
2415 if (commits)
2416 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2418 if (trees)
2419 *trees = count_object_type(bitmap_git, OBJ_TREE);
2421 if (blobs)
2422 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2424 if (tags)
2425 *tags = count_object_type(bitmap_git, OBJ_TAG);
2428 struct bitmap_test_data {
2429 struct bitmap_index *bitmap_git;
2430 struct bitmap *base;
2431 struct bitmap *commits;
2432 struct bitmap *trees;
2433 struct bitmap *blobs;
2434 struct bitmap *tags;
2435 struct progress *prg;
2436 size_t seen;
2439 static void test_bitmap_type(struct bitmap_test_data *tdata,
2440 struct object *obj, int pos)
2442 enum object_type bitmap_type = OBJ_NONE;
2443 int bitmaps_nr = 0;
2445 if (bitmap_get(tdata->commits, pos)) {
2446 bitmap_type = OBJ_COMMIT;
2447 bitmaps_nr++;
2449 if (bitmap_get(tdata->trees, pos)) {
2450 bitmap_type = OBJ_TREE;
2451 bitmaps_nr++;
2453 if (bitmap_get(tdata->blobs, pos)) {
2454 bitmap_type = OBJ_BLOB;
2455 bitmaps_nr++;
2457 if (bitmap_get(tdata->tags, pos)) {
2458 bitmap_type = OBJ_TAG;
2459 bitmaps_nr++;
2462 if (bitmap_type == OBJ_NONE)
2463 die(_("object '%s' not found in type bitmaps"),
2464 oid_to_hex(&obj->oid));
2466 if (bitmaps_nr > 1)
2467 die(_("object '%s' does not have a unique type"),
2468 oid_to_hex(&obj->oid));
2470 if (bitmap_type != obj->type)
2471 die(_("object '%s': real type '%s', expected: '%s'"),
2472 oid_to_hex(&obj->oid),
2473 type_name(obj->type),
2474 type_name(bitmap_type));
2477 static void test_show_object(struct object *object,
2478 const char *name UNUSED,
2479 void *data)
2481 struct bitmap_test_data *tdata = data;
2482 int bitmap_pos;
2484 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2485 if (bitmap_pos < 0)
2486 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2487 test_bitmap_type(tdata, object, bitmap_pos);
2489 bitmap_set(tdata->base, bitmap_pos);
2490 display_progress(tdata->prg, ++tdata->seen);
2493 static void test_show_commit(struct commit *commit, void *data)
2495 struct bitmap_test_data *tdata = data;
2496 int bitmap_pos;
2498 bitmap_pos = bitmap_position(tdata->bitmap_git,
2499 &commit->object.oid);
2500 if (bitmap_pos < 0)
2501 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2502 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2504 bitmap_set(tdata->base, bitmap_pos);
2505 display_progress(tdata->prg, ++tdata->seen);
2508 void test_bitmap_walk(struct rev_info *revs)
2510 struct object *root;
2511 struct bitmap *result = NULL;
2512 size_t result_popcnt;
2513 struct bitmap_test_data tdata;
2514 struct bitmap_index *bitmap_git;
2515 struct ewah_bitmap *bm;
2517 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2518 die(_("failed to load bitmap indexes"));
2520 if (revs->pending.nr != 1)
2521 die(_("you must specify exactly one commit to test"));
2523 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2524 bitmap_git->version,
2525 bitmap_git->entry_count,
2526 bitmap_git->table_lookup ? "" : " loaded");
2528 root = revs->pending.objects[0].item;
2529 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2531 if (bm) {
2532 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2533 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2535 result = ewah_to_bitmap(bm);
2538 if (!result)
2539 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2541 revs->tag_objects = 1;
2542 revs->tree_objects = 1;
2543 revs->blob_objects = 1;
2545 result_popcnt = bitmap_popcount(result);
2547 if (prepare_revision_walk(revs))
2548 die(_("revision walk setup failed"));
2550 tdata.bitmap_git = bitmap_git;
2551 tdata.base = bitmap_new();
2552 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2553 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2554 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2555 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2556 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2557 tdata.seen = 0;
2559 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2561 stop_progress(&tdata.prg);
2563 if (bitmap_equals(result, tdata.base))
2564 fprintf_ln(stderr, "OK!");
2565 else
2566 die(_("mismatch in bitmap results"));
2568 bitmap_free(result);
2569 bitmap_free(tdata.base);
2570 bitmap_free(tdata.commits);
2571 bitmap_free(tdata.trees);
2572 bitmap_free(tdata.blobs);
2573 bitmap_free(tdata.tags);
2574 free_bitmap_index(bitmap_git);
2577 int test_bitmap_commits(struct repository *r)
2579 struct object_id oid;
2580 MAYBE_UNUSED void *value;
2581 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2583 if (!bitmap_git)
2584 die(_("failed to load bitmap indexes"));
2587 * As this function is only used to print bitmap selected
2588 * commits, we don't have to read the commit table.
2590 if (bitmap_git->table_lookup) {
2591 if (load_bitmap_entries_v1(bitmap_git) < 0)
2592 die(_("failed to load bitmap indexes"));
2595 kh_foreach(bitmap_git->bitmaps, oid, value, {
2596 printf_ln("%s", oid_to_hex(&oid));
2599 free_bitmap_index(bitmap_git);
2601 return 0;
2604 int test_bitmap_hashes(struct repository *r)
2606 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2607 struct object_id oid;
2608 uint32_t i, index_pos;
2610 if (!bitmap_git || !bitmap_git->hashes)
2611 goto cleanup;
2613 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2614 if (bitmap_is_midx(bitmap_git))
2615 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2616 else
2617 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2619 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2621 printf_ln("%s %"PRIu32"",
2622 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2625 cleanup:
2626 free_bitmap_index(bitmap_git);
2628 return 0;
2631 static void bit_pos_to_object_id(struct bitmap_index *bitmap_git,
2632 uint32_t bit_pos,
2633 struct object_id *oid)
2635 uint32_t index_pos;
2637 if (bitmap_is_midx(bitmap_git))
2638 index_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2639 else
2640 index_pos = pack_pos_to_index(bitmap_git->pack, bit_pos);
2642 nth_bitmap_object_oid(bitmap_git, oid, index_pos);
2645 int test_bitmap_pseudo_merges(struct repository *r)
2647 struct bitmap_index *bitmap_git;
2648 uint32_t i;
2650 bitmap_git = prepare_bitmap_git(r);
2651 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2652 goto cleanup;
2654 for (i = 0; i < bitmap_git->pseudo_merges.nr; i++) {
2655 struct pseudo_merge *merge;
2656 struct ewah_bitmap *commits_bitmap, *merge_bitmap;
2658 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2659 &bitmap_git->pseudo_merges.v[i]);
2660 commits_bitmap = merge->commits;
2661 merge_bitmap = pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
2662 merge);
2664 printf("at=%"PRIuMAX", commits=%"PRIuMAX", objects=%"PRIuMAX"\n",
2665 (uintmax_t)merge->at,
2666 (uintmax_t)ewah_bitmap_popcount(commits_bitmap),
2667 (uintmax_t)ewah_bitmap_popcount(merge_bitmap));
2670 cleanup:
2671 free_bitmap_index(bitmap_git);
2672 return 0;
2675 static void dump_ewah_object_ids(struct bitmap_index *bitmap_git,
2676 struct ewah_bitmap *bitmap)
2679 struct ewah_iterator it;
2680 eword_t word;
2681 uint32_t pos = 0;
2683 ewah_iterator_init(&it, bitmap);
2685 while (ewah_iterator_next(&word, &it)) {
2686 struct object_id oid;
2687 uint32_t offset;
2689 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2690 if (!(word >> offset))
2691 break;
2693 offset += ewah_bit_ctz64(word >> offset);
2695 bit_pos_to_object_id(bitmap_git, pos + offset, &oid);
2696 printf("%s\n", oid_to_hex(&oid));
2698 pos += BITS_IN_EWORD;
2702 int test_bitmap_pseudo_merge_commits(struct repository *r, uint32_t n)
2704 struct bitmap_index *bitmap_git;
2705 struct pseudo_merge *merge;
2706 int ret = 0;
2708 bitmap_git = prepare_bitmap_git(r);
2709 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2710 goto cleanup;
2712 if (n >= bitmap_git->pseudo_merges.nr) {
2713 ret = error(_("pseudo-merge index out of range "
2714 "(%"PRIu32" >= %"PRIuMAX")"),
2715 n, (uintmax_t)bitmap_git->pseudo_merges.nr);
2716 goto cleanup;
2719 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2720 &bitmap_git->pseudo_merges.v[n]);
2721 dump_ewah_object_ids(bitmap_git, merge->commits);
2723 cleanup:
2724 free_bitmap_index(bitmap_git);
2725 return ret;
2728 int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n)
2730 struct bitmap_index *bitmap_git;
2731 struct pseudo_merge *merge;
2732 int ret = 0;
2734 bitmap_git = prepare_bitmap_git(r);
2735 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2736 goto cleanup;
2738 if (n >= bitmap_git->pseudo_merges.nr) {
2739 ret = error(_("pseudo-merge index out of range "
2740 "(%"PRIu32" >= %"PRIuMAX")"),
2741 n, (uintmax_t)bitmap_git->pseudo_merges.nr);
2742 goto cleanup;
2745 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2746 &bitmap_git->pseudo_merges.v[n]);
2748 dump_ewah_object_ids(bitmap_git,
2749 pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
2750 merge));
2752 cleanup:
2753 free_bitmap_index(bitmap_git);
2754 return ret;
2757 int rebuild_bitmap(const uint32_t *reposition,
2758 struct ewah_bitmap *source,
2759 struct bitmap *dest)
2761 uint32_t pos = 0;
2762 struct ewah_iterator it;
2763 eword_t word;
2765 ewah_iterator_init(&it, source);
2767 while (ewah_iterator_next(&word, &it)) {
2768 uint32_t offset, bit_pos;
2770 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2771 if ((word >> offset) == 0)
2772 break;
2774 offset += ewah_bit_ctz64(word >> offset);
2776 bit_pos = reposition[pos + offset];
2777 if (bit_pos > 0)
2778 bitmap_set(dest, bit_pos - 1);
2779 else /* can't reuse, we don't have the object */
2780 return -1;
2783 pos += BITS_IN_EWORD;
2785 return 0;
2788 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2789 struct packing_data *mapping)
2791 struct repository *r = the_repository;
2792 uint32_t i, num_objects;
2793 uint32_t *reposition;
2795 if (!bitmap_is_midx(bitmap_git))
2796 load_reverse_index(r, bitmap_git);
2797 else if (load_midx_revindex(bitmap_git->midx))
2798 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2799 "extension");
2801 num_objects = bitmap_num_objects(bitmap_git);
2802 CALLOC_ARRAY(reposition, num_objects);
2804 for (i = 0; i < num_objects; ++i) {
2805 struct object_id oid;
2806 struct object_entry *oe;
2807 uint32_t index_pos;
2809 if (bitmap_is_midx(bitmap_git))
2810 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2811 else
2812 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2813 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2814 oe = packlist_find(mapping, &oid);
2816 if (oe) {
2817 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2818 if (bitmap_git->hashes && !oe->hash)
2819 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2823 return reposition;
2826 void free_bitmap_index(struct bitmap_index *b)
2828 if (!b)
2829 return;
2831 if (b->map)
2832 munmap(b->map, b->map_size);
2833 ewah_pool_free(b->commits);
2834 ewah_pool_free(b->trees);
2835 ewah_pool_free(b->blobs);
2836 ewah_pool_free(b->tags);
2837 if (b->bitmaps) {
2838 struct stored_bitmap *sb;
2839 kh_foreach_value(b->bitmaps, sb, {
2840 ewah_pool_free(sb->root);
2841 free(sb);
2844 kh_destroy_oid_map(b->bitmaps);
2845 free(b->ext_index.objects);
2846 free(b->ext_index.hashes);
2847 kh_destroy_oid_pos(b->ext_index.positions);
2848 bitmap_free(b->result);
2849 bitmap_free(b->haves);
2850 if (bitmap_is_midx(b)) {
2852 * Multi-pack bitmaps need to have resources associated with
2853 * their on-disk reverse indexes unmapped so that stale .rev and
2854 * .bitmap files can be removed.
2856 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2857 * written in the same 'git multi-pack-index write --bitmap'
2858 * process. Close resources so they can be removed safely on
2859 * platforms like Windows.
2861 close_midx_revindex(b->midx);
2863 free_pseudo_merge_map(&b->pseudo_merges);
2864 free(b);
2867 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2868 const struct object_id *oid)
2870 return bitmap_git &&
2871 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2874 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2875 enum object_type object_type)
2877 struct bitmap *result = bitmap_git->result;
2878 off_t total = 0;
2879 struct ewah_iterator it;
2880 eword_t filter;
2881 size_t i;
2883 init_type_iterator(&it, bitmap_git, object_type);
2884 for (i = 0; i < result->word_alloc &&
2885 ewah_iterator_next(&filter, &it); i++) {
2886 eword_t word = result->words[i] & filter;
2887 size_t base = (i * BITS_IN_EWORD);
2888 unsigned offset;
2890 if (!word)
2891 continue;
2893 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2894 if ((word >> offset) == 0)
2895 break;
2897 offset += ewah_bit_ctz64(word >> offset);
2899 if (bitmap_is_midx(bitmap_git)) {
2900 uint32_t pack_pos;
2901 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2902 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2904 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2905 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2907 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2908 struct object_id oid;
2909 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2911 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2912 oid_to_hex(&oid),
2913 pack->pack_name,
2914 (uintmax_t)offset);
2917 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2918 } else {
2919 size_t pos = base + offset;
2920 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2921 pack_pos_to_offset(bitmap_git->pack, pos);
2926 return total;
2929 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2931 struct bitmap *result = bitmap_git->result;
2932 struct eindex *eindex = &bitmap_git->ext_index;
2933 off_t total = 0;
2934 struct object_info oi = OBJECT_INFO_INIT;
2935 off_t object_size;
2936 size_t i;
2938 oi.disk_sizep = &object_size;
2940 for (i = 0; i < eindex->count; i++) {
2941 struct object *obj = eindex->objects[i];
2943 if (!bitmap_get(result,
2944 st_add(bitmap_num_objects(bitmap_git), i)))
2945 continue;
2947 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2948 die(_("unable to get disk usage of '%s'"),
2949 oid_to_hex(&obj->oid));
2951 total += object_size;
2953 return total;
2956 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2957 struct rev_info *revs)
2959 off_t total = 0;
2961 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2962 if (revs->tree_objects)
2963 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2964 if (revs->blob_objects)
2965 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2966 if (revs->tag_objects)
2967 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2969 total += get_disk_usage_for_extended(bitmap_git);
2971 return total;
2974 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2976 return !!bitmap_git->midx;
2979 const struct string_list *bitmap_preferred_tips(struct repository *r)
2981 const struct string_list *dest;
2983 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2984 return dest;
2985 return NULL;
2988 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2990 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2991 struct string_list_item *item;
2993 if (!preferred_tips)
2994 return 0;
2996 for_each_string_list_item(item, preferred_tips) {
2997 if (starts_with(refname, item->string))
2998 return 1;
3001 return 0;
3004 static int verify_bitmap_file(const char *name)
3006 struct stat st;
3007 unsigned char *data;
3008 int fd = git_open(name);
3009 int res = 0;
3011 /* It is OK to not have the file. */
3012 if (fd < 0 || fstat(fd, &st)) {
3013 if (fd >= 0)
3014 close(fd);
3015 return 0;
3018 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3019 close(fd);
3020 if (!hashfile_checksum_valid(data, st.st_size))
3021 res = error(_("bitmap file '%s' has invalid checksum"),
3022 name);
3024 munmap(data, st.st_size);
3025 return res;
3028 int verify_bitmap_files(struct repository *r)
3030 int res = 0;
3032 for (struct multi_pack_index *m = get_multi_pack_index(r);
3033 m; m = m->next) {
3034 char *midx_bitmap_name = midx_bitmap_filename(m);
3035 res |= verify_bitmap_file(midx_bitmap_name);
3036 free(midx_bitmap_name);
3039 for (struct packed_git *p = get_all_packs(r);
3040 p; p = p->next) {
3041 char *pack_bitmap_name = pack_bitmap_filename(p);
3042 res |= verify_bitmap_file(pack_bitmap_name);
3043 free(pack_bitmap_name);
3046 return res;