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