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