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