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 exceed 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 trace2_region_enter("pack-bitmap", "reading_lookup_table", the_repository
);
834 /* NEEDSWORK: cache misses aren't recorded */
835 bitmap
= lazy_bitmap_for_commit(bitmap_git
, commit
);
836 trace2_region_leave("pack-bitmap", "reading_lookup_table", the_repository
);
839 return lookup_stored_bitmap(bitmap
);
841 return lookup_stored_bitmap(kh_value(bitmap_git
->bitmaps
, hash_pos
));
844 static inline int bitmap_position_extended(struct bitmap_index
*bitmap_git
,
845 const struct object_id
*oid
)
847 kh_oid_pos_t
*positions
= bitmap_git
->ext_index
.positions
;
848 khiter_t pos
= kh_get_oid_pos(positions
, *oid
);
850 if (pos
< kh_end(positions
)) {
851 int bitmap_pos
= kh_value(positions
, pos
);
852 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
858 static inline int bitmap_position_packfile(struct bitmap_index
*bitmap_git
,
859 const struct object_id
*oid
)
862 off_t offset
= find_pack_entry_one(oid
->hash
, bitmap_git
->pack
);
866 if (offset_to_pack_pos(bitmap_git
->pack
, offset
, &pos
) < 0)
871 static int bitmap_position_midx(struct bitmap_index
*bitmap_git
,
872 const struct object_id
*oid
)
875 if (!bsearch_midx(oid
, bitmap_git
->midx
, &want
))
878 if (midx_to_pack_pos(bitmap_git
->midx
, want
, &got
) < 0)
883 static int bitmap_position(struct bitmap_index
*bitmap_git
,
884 const struct object_id
*oid
)
887 if (bitmap_is_midx(bitmap_git
))
888 pos
= bitmap_position_midx(bitmap_git
, oid
);
890 pos
= bitmap_position_packfile(bitmap_git
, oid
);
891 return (pos
>= 0) ? pos
: bitmap_position_extended(bitmap_git
, oid
);
894 static int ext_index_add_object(struct bitmap_index
*bitmap_git
,
895 struct object
*object
, const char *name
)
897 struct eindex
*eindex
= &bitmap_git
->ext_index
;
903 hash_pos
= kh_put_oid_pos(eindex
->positions
, object
->oid
, &hash_ret
);
905 if (eindex
->count
>= eindex
->alloc
) {
906 eindex
->alloc
= (eindex
->alloc
+ 16) * 3 / 2;
907 REALLOC_ARRAY(eindex
->objects
, eindex
->alloc
);
908 REALLOC_ARRAY(eindex
->hashes
, eindex
->alloc
);
911 bitmap_pos
= eindex
->count
;
912 eindex
->objects
[eindex
->count
] = object
;
913 eindex
->hashes
[eindex
->count
] = pack_name_hash(name
);
914 kh_value(eindex
->positions
, hash_pos
) = bitmap_pos
;
917 bitmap_pos
= kh_value(eindex
->positions
, hash_pos
);
920 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
923 struct bitmap_show_data
{
924 struct bitmap_index
*bitmap_git
;
928 static void show_object(struct object
*object
, const char *name
, void *data_
)
930 struct bitmap_show_data
*data
= data_
;
933 bitmap_pos
= bitmap_position(data
->bitmap_git
, &object
->oid
);
936 bitmap_pos
= ext_index_add_object(data
->bitmap_git
, object
,
939 bitmap_set(data
->base
, bitmap_pos
);
942 static void show_commit(struct commit
*commit
, void *data
)
946 static int add_to_include_set(struct bitmap_index
*bitmap_git
,
947 struct include_data
*data
,
948 struct commit
*commit
,
951 struct ewah_bitmap
*partial
;
953 if (data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
))
956 if (bitmap_get(data
->base
, bitmap_pos
))
959 partial
= bitmap_for_commit(bitmap_git
, commit
);
961 bitmap_or_ewah(data
->base
, partial
);
965 bitmap_set(data
->base
, bitmap_pos
);
969 static int should_include(struct commit
*commit
, void *_data
)
971 struct include_data
*data
= _data
;
974 bitmap_pos
= bitmap_position(data
->bitmap_git
, &commit
->object
.oid
);
976 bitmap_pos
= ext_index_add_object(data
->bitmap_git
,
977 (struct object
*)commit
,
980 if (!add_to_include_set(data
->bitmap_git
, data
, commit
, bitmap_pos
)) {
981 struct commit_list
*parent
= commit
->parents
;
984 parent
->item
->object
.flags
|= SEEN
;
985 parent
= parent
->next
;
994 static int should_include_obj(struct object
*obj
, void *_data
)
996 struct include_data
*data
= _data
;
999 bitmap_pos
= bitmap_position(data
->bitmap_git
, &obj
->oid
);
1002 if ((data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
)) ||
1003 bitmap_get(data
->base
, bitmap_pos
)) {
1010 static int add_commit_to_bitmap(struct bitmap_index
*bitmap_git
,
1011 struct bitmap
**base
,
1012 struct commit
*commit
)
1014 struct ewah_bitmap
*or_with
= bitmap_for_commit(bitmap_git
, commit
);
1020 *base
= ewah_to_bitmap(or_with
);
1022 bitmap_or_ewah(*base
, or_with
);
1027 static struct bitmap
*find_objects(struct bitmap_index
*bitmap_git
,
1028 struct rev_info
*revs
,
1029 struct object_list
*roots
,
1030 struct bitmap
*seen
)
1032 struct bitmap
*base
= NULL
;
1035 struct object_list
*not_mapped
= NULL
;
1038 * Go through all the roots for the walk. The ones that have bitmaps
1039 * on the bitmap index will be `or`ed together to form an initial
1040 * global reachability analysis.
1042 * The ones without bitmaps in the index will be stored in the
1043 * `not_mapped_list` for further processing.
1046 struct object
*object
= roots
->item
;
1047 roots
= roots
->next
;
1049 if (object
->type
== OBJ_COMMIT
&&
1050 add_commit_to_bitmap(bitmap_git
, &base
, (struct commit
*)object
)) {
1051 object
->flags
|= SEEN
;
1055 object_list_insert(object
, ¬_mapped
);
1059 * Best case scenario: We found bitmaps for all the roots,
1060 * so the resulting `or` bitmap has the full reachability analysis
1068 * Let's iterate through all the roots that don't have bitmaps to
1069 * check if we can determine them to be reachable from the existing
1072 * If we cannot find them in the existing global bitmap, we'll need
1073 * to push them to an actual walk and run it until we can confirm
1074 * they are reachable
1077 struct object
*object
= roots
->item
;
1080 roots
= roots
->next
;
1081 pos
= bitmap_position(bitmap_git
, &object
->oid
);
1083 if (pos
< 0 || base
== NULL
|| !bitmap_get(base
, pos
)) {
1084 object
->flags
&= ~UNINTERESTING
;
1085 add_pending_object(revs
, object
, "");
1088 object
->flags
|= SEEN
;
1093 struct include_data incdata
;
1094 struct bitmap_show_data show_data
;
1097 base
= bitmap_new();
1099 incdata
.bitmap_git
= bitmap_git
;
1100 incdata
.base
= base
;
1101 incdata
.seen
= seen
;
1103 revs
->include_check
= should_include
;
1104 revs
->include_check_obj
= should_include_obj
;
1105 revs
->include_check_data
= &incdata
;
1107 if (prepare_revision_walk(revs
))
1108 die(_("revision walk setup failed"));
1110 show_data
.bitmap_git
= bitmap_git
;
1111 show_data
.base
= base
;
1113 traverse_commit_list(revs
,
1114 show_commit
, show_object
,
1117 revs
->include_check
= NULL
;
1118 revs
->include_check_obj
= NULL
;
1119 revs
->include_check_data
= NULL
;
1125 static void show_extended_objects(struct bitmap_index
*bitmap_git
,
1126 struct rev_info
*revs
,
1127 show_reachable_fn show_reach
)
1129 struct bitmap
*objects
= bitmap_git
->result
;
1130 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1133 for (i
= 0; i
< eindex
->count
; ++i
) {
1136 if (!bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1139 obj
= eindex
->objects
[i
];
1140 if ((obj
->type
== OBJ_BLOB
&& !revs
->blob_objects
) ||
1141 (obj
->type
== OBJ_TREE
&& !revs
->tree_objects
) ||
1142 (obj
->type
== OBJ_TAG
&& !revs
->tag_objects
))
1145 show_reach(&obj
->oid
, obj
->type
, 0, eindex
->hashes
[i
], NULL
, 0);
1149 static void init_type_iterator(struct ewah_iterator
*it
,
1150 struct bitmap_index
*bitmap_git
,
1151 enum object_type type
)
1155 ewah_iterator_init(it
, bitmap_git
->commits
);
1159 ewah_iterator_init(it
, bitmap_git
->trees
);
1163 ewah_iterator_init(it
, bitmap_git
->blobs
);
1167 ewah_iterator_init(it
, bitmap_git
->tags
);
1171 BUG("object type %d not stored by bitmap type index", type
);
1176 static void show_objects_for_type(
1177 struct bitmap_index
*bitmap_git
,
1178 enum object_type object_type
,
1179 show_reachable_fn show_reach
)
1184 struct ewah_iterator it
;
1187 struct bitmap
*objects
= bitmap_git
->result
;
1189 init_type_iterator(&it
, bitmap_git
, object_type
);
1191 for (i
= 0; i
< objects
->word_alloc
&&
1192 ewah_iterator_next(&filter
, &it
); i
++) {
1193 eword_t word
= objects
->words
[i
] & filter
;
1194 size_t pos
= (i
* BITS_IN_EWORD
);
1199 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1200 struct packed_git
*pack
;
1201 struct object_id oid
;
1202 uint32_t hash
= 0, index_pos
;
1205 if ((word
>> offset
) == 0)
1208 offset
+= ewah_bit_ctz64(word
>> offset
);
1210 if (bitmap_is_midx(bitmap_git
)) {
1211 struct multi_pack_index
*m
= bitmap_git
->midx
;
1214 index_pos
= pack_pos_to_midx(m
, pos
+ offset
);
1215 ofs
= nth_midxed_offset(m
, index_pos
);
1216 nth_midxed_object_oid(&oid
, m
, index_pos
);
1218 pack_id
= nth_midxed_pack_int_id(m
, index_pos
);
1219 pack
= bitmap_git
->midx
->packs
[pack_id
];
1221 index_pos
= pack_pos_to_index(bitmap_git
->pack
, pos
+ offset
);
1222 ofs
= pack_pos_to_offset(bitmap_git
->pack
, pos
+ offset
);
1223 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
1225 pack
= bitmap_git
->pack
;
1228 if (bitmap_git
->hashes
)
1229 hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
1231 show_reach(&oid
, object_type
, 0, hash
, pack
, ofs
);
1236 static int in_bitmapped_pack(struct bitmap_index
*bitmap_git
,
1237 struct object_list
*roots
)
1240 struct object
*object
= roots
->item
;
1241 roots
= roots
->next
;
1243 if (bitmap_is_midx(bitmap_git
)) {
1244 if (bsearch_midx(&object
->oid
, bitmap_git
->midx
, NULL
))
1247 if (find_pack_entry_one(object
->oid
.hash
, bitmap_git
->pack
) > 0)
1255 static struct bitmap
*find_tip_objects(struct bitmap_index
*bitmap_git
,
1256 struct object_list
*tip_objects
,
1257 enum object_type type
)
1259 struct bitmap
*result
= bitmap_new();
1260 struct object_list
*p
;
1262 for (p
= tip_objects
; p
; p
= p
->next
) {
1265 if (p
->item
->type
!= type
)
1268 pos
= bitmap_position(bitmap_git
, &p
->item
->oid
);
1272 bitmap_set(result
, pos
);
1278 static void filter_bitmap_exclude_type(struct bitmap_index
*bitmap_git
,
1279 struct object_list
*tip_objects
,
1280 struct bitmap
*to_filter
,
1281 enum object_type type
)
1283 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1284 struct bitmap
*tips
;
1285 struct ewah_iterator it
;
1290 * The non-bitmap version of this filter never removes
1291 * objects which the other side specifically asked for,
1292 * so we must match that behavior.
1294 tips
= find_tip_objects(bitmap_git
, tip_objects
, type
);
1297 * We can use the type-level bitmap for 'type' to work in whole
1298 * words for the objects that are actually in the bitmapped
1301 for (i
= 0, init_type_iterator(&it
, bitmap_git
, type
);
1302 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1304 if (i
< tips
->word_alloc
)
1305 mask
&= ~tips
->words
[i
];
1306 to_filter
->words
[i
] &= ~mask
;
1310 * Clear any objects that weren't in the packfile (and so would
1311 * not have been caught by the loop above. We'll have to check
1312 * them individually.
1314 for (i
= 0; i
< eindex
->count
; i
++) {
1315 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1316 if (eindex
->objects
[i
]->type
== type
&&
1317 bitmap_get(to_filter
, pos
) &&
1318 !bitmap_get(tips
, pos
))
1319 bitmap_unset(to_filter
, pos
);
1325 static void filter_bitmap_blob_none(struct bitmap_index
*bitmap_git
,
1326 struct object_list
*tip_objects
,
1327 struct bitmap
*to_filter
)
1329 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1333 static unsigned long get_size_by_pos(struct bitmap_index
*bitmap_git
,
1337 struct object_info oi
= OBJECT_INFO_INIT
;
1341 if (pos
< bitmap_num_objects(bitmap_git
)) {
1342 struct packed_git
*pack
;
1345 if (bitmap_is_midx(bitmap_git
)) {
1346 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, pos
);
1347 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
1349 pack
= bitmap_git
->midx
->packs
[pack_id
];
1350 ofs
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
1352 pack
= bitmap_git
->pack
;
1353 ofs
= pack_pos_to_offset(pack
, pos
);
1356 if (packed_object_info(the_repository
, pack
, ofs
, &oi
) < 0) {
1357 struct object_id oid
;
1358 nth_bitmap_object_oid(bitmap_git
, &oid
,
1359 pack_pos_to_index(pack
, pos
));
1360 die(_("unable to get size of %s"), oid_to_hex(&oid
));
1363 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1364 struct object
*obj
= eindex
->objects
[pos
- bitmap_num_objects(bitmap_git
)];
1365 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
1366 die(_("unable to get size of %s"), oid_to_hex(&obj
->oid
));
1372 static void filter_bitmap_blob_limit(struct bitmap_index
*bitmap_git
,
1373 struct object_list
*tip_objects
,
1374 struct bitmap
*to_filter
,
1375 unsigned long limit
)
1377 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1378 struct bitmap
*tips
;
1379 struct ewah_iterator it
;
1383 tips
= find_tip_objects(bitmap_git
, tip_objects
, OBJ_BLOB
);
1385 for (i
= 0, init_type_iterator(&it
, bitmap_git
, OBJ_BLOB
);
1386 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1388 eword_t word
= to_filter
->words
[i
] & mask
;
1391 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1394 if ((word
>> offset
) == 0)
1396 offset
+= ewah_bit_ctz64(word
>> offset
);
1397 pos
= i
* BITS_IN_EWORD
+ offset
;
1399 if (!bitmap_get(tips
, pos
) &&
1400 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1401 bitmap_unset(to_filter
, pos
);
1405 for (i
= 0; i
< eindex
->count
; i
++) {
1406 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1407 if (eindex
->objects
[i
]->type
== OBJ_BLOB
&&
1408 bitmap_get(to_filter
, pos
) &&
1409 !bitmap_get(tips
, pos
) &&
1410 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1411 bitmap_unset(to_filter
, pos
);
1417 static void filter_bitmap_tree_depth(struct bitmap_index
*bitmap_git
,
1418 struct object_list
*tip_objects
,
1419 struct bitmap
*to_filter
,
1420 unsigned long limit
)
1423 BUG("filter_bitmap_tree_depth given non-zero limit");
1425 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1427 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1431 static void filter_bitmap_object_type(struct bitmap_index
*bitmap_git
,
1432 struct object_list
*tip_objects
,
1433 struct bitmap
*to_filter
,
1434 enum object_type object_type
)
1436 if (object_type
< OBJ_COMMIT
|| object_type
> OBJ_TAG
)
1437 BUG("filter_bitmap_object_type given invalid object");
1439 if (object_type
!= OBJ_TAG
)
1440 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TAG
);
1441 if (object_type
!= OBJ_COMMIT
)
1442 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_COMMIT
);
1443 if (object_type
!= OBJ_TREE
)
1444 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TREE
);
1445 if (object_type
!= OBJ_BLOB
)
1446 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_BLOB
);
1449 static int filter_bitmap(struct bitmap_index
*bitmap_git
,
1450 struct object_list
*tip_objects
,
1451 struct bitmap
*to_filter
,
1452 struct list_objects_filter_options
*filter
)
1454 if (!filter
|| filter
->choice
== LOFC_DISABLED
)
1457 if (filter
->choice
== LOFC_BLOB_NONE
) {
1459 filter_bitmap_blob_none(bitmap_git
, tip_objects
,
1464 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
1466 filter_bitmap_blob_limit(bitmap_git
, tip_objects
,
1468 filter
->blob_limit_value
);
1472 if (filter
->choice
== LOFC_TREE_DEPTH
&&
1473 filter
->tree_exclude_depth
== 0) {
1475 filter_bitmap_tree_depth(bitmap_git
, tip_objects
,
1477 filter
->tree_exclude_depth
);
1481 if (filter
->choice
== LOFC_OBJECT_TYPE
) {
1483 filter_bitmap_object_type(bitmap_git
, tip_objects
,
1485 filter
->object_type
);
1489 if (filter
->choice
== LOFC_COMBINE
) {
1491 for (i
= 0; i
< filter
->sub_nr
; i
++) {
1492 if (filter_bitmap(bitmap_git
, tip_objects
, to_filter
,
1493 &filter
->sub
[i
]) < 0)
1499 /* filter choice not handled */
1503 static int can_filter_bitmap(struct list_objects_filter_options
*filter
)
1505 return !filter_bitmap(NULL
, NULL
, NULL
, filter
);
1508 struct bitmap_index
*prepare_bitmap_walk(struct rev_info
*revs
,
1509 int filter_provided_objects
)
1513 struct object_list
*wants
= NULL
;
1514 struct object_list
*haves
= NULL
;
1516 struct bitmap
*wants_bitmap
= NULL
;
1517 struct bitmap
*haves_bitmap
= NULL
;
1519 struct bitmap_index
*bitmap_git
;
1522 * We can't do pathspec limiting with bitmaps, because we don't know
1523 * which commits are associated with which object changes (let alone
1524 * even which objects are associated with which paths).
1529 if (!can_filter_bitmap(&revs
->filter
))
1532 /* try to open a bitmapped pack, but don't parse it yet
1533 * because we may not need to use it */
1534 CALLOC_ARRAY(bitmap_git
, 1);
1535 if (open_bitmap(revs
->repo
, bitmap_git
) < 0)
1538 for (i
= 0; i
< revs
->pending
.nr
; ++i
) {
1539 struct object
*object
= revs
->pending
.objects
[i
].item
;
1541 if (object
->type
== OBJ_NONE
)
1542 parse_object_or_die(&object
->oid
, NULL
);
1544 while (object
->type
== OBJ_TAG
) {
1545 struct tag
*tag
= (struct tag
*) object
;
1547 if (object
->flags
& UNINTERESTING
)
1548 object_list_insert(object
, &haves
);
1550 object_list_insert(object
, &wants
);
1552 object
= parse_object_or_die(get_tagged_oid(tag
), NULL
);
1553 object
->flags
|= (tag
->object
.flags
& UNINTERESTING
);
1556 if (object
->flags
& UNINTERESTING
)
1557 object_list_insert(object
, &haves
);
1559 object_list_insert(object
, &wants
);
1563 * if we have a HAVES list, but none of those haves is contained
1564 * in the packfile that has a bitmap, we don't have anything to
1567 if (haves
&& !in_bitmapped_pack(bitmap_git
, haves
))
1570 /* if we don't want anything, we're done here */
1575 * now we're going to use bitmaps, so load the actual bitmap entries
1576 * from disk. this is the point of no return; after this the rev_list
1577 * becomes invalidated and we must perform the revwalk through bitmaps
1579 if (load_bitmap(bitmap_git
) < 0)
1582 object_array_clear(&revs
->pending
);
1585 revs
->ignore_missing_links
= 1;
1586 haves_bitmap
= find_objects(bitmap_git
, revs
, haves
, NULL
);
1587 reset_revision_walk();
1588 revs
->ignore_missing_links
= 0;
1591 BUG("failed to perform bitmap walk");
1594 wants_bitmap
= find_objects(bitmap_git
, revs
, wants
, haves_bitmap
);
1597 BUG("failed to perform bitmap walk");
1600 bitmap_and_not(wants_bitmap
, haves_bitmap
);
1602 filter_bitmap(bitmap_git
,
1603 (revs
->filter
.choice
&& filter_provided_objects
) ? NULL
: wants
,
1607 bitmap_git
->result
= wants_bitmap
;
1608 bitmap_git
->haves
= haves_bitmap
;
1610 object_list_free(&wants
);
1611 object_list_free(&haves
);
1616 free_bitmap_index(bitmap_git
);
1617 object_list_free(&wants
);
1618 object_list_free(&haves
);
1623 * -1 means "stop trying further objects"; 0 means we may or may not have
1624 * reused, but you can keep feeding bits.
1626 static int try_partial_reuse(struct packed_git
*pack
,
1628 struct bitmap
*reuse
,
1629 struct pack_window
**w_curs
)
1631 off_t offset
, delta_obj_offset
;
1632 enum object_type type
;
1636 * try_partial_reuse() is called either on (a) objects in the
1637 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1638 * objects in the preferred pack of a multi-pack bitmap.
1639 * Importantly, the latter can pretend as if only a single pack
1642 * - The first pack->num_objects bits of a MIDX bitmap are
1643 * reserved for the preferred pack, and
1645 * - Ties due to duplicate objects are always resolved in
1646 * favor of the preferred pack.
1648 * Therefore we do not need to ever ask the MIDX for its copy of
1649 * an object by OID, since it will always select it from the
1650 * preferred pack. Likewise, the selected copy of the base
1651 * object for any deltas will reside in the same pack.
1653 * This means that we can reuse pos when looking up the bit in
1654 * the reuse bitmap, too, since bits corresponding to the
1655 * preferred pack precede all bits from other packs.
1658 if (pos
>= pack
->num_objects
)
1659 return -1; /* not actually in the pack or MIDX preferred pack */
1661 offset
= delta_obj_offset
= pack_pos_to_offset(pack
, pos
);
1662 type
= unpack_object_header(pack
, w_curs
, &offset
, &size
);
1664 return -1; /* broken packfile, punt */
1666 if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
) {
1671 * Find the position of the base object so we can look it up
1672 * in our bitmaps. If we can't come up with an offset, or if
1673 * that offset is not in the revidx, the pack is corrupt.
1674 * There's nothing we can do, so just punt on this object,
1675 * and the normal slow path will complain about it in
1678 base_offset
= get_delta_base(pack
, w_curs
, &offset
, type
,
1682 if (offset_to_pack_pos(pack
, base_offset
, &base_pos
) < 0)
1686 * We assume delta dependencies always point backwards. This
1687 * lets us do a single pass, and is basically always true
1688 * due to the way OFS_DELTAs work. You would not typically
1689 * find REF_DELTA in a bitmapped pack, since we only bitmap
1690 * packs we write fresh, and OFS_DELTA is the default). But
1691 * let's double check to make sure the pack wasn't written with
1694 if (base_pos
>= pos
)
1698 * And finally, if we're not sending the base as part of our
1699 * reuse chunk, then don't send this object either. The base
1700 * would come after us, along with other objects not
1701 * necessarily in the pack, which means we'd need to convert
1702 * to REF_DELTA on the fly. Better to just let the normal
1703 * object_entry code path handle it.
1705 if (!bitmap_get(reuse
, base_pos
))
1710 * If we got here, then the object is OK to reuse. Mark it.
1712 bitmap_set(reuse
, pos
);
1716 uint32_t midx_preferred_pack(struct bitmap_index
*bitmap_git
)
1718 struct multi_pack_index
*m
= bitmap_git
->midx
;
1720 BUG("midx_preferred_pack: requires non-empty MIDX");
1721 return nth_midxed_pack_int_id(m
, pack_pos_to_midx(bitmap_git
->midx
, 0));
1724 int reuse_partial_packfile_from_bitmap(struct bitmap_index
*bitmap_git
,
1725 struct packed_git
**packfile_out
,
1727 struct bitmap
**reuse_out
)
1729 struct packed_git
*pack
;
1730 struct bitmap
*result
= bitmap_git
->result
;
1731 struct bitmap
*reuse
;
1732 struct pack_window
*w_curs
= NULL
;
1735 uint32_t objects_nr
;
1739 load_reverse_index(bitmap_git
);
1741 if (bitmap_is_midx(bitmap_git
))
1742 pack
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
1744 pack
= bitmap_git
->pack
;
1745 objects_nr
= pack
->num_objects
;
1747 while (i
< result
->word_alloc
&& result
->words
[i
] == (eword_t
)~0)
1751 * Don't mark objects not in the packfile or preferred pack. This bitmap
1752 * marks objects eligible for reuse, but the pack-reuse code only
1753 * understands how to reuse a single pack. Since the preferred pack is
1754 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1755 * we use it instead of another pack. In single-pack bitmaps, the choice
1758 if (i
> objects_nr
/ BITS_IN_EWORD
)
1759 i
= objects_nr
/ BITS_IN_EWORD
;
1761 reuse
= bitmap_word_alloc(i
);
1762 memset(reuse
->words
, 0xFF, i
* sizeof(eword_t
));
1764 for (; i
< result
->word_alloc
; ++i
) {
1765 eword_t word
= result
->words
[i
];
1766 size_t pos
= (i
* BITS_IN_EWORD
);
1768 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1769 if ((word
>> offset
) == 0)
1772 offset
+= ewah_bit_ctz64(word
>> offset
);
1773 if (try_partial_reuse(pack
, pos
+ offset
,
1774 reuse
, &w_curs
) < 0) {
1776 * try_partial_reuse indicated we couldn't reuse
1777 * any bits, so there is no point in trying more
1778 * bits in the current word, or any other words
1781 * Jump out of both loops to avoid future
1782 * unnecessary calls to try_partial_reuse.
1790 unuse_pack(&w_curs
);
1792 *entries
= bitmap_popcount(reuse
);
1799 * Drop any reused objects from the result, since they will not
1800 * need to be handled separately.
1802 bitmap_and_not(result
, reuse
);
1803 *packfile_out
= pack
;
1808 int bitmap_walk_contains(struct bitmap_index
*bitmap_git
,
1809 struct bitmap
*bitmap
, const struct object_id
*oid
)
1816 idx
= bitmap_position(bitmap_git
, oid
);
1817 return idx
>= 0 && bitmap_get(bitmap
, idx
);
1820 void traverse_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1821 struct rev_info
*revs
,
1822 show_reachable_fn show_reachable
)
1824 assert(bitmap_git
->result
);
1826 show_objects_for_type(bitmap_git
, OBJ_COMMIT
, show_reachable
);
1827 if (revs
->tree_objects
)
1828 show_objects_for_type(bitmap_git
, OBJ_TREE
, show_reachable
);
1829 if (revs
->blob_objects
)
1830 show_objects_for_type(bitmap_git
, OBJ_BLOB
, show_reachable
);
1831 if (revs
->tag_objects
)
1832 show_objects_for_type(bitmap_git
, OBJ_TAG
, show_reachable
);
1834 show_extended_objects(bitmap_git
, revs
, show_reachable
);
1837 static uint32_t count_object_type(struct bitmap_index
*bitmap_git
,
1838 enum object_type type
)
1840 struct bitmap
*objects
= bitmap_git
->result
;
1841 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1843 uint32_t i
= 0, count
= 0;
1844 struct ewah_iterator it
;
1847 init_type_iterator(&it
, bitmap_git
, type
);
1849 while (i
< objects
->word_alloc
&& ewah_iterator_next(&filter
, &it
)) {
1850 eword_t word
= objects
->words
[i
++] & filter
;
1851 count
+= ewah_bit_popcount64(word
);
1854 for (i
= 0; i
< eindex
->count
; ++i
) {
1855 if (eindex
->objects
[i
]->type
== type
&&
1856 bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1863 void count_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1864 uint32_t *commits
, uint32_t *trees
,
1865 uint32_t *blobs
, uint32_t *tags
)
1867 assert(bitmap_git
->result
);
1870 *commits
= count_object_type(bitmap_git
, OBJ_COMMIT
);
1873 *trees
= count_object_type(bitmap_git
, OBJ_TREE
);
1876 *blobs
= count_object_type(bitmap_git
, OBJ_BLOB
);
1879 *tags
= count_object_type(bitmap_git
, OBJ_TAG
);
1882 struct bitmap_test_data
{
1883 struct bitmap_index
*bitmap_git
;
1884 struct bitmap
*base
;
1885 struct bitmap
*commits
;
1886 struct bitmap
*trees
;
1887 struct bitmap
*blobs
;
1888 struct bitmap
*tags
;
1889 struct progress
*prg
;
1893 static void test_bitmap_type(struct bitmap_test_data
*tdata
,
1894 struct object
*obj
, int pos
)
1896 enum object_type bitmap_type
= OBJ_NONE
;
1899 if (bitmap_get(tdata
->commits
, pos
)) {
1900 bitmap_type
= OBJ_COMMIT
;
1903 if (bitmap_get(tdata
->trees
, pos
)) {
1904 bitmap_type
= OBJ_TREE
;
1907 if (bitmap_get(tdata
->blobs
, pos
)) {
1908 bitmap_type
= OBJ_BLOB
;
1911 if (bitmap_get(tdata
->tags
, pos
)) {
1912 bitmap_type
= OBJ_TAG
;
1916 if (bitmap_type
== OBJ_NONE
)
1917 die(_("object '%s' not found in type bitmaps"),
1918 oid_to_hex(&obj
->oid
));
1921 die(_("object '%s' does not have a unique type"),
1922 oid_to_hex(&obj
->oid
));
1924 if (bitmap_type
!= obj
->type
)
1925 die(_("object '%s': real type '%s', expected: '%s'"),
1926 oid_to_hex(&obj
->oid
),
1927 type_name(obj
->type
),
1928 type_name(bitmap_type
));
1931 static void test_show_object(struct object
*object
, const char *name
,
1934 struct bitmap_test_data
*tdata
= data
;
1937 bitmap_pos
= bitmap_position(tdata
->bitmap_git
, &object
->oid
);
1939 die(_("object not in bitmap: '%s'"), oid_to_hex(&object
->oid
));
1940 test_bitmap_type(tdata
, object
, bitmap_pos
);
1942 bitmap_set(tdata
->base
, bitmap_pos
);
1943 display_progress(tdata
->prg
, ++tdata
->seen
);
1946 static void test_show_commit(struct commit
*commit
, void *data
)
1948 struct bitmap_test_data
*tdata
= data
;
1951 bitmap_pos
= bitmap_position(tdata
->bitmap_git
,
1952 &commit
->object
.oid
);
1954 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit
->object
.oid
));
1955 test_bitmap_type(tdata
, &commit
->object
, bitmap_pos
);
1957 bitmap_set(tdata
->base
, bitmap_pos
);
1958 display_progress(tdata
->prg
, ++tdata
->seen
);
1961 void test_bitmap_walk(struct rev_info
*revs
)
1963 struct object
*root
;
1964 struct bitmap
*result
= NULL
;
1965 size_t result_popcnt
;
1966 struct bitmap_test_data tdata
;
1967 struct bitmap_index
*bitmap_git
;
1968 struct ewah_bitmap
*bm
;
1970 if (!(bitmap_git
= prepare_bitmap_git(revs
->repo
)))
1971 die(_("failed to load bitmap indexes"));
1973 if (revs
->pending
.nr
!= 1)
1974 die(_("you must specify exactly one commit to test"));
1976 fprintf_ln(stderr
, "Bitmap v%d test (%d entries%s)",
1977 bitmap_git
->version
,
1978 bitmap_git
->entry_count
,
1979 bitmap_git
->table_lookup
? "" : " loaded");
1981 root
= revs
->pending
.objects
[0].item
;
1982 bm
= bitmap_for_commit(bitmap_git
, (struct commit
*)root
);
1985 fprintf_ln(stderr
, "Found bitmap for '%s'. %d bits / %08x checksum",
1986 oid_to_hex(&root
->oid
), (int)bm
->bit_size
, ewah_checksum(bm
));
1988 result
= ewah_to_bitmap(bm
);
1992 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root
->oid
));
1994 revs
->tag_objects
= 1;
1995 revs
->tree_objects
= 1;
1996 revs
->blob_objects
= 1;
1998 result_popcnt
= bitmap_popcount(result
);
2000 if (prepare_revision_walk(revs
))
2001 die(_("revision walk setup failed"));
2003 tdata
.bitmap_git
= bitmap_git
;
2004 tdata
.base
= bitmap_new();
2005 tdata
.commits
= ewah_to_bitmap(bitmap_git
->commits
);
2006 tdata
.trees
= ewah_to_bitmap(bitmap_git
->trees
);
2007 tdata
.blobs
= ewah_to_bitmap(bitmap_git
->blobs
);
2008 tdata
.tags
= ewah_to_bitmap(bitmap_git
->tags
);
2009 tdata
.prg
= start_progress("Verifying bitmap entries", result_popcnt
);
2012 traverse_commit_list(revs
, &test_show_commit
, &test_show_object
, &tdata
);
2014 stop_progress(&tdata
.prg
);
2016 if (bitmap_equals(result
, tdata
.base
))
2017 fprintf_ln(stderr
, "OK!");
2019 die(_("mismatch in bitmap results"));
2021 bitmap_free(result
);
2022 bitmap_free(tdata
.base
);
2023 bitmap_free(tdata
.commits
);
2024 bitmap_free(tdata
.trees
);
2025 bitmap_free(tdata
.blobs
);
2026 bitmap_free(tdata
.tags
);
2027 free_bitmap_index(bitmap_git
);
2030 int test_bitmap_commits(struct repository
*r
)
2032 struct object_id oid
;
2033 MAYBE_UNUSED
void *value
;
2034 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
2037 die(_("failed to load bitmap indexes"));
2040 * As this function is only used to print bitmap selected
2041 * commits, we don't have to read the commit table.
2043 if (bitmap_git
->table_lookup
) {
2044 if (load_bitmap_entries_v1(bitmap_git
) < 0)
2045 die(_("failed to load bitmap indexes"));
2048 kh_foreach(bitmap_git
->bitmaps
, oid
, value
, {
2049 printf_ln("%s", oid_to_hex(&oid
));
2052 free_bitmap_index(bitmap_git
);
2057 int test_bitmap_hashes(struct repository
*r
)
2059 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
2060 struct object_id oid
;
2061 uint32_t i
, index_pos
;
2063 if (!bitmap_git
|| !bitmap_git
->hashes
)
2066 for (i
= 0; i
< bitmap_num_objects(bitmap_git
); i
++) {
2067 if (bitmap_is_midx(bitmap_git
))
2068 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
2070 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
2072 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
2074 printf_ln("%s %"PRIu32
"",
2075 oid_to_hex(&oid
), get_be32(bitmap_git
->hashes
+ index_pos
));
2079 free_bitmap_index(bitmap_git
);
2084 int rebuild_bitmap(const uint32_t *reposition
,
2085 struct ewah_bitmap
*source
,
2086 struct bitmap
*dest
)
2089 struct ewah_iterator it
;
2092 ewah_iterator_init(&it
, source
);
2094 while (ewah_iterator_next(&word
, &it
)) {
2095 uint32_t offset
, bit_pos
;
2097 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
2098 if ((word
>> offset
) == 0)
2101 offset
+= ewah_bit_ctz64(word
>> offset
);
2103 bit_pos
= reposition
[pos
+ offset
];
2105 bitmap_set(dest
, bit_pos
- 1);
2106 else /* can't reuse, we don't have the object */
2110 pos
+= BITS_IN_EWORD
;
2115 uint32_t *create_bitmap_mapping(struct bitmap_index
*bitmap_git
,
2116 struct packing_data
*mapping
)
2118 uint32_t i
, num_objects
;
2119 uint32_t *reposition
;
2121 if (!bitmap_is_midx(bitmap_git
))
2122 load_reverse_index(bitmap_git
);
2123 else if (load_midx_revindex(bitmap_git
->midx
) < 0)
2124 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2127 num_objects
= bitmap_num_objects(bitmap_git
);
2128 CALLOC_ARRAY(reposition
, num_objects
);
2130 for (i
= 0; i
< num_objects
; ++i
) {
2131 struct object_id oid
;
2132 struct object_entry
*oe
;
2135 if (bitmap_is_midx(bitmap_git
))
2136 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
2138 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
2139 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
2140 oe
= packlist_find(mapping
, &oid
);
2143 reposition
[i
] = oe_in_pack_pos(mapping
, oe
) + 1;
2144 if (bitmap_git
->hashes
&& !oe
->hash
)
2145 oe
->hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
2152 void free_bitmap_index(struct bitmap_index
*b
)
2158 munmap(b
->map
, b
->map_size
);
2159 ewah_pool_free(b
->commits
);
2160 ewah_pool_free(b
->trees
);
2161 ewah_pool_free(b
->blobs
);
2162 ewah_pool_free(b
->tags
);
2164 struct stored_bitmap
*sb
;
2165 kh_foreach_value(b
->bitmaps
, sb
, {
2166 ewah_pool_free(sb
->root
);
2170 kh_destroy_oid_map(b
->bitmaps
);
2171 free(b
->ext_index
.objects
);
2172 free(b
->ext_index
.hashes
);
2173 kh_destroy_oid_pos(b
->ext_index
.positions
);
2174 bitmap_free(b
->result
);
2175 bitmap_free(b
->haves
);
2176 if (bitmap_is_midx(b
)) {
2178 * Multi-pack bitmaps need to have resources associated with
2179 * their on-disk reverse indexes unmapped so that stale .rev and
2180 * .bitmap files can be removed.
2182 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2183 * written in the same 'git multi-pack-index write --bitmap'
2184 * process. Close resources so they can be removed safely on
2185 * platforms like Windows.
2187 close_midx_revindex(b
->midx
);
2192 int bitmap_has_oid_in_uninteresting(struct bitmap_index
*bitmap_git
,
2193 const struct object_id
*oid
)
2195 return bitmap_git
&&
2196 bitmap_walk_contains(bitmap_git
, bitmap_git
->haves
, oid
);
2199 static off_t
get_disk_usage_for_type(struct bitmap_index
*bitmap_git
,
2200 enum object_type object_type
)
2202 struct bitmap
*result
= bitmap_git
->result
;
2204 struct ewah_iterator it
;
2208 init_type_iterator(&it
, bitmap_git
, object_type
);
2209 for (i
= 0; i
< result
->word_alloc
&&
2210 ewah_iterator_next(&filter
, &it
); i
++) {
2211 eword_t word
= result
->words
[i
] & filter
;
2212 size_t base
= (i
* BITS_IN_EWORD
);
2218 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
2219 if ((word
>> offset
) == 0)
2222 offset
+= ewah_bit_ctz64(word
>> offset
);
2224 if (bitmap_is_midx(bitmap_git
)) {
2226 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, base
+ offset
);
2227 off_t offset
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
2229 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
2230 struct packed_git
*pack
= bitmap_git
->midx
->packs
[pack_id
];
2232 if (offset_to_pack_pos(pack
, offset
, &pack_pos
) < 0) {
2233 struct object_id oid
;
2234 nth_midxed_object_oid(&oid
, bitmap_git
->midx
, midx_pos
);
2236 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX
),
2242 total
+= pack_pos_to_offset(pack
, pack_pos
+ 1) - offset
;
2244 size_t pos
= base
+ offset
;
2245 total
+= pack_pos_to_offset(bitmap_git
->pack
, pos
+ 1) -
2246 pack_pos_to_offset(bitmap_git
->pack
, pos
);
2254 static off_t
get_disk_usage_for_extended(struct bitmap_index
*bitmap_git
)
2256 struct bitmap
*result
= bitmap_git
->result
;
2257 struct eindex
*eindex
= &bitmap_git
->ext_index
;
2259 struct object_info oi
= OBJECT_INFO_INIT
;
2263 oi
.disk_sizep
= &object_size
;
2265 for (i
= 0; i
< eindex
->count
; i
++) {
2266 struct object
*obj
= eindex
->objects
[i
];
2268 if (!bitmap_get(result
, bitmap_num_objects(bitmap_git
) + i
))
2271 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
2272 die(_("unable to get disk usage of '%s'"),
2273 oid_to_hex(&obj
->oid
));
2275 total
+= object_size
;
2280 off_t
get_disk_usage_from_bitmap(struct bitmap_index
*bitmap_git
,
2281 struct rev_info
*revs
)
2285 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_COMMIT
);
2286 if (revs
->tree_objects
)
2287 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TREE
);
2288 if (revs
->blob_objects
)
2289 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_BLOB
);
2290 if (revs
->tag_objects
)
2291 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TAG
);
2293 total
+= get_disk_usage_for_extended(bitmap_git
);
2298 int bitmap_is_midx(struct bitmap_index
*bitmap_git
)
2300 return !!bitmap_git
->midx
;
2303 const struct string_list
*bitmap_preferred_tips(struct repository
*r
)
2305 return repo_config_get_value_multi(r
, "pack.preferbitmaptips");
2308 int bitmap_is_preferred_refname(struct repository
*r
, const char *refname
)
2310 const struct string_list
*preferred_tips
= bitmap_preferred_tips(r
);
2311 struct string_list_item
*item
;
2313 if (!preferred_tips
)
2316 for_each_string_list_item(item
, preferred_tips
) {
2317 if (starts_with(refname
, item
->string
))