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