7 #include "list-objects.h"
9 #include "pack-bitmap.h"
10 #include "pack-revindex.h"
11 #include "pack-objects.h"
13 #include "repository.h"
14 #include "object-store.h"
15 #include "list-objects-filter-options.h"
19 * An entry on the bitmap index, representing the bitmap for a given
22 struct stored_bitmap {
24 struct ewah_bitmap *root;
25 struct stored_bitmap *xor;
30 * The active bitmap index for a repository. By design, repositories only have
31 * a single bitmap index available (the index for the biggest packfile in
32 * the repository), since bitmap indexes need full closure.
34 * If there is more than one bitmap index available (e.g. because of alternates),
35 * the active bitmap index is the largest one.
38 /* Packfile to which this bitmap index belongs to */
39 struct packed_git *pack;
42 * Mark the first `reuse_objects` in the packfile as reused:
43 * they will be sent as-is without using them for repacking
46 uint32_t reuse_objects;
48 /* mmapped buffer of the whole bitmap index */
50 size_t map_size; /* size of the mmaped buffer */
51 size_t map_pos; /* current position when loading the index */
56 * Each bitmap marks which objects in the packfile are of the given
57 * type. This provides type information when yielding the objects from
58 * the packfile during a walk, which allows for better delta bases.
60 struct ewah_bitmap *commits;
61 struct ewah_bitmap *trees;
62 struct ewah_bitmap *blobs;
63 struct ewah_bitmap *tags;
65 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
66 kh_oid_map_t *bitmaps;
68 /* Number of bitmapped commits */
71 /* If not NULL, this is a name-hash cache pointing into map. */
77 * When trying to perform bitmap operations with objects that are not
78 * packed in `pack`, these objects are added to this "fake index" and
79 * are assumed to appear at the end of the packfile for all operations
82 struct object **objects;
84 uint32_t count, alloc;
85 kh_oid_pos_t *positions;
88 /* Bitmap result of the last performed walk */
89 struct bitmap *result;
91 /* "have" bitmap from the last performed walk */
94 /* Version of the bitmap index */
98 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
100 struct ewah_bitmap *parent;
101 struct ewah_bitmap *composed;
106 composed = ewah_pool_new();
107 parent = lookup_stored_bitmap(st->xor);
108 ewah_xor(st->root, parent, composed);
110 ewah_pool_free(st->root);
118 * Read a bitmap from the current read position on the mmaped
119 * index, and increase the read position accordingly
121 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
123 struct ewah_bitmap *b = ewah_pool_new();
125 ssize_t bitmap_size = ewah_read_mmap(b,
126 index->map + index->map_pos,
127 index->map_size - index->map_pos);
129 if (bitmap_size < 0) {
130 error("Failed to load bitmap index (corrupted?)");
135 index->map_pos += bitmap_size;
139 static int load_bitmap_header(struct bitmap_index *index)
141 struct bitmap_disk_header *header = (void *)index->map;
142 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
144 if (index->map_size < header_size + the_hash_algo->rawsz)
145 return error("Corrupted bitmap index (too small)");
147 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
148 return error("Corrupted bitmap index file (wrong header)");
150 index->version = ntohs(header->version);
151 if (index->version != 1)
152 return error("Unsupported version for bitmap index file (%d)", index->version);
154 /* Parse known bitmap format options */
156 uint32_t flags = ntohs(header->options);
157 size_t cache_size = st_mult(index->pack->num_objects, sizeof(uint32_t));
158 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
160 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
161 return error("Unsupported options for bitmap index file "
162 "(Git requires BITMAP_OPT_FULL_DAG)");
164 if (flags & BITMAP_OPT_HASH_CACHE) {
165 if (cache_size > index_end - index->map - header_size)
166 return error("corrupted bitmap index file (too short to fit hash cache)");
167 index->hashes = (void *)(index_end - cache_size);
168 index_end -= cache_size;
172 index->entry_count = ntohl(header->entry_count);
173 index->map_pos += header_size;
177 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
178 struct ewah_bitmap *root,
179 const struct object_id *oid,
180 struct stored_bitmap *xor_with,
183 struct stored_bitmap *stored;
187 stored = xmalloc(sizeof(struct stored_bitmap));
189 stored->xor = xor_with;
190 stored->flags = flags;
191 oidcpy(&stored->oid, oid);
193 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
195 /* a 0 return code means the insertion succeeded with no changes,
196 * because the SHA1 already existed on the map. this is bad, there
197 * shouldn't be duplicated commits in the index */
199 error("Duplicate entry in bitmap index: %s", oid_to_hex(oid));
203 kh_value(index->bitmaps, hash_pos) = stored;
207 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
209 uint32_t result = get_be32(buffer + *pos);
210 (*pos) += sizeof(result);
214 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
216 return buffer[(*pos)++];
219 #define MAX_XOR_OFFSET 160
221 static int load_bitmap_entries_v1(struct bitmap_index *index)
224 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
226 for (i = 0; i < index->entry_count; ++i) {
227 int xor_offset, flags;
228 struct ewah_bitmap *bitmap = NULL;
229 struct stored_bitmap *xor_bitmap = NULL;
230 uint32_t commit_idx_pos;
231 struct object_id oid;
233 if (index->map_size - index->map_pos < 6)
234 return error("corrupt ewah bitmap: truncated header for entry %d", i);
236 commit_idx_pos = read_be32(index->map, &index->map_pos);
237 xor_offset = read_u8(index->map, &index->map_pos);
238 flags = read_u8(index->map, &index->map_pos);
240 if (nth_packed_object_id(&oid, index->pack, commit_idx_pos) < 0)
241 return error("corrupt ewah bitmap: commit index %u out of range",
242 (unsigned)commit_idx_pos);
244 bitmap = read_bitmap_1(index);
248 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
249 return error("Corrupted bitmap pack index");
251 if (xor_offset > 0) {
252 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
254 if (xor_bitmap == NULL)
255 return error("Invalid XOR offset in bitmap pack index");
258 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
259 index, bitmap, &oid, xor_bitmap, flags);
265 static char *pack_bitmap_filename(struct packed_git *p)
269 if (!strip_suffix(p->pack_name, ".pack", &len))
270 BUG("pack_name does not end in .pack");
271 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
274 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
280 if (open_pack_index(packfile))
283 idx_name = pack_bitmap_filename(packfile);
284 fd = git_open(idx_name);
290 if (fstat(fd, &st)) {
295 if (bitmap_git->pack) {
296 warning("ignoring extra bitmap file: %s", packfile->pack_name);
301 bitmap_git->pack = packfile;
302 bitmap_git->map_size = xsize_t(st.st_size);
303 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
304 bitmap_git->map_pos = 0;
307 if (load_bitmap_header(bitmap_git) < 0) {
308 munmap(bitmap_git->map, bitmap_git->map_size);
309 bitmap_git->map = NULL;
310 bitmap_git->map_size = 0;
317 static int load_pack_bitmap(struct bitmap_index *bitmap_git)
319 assert(bitmap_git->map);
321 bitmap_git->bitmaps = kh_init_oid_map();
322 bitmap_git->ext_index.positions = kh_init_oid_pos();
323 if (load_pack_revindex(bitmap_git->pack))
326 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
327 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
328 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
329 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
332 if (load_bitmap_entries_v1(bitmap_git) < 0)
338 munmap(bitmap_git->map, bitmap_git->map_size);
339 bitmap_git->map = NULL;
340 bitmap_git->map_size = 0;
342 kh_destroy_oid_map(bitmap_git->bitmaps);
343 bitmap_git->bitmaps = NULL;
345 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
346 bitmap_git->ext_index.positions = NULL;
351 static int open_pack_bitmap(struct repository *r,
352 struct bitmap_index *bitmap_git)
354 struct packed_git *p;
357 assert(!bitmap_git->map);
359 for (p = get_all_packs(r); p; p = p->next) {
360 if (open_pack_bitmap_1(bitmap_git, p) == 0)
367 struct bitmap_index *prepare_bitmap_git(struct repository *r)
369 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
371 if (!open_pack_bitmap(r, bitmap_git) && !load_pack_bitmap(bitmap_git))
374 free_bitmap_index(bitmap_git);
378 struct include_data {
379 struct bitmap_index *bitmap_git;
384 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
385 struct commit *commit)
387 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
389 if (hash_pos >= kh_end(bitmap_git->bitmaps))
391 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
394 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
395 const struct object_id *oid)
397 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
398 khiter_t pos = kh_get_oid_pos(positions, *oid);
400 if (pos < kh_end(positions)) {
401 int bitmap_pos = kh_value(positions, pos);
402 return bitmap_pos + bitmap_git->pack->num_objects;
408 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
409 const struct object_id *oid)
412 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
416 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
421 static int bitmap_position(struct bitmap_index *bitmap_git,
422 const struct object_id *oid)
424 int pos = bitmap_position_packfile(bitmap_git, oid);
425 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
428 static int ext_index_add_object(struct bitmap_index *bitmap_git,
429 struct object *object, const char *name)
431 struct eindex *eindex = &bitmap_git->ext_index;
437 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
439 if (eindex->count >= eindex->alloc) {
440 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
441 REALLOC_ARRAY(eindex->objects, eindex->alloc);
442 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
445 bitmap_pos = eindex->count;
446 eindex->objects[eindex->count] = object;
447 eindex->hashes[eindex->count] = pack_name_hash(name);
448 kh_value(eindex->positions, hash_pos) = bitmap_pos;
451 bitmap_pos = kh_value(eindex->positions, hash_pos);
454 return bitmap_pos + bitmap_git->pack->num_objects;
457 struct bitmap_show_data {
458 struct bitmap_index *bitmap_git;
462 static void show_object(struct object *object, const char *name, void *data_)
464 struct bitmap_show_data *data = data_;
467 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
470 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
473 bitmap_set(data->base, bitmap_pos);
476 static void show_commit(struct commit *commit, void *data)
480 static int add_to_include_set(struct bitmap_index *bitmap_git,
481 struct include_data *data,
482 struct commit *commit,
485 struct ewah_bitmap *partial;
487 if (data->seen && bitmap_get(data->seen, bitmap_pos))
490 if (bitmap_get(data->base, bitmap_pos))
493 partial = bitmap_for_commit(bitmap_git, commit);
495 bitmap_or_ewah(data->base, partial);
499 bitmap_set(data->base, bitmap_pos);
503 static int should_include(struct commit *commit, void *_data)
505 struct include_data *data = _data;
508 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
510 bitmap_pos = ext_index_add_object(data->bitmap_git,
511 (struct object *)commit,
514 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
515 struct commit_list *parent = commit->parents;
518 parent->item->object.flags |= SEEN;
519 parent = parent->next;
528 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
529 struct bitmap **base,
530 struct commit *commit)
532 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
538 *base = ewah_to_bitmap(or_with);
540 bitmap_or_ewah(*base, or_with);
545 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
546 struct rev_info *revs,
547 struct object_list *roots,
549 struct list_objects_filter_options *filter)
551 struct bitmap *base = NULL;
554 struct object_list *not_mapped = NULL;
557 * Go through all the roots for the walk. The ones that have bitmaps
558 * on the bitmap index will be `or`ed together to form an initial
559 * global reachability analysis.
561 * The ones without bitmaps in the index will be stored in the
562 * `not_mapped_list` for further processing.
565 struct object *object = roots->item;
568 if (object->type == OBJ_COMMIT &&
569 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
570 object->flags |= SEEN;
574 object_list_insert(object, ¬_mapped);
578 * Best case scenario: We found bitmaps for all the roots,
579 * so the resulting `or` bitmap has the full reachability analysis
581 if (not_mapped == NULL)
587 * Let's iterate through all the roots that don't have bitmaps to
588 * check if we can determine them to be reachable from the existing
591 * If we cannot find them in the existing global bitmap, we'll need
592 * to push them to an actual walk and run it until we can confirm
596 struct object *object = roots->item;
600 pos = bitmap_position(bitmap_git, &object->oid);
602 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
603 object->flags &= ~UNINTERESTING;
604 add_pending_object(revs, object, "");
607 object->flags |= SEEN;
612 struct include_data incdata;
613 struct bitmap_show_data show_data;
618 incdata.bitmap_git = bitmap_git;
622 revs->include_check = should_include;
623 revs->include_check_data = &incdata;
625 if (prepare_revision_walk(revs))
626 die("revision walk setup failed");
628 show_data.bitmap_git = bitmap_git;
629 show_data.base = base;
631 traverse_commit_list_filtered(filter, revs,
632 show_commit, show_object,
639 static void show_extended_objects(struct bitmap_index *bitmap_git,
640 struct rev_info *revs,
641 show_reachable_fn show_reach)
643 struct bitmap *objects = bitmap_git->result;
644 struct eindex *eindex = &bitmap_git->ext_index;
647 for (i = 0; i < eindex->count; ++i) {
650 if (!bitmap_get(objects, bitmap_git->pack->num_objects + i))
653 obj = eindex->objects[i];
654 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
655 (obj->type == OBJ_TREE && !revs->tree_objects) ||
656 (obj->type == OBJ_TAG && !revs->tag_objects))
659 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
663 static void init_type_iterator(struct ewah_iterator *it,
664 struct bitmap_index *bitmap_git,
665 enum object_type type)
669 ewah_iterator_init(it, bitmap_git->commits);
673 ewah_iterator_init(it, bitmap_git->trees);
677 ewah_iterator_init(it, bitmap_git->blobs);
681 ewah_iterator_init(it, bitmap_git->tags);
685 BUG("object type %d not stored by bitmap type index", type);
690 static void show_objects_for_type(
691 struct bitmap_index *bitmap_git,
692 enum object_type object_type,
693 show_reachable_fn show_reach)
698 struct ewah_iterator it;
701 struct bitmap *objects = bitmap_git->result;
703 init_type_iterator(&it, bitmap_git, object_type);
705 for (i = 0; i < objects->word_alloc &&
706 ewah_iterator_next(&filter, &it); i++) {
707 eword_t word = objects->words[i] & filter;
708 size_t pos = (i * BITS_IN_EWORD);
713 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
714 struct object_id oid;
715 uint32_t hash = 0, index_pos;
718 if ((word >> offset) == 0)
721 offset += ewah_bit_ctz64(word >> offset);
723 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
724 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
725 nth_packed_object_id(&oid, bitmap_git->pack, index_pos);
727 if (bitmap_git->hashes)
728 hash = get_be32(bitmap_git->hashes + index_pos);
730 show_reach(&oid, object_type, 0, hash, bitmap_git->pack, ofs);
735 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
736 struct object_list *roots)
739 struct object *object = roots->item;
742 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
749 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
750 struct object_list *tip_objects,
751 enum object_type type)
753 struct bitmap *result = bitmap_new();
754 struct object_list *p;
756 for (p = tip_objects; p; p = p->next) {
759 if (p->item->type != type)
762 pos = bitmap_position(bitmap_git, &p->item->oid);
766 bitmap_set(result, pos);
772 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
773 struct object_list *tip_objects,
774 struct bitmap *to_filter,
775 enum object_type type)
777 struct eindex *eindex = &bitmap_git->ext_index;
779 struct ewah_iterator it;
783 if (type != OBJ_BLOB && type != OBJ_TREE)
784 BUG("filter_bitmap_exclude_type: unsupported type '%d'", type);
787 * The non-bitmap version of this filter never removes
788 * objects which the other side specifically asked for,
789 * so we must match that behavior.
791 tips = find_tip_objects(bitmap_git, tip_objects, type);
794 * We can use the blob type-bitmap to work in whole words
795 * for the objects that are actually in the bitmapped packfile.
797 for (i = 0, init_type_iterator(&it, bitmap_git, type);
798 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
800 if (i < tips->word_alloc)
801 mask &= ~tips->words[i];
802 to_filter->words[i] &= ~mask;
806 * Clear any blobs that weren't in the packfile (and so would not have
807 * been caught by the loop above. We'll have to check them
810 for (i = 0; i < eindex->count; i++) {
811 uint32_t pos = i + bitmap_git->pack->num_objects;
812 if (eindex->objects[i]->type == type &&
813 bitmap_get(to_filter, pos) &&
814 !bitmap_get(tips, pos))
815 bitmap_unset(to_filter, pos);
821 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
822 struct object_list *tip_objects,
823 struct bitmap *to_filter)
825 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
829 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
832 struct packed_git *pack = bitmap_git->pack;
834 struct object_info oi = OBJECT_INFO_INIT;
838 if (pos < pack->num_objects) {
839 off_t ofs = pack_pos_to_offset(pack, pos);
840 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
841 struct object_id oid;
842 nth_packed_object_id(&oid, pack,
843 pack_pos_to_index(pack, pos));
844 die(_("unable to get size of %s"), oid_to_hex(&oid));
847 struct eindex *eindex = &bitmap_git->ext_index;
848 struct object *obj = eindex->objects[pos - pack->num_objects];
849 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
850 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
856 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
857 struct object_list *tip_objects,
858 struct bitmap *to_filter,
861 struct eindex *eindex = &bitmap_git->ext_index;
863 struct ewah_iterator it;
867 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
869 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
870 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
872 eword_t word = to_filter->words[i] & mask;
875 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
878 if ((word >> offset) == 0)
880 offset += ewah_bit_ctz64(word >> offset);
881 pos = i * BITS_IN_EWORD + offset;
883 if (!bitmap_get(tips, pos) &&
884 get_size_by_pos(bitmap_git, pos) >= limit)
885 bitmap_unset(to_filter, pos);
889 for (i = 0; i < eindex->count; i++) {
890 uint32_t pos = i + bitmap_git->pack->num_objects;
891 if (eindex->objects[i]->type == OBJ_BLOB &&
892 bitmap_get(to_filter, pos) &&
893 !bitmap_get(tips, pos) &&
894 get_size_by_pos(bitmap_git, pos) >= limit)
895 bitmap_unset(to_filter, pos);
901 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
902 struct object_list *tip_objects,
903 struct bitmap *to_filter,
907 BUG("filter_bitmap_tree_depth given non-zero limit");
909 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
911 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
915 static int filter_bitmap(struct bitmap_index *bitmap_git,
916 struct object_list *tip_objects,
917 struct bitmap *to_filter,
918 struct list_objects_filter_options *filter)
920 if (!filter || filter->choice == LOFC_DISABLED)
923 if (filter->choice == LOFC_BLOB_NONE) {
925 filter_bitmap_blob_none(bitmap_git, tip_objects,
930 if (filter->choice == LOFC_BLOB_LIMIT) {
932 filter_bitmap_blob_limit(bitmap_git, tip_objects,
934 filter->blob_limit_value);
938 if (filter->choice == LOFC_TREE_DEPTH &&
939 filter->tree_exclude_depth == 0) {
941 filter_bitmap_tree_depth(bitmap_git, tip_objects,
943 filter->tree_exclude_depth);
947 /* filter choice not handled */
951 static int can_filter_bitmap(struct list_objects_filter_options *filter)
953 return !filter_bitmap(NULL, NULL, NULL, filter);
956 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
957 struct list_objects_filter_options *filter)
961 struct object_list *wants = NULL;
962 struct object_list *haves = NULL;
964 struct bitmap *wants_bitmap = NULL;
965 struct bitmap *haves_bitmap = NULL;
967 struct bitmap_index *bitmap_git;
970 * We can't do pathspec limiting with bitmaps, because we don't know
971 * which commits are associated with which object changes (let alone
972 * even which objects are associated with which paths).
977 if (!can_filter_bitmap(filter))
980 /* try to open a bitmapped pack, but don't parse it yet
981 * because we may not need to use it */
982 CALLOC_ARRAY(bitmap_git, 1);
983 if (open_pack_bitmap(revs->repo, bitmap_git) < 0)
986 for (i = 0; i < revs->pending.nr; ++i) {
987 struct object *object = revs->pending.objects[i].item;
989 if (object->type == OBJ_NONE)
990 parse_object_or_die(&object->oid, NULL);
992 while (object->type == OBJ_TAG) {
993 struct tag *tag = (struct tag *) object;
995 if (object->flags & UNINTERESTING)
996 object_list_insert(object, &haves);
998 object_list_insert(object, &wants);
1000 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1001 object->flags |= (tag->object.flags & UNINTERESTING);
1004 if (object->flags & UNINTERESTING)
1005 object_list_insert(object, &haves);
1007 object_list_insert(object, &wants);
1011 * if we have a HAVES list, but none of those haves is contained
1012 * in the packfile that has a bitmap, we don't have anything to
1015 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1018 /* if we don't want anything, we're done here */
1023 * now we're going to use bitmaps, so load the actual bitmap entries
1024 * from disk. this is the point of no return; after this the rev_list
1025 * becomes invalidated and we must perform the revwalk through bitmaps
1027 if (load_pack_bitmap(bitmap_git) < 0)
1030 object_array_clear(&revs->pending);
1033 revs->ignore_missing_links = 1;
1034 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
1036 reset_revision_walk();
1037 revs->ignore_missing_links = 0;
1039 if (haves_bitmap == NULL)
1040 BUG("failed to perform bitmap walk");
1043 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
1047 BUG("failed to perform bitmap walk");
1050 bitmap_and_not(wants_bitmap, haves_bitmap);
1052 filter_bitmap(bitmap_git, wants, wants_bitmap, filter);
1054 bitmap_git->result = wants_bitmap;
1055 bitmap_git->haves = haves_bitmap;
1057 object_list_free(&wants);
1058 object_list_free(&haves);
1063 free_bitmap_index(bitmap_git);
1064 object_list_free(&wants);
1065 object_list_free(&haves);
1069 static void try_partial_reuse(struct bitmap_index *bitmap_git,
1071 struct bitmap *reuse,
1072 struct pack_window **w_curs)
1074 off_t offset, header;
1075 enum object_type type;
1078 if (pos >= bitmap_git->pack->num_objects)
1079 return; /* not actually in the pack */
1081 offset = header = pack_pos_to_offset(bitmap_git->pack, pos);
1082 type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
1084 return; /* broken packfile, punt */
1086 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1091 * Find the position of the base object so we can look it up
1092 * in our bitmaps. If we can't come up with an offset, or if
1093 * that offset is not in the revidx, the pack is corrupt.
1094 * There's nothing we can do, so just punt on this object,
1095 * and the normal slow path will complain about it in
1098 base_offset = get_delta_base(bitmap_git->pack, w_curs,
1099 &offset, type, header);
1102 if (offset_to_pack_pos(bitmap_git->pack, base_offset, &base_pos) < 0)
1106 * We assume delta dependencies always point backwards. This
1107 * lets us do a single pass, and is basically always true
1108 * due to the way OFS_DELTAs work. You would not typically
1109 * find REF_DELTA in a bitmapped pack, since we only bitmap
1110 * packs we write fresh, and OFS_DELTA is the default). But
1111 * let's double check to make sure the pack wasn't written with
1114 if (base_pos >= pos)
1118 * And finally, if we're not sending the base as part of our
1119 * reuse chunk, then don't send this object either. The base
1120 * would come after us, along with other objects not
1121 * necessarily in the pack, which means we'd need to convert
1122 * to REF_DELTA on the fly. Better to just let the normal
1123 * object_entry code path handle it.
1125 if (!bitmap_get(reuse, base_pos))
1130 * If we got here, then the object is OK to reuse. Mark it.
1132 bitmap_set(reuse, pos);
1135 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1136 struct packed_git **packfile_out,
1138 struct bitmap **reuse_out)
1140 struct bitmap *result = bitmap_git->result;
1141 struct bitmap *reuse;
1142 struct pack_window *w_curs = NULL;
1148 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1151 /* Don't mark objects not in the packfile */
1152 if (i > bitmap_git->pack->num_objects / BITS_IN_EWORD)
1153 i = bitmap_git->pack->num_objects / BITS_IN_EWORD;
1155 reuse = bitmap_word_alloc(i);
1156 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1158 for (; i < result->word_alloc; ++i) {
1159 eword_t word = result->words[i];
1160 size_t pos = (i * BITS_IN_EWORD);
1162 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1163 if ((word >> offset) == 0)
1166 offset += ewah_bit_ctz64(word >> offset);
1167 try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs);
1171 unuse_pack(&w_curs);
1173 *entries = bitmap_popcount(reuse);
1180 * Drop any reused objects from the result, since they will not
1181 * need to be handled separately.
1183 bitmap_and_not(result, reuse);
1184 *packfile_out = bitmap_git->pack;
1189 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1190 struct bitmap *bitmap, const struct object_id *oid)
1197 idx = bitmap_position(bitmap_git, oid);
1198 return idx >= 0 && bitmap_get(bitmap, idx);
1201 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1202 struct rev_info *revs,
1203 show_reachable_fn show_reachable)
1205 assert(bitmap_git->result);
1207 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1208 if (revs->tree_objects)
1209 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1210 if (revs->blob_objects)
1211 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1212 if (revs->tag_objects)
1213 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1215 show_extended_objects(bitmap_git, revs, show_reachable);
1218 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1219 enum object_type type)
1221 struct bitmap *objects = bitmap_git->result;
1222 struct eindex *eindex = &bitmap_git->ext_index;
1224 uint32_t i = 0, count = 0;
1225 struct ewah_iterator it;
1228 init_type_iterator(&it, bitmap_git, type);
1230 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1231 eword_t word = objects->words[i++] & filter;
1232 count += ewah_bit_popcount64(word);
1235 for (i = 0; i < eindex->count; ++i) {
1236 if (eindex->objects[i]->type == type &&
1237 bitmap_get(objects, bitmap_git->pack->num_objects + i))
1244 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1245 uint32_t *commits, uint32_t *trees,
1246 uint32_t *blobs, uint32_t *tags)
1248 assert(bitmap_git->result);
1251 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1254 *trees = count_object_type(bitmap_git, OBJ_TREE);
1257 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1260 *tags = count_object_type(bitmap_git, OBJ_TAG);
1263 struct bitmap_test_data {
1264 struct bitmap_index *bitmap_git;
1265 struct bitmap *base;
1266 struct progress *prg;
1270 static void test_show_object(struct object *object, const char *name,
1273 struct bitmap_test_data *tdata = data;
1276 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1278 die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
1280 bitmap_set(tdata->base, bitmap_pos);
1281 display_progress(tdata->prg, ++tdata->seen);
1284 static void test_show_commit(struct commit *commit, void *data)
1286 struct bitmap_test_data *tdata = data;
1289 bitmap_pos = bitmap_position(tdata->bitmap_git,
1290 &commit->object.oid);
1292 die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
1294 bitmap_set(tdata->base, bitmap_pos);
1295 display_progress(tdata->prg, ++tdata->seen);
1298 void test_bitmap_walk(struct rev_info *revs)
1300 struct object *root;
1301 struct bitmap *result = NULL;
1302 size_t result_popcnt;
1303 struct bitmap_test_data tdata;
1304 struct bitmap_index *bitmap_git;
1305 struct ewah_bitmap *bm;
1307 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1308 die("failed to load bitmap indexes");
1310 if (revs->pending.nr != 1)
1311 die("you must specify exactly one commit to test");
1313 fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
1314 bitmap_git->version, bitmap_git->entry_count);
1316 root = revs->pending.objects[0].item;
1317 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
1320 fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
1321 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
1323 result = ewah_to_bitmap(bm);
1327 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
1329 revs->tag_objects = 1;
1330 revs->tree_objects = 1;
1331 revs->blob_objects = 1;
1333 result_popcnt = bitmap_popcount(result);
1335 if (prepare_revision_walk(revs))
1336 die("revision walk setup failed");
1338 tdata.bitmap_git = bitmap_git;
1339 tdata.base = bitmap_new();
1340 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1343 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1345 stop_progress(&tdata.prg);
1347 if (bitmap_equals(result, tdata.base))
1348 fprintf(stderr, "OK!\n");
1350 die("mismatch in bitmap results");
1352 free_bitmap_index(bitmap_git);
1355 int test_bitmap_commits(struct repository *r)
1357 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1358 struct object_id oid;
1359 MAYBE_UNUSED void *value;
1362 die("failed to load bitmap indexes");
1364 kh_foreach(bitmap_git->bitmaps, oid, value, {
1365 printf("%s\n", oid_to_hex(&oid));
1368 free_bitmap_index(bitmap_git);
1373 int rebuild_bitmap(const uint32_t *reposition,
1374 struct ewah_bitmap *source,
1375 struct bitmap *dest)
1378 struct ewah_iterator it;
1381 ewah_iterator_init(&it, source);
1383 while (ewah_iterator_next(&word, &it)) {
1384 uint32_t offset, bit_pos;
1386 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1387 if ((word >> offset) == 0)
1390 offset += ewah_bit_ctz64(word >> offset);
1392 bit_pos = reposition[pos + offset];
1394 bitmap_set(dest, bit_pos - 1);
1395 else /* can't reuse, we don't have the object */
1399 pos += BITS_IN_EWORD;
1404 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1405 struct packing_data *mapping)
1407 uint32_t i, num_objects;
1408 uint32_t *reposition;
1410 num_objects = bitmap_git->pack->num_objects;
1411 CALLOC_ARRAY(reposition, num_objects);
1413 for (i = 0; i < num_objects; ++i) {
1414 struct object_id oid;
1415 struct object_entry *oe;
1417 nth_packed_object_id(&oid, bitmap_git->pack,
1418 pack_pos_to_index(bitmap_git->pack, i));
1419 oe = packlist_find(mapping, &oid);
1422 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
1428 void free_bitmap_index(struct bitmap_index *b)
1434 munmap(b->map, b->map_size);
1435 ewah_pool_free(b->commits);
1436 ewah_pool_free(b->trees);
1437 ewah_pool_free(b->blobs);
1438 ewah_pool_free(b->tags);
1439 kh_destroy_oid_map(b->bitmaps);
1440 free(b->ext_index.objects);
1441 free(b->ext_index.hashes);
1442 bitmap_free(b->result);
1443 bitmap_free(b->haves);
1447 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1448 const struct object_id *oid)
1450 return bitmap_git &&
1451 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
1454 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1455 enum object_type object_type)
1457 struct bitmap *result = bitmap_git->result;
1458 struct packed_git *pack = bitmap_git->pack;
1460 struct ewah_iterator it;
1464 init_type_iterator(&it, bitmap_git, object_type);
1465 for (i = 0; i < result->word_alloc &&
1466 ewah_iterator_next(&filter, &it); i++) {
1467 eword_t word = result->words[i] & filter;
1468 size_t base = (i * BITS_IN_EWORD);
1474 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1477 if ((word >> offset) == 0)
1480 offset += ewah_bit_ctz64(word >> offset);
1481 pos = base + offset;
1482 total += pack_pos_to_offset(pack, pos + 1) -
1483 pack_pos_to_offset(pack, pos);
1490 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1492 struct bitmap *result = bitmap_git->result;
1493 struct packed_git *pack = bitmap_git->pack;
1494 struct eindex *eindex = &bitmap_git->ext_index;
1496 struct object_info oi = OBJECT_INFO_INIT;
1500 oi.disk_sizep = &object_size;
1502 for (i = 0; i < eindex->count; i++) {
1503 struct object *obj = eindex->objects[i];
1505 if (!bitmap_get(result, pack->num_objects + i))
1508 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1509 die(_("unable to get disk usage of %s"),
1510 oid_to_hex(&obj->oid));
1512 total += object_size;
1517 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
1518 struct rev_info *revs)
1522 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
1523 if (revs->tree_objects)
1524 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
1525 if (revs->blob_objects)
1526 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
1527 if (revs->tag_objects)
1528 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
1530 total += get_disk_usage_for_extended(bitmap_git);
1535 const struct string_list *bitmap_preferred_tips(struct repository *r)
1537 return repo_config_get_value_multi(r, "pack.preferbitmaptips");