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