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