Merge branch 'vd/scalar-doc'
[git/debian.git] / pack-bitmap.c
blobef580be9e3ff151b5941cd8c6a26cee9a859ed09
1 #include "cache.h"
2 #include "commit.h"
3 #include "strbuf.h"
4 #include "tag.h"
5 #include "diff.h"
6 #include "revision.h"
7 #include "progress.h"
8 #include "list-objects.h"
9 #include "pack.h"
10 #include "pack-bitmap.h"
11 #include "pack-revindex.h"
12 #include "pack-objects.h"
13 #include "packfile.h"
14 #include "repository.h"
15 #include "object-store.h"
16 #include "list-objects-filter-options.h"
17 #include "midx.h"
18 #include "config.h"
21 * An entry on the bitmap index, representing the bitmap for a given
22 * commit.
24 struct stored_bitmap {
25 struct object_id oid;
26 struct ewah_bitmap *root;
27 struct stored_bitmap *xor;
28 int flags;
32 * The active bitmap index for a repository. By design, repositories only have
33 * a single bitmap index available (the index for the biggest packfile in
34 * the repository), since bitmap indexes need full closure.
36 * If there is more than one bitmap index available (e.g. because of alternates),
37 * the active bitmap index is the largest one.
39 struct bitmap_index {
41 * The pack or multi-pack index (MIDX) that this bitmap index belongs
42 * to.
44 * Exactly one of these must be non-NULL; this specifies the object
45 * order used to interpret this bitmap.
47 struct packed_git *pack;
48 struct multi_pack_index *midx;
51 * Mark the first `reuse_objects` in the packfile as reused:
52 * they will be sent as-is without using them for repacking
53 * calculations
55 uint32_t reuse_objects;
57 /* mmapped buffer of the whole bitmap index */
58 unsigned char *map;
59 size_t map_size; /* size of the mmaped buffer */
60 size_t map_pos; /* current position when loading the index */
63 * Type indexes.
65 * Each bitmap marks which objects in the packfile are of the given
66 * type. This provides type information when yielding the objects from
67 * the packfile during a walk, which allows for better delta bases.
69 struct ewah_bitmap *commits;
70 struct ewah_bitmap *trees;
71 struct ewah_bitmap *blobs;
72 struct ewah_bitmap *tags;
74 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
75 kh_oid_map_t *bitmaps;
77 /* Number of bitmapped commits */
78 uint32_t entry_count;
80 /* If not NULL, this is a name-hash cache pointing into map. */
81 uint32_t *hashes;
83 /* The checksum of the packfile or MIDX; points into map. */
84 const unsigned char *checksum;
87 * Extended index.
89 * When trying to perform bitmap operations with objects that are not
90 * packed in `pack`, these objects are added to this "fake index" and
91 * are assumed to appear at the end of the packfile for all operations
93 struct eindex {
94 struct object **objects;
95 uint32_t *hashes;
96 uint32_t count, alloc;
97 kh_oid_pos_t *positions;
98 } ext_index;
100 /* Bitmap result of the last performed walk */
101 struct bitmap *result;
103 /* "have" bitmap from the last performed walk */
104 struct bitmap *haves;
106 /* Version of the bitmap index */
107 unsigned int version;
110 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
112 struct ewah_bitmap *parent;
113 struct ewah_bitmap *composed;
115 if (!st->xor)
116 return st->root;
118 composed = ewah_pool_new();
119 parent = lookup_stored_bitmap(st->xor);
120 ewah_xor(st->root, parent, composed);
122 ewah_pool_free(st->root);
123 st->root = composed;
124 st->xor = NULL;
126 return composed;
130 * Read a bitmap from the current read position on the mmaped
131 * index, and increase the read position accordingly
133 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
135 struct ewah_bitmap *b = ewah_pool_new();
137 ssize_t bitmap_size = ewah_read_mmap(b,
138 index->map + index->map_pos,
139 index->map_size - index->map_pos);
141 if (bitmap_size < 0) {
142 error(_("failed to load bitmap index (corrupted?)"));
143 ewah_pool_free(b);
144 return NULL;
147 index->map_pos += bitmap_size;
148 return b;
151 static uint32_t bitmap_num_objects(struct bitmap_index *index)
153 if (index->midx)
154 return index->midx->num_objects;
155 return index->pack->num_objects;
158 static int load_bitmap_header(struct bitmap_index *index)
160 struct bitmap_disk_header *header = (void *)index->map;
161 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
163 if (index->map_size < header_size + the_hash_algo->rawsz)
164 return error(_("corrupted bitmap index (too small)"));
166 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
167 return error(_("corrupted bitmap index file (wrong header)"));
169 index->version = ntohs(header->version);
170 if (index->version != 1)
171 return error(_("unsupported version '%d' for bitmap index file"), index->version);
173 /* Parse known bitmap format options */
175 uint32_t flags = ntohs(header->options);
176 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
177 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
179 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
180 BUG("unsupported options for bitmap index file "
181 "(Git requires BITMAP_OPT_FULL_DAG)");
183 if (flags & BITMAP_OPT_HASH_CACHE) {
184 if (cache_size > index_end - index->map - header_size)
185 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
186 index->hashes = (void *)(index_end - cache_size);
187 index_end -= cache_size;
191 index->entry_count = ntohl(header->entry_count);
192 index->checksum = header->checksum;
193 index->map_pos += header_size;
194 return 0;
197 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
198 struct ewah_bitmap *root,
199 const struct object_id *oid,
200 struct stored_bitmap *xor_with,
201 int flags)
203 struct stored_bitmap *stored;
204 khiter_t hash_pos;
205 int ret;
207 stored = xmalloc(sizeof(struct stored_bitmap));
208 stored->root = root;
209 stored->xor = xor_with;
210 stored->flags = flags;
211 oidcpy(&stored->oid, oid);
213 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
215 /* a 0 return code means the insertion succeeded with no changes,
216 * because the SHA1 already existed on the map. this is bad, there
217 * shouldn't be duplicated commits in the index */
218 if (ret == 0) {
219 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
220 return NULL;
223 kh_value(index->bitmaps, hash_pos) = stored;
224 return stored;
227 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
229 uint32_t result = get_be32(buffer + *pos);
230 (*pos) += sizeof(result);
231 return result;
234 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
236 return buffer[(*pos)++];
239 #define MAX_XOR_OFFSET 160
241 static int nth_bitmap_object_oid(struct bitmap_index *index,
242 struct object_id *oid,
243 uint32_t n)
245 if (index->midx)
246 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
247 return nth_packed_object_id(oid, index->pack, n);
250 static int load_bitmap_entries_v1(struct bitmap_index *index)
252 uint32_t i;
253 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
255 for (i = 0; i < index->entry_count; ++i) {
256 int xor_offset, flags;
257 struct ewah_bitmap *bitmap = NULL;
258 struct stored_bitmap *xor_bitmap = NULL;
259 uint32_t commit_idx_pos;
260 struct object_id oid;
262 if (index->map_size - index->map_pos < 6)
263 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
265 commit_idx_pos = read_be32(index->map, &index->map_pos);
266 xor_offset = read_u8(index->map, &index->map_pos);
267 flags = read_u8(index->map, &index->map_pos);
269 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
270 return error(_("corrupt ewah bitmap: commit index %u out of range"),
271 (unsigned)commit_idx_pos);
273 bitmap = read_bitmap_1(index);
274 if (!bitmap)
275 return -1;
277 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
278 return error(_("corrupted bitmap pack index"));
280 if (xor_offset > 0) {
281 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
283 if (!xor_bitmap)
284 return error(_("invalid XOR offset in bitmap pack index"));
287 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
288 index, bitmap, &oid, xor_bitmap, flags);
291 return 0;
294 char *midx_bitmap_filename(struct multi_pack_index *midx)
296 struct strbuf buf = STRBUF_INIT;
298 get_midx_filename(&buf, midx->object_dir);
299 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
301 return strbuf_detach(&buf, NULL);
304 char *pack_bitmap_filename(struct packed_git *p)
306 size_t len;
308 if (!strip_suffix(p->pack_name, ".pack", &len))
309 BUG("pack_name does not end in .pack");
310 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
313 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
314 struct multi_pack_index *midx)
316 struct stat st;
317 char *bitmap_name = midx_bitmap_filename(midx);
318 int fd = git_open(bitmap_name);
319 uint32_t i;
320 struct packed_git *preferred;
322 if (fd < 0) {
323 if (errno != ENOENT)
324 warning_errno("cannot open '%s'", bitmap_name);
325 free(bitmap_name);
326 return -1;
328 free(bitmap_name);
330 if (fstat(fd, &st)) {
331 error_errno(_("cannot fstat bitmap file"));
332 close(fd);
333 return -1;
336 if (bitmap_git->pack || bitmap_git->midx) {
337 struct strbuf buf = STRBUF_INIT;
338 get_midx_filename(&buf, midx->object_dir);
339 /* ignore extra bitmap file; we can only handle one */
340 warning(_("ignoring extra bitmap file: '%s'"), buf.buf);
341 close(fd);
342 strbuf_release(&buf);
343 return -1;
346 bitmap_git->midx = midx;
347 bitmap_git->map_size = xsize_t(st.st_size);
348 bitmap_git->map_pos = 0;
349 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
350 MAP_PRIVATE, fd, 0);
351 close(fd);
353 if (load_bitmap_header(bitmap_git) < 0)
354 goto cleanup;
356 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
357 error(_("checksum doesn't match in MIDX and bitmap"));
358 goto cleanup;
361 if (load_midx_revindex(bitmap_git->midx) < 0) {
362 warning(_("multi-pack bitmap is missing required reverse index"));
363 goto cleanup;
366 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
367 if (prepare_midx_pack(the_repository, bitmap_git->midx, i))
368 die(_("could not open pack %s"),
369 bitmap_git->midx->pack_names[i]);
372 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
373 if (!is_pack_valid(preferred)) {
374 warning(_("preferred pack (%s) is invalid"),
375 preferred->pack_name);
376 goto cleanup;
379 return 0;
381 cleanup:
382 munmap(bitmap_git->map, bitmap_git->map_size);
383 bitmap_git->map_size = 0;
384 bitmap_git->map_pos = 0;
385 bitmap_git->map = NULL;
386 bitmap_git->midx = NULL;
387 return -1;
390 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
392 int fd;
393 struct stat st;
394 char *bitmap_name;
396 if (open_pack_index(packfile))
397 return -1;
399 bitmap_name = pack_bitmap_filename(packfile);
400 fd = git_open(bitmap_name);
402 if (fd < 0) {
403 if (errno != ENOENT)
404 warning_errno("cannot open '%s'", bitmap_name);
405 free(bitmap_name);
406 return -1;
408 free(bitmap_name);
410 if (fstat(fd, &st)) {
411 error_errno(_("cannot fstat bitmap file"));
412 close(fd);
413 return -1;
416 if (bitmap_git->pack || bitmap_git->midx) {
417 /* ignore extra bitmap file; we can only handle one */
418 warning(_("ignoring extra bitmap file: '%s'"), packfile->pack_name);
419 close(fd);
420 return -1;
423 if (!is_pack_valid(packfile)) {
424 close(fd);
425 return -1;
428 bitmap_git->pack = packfile;
429 bitmap_git->map_size = xsize_t(st.st_size);
430 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
431 bitmap_git->map_pos = 0;
432 close(fd);
434 if (load_bitmap_header(bitmap_git) < 0) {
435 munmap(bitmap_git->map, bitmap_git->map_size);
436 bitmap_git->map = NULL;
437 bitmap_git->map_size = 0;
438 bitmap_git->map_pos = 0;
439 bitmap_git->pack = NULL;
440 return -1;
443 return 0;
446 static int load_reverse_index(struct bitmap_index *bitmap_git)
448 if (bitmap_is_midx(bitmap_git)) {
449 uint32_t i;
450 int ret;
453 * The multi-pack-index's .rev file is already loaded via
454 * open_pack_bitmap_1().
456 * But we still need to open the individual pack .rev files,
457 * since we will need to make use of them in pack-objects.
459 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
460 ret = load_pack_revindex(bitmap_git->midx->packs[i]);
461 if (ret)
462 return ret;
464 return 0;
466 return load_pack_revindex(bitmap_git->pack);
469 static int load_bitmap(struct bitmap_index *bitmap_git)
471 assert(bitmap_git->map);
473 bitmap_git->bitmaps = kh_init_oid_map();
474 bitmap_git->ext_index.positions = kh_init_oid_pos();
476 if (load_reverse_index(bitmap_git))
477 goto failed;
479 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
480 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
481 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
482 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
483 goto failed;
485 if (load_bitmap_entries_v1(bitmap_git) < 0)
486 goto failed;
488 return 0;
490 failed:
491 munmap(bitmap_git->map, bitmap_git->map_size);
492 bitmap_git->map = NULL;
493 bitmap_git->map_size = 0;
495 kh_destroy_oid_map(bitmap_git->bitmaps);
496 bitmap_git->bitmaps = NULL;
498 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
499 bitmap_git->ext_index.positions = NULL;
501 return -1;
504 static int open_pack_bitmap(struct repository *r,
505 struct bitmap_index *bitmap_git)
507 struct packed_git *p;
508 int ret = -1;
510 assert(!bitmap_git->map);
512 for (p = get_all_packs(r); p; p = p->next) {
513 if (open_pack_bitmap_1(bitmap_git, p) == 0)
514 ret = 0;
517 return ret;
520 static int open_midx_bitmap(struct repository *r,
521 struct bitmap_index *bitmap_git)
523 int ret = -1;
524 struct multi_pack_index *midx;
526 assert(!bitmap_git->map);
528 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
529 if (!open_midx_bitmap_1(bitmap_git, midx))
530 ret = 0;
532 return ret;
535 static int open_bitmap(struct repository *r,
536 struct bitmap_index *bitmap_git)
538 assert(!bitmap_git->map);
540 if (!open_midx_bitmap(r, bitmap_git))
541 return 0;
542 return open_pack_bitmap(r, bitmap_git);
545 struct bitmap_index *prepare_bitmap_git(struct repository *r)
547 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
549 if (!open_bitmap(r, bitmap_git) && !load_bitmap(bitmap_git))
550 return bitmap_git;
552 free_bitmap_index(bitmap_git);
553 return NULL;
556 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
558 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
560 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(bitmap_git))
561 return bitmap_git;
563 free_bitmap_index(bitmap_git);
564 return NULL;
567 struct include_data {
568 struct bitmap_index *bitmap_git;
569 struct bitmap *base;
570 struct bitmap *seen;
573 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
574 struct commit *commit)
576 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
577 commit->object.oid);
578 if (hash_pos >= kh_end(bitmap_git->bitmaps))
579 return NULL;
580 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
583 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
584 const struct object_id *oid)
586 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
587 khiter_t pos = kh_get_oid_pos(positions, *oid);
589 if (pos < kh_end(positions)) {
590 int bitmap_pos = kh_value(positions, pos);
591 return bitmap_pos + bitmap_num_objects(bitmap_git);
594 return -1;
597 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
598 const struct object_id *oid)
600 uint32_t pos;
601 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
602 if (!offset)
603 return -1;
605 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
606 return -1;
607 return pos;
610 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
611 const struct object_id *oid)
613 uint32_t want, got;
614 if (!bsearch_midx(oid, bitmap_git->midx, &want))
615 return -1;
617 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
618 return -1;
619 return got;
622 static int bitmap_position(struct bitmap_index *bitmap_git,
623 const struct object_id *oid)
625 int pos;
626 if (bitmap_is_midx(bitmap_git))
627 pos = bitmap_position_midx(bitmap_git, oid);
628 else
629 pos = bitmap_position_packfile(bitmap_git, oid);
630 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
633 static int ext_index_add_object(struct bitmap_index *bitmap_git,
634 struct object *object, const char *name)
636 struct eindex *eindex = &bitmap_git->ext_index;
638 khiter_t hash_pos;
639 int hash_ret;
640 int bitmap_pos;
642 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
643 if (hash_ret > 0) {
644 if (eindex->count >= eindex->alloc) {
645 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
646 REALLOC_ARRAY(eindex->objects, eindex->alloc);
647 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
650 bitmap_pos = eindex->count;
651 eindex->objects[eindex->count] = object;
652 eindex->hashes[eindex->count] = pack_name_hash(name);
653 kh_value(eindex->positions, hash_pos) = bitmap_pos;
654 eindex->count++;
655 } else {
656 bitmap_pos = kh_value(eindex->positions, hash_pos);
659 return bitmap_pos + bitmap_num_objects(bitmap_git);
662 struct bitmap_show_data {
663 struct bitmap_index *bitmap_git;
664 struct bitmap *base;
667 static void show_object(struct object *object, const char *name, void *data_)
669 struct bitmap_show_data *data = data_;
670 int bitmap_pos;
672 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
674 if (bitmap_pos < 0)
675 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
676 name);
678 bitmap_set(data->base, bitmap_pos);
681 static void show_commit(struct commit *commit, void *data)
685 static int add_to_include_set(struct bitmap_index *bitmap_git,
686 struct include_data *data,
687 struct commit *commit,
688 int bitmap_pos)
690 struct ewah_bitmap *partial;
692 if (data->seen && bitmap_get(data->seen, bitmap_pos))
693 return 0;
695 if (bitmap_get(data->base, bitmap_pos))
696 return 0;
698 partial = bitmap_for_commit(bitmap_git, commit);
699 if (partial) {
700 bitmap_or_ewah(data->base, partial);
701 return 0;
704 bitmap_set(data->base, bitmap_pos);
705 return 1;
708 static int should_include(struct commit *commit, void *_data)
710 struct include_data *data = _data;
711 int bitmap_pos;
713 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
714 if (bitmap_pos < 0)
715 bitmap_pos = ext_index_add_object(data->bitmap_git,
716 (struct object *)commit,
717 NULL);
719 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
720 struct commit_list *parent = commit->parents;
722 while (parent) {
723 parent->item->object.flags |= SEEN;
724 parent = parent->next;
727 return 0;
730 return 1;
733 static int should_include_obj(struct object *obj, void *_data)
735 struct include_data *data = _data;
736 int bitmap_pos;
738 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
739 if (bitmap_pos < 0)
740 return 1;
741 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
742 bitmap_get(data->base, bitmap_pos)) {
743 obj->flags |= SEEN;
744 return 0;
746 return 1;
749 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
750 struct bitmap **base,
751 struct commit *commit)
753 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
755 if (!or_with)
756 return 0;
758 if (!*base)
759 *base = ewah_to_bitmap(or_with);
760 else
761 bitmap_or_ewah(*base, or_with);
763 return 1;
766 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
767 struct rev_info *revs,
768 struct object_list *roots,
769 struct bitmap *seen)
771 struct bitmap *base = NULL;
772 int needs_walk = 0;
774 struct object_list *not_mapped = NULL;
777 * Go through all the roots for the walk. The ones that have bitmaps
778 * on the bitmap index will be `or`ed together to form an initial
779 * global reachability analysis.
781 * The ones without bitmaps in the index will be stored in the
782 * `not_mapped_list` for further processing.
784 while (roots) {
785 struct object *object = roots->item;
786 roots = roots->next;
788 if (object->type == OBJ_COMMIT &&
789 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
790 object->flags |= SEEN;
791 continue;
794 object_list_insert(object, &not_mapped);
798 * Best case scenario: We found bitmaps for all the roots,
799 * so the resulting `or` bitmap has the full reachability analysis
801 if (!not_mapped)
802 return base;
804 roots = not_mapped;
807 * Let's iterate through all the roots that don't have bitmaps to
808 * check if we can determine them to be reachable from the existing
809 * global bitmap.
811 * If we cannot find them in the existing global bitmap, we'll need
812 * to push them to an actual walk and run it until we can confirm
813 * they are reachable
815 while (roots) {
816 struct object *object = roots->item;
817 int pos;
819 roots = roots->next;
820 pos = bitmap_position(bitmap_git, &object->oid);
822 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
823 object->flags &= ~UNINTERESTING;
824 add_pending_object(revs, object, "");
825 needs_walk = 1;
826 } else {
827 object->flags |= SEEN;
831 if (needs_walk) {
832 struct include_data incdata;
833 struct bitmap_show_data show_data;
835 if (!base)
836 base = bitmap_new();
838 incdata.bitmap_git = bitmap_git;
839 incdata.base = base;
840 incdata.seen = seen;
842 revs->include_check = should_include;
843 revs->include_check_obj = should_include_obj;
844 revs->include_check_data = &incdata;
846 if (prepare_revision_walk(revs))
847 die(_("revision walk setup failed"));
849 show_data.bitmap_git = bitmap_git;
850 show_data.base = base;
852 traverse_commit_list(revs,
853 show_commit, show_object,
854 &show_data);
856 revs->include_check = NULL;
857 revs->include_check_obj = NULL;
858 revs->include_check_data = NULL;
861 return base;
864 static void show_extended_objects(struct bitmap_index *bitmap_git,
865 struct rev_info *revs,
866 show_reachable_fn show_reach)
868 struct bitmap *objects = bitmap_git->result;
869 struct eindex *eindex = &bitmap_git->ext_index;
870 uint32_t i;
872 for (i = 0; i < eindex->count; ++i) {
873 struct object *obj;
875 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
876 continue;
878 obj = eindex->objects[i];
879 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
880 (obj->type == OBJ_TREE && !revs->tree_objects) ||
881 (obj->type == OBJ_TAG && !revs->tag_objects))
882 continue;
884 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
888 static void init_type_iterator(struct ewah_iterator *it,
889 struct bitmap_index *bitmap_git,
890 enum object_type type)
892 switch (type) {
893 case OBJ_COMMIT:
894 ewah_iterator_init(it, bitmap_git->commits);
895 break;
897 case OBJ_TREE:
898 ewah_iterator_init(it, bitmap_git->trees);
899 break;
901 case OBJ_BLOB:
902 ewah_iterator_init(it, bitmap_git->blobs);
903 break;
905 case OBJ_TAG:
906 ewah_iterator_init(it, bitmap_git->tags);
907 break;
909 default:
910 BUG("object type %d not stored by bitmap type index", type);
911 break;
915 static void show_objects_for_type(
916 struct bitmap_index *bitmap_git,
917 enum object_type object_type,
918 show_reachable_fn show_reach)
920 size_t i = 0;
921 uint32_t offset;
923 struct ewah_iterator it;
924 eword_t filter;
926 struct bitmap *objects = bitmap_git->result;
928 init_type_iterator(&it, bitmap_git, object_type);
930 for (i = 0; i < objects->word_alloc &&
931 ewah_iterator_next(&filter, &it); i++) {
932 eword_t word = objects->words[i] & filter;
933 size_t pos = (i * BITS_IN_EWORD);
935 if (!word)
936 continue;
938 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
939 struct packed_git *pack;
940 struct object_id oid;
941 uint32_t hash = 0, index_pos;
942 off_t ofs;
944 if ((word >> offset) == 0)
945 break;
947 offset += ewah_bit_ctz64(word >> offset);
949 if (bitmap_is_midx(bitmap_git)) {
950 struct multi_pack_index *m = bitmap_git->midx;
951 uint32_t pack_id;
953 index_pos = pack_pos_to_midx(m, pos + offset);
954 ofs = nth_midxed_offset(m, index_pos);
955 nth_midxed_object_oid(&oid, m, index_pos);
957 pack_id = nth_midxed_pack_int_id(m, index_pos);
958 pack = bitmap_git->midx->packs[pack_id];
959 } else {
960 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
961 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
962 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
964 pack = bitmap_git->pack;
967 if (bitmap_git->hashes)
968 hash = get_be32(bitmap_git->hashes + index_pos);
970 show_reach(&oid, object_type, 0, hash, pack, ofs);
975 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
976 struct object_list *roots)
978 while (roots) {
979 struct object *object = roots->item;
980 roots = roots->next;
982 if (bitmap_is_midx(bitmap_git)) {
983 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
984 return 1;
985 } else {
986 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
987 return 1;
991 return 0;
994 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
995 struct object_list *tip_objects,
996 enum object_type type)
998 struct bitmap *result = bitmap_new();
999 struct object_list *p;
1001 for (p = tip_objects; p; p = p->next) {
1002 int pos;
1004 if (p->item->type != type)
1005 continue;
1007 pos = bitmap_position(bitmap_git, &p->item->oid);
1008 if (pos < 0)
1009 continue;
1011 bitmap_set(result, pos);
1014 return result;
1017 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1018 struct object_list *tip_objects,
1019 struct bitmap *to_filter,
1020 enum object_type type)
1022 struct eindex *eindex = &bitmap_git->ext_index;
1023 struct bitmap *tips;
1024 struct ewah_iterator it;
1025 eword_t mask;
1026 uint32_t i;
1029 * The non-bitmap version of this filter never removes
1030 * objects which the other side specifically asked for,
1031 * so we must match that behavior.
1033 tips = find_tip_objects(bitmap_git, tip_objects, type);
1036 * We can use the type-level bitmap for 'type' to work in whole
1037 * words for the objects that are actually in the bitmapped
1038 * packfile.
1040 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1041 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1042 i++) {
1043 if (i < tips->word_alloc)
1044 mask &= ~tips->words[i];
1045 to_filter->words[i] &= ~mask;
1049 * Clear any objects that weren't in the packfile (and so would
1050 * not have been caught by the loop above. We'll have to check
1051 * them individually.
1053 for (i = 0; i < eindex->count; i++) {
1054 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1055 if (eindex->objects[i]->type == type &&
1056 bitmap_get(to_filter, pos) &&
1057 !bitmap_get(tips, pos))
1058 bitmap_unset(to_filter, pos);
1061 bitmap_free(tips);
1064 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1065 struct object_list *tip_objects,
1066 struct bitmap *to_filter)
1068 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1069 OBJ_BLOB);
1072 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1073 uint32_t pos)
1075 unsigned long size;
1076 struct object_info oi = OBJECT_INFO_INIT;
1078 oi.sizep = &size;
1080 if (pos < bitmap_num_objects(bitmap_git)) {
1081 struct packed_git *pack;
1082 off_t ofs;
1084 if (bitmap_is_midx(bitmap_git)) {
1085 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1086 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1088 pack = bitmap_git->midx->packs[pack_id];
1089 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1090 } else {
1091 pack = bitmap_git->pack;
1092 ofs = pack_pos_to_offset(pack, pos);
1095 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1096 struct object_id oid;
1097 nth_bitmap_object_oid(bitmap_git, &oid,
1098 pack_pos_to_index(pack, pos));
1099 die(_("unable to get size of %s"), oid_to_hex(&oid));
1101 } else {
1102 struct eindex *eindex = &bitmap_git->ext_index;
1103 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1104 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1105 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1108 return size;
1111 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1112 struct object_list *tip_objects,
1113 struct bitmap *to_filter,
1114 unsigned long limit)
1116 struct eindex *eindex = &bitmap_git->ext_index;
1117 struct bitmap *tips;
1118 struct ewah_iterator it;
1119 eword_t mask;
1120 uint32_t i;
1122 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1124 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1125 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1126 i++) {
1127 eword_t word = to_filter->words[i] & mask;
1128 unsigned offset;
1130 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1131 uint32_t pos;
1133 if ((word >> offset) == 0)
1134 break;
1135 offset += ewah_bit_ctz64(word >> offset);
1136 pos = i * BITS_IN_EWORD + offset;
1138 if (!bitmap_get(tips, pos) &&
1139 get_size_by_pos(bitmap_git, pos) >= limit)
1140 bitmap_unset(to_filter, pos);
1144 for (i = 0; i < eindex->count; i++) {
1145 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1146 if (eindex->objects[i]->type == OBJ_BLOB &&
1147 bitmap_get(to_filter, pos) &&
1148 !bitmap_get(tips, pos) &&
1149 get_size_by_pos(bitmap_git, pos) >= limit)
1150 bitmap_unset(to_filter, pos);
1153 bitmap_free(tips);
1156 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1157 struct object_list *tip_objects,
1158 struct bitmap *to_filter,
1159 unsigned long limit)
1161 if (limit)
1162 BUG("filter_bitmap_tree_depth given non-zero limit");
1164 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1165 OBJ_TREE);
1166 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1167 OBJ_BLOB);
1170 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1171 struct object_list *tip_objects,
1172 struct bitmap *to_filter,
1173 enum object_type object_type)
1175 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1176 BUG("filter_bitmap_object_type given invalid object");
1178 if (object_type != OBJ_TAG)
1179 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1180 if (object_type != OBJ_COMMIT)
1181 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1182 if (object_type != OBJ_TREE)
1183 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1184 if (object_type != OBJ_BLOB)
1185 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1188 static int filter_bitmap(struct bitmap_index *bitmap_git,
1189 struct object_list *tip_objects,
1190 struct bitmap *to_filter,
1191 struct list_objects_filter_options *filter)
1193 if (!filter || filter->choice == LOFC_DISABLED)
1194 return 0;
1196 if (filter->choice == LOFC_BLOB_NONE) {
1197 if (bitmap_git)
1198 filter_bitmap_blob_none(bitmap_git, tip_objects,
1199 to_filter);
1200 return 0;
1203 if (filter->choice == LOFC_BLOB_LIMIT) {
1204 if (bitmap_git)
1205 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1206 to_filter,
1207 filter->blob_limit_value);
1208 return 0;
1211 if (filter->choice == LOFC_TREE_DEPTH &&
1212 filter->tree_exclude_depth == 0) {
1213 if (bitmap_git)
1214 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1215 to_filter,
1216 filter->tree_exclude_depth);
1217 return 0;
1220 if (filter->choice == LOFC_OBJECT_TYPE) {
1221 if (bitmap_git)
1222 filter_bitmap_object_type(bitmap_git, tip_objects,
1223 to_filter,
1224 filter->object_type);
1225 return 0;
1228 if (filter->choice == LOFC_COMBINE) {
1229 int i;
1230 for (i = 0; i < filter->sub_nr; i++) {
1231 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1232 &filter->sub[i]) < 0)
1233 return -1;
1235 return 0;
1238 /* filter choice not handled */
1239 return -1;
1242 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1244 return !filter_bitmap(NULL, NULL, NULL, filter);
1247 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1248 int filter_provided_objects)
1250 unsigned int i;
1252 struct object_list *wants = NULL;
1253 struct object_list *haves = NULL;
1255 struct bitmap *wants_bitmap = NULL;
1256 struct bitmap *haves_bitmap = NULL;
1258 struct bitmap_index *bitmap_git;
1261 * We can't do pathspec limiting with bitmaps, because we don't know
1262 * which commits are associated with which object changes (let alone
1263 * even which objects are associated with which paths).
1265 if (revs->prune)
1266 return NULL;
1268 if (!can_filter_bitmap(&revs->filter))
1269 return NULL;
1271 /* try to open a bitmapped pack, but don't parse it yet
1272 * because we may not need to use it */
1273 CALLOC_ARRAY(bitmap_git, 1);
1274 if (open_bitmap(revs->repo, bitmap_git) < 0)
1275 goto cleanup;
1277 for (i = 0; i < revs->pending.nr; ++i) {
1278 struct object *object = revs->pending.objects[i].item;
1280 if (object->type == OBJ_NONE)
1281 parse_object_or_die(&object->oid, NULL);
1283 while (object->type == OBJ_TAG) {
1284 struct tag *tag = (struct tag *) object;
1286 if (object->flags & UNINTERESTING)
1287 object_list_insert(object, &haves);
1288 else
1289 object_list_insert(object, &wants);
1291 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1292 object->flags |= (tag->object.flags & UNINTERESTING);
1295 if (object->flags & UNINTERESTING)
1296 object_list_insert(object, &haves);
1297 else
1298 object_list_insert(object, &wants);
1302 * if we have a HAVES list, but none of those haves is contained
1303 * in the packfile that has a bitmap, we don't have anything to
1304 * optimize here
1306 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1307 goto cleanup;
1309 /* if we don't want anything, we're done here */
1310 if (!wants)
1311 goto cleanup;
1314 * now we're going to use bitmaps, so load the actual bitmap entries
1315 * from disk. this is the point of no return; after this the rev_list
1316 * becomes invalidated and we must perform the revwalk through bitmaps
1318 if (load_bitmap(bitmap_git) < 0)
1319 goto cleanup;
1321 object_array_clear(&revs->pending);
1323 if (haves) {
1324 revs->ignore_missing_links = 1;
1325 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1326 reset_revision_walk();
1327 revs->ignore_missing_links = 0;
1329 if (!haves_bitmap)
1330 BUG("failed to perform bitmap walk");
1333 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1335 if (!wants_bitmap)
1336 BUG("failed to perform bitmap walk");
1338 if (haves_bitmap)
1339 bitmap_and_not(wants_bitmap, haves_bitmap);
1341 filter_bitmap(bitmap_git,
1342 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1343 wants_bitmap,
1344 &revs->filter);
1346 bitmap_git->result = wants_bitmap;
1347 bitmap_git->haves = haves_bitmap;
1349 object_list_free(&wants);
1350 object_list_free(&haves);
1352 return bitmap_git;
1354 cleanup:
1355 free_bitmap_index(bitmap_git);
1356 object_list_free(&wants);
1357 object_list_free(&haves);
1358 return NULL;
1362 * -1 means "stop trying further objects"; 0 means we may or may not have
1363 * reused, but you can keep feeding bits.
1365 static int try_partial_reuse(struct packed_git *pack,
1366 size_t pos,
1367 struct bitmap *reuse,
1368 struct pack_window **w_curs)
1370 off_t offset, delta_obj_offset;
1371 enum object_type type;
1372 unsigned long size;
1375 * try_partial_reuse() is called either on (a) objects in the
1376 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1377 * objects in the preferred pack of a multi-pack bitmap.
1378 * Importantly, the latter can pretend as if only a single pack
1379 * exists because:
1381 * - The first pack->num_objects bits of a MIDX bitmap are
1382 * reserved for the preferred pack, and
1384 * - Ties due to duplicate objects are always resolved in
1385 * favor of the preferred pack.
1387 * Therefore we do not need to ever ask the MIDX for its copy of
1388 * an object by OID, since it will always select it from the
1389 * preferred pack. Likewise, the selected copy of the base
1390 * object for any deltas will reside in the same pack.
1392 * This means that we can reuse pos when looking up the bit in
1393 * the reuse bitmap, too, since bits corresponding to the
1394 * preferred pack precede all bits from other packs.
1397 if (pos >= pack->num_objects)
1398 return -1; /* not actually in the pack or MIDX preferred pack */
1400 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1401 type = unpack_object_header(pack, w_curs, &offset, &size);
1402 if (type < 0)
1403 return -1; /* broken packfile, punt */
1405 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1406 off_t base_offset;
1407 uint32_t base_pos;
1410 * Find the position of the base object so we can look it up
1411 * in our bitmaps. If we can't come up with an offset, or if
1412 * that offset is not in the revidx, the pack is corrupt.
1413 * There's nothing we can do, so just punt on this object,
1414 * and the normal slow path will complain about it in
1415 * more detail.
1417 base_offset = get_delta_base(pack, w_curs, &offset, type,
1418 delta_obj_offset);
1419 if (!base_offset)
1420 return 0;
1421 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1422 return 0;
1425 * We assume delta dependencies always point backwards. This
1426 * lets us do a single pass, and is basically always true
1427 * due to the way OFS_DELTAs work. You would not typically
1428 * find REF_DELTA in a bitmapped pack, since we only bitmap
1429 * packs we write fresh, and OFS_DELTA is the default). But
1430 * let's double check to make sure the pack wasn't written with
1431 * odd parameters.
1433 if (base_pos >= pos)
1434 return 0;
1437 * And finally, if we're not sending the base as part of our
1438 * reuse chunk, then don't send this object either. The base
1439 * would come after us, along with other objects not
1440 * necessarily in the pack, which means we'd need to convert
1441 * to REF_DELTA on the fly. Better to just let the normal
1442 * object_entry code path handle it.
1444 if (!bitmap_get(reuse, base_pos))
1445 return 0;
1449 * If we got here, then the object is OK to reuse. Mark it.
1451 bitmap_set(reuse, pos);
1452 return 0;
1455 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1457 struct multi_pack_index *m = bitmap_git->midx;
1458 if (!m)
1459 BUG("midx_preferred_pack: requires non-empty MIDX");
1460 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1463 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1464 struct packed_git **packfile_out,
1465 uint32_t *entries,
1466 struct bitmap **reuse_out)
1468 struct packed_git *pack;
1469 struct bitmap *result = bitmap_git->result;
1470 struct bitmap *reuse;
1471 struct pack_window *w_curs = NULL;
1472 size_t i = 0;
1473 uint32_t offset;
1474 uint32_t objects_nr;
1476 assert(result);
1478 load_reverse_index(bitmap_git);
1480 if (bitmap_is_midx(bitmap_git))
1481 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1482 else
1483 pack = bitmap_git->pack;
1484 objects_nr = pack->num_objects;
1486 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1487 i++;
1490 * Don't mark objects not in the packfile or preferred pack. This bitmap
1491 * marks objects eligible for reuse, but the pack-reuse code only
1492 * understands how to reuse a single pack. Since the preferred pack is
1493 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1494 * we use it instead of another pack. In single-pack bitmaps, the choice
1495 * is made for us.
1497 if (i > objects_nr / BITS_IN_EWORD)
1498 i = objects_nr / BITS_IN_EWORD;
1500 reuse = bitmap_word_alloc(i);
1501 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1503 for (; i < result->word_alloc; ++i) {
1504 eword_t word = result->words[i];
1505 size_t pos = (i * BITS_IN_EWORD);
1507 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1508 if ((word >> offset) == 0)
1509 break;
1511 offset += ewah_bit_ctz64(word >> offset);
1512 if (try_partial_reuse(pack, pos + offset,
1513 reuse, &w_curs) < 0) {
1515 * try_partial_reuse indicated we couldn't reuse
1516 * any bits, so there is no point in trying more
1517 * bits in the current word, or any other words
1518 * in result.
1520 * Jump out of both loops to avoid future
1521 * unnecessary calls to try_partial_reuse.
1523 goto done;
1528 done:
1529 unuse_pack(&w_curs);
1531 *entries = bitmap_popcount(reuse);
1532 if (!*entries) {
1533 bitmap_free(reuse);
1534 return -1;
1538 * Drop any reused objects from the result, since they will not
1539 * need to be handled separately.
1541 bitmap_and_not(result, reuse);
1542 *packfile_out = pack;
1543 *reuse_out = reuse;
1544 return 0;
1547 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1548 struct bitmap *bitmap, const struct object_id *oid)
1550 int idx;
1552 if (!bitmap)
1553 return 0;
1555 idx = bitmap_position(bitmap_git, oid);
1556 return idx >= 0 && bitmap_get(bitmap, idx);
1559 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1560 struct rev_info *revs,
1561 show_reachable_fn show_reachable)
1563 assert(bitmap_git->result);
1565 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1566 if (revs->tree_objects)
1567 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1568 if (revs->blob_objects)
1569 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1570 if (revs->tag_objects)
1571 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1573 show_extended_objects(bitmap_git, revs, show_reachable);
1576 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1577 enum object_type type)
1579 struct bitmap *objects = bitmap_git->result;
1580 struct eindex *eindex = &bitmap_git->ext_index;
1582 uint32_t i = 0, count = 0;
1583 struct ewah_iterator it;
1584 eword_t filter;
1586 init_type_iterator(&it, bitmap_git, type);
1588 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1589 eword_t word = objects->words[i++] & filter;
1590 count += ewah_bit_popcount64(word);
1593 for (i = 0; i < eindex->count; ++i) {
1594 if (eindex->objects[i]->type == type &&
1595 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1596 count++;
1599 return count;
1602 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1603 uint32_t *commits, uint32_t *trees,
1604 uint32_t *blobs, uint32_t *tags)
1606 assert(bitmap_git->result);
1608 if (commits)
1609 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1611 if (trees)
1612 *trees = count_object_type(bitmap_git, OBJ_TREE);
1614 if (blobs)
1615 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1617 if (tags)
1618 *tags = count_object_type(bitmap_git, OBJ_TAG);
1621 struct bitmap_test_data {
1622 struct bitmap_index *bitmap_git;
1623 struct bitmap *base;
1624 struct bitmap *commits;
1625 struct bitmap *trees;
1626 struct bitmap *blobs;
1627 struct bitmap *tags;
1628 struct progress *prg;
1629 size_t seen;
1632 static void test_bitmap_type(struct bitmap_test_data *tdata,
1633 struct object *obj, int pos)
1635 enum object_type bitmap_type = OBJ_NONE;
1636 int bitmaps_nr = 0;
1638 if (bitmap_get(tdata->commits, pos)) {
1639 bitmap_type = OBJ_COMMIT;
1640 bitmaps_nr++;
1642 if (bitmap_get(tdata->trees, pos)) {
1643 bitmap_type = OBJ_TREE;
1644 bitmaps_nr++;
1646 if (bitmap_get(tdata->blobs, pos)) {
1647 bitmap_type = OBJ_BLOB;
1648 bitmaps_nr++;
1650 if (bitmap_get(tdata->tags, pos)) {
1651 bitmap_type = OBJ_TAG;
1652 bitmaps_nr++;
1655 if (bitmap_type == OBJ_NONE)
1656 die(_("object '%s' not found in type bitmaps"),
1657 oid_to_hex(&obj->oid));
1659 if (bitmaps_nr > 1)
1660 die(_("object '%s' does not have a unique type"),
1661 oid_to_hex(&obj->oid));
1663 if (bitmap_type != obj->type)
1664 die(_("object '%s': real type '%s', expected: '%s'"),
1665 oid_to_hex(&obj->oid),
1666 type_name(obj->type),
1667 type_name(bitmap_type));
1670 static void test_show_object(struct object *object, const char *name,
1671 void *data)
1673 struct bitmap_test_data *tdata = data;
1674 int bitmap_pos;
1676 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1677 if (bitmap_pos < 0)
1678 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
1679 test_bitmap_type(tdata, object, bitmap_pos);
1681 bitmap_set(tdata->base, bitmap_pos);
1682 display_progress(tdata->prg, ++tdata->seen);
1685 static void test_show_commit(struct commit *commit, void *data)
1687 struct bitmap_test_data *tdata = data;
1688 int bitmap_pos;
1690 bitmap_pos = bitmap_position(tdata->bitmap_git,
1691 &commit->object.oid);
1692 if (bitmap_pos < 0)
1693 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
1694 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1696 bitmap_set(tdata->base, bitmap_pos);
1697 display_progress(tdata->prg, ++tdata->seen);
1700 void test_bitmap_walk(struct rev_info *revs)
1702 struct object *root;
1703 struct bitmap *result = NULL;
1704 size_t result_popcnt;
1705 struct bitmap_test_data tdata;
1706 struct bitmap_index *bitmap_git;
1707 struct ewah_bitmap *bm;
1709 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1710 die(_("failed to load bitmap indexes"));
1712 if (revs->pending.nr != 1)
1713 die(_("you must specify exactly one commit to test"));
1715 fprintf_ln(stderr, "Bitmap v%d test (%d entries loaded)",
1716 bitmap_git->version, bitmap_git->entry_count);
1718 root = revs->pending.objects[0].item;
1719 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
1721 if (bm) {
1722 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
1723 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
1725 result = ewah_to_bitmap(bm);
1728 if (!result)
1729 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
1731 revs->tag_objects = 1;
1732 revs->tree_objects = 1;
1733 revs->blob_objects = 1;
1735 result_popcnt = bitmap_popcount(result);
1737 if (prepare_revision_walk(revs))
1738 die(_("revision walk setup failed"));
1740 tdata.bitmap_git = bitmap_git;
1741 tdata.base = bitmap_new();
1742 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
1743 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
1744 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
1745 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
1746 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1747 tdata.seen = 0;
1749 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1751 stop_progress(&tdata.prg);
1753 if (bitmap_equals(result, tdata.base))
1754 fprintf_ln(stderr, "OK!");
1755 else
1756 die(_("mismatch in bitmap results"));
1758 bitmap_free(result);
1759 bitmap_free(tdata.base);
1760 bitmap_free(tdata.commits);
1761 bitmap_free(tdata.trees);
1762 bitmap_free(tdata.blobs);
1763 bitmap_free(tdata.tags);
1764 free_bitmap_index(bitmap_git);
1767 int test_bitmap_commits(struct repository *r)
1769 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1770 struct object_id oid;
1771 MAYBE_UNUSED void *value;
1773 if (!bitmap_git)
1774 die(_("failed to load bitmap indexes"));
1776 kh_foreach(bitmap_git->bitmaps, oid, value, {
1777 printf_ln("%s", oid_to_hex(&oid));
1780 free_bitmap_index(bitmap_git);
1782 return 0;
1785 int test_bitmap_hashes(struct repository *r)
1787 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1788 struct object_id oid;
1789 uint32_t i, index_pos;
1791 if (!bitmap_git || !bitmap_git->hashes)
1792 goto cleanup;
1794 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
1795 if (bitmap_is_midx(bitmap_git))
1796 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
1797 else
1798 index_pos = pack_pos_to_index(bitmap_git->pack, i);
1800 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1802 printf_ln("%s %"PRIu32"",
1803 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
1806 cleanup:
1807 free_bitmap_index(bitmap_git);
1809 return 0;
1812 int rebuild_bitmap(const uint32_t *reposition,
1813 struct ewah_bitmap *source,
1814 struct bitmap *dest)
1816 uint32_t pos = 0;
1817 struct ewah_iterator it;
1818 eword_t word;
1820 ewah_iterator_init(&it, source);
1822 while (ewah_iterator_next(&word, &it)) {
1823 uint32_t offset, bit_pos;
1825 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1826 if ((word >> offset) == 0)
1827 break;
1829 offset += ewah_bit_ctz64(word >> offset);
1831 bit_pos = reposition[pos + offset];
1832 if (bit_pos > 0)
1833 bitmap_set(dest, bit_pos - 1);
1834 else /* can't reuse, we don't have the object */
1835 return -1;
1838 pos += BITS_IN_EWORD;
1840 return 0;
1843 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1844 struct packing_data *mapping)
1846 uint32_t i, num_objects;
1847 uint32_t *reposition;
1849 if (!bitmap_is_midx(bitmap_git))
1850 load_reverse_index(bitmap_git);
1851 else if (load_midx_revindex(bitmap_git->midx) < 0)
1852 BUG("rebuild_existing_bitmaps: missing required rev-cache "
1853 "extension");
1855 num_objects = bitmap_num_objects(bitmap_git);
1856 CALLOC_ARRAY(reposition, num_objects);
1858 for (i = 0; i < num_objects; ++i) {
1859 struct object_id oid;
1860 struct object_entry *oe;
1861 uint32_t index_pos;
1863 if (bitmap_is_midx(bitmap_git))
1864 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
1865 else
1866 index_pos = pack_pos_to_index(bitmap_git->pack, i);
1867 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1868 oe = packlist_find(mapping, &oid);
1870 if (oe) {
1871 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
1872 if (bitmap_git->hashes && !oe->hash)
1873 oe->hash = get_be32(bitmap_git->hashes + index_pos);
1877 return reposition;
1880 void free_bitmap_index(struct bitmap_index *b)
1882 if (!b)
1883 return;
1885 if (b->map)
1886 munmap(b->map, b->map_size);
1887 ewah_pool_free(b->commits);
1888 ewah_pool_free(b->trees);
1889 ewah_pool_free(b->blobs);
1890 ewah_pool_free(b->tags);
1891 if (b->bitmaps) {
1892 struct stored_bitmap *sb;
1893 kh_foreach_value(b->bitmaps, sb, {
1894 ewah_pool_free(sb->root);
1895 free(sb);
1898 kh_destroy_oid_map(b->bitmaps);
1899 free(b->ext_index.objects);
1900 free(b->ext_index.hashes);
1901 kh_destroy_oid_pos(b->ext_index.positions);
1902 bitmap_free(b->result);
1903 bitmap_free(b->haves);
1904 if (bitmap_is_midx(b)) {
1906 * Multi-pack bitmaps need to have resources associated with
1907 * their on-disk reverse indexes unmapped so that stale .rev and
1908 * .bitmap files can be removed.
1910 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
1911 * written in the same 'git multi-pack-index write --bitmap'
1912 * process. Close resources so they can be removed safely on
1913 * platforms like Windows.
1915 close_midx_revindex(b->midx);
1917 free(b);
1920 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1921 const struct object_id *oid)
1923 return bitmap_git &&
1924 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
1927 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1928 enum object_type object_type)
1930 struct bitmap *result = bitmap_git->result;
1931 off_t total = 0;
1932 struct ewah_iterator it;
1933 eword_t filter;
1934 size_t i;
1936 init_type_iterator(&it, bitmap_git, object_type);
1937 for (i = 0; i < result->word_alloc &&
1938 ewah_iterator_next(&filter, &it); i++) {
1939 eword_t word = result->words[i] & filter;
1940 size_t base = (i * BITS_IN_EWORD);
1941 unsigned offset;
1943 if (!word)
1944 continue;
1946 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1947 if ((word >> offset) == 0)
1948 break;
1950 offset += ewah_bit_ctz64(word >> offset);
1952 if (bitmap_is_midx(bitmap_git)) {
1953 uint32_t pack_pos;
1954 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
1955 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
1957 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1958 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
1960 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
1961 struct object_id oid;
1962 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
1964 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
1965 oid_to_hex(&oid),
1966 pack->pack_name,
1967 (uintmax_t)offset);
1970 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
1971 } else {
1972 size_t pos = base + offset;
1973 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
1974 pack_pos_to_offset(bitmap_git->pack, pos);
1979 return total;
1982 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1984 struct bitmap *result = bitmap_git->result;
1985 struct eindex *eindex = &bitmap_git->ext_index;
1986 off_t total = 0;
1987 struct object_info oi = OBJECT_INFO_INIT;
1988 off_t object_size;
1989 size_t i;
1991 oi.disk_sizep = &object_size;
1993 for (i = 0; i < eindex->count; i++) {
1994 struct object *obj = eindex->objects[i];
1996 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
1997 continue;
1999 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2000 die(_("unable to get disk usage of '%s'"),
2001 oid_to_hex(&obj->oid));
2003 total += object_size;
2005 return total;
2008 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2009 struct rev_info *revs)
2011 off_t total = 0;
2013 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2014 if (revs->tree_objects)
2015 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2016 if (revs->blob_objects)
2017 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2018 if (revs->tag_objects)
2019 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2021 total += get_disk_usage_for_extended(bitmap_git);
2023 return total;
2026 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2028 return !!bitmap_git->midx;
2031 const struct string_list *bitmap_preferred_tips(struct repository *r)
2033 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
2036 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2038 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2039 struct string_list_item *item;
2041 if (!preferred_tips)
2042 return 0;
2044 for_each_string_list_item(item, preferred_tips) {
2045 if (starts_with(refname, item->string))
2046 return 1;
2049 return 0;