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