]> git.ipfire.org Git - thirdparty/git.git/blame - pack-bitmap.c
docs: address typos in Git v2.45 changelog
[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
TB
311 struct strbuf buf = STRBUF_INIT;
312
313 get_midx_filename(&buf, midx->object_dir);
314 strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
315
316 return strbuf_detach(&buf, NULL);
0f533c72
TB
317}
318
319char *pack_bitmap_filename(struct packed_git *p)
cb468050 320{
9ae97018 321 size_t len;
cb468050 322
9ae97018 323 if (!strip_suffix(p->pack_name, ".pack", &len))
033abf97 324 BUG("pack_name does not end in .pack");
9ae97018 325 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
cb468050
JH
326}
327
0f533c72
TB
328static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
329 struct multi_pack_index *midx)
330{
331 struct stat st;
349c26ff
TL
332 char *bitmap_name = midx_bitmap_filename(midx);
333 int fd = git_open(bitmap_name);
b1e33330 334 uint32_t i, preferred_pack;
44f9fd64 335 struct packed_git *preferred;
0f533c72 336
6411cc08
TL
337 if (fd < 0) {
338 if (errno != ENOENT)
339 warning_errno("cannot open '%s'", bitmap_name);
340 free(bitmap_name);
0f533c72 341 return -1;
6411cc08
TL
342 }
343 free(bitmap_name);
0f533c72
TB
344
345 if (fstat(fd, &st)) {
9005eb02 346 error_errno(_("cannot fstat bitmap file"));
0f533c72
TB
347 close(fd);
348 return -1;
349 }
350
351 if (bitmap_git->pack || bitmap_git->midx) {
60980aed
TB
352 struct strbuf buf = STRBUF_INIT;
353 get_midx_filename(&buf, midx->object_dir);
8ddc0663
TL
354 trace2_data_string("bitmap", the_repository,
355 "ignoring extra midx bitmap file", buf.buf);
0f533c72 356 close(fd);
60980aed 357 strbuf_release(&buf);
0f533c72
TB
358 return -1;
359 }
360
361 bitmap_git->midx = midx;
362 bitmap_git->map_size = xsize_t(st.st_size);
363 bitmap_git->map_pos = 0;
364 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
365 MAP_PRIVATE, fd, 0);
366 close(fd);
367
368 if (load_bitmap_header(bitmap_git) < 0)
369 goto cleanup;
370
9005eb02
TL
371 if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
372 error(_("checksum doesn't match in MIDX and bitmap"));
0f533c72 373 goto cleanup;
9005eb02 374 }
0f533c72 375
5a6072f6 376 if (load_midx_revindex(bitmap_git->midx)) {
0f533c72
TB
377 warning(_("multi-pack bitmap is missing required reverse index"));
378 goto cleanup;
379 }
44f9fd64
TB
380
381 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
06f38678
TB
382 if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
383 warning(_("could not open pack %s"),
384 bitmap_git->midx->pack_names[i]);
385 goto cleanup;
386 }
44f9fd64
TB
387 }
388
b1e33330
TB
389 if (midx_preferred_pack(bitmap_git->midx, &preferred_pack) < 0) {
390 warning(_("could not determine MIDX preferred pack"));
391 goto cleanup;
392 }
393
394 preferred = bitmap_git->midx->packs[preferred_pack];
44f9fd64
TB
395 if (!is_pack_valid(preferred)) {
396 warning(_("preferred pack (%s) is invalid"),
397 preferred->pack_name);
398 goto cleanup;
399 }
400
0f533c72
TB
401 return 0;
402
403cleanup:
404 munmap(bitmap_git->map, bitmap_git->map_size);
405 bitmap_git->map_size = 0;
f8b60cf9 406 bitmap_git->map_pos = 0;
0f533c72 407 bitmap_git->map = NULL;
f8b60cf9 408 bitmap_git->midx = NULL;
0f533c72
TB
409 return -1;
410}
411
3ae5fa07 412static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
fff42755
VM
413{
414 int fd;
415 struct stat st;
349c26ff 416 char *bitmap_name;
fff42755 417
349c26ff
TL
418 bitmap_name = pack_bitmap_filename(packfile);
419 fd = git_open(bitmap_name);
fff42755 420
6411cc08
TL
421 if (fd < 0) {
422 if (errno != ENOENT)
423 warning_errno("cannot open '%s'", bitmap_name);
424 free(bitmap_name);
fff42755 425 return -1;
6411cc08
TL
426 }
427 free(bitmap_name);
fff42755
VM
428
429 if (fstat(fd, &st)) {
9005eb02 430 error_errno(_("cannot fstat bitmap file"));
fff42755
VM
431 close(fd);
432 return -1;
433 }
434
0f533c72 435 if (bitmap_git->pack || bitmap_git->midx) {
8ddc0663
TL
436 trace2_data_string("bitmap", the_repository,
437 "ignoring extra bitmap file", packfile->pack_name);
fff42755
VM
438 close(fd);
439 return -1;
440 }
441
dc1daacd
JK
442 if (!is_pack_valid(packfile)) {
443 close(fd);
444 return -1;
445 }
446
3ae5fa07
JT
447 bitmap_git->pack = packfile;
448 bitmap_git->map_size = xsize_t(st.st_size);
449 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
450 bitmap_git->map_pos = 0;
fff42755
VM
451 close(fd);
452
3ae5fa07
JT
453 if (load_bitmap_header(bitmap_git) < 0) {
454 munmap(bitmap_git->map, bitmap_git->map_size);
455 bitmap_git->map = NULL;
456 bitmap_git->map_size = 0;
f8b60cf9
TB
457 bitmap_git->map_pos = 0;
458 bitmap_git->pack = NULL;
fff42755
VM
459 return -1;
460 }
461
8ddc0663
TL
462 trace2_data_string("bitmap", the_repository, "opened bitmap file",
463 packfile->pack_name);
fff42755
VM
464 return 0;
465}
466
65308ad8 467static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
0f533c72
TB
468{
469 if (bitmap_is_midx(bitmap_git)) {
470 uint32_t i;
471 int ret;
472
473 /*
474 * The multi-pack-index's .rev file is already loaded via
475 * open_pack_bitmap_1().
476 *
477 * But we still need to open the individual pack .rev files,
478 * since we will need to make use of them in pack-objects.
479 */
480 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
65308ad8 481 ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
0f533c72
TB
482 if (ret)
483 return ret;
484 }
485 return 0;
486 }
65308ad8 487 return load_pack_revindex(r, bitmap_git->pack);
0f533c72
TB
488}
489
65308ad8 490static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
fff42755 491{
199c86be 492 assert(bitmap_git->map);
fff42755 493
3c771448 494 bitmap_git->bitmaps = kh_init_oid_map();
495 bitmap_git->ext_index.positions = kh_init_oid_pos();
0f533c72 496
65308ad8 497 if (load_reverse_index(r, bitmap_git))
4828ce98 498 goto failed;
fff42755 499
3ae5fa07
JT
500 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
501 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
502 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
503 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
fff42755
VM
504 goto failed;
505
28cd7306 506 if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
fff42755
VM
507 goto failed;
508
fff42755
VM
509 return 0;
510
511failed:
3ae5fa07
JT
512 munmap(bitmap_git->map, bitmap_git->map_size);
513 bitmap_git->map = NULL;
514 bitmap_git->map_size = 0;
bb514de3
JK
515
516 kh_destroy_oid_map(bitmap_git->bitmaps);
517 bitmap_git->bitmaps = NULL;
518
519 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
520 bitmap_git->ext_index.positions = NULL;
521
fff42755
VM
522 return -1;
523}
524
7c141127
NTND
525static int open_pack_bitmap(struct repository *r,
526 struct bitmap_index *bitmap_git)
fff42755
VM
527{
528 struct packed_git *p;
529 int ret = -1;
530
7c141127 531 for (p = get_all_packs(r); p; p = p->next) {
833f4c05 532 if (open_pack_bitmap_1(bitmap_git, p) == 0) {
fff42755 533 ret = 0;
833f4c05
JK
534 /*
535 * The only reason to keep looking is to report
536 * duplicates.
537 */
538 if (!trace2_is_enabled())
539 break;
540 }
fff42755
VM
541 }
542
543 return ret;
544}
545
0f533c72
TB
546static int open_midx_bitmap(struct repository *r,
547 struct bitmap_index *bitmap_git)
548{
5dcee7c7 549 int ret = -1;
0f533c72
TB
550 struct multi_pack_index *midx;
551
552 assert(!bitmap_git->map);
553
554 for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
555 if (!open_midx_bitmap_1(bitmap_git, midx))
5dcee7c7 556 ret = 0;
0f533c72 557 }
5dcee7c7 558 return ret;
0f533c72
TB
559}
560
561static int open_bitmap(struct repository *r,
562 struct bitmap_index *bitmap_git)
563{
c8f43570
JK
564 int found;
565
0f533c72
TB
566 assert(!bitmap_git->map);
567
c8f43570
JK
568 found = !open_midx_bitmap(r, bitmap_git);
569
570 /*
571 * these will all be skipped if we opened a midx bitmap; but run it
572 * anyway if tracing is enabled to report the duplicates
573 */
574 if (!found || trace2_is_enabled())
575 found |= !open_pack_bitmap(r, bitmap_git);
576
577 return found ? 0 : -1;
0f533c72
TB
578}
579
7c141127 580struct bitmap_index *prepare_bitmap_git(struct repository *r)
fff42755 581{
3ae5fa07 582 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
fff42755 583
65308ad8 584 if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
0f533c72
TB
585 return bitmap_git;
586
587 free_bitmap_index(bitmap_git);
588 return NULL;
589}
590
bfbb60d3 591struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
0f533c72 592{
65308ad8 593 struct repository *r = the_repository;
0f533c72
TB
594 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
595
65308ad8 596 if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
3ae5fa07 597 return bitmap_git;
fff42755 598
f3c23db2 599 free_bitmap_index(bitmap_git);
3ae5fa07 600 return NULL;
fff42755
VM
601}
602
603struct include_data {
3ae5fa07 604 struct bitmap_index *bitmap_git;
fff42755
VM
605 struct bitmap *base;
606 struct bitmap *seen;
607};
608
28cd7306
AC
609struct bitmap_lookup_table_triplet {
610 uint32_t commit_pos;
611 uint64_t offset;
612 uint32_t xor_row;
613};
614
615struct bitmap_lookup_table_xor_item {
616 struct object_id oid;
617 uint64_t offset;
618};
619
620/*
621 * Given a `triplet` struct pointer and pointer `p`, this
622 * function reads the triplet beginning at `p` into the struct.
623 * Note that this function assumes that there is enough memory
624 * left for filling the `triplet` struct from `p`.
625 */
626static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
627 const unsigned char *p)
628{
629 if (!triplet)
630 return -1;
631
632 triplet->commit_pos = get_be32(p);
633 p += sizeof(uint32_t);
634 triplet->offset = get_be64(p);
635 p += sizeof(uint64_t);
636 triplet->xor_row = get_be32(p);
637 return 0;
638}
639
640/*
641 * This function gets the raw triplet from `row`'th row in the
642 * lookup table and fills that data to the `triplet`.
643 */
644static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
645 uint32_t pos,
646 struct bitmap_lookup_table_triplet *triplet)
647{
648 unsigned char *p = NULL;
649 if (pos >= bitmap_git->entry_count)
650 return error(_("corrupt bitmap lookup table: triplet position out of index"));
651
652 p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
653
654 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
655}
656
657/*
658 * Searches for a matching triplet. `commit_pos` is a pointer
659 * to the wanted commit position value. `table_entry` points to
660 * a triplet in lookup table. The first 4 bytes of each
661 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
662 */
663static int triplet_cmp(const void *commit_pos, const void *table_entry)
664{
665
666 uint32_t a = *(uint32_t *)commit_pos;
667 uint32_t b = get_be32(table_entry);
668 if (a > b)
669 return 1;
670 else if (a < b)
671 return -1;
672
673 return 0;
674}
675
676static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
677 struct object_id *oid,
678 uint32_t *result)
679{
680 int found;
681
682 if (bitmap_is_midx(bitmap_git))
683 found = bsearch_midx(oid, bitmap_git->midx, result);
684 else
685 found = bsearch_pack(oid, bitmap_git->pack, result);
686
687 return found;
688}
689
690/*
691 * `bsearch_triplet_by_pos` function searches for the raw triplet
692 * having commit position same as `commit_pos` and fills `triplet`
693 * object from the raw triplet. Returns 1 on success and 0 on
694 * failure.
695 */
696static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
697 struct bitmap_index *bitmap_git,
698 struct bitmap_lookup_table_triplet *triplet)
699{
700 unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
701 BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
702
703 if (!p)
704 return -1;
705
706 return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
707}
708
709static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
710 struct commit *commit)
711{
712 uint32_t commit_pos, xor_row;
713 uint64_t offset;
714 int flags;
715 struct bitmap_lookup_table_triplet triplet;
716 struct object_id *oid = &commit->object.oid;
717 struct ewah_bitmap *bitmap;
718 struct stored_bitmap *xor_bitmap = NULL;
719 const int bitmap_header_size = 6;
720 static struct bitmap_lookup_table_xor_item *xor_items = NULL;
721 static size_t xor_items_nr = 0, xor_items_alloc = 0;
722 static int is_corrupt = 0;
723 int xor_flags;
724 khiter_t hash_pos;
725 struct bitmap_lookup_table_xor_item *xor_item;
726
727 if (is_corrupt)
728 return NULL;
729
730 if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
731 return NULL;
732
733 if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
734 return NULL;
735
736 xor_items_nr = 0;
737 offset = triplet.offset;
738 xor_row = triplet.xor_row;
739
740 while (xor_row != 0xffffffff) {
741 ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
742
743 if (xor_items_nr + 1 >= bitmap_git->entry_count) {
711340c7 744 error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
28cd7306
AC
745 goto corrupt;
746 }
747
748 if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
749 goto corrupt;
750
751 xor_item = &xor_items[xor_items_nr];
752 xor_item->offset = triplet.offset;
753
754 if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
755 error(_("corrupt bitmap lookup table: commit index %u out of range"),
756 triplet.commit_pos);
757 goto corrupt;
758 }
759
760 hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
761
762 /*
763 * If desired bitmap is already stored, we don't need
764 * to iterate further. Because we know that bitmaps
765 * that are needed to be parsed to parse this bitmap
766 * has already been stored. So, assign this stored bitmap
767 * to the xor_bitmap.
768 */
769 if (hash_pos < kh_end(bitmap_git->bitmaps) &&
770 (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
771 break;
772 xor_items_nr++;
773 xor_row = triplet.xor_row;
774 }
775
776 while (xor_items_nr) {
777 xor_item = &xor_items[xor_items_nr - 1];
778 bitmap_git->map_pos = xor_item->offset;
779 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
780 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
781 oid_to_hex(&xor_item->oid));
782 goto corrupt;
783 }
784
785 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
786 xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
787 bitmap = read_bitmap_1(bitmap_git);
788
789 if (!bitmap)
790 goto corrupt;
791
792 xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
793 xor_items_nr--;
794 }
795
796 bitmap_git->map_pos = offset;
797 if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
798 error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
799 oid_to_hex(oid));
800 goto corrupt;
801 }
802
803 /*
804 * Don't bother reading the commit's index position or its xor
805 * offset:
806 *
807 * - The commit's index position is irrelevant to us, since
808 * load_bitmap_entries_v1 only uses it to learn the object
809 * id which is used to compute the hashmap's key. We already
810 * have an object id, so no need to look it up again.
811 *
812 * - The xor_offset is unusable for us, since it specifies how
813 * many entries previous to ours we should look at. This
814 * makes sense when reading the bitmaps sequentially (as in
815 * load_bitmap_entries_v1()), since we can keep track of
816 * each bitmap as we read them.
817 *
818 * But it can't work for us, since the bitmap's don't have a
819 * fixed size. So we learn the position of the xor'd bitmap
820 * from the commit table (and resolve it to a bitmap in the
821 * above if-statement).
822 *
823 * Instead, we can skip ahead and immediately read the flags and
824 * ewah bitmap.
825 */
826 bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
827 flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
828 bitmap = read_bitmap_1(bitmap_git);
829
830 if (!bitmap)
831 goto corrupt;
832
833 return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
834
835corrupt:
836 free(xor_items);
837 is_corrupt = 1;
838 return NULL;
839}
840
98c31f36
TB
841struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
842 struct commit *commit)
843{
844 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
845 commit->object.oid);
28cd7306
AC
846 if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
847 struct stored_bitmap *bitmap = NULL;
848 if (!bitmap_git->table_lookup)
849 return NULL;
850
89a1ab8f 851 /* this is a fairly hot codepath - no trace2_region please */
28cd7306
AC
852 /* NEEDSWORK: cache misses aren't recorded */
853 bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
28cd7306
AC
854 if (!bitmap)
855 return NULL;
856 return lookup_stored_bitmap(bitmap);
857 }
98c31f36
TB
858 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
859}
860
3ae5fa07 861static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
3c771448 862 const struct object_id *oid)
fff42755 863{
4ed43d16 864 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
3c771448 865 khiter_t pos = kh_get_oid_pos(positions, *oid);
fff42755
VM
866
867 if (pos < kh_end(positions)) {
868 int bitmap_pos = kh_value(positions, pos);
ed184620 869 return bitmap_pos + bitmap_num_objects(bitmap_git);
fff42755
VM
870 }
871
872 return -1;
873}
874
3ae5fa07 875static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
3c771448 876 const struct object_id *oid)
fff42755 877{
57665086 878 uint32_t pos;
3c771448 879 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
fff42755
VM
880 if (!offset)
881 return -1;
882
57665086
TB
883 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
884 return -1;
885 return pos;
fff42755
VM
886}
887
0f533c72
TB
888static int bitmap_position_midx(struct bitmap_index *bitmap_git,
889 const struct object_id *oid)
890{
891 uint32_t want, got;
892 if (!bsearch_midx(oid, bitmap_git->midx, &want))
893 return -1;
894
895 if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
896 return -1;
897 return got;
898}
899
3ae5fa07 900static int bitmap_position(struct bitmap_index *bitmap_git,
3c771448 901 const struct object_id *oid)
fff42755 902{
0f533c72
TB
903 int pos;
904 if (bitmap_is_midx(bitmap_git))
905 pos = bitmap_position_midx(bitmap_git, oid);
906 else
907 pos = bitmap_position_packfile(bitmap_git, oid);
3c771448 908 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
fff42755
VM
909}
910
3ae5fa07
JT
911static int ext_index_add_object(struct bitmap_index *bitmap_git,
912 struct object *object, const char *name)
fff42755 913{
3ae5fa07 914 struct eindex *eindex = &bitmap_git->ext_index;
fff42755
VM
915
916 khiter_t hash_pos;
917 int hash_ret;
918 int bitmap_pos;
919
3c771448 920 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
fff42755
VM
921 if (hash_ret > 0) {
922 if (eindex->count >= eindex->alloc) {
923 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
2756ca43
RS
924 REALLOC_ARRAY(eindex->objects, eindex->alloc);
925 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
fff42755
VM
926 }
927
928 bitmap_pos = eindex->count;
929 eindex->objects[eindex->count] = object;
930 eindex->hashes[eindex->count] = pack_name_hash(name);
931 kh_value(eindex->positions, hash_pos) = bitmap_pos;
932 eindex->count++;
933 } else {
934 bitmap_pos = kh_value(eindex->positions, hash_pos);
935 }
936
ed184620 937 return bitmap_pos + bitmap_num_objects(bitmap_git);
fff42755
VM
938}
939
3ae5fa07
JT
940struct bitmap_show_data {
941 struct bitmap_index *bitmap_git;
942 struct bitmap *base;
943};
944
945static void show_object(struct object *object, const char *name, void *data_)
fff42755 946{
3ae5fa07 947 struct bitmap_show_data *data = data_;
fff42755
VM
948 int bitmap_pos;
949
3c771448 950 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
fff42755 951
de1e67d0 952 if (bitmap_pos < 0)
3ae5fa07
JT
953 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
954 name);
fff42755 955
3ae5fa07 956 bitmap_set(data->base, bitmap_pos);
fff42755
VM
957}
958
c50dca2a
JK
959static void show_commit(struct commit *commit UNUSED,
960 void *data UNUSED)
fff42755
VM
961{
962}
963
3ae5fa07
JT
964static int add_to_include_set(struct bitmap_index *bitmap_git,
965 struct include_data *data,
98c31f36 966 struct commit *commit,
fff42755
VM
967 int bitmap_pos)
968{
98c31f36 969 struct ewah_bitmap *partial;
fff42755
VM
970
971 if (data->seen && bitmap_get(data->seen, bitmap_pos))
972 return 0;
973
974 if (bitmap_get(data->base, bitmap_pos))
975 return 0;
976
98c31f36
TB
977 partial = bitmap_for_commit(bitmap_git, commit);
978 if (partial) {
979 bitmap_or_ewah(data->base, partial);
fff42755
VM
980 return 0;
981 }
982
983 bitmap_set(data->base, bitmap_pos);
984 return 1;
985}
986
987static int should_include(struct commit *commit, void *_data)
988{
989 struct include_data *data = _data;
990 int bitmap_pos;
991
3c771448 992 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
fff42755 993 if (bitmap_pos < 0)
3ae5fa07
JT
994 bitmap_pos = ext_index_add_object(data->bitmap_git,
995 (struct object *)commit,
996 NULL);
fff42755 997
98c31f36 998 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
fff42755
VM
999 struct commit_list *parent = commit->parents;
1000
1001 while (parent) {
1002 parent->item->object.flags |= SEEN;
1003 parent = parent->next;
1004 }
1005
1006 return 0;
1007 }
1008
1009 return 1;
1010}
1011
aa9ad6fe
JK
1012static int should_include_obj(struct object *obj, void *_data)
1013{
1014 struct include_data *data = _data;
1015 int bitmap_pos;
1016
1017 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1018 if (bitmap_pos < 0)
1019 return 1;
1020 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1021 bitmap_get(data->base, bitmap_pos)) {
1022 obj->flags |= SEEN;
1023 return 0;
1024 }
1025 return 1;
1026}
1027
83578051
TB
1028static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1029 struct bitmap **base,
1030 struct commit *commit)
1031{
1032 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1033
1034 if (!or_with)
1035 return 0;
1036
afe8a907 1037 if (!*base)
83578051
TB
1038 *base = ewah_to_bitmap(or_with);
1039 else
1040 bitmap_or_ewah(*base, or_with);
1041
1042 return 1;
1043}
1044
47ff853f
TB
1045static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1046 struct rev_info *revs,
1047 struct bitmap *base,
1048 struct bitmap *seen)
1049{
1050 struct include_data incdata;
1051 struct bitmap_show_data show_data;
1052
1053 if (!base)
1054 base = bitmap_new();
1055
1056 incdata.bitmap_git = bitmap_git;
1057 incdata.base = base;
1058 incdata.seen = seen;
1059
1060 revs->include_check = should_include;
1061 revs->include_check_obj = should_include_obj;
1062 revs->include_check_data = &incdata;
1063
1064 if (prepare_revision_walk(revs))
1065 die(_("revision walk setup failed"));
1066
1067 show_data.bitmap_git = bitmap_git;
1068 show_data.base = base;
1069
1070 traverse_commit_list(revs, show_commit, show_object, &show_data);
1071
1072 revs->include_check = NULL;
1073 revs->include_check_obj = NULL;
1074 revs->include_check_data = NULL;
1075
1076 return base;
1077}
1078
b0afdce5
TB
1079struct bitmap_boundary_cb {
1080 struct bitmap_index *bitmap_git;
1081 struct bitmap *base;
1082
1083 struct object_array boundary;
1084};
1085
1086static void show_boundary_commit(struct commit *commit, void *_data)
1087{
1088 struct bitmap_boundary_cb *data = _data;
1089
1090 if (commit->object.flags & BOUNDARY)
1091 add_object_array(&commit->object, "", &data->boundary);
1092
1093 if (commit->object.flags & UNINTERESTING) {
1094 if (bitmap_walk_contains(data->bitmap_git, data->base,
1095 &commit->object.oid))
1096 return;
1097
1098 add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1099 }
1100}
1101
d79b9f7c
JK
1102static void show_boundary_object(struct object *object UNUSED,
1103 const char *name UNUSED,
1104 void *data UNUSED)
b0afdce5
TB
1105{
1106 BUG("should not be called");
1107}
1108
1109static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1110 struct rev_info *revs,
1111 struct object_list *roots)
1112{
1113 struct bitmap_boundary_cb cb;
1114 struct object_list *root;
1115 unsigned int i;
1116 unsigned int tmp_blobs, tmp_trees, tmp_tags;
1117 int any_missing = 0;
1118
1119 cb.bitmap_git = bitmap_git;
1120 cb.base = bitmap_new();
1121 object_array_init(&cb.boundary);
1122
1123 revs->ignore_missing_links = 1;
1124
1125 /*
1126 * OR in any existing reachability bitmaps among `roots` into
1127 * `cb.base`.
1128 */
1129 for (root = roots; root; root = root->next) {
1130 struct object *object = root->item;
1131 if (object->type != OBJ_COMMIT ||
1132 bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1133 continue;
1134
1135 if (add_commit_to_bitmap(bitmap_git, &cb.base,
1136 (struct commit *)object))
1137 continue;
1138
1139 any_missing = 1;
1140 }
1141
1142 if (!any_missing)
1143 goto cleanup;
1144
1145 tmp_blobs = revs->blob_objects;
1146 tmp_trees = revs->tree_objects;
1147 tmp_tags = revs->blob_objects;
1148 revs->blob_objects = 0;
1149 revs->tree_objects = 0;
1150 revs->tag_objects = 0;
1151
1152 /*
1153 * We didn't have complete coverage of the roots. First setup a
1154 * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1155 * between the tips and boundary, and (b) record the boundary.
1156 */
1157 trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1158 if (prepare_revision_walk(revs))
1159 die("revision walk setup failed");
1160 trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1161
1162 trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1163 revs->boundary = 1;
1164 traverse_commit_list_filtered(revs,
1165 show_boundary_commit,
1166 show_boundary_object,
1167 &cb, NULL);
1168 revs->boundary = 0;
1169 trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1170
1171 revs->blob_objects = tmp_blobs;
1172 revs->tree_objects = tmp_trees;
1173 revs->tag_objects = tmp_tags;
1174
1175 reset_revision_walk();
1176 clear_object_flags(UNINTERESTING);
1177
1178 /*
1179 * Then add the boundary commit(s) as fill-in traversal tips.
1180 */
1181 trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1182 for (i = 0; i < cb.boundary.nr; i++) {
1183 struct object *obj = cb.boundary.objects[i].item;
1184 if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1185 obj->flags |= SEEN;
1186 else
1187 add_pending_object(revs, obj, "");
1188 }
1189 if (revs->pending.nr)
1190 cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1191 trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1192
1193cleanup:
1194 object_array_clear(&cb.boundary);
1195 revs->ignore_missing_links = 0;
1196
1197 return cb.base;
1198}
1199
3ae5fa07
JT
1200static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1201 struct rev_info *revs,
fff42755 1202 struct object_list *roots,
09d4a79e 1203 struct bitmap *seen)
fff42755
VM
1204{
1205 struct bitmap *base = NULL;
1206 int needs_walk = 0;
1207
1208 struct object_list *not_mapped = NULL;
1209
1210 /*
1211 * Go through all the roots for the walk. The ones that have bitmaps
1212 * on the bitmap index will be `or`ed together to form an initial
1213 * global reachability analysis.
1214 *
1215 * The ones without bitmaps in the index will be stored in the
1216 * `not_mapped_list` for further processing.
1217 */
1218 while (roots) {
1219 struct object *object = roots->item;
1220 roots = roots->next;
1221
83578051
TB
1222 if (object->type == OBJ_COMMIT &&
1223 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1224 object->flags |= SEEN;
1225 continue;
fff42755
VM
1226 }
1227
1228 object_list_insert(object, &not_mapped);
1229 }
1230
1231 /*
1232 * Best case scenario: We found bitmaps for all the roots,
1233 * so the resulting `or` bitmap has the full reachability analysis
1234 */
afe8a907 1235 if (!not_mapped)
fff42755
VM
1236 return base;
1237
1238 roots = not_mapped;
1239
1240 /*
1241 * Let's iterate through all the roots that don't have bitmaps to
1242 * check if we can determine them to be reachable from the existing
1243 * global bitmap.
1244 *
1245 * If we cannot find them in the existing global bitmap, we'll need
1246 * to push them to an actual walk and run it until we can confirm
1247 * they are reachable
1248 */
1249 while (roots) {
1250 struct object *object = roots->item;
1251 int pos;
1252
1253 roots = roots->next;
3c771448 1254 pos = bitmap_position(bitmap_git, &object->oid);
fff42755
VM
1255
1256 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1257 object->flags &= ~UNINTERESTING;
1258 add_pending_object(revs, object, "");
1259 needs_walk = 1;
1260 } else {
1261 object->flags |= SEEN;
1262 }
1263 }
1264
1265 if (needs_walk) {
b0afdce5
TB
1266 /*
1267 * This fill-in traversal may walk over some objects
1268 * again, since we have already traversed in order to
1269 * find the boundary.
1270 *
1271 * But this extra walk should be extremely cheap, since
1272 * all commit objects are loaded into memory, and
1273 * because we skip walking to parents that are
1274 * UNINTERESTING, since it will be marked in the haves
1275 * bitmap already (or it has an on-disk bitmap, since
1276 * OR-ing it in covers all of its ancestors).
1277 */
47ff853f 1278 base = fill_in_bitmap(bitmap_git, revs, base, seen);
fff42755
VM
1279 }
1280
a96015a5
TB
1281 object_list_free(&not_mapped);
1282
fff42755
VM
1283 return base;
1284}
1285
3ae5fa07 1286static void show_extended_objects(struct bitmap_index *bitmap_git,
4eb707eb 1287 struct rev_info *revs,
fff42755
VM
1288 show_reachable_fn show_reach)
1289{
3ae5fa07
JT
1290 struct bitmap *objects = bitmap_git->result;
1291 struct eindex *eindex = &bitmap_git->ext_index;
fff42755
VM
1292 uint32_t i;
1293
1294 for (i = 0; i < eindex->count; ++i) {
1295 struct object *obj;
1296
0948c501 1297 if (!bitmap_get(objects, st_add(bitmap_num_objects(bitmap_git), i)))
fff42755
VM
1298 continue;
1299
1300 obj = eindex->objects[i];
4eb707eb
JK
1301 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1302 (obj->type == OBJ_TREE && !revs->tree_objects) ||
1303 (obj->type == OBJ_TAG && !revs->tag_objects))
1304 continue;
1305
20664967 1306 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
fff42755
VM
1307 }
1308}
1309
551cf8b6
JK
1310static void init_type_iterator(struct ewah_iterator *it,
1311 struct bitmap_index *bitmap_git,
1312 enum object_type type)
1313{
1314 switch (type) {
1315 case OBJ_COMMIT:
1316 ewah_iterator_init(it, bitmap_git->commits);
1317 break;
1318
1319 case OBJ_TREE:
1320 ewah_iterator_init(it, bitmap_git->trees);
1321 break;
1322
1323 case OBJ_BLOB:
1324 ewah_iterator_init(it, bitmap_git->blobs);
1325 break;
1326
1327 case OBJ_TAG:
1328 ewah_iterator_init(it, bitmap_git->tags);
1329 break;
1330
1331 default:
1332 BUG("object type %d not stored by bitmap type index", type);
1333 break;
1334 }
1335}
1336
fff42755 1337static void show_objects_for_type(
3ae5fa07 1338 struct bitmap_index *bitmap_git,
fff42755
VM
1339 enum object_type object_type,
1340 show_reachable_fn show_reach)
1341{
d2ea0310 1342 size_t i = 0;
fff42755
VM
1343 uint32_t offset;
1344
1345 struct ewah_iterator it;
1346 eword_t filter;
1347
3ae5fa07
JT
1348 struct bitmap *objects = bitmap_git->result;
1349
551cf8b6 1350 init_type_iterator(&it, bitmap_git, object_type);
fff42755 1351
d2ea0310
JK
1352 for (i = 0; i < objects->word_alloc &&
1353 ewah_iterator_next(&filter, &it); i++) {
fff42755 1354 eword_t word = objects->words[i] & filter;
d2ea0310
JK
1355 size_t pos = (i * BITS_IN_EWORD);
1356
1357 if (!word)
1358 continue;
fff42755 1359
34b935c0 1360 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
0f533c72 1361 struct packed_git *pack;
20664967 1362 struct object_id oid;
cf98f2e8
TB
1363 uint32_t hash = 0, index_pos;
1364 off_t ofs;
fff42755
VM
1365
1366 if ((word >> offset) == 0)
1367 break;
1368
1369 offset += ewah_bit_ctz64(word >> offset);
1370
0f533c72
TB
1371 if (bitmap_is_midx(bitmap_git)) {
1372 struct multi_pack_index *m = bitmap_git->midx;
1373 uint32_t pack_id;
1374
1375 index_pos = pack_pos_to_midx(m, pos + offset);
1376 ofs = nth_midxed_offset(m, index_pos);
1377 nth_midxed_object_oid(&oid, m, index_pos);
1378
1379 pack_id = nth_midxed_pack_int_id(m, index_pos);
1380 pack = bitmap_git->midx->packs[pack_id];
1381 } else {
1382 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1383 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1384 nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1385
1386 pack = bitmap_git->pack;
1387 }
fff42755 1388
3ae5fa07 1389 if (bitmap_git->hashes)
cf98f2e8 1390 hash = get_be32(bitmap_git->hashes + index_pos);
ae4f07fb 1391
0f533c72 1392 show_reach(&oid, object_type, 0, hash, pack, ofs);
fff42755 1393 }
fff42755
VM
1394 }
1395}
1396
3ae5fa07
JT
1397static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1398 struct object_list *roots)
fff42755
VM
1399{
1400 while (roots) {
1401 struct object *object = roots->item;
1402 roots = roots->next;
1403
0f533c72
TB
1404 if (bitmap_is_midx(bitmap_git)) {
1405 if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1406 return 1;
1407 } else {
1408 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1409 return 1;
1410 }
fff42755
VM
1411 }
1412
1413 return 0;
1414}
1415
856e12c1
TB
1416static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1417 struct object_list *tip_objects,
1418 enum object_type type)
4f3bd560
JK
1419{
1420 struct bitmap *result = bitmap_new();
1421 struct object_list *p;
1422
1423 for (p = tip_objects; p; p = p->next) {
1424 int pos;
1425
856e12c1 1426 if (p->item->type != type)
4f3bd560
JK
1427 continue;
1428
1429 pos = bitmap_position(bitmap_git, &p->item->oid);
1430 if (pos < 0)
1431 continue;
1432
1433 bitmap_set(result, pos);
1434 }
1435
1436 return result;
1437}
1438
856e12c1
TB
1439static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1440 struct object_list *tip_objects,
1441 struct bitmap *to_filter,
1442 enum object_type type)
4f3bd560
JK
1443{
1444 struct eindex *eindex = &bitmap_git->ext_index;
1445 struct bitmap *tips;
1446 struct ewah_iterator it;
1447 eword_t mask;
1448 uint32_t i;
1449
1450 /*
1451 * The non-bitmap version of this filter never removes
856e12c1 1452 * objects which the other side specifically asked for,
4f3bd560
JK
1453 * so we must match that behavior.
1454 */
856e12c1 1455 tips = find_tip_objects(bitmap_git, tip_objects, type);
4f3bd560
JK
1456
1457 /*
ddcb189d
TB
1458 * We can use the type-level bitmap for 'type' to work in whole
1459 * words for the objects that are actually in the bitmapped
1460 * packfile.
4f3bd560 1461 */
856e12c1 1462 for (i = 0, init_type_iterator(&it, bitmap_git, type);
4f3bd560
JK
1463 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1464 i++) {
1465 if (i < tips->word_alloc)
1466 mask &= ~tips->words[i];
1467 to_filter->words[i] &= ~mask;
1468 }
1469
1470 /*
ddcb189d
TB
1471 * Clear any objects that weren't in the packfile (and so would
1472 * not have been caught by the loop above. We'll have to check
1473 * them individually.
4f3bd560
JK
1474 */
1475 for (i = 0; i < eindex->count; i++) {
0948c501 1476 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
856e12c1 1477 if (eindex->objects[i]->type == type &&
4f3bd560
JK
1478 bitmap_get(to_filter, pos) &&
1479 !bitmap_get(tips, pos))
1480 bitmap_unset(to_filter, pos);
1481 }
1482
1483 bitmap_free(tips);
1484}
1485
856e12c1
TB
1486static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1487 struct object_list *tip_objects,
1488 struct bitmap *to_filter)
1489{
1490 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1491 OBJ_BLOB);
1492}
1493
84243da1
JK
1494static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1495 uint32_t pos)
1496{
84243da1
JK
1497 unsigned long size;
1498 struct object_info oi = OBJECT_INFO_INIT;
1499
1500 oi.sizep = &size;
1501
ed184620 1502 if (pos < bitmap_num_objects(bitmap_git)) {
0f533c72
TB
1503 struct packed_git *pack;
1504 off_t ofs;
1505
1506 if (bitmap_is_midx(bitmap_git)) {
1507 uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1508 uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1509
1510 pack = bitmap_git->midx->packs[pack_id];
1511 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1512 } else {
1513 pack = bitmap_git->pack;
1514 ofs = pack_pos_to_offset(pack, pos);
1515 }
1516
a78a9032 1517 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
84243da1 1518 struct object_id oid;
6b4277e6
TB
1519 nth_bitmap_object_oid(bitmap_git, &oid,
1520 pack_pos_to_index(pack, pos));
84243da1
JK
1521 die(_("unable to get size of %s"), oid_to_hex(&oid));
1522 }
1523 } else {
1524 struct eindex *eindex = &bitmap_git->ext_index;
ed184620 1525 struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
84243da1
JK
1526 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1527 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1528 }
1529
1530 return size;
1531}
1532
1533static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1534 struct object_list *tip_objects,
1535 struct bitmap *to_filter,
1536 unsigned long limit)
1537{
1538 struct eindex *eindex = &bitmap_git->ext_index;
1539 struct bitmap *tips;
1540 struct ewah_iterator it;
1541 eword_t mask;
1542 uint32_t i;
1543
856e12c1 1544 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
84243da1
JK
1545
1546 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1547 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1548 i++) {
1549 eword_t word = to_filter->words[i] & mask;
1550 unsigned offset;
1551
1552 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1553 uint32_t pos;
1554
1555 if ((word >> offset) == 0)
1556 break;
1557 offset += ewah_bit_ctz64(word >> offset);
1558 pos = i * BITS_IN_EWORD + offset;
1559
1560 if (!bitmap_get(tips, pos) &&
1561 get_size_by_pos(bitmap_git, pos) >= limit)
1562 bitmap_unset(to_filter, pos);
1563 }
1564 }
1565
1566 for (i = 0; i < eindex->count; i++) {
0948c501 1567 size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
84243da1
JK
1568 if (eindex->objects[i]->type == OBJ_BLOB &&
1569 bitmap_get(to_filter, pos) &&
1570 !bitmap_get(tips, pos) &&
1571 get_size_by_pos(bitmap_git, pos) >= limit)
1572 bitmap_unset(to_filter, pos);
1573 }
1574
1575 bitmap_free(tips);
1576}
1577
b0a8d482
TB
1578static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1579 struct object_list *tip_objects,
1580 struct bitmap *to_filter,
1581 unsigned long limit)
1582{
1583 if (limit)
1584 BUG("filter_bitmap_tree_depth given non-zero limit");
1585
1586 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1587 OBJ_TREE);
1588 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1589 OBJ_BLOB);
1590}
1591
7ab6aafa
PS
1592static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1593 struct object_list *tip_objects,
1594 struct bitmap *to_filter,
1595 enum object_type object_type)
1596{
1597 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1598 BUG("filter_bitmap_object_type given invalid object");
1599
1600 if (object_type != OBJ_TAG)
1601 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1602 if (object_type != OBJ_COMMIT)
1603 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1604 if (object_type != OBJ_TREE)
1605 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1606 if (object_type != OBJ_BLOB)
1607 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1608}
1609
6663ae0a
JK
1610static int filter_bitmap(struct bitmap_index *bitmap_git,
1611 struct object_list *tip_objects,
1612 struct bitmap *to_filter,
1613 struct list_objects_filter_options *filter)
1614{
1615 if (!filter || filter->choice == LOFC_DISABLED)
1616 return 0;
1617
4f3bd560
JK
1618 if (filter->choice == LOFC_BLOB_NONE) {
1619 if (bitmap_git)
1620 filter_bitmap_blob_none(bitmap_git, tip_objects,
1621 to_filter);
1622 return 0;
1623 }
1624
84243da1
JK
1625 if (filter->choice == LOFC_BLOB_LIMIT) {
1626 if (bitmap_git)
1627 filter_bitmap_blob_limit(bitmap_git, tip_objects,
1628 to_filter,
1629 filter->blob_limit_value);
1630 return 0;
1631 }
1632
b0a8d482
TB
1633 if (filter->choice == LOFC_TREE_DEPTH &&
1634 filter->tree_exclude_depth == 0) {
1635 if (bitmap_git)
1636 filter_bitmap_tree_depth(bitmap_git, tip_objects,
1637 to_filter,
1638 filter->tree_exclude_depth);
1639 return 0;
1640 }
1641
7ab6aafa
PS
1642 if (filter->choice == LOFC_OBJECT_TYPE) {
1643 if (bitmap_git)
1644 filter_bitmap_object_type(bitmap_git, tip_objects,
1645 to_filter,
1646 filter->object_type);
1647 return 0;
1648 }
1649
169a15eb
PS
1650 if (filter->choice == LOFC_COMBINE) {
1651 int i;
1652 for (i = 0; i < filter->sub_nr; i++) {
1653 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1654 &filter->sub[i]) < 0)
1655 return -1;
1656 }
1657 return 0;
1658 }
1659
6663ae0a
JK
1660 /* filter choice not handled */
1661 return -1;
1662}
1663
1664static int can_filter_bitmap(struct list_objects_filter_options *filter)
1665{
1666 return !filter_bitmap(NULL, NULL, NULL, filter);
1667}
1668
7b3c8e9f
TB
1669
1670static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
1671 struct bitmap *result)
1672{
1673 struct eindex *eindex = &bitmap_git->ext_index;
1674 uint32_t objects_nr;
1675 size_t i, pos;
1676
1677 objects_nr = bitmap_num_objects(bitmap_git);
1678 pos = objects_nr / BITS_IN_EWORD;
1679
1680 if (pos > result->word_alloc)
1681 pos = result->word_alloc;
1682
1683 memset(result->words, 0x00, sizeof(eword_t) * pos);
1684 for (i = pos * BITS_IN_EWORD; i < objects_nr; i++)
1685 bitmap_unset(result, i);
1686
1687 for (i = 0; i < eindex->count; ++i) {
1688 if (has_object_pack(&eindex->objects[i]->oid))
1689 bitmap_unset(result, objects_nr + i);
1690 }
1691}
1692
6663ae0a 1693struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
9cf68b27 1694 int filter_provided_objects)
fff42755
VM
1695{
1696 unsigned int i;
b0afdce5 1697 int use_boundary_traversal;
fff42755
VM
1698
1699 struct object_list *wants = NULL;
1700 struct object_list *haves = NULL;
1701
1702 struct bitmap *wants_bitmap = NULL;
1703 struct bitmap *haves_bitmap = NULL;
1704
d90fe06e
JK
1705 struct bitmap_index *bitmap_git;
1706
1707 /*
1708 * We can't do pathspec limiting with bitmaps, because we don't know
1709 * which commits are associated with which object changes (let alone
1710 * even which objects are associated with which paths).
1711 */
1712 if (revs->prune)
1713 return NULL;
1714
09d4a79e 1715 if (!can_filter_bitmap(&revs->filter))
6663ae0a
JK
1716 return NULL;
1717
3ae5fa07
JT
1718 /* try to open a bitmapped pack, but don't parse it yet
1719 * because we may not need to use it */
ca56dadb 1720 CALLOC_ARRAY(bitmap_git, 1);
0f533c72 1721 if (open_bitmap(revs->repo, bitmap_git) < 0)
f3c23db2 1722 goto cleanup;
fff42755 1723
4d01a7fa
1724 for (i = 0; i < revs->pending.nr; ++i) {
1725 struct object *object = revs->pending.objects[i].item;
fff42755
VM
1726
1727 if (object->type == OBJ_NONE)
c251c83d 1728 parse_object_or_die(&object->oid, NULL);
fff42755
VM
1729
1730 while (object->type == OBJ_TAG) {
1731 struct tag *tag = (struct tag *) object;
1732
1733 if (object->flags & UNINTERESTING)
1734 object_list_insert(object, &haves);
1735 else
1736 object_list_insert(object, &wants);
1737
dad3f060 1738 object = parse_object_or_die(get_tagged_oid(tag), NULL);
540cdc11 1739 object->flags |= (tag->object.flags & UNINTERESTING);
fff42755
VM
1740 }
1741
1742 if (object->flags & UNINTERESTING)
1743 object_list_insert(object, &haves);
1744 else
1745 object_list_insert(object, &wants);
1746 }
1747
b0afdce5
TB
1748 use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1749 if (use_boundary_traversal < 0) {
1750 prepare_repo_settings(revs->repo);
1751 use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1752 }
1753
1754 if (!use_boundary_traversal) {
1755 /*
1756 * if we have a HAVES list, but none of those haves is contained
1757 * in the packfile that has a bitmap, we don't have anything to
1758 * optimize here
1759 */
1760 if (haves && !in_bitmapped_pack(bitmap_git, haves))
1761 goto cleanup;
1762 }
fff42755
VM
1763
1764 /* if we don't want anything, we're done here */
1765 if (!wants)
f3c23db2 1766 goto cleanup;
fff42755
VM
1767
1768 /*
1769 * now we're going to use bitmaps, so load the actual bitmap entries
1770 * from disk. this is the point of no return; after this the rev_list
1771 * becomes invalidated and we must perform the revwalk through bitmaps
1772 */
65308ad8 1773 if (load_bitmap(revs->repo, bitmap_git) < 0)
f3c23db2 1774 goto cleanup;
fff42755 1775
b0afdce5
TB
1776 if (!use_boundary_traversal)
1777 object_array_clear(&revs->pending);
fff42755
VM
1778
1779 if (haves) {
b0afdce5
TB
1780 if (use_boundary_traversal) {
1781 trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1782 haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1783 trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1784 } else {
1785 trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1786 revs->ignore_missing_links = 1;
1787 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1788 reset_revision_walk();
1789 revs->ignore_missing_links = 0;
1790 trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1791 }
fff42755 1792
afe8a907 1793 if (!haves_bitmap)
033abf97 1794 BUG("failed to perform bitmap walk");
fff42755
VM
1795 }
1796
b0afdce5
TB
1797 if (use_boundary_traversal) {
1798 object_array_clear(&revs->pending);
1799 reset_revision_walk();
1800 }
1801
09d4a79e 1802 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
fff42755
VM
1803
1804 if (!wants_bitmap)
033abf97 1805 BUG("failed to perform bitmap walk");
fff42755
VM
1806
1807 if (haves_bitmap)
1808 bitmap_and_not(wants_bitmap, haves_bitmap);
1809
09d4a79e
DS
1810 filter_bitmap(bitmap_git,
1811 (revs->filter.choice && filter_provided_objects) ? NULL : wants,
1812 wants_bitmap,
1813 &revs->filter);
6663ae0a 1814
7b3c8e9f
TB
1815 if (revs->unpacked)
1816 filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap);
1817
3ae5fa07 1818 bitmap_git->result = wants_bitmap;
30cdc33f 1819 bitmap_git->haves = haves_bitmap;
fff42755 1820
acac50dd
JK
1821 object_list_free(&wants);
1822 object_list_free(&haves);
1823
3ae5fa07 1824 return bitmap_git;
f3c23db2
JT
1825
1826cleanup:
1827 free_bitmap_index(bitmap_git);
acac50dd
JK
1828 object_list_free(&wants);
1829 object_list_free(&haves);
f3c23db2 1830 return NULL;
fff42755
VM
1831}
1832
a5f9f24a
TB
1833/*
1834 * -1 means "stop trying further objects"; 0 means we may or may not have
1835 * reused, but you can keep feeding bits.
1836 */
519e17ff
TB
1837static int try_partial_reuse(struct bitmap_index *bitmap_git,
1838 struct bitmapped_pack *pack,
1839 size_t bitmap_pos,
1840 uint32_t pack_pos,
a5f9f24a
TB
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
519e17ff
TB
1848 if (pack_pos >= pack->p->num_objects)
1849 return -1; /* not actually in the pack */
0f533c72 1850
519e17ff 1851 offset = delta_obj_offset = pack_pos_to_offset(pack->p, pack_pos);
dab60934 1852 type = unpack_object_header(pack->p, w_curs, &offset, &size);
bb514de3 1853 if (type < 0)
a5f9f24a 1854 return -1; /* broken packfile, punt */
bb514de3
JK
1855
1856 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1857 off_t base_offset;
011f3fd5 1858 uint32_t base_pos;
519e17ff 1859 uint32_t base_bitmap_pos;
bb514de3
JK
1860
1861 /*
1862 * Find the position of the base object so we can look it up
1863 * in our bitmaps. If we can't come up with an offset, or if
1864 * that offset is not in the revidx, the pack is corrupt.
1865 * There's nothing we can do, so just punt on this object,
1866 * and the normal slow path will complain about it in
1867 * more detail.
1868 */
dab60934 1869 base_offset = get_delta_base(pack->p, w_curs, &offset, type,
0f533c72 1870 delta_obj_offset);
bb514de3 1871 if (!base_offset)
a5f9f24a 1872 return 0;
bb514de3 1873
519e17ff
TB
1874 offset_to_pack_pos(pack->p, base_offset, &base_pos);
1875
1876 if (bitmap_is_midx(bitmap_git)) {
1877 /*
1878 * Cross-pack deltas are rejected for now, but could
1879 * theoretically be supported in the future.
1880 *
1881 * We would need to ensure that we're sending both
1882 * halves of the delta/base pair, regardless of whether
1883 * or not the two cross a pack boundary. If they do,
1884 * then we must convert the delta to an REF_DELTA to
1885 * refer back to the base in the other pack.
1886 * */
1887 if (midx_pair_to_pack_pos(bitmap_git->midx,
1888 pack->pack_int_id,
1889 base_offset,
1890 &base_bitmap_pos) < 0) {
1891 return 0;
1892 }
1893 } else {
1894 if (offset_to_pack_pos(pack->p, base_offset,
1895 &base_pos) < 0)
1896 return 0;
1897 /*
1898 * We assume delta dependencies always point backwards.
1899 * This lets us do a single pass, and is basically
1900 * always true due to the way OFS_DELTAs work. You would
1901 * not typically find REF_DELTA in a bitmapped pack,
1902 * since we only bitmap packs we write fresh, and
1903 * OFS_DELTA is the default). But let's double check to
1904 * make sure the pack wasn't written with odd
1905 * parameters.
1906 */
1907 if (base_pos >= pack_pos)
1908 return 0;
1909 base_bitmap_pos = pack->bitmap_pos + base_pos;
1910 }
bb514de3
JK
1911
1912 /*
1913 * And finally, if we're not sending the base as part of our
1914 * reuse chunk, then don't send this object either. The base
1915 * would come after us, along with other objects not
1916 * necessarily in the pack, which means we'd need to convert
1917 * to REF_DELTA on the fly. Better to just let the normal
1918 * object_entry code path handle it.
1919 */
519e17ff 1920 if (!bitmap_get(reuse, base_bitmap_pos))
a5f9f24a 1921 return 0;
bb514de3
JK
1922 }
1923
fff42755 1924 /*
bb514de3 1925 * If we got here, then the object is OK to reuse. Mark it.
fff42755 1926 */
519e17ff 1927 bitmap_set(reuse, bitmap_pos);
a5f9f24a 1928 return 0;
bb514de3 1929}
fff42755 1930
dab60934
TB
1931static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
1932 struct bitmapped_pack *pack,
1933 struct bitmap *reuse)
bb514de3 1934{
3ae5fa07 1935 struct bitmap *result = bitmap_git->result;
bb514de3 1936 struct pack_window *w_curs = NULL;
519e17ff 1937 size_t pos = pack->bitmap_pos / BITS_IN_EWORD;
fff42755 1938
519e17ff
TB
1939 if (!pack->bitmap_pos) {
1940 /*
1941 * If we're processing the first (in the case of a MIDX, the
1942 * preferred pack) or the only (in the case of single-pack
1943 * bitmaps) pack, then we can reuse whole words at a time.
1944 *
1945 * This is because we know that any deltas in this range *must*
1946 * have their bases chosen from the same pack, since:
1947 *
1948 * - In the single pack case, there is no other pack to choose
1949 * them from.
1950 *
1951 * - In the MIDX case, the first pack is the preferred pack, so
1952 * all ties are broken in favor of that pack (i.e. the one
1953 * we're currently processing). So any duplicate bases will be
1954 * resolved in favor of the pack we're processing.
1955 */
1956 while (pos < result->word_alloc &&
1957 pos < pack->bitmap_nr / BITS_IN_EWORD &&
1958 result->words[pos] == (eword_t)~0)
1959 pos++;
1960 memset(reuse->words, 0xFF, pos * sizeof(eword_t));
1961 }
fff42755 1962
519e17ff
TB
1963 for (; pos < result->word_alloc; pos++) {
1964 eword_t word = result->words[pos];
dab60934 1965 size_t offset;
fff42755 1966
519e17ff
TB
1967 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1968 size_t bit_pos;
1969 uint32_t pack_pos;
1970
1971 if (word >> offset == 0)
bb514de3 1972 break;
fff42755 1973
bb514de3 1974 offset += ewah_bit_ctz64(word >> offset);
519e17ff
TB
1975
1976 bit_pos = pos * BITS_IN_EWORD + offset;
1977 if (bit_pos < pack->bitmap_pos)
1978 continue;
1979 if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
1980 goto done;
1981
1982 if (bitmap_is_midx(bitmap_git)) {
1983 uint32_t midx_pos;
1984 off_t ofs;
1985
1986 midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
1987 ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1988
1989 if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
1990 BUG("could not find object in pack %s "
1991 "at offset %"PRIuMAX" in MIDX",
1992 pack_basename(pack->p), (uintmax_t)ofs);
1993 } else {
1994 pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
1995 if (pack_pos >= pack->p->num_objects)
1996 BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
1997 pack_basename(pack->p), (uintmax_t)pack_pos,
1998 pack->p->num_objects);
1999 }
2000
2001 if (try_partial_reuse(bitmap_git, pack, bit_pos,
2002 pack_pos, reuse, &w_curs) < 0) {
a5f9f24a
TB
2003 /*
2004 * try_partial_reuse indicated we couldn't reuse
2005 * any bits, so there is no point in trying more
2006 * bits in the current word, or any other words
2007 * in result.
2008 *
2009 * Jump out of both loops to avoid future
2010 * unnecessary calls to try_partial_reuse.
2011 */
2012 goto done;
2013 }
bb514de3 2014 }
fff42755 2015 }
fff42755 2016
a5f9f24a 2017done:
bb514de3 2018 unuse_pack(&w_curs);
dab60934
TB
2019}
2020
2021static int bitmapped_pack_cmp(const void *va, const void *vb)
2022{
2023 const struct bitmapped_pack *a = va;
2024 const struct bitmapped_pack *b = vb;
2025
2026 if (a->bitmap_pos < b->bitmap_pos)
2027 return -1;
2028 if (a->bitmap_pos > b->bitmap_pos)
2029 return 1;
2030 return 0;
2031}
2032
35e156b9 2033void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
83296d20
TB
2034 struct bitmapped_pack **packs_out,
2035 size_t *packs_nr_out,
af626ac0
TB
2036 struct bitmap **reuse_out,
2037 int multi_pack_reuse)
dab60934
TB
2038{
2039 struct repository *r = the_repository;
2040 struct bitmapped_pack *packs = NULL;
2041 struct bitmap *result = bitmap_git->result;
2042 struct bitmap *reuse;
2043 size_t i;
2044 size_t packs_nr = 0, packs_alloc = 0;
2045 size_t word_alloc;
2046 uint32_t objects_nr = 0;
2047
2048 assert(result);
2049
2050 load_reverse_index(r, bitmap_git);
2051
2052 if (bitmap_is_midx(bitmap_git)) {
2053 for (i = 0; i < bitmap_git->midx->num_packs; i++) {
2054 struct bitmapped_pack pack;
2055 if (nth_bitmapped_pack(r, bitmap_git->midx, &pack, i) < 0) {
2056 warning(_("unable to load pack: '%s', disabling pack-reuse"),
2057 bitmap_git->midx->pack_names[i]);
2058 free(packs);
35e156b9 2059 return;
dab60934 2060 }
af626ac0 2061
dab60934 2062 if (!pack.bitmap_nr)
af626ac0
TB
2063 continue;
2064
2065 if (!multi_pack_reuse && pack.bitmap_pos) {
2066 /*
2067 * If we're only reusing a single pack, skip
2068 * over any packs which are not positioned at
2069 * the beginning of the MIDX bitmap.
2070 *
2071 * This is consistent with the existing
2072 * single-pack reuse behavior, which only reuses
2073 * parts of the MIDX's preferred pack.
2074 */
2075 continue;
2076 }
dab60934
TB
2077
2078 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2079 memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2080
2081 objects_nr += pack.p->num_objects;
af626ac0
TB
2082
2083 if (!multi_pack_reuse)
2084 break;
dab60934
TB
2085 }
2086
2087 QSORT(packs, packs_nr, bitmapped_pack_cmp);
2088 } else {
2089 ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2090
2091 packs[packs_nr].p = bitmap_git->pack;
dab60934 2092 packs[packs_nr].bitmap_nr = bitmap_git->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}