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