8 #include "list-objects.h"
10 #include "pack-bitmap.h"
11 #include "pack-revindex.h"
12 #include "pack-objects.h"
14 #include "repository.h"
15 #include "object-store.h"
16 #include "list-objects-filter-options.h"
21 * An entry on the bitmap index, representing the bitmap for a given
24 struct stored_bitmap
{
26 struct ewah_bitmap
*root
;
27 struct stored_bitmap
*xor;
32 * The active bitmap index for a repository. By design, repositories only have
33 * a single bitmap index available (the index for the biggest packfile in
34 * the repository), since bitmap indexes need full closure.
36 * If there is more than one bitmap index available (e.g. because of alternates),
37 * the active bitmap index is the largest one.
41 * The pack or multi-pack index (MIDX) that this bitmap index belongs
44 * Exactly one of these must be non-NULL; this specifies the object
45 * order used to interpret this bitmap.
47 struct packed_git
*pack
;
48 struct multi_pack_index
*midx
;
51 * Mark the first `reuse_objects` in the packfile as reused:
52 * they will be sent as-is without using them for repacking
55 uint32_t reuse_objects
;
57 /* mmapped buffer of the whole bitmap index */
59 size_t map_size
; /* size of the mmaped buffer */
60 size_t map_pos
; /* current position when loading the index */
65 * Each bitmap marks which objects in the packfile are of the given
66 * type. This provides type information when yielding the objects from
67 * the packfile during a walk, which allows for better delta bases.
69 struct ewah_bitmap
*commits
;
70 struct ewah_bitmap
*trees
;
71 struct ewah_bitmap
*blobs
;
72 struct ewah_bitmap
*tags
;
74 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
75 kh_oid_map_t
*bitmaps
;
77 /* Number of bitmapped commits */
80 /* If not NULL, this is a name-hash cache pointing into map. */
83 /* The checksum of the packfile or MIDX; points into map. */
84 const unsigned char *checksum
;
87 * If not NULL, this point into the commit table extension
88 * (within the memory mapped region `map`).
90 unsigned char *table_lookup
;
95 * When trying to perform bitmap operations with objects that are not
96 * packed in `pack`, these objects are added to this "fake index" and
97 * are assumed to appear at the end of the packfile for all operations
100 struct object
**objects
;
102 uint32_t count
, alloc
;
103 kh_oid_pos_t
*positions
;
106 /* Bitmap result of the last performed walk */
107 struct bitmap
*result
;
109 /* "have" bitmap from the last performed walk */
110 struct bitmap
*haves
;
112 /* Version of the bitmap index */
113 unsigned int version
;
116 static struct ewah_bitmap
*lookup_stored_bitmap(struct stored_bitmap
*st
)
118 struct ewah_bitmap
*parent
;
119 struct ewah_bitmap
*composed
;
124 composed
= ewah_pool_new();
125 parent
= lookup_stored_bitmap(st
->xor);
126 ewah_xor(st
->root
, parent
, composed
);
128 ewah_pool_free(st
->root
);
136 * Read a bitmap from the current read position on the mmaped
137 * index, and increase the read position accordingly
139 static struct ewah_bitmap
*read_bitmap_1(struct bitmap_index
*index
)
141 struct ewah_bitmap
*b
= ewah_pool_new();
143 ssize_t bitmap_size
= ewah_read_mmap(b
,
144 index
->map
+ index
->map_pos
,
145 index
->map_size
- index
->map_pos
);
147 if (bitmap_size
< 0) {
148 error(_("failed to load bitmap index (corrupted?)"));
153 index
->map_pos
+= bitmap_size
;
157 static uint32_t bitmap_num_objects(struct bitmap_index
*index
)
160 return index
->midx
->num_objects
;
161 return index
->pack
->num_objects
;
164 static int load_bitmap_header(struct bitmap_index
*index
)
166 struct bitmap_disk_header
*header
= (void *)index
->map
;
167 size_t header_size
= sizeof(*header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
;
169 if (index
->map_size
< header_size
+ the_hash_algo
->rawsz
)
170 return error(_("corrupted bitmap index (too small)"));
172 if (memcmp(header
->magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
)) != 0)
173 return error(_("corrupted bitmap index file (wrong header)"));
175 index
->version
= ntohs(header
->version
);
176 if (index
->version
!= 1)
177 return error(_("unsupported version '%d' for bitmap index file"), index
->version
);
179 /* Parse known bitmap format options */
181 uint32_t flags
= ntohs(header
->options
);
182 size_t cache_size
= st_mult(bitmap_num_objects(index
), sizeof(uint32_t));
183 unsigned char *index_end
= index
->map
+ index
->map_size
- the_hash_algo
->rawsz
;
185 if ((flags
& BITMAP_OPT_FULL_DAG
) == 0)
186 BUG("unsupported options for bitmap index file "
187 "(Git requires BITMAP_OPT_FULL_DAG)");
189 if (flags
& BITMAP_OPT_HASH_CACHE
) {
190 if (cache_size
> index_end
- index
->map
- header_size
)
191 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
192 index
->hashes
= (void *)(index_end
- cache_size
);
193 index_end
-= cache_size
;
196 if (flags
& BITMAP_OPT_LOOKUP_TABLE
) {
197 size_t table_size
= st_mult(ntohl(header
->entry_count
),
198 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
);
199 if (table_size
> index_end
- index
->map
- header_size
)
200 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
201 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
202 index
->table_lookup
= (void *)(index_end
- table_size
);
203 index_end
-= table_size
;
207 index
->entry_count
= ntohl(header
->entry_count
);
208 index
->checksum
= header
->checksum
;
209 index
->map_pos
+= header_size
;
213 static struct stored_bitmap
*store_bitmap(struct bitmap_index
*index
,
214 struct ewah_bitmap
*root
,
215 const struct object_id
*oid
,
216 struct stored_bitmap
*xor_with
,
219 struct stored_bitmap
*stored
;
223 stored
= xmalloc(sizeof(struct stored_bitmap
));
225 stored
->xor = xor_with
;
226 stored
->flags
= flags
;
227 oidcpy(&stored
->oid
, oid
);
229 hash_pos
= kh_put_oid_map(index
->bitmaps
, stored
->oid
, &ret
);
232 * A 0 return code means the insertion succeeded with no changes,
233 * because the SHA1 already existed on the map. This is bad, there
234 * shouldn't be duplicated commits in the index.
237 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid
));
241 kh_value(index
->bitmaps
, hash_pos
) = stored
;
245 static inline uint32_t read_be32(const unsigned char *buffer
, size_t *pos
)
247 uint32_t result
= get_be32(buffer
+ *pos
);
248 (*pos
) += sizeof(result
);
252 static inline uint8_t read_u8(const unsigned char *buffer
, size_t *pos
)
254 return buffer
[(*pos
)++];
257 #define MAX_XOR_OFFSET 160
259 static int nth_bitmap_object_oid(struct bitmap_index
*index
,
260 struct object_id
*oid
,
264 return nth_midxed_object_oid(oid
, index
->midx
, n
) ? 0 : -1;
265 return nth_packed_object_id(oid
, index
->pack
, n
);
268 static int load_bitmap_entries_v1(struct bitmap_index
*index
)
271 struct stored_bitmap
*recent_bitmaps
[MAX_XOR_OFFSET
] = { NULL
};
273 for (i
= 0; i
< index
->entry_count
; ++i
) {
274 int xor_offset
, flags
;
275 struct ewah_bitmap
*bitmap
= NULL
;
276 struct stored_bitmap
*xor_bitmap
= NULL
;
277 uint32_t commit_idx_pos
;
278 struct object_id oid
;
280 if (index
->map_size
- index
->map_pos
< 6)
281 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i
);
283 commit_idx_pos
= read_be32(index
->map
, &index
->map_pos
);
284 xor_offset
= read_u8(index
->map
, &index
->map_pos
);
285 flags
= read_u8(index
->map
, &index
->map_pos
);
287 if (nth_bitmap_object_oid(index
, &oid
, commit_idx_pos
) < 0)
288 return error(_("corrupt ewah bitmap: commit index %u out of range"),
289 (unsigned)commit_idx_pos
);
291 bitmap
= read_bitmap_1(index
);
295 if (xor_offset
> MAX_XOR_OFFSET
|| xor_offset
> i
)
296 return error(_("corrupted bitmap pack index"));
298 if (xor_offset
> 0) {
299 xor_bitmap
= recent_bitmaps
[(i
- xor_offset
) % MAX_XOR_OFFSET
];
302 return error(_("invalid XOR offset in bitmap pack index"));
305 recent_bitmaps
[i
% MAX_XOR_OFFSET
] = store_bitmap(
306 index
, bitmap
, &oid
, xor_bitmap
, flags
);
312 char *midx_bitmap_filename(struct multi_pack_index
*midx
)
314 struct strbuf buf
= STRBUF_INIT
;
316 get_midx_filename(&buf
, midx
->object_dir
);
317 strbuf_addf(&buf
, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx
)));
319 return strbuf_detach(&buf
, NULL
);
322 char *pack_bitmap_filename(struct packed_git
*p
)
326 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
327 BUG("pack_name does not end in .pack");
328 return xstrfmt("%.*s.bitmap", (int)len
, p
->pack_name
);
331 static int open_midx_bitmap_1(struct bitmap_index
*bitmap_git
,
332 struct multi_pack_index
*midx
)
335 char *bitmap_name
= midx_bitmap_filename(midx
);
336 int fd
= git_open(bitmap_name
);
338 struct packed_git
*preferred
;
342 warning_errno("cannot open '%s'", bitmap_name
);
348 if (fstat(fd
, &st
)) {
349 error_errno(_("cannot fstat bitmap file"));
354 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
355 struct strbuf buf
= STRBUF_INIT
;
356 get_midx_filename(&buf
, midx
->object_dir
);
357 /* ignore extra bitmap file; we can only handle one */
358 warning(_("ignoring extra bitmap file: '%s'"), buf
.buf
);
360 strbuf_release(&buf
);
364 bitmap_git
->midx
= midx
;
365 bitmap_git
->map_size
= xsize_t(st
.st_size
);
366 bitmap_git
->map_pos
= 0;
367 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
,
371 if (load_bitmap_header(bitmap_git
) < 0)
374 if (!hasheq(get_midx_checksum(bitmap_git
->midx
), bitmap_git
->checksum
)) {
375 error(_("checksum doesn't match in MIDX and bitmap"));
379 if (load_midx_revindex(bitmap_git
->midx
) < 0) {
380 warning(_("multi-pack bitmap is missing required reverse index"));
384 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
385 if (prepare_midx_pack(the_repository
, bitmap_git
->midx
, i
))
386 die(_("could not open pack %s"),
387 bitmap_git
->midx
->pack_names
[i
]);
390 preferred
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
391 if (!is_pack_valid(preferred
)) {
392 warning(_("preferred pack (%s) is invalid"),
393 preferred
->pack_name
);
400 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
401 bitmap_git
->map_size
= 0;
402 bitmap_git
->map_pos
= 0;
403 bitmap_git
->map
= NULL
;
404 bitmap_git
->midx
= NULL
;
408 static int open_pack_bitmap_1(struct bitmap_index
*bitmap_git
, struct packed_git
*packfile
)
414 if (open_pack_index(packfile
))
417 bitmap_name
= pack_bitmap_filename(packfile
);
418 fd
= git_open(bitmap_name
);
422 warning_errno("cannot open '%s'", bitmap_name
);
428 if (fstat(fd
, &st
)) {
429 error_errno(_("cannot fstat bitmap file"));
434 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
435 /* ignore extra bitmap file; we can only handle one */
436 warning(_("ignoring extra bitmap file: '%s'"), packfile
->pack_name
);
441 if (!is_pack_valid(packfile
)) {
446 bitmap_git
->pack
= packfile
;
447 bitmap_git
->map_size
= xsize_t(st
.st_size
);
448 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
449 bitmap_git
->map_pos
= 0;
452 if (load_bitmap_header(bitmap_git
) < 0) {
453 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
454 bitmap_git
->map
= NULL
;
455 bitmap_git
->map_size
= 0;
456 bitmap_git
->map_pos
= 0;
457 bitmap_git
->pack
= NULL
;
464 static int load_reverse_index(struct bitmap_index
*bitmap_git
)
466 if (bitmap_is_midx(bitmap_git
)) {
471 * The multi-pack-index's .rev file is already loaded via
472 * open_pack_bitmap_1().
474 * But we still need to open the individual pack .rev files,
475 * since we will need to make use of them in pack-objects.
477 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
478 ret
= load_pack_revindex(bitmap_git
->midx
->packs
[i
]);
484 return load_pack_revindex(bitmap_git
->pack
);
487 static int load_bitmap(struct bitmap_index
*bitmap_git
)
489 assert(bitmap_git
->map
);
491 bitmap_git
->bitmaps
= kh_init_oid_map();
492 bitmap_git
->ext_index
.positions
= kh_init_oid_pos();
494 if (load_reverse_index(bitmap_git
))
497 if (!(bitmap_git
->commits
= read_bitmap_1(bitmap_git
)) ||
498 !(bitmap_git
->trees
= read_bitmap_1(bitmap_git
)) ||
499 !(bitmap_git
->blobs
= read_bitmap_1(bitmap_git
)) ||
500 !(bitmap_git
->tags
= read_bitmap_1(bitmap_git
)))
503 if (!bitmap_git
->table_lookup
&& load_bitmap_entries_v1(bitmap_git
) < 0)
509 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
510 bitmap_git
->map
= NULL
;
511 bitmap_git
->map_size
= 0;
513 kh_destroy_oid_map(bitmap_git
->bitmaps
);
514 bitmap_git
->bitmaps
= NULL
;
516 kh_destroy_oid_pos(bitmap_git
->ext_index
.positions
);
517 bitmap_git
->ext_index
.positions
= NULL
;
522 static int open_pack_bitmap(struct repository
*r
,
523 struct bitmap_index
*bitmap_git
)
525 struct packed_git
*p
;
528 assert(!bitmap_git
->map
);
530 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
531 if (open_pack_bitmap_1(bitmap_git
, p
) == 0)
538 static int open_midx_bitmap(struct repository
*r
,
539 struct bitmap_index
*bitmap_git
)
542 struct multi_pack_index
*midx
;
544 assert(!bitmap_git
->map
);
546 for (midx
= get_multi_pack_index(r
); midx
; midx
= midx
->next
) {
547 if (!open_midx_bitmap_1(bitmap_git
, midx
))
553 static int open_bitmap(struct repository
*r
,
554 struct bitmap_index
*bitmap_git
)
556 assert(!bitmap_git
->map
);
558 if (!open_midx_bitmap(r
, bitmap_git
))
560 return open_pack_bitmap(r
, bitmap_git
);
563 struct bitmap_index
*prepare_bitmap_git(struct repository
*r
)
565 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
567 if (!open_bitmap(r
, bitmap_git
) && !load_bitmap(bitmap_git
))
570 free_bitmap_index(bitmap_git
);
574 struct bitmap_index
*prepare_midx_bitmap_git(struct multi_pack_index
*midx
)
576 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
578 if (!open_midx_bitmap_1(bitmap_git
, midx
) && !load_bitmap(bitmap_git
))
581 free_bitmap_index(bitmap_git
);
585 struct include_data
{
586 struct bitmap_index
*bitmap_git
;
591 struct bitmap_lookup_table_triplet
{
597 struct bitmap_lookup_table_xor_item
{
598 struct object_id oid
;
603 * Given a `triplet` struct pointer and pointer `p`, this
604 * function reads the triplet beginning at `p` into the struct.
605 * Note that this function assumes that there is enough memory
606 * left for filling the `triplet` struct from `p`.
608 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet
*triplet
,
609 const unsigned char *p
)
614 triplet
->commit_pos
= get_be32(p
);
615 p
+= sizeof(uint32_t);
616 triplet
->offset
= get_be64(p
);
617 p
+= sizeof(uint64_t);
618 triplet
->xor_row
= get_be32(p
);
623 * This function gets the raw triplet from `row`'th row in the
624 * lookup table and fills that data to the `triplet`.
626 static int bitmap_lookup_table_get_triplet(struct bitmap_index
*bitmap_git
,
628 struct bitmap_lookup_table_triplet
*triplet
)
630 unsigned char *p
= NULL
;
631 if (pos
>= bitmap_git
->entry_count
)
632 return error(_("corrupt bitmap lookup table: triplet position out of index"));
634 p
= bitmap_git
->table_lookup
+ st_mult(pos
, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
);
636 return bitmap_lookup_table_get_triplet_by_pointer(triplet
, p
);
640 * Searches for a matching triplet. `commit_pos` is a pointer
641 * to the wanted commit position value. `table_entry` points to
642 * a triplet in lookup table. The first 4 bytes of each
643 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
645 static int triplet_cmp(const void *commit_pos
, const void *table_entry
)
648 uint32_t a
= *(uint32_t *)commit_pos
;
649 uint32_t b
= get_be32(table_entry
);
658 static uint32_t bitmap_bsearch_pos(struct bitmap_index
*bitmap_git
,
659 struct object_id
*oid
,
664 if (bitmap_is_midx(bitmap_git
))
665 found
= bsearch_midx(oid
, bitmap_git
->midx
, result
);
667 found
= bsearch_pack(oid
, bitmap_git
->pack
, result
);
673 * `bsearch_triplet_by_pos` function searches for the raw triplet
674 * having commit position same as `commit_pos` and fills `triplet`
675 * object from the raw triplet. Returns 1 on success and 0 on
678 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos
,
679 struct bitmap_index
*bitmap_git
,
680 struct bitmap_lookup_table_triplet
*triplet
)
682 unsigned char *p
= bsearch(&commit_pos
, bitmap_git
->table_lookup
, bitmap_git
->entry_count
,
683 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
, triplet_cmp
);
688 return bitmap_lookup_table_get_triplet_by_pointer(triplet
, p
);
691 static struct stored_bitmap
*lazy_bitmap_for_commit(struct bitmap_index
*bitmap_git
,
692 struct commit
*commit
)
694 uint32_t commit_pos
, xor_row
;
697 struct bitmap_lookup_table_triplet triplet
;
698 struct object_id
*oid
= &commit
->object
.oid
;
699 struct ewah_bitmap
*bitmap
;
700 struct stored_bitmap
*xor_bitmap
= NULL
;
701 const int bitmap_header_size
= 6;
702 static struct bitmap_lookup_table_xor_item
*xor_items
= NULL
;
703 static size_t xor_items_nr
= 0, xor_items_alloc
= 0;
704 static int is_corrupt
= 0;
707 struct bitmap_lookup_table_xor_item
*xor_item
;
712 if (!bitmap_bsearch_pos(bitmap_git
, oid
, &commit_pos
))
715 if (bitmap_bsearch_triplet_by_pos(commit_pos
, bitmap_git
, &triplet
) < 0)
719 offset
= triplet
.offset
;
720 xor_row
= triplet
.xor_row
;
722 while (xor_row
!= 0xffffffff) {
723 ALLOC_GROW(xor_items
, xor_items_nr
+ 1, xor_items_alloc
);
725 if (xor_items_nr
+ 1 >= bitmap_git
->entry_count
) {
726 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
730 if (bitmap_lookup_table_get_triplet(bitmap_git
, xor_row
, &triplet
) < 0)
733 xor_item
= &xor_items
[xor_items_nr
];
734 xor_item
->offset
= triplet
.offset
;
736 if (nth_bitmap_object_oid(bitmap_git
, &xor_item
->oid
, triplet
.commit_pos
) < 0) {
737 error(_("corrupt bitmap lookup table: commit index %u out of range"),
742 hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
, xor_item
->oid
);
745 * If desired bitmap is already stored, we don't need
746 * to iterate further. Because we know that bitmaps
747 * that are needed to be parsed to parse this bitmap
748 * has already been stored. So, assign this stored bitmap
751 if (hash_pos
< kh_end(bitmap_git
->bitmaps
) &&
752 (xor_bitmap
= kh_value(bitmap_git
->bitmaps
, hash_pos
)))
755 xor_row
= triplet
.xor_row
;
758 while (xor_items_nr
) {
759 xor_item
= &xor_items
[xor_items_nr
- 1];
760 bitmap_git
->map_pos
= xor_item
->offset
;
761 if (bitmap_git
->map_size
- bitmap_git
->map_pos
< bitmap_header_size
) {
762 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
763 oid_to_hex(&xor_item
->oid
));
767 bitmap_git
->map_pos
+= sizeof(uint32_t) + sizeof(uint8_t);
768 xor_flags
= read_u8(bitmap_git
->map
, &bitmap_git
->map_pos
);
769 bitmap
= read_bitmap_1(bitmap_git
);
774 xor_bitmap
= store_bitmap(bitmap_git
, bitmap
, &xor_item
->oid
, xor_bitmap
, xor_flags
);
778 bitmap_git
->map_pos
= offset
;
779 if (bitmap_git
->map_size
- bitmap_git
->map_pos
< bitmap_header_size
) {
780 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
786 * Don't bother reading the commit's index position or its xor
789 * - The commit's index position is irrelevant to us, since
790 * load_bitmap_entries_v1 only uses it to learn the object
791 * id which is used to compute the hashmap's key. We already
792 * have an object id, so no need to look it up again.
794 * - The xor_offset is unusable for us, since it specifies how
795 * many entries previous to ours we should look at. This
796 * makes sense when reading the bitmaps sequentially (as in
797 * load_bitmap_entries_v1()), since we can keep track of
798 * each bitmap as we read them.
800 * But it can't work for us, since the bitmap's don't have a
801 * fixed size. So we learn the position of the xor'd bitmap
802 * from the commit table (and resolve it to a bitmap in the
803 * above if-statement).
805 * Instead, we can skip ahead and immediately read the flags and
808 bitmap_git
->map_pos
+= sizeof(uint32_t) + sizeof(uint8_t);
809 flags
= read_u8(bitmap_git
->map
, &bitmap_git
->map_pos
);
810 bitmap
= read_bitmap_1(bitmap_git
);
815 return store_bitmap(bitmap_git
, bitmap
, oid
, xor_bitmap
, flags
);
823 struct ewah_bitmap
*bitmap_for_commit(struct bitmap_index
*bitmap_git
,
824 struct commit
*commit
)
826 khiter_t hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
,
828 if (hash_pos
>= kh_end(bitmap_git
->bitmaps
)) {
829 struct stored_bitmap
*bitmap
= NULL
;
830 if (!bitmap_git
->table_lookup
)
833 /* this is a fairly hot codepath - no trace2_region please */
834 /* NEEDSWORK: cache misses aren't recorded */
835 bitmap
= lazy_bitmap_for_commit(bitmap_git
, commit
);
838 return lookup_stored_bitmap(bitmap
);
840 return lookup_stored_bitmap(kh_value(bitmap_git
->bitmaps
, hash_pos
));
843 static inline int bitmap_position_extended(struct bitmap_index
*bitmap_git
,
844 const struct object_id
*oid
)
846 kh_oid_pos_t
*positions
= bitmap_git
->ext_index
.positions
;
847 khiter_t pos
= kh_get_oid_pos(positions
, *oid
);
849 if (pos
< kh_end(positions
)) {
850 int bitmap_pos
= kh_value(positions
, pos
);
851 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
857 static inline int bitmap_position_packfile(struct bitmap_index
*bitmap_git
,
858 const struct object_id
*oid
)
861 off_t offset
= find_pack_entry_one(oid
->hash
, bitmap_git
->pack
);
865 if (offset_to_pack_pos(bitmap_git
->pack
, offset
, &pos
) < 0)
870 static int bitmap_position_midx(struct bitmap_index
*bitmap_git
,
871 const struct object_id
*oid
)
874 if (!bsearch_midx(oid
, bitmap_git
->midx
, &want
))
877 if (midx_to_pack_pos(bitmap_git
->midx
, want
, &got
) < 0)
882 static int bitmap_position(struct bitmap_index
*bitmap_git
,
883 const struct object_id
*oid
)
886 if (bitmap_is_midx(bitmap_git
))
887 pos
= bitmap_position_midx(bitmap_git
, oid
);
889 pos
= bitmap_position_packfile(bitmap_git
, oid
);
890 return (pos
>= 0) ? pos
: bitmap_position_extended(bitmap_git
, oid
);
893 static int ext_index_add_object(struct bitmap_index
*bitmap_git
,
894 struct object
*object
, const char *name
)
896 struct eindex
*eindex
= &bitmap_git
->ext_index
;
902 hash_pos
= kh_put_oid_pos(eindex
->positions
, object
->oid
, &hash_ret
);
904 if (eindex
->count
>= eindex
->alloc
) {
905 eindex
->alloc
= (eindex
->alloc
+ 16) * 3 / 2;
906 REALLOC_ARRAY(eindex
->objects
, eindex
->alloc
);
907 REALLOC_ARRAY(eindex
->hashes
, eindex
->alloc
);
910 bitmap_pos
= eindex
->count
;
911 eindex
->objects
[eindex
->count
] = object
;
912 eindex
->hashes
[eindex
->count
] = pack_name_hash(name
);
913 kh_value(eindex
->positions
, hash_pos
) = bitmap_pos
;
916 bitmap_pos
= kh_value(eindex
->positions
, hash_pos
);
919 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
922 struct bitmap_show_data
{
923 struct bitmap_index
*bitmap_git
;
927 static void show_object(struct object
*object
, const char *name
, void *data_
)
929 struct bitmap_show_data
*data
= data_
;
932 bitmap_pos
= bitmap_position(data
->bitmap_git
, &object
->oid
);
935 bitmap_pos
= ext_index_add_object(data
->bitmap_git
, object
,
938 bitmap_set(data
->base
, bitmap_pos
);
941 static void show_commit(struct commit
*commit
, void *data
)
945 static int add_to_include_set(struct bitmap_index
*bitmap_git
,
946 struct include_data
*data
,
947 struct commit
*commit
,
950 struct ewah_bitmap
*partial
;
952 if (data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
))
955 if (bitmap_get(data
->base
, bitmap_pos
))
958 partial
= bitmap_for_commit(bitmap_git
, commit
);
960 bitmap_or_ewah(data
->base
, partial
);
964 bitmap_set(data
->base
, bitmap_pos
);
968 static int should_include(struct commit
*commit
, void *_data
)
970 struct include_data
*data
= _data
;
973 bitmap_pos
= bitmap_position(data
->bitmap_git
, &commit
->object
.oid
);
975 bitmap_pos
= ext_index_add_object(data
->bitmap_git
,
976 (struct object
*)commit
,
979 if (!add_to_include_set(data
->bitmap_git
, data
, commit
, bitmap_pos
)) {
980 struct commit_list
*parent
= commit
->parents
;
983 parent
->item
->object
.flags
|= SEEN
;
984 parent
= parent
->next
;
993 static int should_include_obj(struct object
*obj
, void *_data
)
995 struct include_data
*data
= _data
;
998 bitmap_pos
= bitmap_position(data
->bitmap_git
, &obj
->oid
);
1001 if ((data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
)) ||
1002 bitmap_get(data
->base
, bitmap_pos
)) {
1009 static int add_commit_to_bitmap(struct bitmap_index
*bitmap_git
,
1010 struct bitmap
**base
,
1011 struct commit
*commit
)
1013 struct ewah_bitmap
*or_with
= bitmap_for_commit(bitmap_git
, commit
);
1019 *base
= ewah_to_bitmap(or_with
);
1021 bitmap_or_ewah(*base
, or_with
);
1026 static struct bitmap
*find_objects(struct bitmap_index
*bitmap_git
,
1027 struct rev_info
*revs
,
1028 struct object_list
*roots
,
1029 struct bitmap
*seen
)
1031 struct bitmap
*base
= NULL
;
1034 struct object_list
*not_mapped
= NULL
;
1037 * Go through all the roots for the walk. The ones that have bitmaps
1038 * on the bitmap index will be `or`ed together to form an initial
1039 * global reachability analysis.
1041 * The ones without bitmaps in the index will be stored in the
1042 * `not_mapped_list` for further processing.
1045 struct object
*object
= roots
->item
;
1046 roots
= roots
->next
;
1048 if (object
->type
== OBJ_COMMIT
&&
1049 add_commit_to_bitmap(bitmap_git
, &base
, (struct commit
*)object
)) {
1050 object
->flags
|= SEEN
;
1054 object_list_insert(object
, ¬_mapped
);
1058 * Best case scenario: We found bitmaps for all the roots,
1059 * so the resulting `or` bitmap has the full reachability analysis
1067 * Let's iterate through all the roots that don't have bitmaps to
1068 * check if we can determine them to be reachable from the existing
1071 * If we cannot find them in the existing global bitmap, we'll need
1072 * to push them to an actual walk and run it until we can confirm
1073 * they are reachable
1076 struct object
*object
= roots
->item
;
1079 roots
= roots
->next
;
1080 pos
= bitmap_position(bitmap_git
, &object
->oid
);
1082 if (pos
< 0 || base
== NULL
|| !bitmap_get(base
, pos
)) {
1083 object
->flags
&= ~UNINTERESTING
;
1084 add_pending_object(revs
, object
, "");
1087 object
->flags
|= SEEN
;
1092 struct include_data incdata
;
1093 struct bitmap_show_data show_data
;
1096 base
= bitmap_new();
1098 incdata
.bitmap_git
= bitmap_git
;
1099 incdata
.base
= base
;
1100 incdata
.seen
= seen
;
1102 revs
->include_check
= should_include
;
1103 revs
->include_check_obj
= should_include_obj
;
1104 revs
->include_check_data
= &incdata
;
1106 if (prepare_revision_walk(revs
))
1107 die(_("revision walk setup failed"));
1109 show_data
.bitmap_git
= bitmap_git
;
1110 show_data
.base
= base
;
1112 traverse_commit_list(revs
,
1113 show_commit
, show_object
,
1116 revs
->include_check
= NULL
;
1117 revs
->include_check_obj
= NULL
;
1118 revs
->include_check_data
= NULL
;
1124 static void show_extended_objects(struct bitmap_index
*bitmap_git
,
1125 struct rev_info
*revs
,
1126 show_reachable_fn show_reach
)
1128 struct bitmap
*objects
= bitmap_git
->result
;
1129 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1132 for (i
= 0; i
< eindex
->count
; ++i
) {
1135 if (!bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1138 obj
= eindex
->objects
[i
];
1139 if ((obj
->type
== OBJ_BLOB
&& !revs
->blob_objects
) ||
1140 (obj
->type
== OBJ_TREE
&& !revs
->tree_objects
) ||
1141 (obj
->type
== OBJ_TAG
&& !revs
->tag_objects
))
1144 show_reach(&obj
->oid
, obj
->type
, 0, eindex
->hashes
[i
], NULL
, 0);
1148 static void init_type_iterator(struct ewah_iterator
*it
,
1149 struct bitmap_index
*bitmap_git
,
1150 enum object_type type
)
1154 ewah_iterator_init(it
, bitmap_git
->commits
);
1158 ewah_iterator_init(it
, bitmap_git
->trees
);
1162 ewah_iterator_init(it
, bitmap_git
->blobs
);
1166 ewah_iterator_init(it
, bitmap_git
->tags
);
1170 BUG("object type %d not stored by bitmap type index", type
);
1175 static void show_objects_for_type(
1176 struct bitmap_index
*bitmap_git
,
1177 enum object_type object_type
,
1178 show_reachable_fn show_reach
)
1183 struct ewah_iterator it
;
1186 struct bitmap
*objects
= bitmap_git
->result
;
1188 init_type_iterator(&it
, bitmap_git
, object_type
);
1190 for (i
= 0; i
< objects
->word_alloc
&&
1191 ewah_iterator_next(&filter
, &it
); i
++) {
1192 eword_t word
= objects
->words
[i
] & filter
;
1193 size_t pos
= (i
* BITS_IN_EWORD
);
1198 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1199 struct packed_git
*pack
;
1200 struct object_id oid
;
1201 uint32_t hash
= 0, index_pos
;
1204 if ((word
>> offset
) == 0)
1207 offset
+= ewah_bit_ctz64(word
>> offset
);
1209 if (bitmap_is_midx(bitmap_git
)) {
1210 struct multi_pack_index
*m
= bitmap_git
->midx
;
1213 index_pos
= pack_pos_to_midx(m
, pos
+ offset
);
1214 ofs
= nth_midxed_offset(m
, index_pos
);
1215 nth_midxed_object_oid(&oid
, m
, index_pos
);
1217 pack_id
= nth_midxed_pack_int_id(m
, index_pos
);
1218 pack
= bitmap_git
->midx
->packs
[pack_id
];
1220 index_pos
= pack_pos_to_index(bitmap_git
->pack
, pos
+ offset
);
1221 ofs
= pack_pos_to_offset(bitmap_git
->pack
, pos
+ offset
);
1222 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
1224 pack
= bitmap_git
->pack
;
1227 if (bitmap_git
->hashes
)
1228 hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
1230 show_reach(&oid
, object_type
, 0, hash
, pack
, ofs
);
1235 static int in_bitmapped_pack(struct bitmap_index
*bitmap_git
,
1236 struct object_list
*roots
)
1239 struct object
*object
= roots
->item
;
1240 roots
= roots
->next
;
1242 if (bitmap_is_midx(bitmap_git
)) {
1243 if (bsearch_midx(&object
->oid
, bitmap_git
->midx
, NULL
))
1246 if (find_pack_entry_one(object
->oid
.hash
, bitmap_git
->pack
) > 0)
1254 static struct bitmap
*find_tip_objects(struct bitmap_index
*bitmap_git
,
1255 struct object_list
*tip_objects
,
1256 enum object_type type
)
1258 struct bitmap
*result
= bitmap_new();
1259 struct object_list
*p
;
1261 for (p
= tip_objects
; p
; p
= p
->next
) {
1264 if (p
->item
->type
!= type
)
1267 pos
= bitmap_position(bitmap_git
, &p
->item
->oid
);
1271 bitmap_set(result
, pos
);
1277 static void filter_bitmap_exclude_type(struct bitmap_index
*bitmap_git
,
1278 struct object_list
*tip_objects
,
1279 struct bitmap
*to_filter
,
1280 enum object_type type
)
1282 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1283 struct bitmap
*tips
;
1284 struct ewah_iterator it
;
1289 * The non-bitmap version of this filter never removes
1290 * objects which the other side specifically asked for,
1291 * so we must match that behavior.
1293 tips
= find_tip_objects(bitmap_git
, tip_objects
, type
);
1296 * We can use the type-level bitmap for 'type' to work in whole
1297 * words for the objects that are actually in the bitmapped
1300 for (i
= 0, init_type_iterator(&it
, bitmap_git
, type
);
1301 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1303 if (i
< tips
->word_alloc
)
1304 mask
&= ~tips
->words
[i
];
1305 to_filter
->words
[i
] &= ~mask
;
1309 * Clear any objects that weren't in the packfile (and so would
1310 * not have been caught by the loop above. We'll have to check
1311 * them individually.
1313 for (i
= 0; i
< eindex
->count
; i
++) {
1314 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1315 if (eindex
->objects
[i
]->type
== type
&&
1316 bitmap_get(to_filter
, pos
) &&
1317 !bitmap_get(tips
, pos
))
1318 bitmap_unset(to_filter
, pos
);
1324 static void filter_bitmap_blob_none(struct bitmap_index
*bitmap_git
,
1325 struct object_list
*tip_objects
,
1326 struct bitmap
*to_filter
)
1328 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1332 static unsigned long get_size_by_pos(struct bitmap_index
*bitmap_git
,
1336 struct object_info oi
= OBJECT_INFO_INIT
;
1340 if (pos
< bitmap_num_objects(bitmap_git
)) {
1341 struct packed_git
*pack
;
1344 if (bitmap_is_midx(bitmap_git
)) {
1345 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, pos
);
1346 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
1348 pack
= bitmap_git
->midx
->packs
[pack_id
];
1349 ofs
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
1351 pack
= bitmap_git
->pack
;
1352 ofs
= pack_pos_to_offset(pack
, pos
);
1355 if (packed_object_info(the_repository
, pack
, ofs
, &oi
) < 0) {
1356 struct object_id oid
;
1357 nth_bitmap_object_oid(bitmap_git
, &oid
,
1358 pack_pos_to_index(pack
, pos
));
1359 die(_("unable to get size of %s"), oid_to_hex(&oid
));
1362 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1363 struct object
*obj
= eindex
->objects
[pos
- bitmap_num_objects(bitmap_git
)];
1364 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
1365 die(_("unable to get size of %s"), oid_to_hex(&obj
->oid
));
1371 static void filter_bitmap_blob_limit(struct bitmap_index
*bitmap_git
,
1372 struct object_list
*tip_objects
,
1373 struct bitmap
*to_filter
,
1374 unsigned long limit
)
1376 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1377 struct bitmap
*tips
;
1378 struct ewah_iterator it
;
1382 tips
= find_tip_objects(bitmap_git
, tip_objects
, OBJ_BLOB
);
1384 for (i
= 0, init_type_iterator(&it
, bitmap_git
, OBJ_BLOB
);
1385 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1387 eword_t word
= to_filter
->words
[i
] & mask
;
1390 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1393 if ((word
>> offset
) == 0)
1395 offset
+= ewah_bit_ctz64(word
>> offset
);
1396 pos
= i
* BITS_IN_EWORD
+ offset
;
1398 if (!bitmap_get(tips
, pos
) &&
1399 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1400 bitmap_unset(to_filter
, pos
);
1404 for (i
= 0; i
< eindex
->count
; i
++) {
1405 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1406 if (eindex
->objects
[i
]->type
== OBJ_BLOB
&&
1407 bitmap_get(to_filter
, pos
) &&
1408 !bitmap_get(tips
, pos
) &&
1409 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1410 bitmap_unset(to_filter
, pos
);
1416 static void filter_bitmap_tree_depth(struct bitmap_index
*bitmap_git
,
1417 struct object_list
*tip_objects
,
1418 struct bitmap
*to_filter
,
1419 unsigned long limit
)
1422 BUG("filter_bitmap_tree_depth given non-zero limit");
1424 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1426 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1430 static void filter_bitmap_object_type(struct bitmap_index
*bitmap_git
,
1431 struct object_list
*tip_objects
,
1432 struct bitmap
*to_filter
,
1433 enum object_type object_type
)
1435 if (object_type
< OBJ_COMMIT
|| object_type
> OBJ_TAG
)
1436 BUG("filter_bitmap_object_type given invalid object");
1438 if (object_type
!= OBJ_TAG
)
1439 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TAG
);
1440 if (object_type
!= OBJ_COMMIT
)
1441 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_COMMIT
);
1442 if (object_type
!= OBJ_TREE
)
1443 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TREE
);
1444 if (object_type
!= OBJ_BLOB
)
1445 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_BLOB
);
1448 static int filter_bitmap(struct bitmap_index
*bitmap_git
,
1449 struct object_list
*tip_objects
,
1450 struct bitmap
*to_filter
,
1451 struct list_objects_filter_options
*filter
)
1453 if (!filter
|| filter
->choice
== LOFC_DISABLED
)
1456 if (filter
->choice
== LOFC_BLOB_NONE
) {
1458 filter_bitmap_blob_none(bitmap_git
, tip_objects
,
1463 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
1465 filter_bitmap_blob_limit(bitmap_git
, tip_objects
,
1467 filter
->blob_limit_value
);
1471 if (filter
->choice
== LOFC_TREE_DEPTH
&&
1472 filter
->tree_exclude_depth
== 0) {
1474 filter_bitmap_tree_depth(bitmap_git
, tip_objects
,
1476 filter
->tree_exclude_depth
);
1480 if (filter
->choice
== LOFC_OBJECT_TYPE
) {
1482 filter_bitmap_object_type(bitmap_git
, tip_objects
,
1484 filter
->object_type
);
1488 if (filter
->choice
== LOFC_COMBINE
) {
1490 for (i
= 0; i
< filter
->sub_nr
; i
++) {
1491 if (filter_bitmap(bitmap_git
, tip_objects
, to_filter
,
1492 &filter
->sub
[i
]) < 0)
1498 /* filter choice not handled */
1502 static int can_filter_bitmap(struct list_objects_filter_options
*filter
)
1504 return !filter_bitmap(NULL
, NULL
, NULL
, filter
);
1507 struct bitmap_index
*prepare_bitmap_walk(struct rev_info
*revs
,
1508 int filter_provided_objects
)
1512 struct object_list
*wants
= NULL
;
1513 struct object_list
*haves
= NULL
;
1515 struct bitmap
*wants_bitmap
= NULL
;
1516 struct bitmap
*haves_bitmap
= NULL
;
1518 struct bitmap_index
*bitmap_git
;
1521 * We can't do pathspec limiting with bitmaps, because we don't know
1522 * which commits are associated with which object changes (let alone
1523 * even which objects are associated with which paths).
1528 if (!can_filter_bitmap(&revs
->filter
))
1531 /* try to open a bitmapped pack, but don't parse it yet
1532 * because we may not need to use it */
1533 CALLOC_ARRAY(bitmap_git
, 1);
1534 if (open_bitmap(revs
->repo
, bitmap_git
) < 0)
1537 for (i
= 0; i
< revs
->pending
.nr
; ++i
) {
1538 struct object
*object
= revs
->pending
.objects
[i
].item
;
1540 if (object
->type
== OBJ_NONE
)
1541 parse_object_or_die(&object
->oid
, NULL
);
1543 while (object
->type
== OBJ_TAG
) {
1544 struct tag
*tag
= (struct tag
*) object
;
1546 if (object
->flags
& UNINTERESTING
)
1547 object_list_insert(object
, &haves
);
1549 object_list_insert(object
, &wants
);
1551 object
= parse_object_or_die(get_tagged_oid(tag
), NULL
);
1552 object
->flags
|= (tag
->object
.flags
& UNINTERESTING
);
1555 if (object
->flags
& UNINTERESTING
)
1556 object_list_insert(object
, &haves
);
1558 object_list_insert(object
, &wants
);
1562 * if we have a HAVES list, but none of those haves is contained
1563 * in the packfile that has a bitmap, we don't have anything to
1566 if (haves
&& !in_bitmapped_pack(bitmap_git
, haves
))
1569 /* if we don't want anything, we're done here */
1574 * now we're going to use bitmaps, so load the actual bitmap entries
1575 * from disk. this is the point of no return; after this the rev_list
1576 * becomes invalidated and we must perform the revwalk through bitmaps
1578 if (load_bitmap(bitmap_git
) < 0)
1581 object_array_clear(&revs
->pending
);
1584 revs
->ignore_missing_links
= 1;
1585 haves_bitmap
= find_objects(bitmap_git
, revs
, haves
, NULL
);
1586 reset_revision_walk();
1587 revs
->ignore_missing_links
= 0;
1590 BUG("failed to perform bitmap walk");
1593 wants_bitmap
= find_objects(bitmap_git
, revs
, wants
, haves_bitmap
);
1596 BUG("failed to perform bitmap walk");
1599 bitmap_and_not(wants_bitmap
, haves_bitmap
);
1601 filter_bitmap(bitmap_git
,
1602 (revs
->filter
.choice
&& filter_provided_objects
) ? NULL
: wants
,
1606 bitmap_git
->result
= wants_bitmap
;
1607 bitmap_git
->haves
= haves_bitmap
;
1609 object_list_free(&wants
);
1610 object_list_free(&haves
);
1615 free_bitmap_index(bitmap_git
);
1616 object_list_free(&wants
);
1617 object_list_free(&haves
);
1622 * -1 means "stop trying further objects"; 0 means we may or may not have
1623 * reused, but you can keep feeding bits.
1625 static int try_partial_reuse(struct packed_git
*pack
,
1627 struct bitmap
*reuse
,
1628 struct pack_window
**w_curs
)
1630 off_t offset
, delta_obj_offset
;
1631 enum object_type type
;
1635 * try_partial_reuse() is called either on (a) objects in the
1636 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1637 * objects in the preferred pack of a multi-pack bitmap.
1638 * Importantly, the latter can pretend as if only a single pack
1641 * - The first pack->num_objects bits of a MIDX bitmap are
1642 * reserved for the preferred pack, and
1644 * - Ties due to duplicate objects are always resolved in
1645 * favor of the preferred pack.
1647 * Therefore we do not need to ever ask the MIDX for its copy of
1648 * an object by OID, since it will always select it from the
1649 * preferred pack. Likewise, the selected copy of the base
1650 * object for any deltas will reside in the same pack.
1652 * This means that we can reuse pos when looking up the bit in
1653 * the reuse bitmap, too, since bits corresponding to the
1654 * preferred pack precede all bits from other packs.
1657 if (pos
>= pack
->num_objects
)
1658 return -1; /* not actually in the pack or MIDX preferred pack */
1660 offset
= delta_obj_offset
= pack_pos_to_offset(pack
, pos
);
1661 type
= unpack_object_header(pack
, w_curs
, &offset
, &size
);
1663 return -1; /* broken packfile, punt */
1665 if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
) {
1670 * Find the position of the base object so we can look it up
1671 * in our bitmaps. If we can't come up with an offset, or if
1672 * that offset is not in the revidx, the pack is corrupt.
1673 * There's nothing we can do, so just punt on this object,
1674 * and the normal slow path will complain about it in
1677 base_offset
= get_delta_base(pack
, w_curs
, &offset
, type
,
1681 if (offset_to_pack_pos(pack
, base_offset
, &base_pos
) < 0)
1685 * We assume delta dependencies always point backwards. This
1686 * lets us do a single pass, and is basically always true
1687 * due to the way OFS_DELTAs work. You would not typically
1688 * find REF_DELTA in a bitmapped pack, since we only bitmap
1689 * packs we write fresh, and OFS_DELTA is the default). But
1690 * let's double check to make sure the pack wasn't written with
1693 if (base_pos
>= pos
)
1697 * And finally, if we're not sending the base as part of our
1698 * reuse chunk, then don't send this object either. The base
1699 * would come after us, along with other objects not
1700 * necessarily in the pack, which means we'd need to convert
1701 * to REF_DELTA on the fly. Better to just let the normal
1702 * object_entry code path handle it.
1704 if (!bitmap_get(reuse
, base_pos
))
1709 * If we got here, then the object is OK to reuse. Mark it.
1711 bitmap_set(reuse
, pos
);
1715 uint32_t midx_preferred_pack(struct bitmap_index
*bitmap_git
)
1717 struct multi_pack_index
*m
= bitmap_git
->midx
;
1719 BUG("midx_preferred_pack: requires non-empty MIDX");
1720 return nth_midxed_pack_int_id(m
, pack_pos_to_midx(bitmap_git
->midx
, 0));
1723 int reuse_partial_packfile_from_bitmap(struct bitmap_index
*bitmap_git
,
1724 struct packed_git
**packfile_out
,
1726 struct bitmap
**reuse_out
)
1728 struct packed_git
*pack
;
1729 struct bitmap
*result
= bitmap_git
->result
;
1730 struct bitmap
*reuse
;
1731 struct pack_window
*w_curs
= NULL
;
1734 uint32_t objects_nr
;
1738 load_reverse_index(bitmap_git
);
1740 if (bitmap_is_midx(bitmap_git
))
1741 pack
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
1743 pack
= bitmap_git
->pack
;
1744 objects_nr
= pack
->num_objects
;
1746 while (i
< result
->word_alloc
&& result
->words
[i
] == (eword_t
)~0)
1750 * Don't mark objects not in the packfile or preferred pack. This bitmap
1751 * marks objects eligible for reuse, but the pack-reuse code only
1752 * understands how to reuse a single pack. Since the preferred pack is
1753 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1754 * we use it instead of another pack. In single-pack bitmaps, the choice
1757 if (i
> objects_nr
/ BITS_IN_EWORD
)
1758 i
= objects_nr
/ BITS_IN_EWORD
;
1760 reuse
= bitmap_word_alloc(i
);
1761 memset(reuse
->words
, 0xFF, i
* sizeof(eword_t
));
1763 for (; i
< result
->word_alloc
; ++i
) {
1764 eword_t word
= result
->words
[i
];
1765 size_t pos
= (i
* BITS_IN_EWORD
);
1767 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1768 if ((word
>> offset
) == 0)
1771 offset
+= ewah_bit_ctz64(word
>> offset
);
1772 if (try_partial_reuse(pack
, pos
+ offset
,
1773 reuse
, &w_curs
) < 0) {
1775 * try_partial_reuse indicated we couldn't reuse
1776 * any bits, so there is no point in trying more
1777 * bits in the current word, or any other words
1780 * Jump out of both loops to avoid future
1781 * unnecessary calls to try_partial_reuse.
1789 unuse_pack(&w_curs
);
1791 *entries
= bitmap_popcount(reuse
);
1798 * Drop any reused objects from the result, since they will not
1799 * need to be handled separately.
1801 bitmap_and_not(result
, reuse
);
1802 *packfile_out
= pack
;
1807 int bitmap_walk_contains(struct bitmap_index
*bitmap_git
,
1808 struct bitmap
*bitmap
, const struct object_id
*oid
)
1815 idx
= bitmap_position(bitmap_git
, oid
);
1816 return idx
>= 0 && bitmap_get(bitmap
, idx
);
1819 void traverse_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1820 struct rev_info
*revs
,
1821 show_reachable_fn show_reachable
)
1823 assert(bitmap_git
->result
);
1825 show_objects_for_type(bitmap_git
, OBJ_COMMIT
, show_reachable
);
1826 if (revs
->tree_objects
)
1827 show_objects_for_type(bitmap_git
, OBJ_TREE
, show_reachable
);
1828 if (revs
->blob_objects
)
1829 show_objects_for_type(bitmap_git
, OBJ_BLOB
, show_reachable
);
1830 if (revs
->tag_objects
)
1831 show_objects_for_type(bitmap_git
, OBJ_TAG
, show_reachable
);
1833 show_extended_objects(bitmap_git
, revs
, show_reachable
);
1836 static uint32_t count_object_type(struct bitmap_index
*bitmap_git
,
1837 enum object_type type
)
1839 struct bitmap
*objects
= bitmap_git
->result
;
1840 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1842 uint32_t i
= 0, count
= 0;
1843 struct ewah_iterator it
;
1846 init_type_iterator(&it
, bitmap_git
, type
);
1848 while (i
< objects
->word_alloc
&& ewah_iterator_next(&filter
, &it
)) {
1849 eword_t word
= objects
->words
[i
++] & filter
;
1850 count
+= ewah_bit_popcount64(word
);
1853 for (i
= 0; i
< eindex
->count
; ++i
) {
1854 if (eindex
->objects
[i
]->type
== type
&&
1855 bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1862 void count_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1863 uint32_t *commits
, uint32_t *trees
,
1864 uint32_t *blobs
, uint32_t *tags
)
1866 assert(bitmap_git
->result
);
1869 *commits
= count_object_type(bitmap_git
, OBJ_COMMIT
);
1872 *trees
= count_object_type(bitmap_git
, OBJ_TREE
);
1875 *blobs
= count_object_type(bitmap_git
, OBJ_BLOB
);
1878 *tags
= count_object_type(bitmap_git
, OBJ_TAG
);
1881 struct bitmap_test_data
{
1882 struct bitmap_index
*bitmap_git
;
1883 struct bitmap
*base
;
1884 struct bitmap
*commits
;
1885 struct bitmap
*trees
;
1886 struct bitmap
*blobs
;
1887 struct bitmap
*tags
;
1888 struct progress
*prg
;
1892 static void test_bitmap_type(struct bitmap_test_data
*tdata
,
1893 struct object
*obj
, int pos
)
1895 enum object_type bitmap_type
= OBJ_NONE
;
1898 if (bitmap_get(tdata
->commits
, pos
)) {
1899 bitmap_type
= OBJ_COMMIT
;
1902 if (bitmap_get(tdata
->trees
, pos
)) {
1903 bitmap_type
= OBJ_TREE
;
1906 if (bitmap_get(tdata
->blobs
, pos
)) {
1907 bitmap_type
= OBJ_BLOB
;
1910 if (bitmap_get(tdata
->tags
, pos
)) {
1911 bitmap_type
= OBJ_TAG
;
1915 if (bitmap_type
== OBJ_NONE
)
1916 die(_("object '%s' not found in type bitmaps"),
1917 oid_to_hex(&obj
->oid
));
1920 die(_("object '%s' does not have a unique type"),
1921 oid_to_hex(&obj
->oid
));
1923 if (bitmap_type
!= obj
->type
)
1924 die(_("object '%s': real type '%s', expected: '%s'"),
1925 oid_to_hex(&obj
->oid
),
1926 type_name(obj
->type
),
1927 type_name(bitmap_type
));
1930 static void test_show_object(struct object
*object
, const char *name
,
1933 struct bitmap_test_data
*tdata
= data
;
1936 bitmap_pos
= bitmap_position(tdata
->bitmap_git
, &object
->oid
);
1938 die(_("object not in bitmap: '%s'"), oid_to_hex(&object
->oid
));
1939 test_bitmap_type(tdata
, object
, bitmap_pos
);
1941 bitmap_set(tdata
->base
, bitmap_pos
);
1942 display_progress(tdata
->prg
, ++tdata
->seen
);
1945 static void test_show_commit(struct commit
*commit
, void *data
)
1947 struct bitmap_test_data
*tdata
= data
;
1950 bitmap_pos
= bitmap_position(tdata
->bitmap_git
,
1951 &commit
->object
.oid
);
1953 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit
->object
.oid
));
1954 test_bitmap_type(tdata
, &commit
->object
, bitmap_pos
);
1956 bitmap_set(tdata
->base
, bitmap_pos
);
1957 display_progress(tdata
->prg
, ++tdata
->seen
);
1960 void test_bitmap_walk(struct rev_info
*revs
)
1962 struct object
*root
;
1963 struct bitmap
*result
= NULL
;
1964 size_t result_popcnt
;
1965 struct bitmap_test_data tdata
;
1966 struct bitmap_index
*bitmap_git
;
1967 struct ewah_bitmap
*bm
;
1969 if (!(bitmap_git
= prepare_bitmap_git(revs
->repo
)))
1970 die(_("failed to load bitmap indexes"));
1972 if (revs
->pending
.nr
!= 1)
1973 die(_("you must specify exactly one commit to test"));
1975 fprintf_ln(stderr
, "Bitmap v%d test (%d entries%s)",
1976 bitmap_git
->version
,
1977 bitmap_git
->entry_count
,
1978 bitmap_git
->table_lookup
? "" : " loaded");
1980 root
= revs
->pending
.objects
[0].item
;
1981 bm
= bitmap_for_commit(bitmap_git
, (struct commit
*)root
);
1984 fprintf_ln(stderr
, "Found bitmap for '%s'. %d bits / %08x checksum",
1985 oid_to_hex(&root
->oid
), (int)bm
->bit_size
, ewah_checksum(bm
));
1987 result
= ewah_to_bitmap(bm
);
1991 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root
->oid
));
1993 revs
->tag_objects
= 1;
1994 revs
->tree_objects
= 1;
1995 revs
->blob_objects
= 1;
1997 result_popcnt
= bitmap_popcount(result
);
1999 if (prepare_revision_walk(revs
))
2000 die(_("revision walk setup failed"));
2002 tdata
.bitmap_git
= bitmap_git
;
2003 tdata
.base
= bitmap_new();
2004 tdata
.commits
= ewah_to_bitmap(bitmap_git
->commits
);
2005 tdata
.trees
= ewah_to_bitmap(bitmap_git
->trees
);
2006 tdata
.blobs
= ewah_to_bitmap(bitmap_git
->blobs
);
2007 tdata
.tags
= ewah_to_bitmap(bitmap_git
->tags
);
2008 tdata
.prg
= start_progress("Verifying bitmap entries", result_popcnt
);
2011 traverse_commit_list(revs
, &test_show_commit
, &test_show_object
, &tdata
);
2013 stop_progress(&tdata
.prg
);
2015 if (bitmap_equals(result
, tdata
.base
))
2016 fprintf_ln(stderr
, "OK!");
2018 die(_("mismatch in bitmap results"));
2020 bitmap_free(result
);
2021 bitmap_free(tdata
.base
);
2022 bitmap_free(tdata
.commits
);
2023 bitmap_free(tdata
.trees
);
2024 bitmap_free(tdata
.blobs
);
2025 bitmap_free(tdata
.tags
);
2026 free_bitmap_index(bitmap_git
);
2029 int test_bitmap_commits(struct repository
*r
)
2031 struct object_id oid
;
2032 MAYBE_UNUSED
void *value
;
2033 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
2036 die(_("failed to load bitmap indexes"));
2039 * As this function is only used to print bitmap selected
2040 * commits, we don't have to read the commit table.
2042 if (bitmap_git
->table_lookup
) {
2043 if (load_bitmap_entries_v1(bitmap_git
) < 0)
2044 die(_("failed to load bitmap indexes"));
2047 kh_foreach(bitmap_git
->bitmaps
, oid
, value
, {
2048 printf_ln("%s", oid_to_hex(&oid
));
2051 free_bitmap_index(bitmap_git
);
2056 int test_bitmap_hashes(struct repository
*r
)
2058 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
2059 struct object_id oid
;
2060 uint32_t i
, index_pos
;
2062 if (!bitmap_git
|| !bitmap_git
->hashes
)
2065 for (i
= 0; i
< bitmap_num_objects(bitmap_git
); i
++) {
2066 if (bitmap_is_midx(bitmap_git
))
2067 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
2069 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
2071 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
2073 printf_ln("%s %"PRIu32
"",
2074 oid_to_hex(&oid
), get_be32(bitmap_git
->hashes
+ index_pos
));
2078 free_bitmap_index(bitmap_git
);
2083 int rebuild_bitmap(const uint32_t *reposition
,
2084 struct ewah_bitmap
*source
,
2085 struct bitmap
*dest
)
2088 struct ewah_iterator it
;
2091 ewah_iterator_init(&it
, source
);
2093 while (ewah_iterator_next(&word
, &it
)) {
2094 uint32_t offset
, bit_pos
;
2096 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
2097 if ((word
>> offset
) == 0)
2100 offset
+= ewah_bit_ctz64(word
>> offset
);
2102 bit_pos
= reposition
[pos
+ offset
];
2104 bitmap_set(dest
, bit_pos
- 1);
2105 else /* can't reuse, we don't have the object */
2109 pos
+= BITS_IN_EWORD
;
2114 uint32_t *create_bitmap_mapping(struct bitmap_index
*bitmap_git
,
2115 struct packing_data
*mapping
)
2117 uint32_t i
, num_objects
;
2118 uint32_t *reposition
;
2120 if (!bitmap_is_midx(bitmap_git
))
2121 load_reverse_index(bitmap_git
);
2122 else if (load_midx_revindex(bitmap_git
->midx
) < 0)
2123 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2126 num_objects
= bitmap_num_objects(bitmap_git
);
2127 CALLOC_ARRAY(reposition
, num_objects
);
2129 for (i
= 0; i
< num_objects
; ++i
) {
2130 struct object_id oid
;
2131 struct object_entry
*oe
;
2134 if (bitmap_is_midx(bitmap_git
))
2135 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
2137 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
2138 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
2139 oe
= packlist_find(mapping
, &oid
);
2142 reposition
[i
] = oe_in_pack_pos(mapping
, oe
) + 1;
2143 if (bitmap_git
->hashes
&& !oe
->hash
)
2144 oe
->hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
2151 void free_bitmap_index(struct bitmap_index
*b
)
2157 munmap(b
->map
, b
->map_size
);
2158 ewah_pool_free(b
->commits
);
2159 ewah_pool_free(b
->trees
);
2160 ewah_pool_free(b
->blobs
);
2161 ewah_pool_free(b
->tags
);
2163 struct stored_bitmap
*sb
;
2164 kh_foreach_value(b
->bitmaps
, sb
, {
2165 ewah_pool_free(sb
->root
);
2169 kh_destroy_oid_map(b
->bitmaps
);
2170 free(b
->ext_index
.objects
);
2171 free(b
->ext_index
.hashes
);
2172 kh_destroy_oid_pos(b
->ext_index
.positions
);
2173 bitmap_free(b
->result
);
2174 bitmap_free(b
->haves
);
2175 if (bitmap_is_midx(b
)) {
2177 * Multi-pack bitmaps need to have resources associated with
2178 * their on-disk reverse indexes unmapped so that stale .rev and
2179 * .bitmap files can be removed.
2181 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2182 * written in the same 'git multi-pack-index write --bitmap'
2183 * process. Close resources so they can be removed safely on
2184 * platforms like Windows.
2186 close_midx_revindex(b
->midx
);
2191 int bitmap_has_oid_in_uninteresting(struct bitmap_index
*bitmap_git
,
2192 const struct object_id
*oid
)
2194 return bitmap_git
&&
2195 bitmap_walk_contains(bitmap_git
, bitmap_git
->haves
, oid
);
2198 static off_t
get_disk_usage_for_type(struct bitmap_index
*bitmap_git
,
2199 enum object_type object_type
)
2201 struct bitmap
*result
= bitmap_git
->result
;
2203 struct ewah_iterator it
;
2207 init_type_iterator(&it
, bitmap_git
, object_type
);
2208 for (i
= 0; i
< result
->word_alloc
&&
2209 ewah_iterator_next(&filter
, &it
); i
++) {
2210 eword_t word
= result
->words
[i
] & filter
;
2211 size_t base
= (i
* BITS_IN_EWORD
);
2217 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
2218 if ((word
>> offset
) == 0)
2221 offset
+= ewah_bit_ctz64(word
>> offset
);
2223 if (bitmap_is_midx(bitmap_git
)) {
2225 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, base
+ offset
);
2226 off_t offset
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
2228 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
2229 struct packed_git
*pack
= bitmap_git
->midx
->packs
[pack_id
];
2231 if (offset_to_pack_pos(pack
, offset
, &pack_pos
) < 0) {
2232 struct object_id oid
;
2233 nth_midxed_object_oid(&oid
, bitmap_git
->midx
, midx_pos
);
2235 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX
),
2241 total
+= pack_pos_to_offset(pack
, pack_pos
+ 1) - offset
;
2243 size_t pos
= base
+ offset
;
2244 total
+= pack_pos_to_offset(bitmap_git
->pack
, pos
+ 1) -
2245 pack_pos_to_offset(bitmap_git
->pack
, pos
);
2253 static off_t
get_disk_usage_for_extended(struct bitmap_index
*bitmap_git
)
2255 struct bitmap
*result
= bitmap_git
->result
;
2256 struct eindex
*eindex
= &bitmap_git
->ext_index
;
2258 struct object_info oi
= OBJECT_INFO_INIT
;
2262 oi
.disk_sizep
= &object_size
;
2264 for (i
= 0; i
< eindex
->count
; i
++) {
2265 struct object
*obj
= eindex
->objects
[i
];
2267 if (!bitmap_get(result
, bitmap_num_objects(bitmap_git
) + i
))
2270 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
2271 die(_("unable to get disk usage of '%s'"),
2272 oid_to_hex(&obj
->oid
));
2274 total
+= object_size
;
2279 off_t
get_disk_usage_from_bitmap(struct bitmap_index
*bitmap_git
,
2280 struct rev_info
*revs
)
2284 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_COMMIT
);
2285 if (revs
->tree_objects
)
2286 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TREE
);
2287 if (revs
->blob_objects
)
2288 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_BLOB
);
2289 if (revs
->tag_objects
)
2290 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TAG
);
2292 total
+= get_disk_usage_for_extended(bitmap_git
);
2297 int bitmap_is_midx(struct bitmap_index
*bitmap_git
)
2299 return !!bitmap_git
->midx
;
2302 const struct string_list
*bitmap_preferred_tips(struct repository
*r
)
2304 return repo_config_get_value_multi(r
, "pack.preferbitmaptips");
2307 int bitmap_is_preferred_refname(struct repository
*r
, const char *refname
)
2309 const struct string_list
*preferred_tips
= bitmap_preferred_tips(r
);
2310 struct string_list_item
*item
;
2312 if (!preferred_tips
)
2315 for_each_string_list_item(item
, preferred_tips
) {
2316 if (starts_with(refname
, item
->string
))