]> git.ipfire.org Git - thirdparty/git.git/blame - pack-bitmap.c
Merge branch 'hn/refs-debug-empty-prefix'
[thirdparty/git.git] / pack-bitmap.c
CommitLineData
fff42755
VM
1#include "cache.h"
2#include "commit.h"
3#include "tag.h"
4#include "diff.h"
5#include "revision.h"
6#include "progress.h"
7#include "list-objects.h"
8#include "pack.h"
9#include "pack-bitmap.h"
10#include "pack-revindex.h"
11#include "pack-objects.h"
0317f455 12#include "packfile.h"
a80d72db
SB
13#include "repository.h"
14#include "object-store.h"
6663ae0a 15#include "list-objects-filter-options.h"
3f267a11 16#include "config.h"
fff42755
VM
17
18/*
19 * An entry on the bitmap index, representing the bitmap for a given
20 * commit.
21 */
22struct stored_bitmap {
53636539 23 struct object_id oid;
fff42755
VM
24 struct ewah_bitmap *root;
25 struct stored_bitmap *xor;
26 int flags;
27};
28
29/*
3ae5fa07 30 * The active bitmap index for a repository. By design, repositories only have
fff42755
VM
31 * a single bitmap index available (the index for the biggest packfile in
32 * the repository), since bitmap indexes need full closure.
33 *
34 * If there is more than one bitmap index available (e.g. because of alternates),
35 * the active bitmap index is the largest one.
36 */
3ae5fa07 37struct bitmap_index {
fff42755
VM
38 /* Packfile to which this bitmap index belongs to */
39 struct packed_git *pack;
40
fff42755
VM
41 /*
42 * Mark the first `reuse_objects` in the packfile as reused:
43 * they will be sent as-is without using them for repacking
44 * calculations
45 */
46 uint32_t reuse_objects;
47
48 /* mmapped buffer of the whole bitmap index */
49 unsigned char *map;
50 size_t map_size; /* size of the mmaped buffer */
51 size_t map_pos; /* current position when loading the index */
52
53 /*
54 * Type indexes.
55 *
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.
59 */
60 struct ewah_bitmap *commits;
61 struct ewah_bitmap *trees;
62 struct ewah_bitmap *blobs;
63 struct ewah_bitmap *tags;
64
3c771448 65 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
66 kh_oid_map_t *bitmaps;
fff42755
VM
67
68 /* Number of bitmapped commits */
69 uint32_t entry_count;
70
f3c23db2 71 /* If not NULL, this is a name-hash cache pointing into map. */
ae4f07fb
VM
72 uint32_t *hashes;
73
fff42755
VM
74 /*
75 * Extended index.
76 *
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
80 */
81 struct eindex {
82 struct object **objects;
83 uint32_t *hashes;
84 uint32_t count, alloc;
3c771448 85 kh_oid_pos_t *positions;
fff42755
VM
86 } ext_index;
87
88 /* Bitmap result of the last performed walk */
89 struct bitmap *result;
90
30cdc33f
JK
91 /* "have" bitmap from the last performed walk */
92 struct bitmap *haves;
93
fff42755
VM
94 /* Version of the bitmap index */
95 unsigned int version;
3ae5fa07 96};
fff42755
VM
97
98static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
99{
100 struct ewah_bitmap *parent;
101 struct ewah_bitmap *composed;
102
103 if (st->xor == NULL)
104 return st->root;
105
106 composed = ewah_pool_new();
107 parent = lookup_stored_bitmap(st->xor);
108 ewah_xor(st->root, parent, composed);
109
110 ewah_pool_free(st->root);
111 st->root = composed;
112 st->xor = NULL;
113
114 return composed;
115}
116
117/*
118 * Read a bitmap from the current read position on the mmaped
119 * index, and increase the read position accordingly
120 */
121static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
122{
123 struct ewah_bitmap *b = ewah_pool_new();
124
1140bf01 125 ssize_t bitmap_size = ewah_read_mmap(b,
fff42755
VM
126 index->map + index->map_pos,
127 index->map_size - index->map_pos);
128
129 if (bitmap_size < 0) {
130 error("Failed to load bitmap index (corrupted?)");
131 ewah_pool_free(b);
132 return NULL;
133 }
134
135 index->map_pos += bitmap_size;
136 return b;
137}
138
139static int load_bitmap_header(struct bitmap_index *index)
140{
141 struct bitmap_disk_header *header = (void *)index->map;
ca510902 142 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
fff42755 143
ca510902
JK
144 if (index->map_size < header_size + the_hash_algo->rawsz)
145 return error("Corrupted bitmap index (too small)");
fff42755
VM
146
147 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
148 return error("Corrupted bitmap index file (wrong header)");
149
150 index->version = ntohs(header->version);
151 if (index->version != 1)
152 return error("Unsupported version for bitmap index file (%d)", index->version);
153
154 /* Parse known bitmap format options */
155 {
156 uint32_t flags = ntohs(header->options);
ec6c7b43
JK
157 size_t cache_size = st_mult(index->pack->num_objects, sizeof(uint32_t));
158 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
fff42755
VM
159
160 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
161 return error("Unsupported options for bitmap index file "
162 "(Git requires BITMAP_OPT_FULL_DAG)");
ae4f07fb
VM
163
164 if (flags & BITMAP_OPT_HASH_CACHE) {
ec6c7b43
JK
165 if (cache_size > index_end - index->map - header_size)
166 return error("corrupted bitmap index file (too short to fit hash cache)");
167 index->hashes = (void *)(index_end - cache_size);
168 index_end -= cache_size;
ae4f07fb 169 }
fff42755
VM
170 }
171
172 index->entry_count = ntohl(header->entry_count);
ca510902 173 index->map_pos += header_size;
fff42755
VM
174 return 0;
175}
176
177static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
178 struct ewah_bitmap *root,
500e4f23 179 const struct object_id *oid,
fff42755
VM
180 struct stored_bitmap *xor_with,
181 int flags)
182{
183 struct stored_bitmap *stored;
184 khiter_t hash_pos;
185 int ret;
186
187 stored = xmalloc(sizeof(struct stored_bitmap));
188 stored->root = root;
189 stored->xor = xor_with;
190 stored->flags = flags;
500e4f23 191 oidcpy(&stored->oid, oid);
fff42755 192
3c771448 193 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
fff42755
VM
194
195 /* a 0 return code means the insertion succeeded with no changes,
196 * because the SHA1 already existed on the map. this is bad, there
197 * shouldn't be duplicated commits in the index */
198 if (ret == 0) {
500e4f23 199 error("Duplicate entry in bitmap index: %s", oid_to_hex(oid));
fff42755
VM
200 return NULL;
201 }
202
203 kh_value(index->bitmaps, hash_pos) = stored;
204 return stored;
205}
206
b5007211
KB
207static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
208{
209 uint32_t result = get_be32(buffer + *pos);
210 (*pos) += sizeof(result);
211 return result;
212}
213
214static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
215{
216 return buffer[(*pos)++];
217}
218
599dc766
RS
219#define MAX_XOR_OFFSET 160
220
fff42755
VM
221static int load_bitmap_entries_v1(struct bitmap_index *index)
222{
fff42755 223 uint32_t i;
599dc766 224 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
fff42755
VM
225
226 for (i = 0; i < index->entry_count; ++i) {
227 int xor_offset, flags;
228 struct ewah_bitmap *bitmap = NULL;
229 struct stored_bitmap *xor_bitmap = NULL;
230 uint32_t commit_idx_pos;
500e4f23 231 struct object_id oid;
fff42755 232
c6b0c391
TB
233 if (index->map_size - index->map_pos < 6)
234 return error("corrupt ewah bitmap: truncated header for entry %d", i);
235
b5007211
KB
236 commit_idx_pos = read_be32(index->map, &index->map_pos);
237 xor_offset = read_u8(index->map, &index->map_pos);
238 flags = read_u8(index->map, &index->map_pos);
fff42755 239
c6b0c391
TB
240 if (nth_packed_object_id(&oid, index->pack, commit_idx_pos) < 0)
241 return error("corrupt ewah bitmap: commit index %u out of range",
242 (unsigned)commit_idx_pos);
fff42755 243
fff42755
VM
244 bitmap = read_bitmap_1(index);
245 if (!bitmap)
246 return -1;
247
248 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
249 return error("Corrupted bitmap pack index");
250
251 if (xor_offset > 0) {
252 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
253
254 if (xor_bitmap == NULL)
255 return error("Invalid XOR offset in bitmap pack index");
256 }
257
258 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
500e4f23 259 index, bitmap, &oid, xor_bitmap, flags);
fff42755
VM
260 }
261
262 return 0;
263}
264
cb468050
JH
265static char *pack_bitmap_filename(struct packed_git *p)
266{
9ae97018 267 size_t len;
cb468050 268
9ae97018 269 if (!strip_suffix(p->pack_name, ".pack", &len))
033abf97 270 BUG("pack_name does not end in .pack");
9ae97018 271 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
cb468050
JH
272}
273
3ae5fa07 274static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
fff42755
VM
275{
276 int fd;
277 struct stat st;
278 char *idx_name;
279
280 if (open_pack_index(packfile))
281 return -1;
282
283 idx_name = pack_bitmap_filename(packfile);
a5436b57 284 fd = git_open(idx_name);
fff42755
VM
285 free(idx_name);
286
287 if (fd < 0)
288 return -1;
289
290 if (fstat(fd, &st)) {
291 close(fd);
292 return -1;
293 }
294
3ae5fa07 295 if (bitmap_git->pack) {
fff42755
VM
296 warning("ignoring extra bitmap file: %s", packfile->pack_name);
297 close(fd);
298 return -1;
299 }
300
3ae5fa07
JT
301 bitmap_git->pack = packfile;
302 bitmap_git->map_size = xsize_t(st.st_size);
303 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
304 bitmap_git->map_pos = 0;
fff42755
VM
305 close(fd);
306
3ae5fa07
JT
307 if (load_bitmap_header(bitmap_git) < 0) {
308 munmap(bitmap_git->map, bitmap_git->map_size);
309 bitmap_git->map = NULL;
310 bitmap_git->map_size = 0;
fff42755
VM
311 return -1;
312 }
313
314 return 0;
315}
316
3ae5fa07 317static int load_pack_bitmap(struct bitmap_index *bitmap_git)
fff42755 318{
199c86be 319 assert(bitmap_git->map);
fff42755 320
3c771448 321 bitmap_git->bitmaps = kh_init_oid_map();
322 bitmap_git->ext_index.positions = kh_init_oid_pos();
4828ce98
JK
323 if (load_pack_revindex(bitmap_git->pack))
324 goto failed;
fff42755 325
3ae5fa07
JT
326 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
327 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
328 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
329 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
fff42755
VM
330 goto failed;
331
3ae5fa07 332 if (load_bitmap_entries_v1(bitmap_git) < 0)
fff42755
VM
333 goto failed;
334
fff42755
VM
335 return 0;
336
337failed:
3ae5fa07
JT
338 munmap(bitmap_git->map, bitmap_git->map_size);
339 bitmap_git->map = NULL;
340 bitmap_git->map_size = 0;
bb514de3
JK
341
342 kh_destroy_oid_map(bitmap_git->bitmaps);
343 bitmap_git->bitmaps = NULL;
344
345 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
346 bitmap_git->ext_index.positions = NULL;
347
fff42755
VM
348 return -1;
349}
350
7c141127
NTND
351static int open_pack_bitmap(struct repository *r,
352 struct bitmap_index *bitmap_git)
fff42755
VM
353{
354 struct packed_git *p;
355 int ret = -1;
356
199c86be 357 assert(!bitmap_git->map);
fff42755 358
7c141127 359 for (p = get_all_packs(r); p; p = p->next) {
3ae5fa07 360 if (open_pack_bitmap_1(bitmap_git, p) == 0)
fff42755
VM
361 ret = 0;
362 }
363
364 return ret;
365}
366
7c141127 367struct bitmap_index *prepare_bitmap_git(struct repository *r)
fff42755 368{
3ae5fa07 369 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
fff42755 370
7c141127 371 if (!open_pack_bitmap(r, bitmap_git) && !load_pack_bitmap(bitmap_git))
3ae5fa07 372 return bitmap_git;
fff42755 373
f3c23db2 374 free_bitmap_index(bitmap_git);
3ae5fa07 375 return NULL;
fff42755
VM
376}
377
378struct include_data {
3ae5fa07 379 struct bitmap_index *bitmap_git;
fff42755
VM
380 struct bitmap *base;
381 struct bitmap *seen;
382};
383
98c31f36
TB
384struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
385 struct commit *commit)
386{
387 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
388 commit->object.oid);
389 if (hash_pos >= kh_end(bitmap_git->bitmaps))
390 return NULL;
391 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
392}
393
3ae5fa07 394static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
3c771448 395 const struct object_id *oid)
fff42755 396{
4ed43d16 397 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
3c771448 398 khiter_t pos = kh_get_oid_pos(positions, *oid);
fff42755
VM
399
400 if (pos < kh_end(positions)) {
401 int bitmap_pos = kh_value(positions, pos);
3ae5fa07 402 return bitmap_pos + bitmap_git->pack->num_objects;
fff42755
VM
403 }
404
405 return -1;
406}
407
3ae5fa07 408static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
3c771448 409 const struct object_id *oid)
fff42755 410{
57665086 411 uint32_t pos;
3c771448 412 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
fff42755
VM
413 if (!offset)
414 return -1;
415
57665086
TB
416 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
417 return -1;
418 return pos;
fff42755
VM
419}
420
3ae5fa07 421static int bitmap_position(struct bitmap_index *bitmap_git,
3c771448 422 const struct object_id *oid)
fff42755 423{
3c771448 424 int pos = bitmap_position_packfile(bitmap_git, oid);
425 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
fff42755
VM
426}
427
3ae5fa07
JT
428static int ext_index_add_object(struct bitmap_index *bitmap_git,
429 struct object *object, const char *name)
fff42755 430{
3ae5fa07 431 struct eindex *eindex = &bitmap_git->ext_index;
fff42755
VM
432
433 khiter_t hash_pos;
434 int hash_ret;
435 int bitmap_pos;
436
3c771448 437 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
fff42755
VM
438 if (hash_ret > 0) {
439 if (eindex->count >= eindex->alloc) {
440 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
2756ca43
RS
441 REALLOC_ARRAY(eindex->objects, eindex->alloc);
442 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
fff42755
VM
443 }
444
445 bitmap_pos = eindex->count;
446 eindex->objects[eindex->count] = object;
447 eindex->hashes[eindex->count] = pack_name_hash(name);
448 kh_value(eindex->positions, hash_pos) = bitmap_pos;
449 eindex->count++;
450 } else {
451 bitmap_pos = kh_value(eindex->positions, hash_pos);
452 }
453
3ae5fa07 454 return bitmap_pos + bitmap_git->pack->num_objects;
fff42755
VM
455}
456
3ae5fa07
JT
457struct bitmap_show_data {
458 struct bitmap_index *bitmap_git;
459 struct bitmap *base;
460};
461
462static void show_object(struct object *object, const char *name, void *data_)
fff42755 463{
3ae5fa07 464 struct bitmap_show_data *data = data_;
fff42755
VM
465 int bitmap_pos;
466
3c771448 467 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
fff42755 468
de1e67d0 469 if (bitmap_pos < 0)
3ae5fa07
JT
470 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
471 name);
fff42755 472
3ae5fa07 473 bitmap_set(data->base, bitmap_pos);
fff42755
VM
474}
475
476static void show_commit(struct commit *commit, void *data)
477{
478}
479
3ae5fa07
JT
480static int add_to_include_set(struct bitmap_index *bitmap_git,
481 struct include_data *data,
98c31f36 482 struct commit *commit,
fff42755
VM
483 int bitmap_pos)
484{
98c31f36 485 struct ewah_bitmap *partial;
fff42755
VM
486
487 if (data->seen && bitmap_get(data->seen, bitmap_pos))
488 return 0;
489
490 if (bitmap_get(data->base, bitmap_pos))
491 return 0;
492
98c31f36
TB
493 partial = bitmap_for_commit(bitmap_git, commit);
494 if (partial) {
495 bitmap_or_ewah(data->base, partial);
fff42755
VM
496 return 0;
497 }
498
499 bitmap_set(data->base, bitmap_pos);
500 return 1;
501}
502
503static int should_include(struct commit *commit, void *_data)
504{
505 struct include_data *data = _data;
506 int bitmap_pos;
507
3c771448 508 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
fff42755 509 if (bitmap_pos < 0)
3ae5fa07
JT
510 bitmap_pos = ext_index_add_object(data->bitmap_git,
511 (struct object *)commit,
512 NULL);
fff42755 513
98c31f36 514 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
fff42755
VM
515 struct commit_list *parent = commit->parents;
516
517 while (parent) {
518 parent->item->object.flags |= SEEN;
519 parent = parent->next;
520 }
521
522 return 0;
523 }
524
525 return 1;
526}
527
aa9ad6fe
JK
528static int should_include_obj(struct object *obj, void *_data)
529{
530 struct include_data *data = _data;
531 int bitmap_pos;
532
533 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
534 if (bitmap_pos < 0)
535 return 1;
536 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
537 bitmap_get(data->base, bitmap_pos)) {
538 obj->flags |= SEEN;
539 return 0;
540 }
541 return 1;
542}
543
83578051
TB
544static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
545 struct bitmap **base,
546 struct commit *commit)
547{
548 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
549
550 if (!or_with)
551 return 0;
552
553 if (*base == NULL)
554 *base = ewah_to_bitmap(or_with);
555 else
556 bitmap_or_ewah(*base, or_with);
557
558 return 1;
559}
560
3ae5fa07
JT
561static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
562 struct rev_info *revs,
fff42755 563 struct object_list *roots,
9639474b
JK
564 struct bitmap *seen,
565 struct list_objects_filter_options *filter)
fff42755
VM
566{
567 struct bitmap *base = NULL;
568 int needs_walk = 0;
569
570 struct object_list *not_mapped = NULL;
571
572 /*
573 * Go through all the roots for the walk. The ones that have bitmaps
574 * on the bitmap index will be `or`ed together to form an initial
575 * global reachability analysis.
576 *
577 * The ones without bitmaps in the index will be stored in the
578 * `not_mapped_list` for further processing.
579 */
580 while (roots) {
581 struct object *object = roots->item;
582 roots = roots->next;
583
83578051
TB
584 if (object->type == OBJ_COMMIT &&
585 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
586 object->flags |= SEEN;
587 continue;
fff42755
VM
588 }
589
590 object_list_insert(object, &not_mapped);
591 }
592
593 /*
594 * Best case scenario: We found bitmaps for all the roots,
595 * so the resulting `or` bitmap has the full reachability analysis
596 */
597 if (not_mapped == NULL)
598 return base;
599
600 roots = not_mapped;
601
602 /*
603 * Let's iterate through all the roots that don't have bitmaps to
604 * check if we can determine them to be reachable from the existing
605 * global bitmap.
606 *
607 * If we cannot find them in the existing global bitmap, we'll need
608 * to push them to an actual walk and run it until we can confirm
609 * they are reachable
610 */
611 while (roots) {
612 struct object *object = roots->item;
613 int pos;
614
615 roots = roots->next;
3c771448 616 pos = bitmap_position(bitmap_git, &object->oid);
fff42755
VM
617
618 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
619 object->flags &= ~UNINTERESTING;
620 add_pending_object(revs, object, "");
621 needs_walk = 1;
622 } else {
623 object->flags |= SEEN;
624 }
625 }
626
627 if (needs_walk) {
628 struct include_data incdata;
3ae5fa07 629 struct bitmap_show_data show_data;
fff42755
VM
630
631 if (base == NULL)
632 base = bitmap_new();
633
3ae5fa07 634 incdata.bitmap_git = bitmap_git;
fff42755
VM
635 incdata.base = base;
636 incdata.seen = seen;
637
638 revs->include_check = should_include;
aa9ad6fe 639 revs->include_check_obj = should_include_obj;
fff42755
VM
640 revs->include_check_data = &incdata;
641
642 if (prepare_revision_walk(revs))
643 die("revision walk setup failed");
644
3ae5fa07
JT
645 show_data.bitmap_git = bitmap_git;
646 show_data.base = base;
647
9639474b
JK
648 traverse_commit_list_filtered(filter, revs,
649 show_commit, show_object,
650 &show_data, NULL);
1e951c64
JK
651
652 revs->include_check = NULL;
aa9ad6fe 653 revs->include_check_obj = NULL;
1e951c64 654 revs->include_check_data = NULL;
fff42755
VM
655 }
656
657 return base;
658}
659
3ae5fa07 660static void show_extended_objects(struct bitmap_index *bitmap_git,
4eb707eb 661 struct rev_info *revs,
fff42755
VM
662 show_reachable_fn show_reach)
663{
3ae5fa07
JT
664 struct bitmap *objects = bitmap_git->result;
665 struct eindex *eindex = &bitmap_git->ext_index;
fff42755
VM
666 uint32_t i;
667
668 for (i = 0; i < eindex->count; ++i) {
669 struct object *obj;
670
3ae5fa07 671 if (!bitmap_get(objects, bitmap_git->pack->num_objects + i))
fff42755
VM
672 continue;
673
674 obj = eindex->objects[i];
4eb707eb
JK
675 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
676 (obj->type == OBJ_TREE && !revs->tree_objects) ||
677 (obj->type == OBJ_TAG && !revs->tag_objects))
678 continue;
679
20664967 680 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
fff42755
VM
681 }
682}
683
551cf8b6
JK
684static void init_type_iterator(struct ewah_iterator *it,
685 struct bitmap_index *bitmap_git,
686 enum object_type type)
687{
688 switch (type) {
689 case OBJ_COMMIT:
690 ewah_iterator_init(it, bitmap_git->commits);
691 break;
692
693 case OBJ_TREE:
694 ewah_iterator_init(it, bitmap_git->trees);
695 break;
696
697 case OBJ_BLOB:
698 ewah_iterator_init(it, bitmap_git->blobs);
699 break;
700
701 case OBJ_TAG:
702 ewah_iterator_init(it, bitmap_git->tags);
703 break;
704
705 default:
706 BUG("object type %d not stored by bitmap type index", type);
707 break;
708 }
709}
710
fff42755 711static void show_objects_for_type(
3ae5fa07 712 struct bitmap_index *bitmap_git,
fff42755
VM
713 enum object_type object_type,
714 show_reachable_fn show_reach)
715{
d2ea0310 716 size_t i = 0;
fff42755
VM
717 uint32_t offset;
718
719 struct ewah_iterator it;
720 eword_t filter;
721
3ae5fa07
JT
722 struct bitmap *objects = bitmap_git->result;
723
551cf8b6 724 init_type_iterator(&it, bitmap_git, object_type);
fff42755 725
d2ea0310
JK
726 for (i = 0; i < objects->word_alloc &&
727 ewah_iterator_next(&filter, &it); i++) {
fff42755 728 eword_t word = objects->words[i] & filter;
d2ea0310
JK
729 size_t pos = (i * BITS_IN_EWORD);
730
731 if (!word)
732 continue;
fff42755 733
34b935c0 734 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
20664967 735 struct object_id oid;
cf98f2e8
TB
736 uint32_t hash = 0, index_pos;
737 off_t ofs;
fff42755
VM
738
739 if ((word >> offset) == 0)
740 break;
741
742 offset += ewah_bit_ctz64(word >> offset);
743
cf98f2e8
TB
744 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
745 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
746 nth_packed_object_id(&oid, bitmap_git->pack, index_pos);
fff42755 747
3ae5fa07 748 if (bitmap_git->hashes)
cf98f2e8 749 hash = get_be32(bitmap_git->hashes + index_pos);
ae4f07fb 750
cf98f2e8 751 show_reach(&oid, object_type, 0, hash, bitmap_git->pack, ofs);
fff42755 752 }
fff42755
VM
753 }
754}
755
3ae5fa07
JT
756static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
757 struct object_list *roots)
fff42755
VM
758{
759 while (roots) {
760 struct object *object = roots->item;
761 roots = roots->next;
762
3ae5fa07 763 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
fff42755
VM
764 return 1;
765 }
766
767 return 0;
768}
769
856e12c1
TB
770static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
771 struct object_list *tip_objects,
772 enum object_type type)
4f3bd560
JK
773{
774 struct bitmap *result = bitmap_new();
775 struct object_list *p;
776
777 for (p = tip_objects; p; p = p->next) {
778 int pos;
779
856e12c1 780 if (p->item->type != type)
4f3bd560
JK
781 continue;
782
783 pos = bitmap_position(bitmap_git, &p->item->oid);
784 if (pos < 0)
785 continue;
786
787 bitmap_set(result, pos);
788 }
789
790 return result;
791}
792
856e12c1
TB
793static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
794 struct object_list *tip_objects,
795 struct bitmap *to_filter,
796 enum object_type type)
4f3bd560
JK
797{
798 struct eindex *eindex = &bitmap_git->ext_index;
799 struct bitmap *tips;
800 struct ewah_iterator it;
801 eword_t mask;
802 uint32_t i;
803
804 /*
805 * The non-bitmap version of this filter never removes
856e12c1 806 * objects which the other side specifically asked for,
4f3bd560
JK
807 * so we must match that behavior.
808 */
856e12c1 809 tips = find_tip_objects(bitmap_git, tip_objects, type);
4f3bd560
JK
810
811 /*
812 * We can use the blob type-bitmap to work in whole words
813 * for the objects that are actually in the bitmapped packfile.
814 */
856e12c1 815 for (i = 0, init_type_iterator(&it, bitmap_git, type);
4f3bd560
JK
816 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
817 i++) {
818 if (i < tips->word_alloc)
819 mask &= ~tips->words[i];
820 to_filter->words[i] &= ~mask;
821 }
822
823 /*
824 * Clear any blobs that weren't in the packfile (and so would not have
825 * been caught by the loop above. We'll have to check them
826 * individually.
827 */
828 for (i = 0; i < eindex->count; i++) {
829 uint32_t pos = i + bitmap_git->pack->num_objects;
856e12c1 830 if (eindex->objects[i]->type == type &&
4f3bd560
JK
831 bitmap_get(to_filter, pos) &&
832 !bitmap_get(tips, pos))
833 bitmap_unset(to_filter, pos);
834 }
835
836 bitmap_free(tips);
837}
838
856e12c1
TB
839static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
840 struct object_list *tip_objects,
841 struct bitmap *to_filter)
842{
843 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
844 OBJ_BLOB);
845}
846
84243da1
JK
847static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
848 uint32_t pos)
849{
850 struct packed_git *pack = bitmap_git->pack;
851 unsigned long size;
852 struct object_info oi = OBJECT_INFO_INIT;
853
854 oi.sizep = &size;
855
856 if (pos < pack->num_objects) {
a78a9032
TB
857 off_t ofs = pack_pos_to_offset(pack, pos);
858 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
84243da1 859 struct object_id oid;
a78a9032
TB
860 nth_packed_object_id(&oid, pack,
861 pack_pos_to_index(pack, pos));
84243da1
JK
862 die(_("unable to get size of %s"), oid_to_hex(&oid));
863 }
864 } else {
865 struct eindex *eindex = &bitmap_git->ext_index;
866 struct object *obj = eindex->objects[pos - pack->num_objects];
867 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
868 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
869 }
870
871 return size;
872}
873
874static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
875 struct object_list *tip_objects,
876 struct bitmap *to_filter,
877 unsigned long limit)
878{
879 struct eindex *eindex = &bitmap_git->ext_index;
880 struct bitmap *tips;
881 struct ewah_iterator it;
882 eword_t mask;
883 uint32_t i;
884
856e12c1 885 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
84243da1
JK
886
887 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
888 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
889 i++) {
890 eword_t word = to_filter->words[i] & mask;
891 unsigned offset;
892
893 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
894 uint32_t pos;
895
896 if ((word >> offset) == 0)
897 break;
898 offset += ewah_bit_ctz64(word >> offset);
899 pos = i * BITS_IN_EWORD + offset;
900
901 if (!bitmap_get(tips, pos) &&
902 get_size_by_pos(bitmap_git, pos) >= limit)
903 bitmap_unset(to_filter, pos);
904 }
905 }
906
907 for (i = 0; i < eindex->count; i++) {
908 uint32_t pos = i + bitmap_git->pack->num_objects;
909 if (eindex->objects[i]->type == OBJ_BLOB &&
910 bitmap_get(to_filter, pos) &&
911 !bitmap_get(tips, pos) &&
912 get_size_by_pos(bitmap_git, pos) >= limit)
913 bitmap_unset(to_filter, pos);
914 }
915
916 bitmap_free(tips);
917}
918
b0a8d482
TB
919static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
920 struct object_list *tip_objects,
921 struct bitmap *to_filter,
922 unsigned long limit)
923{
924 if (limit)
925 BUG("filter_bitmap_tree_depth given non-zero limit");
926
927 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
928 OBJ_TREE);
929 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
930 OBJ_BLOB);
931}
932
7ab6aafa
PS
933static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
934 struct object_list *tip_objects,
935 struct bitmap *to_filter,
936 enum object_type object_type)
937{
938 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
939 BUG("filter_bitmap_object_type given invalid object");
940
941 if (object_type != OBJ_TAG)
942 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
943 if (object_type != OBJ_COMMIT)
944 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
945 if (object_type != OBJ_TREE)
946 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
947 if (object_type != OBJ_BLOB)
948 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
949}
950
6663ae0a
JK
951static int filter_bitmap(struct bitmap_index *bitmap_git,
952 struct object_list *tip_objects,
953 struct bitmap *to_filter,
954 struct list_objects_filter_options *filter)
955{
956 if (!filter || filter->choice == LOFC_DISABLED)
957 return 0;
958
4f3bd560
JK
959 if (filter->choice == LOFC_BLOB_NONE) {
960 if (bitmap_git)
961 filter_bitmap_blob_none(bitmap_git, tip_objects,
962 to_filter);
963 return 0;
964 }
965
84243da1
JK
966 if (filter->choice == LOFC_BLOB_LIMIT) {
967 if (bitmap_git)
968 filter_bitmap_blob_limit(bitmap_git, tip_objects,
969 to_filter,
970 filter->blob_limit_value);
971 return 0;
972 }
973
b0a8d482
TB
974 if (filter->choice == LOFC_TREE_DEPTH &&
975 filter->tree_exclude_depth == 0) {
976 if (bitmap_git)
977 filter_bitmap_tree_depth(bitmap_git, tip_objects,
978 to_filter,
979 filter->tree_exclude_depth);
980 return 0;
981 }
982
7ab6aafa
PS
983 if (filter->choice == LOFC_OBJECT_TYPE) {
984 if (bitmap_git)
985 filter_bitmap_object_type(bitmap_git, tip_objects,
986 to_filter,
987 filter->object_type);
988 return 0;
989 }
990
169a15eb
PS
991 if (filter->choice == LOFC_COMBINE) {
992 int i;
993 for (i = 0; i < filter->sub_nr; i++) {
994 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
995 &filter->sub[i]) < 0)
996 return -1;
997 }
998 return 0;
999 }
1000
6663ae0a
JK
1001 /* filter choice not handled */
1002 return -1;
1003}
1004
1005static int can_filter_bitmap(struct list_objects_filter_options *filter)
1006{
1007 return !filter_bitmap(NULL, NULL, NULL, filter);
1008}
1009
1010struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
9cf68b27
PS
1011 struct list_objects_filter_options *filter,
1012 int filter_provided_objects)
fff42755
VM
1013{
1014 unsigned int i;
fff42755
VM
1015
1016 struct object_list *wants = NULL;
1017 struct object_list *haves = NULL;
1018
1019 struct bitmap *wants_bitmap = NULL;
1020 struct bitmap *haves_bitmap = NULL;
1021
d90fe06e
JK
1022 struct bitmap_index *bitmap_git;
1023
1024 /*
1025 * We can't do pathspec limiting with bitmaps, because we don't know
1026 * which commits are associated with which object changes (let alone
1027 * even which objects are associated with which paths).
1028 */
1029 if (revs->prune)
1030 return NULL;
1031
6663ae0a
JK
1032 if (!can_filter_bitmap(filter))
1033 return NULL;
1034
3ae5fa07
JT
1035 /* try to open a bitmapped pack, but don't parse it yet
1036 * because we may not need to use it */
ca56dadb 1037 CALLOC_ARRAY(bitmap_git, 1);
7c141127 1038 if (open_pack_bitmap(revs->repo, bitmap_git) < 0)
f3c23db2 1039 goto cleanup;
fff42755 1040
4d01a7fa
1041 for (i = 0; i < revs->pending.nr; ++i) {
1042 struct object *object = revs->pending.objects[i].item;
fff42755
VM
1043
1044 if (object->type == OBJ_NONE)
c251c83d 1045 parse_object_or_die(&object->oid, NULL);
fff42755
VM
1046
1047 while (object->type == OBJ_TAG) {
1048 struct tag *tag = (struct tag *) object;
1049
1050 if (object->flags & UNINTERESTING)
1051 object_list_insert(object, &haves);
1052 else
1053 object_list_insert(object, &wants);
1054
dad3f060 1055 object = parse_object_or_die(get_tagged_oid(tag), NULL);
540cdc11 1056 object->flags |= (tag->object.flags & UNINTERESTING);
fff42755
VM
1057 }
1058
1059 if (object->flags & UNINTERESTING)
1060 object_list_insert(object, &haves);
1061 else
1062 object_list_insert(object, &wants);
1063 }
1064
1065 /*
1066 * if we have a HAVES list, but none of those haves is contained
1067 * in the packfile that has a bitmap, we don't have anything to
1068 * optimize here
1069 */
3ae5fa07 1070 if (haves && !in_bitmapped_pack(bitmap_git, haves))
f3c23db2 1071 goto cleanup;
fff42755
VM
1072
1073 /* if we don't want anything, we're done here */
1074 if (!wants)
f3c23db2 1075 goto cleanup;
fff42755
VM
1076
1077 /*
1078 * now we're going to use bitmaps, so load the actual bitmap entries
1079 * from disk. this is the point of no return; after this the rev_list
1080 * becomes invalidated and we must perform the revwalk through bitmaps
1081 */
199c86be 1082 if (load_pack_bitmap(bitmap_git) < 0)
f3c23db2 1083 goto cleanup;
fff42755 1084
4d01a7fa 1085 object_array_clear(&revs->pending);
fff42755
VM
1086
1087 if (haves) {
2db1a43f 1088 revs->ignore_missing_links = 1;
9639474b
JK
1089 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
1090 filter);
fff42755 1091 reset_revision_walk();
2db1a43f 1092 revs->ignore_missing_links = 0;
fff42755
VM
1093
1094 if (haves_bitmap == NULL)
033abf97 1095 BUG("failed to perform bitmap walk");
fff42755
VM
1096 }
1097
9639474b
JK
1098 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
1099 filter);
fff42755
VM
1100
1101 if (!wants_bitmap)
033abf97 1102 BUG("failed to perform bitmap walk");
fff42755
VM
1103
1104 if (haves_bitmap)
1105 bitmap_and_not(wants_bitmap, haves_bitmap);
1106
9cf68b27
PS
1107 filter_bitmap(bitmap_git, (filter && filter_provided_objects) ? NULL : wants,
1108 wants_bitmap, filter);
6663ae0a 1109
3ae5fa07 1110 bitmap_git->result = wants_bitmap;
30cdc33f 1111 bitmap_git->haves = haves_bitmap;
fff42755 1112
acac50dd
JK
1113 object_list_free(&wants);
1114 object_list_free(&haves);
1115
3ae5fa07 1116 return bitmap_git;
f3c23db2
JT
1117
1118cleanup:
1119 free_bitmap_index(bitmap_git);
acac50dd
JK
1120 object_list_free(&wants);
1121 object_list_free(&haves);
f3c23db2 1122 return NULL;
fff42755
VM
1123}
1124
bb514de3
JK
1125static void try_partial_reuse(struct bitmap_index *bitmap_git,
1126 size_t pos,
1127 struct bitmap *reuse,
1128 struct pack_window **w_curs)
fff42755 1129{
011f3fd5 1130 off_t offset, header;
bb514de3
JK
1131 enum object_type type;
1132 unsigned long size;
1133
1134 if (pos >= bitmap_git->pack->num_objects)
1135 return; /* not actually in the pack */
1136
011f3fd5 1137 offset = header = pack_pos_to_offset(bitmap_git->pack, pos);
bb514de3
JK
1138 type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
1139 if (type < 0)
1140 return; /* broken packfile, punt */
1141
1142 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1143 off_t base_offset;
011f3fd5 1144 uint32_t base_pos;
bb514de3
JK
1145
1146 /*
1147 * Find the position of the base object so we can look it up
1148 * in our bitmaps. If we can't come up with an offset, or if
1149 * that offset is not in the revidx, the pack is corrupt.
1150 * There's nothing we can do, so just punt on this object,
1151 * and the normal slow path will complain about it in
1152 * more detail.
1153 */
1154 base_offset = get_delta_base(bitmap_git->pack, w_curs,
011f3fd5 1155 &offset, type, header);
bb514de3
JK
1156 if (!base_offset)
1157 return;
011f3fd5 1158 if (offset_to_pack_pos(bitmap_git->pack, base_offset, &base_pos) < 0)
bb514de3
JK
1159 return;
1160
1161 /*
1162 * We assume delta dependencies always point backwards. This
1163 * lets us do a single pass, and is basically always true
1164 * due to the way OFS_DELTAs work. You would not typically
1165 * find REF_DELTA in a bitmapped pack, since we only bitmap
1166 * packs we write fresh, and OFS_DELTA is the default). But
1167 * let's double check to make sure the pack wasn't written with
1168 * odd parameters.
1169 */
1170 if (base_pos >= pos)
1171 return;
1172
1173 /*
1174 * And finally, if we're not sending the base as part of our
1175 * reuse chunk, then don't send this object either. The base
1176 * would come after us, along with other objects not
1177 * necessarily in the pack, which means we'd need to convert
1178 * to REF_DELTA on the fly. Better to just let the normal
1179 * object_entry code path handle it.
1180 */
1181 if (!bitmap_get(reuse, base_pos))
1182 return;
1183 }
1184
fff42755 1185 /*
bb514de3 1186 * If we got here, then the object is OK to reuse. Mark it.
fff42755 1187 */
bb514de3
JK
1188 bitmap_set(reuse, pos);
1189}
fff42755 1190
bb514de3
JK
1191int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1192 struct packed_git **packfile_out,
1193 uint32_t *entries,
1194 struct bitmap **reuse_out)
1195{
3ae5fa07 1196 struct bitmap *result = bitmap_git->result;
bb514de3
JK
1197 struct bitmap *reuse;
1198 struct pack_window *w_curs = NULL;
1199 size_t i = 0;
1200 uint32_t offset;
fff42755
VM
1201
1202 assert(result);
1203
bb514de3
JK
1204 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1205 i++;
fff42755 1206
bb514de3
JK
1207 /* Don't mark objects not in the packfile */
1208 if (i > bitmap_git->pack->num_objects / BITS_IN_EWORD)
1209 i = bitmap_git->pack->num_objects / BITS_IN_EWORD;
fff42755 1210
bb514de3
JK
1211 reuse = bitmap_word_alloc(i);
1212 memset(reuse->words, 0xFF, i * sizeof(eword_t));
fff42755 1213
bb514de3
JK
1214 for (; i < result->word_alloc; ++i) {
1215 eword_t word = result->words[i];
1216 size_t pos = (i * BITS_IN_EWORD);
fff42755 1217
bb514de3
JK
1218 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1219 if ((word >> offset) == 0)
1220 break;
fff42755 1221
bb514de3
JK
1222 offset += ewah_bit_ctz64(word >> offset);
1223 try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs);
1224 }
fff42755 1225 }
fff42755 1226
bb514de3 1227 unuse_pack(&w_curs);
fff42755 1228
bb514de3
JK
1229 *entries = bitmap_popcount(reuse);
1230 if (!*entries) {
1231 bitmap_free(reuse);
fff42755 1232 return -1;
fff42755
VM
1233 }
1234
bb514de3
JK
1235 /*
1236 * Drop any reused objects from the result, since they will not
1237 * need to be handled separately.
1238 */
1239 bitmap_and_not(result, reuse);
1240 *packfile_out = bitmap_git->pack;
1241 *reuse_out = reuse;
fff42755
VM
1242 return 0;
1243}
fff42755 1244
40d18ff8
JK
1245int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1246 struct bitmap *bitmap, const struct object_id *oid)
1247{
1248 int idx;
fff42755 1249
40d18ff8
JK
1250 if (!bitmap)
1251 return 0;
fff42755 1252
40d18ff8
JK
1253 idx = bitmap_position(bitmap_git, oid);
1254 return idx >= 0 && bitmap_get(bitmap, idx);
fff42755
VM
1255}
1256
3ae5fa07 1257void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
4eb707eb 1258 struct rev_info *revs,
3ae5fa07 1259 show_reachable_fn show_reachable)
fff42755 1260{
3ae5fa07 1261 assert(bitmap_git->result);
fff42755 1262
551cf8b6 1263 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
4eb707eb
JK
1264 if (revs->tree_objects)
1265 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1266 if (revs->blob_objects)
1267 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1268 if (revs->tag_objects)
1269 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
fff42755 1270
4eb707eb 1271 show_extended_objects(bitmap_git, revs, show_reachable);
fff42755
VM
1272}
1273
3ae5fa07 1274static uint32_t count_object_type(struct bitmap_index *bitmap_git,
fff42755
VM
1275 enum object_type type)
1276{
3ae5fa07
JT
1277 struct bitmap *objects = bitmap_git->result;
1278 struct eindex *eindex = &bitmap_git->ext_index;
fff42755
VM
1279
1280 uint32_t i = 0, count = 0;
1281 struct ewah_iterator it;
1282 eword_t filter;
1283
551cf8b6 1284 init_type_iterator(&it, bitmap_git, type);
fff42755
VM
1285
1286 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1287 eword_t word = objects->words[i++] & filter;
1288 count += ewah_bit_popcount64(word);
1289 }
1290
1291 for (i = 0; i < eindex->count; ++i) {
1292 if (eindex->objects[i]->type == type &&
3ae5fa07 1293 bitmap_get(objects, bitmap_git->pack->num_objects + i))
fff42755
VM
1294 count++;
1295 }
1296
1297 return count;
1298}
1299
3ae5fa07
JT
1300void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1301 uint32_t *commits, uint32_t *trees,
fff42755
VM
1302 uint32_t *blobs, uint32_t *tags)
1303{
3ae5fa07 1304 assert(bitmap_git->result);
fff42755
VM
1305
1306 if (commits)
3ae5fa07 1307 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
fff42755
VM
1308
1309 if (trees)
3ae5fa07 1310 *trees = count_object_type(bitmap_git, OBJ_TREE);
fff42755
VM
1311
1312 if (blobs)
3ae5fa07 1313 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
fff42755
VM
1314
1315 if (tags)
3ae5fa07 1316 *tags = count_object_type(bitmap_git, OBJ_TAG);
fff42755
VM
1317}
1318
1319struct bitmap_test_data {
3ae5fa07 1320 struct bitmap_index *bitmap_git;
fff42755
VM
1321 struct bitmap *base;
1322 struct progress *prg;
1323 size_t seen;
1324};
1325
de1e67d0
JK
1326static void test_show_object(struct object *object, const char *name,
1327 void *data)
fff42755
VM
1328{
1329 struct bitmap_test_data *tdata = data;
1330 int bitmap_pos;
1331
3c771448 1332 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
fff42755 1333 if (bitmap_pos < 0)
f2fd0760 1334 die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
fff42755
VM
1335
1336 bitmap_set(tdata->base, bitmap_pos);
1337 display_progress(tdata->prg, ++tdata->seen);
1338}
1339
1340static void test_show_commit(struct commit *commit, void *data)
1341{
1342 struct bitmap_test_data *tdata = data;
1343 int bitmap_pos;
1344
3ae5fa07 1345 bitmap_pos = bitmap_position(tdata->bitmap_git,
3c771448 1346 &commit->object.oid);
fff42755 1347 if (bitmap_pos < 0)
f2fd0760 1348 die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
fff42755
VM
1349
1350 bitmap_set(tdata->base, bitmap_pos);
1351 display_progress(tdata->prg, ++tdata->seen);
1352}
1353
1354void test_bitmap_walk(struct rev_info *revs)
1355{
1356 struct object *root;
1357 struct bitmap *result = NULL;
fff42755
VM
1358 size_t result_popcnt;
1359 struct bitmap_test_data tdata;
3ae5fa07 1360 struct bitmap_index *bitmap_git;
98c31f36 1361 struct ewah_bitmap *bm;
fff42755 1362
7c141127 1363 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
fff42755
VM
1364 die("failed to load bitmap indexes");
1365
1366 if (revs->pending.nr != 1)
1367 die("you must specify exactly one commit to test");
1368
1369 fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
3ae5fa07 1370 bitmap_git->version, bitmap_git->entry_count);
fff42755
VM
1371
1372 root = revs->pending.objects[0].item;
98c31f36 1373 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
fff42755 1374
98c31f36 1375 if (bm) {
fff42755 1376 fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
f2fd0760 1377 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
fff42755
VM
1378
1379 result = ewah_to_bitmap(bm);
1380 }
1381
1382 if (result == NULL)
f2fd0760 1383 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
fff42755
VM
1384
1385 revs->tag_objects = 1;
1386 revs->tree_objects = 1;
1387 revs->blob_objects = 1;
1388
1389 result_popcnt = bitmap_popcount(result);
1390
1391 if (prepare_revision_walk(revs))
1392 die("revision walk setup failed");
1393
3ae5fa07 1394 tdata.bitmap_git = bitmap_git;
fff42755
VM
1395 tdata.base = bitmap_new();
1396 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1397 tdata.seen = 0;
1398
1399 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1400
1401 stop_progress(&tdata.prg);
1402
1403 if (bitmap_equals(result, tdata.base))
1404 fprintf(stderr, "OK!\n");
1405 else
2978b006 1406 die("mismatch in bitmap results");
f86a3747 1407
f3c23db2 1408 free_bitmap_index(bitmap_git);
fff42755 1409}
7cc8f971 1410
dff5e49e
TB
1411int test_bitmap_commits(struct repository *r)
1412{
1413 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1414 struct object_id oid;
1415 MAYBE_UNUSED void *value;
1416
1417 if (!bitmap_git)
1418 die("failed to load bitmap indexes");
1419
1420 kh_foreach(bitmap_git->bitmaps, oid, value, {
1421 printf("%s\n", oid_to_hex(&oid));
1422 });
1423
1424 free_bitmap_index(bitmap_git);
1425
1426 return 0;
1427}
1428
449fa5ee
JK
1429int rebuild_bitmap(const uint32_t *reposition,
1430 struct ewah_bitmap *source,
1431 struct bitmap *dest)
7cc8f971
VM
1432{
1433 uint32_t pos = 0;
1434 struct ewah_iterator it;
1435 eword_t word;
1436
1437 ewah_iterator_init(&it, source);
1438
1439 while (ewah_iterator_next(&word, &it)) {
1440 uint32_t offset, bit_pos;
1441
34b935c0 1442 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
7cc8f971
VM
1443 if ((word >> offset) == 0)
1444 break;
1445
1446 offset += ewah_bit_ctz64(word >> offset);
1447
1448 bit_pos = reposition[pos + offset];
1449 if (bit_pos > 0)
1450 bitmap_set(dest, bit_pos - 1);
1451 else /* can't reuse, we don't have the object */
1452 return -1;
1453 }
1454
34b935c0 1455 pos += BITS_IN_EWORD;
7cc8f971
VM
1456 }
1457 return 0;
1458}
1459
449fa5ee
JK
1460uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1461 struct packing_data *mapping)
7cc8f971
VM
1462{
1463 uint32_t i, num_objects;
1464 uint32_t *reposition;
7cc8f971 1465
3ae5fa07 1466 num_objects = bitmap_git->pack->num_objects;
ca56dadb 1467 CALLOC_ARRAY(reposition, num_objects);
7cc8f971
VM
1468
1469 for (i = 0; i < num_objects; ++i) {
3df28cae 1470 struct object_id oid;
7cc8f971
VM
1471 struct object_entry *oe;
1472
78232bf6
TB
1473 nth_packed_object_id(&oid, bitmap_git->pack,
1474 pack_pos_to_index(bitmap_git->pack, i));
3a37876b 1475 oe = packlist_find(mapping, &oid);
7cc8f971
VM
1476
1477 if (oe)
06af3bba 1478 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
7cc8f971
VM
1479 }
1480
449fa5ee 1481 return reposition;
7cc8f971 1482}
f3c23db2
JT
1483
1484void free_bitmap_index(struct bitmap_index *b)
1485{
1486 if (!b)
1487 return;
1488
1489 if (b->map)
1490 munmap(b->map, b->map_size);
1491 ewah_pool_free(b->commits);
1492 ewah_pool_free(b->trees);
1493 ewah_pool_free(b->blobs);
1494 ewah_pool_free(b->tags);
3c771448 1495 kh_destroy_oid_map(b->bitmaps);
f3c23db2
JT
1496 free(b->ext_index.objects);
1497 free(b->ext_index.hashes);
1498 bitmap_free(b->result);
30cdc33f 1499 bitmap_free(b->haves);
f3c23db2
JT
1500 free(b);
1501}
30cdc33f 1502
3c771448 1503int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1504 const struct object_id *oid)
30cdc33f 1505{
8ebf5296
JK
1506 return bitmap_git &&
1507 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
30cdc33f 1508}
16950f83
JK
1509
1510static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1511 enum object_type object_type)
1512{
1513 struct bitmap *result = bitmap_git->result;
1514 struct packed_git *pack = bitmap_git->pack;
1515 off_t total = 0;
1516 struct ewah_iterator it;
1517 eword_t filter;
1518 size_t i;
1519
1520 init_type_iterator(&it, bitmap_git, object_type);
1521 for (i = 0; i < result->word_alloc &&
1522 ewah_iterator_next(&filter, &it); i++) {
1523 eword_t word = result->words[i] & filter;
1524 size_t base = (i * BITS_IN_EWORD);
1525 unsigned offset;
1526
1527 if (!word)
1528 continue;
1529
1530 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1531 size_t pos;
1532
1533 if ((word >> offset) == 0)
1534 break;
1535
1536 offset += ewah_bit_ctz64(word >> offset);
1537 pos = base + offset;
1538 total += pack_pos_to_offset(pack, pos + 1) -
1539 pack_pos_to_offset(pack, pos);
1540 }
1541 }
1542
1543 return total;
1544}
1545
1546static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1547{
1548 struct bitmap *result = bitmap_git->result;
1549 struct packed_git *pack = bitmap_git->pack;
1550 struct eindex *eindex = &bitmap_git->ext_index;
1551 off_t total = 0;
1552 struct object_info oi = OBJECT_INFO_INIT;
1553 off_t object_size;
1554 size_t i;
1555
1556 oi.disk_sizep = &object_size;
1557
1558 for (i = 0; i < eindex->count; i++) {
1559 struct object *obj = eindex->objects[i];
1560
1561 if (!bitmap_get(result, pack->num_objects + i))
1562 continue;
1563
1564 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1565 die(_("unable to get disk usage of %s"),
1566 oid_to_hex(&obj->oid));
1567
1568 total += object_size;
1569 }
1570 return total;
1571}
1572
1573off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
1574 struct rev_info *revs)
1575{
1576 off_t total = 0;
1577
1578 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
1579 if (revs->tree_objects)
1580 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
1581 if (revs->blob_objects)
1582 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
1583 if (revs->tag_objects)
1584 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
1585
1586 total += get_disk_usage_for_extended(bitmap_git);
1587
1588 return total;
1589}
3f267a11
TB
1590
1591const struct string_list *bitmap_preferred_tips(struct repository *r)
1592{
1593 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
1594}