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