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