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-store.h"
20 #include "list-objects-filter-options.h"
25 * An entry on the bitmap index, representing the bitmap for a given
28 struct stored_bitmap
{
30 struct ewah_bitmap
*root
;
31 struct stored_bitmap
*xor;
36 * The active bitmap index for a repository. By design, repositories only have
37 * a single bitmap index available (the index for the biggest packfile in
38 * the repository), since bitmap indexes need full closure.
40 * If there is more than one bitmap index available (e.g. because of alternates),
41 * the active bitmap index is the largest one.
45 * The pack or multi-pack index (MIDX) that this bitmap index belongs
48 * Exactly one of these must be non-NULL; this specifies the object
49 * order used to interpret this bitmap.
51 struct packed_git
*pack
;
52 struct multi_pack_index
*midx
;
55 * Mark the first `reuse_objects` in the packfile as reused:
56 * they will be sent as-is without using them for repacking
59 uint32_t reuse_objects
;
61 /* mmapped buffer of the whole bitmap index */
63 size_t map_size
; /* size of the mmaped buffer */
64 size_t map_pos
; /* current position when loading the index */
69 * Each bitmap marks which objects in the packfile are of the given
70 * type. This provides type information when yielding the objects from
71 * the packfile during a walk, which allows for better delta bases.
73 struct ewah_bitmap
*commits
;
74 struct ewah_bitmap
*trees
;
75 struct ewah_bitmap
*blobs
;
76 struct ewah_bitmap
*tags
;
78 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
79 kh_oid_map_t
*bitmaps
;
81 /* Number of bitmapped commits */
84 /* If not NULL, this is a name-hash cache pointing into map. */
87 /* The checksum of the packfile or MIDX; points into map. */
88 const unsigned char *checksum
;
91 * If not NULL, this point into the commit table extension
92 * (within the memory mapped region `map`).
94 unsigned char *table_lookup
;
99 * When trying to perform bitmap operations with objects that are not
100 * packed in `pack`, these objects are added to this "fake index" and
101 * are assumed to appear at the end of the packfile for all operations
104 struct object
**objects
;
106 uint32_t count
, alloc
;
107 kh_oid_pos_t
*positions
;
110 /* Bitmap result of the last performed walk */
111 struct bitmap
*result
;
113 /* "have" bitmap from the last performed walk */
114 struct bitmap
*haves
;
116 /* Version of the bitmap index */
117 unsigned int version
;
120 static struct ewah_bitmap
*lookup_stored_bitmap(struct stored_bitmap
*st
)
122 struct ewah_bitmap
*parent
;
123 struct ewah_bitmap
*composed
;
128 composed
= ewah_pool_new();
129 parent
= lookup_stored_bitmap(st
->xor);
130 ewah_xor(st
->root
, parent
, composed
);
132 ewah_pool_free(st
->root
);
140 * Read a bitmap from the current read position on the mmaped
141 * index, and increase the read position accordingly
143 static struct ewah_bitmap
*read_bitmap_1(struct bitmap_index
*index
)
145 struct ewah_bitmap
*b
= ewah_pool_new();
147 ssize_t bitmap_size
= ewah_read_mmap(b
,
148 index
->map
+ index
->map_pos
,
149 index
->map_size
- index
->map_pos
);
151 if (bitmap_size
< 0) {
152 error(_("failed to load bitmap index (corrupted?)"));
157 index
->map_pos
+= bitmap_size
;
161 static uint32_t bitmap_num_objects(struct bitmap_index
*index
)
164 return index
->midx
->num_objects
;
165 return index
->pack
->num_objects
;
168 static int load_bitmap_header(struct bitmap_index
*index
)
170 struct bitmap_disk_header
*header
= (void *)index
->map
;
171 size_t header_size
= sizeof(*header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
;
173 if (index
->map_size
< header_size
+ the_hash_algo
->rawsz
)
174 return error(_("corrupted bitmap index (too small)"));
176 if (memcmp(header
->magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
)) != 0)
177 return error(_("corrupted bitmap index file (wrong header)"));
179 index
->version
= ntohs(header
->version
);
180 if (index
->version
!= 1)
181 return error(_("unsupported version '%d' for bitmap index file"), index
->version
);
183 /* Parse known bitmap format options */
185 uint32_t flags
= ntohs(header
->options
);
186 size_t cache_size
= st_mult(bitmap_num_objects(index
), sizeof(uint32_t));
187 unsigned char *index_end
= index
->map
+ index
->map_size
- the_hash_algo
->rawsz
;
189 if ((flags
& BITMAP_OPT_FULL_DAG
) == 0)
190 BUG("unsupported options for bitmap index file "
191 "(Git requires BITMAP_OPT_FULL_DAG)");
193 if (flags
& BITMAP_OPT_HASH_CACHE
) {
194 if (cache_size
> index_end
- index
->map
- header_size
)
195 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
196 index
->hashes
= (void *)(index_end
- cache_size
);
197 index_end
-= cache_size
;
200 if (flags
& BITMAP_OPT_LOOKUP_TABLE
) {
201 size_t table_size
= st_mult(ntohl(header
->entry_count
),
202 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
);
203 if (table_size
> index_end
- index
->map
- header_size
)
204 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
205 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
206 index
->table_lookup
= (void *)(index_end
- table_size
);
207 index_end
-= table_size
;
211 index
->entry_count
= ntohl(header
->entry_count
);
212 index
->checksum
= header
->checksum
;
213 index
->map_pos
+= header_size
;
217 static struct stored_bitmap
*store_bitmap(struct bitmap_index
*index
,
218 struct ewah_bitmap
*root
,
219 const struct object_id
*oid
,
220 struct stored_bitmap
*xor_with
,
223 struct stored_bitmap
*stored
;
227 stored
= xmalloc(sizeof(struct stored_bitmap
));
229 stored
->xor = xor_with
;
230 stored
->flags
= flags
;
231 oidcpy(&stored
->oid
, oid
);
233 hash_pos
= kh_put_oid_map(index
->bitmaps
, stored
->oid
, &ret
);
236 * A 0 return code means the insertion succeeded with no changes,
237 * because the SHA1 already existed on the map. This is bad, there
238 * shouldn't be duplicated commits in the index.
241 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid
));
245 kh_value(index
->bitmaps
, hash_pos
) = stored
;
249 static inline uint32_t read_be32(const unsigned char *buffer
, size_t *pos
)
251 uint32_t result
= get_be32(buffer
+ *pos
);
252 (*pos
) += sizeof(result
);
256 static inline uint8_t read_u8(const unsigned char *buffer
, size_t *pos
)
258 return buffer
[(*pos
)++];
261 #define MAX_XOR_OFFSET 160
263 static int nth_bitmap_object_oid(struct bitmap_index
*index
,
264 struct object_id
*oid
,
268 return nth_midxed_object_oid(oid
, index
->midx
, n
) ? 0 : -1;
269 return nth_packed_object_id(oid
, index
->pack
, n
);
272 static int load_bitmap_entries_v1(struct bitmap_index
*index
)
275 struct stored_bitmap
*recent_bitmaps
[MAX_XOR_OFFSET
] = { NULL
};
277 for (i
= 0; i
< index
->entry_count
; ++i
) {
278 int xor_offset
, flags
;
279 struct ewah_bitmap
*bitmap
= NULL
;
280 struct stored_bitmap
*xor_bitmap
= NULL
;
281 uint32_t commit_idx_pos
;
282 struct object_id oid
;
284 if (index
->map_size
- index
->map_pos
< 6)
285 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i
);
287 commit_idx_pos
= read_be32(index
->map
, &index
->map_pos
);
288 xor_offset
= read_u8(index
->map
, &index
->map_pos
);
289 flags
= read_u8(index
->map
, &index
->map_pos
);
291 if (nth_bitmap_object_oid(index
, &oid
, commit_idx_pos
) < 0)
292 return error(_("corrupt ewah bitmap: commit index %u out of range"),
293 (unsigned)commit_idx_pos
);
295 bitmap
= read_bitmap_1(index
);
299 if (xor_offset
> MAX_XOR_OFFSET
|| xor_offset
> i
)
300 return error(_("corrupted bitmap pack index"));
302 if (xor_offset
> 0) {
303 xor_bitmap
= recent_bitmaps
[(i
- xor_offset
) % MAX_XOR_OFFSET
];
306 return error(_("invalid XOR offset in bitmap pack index"));
309 recent_bitmaps
[i
% MAX_XOR_OFFSET
] = store_bitmap(
310 index
, bitmap
, &oid
, xor_bitmap
, flags
);
316 char *midx_bitmap_filename(struct multi_pack_index
*midx
)
318 struct strbuf buf
= STRBUF_INIT
;
320 get_midx_filename(&buf
, midx
->object_dir
);
321 strbuf_addf(&buf
, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx
)));
323 return strbuf_detach(&buf
, NULL
);
326 char *pack_bitmap_filename(struct packed_git
*p
)
330 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
331 BUG("pack_name does not end in .pack");
332 return xstrfmt("%.*s.bitmap", (int)len
, p
->pack_name
);
335 static int open_midx_bitmap_1(struct bitmap_index
*bitmap_git
,
336 struct multi_pack_index
*midx
)
339 char *bitmap_name
= midx_bitmap_filename(midx
);
340 int fd
= git_open(bitmap_name
);
342 struct packed_git
*preferred
;
346 warning_errno("cannot open '%s'", bitmap_name
);
352 if (fstat(fd
, &st
)) {
353 error_errno(_("cannot fstat bitmap file"));
358 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
359 struct strbuf buf
= STRBUF_INIT
;
360 get_midx_filename(&buf
, midx
->object_dir
);
361 trace2_data_string("bitmap", the_repository
,
362 "ignoring extra midx bitmap file", buf
.buf
);
364 strbuf_release(&buf
);
368 bitmap_git
->midx
= midx
;
369 bitmap_git
->map_size
= xsize_t(st
.st_size
);
370 bitmap_git
->map_pos
= 0;
371 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
,
375 if (load_bitmap_header(bitmap_git
) < 0)
378 if (!hasheq(get_midx_checksum(bitmap_git
->midx
), bitmap_git
->checksum
)) {
379 error(_("checksum doesn't match in MIDX and bitmap"));
383 if (load_midx_revindex(bitmap_git
->midx
) < 0) {
384 warning(_("multi-pack bitmap is missing required reverse index"));
388 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
389 if (prepare_midx_pack(the_repository
, bitmap_git
->midx
, i
))
390 die(_("could not open pack %s"),
391 bitmap_git
->midx
->pack_names
[i
]);
394 preferred
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
395 if (!is_pack_valid(preferred
)) {
396 warning(_("preferred pack (%s) is invalid"),
397 preferred
->pack_name
);
404 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
405 bitmap_git
->map_size
= 0;
406 bitmap_git
->map_pos
= 0;
407 bitmap_git
->map
= NULL
;
408 bitmap_git
->midx
= NULL
;
412 static int open_pack_bitmap_1(struct bitmap_index
*bitmap_git
, struct packed_git
*packfile
)
418 bitmap_name
= pack_bitmap_filename(packfile
);
419 fd
= git_open(bitmap_name
);
423 warning_errno("cannot open '%s'", bitmap_name
);
429 if (fstat(fd
, &st
)) {
430 error_errno(_("cannot fstat bitmap file"));
435 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
436 trace2_data_string("bitmap", the_repository
,
437 "ignoring extra bitmap file", packfile
->pack_name
);
442 if (!is_pack_valid(packfile
)) {
447 bitmap_git
->pack
= packfile
;
448 bitmap_git
->map_size
= xsize_t(st
.st_size
);
449 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
450 bitmap_git
->map_pos
= 0;
453 if (load_bitmap_header(bitmap_git
) < 0) {
454 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
455 bitmap_git
->map
= NULL
;
456 bitmap_git
->map_size
= 0;
457 bitmap_git
->map_pos
= 0;
458 bitmap_git
->pack
= NULL
;
462 trace2_data_string("bitmap", the_repository
, "opened bitmap file",
463 packfile
->pack_name
);
467 static int load_reverse_index(struct bitmap_index
*bitmap_git
)
469 if (bitmap_is_midx(bitmap_git
)) {
474 * The multi-pack-index's .rev file is already loaded via
475 * open_pack_bitmap_1().
477 * But we still need to open the individual pack .rev files,
478 * since we will need to make use of them in pack-objects.
480 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
481 ret
= load_pack_revindex(bitmap_git
->midx
->packs
[i
]);
487 return load_pack_revindex(bitmap_git
->pack
);
490 static int load_bitmap(struct bitmap_index
*bitmap_git
)
492 assert(bitmap_git
->map
);
494 bitmap_git
->bitmaps
= kh_init_oid_map();
495 bitmap_git
->ext_index
.positions
= kh_init_oid_pos();
497 if (load_reverse_index(bitmap_git
))
500 if (!(bitmap_git
->commits
= read_bitmap_1(bitmap_git
)) ||
501 !(bitmap_git
->trees
= read_bitmap_1(bitmap_git
)) ||
502 !(bitmap_git
->blobs
= read_bitmap_1(bitmap_git
)) ||
503 !(bitmap_git
->tags
= read_bitmap_1(bitmap_git
)))
506 if (!bitmap_git
->table_lookup
&& load_bitmap_entries_v1(bitmap_git
) < 0)
512 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
513 bitmap_git
->map
= NULL
;
514 bitmap_git
->map_size
= 0;
516 kh_destroy_oid_map(bitmap_git
->bitmaps
);
517 bitmap_git
->bitmaps
= NULL
;
519 kh_destroy_oid_pos(bitmap_git
->ext_index
.positions
);
520 bitmap_git
->ext_index
.positions
= NULL
;
525 static int open_pack_bitmap(struct repository
*r
,
526 struct bitmap_index
*bitmap_git
)
528 struct packed_git
*p
;
531 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
532 if (open_pack_bitmap_1(bitmap_git
, p
) == 0) {
535 * The only reason to keep looking is to report
538 if (!trace2_is_enabled())
546 static int open_midx_bitmap(struct repository
*r
,
547 struct bitmap_index
*bitmap_git
)
550 struct multi_pack_index
*midx
;
552 assert(!bitmap_git
->map
);
554 for (midx
= get_multi_pack_index(r
); midx
; midx
= midx
->next
) {
555 if (!open_midx_bitmap_1(bitmap_git
, midx
))
561 static int open_bitmap(struct repository
*r
,
562 struct bitmap_index
*bitmap_git
)
566 assert(!bitmap_git
->map
);
568 found
= !open_midx_bitmap(r
, bitmap_git
);
571 * these will all be skipped if we opened a midx bitmap; but run it
572 * anyway if tracing is enabled to report the duplicates
574 if (!found
|| trace2_is_enabled())
575 found
|= !open_pack_bitmap(r
, bitmap_git
);
577 return found
? 0 : -1;
580 struct bitmap_index
*prepare_bitmap_git(struct repository
*r
)
582 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
584 if (!open_bitmap(r
, bitmap_git
) && !load_bitmap(bitmap_git
))
587 free_bitmap_index(bitmap_git
);
591 struct bitmap_index
*prepare_midx_bitmap_git(struct multi_pack_index
*midx
)
593 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
595 if (!open_midx_bitmap_1(bitmap_git
, midx
) && !load_bitmap(bitmap_git
))
598 free_bitmap_index(bitmap_git
);
602 struct include_data
{
603 struct bitmap_index
*bitmap_git
;
608 struct bitmap_lookup_table_triplet
{
614 struct bitmap_lookup_table_xor_item
{
615 struct object_id oid
;
620 * Given a `triplet` struct pointer and pointer `p`, this
621 * function reads the triplet beginning at `p` into the struct.
622 * Note that this function assumes that there is enough memory
623 * left for filling the `triplet` struct from `p`.
625 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet
*triplet
,
626 const unsigned char *p
)
631 triplet
->commit_pos
= get_be32(p
);
632 p
+= sizeof(uint32_t);
633 triplet
->offset
= get_be64(p
);
634 p
+= sizeof(uint64_t);
635 triplet
->xor_row
= get_be32(p
);
640 * This function gets the raw triplet from `row`'th row in the
641 * lookup table and fills that data to the `triplet`.
643 static int bitmap_lookup_table_get_triplet(struct bitmap_index
*bitmap_git
,
645 struct bitmap_lookup_table_triplet
*triplet
)
647 unsigned char *p
= NULL
;
648 if (pos
>= bitmap_git
->entry_count
)
649 return error(_("corrupt bitmap lookup table: triplet position out of index"));
651 p
= bitmap_git
->table_lookup
+ st_mult(pos
, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
);
653 return bitmap_lookup_table_get_triplet_by_pointer(triplet
, p
);
657 * Searches for a matching triplet. `commit_pos` is a pointer
658 * to the wanted commit position value. `table_entry` points to
659 * a triplet in lookup table. The first 4 bytes of each
660 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
662 static int triplet_cmp(const void *commit_pos
, const void *table_entry
)
665 uint32_t a
= *(uint32_t *)commit_pos
;
666 uint32_t b
= get_be32(table_entry
);
675 static uint32_t bitmap_bsearch_pos(struct bitmap_index
*bitmap_git
,
676 struct object_id
*oid
,
681 if (bitmap_is_midx(bitmap_git
))
682 found
= bsearch_midx(oid
, bitmap_git
->midx
, result
);
684 found
= bsearch_pack(oid
, bitmap_git
->pack
, result
);
690 * `bsearch_triplet_by_pos` function searches for the raw triplet
691 * having commit position same as `commit_pos` and fills `triplet`
692 * object from the raw triplet. Returns 1 on success and 0 on
695 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos
,
696 struct bitmap_index
*bitmap_git
,
697 struct bitmap_lookup_table_triplet
*triplet
)
699 unsigned char *p
= bsearch(&commit_pos
, bitmap_git
->table_lookup
, bitmap_git
->entry_count
,
700 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH
, triplet_cmp
);
705 return bitmap_lookup_table_get_triplet_by_pointer(triplet
, p
);
708 static struct stored_bitmap
*lazy_bitmap_for_commit(struct bitmap_index
*bitmap_git
,
709 struct commit
*commit
)
711 uint32_t commit_pos
, xor_row
;
714 struct bitmap_lookup_table_triplet triplet
;
715 struct object_id
*oid
= &commit
->object
.oid
;
716 struct ewah_bitmap
*bitmap
;
717 struct stored_bitmap
*xor_bitmap
= NULL
;
718 const int bitmap_header_size
= 6;
719 static struct bitmap_lookup_table_xor_item
*xor_items
= NULL
;
720 static size_t xor_items_nr
= 0, xor_items_alloc
= 0;
721 static int is_corrupt
= 0;
724 struct bitmap_lookup_table_xor_item
*xor_item
;
729 if (!bitmap_bsearch_pos(bitmap_git
, oid
, &commit_pos
))
732 if (bitmap_bsearch_triplet_by_pos(commit_pos
, bitmap_git
, &triplet
) < 0)
736 offset
= triplet
.offset
;
737 xor_row
= triplet
.xor_row
;
739 while (xor_row
!= 0xffffffff) {
740 ALLOC_GROW(xor_items
, xor_items_nr
+ 1, xor_items_alloc
);
742 if (xor_items_nr
+ 1 >= bitmap_git
->entry_count
) {
743 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
747 if (bitmap_lookup_table_get_triplet(bitmap_git
, xor_row
, &triplet
) < 0)
750 xor_item
= &xor_items
[xor_items_nr
];
751 xor_item
->offset
= triplet
.offset
;
753 if (nth_bitmap_object_oid(bitmap_git
, &xor_item
->oid
, triplet
.commit_pos
) < 0) {
754 error(_("corrupt bitmap lookup table: commit index %u out of range"),
759 hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
, xor_item
->oid
);
762 * If desired bitmap is already stored, we don't need
763 * to iterate further. Because we know that bitmaps
764 * that are needed to be parsed to parse this bitmap
765 * has already been stored. So, assign this stored bitmap
768 if (hash_pos
< kh_end(bitmap_git
->bitmaps
) &&
769 (xor_bitmap
= kh_value(bitmap_git
->bitmaps
, hash_pos
)))
772 xor_row
= triplet
.xor_row
;
775 while (xor_items_nr
) {
776 xor_item
= &xor_items
[xor_items_nr
- 1];
777 bitmap_git
->map_pos
= xor_item
->offset
;
778 if (bitmap_git
->map_size
- bitmap_git
->map_pos
< bitmap_header_size
) {
779 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
780 oid_to_hex(&xor_item
->oid
));
784 bitmap_git
->map_pos
+= sizeof(uint32_t) + sizeof(uint8_t);
785 xor_flags
= read_u8(bitmap_git
->map
, &bitmap_git
->map_pos
);
786 bitmap
= read_bitmap_1(bitmap_git
);
791 xor_bitmap
= store_bitmap(bitmap_git
, bitmap
, &xor_item
->oid
, xor_bitmap
, xor_flags
);
795 bitmap_git
->map_pos
= offset
;
796 if (bitmap_git
->map_size
- bitmap_git
->map_pos
< bitmap_header_size
) {
797 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
803 * Don't bother reading the commit's index position or its xor
806 * - The commit's index position is irrelevant to us, since
807 * load_bitmap_entries_v1 only uses it to learn the object
808 * id which is used to compute the hashmap's key. We already
809 * have an object id, so no need to look it up again.
811 * - The xor_offset is unusable for us, since it specifies how
812 * many entries previous to ours we should look at. This
813 * makes sense when reading the bitmaps sequentially (as in
814 * load_bitmap_entries_v1()), since we can keep track of
815 * each bitmap as we read them.
817 * But it can't work for us, since the bitmap's don't have a
818 * fixed size. So we learn the position of the xor'd bitmap
819 * from the commit table (and resolve it to a bitmap in the
820 * above if-statement).
822 * Instead, we can skip ahead and immediately read the flags and
825 bitmap_git
->map_pos
+= sizeof(uint32_t) + sizeof(uint8_t);
826 flags
= read_u8(bitmap_git
->map
, &bitmap_git
->map_pos
);
827 bitmap
= read_bitmap_1(bitmap_git
);
832 return store_bitmap(bitmap_git
, bitmap
, oid
, xor_bitmap
, flags
);
840 struct ewah_bitmap
*bitmap_for_commit(struct bitmap_index
*bitmap_git
,
841 struct commit
*commit
)
843 khiter_t hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
,
845 if (hash_pos
>= kh_end(bitmap_git
->bitmaps
)) {
846 struct stored_bitmap
*bitmap
= NULL
;
847 if (!bitmap_git
->table_lookup
)
850 /* this is a fairly hot codepath - no trace2_region please */
851 /* NEEDSWORK: cache misses aren't recorded */
852 bitmap
= lazy_bitmap_for_commit(bitmap_git
, commit
);
855 return lookup_stored_bitmap(bitmap
);
857 return lookup_stored_bitmap(kh_value(bitmap_git
->bitmaps
, hash_pos
));
860 static inline int bitmap_position_extended(struct bitmap_index
*bitmap_git
,
861 const struct object_id
*oid
)
863 kh_oid_pos_t
*positions
= bitmap_git
->ext_index
.positions
;
864 khiter_t pos
= kh_get_oid_pos(positions
, *oid
);
866 if (pos
< kh_end(positions
)) {
867 int bitmap_pos
= kh_value(positions
, pos
);
868 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
874 static inline int bitmap_position_packfile(struct bitmap_index
*bitmap_git
,
875 const struct object_id
*oid
)
878 off_t offset
= find_pack_entry_one(oid
->hash
, bitmap_git
->pack
);
882 if (offset_to_pack_pos(bitmap_git
->pack
, offset
, &pos
) < 0)
887 static int bitmap_position_midx(struct bitmap_index
*bitmap_git
,
888 const struct object_id
*oid
)
891 if (!bsearch_midx(oid
, bitmap_git
->midx
, &want
))
894 if (midx_to_pack_pos(bitmap_git
->midx
, want
, &got
) < 0)
899 static int bitmap_position(struct bitmap_index
*bitmap_git
,
900 const struct object_id
*oid
)
903 if (bitmap_is_midx(bitmap_git
))
904 pos
= bitmap_position_midx(bitmap_git
, oid
);
906 pos
= bitmap_position_packfile(bitmap_git
, oid
);
907 return (pos
>= 0) ? pos
: bitmap_position_extended(bitmap_git
, oid
);
910 static int ext_index_add_object(struct bitmap_index
*bitmap_git
,
911 struct object
*object
, const char *name
)
913 struct eindex
*eindex
= &bitmap_git
->ext_index
;
919 hash_pos
= kh_put_oid_pos(eindex
->positions
, object
->oid
, &hash_ret
);
921 if (eindex
->count
>= eindex
->alloc
) {
922 eindex
->alloc
= (eindex
->alloc
+ 16) * 3 / 2;
923 REALLOC_ARRAY(eindex
->objects
, eindex
->alloc
);
924 REALLOC_ARRAY(eindex
->hashes
, eindex
->alloc
);
927 bitmap_pos
= eindex
->count
;
928 eindex
->objects
[eindex
->count
] = object
;
929 eindex
->hashes
[eindex
->count
] = pack_name_hash(name
);
930 kh_value(eindex
->positions
, hash_pos
) = bitmap_pos
;
933 bitmap_pos
= kh_value(eindex
->positions
, hash_pos
);
936 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
939 struct bitmap_show_data
{
940 struct bitmap_index
*bitmap_git
;
944 static void show_object(struct object
*object
, const char *name
, void *data_
)
946 struct bitmap_show_data
*data
= data_
;
949 bitmap_pos
= bitmap_position(data
->bitmap_git
, &object
->oid
);
952 bitmap_pos
= ext_index_add_object(data
->bitmap_git
, object
,
955 bitmap_set(data
->base
, bitmap_pos
);
958 static void show_commit(struct commit
*commit UNUSED
,
963 static int add_to_include_set(struct bitmap_index
*bitmap_git
,
964 struct include_data
*data
,
965 struct commit
*commit
,
968 struct ewah_bitmap
*partial
;
970 if (data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
))
973 if (bitmap_get(data
->base
, bitmap_pos
))
976 partial
= bitmap_for_commit(bitmap_git
, commit
);
978 bitmap_or_ewah(data
->base
, partial
);
982 bitmap_set(data
->base
, bitmap_pos
);
986 static int should_include(struct commit
*commit
, void *_data
)
988 struct include_data
*data
= _data
;
991 bitmap_pos
= bitmap_position(data
->bitmap_git
, &commit
->object
.oid
);
993 bitmap_pos
= ext_index_add_object(data
->bitmap_git
,
994 (struct object
*)commit
,
997 if (!add_to_include_set(data
->bitmap_git
, data
, commit
, bitmap_pos
)) {
998 struct commit_list
*parent
= commit
->parents
;
1001 parent
->item
->object
.flags
|= SEEN
;
1002 parent
= parent
->next
;
1011 static int should_include_obj(struct object
*obj
, void *_data
)
1013 struct include_data
*data
= _data
;
1016 bitmap_pos
= bitmap_position(data
->bitmap_git
, &obj
->oid
);
1019 if ((data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
)) ||
1020 bitmap_get(data
->base
, bitmap_pos
)) {
1027 static int add_commit_to_bitmap(struct bitmap_index
*bitmap_git
,
1028 struct bitmap
**base
,
1029 struct commit
*commit
)
1031 struct ewah_bitmap
*or_with
= bitmap_for_commit(bitmap_git
, commit
);
1037 *base
= ewah_to_bitmap(or_with
);
1039 bitmap_or_ewah(*base
, or_with
);
1044 static struct bitmap
*find_objects(struct bitmap_index
*bitmap_git
,
1045 struct rev_info
*revs
,
1046 struct object_list
*roots
,
1047 struct bitmap
*seen
)
1049 struct bitmap
*base
= NULL
;
1052 struct object_list
*not_mapped
= NULL
;
1055 * Go through all the roots for the walk. The ones that have bitmaps
1056 * on the bitmap index will be `or`ed together to form an initial
1057 * global reachability analysis.
1059 * The ones without bitmaps in the index will be stored in the
1060 * `not_mapped_list` for further processing.
1063 struct object
*object
= roots
->item
;
1064 roots
= roots
->next
;
1066 if (object
->type
== OBJ_COMMIT
&&
1067 add_commit_to_bitmap(bitmap_git
, &base
, (struct commit
*)object
)) {
1068 object
->flags
|= SEEN
;
1072 object_list_insert(object
, ¬_mapped
);
1076 * Best case scenario: We found bitmaps for all the roots,
1077 * so the resulting `or` bitmap has the full reachability analysis
1085 * Let's iterate through all the roots that don't have bitmaps to
1086 * check if we can determine them to be reachable from the existing
1089 * If we cannot find them in the existing global bitmap, we'll need
1090 * to push them to an actual walk and run it until we can confirm
1091 * they are reachable
1094 struct object
*object
= roots
->item
;
1097 roots
= roots
->next
;
1098 pos
= bitmap_position(bitmap_git
, &object
->oid
);
1100 if (pos
< 0 || base
== NULL
|| !bitmap_get(base
, pos
)) {
1101 object
->flags
&= ~UNINTERESTING
;
1102 add_pending_object(revs
, object
, "");
1105 object
->flags
|= SEEN
;
1110 struct include_data incdata
;
1111 struct bitmap_show_data show_data
;
1114 base
= bitmap_new();
1116 incdata
.bitmap_git
= bitmap_git
;
1117 incdata
.base
= base
;
1118 incdata
.seen
= seen
;
1120 revs
->include_check
= should_include
;
1121 revs
->include_check_obj
= should_include_obj
;
1122 revs
->include_check_data
= &incdata
;
1124 if (prepare_revision_walk(revs
))
1125 die(_("revision walk setup failed"));
1127 show_data
.bitmap_git
= bitmap_git
;
1128 show_data
.base
= base
;
1130 traverse_commit_list(revs
,
1131 show_commit
, show_object
,
1134 revs
->include_check
= NULL
;
1135 revs
->include_check_obj
= NULL
;
1136 revs
->include_check_data
= NULL
;
1142 static void show_extended_objects(struct bitmap_index
*bitmap_git
,
1143 struct rev_info
*revs
,
1144 show_reachable_fn show_reach
)
1146 struct bitmap
*objects
= bitmap_git
->result
;
1147 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1150 for (i
= 0; i
< eindex
->count
; ++i
) {
1153 if (!bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1156 obj
= eindex
->objects
[i
];
1157 if ((obj
->type
== OBJ_BLOB
&& !revs
->blob_objects
) ||
1158 (obj
->type
== OBJ_TREE
&& !revs
->tree_objects
) ||
1159 (obj
->type
== OBJ_TAG
&& !revs
->tag_objects
))
1162 show_reach(&obj
->oid
, obj
->type
, 0, eindex
->hashes
[i
], NULL
, 0);
1166 static void init_type_iterator(struct ewah_iterator
*it
,
1167 struct bitmap_index
*bitmap_git
,
1168 enum object_type type
)
1172 ewah_iterator_init(it
, bitmap_git
->commits
);
1176 ewah_iterator_init(it
, bitmap_git
->trees
);
1180 ewah_iterator_init(it
, bitmap_git
->blobs
);
1184 ewah_iterator_init(it
, bitmap_git
->tags
);
1188 BUG("object type %d not stored by bitmap type index", type
);
1193 static void show_objects_for_type(
1194 struct bitmap_index
*bitmap_git
,
1195 enum object_type object_type
,
1196 show_reachable_fn show_reach
)
1201 struct ewah_iterator it
;
1204 struct bitmap
*objects
= bitmap_git
->result
;
1206 init_type_iterator(&it
, bitmap_git
, object_type
);
1208 for (i
= 0; i
< objects
->word_alloc
&&
1209 ewah_iterator_next(&filter
, &it
); i
++) {
1210 eword_t word
= objects
->words
[i
] & filter
;
1211 size_t pos
= (i
* BITS_IN_EWORD
);
1216 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1217 struct packed_git
*pack
;
1218 struct object_id oid
;
1219 uint32_t hash
= 0, index_pos
;
1222 if ((word
>> offset
) == 0)
1225 offset
+= ewah_bit_ctz64(word
>> offset
);
1227 if (bitmap_is_midx(bitmap_git
)) {
1228 struct multi_pack_index
*m
= bitmap_git
->midx
;
1231 index_pos
= pack_pos_to_midx(m
, pos
+ offset
);
1232 ofs
= nth_midxed_offset(m
, index_pos
);
1233 nth_midxed_object_oid(&oid
, m
, index_pos
);
1235 pack_id
= nth_midxed_pack_int_id(m
, index_pos
);
1236 pack
= bitmap_git
->midx
->packs
[pack_id
];
1238 index_pos
= pack_pos_to_index(bitmap_git
->pack
, pos
+ offset
);
1239 ofs
= pack_pos_to_offset(bitmap_git
->pack
, pos
+ offset
);
1240 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
1242 pack
= bitmap_git
->pack
;
1245 if (bitmap_git
->hashes
)
1246 hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
1248 show_reach(&oid
, object_type
, 0, hash
, pack
, ofs
);
1253 static int in_bitmapped_pack(struct bitmap_index
*bitmap_git
,
1254 struct object_list
*roots
)
1257 struct object
*object
= roots
->item
;
1258 roots
= roots
->next
;
1260 if (bitmap_is_midx(bitmap_git
)) {
1261 if (bsearch_midx(&object
->oid
, bitmap_git
->midx
, NULL
))
1264 if (find_pack_entry_one(object
->oid
.hash
, bitmap_git
->pack
) > 0)
1272 static struct bitmap
*find_tip_objects(struct bitmap_index
*bitmap_git
,
1273 struct object_list
*tip_objects
,
1274 enum object_type type
)
1276 struct bitmap
*result
= bitmap_new();
1277 struct object_list
*p
;
1279 for (p
= tip_objects
; p
; p
= p
->next
) {
1282 if (p
->item
->type
!= type
)
1285 pos
= bitmap_position(bitmap_git
, &p
->item
->oid
);
1289 bitmap_set(result
, pos
);
1295 static void filter_bitmap_exclude_type(struct bitmap_index
*bitmap_git
,
1296 struct object_list
*tip_objects
,
1297 struct bitmap
*to_filter
,
1298 enum object_type type
)
1300 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1301 struct bitmap
*tips
;
1302 struct ewah_iterator it
;
1307 * The non-bitmap version of this filter never removes
1308 * objects which the other side specifically asked for,
1309 * so we must match that behavior.
1311 tips
= find_tip_objects(bitmap_git
, tip_objects
, type
);
1314 * We can use the type-level bitmap for 'type' to work in whole
1315 * words for the objects that are actually in the bitmapped
1318 for (i
= 0, init_type_iterator(&it
, bitmap_git
, type
);
1319 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1321 if (i
< tips
->word_alloc
)
1322 mask
&= ~tips
->words
[i
];
1323 to_filter
->words
[i
] &= ~mask
;
1327 * Clear any objects that weren't in the packfile (and so would
1328 * not have been caught by the loop above. We'll have to check
1329 * them individually.
1331 for (i
= 0; i
< eindex
->count
; i
++) {
1332 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1333 if (eindex
->objects
[i
]->type
== type
&&
1334 bitmap_get(to_filter
, pos
) &&
1335 !bitmap_get(tips
, pos
))
1336 bitmap_unset(to_filter
, pos
);
1342 static void filter_bitmap_blob_none(struct bitmap_index
*bitmap_git
,
1343 struct object_list
*tip_objects
,
1344 struct bitmap
*to_filter
)
1346 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1350 static unsigned long get_size_by_pos(struct bitmap_index
*bitmap_git
,
1354 struct object_info oi
= OBJECT_INFO_INIT
;
1358 if (pos
< bitmap_num_objects(bitmap_git
)) {
1359 struct packed_git
*pack
;
1362 if (bitmap_is_midx(bitmap_git
)) {
1363 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, pos
);
1364 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
1366 pack
= bitmap_git
->midx
->packs
[pack_id
];
1367 ofs
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
1369 pack
= bitmap_git
->pack
;
1370 ofs
= pack_pos_to_offset(pack
, pos
);
1373 if (packed_object_info(the_repository
, pack
, ofs
, &oi
) < 0) {
1374 struct object_id oid
;
1375 nth_bitmap_object_oid(bitmap_git
, &oid
,
1376 pack_pos_to_index(pack
, pos
));
1377 die(_("unable to get size of %s"), oid_to_hex(&oid
));
1380 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1381 struct object
*obj
= eindex
->objects
[pos
- bitmap_num_objects(bitmap_git
)];
1382 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
1383 die(_("unable to get size of %s"), oid_to_hex(&obj
->oid
));
1389 static void filter_bitmap_blob_limit(struct bitmap_index
*bitmap_git
,
1390 struct object_list
*tip_objects
,
1391 struct bitmap
*to_filter
,
1392 unsigned long limit
)
1394 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1395 struct bitmap
*tips
;
1396 struct ewah_iterator it
;
1400 tips
= find_tip_objects(bitmap_git
, tip_objects
, OBJ_BLOB
);
1402 for (i
= 0, init_type_iterator(&it
, bitmap_git
, OBJ_BLOB
);
1403 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1405 eword_t word
= to_filter
->words
[i
] & mask
;
1408 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1411 if ((word
>> offset
) == 0)
1413 offset
+= ewah_bit_ctz64(word
>> offset
);
1414 pos
= i
* BITS_IN_EWORD
+ offset
;
1416 if (!bitmap_get(tips
, pos
) &&
1417 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1418 bitmap_unset(to_filter
, pos
);
1422 for (i
= 0; i
< eindex
->count
; i
++) {
1423 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1424 if (eindex
->objects
[i
]->type
== OBJ_BLOB
&&
1425 bitmap_get(to_filter
, pos
) &&
1426 !bitmap_get(tips
, pos
) &&
1427 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1428 bitmap_unset(to_filter
, pos
);
1434 static void filter_bitmap_tree_depth(struct bitmap_index
*bitmap_git
,
1435 struct object_list
*tip_objects
,
1436 struct bitmap
*to_filter
,
1437 unsigned long limit
)
1440 BUG("filter_bitmap_tree_depth given non-zero limit");
1442 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1444 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1448 static void filter_bitmap_object_type(struct bitmap_index
*bitmap_git
,
1449 struct object_list
*tip_objects
,
1450 struct bitmap
*to_filter
,
1451 enum object_type object_type
)
1453 if (object_type
< OBJ_COMMIT
|| object_type
> OBJ_TAG
)
1454 BUG("filter_bitmap_object_type given invalid object");
1456 if (object_type
!= OBJ_TAG
)
1457 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TAG
);
1458 if (object_type
!= OBJ_COMMIT
)
1459 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_COMMIT
);
1460 if (object_type
!= OBJ_TREE
)
1461 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TREE
);
1462 if (object_type
!= OBJ_BLOB
)
1463 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_BLOB
);
1466 static int filter_bitmap(struct bitmap_index
*bitmap_git
,
1467 struct object_list
*tip_objects
,
1468 struct bitmap
*to_filter
,
1469 struct list_objects_filter_options
*filter
)
1471 if (!filter
|| filter
->choice
== LOFC_DISABLED
)
1474 if (filter
->choice
== LOFC_BLOB_NONE
) {
1476 filter_bitmap_blob_none(bitmap_git
, tip_objects
,
1481 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
1483 filter_bitmap_blob_limit(bitmap_git
, tip_objects
,
1485 filter
->blob_limit_value
);
1489 if (filter
->choice
== LOFC_TREE_DEPTH
&&
1490 filter
->tree_exclude_depth
== 0) {
1492 filter_bitmap_tree_depth(bitmap_git
, tip_objects
,
1494 filter
->tree_exclude_depth
);
1498 if (filter
->choice
== LOFC_OBJECT_TYPE
) {
1500 filter_bitmap_object_type(bitmap_git
, tip_objects
,
1502 filter
->object_type
);
1506 if (filter
->choice
== LOFC_COMBINE
) {
1508 for (i
= 0; i
< filter
->sub_nr
; i
++) {
1509 if (filter_bitmap(bitmap_git
, tip_objects
, to_filter
,
1510 &filter
->sub
[i
]) < 0)
1516 /* filter choice not handled */
1520 static int can_filter_bitmap(struct list_objects_filter_options
*filter
)
1522 return !filter_bitmap(NULL
, NULL
, NULL
, filter
);
1525 struct bitmap_index
*prepare_bitmap_walk(struct rev_info
*revs
,
1526 int filter_provided_objects
)
1530 struct object_list
*wants
= NULL
;
1531 struct object_list
*haves
= NULL
;
1533 struct bitmap
*wants_bitmap
= NULL
;
1534 struct bitmap
*haves_bitmap
= NULL
;
1536 struct bitmap_index
*bitmap_git
;
1539 * We can't do pathspec limiting with bitmaps, because we don't know
1540 * which commits are associated with which object changes (let alone
1541 * even which objects are associated with which paths).
1546 if (!can_filter_bitmap(&revs
->filter
))
1549 /* try to open a bitmapped pack, but don't parse it yet
1550 * because we may not need to use it */
1551 CALLOC_ARRAY(bitmap_git
, 1);
1552 if (open_bitmap(revs
->repo
, bitmap_git
) < 0)
1555 for (i
= 0; i
< revs
->pending
.nr
; ++i
) {
1556 struct object
*object
= revs
->pending
.objects
[i
].item
;
1558 if (object
->type
== OBJ_NONE
)
1559 parse_object_or_die(&object
->oid
, NULL
);
1561 while (object
->type
== OBJ_TAG
) {
1562 struct tag
*tag
= (struct tag
*) object
;
1564 if (object
->flags
& UNINTERESTING
)
1565 object_list_insert(object
, &haves
);
1567 object_list_insert(object
, &wants
);
1569 object
= parse_object_or_die(get_tagged_oid(tag
), NULL
);
1570 object
->flags
|= (tag
->object
.flags
& UNINTERESTING
);
1573 if (object
->flags
& UNINTERESTING
)
1574 object_list_insert(object
, &haves
);
1576 object_list_insert(object
, &wants
);
1580 * if we have a HAVES list, but none of those haves is contained
1581 * in the packfile that has a bitmap, we don't have anything to
1584 if (haves
&& !in_bitmapped_pack(bitmap_git
, haves
))
1587 /* if we don't want anything, we're done here */
1592 * now we're going to use bitmaps, so load the actual bitmap entries
1593 * from disk. this is the point of no return; after this the rev_list
1594 * becomes invalidated and we must perform the revwalk through bitmaps
1596 if (load_bitmap(bitmap_git
) < 0)
1599 object_array_clear(&revs
->pending
);
1602 revs
->ignore_missing_links
= 1;
1603 haves_bitmap
= find_objects(bitmap_git
, revs
, haves
, NULL
);
1604 reset_revision_walk();
1605 revs
->ignore_missing_links
= 0;
1608 BUG("failed to perform bitmap walk");
1611 wants_bitmap
= find_objects(bitmap_git
, revs
, wants
, haves_bitmap
);
1614 BUG("failed to perform bitmap walk");
1617 bitmap_and_not(wants_bitmap
, haves_bitmap
);
1619 filter_bitmap(bitmap_git
,
1620 (revs
->filter
.choice
&& filter_provided_objects
) ? NULL
: wants
,
1624 bitmap_git
->result
= wants_bitmap
;
1625 bitmap_git
->haves
= haves_bitmap
;
1627 object_list_free(&wants
);
1628 object_list_free(&haves
);
1633 free_bitmap_index(bitmap_git
);
1634 object_list_free(&wants
);
1635 object_list_free(&haves
);
1640 * -1 means "stop trying further objects"; 0 means we may or may not have
1641 * reused, but you can keep feeding bits.
1643 static int try_partial_reuse(struct packed_git
*pack
,
1645 struct bitmap
*reuse
,
1646 struct pack_window
**w_curs
)
1648 off_t offset
, delta_obj_offset
;
1649 enum object_type type
;
1653 * try_partial_reuse() is called either on (a) objects in the
1654 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1655 * objects in the preferred pack of a multi-pack bitmap.
1656 * Importantly, the latter can pretend as if only a single pack
1659 * - The first pack->num_objects bits of a MIDX bitmap are
1660 * reserved for the preferred pack, and
1662 * - Ties due to duplicate objects are always resolved in
1663 * favor of the preferred pack.
1665 * Therefore we do not need to ever ask the MIDX for its copy of
1666 * an object by OID, since it will always select it from the
1667 * preferred pack. Likewise, the selected copy of the base
1668 * object for any deltas will reside in the same pack.
1670 * This means that we can reuse pos when looking up the bit in
1671 * the reuse bitmap, too, since bits corresponding to the
1672 * preferred pack precede all bits from other packs.
1675 if (pos
>= pack
->num_objects
)
1676 return -1; /* not actually in the pack or MIDX preferred pack */
1678 offset
= delta_obj_offset
= pack_pos_to_offset(pack
, pos
);
1679 type
= unpack_object_header(pack
, w_curs
, &offset
, &size
);
1681 return -1; /* broken packfile, punt */
1683 if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
) {
1688 * Find the position of the base object so we can look it up
1689 * in our bitmaps. If we can't come up with an offset, or if
1690 * that offset is not in the revidx, the pack is corrupt.
1691 * There's nothing we can do, so just punt on this object,
1692 * and the normal slow path will complain about it in
1695 base_offset
= get_delta_base(pack
, w_curs
, &offset
, type
,
1699 if (offset_to_pack_pos(pack
, base_offset
, &base_pos
) < 0)
1703 * We assume delta dependencies always point backwards. This
1704 * lets us do a single pass, and is basically always true
1705 * due to the way OFS_DELTAs work. You would not typically
1706 * find REF_DELTA in a bitmapped pack, since we only bitmap
1707 * packs we write fresh, and OFS_DELTA is the default). But
1708 * let's double check to make sure the pack wasn't written with
1711 if (base_pos
>= pos
)
1715 * And finally, if we're not sending the base as part of our
1716 * reuse chunk, then don't send this object either. The base
1717 * would come after us, along with other objects not
1718 * necessarily in the pack, which means we'd need to convert
1719 * to REF_DELTA on the fly. Better to just let the normal
1720 * object_entry code path handle it.
1722 if (!bitmap_get(reuse
, base_pos
))
1727 * If we got here, then the object is OK to reuse. Mark it.
1729 bitmap_set(reuse
, pos
);
1733 uint32_t midx_preferred_pack(struct bitmap_index
*bitmap_git
)
1735 struct multi_pack_index
*m
= bitmap_git
->midx
;
1737 BUG("midx_preferred_pack: requires non-empty MIDX");
1738 return nth_midxed_pack_int_id(m
, pack_pos_to_midx(bitmap_git
->midx
, 0));
1741 int reuse_partial_packfile_from_bitmap(struct bitmap_index
*bitmap_git
,
1742 struct packed_git
**packfile_out
,
1744 struct bitmap
**reuse_out
)
1746 struct packed_git
*pack
;
1747 struct bitmap
*result
= bitmap_git
->result
;
1748 struct bitmap
*reuse
;
1749 struct pack_window
*w_curs
= NULL
;
1752 uint32_t objects_nr
;
1756 load_reverse_index(bitmap_git
);
1758 if (bitmap_is_midx(bitmap_git
))
1759 pack
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
1761 pack
= bitmap_git
->pack
;
1762 objects_nr
= pack
->num_objects
;
1764 while (i
< result
->word_alloc
&& result
->words
[i
] == (eword_t
)~0)
1768 * Don't mark objects not in the packfile or preferred pack. This bitmap
1769 * marks objects eligible for reuse, but the pack-reuse code only
1770 * understands how to reuse a single pack. Since the preferred pack is
1771 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1772 * we use it instead of another pack. In single-pack bitmaps, the choice
1775 if (i
> objects_nr
/ BITS_IN_EWORD
)
1776 i
= objects_nr
/ BITS_IN_EWORD
;
1778 reuse
= bitmap_word_alloc(i
);
1779 memset(reuse
->words
, 0xFF, i
* sizeof(eword_t
));
1781 for (; i
< result
->word_alloc
; ++i
) {
1782 eword_t word
= result
->words
[i
];
1783 size_t pos
= (i
* BITS_IN_EWORD
);
1785 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1786 if ((word
>> offset
) == 0)
1789 offset
+= ewah_bit_ctz64(word
>> offset
);
1790 if (try_partial_reuse(pack
, pos
+ offset
,
1791 reuse
, &w_curs
) < 0) {
1793 * try_partial_reuse indicated we couldn't reuse
1794 * any bits, so there is no point in trying more
1795 * bits in the current word, or any other words
1798 * Jump out of both loops to avoid future
1799 * unnecessary calls to try_partial_reuse.
1807 unuse_pack(&w_curs
);
1809 *entries
= bitmap_popcount(reuse
);
1816 * Drop any reused objects from the result, since they will not
1817 * need to be handled separately.
1819 bitmap_and_not(result
, reuse
);
1820 *packfile_out
= pack
;
1825 int bitmap_walk_contains(struct bitmap_index
*bitmap_git
,
1826 struct bitmap
*bitmap
, const struct object_id
*oid
)
1833 idx
= bitmap_position(bitmap_git
, oid
);
1834 return idx
>= 0 && bitmap_get(bitmap
, idx
);
1837 void traverse_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1838 struct rev_info
*revs
,
1839 show_reachable_fn show_reachable
)
1841 assert(bitmap_git
->result
);
1843 show_objects_for_type(bitmap_git
, OBJ_COMMIT
, show_reachable
);
1844 if (revs
->tree_objects
)
1845 show_objects_for_type(bitmap_git
, OBJ_TREE
, show_reachable
);
1846 if (revs
->blob_objects
)
1847 show_objects_for_type(bitmap_git
, OBJ_BLOB
, show_reachable
);
1848 if (revs
->tag_objects
)
1849 show_objects_for_type(bitmap_git
, OBJ_TAG
, show_reachable
);
1851 show_extended_objects(bitmap_git
, revs
, show_reachable
);
1854 static uint32_t count_object_type(struct bitmap_index
*bitmap_git
,
1855 enum object_type type
)
1857 struct bitmap
*objects
= bitmap_git
->result
;
1858 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1860 uint32_t i
= 0, count
= 0;
1861 struct ewah_iterator it
;
1864 init_type_iterator(&it
, bitmap_git
, type
);
1866 while (i
< objects
->word_alloc
&& ewah_iterator_next(&filter
, &it
)) {
1867 eword_t word
= objects
->words
[i
++] & filter
;
1868 count
+= ewah_bit_popcount64(word
);
1871 for (i
= 0; i
< eindex
->count
; ++i
) {
1872 if (eindex
->objects
[i
]->type
== type
&&
1873 bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1880 void count_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1881 uint32_t *commits
, uint32_t *trees
,
1882 uint32_t *blobs
, uint32_t *tags
)
1884 assert(bitmap_git
->result
);
1887 *commits
= count_object_type(bitmap_git
, OBJ_COMMIT
);
1890 *trees
= count_object_type(bitmap_git
, OBJ_TREE
);
1893 *blobs
= count_object_type(bitmap_git
, OBJ_BLOB
);
1896 *tags
= count_object_type(bitmap_git
, OBJ_TAG
);
1899 struct bitmap_test_data
{
1900 struct bitmap_index
*bitmap_git
;
1901 struct bitmap
*base
;
1902 struct bitmap
*commits
;
1903 struct bitmap
*trees
;
1904 struct bitmap
*blobs
;
1905 struct bitmap
*tags
;
1906 struct progress
*prg
;
1910 static void test_bitmap_type(struct bitmap_test_data
*tdata
,
1911 struct object
*obj
, int pos
)
1913 enum object_type bitmap_type
= OBJ_NONE
;
1916 if (bitmap_get(tdata
->commits
, pos
)) {
1917 bitmap_type
= OBJ_COMMIT
;
1920 if (bitmap_get(tdata
->trees
, pos
)) {
1921 bitmap_type
= OBJ_TREE
;
1924 if (bitmap_get(tdata
->blobs
, pos
)) {
1925 bitmap_type
= OBJ_BLOB
;
1928 if (bitmap_get(tdata
->tags
, pos
)) {
1929 bitmap_type
= OBJ_TAG
;
1933 if (bitmap_type
== OBJ_NONE
)
1934 die(_("object '%s' not found in type bitmaps"),
1935 oid_to_hex(&obj
->oid
));
1938 die(_("object '%s' does not have a unique type"),
1939 oid_to_hex(&obj
->oid
));
1941 if (bitmap_type
!= obj
->type
)
1942 die(_("object '%s': real type '%s', expected: '%s'"),
1943 oid_to_hex(&obj
->oid
),
1944 type_name(obj
->type
),
1945 type_name(bitmap_type
));
1948 static void test_show_object(struct object
*object
,
1949 const char *name UNUSED
,
1952 struct bitmap_test_data
*tdata
= data
;
1955 bitmap_pos
= bitmap_position(tdata
->bitmap_git
, &object
->oid
);
1957 die(_("object not in bitmap: '%s'"), oid_to_hex(&object
->oid
));
1958 test_bitmap_type(tdata
, object
, bitmap_pos
);
1960 bitmap_set(tdata
->base
, bitmap_pos
);
1961 display_progress(tdata
->prg
, ++tdata
->seen
);
1964 static void test_show_commit(struct commit
*commit
, void *data
)
1966 struct bitmap_test_data
*tdata
= data
;
1969 bitmap_pos
= bitmap_position(tdata
->bitmap_git
,
1970 &commit
->object
.oid
);
1972 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit
->object
.oid
));
1973 test_bitmap_type(tdata
, &commit
->object
, bitmap_pos
);
1975 bitmap_set(tdata
->base
, bitmap_pos
);
1976 display_progress(tdata
->prg
, ++tdata
->seen
);
1979 void test_bitmap_walk(struct rev_info
*revs
)
1981 struct object
*root
;
1982 struct bitmap
*result
= NULL
;
1983 size_t result_popcnt
;
1984 struct bitmap_test_data tdata
;
1985 struct bitmap_index
*bitmap_git
;
1986 struct ewah_bitmap
*bm
;
1988 if (!(bitmap_git
= prepare_bitmap_git(revs
->repo
)))
1989 die(_("failed to load bitmap indexes"));
1991 if (revs
->pending
.nr
!= 1)
1992 die(_("you must specify exactly one commit to test"));
1994 fprintf_ln(stderr
, "Bitmap v%d test (%d entries%s)",
1995 bitmap_git
->version
,
1996 bitmap_git
->entry_count
,
1997 bitmap_git
->table_lookup
? "" : " loaded");
1999 root
= revs
->pending
.objects
[0].item
;
2000 bm
= bitmap_for_commit(bitmap_git
, (struct commit
*)root
);
2003 fprintf_ln(stderr
, "Found bitmap for '%s'. %d bits / %08x checksum",
2004 oid_to_hex(&root
->oid
), (int)bm
->bit_size
, ewah_checksum(bm
));
2006 result
= ewah_to_bitmap(bm
);
2010 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root
->oid
));
2012 revs
->tag_objects
= 1;
2013 revs
->tree_objects
= 1;
2014 revs
->blob_objects
= 1;
2016 result_popcnt
= bitmap_popcount(result
);
2018 if (prepare_revision_walk(revs
))
2019 die(_("revision walk setup failed"));
2021 tdata
.bitmap_git
= bitmap_git
;
2022 tdata
.base
= bitmap_new();
2023 tdata
.commits
= ewah_to_bitmap(bitmap_git
->commits
);
2024 tdata
.trees
= ewah_to_bitmap(bitmap_git
->trees
);
2025 tdata
.blobs
= ewah_to_bitmap(bitmap_git
->blobs
);
2026 tdata
.tags
= ewah_to_bitmap(bitmap_git
->tags
);
2027 tdata
.prg
= start_progress("Verifying bitmap entries", result_popcnt
);
2030 traverse_commit_list(revs
, &test_show_commit
, &test_show_object
, &tdata
);
2032 stop_progress(&tdata
.prg
);
2034 if (bitmap_equals(result
, tdata
.base
))
2035 fprintf_ln(stderr
, "OK!");
2037 die(_("mismatch in bitmap results"));
2039 bitmap_free(result
);
2040 bitmap_free(tdata
.base
);
2041 bitmap_free(tdata
.commits
);
2042 bitmap_free(tdata
.trees
);
2043 bitmap_free(tdata
.blobs
);
2044 bitmap_free(tdata
.tags
);
2045 free_bitmap_index(bitmap_git
);
2048 int test_bitmap_commits(struct repository
*r
)
2050 struct object_id oid
;
2051 MAYBE_UNUSED
void *value
;
2052 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
2055 die(_("failed to load bitmap indexes"));
2058 * As this function is only used to print bitmap selected
2059 * commits, we don't have to read the commit table.
2061 if (bitmap_git
->table_lookup
) {
2062 if (load_bitmap_entries_v1(bitmap_git
) < 0)
2063 die(_("failed to load bitmap indexes"));
2066 kh_foreach(bitmap_git
->bitmaps
, oid
, value
, {
2067 printf_ln("%s", oid_to_hex(&oid
));
2070 free_bitmap_index(bitmap_git
);
2075 int test_bitmap_hashes(struct repository
*r
)
2077 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
2078 struct object_id oid
;
2079 uint32_t i
, index_pos
;
2081 if (!bitmap_git
|| !bitmap_git
->hashes
)
2084 for (i
= 0; i
< bitmap_num_objects(bitmap_git
); i
++) {
2085 if (bitmap_is_midx(bitmap_git
))
2086 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
2088 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
2090 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
2092 printf_ln("%s %"PRIu32
"",
2093 oid_to_hex(&oid
), get_be32(bitmap_git
->hashes
+ index_pos
));
2097 free_bitmap_index(bitmap_git
);
2102 int rebuild_bitmap(const uint32_t *reposition
,
2103 struct ewah_bitmap
*source
,
2104 struct bitmap
*dest
)
2107 struct ewah_iterator it
;
2110 ewah_iterator_init(&it
, source
);
2112 while (ewah_iterator_next(&word
, &it
)) {
2113 uint32_t offset
, bit_pos
;
2115 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
2116 if ((word
>> offset
) == 0)
2119 offset
+= ewah_bit_ctz64(word
>> offset
);
2121 bit_pos
= reposition
[pos
+ offset
];
2123 bitmap_set(dest
, bit_pos
- 1);
2124 else /* can't reuse, we don't have the object */
2128 pos
+= BITS_IN_EWORD
;
2133 uint32_t *create_bitmap_mapping(struct bitmap_index
*bitmap_git
,
2134 struct packing_data
*mapping
)
2136 uint32_t i
, num_objects
;
2137 uint32_t *reposition
;
2139 if (!bitmap_is_midx(bitmap_git
))
2140 load_reverse_index(bitmap_git
);
2141 else if (load_midx_revindex(bitmap_git
->midx
) < 0)
2142 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2145 num_objects
= bitmap_num_objects(bitmap_git
);
2146 CALLOC_ARRAY(reposition
, num_objects
);
2148 for (i
= 0; i
< num_objects
; ++i
) {
2149 struct object_id oid
;
2150 struct object_entry
*oe
;
2153 if (bitmap_is_midx(bitmap_git
))
2154 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
2156 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
2157 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
2158 oe
= packlist_find(mapping
, &oid
);
2161 reposition
[i
] = oe_in_pack_pos(mapping
, oe
) + 1;
2162 if (bitmap_git
->hashes
&& !oe
->hash
)
2163 oe
->hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
2170 void free_bitmap_index(struct bitmap_index
*b
)
2176 munmap(b
->map
, b
->map_size
);
2177 ewah_pool_free(b
->commits
);
2178 ewah_pool_free(b
->trees
);
2179 ewah_pool_free(b
->blobs
);
2180 ewah_pool_free(b
->tags
);
2182 struct stored_bitmap
*sb
;
2183 kh_foreach_value(b
->bitmaps
, sb
, {
2184 ewah_pool_free(sb
->root
);
2188 kh_destroy_oid_map(b
->bitmaps
);
2189 free(b
->ext_index
.objects
);
2190 free(b
->ext_index
.hashes
);
2191 kh_destroy_oid_pos(b
->ext_index
.positions
);
2192 bitmap_free(b
->result
);
2193 bitmap_free(b
->haves
);
2194 if (bitmap_is_midx(b
)) {
2196 * Multi-pack bitmaps need to have resources associated with
2197 * their on-disk reverse indexes unmapped so that stale .rev and
2198 * .bitmap files can be removed.
2200 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2201 * written in the same 'git multi-pack-index write --bitmap'
2202 * process. Close resources so they can be removed safely on
2203 * platforms like Windows.
2205 close_midx_revindex(b
->midx
);
2210 int bitmap_has_oid_in_uninteresting(struct bitmap_index
*bitmap_git
,
2211 const struct object_id
*oid
)
2213 return bitmap_git
&&
2214 bitmap_walk_contains(bitmap_git
, bitmap_git
->haves
, oid
);
2217 static off_t
get_disk_usage_for_type(struct bitmap_index
*bitmap_git
,
2218 enum object_type object_type
)
2220 struct bitmap
*result
= bitmap_git
->result
;
2222 struct ewah_iterator it
;
2226 init_type_iterator(&it
, bitmap_git
, object_type
);
2227 for (i
= 0; i
< result
->word_alloc
&&
2228 ewah_iterator_next(&filter
, &it
); i
++) {
2229 eword_t word
= result
->words
[i
] & filter
;
2230 size_t base
= (i
* BITS_IN_EWORD
);
2236 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
2237 if ((word
>> offset
) == 0)
2240 offset
+= ewah_bit_ctz64(word
>> offset
);
2242 if (bitmap_is_midx(bitmap_git
)) {
2244 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, base
+ offset
);
2245 off_t offset
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
2247 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
2248 struct packed_git
*pack
= bitmap_git
->midx
->packs
[pack_id
];
2250 if (offset_to_pack_pos(pack
, offset
, &pack_pos
) < 0) {
2251 struct object_id oid
;
2252 nth_midxed_object_oid(&oid
, bitmap_git
->midx
, midx_pos
);
2254 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX
),
2260 total
+= pack_pos_to_offset(pack
, pack_pos
+ 1) - offset
;
2262 size_t pos
= base
+ offset
;
2263 total
+= pack_pos_to_offset(bitmap_git
->pack
, pos
+ 1) -
2264 pack_pos_to_offset(bitmap_git
->pack
, pos
);
2272 static off_t
get_disk_usage_for_extended(struct bitmap_index
*bitmap_git
)
2274 struct bitmap
*result
= bitmap_git
->result
;
2275 struct eindex
*eindex
= &bitmap_git
->ext_index
;
2277 struct object_info oi
= OBJECT_INFO_INIT
;
2281 oi
.disk_sizep
= &object_size
;
2283 for (i
= 0; i
< eindex
->count
; i
++) {
2284 struct object
*obj
= eindex
->objects
[i
];
2286 if (!bitmap_get(result
, bitmap_num_objects(bitmap_git
) + i
))
2289 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
2290 die(_("unable to get disk usage of '%s'"),
2291 oid_to_hex(&obj
->oid
));
2293 total
+= object_size
;
2298 off_t
get_disk_usage_from_bitmap(struct bitmap_index
*bitmap_git
,
2299 struct rev_info
*revs
)
2303 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_COMMIT
);
2304 if (revs
->tree_objects
)
2305 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TREE
);
2306 if (revs
->blob_objects
)
2307 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_BLOB
);
2308 if (revs
->tag_objects
)
2309 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TAG
);
2311 total
+= get_disk_usage_for_extended(bitmap_git
);
2316 int bitmap_is_midx(struct bitmap_index
*bitmap_git
)
2318 return !!bitmap_git
->midx
;
2321 const struct string_list
*bitmap_preferred_tips(struct repository
*r
)
2323 return repo_config_get_value_multi(r
, "pack.preferbitmaptips");
2326 int bitmap_is_preferred_refname(struct repository
*r
, const char *refname
)
2328 const struct string_list
*preferred_tips
= bitmap_preferred_tips(r
);
2329 struct string_list_item
*item
;
2331 if (!preferred_tips
)
2334 for_each_string_list_item(item
, preferred_tips
) {
2335 if (starts_with(refname
, item
->string
))