Sync with 2.34.4
[git/debian.git] / pack-bitmap.c
blobf772d3cb7f7c181a9b703a9535f9ec55dfd60513
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "progress.h"
7 #include "list-objects.h"
8 #include "pack.h"
9 #include "pack-bitmap.h"
10 #include "pack-revindex.h"
11 #include "pack-objects.h"
12 #include "packfile.h"
13 #include "repository.h"
14 #include "object-store.h"
15 #include "list-objects-filter-options.h"
16 #include "midx.h"
17 #include "config.h"
20 * An entry on the bitmap index, representing the bitmap for a given
21 * commit.
23 struct stored_bitmap {
24 struct object_id oid;
25 struct ewah_bitmap *root;
26 struct stored_bitmap *xor;
27 int flags;
31 * The active bitmap index for a repository. By design, repositories only have
32 * a single bitmap index available (the index for the biggest packfile in
33 * the repository), since bitmap indexes need full closure.
35 * If there is more than one bitmap index available (e.g. because of alternates),
36 * the active bitmap index is the largest one.
38 struct bitmap_index {
40 * The pack or multi-pack index (MIDX) that this bitmap index belongs
41 * to.
43 * Exactly one of these must be non-NULL; this specifies the object
44 * order used to interpret this bitmap.
46 struct packed_git *pack;
47 struct multi_pack_index *midx;
50 * Mark the first `reuse_objects` in the packfile as reused:
51 * they will be sent as-is without using them for repacking
52 * calculations
54 uint32_t reuse_objects;
56 /* mmapped buffer of the whole bitmap index */
57 unsigned char *map;
58 size_t map_size; /* size of the mmaped buffer */
59 size_t map_pos; /* current position when loading the index */
62 * Type indexes.
64 * Each bitmap marks which objects in the packfile are of the given
65 * type. This provides type information when yielding the objects from
66 * the packfile during a walk, which allows for better delta bases.
68 struct ewah_bitmap *commits;
69 struct ewah_bitmap *trees;
70 struct ewah_bitmap *blobs;
71 struct ewah_bitmap *tags;
73 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
74 kh_oid_map_t *bitmaps;
76 /* Number of bitmapped commits */
77 uint32_t entry_count;
79 /* If not NULL, this is a name-hash cache pointing into map. */
80 uint32_t *hashes;
82 /* The checksum of the packfile or MIDX; points into map. */
83 const unsigned char *checksum;
86 * Extended index.
88 * When trying to perform bitmap operations with objects that are not
89 * packed in `pack`, these objects are added to this "fake index" and
90 * are assumed to appear at the end of the packfile for all operations
92 struct eindex {
93 struct object **objects;
94 uint32_t *hashes;
95 uint32_t count, alloc;
96 kh_oid_pos_t *positions;
97 } ext_index;
99 /* Bitmap result of the last performed walk */
100 struct bitmap *result;
102 /* "have" bitmap from the last performed walk */
103 struct bitmap *haves;
105 /* Version of the bitmap index */
106 unsigned int version;
109 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
111 struct ewah_bitmap *parent;
112 struct ewah_bitmap *composed;
114 if (st->xor == NULL)
115 return st->root;
117 composed = ewah_pool_new();
118 parent = lookup_stored_bitmap(st->xor);
119 ewah_xor(st->root, parent, composed);
121 ewah_pool_free(st->root);
122 st->root = composed;
123 st->xor = NULL;
125 return composed;
129 * Read a bitmap from the current read position on the mmaped
130 * index, and increase the read position accordingly
132 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
134 struct ewah_bitmap *b = ewah_pool_new();
136 ssize_t bitmap_size = ewah_read_mmap(b,
137 index->map + index->map_pos,
138 index->map_size - index->map_pos);
140 if (bitmap_size < 0) {
141 error("Failed to load bitmap index (corrupted?)");
142 ewah_pool_free(b);
143 return NULL;
146 index->map_pos += bitmap_size;
147 return b;
150 static uint32_t bitmap_num_objects(struct bitmap_index *index)
152 if (index->midx)
153 return index->midx->num_objects;
154 return index->pack->num_objects;
157 static int load_bitmap_header(struct bitmap_index *index)
159 struct bitmap_disk_header *header = (void *)index->map;
160 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
162 if (index->map_size < header_size + the_hash_algo->rawsz)
163 return error("Corrupted bitmap index (too small)");
165 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
166 return error("Corrupted bitmap index file (wrong header)");
168 index->version = ntohs(header->version);
169 if (index->version != 1)
170 return error("Unsupported version for bitmap index file (%d)", index->version);
172 /* Parse known bitmap format options */
174 uint32_t flags = ntohs(header->options);
175 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
176 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
178 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
179 return error("Unsupported options for bitmap index file "
180 "(Git requires BITMAP_OPT_FULL_DAG)");
182 if (flags & BITMAP_OPT_HASH_CACHE) {
183 if (cache_size > index_end - index->map - header_size)
184 return error("corrupted bitmap index file (too short to fit hash cache)");
185 index->hashes = (void *)(index_end - cache_size);
186 index_end -= cache_size;
190 index->entry_count = ntohl(header->entry_count);
191 index->checksum = header->checksum;
192 index->map_pos += header_size;
193 return 0;
196 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
197 struct ewah_bitmap *root,
198 const struct object_id *oid,
199 struct stored_bitmap *xor_with,
200 int flags)
202 struct stored_bitmap *stored;
203 khiter_t hash_pos;
204 int ret;
206 stored = xmalloc(sizeof(struct stored_bitmap));
207 stored->root = root;
208 stored->xor = xor_with;
209 stored->flags = flags;
210 oidcpy(&stored->oid, oid);
212 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
214 /* a 0 return code means the insertion succeeded with no changes,
215 * because the SHA1 already existed on the map. this is bad, there
216 * shouldn't be duplicated commits in the index */
217 if (ret == 0) {
218 error("Duplicate entry in bitmap index: %s", oid_to_hex(oid));
219 return NULL;
222 kh_value(index->bitmaps, hash_pos) = stored;
223 return stored;
226 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
228 uint32_t result = get_be32(buffer + *pos);
229 (*pos) += sizeof(result);
230 return result;
233 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
235 return buffer[(*pos)++];
238 #define MAX_XOR_OFFSET 160
240 static int nth_bitmap_object_oid(struct bitmap_index *index,
241 struct object_id *oid,
242 uint32_t n)
244 if (index->midx)
245 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
246 return nth_packed_object_id(oid, index->pack, n);
249 static int load_bitmap_entries_v1(struct bitmap_index *index)
251 uint32_t i;
252 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
254 for (i = 0; i < index->entry_count; ++i) {
255 int xor_offset, flags;
256 struct ewah_bitmap *bitmap = NULL;
257 struct stored_bitmap *xor_bitmap = NULL;
258 uint32_t commit_idx_pos;
259 struct object_id oid;
261 if (index->map_size - index->map_pos < 6)
262 return error("corrupt ewah bitmap: truncated header for entry %d", i);
264 commit_idx_pos = read_be32(index->map, &index->map_pos);
265 xor_offset = read_u8(index->map, &index->map_pos);
266 flags = read_u8(index->map, &index->map_pos);
268 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
269 return error("corrupt ewah bitmap: commit index %u out of range",
270 (unsigned)commit_idx_pos);
272 bitmap = read_bitmap_1(index);
273 if (!bitmap)
274 return -1;
276 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
277 return error("Corrupted bitmap pack index");
279 if (xor_offset > 0) {
280 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
282 if (xor_bitmap == NULL)
283 return error("Invalid XOR offset in bitmap pack index");
286 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
287 index, bitmap, &oid, xor_bitmap, flags);
290 return 0;
293 char *midx_bitmap_filename(struct multi_pack_index *midx)
295 struct strbuf buf = STRBUF_INIT;
297 get_midx_filename(&buf, midx->object_dir);
298 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
300 return strbuf_detach(&buf, NULL);
303 char *pack_bitmap_filename(struct packed_git *p)
305 size_t len;
307 if (!strip_suffix(p->pack_name, ".pack", &len))
308 BUG("pack_name does not end in .pack");
309 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
312 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
313 struct multi_pack_index *midx)
315 struct stat st;
316 char *idx_name = midx_bitmap_filename(midx);
317 int fd = git_open(idx_name);
319 free(idx_name);
321 if (fd < 0)
322 return -1;
324 if (fstat(fd, &st)) {
325 close(fd);
326 return -1;
329 if (bitmap_git->pack || bitmap_git->midx) {
330 struct strbuf buf = STRBUF_INIT;
331 get_midx_filename(&buf, midx->object_dir);
332 /* ignore extra bitmap file; we can only handle one */
333 warning("ignoring extra bitmap file: %s", buf.buf);
334 close(fd);
335 strbuf_release(&buf);
336 return -1;
339 bitmap_git->midx = midx;
340 bitmap_git->map_size = xsize_t(st.st_size);
341 bitmap_git->map_pos = 0;
342 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
343 MAP_PRIVATE, fd, 0);
344 close(fd);
346 if (load_bitmap_header(bitmap_git) < 0)
347 goto cleanup;
349 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum))
350 goto cleanup;
352 if (load_midx_revindex(bitmap_git->midx) < 0) {
353 warning(_("multi-pack bitmap is missing required reverse index"));
354 goto cleanup;
356 return 0;
358 cleanup:
359 munmap(bitmap_git->map, bitmap_git->map_size);
360 bitmap_git->map_size = 0;
361 bitmap_git->map = NULL;
362 return -1;
365 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
367 int fd;
368 struct stat st;
369 char *idx_name;
371 if (open_pack_index(packfile))
372 return -1;
374 idx_name = pack_bitmap_filename(packfile);
375 fd = git_open(idx_name);
376 free(idx_name);
378 if (fd < 0)
379 return -1;
381 if (fstat(fd, &st)) {
382 close(fd);
383 return -1;
386 if (bitmap_git->pack || bitmap_git->midx) {
387 /* ignore extra bitmap file; we can only handle one */
388 warning("ignoring extra bitmap file: %s", packfile->pack_name);
389 close(fd);
390 return -1;
393 if (!is_pack_valid(packfile)) {
394 close(fd);
395 return -1;
398 bitmap_git->pack = packfile;
399 bitmap_git->map_size = xsize_t(st.st_size);
400 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
401 bitmap_git->map_pos = 0;
402 close(fd);
404 if (load_bitmap_header(bitmap_git) < 0) {
405 munmap(bitmap_git->map, bitmap_git->map_size);
406 bitmap_git->map = NULL;
407 bitmap_git->map_size = 0;
408 return -1;
411 return 0;
414 static int load_reverse_index(struct bitmap_index *bitmap_git)
416 if (bitmap_is_midx(bitmap_git)) {
417 uint32_t i;
418 int ret;
421 * The multi-pack-index's .rev file is already loaded via
422 * open_pack_bitmap_1().
424 * But we still need to open the individual pack .rev files,
425 * since we will need to make use of them in pack-objects.
427 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
428 if (prepare_midx_pack(the_repository, bitmap_git->midx, i))
429 die(_("load_reverse_index: could not open pack"));
430 ret = load_pack_revindex(bitmap_git->midx->packs[i]);
431 if (ret)
432 return ret;
434 return 0;
436 return load_pack_revindex(bitmap_git->pack);
439 static int load_bitmap(struct bitmap_index *bitmap_git)
441 assert(bitmap_git->map);
443 bitmap_git->bitmaps = kh_init_oid_map();
444 bitmap_git->ext_index.positions = kh_init_oid_pos();
446 if (load_reverse_index(bitmap_git))
447 goto failed;
449 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
450 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
451 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
452 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
453 goto failed;
455 if (load_bitmap_entries_v1(bitmap_git) < 0)
456 goto failed;
458 return 0;
460 failed:
461 munmap(bitmap_git->map, bitmap_git->map_size);
462 bitmap_git->map = NULL;
463 bitmap_git->map_size = 0;
465 kh_destroy_oid_map(bitmap_git->bitmaps);
466 bitmap_git->bitmaps = NULL;
468 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
469 bitmap_git->ext_index.positions = NULL;
471 return -1;
474 static int open_pack_bitmap(struct repository *r,
475 struct bitmap_index *bitmap_git)
477 struct packed_git *p;
478 int ret = -1;
480 assert(!bitmap_git->map);
482 for (p = get_all_packs(r); p; p = p->next) {
483 if (open_pack_bitmap_1(bitmap_git, p) == 0)
484 ret = 0;
487 return ret;
490 static int open_midx_bitmap(struct repository *r,
491 struct bitmap_index *bitmap_git)
493 struct multi_pack_index *midx;
495 assert(!bitmap_git->map);
497 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
498 if (!open_midx_bitmap_1(bitmap_git, midx))
499 return 0;
501 return -1;
504 static int open_bitmap(struct repository *r,
505 struct bitmap_index *bitmap_git)
507 assert(!bitmap_git->map);
509 if (!open_midx_bitmap(r, bitmap_git))
510 return 0;
511 return open_pack_bitmap(r, bitmap_git);
514 struct bitmap_index *prepare_bitmap_git(struct repository *r)
516 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
518 if (!open_bitmap(r, bitmap_git) && !load_bitmap(bitmap_git))
519 return bitmap_git;
521 free_bitmap_index(bitmap_git);
522 return NULL;
525 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
527 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
529 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(bitmap_git))
530 return bitmap_git;
532 free_bitmap_index(bitmap_git);
533 return NULL;
536 struct include_data {
537 struct bitmap_index *bitmap_git;
538 struct bitmap *base;
539 struct bitmap *seen;
542 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
543 struct commit *commit)
545 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
546 commit->object.oid);
547 if (hash_pos >= kh_end(bitmap_git->bitmaps))
548 return NULL;
549 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
552 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
553 const struct object_id *oid)
555 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
556 khiter_t pos = kh_get_oid_pos(positions, *oid);
558 if (pos < kh_end(positions)) {
559 int bitmap_pos = kh_value(positions, pos);
560 return bitmap_pos + bitmap_num_objects(bitmap_git);
563 return -1;
566 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
567 const struct object_id *oid)
569 uint32_t pos;
570 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
571 if (!offset)
572 return -1;
574 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
575 return -1;
576 return pos;
579 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
580 const struct object_id *oid)
582 uint32_t want, got;
583 if (!bsearch_midx(oid, bitmap_git->midx, &want))
584 return -1;
586 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
587 return -1;
588 return got;
591 static int bitmap_position(struct bitmap_index *bitmap_git,
592 const struct object_id *oid)
594 int pos;
595 if (bitmap_is_midx(bitmap_git))
596 pos = bitmap_position_midx(bitmap_git, oid);
597 else
598 pos = bitmap_position_packfile(bitmap_git, oid);
599 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
602 static int ext_index_add_object(struct bitmap_index *bitmap_git,
603 struct object *object, const char *name)
605 struct eindex *eindex = &bitmap_git->ext_index;
607 khiter_t hash_pos;
608 int hash_ret;
609 int bitmap_pos;
611 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
612 if (hash_ret > 0) {
613 if (eindex->count >= eindex->alloc) {
614 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
615 REALLOC_ARRAY(eindex->objects, eindex->alloc);
616 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
619 bitmap_pos = eindex->count;
620 eindex->objects[eindex->count] = object;
621 eindex->hashes[eindex->count] = pack_name_hash(name);
622 kh_value(eindex->positions, hash_pos) = bitmap_pos;
623 eindex->count++;
624 } else {
625 bitmap_pos = kh_value(eindex->positions, hash_pos);
628 return bitmap_pos + bitmap_num_objects(bitmap_git);
631 struct bitmap_show_data {
632 struct bitmap_index *bitmap_git;
633 struct bitmap *base;
636 static void show_object(struct object *object, const char *name, void *data_)
638 struct bitmap_show_data *data = data_;
639 int bitmap_pos;
641 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
643 if (bitmap_pos < 0)
644 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
645 name);
647 bitmap_set(data->base, bitmap_pos);
650 static void show_commit(struct commit *commit, void *data)
654 static int add_to_include_set(struct bitmap_index *bitmap_git,
655 struct include_data *data,
656 struct commit *commit,
657 int bitmap_pos)
659 struct ewah_bitmap *partial;
661 if (data->seen && bitmap_get(data->seen, bitmap_pos))
662 return 0;
664 if (bitmap_get(data->base, bitmap_pos))
665 return 0;
667 partial = bitmap_for_commit(bitmap_git, commit);
668 if (partial) {
669 bitmap_or_ewah(data->base, partial);
670 return 0;
673 bitmap_set(data->base, bitmap_pos);
674 return 1;
677 static int should_include(struct commit *commit, void *_data)
679 struct include_data *data = _data;
680 int bitmap_pos;
682 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
683 if (bitmap_pos < 0)
684 bitmap_pos = ext_index_add_object(data->bitmap_git,
685 (struct object *)commit,
686 NULL);
688 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
689 struct commit_list *parent = commit->parents;
691 while (parent) {
692 parent->item->object.flags |= SEEN;
693 parent = parent->next;
696 return 0;
699 return 1;
702 static int should_include_obj(struct object *obj, void *_data)
704 struct include_data *data = _data;
705 int bitmap_pos;
707 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
708 if (bitmap_pos < 0)
709 return 1;
710 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
711 bitmap_get(data->base, bitmap_pos)) {
712 obj->flags |= SEEN;
713 return 0;
715 return 1;
718 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
719 struct bitmap **base,
720 struct commit *commit)
722 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
724 if (!or_with)
725 return 0;
727 if (*base == NULL)
728 *base = ewah_to_bitmap(or_with);
729 else
730 bitmap_or_ewah(*base, or_with);
732 return 1;
735 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
736 struct rev_info *revs,
737 struct object_list *roots,
738 struct bitmap *seen,
739 struct list_objects_filter_options *filter)
741 struct bitmap *base = NULL;
742 int needs_walk = 0;
744 struct object_list *not_mapped = NULL;
747 * Go through all the roots for the walk. The ones that have bitmaps
748 * on the bitmap index will be `or`ed together to form an initial
749 * global reachability analysis.
751 * The ones without bitmaps in the index will be stored in the
752 * `not_mapped_list` for further processing.
754 while (roots) {
755 struct object *object = roots->item;
756 roots = roots->next;
758 if (object->type == OBJ_COMMIT &&
759 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
760 object->flags |= SEEN;
761 continue;
764 object_list_insert(object, &not_mapped);
768 * Best case scenario: We found bitmaps for all the roots,
769 * so the resulting `or` bitmap has the full reachability analysis
771 if (not_mapped == NULL)
772 return base;
774 roots = not_mapped;
777 * Let's iterate through all the roots that don't have bitmaps to
778 * check if we can determine them to be reachable from the existing
779 * global bitmap.
781 * If we cannot find them in the existing global bitmap, we'll need
782 * to push them to an actual walk and run it until we can confirm
783 * they are reachable
785 while (roots) {
786 struct object *object = roots->item;
787 int pos;
789 roots = roots->next;
790 pos = bitmap_position(bitmap_git, &object->oid);
792 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
793 object->flags &= ~UNINTERESTING;
794 add_pending_object(revs, object, "");
795 needs_walk = 1;
796 } else {
797 object->flags |= SEEN;
801 if (needs_walk) {
802 struct include_data incdata;
803 struct bitmap_show_data show_data;
805 if (base == NULL)
806 base = bitmap_new();
808 incdata.bitmap_git = bitmap_git;
809 incdata.base = base;
810 incdata.seen = seen;
812 revs->include_check = should_include;
813 revs->include_check_obj = should_include_obj;
814 revs->include_check_data = &incdata;
816 if (prepare_revision_walk(revs))
817 die("revision walk setup failed");
819 show_data.bitmap_git = bitmap_git;
820 show_data.base = base;
822 traverse_commit_list_filtered(filter, revs,
823 show_commit, show_object,
824 &show_data, NULL);
826 revs->include_check = NULL;
827 revs->include_check_obj = NULL;
828 revs->include_check_data = NULL;
831 return base;
834 static void show_extended_objects(struct bitmap_index *bitmap_git,
835 struct rev_info *revs,
836 show_reachable_fn show_reach)
838 struct bitmap *objects = bitmap_git->result;
839 struct eindex *eindex = &bitmap_git->ext_index;
840 uint32_t i;
842 for (i = 0; i < eindex->count; ++i) {
843 struct object *obj;
845 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
846 continue;
848 obj = eindex->objects[i];
849 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
850 (obj->type == OBJ_TREE && !revs->tree_objects) ||
851 (obj->type == OBJ_TAG && !revs->tag_objects))
852 continue;
854 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
858 static void init_type_iterator(struct ewah_iterator *it,
859 struct bitmap_index *bitmap_git,
860 enum object_type type)
862 switch (type) {
863 case OBJ_COMMIT:
864 ewah_iterator_init(it, bitmap_git->commits);
865 break;
867 case OBJ_TREE:
868 ewah_iterator_init(it, bitmap_git->trees);
869 break;
871 case OBJ_BLOB:
872 ewah_iterator_init(it, bitmap_git->blobs);
873 break;
875 case OBJ_TAG:
876 ewah_iterator_init(it, bitmap_git->tags);
877 break;
879 default:
880 BUG("object type %d not stored by bitmap type index", type);
881 break;
885 static void show_objects_for_type(
886 struct bitmap_index *bitmap_git,
887 enum object_type object_type,
888 show_reachable_fn show_reach)
890 size_t i = 0;
891 uint32_t offset;
893 struct ewah_iterator it;
894 eword_t filter;
896 struct bitmap *objects = bitmap_git->result;
898 init_type_iterator(&it, bitmap_git, object_type);
900 for (i = 0; i < objects->word_alloc &&
901 ewah_iterator_next(&filter, &it); i++) {
902 eword_t word = objects->words[i] & filter;
903 size_t pos = (i * BITS_IN_EWORD);
905 if (!word)
906 continue;
908 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
909 struct packed_git *pack;
910 struct object_id oid;
911 uint32_t hash = 0, index_pos;
912 off_t ofs;
914 if ((word >> offset) == 0)
915 break;
917 offset += ewah_bit_ctz64(word >> offset);
919 if (bitmap_is_midx(bitmap_git)) {
920 struct multi_pack_index *m = bitmap_git->midx;
921 uint32_t pack_id;
923 index_pos = pack_pos_to_midx(m, pos + offset);
924 ofs = nth_midxed_offset(m, index_pos);
925 nth_midxed_object_oid(&oid, m, index_pos);
927 pack_id = nth_midxed_pack_int_id(m, index_pos);
928 pack = bitmap_git->midx->packs[pack_id];
929 } else {
930 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
931 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
932 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
934 pack = bitmap_git->pack;
937 if (bitmap_git->hashes)
938 hash = get_be32(bitmap_git->hashes + index_pos);
940 show_reach(&oid, object_type, 0, hash, pack, ofs);
945 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
946 struct object_list *roots)
948 while (roots) {
949 struct object *object = roots->item;
950 roots = roots->next;
952 if (bitmap_is_midx(bitmap_git)) {
953 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
954 return 1;
955 } else {
956 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
957 return 1;
961 return 0;
964 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
965 struct object_list *tip_objects,
966 enum object_type type)
968 struct bitmap *result = bitmap_new();
969 struct object_list *p;
971 for (p = tip_objects; p; p = p->next) {
972 int pos;
974 if (p->item->type != type)
975 continue;
977 pos = bitmap_position(bitmap_git, &p->item->oid);
978 if (pos < 0)
979 continue;
981 bitmap_set(result, pos);
984 return result;
987 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
988 struct object_list *tip_objects,
989 struct bitmap *to_filter,
990 enum object_type type)
992 struct eindex *eindex = &bitmap_git->ext_index;
993 struct bitmap *tips;
994 struct ewah_iterator it;
995 eword_t mask;
996 uint32_t i;
999 * The non-bitmap version of this filter never removes
1000 * objects which the other side specifically asked for,
1001 * so we must match that behavior.
1003 tips = find_tip_objects(bitmap_git, tip_objects, type);
1006 * We can use the type-level bitmap for 'type' to work in whole
1007 * words for the objects that are actually in the bitmapped
1008 * packfile.
1010 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1011 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1012 i++) {
1013 if (i < tips->word_alloc)
1014 mask &= ~tips->words[i];
1015 to_filter->words[i] &= ~mask;
1019 * Clear any objects that weren't in the packfile (and so would
1020 * not have been caught by the loop above. We'll have to check
1021 * them individually.
1023 for (i = 0; i < eindex->count; i++) {
1024 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1025 if (eindex->objects[i]->type == type &&
1026 bitmap_get(to_filter, pos) &&
1027 !bitmap_get(tips, pos))
1028 bitmap_unset(to_filter, pos);
1031 bitmap_free(tips);
1034 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1035 struct object_list *tip_objects,
1036 struct bitmap *to_filter)
1038 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1039 OBJ_BLOB);
1042 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1043 uint32_t pos)
1045 unsigned long size;
1046 struct object_info oi = OBJECT_INFO_INIT;
1048 oi.sizep = &size;
1050 if (pos < bitmap_num_objects(bitmap_git)) {
1051 struct packed_git *pack;
1052 off_t ofs;
1054 if (bitmap_is_midx(bitmap_git)) {
1055 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1056 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1058 pack = bitmap_git->midx->packs[pack_id];
1059 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1060 } else {
1061 pack = bitmap_git->pack;
1062 ofs = pack_pos_to_offset(pack, pos);
1065 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1066 struct object_id oid;
1067 nth_bitmap_object_oid(bitmap_git, &oid,
1068 pack_pos_to_index(pack, pos));
1069 die(_("unable to get size of %s"), oid_to_hex(&oid));
1071 } else {
1072 struct eindex *eindex = &bitmap_git->ext_index;
1073 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1074 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1075 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1078 return size;
1081 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1082 struct object_list *tip_objects,
1083 struct bitmap *to_filter,
1084 unsigned long limit)
1086 struct eindex *eindex = &bitmap_git->ext_index;
1087 struct bitmap *tips;
1088 struct ewah_iterator it;
1089 eword_t mask;
1090 uint32_t i;
1092 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1094 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1095 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1096 i++) {
1097 eword_t word = to_filter->words[i] & mask;
1098 unsigned offset;
1100 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1101 uint32_t pos;
1103 if ((word >> offset) == 0)
1104 break;
1105 offset += ewah_bit_ctz64(word >> offset);
1106 pos = i * BITS_IN_EWORD + offset;
1108 if (!bitmap_get(tips, pos) &&
1109 get_size_by_pos(bitmap_git, pos) >= limit)
1110 bitmap_unset(to_filter, pos);
1114 for (i = 0; i < eindex->count; i++) {
1115 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1116 if (eindex->objects[i]->type == OBJ_BLOB &&
1117 bitmap_get(to_filter, pos) &&
1118 !bitmap_get(tips, pos) &&
1119 get_size_by_pos(bitmap_git, pos) >= limit)
1120 bitmap_unset(to_filter, pos);
1123 bitmap_free(tips);
1126 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1127 struct object_list *tip_objects,
1128 struct bitmap *to_filter,
1129 unsigned long limit)
1131 if (limit)
1132 BUG("filter_bitmap_tree_depth given non-zero limit");
1134 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1135 OBJ_TREE);
1136 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1137 OBJ_BLOB);
1140 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1141 struct object_list *tip_objects,
1142 struct bitmap *to_filter,
1143 enum object_type object_type)
1145 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1146 BUG("filter_bitmap_object_type given invalid object");
1148 if (object_type != OBJ_TAG)
1149 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1150 if (object_type != OBJ_COMMIT)
1151 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1152 if (object_type != OBJ_TREE)
1153 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1154 if (object_type != OBJ_BLOB)
1155 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1158 static int filter_bitmap(struct bitmap_index *bitmap_git,
1159 struct object_list *tip_objects,
1160 struct bitmap *to_filter,
1161 struct list_objects_filter_options *filter)
1163 if (!filter || filter->choice == LOFC_DISABLED)
1164 return 0;
1166 if (filter->choice == LOFC_BLOB_NONE) {
1167 if (bitmap_git)
1168 filter_bitmap_blob_none(bitmap_git, tip_objects,
1169 to_filter);
1170 return 0;
1173 if (filter->choice == LOFC_BLOB_LIMIT) {
1174 if (bitmap_git)
1175 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1176 to_filter,
1177 filter->blob_limit_value);
1178 return 0;
1181 if (filter->choice == LOFC_TREE_DEPTH &&
1182 filter->tree_exclude_depth == 0) {
1183 if (bitmap_git)
1184 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1185 to_filter,
1186 filter->tree_exclude_depth);
1187 return 0;
1190 if (filter->choice == LOFC_OBJECT_TYPE) {
1191 if (bitmap_git)
1192 filter_bitmap_object_type(bitmap_git, tip_objects,
1193 to_filter,
1194 filter->object_type);
1195 return 0;
1198 if (filter->choice == LOFC_COMBINE) {
1199 int i;
1200 for (i = 0; i < filter->sub_nr; i++) {
1201 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1202 &filter->sub[i]) < 0)
1203 return -1;
1205 return 0;
1208 /* filter choice not handled */
1209 return -1;
1212 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1214 return !filter_bitmap(NULL, NULL, NULL, filter);
1217 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1218 struct list_objects_filter_options *filter,
1219 int filter_provided_objects)
1221 unsigned int i;
1223 struct object_list *wants = NULL;
1224 struct object_list *haves = NULL;
1226 struct bitmap *wants_bitmap = NULL;
1227 struct bitmap *haves_bitmap = NULL;
1229 struct bitmap_index *bitmap_git;
1232 * We can't do pathspec limiting with bitmaps, because we don't know
1233 * which commits are associated with which object changes (let alone
1234 * even which objects are associated with which paths).
1236 if (revs->prune)
1237 return NULL;
1239 if (!can_filter_bitmap(filter))
1240 return NULL;
1242 /* try to open a bitmapped pack, but don't parse it yet
1243 * because we may not need to use it */
1244 CALLOC_ARRAY(bitmap_git, 1);
1245 if (open_bitmap(revs->repo, bitmap_git) < 0)
1246 goto cleanup;
1248 for (i = 0; i < revs->pending.nr; ++i) {
1249 struct object *object = revs->pending.objects[i].item;
1251 if (object->type == OBJ_NONE)
1252 parse_object_or_die(&object->oid, NULL);
1254 while (object->type == OBJ_TAG) {
1255 struct tag *tag = (struct tag *) object;
1257 if (object->flags & UNINTERESTING)
1258 object_list_insert(object, &haves);
1259 else
1260 object_list_insert(object, &wants);
1262 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1263 object->flags |= (tag->object.flags & UNINTERESTING);
1266 if (object->flags & UNINTERESTING)
1267 object_list_insert(object, &haves);
1268 else
1269 object_list_insert(object, &wants);
1273 * if we have a HAVES list, but none of those haves is contained
1274 * in the packfile that has a bitmap, we don't have anything to
1275 * optimize here
1277 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1278 goto cleanup;
1280 /* if we don't want anything, we're done here */
1281 if (!wants)
1282 goto cleanup;
1285 * now we're going to use bitmaps, so load the actual bitmap entries
1286 * from disk. this is the point of no return; after this the rev_list
1287 * becomes invalidated and we must perform the revwalk through bitmaps
1289 if (load_bitmap(bitmap_git) < 0)
1290 goto cleanup;
1292 object_array_clear(&revs->pending);
1294 if (haves) {
1295 revs->ignore_missing_links = 1;
1296 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
1297 filter);
1298 reset_revision_walk();
1299 revs->ignore_missing_links = 0;
1301 if (haves_bitmap == NULL)
1302 BUG("failed to perform bitmap walk");
1305 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
1306 filter);
1308 if (!wants_bitmap)
1309 BUG("failed to perform bitmap walk");
1311 if (haves_bitmap)
1312 bitmap_and_not(wants_bitmap, haves_bitmap);
1314 filter_bitmap(bitmap_git, (filter && filter_provided_objects) ? NULL : wants,
1315 wants_bitmap, filter);
1317 bitmap_git->result = wants_bitmap;
1318 bitmap_git->haves = haves_bitmap;
1320 object_list_free(&wants);
1321 object_list_free(&haves);
1323 return bitmap_git;
1325 cleanup:
1326 free_bitmap_index(bitmap_git);
1327 object_list_free(&wants);
1328 object_list_free(&haves);
1329 return NULL;
1333 * -1 means "stop trying further objects"; 0 means we may or may not have
1334 * reused, but you can keep feeding bits.
1336 static int try_partial_reuse(struct packed_git *pack,
1337 size_t pos,
1338 struct bitmap *reuse,
1339 struct pack_window **w_curs)
1341 off_t offset, delta_obj_offset;
1342 enum object_type type;
1343 unsigned long size;
1346 * try_partial_reuse() is called either on (a) objects in the
1347 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1348 * objects in the preferred pack of a multi-pack bitmap.
1349 * Importantly, the latter can pretend as if only a single pack
1350 * exists because:
1352 * - The first pack->num_objects bits of a MIDX bitmap are
1353 * reserved for the preferred pack, and
1355 * - Ties due to duplicate objects are always resolved in
1356 * favor of the preferred pack.
1358 * Therefore we do not need to ever ask the MIDX for its copy of
1359 * an object by OID, since it will always select it from the
1360 * preferred pack. Likewise, the selected copy of the base
1361 * object for any deltas will reside in the same pack.
1363 * This means that we can reuse pos when looking up the bit in
1364 * the reuse bitmap, too, since bits corresponding to the
1365 * preferred pack precede all bits from other packs.
1368 if (pos >= pack->num_objects)
1369 return -1; /* not actually in the pack or MIDX preferred pack */
1371 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1372 type = unpack_object_header(pack, w_curs, &offset, &size);
1373 if (type < 0)
1374 return -1; /* broken packfile, punt */
1376 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1377 off_t base_offset;
1378 uint32_t base_pos;
1381 * Find the position of the base object so we can look it up
1382 * in our bitmaps. If we can't come up with an offset, or if
1383 * that offset is not in the revidx, the pack is corrupt.
1384 * There's nothing we can do, so just punt on this object,
1385 * and the normal slow path will complain about it in
1386 * more detail.
1388 base_offset = get_delta_base(pack, w_curs, &offset, type,
1389 delta_obj_offset);
1390 if (!base_offset)
1391 return 0;
1392 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1393 return 0;
1396 * We assume delta dependencies always point backwards. This
1397 * lets us do a single pass, and is basically always true
1398 * due to the way OFS_DELTAs work. You would not typically
1399 * find REF_DELTA in a bitmapped pack, since we only bitmap
1400 * packs we write fresh, and OFS_DELTA is the default). But
1401 * let's double check to make sure the pack wasn't written with
1402 * odd parameters.
1404 if (base_pos >= pos)
1405 return 0;
1408 * And finally, if we're not sending the base as part of our
1409 * reuse chunk, then don't send this object either. The base
1410 * would come after us, along with other objects not
1411 * necessarily in the pack, which means we'd need to convert
1412 * to REF_DELTA on the fly. Better to just let the normal
1413 * object_entry code path handle it.
1415 if (!bitmap_get(reuse, base_pos))
1416 return 0;
1420 * If we got here, then the object is OK to reuse. Mark it.
1422 bitmap_set(reuse, pos);
1423 return 0;
1426 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1428 struct multi_pack_index *m = bitmap_git->midx;
1429 if (!m)
1430 BUG("midx_preferred_pack: requires non-empty MIDX");
1431 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1434 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1435 struct packed_git **packfile_out,
1436 uint32_t *entries,
1437 struct bitmap **reuse_out)
1439 struct packed_git *pack;
1440 struct bitmap *result = bitmap_git->result;
1441 struct bitmap *reuse;
1442 struct pack_window *w_curs = NULL;
1443 size_t i = 0;
1444 uint32_t offset;
1445 uint32_t objects_nr;
1447 assert(result);
1449 load_reverse_index(bitmap_git);
1451 if (bitmap_is_midx(bitmap_git))
1452 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1453 else
1454 pack = bitmap_git->pack;
1455 objects_nr = pack->num_objects;
1457 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1458 i++;
1461 * Don't mark objects not in the packfile or preferred pack. This bitmap
1462 * marks objects eligible for reuse, but the pack-reuse code only
1463 * understands how to reuse a single pack. Since the preferred pack is
1464 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1465 * we use it instead of another pack. In single-pack bitmaps, the choice
1466 * is made for us.
1468 if (i > objects_nr / BITS_IN_EWORD)
1469 i = objects_nr / BITS_IN_EWORD;
1471 reuse = bitmap_word_alloc(i);
1472 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1474 for (; i < result->word_alloc; ++i) {
1475 eword_t word = result->words[i];
1476 size_t pos = (i * BITS_IN_EWORD);
1478 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1479 if ((word >> offset) == 0)
1480 break;
1482 offset += ewah_bit_ctz64(word >> offset);
1483 if (try_partial_reuse(pack, pos + offset,
1484 reuse, &w_curs) < 0) {
1486 * try_partial_reuse indicated we couldn't reuse
1487 * any bits, so there is no point in trying more
1488 * bits in the current word, or any other words
1489 * in result.
1491 * Jump out of both loops to avoid future
1492 * unnecessary calls to try_partial_reuse.
1494 goto done;
1499 done:
1500 unuse_pack(&w_curs);
1502 *entries = bitmap_popcount(reuse);
1503 if (!*entries) {
1504 bitmap_free(reuse);
1505 return -1;
1509 * Drop any reused objects from the result, since they will not
1510 * need to be handled separately.
1512 bitmap_and_not(result, reuse);
1513 *packfile_out = pack;
1514 *reuse_out = reuse;
1515 return 0;
1518 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1519 struct bitmap *bitmap, const struct object_id *oid)
1521 int idx;
1523 if (!bitmap)
1524 return 0;
1526 idx = bitmap_position(bitmap_git, oid);
1527 return idx >= 0 && bitmap_get(bitmap, idx);
1530 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1531 struct rev_info *revs,
1532 show_reachable_fn show_reachable)
1534 assert(bitmap_git->result);
1536 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1537 if (revs->tree_objects)
1538 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1539 if (revs->blob_objects)
1540 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1541 if (revs->tag_objects)
1542 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1544 show_extended_objects(bitmap_git, revs, show_reachable);
1547 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1548 enum object_type type)
1550 struct bitmap *objects = bitmap_git->result;
1551 struct eindex *eindex = &bitmap_git->ext_index;
1553 uint32_t i = 0, count = 0;
1554 struct ewah_iterator it;
1555 eword_t filter;
1557 init_type_iterator(&it, bitmap_git, type);
1559 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1560 eword_t word = objects->words[i++] & filter;
1561 count += ewah_bit_popcount64(word);
1564 for (i = 0; i < eindex->count; ++i) {
1565 if (eindex->objects[i]->type == type &&
1566 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1567 count++;
1570 return count;
1573 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1574 uint32_t *commits, uint32_t *trees,
1575 uint32_t *blobs, uint32_t *tags)
1577 assert(bitmap_git->result);
1579 if (commits)
1580 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1582 if (trees)
1583 *trees = count_object_type(bitmap_git, OBJ_TREE);
1585 if (blobs)
1586 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1588 if (tags)
1589 *tags = count_object_type(bitmap_git, OBJ_TAG);
1592 struct bitmap_test_data {
1593 struct bitmap_index *bitmap_git;
1594 struct bitmap *base;
1595 struct bitmap *commits;
1596 struct bitmap *trees;
1597 struct bitmap *blobs;
1598 struct bitmap *tags;
1599 struct progress *prg;
1600 size_t seen;
1603 static void test_bitmap_type(struct bitmap_test_data *tdata,
1604 struct object *obj, int pos)
1606 enum object_type bitmap_type = OBJ_NONE;
1607 int bitmaps_nr = 0;
1609 if (bitmap_get(tdata->commits, pos)) {
1610 bitmap_type = OBJ_COMMIT;
1611 bitmaps_nr++;
1613 if (bitmap_get(tdata->trees, pos)) {
1614 bitmap_type = OBJ_TREE;
1615 bitmaps_nr++;
1617 if (bitmap_get(tdata->blobs, pos)) {
1618 bitmap_type = OBJ_BLOB;
1619 bitmaps_nr++;
1621 if (bitmap_get(tdata->tags, pos)) {
1622 bitmap_type = OBJ_TAG;
1623 bitmaps_nr++;
1626 if (bitmap_type == OBJ_NONE)
1627 die("object %s not found in type bitmaps",
1628 oid_to_hex(&obj->oid));
1630 if (bitmaps_nr > 1)
1631 die("object %s does not have a unique type",
1632 oid_to_hex(&obj->oid));
1634 if (bitmap_type != obj->type)
1635 die("object %s: real type %s, expected: %s",
1636 oid_to_hex(&obj->oid),
1637 type_name(obj->type),
1638 type_name(bitmap_type));
1641 static void test_show_object(struct object *object, const char *name,
1642 void *data)
1644 struct bitmap_test_data *tdata = data;
1645 int bitmap_pos;
1647 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1648 if (bitmap_pos < 0)
1649 die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
1650 test_bitmap_type(tdata, object, bitmap_pos);
1652 bitmap_set(tdata->base, bitmap_pos);
1653 display_progress(tdata->prg, ++tdata->seen);
1656 static void test_show_commit(struct commit *commit, void *data)
1658 struct bitmap_test_data *tdata = data;
1659 int bitmap_pos;
1661 bitmap_pos = bitmap_position(tdata->bitmap_git,
1662 &commit->object.oid);
1663 if (bitmap_pos < 0)
1664 die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
1665 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1667 bitmap_set(tdata->base, bitmap_pos);
1668 display_progress(tdata->prg, ++tdata->seen);
1671 void test_bitmap_walk(struct rev_info *revs)
1673 struct object *root;
1674 struct bitmap *result = NULL;
1675 size_t result_popcnt;
1676 struct bitmap_test_data tdata;
1677 struct bitmap_index *bitmap_git;
1678 struct ewah_bitmap *bm;
1680 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1681 die("failed to load bitmap indexes");
1683 if (revs->pending.nr != 1)
1684 die("you must specify exactly one commit to test");
1686 fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
1687 bitmap_git->version, bitmap_git->entry_count);
1689 root = revs->pending.objects[0].item;
1690 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
1692 if (bm) {
1693 fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
1694 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
1696 result = ewah_to_bitmap(bm);
1699 if (result == NULL)
1700 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
1702 revs->tag_objects = 1;
1703 revs->tree_objects = 1;
1704 revs->blob_objects = 1;
1706 result_popcnt = bitmap_popcount(result);
1708 if (prepare_revision_walk(revs))
1709 die("revision walk setup failed");
1711 tdata.bitmap_git = bitmap_git;
1712 tdata.base = bitmap_new();
1713 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
1714 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
1715 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
1716 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
1717 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1718 tdata.seen = 0;
1720 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1722 stop_progress(&tdata.prg);
1724 if (bitmap_equals(result, tdata.base))
1725 fprintf(stderr, "OK!\n");
1726 else
1727 die("mismatch in bitmap results");
1729 bitmap_free(result);
1730 bitmap_free(tdata.base);
1731 bitmap_free(tdata.commits);
1732 bitmap_free(tdata.trees);
1733 bitmap_free(tdata.blobs);
1734 bitmap_free(tdata.tags);
1735 free_bitmap_index(bitmap_git);
1738 int test_bitmap_commits(struct repository *r)
1740 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1741 struct object_id oid;
1742 MAYBE_UNUSED void *value;
1744 if (!bitmap_git)
1745 die("failed to load bitmap indexes");
1747 kh_foreach(bitmap_git->bitmaps, oid, value, {
1748 printf("%s\n", oid_to_hex(&oid));
1751 free_bitmap_index(bitmap_git);
1753 return 0;
1756 int test_bitmap_hashes(struct repository *r)
1758 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1759 struct object_id oid;
1760 uint32_t i, index_pos;
1762 if (!bitmap_git || !bitmap_git->hashes)
1763 goto cleanup;
1765 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
1766 if (bitmap_is_midx(bitmap_git))
1767 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
1768 else
1769 index_pos = pack_pos_to_index(bitmap_git->pack, i);
1771 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1773 printf("%s %"PRIu32"\n",
1774 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
1777 cleanup:
1778 free_bitmap_index(bitmap_git);
1780 return 0;
1783 int rebuild_bitmap(const uint32_t *reposition,
1784 struct ewah_bitmap *source,
1785 struct bitmap *dest)
1787 uint32_t pos = 0;
1788 struct ewah_iterator it;
1789 eword_t word;
1791 ewah_iterator_init(&it, source);
1793 while (ewah_iterator_next(&word, &it)) {
1794 uint32_t offset, bit_pos;
1796 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1797 if ((word >> offset) == 0)
1798 break;
1800 offset += ewah_bit_ctz64(word >> offset);
1802 bit_pos = reposition[pos + offset];
1803 if (bit_pos > 0)
1804 bitmap_set(dest, bit_pos - 1);
1805 else /* can't reuse, we don't have the object */
1806 return -1;
1809 pos += BITS_IN_EWORD;
1811 return 0;
1814 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1815 struct packing_data *mapping)
1817 uint32_t i, num_objects;
1818 uint32_t *reposition;
1820 if (!bitmap_is_midx(bitmap_git))
1821 load_reverse_index(bitmap_git);
1822 else if (load_midx_revindex(bitmap_git->midx) < 0)
1823 BUG("rebuild_existing_bitmaps: missing required rev-cache "
1824 "extension");
1826 num_objects = bitmap_num_objects(bitmap_git);
1827 CALLOC_ARRAY(reposition, num_objects);
1829 for (i = 0; i < num_objects; ++i) {
1830 struct object_id oid;
1831 struct object_entry *oe;
1832 uint32_t index_pos;
1834 if (bitmap_is_midx(bitmap_git))
1835 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
1836 else
1837 index_pos = pack_pos_to_index(bitmap_git->pack, i);
1838 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1839 oe = packlist_find(mapping, &oid);
1841 if (oe) {
1842 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
1843 if (bitmap_git->hashes && !oe->hash)
1844 oe->hash = get_be32(bitmap_git->hashes + index_pos);
1848 return reposition;
1851 void free_bitmap_index(struct bitmap_index *b)
1853 if (!b)
1854 return;
1856 if (b->map)
1857 munmap(b->map, b->map_size);
1858 ewah_pool_free(b->commits);
1859 ewah_pool_free(b->trees);
1860 ewah_pool_free(b->blobs);
1861 ewah_pool_free(b->tags);
1862 if (b->bitmaps) {
1863 struct stored_bitmap *sb;
1864 kh_foreach_value(b->bitmaps, sb, {
1865 ewah_pool_free(sb->root);
1866 free(sb);
1869 kh_destroy_oid_map(b->bitmaps);
1870 free(b->ext_index.objects);
1871 free(b->ext_index.hashes);
1872 kh_destroy_oid_pos(b->ext_index.positions);
1873 bitmap_free(b->result);
1874 bitmap_free(b->haves);
1875 if (bitmap_is_midx(b)) {
1877 * Multi-pack bitmaps need to have resources associated with
1878 * their on-disk reverse indexes unmapped so that stale .rev and
1879 * .bitmap files can be removed.
1881 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
1882 * written in the same 'git multi-pack-index write --bitmap'
1883 * process. Close resources so they can be removed safely on
1884 * platforms like Windows.
1886 close_midx_revindex(b->midx);
1888 free(b);
1891 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1892 const struct object_id *oid)
1894 return bitmap_git &&
1895 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
1898 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1899 enum object_type object_type)
1901 struct bitmap *result = bitmap_git->result;
1902 off_t total = 0;
1903 struct ewah_iterator it;
1904 eword_t filter;
1905 size_t i;
1907 init_type_iterator(&it, bitmap_git, object_type);
1908 for (i = 0; i < result->word_alloc &&
1909 ewah_iterator_next(&filter, &it); i++) {
1910 eword_t word = result->words[i] & filter;
1911 size_t base = (i * BITS_IN_EWORD);
1912 unsigned offset;
1914 if (!word)
1915 continue;
1917 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1918 if ((word >> offset) == 0)
1919 break;
1921 offset += ewah_bit_ctz64(word >> offset);
1923 if (bitmap_is_midx(bitmap_git)) {
1924 uint32_t pack_pos;
1925 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
1926 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
1928 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1929 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
1931 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
1932 struct object_id oid;
1933 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
1935 die(_("could not find %s in pack %s at offset %"PRIuMAX),
1936 oid_to_hex(&oid),
1937 pack->pack_name,
1938 (uintmax_t)offset);
1941 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
1942 } else {
1943 size_t pos = base + offset;
1944 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
1945 pack_pos_to_offset(bitmap_git->pack, pos);
1950 return total;
1953 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1955 struct bitmap *result = bitmap_git->result;
1956 struct eindex *eindex = &bitmap_git->ext_index;
1957 off_t total = 0;
1958 struct object_info oi = OBJECT_INFO_INIT;
1959 off_t object_size;
1960 size_t i;
1962 oi.disk_sizep = &object_size;
1964 for (i = 0; i < eindex->count; i++) {
1965 struct object *obj = eindex->objects[i];
1967 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
1968 continue;
1970 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1971 die(_("unable to get disk usage of %s"),
1972 oid_to_hex(&obj->oid));
1974 total += object_size;
1976 return total;
1979 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
1980 struct rev_info *revs)
1982 off_t total = 0;
1984 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
1985 if (revs->tree_objects)
1986 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
1987 if (revs->blob_objects)
1988 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
1989 if (revs->tag_objects)
1990 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
1992 total += get_disk_usage_for_extended(bitmap_git);
1994 return total;
1997 int bitmap_is_midx(struct bitmap_index *bitmap_git)
1999 return !!bitmap_git->midx;
2002 const struct string_list *bitmap_preferred_tips(struct repository *r)
2004 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
2007 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2009 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2010 struct string_list_item *item;
2012 if (!preferred_tips)
2013 return 0;
2015 for_each_string_list_item(item, preferred_tips) {
2016 if (starts_with(refname, item->string))
2017 return 1;
2020 return 0;