8 #include "list-objects.h"
10 #include "pack-bitmap.h"
11 #include "pack-revindex.h"
12 #include "pack-objects.h"
14 #include "repository.h"
15 #include "object-store.h"
16 #include "list-objects-filter-options.h"
21 * An entry on the bitmap index, representing the bitmap for a given
24 struct stored_bitmap
{
26 struct ewah_bitmap
*root
;
27 struct stored_bitmap
*xor;
32 * The active bitmap index for a repository. By design, repositories only have
33 * a single bitmap index available (the index for the biggest packfile in
34 * the repository), since bitmap indexes need full closure.
36 * If there is more than one bitmap index available (e.g. because of alternates),
37 * the active bitmap index is the largest one.
41 * The pack or multi-pack index (MIDX) that this bitmap index belongs
44 * Exactly one of these must be non-NULL; this specifies the object
45 * order used to interpret this bitmap.
47 struct packed_git
*pack
;
48 struct multi_pack_index
*midx
;
51 * Mark the first `reuse_objects` in the packfile as reused:
52 * they will be sent as-is without using them for repacking
55 uint32_t reuse_objects
;
57 /* mmapped buffer of the whole bitmap index */
59 size_t map_size
; /* size of the mmaped buffer */
60 size_t map_pos
; /* current position when loading the index */
65 * Each bitmap marks which objects in the packfile are of the given
66 * type. This provides type information when yielding the objects from
67 * the packfile during a walk, which allows for better delta bases.
69 struct ewah_bitmap
*commits
;
70 struct ewah_bitmap
*trees
;
71 struct ewah_bitmap
*blobs
;
72 struct ewah_bitmap
*tags
;
74 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
75 kh_oid_map_t
*bitmaps
;
77 /* Number of bitmapped commits */
80 /* If not NULL, this is a name-hash cache pointing into map. */
83 /* The checksum of the packfile or MIDX; points into map. */
84 const unsigned char *checksum
;
89 * When trying to perform bitmap operations with objects that are not
90 * packed in `pack`, these objects are added to this "fake index" and
91 * are assumed to appear at the end of the packfile for all operations
94 struct object
**objects
;
96 uint32_t count
, alloc
;
97 kh_oid_pos_t
*positions
;
100 /* Bitmap result of the last performed walk */
101 struct bitmap
*result
;
103 /* "have" bitmap from the last performed walk */
104 struct bitmap
*haves
;
106 /* Version of the bitmap index */
107 unsigned int version
;
110 static struct ewah_bitmap
*lookup_stored_bitmap(struct stored_bitmap
*st
)
112 struct ewah_bitmap
*parent
;
113 struct ewah_bitmap
*composed
;
118 composed
= ewah_pool_new();
119 parent
= lookup_stored_bitmap(st
->xor);
120 ewah_xor(st
->root
, parent
, composed
);
122 ewah_pool_free(st
->root
);
130 * Read a bitmap from the current read position on the mmaped
131 * index, and increase the read position accordingly
133 static struct ewah_bitmap
*read_bitmap_1(struct bitmap_index
*index
)
135 struct ewah_bitmap
*b
= ewah_pool_new();
137 ssize_t bitmap_size
= ewah_read_mmap(b
,
138 index
->map
+ index
->map_pos
,
139 index
->map_size
- index
->map_pos
);
141 if (bitmap_size
< 0) {
142 error(_("failed to load bitmap index (corrupted?)"));
147 index
->map_pos
+= bitmap_size
;
151 static uint32_t bitmap_num_objects(struct bitmap_index
*index
)
154 return index
->midx
->num_objects
;
155 return index
->pack
->num_objects
;
158 static int load_bitmap_header(struct bitmap_index
*index
)
160 struct bitmap_disk_header
*header
= (void *)index
->map
;
161 size_t header_size
= sizeof(*header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
;
163 if (index
->map_size
< header_size
+ the_hash_algo
->rawsz
)
164 return error(_("corrupted bitmap index (too small)"));
166 if (memcmp(header
->magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
)) != 0)
167 return error(_("corrupted bitmap index file (wrong header)"));
169 index
->version
= ntohs(header
->version
);
170 if (index
->version
!= 1)
171 return error(_("unsupported version '%d' for bitmap index file"), index
->version
);
173 /* Parse known bitmap format options */
175 uint32_t flags
= ntohs(header
->options
);
176 size_t cache_size
= st_mult(bitmap_num_objects(index
), sizeof(uint32_t));
177 unsigned char *index_end
= index
->map
+ index
->map_size
- the_hash_algo
->rawsz
;
179 if ((flags
& BITMAP_OPT_FULL_DAG
) == 0)
180 BUG("unsupported options for bitmap index file "
181 "(Git requires BITMAP_OPT_FULL_DAG)");
183 if (flags
& BITMAP_OPT_HASH_CACHE
) {
184 if (cache_size
> index_end
- index
->map
- header_size
)
185 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
186 index
->hashes
= (void *)(index_end
- cache_size
);
187 index_end
-= cache_size
;
191 index
->entry_count
= ntohl(header
->entry_count
);
192 index
->checksum
= header
->checksum
;
193 index
->map_pos
+= header_size
;
197 static struct stored_bitmap
*store_bitmap(struct bitmap_index
*index
,
198 struct ewah_bitmap
*root
,
199 const struct object_id
*oid
,
200 struct stored_bitmap
*xor_with
,
203 struct stored_bitmap
*stored
;
207 stored
= xmalloc(sizeof(struct stored_bitmap
));
209 stored
->xor = xor_with
;
210 stored
->flags
= flags
;
211 oidcpy(&stored
->oid
, oid
);
213 hash_pos
= kh_put_oid_map(index
->bitmaps
, stored
->oid
, &ret
);
215 /* a 0 return code means the insertion succeeded with no changes,
216 * because the SHA1 already existed on the map. this is bad, there
217 * shouldn't be duplicated commits in the index */
219 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid
));
223 kh_value(index
->bitmaps
, hash_pos
) = stored
;
227 static inline uint32_t read_be32(const unsigned char *buffer
, size_t *pos
)
229 uint32_t result
= get_be32(buffer
+ *pos
);
230 (*pos
) += sizeof(result
);
234 static inline uint8_t read_u8(const unsigned char *buffer
, size_t *pos
)
236 return buffer
[(*pos
)++];
239 #define MAX_XOR_OFFSET 160
241 static int nth_bitmap_object_oid(struct bitmap_index
*index
,
242 struct object_id
*oid
,
246 return nth_midxed_object_oid(oid
, index
->midx
, n
) ? 0 : -1;
247 return nth_packed_object_id(oid
, index
->pack
, n
);
250 static int load_bitmap_entries_v1(struct bitmap_index
*index
)
253 struct stored_bitmap
*recent_bitmaps
[MAX_XOR_OFFSET
] = { NULL
};
255 for (i
= 0; i
< index
->entry_count
; ++i
) {
256 int xor_offset
, flags
;
257 struct ewah_bitmap
*bitmap
= NULL
;
258 struct stored_bitmap
*xor_bitmap
= NULL
;
259 uint32_t commit_idx_pos
;
260 struct object_id oid
;
262 if (index
->map_size
- index
->map_pos
< 6)
263 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i
);
265 commit_idx_pos
= read_be32(index
->map
, &index
->map_pos
);
266 xor_offset
= read_u8(index
->map
, &index
->map_pos
);
267 flags
= read_u8(index
->map
, &index
->map_pos
);
269 if (nth_bitmap_object_oid(index
, &oid
, commit_idx_pos
) < 0)
270 return error(_("corrupt ewah bitmap: commit index %u out of range"),
271 (unsigned)commit_idx_pos
);
273 bitmap
= read_bitmap_1(index
);
277 if (xor_offset
> MAX_XOR_OFFSET
|| xor_offset
> i
)
278 return error(_("corrupted bitmap pack index"));
280 if (xor_offset
> 0) {
281 xor_bitmap
= recent_bitmaps
[(i
- xor_offset
) % MAX_XOR_OFFSET
];
284 return error(_("invalid XOR offset in bitmap pack index"));
287 recent_bitmaps
[i
% MAX_XOR_OFFSET
] = store_bitmap(
288 index
, bitmap
, &oid
, xor_bitmap
, flags
);
294 char *midx_bitmap_filename(struct multi_pack_index
*midx
)
296 struct strbuf buf
= STRBUF_INIT
;
298 get_midx_filename(&buf
, midx
->object_dir
);
299 strbuf_addf(&buf
, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx
)));
301 return strbuf_detach(&buf
, NULL
);
304 char *pack_bitmap_filename(struct packed_git
*p
)
308 if (!strip_suffix(p
->pack_name
, ".pack", &len
))
309 BUG("pack_name does not end in .pack");
310 return xstrfmt("%.*s.bitmap", (int)len
, p
->pack_name
);
313 static int open_midx_bitmap_1(struct bitmap_index
*bitmap_git
,
314 struct multi_pack_index
*midx
)
317 char *bitmap_name
= midx_bitmap_filename(midx
);
318 int fd
= git_open(bitmap_name
);
320 struct packed_git
*preferred
;
324 warning_errno("cannot open '%s'", bitmap_name
);
330 if (fstat(fd
, &st
)) {
331 error_errno(_("cannot fstat bitmap file"));
336 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
337 struct strbuf buf
= STRBUF_INIT
;
338 get_midx_filename(&buf
, midx
->object_dir
);
339 /* ignore extra bitmap file; we can only handle one */
340 warning(_("ignoring extra bitmap file: '%s'"), buf
.buf
);
342 strbuf_release(&buf
);
346 bitmap_git
->midx
= midx
;
347 bitmap_git
->map_size
= xsize_t(st
.st_size
);
348 bitmap_git
->map_pos
= 0;
349 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
,
353 if (load_bitmap_header(bitmap_git
) < 0)
356 if (!hasheq(get_midx_checksum(bitmap_git
->midx
), bitmap_git
->checksum
)) {
357 error(_("checksum doesn't match in MIDX and bitmap"));
361 if (load_midx_revindex(bitmap_git
->midx
) < 0) {
362 warning(_("multi-pack bitmap is missing required reverse index"));
366 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
367 if (prepare_midx_pack(the_repository
, bitmap_git
->midx
, i
))
368 die(_("could not open pack %s"),
369 bitmap_git
->midx
->pack_names
[i
]);
372 preferred
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
373 if (!is_pack_valid(preferred
)) {
374 warning(_("preferred pack (%s) is invalid"),
375 preferred
->pack_name
);
382 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
383 bitmap_git
->map_size
= 0;
384 bitmap_git
->map_pos
= 0;
385 bitmap_git
->map
= NULL
;
386 bitmap_git
->midx
= NULL
;
390 static int open_pack_bitmap_1(struct bitmap_index
*bitmap_git
, struct packed_git
*packfile
)
396 if (open_pack_index(packfile
))
399 bitmap_name
= pack_bitmap_filename(packfile
);
400 fd
= git_open(bitmap_name
);
404 warning_errno("cannot open '%s'", bitmap_name
);
410 if (fstat(fd
, &st
)) {
411 error_errno(_("cannot fstat bitmap file"));
416 if (bitmap_git
->pack
|| bitmap_git
->midx
) {
417 /* ignore extra bitmap file; we can only handle one */
418 warning(_("ignoring extra bitmap file: '%s'"), packfile
->pack_name
);
423 if (!is_pack_valid(packfile
)) {
428 bitmap_git
->pack
= packfile
;
429 bitmap_git
->map_size
= xsize_t(st
.st_size
);
430 bitmap_git
->map
= xmmap(NULL
, bitmap_git
->map_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
431 bitmap_git
->map_pos
= 0;
434 if (load_bitmap_header(bitmap_git
) < 0) {
435 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
436 bitmap_git
->map
= NULL
;
437 bitmap_git
->map_size
= 0;
438 bitmap_git
->map_pos
= 0;
439 bitmap_git
->pack
= NULL
;
446 static int load_reverse_index(struct bitmap_index
*bitmap_git
)
448 if (bitmap_is_midx(bitmap_git
)) {
453 * The multi-pack-index's .rev file is already loaded via
454 * open_pack_bitmap_1().
456 * But we still need to open the individual pack .rev files,
457 * since we will need to make use of them in pack-objects.
459 for (i
= 0; i
< bitmap_git
->midx
->num_packs
; i
++) {
460 ret
= load_pack_revindex(bitmap_git
->midx
->packs
[i
]);
466 return load_pack_revindex(bitmap_git
->pack
);
469 static int load_bitmap(struct bitmap_index
*bitmap_git
)
471 assert(bitmap_git
->map
);
473 bitmap_git
->bitmaps
= kh_init_oid_map();
474 bitmap_git
->ext_index
.positions
= kh_init_oid_pos();
476 if (load_reverse_index(bitmap_git
))
479 if (!(bitmap_git
->commits
= read_bitmap_1(bitmap_git
)) ||
480 !(bitmap_git
->trees
= read_bitmap_1(bitmap_git
)) ||
481 !(bitmap_git
->blobs
= read_bitmap_1(bitmap_git
)) ||
482 !(bitmap_git
->tags
= read_bitmap_1(bitmap_git
)))
485 if (load_bitmap_entries_v1(bitmap_git
) < 0)
491 munmap(bitmap_git
->map
, bitmap_git
->map_size
);
492 bitmap_git
->map
= NULL
;
493 bitmap_git
->map_size
= 0;
495 kh_destroy_oid_map(bitmap_git
->bitmaps
);
496 bitmap_git
->bitmaps
= NULL
;
498 kh_destroy_oid_pos(bitmap_git
->ext_index
.positions
);
499 bitmap_git
->ext_index
.positions
= NULL
;
504 static int open_pack_bitmap(struct repository
*r
,
505 struct bitmap_index
*bitmap_git
)
507 struct packed_git
*p
;
510 assert(!bitmap_git
->map
);
512 for (p
= get_all_packs(r
); p
; p
= p
->next
) {
513 if (open_pack_bitmap_1(bitmap_git
, p
) == 0)
520 static int open_midx_bitmap(struct repository
*r
,
521 struct bitmap_index
*bitmap_git
)
524 struct multi_pack_index
*midx
;
526 assert(!bitmap_git
->map
);
528 for (midx
= get_multi_pack_index(r
); midx
; midx
= midx
->next
) {
529 if (!open_midx_bitmap_1(bitmap_git
, midx
))
535 static int open_bitmap(struct repository
*r
,
536 struct bitmap_index
*bitmap_git
)
538 assert(!bitmap_git
->map
);
540 if (!open_midx_bitmap(r
, bitmap_git
))
542 return open_pack_bitmap(r
, bitmap_git
);
545 struct bitmap_index
*prepare_bitmap_git(struct repository
*r
)
547 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
549 if (!open_bitmap(r
, bitmap_git
) && !load_bitmap(bitmap_git
))
552 free_bitmap_index(bitmap_git
);
556 struct bitmap_index
*prepare_midx_bitmap_git(struct multi_pack_index
*midx
)
558 struct bitmap_index
*bitmap_git
= xcalloc(1, sizeof(*bitmap_git
));
560 if (!open_midx_bitmap_1(bitmap_git
, midx
) && !load_bitmap(bitmap_git
))
563 free_bitmap_index(bitmap_git
);
567 struct include_data
{
568 struct bitmap_index
*bitmap_git
;
573 struct ewah_bitmap
*bitmap_for_commit(struct bitmap_index
*bitmap_git
,
574 struct commit
*commit
)
576 khiter_t hash_pos
= kh_get_oid_map(bitmap_git
->bitmaps
,
578 if (hash_pos
>= kh_end(bitmap_git
->bitmaps
))
580 return lookup_stored_bitmap(kh_value(bitmap_git
->bitmaps
, hash_pos
));
583 static inline int bitmap_position_extended(struct bitmap_index
*bitmap_git
,
584 const struct object_id
*oid
)
586 kh_oid_pos_t
*positions
= bitmap_git
->ext_index
.positions
;
587 khiter_t pos
= kh_get_oid_pos(positions
, *oid
);
589 if (pos
< kh_end(positions
)) {
590 int bitmap_pos
= kh_value(positions
, pos
);
591 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
597 static inline int bitmap_position_packfile(struct bitmap_index
*bitmap_git
,
598 const struct object_id
*oid
)
601 off_t offset
= find_pack_entry_one(oid
->hash
, bitmap_git
->pack
);
605 if (offset_to_pack_pos(bitmap_git
->pack
, offset
, &pos
) < 0)
610 static int bitmap_position_midx(struct bitmap_index
*bitmap_git
,
611 const struct object_id
*oid
)
614 if (!bsearch_midx(oid
, bitmap_git
->midx
, &want
))
617 if (midx_to_pack_pos(bitmap_git
->midx
, want
, &got
) < 0)
622 static int bitmap_position(struct bitmap_index
*bitmap_git
,
623 const struct object_id
*oid
)
626 if (bitmap_is_midx(bitmap_git
))
627 pos
= bitmap_position_midx(bitmap_git
, oid
);
629 pos
= bitmap_position_packfile(bitmap_git
, oid
);
630 return (pos
>= 0) ? pos
: bitmap_position_extended(bitmap_git
, oid
);
633 static int ext_index_add_object(struct bitmap_index
*bitmap_git
,
634 struct object
*object
, const char *name
)
636 struct eindex
*eindex
= &bitmap_git
->ext_index
;
642 hash_pos
= kh_put_oid_pos(eindex
->positions
, object
->oid
, &hash_ret
);
644 if (eindex
->count
>= eindex
->alloc
) {
645 eindex
->alloc
= (eindex
->alloc
+ 16) * 3 / 2;
646 REALLOC_ARRAY(eindex
->objects
, eindex
->alloc
);
647 REALLOC_ARRAY(eindex
->hashes
, eindex
->alloc
);
650 bitmap_pos
= eindex
->count
;
651 eindex
->objects
[eindex
->count
] = object
;
652 eindex
->hashes
[eindex
->count
] = pack_name_hash(name
);
653 kh_value(eindex
->positions
, hash_pos
) = bitmap_pos
;
656 bitmap_pos
= kh_value(eindex
->positions
, hash_pos
);
659 return bitmap_pos
+ bitmap_num_objects(bitmap_git
);
662 struct bitmap_show_data
{
663 struct bitmap_index
*bitmap_git
;
667 static void show_object(struct object
*object
, const char *name
, void *data_
)
669 struct bitmap_show_data
*data
= data_
;
672 bitmap_pos
= bitmap_position(data
->bitmap_git
, &object
->oid
);
675 bitmap_pos
= ext_index_add_object(data
->bitmap_git
, object
,
678 bitmap_set(data
->base
, bitmap_pos
);
681 static void show_commit(struct commit
*commit
, void *data
)
685 static int add_to_include_set(struct bitmap_index
*bitmap_git
,
686 struct include_data
*data
,
687 struct commit
*commit
,
690 struct ewah_bitmap
*partial
;
692 if (data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
))
695 if (bitmap_get(data
->base
, bitmap_pos
))
698 partial
= bitmap_for_commit(bitmap_git
, commit
);
700 bitmap_or_ewah(data
->base
, partial
);
704 bitmap_set(data
->base
, bitmap_pos
);
708 static int should_include(struct commit
*commit
, void *_data
)
710 struct include_data
*data
= _data
;
713 bitmap_pos
= bitmap_position(data
->bitmap_git
, &commit
->object
.oid
);
715 bitmap_pos
= ext_index_add_object(data
->bitmap_git
,
716 (struct object
*)commit
,
719 if (!add_to_include_set(data
->bitmap_git
, data
, commit
, bitmap_pos
)) {
720 struct commit_list
*parent
= commit
->parents
;
723 parent
->item
->object
.flags
|= SEEN
;
724 parent
= parent
->next
;
733 static int should_include_obj(struct object
*obj
, void *_data
)
735 struct include_data
*data
= _data
;
738 bitmap_pos
= bitmap_position(data
->bitmap_git
, &obj
->oid
);
741 if ((data
->seen
&& bitmap_get(data
->seen
, bitmap_pos
)) ||
742 bitmap_get(data
->base
, bitmap_pos
)) {
749 static int add_commit_to_bitmap(struct bitmap_index
*bitmap_git
,
750 struct bitmap
**base
,
751 struct commit
*commit
)
753 struct ewah_bitmap
*or_with
= bitmap_for_commit(bitmap_git
, commit
);
759 *base
= ewah_to_bitmap(or_with
);
761 bitmap_or_ewah(*base
, or_with
);
766 static struct bitmap
*find_objects(struct bitmap_index
*bitmap_git
,
767 struct rev_info
*revs
,
768 struct object_list
*roots
,
771 struct bitmap
*base
= NULL
;
774 struct object_list
*not_mapped
= NULL
;
777 * Go through all the roots for the walk. The ones that have bitmaps
778 * on the bitmap index will be `or`ed together to form an initial
779 * global reachability analysis.
781 * The ones without bitmaps in the index will be stored in the
782 * `not_mapped_list` for further processing.
785 struct object
*object
= roots
->item
;
788 if (object
->type
== OBJ_COMMIT
&&
789 add_commit_to_bitmap(bitmap_git
, &base
, (struct commit
*)object
)) {
790 object
->flags
|= SEEN
;
794 object_list_insert(object
, ¬_mapped
);
798 * Best case scenario: We found bitmaps for all the roots,
799 * so the resulting `or` bitmap has the full reachability analysis
807 * Let's iterate through all the roots that don't have bitmaps to
808 * check if we can determine them to be reachable from the existing
811 * If we cannot find them in the existing global bitmap, we'll need
812 * to push them to an actual walk and run it until we can confirm
816 struct object
*object
= roots
->item
;
820 pos
= bitmap_position(bitmap_git
, &object
->oid
);
822 if (pos
< 0 || base
== NULL
|| !bitmap_get(base
, pos
)) {
823 object
->flags
&= ~UNINTERESTING
;
824 add_pending_object(revs
, object
, "");
827 object
->flags
|= SEEN
;
832 struct include_data incdata
;
833 struct bitmap_show_data show_data
;
838 incdata
.bitmap_git
= bitmap_git
;
842 revs
->include_check
= should_include
;
843 revs
->include_check_obj
= should_include_obj
;
844 revs
->include_check_data
= &incdata
;
846 if (prepare_revision_walk(revs
))
847 die(_("revision walk setup failed"));
849 show_data
.bitmap_git
= bitmap_git
;
850 show_data
.base
= base
;
852 traverse_commit_list(revs
,
853 show_commit
, show_object
,
856 revs
->include_check
= NULL
;
857 revs
->include_check_obj
= NULL
;
858 revs
->include_check_data
= NULL
;
864 static void show_extended_objects(struct bitmap_index
*bitmap_git
,
865 struct rev_info
*revs
,
866 show_reachable_fn show_reach
)
868 struct bitmap
*objects
= bitmap_git
->result
;
869 struct eindex
*eindex
= &bitmap_git
->ext_index
;
872 for (i
= 0; i
< eindex
->count
; ++i
) {
875 if (!bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
878 obj
= eindex
->objects
[i
];
879 if ((obj
->type
== OBJ_BLOB
&& !revs
->blob_objects
) ||
880 (obj
->type
== OBJ_TREE
&& !revs
->tree_objects
) ||
881 (obj
->type
== OBJ_TAG
&& !revs
->tag_objects
))
884 show_reach(&obj
->oid
, obj
->type
, 0, eindex
->hashes
[i
], NULL
, 0);
888 static void init_type_iterator(struct ewah_iterator
*it
,
889 struct bitmap_index
*bitmap_git
,
890 enum object_type type
)
894 ewah_iterator_init(it
, bitmap_git
->commits
);
898 ewah_iterator_init(it
, bitmap_git
->trees
);
902 ewah_iterator_init(it
, bitmap_git
->blobs
);
906 ewah_iterator_init(it
, bitmap_git
->tags
);
910 BUG("object type %d not stored by bitmap type index", type
);
915 static void show_objects_for_type(
916 struct bitmap_index
*bitmap_git
,
917 enum object_type object_type
,
918 show_reachable_fn show_reach
)
923 struct ewah_iterator it
;
926 struct bitmap
*objects
= bitmap_git
->result
;
928 init_type_iterator(&it
, bitmap_git
, object_type
);
930 for (i
= 0; i
< objects
->word_alloc
&&
931 ewah_iterator_next(&filter
, &it
); i
++) {
932 eword_t word
= objects
->words
[i
] & filter
;
933 size_t pos
= (i
* BITS_IN_EWORD
);
938 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
939 struct packed_git
*pack
;
940 struct object_id oid
;
941 uint32_t hash
= 0, index_pos
;
944 if ((word
>> offset
) == 0)
947 offset
+= ewah_bit_ctz64(word
>> offset
);
949 if (bitmap_is_midx(bitmap_git
)) {
950 struct multi_pack_index
*m
= bitmap_git
->midx
;
953 index_pos
= pack_pos_to_midx(m
, pos
+ offset
);
954 ofs
= nth_midxed_offset(m
, index_pos
);
955 nth_midxed_object_oid(&oid
, m
, index_pos
);
957 pack_id
= nth_midxed_pack_int_id(m
, index_pos
);
958 pack
= bitmap_git
->midx
->packs
[pack_id
];
960 index_pos
= pack_pos_to_index(bitmap_git
->pack
, pos
+ offset
);
961 ofs
= pack_pos_to_offset(bitmap_git
->pack
, pos
+ offset
);
962 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
964 pack
= bitmap_git
->pack
;
967 if (bitmap_git
->hashes
)
968 hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
970 show_reach(&oid
, object_type
, 0, hash
, pack
, ofs
);
975 static int in_bitmapped_pack(struct bitmap_index
*bitmap_git
,
976 struct object_list
*roots
)
979 struct object
*object
= roots
->item
;
982 if (bitmap_is_midx(bitmap_git
)) {
983 if (bsearch_midx(&object
->oid
, bitmap_git
->midx
, NULL
))
986 if (find_pack_entry_one(object
->oid
.hash
, bitmap_git
->pack
) > 0)
994 static struct bitmap
*find_tip_objects(struct bitmap_index
*bitmap_git
,
995 struct object_list
*tip_objects
,
996 enum object_type type
)
998 struct bitmap
*result
= bitmap_new();
999 struct object_list
*p
;
1001 for (p
= tip_objects
; p
; p
= p
->next
) {
1004 if (p
->item
->type
!= type
)
1007 pos
= bitmap_position(bitmap_git
, &p
->item
->oid
);
1011 bitmap_set(result
, pos
);
1017 static void filter_bitmap_exclude_type(struct bitmap_index
*bitmap_git
,
1018 struct object_list
*tip_objects
,
1019 struct bitmap
*to_filter
,
1020 enum object_type type
)
1022 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1023 struct bitmap
*tips
;
1024 struct ewah_iterator it
;
1029 * The non-bitmap version of this filter never removes
1030 * objects which the other side specifically asked for,
1031 * so we must match that behavior.
1033 tips
= find_tip_objects(bitmap_git
, tip_objects
, type
);
1036 * We can use the type-level bitmap for 'type' to work in whole
1037 * words for the objects that are actually in the bitmapped
1040 for (i
= 0, init_type_iterator(&it
, bitmap_git
, type
);
1041 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1043 if (i
< tips
->word_alloc
)
1044 mask
&= ~tips
->words
[i
];
1045 to_filter
->words
[i
] &= ~mask
;
1049 * Clear any objects that weren't in the packfile (and so would
1050 * not have been caught by the loop above. We'll have to check
1051 * them individually.
1053 for (i
= 0; i
< eindex
->count
; i
++) {
1054 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1055 if (eindex
->objects
[i
]->type
== type
&&
1056 bitmap_get(to_filter
, pos
) &&
1057 !bitmap_get(tips
, pos
))
1058 bitmap_unset(to_filter
, pos
);
1064 static void filter_bitmap_blob_none(struct bitmap_index
*bitmap_git
,
1065 struct object_list
*tip_objects
,
1066 struct bitmap
*to_filter
)
1068 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1072 static unsigned long get_size_by_pos(struct bitmap_index
*bitmap_git
,
1076 struct object_info oi
= OBJECT_INFO_INIT
;
1080 if (pos
< bitmap_num_objects(bitmap_git
)) {
1081 struct packed_git
*pack
;
1084 if (bitmap_is_midx(bitmap_git
)) {
1085 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, pos
);
1086 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
1088 pack
= bitmap_git
->midx
->packs
[pack_id
];
1089 ofs
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
1091 pack
= bitmap_git
->pack
;
1092 ofs
= pack_pos_to_offset(pack
, pos
);
1095 if (packed_object_info(the_repository
, pack
, ofs
, &oi
) < 0) {
1096 struct object_id oid
;
1097 nth_bitmap_object_oid(bitmap_git
, &oid
,
1098 pack_pos_to_index(pack
, pos
));
1099 die(_("unable to get size of %s"), oid_to_hex(&oid
));
1102 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1103 struct object
*obj
= eindex
->objects
[pos
- bitmap_num_objects(bitmap_git
)];
1104 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
1105 die(_("unable to get size of %s"), oid_to_hex(&obj
->oid
));
1111 static void filter_bitmap_blob_limit(struct bitmap_index
*bitmap_git
,
1112 struct object_list
*tip_objects
,
1113 struct bitmap
*to_filter
,
1114 unsigned long limit
)
1116 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1117 struct bitmap
*tips
;
1118 struct ewah_iterator it
;
1122 tips
= find_tip_objects(bitmap_git
, tip_objects
, OBJ_BLOB
);
1124 for (i
= 0, init_type_iterator(&it
, bitmap_git
, OBJ_BLOB
);
1125 i
< to_filter
->word_alloc
&& ewah_iterator_next(&mask
, &it
);
1127 eword_t word
= to_filter
->words
[i
] & mask
;
1130 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1133 if ((word
>> offset
) == 0)
1135 offset
+= ewah_bit_ctz64(word
>> offset
);
1136 pos
= i
* BITS_IN_EWORD
+ offset
;
1138 if (!bitmap_get(tips
, pos
) &&
1139 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1140 bitmap_unset(to_filter
, pos
);
1144 for (i
= 0; i
< eindex
->count
; i
++) {
1145 uint32_t pos
= i
+ bitmap_num_objects(bitmap_git
);
1146 if (eindex
->objects
[i
]->type
== OBJ_BLOB
&&
1147 bitmap_get(to_filter
, pos
) &&
1148 !bitmap_get(tips
, pos
) &&
1149 get_size_by_pos(bitmap_git
, pos
) >= limit
)
1150 bitmap_unset(to_filter
, pos
);
1156 static void filter_bitmap_tree_depth(struct bitmap_index
*bitmap_git
,
1157 struct object_list
*tip_objects
,
1158 struct bitmap
*to_filter
,
1159 unsigned long limit
)
1162 BUG("filter_bitmap_tree_depth given non-zero limit");
1164 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1166 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
,
1170 static void filter_bitmap_object_type(struct bitmap_index
*bitmap_git
,
1171 struct object_list
*tip_objects
,
1172 struct bitmap
*to_filter
,
1173 enum object_type object_type
)
1175 if (object_type
< OBJ_COMMIT
|| object_type
> OBJ_TAG
)
1176 BUG("filter_bitmap_object_type given invalid object");
1178 if (object_type
!= OBJ_TAG
)
1179 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TAG
);
1180 if (object_type
!= OBJ_COMMIT
)
1181 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_COMMIT
);
1182 if (object_type
!= OBJ_TREE
)
1183 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_TREE
);
1184 if (object_type
!= OBJ_BLOB
)
1185 filter_bitmap_exclude_type(bitmap_git
, tip_objects
, to_filter
, OBJ_BLOB
);
1188 static int filter_bitmap(struct bitmap_index
*bitmap_git
,
1189 struct object_list
*tip_objects
,
1190 struct bitmap
*to_filter
,
1191 struct list_objects_filter_options
*filter
)
1193 if (!filter
|| filter
->choice
== LOFC_DISABLED
)
1196 if (filter
->choice
== LOFC_BLOB_NONE
) {
1198 filter_bitmap_blob_none(bitmap_git
, tip_objects
,
1203 if (filter
->choice
== LOFC_BLOB_LIMIT
) {
1205 filter_bitmap_blob_limit(bitmap_git
, tip_objects
,
1207 filter
->blob_limit_value
);
1211 if (filter
->choice
== LOFC_TREE_DEPTH
&&
1212 filter
->tree_exclude_depth
== 0) {
1214 filter_bitmap_tree_depth(bitmap_git
, tip_objects
,
1216 filter
->tree_exclude_depth
);
1220 if (filter
->choice
== LOFC_OBJECT_TYPE
) {
1222 filter_bitmap_object_type(bitmap_git
, tip_objects
,
1224 filter
->object_type
);
1228 if (filter
->choice
== LOFC_COMBINE
) {
1230 for (i
= 0; i
< filter
->sub_nr
; i
++) {
1231 if (filter_bitmap(bitmap_git
, tip_objects
, to_filter
,
1232 &filter
->sub
[i
]) < 0)
1238 /* filter choice not handled */
1242 static int can_filter_bitmap(struct list_objects_filter_options
*filter
)
1244 return !filter_bitmap(NULL
, NULL
, NULL
, filter
);
1247 struct bitmap_index
*prepare_bitmap_walk(struct rev_info
*revs
,
1248 int filter_provided_objects
)
1252 struct object_list
*wants
= NULL
;
1253 struct object_list
*haves
= NULL
;
1255 struct bitmap
*wants_bitmap
= NULL
;
1256 struct bitmap
*haves_bitmap
= NULL
;
1258 struct bitmap_index
*bitmap_git
;
1261 * We can't do pathspec limiting with bitmaps, because we don't know
1262 * which commits are associated with which object changes (let alone
1263 * even which objects are associated with which paths).
1268 if (!can_filter_bitmap(&revs
->filter
))
1271 /* try to open a bitmapped pack, but don't parse it yet
1272 * because we may not need to use it */
1273 CALLOC_ARRAY(bitmap_git
, 1);
1274 if (open_bitmap(revs
->repo
, bitmap_git
) < 0)
1277 for (i
= 0; i
< revs
->pending
.nr
; ++i
) {
1278 struct object
*object
= revs
->pending
.objects
[i
].item
;
1280 if (object
->type
== OBJ_NONE
)
1281 parse_object_or_die(&object
->oid
, NULL
);
1283 while (object
->type
== OBJ_TAG
) {
1284 struct tag
*tag
= (struct tag
*) object
;
1286 if (object
->flags
& UNINTERESTING
)
1287 object_list_insert(object
, &haves
);
1289 object_list_insert(object
, &wants
);
1291 object
= parse_object_or_die(get_tagged_oid(tag
), NULL
);
1292 object
->flags
|= (tag
->object
.flags
& UNINTERESTING
);
1295 if (object
->flags
& UNINTERESTING
)
1296 object_list_insert(object
, &haves
);
1298 object_list_insert(object
, &wants
);
1302 * if we have a HAVES list, but none of those haves is contained
1303 * in the packfile that has a bitmap, we don't have anything to
1306 if (haves
&& !in_bitmapped_pack(bitmap_git
, haves
))
1309 /* if we don't want anything, we're done here */
1314 * now we're going to use bitmaps, so load the actual bitmap entries
1315 * from disk. this is the point of no return; after this the rev_list
1316 * becomes invalidated and we must perform the revwalk through bitmaps
1318 if (load_bitmap(bitmap_git
) < 0)
1321 object_array_clear(&revs
->pending
);
1324 revs
->ignore_missing_links
= 1;
1325 haves_bitmap
= find_objects(bitmap_git
, revs
, haves
, NULL
);
1326 reset_revision_walk();
1327 revs
->ignore_missing_links
= 0;
1330 BUG("failed to perform bitmap walk");
1333 wants_bitmap
= find_objects(bitmap_git
, revs
, wants
, haves_bitmap
);
1336 BUG("failed to perform bitmap walk");
1339 bitmap_and_not(wants_bitmap
, haves_bitmap
);
1341 filter_bitmap(bitmap_git
,
1342 (revs
->filter
.choice
&& filter_provided_objects
) ? NULL
: wants
,
1346 bitmap_git
->result
= wants_bitmap
;
1347 bitmap_git
->haves
= haves_bitmap
;
1349 object_list_free(&wants
);
1350 object_list_free(&haves
);
1355 free_bitmap_index(bitmap_git
);
1356 object_list_free(&wants
);
1357 object_list_free(&haves
);
1362 * -1 means "stop trying further objects"; 0 means we may or may not have
1363 * reused, but you can keep feeding bits.
1365 static int try_partial_reuse(struct packed_git
*pack
,
1367 struct bitmap
*reuse
,
1368 struct pack_window
**w_curs
)
1370 off_t offset
, delta_obj_offset
;
1371 enum object_type type
;
1375 * try_partial_reuse() is called either on (a) objects in the
1376 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1377 * objects in the preferred pack of a multi-pack bitmap.
1378 * Importantly, the latter can pretend as if only a single pack
1381 * - The first pack->num_objects bits of a MIDX bitmap are
1382 * reserved for the preferred pack, and
1384 * - Ties due to duplicate objects are always resolved in
1385 * favor of the preferred pack.
1387 * Therefore we do not need to ever ask the MIDX for its copy of
1388 * an object by OID, since it will always select it from the
1389 * preferred pack. Likewise, the selected copy of the base
1390 * object for any deltas will reside in the same pack.
1392 * This means that we can reuse pos when looking up the bit in
1393 * the reuse bitmap, too, since bits corresponding to the
1394 * preferred pack precede all bits from other packs.
1397 if (pos
>= pack
->num_objects
)
1398 return -1; /* not actually in the pack or MIDX preferred pack */
1400 offset
= delta_obj_offset
= pack_pos_to_offset(pack
, pos
);
1401 type
= unpack_object_header(pack
, w_curs
, &offset
, &size
);
1403 return -1; /* broken packfile, punt */
1405 if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
) {
1410 * Find the position of the base object so we can look it up
1411 * in our bitmaps. If we can't come up with an offset, or if
1412 * that offset is not in the revidx, the pack is corrupt.
1413 * There's nothing we can do, so just punt on this object,
1414 * and the normal slow path will complain about it in
1417 base_offset
= get_delta_base(pack
, w_curs
, &offset
, type
,
1421 if (offset_to_pack_pos(pack
, base_offset
, &base_pos
) < 0)
1425 * We assume delta dependencies always point backwards. This
1426 * lets us do a single pass, and is basically always true
1427 * due to the way OFS_DELTAs work. You would not typically
1428 * find REF_DELTA in a bitmapped pack, since we only bitmap
1429 * packs we write fresh, and OFS_DELTA is the default). But
1430 * let's double check to make sure the pack wasn't written with
1433 if (base_pos
>= pos
)
1437 * And finally, if we're not sending the base as part of our
1438 * reuse chunk, then don't send this object either. The base
1439 * would come after us, along with other objects not
1440 * necessarily in the pack, which means we'd need to convert
1441 * to REF_DELTA on the fly. Better to just let the normal
1442 * object_entry code path handle it.
1444 if (!bitmap_get(reuse
, base_pos
))
1449 * If we got here, then the object is OK to reuse. Mark it.
1451 bitmap_set(reuse
, pos
);
1455 uint32_t midx_preferred_pack(struct bitmap_index
*bitmap_git
)
1457 struct multi_pack_index
*m
= bitmap_git
->midx
;
1459 BUG("midx_preferred_pack: requires non-empty MIDX");
1460 return nth_midxed_pack_int_id(m
, pack_pos_to_midx(bitmap_git
->midx
, 0));
1463 int reuse_partial_packfile_from_bitmap(struct bitmap_index
*bitmap_git
,
1464 struct packed_git
**packfile_out
,
1466 struct bitmap
**reuse_out
)
1468 struct packed_git
*pack
;
1469 struct bitmap
*result
= bitmap_git
->result
;
1470 struct bitmap
*reuse
;
1471 struct pack_window
*w_curs
= NULL
;
1474 uint32_t objects_nr
;
1478 load_reverse_index(bitmap_git
);
1480 if (bitmap_is_midx(bitmap_git
))
1481 pack
= bitmap_git
->midx
->packs
[midx_preferred_pack(bitmap_git
)];
1483 pack
= bitmap_git
->pack
;
1484 objects_nr
= pack
->num_objects
;
1486 while (i
< result
->word_alloc
&& result
->words
[i
] == (eword_t
)~0)
1490 * Don't mark objects not in the packfile or preferred pack. This bitmap
1491 * marks objects eligible for reuse, but the pack-reuse code only
1492 * understands how to reuse a single pack. Since the preferred pack is
1493 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1494 * we use it instead of another pack. In single-pack bitmaps, the choice
1497 if (i
> objects_nr
/ BITS_IN_EWORD
)
1498 i
= objects_nr
/ BITS_IN_EWORD
;
1500 reuse
= bitmap_word_alloc(i
);
1501 memset(reuse
->words
, 0xFF, i
* sizeof(eword_t
));
1503 for (; i
< result
->word_alloc
; ++i
) {
1504 eword_t word
= result
->words
[i
];
1505 size_t pos
= (i
* BITS_IN_EWORD
);
1507 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1508 if ((word
>> offset
) == 0)
1511 offset
+= ewah_bit_ctz64(word
>> offset
);
1512 if (try_partial_reuse(pack
, pos
+ offset
,
1513 reuse
, &w_curs
) < 0) {
1515 * try_partial_reuse indicated we couldn't reuse
1516 * any bits, so there is no point in trying more
1517 * bits in the current word, or any other words
1520 * Jump out of both loops to avoid future
1521 * unnecessary calls to try_partial_reuse.
1529 unuse_pack(&w_curs
);
1531 *entries
= bitmap_popcount(reuse
);
1538 * Drop any reused objects from the result, since they will not
1539 * need to be handled separately.
1541 bitmap_and_not(result
, reuse
);
1542 *packfile_out
= pack
;
1547 int bitmap_walk_contains(struct bitmap_index
*bitmap_git
,
1548 struct bitmap
*bitmap
, const struct object_id
*oid
)
1555 idx
= bitmap_position(bitmap_git
, oid
);
1556 return idx
>= 0 && bitmap_get(bitmap
, idx
);
1559 void traverse_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1560 struct rev_info
*revs
,
1561 show_reachable_fn show_reachable
)
1563 assert(bitmap_git
->result
);
1565 show_objects_for_type(bitmap_git
, OBJ_COMMIT
, show_reachable
);
1566 if (revs
->tree_objects
)
1567 show_objects_for_type(bitmap_git
, OBJ_TREE
, show_reachable
);
1568 if (revs
->blob_objects
)
1569 show_objects_for_type(bitmap_git
, OBJ_BLOB
, show_reachable
);
1570 if (revs
->tag_objects
)
1571 show_objects_for_type(bitmap_git
, OBJ_TAG
, show_reachable
);
1573 show_extended_objects(bitmap_git
, revs
, show_reachable
);
1576 static uint32_t count_object_type(struct bitmap_index
*bitmap_git
,
1577 enum object_type type
)
1579 struct bitmap
*objects
= bitmap_git
->result
;
1580 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1582 uint32_t i
= 0, count
= 0;
1583 struct ewah_iterator it
;
1586 init_type_iterator(&it
, bitmap_git
, type
);
1588 while (i
< objects
->word_alloc
&& ewah_iterator_next(&filter
, &it
)) {
1589 eword_t word
= objects
->words
[i
++] & filter
;
1590 count
+= ewah_bit_popcount64(word
);
1593 for (i
= 0; i
< eindex
->count
; ++i
) {
1594 if (eindex
->objects
[i
]->type
== type
&&
1595 bitmap_get(objects
, bitmap_num_objects(bitmap_git
) + i
))
1602 void count_bitmap_commit_list(struct bitmap_index
*bitmap_git
,
1603 uint32_t *commits
, uint32_t *trees
,
1604 uint32_t *blobs
, uint32_t *tags
)
1606 assert(bitmap_git
->result
);
1609 *commits
= count_object_type(bitmap_git
, OBJ_COMMIT
);
1612 *trees
= count_object_type(bitmap_git
, OBJ_TREE
);
1615 *blobs
= count_object_type(bitmap_git
, OBJ_BLOB
);
1618 *tags
= count_object_type(bitmap_git
, OBJ_TAG
);
1621 struct bitmap_test_data
{
1622 struct bitmap_index
*bitmap_git
;
1623 struct bitmap
*base
;
1624 struct bitmap
*commits
;
1625 struct bitmap
*trees
;
1626 struct bitmap
*blobs
;
1627 struct bitmap
*tags
;
1628 struct progress
*prg
;
1632 static void test_bitmap_type(struct bitmap_test_data
*tdata
,
1633 struct object
*obj
, int pos
)
1635 enum object_type bitmap_type
= OBJ_NONE
;
1638 if (bitmap_get(tdata
->commits
, pos
)) {
1639 bitmap_type
= OBJ_COMMIT
;
1642 if (bitmap_get(tdata
->trees
, pos
)) {
1643 bitmap_type
= OBJ_TREE
;
1646 if (bitmap_get(tdata
->blobs
, pos
)) {
1647 bitmap_type
= OBJ_BLOB
;
1650 if (bitmap_get(tdata
->tags
, pos
)) {
1651 bitmap_type
= OBJ_TAG
;
1655 if (bitmap_type
== OBJ_NONE
)
1656 die(_("object '%s' not found in type bitmaps"),
1657 oid_to_hex(&obj
->oid
));
1660 die(_("object '%s' does not have a unique type"),
1661 oid_to_hex(&obj
->oid
));
1663 if (bitmap_type
!= obj
->type
)
1664 die(_("object '%s': real type '%s', expected: '%s'"),
1665 oid_to_hex(&obj
->oid
),
1666 type_name(obj
->type
),
1667 type_name(bitmap_type
));
1670 static void test_show_object(struct object
*object
, const char *name
,
1673 struct bitmap_test_data
*tdata
= data
;
1676 bitmap_pos
= bitmap_position(tdata
->bitmap_git
, &object
->oid
);
1678 die(_("object not in bitmap: '%s'"), oid_to_hex(&object
->oid
));
1679 test_bitmap_type(tdata
, object
, bitmap_pos
);
1681 bitmap_set(tdata
->base
, bitmap_pos
);
1682 display_progress(tdata
->prg
, ++tdata
->seen
);
1685 static void test_show_commit(struct commit
*commit
, void *data
)
1687 struct bitmap_test_data
*tdata
= data
;
1690 bitmap_pos
= bitmap_position(tdata
->bitmap_git
,
1691 &commit
->object
.oid
);
1693 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit
->object
.oid
));
1694 test_bitmap_type(tdata
, &commit
->object
, bitmap_pos
);
1696 bitmap_set(tdata
->base
, bitmap_pos
);
1697 display_progress(tdata
->prg
, ++tdata
->seen
);
1700 void test_bitmap_walk(struct rev_info
*revs
)
1702 struct object
*root
;
1703 struct bitmap
*result
= NULL
;
1704 size_t result_popcnt
;
1705 struct bitmap_test_data tdata
;
1706 struct bitmap_index
*bitmap_git
;
1707 struct ewah_bitmap
*bm
;
1709 if (!(bitmap_git
= prepare_bitmap_git(revs
->repo
)))
1710 die(_("failed to load bitmap indexes"));
1712 if (revs
->pending
.nr
!= 1)
1713 die(_("you must specify exactly one commit to test"));
1715 fprintf_ln(stderr
, "Bitmap v%d test (%d entries loaded)",
1716 bitmap_git
->version
, bitmap_git
->entry_count
);
1718 root
= revs
->pending
.objects
[0].item
;
1719 bm
= bitmap_for_commit(bitmap_git
, (struct commit
*)root
);
1722 fprintf_ln(stderr
, "Found bitmap for '%s'. %d bits / %08x checksum",
1723 oid_to_hex(&root
->oid
), (int)bm
->bit_size
, ewah_checksum(bm
));
1725 result
= ewah_to_bitmap(bm
);
1729 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root
->oid
));
1731 revs
->tag_objects
= 1;
1732 revs
->tree_objects
= 1;
1733 revs
->blob_objects
= 1;
1735 result_popcnt
= bitmap_popcount(result
);
1737 if (prepare_revision_walk(revs
))
1738 die(_("revision walk setup failed"));
1740 tdata
.bitmap_git
= bitmap_git
;
1741 tdata
.base
= bitmap_new();
1742 tdata
.commits
= ewah_to_bitmap(bitmap_git
->commits
);
1743 tdata
.trees
= ewah_to_bitmap(bitmap_git
->trees
);
1744 tdata
.blobs
= ewah_to_bitmap(bitmap_git
->blobs
);
1745 tdata
.tags
= ewah_to_bitmap(bitmap_git
->tags
);
1746 tdata
.prg
= start_progress("Verifying bitmap entries", result_popcnt
);
1749 traverse_commit_list(revs
, &test_show_commit
, &test_show_object
, &tdata
);
1751 stop_progress(&tdata
.prg
);
1753 if (bitmap_equals(result
, tdata
.base
))
1754 fprintf_ln(stderr
, "OK!");
1756 die(_("mismatch in bitmap results"));
1758 bitmap_free(result
);
1759 bitmap_free(tdata
.base
);
1760 bitmap_free(tdata
.commits
);
1761 bitmap_free(tdata
.trees
);
1762 bitmap_free(tdata
.blobs
);
1763 bitmap_free(tdata
.tags
);
1764 free_bitmap_index(bitmap_git
);
1767 int test_bitmap_commits(struct repository
*r
)
1769 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
1770 struct object_id oid
;
1771 MAYBE_UNUSED
void *value
;
1774 die(_("failed to load bitmap indexes"));
1776 kh_foreach(bitmap_git
->bitmaps
, oid
, value
, {
1777 printf_ln("%s", oid_to_hex(&oid
));
1780 free_bitmap_index(bitmap_git
);
1785 int test_bitmap_hashes(struct repository
*r
)
1787 struct bitmap_index
*bitmap_git
= prepare_bitmap_git(r
);
1788 struct object_id oid
;
1789 uint32_t i
, index_pos
;
1791 if (!bitmap_git
|| !bitmap_git
->hashes
)
1794 for (i
= 0; i
< bitmap_num_objects(bitmap_git
); i
++) {
1795 if (bitmap_is_midx(bitmap_git
))
1796 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
1798 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
1800 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
1802 printf_ln("%s %"PRIu32
"",
1803 oid_to_hex(&oid
), get_be32(bitmap_git
->hashes
+ index_pos
));
1807 free_bitmap_index(bitmap_git
);
1812 int rebuild_bitmap(const uint32_t *reposition
,
1813 struct ewah_bitmap
*source
,
1814 struct bitmap
*dest
)
1817 struct ewah_iterator it
;
1820 ewah_iterator_init(&it
, source
);
1822 while (ewah_iterator_next(&word
, &it
)) {
1823 uint32_t offset
, bit_pos
;
1825 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1826 if ((word
>> offset
) == 0)
1829 offset
+= ewah_bit_ctz64(word
>> offset
);
1831 bit_pos
= reposition
[pos
+ offset
];
1833 bitmap_set(dest
, bit_pos
- 1);
1834 else /* can't reuse, we don't have the object */
1838 pos
+= BITS_IN_EWORD
;
1843 uint32_t *create_bitmap_mapping(struct bitmap_index
*bitmap_git
,
1844 struct packing_data
*mapping
)
1846 uint32_t i
, num_objects
;
1847 uint32_t *reposition
;
1849 if (!bitmap_is_midx(bitmap_git
))
1850 load_reverse_index(bitmap_git
);
1851 else if (load_midx_revindex(bitmap_git
->midx
) < 0)
1852 BUG("rebuild_existing_bitmaps: missing required rev-cache "
1855 num_objects
= bitmap_num_objects(bitmap_git
);
1856 CALLOC_ARRAY(reposition
, num_objects
);
1858 for (i
= 0; i
< num_objects
; ++i
) {
1859 struct object_id oid
;
1860 struct object_entry
*oe
;
1863 if (bitmap_is_midx(bitmap_git
))
1864 index_pos
= pack_pos_to_midx(bitmap_git
->midx
, i
);
1866 index_pos
= pack_pos_to_index(bitmap_git
->pack
, i
);
1867 nth_bitmap_object_oid(bitmap_git
, &oid
, index_pos
);
1868 oe
= packlist_find(mapping
, &oid
);
1871 reposition
[i
] = oe_in_pack_pos(mapping
, oe
) + 1;
1872 if (bitmap_git
->hashes
&& !oe
->hash
)
1873 oe
->hash
= get_be32(bitmap_git
->hashes
+ index_pos
);
1880 void free_bitmap_index(struct bitmap_index
*b
)
1886 munmap(b
->map
, b
->map_size
);
1887 ewah_pool_free(b
->commits
);
1888 ewah_pool_free(b
->trees
);
1889 ewah_pool_free(b
->blobs
);
1890 ewah_pool_free(b
->tags
);
1892 struct stored_bitmap
*sb
;
1893 kh_foreach_value(b
->bitmaps
, sb
, {
1894 ewah_pool_free(sb
->root
);
1898 kh_destroy_oid_map(b
->bitmaps
);
1899 free(b
->ext_index
.objects
);
1900 free(b
->ext_index
.hashes
);
1901 kh_destroy_oid_pos(b
->ext_index
.positions
);
1902 bitmap_free(b
->result
);
1903 bitmap_free(b
->haves
);
1904 if (bitmap_is_midx(b
)) {
1906 * Multi-pack bitmaps need to have resources associated with
1907 * their on-disk reverse indexes unmapped so that stale .rev and
1908 * .bitmap files can be removed.
1910 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
1911 * written in the same 'git multi-pack-index write --bitmap'
1912 * process. Close resources so they can be removed safely on
1913 * platforms like Windows.
1915 close_midx_revindex(b
->midx
);
1920 int bitmap_has_oid_in_uninteresting(struct bitmap_index
*bitmap_git
,
1921 const struct object_id
*oid
)
1923 return bitmap_git
&&
1924 bitmap_walk_contains(bitmap_git
, bitmap_git
->haves
, oid
);
1927 static off_t
get_disk_usage_for_type(struct bitmap_index
*bitmap_git
,
1928 enum object_type object_type
)
1930 struct bitmap
*result
= bitmap_git
->result
;
1932 struct ewah_iterator it
;
1936 init_type_iterator(&it
, bitmap_git
, object_type
);
1937 for (i
= 0; i
< result
->word_alloc
&&
1938 ewah_iterator_next(&filter
, &it
); i
++) {
1939 eword_t word
= result
->words
[i
] & filter
;
1940 size_t base
= (i
* BITS_IN_EWORD
);
1946 for (offset
= 0; offset
< BITS_IN_EWORD
; offset
++) {
1947 if ((word
>> offset
) == 0)
1950 offset
+= ewah_bit_ctz64(word
>> offset
);
1952 if (bitmap_is_midx(bitmap_git
)) {
1954 uint32_t midx_pos
= pack_pos_to_midx(bitmap_git
->midx
, base
+ offset
);
1955 off_t offset
= nth_midxed_offset(bitmap_git
->midx
, midx_pos
);
1957 uint32_t pack_id
= nth_midxed_pack_int_id(bitmap_git
->midx
, midx_pos
);
1958 struct packed_git
*pack
= bitmap_git
->midx
->packs
[pack_id
];
1960 if (offset_to_pack_pos(pack
, offset
, &pack_pos
) < 0) {
1961 struct object_id oid
;
1962 nth_midxed_object_oid(&oid
, bitmap_git
->midx
, midx_pos
);
1964 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX
),
1970 total
+= pack_pos_to_offset(pack
, pack_pos
+ 1) - offset
;
1972 size_t pos
= base
+ offset
;
1973 total
+= pack_pos_to_offset(bitmap_git
->pack
, pos
+ 1) -
1974 pack_pos_to_offset(bitmap_git
->pack
, pos
);
1982 static off_t
get_disk_usage_for_extended(struct bitmap_index
*bitmap_git
)
1984 struct bitmap
*result
= bitmap_git
->result
;
1985 struct eindex
*eindex
= &bitmap_git
->ext_index
;
1987 struct object_info oi
= OBJECT_INFO_INIT
;
1991 oi
.disk_sizep
= &object_size
;
1993 for (i
= 0; i
< eindex
->count
; i
++) {
1994 struct object
*obj
= eindex
->objects
[i
];
1996 if (!bitmap_get(result
, bitmap_num_objects(bitmap_git
) + i
))
1999 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
2000 die(_("unable to get disk usage of '%s'"),
2001 oid_to_hex(&obj
->oid
));
2003 total
+= object_size
;
2008 off_t
get_disk_usage_from_bitmap(struct bitmap_index
*bitmap_git
,
2009 struct rev_info
*revs
)
2013 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_COMMIT
);
2014 if (revs
->tree_objects
)
2015 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TREE
);
2016 if (revs
->blob_objects
)
2017 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_BLOB
);
2018 if (revs
->tag_objects
)
2019 total
+= get_disk_usage_for_type(bitmap_git
, OBJ_TAG
);
2021 total
+= get_disk_usage_for_extended(bitmap_git
);
2026 int bitmap_is_midx(struct bitmap_index
*bitmap_git
)
2028 return !!bitmap_git
->midx
;
2031 const struct string_list
*bitmap_preferred_tips(struct repository
*r
)
2033 return repo_config_get_value_multi(r
, "pack.preferbitmaptips");
2036 int bitmap_is_preferred_refname(struct repository
*r
, const char *refname
)
2038 const struct string_list
*preferred_tips
= bitmap_preferred_tips(r
);
2039 struct string_list_item
*item
;
2041 if (!preferred_tips
)
2044 for_each_string_list_item(item
, preferred_tips
) {
2045 if (starts_with(refname
, item
->string
))