]> git.ipfire.org Git - thirdparty/git.git/blame - pack-bitmap.c
Git 2.33
[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"
3f267a11 16#include "config.h"
fff42755
VM
17
18/*
19 * An entry on the bitmap index, representing the bitmap for a given
20 * commit.
21 */
22struct stored_bitmap {
53636539 23 struct object_id oid;
fff42755
VM
24 struct ewah_bitmap *root;
25 struct stored_bitmap *xor;
26 int flags;
27};
28
29/*
3ae5fa07 30 * The active bitmap index for a repository. By design, repositories only have
fff42755
VM
31 * a single bitmap index available (the index for the biggest packfile in
32 * the repository), since bitmap indexes need full closure.
33 *
34 * If there is more than one bitmap index available (e.g. because of alternates),
35 * the active bitmap index is the largest one.
36 */
3ae5fa07 37struct bitmap_index {
fff42755
VM
38 /* Packfile to which this bitmap index belongs to */
39 struct packed_git *pack;
40
fff42755
VM
41 /*
42 * Mark the first `reuse_objects` in the packfile as reused:
43 * they will be sent as-is without using them for repacking
44 * calculations
45 */
46 uint32_t reuse_objects;
47
48 /* mmapped buffer of the whole bitmap index */
49 unsigned char *map;
50 size_t map_size; /* size of the mmaped buffer */
51 size_t map_pos; /* current position when loading the index */
52
53 /*
54 * Type indexes.
55 *
56 * Each bitmap marks which objects in the packfile are of the given
57 * type. This provides type information when yielding the objects from
58 * the packfile during a walk, which allows for better delta bases.
59 */
60 struct ewah_bitmap *commits;
61 struct ewah_bitmap *trees;
62 struct ewah_bitmap *blobs;
63 struct ewah_bitmap *tags;
64
3c771448 65 /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
66 kh_oid_map_t *bitmaps;
fff42755
VM
67
68 /* Number of bitmapped commits */
69 uint32_t entry_count;
70
f3c23db2 71 /* If not NULL, this is a name-hash cache pointing into map. */
ae4f07fb
VM
72 uint32_t *hashes;
73
fff42755
VM
74 /*
75 * Extended index.
76 *
77 * When trying to perform bitmap operations with objects that are not
78 * packed in `pack`, these objects are added to this "fake index" and
79 * are assumed to appear at the end of the packfile for all operations
80 */
81 struct eindex {
82 struct object **objects;
83 uint32_t *hashes;
84 uint32_t count, alloc;
3c771448 85 kh_oid_pos_t *positions;
fff42755
VM
86 } ext_index;
87
88 /* Bitmap result of the last performed walk */
89 struct bitmap *result;
90
30cdc33f
JK
91 /* "have" bitmap from the last performed walk */
92 struct bitmap *haves;
93
fff42755
VM
94 /* Version of the bitmap index */
95 unsigned int version;
3ae5fa07 96};
fff42755
VM
97
98static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
99{
100 struct ewah_bitmap *parent;
101 struct ewah_bitmap *composed;
102
103 if (st->xor == NULL)
104 return st->root;
105
106 composed = ewah_pool_new();
107 parent = lookup_stored_bitmap(st->xor);
108 ewah_xor(st->root, parent, composed);
109
110 ewah_pool_free(st->root);
111 st->root = composed;
112 st->xor = NULL;
113
114 return composed;
115}
116
117/*
118 * Read a bitmap from the current read position on the mmaped
119 * index, and increase the read position accordingly
120 */
121static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
122{
123 struct ewah_bitmap *b = ewah_pool_new();
124
1140bf01 125 ssize_t bitmap_size = ewah_read_mmap(b,
fff42755
VM
126 index->map + index->map_pos,
127 index->map_size - index->map_pos);
128
129 if (bitmap_size < 0) {
130 error("Failed to load bitmap index (corrupted?)");
131 ewah_pool_free(b);
132 return NULL;
133 }
134
135 index->map_pos += bitmap_size;
136 return b;
137}
138
139static int load_bitmap_header(struct bitmap_index *index)
140{
141 struct bitmap_disk_header *header = (void *)index->map;
ca510902 142 size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
fff42755 143
ca510902
JK
144 if (index->map_size < header_size + the_hash_algo->rawsz)
145 return error("Corrupted bitmap index (too small)");
fff42755
VM
146
147 if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
148 return error("Corrupted bitmap index file (wrong header)");
149
150 index->version = ntohs(header->version);
151 if (index->version != 1)
152 return error("Unsupported version for bitmap index file (%d)", index->version);
153
154 /* Parse known bitmap format options */
155 {
156 uint32_t flags = ntohs(header->options);
ec6c7b43
JK
157 size_t cache_size = st_mult(index->pack->num_objects, sizeof(uint32_t));
158 unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
fff42755
VM
159
160 if ((flags & BITMAP_OPT_FULL_DAG) == 0)
161 return error("Unsupported options for bitmap index file "
162 "(Git requires BITMAP_OPT_FULL_DAG)");
ae4f07fb
VM
163
164 if (flags & BITMAP_OPT_HASH_CACHE) {
ec6c7b43
JK
165 if (cache_size > index_end - index->map - header_size)
166 return error("corrupted bitmap index file (too short to fit hash cache)");
167 index->hashes = (void *)(index_end - cache_size);
168 index_end -= cache_size;
ae4f07fb 169 }
fff42755
VM
170 }
171
172 index->entry_count = ntohl(header->entry_count);
ca510902 173 index->map_pos += header_size;
fff42755
VM
174 return 0;
175}
176
177static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
178 struct ewah_bitmap *root,
500e4f23 179 const struct object_id *oid,
fff42755
VM
180 struct stored_bitmap *xor_with,
181 int flags)
182{
183 struct stored_bitmap *stored;
184 khiter_t hash_pos;
185 int ret;
186
187 stored = xmalloc(sizeof(struct stored_bitmap));
188 stored->root = root;
189 stored->xor = xor_with;
190 stored->flags = flags;
500e4f23 191 oidcpy(&stored->oid, oid);
fff42755 192
3c771448 193 hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
fff42755
VM
194
195 /* a 0 return code means the insertion succeeded with no changes,
196 * because the SHA1 already existed on the map. this is bad, there
197 * shouldn't be duplicated commits in the index */
198 if (ret == 0) {
500e4f23 199 error("Duplicate entry in bitmap index: %s", oid_to_hex(oid));
fff42755
VM
200 return NULL;
201 }
202
203 kh_value(index->bitmaps, hash_pos) = stored;
204 return stored;
205}
206
b5007211
KB
207static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
208{
209 uint32_t result = get_be32(buffer + *pos);
210 (*pos) += sizeof(result);
211 return result;
212}
213
214static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
215{
216 return buffer[(*pos)++];
217}
218
599dc766
RS
219#define MAX_XOR_OFFSET 160
220
fff42755
VM
221static int load_bitmap_entries_v1(struct bitmap_index *index)
222{
fff42755 223 uint32_t i;
599dc766 224 struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
fff42755
VM
225
226 for (i = 0; i < index->entry_count; ++i) {
227 int xor_offset, flags;
228 struct ewah_bitmap *bitmap = NULL;
229 struct stored_bitmap *xor_bitmap = NULL;
230 uint32_t commit_idx_pos;
500e4f23 231 struct object_id oid;
fff42755 232
c6b0c391
TB
233 if (index->map_size - index->map_pos < 6)
234 return error("corrupt ewah bitmap: truncated header for entry %d", i);
235
b5007211
KB
236 commit_idx_pos = read_be32(index->map, &index->map_pos);
237 xor_offset = read_u8(index->map, &index->map_pos);
238 flags = read_u8(index->map, &index->map_pos);
fff42755 239
c6b0c391
TB
240 if (nth_packed_object_id(&oid, index->pack, commit_idx_pos) < 0)
241 return error("corrupt ewah bitmap: commit index %u out of range",
242 (unsigned)commit_idx_pos);
fff42755 243
fff42755
VM
244 bitmap = read_bitmap_1(index);
245 if (!bitmap)
246 return -1;
247
248 if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
249 return error("Corrupted bitmap pack index");
250
251 if (xor_offset > 0) {
252 xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
253
254 if (xor_bitmap == NULL)
255 return error("Invalid XOR offset in bitmap pack index");
256 }
257
258 recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
500e4f23 259 index, bitmap, &oid, xor_bitmap, flags);
fff42755
VM
260 }
261
262 return 0;
263}
264
cb468050
JH
265static char *pack_bitmap_filename(struct packed_git *p)
266{
9ae97018 267 size_t len;
cb468050 268
9ae97018 269 if (!strip_suffix(p->pack_name, ".pack", &len))
033abf97 270 BUG("pack_name does not end in .pack");
9ae97018 271 return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
cb468050
JH
272}
273
3ae5fa07 274static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
fff42755
VM
275{
276 int fd;
277 struct stat st;
278 char *idx_name;
279
280 if (open_pack_index(packfile))
281 return -1;
282
283 idx_name = pack_bitmap_filename(packfile);
a5436b57 284 fd = git_open(idx_name);
fff42755
VM
285 free(idx_name);
286
287 if (fd < 0)
288 return -1;
289
290 if (fstat(fd, &st)) {
291 close(fd);
292 return -1;
293 }
294
3ae5fa07 295 if (bitmap_git->pack) {
fff42755
VM
296 warning("ignoring extra bitmap file: %s", packfile->pack_name);
297 close(fd);
298 return -1;
299 }
300
dc1daacd
JK
301 if (!is_pack_valid(packfile)) {
302 close(fd);
303 return -1;
304 }
305
3ae5fa07
JT
306 bitmap_git->pack = packfile;
307 bitmap_git->map_size = xsize_t(st.st_size);
308 bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
309 bitmap_git->map_pos = 0;
fff42755
VM
310 close(fd);
311
3ae5fa07
JT
312 if (load_bitmap_header(bitmap_git) < 0) {
313 munmap(bitmap_git->map, bitmap_git->map_size);
314 bitmap_git->map = NULL;
315 bitmap_git->map_size = 0;
fff42755
VM
316 return -1;
317 }
318
319 return 0;
320}
321
3ae5fa07 322static int load_pack_bitmap(struct bitmap_index *bitmap_git)
fff42755 323{
199c86be 324 assert(bitmap_git->map);
fff42755 325
3c771448 326 bitmap_git->bitmaps = kh_init_oid_map();
327 bitmap_git->ext_index.positions = kh_init_oid_pos();
4828ce98
JK
328 if (load_pack_revindex(bitmap_git->pack))
329 goto failed;
fff42755 330
3ae5fa07
JT
331 if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
332 !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
333 !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
334 !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
fff42755
VM
335 goto failed;
336
3ae5fa07 337 if (load_bitmap_entries_v1(bitmap_git) < 0)
fff42755
VM
338 goto failed;
339
fff42755
VM
340 return 0;
341
342failed:
3ae5fa07
JT
343 munmap(bitmap_git->map, bitmap_git->map_size);
344 bitmap_git->map = NULL;
345 bitmap_git->map_size = 0;
bb514de3
JK
346
347 kh_destroy_oid_map(bitmap_git->bitmaps);
348 bitmap_git->bitmaps = NULL;
349
350 kh_destroy_oid_pos(bitmap_git->ext_index.positions);
351 bitmap_git->ext_index.positions = NULL;
352
fff42755
VM
353 return -1;
354}
355
7c141127
NTND
356static int open_pack_bitmap(struct repository *r,
357 struct bitmap_index *bitmap_git)
fff42755
VM
358{
359 struct packed_git *p;
360 int ret = -1;
361
199c86be 362 assert(!bitmap_git->map);
fff42755 363
7c141127 364 for (p = get_all_packs(r); p; p = p->next) {
3ae5fa07 365 if (open_pack_bitmap_1(bitmap_git, p) == 0)
fff42755
VM
366 ret = 0;
367 }
368
369 return ret;
370}
371
7c141127 372struct bitmap_index *prepare_bitmap_git(struct repository *r)
fff42755 373{
3ae5fa07 374 struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
fff42755 375
7c141127 376 if (!open_pack_bitmap(r, bitmap_git) && !load_pack_bitmap(bitmap_git))
3ae5fa07 377 return bitmap_git;
fff42755 378
f3c23db2 379 free_bitmap_index(bitmap_git);
3ae5fa07 380 return NULL;
fff42755
VM
381}
382
383struct include_data {
3ae5fa07 384 struct bitmap_index *bitmap_git;
fff42755
VM
385 struct bitmap *base;
386 struct bitmap *seen;
387};
388
98c31f36
TB
389struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
390 struct commit *commit)
391{
392 khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
393 commit->object.oid);
394 if (hash_pos >= kh_end(bitmap_git->bitmaps))
395 return NULL;
396 return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
397}
398
3ae5fa07 399static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
3c771448 400 const struct object_id *oid)
fff42755 401{
4ed43d16 402 kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
3c771448 403 khiter_t pos = kh_get_oid_pos(positions, *oid);
fff42755
VM
404
405 if (pos < kh_end(positions)) {
406 int bitmap_pos = kh_value(positions, pos);
3ae5fa07 407 return bitmap_pos + bitmap_git->pack->num_objects;
fff42755
VM
408 }
409
410 return -1;
411}
412
3ae5fa07 413static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
3c771448 414 const struct object_id *oid)
fff42755 415{
57665086 416 uint32_t pos;
3c771448 417 off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
fff42755
VM
418 if (!offset)
419 return -1;
420
57665086
TB
421 if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
422 return -1;
423 return pos;
fff42755
VM
424}
425
3ae5fa07 426static int bitmap_position(struct bitmap_index *bitmap_git,
3c771448 427 const struct object_id *oid)
fff42755 428{
3c771448 429 int pos = bitmap_position_packfile(bitmap_git, oid);
430 return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
fff42755
VM
431}
432
3ae5fa07
JT
433static int ext_index_add_object(struct bitmap_index *bitmap_git,
434 struct object *object, const char *name)
fff42755 435{
3ae5fa07 436 struct eindex *eindex = &bitmap_git->ext_index;
fff42755
VM
437
438 khiter_t hash_pos;
439 int hash_ret;
440 int bitmap_pos;
441
3c771448 442 hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
fff42755
VM
443 if (hash_ret > 0) {
444 if (eindex->count >= eindex->alloc) {
445 eindex->alloc = (eindex->alloc + 16) * 3 / 2;
2756ca43
RS
446 REALLOC_ARRAY(eindex->objects, eindex->alloc);
447 REALLOC_ARRAY(eindex->hashes, eindex->alloc);
fff42755
VM
448 }
449
450 bitmap_pos = eindex->count;
451 eindex->objects[eindex->count] = object;
452 eindex->hashes[eindex->count] = pack_name_hash(name);
453 kh_value(eindex->positions, hash_pos) = bitmap_pos;
454 eindex->count++;
455 } else {
456 bitmap_pos = kh_value(eindex->positions, hash_pos);
457 }
458
3ae5fa07 459 return bitmap_pos + bitmap_git->pack->num_objects;
fff42755
VM
460}
461
3ae5fa07
JT
462struct bitmap_show_data {
463 struct bitmap_index *bitmap_git;
464 struct bitmap *base;
465};
466
467static void show_object(struct object *object, const char *name, void *data_)
fff42755 468{
3ae5fa07 469 struct bitmap_show_data *data = data_;
fff42755
VM
470 int bitmap_pos;
471
3c771448 472 bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
fff42755 473
de1e67d0 474 if (bitmap_pos < 0)
3ae5fa07
JT
475 bitmap_pos = ext_index_add_object(data->bitmap_git, object,
476 name);
fff42755 477
3ae5fa07 478 bitmap_set(data->base, bitmap_pos);
fff42755
VM
479}
480
481static void show_commit(struct commit *commit, void *data)
482{
483}
484
3ae5fa07
JT
485static int add_to_include_set(struct bitmap_index *bitmap_git,
486 struct include_data *data,
98c31f36 487 struct commit *commit,
fff42755
VM
488 int bitmap_pos)
489{
98c31f36 490 struct ewah_bitmap *partial;
fff42755
VM
491
492 if (data->seen && bitmap_get(data->seen, bitmap_pos))
493 return 0;
494
495 if (bitmap_get(data->base, bitmap_pos))
496 return 0;
497
98c31f36
TB
498 partial = bitmap_for_commit(bitmap_git, commit);
499 if (partial) {
500 bitmap_or_ewah(data->base, partial);
fff42755
VM
501 return 0;
502 }
503
504 bitmap_set(data->base, bitmap_pos);
505 return 1;
506}
507
508static int should_include(struct commit *commit, void *_data)
509{
510 struct include_data *data = _data;
511 int bitmap_pos;
512
3c771448 513 bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
fff42755 514 if (bitmap_pos < 0)
3ae5fa07
JT
515 bitmap_pos = ext_index_add_object(data->bitmap_git,
516 (struct object *)commit,
517 NULL);
fff42755 518
98c31f36 519 if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
fff42755
VM
520 struct commit_list *parent = commit->parents;
521
522 while (parent) {
523 parent->item->object.flags |= SEEN;
524 parent = parent->next;
525 }
526
527 return 0;
528 }
529
530 return 1;
531}
532
aa9ad6fe
JK
533static int should_include_obj(struct object *obj, void *_data)
534{
535 struct include_data *data = _data;
536 int bitmap_pos;
537
538 bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
539 if (bitmap_pos < 0)
540 return 1;
541 if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
542 bitmap_get(data->base, bitmap_pos)) {
543 obj->flags |= SEEN;
544 return 0;
545 }
546 return 1;
547}
548
83578051
TB
549static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
550 struct bitmap **base,
551 struct commit *commit)
552{
553 struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
554
555 if (!or_with)
556 return 0;
557
558 if (*base == NULL)
559 *base = ewah_to_bitmap(or_with);
560 else
561 bitmap_or_ewah(*base, or_with);
562
563 return 1;
564}
565
3ae5fa07
JT
566static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
567 struct rev_info *revs,
fff42755 568 struct object_list *roots,
9639474b
JK
569 struct bitmap *seen,
570 struct list_objects_filter_options *filter)
fff42755
VM
571{
572 struct bitmap *base = NULL;
573 int needs_walk = 0;
574
575 struct object_list *not_mapped = NULL;
576
577 /*
578 * Go through all the roots for the walk. The ones that have bitmaps
579 * on the bitmap index will be `or`ed together to form an initial
580 * global reachability analysis.
581 *
582 * The ones without bitmaps in the index will be stored in the
583 * `not_mapped_list` for further processing.
584 */
585 while (roots) {
586 struct object *object = roots->item;
587 roots = roots->next;
588
83578051
TB
589 if (object->type == OBJ_COMMIT &&
590 add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
591 object->flags |= SEEN;
592 continue;
fff42755
VM
593 }
594
595 object_list_insert(object, &not_mapped);
596 }
597
598 /*
599 * Best case scenario: We found bitmaps for all the roots,
600 * so the resulting `or` bitmap has the full reachability analysis
601 */
602 if (not_mapped == NULL)
603 return base;
604
605 roots = not_mapped;
606
607 /*
608 * Let's iterate through all the roots that don't have bitmaps to
609 * check if we can determine them to be reachable from the existing
610 * global bitmap.
611 *
612 * If we cannot find them in the existing global bitmap, we'll need
613 * to push them to an actual walk and run it until we can confirm
614 * they are reachable
615 */
616 while (roots) {
617 struct object *object = roots->item;
618 int pos;
619
620 roots = roots->next;
3c771448 621 pos = bitmap_position(bitmap_git, &object->oid);
fff42755
VM
622
623 if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
624 object->flags &= ~UNINTERESTING;
625 add_pending_object(revs, object, "");
626 needs_walk = 1;
627 } else {
628 object->flags |= SEEN;
629 }
630 }
631
632 if (needs_walk) {
633 struct include_data incdata;
3ae5fa07 634 struct bitmap_show_data show_data;
fff42755
VM
635
636 if (base == NULL)
637 base = bitmap_new();
638
3ae5fa07 639 incdata.bitmap_git = bitmap_git;
fff42755
VM
640 incdata.base = base;
641 incdata.seen = seen;
642
643 revs->include_check = should_include;
aa9ad6fe 644 revs->include_check_obj = should_include_obj;
fff42755
VM
645 revs->include_check_data = &incdata;
646
647 if (prepare_revision_walk(revs))
648 die("revision walk setup failed");
649
3ae5fa07
JT
650 show_data.bitmap_git = bitmap_git;
651 show_data.base = base;
652
9639474b
JK
653 traverse_commit_list_filtered(filter, revs,
654 show_commit, show_object,
655 &show_data, NULL);
1e951c64
JK
656
657 revs->include_check = NULL;
aa9ad6fe 658 revs->include_check_obj = NULL;
1e951c64 659 revs->include_check_data = NULL;
fff42755
VM
660 }
661
662 return base;
663}
664
3ae5fa07 665static void show_extended_objects(struct bitmap_index *bitmap_git,
4eb707eb 666 struct rev_info *revs,
fff42755
VM
667 show_reachable_fn show_reach)
668{
3ae5fa07
JT
669 struct bitmap *objects = bitmap_git->result;
670 struct eindex *eindex = &bitmap_git->ext_index;
fff42755
VM
671 uint32_t i;
672
673 for (i = 0; i < eindex->count; ++i) {
674 struct object *obj;
675
3ae5fa07 676 if (!bitmap_get(objects, bitmap_git->pack->num_objects + i))
fff42755
VM
677 continue;
678
679 obj = eindex->objects[i];
4eb707eb
JK
680 if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
681 (obj->type == OBJ_TREE && !revs->tree_objects) ||
682 (obj->type == OBJ_TAG && !revs->tag_objects))
683 continue;
684
20664967 685 show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
fff42755
VM
686 }
687}
688
551cf8b6
JK
689static void init_type_iterator(struct ewah_iterator *it,
690 struct bitmap_index *bitmap_git,
691 enum object_type type)
692{
693 switch (type) {
694 case OBJ_COMMIT:
695 ewah_iterator_init(it, bitmap_git->commits);
696 break;
697
698 case OBJ_TREE:
699 ewah_iterator_init(it, bitmap_git->trees);
700 break;
701
702 case OBJ_BLOB:
703 ewah_iterator_init(it, bitmap_git->blobs);
704 break;
705
706 case OBJ_TAG:
707 ewah_iterator_init(it, bitmap_git->tags);
708 break;
709
710 default:
711 BUG("object type %d not stored by bitmap type index", type);
712 break;
713 }
714}
715
fff42755 716static void show_objects_for_type(
3ae5fa07 717 struct bitmap_index *bitmap_git,
fff42755
VM
718 enum object_type object_type,
719 show_reachable_fn show_reach)
720{
d2ea0310 721 size_t i = 0;
fff42755
VM
722 uint32_t offset;
723
724 struct ewah_iterator it;
725 eword_t filter;
726
3ae5fa07
JT
727 struct bitmap *objects = bitmap_git->result;
728
551cf8b6 729 init_type_iterator(&it, bitmap_git, object_type);
fff42755 730
d2ea0310
JK
731 for (i = 0; i < objects->word_alloc &&
732 ewah_iterator_next(&filter, &it); i++) {
fff42755 733 eword_t word = objects->words[i] & filter;
d2ea0310
JK
734 size_t pos = (i * BITS_IN_EWORD);
735
736 if (!word)
737 continue;
fff42755 738
34b935c0 739 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
20664967 740 struct object_id oid;
cf98f2e8
TB
741 uint32_t hash = 0, index_pos;
742 off_t ofs;
fff42755
VM
743
744 if ((word >> offset) == 0)
745 break;
746
747 offset += ewah_bit_ctz64(word >> offset);
748
cf98f2e8
TB
749 index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
750 ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
751 nth_packed_object_id(&oid, bitmap_git->pack, index_pos);
fff42755 752
3ae5fa07 753 if (bitmap_git->hashes)
cf98f2e8 754 hash = get_be32(bitmap_git->hashes + index_pos);
ae4f07fb 755
cf98f2e8 756 show_reach(&oid, object_type, 0, hash, bitmap_git->pack, ofs);
fff42755 757 }
fff42755
VM
758 }
759}
760
3ae5fa07
JT
761static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
762 struct object_list *roots)
fff42755
VM
763{
764 while (roots) {
765 struct object *object = roots->item;
766 roots = roots->next;
767
3ae5fa07 768 if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
fff42755
VM
769 return 1;
770 }
771
772 return 0;
773}
774
856e12c1
TB
775static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
776 struct object_list *tip_objects,
777 enum object_type type)
4f3bd560
JK
778{
779 struct bitmap *result = bitmap_new();
780 struct object_list *p;
781
782 for (p = tip_objects; p; p = p->next) {
783 int pos;
784
856e12c1 785 if (p->item->type != type)
4f3bd560
JK
786 continue;
787
788 pos = bitmap_position(bitmap_git, &p->item->oid);
789 if (pos < 0)
790 continue;
791
792 bitmap_set(result, pos);
793 }
794
795 return result;
796}
797
856e12c1
TB
798static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
799 struct object_list *tip_objects,
800 struct bitmap *to_filter,
801 enum object_type type)
4f3bd560
JK
802{
803 struct eindex *eindex = &bitmap_git->ext_index;
804 struct bitmap *tips;
805 struct ewah_iterator it;
806 eword_t mask;
807 uint32_t i;
808
809 /*
810 * The non-bitmap version of this filter never removes
856e12c1 811 * objects which the other side specifically asked for,
4f3bd560
JK
812 * so we must match that behavior.
813 */
856e12c1 814 tips = find_tip_objects(bitmap_git, tip_objects, type);
4f3bd560
JK
815
816 /*
ddcb189d
TB
817 * We can use the type-level bitmap for 'type' to work in whole
818 * words for the objects that are actually in the bitmapped
819 * packfile.
4f3bd560 820 */
856e12c1 821 for (i = 0, init_type_iterator(&it, bitmap_git, type);
4f3bd560
JK
822 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
823 i++) {
824 if (i < tips->word_alloc)
825 mask &= ~tips->words[i];
826 to_filter->words[i] &= ~mask;
827 }
828
829 /*
ddcb189d
TB
830 * Clear any objects that weren't in the packfile (and so would
831 * not have been caught by the loop above. We'll have to check
832 * them individually.
4f3bd560
JK
833 */
834 for (i = 0; i < eindex->count; i++) {
835 uint32_t pos = i + bitmap_git->pack->num_objects;
856e12c1 836 if (eindex->objects[i]->type == type &&
4f3bd560
JK
837 bitmap_get(to_filter, pos) &&
838 !bitmap_get(tips, pos))
839 bitmap_unset(to_filter, pos);
840 }
841
842 bitmap_free(tips);
843}
844
856e12c1
TB
845static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
846 struct object_list *tip_objects,
847 struct bitmap *to_filter)
848{
849 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
850 OBJ_BLOB);
851}
852
84243da1
JK
853static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
854 uint32_t pos)
855{
856 struct packed_git *pack = bitmap_git->pack;
857 unsigned long size;
858 struct object_info oi = OBJECT_INFO_INIT;
859
860 oi.sizep = &size;
861
862 if (pos < pack->num_objects) {
a78a9032
TB
863 off_t ofs = pack_pos_to_offset(pack, pos);
864 if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
84243da1 865 struct object_id oid;
a78a9032
TB
866 nth_packed_object_id(&oid, pack,
867 pack_pos_to_index(pack, pos));
84243da1
JK
868 die(_("unable to get size of %s"), oid_to_hex(&oid));
869 }
870 } else {
871 struct eindex *eindex = &bitmap_git->ext_index;
872 struct object *obj = eindex->objects[pos - pack->num_objects];
873 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
874 die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
875 }
876
877 return size;
878}
879
880static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
881 struct object_list *tip_objects,
882 struct bitmap *to_filter,
883 unsigned long limit)
884{
885 struct eindex *eindex = &bitmap_git->ext_index;
886 struct bitmap *tips;
887 struct ewah_iterator it;
888 eword_t mask;
889 uint32_t i;
890
856e12c1 891 tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
84243da1
JK
892
893 for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
894 i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
895 i++) {
896 eword_t word = to_filter->words[i] & mask;
897 unsigned offset;
898
899 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
900 uint32_t pos;
901
902 if ((word >> offset) == 0)
903 break;
904 offset += ewah_bit_ctz64(word >> offset);
905 pos = i * BITS_IN_EWORD + offset;
906
907 if (!bitmap_get(tips, pos) &&
908 get_size_by_pos(bitmap_git, pos) >= limit)
909 bitmap_unset(to_filter, pos);
910 }
911 }
912
913 for (i = 0; i < eindex->count; i++) {
914 uint32_t pos = i + bitmap_git->pack->num_objects;
915 if (eindex->objects[i]->type == OBJ_BLOB &&
916 bitmap_get(to_filter, pos) &&
917 !bitmap_get(tips, pos) &&
918 get_size_by_pos(bitmap_git, pos) >= limit)
919 bitmap_unset(to_filter, pos);
920 }
921
922 bitmap_free(tips);
923}
924
b0a8d482
TB
925static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
926 struct object_list *tip_objects,
927 struct bitmap *to_filter,
928 unsigned long limit)
929{
930 if (limit)
931 BUG("filter_bitmap_tree_depth given non-zero limit");
932
933 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
934 OBJ_TREE);
935 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
936 OBJ_BLOB);
937}
938
7ab6aafa
PS
939static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
940 struct object_list *tip_objects,
941 struct bitmap *to_filter,
942 enum object_type object_type)
943{
944 if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
945 BUG("filter_bitmap_object_type given invalid object");
946
947 if (object_type != OBJ_TAG)
948 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
949 if (object_type != OBJ_COMMIT)
950 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
951 if (object_type != OBJ_TREE)
952 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
953 if (object_type != OBJ_BLOB)
954 filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
955}
956
6663ae0a
JK
957static int filter_bitmap(struct bitmap_index *bitmap_git,
958 struct object_list *tip_objects,
959 struct bitmap *to_filter,
960 struct list_objects_filter_options *filter)
961{
962 if (!filter || filter->choice == LOFC_DISABLED)
963 return 0;
964
4f3bd560
JK
965 if (filter->choice == LOFC_BLOB_NONE) {
966 if (bitmap_git)
967 filter_bitmap_blob_none(bitmap_git, tip_objects,
968 to_filter);
969 return 0;
970 }
971
84243da1
JK
972 if (filter->choice == LOFC_BLOB_LIMIT) {
973 if (bitmap_git)
974 filter_bitmap_blob_limit(bitmap_git, tip_objects,
975 to_filter,
976 filter->blob_limit_value);
977 return 0;
978 }
979
b0a8d482
TB
980 if (filter->choice == LOFC_TREE_DEPTH &&
981 filter->tree_exclude_depth == 0) {
982 if (bitmap_git)
983 filter_bitmap_tree_depth(bitmap_git, tip_objects,
984 to_filter,
985 filter->tree_exclude_depth);
986 return 0;
987 }
988
7ab6aafa
PS
989 if (filter->choice == LOFC_OBJECT_TYPE) {
990 if (bitmap_git)
991 filter_bitmap_object_type(bitmap_git, tip_objects,
992 to_filter,
993 filter->object_type);
994 return 0;
995 }
996
169a15eb
PS
997 if (filter->choice == LOFC_COMBINE) {
998 int i;
999 for (i = 0; i < filter->sub_nr; i++) {
1000 if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1001 &filter->sub[i]) < 0)
1002 return -1;
1003 }
1004 return 0;
1005 }
1006
6663ae0a
JK
1007 /* filter choice not handled */
1008 return -1;
1009}
1010
1011static int can_filter_bitmap(struct list_objects_filter_options *filter)
1012{
1013 return !filter_bitmap(NULL, NULL, NULL, filter);
1014}
1015
1016struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
9cf68b27
PS
1017 struct list_objects_filter_options *filter,
1018 int filter_provided_objects)
fff42755
VM
1019{
1020 unsigned int i;
fff42755
VM
1021
1022 struct object_list *wants = NULL;
1023 struct object_list *haves = NULL;
1024
1025 struct bitmap *wants_bitmap = NULL;
1026 struct bitmap *haves_bitmap = NULL;
1027
d90fe06e
JK
1028 struct bitmap_index *bitmap_git;
1029
1030 /*
1031 * We can't do pathspec limiting with bitmaps, because we don't know
1032 * which commits are associated with which object changes (let alone
1033 * even which objects are associated with which paths).
1034 */
1035 if (revs->prune)
1036 return NULL;
1037
6663ae0a
JK
1038 if (!can_filter_bitmap(filter))
1039 return NULL;
1040
3ae5fa07
JT
1041 /* try to open a bitmapped pack, but don't parse it yet
1042 * because we may not need to use it */
ca56dadb 1043 CALLOC_ARRAY(bitmap_git, 1);
7c141127 1044 if (open_pack_bitmap(revs->repo, bitmap_git) < 0)
f3c23db2 1045 goto cleanup;
fff42755 1046
4d01a7fa
1047 for (i = 0; i < revs->pending.nr; ++i) {
1048 struct object *object = revs->pending.objects[i].item;
fff42755
VM
1049
1050 if (object->type == OBJ_NONE)
c251c83d 1051 parse_object_or_die(&object->oid, NULL);
fff42755
VM
1052
1053 while (object->type == OBJ_TAG) {
1054 struct tag *tag = (struct tag *) object;
1055
1056 if (object->flags & UNINTERESTING)
1057 object_list_insert(object, &haves);
1058 else
1059 object_list_insert(object, &wants);
1060
dad3f060 1061 object = parse_object_or_die(get_tagged_oid(tag), NULL);
540cdc11 1062 object->flags |= (tag->object.flags & UNINTERESTING);
fff42755
VM
1063 }
1064
1065 if (object->flags & UNINTERESTING)
1066 object_list_insert(object, &haves);
1067 else
1068 object_list_insert(object, &wants);
1069 }
1070
1071 /*
1072 * if we have a HAVES list, but none of those haves is contained
1073 * in the packfile that has a bitmap, we don't have anything to
1074 * optimize here
1075 */
3ae5fa07 1076 if (haves && !in_bitmapped_pack(bitmap_git, haves))
f3c23db2 1077 goto cleanup;
fff42755
VM
1078
1079 /* if we don't want anything, we're done here */
1080 if (!wants)
f3c23db2 1081 goto cleanup;
fff42755
VM
1082
1083 /*
1084 * now we're going to use bitmaps, so load the actual bitmap entries
1085 * from disk. this is the point of no return; after this the rev_list
1086 * becomes invalidated and we must perform the revwalk through bitmaps
1087 */
199c86be 1088 if (load_pack_bitmap(bitmap_git) < 0)
f3c23db2 1089 goto cleanup;
fff42755 1090
4d01a7fa 1091 object_array_clear(&revs->pending);
fff42755
VM
1092
1093 if (haves) {
2db1a43f 1094 revs->ignore_missing_links = 1;
9639474b
JK
1095 haves_bitmap = find_objects(bitmap_git, revs, haves, NULL,
1096 filter);
fff42755 1097 reset_revision_walk();
2db1a43f 1098 revs->ignore_missing_links = 0;
fff42755
VM
1099
1100 if (haves_bitmap == NULL)
033abf97 1101 BUG("failed to perform bitmap walk");
fff42755
VM
1102 }
1103
9639474b
JK
1104 wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap,
1105 filter);
fff42755
VM
1106
1107 if (!wants_bitmap)
033abf97 1108 BUG("failed to perform bitmap walk");
fff42755
VM
1109
1110 if (haves_bitmap)
1111 bitmap_and_not(wants_bitmap, haves_bitmap);
1112
9cf68b27
PS
1113 filter_bitmap(bitmap_git, (filter && filter_provided_objects) ? NULL : wants,
1114 wants_bitmap, filter);
6663ae0a 1115
3ae5fa07 1116 bitmap_git->result = wants_bitmap;
30cdc33f 1117 bitmap_git->haves = haves_bitmap;
fff42755 1118
acac50dd
JK
1119 object_list_free(&wants);
1120 object_list_free(&haves);
1121
3ae5fa07 1122 return bitmap_git;
f3c23db2
JT
1123
1124cleanup:
1125 free_bitmap_index(bitmap_git);
acac50dd
JK
1126 object_list_free(&wants);
1127 object_list_free(&haves);
f3c23db2 1128 return NULL;
fff42755
VM
1129}
1130
bb514de3
JK
1131static void try_partial_reuse(struct bitmap_index *bitmap_git,
1132 size_t pos,
1133 struct bitmap *reuse,
1134 struct pack_window **w_curs)
fff42755 1135{
011f3fd5 1136 off_t offset, header;
bb514de3
JK
1137 enum object_type type;
1138 unsigned long size;
1139
1140 if (pos >= bitmap_git->pack->num_objects)
1141 return; /* not actually in the pack */
1142
011f3fd5 1143 offset = header = pack_pos_to_offset(bitmap_git->pack, pos);
bb514de3
JK
1144 type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
1145 if (type < 0)
1146 return; /* broken packfile, punt */
1147
1148 if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
1149 off_t base_offset;
011f3fd5 1150 uint32_t base_pos;
bb514de3
JK
1151
1152 /*
1153 * Find the position of the base object so we can look it up
1154 * in our bitmaps. If we can't come up with an offset, or if
1155 * that offset is not in the revidx, the pack is corrupt.
1156 * There's nothing we can do, so just punt on this object,
1157 * and the normal slow path will complain about it in
1158 * more detail.
1159 */
1160 base_offset = get_delta_base(bitmap_git->pack, w_curs,
011f3fd5 1161 &offset, type, header);
bb514de3
JK
1162 if (!base_offset)
1163 return;
011f3fd5 1164 if (offset_to_pack_pos(bitmap_git->pack, base_offset, &base_pos) < 0)
bb514de3
JK
1165 return;
1166
1167 /*
1168 * We assume delta dependencies always point backwards. This
1169 * lets us do a single pass, and is basically always true
1170 * due to the way OFS_DELTAs work. You would not typically
1171 * find REF_DELTA in a bitmapped pack, since we only bitmap
1172 * packs we write fresh, and OFS_DELTA is the default). But
1173 * let's double check to make sure the pack wasn't written with
1174 * odd parameters.
1175 */
1176 if (base_pos >= pos)
1177 return;
1178
1179 /*
1180 * And finally, if we're not sending the base as part of our
1181 * reuse chunk, then don't send this object either. The base
1182 * would come after us, along with other objects not
1183 * necessarily in the pack, which means we'd need to convert
1184 * to REF_DELTA on the fly. Better to just let the normal
1185 * object_entry code path handle it.
1186 */
1187 if (!bitmap_get(reuse, base_pos))
1188 return;
1189 }
1190
fff42755 1191 /*
bb514de3 1192 * If we got here, then the object is OK to reuse. Mark it.
fff42755 1193 */
bb514de3
JK
1194 bitmap_set(reuse, pos);
1195}
fff42755 1196
bb514de3
JK
1197int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
1198 struct packed_git **packfile_out,
1199 uint32_t *entries,
1200 struct bitmap **reuse_out)
1201{
3ae5fa07 1202 struct bitmap *result = bitmap_git->result;
bb514de3
JK
1203 struct bitmap *reuse;
1204 struct pack_window *w_curs = NULL;
1205 size_t i = 0;
1206 uint32_t offset;
fff42755
VM
1207
1208 assert(result);
1209
bb514de3
JK
1210 while (i < result->word_alloc && result->words[i] == (eword_t)~0)
1211 i++;
fff42755 1212
bb514de3
JK
1213 /* Don't mark objects not in the packfile */
1214 if (i > bitmap_git->pack->num_objects / BITS_IN_EWORD)
1215 i = bitmap_git->pack->num_objects / BITS_IN_EWORD;
fff42755 1216
bb514de3
JK
1217 reuse = bitmap_word_alloc(i);
1218 memset(reuse->words, 0xFF, i * sizeof(eword_t));
fff42755 1219
bb514de3
JK
1220 for (; i < result->word_alloc; ++i) {
1221 eword_t word = result->words[i];
1222 size_t pos = (i * BITS_IN_EWORD);
fff42755 1223
bb514de3
JK
1224 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1225 if ((word >> offset) == 0)
1226 break;
fff42755 1227
bb514de3
JK
1228 offset += ewah_bit_ctz64(word >> offset);
1229 try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs);
1230 }
fff42755 1231 }
fff42755 1232
bb514de3 1233 unuse_pack(&w_curs);
fff42755 1234
bb514de3
JK
1235 *entries = bitmap_popcount(reuse);
1236 if (!*entries) {
1237 bitmap_free(reuse);
fff42755 1238 return -1;
fff42755
VM
1239 }
1240
bb514de3
JK
1241 /*
1242 * Drop any reused objects from the result, since they will not
1243 * need to be handled separately.
1244 */
1245 bitmap_and_not(result, reuse);
1246 *packfile_out = bitmap_git->pack;
1247 *reuse_out = reuse;
fff42755
VM
1248 return 0;
1249}
fff42755 1250
40d18ff8
JK
1251int bitmap_walk_contains(struct bitmap_index *bitmap_git,
1252 struct bitmap *bitmap, const struct object_id *oid)
1253{
1254 int idx;
fff42755 1255
40d18ff8
JK
1256 if (!bitmap)
1257 return 0;
fff42755 1258
40d18ff8
JK
1259 idx = bitmap_position(bitmap_git, oid);
1260 return idx >= 0 && bitmap_get(bitmap, idx);
fff42755
VM
1261}
1262
3ae5fa07 1263void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
4eb707eb 1264 struct rev_info *revs,
3ae5fa07 1265 show_reachable_fn show_reachable)
fff42755 1266{
3ae5fa07 1267 assert(bitmap_git->result);
fff42755 1268
551cf8b6 1269 show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
4eb707eb
JK
1270 if (revs->tree_objects)
1271 show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
1272 if (revs->blob_objects)
1273 show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
1274 if (revs->tag_objects)
1275 show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
fff42755 1276
4eb707eb 1277 show_extended_objects(bitmap_git, revs, show_reachable);
fff42755
VM
1278}
1279
3ae5fa07 1280static uint32_t count_object_type(struct bitmap_index *bitmap_git,
fff42755
VM
1281 enum object_type type)
1282{
3ae5fa07
JT
1283 struct bitmap *objects = bitmap_git->result;
1284 struct eindex *eindex = &bitmap_git->ext_index;
fff42755
VM
1285
1286 uint32_t i = 0, count = 0;
1287 struct ewah_iterator it;
1288 eword_t filter;
1289
551cf8b6 1290 init_type_iterator(&it, bitmap_git, type);
fff42755
VM
1291
1292 while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
1293 eword_t word = objects->words[i++] & filter;
1294 count += ewah_bit_popcount64(word);
1295 }
1296
1297 for (i = 0; i < eindex->count; ++i) {
1298 if (eindex->objects[i]->type == type &&
3ae5fa07 1299 bitmap_get(objects, bitmap_git->pack->num_objects + i))
fff42755
VM
1300 count++;
1301 }
1302
1303 return count;
1304}
1305
3ae5fa07
JT
1306void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
1307 uint32_t *commits, uint32_t *trees,
fff42755
VM
1308 uint32_t *blobs, uint32_t *tags)
1309{
3ae5fa07 1310 assert(bitmap_git->result);
fff42755
VM
1311
1312 if (commits)
3ae5fa07 1313 *commits = count_object_type(bitmap_git, OBJ_COMMIT);
fff42755
VM
1314
1315 if (trees)
3ae5fa07 1316 *trees = count_object_type(bitmap_git, OBJ_TREE);
fff42755
VM
1317
1318 if (blobs)
3ae5fa07 1319 *blobs = count_object_type(bitmap_git, OBJ_BLOB);
fff42755
VM
1320
1321 if (tags)
3ae5fa07 1322 *tags = count_object_type(bitmap_git, OBJ_TAG);
fff42755
VM
1323}
1324
1325struct bitmap_test_data {
3ae5fa07 1326 struct bitmap_index *bitmap_git;
fff42755
VM
1327 struct bitmap *base;
1328 struct progress *prg;
1329 size_t seen;
1330};
1331
de1e67d0
JK
1332static void test_show_object(struct object *object, const char *name,
1333 void *data)
fff42755
VM
1334{
1335 struct bitmap_test_data *tdata = data;
1336 int bitmap_pos;
1337
3c771448 1338 bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
fff42755 1339 if (bitmap_pos < 0)
f2fd0760 1340 die("Object not in bitmap: %s\n", oid_to_hex(&object->oid));
fff42755
VM
1341
1342 bitmap_set(tdata->base, bitmap_pos);
1343 display_progress(tdata->prg, ++tdata->seen);
1344}
1345
1346static void test_show_commit(struct commit *commit, void *data)
1347{
1348 struct bitmap_test_data *tdata = data;
1349 int bitmap_pos;
1350
3ae5fa07 1351 bitmap_pos = bitmap_position(tdata->bitmap_git,
3c771448 1352 &commit->object.oid);
fff42755 1353 if (bitmap_pos < 0)
f2fd0760 1354 die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid));
fff42755
VM
1355
1356 bitmap_set(tdata->base, bitmap_pos);
1357 display_progress(tdata->prg, ++tdata->seen);
1358}
1359
1360void test_bitmap_walk(struct rev_info *revs)
1361{
1362 struct object *root;
1363 struct bitmap *result = NULL;
fff42755
VM
1364 size_t result_popcnt;
1365 struct bitmap_test_data tdata;
3ae5fa07 1366 struct bitmap_index *bitmap_git;
98c31f36 1367 struct ewah_bitmap *bm;
fff42755 1368
7c141127 1369 if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
fff42755
VM
1370 die("failed to load bitmap indexes");
1371
1372 if (revs->pending.nr != 1)
1373 die("you must specify exactly one commit to test");
1374
1375 fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n",
3ae5fa07 1376 bitmap_git->version, bitmap_git->entry_count);
fff42755
VM
1377
1378 root = revs->pending.objects[0].item;
98c31f36 1379 bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
fff42755 1380
98c31f36 1381 if (bm) {
fff42755 1382 fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n",
f2fd0760 1383 oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
fff42755
VM
1384
1385 result = ewah_to_bitmap(bm);
1386 }
1387
1388 if (result == NULL)
f2fd0760 1389 die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid));
fff42755
VM
1390
1391 revs->tag_objects = 1;
1392 revs->tree_objects = 1;
1393 revs->blob_objects = 1;
1394
1395 result_popcnt = bitmap_popcount(result);
1396
1397 if (prepare_revision_walk(revs))
1398 die("revision walk setup failed");
1399
3ae5fa07 1400 tdata.bitmap_git = bitmap_git;
fff42755
VM
1401 tdata.base = bitmap_new();
1402 tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
1403 tdata.seen = 0;
1404
1405 traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
1406
1407 stop_progress(&tdata.prg);
1408
1409 if (bitmap_equals(result, tdata.base))
1410 fprintf(stderr, "OK!\n");
1411 else
2978b006 1412 die("mismatch in bitmap results");
f86a3747 1413
f3c23db2 1414 free_bitmap_index(bitmap_git);
fff42755 1415}
7cc8f971 1416
dff5e49e
TB
1417int test_bitmap_commits(struct repository *r)
1418{
1419 struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
1420 struct object_id oid;
1421 MAYBE_UNUSED void *value;
1422
1423 if (!bitmap_git)
1424 die("failed to load bitmap indexes");
1425
1426 kh_foreach(bitmap_git->bitmaps, oid, value, {
1427 printf("%s\n", oid_to_hex(&oid));
1428 });
1429
1430 free_bitmap_index(bitmap_git);
1431
1432 return 0;
1433}
1434
449fa5ee
JK
1435int rebuild_bitmap(const uint32_t *reposition,
1436 struct ewah_bitmap *source,
1437 struct bitmap *dest)
7cc8f971
VM
1438{
1439 uint32_t pos = 0;
1440 struct ewah_iterator it;
1441 eword_t word;
1442
1443 ewah_iterator_init(&it, source);
1444
1445 while (ewah_iterator_next(&word, &it)) {
1446 uint32_t offset, bit_pos;
1447
34b935c0 1448 for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
7cc8f971
VM
1449 if ((word >> offset) == 0)
1450 break;
1451
1452 offset += ewah_bit_ctz64(word >> offset);
1453
1454 bit_pos = reposition[pos + offset];
1455 if (bit_pos > 0)
1456 bitmap_set(dest, bit_pos - 1);
1457 else /* can't reuse, we don't have the object */
1458 return -1;
1459 }
1460
34b935c0 1461 pos += BITS_IN_EWORD;
7cc8f971
VM
1462 }
1463 return 0;
1464}
1465
449fa5ee
JK
1466uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
1467 struct packing_data *mapping)
7cc8f971
VM
1468{
1469 uint32_t i, num_objects;
1470 uint32_t *reposition;
7cc8f971 1471
3ae5fa07 1472 num_objects = bitmap_git->pack->num_objects;
ca56dadb 1473 CALLOC_ARRAY(reposition, num_objects);
7cc8f971
VM
1474
1475 for (i = 0; i < num_objects; ++i) {
3df28cae 1476 struct object_id oid;
7cc8f971
VM
1477 struct object_entry *oe;
1478
78232bf6
TB
1479 nth_packed_object_id(&oid, bitmap_git->pack,
1480 pack_pos_to_index(bitmap_git->pack, i));
3a37876b 1481 oe = packlist_find(mapping, &oid);
7cc8f971
VM
1482
1483 if (oe)
06af3bba 1484 reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
7cc8f971
VM
1485 }
1486
449fa5ee 1487 return reposition;
7cc8f971 1488}
f3c23db2
JT
1489
1490void free_bitmap_index(struct bitmap_index *b)
1491{
1492 if (!b)
1493 return;
1494
1495 if (b->map)
1496 munmap(b->map, b->map_size);
1497 ewah_pool_free(b->commits);
1498 ewah_pool_free(b->trees);
1499 ewah_pool_free(b->blobs);
1500 ewah_pool_free(b->tags);
3c771448 1501 kh_destroy_oid_map(b->bitmaps);
f3c23db2
JT
1502 free(b->ext_index.objects);
1503 free(b->ext_index.hashes);
1504 bitmap_free(b->result);
30cdc33f 1505 bitmap_free(b->haves);
f3c23db2
JT
1506 free(b);
1507}
30cdc33f 1508
3c771448 1509int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
1510 const struct object_id *oid)
30cdc33f 1511{
8ebf5296
JK
1512 return bitmap_git &&
1513 bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
30cdc33f 1514}
16950f83
JK
1515
1516static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
1517 enum object_type object_type)
1518{
1519 struct bitmap *result = bitmap_git->result;
1520 struct packed_git *pack = bitmap_git->pack;
1521 off_t total = 0;
1522 struct ewah_iterator it;
1523 eword_t filter;
1524 size_t i;
1525
1526 init_type_iterator(&it, bitmap_git, object_type);
1527 for (i = 0; i < result->word_alloc &&
1528 ewah_iterator_next(&filter, &it); i++) {
1529 eword_t word = result->words[i] & filter;
1530 size_t base = (i * BITS_IN_EWORD);
1531 unsigned offset;
1532
1533 if (!word)
1534 continue;
1535
1536 for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1537 size_t pos;
1538
1539 if ((word >> offset) == 0)
1540 break;
1541
1542 offset += ewah_bit_ctz64(word >> offset);
1543 pos = base + offset;
1544 total += pack_pos_to_offset(pack, pos + 1) -
1545 pack_pos_to_offset(pack, pos);
1546 }
1547 }
1548
1549 return total;
1550}
1551
1552static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
1553{
1554 struct bitmap *result = bitmap_git->result;
1555 struct packed_git *pack = bitmap_git->pack;
1556 struct eindex *eindex = &bitmap_git->ext_index;
1557 off_t total = 0;
1558 struct object_info oi = OBJECT_INFO_INIT;
1559 off_t object_size;
1560 size_t i;
1561
1562 oi.disk_sizep = &object_size;
1563
1564 for (i = 0; i < eindex->count; i++) {
1565 struct object *obj = eindex->objects[i];
1566
1567 if (!bitmap_get(result, pack->num_objects + i))
1568 continue;
1569
1570 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1571 die(_("unable to get disk usage of %s"),
1572 oid_to_hex(&obj->oid));
1573
1574 total += object_size;
1575 }
1576 return total;
1577}
1578
1579off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
1580 struct rev_info *revs)
1581{
1582 off_t total = 0;
1583
1584 total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
1585 if (revs->tree_objects)
1586 total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
1587 if (revs->blob_objects)
1588 total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
1589 if (revs->tag_objects)
1590 total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
1591
1592 total += get_disk_usage_for_extended(bitmap_git);
1593
1594 return total;
1595}
3f267a11
TB
1596
1597const struct string_list *bitmap_preferred_tips(struct repository *r)
1598{
1599 return repo_config_get_value_multi(r, "pack.preferbitmaptips");
1600}