git
/
pack-bitmap.c
3047 строк · 78.1 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "commit.h"
5#include "gettext.h"
6#include "hex.h"
7#include "strbuf.h"
8#include "tag.h"
9#include "diff.h"
10#include "revision.h"
11#include "progress.h"
12#include "list-objects.h"
13#include "pack.h"
14#include "pack-bitmap.h"
15#include "pack-revindex.h"
16#include "pack-objects.h"
17#include "packfile.h"
18#include "repository.h"
19#include "trace2.h"
20#include "object-file.h"
21#include "object-store-ll.h"
22#include "list-objects-filter-options.h"
23#include "midx.h"
24#include "config.h"
25#include "pseudo-merge.h"
26
27/*
28* An entry on the bitmap index, representing the bitmap for a given
29* commit.
30*/
31struct stored_bitmap {
32struct object_id oid;
33struct ewah_bitmap *root;
34struct stored_bitmap *xor;
35int flags;
36};
37
38/*
39* The active bitmap index for a repository. By design, repositories only have
40* a single bitmap index available (the index for the biggest packfile in
41* the repository), since bitmap indexes need full closure.
42*
43* If there is more than one bitmap index available (e.g. because of alternates),
44* the active bitmap index is the largest one.
45*/
46struct bitmap_index {
47/*
48* The pack or multi-pack index (MIDX) that this bitmap index belongs
49* to.
50*
51* Exactly one of these must be non-NULL; this specifies the object
52* order used to interpret this bitmap.
53*/
54struct packed_git *pack;
55struct multi_pack_index *midx;
56
57/* mmapped buffer of the whole bitmap index */
58unsigned char *map;
59size_t map_size; /* size of the mmaped buffer */
60size_t map_pos; /* current position when loading the index */
61
62/*
63* Type indexes.
64*
65* Each bitmap marks which objects in the packfile are of the given
66* type. This provides type information when yielding the objects from
67* the packfile during a walk, which allows for better delta bases.
68*/
69struct ewah_bitmap *commits;
70struct ewah_bitmap *trees;
71struct ewah_bitmap *blobs;
72struct ewah_bitmap *tags;
73
74/* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
75kh_oid_map_t *bitmaps;
76
77/* Number of bitmapped commits */
78uint32_t entry_count;
79
80/* If not NULL, this is a name-hash cache pointing into map. */
81uint32_t *hashes;
82
83/* The checksum of the packfile or MIDX; points into map. */
84const unsigned char *checksum;
85
86/*
87* If not NULL, this point into the commit table extension
88* (within the memory mapped region `map`).
89*/
90unsigned char *table_lookup;
91
92/* This contains the pseudo-merge cache within 'map' (if found). */
93struct pseudo_merge_map pseudo_merges;
94
95/*
96* Extended index.
97*
98* When trying to perform bitmap operations with objects that are not
99* packed in `pack`, these objects are added to this "fake index" and
100* are assumed to appear at the end of the packfile for all operations
101*/
102struct eindex {
103struct object **objects;
104uint32_t *hashes;
105uint32_t count, alloc;
106kh_oid_pos_t *positions;
107} ext_index;
108
109/* Bitmap result of the last performed walk */
110struct bitmap *result;
111
112/* "have" bitmap from the last performed walk */
113struct bitmap *haves;
114
115/* Version of the bitmap index */
116unsigned int version;
117};
118
119static int pseudo_merges_satisfied_nr;
120static int pseudo_merges_cascades_nr;
121static int existing_bitmaps_hits_nr;
122static int existing_bitmaps_misses_nr;
123static int roots_with_bitmaps_nr;
124static int roots_without_bitmaps_nr;
125
126static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
127{
128struct ewah_bitmap *parent;
129struct ewah_bitmap *composed;
130
131if (!st->xor)
132return st->root;
133
134composed = ewah_pool_new();
135parent = lookup_stored_bitmap(st->xor);
136ewah_xor(st->root, parent, composed);
137
138ewah_pool_free(st->root);
139st->root = composed;
140st->xor = NULL;
141
142return composed;
143}
144
145struct ewah_bitmap *read_bitmap(const unsigned char *map,
146size_t map_size, size_t *map_pos)
147{
148struct ewah_bitmap *b = ewah_pool_new();
149
150ssize_t bitmap_size = ewah_read_mmap(b, map + *map_pos,
151map_size - *map_pos);
152
153if (bitmap_size < 0) {
154error(_("failed to load bitmap index (corrupted?)"));
155ewah_pool_free(b);
156return NULL;
157}
158
159*map_pos += bitmap_size;
160
161return b;
162}
163
164/*
165* Read a bitmap from the current read position on the mmaped
166* index, and increase the read position accordingly
167*/
168static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
169{
170return read_bitmap(index->map, index->map_size, &index->map_pos);
171}
172
173static uint32_t bitmap_num_objects(struct bitmap_index *index)
174{
175if (index->midx)
176return index->midx->num_objects;
177return index->pack->num_objects;
178}
179
180static int load_bitmap_header(struct bitmap_index *index)
181{
182struct bitmap_disk_header *header = (void *)index->map;
183size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
184
185if (index->map_size < header_size + the_hash_algo->rawsz)
186return error(_("corrupted bitmap index (too small)"));
187
188if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
189return error(_("corrupted bitmap index file (wrong header)"));
190
191index->version = ntohs(header->version);
192if (index->version != 1)
193return error(_("unsupported version '%d' for bitmap index file"), index->version);
194
195/* Parse known bitmap format options */
196{
197uint32_t flags = ntohs(header->options);
198size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
199unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
200
201if ((flags & BITMAP_OPT_FULL_DAG) == 0)
202BUG("unsupported options for bitmap index file "
203"(Git requires BITMAP_OPT_FULL_DAG)");
204
205if (flags & BITMAP_OPT_HASH_CACHE) {
206if (cache_size > index_end - index->map - header_size)
207return error(_("corrupted bitmap index file (too short to fit hash cache)"));
208index->hashes = (void *)(index_end - cache_size);
209index_end -= cache_size;
210}
211
212if (flags & BITMAP_OPT_LOOKUP_TABLE) {
213size_t table_size = st_mult(ntohl(header->entry_count),
214BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
215if (table_size > index_end - index->map - header_size)
216return error(_("corrupted bitmap index file (too short to fit lookup table)"));
217if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
218index->table_lookup = (void *)(index_end - table_size);
219index_end -= table_size;
220}
221
222if (flags & BITMAP_OPT_PSEUDO_MERGES) {
223unsigned char *pseudo_merge_ofs;
224size_t table_size;
225uint32_t i;
226
227if (sizeof(table_size) > index_end - index->map - header_size)
228return error(_("corrupted bitmap index file (too short to fit pseudo-merge table header)"));
229
230table_size = get_be64(index_end - 8);
231if (table_size > index_end - index->map - header_size)
232return error(_("corrupted bitmap index file (too short to fit pseudo-merge table)"));
233
234if (git_env_bool("GIT_TEST_USE_PSEUDO_MERGES", 1)) {
235const unsigned char *ext = (index_end - table_size);
236
237index->pseudo_merges.map = index->map;
238index->pseudo_merges.map_size = index->map_size;
239index->pseudo_merges.commits = ext + get_be64(index_end - 16);
240index->pseudo_merges.commits_nr = get_be32(index_end - 20);
241index->pseudo_merges.nr = get_be32(index_end - 24);
242
243if (st_add(st_mult(index->pseudo_merges.nr,
244sizeof(uint64_t)),
24524) > table_size)
246return error(_("corrupted bitmap index file, pseudo-merge table too short"));
247
248CALLOC_ARRAY(index->pseudo_merges.v,
249index->pseudo_merges.nr);
250
251pseudo_merge_ofs = index_end - 24 -
252(index->pseudo_merges.nr * sizeof(uint64_t));
253for (i = 0; i < index->pseudo_merges.nr; i++) {
254index->pseudo_merges.v[i].at = get_be64(pseudo_merge_ofs);
255pseudo_merge_ofs += sizeof(uint64_t);
256}
257}
258
259index_end -= table_size;
260}
261}
262
263index->entry_count = ntohl(header->entry_count);
264index->checksum = header->checksum;
265index->map_pos += header_size;
266return 0;
267}
268
269static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
270struct ewah_bitmap *root,
271const struct object_id *oid,
272struct stored_bitmap *xor_with,
273int flags)
274{
275struct stored_bitmap *stored;
276khiter_t hash_pos;
277int ret;
278
279stored = xmalloc(sizeof(struct stored_bitmap));
280stored->root = root;
281stored->xor = xor_with;
282stored->flags = flags;
283oidcpy(&stored->oid, oid);
284
285hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
286
287/*
288* A 0 return code means the insertion succeeded with no changes,
289* because the SHA1 already existed on the map. This is bad, there
290* shouldn't be duplicated commits in the index.
291*/
292if (ret == 0) {
293error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
294return NULL;
295}
296
297kh_value(index->bitmaps, hash_pos) = stored;
298return stored;
299}
300
301static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
302{
303uint32_t result = get_be32(buffer + *pos);
304(*pos) += sizeof(result);
305return result;
306}
307
308static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
309{
310return buffer[(*pos)++];
311}
312
313#define MAX_XOR_OFFSET 160
314
315static int nth_bitmap_object_oid(struct bitmap_index *index,
316struct object_id *oid,
317uint32_t n)
318{
319if (index->midx)
320return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
321return nth_packed_object_id(oid, index->pack, n);
322}
323
324static int load_bitmap_entries_v1(struct bitmap_index *index)
325{
326uint32_t i;
327struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
328
329for (i = 0; i < index->entry_count; ++i) {
330int xor_offset, flags;
331struct ewah_bitmap *bitmap = NULL;
332struct stored_bitmap *xor_bitmap = NULL;
333uint32_t commit_idx_pos;
334struct object_id oid;
335
336if (index->map_size - index->map_pos < 6)
337return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
338
339commit_idx_pos = read_be32(index->map, &index->map_pos);
340xor_offset = read_u8(index->map, &index->map_pos);
341flags = read_u8(index->map, &index->map_pos);
342
343if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
344return error(_("corrupt ewah bitmap: commit index %u out of range"),
345(unsigned)commit_idx_pos);
346
347bitmap = read_bitmap_1(index);
348if (!bitmap)
349return -1;
350
351if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
352return error(_("corrupted bitmap pack index"));
353
354if (xor_offset > 0) {
355xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
356
357if (!xor_bitmap)
358return error(_("invalid XOR offset in bitmap pack index"));
359}
360
361recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
362index, bitmap, &oid, xor_bitmap, flags);
363}
364
365return 0;
366}
367
368char *midx_bitmap_filename(struct multi_pack_index *midx)
369{
370struct strbuf buf = STRBUF_INIT;
371get_midx_filename_ext(&buf, midx->object_dir, get_midx_checksum(midx),
372MIDX_EXT_BITMAP);
373
374return strbuf_detach(&buf, NULL);
375}
376
377char *pack_bitmap_filename(struct packed_git *p)
378{
379size_t len;
380
381if (!strip_suffix(p->pack_name, ".pack", &len))
382BUG("pack_name does not end in .pack");
383return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
384}
385
386static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
387struct multi_pack_index *midx)
388{
389struct stat st;
390char *bitmap_name = midx_bitmap_filename(midx);
391int fd = git_open(bitmap_name);
392uint32_t i, preferred_pack;
393struct packed_git *preferred;
394
395if (fd < 0) {
396if (errno != ENOENT)
397warning_errno("cannot open '%s'", bitmap_name);
398free(bitmap_name);
399return -1;
400}
401free(bitmap_name);
402
403if (fstat(fd, &st)) {
404error_errno(_("cannot fstat bitmap file"));
405close(fd);
406return -1;
407}
408
409if (bitmap_git->pack || bitmap_git->midx) {
410struct strbuf buf = STRBUF_INIT;
411get_midx_filename(&buf, midx->object_dir);
412trace2_data_string("bitmap", the_repository,
413"ignoring extra midx bitmap file", buf.buf);
414close(fd);
415strbuf_release(&buf);
416return -1;
417}
418
419bitmap_git->midx = midx;
420bitmap_git->map_size = xsize_t(st.st_size);
421bitmap_git->map_pos = 0;
422bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
423MAP_PRIVATE, fd, 0);
424close(fd);
425
426if (load_bitmap_header(bitmap_git) < 0)
427goto cleanup;
428
429if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum,
430the_repository->hash_algo)) {
431error(_("checksum doesn't match in MIDX and bitmap"));
432goto cleanup;
433}
434
435if (load_midx_revindex(bitmap_git->midx)) {
436warning(_("multi-pack bitmap is missing required reverse index"));
437goto cleanup;
438}
439
440for (i = 0; i < bitmap_git->midx->num_packs; i++) {
441if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
442warning(_("could not open pack %s"),
443bitmap_git->midx->pack_names[i]);
444goto cleanup;
445}
446}
447
448if (midx_preferred_pack(bitmap_git->midx, &preferred_pack) < 0) {
449warning(_("could not determine MIDX preferred pack"));
450goto cleanup;
451}
452
453preferred = bitmap_git->midx->packs[preferred_pack];
454if (!is_pack_valid(preferred)) {
455warning(_("preferred pack (%s) is invalid"),
456preferred->pack_name);
457goto cleanup;
458}
459
460return 0;
461
462cleanup:
463munmap(bitmap_git->map, bitmap_git->map_size);
464bitmap_git->map_size = 0;
465bitmap_git->map_pos = 0;
466bitmap_git->map = NULL;
467bitmap_git->midx = NULL;
468return -1;
469}
470
471static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
472{
473int fd;
474struct stat st;
475char *bitmap_name;
476
477bitmap_name = pack_bitmap_filename(packfile);
478fd = git_open(bitmap_name);
479
480if (fd < 0) {
481if (errno != ENOENT)
482warning_errno("cannot open '%s'", bitmap_name);
483free(bitmap_name);
484return -1;
485}
486free(bitmap_name);
487
488if (fstat(fd, &st)) {
489error_errno(_("cannot fstat bitmap file"));
490close(fd);
491return -1;
492}
493
494if (bitmap_git->pack || bitmap_git->midx) {
495trace2_data_string("bitmap", the_repository,
496"ignoring extra bitmap file", packfile->pack_name);
497close(fd);
498return -1;
499}
500
501if (!is_pack_valid(packfile)) {
502close(fd);
503return -1;
504}
505
506bitmap_git->pack = packfile;
507bitmap_git->map_size = xsize_t(st.st_size);
508bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
509bitmap_git->map_pos = 0;
510close(fd);
511
512if (load_bitmap_header(bitmap_git) < 0) {
513munmap(bitmap_git->map, bitmap_git->map_size);
514bitmap_git->map = NULL;
515bitmap_git->map_size = 0;
516bitmap_git->map_pos = 0;
517bitmap_git->pack = NULL;
518return -1;
519}
520
521trace2_data_string("bitmap", the_repository, "opened bitmap file",
522packfile->pack_name);
523return 0;
524}
525
526static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
527{
528if (bitmap_is_midx(bitmap_git)) {
529uint32_t i;
530int ret;
531
532/*
533* The multi-pack-index's .rev file is already loaded via
534* open_pack_bitmap_1().
535*
536* But we still need to open the individual pack .rev files,
537* since we will need to make use of them in pack-objects.
538*/
539for (i = 0; i < bitmap_git->midx->num_packs; i++) {
540ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
541if (ret)
542return ret;
543}
544return 0;
545}
546return load_pack_revindex(r, bitmap_git->pack);
547}
548
549static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
550{
551assert(bitmap_git->map);
552
553bitmap_git->bitmaps = kh_init_oid_map();
554bitmap_git->ext_index.positions = kh_init_oid_pos();
555
556if (load_reverse_index(r, bitmap_git))
557goto failed;
558
559if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
560!(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
561!(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
562!(bitmap_git->tags = read_bitmap_1(bitmap_git)))
563goto failed;
564
565if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
566goto failed;
567
568return 0;
569
570failed:
571munmap(bitmap_git->map, bitmap_git->map_size);
572bitmap_git->map = NULL;
573bitmap_git->map_size = 0;
574
575kh_destroy_oid_map(bitmap_git->bitmaps);
576bitmap_git->bitmaps = NULL;
577
578kh_destroy_oid_pos(bitmap_git->ext_index.positions);
579bitmap_git->ext_index.positions = NULL;
580
581return -1;
582}
583
584static int open_pack_bitmap(struct repository *r,
585struct bitmap_index *bitmap_git)
586{
587struct packed_git *p;
588int ret = -1;
589
590for (p = get_all_packs(r); p; p = p->next) {
591if (open_pack_bitmap_1(bitmap_git, p) == 0) {
592ret = 0;
593/*
594* The only reason to keep looking is to report
595* duplicates.
596*/
597if (!trace2_is_enabled())
598break;
599}
600}
601
602return ret;
603}
604
605static int open_midx_bitmap(struct repository *r,
606struct bitmap_index *bitmap_git)
607{
608int ret = -1;
609struct multi_pack_index *midx;
610
611assert(!bitmap_git->map);
612
613for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
614if (!open_midx_bitmap_1(bitmap_git, midx))
615ret = 0;
616}
617return ret;
618}
619
620static int open_bitmap(struct repository *r,
621struct bitmap_index *bitmap_git)
622{
623int found;
624
625assert(!bitmap_git->map);
626
627found = !open_midx_bitmap(r, bitmap_git);
628
629/*
630* these will all be skipped if we opened a midx bitmap; but run it
631* anyway if tracing is enabled to report the duplicates
632*/
633if (!found || trace2_is_enabled())
634found |= !open_pack_bitmap(r, bitmap_git);
635
636return found ? 0 : -1;
637}
638
639struct bitmap_index *prepare_bitmap_git(struct repository *r)
640{
641struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
642
643if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
644return bitmap_git;
645
646free_bitmap_index(bitmap_git);
647return NULL;
648}
649
650struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
651{
652struct repository *r = the_repository;
653struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
654
655if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
656return bitmap_git;
657
658free_bitmap_index(bitmap_git);
659return NULL;
660}
661
662struct include_data {
663struct bitmap_index *bitmap_git;
664struct bitmap *base;
665struct bitmap *seen;
666};
667
668struct bitmap_lookup_table_triplet {
669uint32_t commit_pos;
670uint64_t offset;
671uint32_t xor_row;
672};
673
674struct bitmap_lookup_table_xor_item {
675struct object_id oid;
676uint64_t offset;
677};
678
679/*
680* Given a `triplet` struct pointer and pointer `p`, this
681* function reads the triplet beginning at `p` into the struct.
682* Note that this function assumes that there is enough memory
683* left for filling the `triplet` struct from `p`.
684*/
685static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
686const unsigned char *p)
687{
688if (!triplet)
689return -1;
690
691triplet->commit_pos = get_be32(p);
692p += sizeof(uint32_t);
693triplet->offset = get_be64(p);
694p += sizeof(uint64_t);
695triplet->xor_row = get_be32(p);
696return 0;
697}
698
699/*
700* This function gets the raw triplet from `row`'th row in the
701* lookup table and fills that data to the `triplet`.
702*/
703static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
704uint32_t pos,
705struct bitmap_lookup_table_triplet *triplet)
706{
707unsigned char *p = NULL;
708if (pos >= bitmap_git->entry_count)
709return error(_("corrupt bitmap lookup table: triplet position out of index"));
710
711p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
712
713return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
714}
715
716/*
717* Searches for a matching triplet. `commit_pos` is a pointer
718* to the wanted commit position value. `table_entry` points to
719* a triplet in lookup table. The first 4 bytes of each
720* triplet (pointed by `table_entry`) are compared with `*commit_pos`.
721*/
722static int triplet_cmp(const void *commit_pos, const void *table_entry)
723{
724
725uint32_t a = *(uint32_t *)commit_pos;
726uint32_t b = get_be32(table_entry);
727if (a > b)
728return 1;
729else if (a < b)
730return -1;
731
732return 0;
733}
734
735static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
736struct object_id *oid,
737uint32_t *result)
738{
739int found;
740
741if (bitmap_is_midx(bitmap_git))
742found = bsearch_midx(oid, bitmap_git->midx, result);
743else
744found = bsearch_pack(oid, bitmap_git->pack, result);
745
746return found;
747}
748
749/*
750* `bsearch_triplet_by_pos` function searches for the raw triplet
751* having commit position same as `commit_pos` and fills `triplet`
752* object from the raw triplet. Returns 1 on success and 0 on
753* failure.
754*/
755static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
756struct bitmap_index *bitmap_git,
757struct bitmap_lookup_table_triplet *triplet)
758{
759unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
760BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
761
762if (!p)
763return -1;
764
765return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
766}
767
768static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
769struct commit *commit)
770{
771uint32_t commit_pos, xor_row;
772uint64_t offset;
773int flags;
774struct bitmap_lookup_table_triplet triplet;
775struct object_id *oid = &commit->object.oid;
776struct ewah_bitmap *bitmap;
777struct stored_bitmap *xor_bitmap = NULL;
778const int bitmap_header_size = 6;
779static struct bitmap_lookup_table_xor_item *xor_items = NULL;
780static size_t xor_items_nr = 0, xor_items_alloc = 0;
781static int is_corrupt = 0;
782int xor_flags;
783khiter_t hash_pos;
784struct bitmap_lookup_table_xor_item *xor_item;
785
786if (is_corrupt)
787return NULL;
788
789if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
790return NULL;
791
792if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
793return NULL;
794
795xor_items_nr = 0;
796offset = triplet.offset;
797xor_row = triplet.xor_row;
798
799while (xor_row != 0xffffffff) {
800ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
801
802if (xor_items_nr + 1 >= bitmap_git->entry_count) {
803error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
804goto corrupt;
805}
806
807if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
808goto corrupt;
809
810xor_item = &xor_items[xor_items_nr];
811xor_item->offset = triplet.offset;
812
813if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
814error(_("corrupt bitmap lookup table: commit index %u out of range"),
815triplet.commit_pos);
816goto corrupt;
817}
818
819hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
820
821/*
822* If desired bitmap is already stored, we don't need
823* to iterate further. Because we know that bitmaps
824* that are needed to be parsed to parse this bitmap
825* has already been stored. So, assign this stored bitmap
826* to the xor_bitmap.
827*/
828if (hash_pos < kh_end(bitmap_git->bitmaps) &&
829(xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
830break;
831xor_items_nr++;
832xor_row = triplet.xor_row;
833}
834
835while (xor_items_nr) {
836xor_item = &xor_items[xor_items_nr - 1];
837bitmap_git->map_pos = xor_item->offset;
838if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
839error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
840oid_to_hex(&xor_item->oid));
841goto corrupt;
842}
843
844bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
845xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
846bitmap = read_bitmap_1(bitmap_git);
847
848if (!bitmap)
849goto corrupt;
850
851xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
852xor_items_nr--;
853}
854
855bitmap_git->map_pos = offset;
856if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
857error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
858oid_to_hex(oid));
859goto corrupt;
860}
861
862/*
863* Don't bother reading the commit's index position or its xor
864* offset:
865*
866* - The commit's index position is irrelevant to us, since
867* load_bitmap_entries_v1 only uses it to learn the object
868* id which is used to compute the hashmap's key. We already
869* have an object id, so no need to look it up again.
870*
871* - The xor_offset is unusable for us, since it specifies how
872* many entries previous to ours we should look at. This
873* makes sense when reading the bitmaps sequentially (as in
874* load_bitmap_entries_v1()), since we can keep track of
875* each bitmap as we read them.
876*
877* But it can't work for us, since the bitmap's don't have a
878* fixed size. So we learn the position of the xor'd bitmap
879* from the commit table (and resolve it to a bitmap in the
880* above if-statement).
881*
882* Instead, we can skip ahead and immediately read the flags and
883* ewah bitmap.
884*/
885bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
886flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
887bitmap = read_bitmap_1(bitmap_git);
888
889if (!bitmap)
890goto corrupt;
891
892return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
893
894corrupt:
895free(xor_items);
896is_corrupt = 1;
897return NULL;
898}
899
900struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
901struct commit *commit)
902{
903khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
904commit->object.oid);
905if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
906struct stored_bitmap *bitmap = NULL;
907if (!bitmap_git->table_lookup)
908return NULL;
909
910/* this is a fairly hot codepath - no trace2_region please */
911/* NEEDSWORK: cache misses aren't recorded */
912bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
913if (!bitmap)
914return NULL;
915return lookup_stored_bitmap(bitmap);
916}
917return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
918}
919
920static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
921const struct object_id *oid)
922{
923kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
924khiter_t pos = kh_get_oid_pos(positions, *oid);
925
926if (pos < kh_end(positions)) {
927int bitmap_pos = kh_value(positions, pos);
928return bitmap_pos + bitmap_num_objects(bitmap_git);
929}
930
931return -1;
932}
933
934static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
935const struct object_id *oid)
936{
937uint32_t pos;
938off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
939if (!offset)
940return -1;
941
942if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
943return -1;
944return pos;
945}
946
947static int bitmap_position_midx(struct bitmap_index *bitmap_git,
948const struct object_id *oid)
949{
950uint32_t want, got;
951if (!bsearch_midx(oid, bitmap_git->midx, &want))
952return -1;
953
954if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
955return -1;
956return got;
957}
958
959static int bitmap_position(struct bitmap_index *bitmap_git,
960const struct object_id *oid)
961{
962int pos;
963if (bitmap_is_midx(bitmap_git))
964pos = bitmap_position_midx(bitmap_git, oid);
965else
966pos = bitmap_position_packfile(bitmap_git, oid);
967return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
968}
969
970static int ext_index_add_object(struct bitmap_index *bitmap_git,
971struct object *object, const char *name)
972{
973struct eindex *eindex = &bitmap_git->ext_index;
974
975khiter_t hash_pos;
976int hash_ret;
977int bitmap_pos;
978
979hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
980if (hash_ret > 0) {
981if (eindex->count >= eindex->alloc) {
982eindex->alloc = (eindex->alloc + 16) * 3 / 2;
983REALLOC_ARRAY(eindex->objects, eindex->alloc);
984REALLOC_ARRAY(eindex->hashes, eindex->alloc);
985}
986
987bitmap_pos = eindex->count;
988eindex->objects[eindex->count] = object;
989eindex->hashes[eindex->count] = pack_name_hash(name);
990kh_value(eindex->positions, hash_pos) = bitmap_pos;
991eindex->count++;
992} else {
993bitmap_pos = kh_value(eindex->positions, hash_pos);
994}
995
996return bitmap_pos + bitmap_num_objects(bitmap_git);
997}
998
999struct bitmap_show_data {
1000struct bitmap_index *bitmap_git;
1001struct bitmap *base;
1002};
1003
1004static void show_object(struct object *object, const char *name, void *data_)
1005{
1006struct bitmap_show_data *data = data_;
1007int bitmap_pos;
1008
1009bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
1010
1011if (bitmap_pos < 0)
1012bitmap_pos = ext_index_add_object(data->bitmap_git, object,
1013name);
1014
1015bitmap_set(data->base, bitmap_pos);
1016}
1017
1018static void show_commit(struct commit *commit UNUSED,
1019void *data UNUSED)
1020{
1021}
1022
1023static unsigned apply_pseudo_merges_for_commit_1(struct bitmap_index *bitmap_git,
1024struct bitmap *result,
1025struct commit *commit,
1026uint32_t commit_pos)
1027{
1028int ret;
1029
1030ret = apply_pseudo_merges_for_commit(&bitmap_git->pseudo_merges,
1031result, commit, commit_pos);
1032
1033if (ret)
1034pseudo_merges_satisfied_nr += ret;
1035
1036return ret;
1037}
1038
1039static int add_to_include_set(struct bitmap_index *bitmap_git,
1040struct include_data *data,
1041struct commit *commit,
1042int bitmap_pos)
1043{
1044struct ewah_bitmap *partial;
1045
1046if (data->seen && bitmap_get(data->seen, bitmap_pos))
1047return 0;
1048
1049if (bitmap_get(data->base, bitmap_pos))
1050return 0;
1051
1052partial = bitmap_for_commit(bitmap_git, commit);
1053if (partial) {
1054existing_bitmaps_hits_nr++;
1055
1056bitmap_or_ewah(data->base, partial);
1057return 0;
1058}
1059
1060existing_bitmaps_misses_nr++;
1061
1062bitmap_set(data->base, bitmap_pos);
1063if (apply_pseudo_merges_for_commit_1(bitmap_git, data->base, commit,
1064bitmap_pos))
1065return 0;
1066
1067return 1;
1068}
1069
1070static int should_include(struct commit *commit, void *_data)
1071{
1072struct include_data *data = _data;
1073int bitmap_pos;
1074
1075bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
1076if (bitmap_pos < 0)
1077bitmap_pos = ext_index_add_object(data->bitmap_git,
1078(struct object *)commit,
1079NULL);
1080
1081if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
1082struct commit_list *parent = commit->parents;
1083
1084while (parent) {
1085parent->item->object.flags |= SEEN;
1086parent = parent->next;
1087}
1088
1089return 0;
1090}
1091
1092return 1;
1093}
1094
1095static int should_include_obj(struct object *obj, void *_data)
1096{
1097struct include_data *data = _data;
1098int bitmap_pos;
1099
1100bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1101if (bitmap_pos < 0)
1102return 1;
1103if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1104bitmap_get(data->base, bitmap_pos)) {
1105obj->flags |= SEEN;
1106return 0;
1107}
1108return 1;
1109}
1110
1111static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1112struct bitmap **base,
1113struct commit *commit)
1114{
1115struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1116
1117if (!or_with) {
1118existing_bitmaps_misses_nr++;
1119return 0;
1120}
1121
1122existing_bitmaps_hits_nr++;
1123
1124if (!*base)
1125*base = ewah_to_bitmap(or_with);
1126else
1127bitmap_or_ewah(*base, or_with);
1128
1129return 1;
1130}
1131
1132static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1133struct rev_info *revs,
1134struct bitmap *base,
1135struct bitmap *seen)
1136{
1137struct include_data incdata;
1138struct bitmap_show_data show_data;
1139
1140if (!base)
1141base = bitmap_new();
1142
1143incdata.bitmap_git = bitmap_git;
1144incdata.base = base;
1145incdata.seen = seen;
1146
1147revs->include_check = should_include;
1148revs->include_check_obj = should_include_obj;
1149revs->include_check_data = &incdata;
1150
1151if (prepare_revision_walk(revs))
1152die(_("revision walk setup failed"));
1153
1154show_data.bitmap_git = bitmap_git;
1155show_data.base = base;
1156
1157traverse_commit_list(revs, show_commit, show_object, &show_data);
1158
1159revs->include_check = NULL;
1160revs->include_check_obj = NULL;
1161revs->include_check_data = NULL;
1162
1163return base;
1164}
1165
1166struct bitmap_boundary_cb {
1167struct bitmap_index *bitmap_git;
1168struct bitmap *base;
1169
1170struct object_array boundary;
1171};
1172
1173static void show_boundary_commit(struct commit *commit, void *_data)
1174{
1175struct bitmap_boundary_cb *data = _data;
1176
1177if (commit->object.flags & BOUNDARY)
1178add_object_array(&commit->object, "", &data->boundary);
1179
1180if (commit->object.flags & UNINTERESTING) {
1181if (bitmap_walk_contains(data->bitmap_git, data->base,
1182&commit->object.oid))
1183return;
1184
1185add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1186}
1187}
1188
1189static void show_boundary_object(struct object *object UNUSED,
1190const char *name UNUSED,
1191void *data UNUSED)
1192{
1193BUG("should not be called");
1194}
1195
1196static unsigned cascade_pseudo_merges_1(struct bitmap_index *bitmap_git,
1197struct bitmap *result,
1198struct bitmap *roots)
1199{
1200int ret = cascade_pseudo_merges(&bitmap_git->pseudo_merges,
1201result, roots);
1202if (ret) {
1203pseudo_merges_cascades_nr++;
1204pseudo_merges_satisfied_nr += ret;
1205}
1206
1207return ret;
1208}
1209
1210static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1211struct rev_info *revs,
1212struct object_list *roots)
1213{
1214struct bitmap_boundary_cb cb;
1215struct object_list *root;
1216unsigned int i;
1217unsigned int tmp_blobs, tmp_trees, tmp_tags;
1218int any_missing = 0;
1219int existing_bitmaps = 0;
1220
1221cb.bitmap_git = bitmap_git;
1222cb.base = bitmap_new();
1223object_array_init(&cb.boundary);
1224
1225revs->ignore_missing_links = 1;
1226
1227if (bitmap_git->pseudo_merges.nr) {
1228struct bitmap *roots_bitmap = bitmap_new();
1229struct object_list *objects = NULL;
1230
1231for (objects = roots; objects; objects = objects->next) {
1232struct object *object = objects->item;
1233int pos;
1234
1235pos = bitmap_position(bitmap_git, &object->oid);
1236if (pos < 0)
1237continue;
1238
1239bitmap_set(roots_bitmap, pos);
1240}
1241
1242if (!cascade_pseudo_merges_1(bitmap_git, cb.base, roots_bitmap))
1243bitmap_free(roots_bitmap);
1244}
1245
1246/*
1247* OR in any existing reachability bitmaps among `roots` into
1248* `cb.base`.
1249*/
1250for (root = roots; root; root = root->next) {
1251struct object *object = root->item;
1252if (object->type != OBJ_COMMIT ||
1253bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1254continue;
1255
1256if (add_commit_to_bitmap(bitmap_git, &cb.base,
1257(struct commit *)object)) {
1258existing_bitmaps = 1;
1259continue;
1260}
1261
1262any_missing = 1;
1263}
1264
1265if (!any_missing)
1266goto cleanup;
1267
1268if (existing_bitmaps)
1269cascade_pseudo_merges_1(bitmap_git, cb.base, NULL);
1270
1271tmp_blobs = revs->blob_objects;
1272tmp_trees = revs->tree_objects;
1273tmp_tags = revs->blob_objects;
1274revs->blob_objects = 0;
1275revs->tree_objects = 0;
1276revs->tag_objects = 0;
1277
1278/*
1279* We didn't have complete coverage of the roots. First setup a
1280* revision walk to (a) OR in any bitmaps that are UNINTERESTING
1281* between the tips and boundary, and (b) record the boundary.
1282*/
1283trace2_region_enter("pack-bitmap", "boundary-prepare", the_repository);
1284if (prepare_revision_walk(revs))
1285die("revision walk setup failed");
1286trace2_region_leave("pack-bitmap", "boundary-prepare", the_repository);
1287
1288trace2_region_enter("pack-bitmap", "boundary-traverse", the_repository);
1289revs->boundary = 1;
1290traverse_commit_list_filtered(revs,
1291show_boundary_commit,
1292show_boundary_object,
1293&cb, NULL);
1294revs->boundary = 0;
1295trace2_region_leave("pack-bitmap", "boundary-traverse", the_repository);
1296
1297revs->blob_objects = tmp_blobs;
1298revs->tree_objects = tmp_trees;
1299revs->tag_objects = tmp_tags;
1300
1301reset_revision_walk();
1302clear_object_flags(UNINTERESTING);
1303
1304/*
1305* Then add the boundary commit(s) as fill-in traversal tips.
1306*/
1307trace2_region_enter("pack-bitmap", "boundary-fill-in", the_repository);
1308for (i = 0; i < cb.boundary.nr; i++) {
1309struct object *obj = cb.boundary.objects[i].item;
1310if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1311obj->flags |= SEEN;
1312else
1313add_pending_object(revs, obj, "");
1314}
1315if (revs->pending.nr)
1316cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1317trace2_region_leave("pack-bitmap", "boundary-fill-in", the_repository);
1318
1319cleanup:
1320object_array_clear(&cb.boundary);
1321revs->ignore_missing_links = 0;
1322
1323return cb.base;
1324}
1325
1326struct ewah_bitmap *pseudo_merge_bitmap_for_commit(struct bitmap_index *bitmap_git,
1327struct commit *commit)
1328{
1329struct commit_list *p;
1330struct bitmap *parents;
1331struct pseudo_merge *match = NULL;
1332
1333if (!bitmap_git->pseudo_merges.nr)
1334return NULL;
1335
1336parents = bitmap_new();
1337
1338for (p = commit->parents; p; p = p->next) {
1339int pos = bitmap_position(bitmap_git, &p->item->object.oid);
1340if (pos < 0 || pos >= bitmap_num_objects(bitmap_git))
1341goto done;
1342
1343bitmap_set(parents, pos);
1344}
1345
1346match = pseudo_merge_for_parents(&bitmap_git->pseudo_merges,
1347parents);
1348
1349done:
1350bitmap_free(parents);
1351if (match)
1352return pseudo_merge_bitmap(&bitmap_git->pseudo_merges, match);
1353
1354return NULL;
1355}
1356
1357static void unsatisfy_all_pseudo_merges(struct bitmap_index *bitmap_git)
1358{
1359uint32_t i;
1360for (i = 0; i < bitmap_git->pseudo_merges.nr; i++)
1361bitmap_git->pseudo_merges.v[i].satisfied = 0;
1362}
1363
1364static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1365struct rev_info *revs,
1366struct object_list *roots,
1367struct bitmap *seen)
1368{
1369struct bitmap *base = NULL;
1370int needs_walk = 0;
1371unsigned existing_bitmaps = 0;
1372
1373struct object_list *not_mapped = NULL;
1374
1375unsatisfy_all_pseudo_merges(bitmap_git);
1376
1377if (bitmap_git->pseudo_merges.nr) {
1378struct bitmap *roots_bitmap = bitmap_new();
1379struct object_list *objects = NULL;
1380
1381for (objects = roots; objects; objects = objects->next) {
1382struct object *object = objects->item;
1383int pos;
1384
1385pos = bitmap_position(bitmap_git, &object->oid);
1386if (pos < 0)
1387continue;
1388
1389bitmap_set(roots_bitmap, pos);
1390}
1391
1392base = bitmap_new();
1393if (!cascade_pseudo_merges_1(bitmap_git, base, roots_bitmap))
1394bitmap_free(roots_bitmap);
1395}
1396
1397/*
1398* Go through all the roots for the walk. The ones that have bitmaps
1399* on the bitmap index will be `or`ed together to form an initial
1400* global reachability analysis.
1401*
1402* The ones without bitmaps in the index will be stored in the
1403* `not_mapped_list` for further processing.
1404*/
1405while (roots) {
1406struct object *object = roots->item;
1407
1408roots = roots->next;
1409
1410if (base) {
1411int pos = bitmap_position(bitmap_git, &object->oid);
1412if (pos > 0 && bitmap_get(base, pos)) {
1413object->flags |= SEEN;
1414continue;
1415}
1416}
1417
1418if (object->type == OBJ_COMMIT &&
1419add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1420object->flags |= SEEN;
1421existing_bitmaps = 1;
1422continue;
1423}
1424
1425object_list_insert(object, ¬_mapped);
1426}
1427
1428/*
1429* Best case scenario: We found bitmaps for all the roots,
1430* so the resulting `or` bitmap has the full reachability analysis
1431*/
1432if (!not_mapped)
1433return base;
1434
1435roots = not_mapped;
1436
1437if (existing_bitmaps)
1438cascade_pseudo_merges_1(bitmap_git, base, NULL);
1439
1440/*
1441* Let's iterate through all the roots that don't have bitmaps to
1442* check if we can determine them to be reachable from the existing
1443* global bitmap.
1444*
1445* If we cannot find them in the existing global bitmap, we'll need
1446* to push them to an actual walk and run it until we can confirm
1447* they are reachable
1448*/
1449while (roots) {
1450struct object *object = roots->item;
1451int pos;
1452
1453roots = roots->next;
1454pos = bitmap_position(bitmap_git, &object->oid);
1455
1456if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1457object->flags &= ~UNINTERESTING;
1458add_pending_object(revs, object, "");
1459needs_walk = 1;
1460
1461roots_without_bitmaps_nr++;
1462} else {
1463object->flags |= SEEN;
1464
1465roots_with_bitmaps_nr++;
1466}
1467}
1468
1469if (needs_walk) {
1470/*
1471* This fill-in traversal may walk over some objects
1472* again, since we have already traversed in order to
1473* find the boundary.
1474*
1475* But this extra walk should be extremely cheap, since
1476* all commit objects are loaded into memory, and
1477* because we skip walking to parents that are
1478* UNINTERESTING, since it will be marked in the haves
1479* bitmap already (or it has an on-disk bitmap, since
1480* OR-ing it in covers all of its ancestors).
1481*/
1482base = fill_in_bitmap(bitmap_git, revs, base, seen);
1483}
1484
1485object_list_free(¬_mapped);
1486
1487return base;
1488}
1489
1490static void show_extended_objects(struct bitmap_index *bitmap_git,
1491struct rev_info *revs,
1492show_reachable_fn show_reach)
1493{
1494struct bitmap *objects = bitmap_git->result;
1495struct eindex *eindex = &bitmap_git->ext_index;
1496uint32_t i;
1497
1498for (i = 0; i < eindex->count; ++i) {
1499struct object *obj;
1500
1501if (!bitmap_get(objects, st_add(bitmap_num_objects(bitmap_git), i)))
1502continue;
1503
1504obj = eindex->objects[i];
1505if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1506(obj->type == OBJ_TREE && !revs->tree_objects) ||
1507(obj->type == OBJ_TAG && !revs->tag_objects))
1508continue;
1509
1510show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0);
1511}
1512}
1513
1514static void init_type_iterator(struct ewah_iterator *it,
1515struct bitmap_index *bitmap_git,
1516enum object_type type)
1517{
1518switch (type) {
1519case OBJ_COMMIT:
1520ewah_iterator_init(it, bitmap_git->commits);
1521break;
1522
1523case OBJ_TREE:
1524ewah_iterator_init(it, bitmap_git->trees);
1525break;
1526
1527case OBJ_BLOB:
1528ewah_iterator_init(it, bitmap_git->blobs);
1529break;
1530
1531case OBJ_TAG:
1532ewah_iterator_init(it, bitmap_git->tags);
1533break;
1534
1535default:
1536BUG("object type %d not stored by bitmap type index", type);
1537break;
1538}
1539}
1540
1541static void show_objects_for_type(
1542struct bitmap_index *bitmap_git,
1543enum object_type object_type,
1544show_reachable_fn show_reach)
1545{
1546size_t i = 0;
1547uint32_t offset;
1548
1549struct ewah_iterator it;
1550eword_t filter;
1551
1552struct bitmap *objects = bitmap_git->result;
1553
1554init_type_iterator(&it, bitmap_git, object_type);
1555
1556for (i = 0; i < objects->word_alloc &&
1557ewah_iterator_next(&filter, &it); i++) {
1558eword_t word = objects->words[i] & filter;
1559size_t pos = (i * BITS_IN_EWORD);
1560
1561if (!word)
1562continue;
1563
1564for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1565struct packed_git *pack;
1566struct object_id oid;
1567uint32_t hash = 0, index_pos;
1568off_t ofs;
1569
1570if ((word >> offset) == 0)
1571break;
1572
1573offset += ewah_bit_ctz64(word >> offset);
1574
1575if (bitmap_is_midx(bitmap_git)) {
1576struct multi_pack_index *m = bitmap_git->midx;
1577uint32_t pack_id;
1578
1579index_pos = pack_pos_to_midx(m, pos + offset);
1580ofs = nth_midxed_offset(m, index_pos);
1581nth_midxed_object_oid(&oid, m, index_pos);
1582
1583pack_id = nth_midxed_pack_int_id(m, index_pos);
1584pack = bitmap_git->midx->packs[pack_id];
1585} else {
1586index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1587ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1588nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1589
1590pack = bitmap_git->pack;
1591}
1592
1593if (bitmap_git->hashes)
1594hash = get_be32(bitmap_git->hashes + index_pos);
1595
1596show_reach(&oid, object_type, 0, hash, pack, ofs);
1597}
1598}
1599}
1600
1601static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1602struct object_list *roots)
1603{
1604while (roots) {
1605struct object *object = roots->item;
1606roots = roots->next;
1607
1608if (bitmap_is_midx(bitmap_git)) {
1609if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1610return 1;
1611} else {
1612if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0)
1613return 1;
1614}
1615}
1616
1617return 0;
1618}
1619
1620static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1621struct object_list *tip_objects,
1622enum object_type type)
1623{
1624struct bitmap *result = bitmap_new();
1625struct object_list *p;
1626
1627for (p = tip_objects; p; p = p->next) {
1628int pos;
1629
1630if (p->item->type != type)
1631continue;
1632
1633pos = bitmap_position(bitmap_git, &p->item->oid);
1634if (pos < 0)
1635continue;
1636
1637bitmap_set(result, pos);
1638}
1639
1640return result;
1641}
1642
1643static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1644struct object_list *tip_objects,
1645struct bitmap *to_filter,
1646enum object_type type)
1647{
1648struct eindex *eindex = &bitmap_git->ext_index;
1649struct bitmap *tips;
1650struct ewah_iterator it;
1651eword_t mask;
1652uint32_t i;
1653
1654/*
1655* The non-bitmap version of this filter never removes
1656* objects which the other side specifically asked for,
1657* so we must match that behavior.
1658*/
1659tips = find_tip_objects(bitmap_git, tip_objects, type);
1660
1661/*
1662* We can use the type-level bitmap for 'type' to work in whole
1663* words for the objects that are actually in the bitmapped
1664* packfile.
1665*/
1666for (i = 0, init_type_iterator(&it, bitmap_git, type);
1667i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1668i++) {
1669if (i < tips->word_alloc)
1670mask &= ~tips->words[i];
1671to_filter->words[i] &= ~mask;
1672}
1673
1674/*
1675* Clear any objects that weren't in the packfile (and so would
1676* not have been caught by the loop above. We'll have to check
1677* them individually.
1678*/
1679for (i = 0; i < eindex->count; i++) {
1680size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1681if (eindex->objects[i]->type == type &&
1682bitmap_get(to_filter, pos) &&
1683!bitmap_get(tips, pos))
1684bitmap_unset(to_filter, pos);
1685}
1686
1687bitmap_free(tips);
1688}
1689
1690static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1691struct object_list *tip_objects,
1692struct bitmap *to_filter)
1693{
1694filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1695OBJ_BLOB);
1696}
1697
1698static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1699uint32_t pos)
1700{
1701unsigned long size;
1702struct object_info oi = OBJECT_INFO_INIT;
1703
1704oi.sizep = &size;
1705
1706if (pos < bitmap_num_objects(bitmap_git)) {
1707struct packed_git *pack;
1708off_t ofs;
1709
1710if (bitmap_is_midx(bitmap_git)) {
1711uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1712uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1713
1714pack = bitmap_git->midx->packs[pack_id];
1715ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1716} else {
1717pack = bitmap_git->pack;
1718ofs = pack_pos_to_offset(pack, pos);
1719}
1720
1721if (packed_object_info(the_repository, pack, ofs, &oi) < 0) {
1722struct object_id oid;
1723nth_bitmap_object_oid(bitmap_git, &oid,
1724pack_pos_to_index(pack, pos));
1725die(_("unable to get size of %s"), oid_to_hex(&oid));
1726}
1727} else {
1728struct eindex *eindex = &bitmap_git->ext_index;
1729struct object *obj = eindex->objects[pos - bitmap_num_objects(bitmap_git)];
1730if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
1731die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1732}
1733
1734return size;
1735}
1736
1737static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1738struct object_list *tip_objects,
1739struct bitmap *to_filter,
1740unsigned long limit)
1741{
1742struct eindex *eindex = &bitmap_git->ext_index;
1743struct bitmap *tips;
1744struct ewah_iterator it;
1745eword_t mask;
1746uint32_t i;
1747
1748tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1749
1750for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1751i < to_filter->word_alloc && ewah_iterator_next(&mask, &it);
1752i++) {
1753eword_t word = to_filter->words[i] & mask;
1754unsigned offset;
1755
1756for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1757uint32_t pos;
1758
1759if ((word >> offset) == 0)
1760break;
1761offset += ewah_bit_ctz64(word >> offset);
1762pos = i * BITS_IN_EWORD + offset;
1763
1764if (!bitmap_get(tips, pos) &&
1765get_size_by_pos(bitmap_git, pos) >= limit)
1766bitmap_unset(to_filter, pos);
1767}
1768}
1769
1770for (i = 0; i < eindex->count; i++) {
1771size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1772if (eindex->objects[i]->type == OBJ_BLOB &&
1773bitmap_get(to_filter, pos) &&
1774!bitmap_get(tips, pos) &&
1775get_size_by_pos(bitmap_git, pos) >= limit)
1776bitmap_unset(to_filter, pos);
1777}
1778
1779bitmap_free(tips);
1780}
1781
1782static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1783struct object_list *tip_objects,
1784struct bitmap *to_filter,
1785unsigned long limit)
1786{
1787if (limit)
1788BUG("filter_bitmap_tree_depth given non-zero limit");
1789
1790filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1791OBJ_TREE);
1792filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1793OBJ_BLOB);
1794}
1795
1796static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1797struct object_list *tip_objects,
1798struct bitmap *to_filter,
1799enum object_type object_type)
1800{
1801if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1802BUG("filter_bitmap_object_type given invalid object");
1803
1804if (object_type != OBJ_TAG)
1805filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1806if (object_type != OBJ_COMMIT)
1807filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1808if (object_type != OBJ_TREE)
1809filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1810if (object_type != OBJ_BLOB)
1811filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1812}
1813
1814static int filter_bitmap(struct bitmap_index *bitmap_git,
1815struct object_list *tip_objects,
1816struct bitmap *to_filter,
1817struct list_objects_filter_options *filter)
1818{
1819if (!filter || filter->choice == LOFC_DISABLED)
1820return 0;
1821
1822if (filter->choice == LOFC_BLOB_NONE) {
1823if (bitmap_git)
1824filter_bitmap_blob_none(bitmap_git, tip_objects,
1825to_filter);
1826return 0;
1827}
1828
1829if (filter->choice == LOFC_BLOB_LIMIT) {
1830if (bitmap_git)
1831filter_bitmap_blob_limit(bitmap_git, tip_objects,
1832to_filter,
1833filter->blob_limit_value);
1834return 0;
1835}
1836
1837if (filter->choice == LOFC_TREE_DEPTH &&
1838filter->tree_exclude_depth == 0) {
1839if (bitmap_git)
1840filter_bitmap_tree_depth(bitmap_git, tip_objects,
1841to_filter,
1842filter->tree_exclude_depth);
1843return 0;
1844}
1845
1846if (filter->choice == LOFC_OBJECT_TYPE) {
1847if (bitmap_git)
1848filter_bitmap_object_type(bitmap_git, tip_objects,
1849to_filter,
1850filter->object_type);
1851return 0;
1852}
1853
1854if (filter->choice == LOFC_COMBINE) {
1855int i;
1856for (i = 0; i < filter->sub_nr; i++) {
1857if (filter_bitmap(bitmap_git, tip_objects, to_filter,
1858&filter->sub[i]) < 0)
1859return -1;
1860}
1861return 0;
1862}
1863
1864/* filter choice not handled */
1865return -1;
1866}
1867
1868static int can_filter_bitmap(struct list_objects_filter_options *filter)
1869{
1870return !filter_bitmap(NULL, NULL, NULL, filter);
1871}
1872
1873
1874static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
1875struct bitmap *result)
1876{
1877struct eindex *eindex = &bitmap_git->ext_index;
1878uint32_t objects_nr;
1879size_t i, pos;
1880
1881objects_nr = bitmap_num_objects(bitmap_git);
1882pos = objects_nr / BITS_IN_EWORD;
1883
1884if (pos > result->word_alloc)
1885pos = result->word_alloc;
1886
1887memset(result->words, 0x00, sizeof(eword_t) * pos);
1888for (i = pos * BITS_IN_EWORD; i < objects_nr; i++)
1889bitmap_unset(result, i);
1890
1891for (i = 0; i < eindex->count; ++i) {
1892if (has_object_pack(&eindex->objects[i]->oid))
1893bitmap_unset(result, objects_nr + i);
1894}
1895}
1896
1897struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
1898int filter_provided_objects)
1899{
1900unsigned int i;
1901int use_boundary_traversal;
1902
1903struct object_list *wants = NULL;
1904struct object_list *haves = NULL;
1905
1906struct bitmap *wants_bitmap = NULL;
1907struct bitmap *haves_bitmap = NULL;
1908
1909struct bitmap_index *bitmap_git;
1910
1911/*
1912* We can't do pathspec limiting with bitmaps, because we don't know
1913* which commits are associated with which object changes (let alone
1914* even which objects are associated with which paths).
1915*/
1916if (revs->prune)
1917return NULL;
1918
1919if (!can_filter_bitmap(&revs->filter))
1920return NULL;
1921
1922/* try to open a bitmapped pack, but don't parse it yet
1923* because we may not need to use it */
1924CALLOC_ARRAY(bitmap_git, 1);
1925if (open_bitmap(revs->repo, bitmap_git) < 0)
1926goto cleanup;
1927
1928for (i = 0; i < revs->pending.nr; ++i) {
1929struct object *object = revs->pending.objects[i].item;
1930
1931if (object->type == OBJ_NONE)
1932parse_object_or_die(&object->oid, NULL);
1933
1934while (object->type == OBJ_TAG) {
1935struct tag *tag = (struct tag *) object;
1936
1937if (object->flags & UNINTERESTING)
1938object_list_insert(object, &haves);
1939else
1940object_list_insert(object, &wants);
1941
1942object = parse_object_or_die(get_tagged_oid(tag), NULL);
1943object->flags |= (tag->object.flags & UNINTERESTING);
1944}
1945
1946if (object->flags & UNINTERESTING)
1947object_list_insert(object, &haves);
1948else
1949object_list_insert(object, &wants);
1950}
1951
1952use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
1953if (use_boundary_traversal < 0) {
1954prepare_repo_settings(revs->repo);
1955use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
1956}
1957
1958if (!use_boundary_traversal) {
1959/*
1960* if we have a HAVES list, but none of those haves is contained
1961* in the packfile that has a bitmap, we don't have anything to
1962* optimize here
1963*/
1964if (haves && !in_bitmapped_pack(bitmap_git, haves))
1965goto cleanup;
1966}
1967
1968/* if we don't want anything, we're done here */
1969if (!wants)
1970goto cleanup;
1971
1972/*
1973* now we're going to use bitmaps, so load the actual bitmap entries
1974* from disk. this is the point of no return; after this the rev_list
1975* becomes invalidated and we must perform the revwalk through bitmaps
1976*/
1977if (load_bitmap(revs->repo, bitmap_git) < 0)
1978goto cleanup;
1979
1980if (!use_boundary_traversal)
1981object_array_clear(&revs->pending);
1982
1983if (haves) {
1984if (use_boundary_traversal) {
1985trace2_region_enter("pack-bitmap", "haves/boundary", the_repository);
1986haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
1987trace2_region_leave("pack-bitmap", "haves/boundary", the_repository);
1988} else {
1989trace2_region_enter("pack-bitmap", "haves/classic", the_repository);
1990revs->ignore_missing_links = 1;
1991haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
1992reset_revision_walk();
1993revs->ignore_missing_links = 0;
1994trace2_region_leave("pack-bitmap", "haves/classic", the_repository);
1995}
1996
1997if (!haves_bitmap)
1998BUG("failed to perform bitmap walk");
1999}
2000
2001if (use_boundary_traversal) {
2002object_array_clear(&revs->pending);
2003reset_revision_walk();
2004}
2005
2006wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
2007
2008if (!wants_bitmap)
2009BUG("failed to perform bitmap walk");
2010
2011if (haves_bitmap)
2012bitmap_and_not(wants_bitmap, haves_bitmap);
2013
2014filter_bitmap(bitmap_git,
2015(revs->filter.choice && filter_provided_objects) ? NULL : wants,
2016wants_bitmap,
2017&revs->filter);
2018
2019if (revs->unpacked)
2020filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap);
2021
2022bitmap_git->result = wants_bitmap;
2023bitmap_git->haves = haves_bitmap;
2024
2025object_list_free(&wants);
2026object_list_free(&haves);
2027
2028trace2_data_intmax("bitmap", the_repository, "pseudo_merges_satisfied",
2029pseudo_merges_satisfied_nr);
2030trace2_data_intmax("bitmap", the_repository, "pseudo_merges_cascades",
2031pseudo_merges_cascades_nr);
2032trace2_data_intmax("bitmap", the_repository, "bitmap/hits",
2033existing_bitmaps_hits_nr);
2034trace2_data_intmax("bitmap", the_repository, "bitmap/misses",
2035existing_bitmaps_misses_nr);
2036trace2_data_intmax("bitmap", the_repository, "bitmap/roots_with_bitmap",
2037roots_with_bitmaps_nr);
2038trace2_data_intmax("bitmap", the_repository, "bitmap/roots_without_bitmap",
2039roots_without_bitmaps_nr);
2040
2041return bitmap_git;
2042
2043cleanup:
2044free_bitmap_index(bitmap_git);
2045object_list_free(&wants);
2046object_list_free(&haves);
2047return NULL;
2048}
2049
2050/*
2051* -1 means "stop trying further objects"; 0 means we may or may not have
2052* reused, but you can keep feeding bits.
2053*/
2054static int try_partial_reuse(struct bitmap_index *bitmap_git,
2055struct bitmapped_pack *pack,
2056size_t bitmap_pos,
2057uint32_t pack_pos,
2058struct bitmap *reuse,
2059struct pack_window **w_curs)
2060{
2061off_t offset, delta_obj_offset;
2062enum object_type type;
2063unsigned long size;
2064
2065if (pack_pos >= pack->p->num_objects)
2066return -1; /* not actually in the pack */
2067
2068offset = delta_obj_offset = pack_pos_to_offset(pack->p, pack_pos);
2069type = unpack_object_header(pack->p, w_curs, &offset, &size);
2070if (type < 0)
2071return -1; /* broken packfile, punt */
2072
2073if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
2074off_t base_offset;
2075uint32_t base_pos;
2076uint32_t base_bitmap_pos;
2077
2078/*
2079* Find the position of the base object so we can look it up
2080* in our bitmaps. If we can't come up with an offset, or if
2081* that offset is not in the revidx, the pack is corrupt.
2082* There's nothing we can do, so just punt on this object,
2083* and the normal slow path will complain about it in
2084* more detail.
2085*/
2086base_offset = get_delta_base(pack->p, w_curs, &offset, type,
2087delta_obj_offset);
2088if (!base_offset)
2089return 0;
2090
2091offset_to_pack_pos(pack->p, base_offset, &base_pos);
2092
2093if (bitmap_is_midx(bitmap_git)) {
2094/*
2095* Cross-pack deltas are rejected for now, but could
2096* theoretically be supported in the future.
2097*
2098* We would need to ensure that we're sending both
2099* halves of the delta/base pair, regardless of whether
2100* or not the two cross a pack boundary. If they do,
2101* then we must convert the delta to an REF_DELTA to
2102* refer back to the base in the other pack.
2103* */
2104if (midx_pair_to_pack_pos(bitmap_git->midx,
2105pack->pack_int_id,
2106base_offset,
2107&base_bitmap_pos) < 0) {
2108return 0;
2109}
2110} else {
2111if (offset_to_pack_pos(pack->p, base_offset,
2112&base_pos) < 0)
2113return 0;
2114/*
2115* We assume delta dependencies always point backwards.
2116* This lets us do a single pass, and is basically
2117* always true due to the way OFS_DELTAs work. You would
2118* not typically find REF_DELTA in a bitmapped pack,
2119* since we only bitmap packs we write fresh, and
2120* OFS_DELTA is the default). But let's double check to
2121* make sure the pack wasn't written with odd
2122* parameters.
2123*/
2124if (base_pos >= pack_pos)
2125return 0;
2126base_bitmap_pos = pack->bitmap_pos + base_pos;
2127}
2128
2129/*
2130* And finally, if we're not sending the base as part of our
2131* reuse chunk, then don't send this object either. The base
2132* would come after us, along with other objects not
2133* necessarily in the pack, which means we'd need to convert
2134* to REF_DELTA on the fly. Better to just let the normal
2135* object_entry code path handle it.
2136*/
2137if (!bitmap_get(reuse, base_bitmap_pos))
2138return 0;
2139}
2140
2141/*
2142* If we got here, then the object is OK to reuse. Mark it.
2143*/
2144bitmap_set(reuse, bitmap_pos);
2145return 0;
2146}
2147
2148static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
2149struct bitmapped_pack *pack,
2150struct bitmap *reuse)
2151{
2152struct bitmap *result = bitmap_git->result;
2153struct pack_window *w_curs = NULL;
2154size_t pos = pack->bitmap_pos / BITS_IN_EWORD;
2155
2156if (!pack->bitmap_pos) {
2157/*
2158* If we're processing the first (in the case of a MIDX, the
2159* preferred pack) or the only (in the case of single-pack
2160* bitmaps) pack, then we can reuse whole words at a time.
2161*
2162* This is because we know that any deltas in this range *must*
2163* have their bases chosen from the same pack, since:
2164*
2165* - In the single pack case, there is no other pack to choose
2166* them from.
2167*
2168* - In the MIDX case, the first pack is the preferred pack, so
2169* all ties are broken in favor of that pack (i.e. the one
2170* we're currently processing). So any duplicate bases will be
2171* resolved in favor of the pack we're processing.
2172*/
2173while (pos < result->word_alloc &&
2174pos < pack->bitmap_nr / BITS_IN_EWORD &&
2175result->words[pos] == (eword_t)~0)
2176pos++;
2177memset(reuse->words, 0xFF, pos * sizeof(eword_t));
2178}
2179
2180for (; pos < result->word_alloc; pos++) {
2181eword_t word = result->words[pos];
2182size_t offset;
2183
2184for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2185size_t bit_pos;
2186uint32_t pack_pos;
2187
2188if (word >> offset == 0)
2189break;
2190
2191offset += ewah_bit_ctz64(word >> offset);
2192
2193bit_pos = pos * BITS_IN_EWORD + offset;
2194if (bit_pos < pack->bitmap_pos)
2195continue;
2196if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
2197goto done;
2198
2199if (bitmap_is_midx(bitmap_git)) {
2200uint32_t midx_pos;
2201off_t ofs;
2202
2203midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2204ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
2205
2206if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
2207BUG("could not find object in pack %s "
2208"at offset %"PRIuMAX" in MIDX",
2209pack_basename(pack->p), (uintmax_t)ofs);
2210} else {
2211pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
2212if (pack_pos >= pack->p->num_objects)
2213BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
2214pack_basename(pack->p), (uintmax_t)pack_pos,
2215pack->p->num_objects);
2216}
2217
2218if (try_partial_reuse(bitmap_git, pack, bit_pos,
2219pack_pos, reuse, &w_curs) < 0) {
2220/*
2221* try_partial_reuse indicated we couldn't reuse
2222* any bits, so there is no point in trying more
2223* bits in the current word, or any other words
2224* in result.
2225*
2226* Jump out of both loops to avoid future
2227* unnecessary calls to try_partial_reuse.
2228*/
2229goto done;
2230}
2231}
2232}
2233
2234done:
2235unuse_pack(&w_curs);
2236}
2237
2238static int bitmapped_pack_cmp(const void *va, const void *vb)
2239{
2240const struct bitmapped_pack *a = va;
2241const struct bitmapped_pack *b = vb;
2242
2243if (a->bitmap_pos < b->bitmap_pos)
2244return -1;
2245if (a->bitmap_pos > b->bitmap_pos)
2246return 1;
2247return 0;
2248}
2249
2250void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2251struct bitmapped_pack **packs_out,
2252size_t *packs_nr_out,
2253struct bitmap **reuse_out,
2254int multi_pack_reuse)
2255{
2256struct repository *r = the_repository;
2257struct bitmapped_pack *packs = NULL;
2258struct bitmap *result = bitmap_git->result;
2259struct bitmap *reuse;
2260size_t i;
2261size_t packs_nr = 0, packs_alloc = 0;
2262size_t word_alloc;
2263uint32_t objects_nr = 0;
2264
2265assert(result);
2266
2267load_reverse_index(r, bitmap_git);
2268
2269if (!bitmap_is_midx(bitmap_git) || !bitmap_git->midx->chunk_bitmapped_packs)
2270multi_pack_reuse = 0;
2271
2272if (multi_pack_reuse) {
2273for (i = 0; i < bitmap_git->midx->num_packs; i++) {
2274struct bitmapped_pack pack;
2275if (nth_bitmapped_pack(r, bitmap_git->midx, &pack, i) < 0) {
2276warning(_("unable to load pack: '%s', disabling pack-reuse"),
2277bitmap_git->midx->pack_names[i]);
2278free(packs);
2279return;
2280}
2281
2282if (!pack.bitmap_nr)
2283continue;
2284
2285ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2286memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2287
2288objects_nr += pack.p->num_objects;
2289}
2290
2291QSORT(packs, packs_nr, bitmapped_pack_cmp);
2292} else {
2293struct packed_git *pack;
2294uint32_t pack_int_id;
2295
2296if (bitmap_is_midx(bitmap_git)) {
2297uint32_t preferred_pack_pos;
2298
2299if (midx_preferred_pack(bitmap_git->midx, &preferred_pack_pos) < 0) {
2300warning(_("unable to compute preferred pack, disabling pack-reuse"));
2301return;
2302}
2303
2304pack = bitmap_git->midx->packs[preferred_pack_pos];
2305pack_int_id = preferred_pack_pos;
2306} else {
2307pack = bitmap_git->pack;
2308/*
2309* Any value for 'pack_int_id' will do here. When we
2310* process the pack via try_partial_reuse(), we won't
2311* use the `pack_int_id` field since we have a non-MIDX
2312* bitmap.
2313*
2314* Use '-1' as a sentinel value to make it clear
2315* that we do not expect to read this field.
2316*/
2317pack_int_id = -1;
2318}
2319
2320ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2321packs[packs_nr].p = pack;
2322packs[packs_nr].pack_int_id = pack_int_id;
2323packs[packs_nr].bitmap_nr = pack->num_objects;
2324packs[packs_nr].bitmap_pos = 0;
2325
2326objects_nr = packs[packs_nr++].bitmap_nr;
2327}
2328
2329word_alloc = objects_nr / BITS_IN_EWORD;
2330if (objects_nr % BITS_IN_EWORD)
2331word_alloc++;
2332reuse = bitmap_word_alloc(word_alloc);
2333
2334for (i = 0; i < packs_nr; i++)
2335reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse);
2336
2337if (bitmap_is_empty(reuse)) {
2338free(packs);
2339bitmap_free(reuse);
2340return;
2341}
2342
2343/*
2344* Drop any reused objects from the result, since they will not
2345* need to be handled separately.
2346*/
2347bitmap_and_not(result, reuse);
2348*packs_out = packs;
2349*packs_nr_out = packs_nr;
2350*reuse_out = reuse;
2351}
2352
2353int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2354struct bitmap *bitmap, const struct object_id *oid)
2355{
2356int idx;
2357
2358if (!bitmap)
2359return 0;
2360
2361idx = bitmap_position(bitmap_git, oid);
2362return idx >= 0 && bitmap_get(bitmap, idx);
2363}
2364
2365void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2366struct rev_info *revs,
2367show_reachable_fn show_reachable)
2368{
2369assert(bitmap_git->result);
2370
2371show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable);
2372if (revs->tree_objects)
2373show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable);
2374if (revs->blob_objects)
2375show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable);
2376if (revs->tag_objects)
2377show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable);
2378
2379show_extended_objects(bitmap_git, revs, show_reachable);
2380}
2381
2382static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2383enum object_type type)
2384{
2385struct bitmap *objects = bitmap_git->result;
2386struct eindex *eindex = &bitmap_git->ext_index;
2387
2388uint32_t i = 0, count = 0;
2389struct ewah_iterator it;
2390eword_t filter;
2391
2392init_type_iterator(&it, bitmap_git, type);
2393
2394while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
2395eword_t word = objects->words[i++] & filter;
2396count += ewah_bit_popcount64(word);
2397}
2398
2399for (i = 0; i < eindex->count; ++i) {
2400if (eindex->objects[i]->type == type &&
2401bitmap_get(objects,
2402st_add(bitmap_num_objects(bitmap_git), i)))
2403count++;
2404}
2405
2406return count;
2407}
2408
2409void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2410uint32_t *commits, uint32_t *trees,
2411uint32_t *blobs, uint32_t *tags)
2412{
2413assert(bitmap_git->result);
2414
2415if (commits)
2416*commits = count_object_type(bitmap_git, OBJ_COMMIT);
2417
2418if (trees)
2419*trees = count_object_type(bitmap_git, OBJ_TREE);
2420
2421if (blobs)
2422*blobs = count_object_type(bitmap_git, OBJ_BLOB);
2423
2424if (tags)
2425*tags = count_object_type(bitmap_git, OBJ_TAG);
2426}
2427
2428struct bitmap_test_data {
2429struct bitmap_index *bitmap_git;
2430struct bitmap *base;
2431struct bitmap *commits;
2432struct bitmap *trees;
2433struct bitmap *blobs;
2434struct bitmap *tags;
2435struct progress *prg;
2436size_t seen;
2437};
2438
2439static void test_bitmap_type(struct bitmap_test_data *tdata,
2440struct object *obj, int pos)
2441{
2442enum object_type bitmap_type = OBJ_NONE;
2443int bitmaps_nr = 0;
2444
2445if (bitmap_get(tdata->commits, pos)) {
2446bitmap_type = OBJ_COMMIT;
2447bitmaps_nr++;
2448}
2449if (bitmap_get(tdata->trees, pos)) {
2450bitmap_type = OBJ_TREE;
2451bitmaps_nr++;
2452}
2453if (bitmap_get(tdata->blobs, pos)) {
2454bitmap_type = OBJ_BLOB;
2455bitmaps_nr++;
2456}
2457if (bitmap_get(tdata->tags, pos)) {
2458bitmap_type = OBJ_TAG;
2459bitmaps_nr++;
2460}
2461
2462if (bitmap_type == OBJ_NONE)
2463die(_("object '%s' not found in type bitmaps"),
2464oid_to_hex(&obj->oid));
2465
2466if (bitmaps_nr > 1)
2467die(_("object '%s' does not have a unique type"),
2468oid_to_hex(&obj->oid));
2469
2470if (bitmap_type != obj->type)
2471die(_("object '%s': real type '%s', expected: '%s'"),
2472oid_to_hex(&obj->oid),
2473type_name(obj->type),
2474type_name(bitmap_type));
2475}
2476
2477static void test_show_object(struct object *object,
2478const char *name UNUSED,
2479void *data)
2480{
2481struct bitmap_test_data *tdata = data;
2482int bitmap_pos;
2483
2484bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2485if (bitmap_pos < 0)
2486die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2487test_bitmap_type(tdata, object, bitmap_pos);
2488
2489bitmap_set(tdata->base, bitmap_pos);
2490display_progress(tdata->prg, ++tdata->seen);
2491}
2492
2493static void test_show_commit(struct commit *commit, void *data)
2494{
2495struct bitmap_test_data *tdata = data;
2496int bitmap_pos;
2497
2498bitmap_pos = bitmap_position(tdata->bitmap_git,
2499&commit->object.oid);
2500if (bitmap_pos < 0)
2501die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2502test_bitmap_type(tdata, &commit->object, bitmap_pos);
2503
2504bitmap_set(tdata->base, bitmap_pos);
2505display_progress(tdata->prg, ++tdata->seen);
2506}
2507
2508void test_bitmap_walk(struct rev_info *revs)
2509{
2510struct object *root;
2511struct bitmap *result = NULL;
2512size_t result_popcnt;
2513struct bitmap_test_data tdata;
2514struct bitmap_index *bitmap_git;
2515struct ewah_bitmap *bm;
2516
2517if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2518die(_("failed to load bitmap indexes"));
2519
2520if (revs->pending.nr != 1)
2521die(_("you must specify exactly one commit to test"));
2522
2523fprintf_ln(stderr, "Bitmap v%d test (%d entries%s)",
2524bitmap_git->version,
2525bitmap_git->entry_count,
2526bitmap_git->table_lookup ? "" : " loaded");
2527
2528root = revs->pending.objects[0].item;
2529bm = bitmap_for_commit(bitmap_git, (struct commit *)root);
2530
2531if (bm) {
2532fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2533oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm));
2534
2535result = ewah_to_bitmap(bm);
2536}
2537
2538if (!result)
2539die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2540
2541revs->tag_objects = 1;
2542revs->tree_objects = 1;
2543revs->blob_objects = 1;
2544
2545result_popcnt = bitmap_popcount(result);
2546
2547if (prepare_revision_walk(revs))
2548die(_("revision walk setup failed"));
2549
2550tdata.bitmap_git = bitmap_git;
2551tdata.base = bitmap_new();
2552tdata.commits = ewah_to_bitmap(bitmap_git->commits);
2553tdata.trees = ewah_to_bitmap(bitmap_git->trees);
2554tdata.blobs = ewah_to_bitmap(bitmap_git->blobs);
2555tdata.tags = ewah_to_bitmap(bitmap_git->tags);
2556tdata.prg = start_progress("Verifying bitmap entries", result_popcnt);
2557tdata.seen = 0;
2558
2559traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2560
2561stop_progress(&tdata.prg);
2562
2563if (bitmap_equals(result, tdata.base))
2564fprintf_ln(stderr, "OK!");
2565else
2566die(_("mismatch in bitmap results"));
2567
2568bitmap_free(result);
2569bitmap_free(tdata.base);
2570bitmap_free(tdata.commits);
2571bitmap_free(tdata.trees);
2572bitmap_free(tdata.blobs);
2573bitmap_free(tdata.tags);
2574free_bitmap_index(bitmap_git);
2575}
2576
2577int test_bitmap_commits(struct repository *r)
2578{
2579struct object_id oid;
2580MAYBE_UNUSED void *value;
2581struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2582
2583if (!bitmap_git)
2584die(_("failed to load bitmap indexes"));
2585
2586/*
2587* As this function is only used to print bitmap selected
2588* commits, we don't have to read the commit table.
2589*/
2590if (bitmap_git->table_lookup) {
2591if (load_bitmap_entries_v1(bitmap_git) < 0)
2592die(_("failed to load bitmap indexes"));
2593}
2594
2595kh_foreach(bitmap_git->bitmaps, oid, value, {
2596printf_ln("%s", oid_to_hex(&oid));
2597});
2598
2599free_bitmap_index(bitmap_git);
2600
2601return 0;
2602}
2603
2604int test_bitmap_hashes(struct repository *r)
2605{
2606struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2607struct object_id oid;
2608uint32_t i, index_pos;
2609
2610if (!bitmap_git || !bitmap_git->hashes)
2611goto cleanup;
2612
2613for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2614if (bitmap_is_midx(bitmap_git))
2615index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2616else
2617index_pos = pack_pos_to_index(bitmap_git->pack, i);
2618
2619nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2620
2621printf_ln("%s %"PRIu32"",
2622oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2623}
2624
2625cleanup:
2626free_bitmap_index(bitmap_git);
2627
2628return 0;
2629}
2630
2631static void bit_pos_to_object_id(struct bitmap_index *bitmap_git,
2632uint32_t bit_pos,
2633struct object_id *oid)
2634{
2635uint32_t index_pos;
2636
2637if (bitmap_is_midx(bitmap_git))
2638index_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2639else
2640index_pos = pack_pos_to_index(bitmap_git->pack, bit_pos);
2641
2642nth_bitmap_object_oid(bitmap_git, oid, index_pos);
2643}
2644
2645int test_bitmap_pseudo_merges(struct repository *r)
2646{
2647struct bitmap_index *bitmap_git;
2648uint32_t i;
2649
2650bitmap_git = prepare_bitmap_git(r);
2651if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2652goto cleanup;
2653
2654for (i = 0; i < bitmap_git->pseudo_merges.nr; i++) {
2655struct pseudo_merge *merge;
2656struct ewah_bitmap *commits_bitmap, *merge_bitmap;
2657
2658merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2659&bitmap_git->pseudo_merges.v[i]);
2660commits_bitmap = merge->commits;
2661merge_bitmap = pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
2662merge);
2663
2664printf("at=%"PRIuMAX", commits=%"PRIuMAX", objects=%"PRIuMAX"\n",
2665(uintmax_t)merge->at,
2666(uintmax_t)ewah_bitmap_popcount(commits_bitmap),
2667(uintmax_t)ewah_bitmap_popcount(merge_bitmap));
2668}
2669
2670cleanup:
2671free_bitmap_index(bitmap_git);
2672return 0;
2673}
2674
2675static void dump_ewah_object_ids(struct bitmap_index *bitmap_git,
2676struct ewah_bitmap *bitmap)
2677
2678{
2679struct ewah_iterator it;
2680eword_t word;
2681uint32_t pos = 0;
2682
2683ewah_iterator_init(&it, bitmap);
2684
2685while (ewah_iterator_next(&word, &it)) {
2686struct object_id oid;
2687uint32_t offset;
2688
2689for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2690if (!(word >> offset))
2691break;
2692
2693offset += ewah_bit_ctz64(word >> offset);
2694
2695bit_pos_to_object_id(bitmap_git, pos + offset, &oid);
2696printf("%s\n", oid_to_hex(&oid));
2697}
2698pos += BITS_IN_EWORD;
2699}
2700}
2701
2702int test_bitmap_pseudo_merge_commits(struct repository *r, uint32_t n)
2703{
2704struct bitmap_index *bitmap_git;
2705struct pseudo_merge *merge;
2706int ret = 0;
2707
2708bitmap_git = prepare_bitmap_git(r);
2709if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2710goto cleanup;
2711
2712if (n >= bitmap_git->pseudo_merges.nr) {
2713ret = error(_("pseudo-merge index out of range "
2714"(%"PRIu32" >= %"PRIuMAX")"),
2715n, (uintmax_t)bitmap_git->pseudo_merges.nr);
2716goto cleanup;
2717}
2718
2719merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2720&bitmap_git->pseudo_merges.v[n]);
2721dump_ewah_object_ids(bitmap_git, merge->commits);
2722
2723cleanup:
2724free_bitmap_index(bitmap_git);
2725return ret;
2726}
2727
2728int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n)
2729{
2730struct bitmap_index *bitmap_git;
2731struct pseudo_merge *merge;
2732int ret = 0;
2733
2734bitmap_git = prepare_bitmap_git(r);
2735if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2736goto cleanup;
2737
2738if (n >= bitmap_git->pseudo_merges.nr) {
2739ret = error(_("pseudo-merge index out of range "
2740"(%"PRIu32" >= %"PRIuMAX")"),
2741n, (uintmax_t)bitmap_git->pseudo_merges.nr);
2742goto cleanup;
2743}
2744
2745merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
2746&bitmap_git->pseudo_merges.v[n]);
2747
2748dump_ewah_object_ids(bitmap_git,
2749pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
2750merge));
2751
2752cleanup:
2753free_bitmap_index(bitmap_git);
2754return ret;
2755}
2756
2757int rebuild_bitmap(const uint32_t *reposition,
2758struct ewah_bitmap *source,
2759struct bitmap *dest)
2760{
2761uint32_t pos = 0;
2762struct ewah_iterator it;
2763eword_t word;
2764
2765ewah_iterator_init(&it, source);
2766
2767while (ewah_iterator_next(&word, &it)) {
2768uint32_t offset, bit_pos;
2769
2770for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
2771if ((word >> offset) == 0)
2772break;
2773
2774offset += ewah_bit_ctz64(word >> offset);
2775
2776bit_pos = reposition[pos + offset];
2777if (bit_pos > 0)
2778bitmap_set(dest, bit_pos - 1);
2779else /* can't reuse, we don't have the object */
2780return -1;
2781}
2782
2783pos += BITS_IN_EWORD;
2784}
2785return 0;
2786}
2787
2788uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
2789struct packing_data *mapping)
2790{
2791struct repository *r = the_repository;
2792uint32_t i, num_objects;
2793uint32_t *reposition;
2794
2795if (!bitmap_is_midx(bitmap_git))
2796load_reverse_index(r, bitmap_git);
2797else if (load_midx_revindex(bitmap_git->midx))
2798BUG("rebuild_existing_bitmaps: missing required rev-cache "
2799"extension");
2800
2801num_objects = bitmap_num_objects(bitmap_git);
2802CALLOC_ARRAY(reposition, num_objects);
2803
2804for (i = 0; i < num_objects; ++i) {
2805struct object_id oid;
2806struct object_entry *oe;
2807uint32_t index_pos;
2808
2809if (bitmap_is_midx(bitmap_git))
2810index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2811else
2812index_pos = pack_pos_to_index(bitmap_git->pack, i);
2813nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2814oe = packlist_find(mapping, &oid);
2815
2816if (oe) {
2817reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
2818if (bitmap_git->hashes && !oe->hash)
2819oe->hash = get_be32(bitmap_git->hashes + index_pos);
2820}
2821}
2822
2823return reposition;
2824}
2825
2826void free_bitmap_index(struct bitmap_index *b)
2827{
2828if (!b)
2829return;
2830
2831if (b->map)
2832munmap(b->map, b->map_size);
2833ewah_pool_free(b->commits);
2834ewah_pool_free(b->trees);
2835ewah_pool_free(b->blobs);
2836ewah_pool_free(b->tags);
2837if (b->bitmaps) {
2838struct stored_bitmap *sb;
2839kh_foreach_value(b->bitmaps, sb, {
2840ewah_pool_free(sb->root);
2841free(sb);
2842});
2843}
2844kh_destroy_oid_map(b->bitmaps);
2845free(b->ext_index.objects);
2846free(b->ext_index.hashes);
2847kh_destroy_oid_pos(b->ext_index.positions);
2848bitmap_free(b->result);
2849bitmap_free(b->haves);
2850if (bitmap_is_midx(b)) {
2851/*
2852* Multi-pack bitmaps need to have resources associated with
2853* their on-disk reverse indexes unmapped so that stale .rev and
2854* .bitmap files can be removed.
2855*
2856* Unlike pack-based bitmaps, multi-pack bitmaps can be read and
2857* written in the same 'git multi-pack-index write --bitmap'
2858* process. Close resources so they can be removed safely on
2859* platforms like Windows.
2860*/
2861close_midx_revindex(b->midx);
2862}
2863free_pseudo_merge_map(&b->pseudo_merges);
2864free(b);
2865}
2866
2867int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
2868const struct object_id *oid)
2869{
2870return bitmap_git &&
2871bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
2872}
2873
2874static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
2875enum object_type object_type)
2876{
2877struct bitmap *result = bitmap_git->result;
2878off_t total = 0;
2879struct ewah_iterator it;
2880eword_t filter;
2881size_t i;
2882
2883init_type_iterator(&it, bitmap_git, object_type);
2884for (i = 0; i < result->word_alloc &&
2885ewah_iterator_next(&filter, &it); i++) {
2886eword_t word = result->words[i] & filter;
2887size_t base = (i * BITS_IN_EWORD);
2888unsigned offset;
2889
2890if (!word)
2891continue;
2892
2893for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2894if ((word >> offset) == 0)
2895break;
2896
2897offset += ewah_bit_ctz64(word >> offset);
2898
2899if (bitmap_is_midx(bitmap_git)) {
2900uint32_t pack_pos;
2901uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
2902off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
2903
2904uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
2905struct packed_git *pack = bitmap_git->midx->packs[pack_id];
2906
2907if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
2908struct object_id oid;
2909nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
2910
2911die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
2912oid_to_hex(&oid),
2913pack->pack_name,
2914(uintmax_t)offset);
2915}
2916
2917total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
2918} else {
2919size_t pos = base + offset;
2920total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
2921pack_pos_to_offset(bitmap_git->pack, pos);
2922}
2923}
2924}
2925
2926return total;
2927}
2928
2929static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
2930{
2931struct bitmap *result = bitmap_git->result;
2932struct eindex *eindex = &bitmap_git->ext_index;
2933off_t total = 0;
2934struct object_info oi = OBJECT_INFO_INIT;
2935off_t object_size;
2936size_t i;
2937
2938oi.disk_sizep = &object_size;
2939
2940for (i = 0; i < eindex->count; i++) {
2941struct object *obj = eindex->objects[i];
2942
2943if (!bitmap_get(result,
2944st_add(bitmap_num_objects(bitmap_git), i)))
2945continue;
2946
2947if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
2948die(_("unable to get disk usage of '%s'"),
2949oid_to_hex(&obj->oid));
2950
2951total += object_size;
2952}
2953return total;
2954}
2955
2956off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
2957struct rev_info *revs)
2958{
2959off_t total = 0;
2960
2961total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
2962if (revs->tree_objects)
2963total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
2964if (revs->blob_objects)
2965total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
2966if (revs->tag_objects)
2967total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
2968
2969total += get_disk_usage_for_extended(bitmap_git);
2970
2971return total;
2972}
2973
2974int bitmap_is_midx(struct bitmap_index *bitmap_git)
2975{
2976return !!bitmap_git->midx;
2977}
2978
2979const struct string_list *bitmap_preferred_tips(struct repository *r)
2980{
2981const struct string_list *dest;
2982
2983if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
2984return dest;
2985return NULL;
2986}
2987
2988int bitmap_is_preferred_refname(struct repository *r, const char *refname)
2989{
2990const struct string_list *preferred_tips = bitmap_preferred_tips(r);
2991struct string_list_item *item;
2992
2993if (!preferred_tips)
2994return 0;
2995
2996for_each_string_list_item(item, preferred_tips) {
2997if (starts_with(refname, item->string))
2998return 1;
2999}
3000
3001return 0;
3002}
3003
3004static int verify_bitmap_file(const char *name)
3005{
3006struct stat st;
3007unsigned char *data;
3008int fd = git_open(name);
3009int res = 0;
3010
3011/* It is OK to not have the file. */
3012if (fd < 0 || fstat(fd, &st)) {
3013if (fd >= 0)
3014close(fd);
3015return 0;
3016}
3017
3018data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3019close(fd);
3020if (!hashfile_checksum_valid(data, st.st_size))
3021res = error(_("bitmap file '%s' has invalid checksum"),
3022name);
3023
3024munmap(data, st.st_size);
3025return res;
3026}
3027
3028int verify_bitmap_files(struct repository *r)
3029{
3030int res = 0;
3031
3032for (struct multi_pack_index *m = get_multi_pack_index(r);
3033m; m = m->next) {
3034char *midx_bitmap_name = midx_bitmap_filename(m);
3035res |= verify_bitmap_file(midx_bitmap_name);
3036free(midx_bitmap_name);
3037}
3038
3039for (struct packed_git *p = get_all_packs(r);
3040p; p = p->next) {
3041char *pack_bitmap_name = pack_bitmap_filename(p);
3042res |= verify_bitmap_file(pack_bitmap_name);
3043free(pack_bitmap_name);
3044}
3045
3046return res;
3047}
3048