A bit more topics before -rc1
[git.git] / pack-bitmap.c
blob35c5ef9d3cd15ed34ecc3e77aee0d0f8eba766a5
1 #include "git-compat-util.h"
2 #include "commit.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "strbuf.h"
6 #include "tag.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "progress.h"
10 #include "list-objects.h"
11 #include "pack.h"
12 #include "pack-bitmap.h"
13 #include "pack-revindex.h"
14 #include "pack-objects.h"
15 #include "packfile.h"
16 #include "repository.h"
17 #include "trace2.h"
18 #include "object-file.h"
19 #include "object-store-ll.h"
20 #include "list-objects-filter-options.h"
21 #include "midx.h"
22 #include "config.h"
25 * An entry on the bitmap index, representing the bitmap for a given
26 * commit.
28 struct stored_bitmap {
29 struct object_id oid;
30 struct ewah_bitmap *root;
31 struct stored_bitmap *xor;
32 int flags;
36 * The active bitmap index for a repository. By design, repositories only have
37 * a single bitmap index available (the index for the biggest packfile in
38 * the repository), since bitmap indexes need full closure.
40 * If there is more than one bitmap index available (e.g. because of alternates),
41 * the active bitmap index is the largest one.
43 struct bitmap_index {
45 * The pack or multi-pack index (MIDX) that this bitmap index belongs
46 * to.
48 * Exactly one of these must be non-NULL; this specifies the object
49 * order used to interpret this bitmap.
51 struct packed_git *pack;
52 struct multi_pack_index *midx;
54 /* mmapped buffer of the whole bitmap index */
55 unsigned char *map;
56 size_t map_size; /* size of the mmaped buffer */
57 size_t map_pos; /* current position when loading the index */
60 * Type indexes.
62 * Each bitmap marks which objects in the packfile are of the given
63 * type. This provides type information when yielding the objects from
64 * the packfile during a walk, which allows for better delta bases.
66 struct ewah_bitmap *commits;
67 struct ewah_bitmap *trees;
68 struct ewah_bitmap *blobs;
69 struct ewah_bitmap *tags;
71 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
72 kh_oid_map_t *bitmaps;
74 /* Number of bitmapped commits */
75 uint32_t entry_count;
77 /* If not NULL, this is a name-hash cache pointing into map. */
78 uint32_t *hashes;
80 /* The checksum of the packfile or MIDX; points into map. */
81 const unsigned char *checksum;
84 * If not NULL, this point into the commit table extension
85 * (within the memory mapped region `map`).
87 unsigned char *table_lookup;
90 * Extended index.
92 * When trying to perform bitmap operations with objects that are not
93 * packed in `pack`, these objects are added to this "fake index" and
94 * are assumed to appear at the end of the packfile for all operations
96 struct eindex {
97 struct object **objects;
98 uint32_t *hashes;
99 uint32_t count, alloc;
100 kh_oid_pos_t *positions;
101 } ext_index;
103 /* Bitmap result of the last performed walk */
104 struct bitmap *result;
106 /* "have" bitmap from the last performed walk */
107 struct bitmap *haves;
109 /* Version of the bitmap index */
110 unsigned int version;
113 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
115 struct ewah_bitmap *parent;
116 struct ewah_bitmap *composed;
118 if (!st->xor)
119 return st->root;
121 composed = ewah_pool_new();
122 parent = lookup_stored_bitmap(st->xor);
123 ewah_xor(st->root, parent, composed);
125 ewah_pool_free(st->root);
126 st->root = composed;
127 st->xor = NULL;
129 return composed;
133 * Read a bitmap from the current read position on the mmaped
134 * index, and increase the read position accordingly
136 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
138 struct ewah_bitmap *b = ewah_pool_new();
140 ssize_t bitmap_size = ewah_read_mmap(b,
141 index->map + index->map_pos,
142 index->map_size - index->map_pos);
144 if (bitmap_size < 0) {
145 error(_("failed to load bitmap index (corrupted?)"));
146 ewah_pool_free(b);
147 return NULL;
150 index->map_pos += bitmap_size;
151 return b;
154 static uint32_t bitmap_num_objects(struct bitmap_index *index)
156 if (index->midx)
157 return index->midx->num_objects;
158 return index->pack->num_objects;
161 static int load_bitmap_header(struct bitmap_index *index)
163 struct bitmap_disk_header *header = (void *)index->map;
164 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
166 if (index->map_size < header_size + the_hash_algo->rawsz)
167 return error(_("corrupted bitmap index (too small)"));
169 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
170 return error(_("corrupted bitmap index file (wrong header)"));
172 index->version = ntohs(header->version);
173 if (index->version != 1)
174 return error(_("unsupported version '%d' for bitmap index file"), index->version);
176 /* Parse known bitmap format options */
178 uint32_t flags = ntohs(header->options);
179 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
180 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
182 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
183 BUG("unsupported options for bitmap index file "
184 "(Git requires BITMAP_OPT_FULL_DAG)");
186 if (flags & BITMAP_OPT_HASH_CACHE) {
187 if (cache_size > index_end - index->map - header_size)
188 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
189 index->hashes = (void *)(index_end - cache_size);
190 index_end -= cache_size;
193 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
194 size_t table_size = st_mult(ntohl(header->entry_count),
195 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
196 if (table_size > index_end - index->map - header_size)
197 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
198 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
199 index->table_lookup = (void *)(index_end - table_size);
200 index_end -= table_size;
204 index->entry_count = ntohl(header->entry_count);
205 index->checksum = header->checksum;
206 index->map_pos += header_size;
207 return 0;
210 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
211 struct ewah_bitmap *root,
212 const struct object_id *oid,
213 struct stored_bitmap *xor_with,
214 int flags)
216 struct stored_bitmap *stored;
217 khiter_t hash_pos;
218 int ret;
220 stored = xmalloc(sizeof(struct stored_bitmap));
221 stored->root = root;
222 stored->xor = xor_with;
223 stored->flags = flags;
224 oidcpy(&stored->oid, oid);
226 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
229 * A 0 return code means the insertion succeeded with no changes,
230 * because the SHA1 already existed on the map. This is bad, there
231 * shouldn't be duplicated commits in the index.
233 if (ret == 0) {
234 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
235 return NULL;
238 kh_value(index->bitmaps, hash_pos) = stored;
239 return stored;
242 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
244 uint32_t result = get_be32(buffer + *pos);
245 (*pos) += sizeof(result);
246 return result;
249 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
251 return buffer[(*pos)++];
254 #define MAX_XOR_OFFSET 160
256 static int nth_bitmap_object_oid(struct bitmap_index *index,
257 struct object_id *oid,
258 uint32_t n)
260 if (index->midx)
261 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
262 return nth_packed_object_id(oid, index->pack, n);
265 static int load_bitmap_entries_v1(struct bitmap_index *index)
267 uint32_t i;
268 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
270 for (i = 0; i < index->entry_count; ++i) {
271 int xor_offset, flags;
272 struct ewah_bitmap *bitmap = NULL;
273 struct stored_bitmap *xor_bitmap = NULL;
274 uint32_t commit_idx_pos;
275 struct object_id oid;
277 if (index->map_size - index->map_pos < 6)
278 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
280 commit_idx_pos = read_be32(index->map, &index->map_pos);
281 xor_offset = read_u8(index->map, &index->map_pos);
282 flags = read_u8(index->map, &index->map_pos);
284 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
285 return error(_("corrupt ewah bitmap: commit index %u out of range"),
286 (unsigned)commit_idx_pos);
288 bitmap = read_bitmap_1(index);
289 if (!bitmap)
290 return -1;
292 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
293 return error(_("corrupted bitmap pack index"));
295 if (xor_offset > 0) {
296 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
298 if (!xor_bitmap)
299 return error(_("invalid XOR offset in bitmap pack index"));
302 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
303 index, bitmap, &oid, xor_bitmap, flags);
306 return 0;
309 char *midx_bitmap_filename(struct multi_pack_index *midx)
311 struct strbuf buf = STRBUF_INIT;
313 get_midx_filename(&buf, midx->object_dir);
314 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
316 return strbuf_detach(&buf, NULL);
319 char *pack_bitmap_filename(struct packed_git *p)
321 size_t len;
323 if (!strip_suffix(p->pack_name, ".pack", &len))
324 BUG("pack_name does not end in .pack");
325 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
328 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
329 struct multi_pack_index *midx)
331 struct stat st;
332 char *bitmap_name = midx_bitmap_filename(midx);
333 int fd = git_open(bitmap_name);
334 uint32_t i, preferred_pack;
335 struct packed_git *preferred;
337 if (fd < 0) {
338 if (errno != ENOENT)
339 warning_errno("cannot open '%s'", bitmap_name);
340 free(bitmap_name);
341 return -1;
343 free(bitmap_name);
345 if (fstat(fd, &st)) {
346 error_errno(_("cannot fstat bitmap file"));
347 close(fd);
348 return -1;
351 if (bitmap_git->pack || bitmap_git->midx) {
352 struct strbuf buf = STRBUF_INIT;
353 get_midx_filename(&buf, midx->object_dir);
354 trace2_data_string("bitmap", the_repository,
355 "ignoring extra midx bitmap file", buf.buf);
356 close(fd);
357 strbuf_release(&buf);
358 return -1;
361 bitmap_git->midx = midx;
362 bitmap_git->map_size = xsize_t(st.st_size);
363 bitmap_git->map_pos = 0;
364 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
365 MAP_PRIVATE, fd, 0);
366 close(fd);
368 if (load_bitmap_header(bitmap_git) < 0)
369 goto cleanup;
371 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
372 error(_("checksum doesn't match in MIDX and bitmap"));
373 goto cleanup;
376 if (load_midx_revindex(bitmap_git->midx)) {
377 warning(_("multi-pack bitmap is missing required reverse index"));
378 goto cleanup;
381 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
382 if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
383 warning(_("could not open pack %s"),
384 bitmap_git->midx->pack_names[i]);
385 goto cleanup;
389 if (midx_preferred_pack(bitmap_git->midx, &preferred_pack) < 0) {
390 warning(_("could not determine MIDX preferred pack"));
391 goto cleanup;
394 preferred = bitmap_git->midx->packs[preferred_pack];
395 if (!is_pack_valid(preferred)) {
396 warning(_("preferred pack (%s) is invalid"),
397 preferred->pack_name);
398 goto cleanup;
401 return 0;
403 cleanup:
404 munmap(bitmap_git->map, bitmap_git->map_size);
405 bitmap_git->map_size = 0;
406 bitmap_git->map_pos = 0;
407 bitmap_git->map = NULL;
408 bitmap_git->midx = NULL;
409 return -1;
412 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
414 int fd;
415 struct stat st;
416 char *bitmap_name;
418 bitmap_name = pack_bitmap_filename(packfile);
419 fd = git_open(bitmap_name);
421 if (fd < 0) {
422 if (errno != ENOENT)
423 warning_errno("cannot open '%s'", bitmap_name);
424 free(bitmap_name);
425 return -1;
427 free(bitmap_name);
429 if (fstat(fd, &st)) {
430 error_errno(_("cannot fstat bitmap file"));
431 close(fd);
432 return -1;
435 if (bitmap_git->pack || bitmap_git->midx) {
436 trace2_data_string("bitmap", the_repository,
437 "ignoring extra bitmap file", packfile->pack_name);
438 close(fd);
439 return -1;
442 if (!is_pack_valid(packfile)) {
443 close(fd);
444 return -1;
447 bitmap_git->pack = packfile;
448 bitmap_git->map_size = xsize_t(st.st_size);
449 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
450 bitmap_git->map_pos = 0;
451 close(fd);
453 if (load_bitmap_header(bitmap_git) < 0) {
454 munmap(bitmap_git->map, bitmap_git->map_size);
455 bitmap_git->map = NULL;
456 bitmap_git->map_size = 0;
457 bitmap_git->map_pos = 0;
458 bitmap_git->pack = NULL;
459 return -1;
462 trace2_data_string("bitmap", the_repository, "opened bitmap file",
463 packfile->pack_name);
464 return 0;
467 static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
469 if (bitmap_is_midx(bitmap_git)) {
470 uint32_t i;
471 int ret;
474 * The multi-pack-index's .rev file is already loaded via
475 * open_pack_bitmap_1().
477 * But we still need to open the individual pack .rev files,
478 * since we will need to make use of them in pack-objects.
480 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
481 ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
482 if (ret)
483 return ret;
485 return 0;
487 return load_pack_revindex(r, bitmap_git->pack);
490 static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
492 assert(bitmap_git->map);
494 bitmap_git->bitmaps = kh_init_oid_map();
495 bitmap_git->ext_index.positions = kh_init_oid_pos();
497 if (load_reverse_index(r, bitmap_git))
498 goto failed;
500 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
501 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
502 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
503 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
504 goto failed;
506 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
507 goto failed;
509 return 0;
511 failed:
512 munmap(bitmap_git->map, bitmap_git->map_size);
513 bitmap_git->map = NULL;
514 bitmap_git->map_size = 0;
516 kh_destroy_oid_map(bitmap_git->bitmaps);
517 bitmap_git->bitmaps = NULL;
519 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
520 bitmap_git->ext_index.positions = NULL;
522 return -1;
525 static int open_pack_bitmap(struct repository *r,
526 struct bitmap_index *bitmap_git)
528 struct packed_git *p;
529 int ret = -1;
531 for (p = get_all_packs(r); p; p = p->next) {
532 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
533 ret = 0;
535 * The only reason to keep looking is to report
536 * duplicates.
538 if (!trace2_is_enabled())
539 break;
543 return ret;
546 static int open_midx_bitmap(struct repository *r,
547 struct bitmap_index *bitmap_git)
549 int ret = -1;
550 struct multi_pack_index *midx;
552 assert(!bitmap_git->map);
554 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
555 if (!open_midx_bitmap_1(bitmap_git, midx))
556 ret = 0;
558 return ret;
561 static int open_bitmap(struct repository *r,
562 struct bitmap_index *bitmap_git)
564 int found;
566 assert(!bitmap_git->map);
568 found = !open_midx_bitmap(r, bitmap_git);
571 * these will all be skipped if we opened a midx bitmap; but run it
572 * anyway if tracing is enabled to report the duplicates
574 if (!found || trace2_is_enabled())
575 found |= !open_pack_bitmap(r, bitmap_git);
577 return found ? 0 : -1;
580 struct bitmap_index *prepare_bitmap_git(struct repository *r)
582 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
584 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
585 return bitmap_git;
587 free_bitmap_index(bitmap_git);
588 return NULL;
591 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
593 struct repository *r = the_repository;
594 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
596 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
597 return bitmap_git;
599 free_bitmap_index(bitmap_git);
600 return NULL;
603 struct include_data {
604 struct bitmap_index *bitmap_git;
605 struct bitmap *base;
606 struct bitmap *seen;
609 struct bitmap_lookup_table_triplet {
610 uint32_t commit_pos;
611 uint64_t offset;
612 uint32_t xor_row;
615 struct bitmap_lookup_table_xor_item {
616 struct object_id oid;
617 uint64_t offset;
621 * Given a `triplet` struct pointer and pointer `p`, this
622 * function reads the triplet beginning at `p` into the struct.
623 * Note that this function assumes that there is enough memory
624 * left for filling the `triplet` struct from `p`.
626 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
627 const unsigned char *p)
629 if (!triplet)
630 return -1;
632 triplet->commit_pos = get_be32(p);
633 p += sizeof(uint32_t);
634 triplet->offset = get_be64(p);
635 p += sizeof(uint64_t);
636 triplet->xor_row = get_be32(p);
637 return 0;
641 * This function gets the raw triplet from `row`'th row in the
642 * lookup table and fills that data to the `triplet`.
644 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
645 uint32_t pos,
646 struct bitmap_lookup_table_triplet *triplet)
648 unsigned char *p = NULL;
649 if (pos >= bitmap_git->entry_count)
650 return error(_("corrupt bitmap lookup table: triplet position out of index"));
652 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
654 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
658 * Searches for a matching triplet. `commit_pos` is a pointer
659 * to the wanted commit position value. `table_entry` points to
660 * a triplet in lookup table. The first 4 bytes of each
661 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
663 static int triplet_cmp(const void *commit_pos, const void *table_entry)
666 uint32_t a = *(uint32_t *)commit_pos;
667 uint32_t b = get_be32(table_entry);
668 if (a > b)
669 return 1;
670 else if (a < b)
671 return -1;
673 return 0;
676 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
677 struct object_id *oid,
678 uint32_t *result)
680 int found;
682 if (bitmap_is_midx(bitmap_git))
683 found = bsearch_midx(oid, bitmap_git->midx, result);
684 else
685 found = bsearch_pack(oid, bitmap_git->pack, result);
687 return found;
691 * `bsearch_triplet_by_pos` function searches for the raw triplet
692 * having commit position same as `commit_pos` and fills `triplet`
693 * object from the raw triplet. Returns 1 on success and 0 on
694 * failure.
696 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
697 struct bitmap_index *bitmap_git,
698 struct bitmap_lookup_table_triplet *triplet)
700 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
701 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
703 if (!p)
704 return -1;
706 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
709 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
710 struct commit *commit)
712 uint32_t commit_pos, xor_row;
713 uint64_t offset;
714 int flags;
715 struct bitmap_lookup_table_triplet triplet;
716 struct object_id *oid = &commit->object.oid;
717 struct ewah_bitmap *bitmap;
718 struct stored_bitmap *xor_bitmap = NULL;
719 const int bitmap_header_size = 6;
720 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
721 static size_t xor_items_nr = 0, xor_items_alloc = 0;
722 static int is_corrupt = 0;
723 int xor_flags;
724 khiter_t hash_pos;
725 struct bitmap_lookup_table_xor_item *xor_item;
727 if (is_corrupt)
728 return NULL;
730 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
731 return NULL;
733 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
734 return NULL;
736 xor_items_nr = 0;
737 offset = triplet.offset;
738 xor_row = triplet.xor_row;
740 while (xor_row != 0xffffffff) {
741 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
743 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
744 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
745 goto corrupt;
748 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
749 goto corrupt;
751 xor_item = &xor_items[xor_items_nr];
752 xor_item->offset = triplet.offset;
754 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
755 error(_("corrupt bitmap lookup table: commit index %u out of range"),
756 triplet.commit_pos);
757 goto corrupt;
760 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
763 * If desired bitmap is already stored, we don't need
764 * to iterate further. Because we know that bitmaps
765 * that are needed to be parsed to parse this bitmap
766 * has already been stored. So, assign this stored bitmap
767 * to the xor_bitmap.
769 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
770 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
771 break;
772 xor_items_nr++;
773 xor_row = triplet.xor_row;
776 while (xor_items_nr) {
777 xor_item = &xor_items[xor_items_nr - 1];
778 bitmap_git->map_pos = xor_item->offset;
779 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
780 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
781 oid_to_hex(&xor_item->oid));
782 goto corrupt;
785 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
786 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
787 bitmap = read_bitmap_1(bitmap_git);
789 if (!bitmap)
790 goto corrupt;
792 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
793 xor_items_nr--;
796 bitmap_git->map_pos = offset;
797 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
798 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
799 oid_to_hex(oid));
800 goto corrupt;
804 * Don't bother reading the commit's index position or its xor
805 * offset:
807 * - The commit's index position is irrelevant to us, since
808 * load_bitmap_entries_v1 only uses it to learn the object
809 * id which is used to compute the hashmap's key. We already
810 * have an object id, so no need to look it up again.
812 * - The xor_offset is unusable for us, since it specifies how
813 * many entries previous to ours we should look at. This
814 * makes sense when reading the bitmaps sequentially (as in
815 * load_bitmap_entries_v1()), since we can keep track of
816 * each bitmap as we read them.
818 * But it can't work for us, since the bitmap's don't have a
819 * fixed size. So we learn the position of the xor'd bitmap
820 * from the commit table (and resolve it to a bitmap in the
821 * above if-statement).
823 * Instead, we can skip ahead and immediately read the flags and
824 * ewah bitmap.
826 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
827 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
828 bitmap = read_bitmap_1(bitmap_git);
830 if (!bitmap)
831 goto corrupt;
833 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
835 corrupt:
836 free(xor_items);
837 is_corrupt = 1;
838 return NULL;
841 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
842 struct commit *commit)
844 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
845 commit->object.oid);
846 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
847 struct stored_bitmap *bitmap = NULL;
848 if (!bitmap_git->table_lookup)
849 return NULL;
851 /* this is a fairly hot codepath - no trace2_region please */
852 /* NEEDSWORK: cache misses aren't recorded */
853 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
854 if (!bitmap)
855 return NULL;
856 return lookup_stored_bitmap(bitmap);
858 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
861 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
862 const struct object_id *oid)
864 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
865 khiter_t pos = kh_get_oid_pos(positions, *oid);
867 if (pos < kh_end(positions)) {
868 int bitmap_pos = kh_value(positions, pos);
869 return bitmap_pos + bitmap_num_objects(bitmap_git);
872 return -1;
875 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
876 const struct object_id *oid)
878 uint32_t pos;
879 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
880 if (!offset)
881 return -1;
883 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
884 return -1;
885 return pos;
888 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
889 const struct object_id *oid)
891 uint32_t want, got;
892 if (!bsearch_midx(oid, bitmap_git->midx, &want))
893 return -1;
895 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
896 return -1;
897 return got;
900 static int bitmap_position(struct bitmap_index *bitmap_git,
901 const struct object_id *oid)
903 int pos;
904 if (bitmap_is_midx(bitmap_git))
905 pos = bitmap_position_midx(bitmap_git, oid);
906 else
907 pos = bitmap_position_packfile(bitmap_git, oid);
908 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
911 static int ext_index_add_object(struct bitmap_index *bitmap_git,
912 struct object *object, const char *name)
914 struct eindex *eindex = &bitmap_git->ext_index;
916 khiter_t hash_pos;
917 int hash_ret;
918 int bitmap_pos;
920 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
921 if (hash_ret > 0) {
922 if (eindex->count >= eindex->alloc) {
923 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
924 REALLOC_ARRAY(eindex->objects, eindex->alloc);
925 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
928 bitmap_pos = eindex->count;
929 eindex->objects[eindex->count] = object;
930 eindex->hashes[eindex->count] = pack_name_hash(name);
931 kh_value(eindex->positions, hash_pos) = bitmap_pos;
932 eindex->count++;
933 } else {
934 bitmap_pos = kh_value(eindex->positions, hash_pos);
937 return bitmap_pos + bitmap_num_objects(bitmap_git);
940 struct bitmap_show_data {
941 struct bitmap_index *bitmap_git;
942 struct bitmap *base;
945 static void show_object(struct object *object, const char *name, void *data_)
947 struct bitmap_show_data *data = data_;
948 int bitmap_pos;
950 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
952 if (bitmap_pos < 0)
953 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
954 name);
956 bitmap_set(data->base, bitmap_pos);
959 static void show_commit(struct commit *commit UNUSED,
960 void *data UNUSED)
964 static int add_to_include_set(struct bitmap_index *bitmap_git,
965 struct include_data *data,
966 struct commit *commit,
967 int bitmap_pos)
969 struct ewah_bitmap *partial;
971 if (data->seen && bitmap_get(data->seen, bitmap_pos))
972 return 0;
974 if (bitmap_get(data->base, bitmap_pos))
975 return 0;
977 partial = bitmap_for_commit(bitmap_git, commit);
978 if (partial) {
979 bitmap_or_ewah(data->base, partial);
980 return 0;
983 bitmap_set(data->base, bitmap_pos);
984 return 1;
987 static int should_include(struct commit *commit, void *_data)
989 struct include_data *data = _data;
990 int bitmap_pos;
992 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
993 if (bitmap_pos < 0)
994 bitmap_pos = ext_index_add_object(data->bitmap_git,
995 (struct object *)commit,
996 NULL);
998 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
999 struct commit_list *parent = commit->parents;
1001 while (parent) {
1002 parent->item->object.flags |= SEEN;
1003 parent = parent->next;
1006 return 0;
1009 return 1;
1012 static int should_include_obj(struct object *obj, void *_data)
1014 struct include_data *data = _data;
1015 int bitmap_pos;
1017 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1018 if (bitmap_pos < 0)
1019 return 1;
1020 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1021 bitmap_get(data->base, bitmap_pos)) {
1022 obj->flags |= SEEN;
1023 return 0;
1025 return 1;
1028 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1029 struct bitmap **base,
1030 struct commit *commit)
1032 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1034 if (!or_with)
1035 return 0;
1037 if (!*base)
1038 *base = ewah_to_bitmap(or_with);
1039 else
1040 bitmap_or_ewah(*base, or_with);
1042 return 1;
1045 static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1046 struct rev_info *revs,
1047 struct bitmap *base,
1048 struct bitmap *seen)
1050 struct include_data incdata;
1051 struct bitmap_show_data show_data;
1053 if (!base)
1054 base = bitmap_new();
1056 incdata.bitmap_git = bitmap_git;
1057 incdata.base = base;
1058 incdata.seen = seen;
1060 revs->include_check = should_include;
1061 revs->include_check_obj = should_include_obj;
1062 revs->include_check_data = &incdata;
1064 if (prepare_revision_walk(revs))
1065 die(_("revision walk setup failed"));
1067 show_data.bitmap_git = bitmap_git;
1068 show_data.base = base;
1070 traverse_commit_list(revs, show_commit, show_object, &show_data);
1072 revs->include_check = NULL;
1073 revs->include_check_obj = NULL;
1074 revs->include_check_data = NULL;
1076 return base;
1079 struct bitmap_boundary_cb {
1080 struct bitmap_index *bitmap_git;
1081 struct bitmap *base;
1083 struct object_array boundary;
1086 static void show_boundary_commit(struct commit *commit, void *_data)
1088 struct bitmap_boundary_cb *data = _data;
1090 if (commit->object.flags & BOUNDARY)
1091 add_object_array(&commit->object, "", &data->boundary);
1093 if (commit->object.flags & UNINTERESTING) {
1094 if (bitmap_walk_contains(data->bitmap_git, data->base,
1095 &commit->object.oid))
1096 return;
1098 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1102 static void show_boundary_object(struct object *object UNUSED,
1103 const char *name UNUSED,
1104 void *data UNUSED)
1106 BUG("should not be called");
1109 static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1110 struct rev_info *revs,
1111 struct object_list *roots)
1113 struct bitmap_boundary_cb cb;
1114 struct object_list *root;
1115 unsigned int i;
1116 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1117 int any_missing = 0;
1119 cb.bitmap_git = bitmap_git;
1120 cb.base = bitmap_new();
1121 object_array_init(&cb.boundary);
1123 revs->ignore_missing_links = 1;
1126 * OR in any existing reachability bitmaps among `roots` into
1127 * `cb.base`.
1129 for (root = roots; root; root = root->next) {
1130 struct object *object = root->item;
1131 if (object->type != OBJ_COMMIT ||
1132 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1133 continue;
1135 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1136 (struct commit *)object))
1137 continue;
1139 any_missing = 1;
1142 if (!any_missing)
1143 goto cleanup;
1145 tmp_blobs = revs->blob_objects;
1146 tmp_trees = revs->tree_objects;
1147 tmp_tags = revs->blob_objects;
1148 revs->blob_objects = 0;
1149 revs->tree_objects = 0;
1150 revs->tag_objects = 0;
1153 * We didn't have complete coverage of the roots. First setup a
1154 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1155 * between the tips and boundary, and (b) record the boundary.
1157 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1158 if (prepare_revision_walk(revs))
1159 die("revision walk setup failed");
1160 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1162 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1163 revs->boundary = 1;
1164 traverse_commit_list_filtered(revs,
1165 show_boundary_commit,
1166 show_boundary_object,
1167 &cb, NULL);
1168 revs->boundary = 0;
1169 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1171 revs->blob_objects = tmp_blobs;
1172 revs->tree_objects = tmp_trees;
1173 revs->tag_objects = tmp_tags;
1175 reset_revision_walk();
1176 clear_object_flags(UNINTERESTING);
1179 * Then add the boundary commit(s) as fill-in traversal tips.
1181 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1182 for (i = 0; i < cb.boundary.nr; i++) {
1183 struct object *obj = cb.boundary.objects[i].item;
1184 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1185 obj->flags |= SEEN;
1186 else
1187 add_pending_object(revs, obj, "");
1189 if (revs->pending.nr)
1190 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1191 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1193 cleanup:
1194 object_array_clear(&cb.boundary);
1195 revs->ignore_missing_links = 0;
1197 return cb.base;
1200 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1201 struct rev_info *revs,
1202 struct object_list *roots,
1203 struct bitmap *seen)
1205 struct bitmap *base = NULL;
1206 int needs_walk = 0;
1208 struct object_list *not_mapped = NULL;
1211 * Go through all the roots for the walk. The ones that have bitmaps
1212 * on the bitmap index will be `or`ed together to form an initial
1213 * global reachability analysis.
1215 * The ones without bitmaps in the index will be stored in the
1216 * `not_mapped_list` for further processing.
1218 while (roots) {
1219 struct object *object = roots->item;
1220 roots = roots->next;
1222 if (object->type == OBJ_COMMIT &&
1223 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1224 object->flags |= SEEN;
1225 continue;
1228 object_list_insert(object, &not_mapped);
1232 * Best case scenario: We found bitmaps for all the roots,
1233 * so the resulting `or` bitmap has the full reachability analysis
1235 if (!not_mapped)
1236 return base;
1238 roots = not_mapped;
1241 * Let's iterate through all the roots that don't have bitmaps to
1242 * check if we can determine them to be reachable from the existing
1243 * global bitmap.
1245 * If we cannot find them in the existing global bitmap, we'll need
1246 * to push them to an actual walk and run it until we can confirm
1247 * they are reachable
1249 while (roots) {
1250 struct object *object = roots->item;
1251 int pos;
1253 roots = roots->next;
1254 pos = bitmap_position(bitmap_git, &object->oid);
1256 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1257 object->flags &= ~UNINTERESTING;
1258 add_pending_object(revs, object, "");
1259 needs_walk = 1;
1260 } else {
1261 object->flags |= SEEN;
1265 if (needs_walk) {
1267 * This fill-in traversal may walk over some objects
1268 * again, since we have already traversed in order to
1269 * find the boundary.
1271 * But this extra walk should be extremely cheap, since
1272 * all commit objects are loaded into memory, and
1273 * because we skip walking to parents that are
1274 * UNINTERESTING, since it will be marked in the haves
1275 * bitmap already (or it has an on-disk bitmap, since
1276 * OR-ing it in covers all of its ancestors).
1278 base = fill_in_bitmap(bitmap_git, revs, base, seen);
1281 object_list_free(&not_mapped);
1283 return base;
1286 static void show_extended_objects(struct bitmap_index *bitmap_git,
1287 struct rev_info *revs,
1288 show_reachable_fn show_reach)
1290 struct bitmap *objects = bitmap_git->result;
1291 struct eindex *eindex = &bitmap_git->ext_index;
1292 uint32_t i;
1294 for (i = 0; i < eindex->count; ++i) {
1295 struct object *obj;
1297 if (!bitmap_get(objects, st_add(bitmap_num_objects(bitmap_git), i)))
1298 continue;
1300 obj = eindex->objects[i];
1301 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1302 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1303 (obj->type == OBJ_TAG && !revs->tag_objects))
1304 continue;
1306 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1310 static void init_type_iterator(struct ewah_iterator *it,
1311 struct bitmap_index *bitmap_git,
1312 enum object_type type)
1314 switch (type) {
1315 case OBJ_COMMIT:
1316 ewah_iterator_init(it, bitmap_git->commits);
1317 break;
1319 case OBJ_TREE:
1320 ewah_iterator_init(it, bitmap_git->trees);
1321 break;
1323 case OBJ_BLOB:
1324 ewah_iterator_init(it, bitmap_git->blobs);
1325 break;
1327 case OBJ_TAG:
1328 ewah_iterator_init(it, bitmap_git->tags);
1329 break;
1331 default:
1332 BUG("object type %d not stored by bitmap type index", type);
1333 break;
1337 static void show_objects_for_type(
1338 struct bitmap_index *bitmap_git,
1339 enum object_type object_type,
1340 show_reachable_fn show_reach)
1342 size_t i = 0;
1343 uint32_t offset;
1345 struct ewah_iterator it;
1346 eword_t filter;
1348 struct bitmap *objects = bitmap_git->result;
1350 init_type_iterator(&it, bitmap_git, object_type);
1352 for (i = 0; i < objects->word_alloc &&
1353 ewah_iterator_next(&filter, &it); i++) {
1354 eword_t word = objects->words[i] & filter;
1355 size_t pos = (i * BITS_IN_EWORD);
1357 if (!word)
1358 continue;
1360 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1361 struct packed_git *pack;
1362 struct object_id oid;
1363 uint32_t hash = 0, index_pos;
1364 off_t ofs;
1366 if ((word >> offset) == 0)
1367 break;
1369 offset += ewah_bit_ctz64(word >> offset);
1371 if (bitmap_is_midx(bitmap_git)) {
1372 struct multi_pack_index *m = bitmap_git->midx;
1373 uint32_t pack_id;
1375 index_pos = pack_pos_to_midx(m, pos + offset);
1376 ofs = nth_midxed_offset(m, index_pos);
1377 nth_midxed_object_oid(&oid, m, index_pos);
1379 pack_id = nth_midxed_pack_int_id(m, index_pos);
1380 pack = bitmap_git->midx->packs[pack_id];
1381 } else {
1382 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1383 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1384 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1386 pack = bitmap_git->pack;
1389 if (bitmap_git->hashes)
1390 hash = get_be32(bitmap_git->hashes + index_pos);
1392 show_reach(&oid, object_type, 0, hash, pack, ofs);
1397 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1398 struct object_list *roots)
1400 while (roots) {
1401 struct object *object = roots->item;
1402 roots = roots->next;
1404 if (bitmap_is_midx(bitmap_git)) {
1405 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1406 return 1;
1407 } else {
1408 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1409 return 1;
1413 return 0;
1416 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1417 struct object_list *tip_objects,
1418 enum object_type type)
1420 struct bitmap *result = bitmap_new();
1421 struct object_list *p;
1423 for (p = tip_objects; p; p = p->next) {
1424 int pos;
1426 if (p->item->type != type)
1427 continue;
1429 pos = bitmap_position(bitmap_git, &p->item->oid);
1430 if (pos < 0)
1431 continue;
1433 bitmap_set(result, pos);
1436 return result;
1439 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1440 struct object_list *tip_objects,
1441 struct bitmap *to_filter,
1442 enum object_type type)
1444 struct eindex *eindex = &bitmap_git->ext_index;
1445 struct bitmap *tips;
1446 struct ewah_iterator it;
1447 eword_t mask;
1448 uint32_t i;
1451 * The non-bitmap version of this filter never removes
1452 * objects which the other side specifically asked for,
1453 * so we must match that behavior.
1455 tips = find_tip_objects(bitmap_git, tip_objects, type);
1458 * We can use the type-level bitmap for 'type' to work in whole
1459 * words for the objects that are actually in the bitmapped
1460 * packfile.
1462 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1463 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1464 i++) {
1465 if (i < tips->word_alloc)
1466 mask &= ~tips->words[i];
1467 to_filter->words[i] &= ~mask;
1471 * Clear any objects that weren't in the packfile (and so would
1472 * not have been caught by the loop above. We'll have to check
1473 * them individually.
1475 for (i = 0; i < eindex->count; i++) {
1476 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1477 if (eindex->objects[i]->type == type &&
1478 bitmap_get(to_filter, pos) &&
1479 !bitmap_get(tips, pos))
1480 bitmap_unset(to_filter, pos);
1483 bitmap_free(tips);
1486 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1487 struct object_list *tip_objects,
1488 struct bitmap *to_filter)
1490 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1491 OBJ_BLOB);
1494 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1495 uint32_t pos)
1497 unsigned long size;
1498 struct object_info oi = OBJECT_INFO_INIT;
1500 oi.sizep = &size;
1502 if (pos < bitmap_num_objects(bitmap_git)) {
1503 struct packed_git *pack;
1504 off_t ofs;
1506 if (bitmap_is_midx(bitmap_git)) {
1507 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1508 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1510 pack = bitmap_git->midx->packs[pack_id];
1511 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1512 } else {
1513 pack = bitmap_git->pack;
1514 ofs = pack_pos_to_offset(pack, pos);
1517 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1518 struct object_id oid;
1519 nth_bitmap_object_oid(bitmap_git, &oid,
1520 pack_pos_to_index(pack, pos));
1521 die(_("unable to get size of %s"), oid_to_hex(&oid));
1523 } else {
1524 struct eindex *eindex = &bitmap_git->ext_index;
1525 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1526 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1527 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1530 return size;
1533 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1534 struct object_list *tip_objects,
1535 struct bitmap *to_filter,
1536 unsigned long limit)
1538 struct eindex *eindex = &bitmap_git->ext_index;
1539 struct bitmap *tips;
1540 struct ewah_iterator it;
1541 eword_t mask;
1542 uint32_t i;
1544 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1546 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1547 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1548 i++) {
1549 eword_t word = to_filter->words[i] & mask;
1550 unsigned offset;
1552 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1553 uint32_t pos;
1555 if ((word >> offset) == 0)
1556 break;
1557 offset += ewah_bit_ctz64(word >> offset);
1558 pos = i * BITS_IN_EWORD + offset;
1560 if (!bitmap_get(tips, pos) &&
1561 get_size_by_pos(bitmap_git, pos) >= limit)
1562 bitmap_unset(to_filter, pos);
1566 for (i = 0; i < eindex->count; i++) {
1567 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1568 if (eindex->objects[i]->type == OBJ_BLOB &&
1569 bitmap_get(to_filter, pos) &&
1570 !bitmap_get(tips, pos) &&
1571 get_size_by_pos(bitmap_git, pos) >= limit)
1572 bitmap_unset(to_filter, pos);
1575 bitmap_free(tips);
1578 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1579 struct object_list *tip_objects,
1580 struct bitmap *to_filter,
1581 unsigned long limit)
1583 if (limit)
1584 BUG("filter_bitmap_tree_depth given non-zero limit");
1586 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1587 OBJ_TREE);
1588 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1589 OBJ_BLOB);
1592 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1593 struct object_list *tip_objects,
1594 struct bitmap *to_filter,
1595 enum object_type object_type)
1597 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1598 BUG("filter_bitmap_object_type given invalid object");
1600 if (object_type != OBJ_TAG)
1601 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1602 if (object_type != OBJ_COMMIT)
1603 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1604 if (object_type != OBJ_TREE)
1605 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1606 if (object_type != OBJ_BLOB)
1607 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1610 static int filter_bitmap(struct bitmap_index *bitmap_git,
1611 struct object_list *tip_objects,
1612 struct bitmap *to_filter,
1613 struct list_objects_filter_options *filter)
1615 if (!filter || filter->choice == LOFC_DISABLED)
1616 return 0;
1618 if (filter->choice == LOFC_BLOB_NONE) {
1619 if (bitmap_git)
1620 filter_bitmap_blob_none(bitmap_git, tip_objects,
1621 to_filter);
1622 return 0;
1625 if (filter->choice == LOFC_BLOB_LIMIT) {
1626 if (bitmap_git)
1627 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1628 to_filter,
1629 filter->blob_limit_value);
1630 return 0;
1633 if (filter->choice == LOFC_TREE_DEPTH &&
1634 filter->tree_exclude_depth == 0) {
1635 if (bitmap_git)
1636 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1637 to_filter,
1638 filter->tree_exclude_depth);
1639 return 0;
1642 if (filter->choice == LOFC_OBJECT_TYPE) {
1643 if (bitmap_git)
1644 filter_bitmap_object_type(bitmap_git, tip_objects,
1645 to_filter,
1646 filter->object_type);
1647 return 0;
1650 if (filter->choice == LOFC_COMBINE) {
1651 int i;
1652 for (i = 0; i < filter->sub_nr; i++) {
1653 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1654 &filter->sub[i]) < 0)
1655 return -1;
1657 return 0;
1660 /* filter choice not handled */
1661 return -1;
1664 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1666 return !filter_bitmap(NULL, NULL, NULL, filter);
1670 static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
1671 struct bitmap *result)
1673 struct eindex *eindex = &bitmap_git->ext_index;
1674 uint32_t objects_nr;
1675 size_t i, pos;
1677 objects_nr = bitmap_num_objects(bitmap_git);
1678 pos = objects_nr / BITS_IN_EWORD;
1680 if (pos > result->word_alloc)
1681 pos = result->word_alloc;
1683 memset(result->words, 0x00, sizeof(eword_t) * pos);
1684 for (i = pos * BITS_IN_EWORD; i < objects_nr; i++)
1685 bitmap_unset(result, i);
1687 for (i = 0; i < eindex->count; ++i) {
1688 if (has_object_pack(&eindex->objects[i]->oid))
1689 bitmap_unset(result, objects_nr + i);
1693 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1694 int filter_provided_objects)
1696 unsigned int i;
1697 int use_boundary_traversal;
1699 struct object_list *wants = NULL;
1700 struct object_list *haves = NULL;
1702 struct bitmap *wants_bitmap = NULL;
1703 struct bitmap *haves_bitmap = NULL;
1705 struct bitmap_index *bitmap_git;
1708 * We can't do pathspec limiting with bitmaps, because we don't know
1709 * which commits are associated with which object changes (let alone
1710 * even which objects are associated with which paths).
1712 if (revs->prune)
1713 return NULL;
1715 if (!can_filter_bitmap(&revs->filter))
1716 return NULL;
1718 /* try to open a bitmapped pack, but don't parse it yet
1719 * because we may not need to use it */
1720 CALLOC_ARRAY(bitmap_git, 1);
1721 if (open_bitmap(revs->repo, bitmap_git) < 0)
1722 goto cleanup;
1724 for (i = 0; i < revs->pending.nr; ++i) {
1725 struct object *object = revs->pending.objects[i].item;
1727 if (object->type == OBJ_NONE)
1728 parse_object_or_die(&object->oid, NULL);
1730 while (object->type == OBJ_TAG) {
1731 struct tag *tag = (struct tag *) object;
1733 if (object->flags & UNINTERESTING)
1734 object_list_insert(object, &haves);
1735 else
1736 object_list_insert(object, &wants);
1738 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1739 object->flags |= (tag->object.flags & UNINTERESTING);
1742 if (object->flags & UNINTERESTING)
1743 object_list_insert(object, &haves);
1744 else
1745 object_list_insert(object, &wants);
1748 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1749 if (use_boundary_traversal < 0) {
1750 prepare_repo_settings(revs->repo);
1751 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1754 if (!use_boundary_traversal) {
1756 * if we have a HAVES list, but none of those haves is contained
1757 * in the packfile that has a bitmap, we don't have anything to
1758 * optimize here
1760 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1761 goto cleanup;
1764 /* if we don't want anything, we're done here */
1765 if (!wants)
1766 goto cleanup;
1769 * now we're going to use bitmaps, so load the actual bitmap entries
1770 * from disk. this is the point of no return; after this the rev_list
1771 * becomes invalidated and we must perform the revwalk through bitmaps
1773 if (load_bitmap(revs->repo, bitmap_git) < 0)
1774 goto cleanup;
1776 if (!use_boundary_traversal)
1777 object_array_clear(&revs->pending);
1779 if (haves) {
1780 if (use_boundary_traversal) {
1781 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1782 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1783 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1784 } else {
1785 trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1786 revs->ignore_missing_links = 1;
1787 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1788 reset_revision_walk();
1789 revs->ignore_missing_links = 0;
1790 trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1793 if (!haves_bitmap)
1794 BUG("failed to perform bitmap walk");
1797 if (use_boundary_traversal) {
1798 object_array_clear(&revs->pending);
1799 reset_revision_walk();
1802 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1804 if (!wants_bitmap)
1805 BUG("failed to perform bitmap walk");
1807 if (haves_bitmap)
1808 bitmap_and_not(wants_bitmap, haves_bitmap);
1810 filter_bitmap(bitmap_git,
1811 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1812 wants_bitmap,
1813 &revs->filter);
1815 if (revs->unpacked)
1816 filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap);
1818 bitmap_git->result = wants_bitmap;
1819 bitmap_git->haves = haves_bitmap;
1821 object_list_free(&wants);
1822 object_list_free(&haves);
1824 return bitmap_git;
1826 cleanup:
1827 free_bitmap_index(bitmap_git);
1828 object_list_free(&wants);
1829 object_list_free(&haves);
1830 return NULL;
1834 * -1 means "stop trying further objects"; 0 means we may or may not have
1835 * reused, but you can keep feeding bits.
1837 static int try_partial_reuse(struct bitmap_index *bitmap_git,
1838 struct bitmapped_pack *pack,
1839 size_t bitmap_pos,
1840 uint32_t pack_pos,
1841 struct bitmap *reuse,
1842 struct pack_window **w_curs)
1844 off_t offset, delta_obj_offset;
1845 enum object_type type;
1846 unsigned long size;
1848 if (pack_pos >= pack->p->num_objects)
1849 return -1; /* not actually in the pack */
1851 offset = delta_obj_offset = pack_pos_to_offset(pack->p, pack_pos);
1852 type = unpack_object_header(pack->p, w_curs, &offset, &size);
1853 if (type < 0)
1854 return -1; /* broken packfile, punt */
1856 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1857 off_t base_offset;
1858 uint32_t base_pos;
1859 uint32_t base_bitmap_pos;
1862 * Find the position of the base object so we can look it up
1863 * in our bitmaps. If we can't come up with an offset, or if
1864 * that offset is not in the revidx, the pack is corrupt.
1865 * There's nothing we can do, so just punt on this object,
1866 * and the normal slow path will complain about it in
1867 * more detail.
1869 base_offset = get_delta_base(pack->p, w_curs, &offset, type,
1870 delta_obj_offset);
1871 if (!base_offset)
1872 return 0;
1874 offset_to_pack_pos(pack->p, base_offset, &base_pos);
1876 if (bitmap_is_midx(bitmap_git)) {
1878 * Cross-pack deltas are rejected for now, but could
1879 * theoretically be supported in the future.
1881 * We would need to ensure that we're sending both
1882 * halves of the delta/base pair, regardless of whether
1883 * or not the two cross a pack boundary. If they do,
1884 * then we must convert the delta to an REF_DELTA to
1885 * refer back to the base in the other pack.
1886 * */
1887 if (midx_pair_to_pack_pos(bitmap_git->midx,
1888 pack->pack_int_id,
1889 base_offset,
1890 &base_bitmap_pos) < 0) {
1891 return 0;
1893 } else {
1894 if (offset_to_pack_pos(pack->p, base_offset,
1895 &base_pos) < 0)
1896 return 0;
1898 * We assume delta dependencies always point backwards.
1899 * This lets us do a single pass, and is basically
1900 * always true due to the way OFS_DELTAs work. You would
1901 * not typically find REF_DELTA in a bitmapped pack,
1902 * since we only bitmap packs we write fresh, and
1903 * OFS_DELTA is the default). But let's double check to
1904 * make sure the pack wasn't written with odd
1905 * parameters.
1907 if (base_pos >= pack_pos)
1908 return 0;
1909 base_bitmap_pos = pack->bitmap_pos + base_pos;
1913 * And finally, if we're not sending the base as part of our
1914 * reuse chunk, then don't send this object either. The base
1915 * would come after us, along with other objects not
1916 * necessarily in the pack, which means we'd need to convert
1917 * to REF_DELTA on the fly. Better to just let the normal
1918 * object_entry code path handle it.
1920 if (!bitmap_get(reuse, base_bitmap_pos))
1921 return 0;
1925 * If we got here, then the object is OK to reuse. Mark it.
1927 bitmap_set(reuse, bitmap_pos);
1928 return 0;
1931 static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
1932 struct bitmapped_pack *pack,
1933 struct bitmap *reuse)
1935 struct bitmap *result = bitmap_git->result;
1936 struct pack_window *w_curs = NULL;
1937 size_t pos = pack->bitmap_pos / BITS_IN_EWORD;
1939 if (!pack->bitmap_pos) {
1941 * If we're processing the first (in the case of a MIDX, the
1942 * preferred pack) or the only (in the case of single-pack
1943 * bitmaps) pack, then we can reuse whole words at a time.
1945 * This is because we know that any deltas in this range *must*
1946 * have their bases chosen from the same pack, since:
1948 * - In the single pack case, there is no other pack to choose
1949 * them from.
1951 * - In the MIDX case, the first pack is the preferred pack, so
1952 * all ties are broken in favor of that pack (i.e. the one
1953 * we're currently processing). So any duplicate bases will be
1954 * resolved in favor of the pack we're processing.
1956 while (pos < result->word_alloc &&
1957 pos < pack->bitmap_nr / BITS_IN_EWORD &&
1958 result->words[pos] == (eword_t)~0)
1959 pos++;
1960 memset(reuse->words, 0xFF, pos * sizeof(eword_t));
1963 for (; pos < result->word_alloc; pos++) {
1964 eword_t word = result->words[pos];
1965 size_t offset;
1967 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1968 size_t bit_pos;
1969 uint32_t pack_pos;
1971 if (word >> offset == 0)
1972 break;
1974 offset += ewah_bit_ctz64(word >> offset);
1976 bit_pos = pos * BITS_IN_EWORD + offset;
1977 if (bit_pos < pack->bitmap_pos)
1978 continue;
1979 if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
1980 goto done;
1982 if (bitmap_is_midx(bitmap_git)) {
1983 uint32_t midx_pos;
1984 off_t ofs;
1986 midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
1987 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1989 if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
1990 BUG("could not find object in pack %s "
1991 "at offset %"PRIuMAX" in MIDX",
1992 pack_basename(pack->p), (uintmax_t)ofs);
1993 } else {
1994 pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
1995 if (pack_pos >= pack->p->num_objects)
1996 BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
1997 pack_basename(pack->p), (uintmax_t)pack_pos,
1998 pack->p->num_objects);
2001 if (try_partial_reuse(bitmap_git, pack, bit_pos,
2002 pack_pos, reuse, &w_curs) < 0) {
2004 * try_partial_reuse indicated we couldn't reuse
2005 * any bits, so there is no point in trying more
2006 * bits in the current word, or any other words
2007 * in result.
2009 * Jump out of both loops to avoid future
2010 * unnecessary calls to try_partial_reuse.
2012 goto done;
2017 done:
2018 unuse_pack(&w_curs);
2021 static int bitmapped_pack_cmp(const void *va, const void *vb)
2023 const struct bitmapped_pack *a = va;
2024 const struct bitmapped_pack *b = vb;
2026 if (a->bitmap_pos < b->bitmap_pos)
2027 return -1;
2028 if (a->bitmap_pos > b->bitmap_pos)
2029 return 1;
2030 return 0;
2033 void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2034 struct bitmapped_pack **packs_out,
2035 size_t *packs_nr_out,
2036 struct bitmap **reuse_out,
2037 int multi_pack_reuse)
2039 struct repository *r = the_repository;
2040 struct bitmapped_pack *packs = NULL;
2041 struct bitmap *result = bitmap_git->result;
2042 struct bitmap *reuse;
2043 size_t i;
2044 size_t packs_nr = 0, packs_alloc = 0;
2045 size_t word_alloc;
2046 uint32_t objects_nr = 0;
2048 assert(result);
2050 load_reverse_index(r, bitmap_git);
2052 if (!bitmap_is_midx(bitmap_git) || !bitmap_git->midx->chunk_bitmapped_packs)
2053 multi_pack_reuse = 0;
2055 if (multi_pack_reuse) {
2056 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
2057 struct bitmapped_pack pack;
2058 if (nth_bitmapped_pack(r, bitmap_git->midx, &pack, i) < 0) {
2059 warning(_("unable to load pack: '%s', disabling pack-reuse"),
2060 bitmap_git->midx->pack_names[i]);
2061 free(packs);
2062 return;
2065 if (!pack.bitmap_nr)
2066 continue;
2068 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2069 memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2071 objects_nr += pack.p->num_objects;
2074 QSORT(packs, packs_nr, bitmapped_pack_cmp);
2075 } else {
2076 struct packed_git *pack;
2078 if (bitmap_is_midx(bitmap_git)) {
2079 uint32_t preferred_pack_pos;
2081 if (midx_preferred_pack(bitmap_git->midx, &preferred_pack_pos) < 0) {
2082 warning(_("unable to compute preferred pack, disabling pack-reuse"));
2083 return;
2086 pack = bitmap_git->midx->packs[preferred_pack_pos];
2087 } else {
2088 pack = bitmap_git->pack;
2091 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2092 packs[packs_nr].p = pack;
2093 packs[packs_nr].bitmap_nr = pack->num_objects;
2094 packs[packs_nr].bitmap_pos = 0;
2096 objects_nr = packs[packs_nr++].bitmap_nr;
2099 word_alloc = objects_nr / BITS_IN_EWORD;
2100 if (objects_nr % BITS_IN_EWORD)
2101 word_alloc++;
2102 reuse = bitmap_word_alloc(word_alloc);
2104 for (i = 0; i < packs_nr; i++)
2105 reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse);
2107 if (bitmap_is_empty(reuse)) {
2108 free(packs);
2109 bitmap_free(reuse);
2110 return;
2114 * Drop any reused objects from the result, since they will not
2115 * need to be handled separately.
2117 bitmap_and_not(result, reuse);
2118 *packs_out = packs;
2119 *packs_nr_out = packs_nr;
2120 *reuse_out = reuse;
2123 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2124 struct bitmap *bitmap, const struct object_id *oid)
2126 int idx;
2128 if (!bitmap)
2129 return 0;
2131 idx = bitmap_position(bitmap_git, oid);
2132 return idx >= 0 && bitmap_get(bitmap, idx);
2135 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2136 struct rev_info *revs,
2137 show_reachable_fn show_reachable)
2139 assert(bitmap_git->result);
2141 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2142 if (revs->tree_objects)
2143 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2144 if (revs->blob_objects)
2145 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2146 if (revs->tag_objects)
2147 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2149 show_extended_objects(bitmap_git, revs, show_reachable);
2152 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2153 enum object_type type)
2155 struct bitmap *objects = bitmap_git->result;
2156 struct eindex *eindex = &bitmap_git->ext_index;
2158 uint32_t i = 0, count = 0;
2159 struct ewah_iterator it;
2160 eword_t filter;
2162 init_type_iterator(&it, bitmap_git, type);
2164 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2165 eword_t word = objects->words[i++] & filter;
2166 count += ewah_bit_popcount64(word);
2169 for (i = 0; i < eindex->count; ++i) {
2170 if (eindex->objects[i]->type == type &&
2171 bitmap_get(objects,
2172 st_add(bitmap_num_objects(bitmap_git), i)))
2173 count++;
2176 return count;
2179 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2180 uint32_t *commits, uint32_t *trees,
2181 uint32_t *blobs, uint32_t *tags)
2183 assert(bitmap_git->result);
2185 if (commits)
2186 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2188 if (trees)
2189 *trees = count_object_type(bitmap_git, OBJ_TREE);
2191 if (blobs)
2192 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2194 if (tags)
2195 *tags = count_object_type(bitmap_git, OBJ_TAG);
2198 struct bitmap_test_data {
2199 struct bitmap_index *bitmap_git;
2200 struct bitmap *base;
2201 struct bitmap *commits;
2202 struct bitmap *trees;
2203 struct bitmap *blobs;
2204 struct bitmap *tags;
2205 struct progress *prg;
2206 size_t seen;
2209 static void test_bitmap_type(struct bitmap_test_data *tdata,
2210 struct object *obj, int pos)
2212 enum object_type bitmap_type = OBJ_NONE;
2213 int bitmaps_nr = 0;
2215 if (bitmap_get(tdata->commits, pos)) {
2216 bitmap_type = OBJ_COMMIT;
2217 bitmaps_nr++;
2219 if (bitmap_get(tdata->trees, pos)) {
2220 bitmap_type = OBJ_TREE;
2221 bitmaps_nr++;
2223 if (bitmap_get(tdata->blobs, pos)) {
2224 bitmap_type = OBJ_BLOB;
2225 bitmaps_nr++;
2227 if (bitmap_get(tdata->tags, pos)) {
2228 bitmap_type = OBJ_TAG;
2229 bitmaps_nr++;
2232 if (bitmap_type == OBJ_NONE)
2233 die(_("object '%s' not found in type bitmaps"),
2234 oid_to_hex(&obj->oid));
2236 if (bitmaps_nr > 1)
2237 die(_("object '%s' does not have a unique type"),
2238 oid_to_hex(&obj->oid));
2240 if (bitmap_type != obj->type)
2241 die(_("object '%s': real type '%s', expected: '%s'"),
2242 oid_to_hex(&obj->oid),
2243 type_name(obj->type),
2244 type_name(bitmap_type));
2247 static void test_show_object(struct object *object,
2248 const char *name UNUSED,
2249 void *data)
2251 struct bitmap_test_data *tdata = data;
2252 int bitmap_pos;
2254 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2255 if (bitmap_pos < 0)
2256 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2257 test_bitmap_type(tdata, object, bitmap_pos);
2259 bitmap_set(tdata->base, bitmap_pos);
2260 display_progress(tdata->prg, ++tdata->seen);
2263 static void test_show_commit(struct commit *commit, void *data)
2265 struct bitmap_test_data *tdata = data;
2266 int bitmap_pos;
2268 bitmap_pos = bitmap_position(tdata->bitmap_git,
2269 &commit->object.oid);
2270 if (bitmap_pos < 0)
2271 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2272 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2274 bitmap_set(tdata->base, bitmap_pos);
2275 display_progress(tdata->prg, ++tdata->seen);
2278 void test_bitmap_walk(struct rev_info *revs)
2280 struct object *root;
2281 struct bitmap *result = NULL;
2282 size_t result_popcnt;
2283 struct bitmap_test_data tdata;
2284 struct bitmap_index *bitmap_git;
2285 struct ewah_bitmap *bm;
2287 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2288 die(_("failed to load bitmap indexes"));
2290 if (revs->pending.nr != 1)
2291 die(_("you must specify exactly one commit to test"));
2293 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2294 bitmap_git->version,
2295 bitmap_git->entry_count,
2296 bitmap_git->table_lookup ? "" : " loaded");
2298 root = revs->pending.objects[0].item;
2299 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2301 if (bm) {
2302 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2303 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2305 result = ewah_to_bitmap(bm);
2308 if (!result)
2309 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2311 revs->tag_objects = 1;
2312 revs->tree_objects = 1;
2313 revs->blob_objects = 1;
2315 result_popcnt = bitmap_popcount(result);
2317 if (prepare_revision_walk(revs))
2318 die(_("revision walk setup failed"));
2320 tdata.bitmap_git = bitmap_git;
2321 tdata.base = bitmap_new();
2322 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2323 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2324 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2325 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2326 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2327 tdata.seen = 0;
2329 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2331 stop_progress(&tdata.prg);
2333 if (bitmap_equals(result, tdata.base))
2334 fprintf_ln(stderr, "OK!");
2335 else
2336 die(_("mismatch in bitmap results"));
2338 bitmap_free(result);
2339 bitmap_free(tdata.base);
2340 bitmap_free(tdata.commits);
2341 bitmap_free(tdata.trees);
2342 bitmap_free(tdata.blobs);
2343 bitmap_free(tdata.tags);
2344 free_bitmap_index(bitmap_git);
2347 int test_bitmap_commits(struct repository *r)
2349 struct object_id oid;
2350 MAYBE_UNUSED void *value;
2351 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2353 if (!bitmap_git)
2354 die(_("failed to load bitmap indexes"));
2357 * As this function is only used to print bitmap selected
2358 * commits, we don't have to read the commit table.
2360 if (bitmap_git->table_lookup) {
2361 if (load_bitmap_entries_v1(bitmap_git) < 0)
2362 die(_("failed to load bitmap indexes"));
2365 kh_foreach(bitmap_git->bitmaps, oid, value, {
2366 printf_ln("%s", oid_to_hex(&oid));
2369 free_bitmap_index(bitmap_git);
2371 return 0;
2374 int test_bitmap_hashes(struct repository *r)
2376 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2377 struct object_id oid;
2378 uint32_t i, index_pos;
2380 if (!bitmap_git || !bitmap_git->hashes)
2381 goto cleanup;
2383 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2384 if (bitmap_is_midx(bitmap_git))
2385 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2386 else
2387 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2389 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2391 printf_ln("%s %"PRIu32"",
2392 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2395 cleanup:
2396 free_bitmap_index(bitmap_git);
2398 return 0;
2401 int rebuild_bitmap(const uint32_t *reposition,
2402 struct ewah_bitmap *source,
2403 struct bitmap *dest)
2405 uint32_t pos = 0;
2406 struct ewah_iterator it;
2407 eword_t word;
2409 ewah_iterator_init(&it, source);
2411 while (ewah_iterator_next(&word, &it)) {
2412 uint32_t offset, bit_pos;
2414 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2415 if ((word >> offset) == 0)
2416 break;
2418 offset += ewah_bit_ctz64(word >> offset);
2420 bit_pos = reposition[pos + offset];
2421 if (bit_pos > 0)
2422 bitmap_set(dest, bit_pos - 1);
2423 else /* can't reuse, we don't have the object */
2424 return -1;
2427 pos += BITS_IN_EWORD;
2429 return 0;
2432 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2433 struct packing_data *mapping)
2435 struct repository *r = the_repository;
2436 uint32_t i, num_objects;
2437 uint32_t *reposition;
2439 if (!bitmap_is_midx(bitmap_git))
2440 load_reverse_index(r, bitmap_git);
2441 else if (load_midx_revindex(bitmap_git->midx))
2442 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2443 "extension");
2445 num_objects = bitmap_num_objects(bitmap_git);
2446 CALLOC_ARRAY(reposition, num_objects);
2448 for (i = 0; i < num_objects; ++i) {
2449 struct object_id oid;
2450 struct object_entry *oe;
2451 uint32_t index_pos;
2453 if (bitmap_is_midx(bitmap_git))
2454 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2455 else
2456 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2457 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2458 oe = packlist_find(mapping, &oid);
2460 if (oe) {
2461 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2462 if (bitmap_git->hashes && !oe->hash)
2463 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2467 return reposition;
2470 void free_bitmap_index(struct bitmap_index *b)
2472 if (!b)
2473 return;
2475 if (b->map)
2476 munmap(b->map, b->map_size);
2477 ewah_pool_free(b->commits);
2478 ewah_pool_free(b->trees);
2479 ewah_pool_free(b->blobs);
2480 ewah_pool_free(b->tags);
2481 if (b->bitmaps) {
2482 struct stored_bitmap *sb;
2483 kh_foreach_value(b->bitmaps, sb, {
2484 ewah_pool_free(sb->root);
2485 free(sb);
2488 kh_destroy_oid_map(b->bitmaps);
2489 free(b->ext_index.objects);
2490 free(b->ext_index.hashes);
2491 kh_destroy_oid_pos(b->ext_index.positions);
2492 bitmap_free(b->result);
2493 bitmap_free(b->haves);
2494 if (bitmap_is_midx(b)) {
2496 * Multi-pack bitmaps need to have resources associated with
2497 * their on-disk reverse indexes unmapped so that stale .rev and
2498 * .bitmap files can be removed.
2500 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2501 * written in the same 'git multi-pack-index write --bitmap'
2502 * process. Close resources so they can be removed safely on
2503 * platforms like Windows.
2505 close_midx_revindex(b->midx);
2507 free(b);
2510 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2511 const struct object_id *oid)
2513 return bitmap_git &&
2514 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2517 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2518 enum object_type object_type)
2520 struct bitmap *result = bitmap_git->result;
2521 off_t total = 0;
2522 struct ewah_iterator it;
2523 eword_t filter;
2524 size_t i;
2526 init_type_iterator(&it, bitmap_git, object_type);
2527 for (i = 0; i < result->word_alloc &&
2528 ewah_iterator_next(&filter, &it); i++) {
2529 eword_t word = result->words[i] & filter;
2530 size_t base = (i * BITS_IN_EWORD);
2531 unsigned offset;
2533 if (!word)
2534 continue;
2536 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2537 if ((word >> offset) == 0)
2538 break;
2540 offset += ewah_bit_ctz64(word >> offset);
2542 if (bitmap_is_midx(bitmap_git)) {
2543 uint32_t pack_pos;
2544 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2545 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2547 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2548 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2550 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2551 struct object_id oid;
2552 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2554 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2555 oid_to_hex(&oid),
2556 pack->pack_name,
2557 (uintmax_t)offset);
2560 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2561 } else {
2562 size_t pos = base + offset;
2563 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2564 pack_pos_to_offset(bitmap_git->pack, pos);
2569 return total;
2572 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2574 struct bitmap *result = bitmap_git->result;
2575 struct eindex *eindex = &bitmap_git->ext_index;
2576 off_t total = 0;
2577 struct object_info oi = OBJECT_INFO_INIT;
2578 off_t object_size;
2579 size_t i;
2581 oi.disk_sizep = &object_size;
2583 for (i = 0; i < eindex->count; i++) {
2584 struct object *obj = eindex->objects[i];
2586 if (!bitmap_get(result,
2587 st_add(bitmap_num_objects(bitmap_git), i)))
2588 continue;
2590 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2591 die(_("unable to get disk usage of '%s'"),
2592 oid_to_hex(&obj->oid));
2594 total += object_size;
2596 return total;
2599 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2600 struct rev_info *revs)
2602 off_t total = 0;
2604 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2605 if (revs->tree_objects)
2606 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2607 if (revs->blob_objects)
2608 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2609 if (revs->tag_objects)
2610 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2612 total += get_disk_usage_for_extended(bitmap_git);
2614 return total;
2617 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2619 return !!bitmap_git->midx;
2622 const struct string_list *bitmap_preferred_tips(struct repository *r)
2624 const struct string_list *dest;
2626 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2627 return dest;
2628 return NULL;
2631 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2633 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2634 struct string_list_item *item;
2636 if (!preferred_tips)
2637 return 0;
2639 for_each_string_list_item(item, preferred_tips) {
2640 if (starts_with(refname, item->string))
2641 return 1;
2644 return 0;
2647 static int verify_bitmap_file(const char *name)
2649 struct stat st;
2650 unsigned char *data;
2651 int fd = git_open(name);
2652 int res = 0;
2654 /* It is OK to not have the file. */
2655 if (fd < 0 || fstat(fd, &st)) {
2656 if (fd >= 0)
2657 close(fd);
2658 return 0;
2661 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2662 close(fd);
2663 if (!hashfile_checksum_valid(data, st.st_size))
2664 res = error(_("bitmap file '%s' has invalid checksum"),
2665 name);
2667 munmap(data, st.st_size);
2668 return res;
2671 int verify_bitmap_files(struct repository *r)
2673 int res = 0;
2675 for (struct multi_pack_index *m = get_multi_pack_index(r);
2676 m; m = m->next) {
2677 char *midx_bitmap_name = midx_bitmap_filename(m);
2678 res |= verify_bitmap_file(midx_bitmap_name);
2679 free(midx_bitmap_name);
2682 for (struct packed_git *p = get_all_packs(r);
2683 p; p = p->next) {
2684 char *pack_bitmap_name = pack_bitmap_filename(p);
2685 res |= verify_bitmap_file(pack_bitmap_name);
2686 free(pack_bitmap_name);
2689 return res;