dir.h: move DTYPE defines from cache.h
[git.git] / pack-bitmap.c
blob48fc2ec76de84742cc6330e574e5cda8b6bf5f74
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "commit.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "strbuf.h"
7 #include "tag.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "progress.h"
11 #include "list-objects.h"
12 #include "pack.h"
13 #include "pack-bitmap.h"
14 #include "pack-revindex.h"
15 #include "pack-objects.h"
16 #include "packfile.h"
17 #include "repository.h"
18 #include "trace2.h"
19 #include "object-file.h"
20 #include "object-store.h"
21 #include "list-objects-filter-options.h"
22 #include "midx.h"
23 #include "config.h"
26 * An entry on the bitmap index, representing the bitmap for a given
27 * commit.
29 struct stored_bitmap {
30 struct object_id oid;
31 struct ewah_bitmap *root;
32 struct stored_bitmap *xor;
33 int flags;
37 * The active bitmap index for a repository. By design, repositories only have
38 * a single bitmap index available (the index for the biggest packfile in
39 * the repository), since bitmap indexes need full closure.
41 * If there is more than one bitmap index available (e.g. because of alternates),
42 * the active bitmap index is the largest one.
44 struct bitmap_index {
46 * The pack or multi-pack index (MIDX) that this bitmap index belongs
47 * to.
49 * Exactly one of these must be non-NULL; this specifies the object
50 * order used to interpret this bitmap.
52 struct packed_git *pack;
53 struct multi_pack_index *midx;
56 * Mark the first `reuse_objects` in the packfile as reused:
57 * they will be sent as-is without using them for repacking
58 * calculations
60 uint32_t reuse_objects;
62 /* mmapped buffer of the whole bitmap index */
63 unsigned char *map;
64 size_t map_size; /* size of the mmaped buffer */
65 size_t map_pos; /* current position when loading the index */
68 * Type indexes.
70 * Each bitmap marks which objects in the packfile are of the given
71 * type. This provides type information when yielding the objects from
72 * the packfile during a walk, which allows for better delta bases.
74 struct ewah_bitmap *commits;
75 struct ewah_bitmap *trees;
76 struct ewah_bitmap *blobs;
77 struct ewah_bitmap *tags;
79 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
80 kh_oid_map_t *bitmaps;
82 /* Number of bitmapped commits */
83 uint32_t entry_count;
85 /* If not NULL, this is a name-hash cache pointing into map. */
86 uint32_t *hashes;
88 /* The checksum of the packfile or MIDX; points into map. */
89 const unsigned char *checksum;
92 * If not NULL, this point into the commit table extension
93 * (within the memory mapped region `map`).
95 unsigned char *table_lookup;
98 * Extended index.
100 * When trying to perform bitmap operations with objects that are not
101 * packed in `pack`, these objects are added to this "fake index" and
102 * are assumed to appear at the end of the packfile for all operations
104 struct eindex {
105 struct object **objects;
106 uint32_t *hashes;
107 uint32_t count, alloc;
108 kh_oid_pos_t *positions;
109 } ext_index;
111 /* Bitmap result of the last performed walk */
112 struct bitmap *result;
114 /* "have" bitmap from the last performed walk */
115 struct bitmap *haves;
117 /* Version of the bitmap index */
118 unsigned int version;
121 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
123 struct ewah_bitmap *parent;
124 struct ewah_bitmap *composed;
126 if (!st->xor)
127 return st->root;
129 composed = ewah_pool_new();
130 parent = lookup_stored_bitmap(st->xor);
131 ewah_xor(st->root, parent, composed);
133 ewah_pool_free(st->root);
134 st->root = composed;
135 st->xor = NULL;
137 return composed;
141 * Read a bitmap from the current read position on the mmaped
142 * index, and increase the read position accordingly
144 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
146 struct ewah_bitmap *b = ewah_pool_new();
148 ssize_t bitmap_size = ewah_read_mmap(b,
149 index->map + index->map_pos,
150 index->map_size - index->map_pos);
152 if (bitmap_size < 0) {
153 error(_("failed to load bitmap index (corrupted?)"));
154 ewah_pool_free(b);
155 return NULL;
158 index->map_pos += bitmap_size;
159 return b;
162 static uint32_t bitmap_num_objects(struct bitmap_index *index)
164 if (index->midx)
165 return index->midx->num_objects;
166 return index->pack->num_objects;
169 static int load_bitmap_header(struct bitmap_index *index)
171 struct bitmap_disk_header *header = (void *)index->map;
172 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
174 if (index->map_size < header_size + the_hash_algo->rawsz)
175 return error(_("corrupted bitmap index (too small)"));
177 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
178 return error(_("corrupted bitmap index file (wrong header)"));
180 index->version = ntohs(header->version);
181 if (index->version != 1)
182 return error(_("unsupported version '%d' for bitmap index file"), index->version);
184 /* Parse known bitmap format options */
186 uint32_t flags = ntohs(header->options);
187 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
188 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
190 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
191 BUG("unsupported options for bitmap index file "
192 "(Git requires BITMAP_OPT_FULL_DAG)");
194 if (flags & BITMAP_OPT_HASH_CACHE) {
195 if (cache_size > index_end - index->map - header_size)
196 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
197 index->hashes = (void *)(index_end - cache_size);
198 index_end -= cache_size;
201 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
202 size_t table_size = st_mult(ntohl(header->entry_count),
203 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
204 if (table_size > index_end - index->map - header_size)
205 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
206 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
207 index->table_lookup = (void *)(index_end - table_size);
208 index_end -= table_size;
212 index->entry_count = ntohl(header->entry_count);
213 index->checksum = header->checksum;
214 index->map_pos += header_size;
215 return 0;
218 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
219 struct ewah_bitmap *root,
220 const struct object_id *oid,
221 struct stored_bitmap *xor_with,
222 int flags)
224 struct stored_bitmap *stored;
225 khiter_t hash_pos;
226 int ret;
228 stored = xmalloc(sizeof(struct stored_bitmap));
229 stored->root = root;
230 stored->xor = xor_with;
231 stored->flags = flags;
232 oidcpy(&stored->oid, oid);
234 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
237 * A 0 return code means the insertion succeeded with no changes,
238 * because the SHA1 already existed on the map. This is bad, there
239 * shouldn't be duplicated commits in the index.
241 if (ret == 0) {
242 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
243 return NULL;
246 kh_value(index->bitmaps, hash_pos) = stored;
247 return stored;
250 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
252 uint32_t result = get_be32(buffer + *pos);
253 (*pos) += sizeof(result);
254 return result;
257 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
259 return buffer[(*pos)++];
262 #define MAX_XOR_OFFSET 160
264 static int nth_bitmap_object_oid(struct bitmap_index *index,
265 struct object_id *oid,
266 uint32_t n)
268 if (index->midx)
269 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
270 return nth_packed_object_id(oid, index->pack, n);
273 static int load_bitmap_entries_v1(struct bitmap_index *index)
275 uint32_t i;
276 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
278 for (i = 0; i < index->entry_count; ++i) {
279 int xor_offset, flags;
280 struct ewah_bitmap *bitmap = NULL;
281 struct stored_bitmap *xor_bitmap = NULL;
282 uint32_t commit_idx_pos;
283 struct object_id oid;
285 if (index->map_size - index->map_pos < 6)
286 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
288 commit_idx_pos = read_be32(index->map, &index->map_pos);
289 xor_offset = read_u8(index->map, &index->map_pos);
290 flags = read_u8(index->map, &index->map_pos);
292 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
293 return error(_("corrupt ewah bitmap: commit index %u out of range"),
294 (unsigned)commit_idx_pos);
296 bitmap = read_bitmap_1(index);
297 if (!bitmap)
298 return -1;
300 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
301 return error(_("corrupted bitmap pack index"));
303 if (xor_offset > 0) {
304 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
306 if (!xor_bitmap)
307 return error(_("invalid XOR offset in bitmap pack index"));
310 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
311 index, bitmap, &oid, xor_bitmap, flags);
314 return 0;
317 char *midx_bitmap_filename(struct multi_pack_index *midx)
319 struct strbuf buf = STRBUF_INIT;
321 get_midx_filename(&buf, midx->object_dir);
322 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
324 return strbuf_detach(&buf, NULL);
327 char *pack_bitmap_filename(struct packed_git *p)
329 size_t len;
331 if (!strip_suffix(p->pack_name, ".pack", &len))
332 BUG("pack_name does not end in .pack");
333 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
336 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
337 struct multi_pack_index *midx)
339 struct stat st;
340 char *bitmap_name = midx_bitmap_filename(midx);
341 int fd = git_open(bitmap_name);
342 uint32_t i;
343 struct packed_git *preferred;
345 if (fd < 0) {
346 if (errno != ENOENT)
347 warning_errno("cannot open '%s'", bitmap_name);
348 free(bitmap_name);
349 return -1;
351 free(bitmap_name);
353 if (fstat(fd, &st)) {
354 error_errno(_("cannot fstat bitmap file"));
355 close(fd);
356 return -1;
359 if (bitmap_git->pack || bitmap_git->midx) {
360 struct strbuf buf = STRBUF_INIT;
361 get_midx_filename(&buf, midx->object_dir);
362 trace2_data_string("bitmap", the_repository,
363 "ignoring extra midx bitmap file", buf.buf);
364 close(fd);
365 strbuf_release(&buf);
366 return -1;
369 bitmap_git->midx = midx;
370 bitmap_git->map_size = xsize_t(st.st_size);
371 bitmap_git->map_pos = 0;
372 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
373 MAP_PRIVATE, fd, 0);
374 close(fd);
376 if (load_bitmap_header(bitmap_git) < 0)
377 goto cleanup;
379 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
380 error(_("checksum doesn't match in MIDX and bitmap"));
381 goto cleanup;
384 if (load_midx_revindex(bitmap_git->midx) < 0) {
385 warning(_("multi-pack bitmap is missing required reverse index"));
386 goto cleanup;
389 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
390 if (prepare_midx_pack(the_repository, bitmap_git->midx, i))
391 die(_("could not open pack %s"),
392 bitmap_git->midx->pack_names[i]);
395 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
396 if (!is_pack_valid(preferred)) {
397 warning(_("preferred pack (%s) is invalid"),
398 preferred->pack_name);
399 goto cleanup;
402 return 0;
404 cleanup:
405 munmap(bitmap_git->map, bitmap_git->map_size);
406 bitmap_git->map_size = 0;
407 bitmap_git->map_pos = 0;
408 bitmap_git->map = NULL;
409 bitmap_git->midx = NULL;
410 return -1;
413 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
415 int fd;
416 struct stat st;
417 char *bitmap_name;
419 bitmap_name = pack_bitmap_filename(packfile);
420 fd = git_open(bitmap_name);
422 if (fd < 0) {
423 if (errno != ENOENT)
424 warning_errno("cannot open '%s'", bitmap_name);
425 free(bitmap_name);
426 return -1;
428 free(bitmap_name);
430 if (fstat(fd, &st)) {
431 error_errno(_("cannot fstat bitmap file"));
432 close(fd);
433 return -1;
436 if (bitmap_git->pack || bitmap_git->midx) {
437 trace2_data_string("bitmap", the_repository,
438 "ignoring extra bitmap file", packfile->pack_name);
439 close(fd);
440 return -1;
443 if (!is_pack_valid(packfile)) {
444 close(fd);
445 return -1;
448 bitmap_git->pack = packfile;
449 bitmap_git->map_size = xsize_t(st.st_size);
450 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
451 bitmap_git->map_pos = 0;
452 close(fd);
454 if (load_bitmap_header(bitmap_git) < 0) {
455 munmap(bitmap_git->map, bitmap_git->map_size);
456 bitmap_git->map = NULL;
457 bitmap_git->map_size = 0;
458 bitmap_git->map_pos = 0;
459 bitmap_git->pack = NULL;
460 return -1;
463 trace2_data_string("bitmap", the_repository, "opened bitmap file",
464 packfile->pack_name);
465 return 0;
468 static int load_reverse_index(struct bitmap_index *bitmap_git)
470 if (bitmap_is_midx(bitmap_git)) {
471 uint32_t i;
472 int ret;
475 * The multi-pack-index's .rev file is already loaded via
476 * open_pack_bitmap_1().
478 * But we still need to open the individual pack .rev files,
479 * since we will need to make use of them in pack-objects.
481 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
482 ret = load_pack_revindex(bitmap_git->midx->packs[i]);
483 if (ret)
484 return ret;
486 return 0;
488 return load_pack_revindex(bitmap_git->pack);
491 static int load_bitmap(struct bitmap_index *bitmap_git)
493 assert(bitmap_git->map);
495 bitmap_git->bitmaps = kh_init_oid_map();
496 bitmap_git->ext_index.positions = kh_init_oid_pos();
498 if (load_reverse_index(bitmap_git))
499 goto failed;
501 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
502 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
503 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
504 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
505 goto failed;
507 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
508 goto failed;
510 return 0;
512 failed:
513 munmap(bitmap_git->map, bitmap_git->map_size);
514 bitmap_git->map = NULL;
515 bitmap_git->map_size = 0;
517 kh_destroy_oid_map(bitmap_git->bitmaps);
518 bitmap_git->bitmaps = NULL;
520 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
521 bitmap_git->ext_index.positions = NULL;
523 return -1;
526 static int open_pack_bitmap(struct repository *r,
527 struct bitmap_index *bitmap_git)
529 struct packed_git *p;
530 int ret = -1;
532 for (p = get_all_packs(r); p; p = p->next) {
533 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
534 ret = 0;
536 * The only reason to keep looking is to report
537 * duplicates.
539 if (!trace2_is_enabled())
540 break;
544 return ret;
547 static int open_midx_bitmap(struct repository *r,
548 struct bitmap_index *bitmap_git)
550 int ret = -1;
551 struct multi_pack_index *midx;
553 assert(!bitmap_git->map);
555 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
556 if (!open_midx_bitmap_1(bitmap_git, midx))
557 ret = 0;
559 return ret;
562 static int open_bitmap(struct repository *r,
563 struct bitmap_index *bitmap_git)
565 int found;
567 assert(!bitmap_git->map);
569 found = !open_midx_bitmap(r, bitmap_git);
572 * these will all be skipped if we opened a midx bitmap; but run it
573 * anyway if tracing is enabled to report the duplicates
575 if (!found || trace2_is_enabled())
576 found |= !open_pack_bitmap(r, bitmap_git);
578 return found ? 0 : -1;
581 struct bitmap_index *prepare_bitmap_git(struct repository *r)
583 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
585 if (!open_bitmap(r, bitmap_git) && !load_bitmap(bitmap_git))
586 return bitmap_git;
588 free_bitmap_index(bitmap_git);
589 return NULL;
592 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
594 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
596 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(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 *find_objects(struct bitmap_index *bitmap_git,
1046 struct rev_info *revs,
1047 struct object_list *roots,
1048 struct bitmap *seen)
1050 struct bitmap *base = NULL;
1051 int needs_walk = 0;
1053 struct object_list *not_mapped = NULL;
1056 * Go through all the roots for the walk. The ones that have bitmaps
1057 * on the bitmap index will be `or`ed together to form an initial
1058 * global reachability analysis.
1060 * The ones without bitmaps in the index will be stored in the
1061 * `not_mapped_list` for further processing.
1063 while (roots) {
1064 struct object *object = roots->item;
1065 roots = roots->next;
1067 if (object->type == OBJ_COMMIT &&
1068 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1069 object->flags |= SEEN;
1070 continue;
1073 object_list_insert(object, &not_mapped);
1077 * Best case scenario: We found bitmaps for all the roots,
1078 * so the resulting `or` bitmap has the full reachability analysis
1080 if (!not_mapped)
1081 return base;
1083 roots = not_mapped;
1086 * Let's iterate through all the roots that don't have bitmaps to
1087 * check if we can determine them to be reachable from the existing
1088 * global bitmap.
1090 * If we cannot find them in the existing global bitmap, we'll need
1091 * to push them to an actual walk and run it until we can confirm
1092 * they are reachable
1094 while (roots) {
1095 struct object *object = roots->item;
1096 int pos;
1098 roots = roots->next;
1099 pos = bitmap_position(bitmap_git, &object->oid);
1101 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1102 object->flags &= ~UNINTERESTING;
1103 add_pending_object(revs, object, "");
1104 needs_walk = 1;
1105 } else {
1106 object->flags |= SEEN;
1110 if (needs_walk) {
1111 struct include_data incdata;
1112 struct bitmap_show_data show_data;
1114 if (!base)
1115 base = bitmap_new();
1117 incdata.bitmap_git = bitmap_git;
1118 incdata.base = base;
1119 incdata.seen = seen;
1121 revs->include_check = should_include;
1122 revs->include_check_obj = should_include_obj;
1123 revs->include_check_data = &incdata;
1125 if (prepare_revision_walk(revs))
1126 die(_("revision walk setup failed"));
1128 show_data.bitmap_git = bitmap_git;
1129 show_data.base = base;
1131 traverse_commit_list(revs,
1132 show_commit, show_object,
1133 &show_data);
1135 revs->include_check = NULL;
1136 revs->include_check_obj = NULL;
1137 revs->include_check_data = NULL;
1140 return base;
1143 static void show_extended_objects(struct bitmap_index *bitmap_git,
1144 struct rev_info *revs,
1145 show_reachable_fn show_reach)
1147 struct bitmap *objects = bitmap_git->result;
1148 struct eindex *eindex = &bitmap_git->ext_index;
1149 uint32_t i;
1151 for (i = 0; i < eindex->count; ++i) {
1152 struct object *obj;
1154 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1155 continue;
1157 obj = eindex->objects[i];
1158 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1159 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1160 (obj->type == OBJ_TAG && !revs->tag_objects))
1161 continue;
1163 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1167 static void init_type_iterator(struct ewah_iterator *it,
1168 struct bitmap_index *bitmap_git,
1169 enum object_type type)
1171 switch (type) {
1172 case OBJ_COMMIT:
1173 ewah_iterator_init(it, bitmap_git->commits);
1174 break;
1176 case OBJ_TREE:
1177 ewah_iterator_init(it, bitmap_git->trees);
1178 break;
1180 case OBJ_BLOB:
1181 ewah_iterator_init(it, bitmap_git->blobs);
1182 break;
1184 case OBJ_TAG:
1185 ewah_iterator_init(it, bitmap_git->tags);
1186 break;
1188 default:
1189 BUG("object type %d not stored by bitmap type index", type);
1190 break;
1194 static void show_objects_for_type(
1195 struct bitmap_index *bitmap_git,
1196 enum object_type object_type,
1197 show_reachable_fn show_reach)
1199 size_t i = 0;
1200 uint32_t offset;
1202 struct ewah_iterator it;
1203 eword_t filter;
1205 struct bitmap *objects = bitmap_git->result;
1207 init_type_iterator(&it, bitmap_git, object_type);
1209 for (i = 0; i < objects->word_alloc &&
1210 ewah_iterator_next(&filter, &it); i++) {
1211 eword_t word = objects->words[i] & filter;
1212 size_t pos = (i * BITS_IN_EWORD);
1214 if (!word)
1215 continue;
1217 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1218 struct packed_git *pack;
1219 struct object_id oid;
1220 uint32_t hash = 0, index_pos;
1221 off_t ofs;
1223 if ((word >> offset) == 0)
1224 break;
1226 offset += ewah_bit_ctz64(word >> offset);
1228 if (bitmap_is_midx(bitmap_git)) {
1229 struct multi_pack_index *m = bitmap_git->midx;
1230 uint32_t pack_id;
1232 index_pos = pack_pos_to_midx(m, pos + offset);
1233 ofs = nth_midxed_offset(m, index_pos);
1234 nth_midxed_object_oid(&oid, m, index_pos);
1236 pack_id = nth_midxed_pack_int_id(m, index_pos);
1237 pack = bitmap_git->midx->packs[pack_id];
1238 } else {
1239 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1240 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1241 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1243 pack = bitmap_git->pack;
1246 if (bitmap_git->hashes)
1247 hash = get_be32(bitmap_git->hashes + index_pos);
1249 show_reach(&oid, object_type, 0, hash, pack, ofs);
1254 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1255 struct object_list *roots)
1257 while (roots) {
1258 struct object *object = roots->item;
1259 roots = roots->next;
1261 if (bitmap_is_midx(bitmap_git)) {
1262 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1263 return 1;
1264 } else {
1265 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1266 return 1;
1270 return 0;
1273 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1274 struct object_list *tip_objects,
1275 enum object_type type)
1277 struct bitmap *result = bitmap_new();
1278 struct object_list *p;
1280 for (p = tip_objects; p; p = p->next) {
1281 int pos;
1283 if (p->item->type != type)
1284 continue;
1286 pos = bitmap_position(bitmap_git, &p->item->oid);
1287 if (pos < 0)
1288 continue;
1290 bitmap_set(result, pos);
1293 return result;
1296 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1297 struct object_list *tip_objects,
1298 struct bitmap *to_filter,
1299 enum object_type type)
1301 struct eindex *eindex = &bitmap_git->ext_index;
1302 struct bitmap *tips;
1303 struct ewah_iterator it;
1304 eword_t mask;
1305 uint32_t i;
1308 * The non-bitmap version of this filter never removes
1309 * objects which the other side specifically asked for,
1310 * so we must match that behavior.
1312 tips = find_tip_objects(bitmap_git, tip_objects, type);
1315 * We can use the type-level bitmap for 'type' to work in whole
1316 * words for the objects that are actually in the bitmapped
1317 * packfile.
1319 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1320 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1321 i++) {
1322 if (i < tips->word_alloc)
1323 mask &= ~tips->words[i];
1324 to_filter->words[i] &= ~mask;
1328 * Clear any objects that weren't in the packfile (and so would
1329 * not have been caught by the loop above. We'll have to check
1330 * them individually.
1332 for (i = 0; i < eindex->count; i++) {
1333 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1334 if (eindex->objects[i]->type == type &&
1335 bitmap_get(to_filter, pos) &&
1336 !bitmap_get(tips, pos))
1337 bitmap_unset(to_filter, pos);
1340 bitmap_free(tips);
1343 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1344 struct object_list *tip_objects,
1345 struct bitmap *to_filter)
1347 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1348 OBJ_BLOB);
1351 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1352 uint32_t pos)
1354 unsigned long size;
1355 struct object_info oi = OBJECT_INFO_INIT;
1357 oi.sizep = &size;
1359 if (pos < bitmap_num_objects(bitmap_git)) {
1360 struct packed_git *pack;
1361 off_t ofs;
1363 if (bitmap_is_midx(bitmap_git)) {
1364 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1365 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1367 pack = bitmap_git->midx->packs[pack_id];
1368 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1369 } else {
1370 pack = bitmap_git->pack;
1371 ofs = pack_pos_to_offset(pack, pos);
1374 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1375 struct object_id oid;
1376 nth_bitmap_object_oid(bitmap_git, &oid,
1377 pack_pos_to_index(pack, pos));
1378 die(_("unable to get size of %s"), oid_to_hex(&oid));
1380 } else {
1381 struct eindex *eindex = &bitmap_git->ext_index;
1382 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1383 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1384 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1387 return size;
1390 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1391 struct object_list *tip_objects,
1392 struct bitmap *to_filter,
1393 unsigned long limit)
1395 struct eindex *eindex = &bitmap_git->ext_index;
1396 struct bitmap *tips;
1397 struct ewah_iterator it;
1398 eword_t mask;
1399 uint32_t i;
1401 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1403 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1404 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1405 i++) {
1406 eword_t word = to_filter->words[i] & mask;
1407 unsigned offset;
1409 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1410 uint32_t pos;
1412 if ((word >> offset) == 0)
1413 break;
1414 offset += ewah_bit_ctz64(word >> offset);
1415 pos = i * BITS_IN_EWORD + offset;
1417 if (!bitmap_get(tips, pos) &&
1418 get_size_by_pos(bitmap_git, pos) >= limit)
1419 bitmap_unset(to_filter, pos);
1423 for (i = 0; i < eindex->count; i++) {
1424 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1425 if (eindex->objects[i]->type == OBJ_BLOB &&
1426 bitmap_get(to_filter, pos) &&
1427 !bitmap_get(tips, pos) &&
1428 get_size_by_pos(bitmap_git, pos) >= limit)
1429 bitmap_unset(to_filter, pos);
1432 bitmap_free(tips);
1435 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1436 struct object_list *tip_objects,
1437 struct bitmap *to_filter,
1438 unsigned long limit)
1440 if (limit)
1441 BUG("filter_bitmap_tree_depth given non-zero limit");
1443 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1444 OBJ_TREE);
1445 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1446 OBJ_BLOB);
1449 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1450 struct object_list *tip_objects,
1451 struct bitmap *to_filter,
1452 enum object_type object_type)
1454 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1455 BUG("filter_bitmap_object_type given invalid object");
1457 if (object_type != OBJ_TAG)
1458 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1459 if (object_type != OBJ_COMMIT)
1460 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1461 if (object_type != OBJ_TREE)
1462 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1463 if (object_type != OBJ_BLOB)
1464 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1467 static int filter_bitmap(struct bitmap_index *bitmap_git,
1468 struct object_list *tip_objects,
1469 struct bitmap *to_filter,
1470 struct list_objects_filter_options *filter)
1472 if (!filter || filter->choice == LOFC_DISABLED)
1473 return 0;
1475 if (filter->choice == LOFC_BLOB_NONE) {
1476 if (bitmap_git)
1477 filter_bitmap_blob_none(bitmap_git, tip_objects,
1478 to_filter);
1479 return 0;
1482 if (filter->choice == LOFC_BLOB_LIMIT) {
1483 if (bitmap_git)
1484 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1485 to_filter,
1486 filter->blob_limit_value);
1487 return 0;
1490 if (filter->choice == LOFC_TREE_DEPTH &&
1491 filter->tree_exclude_depth == 0) {
1492 if (bitmap_git)
1493 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1494 to_filter,
1495 filter->tree_exclude_depth);
1496 return 0;
1499 if (filter->choice == LOFC_OBJECT_TYPE) {
1500 if (bitmap_git)
1501 filter_bitmap_object_type(bitmap_git, tip_objects,
1502 to_filter,
1503 filter->object_type);
1504 return 0;
1507 if (filter->choice == LOFC_COMBINE) {
1508 int i;
1509 for (i = 0; i < filter->sub_nr; i++) {
1510 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1511 &filter->sub[i]) < 0)
1512 return -1;
1514 return 0;
1517 /* filter choice not handled */
1518 return -1;
1521 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1523 return !filter_bitmap(NULL, NULL, NULL, filter);
1526 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1527 int filter_provided_objects)
1529 unsigned int i;
1531 struct object_list *wants = NULL;
1532 struct object_list *haves = NULL;
1534 struct bitmap *wants_bitmap = NULL;
1535 struct bitmap *haves_bitmap = NULL;
1537 struct bitmap_index *bitmap_git;
1540 * We can't do pathspec limiting with bitmaps, because we don't know
1541 * which commits are associated with which object changes (let alone
1542 * even which objects are associated with which paths).
1544 if (revs->prune)
1545 return NULL;
1547 if (!can_filter_bitmap(&revs->filter))
1548 return NULL;
1550 /* try to open a bitmapped pack, but don't parse it yet
1551 * because we may not need to use it */
1552 CALLOC_ARRAY(bitmap_git, 1);
1553 if (open_bitmap(revs->repo, bitmap_git) < 0)
1554 goto cleanup;
1556 for (i = 0; i < revs->pending.nr; ++i) {
1557 struct object *object = revs->pending.objects[i].item;
1559 if (object->type == OBJ_NONE)
1560 parse_object_or_die(&object->oid, NULL);
1562 while (object->type == OBJ_TAG) {
1563 struct tag *tag = (struct tag *) object;
1565 if (object->flags & UNINTERESTING)
1566 object_list_insert(object, &haves);
1567 else
1568 object_list_insert(object, &wants);
1570 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1571 object->flags |= (tag->object.flags & UNINTERESTING);
1574 if (object->flags & UNINTERESTING)
1575 object_list_insert(object, &haves);
1576 else
1577 object_list_insert(object, &wants);
1581 * if we have a HAVES list, but none of those haves is contained
1582 * in the packfile that has a bitmap, we don't have anything to
1583 * optimize here
1585 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1586 goto cleanup;
1588 /* if we don't want anything, we're done here */
1589 if (!wants)
1590 goto cleanup;
1593 * now we're going to use bitmaps, so load the actual bitmap entries
1594 * from disk. this is the point of no return; after this the rev_list
1595 * becomes invalidated and we must perform the revwalk through bitmaps
1597 if (load_bitmap(bitmap_git) < 0)
1598 goto cleanup;
1600 object_array_clear(&revs->pending);
1602 if (haves) {
1603 revs->ignore_missing_links = 1;
1604 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1605 reset_revision_walk();
1606 revs->ignore_missing_links = 0;
1608 if (!haves_bitmap)
1609 BUG("failed to perform bitmap walk");
1612 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1614 if (!wants_bitmap)
1615 BUG("failed to perform bitmap walk");
1617 if (haves_bitmap)
1618 bitmap_and_not(wants_bitmap, haves_bitmap);
1620 filter_bitmap(bitmap_git,
1621 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1622 wants_bitmap,
1623 &revs->filter);
1625 bitmap_git->result = wants_bitmap;
1626 bitmap_git->haves = haves_bitmap;
1628 object_list_free(&wants);
1629 object_list_free(&haves);
1631 return bitmap_git;
1633 cleanup:
1634 free_bitmap_index(bitmap_git);
1635 object_list_free(&wants);
1636 object_list_free(&haves);
1637 return NULL;
1641 * -1 means "stop trying further objects"; 0 means we may or may not have
1642 * reused, but you can keep feeding bits.
1644 static int try_partial_reuse(struct packed_git *pack,
1645 size_t pos,
1646 struct bitmap *reuse,
1647 struct pack_window **w_curs)
1649 off_t offset, delta_obj_offset;
1650 enum object_type type;
1651 unsigned long size;
1654 * try_partial_reuse() is called either on (a) objects in the
1655 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1656 * objects in the preferred pack of a multi-pack bitmap.
1657 * Importantly, the latter can pretend as if only a single pack
1658 * exists because:
1660 * - The first pack->num_objects bits of a MIDX bitmap are
1661 * reserved for the preferred pack, and
1663 * - Ties due to duplicate objects are always resolved in
1664 * favor of the preferred pack.
1666 * Therefore we do not need to ever ask the MIDX for its copy of
1667 * an object by OID, since it will always select it from the
1668 * preferred pack. Likewise, the selected copy of the base
1669 * object for any deltas will reside in the same pack.
1671 * This means that we can reuse pos when looking up the bit in
1672 * the reuse bitmap, too, since bits corresponding to the
1673 * preferred pack precede all bits from other packs.
1676 if (pos >= pack->num_objects)
1677 return -1; /* not actually in the pack or MIDX preferred pack */
1679 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1680 type = unpack_object_header(pack, w_curs, &offset, &size);
1681 if (type < 0)
1682 return -1; /* broken packfile, punt */
1684 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1685 off_t base_offset;
1686 uint32_t base_pos;
1689 * Find the position of the base object so we can look it up
1690 * in our bitmaps. If we can't come up with an offset, or if
1691 * that offset is not in the revidx, the pack is corrupt.
1692 * There's nothing we can do, so just punt on this object,
1693 * and the normal slow path will complain about it in
1694 * more detail.
1696 base_offset = get_delta_base(pack, w_curs, &offset, type,
1697 delta_obj_offset);
1698 if (!base_offset)
1699 return 0;
1700 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1701 return 0;
1704 * We assume delta dependencies always point backwards. This
1705 * lets us do a single pass, and is basically always true
1706 * due to the way OFS_DELTAs work. You would not typically
1707 * find REF_DELTA in a bitmapped pack, since we only bitmap
1708 * packs we write fresh, and OFS_DELTA is the default). But
1709 * let's double check to make sure the pack wasn't written with
1710 * odd parameters.
1712 if (base_pos >= pos)
1713 return 0;
1716 * And finally, if we're not sending the base as part of our
1717 * reuse chunk, then don't send this object either. The base
1718 * would come after us, along with other objects not
1719 * necessarily in the pack, which means we'd need to convert
1720 * to REF_DELTA on the fly. Better to just let the normal
1721 * object_entry code path handle it.
1723 if (!bitmap_get(reuse, base_pos))
1724 return 0;
1728 * If we got here, then the object is OK to reuse. Mark it.
1730 bitmap_set(reuse, pos);
1731 return 0;
1734 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1736 struct multi_pack_index *m = bitmap_git->midx;
1737 if (!m)
1738 BUG("midx_preferred_pack: requires non-empty MIDX");
1739 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1742 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1743 struct packed_git **packfile_out,
1744 uint32_t *entries,
1745 struct bitmap **reuse_out)
1747 struct packed_git *pack;
1748 struct bitmap *result = bitmap_git->result;
1749 struct bitmap *reuse;
1750 struct pack_window *w_curs = NULL;
1751 size_t i = 0;
1752 uint32_t offset;
1753 uint32_t objects_nr;
1755 assert(result);
1757 load_reverse_index(bitmap_git);
1759 if (bitmap_is_midx(bitmap_git))
1760 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1761 else
1762 pack = bitmap_git->pack;
1763 objects_nr = pack->num_objects;
1765 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1766 i++;
1769 * Don't mark objects not in the packfile or preferred pack. This bitmap
1770 * marks objects eligible for reuse, but the pack-reuse code only
1771 * understands how to reuse a single pack. Since the preferred pack is
1772 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1773 * we use it instead of another pack. In single-pack bitmaps, the choice
1774 * is made for us.
1776 if (i > objects_nr / BITS_IN_EWORD)
1777 i = objects_nr / BITS_IN_EWORD;
1779 reuse = bitmap_word_alloc(i);
1780 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1782 for (; i < result->word_alloc; ++i) {
1783 eword_t word = result->words[i];
1784 size_t pos = (i * BITS_IN_EWORD);
1786 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1787 if ((word >> offset) == 0)
1788 break;
1790 offset += ewah_bit_ctz64(word >> offset);
1791 if (try_partial_reuse(pack, pos + offset,
1792 reuse, &w_curs) < 0) {
1794 * try_partial_reuse indicated we couldn't reuse
1795 * any bits, so there is no point in trying more
1796 * bits in the current word, or any other words
1797 * in result.
1799 * Jump out of both loops to avoid future
1800 * unnecessary calls to try_partial_reuse.
1802 goto done;
1807 done:
1808 unuse_pack(&w_curs);
1810 *entries = bitmap_popcount(reuse);
1811 if (!*entries) {
1812 bitmap_free(reuse);
1813 return -1;
1817 * Drop any reused objects from the result, since they will not
1818 * need to be handled separately.
1820 bitmap_and_not(result, reuse);
1821 *packfile_out = pack;
1822 *reuse_out = reuse;
1823 return 0;
1826 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1827 struct bitmap *bitmap, const struct object_id *oid)
1829 int idx;
1831 if (!bitmap)
1832 return 0;
1834 idx = bitmap_position(bitmap_git, oid);
1835 return idx >= 0 && bitmap_get(bitmap, idx);
1838 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1839 struct rev_info *revs,
1840 show_reachable_fn show_reachable)
1842 assert(bitmap_git->result);
1844 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1845 if (revs->tree_objects)
1846 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1847 if (revs->blob_objects)
1848 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1849 if (revs->tag_objects)
1850 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1852 show_extended_objects(bitmap_git, revs, show_reachable);
1855 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1856 enum object_type type)
1858 struct bitmap *objects = bitmap_git->result;
1859 struct eindex *eindex = &bitmap_git->ext_index;
1861 uint32_t i = 0, count = 0;
1862 struct ewah_iterator it;
1863 eword_t filter;
1865 init_type_iterator(&it, bitmap_git, type);
1867 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1868 eword_t word = objects->words[i++] & filter;
1869 count += ewah_bit_popcount64(word);
1872 for (i = 0; i < eindex->count; ++i) {
1873 if (eindex->objects[i]->type == type &&
1874 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1875 count++;
1878 return count;
1881 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1882 uint32_t *commits, uint32_t *trees,
1883 uint32_t *blobs, uint32_t *tags)
1885 assert(bitmap_git->result);
1887 if (commits)
1888 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1890 if (trees)
1891 *trees = count_object_type(bitmap_git, OBJ_TREE);
1893 if (blobs)
1894 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1896 if (tags)
1897 *tags = count_object_type(bitmap_git, OBJ_TAG);
1900 struct bitmap_test_data {
1901 struct bitmap_index *bitmap_git;
1902 struct bitmap *base;
1903 struct bitmap *commits;
1904 struct bitmap *trees;
1905 struct bitmap *blobs;
1906 struct bitmap *tags;
1907 struct progress *prg;
1908 size_t seen;
1911 static void test_bitmap_type(struct bitmap_test_data *tdata,
1912 struct object *obj, int pos)
1914 enum object_type bitmap_type = OBJ_NONE;
1915 int bitmaps_nr = 0;
1917 if (bitmap_get(tdata->commits, pos)) {
1918 bitmap_type = OBJ_COMMIT;
1919 bitmaps_nr++;
1921 if (bitmap_get(tdata->trees, pos)) {
1922 bitmap_type = OBJ_TREE;
1923 bitmaps_nr++;
1925 if (bitmap_get(tdata->blobs, pos)) {
1926 bitmap_type = OBJ_BLOB;
1927 bitmaps_nr++;
1929 if (bitmap_get(tdata->tags, pos)) {
1930 bitmap_type = OBJ_TAG;
1931 bitmaps_nr++;
1934 if (bitmap_type == OBJ_NONE)
1935 die(_("object '%s' not found in type bitmaps"),
1936 oid_to_hex(&obj->oid));
1938 if (bitmaps_nr > 1)
1939 die(_("object '%s' does not have a unique type"),
1940 oid_to_hex(&obj->oid));
1942 if (bitmap_type != obj->type)
1943 die(_("object '%s': real type '%s', expected: '%s'"),
1944 oid_to_hex(&obj->oid),
1945 type_name(obj->type),
1946 type_name(bitmap_type));
1949 static void test_show_object(struct object *object,
1950 const char *name UNUSED,
1951 void *data)
1953 struct bitmap_test_data *tdata = data;
1954 int bitmap_pos;
1956 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1957 if (bitmap_pos < 0)
1958 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
1959 test_bitmap_type(tdata, object, bitmap_pos);
1961 bitmap_set(tdata->base, bitmap_pos);
1962 display_progress(tdata->prg, ++tdata->seen);
1965 static void test_show_commit(struct commit *commit, void *data)
1967 struct bitmap_test_data *tdata = data;
1968 int bitmap_pos;
1970 bitmap_pos = bitmap_position(tdata->bitmap_git,
1971 &commit->object.oid);
1972 if (bitmap_pos < 0)
1973 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
1974 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1976 bitmap_set(tdata->base, bitmap_pos);
1977 display_progress(tdata->prg, ++tdata->seen);
1980 void test_bitmap_walk(struct rev_info *revs)
1982 struct object *root;
1983 struct bitmap *result = NULL;
1984 size_t result_popcnt;
1985 struct bitmap_test_data tdata;
1986 struct bitmap_index *bitmap_git;
1987 struct ewah_bitmap *bm;
1989 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1990 die(_("failed to load bitmap indexes"));
1992 if (revs->pending.nr != 1)
1993 die(_("you must specify exactly one commit to test"));
1995 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
1996 bitmap_git->version,
1997 bitmap_git->entry_count,
1998 bitmap_git->table_lookup ? "" : " loaded");
2000 root = revs->pending.objects[0].item;
2001 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2003 if (bm) {
2004 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2005 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2007 result = ewah_to_bitmap(bm);
2010 if (!result)
2011 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2013 revs->tag_objects = 1;
2014 revs->tree_objects = 1;
2015 revs->blob_objects = 1;
2017 result_popcnt = bitmap_popcount(result);
2019 if (prepare_revision_walk(revs))
2020 die(_("revision walk setup failed"));
2022 tdata.bitmap_git = bitmap_git;
2023 tdata.base = bitmap_new();
2024 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2025 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2026 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2027 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2028 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2029 tdata.seen = 0;
2031 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2033 stop_progress(&tdata.prg);
2035 if (bitmap_equals(result, tdata.base))
2036 fprintf_ln(stderr, "OK!");
2037 else
2038 die(_("mismatch in bitmap results"));
2040 bitmap_free(result);
2041 bitmap_free(tdata.base);
2042 bitmap_free(tdata.commits);
2043 bitmap_free(tdata.trees);
2044 bitmap_free(tdata.blobs);
2045 bitmap_free(tdata.tags);
2046 free_bitmap_index(bitmap_git);
2049 int test_bitmap_commits(struct repository *r)
2051 struct object_id oid;
2052 MAYBE_UNUSED void *value;
2053 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2055 if (!bitmap_git)
2056 die(_("failed to load bitmap indexes"));
2059 * As this function is only used to print bitmap selected
2060 * commits, we don't have to read the commit table.
2062 if (bitmap_git->table_lookup) {
2063 if (load_bitmap_entries_v1(bitmap_git) < 0)
2064 die(_("failed to load bitmap indexes"));
2067 kh_foreach(bitmap_git->bitmaps, oid, value, {
2068 printf_ln("%s", oid_to_hex(&oid));
2071 free_bitmap_index(bitmap_git);
2073 return 0;
2076 int test_bitmap_hashes(struct repository *r)
2078 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2079 struct object_id oid;
2080 uint32_t i, index_pos;
2082 if (!bitmap_git || !bitmap_git->hashes)
2083 goto cleanup;
2085 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2086 if (bitmap_is_midx(bitmap_git))
2087 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2088 else
2089 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2091 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2093 printf_ln("%s %"PRIu32"",
2094 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2097 cleanup:
2098 free_bitmap_index(bitmap_git);
2100 return 0;
2103 int rebuild_bitmap(const uint32_t *reposition,
2104 struct ewah_bitmap *source,
2105 struct bitmap *dest)
2107 uint32_t pos = 0;
2108 struct ewah_iterator it;
2109 eword_t word;
2111 ewah_iterator_init(&it, source);
2113 while (ewah_iterator_next(&word, &it)) {
2114 uint32_t offset, bit_pos;
2116 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2117 if ((word >> offset) == 0)
2118 break;
2120 offset += ewah_bit_ctz64(word >> offset);
2122 bit_pos = reposition[pos + offset];
2123 if (bit_pos > 0)
2124 bitmap_set(dest, bit_pos - 1);
2125 else /* can't reuse, we don't have the object */
2126 return -1;
2129 pos += BITS_IN_EWORD;
2131 return 0;
2134 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2135 struct packing_data *mapping)
2137 uint32_t i, num_objects;
2138 uint32_t *reposition;
2140 if (!bitmap_is_midx(bitmap_git))
2141 load_reverse_index(bitmap_git);
2142 else if (load_midx_revindex(bitmap_git->midx) < 0)
2143 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2144 "extension");
2146 num_objects = bitmap_num_objects(bitmap_git);
2147 CALLOC_ARRAY(reposition, num_objects);
2149 for (i = 0; i < num_objects; ++i) {
2150 struct object_id oid;
2151 struct object_entry *oe;
2152 uint32_t index_pos;
2154 if (bitmap_is_midx(bitmap_git))
2155 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2156 else
2157 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2158 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2159 oe = packlist_find(mapping, &oid);
2161 if (oe) {
2162 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2163 if (bitmap_git->hashes && !oe->hash)
2164 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2168 return reposition;
2171 void free_bitmap_index(struct bitmap_index *b)
2173 if (!b)
2174 return;
2176 if (b->map)
2177 munmap(b->map, b->map_size);
2178 ewah_pool_free(b->commits);
2179 ewah_pool_free(b->trees);
2180 ewah_pool_free(b->blobs);
2181 ewah_pool_free(b->tags);
2182 if (b->bitmaps) {
2183 struct stored_bitmap *sb;
2184 kh_foreach_value(b->bitmaps, sb, {
2185 ewah_pool_free(sb->root);
2186 free(sb);
2189 kh_destroy_oid_map(b->bitmaps);
2190 free(b->ext_index.objects);
2191 free(b->ext_index.hashes);
2192 kh_destroy_oid_pos(b->ext_index.positions);
2193 bitmap_free(b->result);
2194 bitmap_free(b->haves);
2195 if (bitmap_is_midx(b)) {
2197 * Multi-pack bitmaps need to have resources associated with
2198 * their on-disk reverse indexes unmapped so that stale .rev and
2199 * .bitmap files can be removed.
2201 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2202 * written in the same 'git multi-pack-index write --bitmap'
2203 * process. Close resources so they can be removed safely on
2204 * platforms like Windows.
2206 close_midx_revindex(b->midx);
2208 free(b);
2211 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2212 const struct object_id *oid)
2214 return bitmap_git &&
2215 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2218 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2219 enum object_type object_type)
2221 struct bitmap *result = bitmap_git->result;
2222 off_t total = 0;
2223 struct ewah_iterator it;
2224 eword_t filter;
2225 size_t i;
2227 init_type_iterator(&it, bitmap_git, object_type);
2228 for (i = 0; i < result->word_alloc &&
2229 ewah_iterator_next(&filter, &it); i++) {
2230 eword_t word = result->words[i] & filter;
2231 size_t base = (i * BITS_IN_EWORD);
2232 unsigned offset;
2234 if (!word)
2235 continue;
2237 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2238 if ((word >> offset) == 0)
2239 break;
2241 offset += ewah_bit_ctz64(word >> offset);
2243 if (bitmap_is_midx(bitmap_git)) {
2244 uint32_t pack_pos;
2245 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2246 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2248 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2249 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2251 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2252 struct object_id oid;
2253 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2255 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2256 oid_to_hex(&oid),
2257 pack->pack_name,
2258 (uintmax_t)offset);
2261 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2262 } else {
2263 size_t pos = base + offset;
2264 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2265 pack_pos_to_offset(bitmap_git->pack, pos);
2270 return total;
2273 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2275 struct bitmap *result = bitmap_git->result;
2276 struct eindex *eindex = &bitmap_git->ext_index;
2277 off_t total = 0;
2278 struct object_info oi = OBJECT_INFO_INIT;
2279 off_t object_size;
2280 size_t i;
2282 oi.disk_sizep = &object_size;
2284 for (i = 0; i < eindex->count; i++) {
2285 struct object *obj = eindex->objects[i];
2287 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2288 continue;
2290 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2291 die(_("unable to get disk usage of '%s'"),
2292 oid_to_hex(&obj->oid));
2294 total += object_size;
2296 return total;
2299 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2300 struct rev_info *revs)
2302 off_t total = 0;
2304 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2305 if (revs->tree_objects)
2306 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2307 if (revs->blob_objects)
2308 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2309 if (revs->tag_objects)
2310 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2312 total += get_disk_usage_for_extended(bitmap_git);
2314 return total;
2317 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2319 return !!bitmap_git->midx;
2322 const struct string_list *bitmap_preferred_tips(struct repository *r)
2324 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
2327 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2329 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2330 struct string_list_item *item;
2332 if (!preferred_tips)
2333 return 0;
2335 for_each_string_list_item(item, preferred_tips) {
2336 if (starts_with(refname, item->string))
2337 return 1;
2340 return 0;