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