interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / pack-bitmap.c

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