]> git.ipfire.org Git - thirdparty/git.git/blob - pack-bitmap.c
hash-ll.h: split out of hash.h to remove dependency on repository.h
[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.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) < 0) {
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 die(_("could not open pack %s"),
392 bitmap_git->midx->pack_names[i]);
393 }
394
395 preferred = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
396 if (!is_pack_valid(preferred)) {
397 warning(_("preferred pack (%s) is invalid"),
398 preferred->pack_name);
399 goto cleanup;
400 }
401
402 return 0;
403
404 cleanup:
405 munmap(bitmap_git->map, bitmap_git->map_size);
406 bitmap_git->map_size = 0;
407 bitmap_git->map_pos = 0;
408 bitmap_git->map = NULL;
409 bitmap_git->midx = NULL;
410 return -1;
411 }
412
413 static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
414 {
415 int fd;
416 struct stat st;
417 char *bitmap_name;
418
419 bitmap_name = pack_bitmap_filename(packfile);
420 fd = git_open(bitmap_name);
421
422 if (fd < 0) {
423 if (errno != ENOENT)
424 warning_errno("cannot open '%s'", bitmap_name);
425 free(bitmap_name);
426 return -1;
427 }
428 free(bitmap_name);
429
430 if (fstat(fd, &st)) {
431 error_errno(_("cannot fstat bitmap file"));
432 close(fd);
433 return -1;
434 }
435
436 if (bitmap_git->pack || bitmap_git->midx) {
437 trace2_data_string("bitmap", the_repository,
438 "ignoring extra bitmap file", packfile->pack_name);
439 close(fd);
440 return -1;
441 }
442
443 if (!is_pack_valid(packfile)) {
444 close(fd);
445 return -1;
446 }
447
448 bitmap_git->pack = packfile;
449 bitmap_git->map_size = xsize_t(st.st_size);
450 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
451 bitmap_git->map_pos = 0;
452 close(fd);
453
454 if (load_bitmap_header(bitmap_git) < 0) {
455 munmap(bitmap_git->map, bitmap_git->map_size);
456 bitmap_git->map = NULL;
457 bitmap_git->map_size = 0;
458 bitmap_git->map_pos = 0;
459 bitmap_git->pack = NULL;
460 return -1;
461 }
462
463 trace2_data_string("bitmap", the_repository, "opened bitmap file",
464 packfile->pack_name);
465 return 0;
466 }
467
468 static int load_reverse_index(struct bitmap_index *bitmap_git)
469 {
470 if (bitmap_is_midx(bitmap_git)) {
471 uint32_t i;
472 int ret;
473
474 /*
475 * The multi-pack-index's .rev file is already loaded via
476 * open_pack_bitmap_1().
477 *
478 * But we still need to open the individual pack .rev files,
479 * since we will need to make use of them in pack-objects.
480 */
481 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
482 ret = load_pack_revindex(bitmap_git->midx->packs[i]);
483 if (ret)
484 return ret;
485 }
486 return 0;
487 }
488 return load_pack_revindex(bitmap_git->pack);
489 }
490
491 static int load_bitmap(struct bitmap_index *bitmap_git)
492 {
493 assert(bitmap_git->map);
494
495 bitmap_git->bitmaps = kh_init_oid_map();
496 bitmap_git->ext_index.positions = kh_init_oid_pos();
497
498 if (load_reverse_index(bitmap_git))
499 goto failed;
500
501 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
502 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
503 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
504 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
505 goto failed;
506
507 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
508 goto failed;
509
510 return 0;
511
512 failed:
513 munmap(bitmap_git->map, bitmap_git->map_size);
514 bitmap_git->map = NULL;
515 bitmap_git->map_size = 0;
516
517 kh_destroy_oid_map(bitmap_git->bitmaps);
518 bitmap_git->bitmaps = NULL;
519
520 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
521 bitmap_git->ext_index.positions = NULL;
522
523 return -1;
524 }
525
526 static int open_pack_bitmap(struct repository *r,
527 struct bitmap_index *bitmap_git)
528 {
529 struct packed_git *p;
530 int ret = -1;
531
532 for (p = get_all_packs(r); p; p = p->next) {
533 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
534 ret = 0;
535 /*
536 * The only reason to keep looking is to report
537 * duplicates.
538 */
539 if (!trace2_is_enabled())
540 break;
541 }
542 }
543
544 return ret;
545 }
546
547 static int open_midx_bitmap(struct repository *r,
548 struct bitmap_index *bitmap_git)
549 {
550 int ret = -1;
551 struct multi_pack_index *midx;
552
553 assert(!bitmap_git->map);
554
555 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
556 if (!open_midx_bitmap_1(bitmap_git, midx))
557 ret = 0;
558 }
559 return ret;
560 }
561
562 static int open_bitmap(struct repository *r,
563 struct bitmap_index *bitmap_git)
564 {
565 int found;
566
567 assert(!bitmap_git->map);
568
569 found = !open_midx_bitmap(r, bitmap_git);
570
571 /*
572 * these will all be skipped if we opened a midx bitmap; but run it
573 * anyway if tracing is enabled to report the duplicates
574 */
575 if (!found || trace2_is_enabled())
576 found |= !open_pack_bitmap(r, bitmap_git);
577
578 return found ? 0 : -1;
579 }
580
581 struct bitmap_index *prepare_bitmap_git(struct repository *r)
582 {
583 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
584
585 if (!open_bitmap(r, bitmap_git) && !load_bitmap(bitmap_git))
586 return bitmap_git;
587
588 free_bitmap_index(bitmap_git);
589 return NULL;
590 }
591
592 struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
593 {
594 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
595
596 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(bitmap_git))
597 return bitmap_git;
598
599 free_bitmap_index(bitmap_git);
600 return NULL;
601 }
602
603 struct include_data {
604 struct bitmap_index *bitmap_git;
605 struct bitmap *base;
606 struct bitmap *seen;
607 };
608
609 struct bitmap_lookup_table_triplet {
610 uint32_t commit_pos;
611 uint64_t offset;
612 uint32_t xor_row;
613 };
614
615 struct bitmap_lookup_table_xor_item {
616 struct object_id oid;
617 uint64_t offset;
618 };
619
620 /*
621 * Given a `triplet` struct pointer and pointer `p`, this
622 * function reads the triplet beginning at `p` into the struct.
623 * Note that this function assumes that there is enough memory
624 * left for filling the `triplet` struct from `p`.
625 */
626 static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
627 const unsigned char *p)
628 {
629 if (!triplet)
630 return -1;
631
632 triplet->commit_pos = get_be32(p);
633 p += sizeof(uint32_t);
634 triplet->offset = get_be64(p);
635 p += sizeof(uint64_t);
636 triplet->xor_row = get_be32(p);
637 return 0;
638 }
639
640 /*
641 * This function gets the raw triplet from `row`'th row in the
642 * lookup table and fills that data to the `triplet`.
643 */
644 static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
645 uint32_t pos,
646 struct bitmap_lookup_table_triplet *triplet)
647 {
648 unsigned char *p = NULL;
649 if (pos >= bitmap_git->entry_count)
650 return error(_("corrupt bitmap lookup table: triplet position out of index"));
651
652 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
653
654 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
655 }
656
657 /*
658 * Searches for a matching triplet. `commit_pos` is a pointer
659 * to the wanted commit position value. `table_entry` points to
660 * a triplet in lookup table. The first 4 bytes of each
661 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
662 */
663 static int triplet_cmp(const void *commit_pos, const void *table_entry)
664 {
665
666 uint32_t a = *(uint32_t *)commit_pos;
667 uint32_t b = get_be32(table_entry);
668 if (a > b)
669 return 1;
670 else if (a < b)
671 return -1;
672
673 return 0;
674 }
675
676 static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
677 struct object_id *oid,
678 uint32_t *result)
679 {
680 int found;
681
682 if (bitmap_is_midx(bitmap_git))
683 found = bsearch_midx(oid, bitmap_git->midx, result);
684 else
685 found = bsearch_pack(oid, bitmap_git->pack, result);
686
687 return found;
688 }
689
690 /*
691 * `bsearch_triplet_by_pos` function searches for the raw triplet
692 * having commit position same as `commit_pos` and fills `triplet`
693 * object from the raw triplet. Returns 1 on success and 0 on
694 * failure.
695 */
696 static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
697 struct bitmap_index *bitmap_git,
698 struct bitmap_lookup_table_triplet *triplet)
699 {
700 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
701 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
702
703 if (!p)
704 return -1;
705
706 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
707 }
708
709 static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
710 struct commit *commit)
711 {
712 uint32_t commit_pos, xor_row;
713 uint64_t offset;
714 int flags;
715 struct bitmap_lookup_table_triplet triplet;
716 struct object_id *oid = &commit->object.oid;
717 struct ewah_bitmap *bitmap;
718 struct stored_bitmap *xor_bitmap = NULL;
719 const int bitmap_header_size = 6;
720 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
721 static size_t xor_items_nr = 0, xor_items_alloc = 0;
722 static int is_corrupt = 0;
723 int xor_flags;
724 khiter_t hash_pos;
725 struct bitmap_lookup_table_xor_item *xor_item;
726
727 if (is_corrupt)
728 return NULL;
729
730 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
731 return NULL;
732
733 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
734 return NULL;
735
736 xor_items_nr = 0;
737 offset = triplet.offset;
738 xor_row = triplet.xor_row;
739
740 while (xor_row != 0xffffffff) {
741 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
742
743 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
744 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
745 goto corrupt;
746 }
747
748 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
749 goto corrupt;
750
751 xor_item = &xor_items[xor_items_nr];
752 xor_item->offset = triplet.offset;
753
754 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
755 error(_("corrupt bitmap lookup table: commit index %u out of range"),
756 triplet.commit_pos);
757 goto corrupt;
758 }
759
760 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
761
762 /*
763 * If desired bitmap is already stored, we don't need
764 * to iterate further. Because we know that bitmaps
765 * that are needed to be parsed to parse this bitmap
766 * has already been stored. So, assign this stored bitmap
767 * to the xor_bitmap.
768 */
769 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
770 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
771 break;
772 xor_items_nr++;
773 xor_row = triplet.xor_row;
774 }
775
776 while (xor_items_nr) {
777 xor_item = &xor_items[xor_items_nr - 1];
778 bitmap_git->map_pos = xor_item->offset;
779 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
780 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
781 oid_to_hex(&xor_item->oid));
782 goto corrupt;
783 }
784
785 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
786 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
787 bitmap = read_bitmap_1(bitmap_git);
788
789 if (!bitmap)
790 goto corrupt;
791
792 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
793 xor_items_nr--;
794 }
795
796 bitmap_git->map_pos = offset;
797 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
798 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
799 oid_to_hex(oid));
800 goto corrupt;
801 }
802
803 /*
804 * Don't bother reading the commit's index position or its xor
805 * offset:
806 *
807 * - The commit's index position is irrelevant to us, since
808 * load_bitmap_entries_v1 only uses it to learn the object
809 * id which is used to compute the hashmap's key. We already
810 * have an object id, so no need to look it up again.
811 *
812 * - The xor_offset is unusable for us, since it specifies how
813 * many entries previous to ours we should look at. This
814 * makes sense when reading the bitmaps sequentially (as in
815 * load_bitmap_entries_v1()), since we can keep track of
816 * each bitmap as we read them.
817 *
818 * But it can't work for us, since the bitmap's don't have a
819 * fixed size. So we learn the position of the xor'd bitmap
820 * from the commit table (and resolve it to a bitmap in the
821 * above if-statement).
822 *
823 * Instead, we can skip ahead and immediately read the flags and
824 * ewah bitmap.
825 */
826 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
827 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
828 bitmap = read_bitmap_1(bitmap_git);
829
830 if (!bitmap)
831 goto corrupt;
832
833 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
834
835 corrupt:
836 free(xor_items);
837 is_corrupt = 1;
838 return NULL;
839 }
840
841 struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
842 struct commit *commit)
843 {
844 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
845 commit->object.oid);
846 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
847 struct stored_bitmap *bitmap = NULL;
848 if (!bitmap_git->table_lookup)
849 return NULL;
850
851 /* this is a fairly hot codepath - no trace2_region please */
852 /* NEEDSWORK: cache misses aren't recorded */
853 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
854 if (!bitmap)
855 return NULL;
856 return lookup_stored_bitmap(bitmap);
857 }
858 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
859 }
860
861 static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
862 const struct object_id *oid)
863 {
864 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
865 khiter_t pos = kh_get_oid_pos(positions, *oid);
866
867 if (pos < kh_end(positions)) {
868 int bitmap_pos = kh_value(positions, pos);
869 return bitmap_pos + bitmap_num_objects(bitmap_git);
870 }
871
872 return -1;
873 }
874
875 static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
876 const struct object_id *oid)
877 {
878 uint32_t pos;
879 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
880 if (!offset)
881 return -1;
882
883 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
884 return -1;
885 return pos;
886 }
887
888 static int bitmap_position_midx(struct bitmap_index *bitmap_git,
889 const struct object_id *oid)
890 {
891 uint32_t want, got;
892 if (!bsearch_midx(oid, bitmap_git->midx, &want))
893 return -1;
894
895 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
896 return -1;
897 return got;
898 }
899
900 static int bitmap_position(struct bitmap_index *bitmap_git,
901 const struct object_id *oid)
902 {
903 int pos;
904 if (bitmap_is_midx(bitmap_git))
905 pos = bitmap_position_midx(bitmap_git, oid);
906 else
907 pos = bitmap_position_packfile(bitmap_git, oid);
908 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
909 }
910
911 static int ext_index_add_object(struct bitmap_index *bitmap_git,
912 struct object *object, const char *name)
913 {
914 struct eindex *eindex = &bitmap_git->ext_index;
915
916 khiter_t hash_pos;
917 int hash_ret;
918 int bitmap_pos;
919
920 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
921 if (hash_ret > 0) {
922 if (eindex->count >= eindex->alloc) {
923 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
924 REALLOC_ARRAY(eindex->objects, eindex->alloc);
925 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
926 }
927
928 bitmap_pos = eindex->count;
929 eindex->objects[eindex->count] = object;
930 eindex->hashes[eindex->count] = pack_name_hash(name);
931 kh_value(eindex->positions, hash_pos) = bitmap_pos;
932 eindex->count++;
933 } else {
934 bitmap_pos = kh_value(eindex->positions, hash_pos);
935 }
936
937 return bitmap_pos + bitmap_num_objects(bitmap_git);
938 }
939
940 struct bitmap_show_data {
941 struct bitmap_index *bitmap_git;
942 struct bitmap *base;
943 };
944
945 static void show_object(struct object *object, const char *name, void *data_)
946 {
947 struct bitmap_show_data *data = data_;
948 int bitmap_pos;
949
950 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
951
952 if (bitmap_pos < 0)
953 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
954 name);
955
956 bitmap_set(data->base, bitmap_pos);
957 }
958
959 static void show_commit(struct commit *commit UNUSED,
960 void *data UNUSED)
961 {
962 }
963
964 static int add_to_include_set(struct bitmap_index *bitmap_git,
965 struct include_data *data,
966 struct commit *commit,
967 int bitmap_pos)
968 {
969 struct ewah_bitmap *partial;
970
971 if (data->seen && bitmap_get(data->seen, bitmap_pos))
972 return 0;
973
974 if (bitmap_get(data->base, bitmap_pos))
975 return 0;
976
977 partial = bitmap_for_commit(bitmap_git, commit);
978 if (partial) {
979 bitmap_or_ewah(data->base, partial);
980 return 0;
981 }
982
983 bitmap_set(data->base, bitmap_pos);
984 return 1;
985 }
986
987 static int should_include(struct commit *commit, void *_data)
988 {
989 struct include_data *data = _data;
990 int bitmap_pos;
991
992 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
993 if (bitmap_pos < 0)
994 bitmap_pos = ext_index_add_object(data->bitmap_git,
995 (struct object *)commit,
996 NULL);
997
998 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
999 struct commit_list *parent = commit->parents;
1000
1001 while (parent) {
1002 parent->item->object.flags |= SEEN;
1003 parent = parent->next;
1004 }
1005
1006 return 0;
1007 }
1008
1009 return 1;
1010 }
1011
1012 static int should_include_obj(struct object *obj, void *_data)
1013 {
1014 struct include_data *data = _data;
1015 int bitmap_pos;
1016
1017 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1018 if (bitmap_pos < 0)
1019 return 1;
1020 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1021 bitmap_get(data->base, bitmap_pos)) {
1022 obj->flags |= SEEN;
1023 return 0;
1024 }
1025 return 1;
1026 }
1027
1028 static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1029 struct bitmap **base,
1030 struct commit *commit)
1031 {
1032 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1033
1034 if (!or_with)
1035 return 0;
1036
1037 if (!*base)
1038 *base = ewah_to_bitmap(or_with);
1039 else
1040 bitmap_or_ewah(*base, or_with);
1041
1042 return 1;
1043 }
1044
1045 static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1046 struct rev_info *revs,
1047 struct object_list *roots,
1048 struct bitmap *seen)
1049 {
1050 struct bitmap *base = NULL;
1051 int needs_walk = 0;
1052
1053 struct object_list *not_mapped = NULL;
1054
1055 /*
1056 * Go through all the roots for the walk. The ones that have bitmaps
1057 * on the bitmap index will be `or`ed together to form an initial
1058 * global reachability analysis.
1059 *
1060 * The ones without bitmaps in the index will be stored in the
1061 * `not_mapped_list` for further processing.
1062 */
1063 while (roots) {
1064 struct object *object = roots->item;
1065 roots = roots->next;
1066
1067 if (object->type == OBJ_COMMIT &&
1068 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1069 object->flags |= SEEN;
1070 continue;
1071 }
1072
1073 object_list_insert(object, &not_mapped);
1074 }
1075
1076 /*
1077 * Best case scenario: We found bitmaps for all the roots,
1078 * so the resulting `or` bitmap has the full reachability analysis
1079 */
1080 if (!not_mapped)
1081 return base;
1082
1083 roots = not_mapped;
1084
1085 /*
1086 * Let's iterate through all the roots that don't have bitmaps to
1087 * check if we can determine them to be reachable from the existing
1088 * global bitmap.
1089 *
1090 * If we cannot find them in the existing global bitmap, we'll need
1091 * to push them to an actual walk and run it until we can confirm
1092 * they are reachable
1093 */
1094 while (roots) {
1095 struct object *object = roots->item;
1096 int pos;
1097
1098 roots = roots->next;
1099 pos = bitmap_position(bitmap_git, &object->oid);
1100
1101 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1102 object->flags &= ~UNINTERESTING;
1103 add_pending_object(revs, object, "");
1104 needs_walk = 1;
1105 } else {
1106 object->flags |= SEEN;
1107 }
1108 }
1109
1110 if (needs_walk) {
1111 struct include_data incdata;
1112 struct bitmap_show_data show_data;
1113
1114 if (!base)
1115 base = bitmap_new();
1116
1117 incdata.bitmap_git = bitmap_git;
1118 incdata.base = base;
1119 incdata.seen = seen;
1120
1121 revs->include_check = should_include;
1122 revs->include_check_obj = should_include_obj;
1123 revs->include_check_data = &incdata;
1124
1125 if (prepare_revision_walk(revs))
1126 die(_("revision walk setup failed"));
1127
1128 show_data.bitmap_git = bitmap_git;
1129 show_data.base = base;
1130
1131 traverse_commit_list(revs,
1132 show_commit, show_object,
1133 &show_data);
1134
1135 revs->include_check = NULL;
1136 revs->include_check_obj = NULL;
1137 revs->include_check_data = NULL;
1138 }
1139
1140 return base;
1141 }
1142
1143 static void show_extended_objects(struct bitmap_index *bitmap_git,
1144 struct rev_info *revs,
1145 show_reachable_fn show_reach)
1146 {
1147 struct bitmap *objects = bitmap_git->result;
1148 struct eindex *eindex = &bitmap_git->ext_index;
1149 uint32_t i;
1150
1151 for (i = 0; i < eindex->count; ++i) {
1152 struct object *obj;
1153
1154 if (!bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1155 continue;
1156
1157 obj = eindex->objects[i];
1158 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1159 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1160 (obj->type == OBJ_TAG && !revs->tag_objects))
1161 continue;
1162
1163 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1164 }
1165 }
1166
1167 static void init_type_iterator(struct ewah_iterator *it,
1168 struct bitmap_index *bitmap_git,
1169 enum object_type type)
1170 {
1171 switch (type) {
1172 case OBJ_COMMIT:
1173 ewah_iterator_init(it, bitmap_git->commits);
1174 break;
1175
1176 case OBJ_TREE:
1177 ewah_iterator_init(it, bitmap_git->trees);
1178 break;
1179
1180 case OBJ_BLOB:
1181 ewah_iterator_init(it, bitmap_git->blobs);
1182 break;
1183
1184 case OBJ_TAG:
1185 ewah_iterator_init(it, bitmap_git->tags);
1186 break;
1187
1188 default:
1189 BUG("object type %d not stored by bitmap type index", type);
1190 break;
1191 }
1192 }
1193
1194 static void show_objects_for_type(
1195 struct bitmap_index *bitmap_git,
1196 enum object_type object_type,
1197 show_reachable_fn show_reach)
1198 {
1199 size_t i = 0;
1200 uint32_t offset;
1201
1202 struct ewah_iterator it;
1203 eword_t filter;
1204
1205 struct bitmap *objects = bitmap_git->result;
1206
1207 init_type_iterator(&it, bitmap_git, object_type);
1208
1209 for (i = 0; i < objects->word_alloc &&
1210 ewah_iterator_next(&filter, &it); i++) {
1211 eword_t word = objects->words[i] & filter;
1212 size_t pos = (i * BITS_IN_EWORD);
1213
1214 if (!word)
1215 continue;
1216
1217 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1218 struct packed_git *pack;
1219 struct object_id oid;
1220 uint32_t hash = 0, index_pos;
1221 off_t ofs;
1222
1223 if ((word >> offset) == 0)
1224 break;
1225
1226 offset += ewah_bit_ctz64(word >> offset);
1227
1228 if (bitmap_is_midx(bitmap_git)) {
1229 struct multi_pack_index *m = bitmap_git->midx;
1230 uint32_t pack_id;
1231
1232 index_pos = pack_pos_to_midx(m, pos + offset);
1233 ofs = nth_midxed_offset(m, index_pos);
1234 nth_midxed_object_oid(&oid, m, index_pos);
1235
1236 pack_id = nth_midxed_pack_int_id(m, index_pos);
1237 pack = bitmap_git->midx->packs[pack_id];
1238 } else {
1239 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1240 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1241 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1242
1243 pack = bitmap_git->pack;
1244 }
1245
1246 if (bitmap_git->hashes)
1247 hash = get_be32(bitmap_git->hashes + index_pos);
1248
1249 show_reach(&oid, object_type, 0, hash, pack, ofs);
1250 }
1251 }
1252 }
1253
1254 static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1255 struct object_list *roots)
1256 {
1257 while (roots) {
1258 struct object *object = roots->item;
1259 roots = roots->next;
1260
1261 if (bitmap_is_midx(bitmap_git)) {
1262 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1263 return 1;
1264 } else {
1265 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1266 return 1;
1267 }
1268 }
1269
1270 return 0;
1271 }
1272
1273 static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1274 struct object_list *tip_objects,
1275 enum object_type type)
1276 {
1277 struct bitmap *result = bitmap_new();
1278 struct object_list *p;
1279
1280 for (p = tip_objects; p; p = p->next) {
1281 int pos;
1282
1283 if (p->item->type != type)
1284 continue;
1285
1286 pos = bitmap_position(bitmap_git, &p->item->oid);
1287 if (pos < 0)
1288 continue;
1289
1290 bitmap_set(result, pos);
1291 }
1292
1293 return result;
1294 }
1295
1296 static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1297 struct object_list *tip_objects,
1298 struct bitmap *to_filter,
1299 enum object_type type)
1300 {
1301 struct eindex *eindex = &bitmap_git->ext_index;
1302 struct bitmap *tips;
1303 struct ewah_iterator it;
1304 eword_t mask;
1305 uint32_t i;
1306
1307 /*
1308 * The non-bitmap version of this filter never removes
1309 * objects which the other side specifically asked for,
1310 * so we must match that behavior.
1311 */
1312 tips = find_tip_objects(bitmap_git, tip_objects, type);
1313
1314 /*
1315 * We can use the type-level bitmap for 'type' to work in whole
1316 * words for the objects that are actually in the bitmapped
1317 * packfile.
1318 */
1319 for (i = 0, init_type_iterator(&it, bitmap_git, type);
1320 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1321 i++) {
1322 if (i < tips->word_alloc)
1323 mask &= ~tips->words[i];
1324 to_filter->words[i] &= ~mask;
1325 }
1326
1327 /*
1328 * Clear any objects that weren't in the packfile (and so would
1329 * not have been caught by the loop above. We'll have to check
1330 * them individually.
1331 */
1332 for (i = 0; i < eindex->count; i++) {
1333 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1334 if (eindex->objects[i]->type == type &&
1335 bitmap_get(to_filter, pos) &&
1336 !bitmap_get(tips, pos))
1337 bitmap_unset(to_filter, pos);
1338 }
1339
1340 bitmap_free(tips);
1341 }
1342
1343 static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1344 struct object_list *tip_objects,
1345 struct bitmap *to_filter)
1346 {
1347 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1348 OBJ_BLOB);
1349 }
1350
1351 static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1352 uint32_t pos)
1353 {
1354 unsigned long size;
1355 struct object_info oi = OBJECT_INFO_INIT;
1356
1357 oi.sizep = &size;
1358
1359 if (pos < bitmap_num_objects(bitmap_git)) {
1360 struct packed_git *pack;
1361 off_t ofs;
1362
1363 if (bitmap_is_midx(bitmap_git)) {
1364 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1365 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1366
1367 pack = bitmap_git->midx->packs[pack_id];
1368 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1369 } else {
1370 pack = bitmap_git->pack;
1371 ofs = pack_pos_to_offset(pack, pos);
1372 }
1373
1374 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1375 struct object_id oid;
1376 nth_bitmap_object_oid(bitmap_git, &oid,
1377 pack_pos_to_index(pack, pos));
1378 die(_("unable to get size of %s"), oid_to_hex(&oid));
1379 }
1380 } else {
1381 struct eindex *eindex = &bitmap_git->ext_index;
1382 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1383 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1384 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1385 }
1386
1387 return size;
1388 }
1389
1390 static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1391 struct object_list *tip_objects,
1392 struct bitmap *to_filter,
1393 unsigned long limit)
1394 {
1395 struct eindex *eindex = &bitmap_git->ext_index;
1396 struct bitmap *tips;
1397 struct ewah_iterator it;
1398 eword_t mask;
1399 uint32_t i;
1400
1401 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1402
1403 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1404 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1405 i++) {
1406 eword_t word = to_filter->words[i] & mask;
1407 unsigned offset;
1408
1409 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1410 uint32_t pos;
1411
1412 if ((word >> offset) == 0)
1413 break;
1414 offset += ewah_bit_ctz64(word >> offset);
1415 pos = i * BITS_IN_EWORD + offset;
1416
1417 if (!bitmap_get(tips, pos) &&
1418 get_size_by_pos(bitmap_git, pos) >= limit)
1419 bitmap_unset(to_filter, pos);
1420 }
1421 }
1422
1423 for (i = 0; i < eindex->count; i++) {
1424 uint32_t pos = i + bitmap_num_objects(bitmap_git);
1425 if (eindex->objects[i]->type == OBJ_BLOB &&
1426 bitmap_get(to_filter, pos) &&
1427 !bitmap_get(tips, pos) &&
1428 get_size_by_pos(bitmap_git, pos) >= limit)
1429 bitmap_unset(to_filter, pos);
1430 }
1431
1432 bitmap_free(tips);
1433 }
1434
1435 static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1436 struct object_list *tip_objects,
1437 struct bitmap *to_filter,
1438 unsigned long limit)
1439 {
1440 if (limit)
1441 BUG("filter_bitmap_tree_depth given non-zero limit");
1442
1443 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1444 OBJ_TREE);
1445 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1446 OBJ_BLOB);
1447 }
1448
1449 static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1450 struct object_list *tip_objects,
1451 struct bitmap *to_filter,
1452 enum object_type object_type)
1453 {
1454 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1455 BUG("filter_bitmap_object_type given invalid object");
1456
1457 if (object_type != OBJ_TAG)
1458 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1459 if (object_type != OBJ_COMMIT)
1460 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1461 if (object_type != OBJ_TREE)
1462 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1463 if (object_type != OBJ_BLOB)
1464 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1465 }
1466
1467 static int filter_bitmap(struct bitmap_index *bitmap_git,
1468 struct object_list *tip_objects,
1469 struct bitmap *to_filter,
1470 struct list_objects_filter_options *filter)
1471 {
1472 if (!filter || filter->choice == LOFC_DISABLED)
1473 return 0;
1474
1475 if (filter->choice == LOFC_BLOB_NONE) {
1476 if (bitmap_git)
1477 filter_bitmap_blob_none(bitmap_git, tip_objects,
1478 to_filter);
1479 return 0;
1480 }
1481
1482 if (filter->choice == LOFC_BLOB_LIMIT) {
1483 if (bitmap_git)
1484 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1485 to_filter,
1486 filter->blob_limit_value);
1487 return 0;
1488 }
1489
1490 if (filter->choice == LOFC_TREE_DEPTH &&
1491 filter->tree_exclude_depth == 0) {
1492 if (bitmap_git)
1493 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1494 to_filter,
1495 filter->tree_exclude_depth);
1496 return 0;
1497 }
1498
1499 if (filter->choice == LOFC_OBJECT_TYPE) {
1500 if (bitmap_git)
1501 filter_bitmap_object_type(bitmap_git, tip_objects,
1502 to_filter,
1503 filter->object_type);
1504 return 0;
1505 }
1506
1507 if (filter->choice == LOFC_COMBINE) {
1508 int i;
1509 for (i = 0; i < filter->sub_nr; i++) {
1510 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1511 &filter->sub[i]) < 0)
1512 return -1;
1513 }
1514 return 0;
1515 }
1516
1517 /* filter choice not handled */
1518 return -1;
1519 }
1520
1521 static int can_filter_bitmap(struct list_objects_filter_options *filter)
1522 {
1523 return !filter_bitmap(NULL, NULL, NULL, filter);
1524 }
1525
1526 struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1527 int filter_provided_objects)
1528 {
1529 unsigned int i;
1530
1531 struct object_list *wants = NULL;
1532 struct object_list *haves = NULL;
1533
1534 struct bitmap *wants_bitmap = NULL;
1535 struct bitmap *haves_bitmap = NULL;
1536
1537 struct bitmap_index *bitmap_git;
1538
1539 /*
1540 * We can't do pathspec limiting with bitmaps, because we don't know
1541 * which commits are associated with which object changes (let alone
1542 * even which objects are associated with which paths).
1543 */
1544 if (revs->prune)
1545 return NULL;
1546
1547 if (!can_filter_bitmap(&revs->filter))
1548 return NULL;
1549
1550 /* try to open a bitmapped pack, but don't parse it yet
1551 * because we may not need to use it */
1552 CALLOC_ARRAY(bitmap_git, 1);
1553 if (open_bitmap(revs->repo, bitmap_git) < 0)
1554 goto cleanup;
1555
1556 for (i = 0; i < revs->pending.nr; ++i) {
1557 struct object *object = revs->pending.objects[i].item;
1558
1559 if (object->type == OBJ_NONE)
1560 parse_object_or_die(&object->oid, NULL);
1561
1562 while (object->type == OBJ_TAG) {
1563 struct tag *tag = (struct tag *) object;
1564
1565 if (object->flags & UNINTERESTING)
1566 object_list_insert(object, &haves);
1567 else
1568 object_list_insert(object, &wants);
1569
1570 object = parse_object_or_die(get_tagged_oid(tag), NULL);
1571 object->flags |= (tag->object.flags & UNINTERESTING);
1572 }
1573
1574 if (object->flags & UNINTERESTING)
1575 object_list_insert(object, &haves);
1576 else
1577 object_list_insert(object, &wants);
1578 }
1579
1580 /*
1581 * if we have a HAVES list, but none of those haves is contained
1582 * in the packfile that has a bitmap, we don't have anything to
1583 * optimize here
1584 */
1585 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1586 goto cleanup;
1587
1588 /* if we don't want anything, we're done here */
1589 if (!wants)
1590 goto cleanup;
1591
1592 /*
1593 * now we're going to use bitmaps, so load the actual bitmap entries
1594 * from disk. this is the point of no return; after this the rev_list
1595 * becomes invalidated and we must perform the revwalk through bitmaps
1596 */
1597 if (load_bitmap(bitmap_git) < 0)
1598 goto cleanup;
1599
1600 object_array_clear(&revs->pending);
1601
1602 if (haves) {
1603 revs->ignore_missing_links = 1;
1604 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1605 reset_revision_walk();
1606 revs->ignore_missing_links = 0;
1607
1608 if (!haves_bitmap)
1609 BUG("failed to perform bitmap walk");
1610 }
1611
1612 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
1613
1614 if (!wants_bitmap)
1615 BUG("failed to perform bitmap walk");
1616
1617 if (haves_bitmap)
1618 bitmap_and_not(wants_bitmap, haves_bitmap);
1619
1620 filter_bitmap(bitmap_git,
1621 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1622 wants_bitmap,
1623 &revs->filter);
1624
1625 bitmap_git->result = wants_bitmap;
1626 bitmap_git->haves = haves_bitmap;
1627
1628 object_list_free(&wants);
1629 object_list_free(&haves);
1630
1631 return bitmap_git;
1632
1633 cleanup:
1634 free_bitmap_index(bitmap_git);
1635 object_list_free(&wants);
1636 object_list_free(&haves);
1637 return NULL;
1638 }
1639
1640 /*
1641 * -1 means "stop trying further objects"; 0 means we may or may not have
1642 * reused, but you can keep feeding bits.
1643 */
1644 static int try_partial_reuse(struct packed_git *pack,
1645 size_t pos,
1646 struct bitmap *reuse,
1647 struct pack_window **w_curs)
1648 {
1649 off_t offset, delta_obj_offset;
1650 enum object_type type;
1651 unsigned long size;
1652
1653 /*
1654 * try_partial_reuse() is called either on (a) objects in the
1655 * bitmapped pack (in the case of a single-pack bitmap) or (b)
1656 * objects in the preferred pack of a multi-pack bitmap.
1657 * Importantly, the latter can pretend as if only a single pack
1658 * exists because:
1659 *
1660 * - The first pack->num_objects bits of a MIDX bitmap are
1661 * reserved for the preferred pack, and
1662 *
1663 * - Ties due to duplicate objects are always resolved in
1664 * favor of the preferred pack.
1665 *
1666 * Therefore we do not need to ever ask the MIDX for its copy of
1667 * an object by OID, since it will always select it from the
1668 * preferred pack. Likewise, the selected copy of the base
1669 * object for any deltas will reside in the same pack.
1670 *
1671 * This means that we can reuse pos when looking up the bit in
1672 * the reuse bitmap, too, since bits corresponding to the
1673 * preferred pack precede all bits from other packs.
1674 */
1675
1676 if (pos >= pack->num_objects)
1677 return -1; /* not actually in the pack or MIDX preferred pack */
1678
1679 offset = delta_obj_offset = pack_pos_to_offset(pack, pos);
1680 type = unpack_object_header(pack, w_curs, &offset, &size);
1681 if (type < 0)
1682 return -1; /* broken packfile, punt */
1683
1684 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1685 off_t base_offset;
1686 uint32_t base_pos;
1687
1688 /*
1689 * Find the position of the base object so we can look it up
1690 * in our bitmaps. If we can't come up with an offset, or if
1691 * that offset is not in the revidx, the pack is corrupt.
1692 * There's nothing we can do, so just punt on this object,
1693 * and the normal slow path will complain about it in
1694 * more detail.
1695 */
1696 base_offset = get_delta_base(pack, w_curs, &offset, type,
1697 delta_obj_offset);
1698 if (!base_offset)
1699 return 0;
1700 if (offset_to_pack_pos(pack, base_offset, &base_pos) < 0)
1701 return 0;
1702
1703 /*
1704 * We assume delta dependencies always point backwards. This
1705 * lets us do a single pass, and is basically always true
1706 * due to the way OFS_DELTAs work. You would not typically
1707 * find REF_DELTA in a bitmapped pack, since we only bitmap
1708 * packs we write fresh, and OFS_DELTA is the default). But
1709 * let's double check to make sure the pack wasn't written with
1710 * odd parameters.
1711 */
1712 if (base_pos >= pos)
1713 return 0;
1714
1715 /*
1716 * And finally, if we're not sending the base as part of our
1717 * reuse chunk, then don't send this object either. The base
1718 * would come after us, along with other objects not
1719 * necessarily in the pack, which means we'd need to convert
1720 * to REF_DELTA on the fly. Better to just let the normal
1721 * object_entry code path handle it.
1722 */
1723 if (!bitmap_get(reuse, base_pos))
1724 return 0;
1725 }
1726
1727 /*
1728 * If we got here, then the object is OK to reuse. Mark it.
1729 */
1730 bitmap_set(reuse, pos);
1731 return 0;
1732 }
1733
1734 uint32_t midx_preferred_pack(struct bitmap_index *bitmap_git)
1735 {
1736 struct multi_pack_index *m = bitmap_git->midx;
1737 if (!m)
1738 BUG("midx_preferred_pack: requires non-empty MIDX");
1739 return nth_midxed_pack_int_id(m, pack_pos_to_midx(bitmap_git->midx, 0));
1740 }
1741
1742 int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1743 struct packed_git **packfile_out,
1744 uint32_t *entries,
1745 struct bitmap **reuse_out)
1746 {
1747 struct packed_git *pack;
1748 struct bitmap *result = bitmap_git->result;
1749 struct bitmap *reuse;
1750 struct pack_window *w_curs = NULL;
1751 size_t i = 0;
1752 uint32_t offset;
1753 uint32_t objects_nr;
1754
1755 assert(result);
1756
1757 load_reverse_index(bitmap_git);
1758
1759 if (bitmap_is_midx(bitmap_git))
1760 pack = bitmap_git->midx->packs[midx_preferred_pack(bitmap_git)];
1761 else
1762 pack = bitmap_git->pack;
1763 objects_nr = pack->num_objects;
1764
1765 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1766 i++;
1767
1768 /*
1769 * Don't mark objects not in the packfile or preferred pack. This bitmap
1770 * marks objects eligible for reuse, but the pack-reuse code only
1771 * understands how to reuse a single pack. Since the preferred pack is
1772 * guaranteed to have all bases for its deltas (in a multi-pack bitmap),
1773 * we use it instead of another pack. In single-pack bitmaps, the choice
1774 * is made for us.
1775 */
1776 if (i > objects_nr / BITS_IN_EWORD)
1777 i = objects_nr / BITS_IN_EWORD;
1778
1779 reuse = bitmap_word_alloc(i);
1780 memset(reuse->words, 0xFF, i * sizeof(eword_t));
1781
1782 for (; i < result->word_alloc; ++i) {
1783 eword_t word = result->words[i];
1784 size_t pos = (i * BITS_IN_EWORD);
1785
1786 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1787 if ((word >> offset) == 0)
1788 break;
1789
1790 offset += ewah_bit_ctz64(word >> offset);
1791 if (try_partial_reuse(pack, pos + offset,
1792 reuse, &w_curs) < 0) {
1793 /*
1794 * try_partial_reuse indicated we couldn't reuse
1795 * any bits, so there is no point in trying more
1796 * bits in the current word, or any other words
1797 * in result.
1798 *
1799 * Jump out of both loops to avoid future
1800 * unnecessary calls to try_partial_reuse.
1801 */
1802 goto done;
1803 }
1804 }
1805 }
1806
1807 done:
1808 unuse_pack(&w_curs);
1809
1810 *entries = bitmap_popcount(reuse);
1811 if (!*entries) {
1812 bitmap_free(reuse);
1813 return -1;
1814 }
1815
1816 /*
1817 * Drop any reused objects from the result, since they will not
1818 * need to be handled separately.
1819 */
1820 bitmap_and_not(result, reuse);
1821 *packfile_out = pack;
1822 *reuse_out = reuse;
1823 return 0;
1824 }
1825
1826 int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1827 struct bitmap *bitmap, const struct object_id *oid)
1828 {
1829 int idx;
1830
1831 if (!bitmap)
1832 return 0;
1833
1834 idx = bitmap_position(bitmap_git, oid);
1835 return idx >= 0 && bitmap_get(bitmap, idx);
1836 }
1837
1838 void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
1839 struct rev_info *revs,
1840 show_reachable_fn show_reachable)
1841 {
1842 assert(bitmap_git->result);
1843
1844 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
1845 if (revs->tree_objects)
1846 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1847 if (revs->blob_objects)
1848 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1849 if (revs->tag_objects)
1850 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
1851
1852 show_extended_objects(bitmap_git, revs, show_reachable);
1853 }
1854
1855 static uint32_t count_object_type(struct bitmap_index *bitmap_git,
1856 enum object_type type)
1857 {
1858 struct bitmap *objects = bitmap_git->result;
1859 struct eindex *eindex = &bitmap_git->ext_index;
1860
1861 uint32_t i = 0, count = 0;
1862 struct ewah_iterator it;
1863 eword_t filter;
1864
1865 init_type_iterator(&it, bitmap_git, type);
1866
1867 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1868 eword_t word = objects->words[i++] & filter;
1869 count += ewah_bit_popcount64(word);
1870 }
1871
1872 for (i = 0; i < eindex->count; ++i) {
1873 if (eindex->objects[i]->type == type &&
1874 bitmap_get(objects, bitmap_num_objects(bitmap_git) + i))
1875 count++;
1876 }
1877
1878 return count;
1879 }
1880
1881 void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1882 uint32_t *commits, uint32_t *trees,
1883 uint32_t *blobs, uint32_t *tags)
1884 {
1885 assert(bitmap_git->result);
1886
1887 if (commits)
1888 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
1889
1890 if (trees)
1891 *trees = count_object_type(bitmap_git, OBJ_TREE);
1892
1893 if (blobs)
1894 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
1895
1896 if (tags)
1897 *tags = count_object_type(bitmap_git, OBJ_TAG);
1898 }
1899
1900 struct bitmap_test_data {
1901 struct bitmap_index *bitmap_git;
1902 struct bitmap *base;
1903 struct bitmap *commits;
1904 struct bitmap *trees;
1905 struct bitmap *blobs;
1906 struct bitmap *tags;
1907 struct progress *prg;
1908 size_t seen;
1909 };
1910
1911 static void test_bitmap_type(struct bitmap_test_data *tdata,
1912 struct object *obj, int pos)
1913 {
1914 enum object_type bitmap_type = OBJ_NONE;
1915 int bitmaps_nr = 0;
1916
1917 if (bitmap_get(tdata->commits, pos)) {
1918 bitmap_type = OBJ_COMMIT;
1919 bitmaps_nr++;
1920 }
1921 if (bitmap_get(tdata->trees, pos)) {
1922 bitmap_type = OBJ_TREE;
1923 bitmaps_nr++;
1924 }
1925 if (bitmap_get(tdata->blobs, pos)) {
1926 bitmap_type = OBJ_BLOB;
1927 bitmaps_nr++;
1928 }
1929 if (bitmap_get(tdata->tags, pos)) {
1930 bitmap_type = OBJ_TAG;
1931 bitmaps_nr++;
1932 }
1933
1934 if (bitmap_type == OBJ_NONE)
1935 die(_("object '%s' not found in type bitmaps"),
1936 oid_to_hex(&obj->oid));
1937
1938 if (bitmaps_nr > 1)
1939 die(_("object '%s' does not have a unique type"),
1940 oid_to_hex(&obj->oid));
1941
1942 if (bitmap_type != obj->type)
1943 die(_("object '%s': real type '%s', expected: '%s'"),
1944 oid_to_hex(&obj->oid),
1945 type_name(obj->type),
1946 type_name(bitmap_type));
1947 }
1948
1949 static void test_show_object(struct object *object,
1950 const char *name UNUSED,
1951 void *data)
1952 {
1953 struct bitmap_test_data *tdata = data;
1954 int bitmap_pos;
1955
1956 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
1957 if (bitmap_pos < 0)
1958 die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
1959 test_bitmap_type(tdata, object, bitmap_pos);
1960
1961 bitmap_set(tdata->base, bitmap_pos);
1962 display_progress(tdata->prg, ++tdata->seen);
1963 }
1964
1965 static void test_show_commit(struct commit *commit, void *data)
1966 {
1967 struct bitmap_test_data *tdata = data;
1968 int bitmap_pos;
1969
1970 bitmap_pos = bitmap_position(tdata->bitmap_git,
1971 &commit->object.oid);
1972 if (bitmap_pos < 0)
1973 die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
1974 test_bitmap_type(tdata, &commit->object, bitmap_pos);
1975
1976 bitmap_set(tdata->base, bitmap_pos);
1977 display_progress(tdata->prg, ++tdata->seen);
1978 }
1979
1980 void test_bitmap_walk(struct rev_info *revs)
1981 {
1982 struct object *root;
1983 struct bitmap *result = NULL;
1984 size_t result_popcnt;
1985 struct bitmap_test_data tdata;
1986 struct bitmap_index *bitmap_git;
1987 struct ewah_bitmap *bm;
1988
1989 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
1990 die(_("failed to load bitmap indexes"));
1991
1992 if (revs->pending.nr != 1)
1993 die(_("you must specify exactly one commit to test"));
1994
1995 fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
1996 bitmap_git->version,
1997 bitmap_git->entry_count,
1998 bitmap_git->table_lookup ? "" : " loaded");
1999
2000 root = revs->pending.objects[0].item;
2001 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2002
2003 if (bm) {
2004 fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2005 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2006
2007 result = ewah_to_bitmap(bm);
2008 }
2009
2010 if (!result)
2011 die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2012
2013 revs->tag_objects = 1;
2014 revs->tree_objects = 1;
2015 revs->blob_objects = 1;
2016
2017 result_popcnt = bitmap_popcount(result);
2018
2019 if (prepare_revision_walk(revs))
2020 die(_("revision walk setup failed"));
2021
2022 tdata.bitmap_git = bitmap_git;
2023 tdata.base = bitmap_new();
2024 tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2025 tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2026 tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2027 tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2028 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2029 tdata.seen = 0;
2030
2031 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2032
2033 stop_progress(&tdata.prg);
2034
2035 if (bitmap_equals(result, tdata.base))
2036 fprintf_ln(stderr, "OK!");
2037 else
2038 die(_("mismatch in bitmap results"));
2039
2040 bitmap_free(result);
2041 bitmap_free(tdata.base);
2042 bitmap_free(tdata.commits);
2043 bitmap_free(tdata.trees);
2044 bitmap_free(tdata.blobs);
2045 bitmap_free(tdata.tags);
2046 free_bitmap_index(bitmap_git);
2047 }
2048
2049 int test_bitmap_commits(struct repository *r)
2050 {
2051 struct object_id oid;
2052 MAYBE_UNUSED void *value;
2053 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2054
2055 if (!bitmap_git)
2056 die(_("failed to load bitmap indexes"));
2057
2058 /*
2059 * As this function is only used to print bitmap selected
2060 * commits, we don't have to read the commit table.
2061 */
2062 if (bitmap_git->table_lookup) {
2063 if (load_bitmap_entries_v1(bitmap_git) < 0)
2064 die(_("failed to load bitmap indexes"));
2065 }
2066
2067 kh_foreach(bitmap_git->bitmaps, oid, value, {
2068 printf_ln("%s", oid_to_hex(&oid));
2069 });
2070
2071 free_bitmap_index(bitmap_git);
2072
2073 return 0;
2074 }
2075
2076 int test_bitmap_hashes(struct repository *r)
2077 {
2078 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2079 struct object_id oid;
2080 uint32_t i, index_pos;
2081
2082 if (!bitmap_git || !bitmap_git->hashes)
2083 goto cleanup;
2084
2085 for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2086 if (bitmap_is_midx(bitmap_git))
2087 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2088 else
2089 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2090
2091 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2092
2093 printf_ln("%s %"PRIu32"",
2094 oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2095 }
2096
2097 cleanup:
2098 free_bitmap_index(bitmap_git);
2099
2100 return 0;
2101 }
2102
2103 int rebuild_bitmap(const uint32_t *reposition,
2104 struct ewah_bitmap *source,
2105 struct bitmap *dest)
2106 {
2107 uint32_t pos = 0;
2108 struct ewah_iterator it;
2109 eword_t word;
2110
2111 ewah_iterator_init(&it, source);
2112
2113 while (ewah_iterator_next(&word, &it)) {
2114 uint32_t offset, bit_pos;
2115
2116 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2117 if ((word >> offset) == 0)
2118 break;
2119
2120 offset += ewah_bit_ctz64(word >> offset);
2121
2122 bit_pos = reposition[pos + offset];
2123 if (bit_pos > 0)
2124 bitmap_set(dest, bit_pos - 1);
2125 else /* can't reuse, we don't have the object */
2126 return -1;
2127 }
2128
2129 pos += BITS_IN_EWORD;
2130 }
2131 return 0;
2132 }
2133
2134 uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2135 struct packing_data *mapping)
2136 {
2137 uint32_t i, num_objects;
2138 uint32_t *reposition;
2139
2140 if (!bitmap_is_midx(bitmap_git))
2141 load_reverse_index(bitmap_git);
2142 else if (load_midx_revindex(bitmap_git->midx) < 0)
2143 BUG("rebuild_existing_bitmaps: missing required rev-cache "
2144 "extension");
2145
2146 num_objects = bitmap_num_objects(bitmap_git);
2147 CALLOC_ARRAY(reposition, num_objects);
2148
2149 for (i = 0; i < num_objects; ++i) {
2150 struct object_id oid;
2151 struct object_entry *oe;
2152 uint32_t index_pos;
2153
2154 if (bitmap_is_midx(bitmap_git))
2155 index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2156 else
2157 index_pos = pack_pos_to_index(bitmap_git->pack, i);
2158 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2159 oe = packlist_find(mapping, &oid);
2160
2161 if (oe) {
2162 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2163 if (bitmap_git->hashes && !oe->hash)
2164 oe->hash = get_be32(bitmap_git->hashes + index_pos);
2165 }
2166 }
2167
2168 return reposition;
2169 }
2170
2171 void free_bitmap_index(struct bitmap_index *b)
2172 {
2173 if (!b)
2174 return;
2175
2176 if (b->map)
2177 munmap(b->map, b->map_size);
2178 ewah_pool_free(b->commits);
2179 ewah_pool_free(b->trees);
2180 ewah_pool_free(b->blobs);
2181 ewah_pool_free(b->tags);
2182 if (b->bitmaps) {
2183 struct stored_bitmap *sb;
2184 kh_foreach_value(b->bitmaps, sb, {
2185 ewah_pool_free(sb->root);
2186 free(sb);
2187 });
2188 }
2189 kh_destroy_oid_map(b->bitmaps);
2190 free(b->ext_index.objects);
2191 free(b->ext_index.hashes);
2192 kh_destroy_oid_pos(b->ext_index.positions);
2193 bitmap_free(b->result);
2194 bitmap_free(b->haves);
2195 if (bitmap_is_midx(b)) {
2196 /*
2197 * Multi-pack bitmaps need to have resources associated with
2198 * their on-disk reverse indexes unmapped so that stale .rev and
2199 * .bitmap files can be removed.
2200 *
2201 * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2202 * written in the same 'git multi-pack-index write --bitmap'
2203 * process. Close resources so they can be removed safely on
2204 * platforms like Windows.
2205 */
2206 close_midx_revindex(b->midx);
2207 }
2208 free(b);
2209 }
2210
2211 int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2212 const struct object_id *oid)
2213 {
2214 return bitmap_git &&
2215 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2216 }
2217
2218 static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2219 enum object_type object_type)
2220 {
2221 struct bitmap *result = bitmap_git->result;
2222 off_t total = 0;
2223 struct ewah_iterator it;
2224 eword_t filter;
2225 size_t i;
2226
2227 init_type_iterator(&it, bitmap_git, object_type);
2228 for (i = 0; i < result->word_alloc &&
2229 ewah_iterator_next(&filter, &it); i++) {
2230 eword_t word = result->words[i] & filter;
2231 size_t base = (i * BITS_IN_EWORD);
2232 unsigned offset;
2233
2234 if (!word)
2235 continue;
2236
2237 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2238 if ((word >> offset) == 0)
2239 break;
2240
2241 offset += ewah_bit_ctz64(word >> offset);
2242
2243 if (bitmap_is_midx(bitmap_git)) {
2244 uint32_t pack_pos;
2245 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2246 off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2247
2248 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2249 struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2250
2251 if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2252 struct object_id oid;
2253 nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2254
2255 die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2256 oid_to_hex(&oid),
2257 pack->pack_name,
2258 (uintmax_t)offset);
2259 }
2260
2261 total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2262 } else {
2263 size_t pos = base + offset;
2264 total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2265 pack_pos_to_offset(bitmap_git->pack, pos);
2266 }
2267 }
2268 }
2269
2270 return total;
2271 }
2272
2273 static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2274 {
2275 struct bitmap *result = bitmap_git->result;
2276 struct eindex *eindex = &bitmap_git->ext_index;
2277 off_t total = 0;
2278 struct object_info oi = OBJECT_INFO_INIT;
2279 off_t object_size;
2280 size_t i;
2281
2282 oi.disk_sizep = &object_size;
2283
2284 for (i = 0; i < eindex->count; i++) {
2285 struct object *obj = eindex->objects[i];
2286
2287 if (!bitmap_get(result, bitmap_num_objects(bitmap_git) + i))
2288 continue;
2289
2290 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2291 die(_("unable to get disk usage of '%s'"),
2292 oid_to_hex(&obj->oid));
2293
2294 total += object_size;
2295 }
2296 return total;
2297 }
2298
2299 off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2300 struct rev_info *revs)
2301 {
2302 off_t total = 0;
2303
2304 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2305 if (revs->tree_objects)
2306 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2307 if (revs->blob_objects)
2308 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2309 if (revs->tag_objects)
2310 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2311
2312 total += get_disk_usage_for_extended(bitmap_git);
2313
2314 return total;
2315 }
2316
2317 int bitmap_is_midx(struct bitmap_index *bitmap_git)
2318 {
2319 return !!bitmap_git->midx;
2320 }
2321
2322 const struct string_list *bitmap_preferred_tips(struct repository *r)
2323 {
2324 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
2325 }
2326
2327 int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2328 {
2329 const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2330 struct string_list_item *item;
2331
2332 if (!preferred_tips)
2333 return 0;
2334
2335 for_each_string_list_item(item, preferred_tips) {
2336 if (starts_with(refname, item->string))
2337 return 1;
2338 }
2339
2340 return 0;
2341 }