1 #include "git-compat-util.h"
11 #include "list-objects.h"
13 #include "pack-bitmap.h"
14 #include "pack-revindex.h"
15 #include "pack-objects.h"
17 #include "repository.h"
19 #include "object-file.h"
20 #include "object-store.h"
21 #include "list-objects-filter-options.h"
26 * An entry on the bitmap index, representing the bitmap for a given
29 struct stored_bitmap
{
31 struct ewah_bitmap
*root
;
32 struct stored_bitmap
*xor;
37 * The active bitmap index for a repository. By design, repositories only have
38 * a single bitmap index available (the index for the biggest packfile in
39 * the repository), since bitmap indexes need full closure.
41 * If there is more than one bitmap index available (e.g. because of alternates),
42 * the active bitmap index is the largest one.
46 * The pack or multi-pack index (MIDX) that this bitmap index belongs
49 * Exactly one of these must be non-NULL; this specifies the object
50 * order used to interpret this bitmap.
52 struct packed_git
*pack
;
53 struct multi_pack_index
*midx
;
56 * Mark the first `reuse_objects` in the packfile as reused:
57 * they will be sent as-is without using them for repacking
60 uint32_t reuse_objects
;
62 /* mmapped buffer of the whole bitmap index */
64 size_t map_size
; /* size of the mmaped buffer */
65 size_t map_pos
; /* current position when loading the index */
70 * Each bitmap marks which objects in the packfile are of the given
71 * type. This provides type information when yielding the objects from
72 * the packfile during a walk, which allows for better delta bases.
74 struct ewah_bitmap
*commits
;
75 struct ewah_bitmap
*trees
;
76 struct ewah_bitmap
*blobs
;
77 struct ewah_bitmap
*tags
;
79 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
80 kh_oid_map_t
*bitmaps
;
82 /* Number of bitmapped commits */
85 /* If not NULL, this is a name-hash cache pointing into map. */
88 /* The checksum of the packfile or MIDX; points into map. */
89 const unsigned char *checksum
;
92 * If not NULL, this point into the commit table extension
93 * (within the memory mapped region `map`).
95 unsigned char *table_lookup
;
100 * When trying to perform bitmap operations with objects that are not
101 * packed in `pack`, these objects are added to this "fake index" and
102 * are assumed to appear at the end of the packfile for all operations
105 struct object
**objects
;
107 uint32_t count
, alloc
;
108 kh_oid_pos_t
*positions
;
111 /* Bitmap result of the last performed walk */
112 struct bitmap
*result
;
114 /* "have" bitmap from the last performed walk */
115 struct bitmap
*haves
;
117 /* Version of the bitmap index */
118 unsigned int version
;
121 static struct ewah_bitmap
*lookup_stored_bitmap(struct stored_bitmap
*st
)
123 struct ewah_bitmap
*parent
;
124 struct ewah_bitmap
*composed
;
129 composed
= ewah_pool_new();
130 parent
= lookup_stored_bitmap(st
->xor);
131 ewah_xor(st
->root
, parent
, composed
);
133 ewah_pool_free(st
->root
);
141 * Read a bitmap from the current read position on the mmaped
142 * index, and increase the read position accordingly
144 static struct ewah_bitmap
*read_bitmap_1(struct bitmap_index
*index
)
146 struct ewah_bitmap
*b
= ewah_pool_new();
148 ssize_t bitmap_size
= ewah_read_mmap(b
,
149 index
->map
+ index
->map_pos
,
150 index
->map_size
- index
->map_pos
);
152 if (bitmap_size
< 0) {
153 error(_("failed to load bitmap index (corrupted?)"));
158 index
->map_pos
+= bitmap_size
;
162 static uint32_t bitmap_num_objects(struct bitmap_index
*index
)
165 return index
->midx
->num_objects
;
166 return index
->pack
->num_objects
;
169 static int load_bitmap_header(struct bitmap_index
*index
)
171 struct bitmap_disk_header
*header
= (void *)index
->map
;
172 size_t header_size
= sizeof(*header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
;
174 if (index
->map_size
< header_size
+ the_hash_algo
->rawsz
)
175 return error(_("corrupted bitmap index (too small)"));
177 if (memcmp(header
->magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
)) != 0)
178 return error(_("corrupted bitmap index file (wrong header)"));
180 index
->version
= ntohs(header
->version
);
181 if (index
->version
!= 1)
182 return error(_("unsupported version '%d' for bitmap index file"), index
->version
);
184 /* Parse known bitmap format options */
186 uint32_t flags
= ntohs(header
->options
);
187 size_t cache_size
= st_mult(bitmap_num_objects(index
), sizeof(uint32_t));
188 unsigned char *index_end
= index
->map
+ index
->map_size
- the_hash_algo
->rawsz
;
190 if ((flags
& BITMAP_OPT_FULL_DAG
) == 0)
191 BUG("unsupported options for bitmap index file "
192 "(Git requires BITMAP_OPT_FULL_DAG)");
194 if (flags
& BITMAP_OPT_HASH_CACHE
) {
195 if (cache_size
> index_end
- index
->map
- header_size
)
196 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
197 index
->hashes
= (void *)(index_end
- cache_size
);
198 index_end
-= cache_size
;
201 if (flags
& BITMAP_OPT_LOOKUP_TABLE
) {
202 size_t table_size
= st_mult(ntohl(header
->entry_count
),
203 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
);
204 if (table_size
> index_end
- index
->map
- header_size
)
205 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
206 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
207 index
->table_lookup
= (void *)(index_end
- table_size
);
208 index_end
-= table_size
;
212 index
->entry_count
= ntohl(header
->entry_count
);
213 index
->checksum
= header
->checksum
;
214 index
->map_pos
+= header_size
;
218 static struct stored_bitmap
*store_bitmap(struct bitmap_index
*index
,
219 struct ewah_bitmap
*root
,
220 const struct object_id
*oid
,
221 struct stored_bitmap
*xor_with
,
224 struct stored_bitmap
*stored
;
228 stored
= xmalloc(sizeof(struct stored_bitmap
));
230 stored
->xor = xor_with
;
231 stored
->flags
= flags
;
232 oidcpy(&stored
->oid
, oid
);
234 hash_pos
= kh_put_oid_map(index
->bitmaps
, stored
->oid
, &ret
);
237 * A 0 return code means the insertion succeeded with no changes,
238 * because the SHA1 already existed on the map. This is bad, there
239 * shouldn't be duplicated commits in the index.
242 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid
));
246 kh_value(index
->bitmaps
, hash_pos
) = stored
;
250 static inline uint32_t read_be32(const unsigned char *buffer
, size_t *pos
)
252 uint32_t result
= get_be32(buffer
+ *pos
);
253 (*pos
) += sizeof(result
);
257 static inline uint8_t read_u8(const unsigned char *buffer
, size_t *pos
)
259 return buffer
[(*pos
)++];
262 #define MAX_XOR_OFFSET 160
264 static int nth_bitmap_object_oid(struct bitmap_index
*index
,
265 struct object_id
*oid
,
269 return nth_midxed_object_oid(oid
, index
->midx
, n
) ? 0 : -1;
270 return nth_packed_object_id(oid
, index
->pack
, n
);
273 static int load_bitmap_entries_v1(struct bitmap_index
*index
)
276 struct stored_bitmap
*recent_bitmaps
[MAX_XOR_OFFSET
] = { NULL
};
278 for (i
= 0; i
< index
->entry_count
; ++i
) {
279 int xor_offset
, flags
;
280 struct ewah_bitmap
*bitmap
= NULL
;
281 struct stored_bitmap
*xor_bitmap
= NULL
;
282 uint32_t commit_idx_pos
;
283 struct object_id oid
;
285 if (index
->map_size
- index
->map_pos
< 6)
286 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i
);
288 commit_idx_pos
= read_be32(index
->map
, &index
->map_pos
);
289 xor_offset
= read_u8(index
->map
, &index
->map_pos
);
290 flags
= read_u8(index
->map
, &index
->map_pos
);
292 if (nth_bitmap_object_oid(index
, &oid
, commit_idx_pos
) < 0)
293 return error(_("corrupt ewah bitmap: commit index %u out of range"),
294 (unsigned)commit_idx_pos
);
296 bitmap
= read_bitmap_1(index
);
300 if (xor_offset
> MAX_XOR_OFFSET
|| xor_offset
> i
)
301 return error(_("corrupted bitmap pack index"));
303 if (xor_offset
> 0) {
304 xor_bitmap
= recent_bitmaps
[(i
- xor_offset
) % MAX_XOR_OFFSET
];
307 return error(_("invalid XOR offset in bitmap pack index"));
310 recent_bitmaps
[i
% MAX_XOR_OFFSET
] = store_bitmap(
311 index
, bitmap
, &oid
, xor_bitmap
, flags
);
317 char *midx_bitmap_filename(struct multi_pack_index
*midx
)
319 struct strbuf buf
= STRBUF_INIT
;
321 get_midx_filename(&buf
, midx
->object_dir
);
322 strbuf_addf(&buf
, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx
)));
324 return strbuf_detach(&buf
, NULL
);
327 char *pack_bitmap_filename(struct packed_git
*p
)
331 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
332 BUG("pack_name does not end in .pack");
333 return xstrfmt("%.*s.bitmap", (int)len
, p
->pack_name
);
336 static int open_midx_bitmap_1(struct bitmap_index
*bitmap_git
,
337 struct multi_pack_index
*midx
)
340 char *bitmap_name
= midx_bitmap_filename(midx
);
341 int fd
= git_open(bitmap_name
);
343 struct packed_git
*preferred
;
347 warning_errno("cannot open '%s'", bitmap_name
);
353 if (fstat(fd
, &st
)) {
354 error_errno(_("cannot fstat bitmap file"));
359 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
360 struct strbuf buf
= STRBUF_INIT
;
361 get_midx_filename(&buf
, midx
->object_dir
);
362 trace2_data_string("bitmap", the_repository
,
363 "ignoring extra midx bitmap file", buf
.buf
);
365 strbuf_release(&buf
);
369 bitmap_git
->midx
= midx
;
370 bitmap_git
->map_size
= xsize_t(st
.st_size
);
371 bitmap_git
->map_pos
= 0;
372 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
,
376 if (load_bitmap_header(bitmap_git
) < 0)
379 if (!hasheq(get_midx_checksum(bitmap_git
->midx
), bitmap_git
->checksum
)) {
380 error(_("checksum doesn't match in MIDX and bitmap"));
384 if (load_midx_revindex(bitmap_git
->midx
)) {
385 warning(_("multi-pack bitmap is missing required reverse index"));
389 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
390 if (prepare_midx_pack(the_repository
, bitmap_git
->midx
, i
)) {
391 warning(_("could not open pack %s"),
392 bitmap_git
->midx
->pack_names
[i
]);
397 preferred
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
398 if (!is_pack_valid(preferred
)) {
399 warning(_("preferred pack (%s) is invalid"),
400 preferred
->pack_name
);
407 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
408 bitmap_git
->map_size
= 0;
409 bitmap_git
->map_pos
= 0;
410 bitmap_git
->map
= NULL
;
411 bitmap_git
->midx
= NULL
;
415 static int open_pack_bitmap_1(struct bitmap_index
*bitmap_git
, struct packed_git
*packfile
)
421 bitmap_name
= pack_bitmap_filename(packfile
);
422 fd
= git_open(bitmap_name
);
426 warning_errno("cannot open '%s'", bitmap_name
);
432 if (fstat(fd
, &st
)) {
433 error_errno(_("cannot fstat bitmap file"));
438 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
439 trace2_data_string("bitmap", the_repository
,
440 "ignoring extra bitmap file", packfile
->pack_name
);
445 if (!is_pack_valid(packfile
)) {
450 bitmap_git
->pack
= packfile
;
451 bitmap_git
->map_size
= xsize_t(st
.st_size
);
452 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
453 bitmap_git
->map_pos
= 0;
456 if (load_bitmap_header(bitmap_git
) < 0) {
457 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
458 bitmap_git
->map
= NULL
;
459 bitmap_git
->map_size
= 0;
460 bitmap_git
->map_pos
= 0;
461 bitmap_git
->pack
= NULL
;
465 trace2_data_string("bitmap", the_repository
, "opened bitmap file",
466 packfile
->pack_name
);
470 static int load_reverse_index(struct repository
*r
, struct bitmap_index
*bitmap_git
)
472 if (bitmap_is_midx(bitmap_git
)) {
477 * The multi-pack-index's .rev file is already loaded via
478 * open_pack_bitmap_1().
480 * But we still need to open the individual pack .rev files,
481 * since we will need to make use of them in pack-objects.
483 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
484 ret
= load_pack_revindex(r
, bitmap_git
->midx
->packs
[i
]);
490 return load_pack_revindex(r
, bitmap_git
->pack
);
493 static int load_bitmap(struct repository
*r
, struct bitmap_index
*bitmap_git
)
495 assert(bitmap_git
->map
);
497 bitmap_git
->bitmaps
= kh_init_oid_map();
498 bitmap_git
->ext_index
.positions
= kh_init_oid_pos();
500 if (load_reverse_index(r
, bitmap_git
))
503 if (!(bitmap_git
->commits
= read_bitmap_1(bitmap_git
)) ||
504 !(bitmap_git
->trees
= read_bitmap_1(bitmap_git
)) ||
505 !(bitmap_git
->blobs
= read_bitmap_1(bitmap_git
)) ||
506 !(bitmap_git
->tags
= read_bitmap_1(bitmap_git
)))
509 if (!bitmap_git
->table_lookup
&& load_bitmap_entries_v1(bitmap_git
) < 0)
515 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
516 bitmap_git
->map
= NULL
;
517 bitmap_git
->map_size
= 0;
519 kh_destroy_oid_map(bitmap_git
->bitmaps
);
520 bitmap_git
->bitmaps
= NULL
;
522 kh_destroy_oid_pos(bitmap_git
->ext_index
.positions
);
523 bitmap_git
->ext_index
.positions
= NULL
;
528 static int open_pack_bitmap(struct repository
*r
,
529 struct bitmap_index
*bitmap_git
)
531 struct packed_git
*p
;
534 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
535 if (open_pack_bitmap_1(bitmap_git
, p
) == 0) {
538 * The only reason to keep looking is to report
541 if (!trace2_is_enabled())
549 static int open_midx_bitmap(struct repository
*r
,
550 struct bitmap_index
*bitmap_git
)
553 struct multi_pack_index
*midx
;
555 assert(!bitmap_git
->map
);
557 for (midx
= get_multi_pack_index(r
); midx
; midx
= midx
->next
) {
558 if (!open_midx_bitmap_1(bitmap_git
, midx
))
564 static int open_bitmap(struct repository
*r
,
565 struct bitmap_index
*bitmap_git
)
569 assert(!bitmap_git
->map
);
571 found
= !open_midx_bitmap(r
, bitmap_git
);
574 * these will all be skipped if we opened a midx bitmap; but run it
575 * anyway if tracing is enabled to report the duplicates
577 if (!found
|| trace2_is_enabled())
578 found
|= !open_pack_bitmap(r
, bitmap_git
);
580 return found
? 0 : -1;
583 struct bitmap_index
*prepare_bitmap_git(struct repository
*r
)
585 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
587 if (!open_bitmap(r
, bitmap_git
) && !load_bitmap(r
, bitmap_git
))
590 free_bitmap_index(bitmap_git
);
594 struct bitmap_index
*prepare_midx_bitmap_git(struct multi_pack_index
*midx
)
596 struct repository
*r
= the_repository
;
597 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
599 if (!open_midx_bitmap_1(bitmap_git
, midx
) && !load_bitmap(r
, bitmap_git
))
602 free_bitmap_index(bitmap_git
);
606 struct include_data
{
607 struct bitmap_index
*bitmap_git
;
612 struct bitmap_lookup_table_triplet
{
618 struct bitmap_lookup_table_xor_item
{
619 struct object_id oid
;
624 * Given a `triplet` struct pointer and pointer `p`, this
625 * function reads the triplet beginning at `p` into the struct.
626 * Note that this function assumes that there is enough memory
627 * left for filling the `triplet` struct from `p`.
629 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet
*triplet
,
630 const unsigned char *p
)
635 triplet
->commit_pos
= get_be32(p
);
636 p
+= sizeof(uint32_t);
637 triplet
->offset
= get_be64(p
);
638 p
+= sizeof(uint64_t);
639 triplet
->xor_row
= get_be32(p
);
644 * This function gets the raw triplet from `row`'th row in the
645 * lookup table and fills that data to the `triplet`.
647 static int bitmap_lookup_table_get_triplet(struct bitmap_index
*bitmap_git
,
649 struct bitmap_lookup_table_triplet
*triplet
)
651 unsigned char *p
= NULL
;
652 if (pos
>= bitmap_git
->entry_count
)
653 return error(_("corrupt bitmap lookup table: triplet position out of index"));
655 p
= bitmap_git
->table_lookup
+ st_mult(pos
, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
);
657 return bitmap_lookup_table_get_triplet_by_pointer(triplet
, p
);
661 * Searches for a matching triplet. `commit_pos` is a pointer
662 * to the wanted commit position value. `table_entry` points to
663 * a triplet in lookup table. The first 4 bytes of each
664 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
666 static int triplet_cmp(const void *commit_pos
, const void *table_entry
)
669 uint32_t a
= *(uint32_t *)commit_pos
;
670 uint32_t b
= get_be32(table_entry
);
679 static uint32_t bitmap_bsearch_pos(struct bitmap_index
*bitmap_git
,
680 struct object_id
*oid
,
685 if (bitmap_is_midx(bitmap_git
))
686 found
= bsearch_midx(oid
, bitmap_git
->midx
, result
);
688 found
= bsearch_pack(oid
, bitmap_git
->pack
, result
);
694 * `bsearch_triplet_by_pos` function searches for the raw triplet
695 * having commit position same as `commit_pos` and fills `triplet`
696 * object from the raw triplet. Returns 1 on success and 0 on
699 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos
,
700 struct bitmap_index
*bitmap_git
,
701 struct bitmap_lookup_table_triplet
*triplet
)
703 unsigned char *p
= bsearch(&commit_pos
, bitmap_git
->table_lookup
, bitmap_git
->entry_count
,
704 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
, triplet_cmp
);
709 return bitmap_lookup_table_get_triplet_by_pointer(triplet
, p
);
712 static struct stored_bitmap
*lazy_bitmap_for_commit(struct bitmap_index
*bitmap_git
,
713 struct commit
*commit
)
715 uint32_t commit_pos
, xor_row
;
718 struct bitmap_lookup_table_triplet triplet
;
719 struct object_id
*oid
= &commit
->object
.oid
;
720 struct ewah_bitmap
*bitmap
;
721 struct stored_bitmap
*xor_bitmap
= NULL
;
722 const int bitmap_header_size
= 6;
723 static struct bitmap_lookup_table_xor_item
*xor_items
= NULL
;
724 static size_t xor_items_nr
= 0, xor_items_alloc
= 0;
725 static int is_corrupt
= 0;
728 struct bitmap_lookup_table_xor_item
*xor_item
;
733 if (!bitmap_bsearch_pos(bitmap_git
, oid
, &commit_pos
))
736 if (bitmap_bsearch_triplet_by_pos(commit_pos
, bitmap_git
, &triplet
) < 0)
740 offset
= triplet
.offset
;
741 xor_row
= triplet
.xor_row
;
743 while (xor_row
!= 0xffffffff) {
744 ALLOC_GROW(xor_items
, xor_items_nr
+ 1, xor_items_alloc
);
746 if (xor_items_nr
+ 1 >= bitmap_git
->entry_count
) {
747 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
751 if (bitmap_lookup_table_get_triplet(bitmap_git
, xor_row
, &triplet
) < 0)
754 xor_item
= &xor_items
[xor_items_nr
];
755 xor_item
->offset
= triplet
.offset
;
757 if (nth_bitmap_object_oid(bitmap_git
, &xor_item
->oid
, triplet
.commit_pos
) < 0) {
758 error(_("corrupt bitmap lookup table: commit index %u out of range"),
763 hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
, xor_item
->oid
);
766 * If desired bitmap is already stored, we don't need
767 * to iterate further. Because we know that bitmaps
768 * that are needed to be parsed to parse this bitmap
769 * has already been stored. So, assign this stored bitmap
772 if (hash_pos
< kh_end(bitmap_git
->bitmaps
) &&
773 (xor_bitmap
= kh_value(bitmap_git
->bitmaps
, hash_pos
)))
776 xor_row
= triplet
.xor_row
;
779 while (xor_items_nr
) {
780 xor_item
= &xor_items
[xor_items_nr
- 1];
781 bitmap_git
->map_pos
= xor_item
->offset
;
782 if (bitmap_git
->map_size
- bitmap_git
->map_pos
< bitmap_header_size
) {
783 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
784 oid_to_hex(&xor_item
->oid
));
788 bitmap_git
->map_pos
+= sizeof(uint32_t) + sizeof(uint8_t);
789 xor_flags
= read_u8(bitmap_git
->map
, &bitmap_git
->map_pos
);
790 bitmap
= read_bitmap_1(bitmap_git
);
795 xor_bitmap
= store_bitmap(bitmap_git
, bitmap
, &xor_item
->oid
, xor_bitmap
, xor_flags
);
799 bitmap_git
->map_pos
= offset
;
800 if (bitmap_git
->map_size
- bitmap_git
->map_pos
< bitmap_header_size
) {
801 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
807 * Don't bother reading the commit's index position or its xor
810 * - The commit's index position is irrelevant to us, since
811 * load_bitmap_entries_v1 only uses it to learn the object
812 * id which is used to compute the hashmap's key. We already
813 * have an object id, so no need to look it up again.
815 * - The xor_offset is unusable for us, since it specifies how
816 * many entries previous to ours we should look at. This
817 * makes sense when reading the bitmaps sequentially (as in
818 * load_bitmap_entries_v1()), since we can keep track of
819 * each bitmap as we read them.
821 * But it can't work for us, since the bitmap's don't have a
822 * fixed size. So we learn the position of the xor'd bitmap
823 * from the commit table (and resolve it to a bitmap in the
824 * above if-statement).
826 * Instead, we can skip ahead and immediately read the flags and
829 bitmap_git
->map_pos
+= sizeof(uint32_t) + sizeof(uint8_t);
830 flags
= read_u8(bitmap_git
->map
, &bitmap_git
->map_pos
);
831 bitmap
= read_bitmap_1(bitmap_git
);
836 return store_bitmap(bitmap_git
, bitmap
, oid
, xor_bitmap
, flags
);
844 struct ewah_bitmap
*bitmap_for_commit(struct bitmap_index
*bitmap_git
,
845 struct commit
*commit
)
847 khiter_t hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
,
849 if (hash_pos
>= kh_end(bitmap_git
->bitmaps
)) {
850 struct stored_bitmap
*bitmap
= NULL
;
851 if (!bitmap_git
->table_lookup
)
854 /* this is a fairly hot codepath - no trace2_region please */
855 /* NEEDSWORK: cache misses aren't recorded */
856 bitmap
= lazy_bitmap_for_commit(bitmap_git
, commit
);
859 return lookup_stored_bitmap(bitmap
);
861 return lookup_stored_bitmap(kh_value(bitmap_git
->bitmaps
, hash_pos
));
864 static inline int bitmap_position_extended(struct bitmap_index
*bitmap_git
,
865 const struct object_id
*oid
)
867 kh_oid_pos_t
*positions
= bitmap_git
->ext_index
.positions
;
868 khiter_t pos
= kh_get_oid_pos(positions
, *oid
);
870 if (pos
< kh_end(positions
)) {
871 int bitmap_pos
= kh_value(positions
, pos
);
872 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
878 static inline int bitmap_position_packfile(struct bitmap_index
*bitmap_git
,
879 const struct object_id
*oid
)
882 off_t offset
= find_pack_entry_one(oid
->hash
, bitmap_git
->pack
);
886 if (offset_to_pack_pos(bitmap_git
->pack
, offset
, &pos
) < 0)
891 static int bitmap_position_midx(struct bitmap_index
*bitmap_git
,
892 const struct object_id
*oid
)
895 if (!bsearch_midx(oid
, bitmap_git
->midx
, &want
))
898 if (midx_to_pack_pos(bitmap_git
->midx
, want
, &got
) < 0)
903 static int bitmap_position(struct bitmap_index
*bitmap_git
,
904 const struct object_id
*oid
)
907 if (bitmap_is_midx(bitmap_git
))
908 pos
= bitmap_position_midx(bitmap_git
, oid
);
910 pos
= bitmap_position_packfile(bitmap_git
, oid
);
911 return (pos
>= 0) ? pos
: bitmap_position_extended(bitmap_git
, oid
);
914 static int ext_index_add_object(struct bitmap_index
*bitmap_git
,
915 struct object
*object
, const char *name
)
917 struct eindex
*eindex
= &bitmap_git
->ext_index
;
923 hash_pos
= kh_put_oid_pos(eindex
->positions
, object
->oid
, &hash_ret
);
925 if (eindex
->count
>= eindex
->alloc
) {
926 eindex
->alloc
= (eindex
->alloc
+ 16) * 3 / 2;
927 REALLOC_ARRAY(eindex
->objects
, eindex
->alloc
);
928 REALLOC_ARRAY(eindex
->hashes
, eindex
->alloc
);
931 bitmap_pos
= eindex
->count
;
932 eindex
->objects
[eindex
->count
] = object
;
933 eindex
->hashes
[eindex
->count
] = pack_name_hash(name
);
934 kh_value(eindex
->positions
, hash_pos
) = bitmap_pos
;
937 bitmap_pos
= kh_value(eindex
->positions
, hash_pos
);
940 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
943 struct bitmap_show_data
{
944 struct bitmap_index
*bitmap_git
;
948 static void show_object(struct object
*object
, const char *name
, void *data_
)
950 struct bitmap_show_data
*data
= data_
;
953 bitmap_pos
= bitmap_position(data
->bitmap_git
, &object
->oid
);
956 bitmap_pos
= ext_index_add_object(data
->bitmap_git
, object
,
959 bitmap_set(data
->base
, bitmap_pos
);
962 static void show_commit(struct commit
*commit UNUSED
,
967 static int add_to_include_set(struct bitmap_index
*bitmap_git
,
968 struct include_data
*data
,
969 struct commit
*commit
,
972 struct ewah_bitmap
*partial
;
974 if (data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
))
977 if (bitmap_get(data
->base
, bitmap_pos
))
980 partial
= bitmap_for_commit(bitmap_git
, commit
);
982 bitmap_or_ewah(data
->base
, partial
);
986 bitmap_set(data
->base
, bitmap_pos
);
990 static int should_include(struct commit
*commit
, void *_data
)
992 struct include_data
*data
= _data
;
995 bitmap_pos
= bitmap_position(data
->bitmap_git
, &commit
->object
.oid
);
997 bitmap_pos
= ext_index_add_object(data
->bitmap_git
,
998 (struct object
*)commit
,
1001 if (!add_to_include_set(data
->bitmap_git
, data
, commit
, bitmap_pos
)) {
1002 struct commit_list
*parent
= commit
->parents
;
1005 parent
->item
->object
.flags
|= SEEN
;
1006 parent
= parent
->next
;
1015 static int should_include_obj(struct object
*obj
, void *_data
)
1017 struct include_data
*data
= _data
;
1020 bitmap_pos
= bitmap_position(data
->bitmap_git
, &obj
->oid
);
1023 if ((data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
)) ||
1024 bitmap_get(data
->base
, bitmap_pos
)) {
1031 static int add_commit_to_bitmap(struct bitmap_index
*bitmap_git
,
1032 struct bitmap
**base
,
1033 struct commit
*commit
)
1035 struct ewah_bitmap
*or_with
= bitmap_for_commit(bitmap_git
, commit
);
1041 *base
= ewah_to_bitmap(or_with
);
1043 bitmap_or_ewah(*base
, or_with
);
1048 static struct bitmap
*fill_in_bitmap(struct bitmap_index
*bitmap_git
,
1049 struct rev_info
*revs
,
1050 struct bitmap
*base
,
1051 struct bitmap
*seen
)
1053 struct include_data incdata
;
1054 struct bitmap_show_data show_data
;
1057 base
= bitmap_new();
1059 incdata
.bitmap_git
= bitmap_git
;
1060 incdata
.base
= base
;
1061 incdata
.seen
= seen
;
1063 revs
->include_check
= should_include
;
1064 revs
->include_check_obj
= should_include_obj
;
1065 revs
->include_check_data
= &incdata
;
1067 if (prepare_revision_walk(revs
))
1068 die(_("revision walk setup failed"));
1070 show_data
.bitmap_git
= bitmap_git
;
1071 show_data
.base
= base
;
1073 traverse_commit_list(revs
, show_commit
, show_object
, &show_data
);
1075 revs
->include_check
= NULL
;
1076 revs
->include_check_obj
= NULL
;
1077 revs
->include_check_data
= NULL
;
1082 struct bitmap_boundary_cb
{
1083 struct bitmap_index
*bitmap_git
;
1084 struct bitmap
*base
;
1086 struct object_array boundary
;
1089 static void show_boundary_commit(struct commit
*commit
, void *_data
)
1091 struct bitmap_boundary_cb
*data
= _data
;
1093 if (commit
->object
.flags
& BOUNDARY
)
1094 add_object_array(&commit
->object
, "", &data
->boundary
);
1096 if (commit
->object
.flags
& UNINTERESTING
) {
1097 if (bitmap_walk_contains(data
->bitmap_git
, data
->base
,
1098 &commit
->object
.oid
))
1101 add_commit_to_bitmap(data
->bitmap_git
, &data
->base
, commit
);
1105 static void show_boundary_object(struct object
*object
,
1106 const char *name
, void *data
)
1108 BUG("should not be called");
1111 static struct bitmap
*find_boundary_objects(struct bitmap_index
*bitmap_git
,
1112 struct rev_info
*revs
,
1113 struct object_list
*roots
)
1115 struct bitmap_boundary_cb cb
;
1116 struct object_list
*root
;
1118 unsigned int tmp_blobs
, tmp_trees
, tmp_tags
;
1119 int any_missing
= 0;
1121 cb
.bitmap_git
= bitmap_git
;
1122 cb
.base
= bitmap_new();
1123 object_array_init(&cb
.boundary
);
1125 revs
->ignore_missing_links
= 1;
1128 * OR in any existing reachability bitmaps among `roots` into
1131 for (root
= roots
; root
; root
= root
->next
) {
1132 struct object
*object
= root
->item
;
1133 if (object
->type
!= OBJ_COMMIT
||
1134 bitmap_walk_contains(bitmap_git
, cb
.base
, &object
->oid
))
1137 if (add_commit_to_bitmap(bitmap_git
, &cb
.base
,
1138 (struct commit
*)object
))
1147 tmp_blobs
= revs
->blob_objects
;
1148 tmp_trees
= revs
->tree_objects
;
1149 tmp_tags
= revs
->blob_objects
;
1150 revs
->blob_objects
= 0;
1151 revs
->tree_objects
= 0;
1152 revs
->tag_objects
= 0;
1155 * We didn't have complete coverage of the roots. First setup a
1156 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1157 * between the tips and boundary, and (b) record the boundary.
1159 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository
);
1160 if (prepare_revision_walk(revs
))
1161 die("revision walk setup failed");
1162 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository
);
1164 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository
);
1166 traverse_commit_list_filtered(revs
,
1167 show_boundary_commit
,
1168 show_boundary_object
,
1171 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository
);
1173 revs
->blob_objects
= tmp_blobs
;
1174 revs
->tree_objects
= tmp_trees
;
1175 revs
->tag_objects
= tmp_tags
;
1177 reset_revision_walk();
1178 clear_object_flags(UNINTERESTING
);
1181 * Then add the boundary commit(s) as fill-in traversal tips.
1183 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository
);
1184 for (i
= 0; i
< cb
.boundary
.nr
; i
++) {
1185 struct object
*obj
= cb
.boundary
.objects
[i
].item
;
1186 if (bitmap_walk_contains(bitmap_git
, cb
.base
, &obj
->oid
))
1189 add_pending_object(revs
, obj
, "");
1191 if (revs
->pending
.nr
)
1192 cb
.base
= fill_in_bitmap(bitmap_git
, revs
, cb
.base
, NULL
);
1193 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository
);
1196 object_array_clear(&cb
.boundary
);
1197 revs
->ignore_missing_links
= 0;
1202 static struct bitmap
*find_objects(struct bitmap_index
*bitmap_git
,
1203 struct rev_info
*revs
,
1204 struct object_list
*roots
,
1205 struct bitmap
*seen
)
1207 struct bitmap
*base
= NULL
;
1210 struct object_list
*not_mapped
= NULL
;
1213 * Go through all the roots for the walk. The ones that have bitmaps
1214 * on the bitmap index will be `or`ed together to form an initial
1215 * global reachability analysis.
1217 * The ones without bitmaps in the index will be stored in the
1218 * `not_mapped_list` for further processing.
1221 struct object
*object
= roots
->item
;
1222 roots
= roots
->next
;
1224 if (object
->type
== OBJ_COMMIT
&&
1225 add_commit_to_bitmap(bitmap_git
, &base
, (struct commit
*)object
)) {
1226 object
->flags
|= SEEN
;
1230 object_list_insert(object
, ¬_mapped
);
1234 * Best case scenario: We found bitmaps for all the roots,
1235 * so the resulting `or` bitmap has the full reachability analysis
1243 * Let's iterate through all the roots that don't have bitmaps to
1244 * check if we can determine them to be reachable from the existing
1247 * If we cannot find them in the existing global bitmap, we'll need
1248 * to push them to an actual walk and run it until we can confirm
1249 * they are reachable
1252 struct object
*object
= roots
->item
;
1255 roots
= roots
->next
;
1256 pos
= bitmap_position(bitmap_git
, &object
->oid
);
1258 if (pos
< 0 || base
== NULL
|| !bitmap_get(base
, pos
)) {
1259 object
->flags
&= ~UNINTERESTING
;
1260 add_pending_object(revs
, object
, "");
1263 object
->flags
|= SEEN
;
1269 * This fill-in traversal may walk over some objects
1270 * again, since we have already traversed in order to
1271 * find the boundary.
1273 * But this extra walk should be extremely cheap, since
1274 * all commit objects are loaded into memory, and
1275 * because we skip walking to parents that are
1276 * UNINTERESTING, since it will be marked in the haves
1277 * bitmap already (or it has an on-disk bitmap, since
1278 * OR-ing it in covers all of its ancestors).
1280 base
= fill_in_bitmap(bitmap_git
, revs
, base
, seen
);
1286 static void show_extended_objects(struct bitmap_index
*bitmap_git
,
1287 struct rev_info
*revs
,
1288 show_reachable_fn show_reach
)
1290 struct bitmap
*objects
= bitmap_git
->result
;
1291 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1294 for (i
= 0; i
< eindex
->count
; ++i
) {
1297 if (!bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1300 obj
= eindex
->objects
[i
];
1301 if ((obj
->type
== OBJ_BLOB
&& !revs
->blob_objects
) ||
1302 (obj
->type
== OBJ_TREE
&& !revs
->tree_objects
) ||
1303 (obj
->type
== OBJ_TAG
&& !revs
->tag_objects
))
1306 show_reach(&obj
->oid
, obj
->type
, 0, eindex
->hashes
[i
], NULL
, 0);
1310 static void init_type_iterator(struct ewah_iterator
*it
,
1311 struct bitmap_index
*bitmap_git
,
1312 enum object_type type
)
1316 ewah_iterator_init(it
, bitmap_git
->commits
);
1320 ewah_iterator_init(it
, bitmap_git
->trees
);
1324 ewah_iterator_init(it
, bitmap_git
->blobs
);
1328 ewah_iterator_init(it
, bitmap_git
->tags
);
1332 BUG("object type %d not stored by bitmap type index", type
);
1337 static void show_objects_for_type(
1338 struct bitmap_index
*bitmap_git
,
1339 enum object_type object_type
,
1340 show_reachable_fn show_reach
)
1345 struct ewah_iterator it
;
1348 struct bitmap
*objects
= bitmap_git
->result
;
1350 init_type_iterator(&it
, bitmap_git
, object_type
);
1352 for (i
= 0; i
< objects
->word_alloc
&&
1353 ewah_iterator_next(&filter
, &it
); i
++) {
1354 eword_t word
= objects
->words
[i
] & filter
;
1355 size_t pos
= (i
* BITS_IN_EWORD
);
1360 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1361 struct packed_git
*pack
;
1362 struct object_id oid
;
1363 uint32_t hash
= 0, index_pos
;
1366 if ((word
>> offset
) == 0)
1369 offset
+= ewah_bit_ctz64(word
>> offset
);
1371 if (bitmap_is_midx(bitmap_git
)) {
1372 struct multi_pack_index
*m
= bitmap_git
->midx
;
1375 index_pos
= pack_pos_to_midx(m
, pos
+ offset
);
1376 ofs
= nth_midxed_offset(m
, index_pos
);
1377 nth_midxed_object_oid(&oid
, m
, index_pos
);
1379 pack_id
= nth_midxed_pack_int_id(m
, index_pos
);
1380 pack
= bitmap_git
->midx
->packs
[pack_id
];
1382 index_pos
= pack_pos_to_index(bitmap_git
->pack
, pos
+ offset
);
1383 ofs
= pack_pos_to_offset(bitmap_git
->pack
, pos
+ offset
);
1384 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
1386 pack
= bitmap_git
->pack
;
1389 if (bitmap_git
->hashes
)
1390 hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
1392 show_reach(&oid
, object_type
, 0, hash
, pack
, ofs
);
1397 static int in_bitmapped_pack(struct bitmap_index
*bitmap_git
,
1398 struct object_list
*roots
)
1401 struct object
*object
= roots
->item
;
1402 roots
= roots
->next
;
1404 if (bitmap_is_midx(bitmap_git
)) {
1405 if (bsearch_midx(&object
->oid
, bitmap_git
->midx
, NULL
))
1408 if (find_pack_entry_one(object
->oid
.hash
, bitmap_git
->pack
) > 0)
1416 static struct bitmap
*find_tip_objects(struct bitmap_index
*bitmap_git
,
1417 struct object_list
*tip_objects
,
1418 enum object_type type
)
1420 struct bitmap
*result
= bitmap_new();
1421 struct object_list
*p
;
1423 for (p
= tip_objects
; p
; p
= p
->next
) {
1426 if (p
->item
->type
!= type
)
1429 pos
= bitmap_position(bitmap_git
, &p
->item
->oid
);
1433 bitmap_set(result
, pos
);
1439 static void filter_bitmap_exclude_type(struct bitmap_index
*bitmap_git
,
1440 struct object_list
*tip_objects
,
1441 struct bitmap
*to_filter
,
1442 enum object_type type
)
1444 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1445 struct bitmap
*tips
;
1446 struct ewah_iterator it
;
1451 * The non-bitmap version of this filter never removes
1452 * objects which the other side specifically asked for,
1453 * so we must match that behavior.
1455 tips
= find_tip_objects(bitmap_git
, tip_objects
, type
);
1458 * We can use the type-level bitmap for 'type' to work in whole
1459 * words for the objects that are actually in the bitmapped
1462 for (i
= 0, init_type_iterator(&it
, bitmap_git
, type
);
1463 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1465 if (i
< tips
->word_alloc
)
1466 mask
&= ~tips
->words
[i
];
1467 to_filter
->words
[i
] &= ~mask
;
1471 * Clear any objects that weren't in the packfile (and so would
1472 * not have been caught by the loop above. We'll have to check
1473 * them individually.
1475 for (i
= 0; i
< eindex
->count
; i
++) {
1476 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1477 if (eindex
->objects
[i
]->type
== type
&&
1478 bitmap_get(to_filter
, pos
) &&
1479 !bitmap_get(tips
, pos
))
1480 bitmap_unset(to_filter
, pos
);
1486 static void filter_bitmap_blob_none(struct bitmap_index
*bitmap_git
,
1487 struct object_list
*tip_objects
,
1488 struct bitmap
*to_filter
)
1490 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1494 static unsigned long get_size_by_pos(struct bitmap_index
*bitmap_git
,
1498 struct object_info oi
= OBJECT_INFO_INIT
;
1502 if (pos
< bitmap_num_objects(bitmap_git
)) {
1503 struct packed_git
*pack
;
1506 if (bitmap_is_midx(bitmap_git
)) {
1507 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, pos
);
1508 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
1510 pack
= bitmap_git
->midx
->packs
[pack_id
];
1511 ofs
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
1513 pack
= bitmap_git
->pack
;
1514 ofs
= pack_pos_to_offset(pack
, pos
);
1517 if (packed_object_info(the_repository
, pack
, ofs
, &oi
) < 0) {
1518 struct object_id oid
;
1519 nth_bitmap_object_oid(bitmap_git
, &oid
,
1520 pack_pos_to_index(pack
, pos
));
1521 die(_("unable to get size of %s"), oid_to_hex(&oid
));
1524 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1525 struct object
*obj
= eindex
->objects
[pos
- bitmap_num_objects(bitmap_git
)];
1526 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
1527 die(_("unable to get size of %s"), oid_to_hex(&obj
->oid
));
1533 static void filter_bitmap_blob_limit(struct bitmap_index
*bitmap_git
,
1534 struct object_list
*tip_objects
,
1535 struct bitmap
*to_filter
,
1536 unsigned long limit
)
1538 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1539 struct bitmap
*tips
;
1540 struct ewah_iterator it
;
1544 tips
= find_tip_objects(bitmap_git
, tip_objects
, OBJ_BLOB
);
1546 for (i
= 0, init_type_iterator(&it
, bitmap_git
, OBJ_BLOB
);
1547 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1549 eword_t word
= to_filter
->words
[i
] & mask
;
1552 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1555 if ((word
>> offset
) == 0)
1557 offset
+= ewah_bit_ctz64(word
>> offset
);
1558 pos
= i
* BITS_IN_EWORD
+ offset
;
1560 if (!bitmap_get(tips
, pos
) &&
1561 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1562 bitmap_unset(to_filter
, pos
);
1566 for (i
= 0; i
< eindex
->count
; i
++) {
1567 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1568 if (eindex
->objects
[i
]->type
== OBJ_BLOB
&&
1569 bitmap_get(to_filter
, pos
) &&
1570 !bitmap_get(tips
, pos
) &&
1571 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1572 bitmap_unset(to_filter
, pos
);
1578 static void filter_bitmap_tree_depth(struct bitmap_index
*bitmap_git
,
1579 struct object_list
*tip_objects
,
1580 struct bitmap
*to_filter
,
1581 unsigned long limit
)
1584 BUG("filter_bitmap_tree_depth given non-zero limit");
1586 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1588 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1592 static void filter_bitmap_object_type(struct bitmap_index
*bitmap_git
,
1593 struct object_list
*tip_objects
,
1594 struct bitmap
*to_filter
,
1595 enum object_type object_type
)
1597 if (object_type
< OBJ_COMMIT
|| object_type
> OBJ_TAG
)
1598 BUG("filter_bitmap_object_type given invalid object");
1600 if (object_type
!= OBJ_TAG
)
1601 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TAG
);
1602 if (object_type
!= OBJ_COMMIT
)
1603 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_COMMIT
);
1604 if (object_type
!= OBJ_TREE
)
1605 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TREE
);
1606 if (object_type
!= OBJ_BLOB
)
1607 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_BLOB
);
1610 static int filter_bitmap(struct bitmap_index
*bitmap_git
,
1611 struct object_list
*tip_objects
,
1612 struct bitmap
*to_filter
,
1613 struct list_objects_filter_options
*filter
)
1615 if (!filter
|| filter
->choice
== LOFC_DISABLED
)
1618 if (filter
->choice
== LOFC_BLOB_NONE
) {
1620 filter_bitmap_blob_none(bitmap_git
, tip_objects
,
1625 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
1627 filter_bitmap_blob_limit(bitmap_git
, tip_objects
,
1629 filter
->blob_limit_value
);
1633 if (filter
->choice
== LOFC_TREE_DEPTH
&&
1634 filter
->tree_exclude_depth
== 0) {
1636 filter_bitmap_tree_depth(bitmap_git
, tip_objects
,
1638 filter
->tree_exclude_depth
);
1642 if (filter
->choice
== LOFC_OBJECT_TYPE
) {
1644 filter_bitmap_object_type(bitmap_git
, tip_objects
,
1646 filter
->object_type
);
1650 if (filter
->choice
== LOFC_COMBINE
) {
1652 for (i
= 0; i
< filter
->sub_nr
; i
++) {
1653 if (filter_bitmap(bitmap_git
, tip_objects
, to_filter
,
1654 &filter
->sub
[i
]) < 0)
1660 /* filter choice not handled */
1664 static int can_filter_bitmap(struct list_objects_filter_options
*filter
)
1666 return !filter_bitmap(NULL
, NULL
, NULL
, filter
);
1669 struct bitmap_index
*prepare_bitmap_walk(struct rev_info
*revs
,
1670 int filter_provided_objects
)
1673 int use_boundary_traversal
;
1675 struct object_list
*wants
= NULL
;
1676 struct object_list
*haves
= NULL
;
1678 struct bitmap
*wants_bitmap
= NULL
;
1679 struct bitmap
*haves_bitmap
= NULL
;
1681 struct bitmap_index
*bitmap_git
;
1684 * We can't do pathspec limiting with bitmaps, because we don't know
1685 * which commits are associated with which object changes (let alone
1686 * even which objects are associated with which paths).
1691 if (!can_filter_bitmap(&revs
->filter
))
1694 /* try to open a bitmapped pack, but don't parse it yet
1695 * because we may not need to use it */
1696 CALLOC_ARRAY(bitmap_git
, 1);
1697 if (open_bitmap(revs
->repo
, bitmap_git
) < 0)
1700 for (i
= 0; i
< revs
->pending
.nr
; ++i
) {
1701 struct object
*object
= revs
->pending
.objects
[i
].item
;
1703 if (object
->type
== OBJ_NONE
)
1704 parse_object_or_die(&object
->oid
, NULL
);
1706 while (object
->type
== OBJ_TAG
) {
1707 struct tag
*tag
= (struct tag
*) object
;
1709 if (object
->flags
& UNINTERESTING
)
1710 object_list_insert(object
, &haves
);
1712 object_list_insert(object
, &wants
);
1714 object
= parse_object_or_die(get_tagged_oid(tag
), NULL
);
1715 object
->flags
|= (tag
->object
.flags
& UNINTERESTING
);
1718 if (object
->flags
& UNINTERESTING
)
1719 object_list_insert(object
, &haves
);
1721 object_list_insert(object
, &wants
);
1724 use_boundary_traversal
= git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL
, -1);
1725 if (use_boundary_traversal
< 0) {
1726 prepare_repo_settings(revs
->repo
);
1727 use_boundary_traversal
= revs
->repo
->settings
.pack_use_bitmap_boundary_traversal
;
1730 if (!use_boundary_traversal
) {
1732 * if we have a HAVES list, but none of those haves is contained
1733 * in the packfile that has a bitmap, we don't have anything to
1736 if (haves
&& !in_bitmapped_pack(bitmap_git
, haves
))
1740 /* if we don't want anything, we're done here */
1745 * now we're going to use bitmaps, so load the actual bitmap entries
1746 * from disk. this is the point of no return; after this the rev_list
1747 * becomes invalidated and we must perform the revwalk through bitmaps
1749 if (load_bitmap(revs
->repo
, bitmap_git
) < 0)
1752 if (!use_boundary_traversal
)
1753 object_array_clear(&revs
->pending
);
1756 if (use_boundary_traversal
) {
1757 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository
);
1758 haves_bitmap
= find_boundary_objects(bitmap_git
, revs
, haves
);
1759 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository
);
1761 trace2_region_enter("pack-bitmap", "haves/classic", the_repository
);
1762 revs
->ignore_missing_links
= 1;
1763 haves_bitmap
= find_objects(bitmap_git
, revs
, haves
, NULL
);
1764 reset_revision_walk();
1765 revs
->ignore_missing_links
= 0;
1766 trace2_region_leave("pack-bitmap", "haves/classic", the_repository
);
1770 BUG("failed to perform bitmap walk");
1773 if (use_boundary_traversal
) {
1774 object_array_clear(&revs
->pending
);
1775 reset_revision_walk();
1778 wants_bitmap
= find_objects(bitmap_git
, revs
, wants
, haves_bitmap
);
1781 BUG("failed to perform bitmap walk");
1784 bitmap_and_not(wants_bitmap
, haves_bitmap
);
1786 filter_bitmap(bitmap_git
,
1787 (revs
->filter
.choice
&& filter_provided_objects
) ? NULL
: wants
,
1791 bitmap_git
->result
= wants_bitmap
;
1792 bitmap_git
->haves
= haves_bitmap
;
1794 object_list_free(&wants
);
1795 object_list_free(&haves
);
1800 free_bitmap_index(bitmap_git
);
1801 object_list_free(&wants
);
1802 object_list_free(&haves
);
1807 * -1 means "stop trying further objects"; 0 means we may or may not have
1808 * reused, but you can keep feeding bits.
1810 static int try_partial_reuse(struct packed_git
*pack
,
1812 struct bitmap
*reuse
,
1813 struct pack_window
**w_curs
)
1815 off_t offset
, delta_obj_offset
;
1816 enum object_type type
;
1820 * try_partial_reuse() is called either on (a) objects in the
1821 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1822 * objects in the preferred pack of a multi-pack bitmap.
1823 * Importantly, the latter can pretend as if only a single pack
1826 * - The first pack->num_objects bits of a MIDX bitmap are
1827 * reserved for the preferred pack, and
1829 * - Ties due to duplicate objects are always resolved in
1830 * favor of the preferred pack.
1832 * Therefore we do not need to ever ask the MIDX for its copy of
1833 * an object by OID, since it will always select it from the
1834 * preferred pack. Likewise, the selected copy of the base
1835 * object for any deltas will reside in the same pack.
1837 * This means that we can reuse pos when looking up the bit in
1838 * the reuse bitmap, too, since bits corresponding to the
1839 * preferred pack precede all bits from other packs.
1842 if (pos
>= pack
->num_objects
)
1843 return -1; /* not actually in the pack or MIDX preferred pack */
1845 offset
= delta_obj_offset
= pack_pos_to_offset(pack
, pos
);
1846 type
= unpack_object_header(pack
, w_curs
, &offset
, &size
);
1848 return -1; /* broken packfile, punt */
1850 if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
) {
1855 * Find the position of the base object so we can look it up
1856 * in our bitmaps. If we can't come up with an offset, or if
1857 * that offset is not in the revidx, the pack is corrupt.
1858 * There's nothing we can do, so just punt on this object,
1859 * and the normal slow path will complain about it in
1862 base_offset
= get_delta_base(pack
, w_curs
, &offset
, type
,
1866 if (offset_to_pack_pos(pack
, base_offset
, &base_pos
) < 0)
1870 * We assume delta dependencies always point backwards. This
1871 * lets us do a single pass, and is basically always true
1872 * due to the way OFS_DELTAs work. You would not typically
1873 * find REF_DELTA in a bitmapped pack, since we only bitmap
1874 * packs we write fresh, and OFS_DELTA is the default). But
1875 * let's double check to make sure the pack wasn't written with
1878 if (base_pos
>= pos
)
1882 * And finally, if we're not sending the base as part of our
1883 * reuse chunk, then don't send this object either. The base
1884 * would come after us, along with other objects not
1885 * necessarily in the pack, which means we'd need to convert
1886 * to REF_DELTA on the fly. Better to just let the normal
1887 * object_entry code path handle it.
1889 if (!bitmap_get(reuse
, base_pos
))
1894 * If we got here, then the object is OK to reuse. Mark it.
1896 bitmap_set(reuse
, pos
);
1900 uint32_t midx_preferred_pack(struct bitmap_index
*bitmap_git
)
1902 struct multi_pack_index
*m
= bitmap_git
->midx
;
1904 BUG("midx_preferred_pack: requires non-empty MIDX");
1905 return nth_midxed_pack_int_id(m
, pack_pos_to_midx(bitmap_git
->midx
, 0));
1908 int reuse_partial_packfile_from_bitmap(struct bitmap_index
*bitmap_git
,
1909 struct packed_git
**packfile_out
,
1911 struct bitmap
**reuse_out
)
1913 struct repository
*r
= the_repository
;
1914 struct packed_git
*pack
;
1915 struct bitmap
*result
= bitmap_git
->result
;
1916 struct bitmap
*reuse
;
1917 struct pack_window
*w_curs
= NULL
;
1920 uint32_t objects_nr
;
1924 load_reverse_index(r
, bitmap_git
);
1926 if (bitmap_is_midx(bitmap_git
))
1927 pack
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
1929 pack
= bitmap_git
->pack
;
1930 objects_nr
= pack
->num_objects
;
1932 while (i
< result
->word_alloc
&& result
->words
[i
] == (eword_t
)~0)
1936 * Don't mark objects not in the packfile or preferred pack. This bitmap
1937 * marks objects eligible for reuse, but the pack-reuse code only
1938 * understands how to reuse a single pack. Since the preferred pack is
1939 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1940 * we use it instead of another pack. In single-pack bitmaps, the choice
1943 if (i
> objects_nr
/ BITS_IN_EWORD
)
1944 i
= objects_nr
/ BITS_IN_EWORD
;
1946 reuse
= bitmap_word_alloc(i
);
1947 memset(reuse
->words
, 0xFF, i
* sizeof(eword_t
));
1949 for (; i
< result
->word_alloc
; ++i
) {
1950 eword_t word
= result
->words
[i
];
1951 size_t pos
= (i
* BITS_IN_EWORD
);
1953 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1954 if ((word
>> offset
) == 0)
1957 offset
+= ewah_bit_ctz64(word
>> offset
);
1958 if (try_partial_reuse(pack
, pos
+ offset
,
1959 reuse
, &w_curs
) < 0) {
1961 * try_partial_reuse indicated we couldn't reuse
1962 * any bits, so there is no point in trying more
1963 * bits in the current word, or any other words
1966 * Jump out of both loops to avoid future
1967 * unnecessary calls to try_partial_reuse.
1975 unuse_pack(&w_curs
);
1977 *entries
= bitmap_popcount(reuse
);
1984 * Drop any reused objects from the result, since they will not
1985 * need to be handled separately.
1987 bitmap_and_not(result
, reuse
);
1988 *packfile_out
= pack
;
1993 int bitmap_walk_contains(struct bitmap_index
*bitmap_git
,
1994 struct bitmap
*bitmap
, const struct object_id
*oid
)
2001 idx
= bitmap_position(bitmap_git
, oid
);
2002 return idx
>= 0 && bitmap_get(bitmap
, idx
);
2005 void traverse_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
2006 struct rev_info
*revs
,
2007 show_reachable_fn show_reachable
)
2009 assert(bitmap_git
->result
);
2011 show_objects_for_type(bitmap_git
, OBJ_COMMIT
, show_reachable
);
2012 if (revs
->tree_objects
)
2013 show_objects_for_type(bitmap_git
, OBJ_TREE
, show_reachable
);
2014 if (revs
->blob_objects
)
2015 show_objects_for_type(bitmap_git
, OBJ_BLOB
, show_reachable
);
2016 if (revs
->tag_objects
)
2017 show_objects_for_type(bitmap_git
, OBJ_TAG
, show_reachable
);
2019 show_extended_objects(bitmap_git
, revs
, show_reachable
);
2022 static uint32_t count_object_type(struct bitmap_index
*bitmap_git
,
2023 enum object_type type
)
2025 struct bitmap
*objects
= bitmap_git
->result
;
2026 struct eindex
*eindex
= &bitmap_git
->ext_index
;
2028 uint32_t i
= 0, count
= 0;
2029 struct ewah_iterator it
;
2032 init_type_iterator(&it
, bitmap_git
, type
);
2034 while (i
< objects
->word_alloc
&& ewah_iterator_next(&filter
, &it
)) {
2035 eword_t word
= objects
->words
[i
++] & filter
;
2036 count
+= ewah_bit_popcount64(word
);
2039 for (i
= 0; i
< eindex
->count
; ++i
) {
2040 if (eindex
->objects
[i
]->type
== type
&&
2041 bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
2048 void count_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
2049 uint32_t *commits
, uint32_t *trees
,
2050 uint32_t *blobs
, uint32_t *tags
)
2052 assert(bitmap_git
->result
);
2055 *commits
= count_object_type(bitmap_git
, OBJ_COMMIT
);
2058 *trees
= count_object_type(bitmap_git
, OBJ_TREE
);
2061 *blobs
= count_object_type(bitmap_git
, OBJ_BLOB
);
2064 *tags
= count_object_type(bitmap_git
, OBJ_TAG
);
2067 struct bitmap_test_data
{
2068 struct bitmap_index
*bitmap_git
;
2069 struct bitmap
*base
;
2070 struct bitmap
*commits
;
2071 struct bitmap
*trees
;
2072 struct bitmap
*blobs
;
2073 struct bitmap
*tags
;
2074 struct progress
*prg
;
2078 static void test_bitmap_type(struct bitmap_test_data
*tdata
,
2079 struct object
*obj
, int pos
)
2081 enum object_type bitmap_type
= OBJ_NONE
;
2084 if (bitmap_get(tdata
->commits
, pos
)) {
2085 bitmap_type
= OBJ_COMMIT
;
2088 if (bitmap_get(tdata
->trees
, pos
)) {
2089 bitmap_type
= OBJ_TREE
;
2092 if (bitmap_get(tdata
->blobs
, pos
)) {
2093 bitmap_type
= OBJ_BLOB
;
2096 if (bitmap_get(tdata
->tags
, pos
)) {
2097 bitmap_type
= OBJ_TAG
;
2101 if (bitmap_type
== OBJ_NONE
)
2102 die(_("object '%s' not found in type bitmaps"),
2103 oid_to_hex(&obj
->oid
));
2106 die(_("object '%s' does not have a unique type"),
2107 oid_to_hex(&obj
->oid
));
2109 if (bitmap_type
!= obj
->type
)
2110 die(_("object '%s': real type '%s', expected: '%s'"),
2111 oid_to_hex(&obj
->oid
),
2112 type_name(obj
->type
),
2113 type_name(bitmap_type
));
2116 static void test_show_object(struct object
*object
,
2117 const char *name UNUSED
,
2120 struct bitmap_test_data
*tdata
= data
;
2123 bitmap_pos
= bitmap_position(tdata
->bitmap_git
, &object
->oid
);
2125 die(_("object not in bitmap: '%s'"), oid_to_hex(&object
->oid
));
2126 test_bitmap_type(tdata
, object
, bitmap_pos
);
2128 bitmap_set(tdata
->base
, bitmap_pos
);
2129 display_progress(tdata
->prg
, ++tdata
->seen
);
2132 static void test_show_commit(struct commit
*commit
, void *data
)
2134 struct bitmap_test_data
*tdata
= data
;
2137 bitmap_pos
= bitmap_position(tdata
->bitmap_git
,
2138 &commit
->object
.oid
);
2140 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit
->object
.oid
));
2141 test_bitmap_type(tdata
, &commit
->object
, bitmap_pos
);
2143 bitmap_set(tdata
->base
, bitmap_pos
);
2144 display_progress(tdata
->prg
, ++tdata
->seen
);
2147 void test_bitmap_walk(struct rev_info
*revs
)
2149 struct object
*root
;
2150 struct bitmap
*result
= NULL
;
2151 size_t result_popcnt
;
2152 struct bitmap_test_data tdata
;
2153 struct bitmap_index
*bitmap_git
;
2154 struct ewah_bitmap
*bm
;
2156 if (!(bitmap_git
= prepare_bitmap_git(revs
->repo
)))
2157 die(_("failed to load bitmap indexes"));
2159 if (revs
->pending
.nr
!= 1)
2160 die(_("you must specify exactly one commit to test"));
2162 fprintf_ln(stderr
, "Bitmap v%d test (%d entries%s)",
2163 bitmap_git
->version
,
2164 bitmap_git
->entry_count
,
2165 bitmap_git
->table_lookup
? "" : " loaded");
2167 root
= revs
->pending
.objects
[0].item
;
2168 bm
= bitmap_for_commit(bitmap_git
, (struct commit
*)root
);
2171 fprintf_ln(stderr
, "Found bitmap for '%s'. %d bits / %08x checksum",
2172 oid_to_hex(&root
->oid
), (int)bm
->bit_size
, ewah_checksum(bm
));
2174 result
= ewah_to_bitmap(bm
);
2178 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root
->oid
));
2180 revs
->tag_objects
= 1;
2181 revs
->tree_objects
= 1;
2182 revs
->blob_objects
= 1;
2184 result_popcnt
= bitmap_popcount(result
);
2186 if (prepare_revision_walk(revs
))
2187 die(_("revision walk setup failed"));
2189 tdata
.bitmap_git
= bitmap_git
;
2190 tdata
.base
= bitmap_new();
2191 tdata
.commits
= ewah_to_bitmap(bitmap_git
->commits
);
2192 tdata
.trees
= ewah_to_bitmap(bitmap_git
->trees
);
2193 tdata
.blobs
= ewah_to_bitmap(bitmap_git
->blobs
);
2194 tdata
.tags
= ewah_to_bitmap(bitmap_git
->tags
);
2195 tdata
.prg
= start_progress("Verifying bitmap entries", result_popcnt
);
2198 traverse_commit_list(revs
, &test_show_commit
, &test_show_object
, &tdata
);
2200 stop_progress(&tdata
.prg
);
2202 if (bitmap_equals(result
, tdata
.base
))
2203 fprintf_ln(stderr
, "OK!");
2205 die(_("mismatch in bitmap results"));
2207 bitmap_free(result
);
2208 bitmap_free(tdata
.base
);
2209 bitmap_free(tdata
.commits
);
2210 bitmap_free(tdata
.trees
);
2211 bitmap_free(tdata
.blobs
);
2212 bitmap_free(tdata
.tags
);
2213 free_bitmap_index(bitmap_git
);
2216 int test_bitmap_commits(struct repository
*r
)
2218 struct object_id oid
;
2219 MAYBE_UNUSED
void *value
;
2220 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
2223 die(_("failed to load bitmap indexes"));
2226 * As this function is only used to print bitmap selected
2227 * commits, we don't have to read the commit table.
2229 if (bitmap_git
->table_lookup
) {
2230 if (load_bitmap_entries_v1(bitmap_git
) < 0)
2231 die(_("failed to load bitmap indexes"));
2234 kh_foreach(bitmap_git
->bitmaps
, oid
, value
, {
2235 printf_ln("%s", oid_to_hex(&oid
));
2238 free_bitmap_index(bitmap_git
);
2243 int test_bitmap_hashes(struct repository
*r
)
2245 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
2246 struct object_id oid
;
2247 uint32_t i
, index_pos
;
2249 if (!bitmap_git
|| !bitmap_git
->hashes
)
2252 for (i
= 0; i
< bitmap_num_objects(bitmap_git
); i
++) {
2253 if (bitmap_is_midx(bitmap_git
))
2254 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
2256 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
2258 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
2260 printf_ln("%s %"PRIu32
"",
2261 oid_to_hex(&oid
), get_be32(bitmap_git
->hashes
+ index_pos
));
2265 free_bitmap_index(bitmap_git
);
2270 int rebuild_bitmap(const uint32_t *reposition
,
2271 struct ewah_bitmap
*source
,
2272 struct bitmap
*dest
)
2275 struct ewah_iterator it
;
2278 ewah_iterator_init(&it
, source
);
2280 while (ewah_iterator_next(&word
, &it
)) {
2281 uint32_t offset
, bit_pos
;
2283 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
2284 if ((word
>> offset
) == 0)
2287 offset
+= ewah_bit_ctz64(word
>> offset
);
2289 bit_pos
= reposition
[pos
+ offset
];
2291 bitmap_set(dest
, bit_pos
- 1);
2292 else /* can't reuse, we don't have the object */
2296 pos
+= BITS_IN_EWORD
;
2301 uint32_t *create_bitmap_mapping(struct bitmap_index
*bitmap_git
,
2302 struct packing_data
*mapping
)
2304 struct repository
*r
= the_repository
;
2305 uint32_t i
, num_objects
;
2306 uint32_t *reposition
;
2308 if (!bitmap_is_midx(bitmap_git
))
2309 load_reverse_index(r
, bitmap_git
);
2310 else if (load_midx_revindex(bitmap_git
->midx
))
2311 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2314 num_objects
= bitmap_num_objects(bitmap_git
);
2315 CALLOC_ARRAY(reposition
, num_objects
);
2317 for (i
= 0; i
< num_objects
; ++i
) {
2318 struct object_id oid
;
2319 struct object_entry
*oe
;
2322 if (bitmap_is_midx(bitmap_git
))
2323 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
2325 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
2326 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
2327 oe
= packlist_find(mapping
, &oid
);
2330 reposition
[i
] = oe_in_pack_pos(mapping
, oe
) + 1;
2331 if (bitmap_git
->hashes
&& !oe
->hash
)
2332 oe
->hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
2339 void free_bitmap_index(struct bitmap_index
*b
)
2345 munmap(b
->map
, b
->map_size
);
2346 ewah_pool_free(b
->commits
);
2347 ewah_pool_free(b
->trees
);
2348 ewah_pool_free(b
->blobs
);
2349 ewah_pool_free(b
->tags
);
2351 struct stored_bitmap
*sb
;
2352 kh_foreach_value(b
->bitmaps
, sb
, {
2353 ewah_pool_free(sb
->root
);
2357 kh_destroy_oid_map(b
->bitmaps
);
2358 free(b
->ext_index
.objects
);
2359 free(b
->ext_index
.hashes
);
2360 kh_destroy_oid_pos(b
->ext_index
.positions
);
2361 bitmap_free(b
->result
);
2362 bitmap_free(b
->haves
);
2363 if (bitmap_is_midx(b
)) {
2365 * Multi-pack bitmaps need to have resources associated with
2366 * their on-disk reverse indexes unmapped so that stale .rev and
2367 * .bitmap files can be removed.
2369 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2370 * written in the same 'git multi-pack-index write --bitmap'
2371 * process. Close resources so they can be removed safely on
2372 * platforms like Windows.
2374 close_midx_revindex(b
->midx
);
2379 int bitmap_has_oid_in_uninteresting(struct bitmap_index
*bitmap_git
,
2380 const struct object_id
*oid
)
2382 return bitmap_git
&&
2383 bitmap_walk_contains(bitmap_git
, bitmap_git
->haves
, oid
);
2386 static off_t
get_disk_usage_for_type(struct bitmap_index
*bitmap_git
,
2387 enum object_type object_type
)
2389 struct bitmap
*result
= bitmap_git
->result
;
2391 struct ewah_iterator it
;
2395 init_type_iterator(&it
, bitmap_git
, object_type
);
2396 for (i
= 0; i
< result
->word_alloc
&&
2397 ewah_iterator_next(&filter
, &it
); i
++) {
2398 eword_t word
= result
->words
[i
] & filter
;
2399 size_t base
= (i
* BITS_IN_EWORD
);
2405 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
2406 if ((word
>> offset
) == 0)
2409 offset
+= ewah_bit_ctz64(word
>> offset
);
2411 if (bitmap_is_midx(bitmap_git
)) {
2413 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, base
+ offset
);
2414 off_t offset
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
2416 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
2417 struct packed_git
*pack
= bitmap_git
->midx
->packs
[pack_id
];
2419 if (offset_to_pack_pos(pack
, offset
, &pack_pos
) < 0) {
2420 struct object_id oid
;
2421 nth_midxed_object_oid(&oid
, bitmap_git
->midx
, midx_pos
);
2423 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX
),
2429 total
+= pack_pos_to_offset(pack
, pack_pos
+ 1) - offset
;
2431 size_t pos
= base
+ offset
;
2432 total
+= pack_pos_to_offset(bitmap_git
->pack
, pos
+ 1) -
2433 pack_pos_to_offset(bitmap_git
->pack
, pos
);
2441 static off_t
get_disk_usage_for_extended(struct bitmap_index
*bitmap_git
)
2443 struct bitmap
*result
= bitmap_git
->result
;
2444 struct eindex
*eindex
= &bitmap_git
->ext_index
;
2446 struct object_info oi
= OBJECT_INFO_INIT
;
2450 oi
.disk_sizep
= &object_size
;
2452 for (i
= 0; i
< eindex
->count
; i
++) {
2453 struct object
*obj
= eindex
->objects
[i
];
2455 if (!bitmap_get(result
, bitmap_num_objects(bitmap_git
) + i
))
2458 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
2459 die(_("unable to get disk usage of '%s'"),
2460 oid_to_hex(&obj
->oid
));
2462 total
+= object_size
;
2467 off_t
get_disk_usage_from_bitmap(struct bitmap_index
*bitmap_git
,
2468 struct rev_info
*revs
)
2472 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_COMMIT
);
2473 if (revs
->tree_objects
)
2474 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TREE
);
2475 if (revs
->blob_objects
)
2476 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_BLOB
);
2477 if (revs
->tag_objects
)
2478 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TAG
);
2480 total
+= get_disk_usage_for_extended(bitmap_git
);
2485 int bitmap_is_midx(struct bitmap_index
*bitmap_git
)
2487 return !!bitmap_git
->midx
;
2490 const struct string_list
*bitmap_preferred_tips(struct repository
*r
)
2492 const struct string_list
*dest
;
2494 if (!repo_config_get_string_multi(r
, "pack.preferbitmaptips", &dest
))
2499 int bitmap_is_preferred_refname(struct repository
*r
, const char *refname
)
2501 const struct string_list
*preferred_tips
= bitmap_preferred_tips(r
);
2502 struct string_list_item
*item
;
2504 if (!preferred_tips
)
2507 for_each_string_list_item(item
, preferred_tips
) {
2508 if (starts_with(refname
, item
->string
))
2515 static int verify_bitmap_file(const char *name
)
2518 unsigned char *data
;
2519 int fd
= git_open(name
);
2522 /* It is OK to not have the file. */
2523 if (fd
< 0 || fstat(fd
, &st
)) {
2529 data
= xmmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
2531 if (!hashfile_checksum_valid(data
, st
.st_size
))
2532 res
= error(_("bitmap file '%s' has invalid checksum"),
2535 munmap(data
, st
.st_size
);
2539 int verify_bitmap_files(struct repository
*r
)
2543 for (struct multi_pack_index
*m
= get_multi_pack_index(r
);
2545 char *midx_bitmap_name
= midx_bitmap_filename(m
);
2546 res
|= verify_bitmap_file(midx_bitmap_name
);
2547 free(midx_bitmap_name
);
2550 for (struct packed_git
*p
= get_all_packs(r
);
2552 char *pack_bitmap_name
= pack_bitmap_filename(p
);
2553 res
|= verify_bitmap_file(pack_bitmap_name
);
2554 free(pack_bitmap_name
);