]> git.ipfire.org Git - thirdparty/git.git/blob - pack-bitmap.c
7367f62bb6ced4266e4bbf2d72445013315ddc00
[thirdparty/git.git] / pack-bitmap.c
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "commit.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "strbuf.h"
7 #include "tag.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "progress.h"
11 #include "list-objects.h"
12 #include "pack.h"
13 #include "pack-bitmap.h"
14 #include "pack-revindex.h"
15 #include "pack-objects.h"
16 #include "packfile.h"
17 #include "repository.h"
18 #include "trace2.h"
19 #include "object-file.h"
20 #include "object-store-ll.h"
21 #include "list-objects-filter-options.h"
22 #include "midx.h"
23 #include "config.h"
24
25 /*
26 * An entry on the bitmap index, representing the bitmap for a given
27 * commit.
28 */
29 struct stored_bitmap {
30 struct object_id oid;
31 struct ewah_bitmap *root;
32 struct stored_bitmap *xor;
33 int flags;
34 };
35
36 /*
37 * The active bitmap index for a repository. By design, repositories only have
38 * a single bitmap index available (the index for the biggest packfile in
39 * the repository), since bitmap indexes need full closure.
40 *
41 * If there is more than one bitmap index available (e.g. because of alternates),
42 * the active bitmap index is the largest one.
43 */
44 struct bitmap_index {
45 /*
46 * The pack or multi-pack index (MIDX) that this bitmap index belongs
47 * to.
48 *
49 * Exactly one of these must be non-NULL; this specifies the object
50 * order used to interpret this bitmap.
51 */
52 struct packed_git *pack;
53 struct multi_pack_index *midx;
54
55 /*
56 * Mark the first `reuse_objects` in the packfile as reused:
57 * they will be sent as-is without using them for repacking
58 * calculations
59 */
60 uint32_t reuse_objects;
61
62 /* mmapped buffer of the whole bitmap index */
63 unsigned char *map;
64 size_t map_size; /* size of the mmaped buffer */
65 size_t map_pos; /* current position when loading the index */
66
67 /*
68 * Type indexes.
69 *
70 * Each bitmap marks which objects in the packfile are of the given
71 * type. This provides type information when yielding the objects from
72 * the packfile during a walk, which allows for better delta bases.
73 */
74 struct ewah_bitmap *commits;
75 struct ewah_bitmap *trees;
76 struct ewah_bitmap *blobs;
77 struct ewah_bitmap *tags;
78
79 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
80 kh_oid_map_t *bitmaps;
81
82 /* Number of bitmapped commits */
83 uint32_t entry_count;
84
85 /* If not NULL, this is a name-hash cache pointing into map. */
86 uint32_t *hashes;
87
88 /* The checksum of the packfile or MIDX; points into map. */
89 const unsigned char *checksum;
90
91 /*
92 * If not NULL, this point into the commit table extension
93 * (within the memory mapped region `map`).
94 */
95 unsigned char *table_lookup;
96
97 /*
98 * Extended index.
99 *
100 * When trying to perform bitmap operations with objects that are not
101 * packed in `pack`, these objects are added to this "fake index" and
102 * are assumed to appear at the end of the packfile for all operations
103 */
104 struct eindex {
105 struct object **objects;
106 uint32_t *hashes;
107 uint32_t count, alloc;
108 kh_oid_pos_t *positions;
109 } ext_index;
110
111 /* Bitmap result of the last performed walk */
112 struct bitmap *result;
113
114 /* "have" bitmap from the last performed walk */
115 struct bitmap *haves;
116
117 /* Version of the bitmap index */
118 unsigned int version;
119 };
120
121 static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
122 {
123 struct ewah_bitmap *parent;
124 struct ewah_bitmap *composed;
125
126 if (!st->xor)
127 return st->root;
128
129 composed = ewah_pool_new();
130 parent = lookup_stored_bitmap(st->xor);
131 ewah_xor(st->root, parent, composed);
132
133 ewah_pool_free(st->root);
134 st->root = composed;
135 st->xor = NULL;
136
137 return composed;
138 }
139
140 /*
141 * Read a bitmap from the current read position on the mmaped
142 * index, and increase the read position accordingly
143 */
144 static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
145 {
146 struct ewah_bitmap *b = ewah_pool_new();
147
148 ssize_t bitmap_size = ewah_read_mmap(b,
149 index->map + index->map_pos,
150 index->map_size - index->map_pos);
151
152 if (bitmap_size < 0) {
153 error(_("failed to load bitmap index (corrupted?)"));
154 ewah_pool_free(b);
155 return NULL;
156 }
157
158 index->map_pos += bitmap_size;
159 return b;
160 }
161
162 static uint32_t bitmap_num_objects(struct bitmap_index *index)
163 {
164 if (index->midx)
165 return index->midx->num_objects;
166 return index->pack->num_objects;
167 }
168
169 static int load_bitmap_header(struct bitmap_index *index)
170 {
171 struct bitmap_disk_header *header = (void *)index->map;
172 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
173
174 if (index->map_size < header_size + the_hash_algo->rawsz)
175 return error(_("corrupted bitmap index (too small)"));
176
177 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
178 return error(_("corrupted bitmap index file (wrong header)"));
179
180 index->version = ntohs(header->version);
181 if (index->version != 1)
182 return error(_("unsupported version '%d' for bitmap index file"), index->version);
183
184 /* Parse known bitmap format options */
185 {
186 uint32_t flags = ntohs(header->options);
187 size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
188 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
189
190 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
191 BUG("unsupported options for bitmap index file "
192 "(Git requires BITMAP_OPT_FULL_DAG)");
193
194 if (flags & BITMAP_OPT_HASH_CACHE) {
195 if (cache_size > index_end - index->map - header_size)
196 return error(_("corrupted bitmap index file (too short to fit hash cache)"));
197 index->hashes = (void *)(index_end - cache_size);
198 index_end -= cache_size;
199 }
200
201 if (flags & BITMAP_OPT_LOOKUP_TABLE) {
202 size_t table_size = st_mult(ntohl(header->entry_count),
203 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
204 if (table_size > index_end - index->map - header_size)
205 return error(_("corrupted bitmap index file (too short to fit lookup table)"));
206 if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
207 index->table_lookup = (void *)(index_end - table_size);
208 index_end -= table_size;
209 }
210 }
211
212 index->entry_count = ntohl(header->entry_count);
213 index->checksum = header->checksum;
214 index->map_pos += header_size;
215 return 0;
216 }
217
218 static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
219 struct ewah_bitmap *root,
220 const struct object_id *oid,
221 struct stored_bitmap *xor_with,
222 int flags)
223 {
224 struct stored_bitmap *stored;
225 khiter_t hash_pos;
226 int ret;
227
228 stored = xmalloc(sizeof(struct stored_bitmap));
229 stored->root = root;
230 stored->xor = xor_with;
231 stored->flags = flags;
232 oidcpy(&stored->oid, oid);
233
234 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
235
236 /*
237 * A 0 return code means the insertion succeeded with no changes,
238 * because the SHA1 already existed on the map. This is bad, there
239 * shouldn't be duplicated commits in the index.
240 */
241 if (ret == 0) {
242 error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
243 return NULL;
244 }
245
246 kh_value(index->bitmaps, hash_pos) = stored;
247 return stored;
248 }
249
250 static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
251 {
252 uint32_t result = get_be32(buffer + *pos);
253 (*pos) += sizeof(result);
254 return result;
255 }
256
257 static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
258 {
259 return buffer[(*pos)++];
260 }
261
262 #define MAX_XOR_OFFSET 160
263
264 static int nth_bitmap_object_oid(struct bitmap_index *index,
265 struct object_id *oid,
266 uint32_t n)
267 {
268 if (index->midx)
269 return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
270 return nth_packed_object_id(oid, index->pack, n);
271 }
272
273 static int load_bitmap_entries_v1(struct bitmap_index *index)
274 {
275 uint32_t i;
276 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
277
278 for (i = 0; i < index->entry_count; ++i) {
279 int xor_offset, flags;
280 struct ewah_bitmap *bitmap = NULL;
281 struct stored_bitmap *xor_bitmap = NULL;
282 uint32_t commit_idx_pos;
283 struct object_id oid;
284
285 if (index->map_size - index->map_pos < 6)
286 return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
287
288 commit_idx_pos = read_be32(index->map, &index->map_pos);
289 xor_offset = read_u8(index->map, &index->map_pos);
290 flags = read_u8(index->map, &index->map_pos);
291
292 if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
293 return error(_("corrupt ewah bitmap: commit index %u out of range"),
294 (unsigned)commit_idx_pos);
295
296 bitmap = read_bitmap_1(index);
297 if (!bitmap)
298 return -1;
299
300 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
301 return error(_("corrupted bitmap pack index"));
302
303 if (xor_offset > 0) {
304 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
305
306 if (!xor_bitmap)
307 return error(_("invalid XOR offset in bitmap pack index"));
308 }
309
310 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
311 index, bitmap, &oid, xor_bitmap, flags);
312 }
313
314 return 0;
315 }
316
317 char *midx_bitmap_filename(struct multi_pack_index *midx)
318 {
319 struct strbuf buf = STRBUF_INIT;
320
321 get_midx_filename(&buf, midx->object_dir);
322 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
323
324 return strbuf_detach(&buf, NULL);
325 }
326
327 char *pack_bitmap_filename(struct packed_git *p)
328 {
329 size_t len;
330
331 if (!strip_suffix(p->pack_name, ".pack", &len))
332 BUG("pack_name does not end in .pack");
333 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
334 }
335
336 static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
337 struct multi_pack_index *midx)
338 {
339 struct stat st;
340 char *bitmap_name = midx_bitmap_filename(midx);
341 int fd = git_open(bitmap_name);
342 uint32_t i;
343 struct packed_git *preferred;
344
345 if (fd < 0) {
346 if (errno != ENOENT)
347 warning_errno("cannot open '%s'", bitmap_name);
348 free(bitmap_name);
349 return -1;
350 }
351 free(bitmap_name);
352
353 if (fstat(fd, &st)) {
354 error_errno(_("cannot fstat bitmap file"));
355 close(fd);
356 return -1;
357 }
358
359 if (bitmap_git->pack || bitmap_git->midx) {
360 struct strbuf buf = STRBUF_INIT;
361 get_midx_filename(&buf, midx->object_dir);
362 trace2_data_string("bitmap", the_repository,
363 "ignoring extra midx bitmap file", buf.buf);
364 close(fd);
365 strbuf_release(&buf);
366 return -1;
367 }
368
369 bitmap_git->midx = midx;
370 bitmap_git->map_size = xsize_t(st.st_size);
371 bitmap_git->map_pos = 0;
372 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
373 MAP_PRIVATE, fd, 0);
374 close(fd);
375
376 if (load_bitmap_header(bitmap_git) < 0)
377 goto cleanup;
378
379 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
380 error(_("checksum doesn't match in MIDX and bitmap"));
381 goto cleanup;
382 }
383
384 if (load_midx_revindex(bitmap_git->midx)) {
385 warning(_("multi-pack bitmap is missing required reverse index"));
386 goto cleanup;
387 }
388
389 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
390 if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
391 warning(_("could not open pack %s"),
392 bitmap_git->midx->pack_names[i]);
393 goto cleanup;
394 }
395 }
396
397 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
398 if (!is_pack_valid(preferred)) {
399 warning(_("preferred pack (%s) is invalid"),
400 preferred->pack_name);
401 goto cleanup;
402 }
403
404 return 0;
405
406 cleanup:
407 munmap(bitmap_git->map, bitmap_git->map_size);
408 bitmap_git->map_size = 0;
409 bitmap_git->map_pos = 0;
410 bitmap_git->map = NULL;
411 bitmap_git->midx = NULL;
412 return -1;
413 }
414
415 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
416 {
417 int fd;
418 struct stat st;
419 char *bitmap_name;
420
421 bitmap_name = pack_bitmap_filename(packfile);
422 fd = git_open(bitmap_name);
423
424 if (fd < 0) {
425 if (errno != ENOENT)
426 warning_errno("cannot open '%s'", bitmap_name);
427 free(bitmap_name);
428 return -1;
429 }
430 free(bitmap_name);
431
432 if (fstat(fd, &st)) {
433 error_errno(_("cannot fstat bitmap file"));
434 close(fd);
435 return -1;
436 }
437
438 if (bitmap_git->pack || bitmap_git->midx) {
439 trace2_data_string("bitmap", the_repository,
440 "ignoring extra bitmap file", packfile->pack_name);
441 close(fd);
442 return -1;
443 }
444
445 if (!is_pack_valid(packfile)) {
446 close(fd);
447 return -1;
448 }
449
450 bitmap_git->pack = packfile;
451 bitmap_git->map_size = xsize_t(st.st_size);
452 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
453 bitmap_git->map_pos = 0;
454 close(fd);
455
456 if (load_bitmap_header(bitmap_git) < 0) {
457 munmap(bitmap_git->map, bitmap_git->map_size);
458 bitmap_git->map = NULL;
459 bitmap_git->map_size = 0;
460 bitmap_git->map_pos = 0;
461 bitmap_git->pack = NULL;
462 return -1;
463 }
464
465 trace2_data_string("bitmap", the_repository, "opened bitmap file",
466 packfile->pack_name);
467 return 0;
468 }
469
470 static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
471 {
472 if (bitmap_is_midx(bitmap_git)) {
473 uint32_t i;
474 int ret;
475
476 /*
477 * The multi-pack-index's .rev file is already loaded via
478 * open_pack_bitmap_1().
479 *
480 * But we still need to open the individual pack .rev files,
481 * since we will need to make use of them in pack-objects.
482 */
483 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
484 ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
485 if (ret)
486 return ret;
487 }
488 return 0;
489 }
490 return load_pack_revindex(r, bitmap_git->pack);
491 }
492
493 static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
494 {
495 assert(bitmap_git->map);
496
497 bitmap_git->bitmaps = kh_init_oid_map();
498 bitmap_git->ext_index.positions = kh_init_oid_pos();
499
500 if (load_reverse_index(r, bitmap_git))
501 goto failed;
502
503 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
504 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
505 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
506 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
507 goto failed;
508
509 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
510 goto failed;
511
512 return 0;
513
514 failed:
515 munmap(bitmap_git->map, bitmap_git->map_size);
516 bitmap_git->map = NULL;
517 bitmap_git->map_size = 0;
518
519 kh_destroy_oid_map(bitmap_git->bitmaps);
520 bitmap_git->bitmaps = NULL;
521
522 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
523 bitmap_git->ext_index.positions = NULL;
524
525 return -1;
526 }
527
528 static int open_pack_bitmap(struct repository *r,
529 struct bitmap_index *bitmap_git)
530 {
531 struct packed_git *p;
532 int ret = -1;
533
534 for (p = get_all_packs(r); p; p = p->next) {
535 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
536 ret = 0;
537 /*
538 * The only reason to keep looking is to report
539 * duplicates.
540 */
541 if (!trace2_is_enabled())
542 break;
543 }
544 }
545
546 return ret;
547 }
548
549 static int open_midx_bitmap(struct repository *r,
550 struct bitmap_index *bitmap_git)
551 {
552 int ret = -1;
553 struct multi_pack_index *midx;
554
555 assert(!bitmap_git->map);
556
557 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
558 if (!open_midx_bitmap_1(bitmap_git, midx))
559 ret = 0;
560 }
561 return ret;
562 }
563
564 static int open_bitmap(struct repository *r,
565 struct bitmap_index *bitmap_git)
566 {
567 int found;
568
569 assert(!bitmap_git->map);
570
571 found = !open_midx_bitmap(r, bitmap_git);
572
573 /*
574 * these will all be skipped if we opened a midx bitmap; but run it
575 * anyway if tracing is enabled to report the duplicates
576 */
577 if (!found || trace2_is_enabled())
578 found |= !open_pack_bitmap(r, bitmap_git);
579
580 return found ? 0 : -1;
581 }
582
583 struct bitmap_index *prepare_bitmap_git(struct repository *r)
584 {
585 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
586
587 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
588 return bitmap_git;
589
590 free_bitmap_index(bitmap_git);
591 return NULL;
592 }
593
594 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
595 {
596 struct repository *r = the_repository;
597 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
598
599 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
600 return bitmap_git;
601
602 free_bitmap_index(bitmap_git);
603 return NULL;
604 }
605
606 struct include_data {
607 struct bitmap_index *bitmap_git;
608 struct bitmap *base;
609 struct bitmap *seen;
610 };
611
612 struct bitmap_lookup_table_triplet {
613 uint32_t commit_pos;
614 uint64_t offset;
615 uint32_t xor_row;
616 };
617
618 struct bitmap_lookup_table_xor_item {
619 struct object_id oid;
620 uint64_t offset;
621 };
622
623 /*
624 * Given a `triplet` struct pointer and pointer `p`, this
625 * function reads the triplet beginning at `p` into the struct.
626 * Note that this function assumes that there is enough memory
627 * left for filling the `triplet` struct from `p`.
628 */
629 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
630 const unsigned char *p)
631 {
632 if (!triplet)
633 return -1;
634
635 triplet->commit_pos = get_be32(p);
636 p += sizeof(uint32_t);
637 triplet->offset = get_be64(p);
638 p += sizeof(uint64_t);
639 triplet->xor_row = get_be32(p);
640 return 0;
641 }
642
643 /*
644 * This function gets the raw triplet from `row`'th row in the
645 * lookup table and fills that data to the `triplet`.
646 */
647 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
648 uint32_t pos,
649 struct bitmap_lookup_table_triplet *triplet)
650 {
651 unsigned char *p = NULL;
652 if (pos >= bitmap_git->entry_count)
653 return error(_("corrupt bitmap lookup table: triplet position out of index"));
654
655 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
656
657 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
658 }
659
660 /*
661 * Searches for a matching triplet. `commit_pos` is a pointer
662 * to the wanted commit position value. `table_entry` points to
663 * a triplet in lookup table. The first 4 bytes of each
664 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
665 */
666 static int triplet_cmp(const void *commit_pos, const void *table_entry)
667 {
668
669 uint32_t a = *(uint32_t *)commit_pos;
670 uint32_t b = get_be32(table_entry);
671 if (a > b)
672 return 1;
673 else if (a < b)
674 return -1;
675
676 return 0;
677 }
678
679 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
680 struct object_id *oid,
681 uint32_t *result)
682 {
683 int found;
684
685 if (bitmap_is_midx(bitmap_git))
686 found = bsearch_midx(oid, bitmap_git->midx, result);
687 else
688 found = bsearch_pack(oid, bitmap_git->pack, result);
689
690 return found;
691 }
692
693 /*
694 * `bsearch_triplet_by_pos` function searches for the raw triplet
695 * having commit position same as `commit_pos` and fills `triplet`
696 * object from the raw triplet. Returns 1 on success and 0 on
697 * failure.
698 */
699 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
700 struct bitmap_index *bitmap_git,
701 struct bitmap_lookup_table_triplet *triplet)
702 {
703 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
704 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
705
706 if (!p)
707 return -1;
708
709 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
710 }
711
712 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
713 struct commit *commit)
714 {
715 uint32_t commit_pos, xor_row;
716 uint64_t offset;
717 int flags;
718 struct bitmap_lookup_table_triplet triplet;
719 struct object_id *oid = &commit->object.oid;
720 struct ewah_bitmap *bitmap;
721 struct stored_bitmap *xor_bitmap = NULL;
722 const int bitmap_header_size = 6;
723 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
724 static size_t xor_items_nr = 0, xor_items_alloc = 0;
725 static int is_corrupt = 0;
726 int xor_flags;
727 khiter_t hash_pos;
728 struct bitmap_lookup_table_xor_item *xor_item;
729
730 if (is_corrupt)
731 return NULL;
732
733 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
734 return NULL;
735
736 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
737 return NULL;
738
739 xor_items_nr = 0;
740 offset = triplet.offset;
741 xor_row = triplet.xor_row;
742
743 while (xor_row != 0xffffffff) {
744 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
745
746 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
747 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
748 goto corrupt;
749 }
750
751 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
752 goto corrupt;
753
754 xor_item = &xor_items[xor_items_nr];
755 xor_item->offset = triplet.offset;
756
757 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
758 error(_("corrupt bitmap lookup table: commit index %u out of range"),
759 triplet.commit_pos);
760 goto corrupt;
761 }
762
763 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
764
765 /*
766 * If desired bitmap is already stored, we don't need
767 * to iterate further. Because we know that bitmaps
768 * that are needed to be parsed to parse this bitmap
769 * has already been stored. So, assign this stored bitmap
770 * to the xor_bitmap.
771 */
772 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
773 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
774 break;
775 xor_items_nr++;
776 xor_row = triplet.xor_row;
777 }
778
779 while (xor_items_nr) {
780 xor_item = &xor_items[xor_items_nr - 1];
781 bitmap_git->map_pos = xor_item->offset;
782 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
783 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
784 oid_to_hex(&xor_item->oid));
785 goto corrupt;
786 }
787
788 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
789 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
790 bitmap = read_bitmap_1(bitmap_git);
791
792 if (!bitmap)
793 goto corrupt;
794
795 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
796 xor_items_nr--;
797 }
798
799 bitmap_git->map_pos = offset;
800 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
801 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
802 oid_to_hex(oid));
803 goto corrupt;
804 }
805
806 /*
807 * Don't bother reading the commit's index position or its xor
808 * offset:
809 *
810 * - The commit's index position is irrelevant to us, since
811 * load_bitmap_entries_v1 only uses it to learn the object
812 * id which is used to compute the hashmap's key. We already
813 * have an object id, so no need to look it up again.
814 *
815 * - The xor_offset is unusable for us, since it specifies how
816 * many entries previous to ours we should look at. This
817 * makes sense when reading the bitmaps sequentially (as in
818 * load_bitmap_entries_v1()), since we can keep track of
819 * each bitmap as we read them.
820 *
821 * But it can't work for us, since the bitmap's don't have a
822 * fixed size. So we learn the position of the xor'd bitmap
823 * from the commit table (and resolve it to a bitmap in the
824 * above if-statement).
825 *
826 * Instead, we can skip ahead and immediately read the flags and
827 * ewah bitmap.
828 */
829 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
830 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
831 bitmap = read_bitmap_1(bitmap_git);
832
833 if (!bitmap)
834 goto corrupt;
835
836 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
837
838 corrupt:
839 free(xor_items);
840 is_corrupt = 1;
841 return NULL;
842 }
843
844 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
845 struct commit *commit)
846 {
847 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
848 commit->object.oid);
849 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
850 struct stored_bitmap *bitmap = NULL;
851 if (!bitmap_git->table_lookup)
852 return NULL;
853
854 /* this is a fairly hot codepath - no trace2_region please */
855 /* NEEDSWORK: cache misses aren't recorded */
856 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
857 if (!bitmap)
858 return NULL;
859 return lookup_stored_bitmap(bitmap);
860 }
861 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
862 }
863
864 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
865 const struct object_id *oid)
866 {
867 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
868 khiter_t pos = kh_get_oid_pos(positions, *oid);
869
870 if (pos < kh_end(positions)) {
871 int bitmap_pos = kh_value(positions, pos);
872 return bitmap_pos + bitmap_num_objects(bitmap_git);
873 }
874
875 return -1;
876 }
877
878 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
879 const struct object_id *oid)
880 {
881 uint32_t pos;
882 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
883 if (!offset)
884 return -1;
885
886 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
887 return -1;
888 return pos;
889 }
890
891 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
892 const struct object_id *oid)
893 {
894 uint32_t want, got;
895 if (!bsearch_midx(oid, bitmap_git->midx, &want))
896 return -1;
897
898 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
899 return -1;
900 return got;
901 }
902
903 static int bitmap_position(struct bitmap_index *bitmap_git,
904 const struct object_id *oid)
905 {
906 int pos;
907 if (bitmap_is_midx(bitmap_git))
908 pos = bitmap_position_midx(bitmap_git, oid);
909 else
910 pos = bitmap_position_packfile(bitmap_git, oid);
911 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
912 }
913
914 static int ext_index_add_object(struct bitmap_index *bitmap_git,
915 struct object *object, const char *name)
916 {
917 struct eindex *eindex = &bitmap_git->ext_index;
918
919 khiter_t hash_pos;
920 int hash_ret;
921 int bitmap_pos;
922
923 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
924 if (hash_ret > 0) {
925 if (eindex->count >= eindex->alloc) {
926 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
927 REALLOC_ARRAY(eindex->objects, eindex->alloc);
928 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
929 }
930
931 bitmap_pos = eindex->count;
932 eindex->objects[eindex->count] = object;
933 eindex->hashes[eindex->count] = pack_name_hash(name);
934 kh_value(eindex->positions, hash_pos) = bitmap_pos;
935 eindex->count++;
936 } else {
937 bitmap_pos = kh_value(eindex->positions, hash_pos);
938 }
939
940 return bitmap_pos + bitmap_num_objects(bitmap_git);
941 }
942
943 struct bitmap_show_data {
944 struct bitmap_index *bitmap_git;
945 struct bitmap *base;
946 };
947
948 static void show_object(struct object *object, const char *name, void *data_)
949 {
950 struct bitmap_show_data *data = data_;
951 int bitmap_pos;
952
953 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
954
955 if (bitmap_pos < 0)
956 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
957 name);
958
959 bitmap_set(data->base, bitmap_pos);
960 }
961
962 static void show_commit(struct commit *commit UNUSED,
963 void *data UNUSED)
964 {
965 }
966
967 static int add_to_include_set(struct bitmap_index *bitmap_git,
968 struct include_data *data,
969 struct commit *commit,
970 int bitmap_pos)
971 {
972 struct ewah_bitmap *partial;
973
974 if (data->seen && bitmap_get(data->seen, bitmap_pos))
975 return 0;
976
977 if (bitmap_get(data->base, bitmap_pos))
978 return 0;
979
980 partial = bitmap_for_commit(bitmap_git, commit);
981 if (partial) {
982 bitmap_or_ewah(data->base, partial);
983 return 0;
984 }
985
986 bitmap_set(data->base, bitmap_pos);
987 return 1;
988 }
989
990 static int should_include(struct commit *commit, void *_data)
991 {
992 struct include_data *data = _data;
993 int bitmap_pos;
994
995 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
996 if (bitmap_pos < 0)
997 bitmap_pos = ext_index_add_object(data->bitmap_git,
998 (struct object *)commit,
999 NULL);
1000
1001 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
1002 struct commit_list *parent = commit->parents;
1003
1004 while (parent) {
1005 parent->item->object.flags |= SEEN;
1006 parent = parent->next;
1007 }
1008
1009 return 0;
1010 }
1011
1012 return 1;
1013 }
1014
1015 static int should_include_obj(struct object *obj, void *_data)
1016 {
1017 struct include_data *data = _data;
1018 int bitmap_pos;
1019
1020 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1021 if (bitmap_pos < 0)
1022 return 1;
1023 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1024 bitmap_get(data->base, bitmap_pos)) {
1025 obj->flags |= SEEN;
1026 return 0;
1027 }
1028 return 1;
1029 }
1030
1031 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1032 struct bitmap **base,
1033 struct commit *commit)
1034 {
1035 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1036
1037 if (!or_with)
1038 return 0;
1039
1040 if (!*base)
1041 *base = ewah_to_bitmap(or_with);
1042 else
1043 bitmap_or_ewah(*base, or_with);
1044
1045 return 1;
1046 }
1047
1048 static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1049 struct rev_info *revs,
1050 struct bitmap *base,
1051 struct bitmap *seen)
1052 {
1053 struct include_data incdata;
1054 struct bitmap_show_data show_data;
1055
1056 if (!base)
1057 base = bitmap_new();
1058
1059 incdata.bitmap_git = bitmap_git;
1060 incdata.base = base;
1061 incdata.seen = seen;
1062
1063 revs->include_check = should_include;
1064 revs->include_check_obj = should_include_obj;
1065 revs->include_check_data = &incdata;
1066
1067 if (prepare_revision_walk(revs))
1068 die(_("revision walk setup failed"));
1069
1070 show_data.bitmap_git = bitmap_git;
1071 show_data.base = base;
1072
1073 traverse_commit_list(revs, show_commit, show_object, &show_data);
1074
1075 revs->include_check = NULL;
1076 revs->include_check_obj = NULL;
1077 revs->include_check_data = NULL;
1078
1079 return base;
1080 }
1081
1082 struct bitmap_boundary_cb {
1083 struct bitmap_index *bitmap_git;
1084 struct bitmap *base;
1085
1086 struct object_array boundary;
1087 };
1088
1089 static void show_boundary_commit(struct commit *commit, void *_data)
1090 {
1091 struct bitmap_boundary_cb *data = _data;
1092
1093 if (commit->object.flags & BOUNDARY)
1094 add_object_array(&commit->object, "", &data->boundary);
1095
1096 if (commit->object.flags & UNINTERESTING) {
1097 if (bitmap_walk_contains(data->bitmap_git, data->base,
1098 &commit->object.oid))
1099 return;
1100
1101 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1102 }
1103 }
1104
1105 static void show_boundary_object(struct object *object,
1106 const char *name, void *data)
1107 {
1108 BUG("should not be called");
1109 }
1110
1111 static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1112 struct rev_info *revs,
1113 struct object_list *roots)
1114 {
1115 struct bitmap_boundary_cb cb;
1116 struct object_list *root;
1117 unsigned int i;
1118 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1119 int any_missing = 0;
1120
1121 cb.bitmap_git = bitmap_git;
1122 cb.base = bitmap_new();
1123 object_array_init(&cb.boundary);
1124
1125 revs->ignore_missing_links = 1;
1126
1127 /*
1128 * OR in any existing reachability bitmaps among `roots` into
1129 * `cb.base`.
1130 */
1131 for (root = roots; root; root = root->next) {
1132 struct object *object = root->item;
1133 if (object->type != OBJ_COMMIT ||
1134 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1135 continue;
1136
1137 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1138 (struct commit *)object))
1139 continue;
1140
1141 any_missing = 1;
1142 }
1143
1144 if (!any_missing)
1145 goto cleanup;
1146
1147 tmp_blobs = revs->blob_objects;
1148 tmp_trees = revs->tree_objects;
1149 tmp_tags = revs->blob_objects;
1150 revs->blob_objects = 0;
1151 revs->tree_objects = 0;
1152 revs->tag_objects = 0;
1153
1154 /*
1155 * We didn't have complete coverage of the roots. First setup a
1156 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1157 * between the tips and boundary, and (b) record the boundary.
1158 */
1159 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1160 if (prepare_revision_walk(revs))
1161 die("revision walk setup failed");
1162 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1163
1164 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1165 revs->boundary = 1;
1166 traverse_commit_list_filtered(revs,
1167 show_boundary_commit,
1168 show_boundary_object,
1169 &cb, NULL);
1170 revs->boundary = 0;
1171 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1172
1173 revs->blob_objects = tmp_blobs;
1174 revs->tree_objects = tmp_trees;
1175 revs->tag_objects = tmp_tags;
1176
1177 reset_revision_walk();
1178 clear_object_flags(UNINTERESTING);
1179
1180 /*
1181 * Then add the boundary commit(s) as fill-in traversal tips.
1182 */
1183 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1184 for (i = 0; i < cb.boundary.nr; i++) {
1185 struct object *obj = cb.boundary.objects[i].item;
1186 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1187 obj->flags |= SEEN;
1188 else
1189 add_pending_object(revs, obj, "");
1190 }
1191 if (revs->pending.nr)
1192 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1193 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1194
1195 cleanup:
1196 object_array_clear(&cb.boundary);
1197 revs->ignore_missing_links = 0;
1198
1199 return cb.base;
1200 }
1201
1202 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1203 struct rev_info *revs,
1204 struct object_list *roots,
1205 struct bitmap *seen)
1206 {
1207 struct bitmap *base = NULL;
1208 int needs_walk = 0;
1209
1210 struct object_list *not_mapped = NULL;
1211
1212 /*
1213 * Go through all the roots for the walk. The ones that have bitmaps
1214 * on the bitmap index will be `or`ed together to form an initial
1215 * global reachability analysis.
1216 *
1217 * The ones without bitmaps in the index will be stored in the
1218 * `not_mapped_list` for further processing.
1219 */
1220 while (roots) {
1221 struct object *object = roots->item;
1222 roots = roots->next;
1223
1224 if (object->type == OBJ_COMMIT &&
1225 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1226 object->flags |= SEEN;
1227 continue;
1228 }
1229
1230 object_list_insert(object, &not_mapped);
1231 }
1232
1233 /*
1234 * Best case scenario: We found bitmaps for all the roots,
1235 * so the resulting `or` bitmap has the full reachability analysis
1236 */
1237 if (!not_mapped)
1238 return base;
1239
1240 roots = not_mapped;
1241
1242 /*
1243 * Let's iterate through all the roots that don't have bitmaps to
1244 * check if we can determine them to be reachable from the existing
1245 * global bitmap.
1246 *
1247 * If we cannot find them in the existing global bitmap, we'll need
1248 * to push them to an actual walk and run it until we can confirm
1249 * they are reachable
1250 */
1251 while (roots) {
1252 struct object *object = roots->item;
1253 int pos;
1254
1255 roots = roots->next;
1256 pos = bitmap_position(bitmap_git, &object->oid);
1257
1258 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1259 object->flags &= ~UNINTERESTING;
1260 add_pending_object(revs, object, "");
1261 needs_walk = 1;
1262 } else {
1263 object->flags |= SEEN;
1264 }
1265 }
1266
1267 if (needs_walk) {
1268 /*
1269 * This fill-in traversal may walk over some objects
1270 * again, since we have already traversed in order to
1271 * find the boundary.
1272 *
1273 * But this extra walk should be extremely cheap, since
1274 * all commit objects are loaded into memory, and
1275 * because we skip walking to parents that are
1276 * UNINTERESTING, since it will be marked in the haves
1277 * bitmap already (or it has an on-disk bitmap, since
1278 * OR-ing it in covers all of its ancestors).
1279 */
1280 base = fill_in_bitmap(bitmap_git, revs, base, seen);
1281 }
1282
1283 return base;
1284 }
1285
1286 static void show_extended_objects(struct bitmap_index *bitmap_git,
1287 struct rev_info *revs,
1288 show_reachable_fn show_reach)
1289 {
1290 struct bitmap *objects = bitmap_git->result;
1291 struct eindex *eindex = &bitmap_git->ext_index;
1292 uint32_t i;
1293
1294 for (i = 0; i < eindex->count; ++i) {
1295 struct object *obj;
1296
1297 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1298 continue;
1299
1300 obj = eindex->objects[i];
1301 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1302 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1303 (obj->type == OBJ_TAG && !revs->tag_objects))
1304 continue;
1305
1306 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1307 }
1308 }
1309
1310 static void init_type_iterator(struct ewah_iterator *it,
1311 struct bitmap_index *bitmap_git,
1312 enum object_type type)
1313 {
1314 switch (type) {
1315 case OBJ_COMMIT:
1316 ewah_iterator_init(it, bitmap_git->commits);
1317 break;
1318
1319 case OBJ_TREE:
1320 ewah_iterator_init(it, bitmap_git->trees);
1321 break;
1322
1323 case OBJ_BLOB:
1324 ewah_iterator_init(it, bitmap_git->blobs);
1325 break;
1326
1327 case OBJ_TAG:
1328 ewah_iterator_init(it, bitmap_git->tags);
1329 break;
1330
1331 default:
1332 BUG("object type %d not stored by bitmap type index", type);
1333 break;
1334 }
1335 }
1336
1337 static void show_objects_for_type(
1338 struct bitmap_index *bitmap_git,
1339 enum object_type object_type,
1340 show_reachable_fn show_reach)
1341 {
1342 size_t i = 0;
1343 uint32_t offset;
1344
1345 struct ewah_iterator it;
1346 eword_t filter;
1347
1348 struct bitmap *objects = bitmap_git->result;
1349
1350 init_type_iterator(&it, bitmap_git, object_type);
1351
1352 for (i = 0; i < objects->word_alloc &&
1353 ewah_iterator_next(&filter, &it); i++) {
1354 eword_t word = objects->words[i] & filter;
1355 size_t pos = (i * BITS_IN_EWORD);
1356
1357 if (!word)
1358 continue;
1359
1360 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1361 struct packed_git *pack;
1362 struct object_id oid;
1363 uint32_t hash = 0, index_pos;
1364 off_t ofs;
1365
1366 if ((word >> offset) == 0)
1367 break;
1368
1369 offset += ewah_bit_ctz64(word >> offset);
1370
1371 if (bitmap_is_midx(bitmap_git)) {
1372 struct multi_pack_index *m = bitmap_git->midx;
1373 uint32_t pack_id;
1374
1375 index_pos = pack_pos_to_midx(m, pos + offset);
1376 ofs = nth_midxed_offset(m, index_pos);
1377 nth_midxed_object_oid(&oid, m, index_pos);
1378
1379 pack_id = nth_midxed_pack_int_id(m, index_pos);
1380 pack = bitmap_git->midx->packs[pack_id];
1381 } else {
1382 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1383 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1384 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1385
1386 pack = bitmap_git->pack;
1387 }
1388
1389 if (bitmap_git->hashes)
1390 hash = get_be32(bitmap_git->hashes + index_pos);
1391
1392 show_reach(&oid, object_type, 0, hash, pack, ofs);
1393 }
1394 }
1395 }
1396
1397 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1398 struct object_list *roots)
1399 {
1400 while (roots) {
1401 struct object *object = roots->item;
1402 roots = roots->next;
1403
1404 if (bitmap_is_midx(bitmap_git)) {
1405 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1406 return 1;
1407 } else {
1408 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1409 return 1;
1410 }
1411 }
1412
1413 return 0;
1414 }
1415
1416 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1417 struct object_list *tip_objects,
1418 enum object_type type)
1419 {
1420 struct bitmap *result = bitmap_new();
1421 struct object_list *p;
1422
1423 for (p = tip_objects; p; p = p->next) {
1424 int pos;
1425
1426 if (p->item->type != type)
1427 continue;
1428
1429 pos = bitmap_position(bitmap_git, &p->item->oid);
1430 if (pos < 0)
1431 continue;
1432
1433 bitmap_set(result, pos);
1434 }
1435
1436 return result;
1437 }
1438
1439 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1440 struct object_list *tip_objects,
1441 struct bitmap *to_filter,
1442 enum object_type type)
1443 {
1444 struct eindex *eindex = &bitmap_git->ext_index;
1445 struct bitmap *tips;
1446 struct ewah_iterator it;
1447 eword_t mask;
1448 uint32_t i;
1449
1450 /*
1451 * The non-bitmap version of this filter never removes
1452 * objects which the other side specifically asked for,
1453 * so we must match that behavior.
1454 */
1455 tips = find_tip_objects(bitmap_git, tip_objects, type);
1456
1457 /*
1458 * We can use the type-level bitmap for 'type' to work in whole
1459 * words for the objects that are actually in the bitmapped
1460 * packfile.
1461 */
1462 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1463 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1464 i++) {
1465 if (i < tips->word_alloc)
1466 mask &= ~tips->words[i];
1467 to_filter->words[i] &= ~mask;
1468 }
1469
1470 /*
1471 * Clear any objects that weren't in the packfile (and so would
1472 * not have been caught by the loop above. We'll have to check
1473 * them individually.
1474 */
1475 for (i = 0; i < eindex->count; i++) {
1476 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1477 if (eindex->objects[i]->type == type &&
1478 bitmap_get(to_filter, pos) &&
1479 !bitmap_get(tips, pos))
1480 bitmap_unset(to_filter, pos);
1481 }
1482
1483 bitmap_free(tips);
1484 }
1485
1486 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1487 struct object_list *tip_objects,
1488 struct bitmap *to_filter)
1489 {
1490 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1491 OBJ_BLOB);
1492 }
1493
1494 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1495 uint32_t pos)
1496 {
1497 unsigned long size;
1498 struct object_info oi = OBJECT_INFO_INIT;
1499
1500 oi.sizep = &size;
1501
1502 if (pos < bitmap_num_objects(bitmap_git)) {
1503 struct packed_git *pack;
1504 off_t ofs;
1505
1506 if (bitmap_is_midx(bitmap_git)) {
1507 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1508 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1509
1510 pack = bitmap_git->midx->packs[pack_id];
1511 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1512 } else {
1513 pack = bitmap_git->pack;
1514 ofs = pack_pos_to_offset(pack, pos);
1515 }
1516
1517 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1518 struct object_id oid;
1519 nth_bitmap_object_oid(bitmap_git, &oid,
1520 pack_pos_to_index(pack, pos));
1521 die(_("unable to get size of %s"), oid_to_hex(&oid));
1522 }
1523 } else {
1524 struct eindex *eindex = &bitmap_git->ext_index;
1525 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1526 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1527 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1528 }
1529
1530 return size;
1531 }
1532
1533 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1534 struct object_list *tip_objects,
1535 struct bitmap *to_filter,
1536 unsigned long limit)
1537 {
1538 struct eindex *eindex = &bitmap_git->ext_index;
1539 struct bitmap *tips;
1540 struct ewah_iterator it;
1541 eword_t mask;
1542 uint32_t i;
1543
1544 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1545
1546 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1547 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1548 i++) {
1549 eword_t word = to_filter->words[i] & mask;
1550 unsigned offset;
1551
1552 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1553 uint32_t pos;
1554
1555 if ((word >> offset) == 0)
1556 break;
1557 offset += ewah_bit_ctz64(word >> offset);
1558 pos = i * BITS_IN_EWORD + offset;
1559
1560 if (!bitmap_get(tips, pos) &&
1561 get_size_by_pos(bitmap_git, pos) >= limit)
1562 bitmap_unset(to_filter, pos);
1563 }
1564 }
1565
1566 for (i = 0; i < eindex->count; i++) {
1567 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1568 if (eindex->objects[i]->type == OBJ_BLOB &&
1569 bitmap_get(to_filter, pos) &&
1570 !bitmap_get(tips, pos) &&
1571 get_size_by_pos(bitmap_git, pos) >= limit)
1572 bitmap_unset(to_filter, pos);
1573 }
1574
1575 bitmap_free(tips);
1576 }
1577
1578 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1579 struct object_list *tip_objects,
1580 struct bitmap *to_filter,
1581 unsigned long limit)
1582 {
1583 if (limit)
1584 BUG("filter_bitmap_tree_depth given non-zero limit");
1585
1586 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1587 OBJ_TREE);
1588 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1589 OBJ_BLOB);
1590 }
1591
1592 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1593 struct object_list *tip_objects,
1594 struct bitmap *to_filter,
1595 enum object_type object_type)
1596 {
1597 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1598 BUG("filter_bitmap_object_type given invalid object");
1599
1600 if (object_type != OBJ_TAG)
1601 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1602 if (object_type != OBJ_COMMIT)
1603 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1604 if (object_type != OBJ_TREE)
1605 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1606 if (object_type != OBJ_BLOB)
1607 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1608 }
1609
1610 static int filter_bitmap(struct bitmap_index *bitmap_git,
1611 struct object_list *tip_objects,
1612 struct bitmap *to_filter,
1613 struct list_objects_filter_options *filter)
1614 {
1615 if (!filter || filter->choice == LOFC_DISABLED)
1616 return 0;
1617
1618 if (filter->choice == LOFC_BLOB_NONE) {
1619 if (bitmap_git)
1620 filter_bitmap_blob_none(bitmap_git, tip_objects,
1621 to_filter);
1622 return 0;
1623 }
1624
1625 if (filter->choice == LOFC_BLOB_LIMIT) {
1626 if (bitmap_git)
1627 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1628 to_filter,
1629 filter->blob_limit_value);
1630 return 0;
1631 }
1632
1633 if (filter->choice == LOFC_TREE_DEPTH &&
1634 filter->tree_exclude_depth == 0) {
1635 if (bitmap_git)
1636 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1637 to_filter,
1638 filter->tree_exclude_depth);
1639 return 0;
1640 }
1641
1642 if (filter->choice == LOFC_OBJECT_TYPE) {
1643 if (bitmap_git)
1644 filter_bitmap_object_type(bitmap_git, tip_objects,
1645 to_filter,
1646 filter->object_type);
1647 return 0;
1648 }
1649
1650 if (filter->choice == LOFC_COMBINE) {
1651 int i;
1652 for (i = 0; i < filter->sub_nr; i++) {
1653 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1654 &filter->sub[i]) < 0)
1655 return -1;
1656 }
1657 return 0;
1658 }
1659
1660 /* filter choice not handled */
1661 return -1;
1662 }
1663
1664 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1665 {
1666 return !filter_bitmap(NULL, NULL, NULL, filter);
1667 }
1668
1669 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1670 int filter_provided_objects)
1671 {
1672 unsigned int i;
1673 int use_boundary_traversal;
1674
1675 struct object_list *wants = NULL;
1676 struct object_list *haves = NULL;
1677
1678 struct bitmap *wants_bitmap = NULL;
1679 struct bitmap *haves_bitmap = NULL;
1680
1681 struct bitmap_index *bitmap_git;
1682
1683 /*
1684 * We can't do pathspec limiting with bitmaps, because we don't know
1685 * which commits are associated with which object changes (let alone
1686 * even which objects are associated with which paths).
1687 */
1688 if (revs->prune)
1689 return NULL;
1690
1691 if (!can_filter_bitmap(&revs->filter))
1692 return NULL;
1693
1694 /* try to open a bitmapped pack, but don't parse it yet
1695 * because we may not need to use it */
1696 CALLOC_ARRAY(bitmap_git, 1);
1697 if (open_bitmap(revs->repo, bitmap_git) < 0)
1698 goto cleanup;
1699
1700 for (i = 0; i < revs->pending.nr; ++i) {
1701 struct object *object = revs->pending.objects[i].item;
1702
1703 if (object->type == OBJ_NONE)
1704 parse_object_or_die(&object->oid, NULL);
1705
1706 while (object->type == OBJ_TAG) {
1707 struct tag *tag = (struct tag *) object;
1708
1709 if (object->flags & UNINTERESTING)
1710 object_list_insert(object, &haves);
1711 else
1712 object_list_insert(object, &wants);
1713
1714 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1715 object->flags |= (tag->object.flags & UNINTERESTING);
1716 }
1717
1718 if (object->flags & UNINTERESTING)
1719 object_list_insert(object, &haves);
1720 else
1721 object_list_insert(object, &wants);
1722 }
1723
1724 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1725 if (use_boundary_traversal < 0) {
1726 prepare_repo_settings(revs->repo);
1727 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1728 }
1729
1730 if (!use_boundary_traversal) {
1731 /*
1732 * if we have a HAVES list, but none of those haves is contained
1733 * in the packfile that has a bitmap, we don't have anything to
1734 * optimize here
1735 */
1736 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1737 goto cleanup;
1738 }
1739
1740 /* if we don't want anything, we're done here */
1741 if (!wants)
1742 goto cleanup;
1743
1744 /*
1745 * now we're going to use bitmaps, so load the actual bitmap entries
1746 * from disk. this is the point of no return; after this the rev_list
1747 * becomes invalidated and we must perform the revwalk through bitmaps
1748 */
1749 if (load_bitmap(revs->repo, bitmap_git) < 0)
1750 goto cleanup;
1751
1752 if (!use_boundary_traversal)
1753 object_array_clear(&revs->pending);
1754
1755 if (haves) {
1756 if (use_boundary_traversal) {
1757 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1758 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1759 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1760 } else {
1761 trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1762 revs->ignore_missing_links = 1;
1763 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1764 reset_revision_walk();
1765 revs->ignore_missing_links = 0;
1766 trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1767 }
1768
1769 if (!haves_bitmap)
1770 BUG("failed to perform bitmap walk");
1771 }
1772
1773 if (use_boundary_traversal) {
1774 object_array_clear(&revs->pending);
1775 reset_revision_walk();
1776 }
1777
1778 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1779
1780 if (!wants_bitmap)
1781 BUG("failed to perform bitmap walk");
1782
1783 if (haves_bitmap)
1784 bitmap_and_not(wants_bitmap, haves_bitmap);
1785
1786 filter_bitmap(bitmap_git,
1787 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1788 wants_bitmap,
1789 &revs->filter);
1790
1791 bitmap_git->result = wants_bitmap;
1792 bitmap_git->haves = haves_bitmap;
1793
1794 object_list_free(&wants);
1795 object_list_free(&haves);
1796
1797 return bitmap_git;
1798
1799 cleanup:
1800 free_bitmap_index(bitmap_git);
1801 object_list_free(&wants);
1802 object_list_free(&haves);
1803 return NULL;
1804 }
1805
1806 /*
1807 * -1 means "stop trying further objects"; 0 means we may or may not have
1808 * reused, but you can keep feeding bits.
1809 */
1810 static int try_partial_reuse(struct packed_git *pack,
1811 size_t pos,
1812 struct bitmap *reuse,
1813 struct pack_window **w_curs)
1814 {
1815 off_t offset, delta_obj_offset;
1816 enum object_type type;
1817 unsigned long size;
1818
1819 /*
1820 * try_partial_reuse() is called either on (a) objects in the
1821 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1822 * objects in the preferred pack of a multi-pack bitmap.
1823 * Importantly, the latter can pretend as if only a single pack
1824 * exists because:
1825 *
1826 * - The first pack->num_objects bits of a MIDX bitmap are
1827 * reserved for the preferred pack, and
1828 *
1829 * - Ties due to duplicate objects are always resolved in
1830 * favor of the preferred pack.
1831 *
1832 * Therefore we do not need to ever ask the MIDX for its copy of
1833 * an object by OID, since it will always select it from the
1834 * preferred pack. Likewise, the selected copy of the base
1835 * object for any deltas will reside in the same pack.
1836 *
1837 * This means that we can reuse pos when looking up the bit in
1838 * the reuse bitmap, too, since bits corresponding to the
1839 * preferred pack precede all bits from other packs.
1840 */
1841
1842 if (pos >= pack->num_objects)
1843 return -1; /* not actually in the pack or MIDX preferred pack */
1844
1845 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1846 type = unpack_object_header(pack, w_curs, &offset, &size);
1847 if (type < 0)
1848 return -1; /* broken packfile, punt */
1849
1850 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1851 off_t base_offset;
1852 uint32_t base_pos;
1853
1854 /*
1855 * Find the position of the base object so we can look it up
1856 * in our bitmaps. If we can't come up with an offset, or if
1857 * that offset is not in the revidx, the pack is corrupt.
1858 * There's nothing we can do, so just punt on this object,
1859 * and the normal slow path will complain about it in
1860 * more detail.
1861 */
1862 base_offset = get_delta_base(pack, w_curs, &offset, type,
1863 delta_obj_offset);
1864 if (!base_offset)
1865 return 0;
1866 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1867 return 0;
1868
1869 /*
1870 * We assume delta dependencies always point backwards. This
1871 * lets us do a single pass, and is basically always true
1872 * due to the way OFS_DELTAs work. You would not typically
1873 * find REF_DELTA in a bitmapped pack, since we only bitmap
1874 * packs we write fresh, and OFS_DELTA is the default). But
1875 * let's double check to make sure the pack wasn't written with
1876 * odd parameters.
1877 */
1878 if (base_pos >= pos)
1879 return 0;
1880
1881 /*
1882 * And finally, if we're not sending the base as part of our
1883 * reuse chunk, then don't send this object either. The base
1884 * would come after us, along with other objects not
1885 * necessarily in the pack, which means we'd need to convert
1886 * to REF_DELTA on the fly. Better to just let the normal
1887 * object_entry code path handle it.
1888 */
1889 if (!bitmap_get(reuse, base_pos))
1890 return 0;
1891 }
1892
1893 /*
1894 * If we got here, then the object is OK to reuse. Mark it.
1895 */
1896 bitmap_set(reuse, pos);
1897 return 0;
1898 }
1899
1900 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1901 {
1902 struct multi_pack_index *m = bitmap_git->midx;
1903 if (!m)
1904 BUG("midx_preferred_pack: requires non-empty MIDX");
1905 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1906 }
1907
1908 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1909 struct packed_git **packfile_out,
1910 uint32_t *entries,
1911 struct bitmap **reuse_out)
1912 {
1913 struct repository *r = the_repository;
1914 struct packed_git *pack;
1915 struct bitmap *result = bitmap_git->result;
1916 struct bitmap *reuse;
1917 struct pack_window *w_curs = NULL;
1918 size_t i = 0;
1919 uint32_t offset;
1920 uint32_t objects_nr;
1921
1922 assert(result);
1923
1924 load_reverse_index(r, bitmap_git);
1925
1926 if (bitmap_is_midx(bitmap_git))
1927 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1928 else
1929 pack = bitmap_git->pack;
1930 objects_nr = pack->num_objects;
1931
1932 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1933 i++;
1934
1935 /*
1936 * Don't mark objects not in the packfile or preferred pack. This bitmap
1937 * marks objects eligible for reuse, but the pack-reuse code only
1938 * understands how to reuse a single pack. Since the preferred pack is
1939 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1940 * we use it instead of another pack. In single-pack bitmaps, the choice
1941 * is made for us.
1942 */
1943 if (i > objects_nr / BITS_IN_EWORD)
1944 i = objects_nr / BITS_IN_EWORD;
1945
1946 reuse = bitmap_word_alloc(i);
1947 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1948
1949 for (; i < result->word_alloc; ++i) {
1950 eword_t word = result->words[i];
1951 size_t pos = (i * BITS_IN_EWORD);
1952
1953 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1954 if ((word >> offset) == 0)
1955 break;
1956
1957 offset += ewah_bit_ctz64(word >> offset);
1958 if (try_partial_reuse(pack, pos + offset,
1959 reuse, &w_curs) < 0) {
1960 /*
1961 * try_partial_reuse indicated we couldn't reuse
1962 * any bits, so there is no point in trying more
1963 * bits in the current word, or any other words
1964 * in result.
1965 *
1966 * Jump out of both loops to avoid future
1967 * unnecessary calls to try_partial_reuse.
1968 */
1969 goto done;
1970 }
1971 }
1972 }
1973
1974 done:
1975 unuse_pack(&w_curs);
1976
1977 *entries = bitmap_popcount(reuse);
1978 if (!*entries) {
1979 bitmap_free(reuse);
1980 return -1;
1981 }
1982
1983 /*
1984 * Drop any reused objects from the result, since they will not
1985 * need to be handled separately.
1986 */
1987 bitmap_and_not(result, reuse);
1988 *packfile_out = pack;
1989 *reuse_out = reuse;
1990 return 0;
1991 }
1992
1993 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1994 struct bitmap *bitmap, const struct object_id *oid)
1995 {
1996 int idx;
1997
1998 if (!bitmap)
1999 return 0;
2000
2001 idx = bitmap_position(bitmap_git, oid);
2002 return idx >= 0 && bitmap_get(bitmap, idx);
2003 }
2004
2005 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2006 struct rev_info *revs,
2007 show_reachable_fn show_reachable)
2008 {
2009 assert(bitmap_git->result);
2010
2011 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2012 if (revs->tree_objects)
2013 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2014 if (revs->blob_objects)
2015 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2016 if (revs->tag_objects)
2017 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2018
2019 show_extended_objects(bitmap_git, revs, show_reachable);
2020 }
2021
2022 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2023 enum object_type type)
2024 {
2025 struct bitmap *objects = bitmap_git->result;
2026 struct eindex *eindex = &bitmap_git->ext_index;
2027
2028 uint32_t i = 0, count = 0;
2029 struct ewah_iterator it;
2030 eword_t filter;
2031
2032 init_type_iterator(&it, bitmap_git, type);
2033
2034 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2035 eword_t word = objects->words[i++] & filter;
2036 count += ewah_bit_popcount64(word);
2037 }
2038
2039 for (i = 0; i < eindex->count; ++i) {
2040 if (eindex->objects[i]->type == type &&
2041 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
2042 count++;
2043 }
2044
2045 return count;
2046 }
2047
2048 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2049 uint32_t *commits, uint32_t *trees,
2050 uint32_t *blobs, uint32_t *tags)
2051 {
2052 assert(bitmap_git->result);
2053
2054 if (commits)
2055 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2056
2057 if (trees)
2058 *trees = count_object_type(bitmap_git, OBJ_TREE);
2059
2060 if (blobs)
2061 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2062
2063 if (tags)
2064 *tags = count_object_type(bitmap_git, OBJ_TAG);
2065 }
2066
2067 struct bitmap_test_data {
2068 struct bitmap_index *bitmap_git;
2069 struct bitmap *base;
2070 struct bitmap *commits;
2071 struct bitmap *trees;
2072 struct bitmap *blobs;
2073 struct bitmap *tags;
2074 struct progress *prg;
2075 size_t seen;
2076 };
2077
2078 static void test_bitmap_type(struct bitmap_test_data *tdata,
2079 struct object *obj, int pos)
2080 {
2081 enum object_type bitmap_type = OBJ_NONE;
2082 int bitmaps_nr = 0;
2083
2084 if (bitmap_get(tdata->commits, pos)) {
2085 bitmap_type = OBJ_COMMIT;
2086 bitmaps_nr++;
2087 }
2088 if (bitmap_get(tdata->trees, pos)) {
2089 bitmap_type = OBJ_TREE;
2090 bitmaps_nr++;
2091 }
2092 if (bitmap_get(tdata->blobs, pos)) {
2093 bitmap_type = OBJ_BLOB;
2094 bitmaps_nr++;
2095 }
2096 if (bitmap_get(tdata->tags, pos)) {
2097 bitmap_type = OBJ_TAG;
2098 bitmaps_nr++;
2099 }
2100
2101 if (bitmap_type == OBJ_NONE)
2102 die(_("object '%s' not found in type bitmaps"),
2103 oid_to_hex(&obj->oid));
2104
2105 if (bitmaps_nr > 1)
2106 die(_("object '%s' does not have a unique type"),
2107 oid_to_hex(&obj->oid));
2108
2109 if (bitmap_type != obj->type)
2110 die(_("object '%s': real type '%s', expected: '%s'"),
2111 oid_to_hex(&obj->oid),
2112 type_name(obj->type),
2113 type_name(bitmap_type));
2114 }
2115
2116 static void test_show_object(struct object *object,
2117 const char *name UNUSED,
2118 void *data)
2119 {
2120 struct bitmap_test_data *tdata = data;
2121 int bitmap_pos;
2122
2123 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2124 if (bitmap_pos < 0)
2125 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2126 test_bitmap_type(tdata, object, bitmap_pos);
2127
2128 bitmap_set(tdata->base, bitmap_pos);
2129 display_progress(tdata->prg, ++tdata->seen);
2130 }
2131
2132 static void test_show_commit(struct commit *commit, void *data)
2133 {
2134 struct bitmap_test_data *tdata = data;
2135 int bitmap_pos;
2136
2137 bitmap_pos = bitmap_position(tdata->bitmap_git,
2138 &commit->object.oid);
2139 if (bitmap_pos < 0)
2140 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2141 test_bitmap_type(tdata, &commit->object, bitmap_pos);
2142
2143 bitmap_set(tdata->base, bitmap_pos);
2144 display_progress(tdata->prg, ++tdata->seen);
2145 }
2146
2147 void test_bitmap_walk(struct rev_info *revs)
2148 {
2149 struct object *root;
2150 struct bitmap *result = NULL;
2151 size_t result_popcnt;
2152 struct bitmap_test_data tdata;
2153 struct bitmap_index *bitmap_git;
2154 struct ewah_bitmap *bm;
2155
2156 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2157 die(_("failed to load bitmap indexes"));
2158
2159 if (revs->pending.nr != 1)
2160 die(_("you must specify exactly one commit to test"));
2161
2162 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2163 bitmap_git->version,
2164 bitmap_git->entry_count,
2165 bitmap_git->table_lookup ? "" : " loaded");
2166
2167 root = revs->pending.objects[0].item;
2168 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2169
2170 if (bm) {
2171 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2172 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2173
2174 result = ewah_to_bitmap(bm);
2175 }
2176
2177 if (!result)
2178 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2179
2180 revs->tag_objects = 1;
2181 revs->tree_objects = 1;
2182 revs->blob_objects = 1;
2183
2184 result_popcnt = bitmap_popcount(result);
2185
2186 if (prepare_revision_walk(revs))
2187 die(_("revision walk setup failed"));
2188
2189 tdata.bitmap_git = bitmap_git;
2190 tdata.base = bitmap_new();
2191 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2192 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2193 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2194 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2195 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2196 tdata.seen = 0;
2197
2198 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2199
2200 stop_progress(&tdata.prg);
2201
2202 if (bitmap_equals(result, tdata.base))
2203 fprintf_ln(stderr, "OK!");
2204 else
2205 die(_("mismatch in bitmap results"));
2206
2207 bitmap_free(result);
2208 bitmap_free(tdata.base);
2209 bitmap_free(tdata.commits);
2210 bitmap_free(tdata.trees);
2211 bitmap_free(tdata.blobs);
2212 bitmap_free(tdata.tags);
2213 free_bitmap_index(bitmap_git);
2214 }
2215
2216 int test_bitmap_commits(struct repository *r)
2217 {
2218 struct object_id oid;
2219 MAYBE_UNUSED void *value;
2220 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2221
2222 if (!bitmap_git)
2223 die(_("failed to load bitmap indexes"));
2224
2225 /*
2226 * As this function is only used to print bitmap selected
2227 * commits, we don't have to read the commit table.
2228 */
2229 if (bitmap_git->table_lookup) {
2230 if (load_bitmap_entries_v1(bitmap_git) < 0)
2231 die(_("failed to load bitmap indexes"));
2232 }
2233
2234 kh_foreach(bitmap_git->bitmaps, oid, value, {
2235 printf_ln("%s", oid_to_hex(&oid));
2236 });
2237
2238 free_bitmap_index(bitmap_git);
2239
2240 return 0;
2241 }
2242
2243 int test_bitmap_hashes(struct repository *r)
2244 {
2245 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2246 struct object_id oid;
2247 uint32_t i, index_pos;
2248
2249 if (!bitmap_git || !bitmap_git->hashes)
2250 goto cleanup;
2251
2252 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2253 if (bitmap_is_midx(bitmap_git))
2254 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2255 else
2256 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2257
2258 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2259
2260 printf_ln("%s %"PRIu32"",
2261 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2262 }
2263
2264 cleanup:
2265 free_bitmap_index(bitmap_git);
2266
2267 return 0;
2268 }
2269
2270 int rebuild_bitmap(const uint32_t *reposition,
2271 struct ewah_bitmap *source,
2272 struct bitmap *dest)
2273 {
2274 uint32_t pos = 0;
2275 struct ewah_iterator it;
2276 eword_t word;
2277
2278 ewah_iterator_init(&it, source);
2279
2280 while (ewah_iterator_next(&word, &it)) {
2281 uint32_t offset, bit_pos;
2282
2283 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2284 if ((word >> offset) == 0)
2285 break;
2286
2287 offset += ewah_bit_ctz64(word >> offset);
2288
2289 bit_pos = reposition[pos + offset];
2290 if (bit_pos > 0)
2291 bitmap_set(dest, bit_pos - 1);
2292 else /* can't reuse, we don't have the object */
2293 return -1;
2294 }
2295
2296 pos += BITS_IN_EWORD;
2297 }
2298 return 0;
2299 }
2300
2301 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2302 struct packing_data *mapping)
2303 {
2304 struct repository *r = the_repository;
2305 uint32_t i, num_objects;
2306 uint32_t *reposition;
2307
2308 if (!bitmap_is_midx(bitmap_git))
2309 load_reverse_index(r, bitmap_git);
2310 else if (load_midx_revindex(bitmap_git->midx))
2311 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2312 "extension");
2313
2314 num_objects = bitmap_num_objects(bitmap_git);
2315 CALLOC_ARRAY(reposition, num_objects);
2316
2317 for (i = 0; i < num_objects; ++i) {
2318 struct object_id oid;
2319 struct object_entry *oe;
2320 uint32_t index_pos;
2321
2322 if (bitmap_is_midx(bitmap_git))
2323 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2324 else
2325 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2326 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2327 oe = packlist_find(mapping, &oid);
2328
2329 if (oe) {
2330 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2331 if (bitmap_git->hashes && !oe->hash)
2332 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2333 }
2334 }
2335
2336 return reposition;
2337 }
2338
2339 void free_bitmap_index(struct bitmap_index *b)
2340 {
2341 if (!b)
2342 return;
2343
2344 if (b->map)
2345 munmap(b->map, b->map_size);
2346 ewah_pool_free(b->commits);
2347 ewah_pool_free(b->trees);
2348 ewah_pool_free(b->blobs);
2349 ewah_pool_free(b->tags);
2350 if (b->bitmaps) {
2351 struct stored_bitmap *sb;
2352 kh_foreach_value(b->bitmaps, sb, {
2353 ewah_pool_free(sb->root);
2354 free(sb);
2355 });
2356 }
2357 kh_destroy_oid_map(b->bitmaps);
2358 free(b->ext_index.objects);
2359 free(b->ext_index.hashes);
2360 kh_destroy_oid_pos(b->ext_index.positions);
2361 bitmap_free(b->result);
2362 bitmap_free(b->haves);
2363 if (bitmap_is_midx(b)) {
2364 /*
2365 * Multi-pack bitmaps need to have resources associated with
2366 * their on-disk reverse indexes unmapped so that stale .rev and
2367 * .bitmap files can be removed.
2368 *
2369 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2370 * written in the same 'git multi-pack-index write --bitmap'
2371 * process. Close resources so they can be removed safely on
2372 * platforms like Windows.
2373 */
2374 close_midx_revindex(b->midx);
2375 }
2376 free(b);
2377 }
2378
2379 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2380 const struct object_id *oid)
2381 {
2382 return bitmap_git &&
2383 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2384 }
2385
2386 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2387 enum object_type object_type)
2388 {
2389 struct bitmap *result = bitmap_git->result;
2390 off_t total = 0;
2391 struct ewah_iterator it;
2392 eword_t filter;
2393 size_t i;
2394
2395 init_type_iterator(&it, bitmap_git, object_type);
2396 for (i = 0; i < result->word_alloc &&
2397 ewah_iterator_next(&filter, &it); i++) {
2398 eword_t word = result->words[i] & filter;
2399 size_t base = (i * BITS_IN_EWORD);
2400 unsigned offset;
2401
2402 if (!word)
2403 continue;
2404
2405 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2406 if ((word >> offset) == 0)
2407 break;
2408
2409 offset += ewah_bit_ctz64(word >> offset);
2410
2411 if (bitmap_is_midx(bitmap_git)) {
2412 uint32_t pack_pos;
2413 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2414 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2415
2416 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2417 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2418
2419 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2420 struct object_id oid;
2421 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2422
2423 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2424 oid_to_hex(&oid),
2425 pack->pack_name,
2426 (uintmax_t)offset);
2427 }
2428
2429 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2430 } else {
2431 size_t pos = base + offset;
2432 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2433 pack_pos_to_offset(bitmap_git->pack, pos);
2434 }
2435 }
2436 }
2437
2438 return total;
2439 }
2440
2441 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2442 {
2443 struct bitmap *result = bitmap_git->result;
2444 struct eindex *eindex = &bitmap_git->ext_index;
2445 off_t total = 0;
2446 struct object_info oi = OBJECT_INFO_INIT;
2447 off_t object_size;
2448 size_t i;
2449
2450 oi.disk_sizep = &object_size;
2451
2452 for (i = 0; i < eindex->count; i++) {
2453 struct object *obj = eindex->objects[i];
2454
2455 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2456 continue;
2457
2458 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2459 die(_("unable to get disk usage of '%s'"),
2460 oid_to_hex(&obj->oid));
2461
2462 total += object_size;
2463 }
2464 return total;
2465 }
2466
2467 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2468 struct rev_info *revs)
2469 {
2470 off_t total = 0;
2471
2472 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2473 if (revs->tree_objects)
2474 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2475 if (revs->blob_objects)
2476 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2477 if (revs->tag_objects)
2478 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2479
2480 total += get_disk_usage_for_extended(bitmap_git);
2481
2482 return total;
2483 }
2484
2485 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2486 {
2487 return !!bitmap_git->midx;
2488 }
2489
2490 const struct string_list *bitmap_preferred_tips(struct repository *r)
2491 {
2492 const struct string_list *dest;
2493
2494 if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2495 return dest;
2496 return NULL;
2497 }
2498
2499 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2500 {
2501 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2502 struct string_list_item *item;
2503
2504 if (!preferred_tips)
2505 return 0;
2506
2507 for_each_string_list_item(item, preferred_tips) {
2508 if (starts_with(refname, item->string))
2509 return 1;
2510 }
2511
2512 return 0;
2513 }
2514
2515 static int verify_bitmap_file(const char *name)
2516 {
2517 struct stat st;
2518 unsigned char *data;
2519 int fd = git_open(name);
2520 int res = 0;
2521
2522 /* It is OK to not have the file. */
2523 if (fd < 0 || fstat(fd, &st)) {
2524 if (fd >= 0)
2525 close(fd);
2526 return 0;
2527 }
2528
2529 data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
2530 close(fd);
2531 if (!hashfile_checksum_valid(data, st.st_size))
2532 res = error(_("bitmap file '%s' has invalid checksum"),
2533 name);
2534
2535 munmap(data, st.st_size);
2536 return res;
2537 }
2538
2539 int verify_bitmap_files(struct repository *r)
2540 {
2541 int res = 0;
2542
2543 for (struct multi_pack_index *m = get_multi_pack_index(r);
2544 m; m = m->next) {
2545 char *midx_bitmap_name = midx_bitmap_filename(m);
2546 res |= verify_bitmap_file(midx_bitmap_name);
2547 free(midx_bitmap_name);
2548 }
2549
2550 for (struct packed_git *p = get_all_packs(r);
2551 p; p = p->next) {
2552 char *pack_bitmap_name = pack_bitmap_filename(p);
2553 res |= verify_bitmap_file(pack_bitmap_name);
2554 free(pack_bitmap_name);
2555 }
2556
2557 return res;
2558 }