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