p2000: add test for 'git sparse-checkout [add|set]'
[git.git] / pack-bitmap.c
blob97909d48da381f81622d36fb5889fd5d7013b04a
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)
744 struct bitmap *base = NULL;
745 int needs_walk = 0;
747 struct object_list *not_mapped = NULL;
750 * Go through all the roots for the walk. The ones that have bitmaps
751 * on the bitmap index will be `or`ed together to form an initial
752 * global reachability analysis.
754 * The ones without bitmaps in the index will be stored in the
755 * `not_mapped_list` for further processing.
757 while (roots) {
758 struct object *object = roots->item;
759 roots = roots->next;
761 if (object->type == OBJ_COMMIT &&
762 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
763 object->flags |= SEEN;
764 continue;
767 object_list_insert(object, &not_mapped);
771 * Best case scenario: We found bitmaps for all the roots,
772 * so the resulting `or` bitmap has the full reachability analysis
774 if (not_mapped == NULL)
775 return base;
777 roots = not_mapped;
780 * Let's iterate through all the roots that don't have bitmaps to
781 * check if we can determine them to be reachable from the existing
782 * global bitmap.
784 * If we cannot find them in the existing global bitmap, we'll need
785 * to push them to an actual walk and run it until we can confirm
786 * they are reachable
788 while (roots) {
789 struct object *object = roots->item;
790 int pos;
792 roots = roots->next;
793 pos = bitmap_position(bitmap_git, &object->oid);
795 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
796 object->flags &= ~UNINTERESTING;
797 add_pending_object(revs, object, "");
798 needs_walk = 1;
799 } else {
800 object->flags |= SEEN;
804 if (needs_walk) {
805 struct include_data incdata;
806 struct bitmap_show_data show_data;
808 if (base == NULL)
809 base = bitmap_new();
811 incdata.bitmap_git = bitmap_git;
812 incdata.base = base;
813 incdata.seen = seen;
815 revs->include_check = should_include;
816 revs->include_check_obj = should_include_obj;
817 revs->include_check_data = &incdata;
819 if (prepare_revision_walk(revs))
820 die("revision walk setup failed");
822 show_data.bitmap_git = bitmap_git;
823 show_data.base = base;
825 traverse_commit_list(revs,
826 show_commit, show_object,
827 &show_data);
829 revs->include_check = NULL;
830 revs->include_check_obj = NULL;
831 revs->include_check_data = NULL;
834 return base;
837 static void show_extended_objects(struct bitmap_index *bitmap_git,
838 struct rev_info *revs,
839 show_reachable_fn show_reach)
841 struct bitmap *objects = bitmap_git->result;
842 struct eindex *eindex = &bitmap_git->ext_index;
843 uint32_t i;
845 for (i = 0; i < eindex->count; ++i) {
846 struct object *obj;
848 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
849 continue;
851 obj = eindex->objects[i];
852 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
853 (obj->type == OBJ_TREE && !revs->tree_objects) ||
854 (obj->type == OBJ_TAG && !revs->tag_objects))
855 continue;
857 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
861 static void init_type_iterator(struct ewah_iterator *it,
862 struct bitmap_index *bitmap_git,
863 enum object_type type)
865 switch (type) {
866 case OBJ_COMMIT:
867 ewah_iterator_init(it, bitmap_git->commits);
868 break;
870 case OBJ_TREE:
871 ewah_iterator_init(it, bitmap_git->trees);
872 break;
874 case OBJ_BLOB:
875 ewah_iterator_init(it, bitmap_git->blobs);
876 break;
878 case OBJ_TAG:
879 ewah_iterator_init(it, bitmap_git->tags);
880 break;
882 default:
883 BUG("object type %d not stored by bitmap type index", type);
884 break;
888 static void show_objects_for_type(
889 struct bitmap_index *bitmap_git,
890 enum object_type object_type,
891 show_reachable_fn show_reach)
893 size_t i = 0;
894 uint32_t offset;
896 struct ewah_iterator it;
897 eword_t filter;
899 struct bitmap *objects = bitmap_git->result;
901 init_type_iterator(&it, bitmap_git, object_type);
903 for (i = 0; i < objects->word_alloc &&
904 ewah_iterator_next(&filter, &it); i++) {
905 eword_t word = objects->words[i] & filter;
906 size_t pos = (i * BITS_IN_EWORD);
908 if (!word)
909 continue;
911 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
912 struct packed_git *pack;
913 struct object_id oid;
914 uint32_t hash = 0, index_pos;
915 off_t ofs;
917 if ((word >> offset) == 0)
918 break;
920 offset += ewah_bit_ctz64(word >> offset);
922 if (bitmap_is_midx(bitmap_git)) {
923 struct multi_pack_index *m = bitmap_git->midx;
924 uint32_t pack_id;
926 index_pos = pack_pos_to_midx(m, pos + offset);
927 ofs = nth_midxed_offset(m, index_pos);
928 nth_midxed_object_oid(&oid, m, index_pos);
930 pack_id = nth_midxed_pack_int_id(m, index_pos);
931 pack = bitmap_git->midx->packs[pack_id];
932 } else {
933 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
934 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
935 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
937 pack = bitmap_git->pack;
940 if (bitmap_git->hashes)
941 hash = get_be32(bitmap_git->hashes + index_pos);
943 show_reach(&oid, object_type, 0, hash, pack, ofs);
948 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
949 struct object_list *roots)
951 while (roots) {
952 struct object *object = roots->item;
953 roots = roots->next;
955 if (bitmap_is_midx(bitmap_git)) {
956 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
957 return 1;
958 } else {
959 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
960 return 1;
964 return 0;
967 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
968 struct object_list *tip_objects,
969 enum object_type type)
971 struct bitmap *result = bitmap_new();
972 struct object_list *p;
974 for (p = tip_objects; p; p = p->next) {
975 int pos;
977 if (p->item->type != type)
978 continue;
980 pos = bitmap_position(bitmap_git, &p->item->oid);
981 if (pos < 0)
982 continue;
984 bitmap_set(result, pos);
987 return result;
990 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
991 struct object_list *tip_objects,
992 struct bitmap *to_filter,
993 enum object_type type)
995 struct eindex *eindex = &bitmap_git->ext_index;
996 struct bitmap *tips;
997 struct ewah_iterator it;
998 eword_t mask;
999 uint32_t i;
1002 * The non-bitmap version of this filter never removes
1003 * objects which the other side specifically asked for,
1004 * so we must match that behavior.
1006 tips = find_tip_objects(bitmap_git, tip_objects, type);
1009 * We can use the type-level bitmap for 'type' to work in whole
1010 * words for the objects that are actually in the bitmapped
1011 * packfile.
1013 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1014 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1015 i++) {
1016 if (i < tips->word_alloc)
1017 mask &= ~tips->words[i];
1018 to_filter->words[i] &= ~mask;
1022 * Clear any objects that weren't in the packfile (and so would
1023 * not have been caught by the loop above. We'll have to check
1024 * them individually.
1026 for (i = 0; i < eindex->count; i++) {
1027 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1028 if (eindex->objects[i]->type == type &&
1029 bitmap_get(to_filter, pos) &&
1030 !bitmap_get(tips, pos))
1031 bitmap_unset(to_filter, pos);
1034 bitmap_free(tips);
1037 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1038 struct object_list *tip_objects,
1039 struct bitmap *to_filter)
1041 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1042 OBJ_BLOB);
1045 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1046 uint32_t pos)
1048 unsigned long size;
1049 struct object_info oi = OBJECT_INFO_INIT;
1051 oi.sizep = &size;
1053 if (pos < bitmap_num_objects(bitmap_git)) {
1054 struct packed_git *pack;
1055 off_t ofs;
1057 if (bitmap_is_midx(bitmap_git)) {
1058 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1059 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1061 pack = bitmap_git->midx->packs[pack_id];
1062 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1063 } else {
1064 pack = bitmap_git->pack;
1065 ofs = pack_pos_to_offset(pack, pos);
1068 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1069 struct object_id oid;
1070 nth_bitmap_object_oid(bitmap_git, &oid,
1071 pack_pos_to_index(pack, pos));
1072 die(_("unable to get size of %s"), oid_to_hex(&oid));
1074 } else {
1075 struct eindex *eindex = &bitmap_git->ext_index;
1076 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1077 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1078 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1081 return size;
1084 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1085 struct object_list *tip_objects,
1086 struct bitmap *to_filter,
1087 unsigned long limit)
1089 struct eindex *eindex = &bitmap_git->ext_index;
1090 struct bitmap *tips;
1091 struct ewah_iterator it;
1092 eword_t mask;
1093 uint32_t i;
1095 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1097 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1098 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1099 i++) {
1100 eword_t word = to_filter->words[i] & mask;
1101 unsigned offset;
1103 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1104 uint32_t pos;
1106 if ((word >> offset) == 0)
1107 break;
1108 offset += ewah_bit_ctz64(word >> offset);
1109 pos = i * BITS_IN_EWORD + offset;
1111 if (!bitmap_get(tips, pos) &&
1112 get_size_by_pos(bitmap_git, pos) >= limit)
1113 bitmap_unset(to_filter, pos);
1117 for (i = 0; i < eindex->count; i++) {
1118 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1119 if (eindex->objects[i]->type == OBJ_BLOB &&
1120 bitmap_get(to_filter, pos) &&
1121 !bitmap_get(tips, pos) &&
1122 get_size_by_pos(bitmap_git, pos) >= limit)
1123 bitmap_unset(to_filter, pos);
1126 bitmap_free(tips);
1129 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1130 struct object_list *tip_objects,
1131 struct bitmap *to_filter,
1132 unsigned long limit)
1134 if (limit)
1135 BUG("filter_bitmap_tree_depth given non-zero limit");
1137 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1138 OBJ_TREE);
1139 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1140 OBJ_BLOB);
1143 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1144 struct object_list *tip_objects,
1145 struct bitmap *to_filter,
1146 enum object_type object_type)
1148 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1149 BUG("filter_bitmap_object_type given invalid object");
1151 if (object_type != OBJ_TAG)
1152 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1153 if (object_type != OBJ_COMMIT)
1154 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1155 if (object_type != OBJ_TREE)
1156 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1157 if (object_type != OBJ_BLOB)
1158 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1161 static int filter_bitmap(struct bitmap_index *bitmap_git,
1162 struct object_list *tip_objects,
1163 struct bitmap *to_filter,
1164 struct list_objects_filter_options *filter)
1166 if (!filter || filter->choice == LOFC_DISABLED)
1167 return 0;
1169 if (filter->choice == LOFC_BLOB_NONE) {
1170 if (bitmap_git)
1171 filter_bitmap_blob_none(bitmap_git, tip_objects,
1172 to_filter);
1173 return 0;
1176 if (filter->choice == LOFC_BLOB_LIMIT) {
1177 if (bitmap_git)
1178 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1179 to_filter,
1180 filter->blob_limit_value);
1181 return 0;
1184 if (filter->choice == LOFC_TREE_DEPTH &&
1185 filter->tree_exclude_depth == 0) {
1186 if (bitmap_git)
1187 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1188 to_filter,
1189 filter->tree_exclude_depth);
1190 return 0;
1193 if (filter->choice == LOFC_OBJECT_TYPE) {
1194 if (bitmap_git)
1195 filter_bitmap_object_type(bitmap_git, tip_objects,
1196 to_filter,
1197 filter->object_type);
1198 return 0;
1201 if (filter->choice == LOFC_COMBINE) {
1202 int i;
1203 for (i = 0; i < filter->sub_nr; i++) {
1204 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1205 &filter->sub[i]) < 0)
1206 return -1;
1208 return 0;
1211 /* filter choice not handled */
1212 return -1;
1215 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1217 return !filter_bitmap(NULL, NULL, NULL, filter);
1220 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1221 int filter_provided_objects)
1223 unsigned int i;
1225 struct object_list *wants = NULL;
1226 struct object_list *haves = NULL;
1228 struct bitmap *wants_bitmap = NULL;
1229 struct bitmap *haves_bitmap = NULL;
1231 struct bitmap_index *bitmap_git;
1234 * We can't do pathspec limiting with bitmaps, because we don't know
1235 * which commits are associated with which object changes (let alone
1236 * even which objects are associated with which paths).
1238 if (revs->prune)
1239 return NULL;
1241 if (!can_filter_bitmap(&revs->filter))
1242 return NULL;
1244 /* try to open a bitmapped pack, but don't parse it yet
1245 * because we may not need to use it */
1246 CALLOC_ARRAY(bitmap_git, 1);
1247 if (open_bitmap(revs->repo, bitmap_git) < 0)
1248 goto cleanup;
1250 for (i = 0; i < revs->pending.nr; ++i) {
1251 struct object *object = revs->pending.objects[i].item;
1253 if (object->type == OBJ_NONE)
1254 parse_object_or_die(&object->oid, NULL);
1256 while (object->type == OBJ_TAG) {
1257 struct tag *tag = (struct tag *) object;
1259 if (object->flags & UNINTERESTING)
1260 object_list_insert(object, &haves);
1261 else
1262 object_list_insert(object, &wants);
1264 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1265 object->flags |= (tag->object.flags & UNINTERESTING);
1268 if (object->flags & UNINTERESTING)
1269 object_list_insert(object, &haves);
1270 else
1271 object_list_insert(object, &wants);
1275 * if we have a HAVES list, but none of those haves is contained
1276 * in the packfile that has a bitmap, we don't have anything to
1277 * optimize here
1279 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1280 goto cleanup;
1282 /* if we don't want anything, we're done here */
1283 if (!wants)
1284 goto cleanup;
1287 * now we're going to use bitmaps, so load the actual bitmap entries
1288 * from disk. this is the point of no return; after this the rev_list
1289 * becomes invalidated and we must perform the revwalk through bitmaps
1291 if (load_bitmap(bitmap_git) < 0)
1292 goto cleanup;
1294 object_array_clear(&revs->pending);
1296 if (haves) {
1297 revs->ignore_missing_links = 1;
1298 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1299 reset_revision_walk();
1300 revs->ignore_missing_links = 0;
1302 if (haves_bitmap == NULL)
1303 BUG("failed to perform bitmap walk");
1306 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1308 if (!wants_bitmap)
1309 BUG("failed to perform bitmap walk");
1311 if (haves_bitmap)
1312 bitmap_and_not(wants_bitmap, haves_bitmap);
1314 filter_bitmap(bitmap_git,
1315 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1316 wants_bitmap,
1317 &revs->filter);
1319 bitmap_git->result = wants_bitmap;
1320 bitmap_git->haves = haves_bitmap;
1322 object_list_free(&wants);
1323 object_list_free(&haves);
1325 return bitmap_git;
1327 cleanup:
1328 free_bitmap_index(bitmap_git);
1329 object_list_free(&wants);
1330 object_list_free(&haves);
1331 return NULL;
1335 * -1 means "stop trying further objects"; 0 means we may or may not have
1336 * reused, but you can keep feeding bits.
1338 static int try_partial_reuse(struct packed_git *pack,
1339 size_t pos,
1340 struct bitmap *reuse,
1341 struct pack_window **w_curs)
1343 off_t offset, delta_obj_offset;
1344 enum object_type type;
1345 unsigned long size;
1348 * try_partial_reuse() is called either on (a) objects in the
1349 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1350 * objects in the preferred pack of a multi-pack bitmap.
1351 * Importantly, the latter can pretend as if only a single pack
1352 * exists because:
1354 * - The first pack->num_objects bits of a MIDX bitmap are
1355 * reserved for the preferred pack, and
1357 * - Ties due to duplicate objects are always resolved in
1358 * favor of the preferred pack.
1360 * Therefore we do not need to ever ask the MIDX for its copy of
1361 * an object by OID, since it will always select it from the
1362 * preferred pack. Likewise, the selected copy of the base
1363 * object for any deltas will reside in the same pack.
1365 * This means that we can reuse pos when looking up the bit in
1366 * the reuse bitmap, too, since bits corresponding to the
1367 * preferred pack precede all bits from other packs.
1370 if (pos >= pack->num_objects)
1371 return -1; /* not actually in the pack or MIDX preferred pack */
1373 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1374 type = unpack_object_header(pack, w_curs, &offset, &size);
1375 if (type < 0)
1376 return -1; /* broken packfile, punt */
1378 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1379 off_t base_offset;
1380 uint32_t base_pos;
1383 * Find the position of the base object so we can look it up
1384 * in our bitmaps. If we can't come up with an offset, or if
1385 * that offset is not in the revidx, the pack is corrupt.
1386 * There's nothing we can do, so just punt on this object,
1387 * and the normal slow path will complain about it in
1388 * more detail.
1390 base_offset = get_delta_base(pack, w_curs, &offset, type,
1391 delta_obj_offset);
1392 if (!base_offset)
1393 return 0;
1394 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1395 return 0;
1398 * We assume delta dependencies always point backwards. This
1399 * lets us do a single pass, and is basically always true
1400 * due to the way OFS_DELTAs work. You would not typically
1401 * find REF_DELTA in a bitmapped pack, since we only bitmap
1402 * packs we write fresh, and OFS_DELTA is the default). But
1403 * let's double check to make sure the pack wasn't written with
1404 * odd parameters.
1406 if (base_pos >= pos)
1407 return 0;
1410 * And finally, if we're not sending the base as part of our
1411 * reuse chunk, then don't send this object either. The base
1412 * would come after us, along with other objects not
1413 * necessarily in the pack, which means we'd need to convert
1414 * to REF_DELTA on the fly. Better to just let the normal
1415 * object_entry code path handle it.
1417 if (!bitmap_get(reuse, base_pos))
1418 return 0;
1422 * If we got here, then the object is OK to reuse. Mark it.
1424 bitmap_set(reuse, pos);
1425 return 0;
1428 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1430 struct multi_pack_index *m = bitmap_git->midx;
1431 if (!m)
1432 BUG("midx_preferred_pack: requires non-empty MIDX");
1433 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1436 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1437 struct packed_git **packfile_out,
1438 uint32_t *entries,
1439 struct bitmap **reuse_out)
1441 struct packed_git *pack;
1442 struct bitmap *result = bitmap_git->result;
1443 struct bitmap *reuse;
1444 struct pack_window *w_curs = NULL;
1445 size_t i = 0;
1446 uint32_t offset;
1447 uint32_t objects_nr;
1449 assert(result);
1451 load_reverse_index(bitmap_git);
1453 if (bitmap_is_midx(bitmap_git))
1454 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1455 else
1456 pack = bitmap_git->pack;
1457 objects_nr = pack->num_objects;
1459 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1460 i++;
1463 * Don't mark objects not in the packfile or preferred pack. This bitmap
1464 * marks objects eligible for reuse, but the pack-reuse code only
1465 * understands how to reuse a single pack. Since the preferred pack is
1466 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1467 * we use it instead of another pack. In single-pack bitmaps, the choice
1468 * is made for us.
1470 if (i > objects_nr / BITS_IN_EWORD)
1471 i = objects_nr / BITS_IN_EWORD;
1473 reuse = bitmap_word_alloc(i);
1474 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1476 for (; i < result->word_alloc; ++i) {
1477 eword_t word = result->words[i];
1478 size_t pos = (i * BITS_IN_EWORD);
1480 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1481 if ((word >> offset) == 0)
1482 break;
1484 offset += ewah_bit_ctz64(word >> offset);
1485 if (try_partial_reuse(pack, pos + offset,
1486 reuse, &w_curs) < 0) {
1488 * try_partial_reuse indicated we couldn't reuse
1489 * any bits, so there is no point in trying more
1490 * bits in the current word, or any other words
1491 * in result.
1493 * Jump out of both loops to avoid future
1494 * unnecessary calls to try_partial_reuse.
1496 goto done;
1501 done:
1502 unuse_pack(&w_curs);
1504 *entries = bitmap_popcount(reuse);
1505 if (!*entries) {
1506 bitmap_free(reuse);
1507 return -1;
1511 * Drop any reused objects from the result, since they will not
1512 * need to be handled separately.
1514 bitmap_and_not(result, reuse);
1515 *packfile_out = pack;
1516 *reuse_out = reuse;
1517 return 0;
1520 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1521 struct bitmap *bitmap, const struct object_id *oid)
1523 int idx;
1525 if (!bitmap)
1526 return 0;
1528 idx = bitmap_position(bitmap_git, oid);
1529 return idx >= 0 && bitmap_get(bitmap, idx);
1532 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1533 struct rev_info *revs,
1534 show_reachable_fn show_reachable)
1536 assert(bitmap_git->result);
1538 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1539 if (revs->tree_objects)
1540 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1541 if (revs->blob_objects)
1542 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1543 if (revs->tag_objects)
1544 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1546 show_extended_objects(bitmap_git, revs, show_reachable);
1549 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1550 enum object_type type)
1552 struct bitmap *objects = bitmap_git->result;
1553 struct eindex *eindex = &bitmap_git->ext_index;
1555 uint32_t i = 0, count = 0;
1556 struct ewah_iterator it;
1557 eword_t filter;
1559 init_type_iterator(&it, bitmap_git, type);
1561 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1562 eword_t word = objects->words[i++] & filter;
1563 count += ewah_bit_popcount64(word);
1566 for (i = 0; i < eindex->count; ++i) {
1567 if (eindex->objects[i]->type == type &&
1568 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1569 count++;
1572 return count;
1575 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1576 uint32_t *commits, uint32_t *trees,
1577 uint32_t *blobs, uint32_t *tags)
1579 assert(bitmap_git->result);
1581 if (commits)
1582 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1584 if (trees)
1585 *trees = count_object_type(bitmap_git, OBJ_TREE);
1587 if (blobs)
1588 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1590 if (tags)
1591 *tags = count_object_type(bitmap_git, OBJ_TAG);
1594 struct bitmap_test_data {
1595 struct bitmap_index *bitmap_git;
1596 struct bitmap *base;
1597 struct bitmap *commits;
1598 struct bitmap *trees;
1599 struct bitmap *blobs;
1600 struct bitmap *tags;
1601 struct progress *prg;
1602 size_t seen;
1605 static void test_bitmap_type(struct bitmap_test_data *tdata,
1606 struct object *obj, int pos)
1608 enum object_type bitmap_type = OBJ_NONE;
1609 int bitmaps_nr = 0;
1611 if (bitmap_get(tdata->commits, pos)) {
1612 bitmap_type = OBJ_COMMIT;
1613 bitmaps_nr++;
1615 if (bitmap_get(tdata->trees, pos)) {
1616 bitmap_type = OBJ_TREE;
1617 bitmaps_nr++;
1619 if (bitmap_get(tdata->blobs, pos)) {
1620 bitmap_type = OBJ_BLOB;
1621 bitmaps_nr++;
1623 if (bitmap_get(tdata->tags, pos)) {
1624 bitmap_type = OBJ_TAG;
1625 bitmaps_nr++;
1628 if (bitmap_type == OBJ_NONE)
1629 die("object %s not found in type bitmaps",
1630 oid_to_hex(&obj->oid));
1632 if (bitmaps_nr > 1)
1633 die("object %s does not have a unique type",
1634 oid_to_hex(&obj->oid));
1636 if (bitmap_type != obj->type)
1637 die("object %s: real type %s, expected: %s",
1638 oid_to_hex(&obj->oid),
1639 type_name(obj->type),
1640 type_name(bitmap_type));
1643 static void test_show_object(struct object *object, const char *name,
1644 void *data)
1646 struct bitmap_test_data *tdata = data;
1647 int bitmap_pos;
1649 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1650 if (bitmap_pos < 0)
1651 die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
1652 test_bitmap_type(tdata, object, bitmap_pos);
1654 bitmap_set(tdata->base, bitmap_pos);
1655 display_progress(tdata->prg, ++tdata->seen);
1658 static void test_show_commit(struct commit *commit, void *data)
1660 struct bitmap_test_data *tdata = data;
1661 int bitmap_pos;
1663 bitmap_pos = bitmap_position(tdata->bitmap_git,
1664 &commit->object.oid);
1665 if (bitmap_pos < 0)
1666 die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
1667 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1669 bitmap_set(tdata->base, bitmap_pos);
1670 display_progress(tdata->prg, ++tdata->seen);
1673 void test_bitmap_walk(struct rev_info *revs)
1675 struct object *root;
1676 struct bitmap *result = NULL;
1677 size_t result_popcnt;
1678 struct bitmap_test_data tdata;
1679 struct bitmap_index *bitmap_git;
1680 struct ewah_bitmap *bm;
1682 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1683 die("failed to load bitmap indexes");
1685 if (revs->pending.nr != 1)
1686 die("you must specify exactly one commit to test");
1688 fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
1689 bitmap_git->version, bitmap_git->entry_count);
1691 root = revs->pending.objects[0].item;
1692 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
1694 if (bm) {
1695 fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
1696 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
1698 result = ewah_to_bitmap(bm);
1701 if (result == NULL)
1702 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
1704 revs->tag_objects = 1;
1705 revs->tree_objects = 1;
1706 revs->blob_objects = 1;
1708 result_popcnt = bitmap_popcount(result);
1710 if (prepare_revision_walk(revs))
1711 die("revision walk setup failed");
1713 tdata.bitmap_git = bitmap_git;
1714 tdata.base = bitmap_new();
1715 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
1716 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
1717 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
1718 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
1719 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1720 tdata.seen = 0;
1722 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1724 stop_progress(&tdata.prg);
1726 if (bitmap_equals(result, tdata.base))
1727 fprintf(stderr, "OK!\n");
1728 else
1729 die("mismatch in bitmap results");
1731 bitmap_free(result);
1732 bitmap_free(tdata.base);
1733 bitmap_free(tdata.commits);
1734 bitmap_free(tdata.trees);
1735 bitmap_free(tdata.blobs);
1736 bitmap_free(tdata.tags);
1737 free_bitmap_index(bitmap_git);
1740 int test_bitmap_commits(struct repository *r)
1742 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1743 struct object_id oid;
1744 MAYBE_UNUSED void *value;
1746 if (!bitmap_git)
1747 die("failed to load bitmap indexes");
1749 kh_foreach(bitmap_git->bitmaps, oid, value, {
1750 printf("%s\n", oid_to_hex(&oid));
1753 free_bitmap_index(bitmap_git);
1755 return 0;
1758 int test_bitmap_hashes(struct repository *r)
1760 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1761 struct object_id oid;
1762 uint32_t i, index_pos;
1764 if (!bitmap_git || !bitmap_git->hashes)
1765 goto cleanup;
1767 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
1768 if (bitmap_is_midx(bitmap_git))
1769 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
1770 else
1771 index_pos = pack_pos_to_index(bitmap_git->pack, i);
1773 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1775 printf("%s %"PRIu32"\n",
1776 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
1779 cleanup:
1780 free_bitmap_index(bitmap_git);
1782 return 0;
1785 int rebuild_bitmap(const uint32_t *reposition,
1786 struct ewah_bitmap *source,
1787 struct bitmap *dest)
1789 uint32_t pos = 0;
1790 struct ewah_iterator it;
1791 eword_t word;
1793 ewah_iterator_init(&it, source);
1795 while (ewah_iterator_next(&word, &it)) {
1796 uint32_t offset, bit_pos;
1798 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1799 if ((word >> offset) == 0)
1800 break;
1802 offset += ewah_bit_ctz64(word >> offset);
1804 bit_pos = reposition[pos + offset];
1805 if (bit_pos > 0)
1806 bitmap_set(dest, bit_pos - 1);
1807 else /* can't reuse, we don't have the object */
1808 return -1;
1811 pos += BITS_IN_EWORD;
1813 return 0;
1816 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1817 struct packing_data *mapping)
1819 uint32_t i, num_objects;
1820 uint32_t *reposition;
1822 if (!bitmap_is_midx(bitmap_git))
1823 load_reverse_index(bitmap_git);
1824 else if (load_midx_revindex(bitmap_git->midx) < 0)
1825 BUG("rebuild_existing_bitmaps: missing required rev-cache "
1826 "extension");
1828 num_objects = bitmap_num_objects(bitmap_git);
1829 CALLOC_ARRAY(reposition, num_objects);
1831 for (i = 0; i < num_objects; ++i) {
1832 struct object_id oid;
1833 struct object_entry *oe;
1834 uint32_t index_pos;
1836 if (bitmap_is_midx(bitmap_git))
1837 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
1838 else
1839 index_pos = pack_pos_to_index(bitmap_git->pack, i);
1840 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1841 oe = packlist_find(mapping, &oid);
1843 if (oe) {
1844 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
1845 if (bitmap_git->hashes && !oe->hash)
1846 oe->hash = get_be32(bitmap_git->hashes + index_pos);
1850 return reposition;
1853 void free_bitmap_index(struct bitmap_index *b)
1855 if (!b)
1856 return;
1858 if (b->map)
1859 munmap(b->map, b->map_size);
1860 ewah_pool_free(b->commits);
1861 ewah_pool_free(b->trees);
1862 ewah_pool_free(b->blobs);
1863 ewah_pool_free(b->tags);
1864 if (b->bitmaps) {
1865 struct stored_bitmap *sb;
1866 kh_foreach_value(b->bitmaps, sb, {
1867 ewah_pool_free(sb->root);
1868 free(sb);
1871 kh_destroy_oid_map(b->bitmaps);
1872 free(b->ext_index.objects);
1873 free(b->ext_index.hashes);
1874 kh_destroy_oid_pos(b->ext_index.positions);
1875 bitmap_free(b->result);
1876 bitmap_free(b->haves);
1877 if (bitmap_is_midx(b)) {
1879 * Multi-pack bitmaps need to have resources associated with
1880 * their on-disk reverse indexes unmapped so that stale .rev and
1881 * .bitmap files can be removed.
1883 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
1884 * written in the same 'git multi-pack-index write --bitmap'
1885 * process. Close resources so they can be removed safely on
1886 * platforms like Windows.
1888 close_midx_revindex(b->midx);
1890 free(b);
1893 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1894 const struct object_id *oid)
1896 return bitmap_git &&
1897 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
1900 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1901 enum object_type object_type)
1903 struct bitmap *result = bitmap_git->result;
1904 off_t total = 0;
1905 struct ewah_iterator it;
1906 eword_t filter;
1907 size_t i;
1909 init_type_iterator(&it, bitmap_git, object_type);
1910 for (i = 0; i < result->word_alloc &&
1911 ewah_iterator_next(&filter, &it); i++) {
1912 eword_t word = result->words[i] & filter;
1913 size_t base = (i * BITS_IN_EWORD);
1914 unsigned offset;
1916 if (!word)
1917 continue;
1919 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1920 if ((word >> offset) == 0)
1921 break;
1923 offset += ewah_bit_ctz64(word >> offset);
1925 if (bitmap_is_midx(bitmap_git)) {
1926 uint32_t pack_pos;
1927 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
1928 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
1930 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1931 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
1933 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
1934 struct object_id oid;
1935 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
1937 die(_("could not find %s in pack %s at offset %"PRIuMAX),
1938 oid_to_hex(&oid),
1939 pack->pack_name,
1940 (uintmax_t)offset);
1943 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
1944 } else {
1945 size_t pos = base + offset;
1946 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
1947 pack_pos_to_offset(bitmap_git->pack, pos);
1952 return total;
1955 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1957 struct bitmap *result = bitmap_git->result;
1958 struct eindex *eindex = &bitmap_git->ext_index;
1959 off_t total = 0;
1960 struct object_info oi = OBJECT_INFO_INIT;
1961 off_t object_size;
1962 size_t i;
1964 oi.disk_sizep = &object_size;
1966 for (i = 0; i < eindex->count; i++) {
1967 struct object *obj = eindex->objects[i];
1969 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
1970 continue;
1972 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1973 die(_("unable to get disk usage of %s"),
1974 oid_to_hex(&obj->oid));
1976 total += object_size;
1978 return total;
1981 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
1982 struct rev_info *revs)
1984 off_t total = 0;
1986 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
1987 if (revs->tree_objects)
1988 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
1989 if (revs->blob_objects)
1990 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
1991 if (revs->tag_objects)
1992 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
1994 total += get_disk_usage_for_extended(bitmap_git);
1996 return total;
1999 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2001 return !!bitmap_git->midx;
2004 const struct string_list *bitmap_preferred_tips(struct repository *r)
2006 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
2009 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2011 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2012 struct string_list_item *item;
2014 if (!preferred_tips)
2015 return 0;
2017 for_each_string_list_item(item, preferred_tips) {
2018 if (starts_with(refname, item->string))
2019 return 1;
2022 return 0;