diff tests: don't ignore "git rev-list" exit code
[alt-git.git] / pack-bitmap.c
blob9c666cdb8bd9203f0749b54b6c012ba33896341e
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_pos = 0;
362 bitmap_git->map = NULL;
363 bitmap_git->midx = NULL;
364 return -1;
367 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
369 int fd;
370 struct stat st;
371 char *idx_name;
373 if (open_pack_index(packfile))
374 return -1;
376 idx_name = pack_bitmap_filename(packfile);
377 fd = git_open(idx_name);
378 free(idx_name);
380 if (fd < 0)
381 return -1;
383 if (fstat(fd, &st)) {
384 close(fd);
385 return -1;
388 if (bitmap_git->pack || bitmap_git->midx) {
389 /* ignore extra bitmap file; we can only handle one */
390 warning("ignoring extra bitmap file: %s", packfile->pack_name);
391 close(fd);
392 return -1;
395 if (!is_pack_valid(packfile)) {
396 close(fd);
397 return -1;
400 bitmap_git->pack = packfile;
401 bitmap_git->map_size = xsize_t(st.st_size);
402 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
403 bitmap_git->map_pos = 0;
404 close(fd);
406 if (load_bitmap_header(bitmap_git) < 0) {
407 munmap(bitmap_git->map, bitmap_git->map_size);
408 bitmap_git->map = NULL;
409 bitmap_git->map_size = 0;
410 bitmap_git->map_pos = 0;
411 bitmap_git->pack = NULL;
412 return -1;
415 return 0;
418 static int load_reverse_index(struct bitmap_index *bitmap_git)
420 if (bitmap_is_midx(bitmap_git)) {
421 uint32_t i;
422 int ret;
425 * The multi-pack-index's .rev file is already loaded via
426 * open_pack_bitmap_1().
428 * But we still need to open the individual pack .rev files,
429 * since we will need to make use of them in pack-objects.
431 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
432 if (prepare_midx_pack(the_repository, bitmap_git->midx, i))
433 die(_("load_reverse_index: could not open pack"));
434 ret = load_pack_revindex(bitmap_git->midx->packs[i]);
435 if (ret)
436 return ret;
438 return 0;
440 return load_pack_revindex(bitmap_git->pack);
443 static int load_bitmap(struct bitmap_index *bitmap_git)
445 assert(bitmap_git->map);
447 bitmap_git->bitmaps = kh_init_oid_map();
448 bitmap_git->ext_index.positions = kh_init_oid_pos();
450 if (load_reverse_index(bitmap_git))
451 goto failed;
453 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
454 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
455 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
456 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
457 goto failed;
459 if (load_bitmap_entries_v1(bitmap_git) < 0)
460 goto failed;
462 return 0;
464 failed:
465 munmap(bitmap_git->map, bitmap_git->map_size);
466 bitmap_git->map = NULL;
467 bitmap_git->map_size = 0;
469 kh_destroy_oid_map(bitmap_git->bitmaps);
470 bitmap_git->bitmaps = NULL;
472 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
473 bitmap_git->ext_index.positions = NULL;
475 return -1;
478 static int open_pack_bitmap(struct repository *r,
479 struct bitmap_index *bitmap_git)
481 struct packed_git *p;
482 int ret = -1;
484 assert(!bitmap_git->map);
486 for (p = get_all_packs(r); p; p = p->next) {
487 if (open_pack_bitmap_1(bitmap_git, p) == 0)
488 ret = 0;
491 return ret;
494 static int open_midx_bitmap(struct repository *r,
495 struct bitmap_index *bitmap_git)
497 struct multi_pack_index *midx;
499 assert(!bitmap_git->map);
501 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
502 if (!open_midx_bitmap_1(bitmap_git, midx))
503 return 0;
505 return -1;
508 static int open_bitmap(struct repository *r,
509 struct bitmap_index *bitmap_git)
511 assert(!bitmap_git->map);
513 if (!open_midx_bitmap(r, bitmap_git))
514 return 0;
515 return open_pack_bitmap(r, bitmap_git);
518 struct bitmap_index *prepare_bitmap_git(struct repository *r)
520 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
522 if (!open_bitmap(r, bitmap_git) && !load_bitmap(bitmap_git))
523 return bitmap_git;
525 free_bitmap_index(bitmap_git);
526 return NULL;
529 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
531 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
533 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(bitmap_git))
534 return bitmap_git;
536 free_bitmap_index(bitmap_git);
537 return NULL;
540 struct include_data {
541 struct bitmap_index *bitmap_git;
542 struct bitmap *base;
543 struct bitmap *seen;
546 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
547 struct commit *commit)
549 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
550 commit->object.oid);
551 if (hash_pos >= kh_end(bitmap_git->bitmaps))
552 return NULL;
553 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
556 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
557 const struct object_id *oid)
559 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
560 khiter_t pos = kh_get_oid_pos(positions, *oid);
562 if (pos < kh_end(positions)) {
563 int bitmap_pos = kh_value(positions, pos);
564 return bitmap_pos + bitmap_num_objects(bitmap_git);
567 return -1;
570 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
571 const struct object_id *oid)
573 uint32_t pos;
574 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
575 if (!offset)
576 return -1;
578 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
579 return -1;
580 return pos;
583 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
584 const struct object_id *oid)
586 uint32_t want, got;
587 if (!bsearch_midx(oid, bitmap_git->midx, &want))
588 return -1;
590 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
591 return -1;
592 return got;
595 static int bitmap_position(struct bitmap_index *bitmap_git,
596 const struct object_id *oid)
598 int pos;
599 if (bitmap_is_midx(bitmap_git))
600 pos = bitmap_position_midx(bitmap_git, oid);
601 else
602 pos = bitmap_position_packfile(bitmap_git, oid);
603 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
606 static int ext_index_add_object(struct bitmap_index *bitmap_git,
607 struct object *object, const char *name)
609 struct eindex *eindex = &bitmap_git->ext_index;
611 khiter_t hash_pos;
612 int hash_ret;
613 int bitmap_pos;
615 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
616 if (hash_ret > 0) {
617 if (eindex->count >= eindex->alloc) {
618 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
619 REALLOC_ARRAY(eindex->objects, eindex->alloc);
620 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
623 bitmap_pos = eindex->count;
624 eindex->objects[eindex->count] = object;
625 eindex->hashes[eindex->count] = pack_name_hash(name);
626 kh_value(eindex->positions, hash_pos) = bitmap_pos;
627 eindex->count++;
628 } else {
629 bitmap_pos = kh_value(eindex->positions, hash_pos);
632 return bitmap_pos + bitmap_num_objects(bitmap_git);
635 struct bitmap_show_data {
636 struct bitmap_index *bitmap_git;
637 struct bitmap *base;
640 static void show_object(struct object *object, const char *name, void *data_)
642 struct bitmap_show_data *data = data_;
643 int bitmap_pos;
645 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
647 if (bitmap_pos < 0)
648 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
649 name);
651 bitmap_set(data->base, bitmap_pos);
654 static void show_commit(struct commit *commit, void *data)
658 static int add_to_include_set(struct bitmap_index *bitmap_git,
659 struct include_data *data,
660 struct commit *commit,
661 int bitmap_pos)
663 struct ewah_bitmap *partial;
665 if (data->seen && bitmap_get(data->seen, bitmap_pos))
666 return 0;
668 if (bitmap_get(data->base, bitmap_pos))
669 return 0;
671 partial = bitmap_for_commit(bitmap_git, commit);
672 if (partial) {
673 bitmap_or_ewah(data->base, partial);
674 return 0;
677 bitmap_set(data->base, bitmap_pos);
678 return 1;
681 static int should_include(struct commit *commit, void *_data)
683 struct include_data *data = _data;
684 int bitmap_pos;
686 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
687 if (bitmap_pos < 0)
688 bitmap_pos = ext_index_add_object(data->bitmap_git,
689 (struct object *)commit,
690 NULL);
692 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
693 struct commit_list *parent = commit->parents;
695 while (parent) {
696 parent->item->object.flags |= SEEN;
697 parent = parent->next;
700 return 0;
703 return 1;
706 static int should_include_obj(struct object *obj, void *_data)
708 struct include_data *data = _data;
709 int bitmap_pos;
711 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
712 if (bitmap_pos < 0)
713 return 1;
714 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
715 bitmap_get(data->base, bitmap_pos)) {
716 obj->flags |= SEEN;
717 return 0;
719 return 1;
722 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
723 struct bitmap **base,
724 struct commit *commit)
726 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
728 if (!or_with)
729 return 0;
731 if (*base == NULL)
732 *base = ewah_to_bitmap(or_with);
733 else
734 bitmap_or_ewah(*base, or_with);
736 return 1;
739 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
740 struct rev_info *revs,
741 struct object_list *roots,
742 struct bitmap *seen,
743 struct list_objects_filter_options *filter)
745 struct bitmap *base = NULL;
746 int needs_walk = 0;
748 struct object_list *not_mapped = NULL;
751 * Go through all the roots for the walk. The ones that have bitmaps
752 * on the bitmap index will be `or`ed together to form an initial
753 * global reachability analysis.
755 * The ones without bitmaps in the index will be stored in the
756 * `not_mapped_list` for further processing.
758 while (roots) {
759 struct object *object = roots->item;
760 roots = roots->next;
762 if (object->type == OBJ_COMMIT &&
763 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
764 object->flags |= SEEN;
765 continue;
768 object_list_insert(object, &not_mapped);
772 * Best case scenario: We found bitmaps for all the roots,
773 * so the resulting `or` bitmap has the full reachability analysis
775 if (not_mapped == NULL)
776 return base;
778 roots = not_mapped;
781 * Let's iterate through all the roots that don't have bitmaps to
782 * check if we can determine them to be reachable from the existing
783 * global bitmap.
785 * If we cannot find them in the existing global bitmap, we'll need
786 * to push them to an actual walk and run it until we can confirm
787 * they are reachable
789 while (roots) {
790 struct object *object = roots->item;
791 int pos;
793 roots = roots->next;
794 pos = bitmap_position(bitmap_git, &object->oid);
796 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
797 object->flags &= ~UNINTERESTING;
798 add_pending_object(revs, object, "");
799 needs_walk = 1;
800 } else {
801 object->flags |= SEEN;
805 if (needs_walk) {
806 struct include_data incdata;
807 struct bitmap_show_data show_data;
809 if (base == NULL)
810 base = bitmap_new();
812 incdata.bitmap_git = bitmap_git;
813 incdata.base = base;
814 incdata.seen = seen;
816 revs->include_check = should_include;
817 revs->include_check_obj = should_include_obj;
818 revs->include_check_data = &incdata;
820 if (prepare_revision_walk(revs))
821 die("revision walk setup failed");
823 show_data.bitmap_git = bitmap_git;
824 show_data.base = base;
826 traverse_commit_list_filtered(filter, revs,
827 show_commit, show_object,
828 &show_data, NULL);
830 revs->include_check = NULL;
831 revs->include_check_obj = NULL;
832 revs->include_check_data = NULL;
835 return base;
838 static void show_extended_objects(struct bitmap_index *bitmap_git,
839 struct rev_info *revs,
840 show_reachable_fn show_reach)
842 struct bitmap *objects = bitmap_git->result;
843 struct eindex *eindex = &bitmap_git->ext_index;
844 uint32_t i;
846 for (i = 0; i < eindex->count; ++i) {
847 struct object *obj;
849 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
850 continue;
852 obj = eindex->objects[i];
853 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
854 (obj->type == OBJ_TREE && !revs->tree_objects) ||
855 (obj->type == OBJ_TAG && !revs->tag_objects))
856 continue;
858 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
862 static void init_type_iterator(struct ewah_iterator *it,
863 struct bitmap_index *bitmap_git,
864 enum object_type type)
866 switch (type) {
867 case OBJ_COMMIT:
868 ewah_iterator_init(it, bitmap_git->commits);
869 break;
871 case OBJ_TREE:
872 ewah_iterator_init(it, bitmap_git->trees);
873 break;
875 case OBJ_BLOB:
876 ewah_iterator_init(it, bitmap_git->blobs);
877 break;
879 case OBJ_TAG:
880 ewah_iterator_init(it, bitmap_git->tags);
881 break;
883 default:
884 BUG("object type %d not stored by bitmap type index", type);
885 break;
889 static void show_objects_for_type(
890 struct bitmap_index *bitmap_git,
891 enum object_type object_type,
892 show_reachable_fn show_reach)
894 size_t i = 0;
895 uint32_t offset;
897 struct ewah_iterator it;
898 eword_t filter;
900 struct bitmap *objects = bitmap_git->result;
902 init_type_iterator(&it, bitmap_git, object_type);
904 for (i = 0; i < objects->word_alloc &&
905 ewah_iterator_next(&filter, &it); i++) {
906 eword_t word = objects->words[i] & filter;
907 size_t pos = (i * BITS_IN_EWORD);
909 if (!word)
910 continue;
912 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
913 struct packed_git *pack;
914 struct object_id oid;
915 uint32_t hash = 0, index_pos;
916 off_t ofs;
918 if ((word >> offset) == 0)
919 break;
921 offset += ewah_bit_ctz64(word >> offset);
923 if (bitmap_is_midx(bitmap_git)) {
924 struct multi_pack_index *m = bitmap_git->midx;
925 uint32_t pack_id;
927 index_pos = pack_pos_to_midx(m, pos + offset);
928 ofs = nth_midxed_offset(m, index_pos);
929 nth_midxed_object_oid(&oid, m, index_pos);
931 pack_id = nth_midxed_pack_int_id(m, index_pos);
932 pack = bitmap_git->midx->packs[pack_id];
933 } else {
934 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
935 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
936 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
938 pack = bitmap_git->pack;
941 if (bitmap_git->hashes)
942 hash = get_be32(bitmap_git->hashes + index_pos);
944 show_reach(&oid, object_type, 0, hash, pack, ofs);
949 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
950 struct object_list *roots)
952 while (roots) {
953 struct object *object = roots->item;
954 roots = roots->next;
956 if (bitmap_is_midx(bitmap_git)) {
957 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
958 return 1;
959 } else {
960 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
961 return 1;
965 return 0;
968 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
969 struct object_list *tip_objects,
970 enum object_type type)
972 struct bitmap *result = bitmap_new();
973 struct object_list *p;
975 for (p = tip_objects; p; p = p->next) {
976 int pos;
978 if (p->item->type != type)
979 continue;
981 pos = bitmap_position(bitmap_git, &p->item->oid);
982 if (pos < 0)
983 continue;
985 bitmap_set(result, pos);
988 return result;
991 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
992 struct object_list *tip_objects,
993 struct bitmap *to_filter,
994 enum object_type type)
996 struct eindex *eindex = &bitmap_git->ext_index;
997 struct bitmap *tips;
998 struct ewah_iterator it;
999 eword_t mask;
1000 uint32_t i;
1003 * The non-bitmap version of this filter never removes
1004 * objects which the other side specifically asked for,
1005 * so we must match that behavior.
1007 tips = find_tip_objects(bitmap_git, tip_objects, type);
1010 * We can use the type-level bitmap for 'type' to work in whole
1011 * words for the objects that are actually in the bitmapped
1012 * packfile.
1014 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1015 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1016 i++) {
1017 if (i < tips->word_alloc)
1018 mask &= ~tips->words[i];
1019 to_filter->words[i] &= ~mask;
1023 * Clear any objects that weren't in the packfile (and so would
1024 * not have been caught by the loop above. We'll have to check
1025 * them individually.
1027 for (i = 0; i < eindex->count; i++) {
1028 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1029 if (eindex->objects[i]->type == type &&
1030 bitmap_get(to_filter, pos) &&
1031 !bitmap_get(tips, pos))
1032 bitmap_unset(to_filter, pos);
1035 bitmap_free(tips);
1038 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1039 struct object_list *tip_objects,
1040 struct bitmap *to_filter)
1042 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1043 OBJ_BLOB);
1046 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1047 uint32_t pos)
1049 unsigned long size;
1050 struct object_info oi = OBJECT_INFO_INIT;
1052 oi.sizep = &size;
1054 if (pos < bitmap_num_objects(bitmap_git)) {
1055 struct packed_git *pack;
1056 off_t ofs;
1058 if (bitmap_is_midx(bitmap_git)) {
1059 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1060 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1062 pack = bitmap_git->midx->packs[pack_id];
1063 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1064 } else {
1065 pack = bitmap_git->pack;
1066 ofs = pack_pos_to_offset(pack, pos);
1069 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1070 struct object_id oid;
1071 nth_bitmap_object_oid(bitmap_git, &oid,
1072 pack_pos_to_index(pack, pos));
1073 die(_("unable to get size of %s"), oid_to_hex(&oid));
1075 } else {
1076 struct eindex *eindex = &bitmap_git->ext_index;
1077 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1078 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1079 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1082 return size;
1085 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1086 struct object_list *tip_objects,
1087 struct bitmap *to_filter,
1088 unsigned long limit)
1090 struct eindex *eindex = &bitmap_git->ext_index;
1091 struct bitmap *tips;
1092 struct ewah_iterator it;
1093 eword_t mask;
1094 uint32_t i;
1096 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1098 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1099 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1100 i++) {
1101 eword_t word = to_filter->words[i] & mask;
1102 unsigned offset;
1104 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1105 uint32_t pos;
1107 if ((word >> offset) == 0)
1108 break;
1109 offset += ewah_bit_ctz64(word >> offset);
1110 pos = i * BITS_IN_EWORD + offset;
1112 if (!bitmap_get(tips, pos) &&
1113 get_size_by_pos(bitmap_git, pos) >= limit)
1114 bitmap_unset(to_filter, pos);
1118 for (i = 0; i < eindex->count; i++) {
1119 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1120 if (eindex->objects[i]->type == OBJ_BLOB &&
1121 bitmap_get(to_filter, pos) &&
1122 !bitmap_get(tips, pos) &&
1123 get_size_by_pos(bitmap_git, pos) >= limit)
1124 bitmap_unset(to_filter, pos);
1127 bitmap_free(tips);
1130 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1131 struct object_list *tip_objects,
1132 struct bitmap *to_filter,
1133 unsigned long limit)
1135 if (limit)
1136 BUG("filter_bitmap_tree_depth given non-zero limit");
1138 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1139 OBJ_TREE);
1140 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1141 OBJ_BLOB);
1144 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1145 struct object_list *tip_objects,
1146 struct bitmap *to_filter,
1147 enum object_type object_type)
1149 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1150 BUG("filter_bitmap_object_type given invalid object");
1152 if (object_type != OBJ_TAG)
1153 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1154 if (object_type != OBJ_COMMIT)
1155 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1156 if (object_type != OBJ_TREE)
1157 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1158 if (object_type != OBJ_BLOB)
1159 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1162 static int filter_bitmap(struct bitmap_index *bitmap_git,
1163 struct object_list *tip_objects,
1164 struct bitmap *to_filter,
1165 struct list_objects_filter_options *filter)
1167 if (!filter || filter->choice == LOFC_DISABLED)
1168 return 0;
1170 if (filter->choice == LOFC_BLOB_NONE) {
1171 if (bitmap_git)
1172 filter_bitmap_blob_none(bitmap_git, tip_objects,
1173 to_filter);
1174 return 0;
1177 if (filter->choice == LOFC_BLOB_LIMIT) {
1178 if (bitmap_git)
1179 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1180 to_filter,
1181 filter->blob_limit_value);
1182 return 0;
1185 if (filter->choice == LOFC_TREE_DEPTH &&
1186 filter->tree_exclude_depth == 0) {
1187 if (bitmap_git)
1188 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1189 to_filter,
1190 filter->tree_exclude_depth);
1191 return 0;
1194 if (filter->choice == LOFC_OBJECT_TYPE) {
1195 if (bitmap_git)
1196 filter_bitmap_object_type(bitmap_git, tip_objects,
1197 to_filter,
1198 filter->object_type);
1199 return 0;
1202 if (filter->choice == LOFC_COMBINE) {
1203 int i;
1204 for (i = 0; i < filter->sub_nr; i++) {
1205 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1206 &filter->sub[i]) < 0)
1207 return -1;
1209 return 0;
1212 /* filter choice not handled */
1213 return -1;
1216 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1218 return !filter_bitmap(NULL, NULL, NULL, filter);
1221 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1222 struct list_objects_filter_options *filter,
1223 int filter_provided_objects)
1225 unsigned int i;
1227 struct object_list *wants = NULL;
1228 struct object_list *haves = NULL;
1230 struct bitmap *wants_bitmap = NULL;
1231 struct bitmap *haves_bitmap = NULL;
1233 struct bitmap_index *bitmap_git;
1236 * We can't do pathspec limiting with bitmaps, because we don't know
1237 * which commits are associated with which object changes (let alone
1238 * even which objects are associated with which paths).
1240 if (revs->prune)
1241 return NULL;
1243 if (!can_filter_bitmap(filter))
1244 return NULL;
1246 /* try to open a bitmapped pack, but don't parse it yet
1247 * because we may not need to use it */
1248 CALLOC_ARRAY(bitmap_git, 1);
1249 if (open_bitmap(revs->repo, bitmap_git) < 0)
1250 goto cleanup;
1252 for (i = 0; i < revs->pending.nr; ++i) {
1253 struct object *object = revs->pending.objects[i].item;
1255 if (object->type == OBJ_NONE)
1256 parse_object_or_die(&object->oid, NULL);
1258 while (object->type == OBJ_TAG) {
1259 struct tag *tag = (struct tag *) object;
1261 if (object->flags & UNINTERESTING)
1262 object_list_insert(object, &haves);
1263 else
1264 object_list_insert(object, &wants);
1266 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1267 object->flags |= (tag->object.flags & UNINTERESTING);
1270 if (object->flags & UNINTERESTING)
1271 object_list_insert(object, &haves);
1272 else
1273 object_list_insert(object, &wants);
1277 * if we have a HAVES list, but none of those haves is contained
1278 * in the packfile that has a bitmap, we don't have anything to
1279 * optimize here
1281 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1282 goto cleanup;
1284 /* if we don't want anything, we're done here */
1285 if (!wants)
1286 goto cleanup;
1289 * now we're going to use bitmaps, so load the actual bitmap entries
1290 * from disk. this is the point of no return; after this the rev_list
1291 * becomes invalidated and we must perform the revwalk through bitmaps
1293 if (load_bitmap(bitmap_git) < 0)
1294 goto cleanup;
1296 object_array_clear(&revs->pending);
1298 if (haves) {
1299 revs->ignore_missing_links = 1;
1300 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
1301 filter);
1302 reset_revision_walk();
1303 revs->ignore_missing_links = 0;
1305 if (haves_bitmap == NULL)
1306 BUG("failed to perform bitmap walk");
1309 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
1310 filter);
1312 if (!wants_bitmap)
1313 BUG("failed to perform bitmap walk");
1315 if (haves_bitmap)
1316 bitmap_and_not(wants_bitmap, haves_bitmap);
1318 filter_bitmap(bitmap_git, (filter && filter_provided_objects) ? NULL : wants,
1319 wants_bitmap, filter);
1321 bitmap_git->result = wants_bitmap;
1322 bitmap_git->haves = haves_bitmap;
1324 object_list_free(&wants);
1325 object_list_free(&haves);
1327 return bitmap_git;
1329 cleanup:
1330 free_bitmap_index(bitmap_git);
1331 object_list_free(&wants);
1332 object_list_free(&haves);
1333 return NULL;
1337 * -1 means "stop trying further objects"; 0 means we may or may not have
1338 * reused, but you can keep feeding bits.
1340 static int try_partial_reuse(struct packed_git *pack,
1341 size_t pos,
1342 struct bitmap *reuse,
1343 struct pack_window **w_curs)
1345 off_t offset, delta_obj_offset;
1346 enum object_type type;
1347 unsigned long size;
1350 * try_partial_reuse() is called either on (a) objects in the
1351 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1352 * objects in the preferred pack of a multi-pack bitmap.
1353 * Importantly, the latter can pretend as if only a single pack
1354 * exists because:
1356 * - The first pack->num_objects bits of a MIDX bitmap are
1357 * reserved for the preferred pack, and
1359 * - Ties due to duplicate objects are always resolved in
1360 * favor of the preferred pack.
1362 * Therefore we do not need to ever ask the MIDX for its copy of
1363 * an object by OID, since it will always select it from the
1364 * preferred pack. Likewise, the selected copy of the base
1365 * object for any deltas will reside in the same pack.
1367 * This means that we can reuse pos when looking up the bit in
1368 * the reuse bitmap, too, since bits corresponding to the
1369 * preferred pack precede all bits from other packs.
1372 if (pos >= pack->num_objects)
1373 return -1; /* not actually in the pack or MIDX preferred pack */
1375 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1376 type = unpack_object_header(pack, w_curs, &offset, &size);
1377 if (type < 0)
1378 return -1; /* broken packfile, punt */
1380 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1381 off_t base_offset;
1382 uint32_t base_pos;
1385 * Find the position of the base object so we can look it up
1386 * in our bitmaps. If we can't come up with an offset, or if
1387 * that offset is not in the revidx, the pack is corrupt.
1388 * There's nothing we can do, so just punt on this object,
1389 * and the normal slow path will complain about it in
1390 * more detail.
1392 base_offset = get_delta_base(pack, w_curs, &offset, type,
1393 delta_obj_offset);
1394 if (!base_offset)
1395 return 0;
1396 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1397 return 0;
1400 * We assume delta dependencies always point backwards. This
1401 * lets us do a single pass, and is basically always true
1402 * due to the way OFS_DELTAs work. You would not typically
1403 * find REF_DELTA in a bitmapped pack, since we only bitmap
1404 * packs we write fresh, and OFS_DELTA is the default). But
1405 * let's double check to make sure the pack wasn't written with
1406 * odd parameters.
1408 if (base_pos >= pos)
1409 return 0;
1412 * And finally, if we're not sending the base as part of our
1413 * reuse chunk, then don't send this object either. The base
1414 * would come after us, along with other objects not
1415 * necessarily in the pack, which means we'd need to convert
1416 * to REF_DELTA on the fly. Better to just let the normal
1417 * object_entry code path handle it.
1419 if (!bitmap_get(reuse, base_pos))
1420 return 0;
1424 * If we got here, then the object is OK to reuse. Mark it.
1426 bitmap_set(reuse, pos);
1427 return 0;
1430 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1432 struct multi_pack_index *m = bitmap_git->midx;
1433 if (!m)
1434 BUG("midx_preferred_pack: requires non-empty MIDX");
1435 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1438 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1439 struct packed_git **packfile_out,
1440 uint32_t *entries,
1441 struct bitmap **reuse_out)
1443 struct packed_git *pack;
1444 struct bitmap *result = bitmap_git->result;
1445 struct bitmap *reuse;
1446 struct pack_window *w_curs = NULL;
1447 size_t i = 0;
1448 uint32_t offset;
1449 uint32_t objects_nr;
1451 assert(result);
1453 load_reverse_index(bitmap_git);
1455 if (bitmap_is_midx(bitmap_git))
1456 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1457 else
1458 pack = bitmap_git->pack;
1459 objects_nr = pack->num_objects;
1461 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1462 i++;
1465 * Don't mark objects not in the packfile or preferred pack. This bitmap
1466 * marks objects eligible for reuse, but the pack-reuse code only
1467 * understands how to reuse a single pack. Since the preferred pack is
1468 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1469 * we use it instead of another pack. In single-pack bitmaps, the choice
1470 * is made for us.
1472 if (i > objects_nr / BITS_IN_EWORD)
1473 i = objects_nr / BITS_IN_EWORD;
1475 reuse = bitmap_word_alloc(i);
1476 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1478 for (; i < result->word_alloc; ++i) {
1479 eword_t word = result->words[i];
1480 size_t pos = (i * BITS_IN_EWORD);
1482 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1483 if ((word >> offset) == 0)
1484 break;
1486 offset += ewah_bit_ctz64(word >> offset);
1487 if (try_partial_reuse(pack, pos + offset,
1488 reuse, &w_curs) < 0) {
1490 * try_partial_reuse indicated we couldn't reuse
1491 * any bits, so there is no point in trying more
1492 * bits in the current word, or any other words
1493 * in result.
1495 * Jump out of both loops to avoid future
1496 * unnecessary calls to try_partial_reuse.
1498 goto done;
1503 done:
1504 unuse_pack(&w_curs);
1506 *entries = bitmap_popcount(reuse);
1507 if (!*entries) {
1508 bitmap_free(reuse);
1509 return -1;
1513 * Drop any reused objects from the result, since they will not
1514 * need to be handled separately.
1516 bitmap_and_not(result, reuse);
1517 *packfile_out = pack;
1518 *reuse_out = reuse;
1519 return 0;
1522 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1523 struct bitmap *bitmap, const struct object_id *oid)
1525 int idx;
1527 if (!bitmap)
1528 return 0;
1530 idx = bitmap_position(bitmap_git, oid);
1531 return idx >= 0 && bitmap_get(bitmap, idx);
1534 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1535 struct rev_info *revs,
1536 show_reachable_fn show_reachable)
1538 assert(bitmap_git->result);
1540 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1541 if (revs->tree_objects)
1542 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1543 if (revs->blob_objects)
1544 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1545 if (revs->tag_objects)
1546 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1548 show_extended_objects(bitmap_git, revs, show_reachable);
1551 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1552 enum object_type type)
1554 struct bitmap *objects = bitmap_git->result;
1555 struct eindex *eindex = &bitmap_git->ext_index;
1557 uint32_t i = 0, count = 0;
1558 struct ewah_iterator it;
1559 eword_t filter;
1561 init_type_iterator(&it, bitmap_git, type);
1563 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1564 eword_t word = objects->words[i++] & filter;
1565 count += ewah_bit_popcount64(word);
1568 for (i = 0; i < eindex->count; ++i) {
1569 if (eindex->objects[i]->type == type &&
1570 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1571 count++;
1574 return count;
1577 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1578 uint32_t *commits, uint32_t *trees,
1579 uint32_t *blobs, uint32_t *tags)
1581 assert(bitmap_git->result);
1583 if (commits)
1584 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1586 if (trees)
1587 *trees = count_object_type(bitmap_git, OBJ_TREE);
1589 if (blobs)
1590 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1592 if (tags)
1593 *tags = count_object_type(bitmap_git, OBJ_TAG);
1596 struct bitmap_test_data {
1597 struct bitmap_index *bitmap_git;
1598 struct bitmap *base;
1599 struct bitmap *commits;
1600 struct bitmap *trees;
1601 struct bitmap *blobs;
1602 struct bitmap *tags;
1603 struct progress *prg;
1604 size_t seen;
1607 static void test_bitmap_type(struct bitmap_test_data *tdata,
1608 struct object *obj, int pos)
1610 enum object_type bitmap_type = OBJ_NONE;
1611 int bitmaps_nr = 0;
1613 if (bitmap_get(tdata->commits, pos)) {
1614 bitmap_type = OBJ_COMMIT;
1615 bitmaps_nr++;
1617 if (bitmap_get(tdata->trees, pos)) {
1618 bitmap_type = OBJ_TREE;
1619 bitmaps_nr++;
1621 if (bitmap_get(tdata->blobs, pos)) {
1622 bitmap_type = OBJ_BLOB;
1623 bitmaps_nr++;
1625 if (bitmap_get(tdata->tags, pos)) {
1626 bitmap_type = OBJ_TAG;
1627 bitmaps_nr++;
1630 if (bitmap_type == OBJ_NONE)
1631 die("object %s not found in type bitmaps",
1632 oid_to_hex(&obj->oid));
1634 if (bitmaps_nr > 1)
1635 die("object %s does not have a unique type",
1636 oid_to_hex(&obj->oid));
1638 if (bitmap_type != obj->type)
1639 die("object %s: real type %s, expected: %s",
1640 oid_to_hex(&obj->oid),
1641 type_name(obj->type),
1642 type_name(bitmap_type));
1645 static void test_show_object(struct object *object, const char *name,
1646 void *data)
1648 struct bitmap_test_data *tdata = data;
1649 int bitmap_pos;
1651 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1652 if (bitmap_pos < 0)
1653 die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
1654 test_bitmap_type(tdata, object, bitmap_pos);
1656 bitmap_set(tdata->base, bitmap_pos);
1657 display_progress(tdata->prg, ++tdata->seen);
1660 static void test_show_commit(struct commit *commit, void *data)
1662 struct bitmap_test_data *tdata = data;
1663 int bitmap_pos;
1665 bitmap_pos = bitmap_position(tdata->bitmap_git,
1666 &commit->object.oid);
1667 if (bitmap_pos < 0)
1668 die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
1669 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1671 bitmap_set(tdata->base, bitmap_pos);
1672 display_progress(tdata->prg, ++tdata->seen);
1675 void test_bitmap_walk(struct rev_info *revs)
1677 struct object *root;
1678 struct bitmap *result = NULL;
1679 size_t result_popcnt;
1680 struct bitmap_test_data tdata;
1681 struct bitmap_index *bitmap_git;
1682 struct ewah_bitmap *bm;
1684 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1685 die("failed to load bitmap indexes");
1687 if (revs->pending.nr != 1)
1688 die("you must specify exactly one commit to test");
1690 fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
1691 bitmap_git->version, bitmap_git->entry_count);
1693 root = revs->pending.objects[0].item;
1694 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
1696 if (bm) {
1697 fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
1698 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
1700 result = ewah_to_bitmap(bm);
1703 if (result == NULL)
1704 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
1706 revs->tag_objects = 1;
1707 revs->tree_objects = 1;
1708 revs->blob_objects = 1;
1710 result_popcnt = bitmap_popcount(result);
1712 if (prepare_revision_walk(revs))
1713 die("revision walk setup failed");
1715 tdata.bitmap_git = bitmap_git;
1716 tdata.base = bitmap_new();
1717 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
1718 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
1719 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
1720 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
1721 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1722 tdata.seen = 0;
1724 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1726 stop_progress(&tdata.prg);
1728 if (bitmap_equals(result, tdata.base))
1729 fprintf(stderr, "OK!\n");
1730 else
1731 die("mismatch in bitmap results");
1733 bitmap_free(result);
1734 bitmap_free(tdata.base);
1735 bitmap_free(tdata.commits);
1736 bitmap_free(tdata.trees);
1737 bitmap_free(tdata.blobs);
1738 bitmap_free(tdata.tags);
1739 free_bitmap_index(bitmap_git);
1742 int test_bitmap_commits(struct repository *r)
1744 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1745 struct object_id oid;
1746 MAYBE_UNUSED void *value;
1748 if (!bitmap_git)
1749 die("failed to load bitmap indexes");
1751 kh_foreach(bitmap_git->bitmaps, oid, value, {
1752 printf("%s\n", oid_to_hex(&oid));
1755 free_bitmap_index(bitmap_git);
1757 return 0;
1760 int test_bitmap_hashes(struct repository *r)
1762 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1763 struct object_id oid;
1764 uint32_t i, index_pos;
1766 if (!bitmap_git || !bitmap_git->hashes)
1767 goto cleanup;
1769 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
1770 if (bitmap_is_midx(bitmap_git))
1771 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
1772 else
1773 index_pos = pack_pos_to_index(bitmap_git->pack, i);
1775 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1777 printf("%s %"PRIu32"\n",
1778 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
1781 cleanup:
1782 free_bitmap_index(bitmap_git);
1784 return 0;
1787 int rebuild_bitmap(const uint32_t *reposition,
1788 struct ewah_bitmap *source,
1789 struct bitmap *dest)
1791 uint32_t pos = 0;
1792 struct ewah_iterator it;
1793 eword_t word;
1795 ewah_iterator_init(&it, source);
1797 while (ewah_iterator_next(&word, &it)) {
1798 uint32_t offset, bit_pos;
1800 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1801 if ((word >> offset) == 0)
1802 break;
1804 offset += ewah_bit_ctz64(word >> offset);
1806 bit_pos = reposition[pos + offset];
1807 if (bit_pos > 0)
1808 bitmap_set(dest, bit_pos - 1);
1809 else /* can't reuse, we don't have the object */
1810 return -1;
1813 pos += BITS_IN_EWORD;
1815 return 0;
1818 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1819 struct packing_data *mapping)
1821 uint32_t i, num_objects;
1822 uint32_t *reposition;
1824 if (!bitmap_is_midx(bitmap_git))
1825 load_reverse_index(bitmap_git);
1826 else if (load_midx_revindex(bitmap_git->midx) < 0)
1827 BUG("rebuild_existing_bitmaps: missing required rev-cache "
1828 "extension");
1830 num_objects = bitmap_num_objects(bitmap_git);
1831 CALLOC_ARRAY(reposition, num_objects);
1833 for (i = 0; i < num_objects; ++i) {
1834 struct object_id oid;
1835 struct object_entry *oe;
1836 uint32_t index_pos;
1838 if (bitmap_is_midx(bitmap_git))
1839 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
1840 else
1841 index_pos = pack_pos_to_index(bitmap_git->pack, i);
1842 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1843 oe = packlist_find(mapping, &oid);
1845 if (oe) {
1846 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
1847 if (bitmap_git->hashes && !oe->hash)
1848 oe->hash = get_be32(bitmap_git->hashes + index_pos);
1852 return reposition;
1855 void free_bitmap_index(struct bitmap_index *b)
1857 if (!b)
1858 return;
1860 if (b->map)
1861 munmap(b->map, b->map_size);
1862 ewah_pool_free(b->commits);
1863 ewah_pool_free(b->trees);
1864 ewah_pool_free(b->blobs);
1865 ewah_pool_free(b->tags);
1866 if (b->bitmaps) {
1867 struct stored_bitmap *sb;
1868 kh_foreach_value(b->bitmaps, sb, {
1869 ewah_pool_free(sb->root);
1870 free(sb);
1873 kh_destroy_oid_map(b->bitmaps);
1874 free(b->ext_index.objects);
1875 free(b->ext_index.hashes);
1876 kh_destroy_oid_pos(b->ext_index.positions);
1877 bitmap_free(b->result);
1878 bitmap_free(b->haves);
1879 if (bitmap_is_midx(b)) {
1881 * Multi-pack bitmaps need to have resources associated with
1882 * their on-disk reverse indexes unmapped so that stale .rev and
1883 * .bitmap files can be removed.
1885 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
1886 * written in the same 'git multi-pack-index write --bitmap'
1887 * process. Close resources so they can be removed safely on
1888 * platforms like Windows.
1890 close_midx_revindex(b->midx);
1892 free(b);
1895 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1896 const struct object_id *oid)
1898 return bitmap_git &&
1899 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
1902 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1903 enum object_type object_type)
1905 struct bitmap *result = bitmap_git->result;
1906 off_t total = 0;
1907 struct ewah_iterator it;
1908 eword_t filter;
1909 size_t i;
1911 init_type_iterator(&it, bitmap_git, object_type);
1912 for (i = 0; i < result->word_alloc &&
1913 ewah_iterator_next(&filter, &it); i++) {
1914 eword_t word = result->words[i] & filter;
1915 size_t base = (i * BITS_IN_EWORD);
1916 unsigned offset;
1918 if (!word)
1919 continue;
1921 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1922 if ((word >> offset) == 0)
1923 break;
1925 offset += ewah_bit_ctz64(word >> offset);
1927 if (bitmap_is_midx(bitmap_git)) {
1928 uint32_t pack_pos;
1929 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
1930 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
1932 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1933 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
1935 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
1936 struct object_id oid;
1937 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
1939 die(_("could not find %s in pack %s at offset %"PRIuMAX),
1940 oid_to_hex(&oid),
1941 pack->pack_name,
1942 (uintmax_t)offset);
1945 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
1946 } else {
1947 size_t pos = base + offset;
1948 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
1949 pack_pos_to_offset(bitmap_git->pack, pos);
1954 return total;
1957 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1959 struct bitmap *result = bitmap_git->result;
1960 struct eindex *eindex = &bitmap_git->ext_index;
1961 off_t total = 0;
1962 struct object_info oi = OBJECT_INFO_INIT;
1963 off_t object_size;
1964 size_t i;
1966 oi.disk_sizep = &object_size;
1968 for (i = 0; i < eindex->count; i++) {
1969 struct object *obj = eindex->objects[i];
1971 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
1972 continue;
1974 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1975 die(_("unable to get disk usage of %s"),
1976 oid_to_hex(&obj->oid));
1978 total += object_size;
1980 return total;
1983 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
1984 struct rev_info *revs)
1986 off_t total = 0;
1988 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
1989 if (revs->tree_objects)
1990 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
1991 if (revs->blob_objects)
1992 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
1993 if (revs->tag_objects)
1994 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
1996 total += get_disk_usage_for_extended(bitmap_git);
1998 return total;
2001 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2003 return !!bitmap_git->midx;
2006 const struct string_list *bitmap_preferred_tips(struct repository *r)
2008 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
2011 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2013 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2014 struct string_list_item *item;
2016 if (!preferred_tips)
2017 return 0;
2019 for_each_string_list_item(item, preferred_tips) {
2020 if (starts_with(refname, item->string))
2021 return 1;
2024 return 0;