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"
19 * An entry on the bitmap index, representing the bitmap for a given
22 struct stored_bitmap
{
24 struct ewah_bitmap
*root
;
25 struct stored_bitmap
*xor;
30 * The active bitmap index for a repository. By design, repositories only have
31 * a single bitmap index available (the index for the biggest packfile in
32 * the repository), since bitmap indexes need full closure.
34 * If there is more than one bitmap index available (e.g. because of alternates),
35 * the active bitmap index is the largest one.
38 /* Packfile to which this bitmap index belongs to */
39 struct packed_git
*pack
;
42 * Mark the first `reuse_objects` in the packfile as reused:
43 * they will be sent as-is without using them for repacking
46 uint32_t reuse_objects
;
48 /* mmapped buffer of the whole bitmap index */
50 size_t map_size
; /* size of the mmaped buffer */
51 size_t map_pos
; /* current position when loading the index */
56 * Each bitmap marks which objects in the packfile are of the given
57 * type. This provides type information when yielding the objects from
58 * the packfile during a walk, which allows for better delta bases.
60 struct ewah_bitmap
*commits
;
61 struct ewah_bitmap
*trees
;
62 struct ewah_bitmap
*blobs
;
63 struct ewah_bitmap
*tags
;
65 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
66 kh_oid_map_t
*bitmaps
;
68 /* Number of bitmapped commits */
71 /* If not NULL, this is a name-hash cache pointing into map. */
77 * When trying to perform bitmap operations with objects that are not
78 * packed in `pack`, these objects are added to this "fake index" and
79 * are assumed to appear at the end of the packfile for all operations
82 struct object
**objects
;
84 uint32_t count
, alloc
;
85 kh_oid_pos_t
*positions
;
88 /* Bitmap result of the last performed walk */
89 struct bitmap
*result
;
91 /* "have" bitmap from the last performed walk */
94 /* Version of the bitmap index */
98 static struct ewah_bitmap
*lookup_stored_bitmap(struct stored_bitmap
*st
)
100 struct ewah_bitmap
*parent
;
101 struct ewah_bitmap
*composed
;
106 composed
= ewah_pool_new();
107 parent
= lookup_stored_bitmap(st
->xor);
108 ewah_xor(st
->root
, parent
, composed
);
110 ewah_pool_free(st
->root
);
118 * Read a bitmap from the current read position on the mmaped
119 * index, and increase the read position accordingly
121 static struct ewah_bitmap
*read_bitmap_1(struct bitmap_index
*index
)
123 struct ewah_bitmap
*b
= ewah_pool_new();
125 ssize_t bitmap_size
= ewah_read_mmap(b
,
126 index
->map
+ index
->map_pos
,
127 index
->map_size
- index
->map_pos
);
129 if (bitmap_size
< 0) {
130 error("Failed to load bitmap index (corrupted?)");
135 index
->map_pos
+= bitmap_size
;
139 static uint32_t bitmap_num_objects(struct bitmap_index
*index
)
141 return index
->pack
->num_objects
;
144 static int load_bitmap_header(struct bitmap_index
*index
)
146 struct bitmap_disk_header
*header
= (void *)index
->map
;
147 size_t header_size
= sizeof(*header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
;
149 if (index
->map_size
< header_size
+ the_hash_algo
->rawsz
)
150 return error("Corrupted bitmap index (too small)");
152 if (memcmp(header
->magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
)) != 0)
153 return error("Corrupted bitmap index file (wrong header)");
155 index
->version
= ntohs(header
->version
);
156 if (index
->version
!= 1)
157 return error("Unsupported version for bitmap index file (%d)", index
->version
);
159 /* Parse known bitmap format options */
161 uint32_t flags
= ntohs(header
->options
);
162 size_t cache_size
= st_mult(bitmap_num_objects(index
), sizeof(uint32_t));
163 unsigned char *index_end
= index
->map
+ index
->map_size
- the_hash_algo
->rawsz
;
165 if ((flags
& BITMAP_OPT_FULL_DAG
) == 0)
166 return error("Unsupported options for bitmap index file "
167 "(Git requires BITMAP_OPT_FULL_DAG)");
169 if (flags
& BITMAP_OPT_HASH_CACHE
) {
170 if (cache_size
> index_end
- index
->map
- header_size
)
171 return error("corrupted bitmap index file (too short to fit hash cache)");
172 index
->hashes
= (void *)(index_end
- cache_size
);
173 index_end
-= cache_size
;
177 index
->entry_count
= ntohl(header
->entry_count
);
178 index
->map_pos
+= header_size
;
182 static struct stored_bitmap
*store_bitmap(struct bitmap_index
*index
,
183 struct ewah_bitmap
*root
,
184 const struct object_id
*oid
,
185 struct stored_bitmap
*xor_with
,
188 struct stored_bitmap
*stored
;
192 stored
= xmalloc(sizeof(struct stored_bitmap
));
194 stored
->xor = xor_with
;
195 stored
->flags
= flags
;
196 oidcpy(&stored
->oid
, oid
);
198 hash_pos
= kh_put_oid_map(index
->bitmaps
, stored
->oid
, &ret
);
200 /* a 0 return code means the insertion succeeded with no changes,
201 * because the SHA1 already existed on the map. this is bad, there
202 * shouldn't be duplicated commits in the index */
204 error("Duplicate entry in bitmap index: %s", oid_to_hex(oid
));
208 kh_value(index
->bitmaps
, hash_pos
) = stored
;
212 static inline uint32_t read_be32(const unsigned char *buffer
, size_t *pos
)
214 uint32_t result
= get_be32(buffer
+ *pos
);
215 (*pos
) += sizeof(result
);
219 static inline uint8_t read_u8(const unsigned char *buffer
, size_t *pos
)
221 return buffer
[(*pos
)++];
224 #define MAX_XOR_OFFSET 160
226 static int load_bitmap_entries_v1(struct bitmap_index
*index
)
229 struct stored_bitmap
*recent_bitmaps
[MAX_XOR_OFFSET
] = { NULL
};
231 for (i
= 0; i
< index
->entry_count
; ++i
) {
232 int xor_offset
, flags
;
233 struct ewah_bitmap
*bitmap
= NULL
;
234 struct stored_bitmap
*xor_bitmap
= NULL
;
235 uint32_t commit_idx_pos
;
236 struct object_id oid
;
238 if (index
->map_size
- index
->map_pos
< 6)
239 return error("corrupt ewah bitmap: truncated header for entry %d", i
);
241 commit_idx_pos
= read_be32(index
->map
, &index
->map_pos
);
242 xor_offset
= read_u8(index
->map
, &index
->map_pos
);
243 flags
= read_u8(index
->map
, &index
->map_pos
);
245 if (nth_packed_object_id(&oid
, index
->pack
, commit_idx_pos
) < 0)
246 return error("corrupt ewah bitmap: commit index %u out of range",
247 (unsigned)commit_idx_pos
);
249 bitmap
= read_bitmap_1(index
);
253 if (xor_offset
> MAX_XOR_OFFSET
|| xor_offset
> i
)
254 return error("Corrupted bitmap pack index");
256 if (xor_offset
> 0) {
257 xor_bitmap
= recent_bitmaps
[(i
- xor_offset
) % MAX_XOR_OFFSET
];
259 if (xor_bitmap
== NULL
)
260 return error("Invalid XOR offset in bitmap pack index");
263 recent_bitmaps
[i
% MAX_XOR_OFFSET
] = store_bitmap(
264 index
, bitmap
, &oid
, xor_bitmap
, flags
);
270 static char *pack_bitmap_filename(struct packed_git
*p
)
274 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
275 BUG("pack_name does not end in .pack");
276 return xstrfmt("%.*s.bitmap", (int)len
, p
->pack_name
);
279 static int open_pack_bitmap_1(struct bitmap_index
*bitmap_git
, struct packed_git
*packfile
)
285 if (open_pack_index(packfile
))
288 idx_name
= pack_bitmap_filename(packfile
);
289 fd
= git_open(idx_name
);
295 if (fstat(fd
, &st
)) {
300 if (bitmap_git
->pack
) {
301 warning("ignoring extra bitmap file: %s", packfile
->pack_name
);
306 if (!is_pack_valid(packfile
)) {
311 bitmap_git
->pack
= packfile
;
312 bitmap_git
->map_size
= xsize_t(st
.st_size
);
313 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
314 bitmap_git
->map_pos
= 0;
317 if (load_bitmap_header(bitmap_git
) < 0) {
318 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
319 bitmap_git
->map
= NULL
;
320 bitmap_git
->map_size
= 0;
327 static int load_pack_bitmap(struct bitmap_index
*bitmap_git
)
329 assert(bitmap_git
->map
);
331 bitmap_git
->bitmaps
= kh_init_oid_map();
332 bitmap_git
->ext_index
.positions
= kh_init_oid_pos();
333 if (load_pack_revindex(bitmap_git
->pack
))
336 if (!(bitmap_git
->commits
= read_bitmap_1(bitmap_git
)) ||
337 !(bitmap_git
->trees
= read_bitmap_1(bitmap_git
)) ||
338 !(bitmap_git
->blobs
= read_bitmap_1(bitmap_git
)) ||
339 !(bitmap_git
->tags
= read_bitmap_1(bitmap_git
)))
342 if (load_bitmap_entries_v1(bitmap_git
) < 0)
348 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
349 bitmap_git
->map
= NULL
;
350 bitmap_git
->map_size
= 0;
352 kh_destroy_oid_map(bitmap_git
->bitmaps
);
353 bitmap_git
->bitmaps
= NULL
;
355 kh_destroy_oid_pos(bitmap_git
->ext_index
.positions
);
356 bitmap_git
->ext_index
.positions
= NULL
;
361 static int open_pack_bitmap(struct repository
*r
,
362 struct bitmap_index
*bitmap_git
)
364 struct packed_git
*p
;
367 assert(!bitmap_git
->map
);
369 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
370 if (open_pack_bitmap_1(bitmap_git
, p
) == 0)
377 struct bitmap_index
*prepare_bitmap_git(struct repository
*r
)
379 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
381 if (!open_pack_bitmap(r
, bitmap_git
) && !load_pack_bitmap(bitmap_git
))
384 free_bitmap_index(bitmap_git
);
388 struct include_data
{
389 struct bitmap_index
*bitmap_git
;
394 struct ewah_bitmap
*bitmap_for_commit(struct bitmap_index
*bitmap_git
,
395 struct commit
*commit
)
397 khiter_t hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
,
399 if (hash_pos
>= kh_end(bitmap_git
->bitmaps
))
401 return lookup_stored_bitmap(kh_value(bitmap_git
->bitmaps
, hash_pos
));
404 static inline int bitmap_position_extended(struct bitmap_index
*bitmap_git
,
405 const struct object_id
*oid
)
407 kh_oid_pos_t
*positions
= bitmap_git
->ext_index
.positions
;
408 khiter_t pos
= kh_get_oid_pos(positions
, *oid
);
410 if (pos
< kh_end(positions
)) {
411 int bitmap_pos
= kh_value(positions
, pos
);
412 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
418 static inline int bitmap_position_packfile(struct bitmap_index
*bitmap_git
,
419 const struct object_id
*oid
)
422 off_t offset
= find_pack_entry_one(oid
->hash
, bitmap_git
->pack
);
426 if (offset_to_pack_pos(bitmap_git
->pack
, offset
, &pos
) < 0)
431 static int bitmap_position(struct bitmap_index
*bitmap_git
,
432 const struct object_id
*oid
)
434 int pos
= bitmap_position_packfile(bitmap_git
, oid
);
435 return (pos
>= 0) ? pos
: bitmap_position_extended(bitmap_git
, oid
);
438 static int ext_index_add_object(struct bitmap_index
*bitmap_git
,
439 struct object
*object
, const char *name
)
441 struct eindex
*eindex
= &bitmap_git
->ext_index
;
447 hash_pos
= kh_put_oid_pos(eindex
->positions
, object
->oid
, &hash_ret
);
449 if (eindex
->count
>= eindex
->alloc
) {
450 eindex
->alloc
= (eindex
->alloc
+ 16) * 3 / 2;
451 REALLOC_ARRAY(eindex
->objects
, eindex
->alloc
);
452 REALLOC_ARRAY(eindex
->hashes
, eindex
->alloc
);
455 bitmap_pos
= eindex
->count
;
456 eindex
->objects
[eindex
->count
] = object
;
457 eindex
->hashes
[eindex
->count
] = pack_name_hash(name
);
458 kh_value(eindex
->positions
, hash_pos
) = bitmap_pos
;
461 bitmap_pos
= kh_value(eindex
->positions
, hash_pos
);
464 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
467 struct bitmap_show_data
{
468 struct bitmap_index
*bitmap_git
;
472 static void show_object(struct object
*object
, const char *name
, void *data_
)
474 struct bitmap_show_data
*data
= data_
;
477 bitmap_pos
= bitmap_position(data
->bitmap_git
, &object
->oid
);
480 bitmap_pos
= ext_index_add_object(data
->bitmap_git
, object
,
483 bitmap_set(data
->base
, bitmap_pos
);
486 static void show_commit(struct commit
*commit
, void *data
)
490 static int add_to_include_set(struct bitmap_index
*bitmap_git
,
491 struct include_data
*data
,
492 struct commit
*commit
,
495 struct ewah_bitmap
*partial
;
497 if (data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
))
500 if (bitmap_get(data
->base
, bitmap_pos
))
503 partial
= bitmap_for_commit(bitmap_git
, commit
);
505 bitmap_or_ewah(data
->base
, partial
);
509 bitmap_set(data
->base
, bitmap_pos
);
513 static int should_include(struct commit
*commit
, void *_data
)
515 struct include_data
*data
= _data
;
518 bitmap_pos
= bitmap_position(data
->bitmap_git
, &commit
->object
.oid
);
520 bitmap_pos
= ext_index_add_object(data
->bitmap_git
,
521 (struct object
*)commit
,
524 if (!add_to_include_set(data
->bitmap_git
, data
, commit
, bitmap_pos
)) {
525 struct commit_list
*parent
= commit
->parents
;
528 parent
->item
->object
.flags
|= SEEN
;
529 parent
= parent
->next
;
538 static int should_include_obj(struct object
*obj
, void *_data
)
540 struct include_data
*data
= _data
;
543 bitmap_pos
= bitmap_position(data
->bitmap_git
, &obj
->oid
);
546 if ((data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
)) ||
547 bitmap_get(data
->base
, bitmap_pos
)) {
554 static int add_commit_to_bitmap(struct bitmap_index
*bitmap_git
,
555 struct bitmap
**base
,
556 struct commit
*commit
)
558 struct ewah_bitmap
*or_with
= bitmap_for_commit(bitmap_git
, commit
);
564 *base
= ewah_to_bitmap(or_with
);
566 bitmap_or_ewah(*base
, or_with
);
571 static struct bitmap
*find_objects(struct bitmap_index
*bitmap_git
,
572 struct rev_info
*revs
,
573 struct object_list
*roots
,
575 struct list_objects_filter_options
*filter
)
577 struct bitmap
*base
= NULL
;
580 struct object_list
*not_mapped
= NULL
;
583 * Go through all the roots for the walk. The ones that have bitmaps
584 * on the bitmap index will be `or`ed together to form an initial
585 * global reachability analysis.
587 * The ones without bitmaps in the index will be stored in the
588 * `not_mapped_list` for further processing.
591 struct object
*object
= roots
->item
;
594 if (object
->type
== OBJ_COMMIT
&&
595 add_commit_to_bitmap(bitmap_git
, &base
, (struct commit
*)object
)) {
596 object
->flags
|= SEEN
;
600 object_list_insert(object
, ¬_mapped
);
604 * Best case scenario: We found bitmaps for all the roots,
605 * so the resulting `or` bitmap has the full reachability analysis
607 if (not_mapped
== NULL
)
613 * Let's iterate through all the roots that don't have bitmaps to
614 * check if we can determine them to be reachable from the existing
617 * If we cannot find them in the existing global bitmap, we'll need
618 * to push them to an actual walk and run it until we can confirm
622 struct object
*object
= roots
->item
;
626 pos
= bitmap_position(bitmap_git
, &object
->oid
);
628 if (pos
< 0 || base
== NULL
|| !bitmap_get(base
, pos
)) {
629 object
->flags
&= ~UNINTERESTING
;
630 add_pending_object(revs
, object
, "");
633 object
->flags
|= SEEN
;
638 struct include_data incdata
;
639 struct bitmap_show_data show_data
;
644 incdata
.bitmap_git
= bitmap_git
;
648 revs
->include_check
= should_include
;
649 revs
->include_check_obj
= should_include_obj
;
650 revs
->include_check_data
= &incdata
;
652 if (prepare_revision_walk(revs
))
653 die("revision walk setup failed");
655 show_data
.bitmap_git
= bitmap_git
;
656 show_data
.base
= base
;
658 traverse_commit_list_filtered(filter
, revs
,
659 show_commit
, show_object
,
662 revs
->include_check
= NULL
;
663 revs
->include_check_obj
= NULL
;
664 revs
->include_check_data
= NULL
;
670 static void show_extended_objects(struct bitmap_index
*bitmap_git
,
671 struct rev_info
*revs
,
672 show_reachable_fn show_reach
)
674 struct bitmap
*objects
= bitmap_git
->result
;
675 struct eindex
*eindex
= &bitmap_git
->ext_index
;
678 for (i
= 0; i
< eindex
->count
; ++i
) {
681 if (!bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
684 obj
= eindex
->objects
[i
];
685 if ((obj
->type
== OBJ_BLOB
&& !revs
->blob_objects
) ||
686 (obj
->type
== OBJ_TREE
&& !revs
->tree_objects
) ||
687 (obj
->type
== OBJ_TAG
&& !revs
->tag_objects
))
690 show_reach(&obj
->oid
, obj
->type
, 0, eindex
->hashes
[i
], NULL
, 0);
694 static void init_type_iterator(struct ewah_iterator
*it
,
695 struct bitmap_index
*bitmap_git
,
696 enum object_type type
)
700 ewah_iterator_init(it
, bitmap_git
->commits
);
704 ewah_iterator_init(it
, bitmap_git
->trees
);
708 ewah_iterator_init(it
, bitmap_git
->blobs
);
712 ewah_iterator_init(it
, bitmap_git
->tags
);
716 BUG("object type %d not stored by bitmap type index", type
);
721 static void show_objects_for_type(
722 struct bitmap_index
*bitmap_git
,
723 enum object_type object_type
,
724 show_reachable_fn show_reach
)
729 struct ewah_iterator it
;
732 struct bitmap
*objects
= bitmap_git
->result
;
734 init_type_iterator(&it
, bitmap_git
, object_type
);
736 for (i
= 0; i
< objects
->word_alloc
&&
737 ewah_iterator_next(&filter
, &it
); i
++) {
738 eword_t word
= objects
->words
[i
] & filter
;
739 size_t pos
= (i
* BITS_IN_EWORD
);
744 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
745 struct object_id oid
;
746 uint32_t hash
= 0, index_pos
;
749 if ((word
>> offset
) == 0)
752 offset
+= ewah_bit_ctz64(word
>> offset
);
754 index_pos
= pack_pos_to_index(bitmap_git
->pack
, pos
+ offset
);
755 ofs
= pack_pos_to_offset(bitmap_git
->pack
, pos
+ offset
);
756 nth_packed_object_id(&oid
, bitmap_git
->pack
, index_pos
);
758 if (bitmap_git
->hashes
)
759 hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
761 show_reach(&oid
, object_type
, 0, hash
, bitmap_git
->pack
, ofs
);
766 static int in_bitmapped_pack(struct bitmap_index
*bitmap_git
,
767 struct object_list
*roots
)
770 struct object
*object
= roots
->item
;
773 if (find_pack_entry_one(object
->oid
.hash
, bitmap_git
->pack
) > 0)
780 static struct bitmap
*find_tip_objects(struct bitmap_index
*bitmap_git
,
781 struct object_list
*tip_objects
,
782 enum object_type type
)
784 struct bitmap
*result
= bitmap_new();
785 struct object_list
*p
;
787 for (p
= tip_objects
; p
; p
= p
->next
) {
790 if (p
->item
->type
!= type
)
793 pos
= bitmap_position(bitmap_git
, &p
->item
->oid
);
797 bitmap_set(result
, pos
);
803 static void filter_bitmap_exclude_type(struct bitmap_index
*bitmap_git
,
804 struct object_list
*tip_objects
,
805 struct bitmap
*to_filter
,
806 enum object_type type
)
808 struct eindex
*eindex
= &bitmap_git
->ext_index
;
810 struct ewah_iterator it
;
815 * The non-bitmap version of this filter never removes
816 * objects which the other side specifically asked for,
817 * so we must match that behavior.
819 tips
= find_tip_objects(bitmap_git
, tip_objects
, type
);
822 * We can use the type-level bitmap for 'type' to work in whole
823 * words for the objects that are actually in the bitmapped
826 for (i
= 0, init_type_iterator(&it
, bitmap_git
, type
);
827 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
829 if (i
< tips
->word_alloc
)
830 mask
&= ~tips
->words
[i
];
831 to_filter
->words
[i
] &= ~mask
;
835 * Clear any objects that weren't in the packfile (and so would
836 * not have been caught by the loop above. We'll have to check
839 for (i
= 0; i
< eindex
->count
; i
++) {
840 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
841 if (eindex
->objects
[i
]->type
== type
&&
842 bitmap_get(to_filter
, pos
) &&
843 !bitmap_get(tips
, pos
))
844 bitmap_unset(to_filter
, pos
);
850 static void filter_bitmap_blob_none(struct bitmap_index
*bitmap_git
,
851 struct object_list
*tip_objects
,
852 struct bitmap
*to_filter
)
854 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
858 static unsigned long get_size_by_pos(struct bitmap_index
*bitmap_git
,
861 struct packed_git
*pack
= bitmap_git
->pack
;
863 struct object_info oi
= OBJECT_INFO_INIT
;
867 if (pos
< bitmap_num_objects(bitmap_git
)) {
868 off_t ofs
= pack_pos_to_offset(pack
, pos
);
869 if (packed_object_info(the_repository
, pack
, ofs
, &oi
) < 0) {
870 struct object_id oid
;
871 nth_packed_object_id(&oid
, pack
,
872 pack_pos_to_index(pack
, pos
));
873 die(_("unable to get size of %s"), oid_to_hex(&oid
));
876 struct eindex
*eindex
= &bitmap_git
->ext_index
;
877 struct object
*obj
= eindex
->objects
[pos
- bitmap_num_objects(bitmap_git
)];
878 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
879 die(_("unable to get size of %s"), oid_to_hex(&obj
->oid
));
885 static void filter_bitmap_blob_limit(struct bitmap_index
*bitmap_git
,
886 struct object_list
*tip_objects
,
887 struct bitmap
*to_filter
,
890 struct eindex
*eindex
= &bitmap_git
->ext_index
;
892 struct ewah_iterator it
;
896 tips
= find_tip_objects(bitmap_git
, tip_objects
, OBJ_BLOB
);
898 for (i
= 0, init_type_iterator(&it
, bitmap_git
, OBJ_BLOB
);
899 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
901 eword_t word
= to_filter
->words
[i
] & mask
;
904 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
907 if ((word
>> offset
) == 0)
909 offset
+= ewah_bit_ctz64(word
>> offset
);
910 pos
= i
* BITS_IN_EWORD
+ offset
;
912 if (!bitmap_get(tips
, pos
) &&
913 get_size_by_pos(bitmap_git
, pos
) >= limit
)
914 bitmap_unset(to_filter
, pos
);
918 for (i
= 0; i
< eindex
->count
; i
++) {
919 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
920 if (eindex
->objects
[i
]->type
== OBJ_BLOB
&&
921 bitmap_get(to_filter
, pos
) &&
922 !bitmap_get(tips
, pos
) &&
923 get_size_by_pos(bitmap_git
, pos
) >= limit
)
924 bitmap_unset(to_filter
, pos
);
930 static void filter_bitmap_tree_depth(struct bitmap_index
*bitmap_git
,
931 struct object_list
*tip_objects
,
932 struct bitmap
*to_filter
,
936 BUG("filter_bitmap_tree_depth given non-zero limit");
938 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
940 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
944 static void filter_bitmap_object_type(struct bitmap_index
*bitmap_git
,
945 struct object_list
*tip_objects
,
946 struct bitmap
*to_filter
,
947 enum object_type object_type
)
949 if (object_type
< OBJ_COMMIT
|| object_type
> OBJ_TAG
)
950 BUG("filter_bitmap_object_type given invalid object");
952 if (object_type
!= OBJ_TAG
)
953 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TAG
);
954 if (object_type
!= OBJ_COMMIT
)
955 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_COMMIT
);
956 if (object_type
!= OBJ_TREE
)
957 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TREE
);
958 if (object_type
!= OBJ_BLOB
)
959 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_BLOB
);
962 static int filter_bitmap(struct bitmap_index
*bitmap_git
,
963 struct object_list
*tip_objects
,
964 struct bitmap
*to_filter
,
965 struct list_objects_filter_options
*filter
)
967 if (!filter
|| filter
->choice
== LOFC_DISABLED
)
970 if (filter
->choice
== LOFC_BLOB_NONE
) {
972 filter_bitmap_blob_none(bitmap_git
, tip_objects
,
977 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
979 filter_bitmap_blob_limit(bitmap_git
, tip_objects
,
981 filter
->blob_limit_value
);
985 if (filter
->choice
== LOFC_TREE_DEPTH
&&
986 filter
->tree_exclude_depth
== 0) {
988 filter_bitmap_tree_depth(bitmap_git
, tip_objects
,
990 filter
->tree_exclude_depth
);
994 if (filter
->choice
== LOFC_OBJECT_TYPE
) {
996 filter_bitmap_object_type(bitmap_git
, tip_objects
,
998 filter
->object_type
);
1002 if (filter
->choice
== LOFC_COMBINE
) {
1004 for (i
= 0; i
< filter
->sub_nr
; i
++) {
1005 if (filter_bitmap(bitmap_git
, tip_objects
, to_filter
,
1006 &filter
->sub
[i
]) < 0)
1012 /* filter choice not handled */
1016 static int can_filter_bitmap(struct list_objects_filter_options
*filter
)
1018 return !filter_bitmap(NULL
, NULL
, NULL
, filter
);
1021 struct bitmap_index
*prepare_bitmap_walk(struct rev_info
*revs
,
1022 struct list_objects_filter_options
*filter
,
1023 int filter_provided_objects
)
1027 struct object_list
*wants
= NULL
;
1028 struct object_list
*haves
= NULL
;
1030 struct bitmap
*wants_bitmap
= NULL
;
1031 struct bitmap
*haves_bitmap
= NULL
;
1033 struct bitmap_index
*bitmap_git
;
1036 * We can't do pathspec limiting with bitmaps, because we don't know
1037 * which commits are associated with which object changes (let alone
1038 * even which objects are associated with which paths).
1043 if (!can_filter_bitmap(filter
))
1046 /* try to open a bitmapped pack, but don't parse it yet
1047 * because we may not need to use it */
1048 CALLOC_ARRAY(bitmap_git
, 1);
1049 if (open_pack_bitmap(revs
->repo
, bitmap_git
) < 0)
1052 for (i
= 0; i
< revs
->pending
.nr
; ++i
) {
1053 struct object
*object
= revs
->pending
.objects
[i
].item
;
1055 if (object
->type
== OBJ_NONE
)
1056 parse_object_or_die(&object
->oid
, NULL
);
1058 while (object
->type
== OBJ_TAG
) {
1059 struct tag
*tag
= (struct tag
*) object
;
1061 if (object
->flags
& UNINTERESTING
)
1062 object_list_insert(object
, &haves
);
1064 object_list_insert(object
, &wants
);
1066 object
= parse_object_or_die(get_tagged_oid(tag
), NULL
);
1067 object
->flags
|= (tag
->object
.flags
& UNINTERESTING
);
1070 if (object
->flags
& UNINTERESTING
)
1071 object_list_insert(object
, &haves
);
1073 object_list_insert(object
, &wants
);
1077 * if we have a HAVES list, but none of those haves is contained
1078 * in the packfile that has a bitmap, we don't have anything to
1081 if (haves
&& !in_bitmapped_pack(bitmap_git
, haves
))
1084 /* if we don't want anything, we're done here */
1089 * now we're going to use bitmaps, so load the actual bitmap entries
1090 * from disk. this is the point of no return; after this the rev_list
1091 * becomes invalidated and we must perform the revwalk through bitmaps
1093 if (load_pack_bitmap(bitmap_git
) < 0)
1096 object_array_clear(&revs
->pending
);
1099 revs
->ignore_missing_links
= 1;
1100 haves_bitmap
= find_objects(bitmap_git
, revs
, haves
, NULL
,
1102 reset_revision_walk();
1103 revs
->ignore_missing_links
= 0;
1105 if (haves_bitmap
== NULL
)
1106 BUG("failed to perform bitmap walk");
1109 wants_bitmap
= find_objects(bitmap_git
, revs
, wants
, haves_bitmap
,
1113 BUG("failed to perform bitmap walk");
1116 bitmap_and_not(wants_bitmap
, haves_bitmap
);
1118 filter_bitmap(bitmap_git
, (filter
&& filter_provided_objects
) ? NULL
: wants
,
1119 wants_bitmap
, filter
);
1121 bitmap_git
->result
= wants_bitmap
;
1122 bitmap_git
->haves
= haves_bitmap
;
1124 object_list_free(&wants
);
1125 object_list_free(&haves
);
1130 free_bitmap_index(bitmap_git
);
1131 object_list_free(&wants
);
1132 object_list_free(&haves
);
1136 static void try_partial_reuse(struct bitmap_index
*bitmap_git
,
1138 struct bitmap
*reuse
,
1139 struct pack_window
**w_curs
)
1141 off_t offset
, header
;
1142 enum object_type type
;
1145 if (pos
>= bitmap_num_objects(bitmap_git
))
1146 return; /* not actually in the pack or MIDX */
1148 offset
= header
= pack_pos_to_offset(bitmap_git
->pack
, pos
);
1149 type
= unpack_object_header(bitmap_git
->pack
, w_curs
, &offset
, &size
);
1151 return; /* broken packfile, punt */
1153 if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
) {
1158 * Find the position of the base object so we can look it up
1159 * in our bitmaps. If we can't come up with an offset, or if
1160 * that offset is not in the revidx, the pack is corrupt.
1161 * There's nothing we can do, so just punt on this object,
1162 * and the normal slow path will complain about it in
1165 base_offset
= get_delta_base(bitmap_git
->pack
, w_curs
,
1166 &offset
, type
, header
);
1169 if (offset_to_pack_pos(bitmap_git
->pack
, base_offset
, &base_pos
) < 0)
1173 * We assume delta dependencies always point backwards. This
1174 * lets us do a single pass, and is basically always true
1175 * due to the way OFS_DELTAs work. You would not typically
1176 * find REF_DELTA in a bitmapped pack, since we only bitmap
1177 * packs we write fresh, and OFS_DELTA is the default). But
1178 * let's double check to make sure the pack wasn't written with
1181 if (base_pos
>= pos
)
1185 * And finally, if we're not sending the base as part of our
1186 * reuse chunk, then don't send this object either. The base
1187 * would come after us, along with other objects not
1188 * necessarily in the pack, which means we'd need to convert
1189 * to REF_DELTA on the fly. Better to just let the normal
1190 * object_entry code path handle it.
1192 if (!bitmap_get(reuse
, base_pos
))
1197 * If we got here, then the object is OK to reuse. Mark it.
1199 bitmap_set(reuse
, pos
);
1202 int reuse_partial_packfile_from_bitmap(struct bitmap_index
*bitmap_git
,
1203 struct packed_git
**packfile_out
,
1205 struct bitmap
**reuse_out
)
1207 struct bitmap
*result
= bitmap_git
->result
;
1208 struct bitmap
*reuse
;
1209 struct pack_window
*w_curs
= NULL
;
1212 uint32_t objects_nr
= bitmap_num_objects(bitmap_git
);
1216 while (i
< result
->word_alloc
&& result
->words
[i
] == (eword_t
)~0)
1219 /* Don't mark objects not in the packfile */
1220 if (i
> objects_nr
/ BITS_IN_EWORD
)
1221 i
= objects_nr
/ BITS_IN_EWORD
;
1223 reuse
= bitmap_word_alloc(i
);
1224 memset(reuse
->words
, 0xFF, i
* sizeof(eword_t
));
1226 for (; i
< result
->word_alloc
; ++i
) {
1227 eword_t word
= result
->words
[i
];
1228 size_t pos
= (i
* BITS_IN_EWORD
);
1230 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1231 if ((word
>> offset
) == 0)
1234 offset
+= ewah_bit_ctz64(word
>> offset
);
1235 try_partial_reuse(bitmap_git
, pos
+ offset
, reuse
, &w_curs
);
1239 unuse_pack(&w_curs
);
1241 *entries
= bitmap_popcount(reuse
);
1248 * Drop any reused objects from the result, since they will not
1249 * need to be handled separately.
1251 bitmap_and_not(result
, reuse
);
1252 *packfile_out
= bitmap_git
->pack
;
1257 int bitmap_walk_contains(struct bitmap_index
*bitmap_git
,
1258 struct bitmap
*bitmap
, const struct object_id
*oid
)
1265 idx
= bitmap_position(bitmap_git
, oid
);
1266 return idx
>= 0 && bitmap_get(bitmap
, idx
);
1269 void traverse_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1270 struct rev_info
*revs
,
1271 show_reachable_fn show_reachable
)
1273 assert(bitmap_git
->result
);
1275 show_objects_for_type(bitmap_git
, OBJ_COMMIT
, show_reachable
);
1276 if (revs
->tree_objects
)
1277 show_objects_for_type(bitmap_git
, OBJ_TREE
, show_reachable
);
1278 if (revs
->blob_objects
)
1279 show_objects_for_type(bitmap_git
, OBJ_BLOB
, show_reachable
);
1280 if (revs
->tag_objects
)
1281 show_objects_for_type(bitmap_git
, OBJ_TAG
, show_reachable
);
1283 show_extended_objects(bitmap_git
, revs
, show_reachable
);
1286 static uint32_t count_object_type(struct bitmap_index
*bitmap_git
,
1287 enum object_type type
)
1289 struct bitmap
*objects
= bitmap_git
->result
;
1290 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1292 uint32_t i
= 0, count
= 0;
1293 struct ewah_iterator it
;
1296 init_type_iterator(&it
, bitmap_git
, type
);
1298 while (i
< objects
->word_alloc
&& ewah_iterator_next(&filter
, &it
)) {
1299 eword_t word
= objects
->words
[i
++] & filter
;
1300 count
+= ewah_bit_popcount64(word
);
1303 for (i
= 0; i
< eindex
->count
; ++i
) {
1304 if (eindex
->objects
[i
]->type
== type
&&
1305 bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1312 void count_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1313 uint32_t *commits
, uint32_t *trees
,
1314 uint32_t *blobs
, uint32_t *tags
)
1316 assert(bitmap_git
->result
);
1319 *commits
= count_object_type(bitmap_git
, OBJ_COMMIT
);
1322 *trees
= count_object_type(bitmap_git
, OBJ_TREE
);
1325 *blobs
= count_object_type(bitmap_git
, OBJ_BLOB
);
1328 *tags
= count_object_type(bitmap_git
, OBJ_TAG
);
1331 struct bitmap_test_data
{
1332 struct bitmap_index
*bitmap_git
;
1333 struct bitmap
*base
;
1334 struct bitmap
*commits
;
1335 struct bitmap
*trees
;
1336 struct bitmap
*blobs
;
1337 struct bitmap
*tags
;
1338 struct progress
*prg
;
1342 static void test_bitmap_type(struct bitmap_test_data
*tdata
,
1343 struct object
*obj
, int pos
)
1345 enum object_type bitmap_type
= OBJ_NONE
;
1348 if (bitmap_get(tdata
->commits
, pos
)) {
1349 bitmap_type
= OBJ_COMMIT
;
1352 if (bitmap_get(tdata
->trees
, pos
)) {
1353 bitmap_type
= OBJ_TREE
;
1356 if (bitmap_get(tdata
->blobs
, pos
)) {
1357 bitmap_type
= OBJ_BLOB
;
1360 if (bitmap_get(tdata
->tags
, pos
)) {
1361 bitmap_type
= OBJ_TAG
;
1365 if (bitmap_type
== OBJ_NONE
)
1366 die("object %s not found in type bitmaps",
1367 oid_to_hex(&obj
->oid
));
1370 die("object %s does not have a unique type",
1371 oid_to_hex(&obj
->oid
));
1373 if (bitmap_type
!= obj
->type
)
1374 die("object %s: real type %s, expected: %s",
1375 oid_to_hex(&obj
->oid
),
1376 type_name(obj
->type
),
1377 type_name(bitmap_type
));
1380 static void test_show_object(struct object
*object
, const char *name
,
1383 struct bitmap_test_data
*tdata
= data
;
1386 bitmap_pos
= bitmap_position(tdata
->bitmap_git
, &object
->oid
);
1388 die("Object not in bitmap: %s\n", oid_to_hex(&object
->oid
));
1389 test_bitmap_type(tdata
, object
, bitmap_pos
);
1391 bitmap_set(tdata
->base
, bitmap_pos
);
1392 display_progress(tdata
->prg
, ++tdata
->seen
);
1395 static void test_show_commit(struct commit
*commit
, void *data
)
1397 struct bitmap_test_data
*tdata
= data
;
1400 bitmap_pos
= bitmap_position(tdata
->bitmap_git
,
1401 &commit
->object
.oid
);
1403 die("Object not in bitmap: %s\n", oid_to_hex(&commit
->object
.oid
));
1404 test_bitmap_type(tdata
, &commit
->object
, bitmap_pos
);
1406 bitmap_set(tdata
->base
, bitmap_pos
);
1407 display_progress(tdata
->prg
, ++tdata
->seen
);
1410 void test_bitmap_walk(struct rev_info
*revs
)
1412 struct object
*root
;
1413 struct bitmap
*result
= NULL
;
1414 size_t result_popcnt
;
1415 struct bitmap_test_data tdata
;
1416 struct bitmap_index
*bitmap_git
;
1417 struct ewah_bitmap
*bm
;
1419 if (!(bitmap_git
= prepare_bitmap_git(revs
->repo
)))
1420 die("failed to load bitmap indexes");
1422 if (revs
->pending
.nr
!= 1)
1423 die("you must specify exactly one commit to test");
1425 fprintf(stderr
, "Bitmap v%d test (%d entries loaded)\n",
1426 bitmap_git
->version
, bitmap_git
->entry_count
);
1428 root
= revs
->pending
.objects
[0].item
;
1429 bm
= bitmap_for_commit(bitmap_git
, (struct commit
*)root
);
1432 fprintf(stderr
, "Found bitmap for %s. %d bits / %08x checksum\n",
1433 oid_to_hex(&root
->oid
), (int)bm
->bit_size
, ewah_checksum(bm
));
1435 result
= ewah_to_bitmap(bm
);
1439 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root
->oid
));
1441 revs
->tag_objects
= 1;
1442 revs
->tree_objects
= 1;
1443 revs
->blob_objects
= 1;
1445 result_popcnt
= bitmap_popcount(result
);
1447 if (prepare_revision_walk(revs
))
1448 die("revision walk setup failed");
1450 tdata
.bitmap_git
= bitmap_git
;
1451 tdata
.base
= bitmap_new();
1452 tdata
.commits
= ewah_to_bitmap(bitmap_git
->commits
);
1453 tdata
.trees
= ewah_to_bitmap(bitmap_git
->trees
);
1454 tdata
.blobs
= ewah_to_bitmap(bitmap_git
->blobs
);
1455 tdata
.tags
= ewah_to_bitmap(bitmap_git
->tags
);
1456 tdata
.prg
= start_progress("Verifying bitmap entries", result_popcnt
);
1459 traverse_commit_list(revs
, &test_show_commit
, &test_show_object
, &tdata
);
1461 stop_progress(&tdata
.prg
);
1463 if (bitmap_equals(result
, tdata
.base
))
1464 fprintf(stderr
, "OK!\n");
1466 die("mismatch in bitmap results");
1468 free_bitmap_index(bitmap_git
);
1471 int test_bitmap_commits(struct repository
*r
)
1473 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
1474 struct object_id oid
;
1475 MAYBE_UNUSED
void *value
;
1478 die("failed to load bitmap indexes");
1480 kh_foreach(bitmap_git
->bitmaps
, oid
, value
, {
1481 printf("%s\n", oid_to_hex(&oid
));
1484 free_bitmap_index(bitmap_git
);
1489 int rebuild_bitmap(const uint32_t *reposition
,
1490 struct ewah_bitmap
*source
,
1491 struct bitmap
*dest
)
1494 struct ewah_iterator it
;
1497 ewah_iterator_init(&it
, source
);
1499 while (ewah_iterator_next(&word
, &it
)) {
1500 uint32_t offset
, bit_pos
;
1502 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1503 if ((word
>> offset
) == 0)
1506 offset
+= ewah_bit_ctz64(word
>> offset
);
1508 bit_pos
= reposition
[pos
+ offset
];
1510 bitmap_set(dest
, bit_pos
- 1);
1511 else /* can't reuse, we don't have the object */
1515 pos
+= BITS_IN_EWORD
;
1520 uint32_t *create_bitmap_mapping(struct bitmap_index
*bitmap_git
,
1521 struct packing_data
*mapping
)
1523 uint32_t i
, num_objects
;
1524 uint32_t *reposition
;
1526 num_objects
= bitmap_num_objects(bitmap_git
);
1527 CALLOC_ARRAY(reposition
, num_objects
);
1529 for (i
= 0; i
< num_objects
; ++i
) {
1530 struct object_id oid
;
1531 struct object_entry
*oe
;
1533 nth_packed_object_id(&oid
, bitmap_git
->pack
,
1534 pack_pos_to_index(bitmap_git
->pack
, i
));
1535 oe
= packlist_find(mapping
, &oid
);
1538 reposition
[i
] = oe_in_pack_pos(mapping
, oe
) + 1;
1544 void free_bitmap_index(struct bitmap_index
*b
)
1550 munmap(b
->map
, b
->map_size
);
1551 ewah_pool_free(b
->commits
);
1552 ewah_pool_free(b
->trees
);
1553 ewah_pool_free(b
->blobs
);
1554 ewah_pool_free(b
->tags
);
1555 kh_destroy_oid_map(b
->bitmaps
);
1556 free(b
->ext_index
.objects
);
1557 free(b
->ext_index
.hashes
);
1558 bitmap_free(b
->result
);
1559 bitmap_free(b
->haves
);
1563 int bitmap_has_oid_in_uninteresting(struct bitmap_index
*bitmap_git
,
1564 const struct object_id
*oid
)
1566 return bitmap_git
&&
1567 bitmap_walk_contains(bitmap_git
, bitmap_git
->haves
, oid
);
1570 static off_t
get_disk_usage_for_type(struct bitmap_index
*bitmap_git
,
1571 enum object_type object_type
)
1573 struct bitmap
*result
= bitmap_git
->result
;
1574 struct packed_git
*pack
= bitmap_git
->pack
;
1576 struct ewah_iterator it
;
1580 init_type_iterator(&it
, bitmap_git
, object_type
);
1581 for (i
= 0; i
< result
->word_alloc
&&
1582 ewah_iterator_next(&filter
, &it
); i
++) {
1583 eword_t word
= result
->words
[i
] & filter
;
1584 size_t base
= (i
* BITS_IN_EWORD
);
1590 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1593 if ((word
>> offset
) == 0)
1596 offset
+= ewah_bit_ctz64(word
>> offset
);
1597 pos
= base
+ offset
;
1598 total
+= pack_pos_to_offset(pack
, pos
+ 1) -
1599 pack_pos_to_offset(pack
, pos
);
1606 static off_t
get_disk_usage_for_extended(struct bitmap_index
*bitmap_git
)
1608 struct bitmap
*result
= bitmap_git
->result
;
1609 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1611 struct object_info oi
= OBJECT_INFO_INIT
;
1615 oi
.disk_sizep
= &object_size
;
1617 for (i
= 0; i
< eindex
->count
; i
++) {
1618 struct object
*obj
= eindex
->objects
[i
];
1620 if (!bitmap_get(result
, bitmap_num_objects(bitmap_git
) + i
))
1623 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
1624 die(_("unable to get disk usage of %s"),
1625 oid_to_hex(&obj
->oid
));
1627 total
+= object_size
;
1632 off_t
get_disk_usage_from_bitmap(struct bitmap_index
*bitmap_git
,
1633 struct rev_info
*revs
)
1637 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_COMMIT
);
1638 if (revs
->tree_objects
)
1639 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TREE
);
1640 if (revs
->blob_objects
)
1641 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_BLOB
);
1642 if (revs
->tag_objects
)
1643 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TAG
);
1645 total
+= get_disk_usage_for_extended(bitmap_git
);
1650 const struct string_list
*bitmap_preferred_tips(struct repository
*r
)
1652 return repo_config_get_value_multi(r
, "pack.preferbitmaptips");