at commit d306f3f0

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