7 #include "list-objects.h"
9 #include "pack-bitmap.h"
10 #include "pack-revindex.h"
11 #include "pack-objects.h"
13 #include "repository.h"
14 #include "object-store.h"
15 #include "list-objects-filter-options.h"
20 * An entry on the bitmap index, representing the bitmap for a given
23 struct stored_bitmap
{
25 struct ewah_bitmap
*root
;
26 struct stored_bitmap
*xor;
31 * The active bitmap index for a repository. By design, repositories only have
32 * a single bitmap index available (the index for the biggest packfile in
33 * the repository), since bitmap indexes need full closure.
35 * If there is more than one bitmap index available (e.g. because of alternates),
36 * the active bitmap index is the largest one.
40 * The pack or multi-pack index (MIDX) that this bitmap index belongs
43 * Exactly one of these must be non-NULL; this specifies the object
44 * order used to interpret this bitmap.
46 struct packed_git
*pack
;
47 struct multi_pack_index
*midx
;
50 * Mark the first `reuse_objects` in the packfile as reused:
51 * they will be sent as-is without using them for repacking
54 uint32_t reuse_objects
;
56 /* mmapped buffer of the whole bitmap index */
58 size_t map_size
; /* size of the mmaped buffer */
59 size_t map_pos
; /* current position when loading the index */
64 * Each bitmap marks which objects in the packfile are of the given
65 * type. This provides type information when yielding the objects from
66 * the packfile during a walk, which allows for better delta bases.
68 struct ewah_bitmap
*commits
;
69 struct ewah_bitmap
*trees
;
70 struct ewah_bitmap
*blobs
;
71 struct ewah_bitmap
*tags
;
73 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
74 kh_oid_map_t
*bitmaps
;
76 /* Number of bitmapped commits */
79 /* If not NULL, this is a name-hash cache pointing into map. */
82 /* The checksum of the packfile or MIDX; points into map. */
83 const unsigned char *checksum
;
88 * When trying to perform bitmap operations with objects that are not
89 * packed in `pack`, these objects are added to this "fake index" and
90 * are assumed to appear at the end of the packfile for all operations
93 struct object
**objects
;
95 uint32_t count
, alloc
;
96 kh_oid_pos_t
*positions
;
99 /* Bitmap result of the last performed walk */
100 struct bitmap
*result
;
102 /* "have" bitmap from the last performed walk */
103 struct bitmap
*haves
;
105 /* Version of the bitmap index */
106 unsigned int version
;
109 static struct ewah_bitmap
*lookup_stored_bitmap(struct stored_bitmap
*st
)
111 struct ewah_bitmap
*parent
;
112 struct ewah_bitmap
*composed
;
117 composed
= ewah_pool_new();
118 parent
= lookup_stored_bitmap(st
->xor);
119 ewah_xor(st
->root
, parent
, composed
);
121 ewah_pool_free(st
->root
);
129 * Read a bitmap from the current read position on the mmaped
130 * index, and increase the read position accordingly
132 static struct ewah_bitmap
*read_bitmap_1(struct bitmap_index
*index
)
134 struct ewah_bitmap
*b
= ewah_pool_new();
136 ssize_t bitmap_size
= ewah_read_mmap(b
,
137 index
->map
+ index
->map_pos
,
138 index
->map_size
- index
->map_pos
);
140 if (bitmap_size
< 0) {
141 error("Failed to load bitmap index (corrupted?)");
146 index
->map_pos
+= bitmap_size
;
150 static uint32_t bitmap_num_objects(struct bitmap_index
*index
)
153 return index
->midx
->num_objects
;
154 return index
->pack
->num_objects
;
157 static int load_bitmap_header(struct bitmap_index
*index
)
159 struct bitmap_disk_header
*header
= (void *)index
->map
;
160 size_t header_size
= sizeof(*header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
;
162 if (index
->map_size
< header_size
+ the_hash_algo
->rawsz
)
163 return error("Corrupted bitmap index (too small)");
165 if (memcmp(header
->magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
)) != 0)
166 return error("Corrupted bitmap index file (wrong header)");
168 index
->version
= ntohs(header
->version
);
169 if (index
->version
!= 1)
170 return error("Unsupported version for bitmap index file (%d)", index
->version
);
172 /* Parse known bitmap format options */
174 uint32_t flags
= ntohs(header
->options
);
175 size_t cache_size
= st_mult(bitmap_num_objects(index
), sizeof(uint32_t));
176 unsigned char *index_end
= index
->map
+ index
->map_size
- the_hash_algo
->rawsz
;
178 if ((flags
& BITMAP_OPT_FULL_DAG
) == 0)
179 return error("Unsupported options for bitmap index file "
180 "(Git requires BITMAP_OPT_FULL_DAG)");
182 if (flags
& BITMAP_OPT_HASH_CACHE
) {
183 if (cache_size
> index_end
- index
->map
- header_size
)
184 return error("corrupted bitmap index file (too short to fit hash cache)");
185 index
->hashes
= (void *)(index_end
- cache_size
);
186 index_end
-= cache_size
;
190 index
->entry_count
= ntohl(header
->entry_count
);
191 index
->checksum
= header
->checksum
;
192 index
->map_pos
+= header_size
;
196 static struct stored_bitmap
*store_bitmap(struct bitmap_index
*index
,
197 struct ewah_bitmap
*root
,
198 const struct object_id
*oid
,
199 struct stored_bitmap
*xor_with
,
202 struct stored_bitmap
*stored
;
206 stored
= xmalloc(sizeof(struct stored_bitmap
));
208 stored
->xor = xor_with
;
209 stored
->flags
= flags
;
210 oidcpy(&stored
->oid
, oid
);
212 hash_pos
= kh_put_oid_map(index
->bitmaps
, stored
->oid
, &ret
);
214 /* a 0 return code means the insertion succeeded with no changes,
215 * because the SHA1 already existed on the map. this is bad, there
216 * shouldn't be duplicated commits in the index */
218 error("Duplicate entry in bitmap index: %s", oid_to_hex(oid
));
222 kh_value(index
->bitmaps
, hash_pos
) = stored
;
226 static inline uint32_t read_be32(const unsigned char *buffer
, size_t *pos
)
228 uint32_t result
= get_be32(buffer
+ *pos
);
229 (*pos
) += sizeof(result
);
233 static inline uint8_t read_u8(const unsigned char *buffer
, size_t *pos
)
235 return buffer
[(*pos
)++];
238 #define MAX_XOR_OFFSET 160
240 static int nth_bitmap_object_oid(struct bitmap_index
*index
,
241 struct object_id
*oid
,
245 return nth_midxed_object_oid(oid
, index
->midx
, n
) ? 0 : -1;
246 return nth_packed_object_id(oid
, index
->pack
, n
);
249 static int load_bitmap_entries_v1(struct bitmap_index
*index
)
252 struct stored_bitmap
*recent_bitmaps
[MAX_XOR_OFFSET
] = { NULL
};
254 for (i
= 0; i
< index
->entry_count
; ++i
) {
255 int xor_offset
, flags
;
256 struct ewah_bitmap
*bitmap
= NULL
;
257 struct stored_bitmap
*xor_bitmap
= NULL
;
258 uint32_t commit_idx_pos
;
259 struct object_id oid
;
261 if (index
->map_size
- index
->map_pos
< 6)
262 return error("corrupt ewah bitmap: truncated header for entry %d", i
);
264 commit_idx_pos
= read_be32(index
->map
, &index
->map_pos
);
265 xor_offset
= read_u8(index
->map
, &index
->map_pos
);
266 flags
= read_u8(index
->map
, &index
->map_pos
);
268 if (nth_bitmap_object_oid(index
, &oid
, commit_idx_pos
) < 0)
269 return error("corrupt ewah bitmap: commit index %u out of range",
270 (unsigned)commit_idx_pos
);
272 bitmap
= read_bitmap_1(index
);
276 if (xor_offset
> MAX_XOR_OFFSET
|| xor_offset
> i
)
277 return error("Corrupted bitmap pack index");
279 if (xor_offset
> 0) {
280 xor_bitmap
= recent_bitmaps
[(i
- xor_offset
) % MAX_XOR_OFFSET
];
283 return error("Invalid XOR offset in bitmap pack index");
286 recent_bitmaps
[i
% MAX_XOR_OFFSET
] = store_bitmap(
287 index
, bitmap
, &oid
, xor_bitmap
, flags
);
293 char *midx_bitmap_filename(struct multi_pack_index
*midx
)
295 struct strbuf buf
= STRBUF_INIT
;
297 get_midx_filename(&buf
, midx
->object_dir
);
298 strbuf_addf(&buf
, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx
)));
300 return strbuf_detach(&buf
, NULL
);
303 char *pack_bitmap_filename(struct packed_git
*p
)
307 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
308 BUG("pack_name does not end in .pack");
309 return xstrfmt("%.*s.bitmap", (int)len
, p
->pack_name
);
312 static int open_midx_bitmap_1(struct bitmap_index
*bitmap_git
,
313 struct multi_pack_index
*midx
)
316 char *idx_name
= midx_bitmap_filename(midx
);
317 int fd
= git_open(idx_name
);
319 struct packed_git
*preferred
;
326 if (fstat(fd
, &st
)) {
331 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
332 struct strbuf buf
= STRBUF_INIT
;
333 get_midx_filename(&buf
, midx
->object_dir
);
334 /* ignore extra bitmap file; we can only handle one */
335 warning("ignoring extra bitmap file: %s", buf
.buf
);
337 strbuf_release(&buf
);
341 bitmap_git
->midx
= midx
;
342 bitmap_git
->map_size
= xsize_t(st
.st_size
);
343 bitmap_git
->map_pos
= 0;
344 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
,
348 if (load_bitmap_header(bitmap_git
) < 0)
351 if (!hasheq(get_midx_checksum(bitmap_git
->midx
), bitmap_git
->checksum
))
354 if (load_midx_revindex(bitmap_git
->midx
) < 0) {
355 warning(_("multi-pack bitmap is missing required reverse index"));
359 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
360 if (prepare_midx_pack(the_repository
, bitmap_git
->midx
, i
))
361 die(_("could not open pack %s"),
362 bitmap_git
->midx
->pack_names
[i
]);
365 preferred
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
366 if (!is_pack_valid(preferred
)) {
367 warning(_("preferred pack (%s) is invalid"),
368 preferred
->pack_name
);
375 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
376 bitmap_git
->map_size
= 0;
377 bitmap_git
->map_pos
= 0;
378 bitmap_git
->map
= NULL
;
379 bitmap_git
->midx
= NULL
;
383 static int open_pack_bitmap_1(struct bitmap_index
*bitmap_git
, struct packed_git
*packfile
)
389 if (open_pack_index(packfile
))
392 idx_name
= pack_bitmap_filename(packfile
);
393 fd
= git_open(idx_name
);
399 if (fstat(fd
, &st
)) {
404 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
405 /* ignore extra bitmap file; we can only handle one */
406 warning("ignoring extra bitmap file: %s", packfile
->pack_name
);
411 if (!is_pack_valid(packfile
)) {
416 bitmap_git
->pack
= packfile
;
417 bitmap_git
->map_size
= xsize_t(st
.st_size
);
418 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
419 bitmap_git
->map_pos
= 0;
422 if (load_bitmap_header(bitmap_git
) < 0) {
423 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
424 bitmap_git
->map
= NULL
;
425 bitmap_git
->map_size
= 0;
426 bitmap_git
->map_pos
= 0;
427 bitmap_git
->pack
= NULL
;
434 static int load_reverse_index(struct bitmap_index
*bitmap_git
)
436 if (bitmap_is_midx(bitmap_git
)) {
441 * The multi-pack-index's .rev file is already loaded via
442 * open_pack_bitmap_1().
444 * But we still need to open the individual pack .rev files,
445 * since we will need to make use of them in pack-objects.
447 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
448 ret
= load_pack_revindex(bitmap_git
->midx
->packs
[i
]);
454 return load_pack_revindex(bitmap_git
->pack
);
457 static int load_bitmap(struct bitmap_index
*bitmap_git
)
459 assert(bitmap_git
->map
);
461 bitmap_git
->bitmaps
= kh_init_oid_map();
462 bitmap_git
->ext_index
.positions
= kh_init_oid_pos();
464 if (load_reverse_index(bitmap_git
))
467 if (!(bitmap_git
->commits
= read_bitmap_1(bitmap_git
)) ||
468 !(bitmap_git
->trees
= read_bitmap_1(bitmap_git
)) ||
469 !(bitmap_git
->blobs
= read_bitmap_1(bitmap_git
)) ||
470 !(bitmap_git
->tags
= read_bitmap_1(bitmap_git
)))
473 if (load_bitmap_entries_v1(bitmap_git
) < 0)
479 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
480 bitmap_git
->map
= NULL
;
481 bitmap_git
->map_size
= 0;
483 kh_destroy_oid_map(bitmap_git
->bitmaps
);
484 bitmap_git
->bitmaps
= NULL
;
486 kh_destroy_oid_pos(bitmap_git
->ext_index
.positions
);
487 bitmap_git
->ext_index
.positions
= NULL
;
492 static int open_pack_bitmap(struct repository
*r
,
493 struct bitmap_index
*bitmap_git
)
495 struct packed_git
*p
;
498 assert(!bitmap_git
->map
);
500 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
501 if (open_pack_bitmap_1(bitmap_git
, p
) == 0)
508 static int open_midx_bitmap(struct repository
*r
,
509 struct bitmap_index
*bitmap_git
)
511 struct multi_pack_index
*midx
;
513 assert(!bitmap_git
->map
);
515 for (midx
= get_multi_pack_index(r
); midx
; midx
= midx
->next
) {
516 if (!open_midx_bitmap_1(bitmap_git
, midx
))
522 static int open_bitmap(struct repository
*r
,
523 struct bitmap_index
*bitmap_git
)
525 assert(!bitmap_git
->map
);
527 if (!open_midx_bitmap(r
, bitmap_git
))
529 return open_pack_bitmap(r
, bitmap_git
);
532 struct bitmap_index
*prepare_bitmap_git(struct repository
*r
)
534 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
536 if (!open_bitmap(r
, bitmap_git
) && !load_bitmap(bitmap_git
))
539 free_bitmap_index(bitmap_git
);
543 struct bitmap_index
*prepare_midx_bitmap_git(struct multi_pack_index
*midx
)
545 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
547 if (!open_midx_bitmap_1(bitmap_git
, midx
) && !load_bitmap(bitmap_git
))
550 free_bitmap_index(bitmap_git
);
554 struct include_data
{
555 struct bitmap_index
*bitmap_git
;
560 struct ewah_bitmap
*bitmap_for_commit(struct bitmap_index
*bitmap_git
,
561 struct commit
*commit
)
563 khiter_t hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
,
565 if (hash_pos
>= kh_end(bitmap_git
->bitmaps
))
567 return lookup_stored_bitmap(kh_value(bitmap_git
->bitmaps
, hash_pos
));
570 static inline int bitmap_position_extended(struct bitmap_index
*bitmap_git
,
571 const struct object_id
*oid
)
573 kh_oid_pos_t
*positions
= bitmap_git
->ext_index
.positions
;
574 khiter_t pos
= kh_get_oid_pos(positions
, *oid
);
576 if (pos
< kh_end(positions
)) {
577 int bitmap_pos
= kh_value(positions
, pos
);
578 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
584 static inline int bitmap_position_packfile(struct bitmap_index
*bitmap_git
,
585 const struct object_id
*oid
)
588 off_t offset
= find_pack_entry_one(oid
->hash
, bitmap_git
->pack
);
592 if (offset_to_pack_pos(bitmap_git
->pack
, offset
, &pos
) < 0)
597 static int bitmap_position_midx(struct bitmap_index
*bitmap_git
,
598 const struct object_id
*oid
)
601 if (!bsearch_midx(oid
, bitmap_git
->midx
, &want
))
604 if (midx_to_pack_pos(bitmap_git
->midx
, want
, &got
) < 0)
609 static int bitmap_position(struct bitmap_index
*bitmap_git
,
610 const struct object_id
*oid
)
613 if (bitmap_is_midx(bitmap_git
))
614 pos
= bitmap_position_midx(bitmap_git
, oid
);
616 pos
= bitmap_position_packfile(bitmap_git
, oid
);
617 return (pos
>= 0) ? pos
: bitmap_position_extended(bitmap_git
, oid
);
620 static int ext_index_add_object(struct bitmap_index
*bitmap_git
,
621 struct object
*object
, const char *name
)
623 struct eindex
*eindex
= &bitmap_git
->ext_index
;
629 hash_pos
= kh_put_oid_pos(eindex
->positions
, object
->oid
, &hash_ret
);
631 if (eindex
->count
>= eindex
->alloc
) {
632 eindex
->alloc
= (eindex
->alloc
+ 16) * 3 / 2;
633 REALLOC_ARRAY(eindex
->objects
, eindex
->alloc
);
634 REALLOC_ARRAY(eindex
->hashes
, eindex
->alloc
);
637 bitmap_pos
= eindex
->count
;
638 eindex
->objects
[eindex
->count
] = object
;
639 eindex
->hashes
[eindex
->count
] = pack_name_hash(name
);
640 kh_value(eindex
->positions
, hash_pos
) = bitmap_pos
;
643 bitmap_pos
= kh_value(eindex
->positions
, hash_pos
);
646 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
649 struct bitmap_show_data
{
650 struct bitmap_index
*bitmap_git
;
654 static void show_object(struct object
*object
, const char *name
, void *data_
)
656 struct bitmap_show_data
*data
= data_
;
659 bitmap_pos
= bitmap_position(data
->bitmap_git
, &object
->oid
);
662 bitmap_pos
= ext_index_add_object(data
->bitmap_git
, object
,
665 bitmap_set(data
->base
, bitmap_pos
);
668 static void show_commit(struct commit
*commit
, void *data
)
672 static int add_to_include_set(struct bitmap_index
*bitmap_git
,
673 struct include_data
*data
,
674 struct commit
*commit
,
677 struct ewah_bitmap
*partial
;
679 if (data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
))
682 if (bitmap_get(data
->base
, bitmap_pos
))
685 partial
= bitmap_for_commit(bitmap_git
, commit
);
687 bitmap_or_ewah(data
->base
, partial
);
691 bitmap_set(data
->base
, bitmap_pos
);
695 static int should_include(struct commit
*commit
, void *_data
)
697 struct include_data
*data
= _data
;
700 bitmap_pos
= bitmap_position(data
->bitmap_git
, &commit
->object
.oid
);
702 bitmap_pos
= ext_index_add_object(data
->bitmap_git
,
703 (struct object
*)commit
,
706 if (!add_to_include_set(data
->bitmap_git
, data
, commit
, bitmap_pos
)) {
707 struct commit_list
*parent
= commit
->parents
;
710 parent
->item
->object
.flags
|= SEEN
;
711 parent
= parent
->next
;
720 static int should_include_obj(struct object
*obj
, void *_data
)
722 struct include_data
*data
= _data
;
725 bitmap_pos
= bitmap_position(data
->bitmap_git
, &obj
->oid
);
728 if ((data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
)) ||
729 bitmap_get(data
->base
, bitmap_pos
)) {
736 static int add_commit_to_bitmap(struct bitmap_index
*bitmap_git
,
737 struct bitmap
**base
,
738 struct commit
*commit
)
740 struct ewah_bitmap
*or_with
= bitmap_for_commit(bitmap_git
, commit
);
746 *base
= ewah_to_bitmap(or_with
);
748 bitmap_or_ewah(*base
, or_with
);
753 static struct bitmap
*find_objects(struct bitmap_index
*bitmap_git
,
754 struct rev_info
*revs
,
755 struct object_list
*roots
,
758 struct bitmap
*base
= NULL
;
761 struct object_list
*not_mapped
= NULL
;
764 * Go through all the roots for the walk. The ones that have bitmaps
765 * on the bitmap index will be `or`ed together to form an initial
766 * global reachability analysis.
768 * The ones without bitmaps in the index will be stored in the
769 * `not_mapped_list` for further processing.
772 struct object
*object
= roots
->item
;
775 if (object
->type
== OBJ_COMMIT
&&
776 add_commit_to_bitmap(bitmap_git
, &base
, (struct commit
*)object
)) {
777 object
->flags
|= SEEN
;
781 object_list_insert(object
, ¬_mapped
);
785 * Best case scenario: We found bitmaps for all the roots,
786 * so the resulting `or` bitmap has the full reachability analysis
794 * Let's iterate through all the roots that don't have bitmaps to
795 * check if we can determine them to be reachable from the existing
798 * If we cannot find them in the existing global bitmap, we'll need
799 * to push them to an actual walk and run it until we can confirm
803 struct object
*object
= roots
->item
;
807 pos
= bitmap_position(bitmap_git
, &object
->oid
);
809 if (pos
< 0 || base
== NULL
|| !bitmap_get(base
, pos
)) {
810 object
->flags
&= ~UNINTERESTING
;
811 add_pending_object(revs
, object
, "");
814 object
->flags
|= SEEN
;
819 struct include_data incdata
;
820 struct bitmap_show_data show_data
;
825 incdata
.bitmap_git
= bitmap_git
;
829 revs
->include_check
= should_include
;
830 revs
->include_check_obj
= should_include_obj
;
831 revs
->include_check_data
= &incdata
;
833 if (prepare_revision_walk(revs
))
834 die("revision walk setup failed");
836 show_data
.bitmap_git
= bitmap_git
;
837 show_data
.base
= base
;
839 traverse_commit_list(revs
,
840 show_commit
, show_object
,
843 revs
->include_check
= NULL
;
844 revs
->include_check_obj
= NULL
;
845 revs
->include_check_data
= NULL
;
851 static void show_extended_objects(struct bitmap_index
*bitmap_git
,
852 struct rev_info
*revs
,
853 show_reachable_fn show_reach
)
855 struct bitmap
*objects
= bitmap_git
->result
;
856 struct eindex
*eindex
= &bitmap_git
->ext_index
;
859 for (i
= 0; i
< eindex
->count
; ++i
) {
862 if (!bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
865 obj
= eindex
->objects
[i
];
866 if ((obj
->type
== OBJ_BLOB
&& !revs
->blob_objects
) ||
867 (obj
->type
== OBJ_TREE
&& !revs
->tree_objects
) ||
868 (obj
->type
== OBJ_TAG
&& !revs
->tag_objects
))
871 show_reach(&obj
->oid
, obj
->type
, 0, eindex
->hashes
[i
], NULL
, 0);
875 static void init_type_iterator(struct ewah_iterator
*it
,
876 struct bitmap_index
*bitmap_git
,
877 enum object_type type
)
881 ewah_iterator_init(it
, bitmap_git
->commits
);
885 ewah_iterator_init(it
, bitmap_git
->trees
);
889 ewah_iterator_init(it
, bitmap_git
->blobs
);
893 ewah_iterator_init(it
, bitmap_git
->tags
);
897 BUG("object type %d not stored by bitmap type index", type
);
902 static void show_objects_for_type(
903 struct bitmap_index
*bitmap_git
,
904 enum object_type object_type
,
905 show_reachable_fn show_reach
)
910 struct ewah_iterator it
;
913 struct bitmap
*objects
= bitmap_git
->result
;
915 init_type_iterator(&it
, bitmap_git
, object_type
);
917 for (i
= 0; i
< objects
->word_alloc
&&
918 ewah_iterator_next(&filter
, &it
); i
++) {
919 eword_t word
= objects
->words
[i
] & filter
;
920 size_t pos
= (i
* BITS_IN_EWORD
);
925 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
926 struct packed_git
*pack
;
927 struct object_id oid
;
928 uint32_t hash
= 0, index_pos
;
931 if ((word
>> offset
) == 0)
934 offset
+= ewah_bit_ctz64(word
>> offset
);
936 if (bitmap_is_midx(bitmap_git
)) {
937 struct multi_pack_index
*m
= bitmap_git
->midx
;
940 index_pos
= pack_pos_to_midx(m
, pos
+ offset
);
941 ofs
= nth_midxed_offset(m
, index_pos
);
942 nth_midxed_object_oid(&oid
, m
, index_pos
);
944 pack_id
= nth_midxed_pack_int_id(m
, index_pos
);
945 pack
= bitmap_git
->midx
->packs
[pack_id
];
947 index_pos
= pack_pos_to_index(bitmap_git
->pack
, pos
+ offset
);
948 ofs
= pack_pos_to_offset(bitmap_git
->pack
, pos
+ offset
);
949 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
951 pack
= bitmap_git
->pack
;
954 if (bitmap_git
->hashes
)
955 hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
957 show_reach(&oid
, object_type
, 0, hash
, pack
, ofs
);
962 static int in_bitmapped_pack(struct bitmap_index
*bitmap_git
,
963 struct object_list
*roots
)
966 struct object
*object
= roots
->item
;
969 if (bitmap_is_midx(bitmap_git
)) {
970 if (bsearch_midx(&object
->oid
, bitmap_git
->midx
, NULL
))
973 if (find_pack_entry_one(object
->oid
.hash
, bitmap_git
->pack
) > 0)
981 static struct bitmap
*find_tip_objects(struct bitmap_index
*bitmap_git
,
982 struct object_list
*tip_objects
,
983 enum object_type type
)
985 struct bitmap
*result
= bitmap_new();
986 struct object_list
*p
;
988 for (p
= tip_objects
; p
; p
= p
->next
) {
991 if (p
->item
->type
!= type
)
994 pos
= bitmap_position(bitmap_git
, &p
->item
->oid
);
998 bitmap_set(result
, pos
);
1004 static void filter_bitmap_exclude_type(struct bitmap_index
*bitmap_git
,
1005 struct object_list
*tip_objects
,
1006 struct bitmap
*to_filter
,
1007 enum object_type type
)
1009 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1010 struct bitmap
*tips
;
1011 struct ewah_iterator it
;
1016 * The non-bitmap version of this filter never removes
1017 * objects which the other side specifically asked for,
1018 * so we must match that behavior.
1020 tips
= find_tip_objects(bitmap_git
, tip_objects
, type
);
1023 * We can use the type-level bitmap for 'type' to work in whole
1024 * words for the objects that are actually in the bitmapped
1027 for (i
= 0, init_type_iterator(&it
, bitmap_git
, type
);
1028 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1030 if (i
< tips
->word_alloc
)
1031 mask
&= ~tips
->words
[i
];
1032 to_filter
->words
[i
] &= ~mask
;
1036 * Clear any objects that weren't in the packfile (and so would
1037 * not have been caught by the loop above. We'll have to check
1038 * them individually.
1040 for (i
= 0; i
< eindex
->count
; i
++) {
1041 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1042 if (eindex
->objects
[i
]->type
== type
&&
1043 bitmap_get(to_filter
, pos
) &&
1044 !bitmap_get(tips
, pos
))
1045 bitmap_unset(to_filter
, pos
);
1051 static void filter_bitmap_blob_none(struct bitmap_index
*bitmap_git
,
1052 struct object_list
*tip_objects
,
1053 struct bitmap
*to_filter
)
1055 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1059 static unsigned long get_size_by_pos(struct bitmap_index
*bitmap_git
,
1063 struct object_info oi
= OBJECT_INFO_INIT
;
1067 if (pos
< bitmap_num_objects(bitmap_git
)) {
1068 struct packed_git
*pack
;
1071 if (bitmap_is_midx(bitmap_git
)) {
1072 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, pos
);
1073 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
1075 pack
= bitmap_git
->midx
->packs
[pack_id
];
1076 ofs
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
1078 pack
= bitmap_git
->pack
;
1079 ofs
= pack_pos_to_offset(pack
, pos
);
1082 if (packed_object_info(the_repository
, pack
, ofs
, &oi
) < 0) {
1083 struct object_id oid
;
1084 nth_bitmap_object_oid(bitmap_git
, &oid
,
1085 pack_pos_to_index(pack
, pos
));
1086 die(_("unable to get size of %s"), oid_to_hex(&oid
));
1089 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1090 struct object
*obj
= eindex
->objects
[pos
- bitmap_num_objects(bitmap_git
)];
1091 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
1092 die(_("unable to get size of %s"), oid_to_hex(&obj
->oid
));
1098 static void filter_bitmap_blob_limit(struct bitmap_index
*bitmap_git
,
1099 struct object_list
*tip_objects
,
1100 struct bitmap
*to_filter
,
1101 unsigned long limit
)
1103 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1104 struct bitmap
*tips
;
1105 struct ewah_iterator it
;
1109 tips
= find_tip_objects(bitmap_git
, tip_objects
, OBJ_BLOB
);
1111 for (i
= 0, init_type_iterator(&it
, bitmap_git
, OBJ_BLOB
);
1112 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1114 eword_t word
= to_filter
->words
[i
] & mask
;
1117 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1120 if ((word
>> offset
) == 0)
1122 offset
+= ewah_bit_ctz64(word
>> offset
);
1123 pos
= i
* BITS_IN_EWORD
+ offset
;
1125 if (!bitmap_get(tips
, pos
) &&
1126 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1127 bitmap_unset(to_filter
, pos
);
1131 for (i
= 0; i
< eindex
->count
; i
++) {
1132 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1133 if (eindex
->objects
[i
]->type
== OBJ_BLOB
&&
1134 bitmap_get(to_filter
, pos
) &&
1135 !bitmap_get(tips
, pos
) &&
1136 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1137 bitmap_unset(to_filter
, pos
);
1143 static void filter_bitmap_tree_depth(struct bitmap_index
*bitmap_git
,
1144 struct object_list
*tip_objects
,
1145 struct bitmap
*to_filter
,
1146 unsigned long limit
)
1149 BUG("filter_bitmap_tree_depth given non-zero limit");
1151 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1153 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1157 static void filter_bitmap_object_type(struct bitmap_index
*bitmap_git
,
1158 struct object_list
*tip_objects
,
1159 struct bitmap
*to_filter
,
1160 enum object_type object_type
)
1162 if (object_type
< OBJ_COMMIT
|| object_type
> OBJ_TAG
)
1163 BUG("filter_bitmap_object_type given invalid object");
1165 if (object_type
!= OBJ_TAG
)
1166 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TAG
);
1167 if (object_type
!= OBJ_COMMIT
)
1168 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_COMMIT
);
1169 if (object_type
!= OBJ_TREE
)
1170 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TREE
);
1171 if (object_type
!= OBJ_BLOB
)
1172 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_BLOB
);
1175 static int filter_bitmap(struct bitmap_index
*bitmap_git
,
1176 struct object_list
*tip_objects
,
1177 struct bitmap
*to_filter
,
1178 struct list_objects_filter_options
*filter
)
1180 if (!filter
|| filter
->choice
== LOFC_DISABLED
)
1183 if (filter
->choice
== LOFC_BLOB_NONE
) {
1185 filter_bitmap_blob_none(bitmap_git
, tip_objects
,
1190 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
1192 filter_bitmap_blob_limit(bitmap_git
, tip_objects
,
1194 filter
->blob_limit_value
);
1198 if (filter
->choice
== LOFC_TREE_DEPTH
&&
1199 filter
->tree_exclude_depth
== 0) {
1201 filter_bitmap_tree_depth(bitmap_git
, tip_objects
,
1203 filter
->tree_exclude_depth
);
1207 if (filter
->choice
== LOFC_OBJECT_TYPE
) {
1209 filter_bitmap_object_type(bitmap_git
, tip_objects
,
1211 filter
->object_type
);
1215 if (filter
->choice
== LOFC_COMBINE
) {
1217 for (i
= 0; i
< filter
->sub_nr
; i
++) {
1218 if (filter_bitmap(bitmap_git
, tip_objects
, to_filter
,
1219 &filter
->sub
[i
]) < 0)
1225 /* filter choice not handled */
1229 static int can_filter_bitmap(struct list_objects_filter_options
*filter
)
1231 return !filter_bitmap(NULL
, NULL
, NULL
, filter
);
1234 struct bitmap_index
*prepare_bitmap_walk(struct rev_info
*revs
,
1235 int filter_provided_objects
)
1239 struct object_list
*wants
= NULL
;
1240 struct object_list
*haves
= NULL
;
1242 struct bitmap
*wants_bitmap
= NULL
;
1243 struct bitmap
*haves_bitmap
= NULL
;
1245 struct bitmap_index
*bitmap_git
;
1248 * We can't do pathspec limiting with bitmaps, because we don't know
1249 * which commits are associated with which object changes (let alone
1250 * even which objects are associated with which paths).
1255 if (!can_filter_bitmap(&revs
->filter
))
1258 /* try to open a bitmapped pack, but don't parse it yet
1259 * because we may not need to use it */
1260 CALLOC_ARRAY(bitmap_git
, 1);
1261 if (open_bitmap(revs
->repo
, bitmap_git
) < 0)
1264 for (i
= 0; i
< revs
->pending
.nr
; ++i
) {
1265 struct object
*object
= revs
->pending
.objects
[i
].item
;
1267 if (object
->type
== OBJ_NONE
)
1268 parse_object_or_die(&object
->oid
, NULL
);
1270 while (object
->type
== OBJ_TAG
) {
1271 struct tag
*tag
= (struct tag
*) object
;
1273 if (object
->flags
& UNINTERESTING
)
1274 object_list_insert(object
, &haves
);
1276 object_list_insert(object
, &wants
);
1278 object
= parse_object_or_die(get_tagged_oid(tag
), NULL
);
1279 object
->flags
|= (tag
->object
.flags
& UNINTERESTING
);
1282 if (object
->flags
& UNINTERESTING
)
1283 object_list_insert(object
, &haves
);
1285 object_list_insert(object
, &wants
);
1289 * if we have a HAVES list, but none of those haves is contained
1290 * in the packfile that has a bitmap, we don't have anything to
1293 if (haves
&& !in_bitmapped_pack(bitmap_git
, haves
))
1296 /* if we don't want anything, we're done here */
1301 * now we're going to use bitmaps, so load the actual bitmap entries
1302 * from disk. this is the point of no return; after this the rev_list
1303 * becomes invalidated and we must perform the revwalk through bitmaps
1305 if (load_bitmap(bitmap_git
) < 0)
1308 object_array_clear(&revs
->pending
);
1311 revs
->ignore_missing_links
= 1;
1312 haves_bitmap
= find_objects(bitmap_git
, revs
, haves
, NULL
);
1313 reset_revision_walk();
1314 revs
->ignore_missing_links
= 0;
1317 BUG("failed to perform bitmap walk");
1320 wants_bitmap
= find_objects(bitmap_git
, revs
, wants
, haves_bitmap
);
1323 BUG("failed to perform bitmap walk");
1326 bitmap_and_not(wants_bitmap
, haves_bitmap
);
1328 filter_bitmap(bitmap_git
,
1329 (revs
->filter
.choice
&& filter_provided_objects
) ? NULL
: wants
,
1333 bitmap_git
->result
= wants_bitmap
;
1334 bitmap_git
->haves
= haves_bitmap
;
1336 object_list_free(&wants
);
1337 object_list_free(&haves
);
1342 free_bitmap_index(bitmap_git
);
1343 object_list_free(&wants
);
1344 object_list_free(&haves
);
1349 * -1 means "stop trying further objects"; 0 means we may or may not have
1350 * reused, but you can keep feeding bits.
1352 static int try_partial_reuse(struct packed_git
*pack
,
1354 struct bitmap
*reuse
,
1355 struct pack_window
**w_curs
)
1357 off_t offset
, delta_obj_offset
;
1358 enum object_type type
;
1362 * try_partial_reuse() is called either on (a) objects in the
1363 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1364 * objects in the preferred pack of a multi-pack bitmap.
1365 * Importantly, the latter can pretend as if only a single pack
1368 * - The first pack->num_objects bits of a MIDX bitmap are
1369 * reserved for the preferred pack, and
1371 * - Ties due to duplicate objects are always resolved in
1372 * favor of the preferred pack.
1374 * Therefore we do not need to ever ask the MIDX for its copy of
1375 * an object by OID, since it will always select it from the
1376 * preferred pack. Likewise, the selected copy of the base
1377 * object for any deltas will reside in the same pack.
1379 * This means that we can reuse pos when looking up the bit in
1380 * the reuse bitmap, too, since bits corresponding to the
1381 * preferred pack precede all bits from other packs.
1384 if (pos
>= pack
->num_objects
)
1385 return -1; /* not actually in the pack or MIDX preferred pack */
1387 offset
= delta_obj_offset
= pack_pos_to_offset(pack
, pos
);
1388 type
= unpack_object_header(pack
, w_curs
, &offset
, &size
);
1390 return -1; /* broken packfile, punt */
1392 if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
) {
1397 * Find the position of the base object so we can look it up
1398 * in our bitmaps. If we can't come up with an offset, or if
1399 * that offset is not in the revidx, the pack is corrupt.
1400 * There's nothing we can do, so just punt on this object,
1401 * and the normal slow path will complain about it in
1404 base_offset
= get_delta_base(pack
, w_curs
, &offset
, type
,
1408 if (offset_to_pack_pos(pack
, base_offset
, &base_pos
) < 0)
1412 * We assume delta dependencies always point backwards. This
1413 * lets us do a single pass, and is basically always true
1414 * due to the way OFS_DELTAs work. You would not typically
1415 * find REF_DELTA in a bitmapped pack, since we only bitmap
1416 * packs we write fresh, and OFS_DELTA is the default). But
1417 * let's double check to make sure the pack wasn't written with
1420 if (base_pos
>= pos
)
1424 * And finally, if we're not sending the base as part of our
1425 * reuse chunk, then don't send this object either. The base
1426 * would come after us, along with other objects not
1427 * necessarily in the pack, which means we'd need to convert
1428 * to REF_DELTA on the fly. Better to just let the normal
1429 * object_entry code path handle it.
1431 if (!bitmap_get(reuse
, base_pos
))
1436 * If we got here, then the object is OK to reuse. Mark it.
1438 bitmap_set(reuse
, pos
);
1442 uint32_t midx_preferred_pack(struct bitmap_index
*bitmap_git
)
1444 struct multi_pack_index
*m
= bitmap_git
->midx
;
1446 BUG("midx_preferred_pack: requires non-empty MIDX");
1447 return nth_midxed_pack_int_id(m
, pack_pos_to_midx(bitmap_git
->midx
, 0));
1450 int reuse_partial_packfile_from_bitmap(struct bitmap_index
*bitmap_git
,
1451 struct packed_git
**packfile_out
,
1453 struct bitmap
**reuse_out
)
1455 struct packed_git
*pack
;
1456 struct bitmap
*result
= bitmap_git
->result
;
1457 struct bitmap
*reuse
;
1458 struct pack_window
*w_curs
= NULL
;
1461 uint32_t objects_nr
;
1465 load_reverse_index(bitmap_git
);
1467 if (bitmap_is_midx(bitmap_git
))
1468 pack
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
1470 pack
= bitmap_git
->pack
;
1471 objects_nr
= pack
->num_objects
;
1473 while (i
< result
->word_alloc
&& result
->words
[i
] == (eword_t
)~0)
1477 * Don't mark objects not in the packfile or preferred pack. This bitmap
1478 * marks objects eligible for reuse, but the pack-reuse code only
1479 * understands how to reuse a single pack. Since the preferred pack is
1480 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1481 * we use it instead of another pack. In single-pack bitmaps, the choice
1484 if (i
> objects_nr
/ BITS_IN_EWORD
)
1485 i
= objects_nr
/ BITS_IN_EWORD
;
1487 reuse
= bitmap_word_alloc(i
);
1488 memset(reuse
->words
, 0xFF, i
* sizeof(eword_t
));
1490 for (; i
< result
->word_alloc
; ++i
) {
1491 eword_t word
= result
->words
[i
];
1492 size_t pos
= (i
* BITS_IN_EWORD
);
1494 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1495 if ((word
>> offset
) == 0)
1498 offset
+= ewah_bit_ctz64(word
>> offset
);
1499 if (try_partial_reuse(pack
, pos
+ offset
,
1500 reuse
, &w_curs
) < 0) {
1502 * try_partial_reuse indicated we couldn't reuse
1503 * any bits, so there is no point in trying more
1504 * bits in the current word, or any other words
1507 * Jump out of both loops to avoid future
1508 * unnecessary calls to try_partial_reuse.
1516 unuse_pack(&w_curs
);
1518 *entries
= bitmap_popcount(reuse
);
1525 * Drop any reused objects from the result, since they will not
1526 * need to be handled separately.
1528 bitmap_and_not(result
, reuse
);
1529 *packfile_out
= pack
;
1534 int bitmap_walk_contains(struct bitmap_index
*bitmap_git
,
1535 struct bitmap
*bitmap
, const struct object_id
*oid
)
1542 idx
= bitmap_position(bitmap_git
, oid
);
1543 return idx
>= 0 && bitmap_get(bitmap
, idx
);
1546 void traverse_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1547 struct rev_info
*revs
,
1548 show_reachable_fn show_reachable
)
1550 assert(bitmap_git
->result
);
1552 show_objects_for_type(bitmap_git
, OBJ_COMMIT
, show_reachable
);
1553 if (revs
->tree_objects
)
1554 show_objects_for_type(bitmap_git
, OBJ_TREE
, show_reachable
);
1555 if (revs
->blob_objects
)
1556 show_objects_for_type(bitmap_git
, OBJ_BLOB
, show_reachable
);
1557 if (revs
->tag_objects
)
1558 show_objects_for_type(bitmap_git
, OBJ_TAG
, show_reachable
);
1560 show_extended_objects(bitmap_git
, revs
, show_reachable
);
1563 static uint32_t count_object_type(struct bitmap_index
*bitmap_git
,
1564 enum object_type type
)
1566 struct bitmap
*objects
= bitmap_git
->result
;
1567 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1569 uint32_t i
= 0, count
= 0;
1570 struct ewah_iterator it
;
1573 init_type_iterator(&it
, bitmap_git
, type
);
1575 while (i
< objects
->word_alloc
&& ewah_iterator_next(&filter
, &it
)) {
1576 eword_t word
= objects
->words
[i
++] & filter
;
1577 count
+= ewah_bit_popcount64(word
);
1580 for (i
= 0; i
< eindex
->count
; ++i
) {
1581 if (eindex
->objects
[i
]->type
== type
&&
1582 bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1589 void count_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1590 uint32_t *commits
, uint32_t *trees
,
1591 uint32_t *blobs
, uint32_t *tags
)
1593 assert(bitmap_git
->result
);
1596 *commits
= count_object_type(bitmap_git
, OBJ_COMMIT
);
1599 *trees
= count_object_type(bitmap_git
, OBJ_TREE
);
1602 *blobs
= count_object_type(bitmap_git
, OBJ_BLOB
);
1605 *tags
= count_object_type(bitmap_git
, OBJ_TAG
);
1608 struct bitmap_test_data
{
1609 struct bitmap_index
*bitmap_git
;
1610 struct bitmap
*base
;
1611 struct bitmap
*commits
;
1612 struct bitmap
*trees
;
1613 struct bitmap
*blobs
;
1614 struct bitmap
*tags
;
1615 struct progress
*prg
;
1619 static void test_bitmap_type(struct bitmap_test_data
*tdata
,
1620 struct object
*obj
, int pos
)
1622 enum object_type bitmap_type
= OBJ_NONE
;
1625 if (bitmap_get(tdata
->commits
, pos
)) {
1626 bitmap_type
= OBJ_COMMIT
;
1629 if (bitmap_get(tdata
->trees
, pos
)) {
1630 bitmap_type
= OBJ_TREE
;
1633 if (bitmap_get(tdata
->blobs
, pos
)) {
1634 bitmap_type
= OBJ_BLOB
;
1637 if (bitmap_get(tdata
->tags
, pos
)) {
1638 bitmap_type
= OBJ_TAG
;
1642 if (bitmap_type
== OBJ_NONE
)
1643 die("object %s not found in type bitmaps",
1644 oid_to_hex(&obj
->oid
));
1647 die("object %s does not have a unique type",
1648 oid_to_hex(&obj
->oid
));
1650 if (bitmap_type
!= obj
->type
)
1651 die("object %s: real type %s, expected: %s",
1652 oid_to_hex(&obj
->oid
),
1653 type_name(obj
->type
),
1654 type_name(bitmap_type
));
1657 static void test_show_object(struct object
*object
, const char *name
,
1660 struct bitmap_test_data
*tdata
= data
;
1663 bitmap_pos
= bitmap_position(tdata
->bitmap_git
, &object
->oid
);
1665 die("Object not in bitmap: %s\n", oid_to_hex(&object
->oid
));
1666 test_bitmap_type(tdata
, object
, bitmap_pos
);
1668 bitmap_set(tdata
->base
, bitmap_pos
);
1669 display_progress(tdata
->prg
, ++tdata
->seen
);
1672 static void test_show_commit(struct commit
*commit
, void *data
)
1674 struct bitmap_test_data
*tdata
= data
;
1677 bitmap_pos
= bitmap_position(tdata
->bitmap_git
,
1678 &commit
->object
.oid
);
1680 die("Object not in bitmap: %s\n", oid_to_hex(&commit
->object
.oid
));
1681 test_bitmap_type(tdata
, &commit
->object
, bitmap_pos
);
1683 bitmap_set(tdata
->base
, bitmap_pos
);
1684 display_progress(tdata
->prg
, ++tdata
->seen
);
1687 void test_bitmap_walk(struct rev_info
*revs
)
1689 struct object
*root
;
1690 struct bitmap
*result
= NULL
;
1691 size_t result_popcnt
;
1692 struct bitmap_test_data tdata
;
1693 struct bitmap_index
*bitmap_git
;
1694 struct ewah_bitmap
*bm
;
1696 if (!(bitmap_git
= prepare_bitmap_git(revs
->repo
)))
1697 die("failed to load bitmap indexes");
1699 if (revs
->pending
.nr
!= 1)
1700 die("you must specify exactly one commit to test");
1702 fprintf(stderr
, "Bitmap v%d test (%d entries loaded)\n",
1703 bitmap_git
->version
, bitmap_git
->entry_count
);
1705 root
= revs
->pending
.objects
[0].item
;
1706 bm
= bitmap_for_commit(bitmap_git
, (struct commit
*)root
);
1709 fprintf(stderr
, "Found bitmap for %s. %d bits / %08x checksum\n",
1710 oid_to_hex(&root
->oid
), (int)bm
->bit_size
, ewah_checksum(bm
));
1712 result
= ewah_to_bitmap(bm
);
1716 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root
->oid
));
1718 revs
->tag_objects
= 1;
1719 revs
->tree_objects
= 1;
1720 revs
->blob_objects
= 1;
1722 result_popcnt
= bitmap_popcount(result
);
1724 if (prepare_revision_walk(revs
))
1725 die("revision walk setup failed");
1727 tdata
.bitmap_git
= bitmap_git
;
1728 tdata
.base
= bitmap_new();
1729 tdata
.commits
= ewah_to_bitmap(bitmap_git
->commits
);
1730 tdata
.trees
= ewah_to_bitmap(bitmap_git
->trees
);
1731 tdata
.blobs
= ewah_to_bitmap(bitmap_git
->blobs
);
1732 tdata
.tags
= ewah_to_bitmap(bitmap_git
->tags
);
1733 tdata
.prg
= start_progress("Verifying bitmap entries", result_popcnt
);
1736 traverse_commit_list(revs
, &test_show_commit
, &test_show_object
, &tdata
);
1738 stop_progress(&tdata
.prg
);
1740 if (bitmap_equals(result
, tdata
.base
))
1741 fprintf(stderr
, "OK!\n");
1743 die("mismatch in bitmap results");
1745 bitmap_free(result
);
1746 bitmap_free(tdata
.base
);
1747 bitmap_free(tdata
.commits
);
1748 bitmap_free(tdata
.trees
);
1749 bitmap_free(tdata
.blobs
);
1750 bitmap_free(tdata
.tags
);
1751 free_bitmap_index(bitmap_git
);
1754 int test_bitmap_commits(struct repository
*r
)
1756 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
1757 struct object_id oid
;
1758 MAYBE_UNUSED
void *value
;
1761 die("failed to load bitmap indexes");
1763 kh_foreach(bitmap_git
->bitmaps
, oid
, value
, {
1764 printf("%s\n", oid_to_hex(&oid
));
1767 free_bitmap_index(bitmap_git
);
1772 int test_bitmap_hashes(struct repository
*r
)
1774 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
1775 struct object_id oid
;
1776 uint32_t i
, index_pos
;
1778 if (!bitmap_git
|| !bitmap_git
->hashes
)
1781 for (i
= 0; i
< bitmap_num_objects(bitmap_git
); i
++) {
1782 if (bitmap_is_midx(bitmap_git
))
1783 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
1785 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
1787 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
1789 printf("%s %"PRIu32
"\n",
1790 oid_to_hex(&oid
), get_be32(bitmap_git
->hashes
+ index_pos
));
1794 free_bitmap_index(bitmap_git
);
1799 int rebuild_bitmap(const uint32_t *reposition
,
1800 struct ewah_bitmap
*source
,
1801 struct bitmap
*dest
)
1804 struct ewah_iterator it
;
1807 ewah_iterator_init(&it
, source
);
1809 while (ewah_iterator_next(&word
, &it
)) {
1810 uint32_t offset
, bit_pos
;
1812 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1813 if ((word
>> offset
) == 0)
1816 offset
+= ewah_bit_ctz64(word
>> offset
);
1818 bit_pos
= reposition
[pos
+ offset
];
1820 bitmap_set(dest
, bit_pos
- 1);
1821 else /* can't reuse, we don't have the object */
1825 pos
+= BITS_IN_EWORD
;
1830 uint32_t *create_bitmap_mapping(struct bitmap_index
*bitmap_git
,
1831 struct packing_data
*mapping
)
1833 uint32_t i
, num_objects
;
1834 uint32_t *reposition
;
1836 if (!bitmap_is_midx(bitmap_git
))
1837 load_reverse_index(bitmap_git
);
1838 else if (load_midx_revindex(bitmap_git
->midx
) < 0)
1839 BUG("rebuild_existing_bitmaps: missing required rev-cache "
1842 num_objects
= bitmap_num_objects(bitmap_git
);
1843 CALLOC_ARRAY(reposition
, num_objects
);
1845 for (i
= 0; i
< num_objects
; ++i
) {
1846 struct object_id oid
;
1847 struct object_entry
*oe
;
1850 if (bitmap_is_midx(bitmap_git
))
1851 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
1853 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
1854 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
1855 oe
= packlist_find(mapping
, &oid
);
1858 reposition
[i
] = oe_in_pack_pos(mapping
, oe
) + 1;
1859 if (bitmap_git
->hashes
&& !oe
->hash
)
1860 oe
->hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
1867 void free_bitmap_index(struct bitmap_index
*b
)
1873 munmap(b
->map
, b
->map_size
);
1874 ewah_pool_free(b
->commits
);
1875 ewah_pool_free(b
->trees
);
1876 ewah_pool_free(b
->blobs
);
1877 ewah_pool_free(b
->tags
);
1879 struct stored_bitmap
*sb
;
1880 kh_foreach_value(b
->bitmaps
, sb
, {
1881 ewah_pool_free(sb
->root
);
1885 kh_destroy_oid_map(b
->bitmaps
);
1886 free(b
->ext_index
.objects
);
1887 free(b
->ext_index
.hashes
);
1888 kh_destroy_oid_pos(b
->ext_index
.positions
);
1889 bitmap_free(b
->result
);
1890 bitmap_free(b
->haves
);
1891 if (bitmap_is_midx(b
)) {
1893 * Multi-pack bitmaps need to have resources associated with
1894 * their on-disk reverse indexes unmapped so that stale .rev and
1895 * .bitmap files can be removed.
1897 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
1898 * written in the same 'git multi-pack-index write --bitmap'
1899 * process. Close resources so they can be removed safely on
1900 * platforms like Windows.
1902 close_midx_revindex(b
->midx
);
1907 int bitmap_has_oid_in_uninteresting(struct bitmap_index
*bitmap_git
,
1908 const struct object_id
*oid
)
1910 return bitmap_git
&&
1911 bitmap_walk_contains(bitmap_git
, bitmap_git
->haves
, oid
);
1914 static off_t
get_disk_usage_for_type(struct bitmap_index
*bitmap_git
,
1915 enum object_type object_type
)
1917 struct bitmap
*result
= bitmap_git
->result
;
1919 struct ewah_iterator it
;
1923 init_type_iterator(&it
, bitmap_git
, object_type
);
1924 for (i
= 0; i
< result
->word_alloc
&&
1925 ewah_iterator_next(&filter
, &it
); i
++) {
1926 eword_t word
= result
->words
[i
] & filter
;
1927 size_t base
= (i
* BITS_IN_EWORD
);
1933 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1934 if ((word
>> offset
) == 0)
1937 offset
+= ewah_bit_ctz64(word
>> offset
);
1939 if (bitmap_is_midx(bitmap_git
)) {
1941 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, base
+ offset
);
1942 off_t offset
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
1944 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
1945 struct packed_git
*pack
= bitmap_git
->midx
->packs
[pack_id
];
1947 if (offset_to_pack_pos(pack
, offset
, &pack_pos
) < 0) {
1948 struct object_id oid
;
1949 nth_midxed_object_oid(&oid
, bitmap_git
->midx
, midx_pos
);
1951 die(_("could not find %s in pack %s at offset %"PRIuMAX
),
1957 total
+= pack_pos_to_offset(pack
, pack_pos
+ 1) - offset
;
1959 size_t pos
= base
+ offset
;
1960 total
+= pack_pos_to_offset(bitmap_git
->pack
, pos
+ 1) -
1961 pack_pos_to_offset(bitmap_git
->pack
, pos
);
1969 static off_t
get_disk_usage_for_extended(struct bitmap_index
*bitmap_git
)
1971 struct bitmap
*result
= bitmap_git
->result
;
1972 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1974 struct object_info oi
= OBJECT_INFO_INIT
;
1978 oi
.disk_sizep
= &object_size
;
1980 for (i
= 0; i
< eindex
->count
; i
++) {
1981 struct object
*obj
= eindex
->objects
[i
];
1983 if (!bitmap_get(result
, bitmap_num_objects(bitmap_git
) + i
))
1986 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
1987 die(_("unable to get disk usage of %s"),
1988 oid_to_hex(&obj
->oid
));
1990 total
+= object_size
;
1995 off_t
get_disk_usage_from_bitmap(struct bitmap_index
*bitmap_git
,
1996 struct rev_info
*revs
)
2000 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_COMMIT
);
2001 if (revs
->tree_objects
)
2002 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TREE
);
2003 if (revs
->blob_objects
)
2004 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_BLOB
);
2005 if (revs
->tag_objects
)
2006 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TAG
);
2008 total
+= get_disk_usage_for_extended(bitmap_git
);
2013 int bitmap_is_midx(struct bitmap_index
*bitmap_git
)
2015 return !!bitmap_git
->midx
;
2018 const struct string_list
*bitmap_preferred_tips(struct repository
*r
)
2020 return repo_config_get_value_multi(r
, "pack.preferbitmaptips");
2023 int bitmap_is_preferred_refname(struct repository
*r
, const char *refname
)
2025 const struct string_list
*preferred_tips
= bitmap_preferred_tips(r
);
2026 struct string_list_item
*item
;
2028 if (!preferred_tips
)
2031 for_each_string_list_item(item
, preferred_tips
) {
2032 if (starts_with(refname
, item
->string
))