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