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