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