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