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 pack->bitmap_nr == pack->p->num_objects) {
2393 /*
2394 * If we're processing the first (in the case of a MIDX, the
2395 * preferred pack) or the only (in the case of single-pack
2396 * bitmaps) pack, then any delta in this range must have its
2397 * base chosen from the same pack:
2398 *
2399 * - In the single pack case, there is no other pack to choose
2400 * them from.
2401 *
2402 * - In the MIDX case, the first pack is the preferred pack, so
2403 * all ties are broken in favor of that pack (i.e. the one
2404 * we're currently processing). So any duplicate bases will be
2405 * resolved in favor of the pack we're processing.
2406 *
2407 * When REF_DELTAs are allowed, we can therefore reuse whole
2408 * words at a time without inspecting object headers. Otherwise,
2409 * inspect each object below to avoid reusing a REF_DELTA entry.
2410 *
2411 * The range must also contain every physical pack entry so that
2412 * bitmap and pack positions correspond.
2413 */
2414 while (pos < result->word_alloc &&
2415 pos < pack->bitmap_nr / BITS_IN_EWORD &&
2416 result->words[pos] == (eword_t)~0)
2417 pos++;
2418 memset(reuse->words, 0xFF, pos * sizeof(eword_t));
2419 }
2420
2421 for (; pos < result->word_alloc; pos++) {
2422 eword_t word = result->words[pos];
2423 size_t offset;
2424
2425 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2426 size_t bit_pos;
2427 uint32_t pack_pos;
2428 off_t ofs;
2429
2430 if (word >> offset == 0)
2431 break;
2432
2433 offset += ewah_bit_ctz64(word >> offset);
2434
2435 bit_pos = pos * BITS_IN_EWORD + offset;
2436 if (bit_pos < pack->bitmap_pos)
2437 continue;
2438 if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
2439 goto done;
2440
2441 if (bitmap_is_midx(bitmap_git)) {
2442 uint32_t midx_pos;
2443
2444 midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2445 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
2446
2447 if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
2448 BUG("could not find object in pack %s "
2449 "at offset %"PRIuMAX" in MIDX",
2450 pack_basename(pack->p), (uintmax_t)ofs);
2451 } else {
2452 pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
2453 if (pack_pos >= pack->p->num_objects)
2454 BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
2455 pack_basename(pack->p), (uintmax_t)pack_pos,
2456 pack->p->num_objects);
2457
2458 ofs = pack_pos_to_offset(pack->p, pack_pos);
2459 }
2460
2461 if (try_partial_reuse(bitmap_git, pack, bit_pos,
2462 pack_pos, ofs, reuse, &w_curs,
2463 allow_ref_delta) < 0) {
2464 /*
2465 * try_partial_reuse indicated we couldn't reuse
2466 * any bits, so there is no point in trying more
2467 * bits in the current word, or any other words
2468 * in result.
2469 *
2470 * Jump out of both loops to avoid future
2471 * unnecessary calls to try_partial_reuse.
2472 */
2473 goto done;
2474 }
2475 }
2476 }
2477
2478 done:
2479 unuse_pack(&w_curs);
2480 }
2481
2482 static int bitmapped_pack_cmp(const void *va, const void *vb)
2483 {
2484 const struct bitmapped_pack *a = va;
2485 const struct bitmapped_pack *b = vb;
2486
2487 if (a->bitmap_pos < b->bitmap_pos)
2488 return -1;
2489 if (a->bitmap_pos > b->bitmap_pos)
2490 return 1;
2491 return 0;
2492 }
2493
2494 void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2495 struct bitmapped_pack **packs_out,
2496 size_t *packs_nr_out,
2497 struct bitmap **reuse_out,
2498 int multi_pack_reuse,
2499 int allow_ref_delta)
2500 {
2501 struct repository *r = bitmap_repo(bitmap_git);
2502 struct bitmapped_pack *packs = NULL;
2503 struct bitmap *result = bitmap_git->result;
2504 struct bitmap *reuse;
2505 size_t i;
2506 size_t packs_nr = 0, packs_alloc = 0;
2507 size_t word_alloc;
2508 uint32_t objects_nr = 0;
2509
2510 assert(result);
2511
2512 load_reverse_index(r, bitmap_git);
2513
2514 if (!bitmap_is_midx(bitmap_git) || !bitmap_git->midx->chunk_bitmapped_packs)
2515 multi_pack_reuse = 0;
2516
2517 if (multi_pack_reuse) {
2518 struct multi_pack_index *m = bitmap_git->midx;
2519 for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) {
2520 struct bitmapped_pack pack;
2521 if (nth_bitmapped_pack(bitmap_git->midx, &pack, i) < 0) {
2522 warning(_("unable to load pack: '%s', disabling pack-reuse"),
2523 bitmap_git->midx->pack_names[i]);
2524 free(packs);
2525 return;
2526 }
2527
2528 if (!pack.bitmap_nr)
2529 continue;
2530
2531 if (is_pack_valid(pack.p)) {
2532 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2533 memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2534 }
2535
2536 objects_nr += pack.p->num_objects;
2537 }
2538
2539 QSORT(packs, packs_nr, bitmapped_pack_cmp);
2540 } else {
2541 struct packed_git *pack;
2542 uint32_t pack_int_id;
2543 uint32_t bitmap_nr;
2544
2545 if (bitmap_is_midx(bitmap_git)) {
2546 struct bitmapped_pack bitmapped_pack;
2547 struct multi_pack_index *m = bitmap_git->midx;
2548 uint32_t preferred_pack_pos;
2549
2550 while (m->base_midx)
2551 m = m->base_midx;
2552
2553 if (!m->chunk_bitmapped_packs) {
2554 /*
2555 * Without BTMP, determining the preferred
2556 * pack's range requires scanning the pseudo-pack.
2557 * Skip reuse and leave the bitmap result for
2558 * normal packing.
2559 */
2560 return;
2561 }
2562
2563 if (midx_preferred_pack(m, &preferred_pack_pos) < 0) {
2564 warning(_("unable to compute preferred pack, disabling pack-reuse"));
2565 return;
2566 }
2567
2568 pack = nth_midxed_pack(m, preferred_pack_pos);
2569 pack_int_id = preferred_pack_pos;
2570
2571 if (nth_bitmapped_pack(m, &bitmapped_pack,
2572 pack_int_id) < 0)
2573 return;
2574 pack = bitmapped_pack.p;
2575 bitmap_nr = bitmapped_pack.bitmap_nr;
2576 } else {
2577 pack = bitmap_git->pack;
2578 /*
2579 * Any value for 'pack_int_id' will do here. When we
2580 * process the pack via try_partial_reuse(), we won't
2581 * use the `pack_int_id` field since we have a non-MIDX
2582 * bitmap.
2583 *
2584 * Use '-1' as a sentinel value to make it clear
2585 * that we do not expect to read this field.
2586 */
2587 pack_int_id = -1;
2588 bitmap_nr = pack->num_objects;
2589 }
2590
2591 if (is_pack_valid(pack)) {
2592 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2593 packs[packs_nr].p = pack;
2594 packs[packs_nr].pack_int_id = pack_int_id;
2595 packs[packs_nr].bitmap_nr = bitmap_nr;
2596 packs[packs_nr].bitmap_pos = 0;
2597 packs[packs_nr].from_midx = bitmap_git->midx;
2598 packs_nr++;
2599 }
2600
2601 objects_nr = pack->num_objects;
2602 }
2603
2604 if (!packs_nr)
2605 return;
2606
2607 word_alloc = objects_nr / BITS_IN_EWORD;
2608 if (objects_nr % BITS_IN_EWORD)
2609 word_alloc++;
2610 reuse = bitmap_word_alloc(word_alloc);
2611
2612 for (i = 0; i < packs_nr; i++)
2613 reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse,
2614 allow_ref_delta);
2615
2616 if (bitmap_is_empty(reuse)) {
2617 free(packs);
2618 bitmap_free(reuse);
2619 return;
2620 }
2621
2622 /*
2623 * Drop any reused objects from the result, since they will not
2624 * need to be handled separately.
2625 */
2626 bitmap_and_not(result, reuse);
2627 *packs_out = packs;
2628 *packs_nr_out = packs_nr;
2629 *reuse_out = reuse;
2630 }
2631
2632 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2633 struct bitmap *bitmap, const struct object_id *oid)
2634 {
2635 int idx;
2636
2637 if (!bitmap)
2638 return 0;
2639
2640 idx = bitmap_position(bitmap_git, oid);
2641 return idx >= 0 && bitmap_get(bitmap, idx);
2642 }
2643
2644 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2645 struct rev_info *revs,
2646 show_reachable_fn show_reachable)
2647 {
2648 assert(bitmap_git->result);
2649
2650 show_objects_for_type(bitmap_git, bitmap_git->result,
2651 OBJ_COMMIT, show_reachable, NULL);
2652 if (revs->tree_objects)
2653 show_objects_for_type(bitmap_git, bitmap_git->result,
2654 OBJ_TREE, show_reachable, NULL);
2655 if (revs->blob_objects)
2656 show_objects_for_type(bitmap_git, bitmap_git->result,
2657 OBJ_BLOB, show_reachable, NULL);
2658 if (revs->tag_objects)
2659 show_objects_for_type(bitmap_git, bitmap_git->result,
2660 OBJ_TAG, show_reachable, NULL);
2661
2662 show_extended_objects(bitmap_git, revs, show_reachable);
2663 }
2664
2665 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2666 enum object_type type)
2667 {
2668 struct bitmap *objects = bitmap_git->result;
2669 struct eindex *eindex = &bitmap_git->ext_index;
2670
2671 uint32_t i = 0, count = 0;
2672 struct ewah_or_iterator it;
2673 eword_t filter;
2674
2675 init_type_iterator(&it, bitmap_git, type);
2676
2677 while (i < objects->word_alloc && ewah_or_iterator_next(&filter, &it)) {
2678 eword_t word = objects->words[i++] & filter;
2679 count += ewah_bit_popcount64(word);
2680 }
2681
2682 for (i = 0; i < eindex->count; ++i) {
2683 if (eindex->objects[i]->type == type &&
2684 bitmap_get(objects,
2685 st_add(bitmap_num_objects_total(bitmap_git), i)))
2686 count++;
2687 }
2688
2689 ewah_or_iterator_release(&it);
2690
2691 return count;
2692 }
2693
2694 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2695 uint32_t *commits, uint32_t *trees,
2696 uint32_t *blobs, uint32_t *tags)
2697 {
2698 assert(bitmap_git->result);
2699
2700 if (commits)
2701 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2702
2703 if (trees)
2704 *trees = count_object_type(bitmap_git, OBJ_TREE);
2705
2706 if (blobs)
2707 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2708
2709 if (tags)
2710 *tags = count_object_type(bitmap_git, OBJ_TAG);
2711 }
2712
2713 struct bitmap_test_data {
2714 struct bitmap_index *bitmap_git;
2715 struct bitmap *base;
2716 struct bitmap *commits;
2717 struct bitmap *trees;
2718 struct bitmap *blobs;
2719 struct bitmap *tags;
2720 struct progress *prg;
2721 size_t seen;
2722
2723 struct bitmap_test_data *base_tdata;
2724 };
2725
2726 static void test_bitmap_type(struct bitmap_test_data *tdata,
2727 struct object *obj, int pos)
2728 {
2729 enum object_type bitmap_type = OBJ_NONE;
2730 int bitmaps_nr = 0;
2731
2732 if (bitmap_is_midx(tdata->bitmap_git)) {
2733 while (pos < tdata->bitmap_git->midx->num_objects_in_base)
2734 tdata = tdata->base_tdata;
2735 }
2736
2737 if (bitmap_get(tdata->commits, pos)) {
2738 bitmap_type = OBJ_COMMIT;
2739 bitmaps_nr++;
2740 }
2741 if (bitmap_get(tdata->trees, pos)) {
2742 bitmap_type = OBJ_TREE;
2743 bitmaps_nr++;
2744 }
2745 if (bitmap_get(tdata->blobs, pos)) {
2746 bitmap_type = OBJ_BLOB;
2747 bitmaps_nr++;
2748 }
2749 if (bitmap_get(tdata->tags, pos)) {
2750 bitmap_type = OBJ_TAG;
2751 bitmaps_nr++;
2752 }
2753
2754 if (bitmap_type == OBJ_NONE)
2755 die(_("object '%s' not found in type bitmaps"),
2756 oid_to_hex(&obj->oid));
2757
2758 if (bitmaps_nr > 1)
2759 die(_("object '%s' does not have a unique type"),
2760 oid_to_hex(&obj->oid));
2761
2762 if (bitmap_type != obj->type)
2763 die(_("object '%s': real type '%s', expected: '%s'"),
2764 oid_to_hex(&obj->oid),
2765 type_name(obj->type),
2766 type_name(bitmap_type));
2767 }
2768
2769 static void test_show_object(struct object *object,
2770 const char *name UNUSED,
2771 void *data)
2772 {
2773 struct bitmap_test_data *tdata = data;
2774 int bitmap_pos;
2775
2776 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2777 if (bitmap_pos < 0)
2778 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2779 test_bitmap_type(tdata, object, bitmap_pos);
2780
2781 bitmap_set(tdata->base, bitmap_pos);
2782 display_progress(tdata->prg, ++tdata->seen);
2783 }
2784
2785 static void test_show_commit(struct commit *commit, void *data)
2786 {
2787 struct bitmap_test_data *tdata = data;
2788 int bitmap_pos;
2789
2790 bitmap_pos = bitmap_position(tdata->bitmap_git,
2791 &commit->object.oid);
2792 if (bitmap_pos < 0)
2793 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2794 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2795
2796 bitmap_set(tdata->base, bitmap_pos);
2797 display_progress(tdata->prg, ++tdata->seen);
2798 }
2799
2800 static uint32_t bitmap_total_entry_count(struct bitmap_index *bitmap_git)
2801 {
2802 uint32_t total = 0;
2803 do {
2804 total = st_add(total, bitmap_git->entry_count);
2805 bitmap_git = bitmap_git->base;
2806 } while (bitmap_git);
2807
2808 return total;
2809 }
2810
2811 static void bitmap_test_data_prepare(struct bitmap_test_data *tdata,
2812 struct bitmap_index *bitmap_git)
2813 {
2814 memset(tdata, 0, sizeof(struct bitmap_test_data));
2815
2816 tdata->bitmap_git = bitmap_git;
2817 tdata->base = bitmap_new();
2818 tdata->commits = ewah_to_bitmap(bitmap_git->commits);
2819 tdata->trees = ewah_to_bitmap(bitmap_git->trees);
2820 tdata->blobs = ewah_to_bitmap(bitmap_git->blobs);
2821 tdata->tags = ewah_to_bitmap(bitmap_git->tags);
2822
2823 if (bitmap_git->base) {
2824 tdata->base_tdata = xmalloc(sizeof(struct bitmap_test_data));
2825 bitmap_test_data_prepare(tdata->base_tdata, bitmap_git->base);
2826 }
2827 }
2828
2829 static void bitmap_test_data_release(struct bitmap_test_data *tdata)
2830 {
2831 if (!tdata)
2832 return;
2833
2834 bitmap_test_data_release(tdata->base_tdata);
2835 free(tdata->base_tdata);
2836
2837 bitmap_free(tdata->base);
2838 bitmap_free(tdata->commits);
2839 bitmap_free(tdata->trees);
2840 bitmap_free(tdata->blobs);
2841 bitmap_free(tdata->tags);
2842 }
2843
2844 void test_bitmap_walk(struct rev_info *revs)
2845 {
2846 struct object *root;
2847 struct bitmap *result = NULL;
2848 size_t result_popcnt;
2849 struct bitmap_test_data tdata;
2850 struct bitmap_index *bitmap_git, *found;
2851 struct ewah_bitmap *bm;
2852
2853 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2854 die(_("failed to load bitmap indexes"));
2855
2856 if (revs->pending.nr != 1)
2857 die(_("you must specify exactly one commit to test"));
2858
2859 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s, %d total)",
2860 bitmap_git->version,
2861 bitmap_git->entry_count,
2862 bitmap_git->table_lookup ? "" : " loaded",
2863 bitmap_total_entry_count(bitmap_git));
2864
2865 root = revs->pending.objects[0].item;
2866 bm = find_bitmap_for_commit(bitmap_git, (struct commit *)root, &found);
2867
2868 if (bm) {
2869 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2870 oid_to_hex(&root->oid),
2871 (int)bm->bit_size, ewah_checksum(bm));
2872
2873 if (bitmap_is_midx(found))
2874 fprintf_ln(stderr, "Located via MIDX '%s'.",
2875 midx_get_checksum_hex(found->midx));
2876 else
2877 fprintf_ln(stderr, "Located via pack '%s'.",
2878 hash_to_hex_algop(found->pack->hash,
2879 revs->repo->hash_algo));
2880
2881 result = ewah_to_bitmap(bm);
2882 }
2883
2884 if (!result)
2885 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2886
2887 revs->tag_objects = 1;
2888 revs->tree_objects = 1;
2889 revs->blob_objects = 1;
2890
2891 result_popcnt = bitmap_popcount(result);
2892
2893 if (prepare_revision_walk(revs))
2894 die(_("revision walk setup failed"));
2895
2896 bitmap_test_data_prepare(&tdata, bitmap_git);
2897 tdata.prg = start_progress(revs->repo,
2898 "Verifying bitmap entries",
2899 result_popcnt);
2900
2901 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2902
2903 stop_progress(&tdata.prg);
2904
2905 if (bitmap_equals(result, tdata.base))
2906 fprintf_ln(stderr, "OK!");
2907 else
2908 die(_("mismatch in bitmap results"));
2909
2910 bitmap_free(result);
2911 bitmap_test_data_release(&tdata);
2912 free_bitmap_index(bitmap_git);
2913 }
2914
2915 int test_bitmap_commits(struct repository *r)
2916 {
2917 struct object_id oid;
2918 MAYBE_UNUSED void *value;
2919 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2920
2921 if (!bitmap_git)
2922 die(_("failed to load bitmap indexes"));
2923
2924 /*
2925 * Since this function needs to print the bitmapped
2926 * commits, bypass the commit lookup table (if one exists)
2927 * by forcing the bitmap to eagerly load its entries.
2928 */
2929 if (bitmap_git->table_lookup) {
2930 if (load_bitmap_entries_v1(bitmap_git) < 0)
2931 die(_("failed to load bitmap indexes"));
2932 }
2933
2934 kh_foreach(bitmap_git->bitmaps, oid, value, {
2935 printf_ln("%s", oid_to_hex(&oid));
2936 });
2937
2938 free_bitmap_index(bitmap_git);
2939
2940 return 0;
2941 }
2942
2943 int test_bitmap_commits_with_offset(struct repository *r)
2944 {
2945 struct object_id oid;
2946 struct stored_bitmap *stored;
2947 struct bitmap_index *bitmap_git;
2948 size_t commit_idx_pos_map_pos, xor_offset_map_pos, flag_map_pos,
2949 ewah_bitmap_map_pos;
2950
2951 bitmap_git = prepare_bitmap_git(r);
2952 if (!bitmap_git)
2953 die(_("failed to load bitmap indexes"));
2954
2955 /*
2956 * Since this function needs to know the position of each individual
2957 * bitmap, bypass the commit lookup table (if one exists) by forcing
2958 * the bitmap to eagerly load its entries.
2959 */
2960 if (bitmap_git->table_lookup) {
2961 if (load_bitmap_entries_v1(bitmap_git) < 0)
2962 die(_("failed to load bitmap indexes"));
2963 }
2964
2965 kh_foreach (bitmap_git->bitmaps, oid, stored, {
2966 commit_idx_pos_map_pos = stored->map_pos;
2967 xor_offset_map_pos = stored->map_pos + sizeof(uint32_t);
2968 flag_map_pos = xor_offset_map_pos + sizeof(uint8_t);
2969 ewah_bitmap_map_pos = flag_map_pos + sizeof(uint8_t);
2970
2971 printf_ln("%s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
2972 oid_to_hex(&oid),
2973 (uintmax_t)commit_idx_pos_map_pos,
2974 (uintmax_t)xor_offset_map_pos,
2975 (uintmax_t)flag_map_pos,
2976 (uintmax_t)ewah_bitmap_map_pos);
2977 })
2978 ;
2979
2980 free_bitmap_index(bitmap_git);
2981
2982 return 0;
2983 }
2984
2985 int test_bitmap_hashes(struct repository *r)
2986 {
2987 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2988 struct object_id oid;
2989 uint32_t i, index_pos;
2990
2991 if (!bitmap_git || !bitmap_git->hashes)
2992 goto cleanup;
2993
2994 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2995 if (bitmap_is_midx(bitmap_git))
2996 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2997 else
2998 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2999
3000 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
3001
3002 printf_ln("%s %"PRIu32"",
3003 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
3004 }
3005
3006 cleanup:
3007 free_bitmap_index(bitmap_git);
3008
3009 return 0;
3010 }
3011
3012 static void bit_pos_to_object_id(struct bitmap_index *bitmap_git,
3013 uint32_t bit_pos,
3014 struct object_id *oid)
3015 {
3016 uint32_t index_pos;
3017
3018 if (bitmap_is_midx(bitmap_git))
3019 index_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
3020 else
3021 index_pos = pack_pos_to_index(bitmap_git->pack, bit_pos);
3022
3023 nth_bitmap_object_oid(bitmap_git, oid, index_pos);
3024 }
3025
3026 int test_bitmap_pseudo_merges(struct repository *r)
3027 {
3028 struct bitmap_index *bitmap_git;
3029 uint32_t i;
3030
3031 bitmap_git = prepare_bitmap_git(r);
3032 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
3033 goto cleanup;
3034
3035 for (i = 0; i < bitmap_git->pseudo_merges.nr; i++) {
3036 struct pseudo_merge *merge;
3037 struct ewah_bitmap *commits_bitmap, *merge_bitmap;
3038
3039 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3040 &bitmap_git->pseudo_merges.v[i]);
3041 commits_bitmap = merge->commits;
3042 merge_bitmap = pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
3043 merge);
3044
3045 printf("at=%"PRIuMAX", commits=%"PRIuMAX", objects=%"PRIuMAX"\n",
3046 (uintmax_t)merge->at,
3047 (uintmax_t)ewah_bitmap_popcount(commits_bitmap),
3048 (uintmax_t)ewah_bitmap_popcount(merge_bitmap));
3049 }
3050
3051 cleanup:
3052 free_bitmap_index(bitmap_git);
3053 return 0;
3054 }
3055
3056 static void dump_ewah_object_ids(struct bitmap_index *bitmap_git,
3057 struct ewah_bitmap *bitmap)
3058
3059 {
3060 struct ewah_iterator it;
3061 eword_t word;
3062 uint32_t pos = 0;
3063
3064 ewah_iterator_init(&it, bitmap);
3065
3066 while (ewah_iterator_next(&word, &it)) {
3067 struct object_id oid;
3068 uint32_t offset;
3069
3070 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
3071 if (!(word >> offset))
3072 break;
3073
3074 offset += ewah_bit_ctz64(word >> offset);
3075
3076 bit_pos_to_object_id(bitmap_git, pos + offset, &oid);
3077 printf("%s\n", oid_to_hex(&oid));
3078 }
3079 pos += BITS_IN_EWORD;
3080 }
3081 }
3082
3083 int test_bitmap_pseudo_merge_commits(struct repository *r, uint32_t n)
3084 {
3085 struct bitmap_index *bitmap_git;
3086 struct pseudo_merge *merge;
3087 int ret = 0;
3088
3089 bitmap_git = prepare_bitmap_git(r);
3090 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
3091 goto cleanup;
3092
3093 if (n >= bitmap_git->pseudo_merges.nr) {
3094 ret = error(_("pseudo-merge index out of range "
3095 "(%"PRIu32" >= %"PRIuMAX")"),
3096 n, (uintmax_t)bitmap_git->pseudo_merges.nr);
3097 goto cleanup;
3098 }
3099
3100 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3101 &bitmap_git->pseudo_merges.v[n]);
3102 dump_ewah_object_ids(bitmap_git, merge->commits);
3103
3104 cleanup:
3105 free_bitmap_index(bitmap_git);
3106 return ret;
3107 }
3108
3109 int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n)
3110 {
3111 struct bitmap_index *bitmap_git;
3112 struct pseudo_merge *merge;
3113 int ret = 0;
3114
3115 bitmap_git = prepare_bitmap_git(r);
3116 if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
3117 goto cleanup;
3118
3119 if (n >= bitmap_git->pseudo_merges.nr) {
3120 ret = error(_("pseudo-merge index out of range "
3121 "(%"PRIu32" >= %"PRIuMAX")"),
3122 n, (uintmax_t)bitmap_git->pseudo_merges.nr);
3123 goto cleanup;
3124 }
3125
3126 merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3127 &bitmap_git->pseudo_merges.v[n]);
3128
3129 dump_ewah_object_ids(bitmap_git,
3130 pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
3131 merge));
3132
3133 cleanup:
3134 free_bitmap_index(bitmap_git);
3135 return ret;
3136 }
3137
3138 int rebuild_bitmap(const uint32_t *reposition,
3139 struct ewah_bitmap *source,
3140 struct bitmap *dest)
3141 {
3142 uint32_t pos = 0;
3143 struct ewah_iterator it;
3144 eword_t word;
3145
3146 ewah_iterator_init(&it, source);
3147
3148 while (ewah_iterator_next(&word, &it)) {
3149 uint32_t offset, bit_pos;
3150
3151 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
3152 if ((word >> offset) == 0)
3153 break;
3154
3155 offset += ewah_bit_ctz64(word >> offset);
3156
3157 bit_pos = reposition[pos + offset];
3158 if (bit_pos > 0)
3159 bitmap_set(dest, bit_pos - 1);
3160 else /* can't reuse, we don't have the object */
3161 return -1;
3162 }
3163
3164 pos += BITS_IN_EWORD;
3165 }
3166 return 0;
3167 }
3168
3169 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
3170 struct packing_data *mapping)
3171 {
3172 struct repository *r = bitmap_repo(bitmap_git);
3173 uint32_t i, num_objects;
3174 uint32_t *reposition;
3175
3176 if (!bitmap_is_midx(bitmap_git))
3177 load_reverse_index(r, bitmap_git);
3178 else if (load_midx_revindex(bitmap_git->midx))
3179 BUG("rebuild_existing_bitmaps: missing required rev-cache "
3180 "extension");
3181
3182 num_objects = bitmap_num_objects_total(bitmap_git);
3183 CALLOC_ARRAY(reposition, num_objects);
3184
3185 for (i = 0; i < num_objects; ++i) {
3186 struct object_id oid;
3187 struct object_entry *oe;
3188 uint32_t index_pos;
3189
3190 if (bitmap_is_midx(bitmap_git))
3191 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
3192 else
3193 index_pos = pack_pos_to_index(bitmap_git->pack, i);
3194 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
3195 oe = packlist_find(mapping, &oid);
3196
3197 if (oe) {
3198 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
3199 if (!oe->hash)
3200 oe->hash = bitmap_name_hash(bitmap_git, index_pos);
3201 }
3202 }
3203
3204 return reposition;
3205 }
3206
3207 void free_bitmap_index(struct bitmap_index *b)
3208 {
3209 if (!b)
3210 return;
3211
3212 if (b->map)
3213 munmap(b->map, b->map_size);
3214 ewah_pool_free(b->commits);
3215 ewah_pool_free(b->trees);
3216 ewah_pool_free(b->blobs);
3217 ewah_pool_free(b->tags);
3218 free(b->commits_all);
3219 free(b->trees_all);
3220 free(b->blobs_all);
3221 free(b->tags_all);
3222 if (b->bitmaps) {
3223 struct stored_bitmap *sb;
3224 kh_foreach_value(b->bitmaps, sb, {
3225 ewah_pool_free(sb->root);
3226 free(sb);
3227 });
3228 }
3229 kh_destroy_oid_map(b->bitmaps);
3230 free(b->ext_index.objects);
3231 free(b->ext_index.hashes);
3232 kh_destroy_oid_pos(b->ext_index.positions);
3233 bitmap_free(b->result);
3234 bitmap_free(b->haves);
3235 if (bitmap_is_midx(b)) {
3236 /*
3237 * Multi-pack bitmaps need to have resources associated with
3238 * their on-disk reverse indexes unmapped so that stale .rev and
3239 * .bitmap files can be removed.
3240 *
3241 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
3242 * written in the same 'git multi-pack-index write --bitmap'
3243 * process. Close resources so they can be removed safely on
3244 * platforms like Windows.
3245 */
3246 close_midx_revindex(b->midx);
3247 }
3248 free_pseudo_merge_map(&b->pseudo_merges);
3249 free_bitmap_index(b->base);
3250 free(b);
3251 }
3252
3253 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
3254 const struct object_id *oid)
3255 {
3256 return bitmap_git &&
3257 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
3258 }
3259
3260 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
3261 enum object_type object_type)
3262 {
3263 struct bitmap *result = bitmap_git->result;
3264 off_t total = 0;
3265 struct ewah_or_iterator it;
3266 eword_t filter;
3267 size_t i;
3268
3269 init_type_iterator(&it, bitmap_git, object_type);
3270 for (i = 0; i < result->word_alloc &&
3271 ewah_or_iterator_next(&filter, &it); i++) {
3272 eword_t word = result->words[i] & filter;
3273 size_t base = (i * BITS_IN_EWORD);
3274 unsigned offset;
3275
3276 if (!word)
3277 continue;
3278
3279 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
3280 if ((word >> offset) == 0)
3281 break;
3282
3283 offset += ewah_bit_ctz64(word >> offset);
3284
3285 if (bitmap_is_midx(bitmap_git)) {
3286 uint32_t pack_pos;
3287 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
3288 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
3289
3290 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
3291 struct packed_git *pack = nth_midxed_pack(bitmap_git->midx, pack_id);
3292
3293 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
3294 struct object_id oid;
3295 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
3296
3297 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
3298 oid_to_hex(&oid),
3299 pack->pack_name,
3300 (uintmax_t)offset);
3301 }
3302
3303 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
3304 } else {
3305 size_t pos = base + offset;
3306 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
3307 pack_pos_to_offset(bitmap_git->pack, pos);
3308 }
3309 }
3310 }
3311
3312 ewah_or_iterator_release(&it);
3313
3314 return total;
3315 }
3316
3317 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
3318 {
3319 struct bitmap *result = bitmap_git->result;
3320 struct eindex *eindex = &bitmap_git->ext_index;
3321 off_t total = 0;
3322 struct object_info oi = OBJECT_INFO_INIT;
3323 off_t object_size;
3324 size_t i;
3325
3326 oi.disk_sizep = &object_size;
3327
3328 for (i = 0; i < eindex->count; i++) {
3329 struct object *obj = eindex->objects[i];
3330
3331 if (!bitmap_get(result,
3332 st_add(bitmap_num_objects_total(bitmap_git),
3333 i)))
3334 continue;
3335
3336 if (odb_read_object_info_extended(bitmap_repo(bitmap_git)->objects,
3337 &obj->oid, &oi, 0) < 0)
3338 die(_("unable to get disk usage of '%s'"),
3339 oid_to_hex(&obj->oid));
3340
3341 total += object_size;
3342 }
3343 return total;
3344 }
3345
3346 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
3347 struct rev_info *revs)
3348 {
3349 off_t total = 0;
3350
3351 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
3352 if (revs->tree_objects)
3353 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
3354 if (revs->blob_objects)
3355 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
3356 if (revs->tag_objects)
3357 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
3358
3359 total += get_disk_usage_for_extended(bitmap_git);
3360
3361 return total;
3362 }
3363
3364 int bitmap_is_midx(struct bitmap_index *bitmap_git)
3365 {
3366 return !!bitmap_git->midx;
3367 }
3368
3369 static const struct string_list *bitmap_preferred_tips(struct repository *r)
3370 {
3371 const struct string_list *dest;
3372
3373 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
3374 return dest;
3375 return NULL;
3376 }
3377
3378 void for_each_preferred_bitmap_tip(struct repository *repo,
3379 refs_for_each_cb cb, void *cb_data)
3380 {
3381 struct refs_for_each_ref_options opts = { 0 };
3382 struct string_list_item *item;
3383 const struct string_list *preferred_tips;
3384 struct strbuf buf = STRBUF_INIT;
3385
3386 preferred_tips = bitmap_preferred_tips(repo);
3387 if (!preferred_tips)
3388 return;
3389
3390 for_each_string_list_item(item, preferred_tips) {
3391 opts.prefix = item->string;
3392
3393 if (!ends_with(opts.prefix, "/")) {
3394 strbuf_reset(&buf);
3395 strbuf_addf(&buf, "%s/", opts.prefix);
3396 opts.prefix = buf.buf;
3397 }
3398
3399 refs_for_each_ref_ext(get_main_ref_store(repo),
3400 cb, cb_data, &opts);
3401 }
3402
3403 strbuf_release(&buf);
3404 }
3405
3406 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
3407 {
3408 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
3409 struct string_list_item *item;
3410
3411 if (!preferred_tips)
3412 return 0;
3413
3414 for_each_string_list_item(item, preferred_tips) {
3415 if (starts_with(refname, item->string))
3416 return 1;
3417 }
3418
3419 return 0;
3420 }
3421
3422 static int verify_bitmap_file(const struct git_hash_algo *algop,
3423 const char *name)
3424 {
3425 struct stat st;
3426 unsigned char *data;
3427 int fd = git_open(name);
3428 int res = 0;
3429
3430 /* It is OK to not have the file. */
3431 if (fd < 0 || fstat(fd, &st)) {
3432 if (fd >= 0)
3433 close(fd);
3434 return 0;
3435 }
3436
3437 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3438 close(fd);
3439 if (!hashfile_checksum_valid(algop, data, st.st_size))
3440 res = error(_("bitmap file '%s' has invalid checksum"),
3441 name);
3442
3443 munmap(data, st.st_size);
3444 return res;
3445 }
3446
3447 int verify_bitmap_files(struct repository *r)
3448 {
3449 struct odb_source *source;
3450 struct packed_git *p;
3451 int res = 0;
3452
3453 odb_prepare_alternates(r->objects);
3454 for (source = r->objects->sources; source; source = source->next) {
3455 struct odb_source_files *files = odb_source_files_downcast(source);
3456 struct multi_pack_index *m = get_multi_pack_index(files->packed);
3457 char *midx_bitmap_name;
3458
3459 if (!m)
3460 continue;
3461
3462 midx_bitmap_name = midx_bitmap_filename(m);
3463 res |= verify_bitmap_file(r->hash_algo, midx_bitmap_name);
3464 free(midx_bitmap_name);
3465 }
3466
3467 repo_for_each_pack(r, p) {
3468 char *pack_bitmap_name = pack_bitmap_filename(p);
3469 res |= verify_bitmap_file(r->hash_algo, pack_bitmap_name);
3470 free(pack_bitmap_name);
3471 }
3472
3473 return res;
3474 }