Raw
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
4 #include "commit.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "strbuf.h"
8 #include "tag.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "progress.h"
12 #include "list-objects.h"
13 #include "pack.h"
14 #include "pack-bitmap.h"
15 #include "pack-revindex.h"
16 #include "pack-objects.h"
17 #include "packfile.h"
18 #include "repository.h"
19 #include "trace2.h"
20 #include "odb.h"
21 #include "list-objects-filter-options.h"
22 #include "midx.h"
23 #include "config.h"
24 #include "pseudo-merge.h"
25
26 /*
27 * An entry on the bitmap index, representing the bitmap for a given
28 * commit.
29 */
30 struct stored_bitmap {
31 struct object_id oid;
32 struct ewah_bitmap *root;
33 struct stored_bitmap *xor;
34 size_t map_pos;
35 int flags;
36 };
37
38 /*
39 * The active bitmap index for a repository. By design, repositories only have
40 * a single bitmap index available (the index for the biggest packfile in
41 * the repository), since bitmap indexes need full closure.
42 *
43 * If there is more than one bitmap index available (e.g. because of alternates),
44 * the active bitmap index is the largest one.
45 */
46 struct bitmap_index {
47 /*
48 * The pack or multi-pack index (MIDX) that this bitmap index belongs
49 * to.
50 *
51 * Exactly one of these must be non-NULL; this specifies the object
52 * order used to interpret this bitmap.
53 */
54 struct packed_git *pack;
55 struct multi_pack_index *midx;
56
57 /*
58 * If using a multi-pack index chain, 'base' points to the
59 * bitmap index corresponding to this bitmap's midx->base_midx.
60 *
61 * base_nr indicates how many layers precede this one, and is
62 * zero when base is NULL.
63 */
64 struct bitmap_index *base;
65 uint32_t base_nr;
66
67 /* mmapped buffer of the whole bitmap index */
68 unsigned char *map;
69 size_t map_size; /* size of the mmaped buffer */
70 size_t map_pos; /* current position when loading the index */
71
72 /*
73 * Type indexes.
74 *
75 * Each bitmap marks which objects in the packfile are of the given
76 * type. This provides type information when yielding the objects from
77 * the packfile during a walk, which allows for better delta bases.
78 */
79 struct ewah_bitmap *commits;
80 struct ewah_bitmap *trees;
81 struct ewah_bitmap *blobs;
82 struct ewah_bitmap *tags;
83
84 /*
85 * Type index arrays when this bitmap is associated with an
86 * incremental multi-pack index chain.
87 *
88 * If n is the number of unique layers in the MIDX chain, then
89 * commits_all[n-1] is this structs 'commits' field,
90 * commits_all[n-2] is the commits field of this bitmap's
91 * 'base', and so on.
92 *
93 * When associated either with a non-incremental MIDX or a
94 * single packfile, these arrays each contain a single element.
95 */
96 struct ewah_bitmap **commits_all;
97 struct ewah_bitmap **trees_all;
98 struct ewah_bitmap **blobs_all;
99 struct ewah_bitmap **tags_all;
100
101 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
102 kh_oid_map_t *bitmaps;
103
104 /* Number of bitmapped commits */
105 uint32_t entry_count;
106
107 /* If not NULL, this is a name-hash cache pointing into map. */
108 uint32_t *hashes;
109
110 /* The checksum of the packfile or MIDX; points into map. */
111 const unsigned char *checksum;
112
113 /*
114 * If not NULL, this point into the commit table extension
115 * (within the memory mapped region `map`).
116 */
117 unsigned char *table_lookup;
118
119 /* This contains the pseudo-merge cache within 'map' (if found). */
120 struct pseudo_merge_map pseudo_merges;
121
122 /*
123 * Extended index.
124 *
125 * When trying to perform bitmap operations with objects that are not
126 * packed in `pack`, these objects are added to this "fake index" and
127 * are assumed to appear at the end of the packfile for all operations
128 */
129 struct eindex {
130 struct object **objects;
131 uint32_t *hashes;
132 uint32_t count, alloc;
133 kh_oid_pos_t *positions;
134 } ext_index;
135
136 /* Bitmap result of the last performed walk */
137 struct bitmap *result;
138
139 /* "have" bitmap from the last performed walk */
140 struct bitmap *haves;
141
142 /* Version of the bitmap index */
143 unsigned int version;
144 };
145
146 static int pseudo_merges_satisfied_nr;
147 static int pseudo_merges_cascades_nr;
148 static int existing_bitmaps_hits_nr;
149 static int existing_bitmaps_misses_nr;
150 static int roots_with_bitmaps_nr;
151 static int roots_without_bitmaps_nr;
152
153 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
154 {
155 struct ewah_bitmap *parent;
156 struct ewah_bitmap *composed;
157
158 if (!st->xor)
159 return st->root;
160
161 composed = ewah_pool_new();
162 parent = lookup_stored_bitmap(st->xor);
163 ewah_xor(st->root, parent, composed);
164
165 ewah_pool_free(st->root);
166 st->root = composed;
167 st->xor = NULL;
168
169 return composed;
170 }
171
172 struct ewah_bitmap *read_bitmap(const unsigned char *map,
173 size_t map_size, size_t *map_pos)
174 {
175 struct ewah_bitmap *b = ewah_pool_new();
176
177 ssize_t bitmap_size = ewah_read_mmap(b, map + *map_pos,
178 map_size - *map_pos);
179
180 if (bitmap_size < 0) {
181 error(_("failed to load bitmap index (corrupted?)"));
182 ewah_pool_free(b);
183 return NULL;
184 }
185
186 *map_pos += bitmap_size;
187
188 return b;
189 }
190
191 /*
192 * Read a bitmap from the current read position on the mmaped
193 * index, and increase the read position accordingly
194 */
195 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
196 {
197 return read_bitmap(index->map, index->map_size, &index->map_pos);
198 }
199
200 static uint32_t bitmap_num_objects_total(struct bitmap_index *index)
201 {
202 if (index->midx) {
203 struct multi_pack_index *m = index->midx;
204 return m->num_objects + m->num_objects_in_base;
205 }
206 return index->pack->num_objects;
207 }
208
209 static uint32_t bitmap_num_objects(struct bitmap_index *index)
210 {
211 if (index->midx)
212 return index->midx->num_objects;
213 return index->pack->num_objects;
214 }
215
216 static uint32_t bitmap_name_hash(struct bitmap_index *index, uint32_t pos)
217 {
218 if (bitmap_is_midx(index)) {
219 while (index && pos < index->midx->num_objects_in_base) {
220 ASSERT(bitmap_is_midx(index));
221 index = index->base;
222 }
223
224 if (!index)
225 BUG("NULL base bitmap for object position: %"PRIu32, pos);
226
227 pos -= index->midx->num_objects_in_base;
228 if (pos >= index->midx->num_objects)
229 BUG("out-of-bounds midx bitmap object at %"PRIu32, pos);
230 }
231
232 if (!index->hashes)
233 return 0;
234
235 return get_be32(index->hashes + pos);
236 }
237
238 static struct repository *bitmap_repo(struct bitmap_index *bitmap_git)
239 {
240 if (bitmap_is_midx(bitmap_git))
241 return bitmap_git->midx->source->base.odb->repo;
242 return bitmap_git->pack->repo;
243 }
244
245 static int load_bitmap_header(struct bitmap_index *index)
246 {
247 struct bitmap_disk_header *header = (void *)index->map;
248 const struct git_hash_algo *hash_algo = bitmap_repo(index)->hash_algo;
249
250 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + hash_algo->rawsz;
251
252 if (index->map_size < header_size + hash_algo->rawsz)
253 return error(_("corrupted bitmap index (too small)"));
254
255 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
256 return error(_("corrupted bitmap index file (wrong header)"));
257
258 index->version = ntohs(header->version);
259 if (index->version != 1)
260 return error(_("unsupported version '%d' for bitmap index file"), index->version);
261
262 /* Parse known bitmap format options */
263 {
264 uint32_t flags = ntohs(header->options);
265 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
266 unsigned char *index_end = index->map + index->map_size - hash_algo->rawsz;
267
268 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
269 BUG("unsupported options for bitmap index file "
270 "(Git requires BITMAP_OPT_FULL_DAG)");
271
272 if (flags & BITMAP_OPT_HASH_CACHE) {
273 if (cache_size > index_end - index->map - header_size)
274 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
275 index->hashes = (void *)(index_end - cache_size);
276 index_end -= cache_size;
277 }
278
279 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
280 size_t table_size = st_mult(ntohl(header->entry_count),
281 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
282 if (table_size > index_end - index->map - header_size)
283 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
284 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
285 index->table_lookup = (void *)(index_end - table_size);
286 index_end -= table_size;
287 }
288
289 if (flags & BITMAP_OPT_PSEUDO_MERGES) {
290 unsigned char *pseudo_merge_ofs;
291 size_t table_size;
292 uint32_t i;
293
294 if (sizeof(table_size) > index_end - index->map - header_size)
295 return error(_("corrupted bitmap index file (too short to fit pseudo-merge table header)"));
296
297 table_size = get_be64(index_end - 8);
298 if (table_size > index_end - index->map - header_size)
299 return error(_("corrupted bitmap index file (too short to fit pseudo-merge table)"));
300
301 if (git_env_bool("GIT_TEST_USE_PSEUDO_MERGES", 1)) {
302 const unsigned char *ext = (index_end - table_size);
303
304 index->pseudo_merges.map = index->map;
305 index->pseudo_merges.map_size = index->map_size;
306 index->pseudo_merges.commits = ext + get_be64(index_end - 16);
307 index->pseudo_merges.commits_nr = get_be32(index_end - 20);
308 index->pseudo_merges.nr = get_be32(index_end - 24);
309
310 if (st_add(st_mult(index->pseudo_merges.nr,
311 sizeof(uint64_t)),
312 24) > table_size)
313 return error(_("corrupted bitmap index file, pseudo-merge table too short"));
314
315 CALLOC_ARRAY(index->pseudo_merges.v,
316 index->pseudo_merges.nr);
317
318 pseudo_merge_ofs = index_end - 24 -
319 (index->pseudo_merges.nr * sizeof(uint64_t));
320 for (i = 0; i < index->pseudo_merges.nr; i++) {
321 index->pseudo_merges.v[i].at = get_be64(pseudo_merge_ofs);
322 pseudo_merge_ofs += sizeof(uint64_t);
323 }
324 }
325
326 index_end -= table_size;
327 }
328 }
329
330 index->entry_count = ntohl(header->entry_count);
331 index->checksum = header->checksum;
332 index->map_pos += header_size;
333 return 0;
334 }
335
336 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
337 struct ewah_bitmap *root,
338 const struct object_id *oid,
339 struct stored_bitmap *xor_with,
340 int flags, size_t map_pos)
341 {
342 struct stored_bitmap *stored;
343 khiter_t hash_pos;
344 int ret;
345
346 stored = xmalloc(sizeof(struct stored_bitmap));
347 stored->map_pos = map_pos;
348 stored->root = root;
349 stored->xor = xor_with;
350 stored->flags = flags;
351 oidcpy(&stored->oid, oid);
352
353 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
354
355 /*
356 * A 0 return code means the insertion succeeded with no changes,
357 * because the SHA1 already existed on the map. This is bad, there
358 * shouldn't be duplicated commits in the index.
359 */
360 if (ret == 0) {
361 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
362 return NULL;
363 }
364
365 kh_value(index->bitmaps, hash_pos) = stored;
366 return stored;
367 }
368
369 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
370 {
371 uint32_t result = get_be32(buffer + *pos);
372 (*pos) += sizeof(result);
373 return result;
374 }
375
376 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
377 {
378 return buffer[(*pos)++];
379 }
380
381 #define MAX_XOR_OFFSET 160
382
383 static int nth_bitmap_object_oid(struct bitmap_index *index,
384 struct object_id *oid,
385 uint32_t n)
386 {
387 if (index->midx)
388 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
389 return nth_packed_object_id(oid, index->pack, n);
390 }
391
392 static int load_bitmap_entries_v1(struct bitmap_index *index)
393 {
394 uint32_t i;
395 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
396
397 for (i = 0; i < index->entry_count; ++i) {
398 int xor_offset, flags;
399 struct ewah_bitmap *bitmap = NULL;
400 struct stored_bitmap *xor_bitmap = NULL;
401 uint32_t commit_idx_pos;
402 struct object_id oid;
403 size_t entry_map_pos;
404
405 if (index->map_size - index->map_pos < 6)
406 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
407
408 entry_map_pos = index->map_pos;
409 commit_idx_pos = read_be32(index->map, &index->map_pos);
410 xor_offset = read_u8(index->map, &index->map_pos);
411 flags = read_u8(index->map, &index->map_pos);
412
413 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
414 return error(_("corrupt ewah bitmap: commit index %u out of range"),
415 (unsigned)commit_idx_pos);
416
417 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
418 return error(_("corrupted bitmap pack index"));
419
420 if (xor_offset > 0) {
421 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
422
423 if (!xor_bitmap)
424 return error(_("invalid XOR offset in bitmap pack index"));
425 }
426
427 bitmap = read_bitmap_1(index);
428 if (!bitmap)
429 return -1;
430
431 recent_bitmaps[i % MAX_XOR_OFFSET] =
432 store_bitmap(index, bitmap, &oid, xor_bitmap, flags,
433 entry_map_pos);
434 }
435
436 return 0;
437 }
438
439 char *midx_bitmap_filename(struct multi_pack_index *midx)
440 {
441 struct strbuf buf = STRBUF_INIT;
442 if (midx->has_chain)
443 get_split_midx_filename_ext(midx->source, &buf,
444 midx_get_checksum_hash(midx),
445 MIDX_EXT_BITMAP);
446 else
447 get_midx_filename_ext(midx->source, &buf,
448 midx_get_checksum_hash(midx),
449 MIDX_EXT_BITMAP);
450
451 return strbuf_detach(&buf, NULL);
452 }
453
454 char *pack_bitmap_filename(struct packed_git *p)
455 {
456 size_t len;
457
458 if (!strip_suffix(p->pack_name, ".pack", &len))
459 BUG("pack_name does not end in .pack");
460 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
461 }
462
463 static int open_midx_bitmap(struct bitmap_index *bitmap_git,
464 struct multi_pack_index *midx)
465 {
466 struct stat st;
467 char *bitmap_name = midx_bitmap_filename(midx);
468 int fd = git_open(bitmap_name);
469 uint32_t i;
470
471 if (fd < 0) {
472 if (errno != ENOENT)
473 warning_errno("cannot open '%s'", bitmap_name);
474 free(bitmap_name);
475 return -1;
476 }
477 free(bitmap_name);
478
479 if (fstat(fd, &st)) {
480 error_errno(_("cannot fstat bitmap file"));
481 close(fd);
482 return -1;
483 }
484
485 if (bitmap_git->pack || bitmap_git->midx) {
486 struct strbuf buf = STRBUF_INIT;
487 get_midx_filename(midx->source, &buf);
488 trace2_data_string("bitmap", bitmap_repo(bitmap_git),
489 "ignoring extra midx bitmap file", buf.buf);
490 close(fd);
491 strbuf_release(&buf);
492 return -1;
493 }
494
495 bitmap_git->midx = midx;
496 bitmap_git->map_size = xsize_t(st.st_size);
497 bitmap_git->map_pos = 0;
498 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
499 MAP_PRIVATE, fd, 0);
500 close(fd);
501
502 if (load_bitmap_header(bitmap_git) < 0)
503 goto cleanup;
504
505 if (!hasheq(midx_get_checksum_hash(bitmap_git->midx), bitmap_git->checksum,
506 bitmap_repo(bitmap_git)->hash_algo)) {
507 error(_("checksum doesn't match in MIDX and bitmap"));
508 goto cleanup;
509 }
510
511 if (load_midx_revindex(bitmap_git->midx)) {
512 warning(_("multi-pack bitmap is missing required reverse index"));
513 goto cleanup;
514 }
515
516 for (i = 0; i < bitmap_git->midx->num_packs + bitmap_git->midx->num_packs_in_base; i++) {
517 if (prepare_midx_pack(bitmap_git->midx, i)) {
518 warning(_("could not open pack %s"),
519 bitmap_git->midx->pack_names[i]);
520 goto cleanup;
521 }
522 }
523
524 if (midx->base_midx) {
525 bitmap_git->base = prepare_midx_bitmap_git(midx->base_midx);
526 if (!bitmap_git->base) {
527 warning(_("could not open bitmap for base MIDX"));
528 goto cleanup;
529 }
530 bitmap_git->base_nr = bitmap_git->base->base_nr + 1;
531 } else {
532 bitmap_git->base_nr = 0;
533 }
534
535 return 0;
536
537 cleanup:
538 munmap(bitmap_git->map, bitmap_git->map_size);
539 bitmap_git->map_size = 0;
540 bitmap_git->map_pos = 0;
541 bitmap_git->map = NULL;
542 bitmap_git->midx = NULL;
543 return -1;
544 }
545
546 static int open_pack_bitmap(struct bitmap_index *bitmap_git, struct packed_git *packfile)
547 {
548 int fd;
549 struct stat st;
550 char *bitmap_name;
551
552 bitmap_name = pack_bitmap_filename(packfile);
553 fd = git_open(bitmap_name);
554
555 if (fd < 0) {
556 if (errno != ENOENT)
557 warning_errno("cannot open '%s'", bitmap_name);
558 free(bitmap_name);
559 return -1;
560 }
561 free(bitmap_name);
562
563 if (fstat(fd, &st)) {
564 error_errno(_("cannot fstat bitmap file"));
565 close(fd);
566 return -1;
567 }
568
569 if (bitmap_git->pack || bitmap_git->midx) {
570 trace2_data_string("bitmap", bitmap_repo(bitmap_git),
571 "ignoring extra bitmap file",
572 packfile->pack_name);
573 close(fd);
574 return -1;
575 }
576
577 if (!is_pack_valid(packfile)) {
578 close(fd);
579 return -1;
580 }
581
582 bitmap_git->pack = packfile;
583 bitmap_git->map_size = xsize_t(st.st_size);
584 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
585 bitmap_git->map_pos = 0;
586 bitmap_git->base_nr = 0;
587 close(fd);
588
589 if (load_bitmap_header(bitmap_git) < 0) {
590 munmap(bitmap_git->map, bitmap_git->map_size);
591 bitmap_git->map = NULL;
592 bitmap_git->map_size = 0;
593 bitmap_git->map_pos = 0;
594 bitmap_git->pack = NULL;
595 return -1;
596 }
597
598 trace2_data_string("bitmap", bitmap_repo(bitmap_git),
599 "opened bitmap file", packfile->pack_name);
600 return 0;
601 }
602
603 static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
604 {
605 if (bitmap_is_midx(bitmap_git)) {
606 struct multi_pack_index *m;
607
608 /*
609 * The multi-pack-index's .rev file is already loaded via
610 * open_pack_bitmap().
611 *
612 * But we still need to open the individual pack .rev files,
613 * since we will need to make use of them in pack-objects.
614 */
615 for (m = bitmap_git->midx; m; m = m->base_midx) {
616 uint32_t i;
617 int ret;
618
619 for (i = 0; i < m->num_packs; i++) {
620 ret = load_pack_revindex(r, m->packs[i]);
621 if (ret)
622 return ret;
623 }
624 }
625 return 0;
626 }
627 return load_pack_revindex(r, bitmap_git->pack);
628 }
629
630 static void load_all_type_bitmaps(struct bitmap_index *bitmap_git)
631 {
632 struct bitmap_index *curr = bitmap_git;
633 size_t i = bitmap_git->base_nr;
634
635 ALLOC_ARRAY(bitmap_git->commits_all, bitmap_git->base_nr + 1);
636 ALLOC_ARRAY(bitmap_git->trees_all, bitmap_git->base_nr + 1);
637 ALLOC_ARRAY(bitmap_git->blobs_all, bitmap_git->base_nr + 1);
638 ALLOC_ARRAY(bitmap_git->tags_all, bitmap_git->base_nr + 1);
639
640 while (curr) {
641 bitmap_git->commits_all[i] = curr->commits;
642 bitmap_git->trees_all[i] = curr->trees;
643 bitmap_git->blobs_all[i] = curr->blobs;
644 bitmap_git->tags_all[i] = curr->tags;
645
646 curr = curr->base;
647 if (curr && !i)
648 BUG("unexpected number of bitmap layers, expected %"PRIu32,
649 bitmap_git->base_nr + 1);
650 i -= 1;
651 }
652 }
653
654 static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git,
655 int recursing)
656 {
657 assert(bitmap_git->map);
658
659 bitmap_git->bitmaps = kh_init_oid_map();
660 bitmap_git->ext_index.positions = kh_init_oid_pos();
661
662 if (load_reverse_index(r, bitmap_git))
663 return -1;
664
665 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
666 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
667 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
668 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
669 return -1;
670
671 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
672 return -1;
673
674 if (bitmap_git->base) {
675 if (!bitmap_is_midx(bitmap_git))
676 BUG("non-MIDX bitmap has non-NULL base bitmap index");
677 if (load_bitmap(r, bitmap_git->base, 1) < 0)
678 return -1;
679 }
680
681 if (!recursing)
682 load_all_type_bitmaps(bitmap_git);
683
684 return 0;
685 }
686
687 static int open_bitmap_for_source(struct odb_source_packed *source,
688 struct bitmap_index *bitmap_git)
689 {
690 struct multi_pack_index *midx = get_multi_pack_index(source);
691 struct packfile_list_entry *e;
692 bool found = false;
693
694 if (midx && !open_midx_bitmap(bitmap_git, midx))
695 found = true;
696
697 for (e = packfile_store_get_packs(source); e; e = e->next) {
698 /*
699 * When tracing is enabled we want to keep looking to report
700 * duplicates even if we have already found a bitmap.
701 */
702 if (found && !trace2_is_enabled())
703 break;
704
705 if (!open_pack_bitmap(bitmap_git, e->pack))
706 found = true;
707 }
708
709 return found ? 0 : -1;
710 }
711
712 static int open_bitmap(struct repository *r,
713 struct bitmap_index *bitmap_git)
714 {
715 struct odb_source *source;
716 bool found = false;
717
718 assert(!bitmap_git->map);
719
720 odb_prepare_alternates(r->objects);
721 for (source = r->objects->sources; source; source = source->next) {
722 struct odb_source_files *files = odb_source_files_downcast(source);
723
724 if (!open_bitmap_for_source(files->packed, bitmap_git))
725 found = true;
726
727 /*
728 * The only reason to keep looking after having found a bitmap
729 * is to report duplicates.
730 */
731 if (found && !trace2_is_enabled())
732 break;
733 }
734
735 return found ? 0 : -1;
736 }
737
738 struct bitmap_index *prepare_bitmap_git(struct repository *r)
739 {
740 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
741
742 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git, 0))
743 return bitmap_git;
744
745 free_bitmap_index(bitmap_git);
746 return NULL;
747 }
748
749 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
750 {
751 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
752
753 if (!open_midx_bitmap(bitmap_git, midx))
754 return bitmap_git;
755
756 free_bitmap_index(bitmap_git);
757 return NULL;
758 }
759
760 struct bitmap_index *prepare_bitmap_git_for_source(struct odb_source_packed *source)
761 {
762 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
763
764 if (!open_bitmap_for_source(source, bitmap_git) &&
765 !load_bitmap(source->base.odb->repo, bitmap_git, 0))
766 return bitmap_git;
767
768 free_bitmap_index(bitmap_git);
769 return NULL;
770 }
771
772 int bitmap_index_contains_pack(struct bitmap_index *bitmap, struct packed_git *pack)
773 {
774 for (; bitmap; bitmap = bitmap->base) {
775 if (bitmap_is_midx(bitmap)) {
776 for (size_t i = 0; i < bitmap->midx->num_packs; i++)
777 if (bitmap->midx->packs[i] == pack)
778 return 1;
779 } else if (bitmap->pack == pack) {
780 return 1;
781 }
782 }
783
784 return 0;
785 }
786
787 struct include_data {
788 struct bitmap_index *bitmap_git;
789 struct bitmap *base;
790 struct bitmap *seen;
791 };
792
793 struct bitmap_lookup_table_triplet {
794 uint32_t commit_pos;
795 uint64_t offset;
796 uint32_t xor_row;
797 };
798
799 struct bitmap_lookup_table_xor_item {
800 struct object_id oid;
801 uint64_t offset;
802 };
803
804 /*
805 * Given a `triplet` struct pointer and pointer `p`, this
806 * function reads the triplet beginning at `p` into the struct.
807 * Note that this function assumes that there is enough memory
808 * left for filling the `triplet` struct from `p`.
809 */
810 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
811 const unsigned char *p)
812 {
813 if (!triplet)
814 return -1;
815
816 triplet->commit_pos = get_be32(p);
817 p += sizeof(uint32_t);
818 triplet->offset = get_be64(p);
819 p += sizeof(uint64_t);
820 triplet->xor_row = get_be32(p);
821 return 0;
822 }
823
824 /*
825 * This function gets the raw triplet from `row`'th row in the
826 * lookup table and fills that data to the `triplet`.
827 */
828 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
829 uint32_t pos,
830 struct bitmap_lookup_table_triplet *triplet)
831 {
832 unsigned char *p = NULL;
833 if (pos >= bitmap_git->entry_count)
834 return error(_("corrupt bitmap lookup table: triplet position out of index"));
835
836 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
837
838 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
839 }
840
841 /*
842 * Searches for a matching triplet. `commit_pos` is a pointer
843 * to the wanted commit position value. `table_entry` points to
844 * a triplet in lookup table. The first 4 bytes of each
845 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
846 */
847 static int triplet_cmp(const void *commit_pos, const void *table_entry)
848 {
849
850 uint32_t a = *(uint32_t *)commit_pos;
851 uint32_t b = get_be32(table_entry);
852 if (a > b)
853 return 1;
854 else if (a < b)
855 return -1;
856
857 return 0;
858 }
859
860 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
861 struct object_id *oid,
862 uint32_t *result)
863 {
864 int found;
865
866 if (bitmap_is_midx(bitmap_git))
867 found = bsearch_midx(oid, bitmap_git->midx, result);
868 else
869 found = bsearch_pack(oid, bitmap_git->pack, result);
870
871 return found;
872 }
873
874 /*
875 * `bsearch_triplet_by_pos` function searches for the raw triplet
876 * having commit position same as `commit_pos` and fills `triplet`
877 * object from the raw triplet. Returns 1 on success and 0 on
878 * failure.
879 */
880 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
881 struct bitmap_index *bitmap_git,
882 struct bitmap_lookup_table_triplet *triplet)
883 {
884 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
885 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
886
887 if (!p)
888 return -1;
889
890 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
891 }
892
893 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
894 struct commit *commit)
895 {
896 uint32_t commit_pos, xor_row;
897 uint64_t offset;
898 int flags;
899 struct bitmap_lookup_table_triplet triplet;
900 struct object_id *oid = &commit->object.oid;
901 struct ewah_bitmap *bitmap;
902 struct stored_bitmap *xor_bitmap = NULL;
903 const int bitmap_header_size = 6;
904 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
905 static size_t xor_items_nr = 0, xor_items_alloc = 0;
906 static int is_corrupt = 0;
907 int xor_flags;
908 khiter_t hash_pos;
909 struct bitmap_lookup_table_xor_item *xor_item;
910 size_t entry_map_pos;
911
912 if (is_corrupt)
913 return NULL;
914
915 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
916 return NULL;
917
918 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
919 return NULL;
920
921 xor_items_nr = 0;
922 offset = triplet.offset;
923 xor_row = triplet.xor_row;
924
925 while (xor_row != 0xffffffff) {
926 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
927
928 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
929 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
930 goto corrupt;
931 }
932
933 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
934 goto corrupt;
935
936 xor_item = &xor_items[xor_items_nr];
937 xor_item->offset = triplet.offset;
938
939 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
940 error(_("corrupt bitmap lookup table: commit index %u out of range"),
941 triplet.commit_pos);
942 goto corrupt;
943 }
944
945 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
946
947 /*
948 * If desired bitmap is already stored, we don't need
949 * to iterate further. Because we know that bitmaps
950 * that are needed to be parsed to parse this bitmap
951 * has already been stored. So, assign this stored bitmap
952 * to the xor_bitmap.
953 */
954 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
955 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
956 break;
957 xor_items_nr++;
958 xor_row = triplet.xor_row;
959 }
960
961 while (xor_items_nr) {
962 xor_item = &xor_items[xor_items_nr - 1];
963 bitmap_git->map_pos = xor_item->offset;
964 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
965 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
966 oid_to_hex(&xor_item->oid));
967 goto corrupt;
968 }
969
970 entry_map_pos = bitmap_git->map_pos;
971 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
972 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
973 bitmap = read_bitmap_1(bitmap_git);
974
975 if (!bitmap)
976 goto corrupt;
977
978 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid,
979 xor_bitmap, xor_flags, entry_map_pos);
980 xor_items_nr--;
981 }
982
983 bitmap_git->map_pos = offset;
984 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
985 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
986 oid_to_hex(oid));
987 goto corrupt;
988 }
989
990 /*
991 * Don't bother reading the commit's index position or its xor
992 * offset:
993 *
994 * - The commit's index position is irrelevant to us, since
995 * load_bitmap_entries_v1 only uses it to learn the object
996 * id which is used to compute the hashmap's key. We already
997 * have an object id, so no need to look it up again.
998 *
999 * - The xor_offset is unusable for us, since it specifies how
1000 * many entries previous to ours we should look at. This
1001 * makes sense when reading the bitmaps sequentially (as in
1002 * load_bitmap_entries_v1()), since we can keep track of
1003 * each bitmap as we read them.
1004 *
1005 * But it can't work for us, since the bitmap's don't have a
1006 * fixed size. So we learn the position of the xor'd bitmap
1007 * from the commit table (and resolve it to a bitmap in the
1008 * above if-statement).
1009 *
1010 * Instead, we can skip ahead and immediately read the flags and
1011 * ewah bitmap.
1012 */
1013 entry_map_pos = bitmap_git->map_pos;
1014 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
1015 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
1016 bitmap = read_bitmap_1(bitmap_git);
1017
1018 if (!bitmap)
1019 goto corrupt;
1020
1021 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags,
1022 entry_map_pos);
1023
1024 corrupt:
1025 free(xor_items);
1026 is_corrupt = 1;
1027 return NULL;
1028 }
1029
1030 static struct ewah_bitmap *find_bitmap_for_commit(struct bitmap_index *bitmap_git,
1031 struct commit *commit,
1032 struct bitmap_index **found)
1033 {
1034 khiter_t hash_pos;
1035 if (!bitmap_git)
1036 return NULL;
1037
1038 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, commit->object.oid);
1039 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
1040 struct stored_bitmap *bitmap = NULL;
1041 if (!bitmap_git->table_lookup)
1042 return find_bitmap_for_commit(bitmap_git->base, commit,
1043 found);
1044
1045 /* this is a fairly hot codepath - no trace2_region please */
1046 /* NEEDSWORK: cache misses aren't recorded */
1047 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
1048 if (!bitmap)
1049 return find_bitmap_for_commit(bitmap_git->base, commit,
1050 found);
1051 if (found)
1052 *found = bitmap_git;
1053 return lookup_stored_bitmap(bitmap);
1054 }
1055 if (found)
1056 *found = bitmap_git;
1057 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
1058 }
1059
1060 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
1061 struct commit *commit)
1062 {
1063 return find_bitmap_for_commit(bitmap_git, commit, NULL);
1064 }
1065
1066 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
1067 const struct object_id *oid)
1068 {
1069 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
1070 khiter_t pos = kh_get_oid_pos(positions, *oid);
1071
1072 if (pos < kh_end(positions)) {
1073 int bitmap_pos = kh_value(positions, pos);
1074 return bitmap_pos + bitmap_num_objects_total(bitmap_git);
1075 }
1076
1077 return -1;
1078 }
1079
1080 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
1081 const struct object_id *oid)
1082 {
1083 uint32_t pos;
1084 off_t offset = find_pack_entry_one(oid, bitmap_git->pack);
1085 if (!offset)
1086 return -1;
1087
1088 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
1089 return -1;
1090 return pos;
1091 }
1092
1093 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
1094 const struct object_id *oid)
1095 {
1096 uint32_t want, got;
1097 if (!bsearch_midx(oid, bitmap_git->midx, &want))
1098 return -1;
1099
1100 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
1101 return -1;
1102 return got;
1103 }
1104
1105 static int bitmap_position(struct bitmap_index *bitmap_git,
1106 const struct object_id *oid)
1107 {
1108 int pos;
1109 if (bitmap_is_midx(bitmap_git))
1110 pos = bitmap_position_midx(bitmap_git, oid);
1111 else
1112 pos = bitmap_position_packfile(bitmap_git, oid);
1113 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
1114 }
1115
1116 static int ext_index_add_object(struct bitmap_index *bitmap_git,
1117 struct object *object, const char *name)
1118 {
1119 struct eindex *eindex = &bitmap_git->ext_index;
1120
1121 khiter_t hash_pos;
1122 int hash_ret;
1123 int bitmap_pos;
1124
1125 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
1126 if (hash_ret > 0) {
1127 if (eindex->count >= eindex->alloc) {
1128 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
1129 REALLOC_ARRAY(eindex->objects, eindex->alloc);
1130 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
1131 }
1132
1133 bitmap_pos = eindex->count;
1134 eindex->objects[eindex->count] = object;
1135 eindex->hashes[eindex->count] = pack_name_hash(name);
1136 kh_value(eindex->positions, hash_pos) = bitmap_pos;
1137 eindex->count++;
1138 } else {
1139 bitmap_pos = kh_value(eindex->positions, hash_pos);
1140 }
1141
1142 return bitmap_pos + bitmap_num_objects_total(bitmap_git);
1143 }
1144
1145 struct bitmap_show_data {
1146 struct bitmap_index *bitmap_git;
1147 struct bitmap *base;
1148 };
1149
1150 static void show_object(struct object *object, const char *name, void *data_)
1151 {
1152 struct bitmap_show_data *data = data_;
1153 int bitmap_pos;
1154
1155 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
1156
1157 if (bitmap_pos < 0)
1158 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
1159 name);
1160
1161 bitmap_set(data->base, bitmap_pos);
1162 }
1163
1164 static void show_commit(struct commit *commit UNUSED,
1165 void *data UNUSED)
1166 {
1167 }
1168
1169 static unsigned apply_pseudo_merges_for_commit_1(struct bitmap_index *bitmap_git,
1170 struct bitmap *result,
1171 struct commit *commit,
1172 uint32_t commit_pos)
1173 {
1174 struct bitmap_index *curr = bitmap_git;
1175 int ret = 0;
1176
1177 while (curr) {
1178 ret += apply_pseudo_merges_for_commit(&curr->pseudo_merges,
1179 result, commit,
1180 commit_pos);
1181 curr = curr->base;
1182 }
1183
1184 if (ret)
1185 pseudo_merges_satisfied_nr += ret;
1186
1187 return ret;
1188 }
1189
1190 static int add_to_include_set(struct bitmap_index *bitmap_git,
1191 struct include_data *data,
1192 struct commit *commit,
1193 int bitmap_pos)
1194 {
1195 struct ewah_bitmap *partial;
1196
1197 if (data->seen && bitmap_get(data->seen, bitmap_pos))
1198 return 0;
1199
1200 if (bitmap_get(data->base, bitmap_pos))
1201 return 0;
1202
1203 partial = bitmap_for_commit(bitmap_git, commit);
1204 if (partial) {
1205 existing_bitmaps_hits_nr++;
1206
1207 bitmap_or_ewah(data->base, partial);
1208 return 0;
1209 }
1210
1211 existing_bitmaps_misses_nr++;
1212
1213 bitmap_set(data->base, bitmap_pos);
1214 if (apply_pseudo_merges_for_commit_1(bitmap_git, data->base, commit,
1215 bitmap_pos))
1216 return 0;
1217
1218 return 1;
1219 }
1220
1221 static int should_include(struct commit *commit, void *_data)
1222 {
1223 struct include_data *data = _data;
1224 int bitmap_pos;
1225
1226 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
1227 if (bitmap_pos < 0)
1228 bitmap_pos = ext_index_add_object(data->bitmap_git,
1229 (struct object *)commit,
1230 NULL);
1231
1232 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
1233 struct commit_list *parent = commit->parents;
1234
1235 while (parent) {
1236 parent->item->object.flags |= SEEN;
1237 parent = parent->next;
1238 }
1239
1240 return 0;
1241 }
1242
1243 return 1;
1244 }
1245
1246 static int should_include_obj(struct object *obj, void *_data)
1247 {
1248 struct include_data *data = _data;
1249 int bitmap_pos;
1250
1251 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1252 if (bitmap_pos < 0)
1253 return 1;
1254 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1255 bitmap_get(data->base, bitmap_pos)) {
1256 obj->flags |= SEEN;
1257 return 0;
1258 }
1259 return 1;
1260 }
1261
1262 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1263 struct bitmap **base,
1264 struct commit *commit)
1265 {
1266 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1267
1268 if (!or_with) {
1269 existing_bitmaps_misses_nr++;
1270 return 0;
1271 }
1272
1273 existing_bitmaps_hits_nr++;
1274
1275 if (!*base)
1276 *base = ewah_to_bitmap(or_with);
1277 else
1278 bitmap_or_ewah(*base, or_with);
1279
1280 return 1;
1281 }
1282
1283 static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1284 struct rev_info *revs,
1285 struct bitmap *base,
1286 struct bitmap *seen)
1287 {
1288 struct include_data incdata;
1289 struct bitmap_show_data show_data;
1290
1291 if (!base)
1292 base = bitmap_new();
1293
1294 incdata.bitmap_git = bitmap_git;
1295 incdata.base = base;
1296 incdata.seen = seen;
1297
1298 revs->include_check = should_include;
1299 revs->include_check_obj = should_include_obj;
1300 revs->include_check_data = &incdata;
1301
1302 if (prepare_revision_walk(revs))
1303 die(_("revision walk setup failed"));
1304
1305 show_data.bitmap_git = bitmap_git;
1306 show_data.base = base;
1307
1308 traverse_commit_list(revs, show_commit, show_object, &show_data);
1309
1310 revs->include_check = NULL;
1311 revs->include_check_obj = NULL;
1312 revs->include_check_data = NULL;
1313
1314 return base;
1315 }
1316
1317 struct bitmap_boundary_cb {
1318 struct bitmap_index *bitmap_git;
1319 struct bitmap *base;
1320
1321 struct object_array boundary;
1322 };
1323
1324 static void show_boundary_commit(struct commit *commit, void *_data)
1325 {
1326 struct bitmap_boundary_cb *data = _data;
1327
1328 if (commit->object.flags & BOUNDARY)
1329 add_object_array(&commit->object, "", &data->boundary);
1330
1331 if (commit->object.flags & UNINTERESTING) {
1332 if (bitmap_walk_contains(data->bitmap_git, data->base,
1333 &commit->object.oid))
1334 return;
1335
1336 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1337 }
1338 }
1339
1340 static void show_boundary_object(struct object *object UNUSED,
1341 const char *name UNUSED,
1342 void *data UNUSED)
1343 {
1344 BUG("should not be called");
1345 }
1346
1347 static unsigned cascade_pseudo_merges_1(struct bitmap_index *bitmap_git,
1348 struct bitmap *result,
1349 struct bitmap *roots)
1350 {
1351 int ret = cascade_pseudo_merges(&bitmap_git->pseudo_merges,
1352 result, roots);
1353 if (ret) {
1354 pseudo_merges_cascades_nr++;
1355 pseudo_merges_satisfied_nr += ret;
1356 }
1357
1358 return ret;
1359 }
1360
1361 static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1362 struct rev_info *revs,
1363 struct object_list *roots)
1364 {
1365 struct bitmap_boundary_cb cb;
1366 struct object_list *root;
1367 struct repository *repo;
1368 unsigned int i;
1369 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1370 int any_missing = 0;
1371 int existing_bitmaps = 0;
1372
1373 cb.bitmap_git = bitmap_git;
1374 cb.base = bitmap_new();
1375 object_array_init(&cb.boundary);
1376
1377 repo = bitmap_repo(bitmap_git);
1378
1379 revs->ignore_missing_links = 1;
1380
1381 if (bitmap_git->pseudo_merges.nr) {
1382 struct bitmap *roots_bitmap = bitmap_new();
1383 struct object_list *objects = NULL;
1384
1385 for (objects = roots; objects; objects = objects->next) {
1386 struct object *object = objects->item;
1387 int pos;
1388
1389 pos = bitmap_position(bitmap_git, &object->oid);
1390 if (pos < 0)
1391 continue;
1392
1393 bitmap_set(roots_bitmap, pos);
1394 }
1395
1396 cascade_pseudo_merges_1(bitmap_git, cb.base, roots_bitmap);
1397 bitmap_free(roots_bitmap);
1398 }
1399
1400 /*
1401 * OR in any existing reachability bitmaps among `roots` into
1402 * `cb.base`.
1403 */
1404 for (root = roots; root; root = root->next) {
1405 struct object *object = root->item;
1406 if (object->type != OBJ_COMMIT ||
1407 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1408 continue;
1409
1410 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1411 (struct commit *)object)) {
1412 existing_bitmaps = 1;
1413 continue;
1414 }
1415
1416 any_missing = 1;
1417 }
1418
1419 if (!any_missing)
1420 goto cleanup;
1421
1422 if (existing_bitmaps)
1423 cascade_pseudo_merges_1(bitmap_git, cb.base, NULL);
1424
1425 tmp_blobs = revs->blob_objects;
1426 tmp_trees = revs->tree_objects;
1427 tmp_tags = revs->tag_objects;
1428 revs->blob_objects = 0;
1429 revs->tree_objects = 0;
1430 revs->tag_objects = 0;
1431
1432 /*
1433 * We didn't have complete coverage of the roots. First setup a
1434 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1435 * between the tips and boundary, and (b) record the boundary.
1436 */
1437 trace2_region_enter("pack-bitmap", "boundary-prepare", repo);
1438 if (prepare_revision_walk(revs))
1439 die("revision walk setup failed");
1440 trace2_region_leave("pack-bitmap", "boundary-prepare", repo);
1441
1442 trace2_region_enter("pack-bitmap", "boundary-traverse", repo);
1443 revs->boundary = 1;
1444 traverse_commit_list_filtered(revs,
1445 show_boundary_commit,
1446 show_boundary_object,
1447 &cb, NULL);
1448 revs->boundary = 0;
1449 trace2_region_leave("pack-bitmap", "boundary-traverse", repo);
1450
1451 revs->blob_objects = tmp_blobs;
1452 revs->tree_objects = tmp_trees;
1453 revs->tag_objects = tmp_tags;
1454
1455 reset_revision_walk();
1456 clear_object_flags(repo, UNINTERESTING);
1457
1458 /*
1459 * Then add the boundary commit(s) as fill-in traversal tips.
1460 */
1461 trace2_region_enter("pack-bitmap", "boundary-fill-in", repo);
1462 for (i = 0; i < cb.boundary.nr; i++) {
1463 struct object *obj = cb.boundary.objects[i].item;
1464 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1465 obj->flags |= SEEN;
1466 else
1467 add_pending_object(revs, obj, "");
1468 }
1469 if (revs->pending.nr)
1470 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1471 trace2_region_leave("pack-bitmap", "boundary-fill-in", repo);
1472
1473 cleanup:
1474 object_array_clear(&cb.boundary);
1475 revs->ignore_missing_links = 0;
1476
1477 return cb.base;
1478 }
1479
1480 struct ewah_bitmap *pseudo_merge_bitmap_for_commit(struct bitmap_index *bitmap_git,
1481 struct commit *commit)
1482 {
1483 struct commit_list *p;
1484 struct bitmap *parents;
1485 struct pseudo_merge *match = NULL;
1486
1487 if (!bitmap_git->pseudo_merges.nr)
1488 return NULL;
1489
1490 parents = bitmap_new();
1491
1492 for (p = commit->parents; p; p = p->next) {
1493 int pos = bitmap_position(bitmap_git, &p->item->object.oid);
1494 if (pos < 0 || pos >= bitmap_num_objects(bitmap_git))
1495 goto done;
1496
1497 /*
1498 * Use bitmap-relative positions instead of offsetting
1499 * by bitmap_git->num_objects_in_base because we use
1500 * this to find a match in pseudo_merge_for_parents(),
1501 * and pseudo-merge groups cannot span multiple bitmap
1502 * layers.
1503 */
1504 bitmap_set(parents, pos);
1505 }
1506
1507 match = pseudo_merge_for_parents(&bitmap_git->pseudo_merges, parents);
1508
1509 done:
1510 bitmap_free(parents);
1511 if (match)
1512 return pseudo_merge_bitmap(&bitmap_git->pseudo_merges, match);
1513
1514 return NULL;
1515 }
1516
1517 static void unsatisfy_all_pseudo_merges(struct bitmap_index *bitmap_git)
1518 {
1519 uint32_t i;
1520 for (i = 0; i < bitmap_git->pseudo_merges.nr; i++)
1521 bitmap_git->pseudo_merges.v[i].satisfied = 0;
1522 }
1523
1524 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1525 struct rev_info *revs,
1526 struct object_list *roots,
1527 struct bitmap *seen)
1528 {
1529 struct bitmap *base = NULL;
1530 int needs_walk = 0;
1531 unsigned existing_bitmaps = 0;
1532
1533 struct object_list *not_mapped = NULL;
1534
1535 unsatisfy_all_pseudo_merges(bitmap_git);
1536
1537 if (bitmap_git->pseudo_merges.nr) {
1538 struct bitmap *roots_bitmap = bitmap_new();
1539 struct object_list *objects = NULL;
1540
1541 for (objects = roots; objects; objects = objects->next) {
1542 struct object *object = objects->item;
1543 int pos;
1544
1545 pos = bitmap_position(bitmap_git, &object->oid);
1546 if (pos < 0)
1547 continue;
1548
1549 bitmap_set(roots_bitmap, pos);
1550 }
1551
1552 base = bitmap_new();
1553 cascade_pseudo_merges_1(bitmap_git, base, roots_bitmap);
1554 bitmap_free(roots_bitmap);
1555 }
1556
1557 /*
1558 * Go through all the roots for the walk. The ones that have bitmaps
1559 * on the bitmap index will be `or`ed together to form an initial
1560 * global reachability analysis.
1561 *
1562 * The ones without bitmaps in the index will be stored in the
1563 * `not_mapped_list` for further processing.
1564 */
1565 while (roots) {
1566 struct object *object = roots->item;
1567
1568 roots = roots->next;
1569
1570 if (base) {
1571 int pos = bitmap_position(bitmap_git, &object->oid);
1572 if (pos > 0 && bitmap_get(base, pos)) {
1573 object->flags |= SEEN;
1574 continue;
1575 }
1576 }
1577
1578 if (object->type == OBJ_COMMIT &&
1579 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1580 object->flags |= SEEN;
1581 existing_bitmaps = 1;
1582 continue;
1583 }
1584
1585 object_list_insert(object, &not_mapped);
1586 }
1587
1588 /*
1589 * Best case scenario: We found bitmaps for all the roots,
1590 * so the resulting `or` bitmap has the full reachability analysis
1591 */
1592 if (!not_mapped)
1593 return base;
1594
1595 roots = not_mapped;
1596
1597 if (existing_bitmaps)
1598 cascade_pseudo_merges_1(bitmap_git, base, NULL);
1599
1600 /*
1601 * Let's iterate through all the roots that don't have bitmaps to
1602 * check if we can determine them to be reachable from the existing
1603 * global bitmap.
1604 *
1605 * If we cannot find them in the existing global bitmap, we'll need
1606 * to push them to an actual walk and run it until we can confirm
1607 * they are reachable
1608 */
1609 while (roots) {
1610 struct object *object = roots->item;
1611 int pos;
1612
1613 roots = roots->next;
1614 pos = bitmap_position(bitmap_git, &object->oid);
1615
1616 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1617 object->flags &= ~UNINTERESTING;
1618 add_pending_object(revs, object, "");
1619 needs_walk = 1;
1620
1621 roots_without_bitmaps_nr++;
1622 } else {
1623 object->flags |= SEEN;
1624
1625 roots_with_bitmaps_nr++;
1626 }
1627 }
1628
1629 if (needs_walk) {
1630 /*
1631 * This fill-in traversal may walk over some objects
1632 * again, since we have already traversed in order to
1633 * find the boundary.
1634 *
1635 * But this extra walk should be extremely cheap, since
1636 * all commit objects are loaded into memory, and
1637 * because we skip walking to parents that are
1638 * UNINTERESTING, since it will be marked in the haves
1639 * bitmap already (or it has an on-disk bitmap, since
1640 * OR-ing it in covers all of its ancestors).
1641 */
1642 base = fill_in_bitmap(bitmap_git, revs, base, seen);
1643 }
1644
1645 object_list_free(&not_mapped);
1646
1647 return base;
1648 }
1649
1650 static void show_extended_objects(struct bitmap_index *bitmap_git,
1651 struct rev_info *revs,
1652 show_reachable_fn show_reach)
1653 {
1654 struct bitmap *objects = bitmap_git->result;
1655 struct eindex *eindex = &bitmap_git->ext_index;
1656 uint32_t i;
1657
1658 for (i = 0; i < eindex->count; ++i) {
1659 struct object *obj;
1660
1661 if (!bitmap_get(objects,
1662 st_add(bitmap_num_objects_total(bitmap_git),
1663 i)))
1664 continue;
1665
1666 obj = eindex->objects[i];
1667 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1668 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1669 (obj->type == OBJ_TAG && !revs->tag_objects))
1670 continue;
1671
1672 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0, NULL);
1673 }
1674 }
1675
1676 static void init_type_iterator(struct ewah_or_iterator *it,
1677 struct bitmap_index *bitmap_git,
1678 enum object_type type)
1679 {
1680 switch (type) {
1681 case OBJ_COMMIT:
1682 ewah_or_iterator_init(it, bitmap_git->commits_all,
1683 bitmap_git->base_nr + 1);
1684 break;
1685
1686 case OBJ_TREE:
1687 ewah_or_iterator_init(it, bitmap_git->trees_all,
1688 bitmap_git->base_nr + 1);
1689 break;
1690
1691 case OBJ_BLOB:
1692 ewah_or_iterator_init(it, bitmap_git->blobs_all,
1693 bitmap_git->base_nr + 1);
1694 break;
1695
1696 case OBJ_TAG:
1697 ewah_or_iterator_init(it, bitmap_git->tags_all,
1698 bitmap_git->base_nr + 1);
1699 break;
1700
1701 default:
1702 BUG("object type %d not stored by bitmap type index", type);
1703 break;
1704 }
1705 }
1706
1707 static int show_objects_for_type(
1708 struct bitmap_index *bitmap_git,
1709 struct bitmap *objects,
1710 enum object_type object_type,
1711 show_reachable_fn show_reach,
1712 void *payload)
1713 {
1714 size_t i = 0;
1715 uint32_t offset;
1716 int ret;
1717
1718 struct ewah_or_iterator it;
1719 eword_t filter;
1720
1721 init_type_iterator(&it, bitmap_git, object_type);
1722
1723 for (i = 0; i < objects->word_alloc &&
1724 ewah_or_iterator_next(&filter, &it); i++) {
1725 eword_t word = objects->words[i] & filter;
1726 size_t pos = (i * BITS_IN_EWORD);
1727
1728 if (!word)
1729 continue;
1730
1731 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1732 struct packed_git *pack;
1733 struct object_id oid;
1734 uint32_t hash = 0, index_pos;
1735 off_t ofs;
1736
1737 if ((word >> offset) == 0)
1738 break;
1739
1740 offset += ewah_bit_ctz64(word >> offset);
1741
1742 if (bitmap_is_midx(bitmap_git)) {
1743 struct multi_pack_index *m = bitmap_git->midx;
1744 uint32_t pack_id;
1745
1746 index_pos = pack_pos_to_midx(m, pos + offset);
1747 ofs = nth_midxed_offset(m, index_pos);
1748 nth_midxed_object_oid(&oid, m, index_pos);
1749
1750 pack_id = nth_midxed_pack_int_id(m, index_pos);
1751 pack = nth_midxed_pack(bitmap_git->midx, pack_id);
1752 } else {
1753 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1754 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1755 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1756
1757 pack = bitmap_git->pack;
1758 }
1759
1760 hash = bitmap_name_hash(bitmap_git, index_pos);
1761
1762 ret = show_reach(&oid, object_type, 0, hash, pack, ofs, payload);
1763 if (ret)
1764 goto out;
1765 }
1766 }
1767
1768 ret = 0;
1769
1770 out:
1771 ewah_or_iterator_release(&it);
1772 return ret;
1773 }
1774
1775 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1776 struct object_list *roots)
1777 {
1778 while (roots) {
1779 struct object *object = roots->item;
1780 roots = roots->next;
1781
1782 if (bitmap_is_midx(bitmap_git)) {
1783 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1784 return 1;
1785 } else {
1786 if (find_pack_entry_one(&object->oid, bitmap_git->pack) > 0)
1787 return 1;
1788 }
1789 }
1790
1791 return 0;
1792 }
1793
1794 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1795 struct object_list *tip_objects,
1796 enum object_type type)
1797 {
1798 struct bitmap *result = bitmap_new();
1799 struct object_list *p;
1800
1801 for (p = tip_objects; p; p = p->next) {
1802 int pos;
1803
1804 if (p->item->type != type)
1805 continue;
1806
1807 pos = bitmap_position(bitmap_git, &p->item->oid);
1808 if (pos < 0)
1809 continue;
1810
1811 bitmap_set(result, pos);
1812 }
1813
1814 return result;
1815 }
1816
1817 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1818 struct object_list *tip_objects,
1819 struct bitmap *to_filter,
1820 enum object_type type)
1821 {
1822 struct eindex *eindex = &bitmap_git->ext_index;
1823 struct bitmap *tips;
1824 struct ewah_or_iterator it;
1825 eword_t mask;
1826 uint32_t i;
1827
1828 /*
1829 * The non-bitmap version of this filter never removes
1830 * objects which the other side specifically asked for,
1831 * so we must match that behavior.
1832 */
1833 tips = find_tip_objects(bitmap_git, tip_objects, type);
1834
1835 /*
1836 * We can use the type-level bitmap for 'type' to work in whole
1837 * words for the objects that are actually in the bitmapped
1838 * packfile.
1839 */
1840 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1841 i < to_filter->word_alloc && ewah_or_iterator_next(&mask, &it);
1842 i++) {
1843 if (i < tips->word_alloc)
1844 mask &= ~tips->words[i];
1845 to_filter->words[i] &= ~mask;
1846 }
1847
1848 /*
1849 * Clear any objects that weren't in the packfile (and so would
1850 * not have been caught by the loop above. We'll have to check
1851 * them individually.
1852 */
1853 for (i = 0; i < eindex->count; i++) {
1854 size_t pos = st_add(i, bitmap_num_objects_total(bitmap_git));
1855 if (eindex->objects[i]->type == type &&
1856 bitmap_get(to_filter, pos) &&
1857 !bitmap_get(tips, pos))
1858 bitmap_unset(to_filter, pos);
1859 }
1860
1861 ewah_or_iterator_release(&it);
1862 bitmap_free(tips);
1863 }
1864
1865 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1866 struct object_list *tip_objects,
1867 struct bitmap *to_filter)
1868 {
1869 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1870 OBJ_BLOB);
1871 }
1872
1873 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1874 uint32_t pos)
1875 {
1876 size_t size;
1877 struct object_info oi = OBJECT_INFO_INIT;
1878
1879 oi.sizep = &size;
1880
1881 if (pos < bitmap_num_objects_total(bitmap_git)) {
1882 struct packed_git *pack;
1883 off_t ofs;
1884
1885 if (bitmap_is_midx(bitmap_git)) {
1886 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1887 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1888
1889 pack = nth_midxed_pack(bitmap_git->midx, pack_id);
1890 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1891 } else {
1892 pack = bitmap_git->pack;
1893 ofs = pack_pos_to_offset(pack, pos);
1894 }
1895
1896 if (packed_object_info(NULL, pack, ofs, &oi) < 0) {
1897 struct object_id oid;
1898 nth_bitmap_object_oid(bitmap_git, &oid,
1899 pack_pos_to_index(pack, pos));
1900 die(_("unable to get size of %s"), oid_to_hex(&oid));
1901 }
1902 } else {
1903 size_t eindex_pos = pos - bitmap_num_objects_total(bitmap_git);
1904 struct eindex *eindex = &bitmap_git->ext_index;
1905 struct object *obj = eindex->objects[eindex_pos];
1906 if (odb_read_object_info_extended(bitmap_repo(bitmap_git)->objects, &obj->oid,
1907 &oi, 0) < 0)
1908 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1909 }
1910
1911 return cast_size_t_to_ulong(size);
1912 }
1913
1914 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1915 struct object_list *tip_objects,
1916 struct bitmap *to_filter,
1917 unsigned long limit)
1918 {
1919 struct eindex *eindex = &bitmap_git->ext_index;
1920 struct bitmap *tips;
1921 struct ewah_or_iterator it;
1922 eword_t mask;
1923 uint32_t i;
1924
1925 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1926
1927 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1928 i < to_filter->word_alloc && ewah_or_iterator_next(&mask, &it);
1929 i++) {
1930 eword_t word = to_filter->words[i] & mask;
1931 unsigned offset;
1932
1933 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1934 uint32_t pos;
1935
1936 if ((word >> offset) == 0)
1937 break;
1938 offset += ewah_bit_ctz64(word >> offset);
1939 pos = i * BITS_IN_EWORD + offset;
1940
1941 if (!bitmap_get(tips, pos) &&
1942 get_size_by_pos(bitmap_git, pos) >= limit)
1943 bitmap_unset(to_filter, pos);
1944 }
1945 }
1946
1947 for (i = 0; i < eindex->count; i++) {
1948 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1949 if (eindex->objects[i]->type == OBJ_BLOB &&
1950 bitmap_get(to_filter, pos) &&
1951 !bitmap_get(tips, pos) &&
1952 get_size_by_pos(bitmap_git, pos) >= limit)
1953 bitmap_unset(to_filter, pos);
1954 }
1955
1956 ewah_or_iterator_release(&it);
1957 bitmap_free(tips);
1958 }
1959
1960 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1961 struct object_list *tip_objects,
1962 struct bitmap *to_filter,
1963 unsigned long limit)
1964 {
1965 if (limit)
1966 BUG("filter_bitmap_tree_depth given non-zero limit");
1967
1968 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1969 OBJ_TREE);
1970 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1971 OBJ_BLOB);
1972 }
1973
1974 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1975 struct object_list *tip_objects,
1976 struct bitmap *to_filter,
1977 enum object_type object_type)
1978 {
1979 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1980 BUG("filter_bitmap_object_type given invalid object");
1981
1982 if (object_type != OBJ_TAG)
1983 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1984 if (object_type != OBJ_COMMIT)
1985 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1986 if (object_type != OBJ_TREE)
1987 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1988 if (object_type != OBJ_BLOB)
1989 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1990 }
1991
1992 static int filter_bitmap(struct bitmap_index *bitmap_git,
1993 struct object_list *tip_objects,
1994 struct bitmap *to_filter,
1995 const struct list_objects_filter_options *filter)
1996 {
1997 if (!filter || filter->choice == LOFC_DISABLED)
1998 return 0;
1999
2000 if (filter->choice == LOFC_BLOB_NONE) {
2001 if (bitmap_git)
2002 filter_bitmap_blob_none(bitmap_git, tip_objects,
2003 to_filter);
2004 return 0;
2005 }
2006
2007 if (filter->choice == LOFC_BLOB_LIMIT) {
2008 if (bitmap_git)
2009 filter_bitmap_blob_limit(bitmap_git, tip_objects,
2010 to_filter,
2011 filter->blob_limit_value);
2012 return 0;
2013 }
2014
2015 if (filter->choice == LOFC_TREE_DEPTH &&
2016 filter->tree_exclude_depth == 0) {
2017 if (bitmap_git)
2018 filter_bitmap_tree_depth(bitmap_git, tip_objects,
2019 to_filter,
2020 filter->tree_exclude_depth);
2021 return 0;
2022 }
2023
2024 if (filter->choice == LOFC_OBJECT_TYPE) {
2025 if (bitmap_git)
2026 filter_bitmap_object_type(bitmap_git, tip_objects,
2027 to_filter,
2028 filter->object_type);
2029 return 0;
2030 }
2031
2032 if (filter->choice == LOFC_COMBINE) {
2033 int i;
2034 for (i = 0; i < filter->sub_nr; i++) {
2035 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
2036 &filter->sub[i]) < 0)
2037 return -1;
2038 }
2039 return 0;
2040 }
2041
2042 /* filter choice not handled */
2043 return -1;
2044 }
2045
2046 bool can_filter_bitmap(const struct list_objects_filter_options *filter)
2047 {
2048 return !filter_bitmap(NULL, NULL, NULL, filter);
2049 }
2050
2051 static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
2052 struct bitmap *result)
2053 {
2054 struct eindex *eindex = &bitmap_git->ext_index;
2055 uint32_t objects_nr;
2056 size_t i, pos;
2057
2058 objects_nr = bitmap_num_objects_total(bitmap_git);
2059 pos = objects_nr / BITS_IN_EWORD;
2060
2061 if (pos > result->word_alloc)
2062 pos = result->word_alloc;
2063
2064 memset(result->words, 0x00, sizeof(eword_t) * pos);
2065 for (i = pos * BITS_IN_EWORD; i < objects_nr; i++)
2066 bitmap_unset(result, i);
2067
2068 for (i = 0; i < eindex->count; ++i) {
2069 if (has_object_pack(bitmap_repo(bitmap_git),
2070 &eindex->objects[i]->oid))
2071 bitmap_unset(result, objects_nr + i);
2072 }
2073 }
2074
2075 int for_each_bitmapped_object(struct bitmap_index *bitmap_git,
2076 const struct list_objects_filter_options *filter,
2077 show_reachable_fn show_reach,
2078 void *payload)
2079 {
2080 const enum object_type types[] = {
2081 OBJ_COMMIT,
2082 OBJ_TREE,
2083 OBJ_BLOB,
2084 OBJ_TAG,
2085 };
2086 struct bitmap *filtered_bitmap = NULL;
2087 uint32_t objects_nr;
2088 size_t full_word_count;
2089 int ret;
2090
2091 if (!can_filter_bitmap(filter)) {
2092 ret = -1;
2093 goto out;
2094 }
2095
2096 objects_nr = bitmap_num_objects(bitmap_git);
2097 full_word_count = objects_nr / BITS_IN_EWORD;
2098
2099 /* We start from the all-1 bitmap and then filter down from there. */
2100 filtered_bitmap = bitmap_word_alloc(full_word_count + !!(objects_nr % BITS_IN_EWORD));
2101 memset(filtered_bitmap->words, 0xff, full_word_count * sizeof(*filtered_bitmap->words));
2102 for (size_t i = full_word_count * BITS_IN_EWORD; i < objects_nr; i++)
2103 bitmap_set(filtered_bitmap, i);
2104
2105 if (filter_bitmap(bitmap_git, NULL, filtered_bitmap, filter) < 0) {
2106 ret = -1;
2107 goto out;
2108 }
2109
2110 for (size_t i = 0; i < ARRAY_SIZE(types); i++) {
2111 ret = show_objects_for_type(bitmap_git, filtered_bitmap,
2112 types[i], show_reach, payload);
2113 if (ret)
2114 goto out;
2115 }
2116
2117 ret = 0;
2118 out:
2119 bitmap_free(filtered_bitmap);
2120 return ret;
2121 }
2122
2123 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
2124 int filter_provided_objects)
2125 {
2126 unsigned int i;
2127 int use_boundary_traversal;
2128
2129 struct object_list *wants = NULL;
2130 struct object_list *haves = NULL;
2131
2132 struct bitmap *wants_bitmap = NULL;
2133 struct bitmap *haves_bitmap = NULL;
2134
2135 struct bitmap_index *bitmap_git;
2136 struct repository *repo;
2137
2138 /*
2139 * We can't do pathspec limiting with bitmaps, because we don't know
2140 * which commits are associated with which object changes (let alone
2141 * even which objects are associated with which paths).
2142 */
2143 if (revs->prune)
2144 return NULL;
2145
2146 if (!can_filter_bitmap(&revs->filter))
2147 return NULL;
2148
2149 /* try to open a bitmapped pack, but don't parse it yet
2150 * because we may not need to use it */
2151 CALLOC_ARRAY(bitmap_git, 1);
2152 if (open_bitmap(revs->repo, bitmap_git) < 0)
2153 goto cleanup;
2154
2155 for (i = 0; i < revs->pending.nr; ++i) {
2156 struct object *object = revs->pending.objects[i].item;
2157
2158 if (object->type == OBJ_NONE)
2159 parse_object_or_die(revs->repo, &object->oid, NULL);
2160
2161 while (object->type == OBJ_TAG) {
2162 struct tag *tag = (struct tag *) object;
2163
2164 if (object->flags & UNINTERESTING)
2165 object_list_insert(object, &haves);
2166 else
2167 object_list_insert(object, &wants);
2168
2169 object = parse_object_or_die(revs->repo, get_tagged_oid(tag), NULL);
2170 object->flags |= (tag->object.flags & UNINTERESTING);
2171 }
2172
2173 if (object->flags & UNINTERESTING)
2174 object_list_insert(object, &haves);
2175 else
2176 object_list_insert(object, &wants);
2177 }
2178
2179 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
2180 if (use_boundary_traversal < 0) {
2181 prepare_repo_settings(revs->repo);
2182 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
2183 }
2184
2185 if (!use_boundary_traversal) {
2186 /*
2187 * if we have a HAVES list, but none of those haves is contained
2188 * in the packfile that has a bitmap, we don't have anything to
2189 * optimize here
2190 */
2191 if (haves && !in_bitmapped_pack(bitmap_git, haves))
2192 goto cleanup;
2193 }
2194
2195 /* if we don't want anything, we're done here */
2196 if (!wants)
2197 goto cleanup;
2198
2199 /*
2200 * now we're going to use bitmaps, so load the actual bitmap entries
2201 * from disk. this is the point of no return; after this the rev_list
2202 * becomes invalidated and we must perform the revwalk through bitmaps
2203 */
2204 if (load_bitmap(revs->repo, bitmap_git, 0) < 0)
2205 goto cleanup;
2206
2207 if (!use_boundary_traversal)
2208 object_array_clear(&revs->pending);
2209
2210 repo = bitmap_repo(bitmap_git);
2211
2212 if (haves) {
2213 if (use_boundary_traversal) {
2214 trace2_region_enter("pack-bitmap", "haves/boundary", repo);
2215 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
2216 trace2_region_leave("pack-bitmap", "haves/boundary", repo);
2217 } else {
2218 trace2_region_enter("pack-bitmap", "haves/classic", repo);
2219 revs->ignore_missing_links = 1;
2220 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
2221 reset_revision_walk();
2222 revs->ignore_missing_links = 0;
2223 trace2_region_leave("pack-bitmap", "haves/classic", repo);
2224 }
2225
2226 if (!haves_bitmap)
2227 BUG("failed to perform bitmap walk");
2228 }
2229
2230 if (use_boundary_traversal) {
2231 object_array_clear(&revs->pending);
2232 reset_revision_walk();
2233 }
2234
2235 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
2236
2237 if (!wants_bitmap)
2238 BUG("failed to perform bitmap walk");
2239
2240 if (haves_bitmap)
2241 bitmap_and_not(wants_bitmap, haves_bitmap);
2242
2243 filter_bitmap(bitmap_git,
2244 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
2245 wants_bitmap,
2246 &revs->filter);
2247
2248 if (revs->unpacked)
2249 filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap);
2250
2251 bitmap_git->result = wants_bitmap;
2252 bitmap_git->haves = haves_bitmap;
2253
2254 object_list_free(&wants);
2255 object_list_free(&haves);
2256
2257 trace2_data_intmax("bitmap", repo, "pseudo_merges_satisfied",
2258 pseudo_merges_satisfied_nr);
2259 trace2_data_intmax("bitmap", repo, "pseudo_merges_cascades",
2260 pseudo_merges_cascades_nr);
2261 trace2_data_intmax("bitmap", repo, "bitmap/hits",
2262 existing_bitmaps_hits_nr);
2263 trace2_data_intmax("bitmap", repo, "bitmap/misses",
2264 existing_bitmaps_misses_nr);
2265 trace2_data_intmax("bitmap", repo, "bitmap/roots_with_bitmap",
2266 roots_with_bitmaps_nr);
2267 trace2_data_intmax("bitmap", repo, "bitmap/roots_without_bitmap",
2268 roots_without_bitmaps_nr);
2269
2270 return bitmap_git;
2271
2272 cleanup:
2273 free_bitmap_index(bitmap_git);
2274 object_list_free(&wants);
2275 object_list_free(&haves);
2276 return NULL;
2277 }
2278
2279 /*
2280 * -1 means "stop trying further objects"; 0 means we may or may not have
2281 * reused, but you can keep feeding bits.
2282 */
2283 static int try_partial_reuse(struct bitmap_index *bitmap_git,
2284 struct bitmapped_pack *pack,
2285 size_t bitmap_pos,
2286 uint32_t pack_pos,
2287 off_t offset,
2288 struct bitmap *reuse,
2289 struct pack_window **w_curs,
2290 int allow_ref_delta)
2291 {
2292 off_t delta_obj_offset;
2293 enum object_type type;
2294 size_t size;
2295
2296 if (pack_pos >= pack->p->num_objects)
2297 return -1; /* not actually in the pack */
2298
2299 delta_obj_offset = offset;
2300 type = unpack_object_header(pack->p, w_curs, &offset, &size);
2301 if (type < 0)
2302 return -1; /* broken packfile, punt */
2303
2304 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
2305 off_t base_offset;
2306 uint32_t base_pos;
2307 uint32_t base_bitmap_pos;
2308
2309 if (type == OBJ_REF_DELTA && !allow_ref_delta)
2310 return 0;
2311
2312 /*
2313 * Find the position of the base object so we can look it up
2314 * in our bitmaps. If we can't come up with an offset, or if
2315 * that offset is not in the revidx, the pack is corrupt.
2316 * There's nothing we can do, so just punt on this object,
2317 * and the normal slow path will complain about it in
2318 * more detail.
2319 */
2320 base_offset = get_delta_base(pack->p, w_curs, &offset, type,
2321 delta_obj_offset);
2322 if (!base_offset)
2323 return 0;
2324
2325 offset_to_pack_pos(pack->p, base_offset, &base_pos);
2326
2327 if (bitmap_is_midx(bitmap_git)) {
2328 /*
2329 * Cross-pack deltas are rejected for now, but could
2330 * theoretically be supported in the future.
2331 *
2332 * We would need to ensure that we're sending both
2333 * halves of the delta/base pair, regardless of whether
2334 * or not the two cross a pack boundary. If they do,
2335 * then we must convert the delta to an REF_DELTA to
2336 * refer back to the base in the other pack.
2337 * */
2338 if (midx_pair_to_pack_pos(bitmap_git->midx,
2339 pack->pack_int_id,
2340 base_offset,
2341 &base_bitmap_pos) < 0) {
2342 return 0;
2343 }
2344 } else {
2345 if (offset_to_pack_pos(pack->p, base_offset,
2346 &base_pos) < 0)
2347 return 0;
2348 /*
2349 * We assume delta dependencies always point backwards.
2350 * This lets us do a single pass, and is basically
2351 * always true due to the way OFS_DELTAs work. You would
2352 * not typically find REF_DELTA in a bitmapped pack,
2353 * since we only bitmap packs we write fresh, and
2354 * OFS_DELTA is the default). But let's double check to
2355 * make sure the pack wasn't written with odd
2356 * parameters.
2357 */
2358 if (base_pos >= pack_pos)
2359 return 0;
2360 base_bitmap_pos = pack->bitmap_pos + base_pos;
2361 }
2362
2363 /*
2364 * And finally, if we're not sending the base as part of our
2365 * reuse chunk, then don't send this object either. The base
2366 * would come after us, along with other objects not
2367 * necessarily in the pack, which means we'd need to convert
2368 * to REF_DELTA on the fly. Better to just let the normal
2369 * object_entry code path handle it.
2370 */
2371 if (!bitmap_get(reuse, base_bitmap_pos))
2372 return 0;
2373 }
2374
2375 /*
2376 * If we got here, then the object is OK to reuse. Mark it.
2377 */
2378 bitmap_set(reuse, bitmap_pos);
2379 return 0;
2380 }
2381
2382 static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
2383 struct bitmapped_pack *pack,
2384 struct bitmap *reuse,
2385 int allow_ref_delta)
2386 {
2387 struct bitmap *result = bitmap_git->result;
2388 struct pack_window *w_curs = NULL;
2389 size_t pos = pack->bitmap_pos / BITS_IN_EWORD;
2390
2391 if (allow_ref_delta && !pack->bitmap_pos) {
2392 /*
2393 * If we're processing the first (in the case of a MIDX, the
2394 * preferred pack) or the only (in the case of single-pack
2395 * bitmaps) pack, then any delta in this range must have its
2396 * base chosen from the same pack:
2397 *
2398 * - In the single pack case, there is no other pack to choose
2399 * them from.
2400 *
2401 * - In the MIDX case, the first pack is the preferred pack, so
2402 * all ties are broken in favor of that pack (i.e. the one
2403 * we're currently processing). So any duplicate bases will be
2404 * resolved in favor of the pack we're processing.
2405 *
2406 * When REF_DELTAs are allowed, we can therefore reuse whole
2407 * words at a time without inspecting object headers. Otherwise,
2408 * inspect each object below to avoid reusing a REF_DELTA entry.
2409 */
2410 while (pos < result->word_alloc &&
2411 pos < pack->bitmap_nr / BITS_IN_EWORD &&
2412 result->words[pos] == (eword_t)~0)
2413 pos++;
2414 memset(reuse->words, 0xFF, pos * sizeof(eword_t));
2415 }
2416
2417 for (; pos < result->word_alloc; pos++) {
2418 eword_t word = result->words[pos];
2419 size_t offset;
2420
2421 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2422 size_t bit_pos;
2423 uint32_t pack_pos;
2424 off_t ofs;
2425
2426 if (word >> offset == 0)
2427 break;
2428
2429 offset += ewah_bit_ctz64(word >> offset);
2430
2431 bit_pos = pos * BITS_IN_EWORD + offset;
2432 if (bit_pos < pack->bitmap_pos)
2433 continue;
2434 if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
2435 goto done;
2436
2437 if (bitmap_is_midx(bitmap_git)) {
2438 uint32_t midx_pos;
2439
2440 midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2441 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
2442
2443 if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
2444 BUG("could not find object in pack %s "
2445 "at offset %"PRIuMAX" in MIDX",
2446 pack_basename(pack->p), (uintmax_t)ofs);
2447 } else {
2448 pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
2449 if (pack_pos >= pack->p->num_objects)
2450 BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
2451 pack_basename(pack->p), (uintmax_t)pack_pos,
2452 pack->p->num_objects);
2453
2454 ofs = pack_pos_to_offset(pack->p, pack_pos);
2455 }
2456
2457 if (try_partial_reuse(bitmap_git, pack, bit_pos,
2458 pack_pos, ofs, reuse, &w_curs,
2459 allow_ref_delta) < 0) {
2460 /*
2461 * try_partial_reuse indicated we couldn't reuse
2462 * any bits, so there is no point in trying more
2463 * bits in the current word, or any other words
2464 * in result.
2465 *
2466 * Jump out of both loops to avoid future
2467 * unnecessary calls to try_partial_reuse.
2468 */
2469 goto done;
2470 }
2471 }
2472 }
2473
2474 done:
2475 unuse_pack(&w_curs);
2476 }
2477
2478 static int bitmapped_pack_cmp(const void *va, const void *vb)
2479 {
2480 const struct bitmapped_pack *a = va;
2481 const struct bitmapped_pack *b = vb;
2482
2483 if (a->bitmap_pos < b->bitmap_pos)
2484 return -1;
2485 if (a->bitmap_pos > b->bitmap_pos)
2486 return 1;
2487 return 0;
2488 }
2489
2490 void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2491 struct bitmapped_pack **packs_out,
2492 size_t *packs_nr_out,
2493 struct bitmap **reuse_out,
2494 int multi_pack_reuse,
2495 int allow_ref_delta)
2496 {
2497 struct repository *r = bitmap_repo(bitmap_git);
2498 struct bitmapped_pack *packs = NULL;
2499 struct bitmap *result = bitmap_git->result;
2500 struct bitmap *reuse;
2501 size_t i;
2502 size_t packs_nr = 0, packs_alloc = 0;
2503 size_t word_alloc;
2504 uint32_t objects_nr = 0;
2505
2506 assert(result);
2507
2508 load_reverse_index(r, bitmap_git);
2509
2510 if (!bitmap_is_midx(bitmap_git) || !bitmap_git->midx->chunk_bitmapped_packs)
2511 multi_pack_reuse = 0;
2512
2513 if (multi_pack_reuse) {
2514 struct multi_pack_index *m = bitmap_git->midx;
2515 for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) {
2516 struct bitmapped_pack pack;
2517 if (nth_bitmapped_pack(bitmap_git->midx, &pack, i) < 0) {
2518 warning(_("unable to load pack: '%s', disabling pack-reuse"),
2519 bitmap_git->midx->pack_names[i]);
2520 free(packs);
2521 return;
2522 }
2523
2524 if (!pack.bitmap_nr)
2525 continue;
2526
2527 if (is_pack_valid(pack.p)) {
2528 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2529 memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2530 }
2531
2532 objects_nr += pack.p->num_objects;
2533 }
2534
2535 QSORT(packs, packs_nr, bitmapped_pack_cmp);
2536 } else {
2537 struct packed_git *pack;
2538 uint32_t pack_int_id;
2539
2540 if (bitmap_is_midx(bitmap_git)) {
2541 struct multi_pack_index *m = bitmap_git->midx;
2542 uint32_t preferred_pack_pos;
2543
2544 while (m->base_midx)
2545 m = m->base_midx;
2546
2547 if (midx_preferred_pack(m, &preferred_pack_pos) < 0) {
2548 warning(_("unable to compute preferred pack, disabling pack-reuse"));
2549 return;
2550 }
2551
2552 pack = nth_midxed_pack(m, preferred_pack_pos);
2553 pack_int_id = preferred_pack_pos;
2554 } else {
2555 pack = bitmap_git->pack;
2556 /*
2557 * Any value for 'pack_int_id' will do here. When we
2558 * process the pack via try_partial_reuse(), we won't
2559 * use the `pack_int_id` field since we have a non-MIDX
2560 * bitmap.
2561 *
2562 * Use '-1' as a sentinel value to make it clear
2563 * that we do not expect to read this field.
2564 */
2565 pack_int_id = -1;
2566 }
2567
2568 if (is_pack_valid(pack)) {
2569 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2570 packs[packs_nr].p = pack;
2571 packs[packs_nr].pack_int_id = pack_int_id;
2572 packs[packs_nr].bitmap_nr = pack->num_objects;
2573 packs[packs_nr].bitmap_pos = 0;
2574 packs[packs_nr].from_midx = bitmap_git->midx;
2575 packs_nr++;
2576 }
2577
2578 objects_nr = pack->num_objects;
2579 }
2580
2581 if (!packs_nr)
2582 return;
2583
2584 word_alloc = objects_nr / BITS_IN_EWORD;
2585 if (objects_nr % BITS_IN_EWORD)
2586 word_alloc++;
2587 reuse = bitmap_word_alloc(word_alloc);
2588
2589 for (i = 0; i < packs_nr; i++)
2590 reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse,
2591 allow_ref_delta);
2592
2593 if (bitmap_is_empty(reuse)) {
2594 free(packs);
2595 bitmap_free(reuse);
2596 return;
2597 }
2598
2599 /*
2600 * Drop any reused objects from the result, since they will not
2601 * need to be handled separately.
2602 */
2603 bitmap_and_not(result, reuse);
2604 *packs_out = packs;
2605 *packs_nr_out = packs_nr;
2606 *reuse_out = reuse;
2607 }
2608
2609 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2610 struct bitmap *bitmap, const struct object_id *oid)
2611 {
2612 int idx;
2613
2614 if (!bitmap)
2615 return 0;
2616
2617 idx = bitmap_position(bitmap_git, oid);
2618 return idx >= 0 && bitmap_get(bitmap, idx);
2619 }
2620
2621 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2622 struct rev_info *revs,
2623 show_reachable_fn show_reachable)
2624 {
2625 assert(bitmap_git->result);
2626
2627 show_objects_for_type(bitmap_git, bitmap_git->result,
2628 OBJ_COMMIT, show_reachable, NULL);
2629 if (revs->tree_objects)
2630 show_objects_for_type(bitmap_git, bitmap_git->result,
2631 OBJ_TREE, show_reachable, NULL);
2632 if (revs->blob_objects)
2633 show_objects_for_type(bitmap_git, bitmap_git->result,
2634 OBJ_BLOB, show_reachable, NULL);
2635 if (revs->tag_objects)
2636 show_objects_for_type(bitmap_git, bitmap_git->result,
2637 OBJ_TAG, show_reachable, NULL);
2638
2639 show_extended_objects(bitmap_git, revs, show_reachable);
2640 }
2641
2642 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2643 enum object_type type)
2644 {
2645 struct bitmap *objects = bitmap_git->result;
2646 struct eindex *eindex = &bitmap_git->ext_index;
2647
2648 uint32_t i = 0, count = 0;
2649 struct ewah_or_iterator it;
2650 eword_t filter;
2651
2652 init_type_iterator(&it, bitmap_git, type);
2653
2654 while (i < objects->word_alloc && ewah_or_iterator_next(&filter, &it)) {
2655 eword_t word = objects->words[i++] & filter;
2656 count += ewah_bit_popcount64(word);
2657 }
2658
2659 for (i = 0; i < eindex->count; ++i) {
2660 if (eindex->objects[i]->type == type &&
2661 bitmap_get(objects,
2662 st_add(bitmap_num_objects_total(bitmap_git), i)))
2663 count++;
2664 }
2665
2666 ewah_or_iterator_release(&it);
2667
2668 return count;
2669 }
2670
2671 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2672 uint32_t *commits, uint32_t *trees,
2673 uint32_t *blobs, uint32_t *tags)
2674 {
2675 assert(bitmap_git->result);
2676
2677 if (commits)
2678 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2679
2680 if (trees)
2681 *trees = count_object_type(bitmap_git, OBJ_TREE);
2682
2683 if (blobs)
2684 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2685
2686 if (tags)
2687 *tags = count_object_type(bitmap_git, OBJ_TAG);
2688 }
2689
2690 struct bitmap_test_data {
2691 struct bitmap_index *bitmap_git;
2692 struct bitmap *base;
2693 struct bitmap *commits;
2694 struct bitmap *trees;
2695 struct bitmap *blobs;
2696 struct bitmap *tags;
2697 struct progress *prg;
2698 size_t seen;
2699
2700 struct bitmap_test_data *base_tdata;
2701 };
2702
2703 static void test_bitmap_type(struct bitmap_test_data *tdata,
2704 struct object *obj, int pos)
2705 {
2706 enum object_type bitmap_type = OBJ_NONE;
2707 int bitmaps_nr = 0;
2708
2709 if (bitmap_is_midx(tdata->bitmap_git)) {
2710 while (pos < tdata->bitmap_git->midx->num_objects_in_base)
2711 tdata = tdata->base_tdata;
2712 }
2713
2714 if (bitmap_get(tdata->commits, pos)) {
2715 bitmap_type = OBJ_COMMIT;
2716 bitmaps_nr++;
2717 }
2718 if (bitmap_get(tdata->trees, pos)) {
2719 bitmap_type = OBJ_TREE;
2720 bitmaps_nr++;
2721 }
2722 if (bitmap_get(tdata->blobs, pos)) {
2723 bitmap_type = OBJ_BLOB;
2724 bitmaps_nr++;
2725 }
2726 if (bitmap_get(tdata->tags, pos)) {
2727 bitmap_type = OBJ_TAG;
2728 bitmaps_nr++;
2729 }
2730
2731 if (bitmap_type == OBJ_NONE)
2732 die(_("object '%s' not found in type bitmaps"),
2733 oid_to_hex(&obj->oid));
2734
2735 if (bitmaps_nr > 1)
2736 die(_("object '%s' does not have a unique type"),
2737 oid_to_hex(&obj->oid));
2738
2739 if (bitmap_type != obj->type)
2740 die(_("object '%s': real type '%s', expected: '%s'"),
2741 oid_to_hex(&obj->oid),
2742 type_name(obj->type),
2743 type_name(bitmap_type));
2744 }
2745
2746 static void test_show_object(struct object *object,
2747 const char *name UNUSED,
2748 void *data)
2749 {
2750 struct bitmap_test_data *tdata = data;
2751 int bitmap_pos;
2752
2753 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2754 if (bitmap_pos < 0)
2755 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2756 test_bitmap_type(tdata, object, bitmap_pos);
2757
2758 bitmap_set(tdata->base, bitmap_pos);
2759 display_progress(tdata->prg, ++tdata->seen);
2760 }
2761
2762 static void test_show_commit(struct commit *commit, void *data)
2763 {
2764 struct bitmap_test_data *tdata = data;
2765 int bitmap_pos;
2766
2767 bitmap_pos = bitmap_position(tdata->bitmap_git,
2768 &commit->object.oid);
2769 if (bitmap_pos < 0)
2770 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2771 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2772
2773 bitmap_set(tdata->base, bitmap_pos);
2774 display_progress(tdata->prg, ++tdata->seen);
2775 }
2776
2777 static uint32_t bitmap_total_entry_count(struct bitmap_index *bitmap_git)
2778 {
2779 uint32_t total = 0;
2780 do {
2781 total = st_add(total, bitmap_git->entry_count);
2782 bitmap_git = bitmap_git->base;
2783 } while (bitmap_git);
2784
2785 return total;
2786 }
2787
2788 static void bitmap_test_data_prepare(struct bitmap_test_data *tdata,
2789 struct bitmap_index *bitmap_git)
2790 {
2791 memset(tdata, 0, sizeof(struct bitmap_test_data));
2792
2793 tdata->bitmap_git = bitmap_git;
2794 tdata->base = bitmap_new();
2795 tdata->commits = ewah_to_bitmap(bitmap_git->commits);
2796 tdata->trees = ewah_to_bitmap(bitmap_git->trees);
2797 tdata->blobs = ewah_to_bitmap(bitmap_git->blobs);
2798 tdata->tags = ewah_to_bitmap(bitmap_git->tags);
2799
2800 if (bitmap_git->base) {
2801 tdata->base_tdata = xmalloc(sizeof(struct bitmap_test_data));
2802 bitmap_test_data_prepare(tdata->base_tdata, bitmap_git->base);
2803 }
2804 }
2805
2806 static void bitmap_test_data_release(struct bitmap_test_data *tdata)
2807 {
2808 if (!tdata)
2809 return;
2810
2811 bitmap_test_data_release(tdata->base_tdata);
2812 free(tdata->base_tdata);
2813
2814 bitmap_free(tdata->base);
2815 bitmap_free(tdata->commits);
2816 bitmap_free(tdata->trees);
2817 bitmap_free(tdata->blobs);
2818 bitmap_free(tdata->tags);
2819 }
2820
2821 void test_bitmap_walk(struct rev_info *revs)
2822 {
2823 struct object *root;
2824 struct bitmap *result = NULL;
2825 size_t result_popcnt;
2826 struct bitmap_test_data tdata;
2827 struct bitmap_index *bitmap_git, *found;
2828 struct ewah_bitmap *bm;
2829
2830 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2831 die(_("failed to load bitmap indexes"));
2832
2833 if (revs->pending.nr != 1)
2834 die(_("you must specify exactly one commit to test"));
2835
2836 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s, %d total)",
2837 bitmap_git->version,
2838 bitmap_git->entry_count,
2839 bitmap_git->table_lookup ? "" : " loaded",
2840 bitmap_total_entry_count(bitmap_git));
2841
2842 root = revs->pending.objects[0].item;
2843 bm = find_bitmap_for_commit(bitmap_git, (struct commit *)root, &found);
2844
2845 if (bm) {
2846 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2847 oid_to_hex(&root->oid),
2848 (int)bm->bit_size, ewah_checksum(bm));
2849
2850 if (bitmap_is_midx(found))
2851 fprintf_ln(stderr, "Located via MIDX '%s'.",
2852 midx_get_checksum_hex(found->midx));
2853 else
2854 fprintf_ln(stderr, "Located via pack '%s'.",
2855 hash_to_hex_algop(found->pack->hash,
2856 revs->repo->hash_algo));
2857
2858 result = ewah_to_bitmap(bm);
2859 }
2860
2861 if (!result)
2862 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2863
2864 revs->tag_objects = 1;
2865 revs->tree_objects = 1;
2866 revs->blob_objects = 1;
2867
2868 result_popcnt = bitmap_popcount(result);
2869
2870 if (prepare_revision_walk(revs))
2871 die(_("revision walk setup failed"));
2872
2873 bitmap_test_data_prepare(&tdata, bitmap_git);
2874 tdata.prg = start_progress(revs->repo,
2875 "Verifying bitmap entries",
2876 result_popcnt);
2877
2878 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2879
2880 stop_progress(&tdata.prg);
2881
2882 if (bitmap_equals(result, tdata.base))
2883 fprintf_ln(stderr, "OK!");
2884 else
2885 die(_("mismatch in bitmap results"));
2886
2887 bitmap_free(result);
2888 bitmap_test_data_release(&tdata);
2889 free_bitmap_index(bitmap_git);
2890 }
2891
2892 int test_bitmap_commits(struct repository *r)
2893 {
2894 struct object_id oid;
2895 MAYBE_UNUSED void *value;
2896 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2897
2898 if (!bitmap_git)
2899 die(_("failed to load bitmap indexes"));
2900
2901 /*
2902 * Since this function needs to print the bitmapped
2903 * commits, bypass the commit lookup table (if one exists)
2904 * by forcing the bitmap to eagerly load its entries.
2905 */
2906 if (bitmap_git->table_lookup) {
2907 if (load_bitmap_entries_v1(bitmap_git) < 0)
2908 die(_("failed to load bitmap indexes"));
2909 }
2910
2911 kh_foreach(bitmap_git->bitmaps, oid, value, {
2912 printf_ln("%s", oid_to_hex(&oid));
2913 });
2914
2915 free_bitmap_index(bitmap_git);
2916
2917 return 0;
2918 }
2919
2920 int test_bitmap_commits_with_offset(struct repository *r)
2921 {
2922 struct object_id oid;
2923 struct stored_bitmap *stored;
2924 struct bitmap_index *bitmap_git;
2925 size_t commit_idx_pos_map_pos, xor_offset_map_pos, flag_map_pos,
2926 ewah_bitmap_map_pos;
2927
2928 bitmap_git = prepare_bitmap_git(r);
2929 if (!bitmap_git)
2930 die(_("failed to load bitmap indexes"));
2931
2932 /*
2933 * Since this function needs to know the position of each individual
2934 * bitmap, bypass the commit lookup table (if one exists) by forcing
2935 * the bitmap to eagerly load its entries.
2936 */
2937 if (bitmap_git->table_lookup) {
2938 if (load_bitmap_entries_v1(bitmap_git) < 0)
2939 die(_("failed to load bitmap indexes"));
2940 }
2941
2942 kh_foreach (bitmap_git->bitmaps, oid, stored, {
2943 commit_idx_pos_map_pos = stored->map_pos;
2944 xor_offset_map_pos = stored->map_pos + sizeof(uint32_t);
2945 flag_map_pos = xor_offset_map_pos + sizeof(uint8_t);
2946 ewah_bitmap_map_pos = flag_map_pos + sizeof(uint8_t);
2947
2948 printf_ln("%s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
2949 oid_to_hex(&oid),
2950 (uintmax_t)commit_idx_pos_map_pos,
2951 (uintmax_t)xor_offset_map_pos,
2952 (uintmax_t)flag_map_pos,
2953 (uintmax_t)ewah_bitmap_map_pos);
2954 })
2955 ;
2956
2957 free_bitmap_index(bitmap_git);
2958
2959 return 0;
2960 }
2961
2962 int test_bitmap_hashes(struct repository *r)
2963 {
2964 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2965 struct object_id oid;
2966 uint32_t i, index_pos;
2967
2968 if (!bitmap_git || !bitmap_git->hashes)
2969 goto cleanup;
2970
2971 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2972 if (bitmap_is_midx(bitmap_git))
2973 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2974 else
2975 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2976
2977 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2978
2979 printf_ln("%s %"PRIu32"",
2980 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2981 }
2982
2983 cleanup:
2984 free_bitmap_index(bitmap_git);
2985
2986 return 0;
2987 }
2988
2989 static void bit_pos_to_object_id(struct bitmap_index *bitmap_git,
2990 uint32_t bit_pos,
2991 struct object_id *oid)
2992 {
2993 uint32_t index_pos;
2994
2995 if (bitmap_is_midx(bitmap_git))
2996 index_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2997 else
2998 index_pos = pack_pos_to_index(bitmap_git->pack, bit_pos);
2999
3000 nth_bitmap_object_oid(bitmap_git, oid, index_pos);
3001 }
3002
3003 int test_bitmap_pseudo_merges(struct repository *r)
3004 {
3005 struct bitmap_index *bitmap_git;
3006 uint32_t i;
3007
3008 bitmap_git = prepare_bitmap_git(r);
3009 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
3010 goto cleanup;
3011
3012 for (i = 0; i < bitmap_git->pseudo_merges.nr; i++) {
3013 struct pseudo_merge *merge;
3014 struct ewah_bitmap *commits_bitmap, *merge_bitmap;
3015
3016 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3017 &bitmap_git->pseudo_merges.v[i]);
3018 commits_bitmap = merge->commits;
3019 merge_bitmap = pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
3020 merge);
3021
3022 printf("at=%"PRIuMAX", commits=%"PRIuMAX", objects=%"PRIuMAX"\n",
3023 (uintmax_t)merge->at,
3024 (uintmax_t)ewah_bitmap_popcount(commits_bitmap),
3025 (uintmax_t)ewah_bitmap_popcount(merge_bitmap));
3026 }
3027
3028 cleanup:
3029 free_bitmap_index(bitmap_git);
3030 return 0;
3031 }
3032
3033 static void dump_ewah_object_ids(struct bitmap_index *bitmap_git,
3034 struct ewah_bitmap *bitmap)
3035
3036 {
3037 struct ewah_iterator it;
3038 eword_t word;
3039 uint32_t pos = 0;
3040
3041 ewah_iterator_init(&it, bitmap);
3042
3043 while (ewah_iterator_next(&word, &it)) {
3044 struct object_id oid;
3045 uint32_t offset;
3046
3047 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
3048 if (!(word >> offset))
3049 break;
3050
3051 offset += ewah_bit_ctz64(word >> offset);
3052
3053 bit_pos_to_object_id(bitmap_git, pos + offset, &oid);
3054 printf("%s\n", oid_to_hex(&oid));
3055 }
3056 pos += BITS_IN_EWORD;
3057 }
3058 }
3059
3060 int test_bitmap_pseudo_merge_commits(struct repository *r, uint32_t n)
3061 {
3062 struct bitmap_index *bitmap_git;
3063 struct pseudo_merge *merge;
3064 int ret = 0;
3065
3066 bitmap_git = prepare_bitmap_git(r);
3067 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
3068 goto cleanup;
3069
3070 if (n >= bitmap_git->pseudo_merges.nr) {
3071 ret = error(_("pseudo-merge index out of range "
3072 "(%"PRIu32" >= %"PRIuMAX")"),
3073 n, (uintmax_t)bitmap_git->pseudo_merges.nr);
3074 goto cleanup;
3075 }
3076
3077 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3078 &bitmap_git->pseudo_merges.v[n]);
3079 dump_ewah_object_ids(bitmap_git, merge->commits);
3080
3081 cleanup:
3082 free_bitmap_index(bitmap_git);
3083 return ret;
3084 }
3085
3086 int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n)
3087 {
3088 struct bitmap_index *bitmap_git;
3089 struct pseudo_merge *merge;
3090 int ret = 0;
3091
3092 bitmap_git = prepare_bitmap_git(r);
3093 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
3094 goto cleanup;
3095
3096 if (n >= bitmap_git->pseudo_merges.nr) {
3097 ret = error(_("pseudo-merge index out of range "
3098 "(%"PRIu32" >= %"PRIuMAX")"),
3099 n, (uintmax_t)bitmap_git->pseudo_merges.nr);
3100 goto cleanup;
3101 }
3102
3103 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3104 &bitmap_git->pseudo_merges.v[n]);
3105
3106 dump_ewah_object_ids(bitmap_git,
3107 pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
3108 merge));
3109
3110 cleanup:
3111 free_bitmap_index(bitmap_git);
3112 return ret;
3113 }
3114
3115 int rebuild_bitmap(const uint32_t *reposition,
3116 struct ewah_bitmap *source,
3117 struct bitmap *dest)
3118 {
3119 uint32_t pos = 0;
3120 struct ewah_iterator it;
3121 eword_t word;
3122
3123 ewah_iterator_init(&it, source);
3124
3125 while (ewah_iterator_next(&word, &it)) {
3126 uint32_t offset, bit_pos;
3127
3128 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
3129 if ((word >> offset) == 0)
3130 break;
3131
3132 offset += ewah_bit_ctz64(word >> offset);
3133
3134 bit_pos = reposition[pos + offset];
3135 if (bit_pos > 0)
3136 bitmap_set(dest, bit_pos - 1);
3137 else /* can't reuse, we don't have the object */
3138 return -1;
3139 }
3140
3141 pos += BITS_IN_EWORD;
3142 }
3143 return 0;
3144 }
3145
3146 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
3147 struct packing_data *mapping)
3148 {
3149 struct repository *r = bitmap_repo(bitmap_git);
3150 uint32_t i, num_objects;
3151 uint32_t *reposition;
3152
3153 if (!bitmap_is_midx(bitmap_git))
3154 load_reverse_index(r, bitmap_git);
3155 else if (load_midx_revindex(bitmap_git->midx))
3156 BUG("rebuild_existing_bitmaps: missing required rev-cache "
3157 "extension");
3158
3159 num_objects = bitmap_num_objects_total(bitmap_git);
3160 CALLOC_ARRAY(reposition, num_objects);
3161
3162 for (i = 0; i < num_objects; ++i) {
3163 struct object_id oid;
3164 struct object_entry *oe;
3165 uint32_t index_pos;
3166
3167 if (bitmap_is_midx(bitmap_git))
3168 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
3169 else
3170 index_pos = pack_pos_to_index(bitmap_git->pack, i);
3171 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
3172 oe = packlist_find(mapping, &oid);
3173
3174 if (oe) {
3175 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
3176 if (!oe->hash)
3177 oe->hash = bitmap_name_hash(bitmap_git, index_pos);
3178 }
3179 }
3180
3181 return reposition;
3182 }
3183
3184 void free_bitmap_index(struct bitmap_index *b)
3185 {
3186 if (!b)
3187 return;
3188
3189 if (b->map)
3190 munmap(b->map, b->map_size);
3191 ewah_pool_free(b->commits);
3192 ewah_pool_free(b->trees);
3193 ewah_pool_free(b->blobs);
3194 ewah_pool_free(b->tags);
3195 free(b->commits_all);
3196 free(b->trees_all);
3197 free(b->blobs_all);
3198 free(b->tags_all);
3199 if (b->bitmaps) {
3200 struct stored_bitmap *sb;
3201 kh_foreach_value(b->bitmaps, sb, {
3202 ewah_pool_free(sb->root);
3203 free(sb);
3204 });
3205 }
3206 kh_destroy_oid_map(b->bitmaps);
3207 free(b->ext_index.objects);
3208 free(b->ext_index.hashes);
3209 kh_destroy_oid_pos(b->ext_index.positions);
3210 bitmap_free(b->result);
3211 bitmap_free(b->haves);
3212 if (bitmap_is_midx(b)) {
3213 /*
3214 * Multi-pack bitmaps need to have resources associated with
3215 * their on-disk reverse indexes unmapped so that stale .rev and
3216 * .bitmap files can be removed.
3217 *
3218 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
3219 * written in the same 'git multi-pack-index write --bitmap'
3220 * process. Close resources so they can be removed safely on
3221 * platforms like Windows.
3222 */
3223 close_midx_revindex(b->midx);
3224 }
3225 free_pseudo_merge_map(&b->pseudo_merges);
3226 free_bitmap_index(b->base);
3227 free(b);
3228 }
3229
3230 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
3231 const struct object_id *oid)
3232 {
3233 return bitmap_git &&
3234 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
3235 }
3236
3237 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
3238 enum object_type object_type)
3239 {
3240 struct bitmap *result = bitmap_git->result;
3241 off_t total = 0;
3242 struct ewah_or_iterator it;
3243 eword_t filter;
3244 size_t i;
3245
3246 init_type_iterator(&it, bitmap_git, object_type);
3247 for (i = 0; i < result->word_alloc &&
3248 ewah_or_iterator_next(&filter, &it); i++) {
3249 eword_t word = result->words[i] & filter;
3250 size_t base = (i * BITS_IN_EWORD);
3251 unsigned offset;
3252
3253 if (!word)
3254 continue;
3255
3256 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
3257 if ((word >> offset) == 0)
3258 break;
3259
3260 offset += ewah_bit_ctz64(word >> offset);
3261
3262 if (bitmap_is_midx(bitmap_git)) {
3263 uint32_t pack_pos;
3264 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
3265 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
3266
3267 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
3268 struct packed_git *pack = nth_midxed_pack(bitmap_git->midx, pack_id);
3269
3270 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
3271 struct object_id oid;
3272 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
3273
3274 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
3275 oid_to_hex(&oid),
3276 pack->pack_name,
3277 (uintmax_t)offset);
3278 }
3279
3280 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
3281 } else {
3282 size_t pos = base + offset;
3283 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
3284 pack_pos_to_offset(bitmap_git->pack, pos);
3285 }
3286 }
3287 }
3288
3289 ewah_or_iterator_release(&it);
3290
3291 return total;
3292 }
3293
3294 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
3295 {
3296 struct bitmap *result = bitmap_git->result;
3297 struct eindex *eindex = &bitmap_git->ext_index;
3298 off_t total = 0;
3299 struct object_info oi = OBJECT_INFO_INIT;
3300 off_t object_size;
3301 size_t i;
3302
3303 oi.disk_sizep = &object_size;
3304
3305 for (i = 0; i < eindex->count; i++) {
3306 struct object *obj = eindex->objects[i];
3307
3308 if (!bitmap_get(result,
3309 st_add(bitmap_num_objects_total(bitmap_git),
3310 i)))
3311 continue;
3312
3313 if (odb_read_object_info_extended(bitmap_repo(bitmap_git)->objects,
3314 &obj->oid, &oi, 0) < 0)
3315 die(_("unable to get disk usage of '%s'"),
3316 oid_to_hex(&obj->oid));
3317
3318 total += object_size;
3319 }
3320 return total;
3321 }
3322
3323 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
3324 struct rev_info *revs)
3325 {
3326 off_t total = 0;
3327
3328 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
3329 if (revs->tree_objects)
3330 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
3331 if (revs->blob_objects)
3332 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
3333 if (revs->tag_objects)
3334 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
3335
3336 total += get_disk_usage_for_extended(bitmap_git);
3337
3338 return total;
3339 }
3340
3341 int bitmap_is_midx(struct bitmap_index *bitmap_git)
3342 {
3343 return !!bitmap_git->midx;
3344 }
3345
3346 static const struct string_list *bitmap_preferred_tips(struct repository *r)
3347 {
3348 const struct string_list *dest;
3349
3350 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
3351 return dest;
3352 return NULL;
3353 }
3354
3355 void for_each_preferred_bitmap_tip(struct repository *repo,
3356 refs_for_each_cb cb, void *cb_data)
3357 {
3358 struct refs_for_each_ref_options opts = { 0 };
3359 struct string_list_item *item;
3360 const struct string_list *preferred_tips;
3361 struct strbuf buf = STRBUF_INIT;
3362
3363 preferred_tips = bitmap_preferred_tips(repo);
3364 if (!preferred_tips)
3365 return;
3366
3367 for_each_string_list_item(item, preferred_tips) {
3368 opts.prefix = item->string;
3369
3370 if (!ends_with(opts.prefix, "/")) {
3371 strbuf_reset(&buf);
3372 strbuf_addf(&buf, "%s/", opts.prefix);
3373 opts.prefix = buf.buf;
3374 }
3375
3376 refs_for_each_ref_ext(get_main_ref_store(repo),
3377 cb, cb_data, &opts);
3378 }
3379
3380 strbuf_release(&buf);
3381 }
3382
3383 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
3384 {
3385 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
3386 struct string_list_item *item;
3387
3388 if (!preferred_tips)
3389 return 0;
3390
3391 for_each_string_list_item(item, preferred_tips) {
3392 if (starts_with(refname, item->string))
3393 return 1;
3394 }
3395
3396 return 0;
3397 }
3398
3399 static int verify_bitmap_file(const struct git_hash_algo *algop,
3400 const char *name)
3401 {
3402 struct stat st;
3403 unsigned char *data;
3404 int fd = git_open(name);
3405 int res = 0;
3406
3407 /* It is OK to not have the file. */
3408 if (fd < 0 || fstat(fd, &st)) {
3409 if (fd >= 0)
3410 close(fd);
3411 return 0;
3412 }
3413
3414 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3415 close(fd);
3416 if (!hashfile_checksum_valid(algop, data, st.st_size))
3417 res = error(_("bitmap file '%s' has invalid checksum"),
3418 name);
3419
3420 munmap(data, st.st_size);
3421 return res;
3422 }
3423
3424 int verify_bitmap_files(struct repository *r)
3425 {
3426 struct odb_source *source;
3427 struct packed_git *p;
3428 int res = 0;
3429
3430 odb_prepare_alternates(r->objects);
3431 for (source = r->objects->sources; source; source = source->next) {
3432 struct odb_source_files *files = odb_source_files_downcast(source);
3433 struct multi_pack_index *m = get_multi_pack_index(files->packed);
3434 char *midx_bitmap_name;
3435
3436 if (!m)
3437 continue;
3438
3439 midx_bitmap_name = midx_bitmap_filename(m);
3440 res |= verify_bitmap_file(r->hash_algo, midx_bitmap_name);
3441 free(midx_bitmap_name);
3442 }
3443
3444 repo_for_each_pack(r, p) {
3445 char *pack_bitmap_name = pack_bitmap_filename(p);
3446 res |= verify_bitmap_file(r->hash_algo, pack_bitmap_name);
3447 free(pack_bitmap_name);
3448 }
3449
3450 return res;
3451 }