]>
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" |
fff42755 VM |
16 | |
17 | /* | |
18 | * An entry on the bitmap index, representing the bitmap for a given | |
19 | * commit. | |
20 | */ | |
21 | struct stored_bitmap { | |
53636539 | 22 | struct object_id oid; |
fff42755 VM |
23 | struct ewah_bitmap *root; |
24 | struct stored_bitmap *xor; | |
25 | int flags; | |
26 | }; | |
27 | ||
28 | /* | |
3ae5fa07 | 29 | * The active bitmap index for a repository. By design, repositories only have |
fff42755 VM |
30 | * a single bitmap index available (the index for the biggest packfile in |
31 | * the repository), since bitmap indexes need full closure. | |
32 | * | |
33 | * If there is more than one bitmap index available (e.g. because of alternates), | |
34 | * the active bitmap index is the largest one. | |
35 | */ | |
3ae5fa07 | 36 | struct bitmap_index { |
fff42755 VM |
37 | /* Packfile to which this bitmap index belongs to */ |
38 | struct packed_git *pack; | |
39 | ||
fff42755 VM |
40 | /* |
41 | * Mark the first `reuse_objects` in the packfile as reused: | |
42 | * they will be sent as-is without using them for repacking | |
43 | * calculations | |
44 | */ | |
45 | uint32_t reuse_objects; | |
46 | ||
47 | /* mmapped buffer of the whole bitmap index */ | |
48 | unsigned char *map; | |
49 | size_t map_size; /* size of the mmaped buffer */ | |
50 | size_t map_pos; /* current position when loading the index */ | |
51 | ||
52 | /* | |
53 | * Type indexes. | |
54 | * | |
55 | * Each bitmap marks which objects in the packfile are of the given | |
56 | * type. This provides type information when yielding the objects from | |
57 | * the packfile during a walk, which allows for better delta bases. | |
58 | */ | |
59 | struct ewah_bitmap *commits; | |
60 | struct ewah_bitmap *trees; | |
61 | struct ewah_bitmap *blobs; | |
62 | struct ewah_bitmap *tags; | |
63 | ||
3c771448 | 64 | /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */ |
65 | kh_oid_map_t *bitmaps; | |
fff42755 VM |
66 | |
67 | /* Number of bitmapped commits */ | |
68 | uint32_t entry_count; | |
69 | ||
f3c23db2 | 70 | /* If not NULL, this is a name-hash cache pointing into map. */ |
ae4f07fb VM |
71 | uint32_t *hashes; |
72 | ||
fff42755 VM |
73 | /* |
74 | * Extended index. | |
75 | * | |
76 | * When trying to perform bitmap operations with objects that are not | |
77 | * packed in `pack`, these objects are added to this "fake index" and | |
78 | * are assumed to appear at the end of the packfile for all operations | |
79 | */ | |
80 | struct eindex { | |
81 | struct object **objects; | |
82 | uint32_t *hashes; | |
83 | uint32_t count, alloc; | |
3c771448 | 84 | kh_oid_pos_t *positions; |
fff42755 VM |
85 | } ext_index; |
86 | ||
87 | /* Bitmap result of the last performed walk */ | |
88 | struct bitmap *result; | |
89 | ||
30cdc33f JK |
90 | /* "have" bitmap from the last performed walk */ |
91 | struct bitmap *haves; | |
92 | ||
fff42755 VM |
93 | /* Version of the bitmap index */ |
94 | unsigned int version; | |
3ae5fa07 | 95 | }; |
fff42755 VM |
96 | |
97 | static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st) | |
98 | { | |
99 | struct ewah_bitmap *parent; | |
100 | struct ewah_bitmap *composed; | |
101 | ||
102 | if (st->xor == NULL) | |
103 | return st->root; | |
104 | ||
105 | composed = ewah_pool_new(); | |
106 | parent = lookup_stored_bitmap(st->xor); | |
107 | ewah_xor(st->root, parent, composed); | |
108 | ||
109 | ewah_pool_free(st->root); | |
110 | st->root = composed; | |
111 | st->xor = NULL; | |
112 | ||
113 | return composed; | |
114 | } | |
115 | ||
116 | /* | |
117 | * Read a bitmap from the current read position on the mmaped | |
118 | * index, and increase the read position accordingly | |
119 | */ | |
120 | static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index) | |
121 | { | |
122 | struct ewah_bitmap *b = ewah_pool_new(); | |
123 | ||
1140bf01 | 124 | ssize_t bitmap_size = ewah_read_mmap(b, |
fff42755 VM |
125 | index->map + index->map_pos, |
126 | index->map_size - index->map_pos); | |
127 | ||
128 | if (bitmap_size < 0) { | |
129 | error("Failed to load bitmap index (corrupted?)"); | |
130 | ewah_pool_free(b); | |
131 | return NULL; | |
132 | } | |
133 | ||
134 | index->map_pos += bitmap_size; | |
135 | return b; | |
136 | } | |
137 | ||
138 | static int load_bitmap_header(struct bitmap_index *index) | |
139 | { | |
140 | struct bitmap_disk_header *header = (void *)index->map; | |
ca510902 | 141 | size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz; |
fff42755 | 142 | |
ca510902 JK |
143 | if (index->map_size < header_size + the_hash_algo->rawsz) |
144 | return error("Corrupted bitmap index (too small)"); | |
fff42755 VM |
145 | |
146 | if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0) | |
147 | return error("Corrupted bitmap index file (wrong header)"); | |
148 | ||
149 | index->version = ntohs(header->version); | |
150 | if (index->version != 1) | |
151 | return error("Unsupported version for bitmap index file (%d)", index->version); | |
152 | ||
153 | /* Parse known bitmap format options */ | |
154 | { | |
155 | uint32_t flags = ntohs(header->options); | |
ec6c7b43 JK |
156 | size_t cache_size = st_mult(index->pack->num_objects, sizeof(uint32_t)); |
157 | unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz; | |
fff42755 VM |
158 | |
159 | if ((flags & BITMAP_OPT_FULL_DAG) == 0) | |
160 | return error("Unsupported options for bitmap index file " | |
161 | "(Git requires BITMAP_OPT_FULL_DAG)"); | |
ae4f07fb VM |
162 | |
163 | if (flags & BITMAP_OPT_HASH_CACHE) { | |
ec6c7b43 JK |
164 | if (cache_size > index_end - index->map - header_size) |
165 | return error("corrupted bitmap index file (too short to fit hash cache)"); | |
166 | index->hashes = (void *)(index_end - cache_size); | |
167 | index_end -= cache_size; | |
ae4f07fb | 168 | } |
fff42755 VM |
169 | } |
170 | ||
171 | index->entry_count = ntohl(header->entry_count); | |
ca510902 | 172 | index->map_pos += header_size; |
fff42755 VM |
173 | return 0; |
174 | } | |
175 | ||
176 | static struct stored_bitmap *store_bitmap(struct bitmap_index *index, | |
177 | struct ewah_bitmap *root, | |
500e4f23 | 178 | const struct object_id *oid, |
fff42755 VM |
179 | struct stored_bitmap *xor_with, |
180 | int flags) | |
181 | { | |
182 | struct stored_bitmap *stored; | |
183 | khiter_t hash_pos; | |
184 | int ret; | |
185 | ||
186 | stored = xmalloc(sizeof(struct stored_bitmap)); | |
187 | stored->root = root; | |
188 | stored->xor = xor_with; | |
189 | stored->flags = flags; | |
500e4f23 | 190 | oidcpy(&stored->oid, oid); |
fff42755 | 191 | |
3c771448 | 192 | hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret); |
fff42755 VM |
193 | |
194 | /* a 0 return code means the insertion succeeded with no changes, | |
195 | * because the SHA1 already existed on the map. this is bad, there | |
196 | * shouldn't be duplicated commits in the index */ | |
197 | if (ret == 0) { | |
500e4f23 | 198 | error("Duplicate entry in bitmap index: %s", oid_to_hex(oid)); |
fff42755 VM |
199 | return NULL; |
200 | } | |
201 | ||
202 | kh_value(index->bitmaps, hash_pos) = stored; | |
203 | return stored; | |
204 | } | |
205 | ||
b5007211 KB |
206 | static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos) |
207 | { | |
208 | uint32_t result = get_be32(buffer + *pos); | |
209 | (*pos) += sizeof(result); | |
210 | return result; | |
211 | } | |
212 | ||
213 | static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos) | |
214 | { | |
215 | return buffer[(*pos)++]; | |
216 | } | |
217 | ||
599dc766 RS |
218 | #define MAX_XOR_OFFSET 160 |
219 | ||
fff42755 VM |
220 | static int load_bitmap_entries_v1(struct bitmap_index *index) |
221 | { | |
fff42755 | 222 | uint32_t i; |
599dc766 | 223 | struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL }; |
fff42755 VM |
224 | |
225 | for (i = 0; i < index->entry_count; ++i) { | |
226 | int xor_offset, flags; | |
227 | struct ewah_bitmap *bitmap = NULL; | |
228 | struct stored_bitmap *xor_bitmap = NULL; | |
229 | uint32_t commit_idx_pos; | |
500e4f23 | 230 | struct object_id oid; |
fff42755 | 231 | |
c6b0c391 TB |
232 | if (index->map_size - index->map_pos < 6) |
233 | return error("corrupt ewah bitmap: truncated header for entry %d", i); | |
234 | ||
b5007211 KB |
235 | commit_idx_pos = read_be32(index->map, &index->map_pos); |
236 | xor_offset = read_u8(index->map, &index->map_pos); | |
237 | flags = read_u8(index->map, &index->map_pos); | |
fff42755 | 238 | |
c6b0c391 TB |
239 | if (nth_packed_object_id(&oid, index->pack, commit_idx_pos) < 0) |
240 | return error("corrupt ewah bitmap: commit index %u out of range", | |
241 | (unsigned)commit_idx_pos); | |
fff42755 | 242 | |
fff42755 VM |
243 | bitmap = read_bitmap_1(index); |
244 | if (!bitmap) | |
245 | return -1; | |
246 | ||
247 | if (xor_offset > MAX_XOR_OFFSET || xor_offset > i) | |
248 | return error("Corrupted bitmap pack index"); | |
249 | ||
250 | if (xor_offset > 0) { | |
251 | xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET]; | |
252 | ||
253 | if (xor_bitmap == NULL) | |
254 | return error("Invalid XOR offset in bitmap pack index"); | |
255 | } | |
256 | ||
257 | recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap( | |
500e4f23 | 258 | index, bitmap, &oid, xor_bitmap, flags); |
fff42755 VM |
259 | } |
260 | ||
261 | return 0; | |
262 | } | |
263 | ||
cb468050 JH |
264 | static char *pack_bitmap_filename(struct packed_git *p) |
265 | { | |
9ae97018 | 266 | size_t len; |
cb468050 | 267 | |
9ae97018 | 268 | if (!strip_suffix(p->pack_name, ".pack", &len)) |
033abf97 | 269 | BUG("pack_name does not end in .pack"); |
9ae97018 | 270 | return xstrfmt("%.*s.bitmap", (int)len, p->pack_name); |
cb468050 JH |
271 | } |
272 | ||
3ae5fa07 | 273 | static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile) |
fff42755 VM |
274 | { |
275 | int fd; | |
276 | struct stat st; | |
277 | char *idx_name; | |
278 | ||
279 | if (open_pack_index(packfile)) | |
280 | return -1; | |
281 | ||
282 | idx_name = pack_bitmap_filename(packfile); | |
a5436b57 | 283 | fd = git_open(idx_name); |
fff42755 VM |
284 | free(idx_name); |
285 | ||
286 | if (fd < 0) | |
287 | return -1; | |
288 | ||
289 | if (fstat(fd, &st)) { | |
290 | close(fd); | |
291 | return -1; | |
292 | } | |
293 | ||
3ae5fa07 | 294 | if (bitmap_git->pack) { |
fff42755 VM |
295 | warning("ignoring extra bitmap file: %s", packfile->pack_name); |
296 | close(fd); | |
297 | return -1; | |
298 | } | |
299 | ||
3ae5fa07 JT |
300 | bitmap_git->pack = packfile; |
301 | bitmap_git->map_size = xsize_t(st.st_size); | |
302 | bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
303 | bitmap_git->map_pos = 0; | |
fff42755 VM |
304 | close(fd); |
305 | ||
3ae5fa07 JT |
306 | if (load_bitmap_header(bitmap_git) < 0) { |
307 | munmap(bitmap_git->map, bitmap_git->map_size); | |
308 | bitmap_git->map = NULL; | |
309 | bitmap_git->map_size = 0; | |
fff42755 VM |
310 | return -1; |
311 | } | |
312 | ||
313 | return 0; | |
314 | } | |
315 | ||
3ae5fa07 | 316 | static int load_pack_bitmap(struct bitmap_index *bitmap_git) |
fff42755 | 317 | { |
199c86be | 318 | assert(bitmap_git->map); |
fff42755 | 319 | |
3c771448 | 320 | bitmap_git->bitmaps = kh_init_oid_map(); |
321 | bitmap_git->ext_index.positions = kh_init_oid_pos(); | |
4828ce98 JK |
322 | if (load_pack_revindex(bitmap_git->pack)) |
323 | goto failed; | |
fff42755 | 324 | |
3ae5fa07 JT |
325 | if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) || |
326 | !(bitmap_git->trees = read_bitmap_1(bitmap_git)) || | |
327 | !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) || | |
328 | !(bitmap_git->tags = read_bitmap_1(bitmap_git))) | |
fff42755 VM |
329 | goto failed; |
330 | ||
3ae5fa07 | 331 | if (load_bitmap_entries_v1(bitmap_git) < 0) |
fff42755 VM |
332 | goto failed; |
333 | ||
fff42755 VM |
334 | return 0; |
335 | ||
336 | failed: | |
3ae5fa07 JT |
337 | munmap(bitmap_git->map, bitmap_git->map_size); |
338 | bitmap_git->map = NULL; | |
339 | bitmap_git->map_size = 0; | |
bb514de3 JK |
340 | |
341 | kh_destroy_oid_map(bitmap_git->bitmaps); | |
342 | bitmap_git->bitmaps = NULL; | |
343 | ||
344 | kh_destroy_oid_pos(bitmap_git->ext_index.positions); | |
345 | bitmap_git->ext_index.positions = NULL; | |
346 | ||
fff42755 VM |
347 | return -1; |
348 | } | |
349 | ||
7c141127 NTND |
350 | static int open_pack_bitmap(struct repository *r, |
351 | struct bitmap_index *bitmap_git) | |
fff42755 VM |
352 | { |
353 | struct packed_git *p; | |
354 | int ret = -1; | |
355 | ||
199c86be | 356 | assert(!bitmap_git->map); |
fff42755 | 357 | |
7c141127 | 358 | for (p = get_all_packs(r); p; p = p->next) { |
3ae5fa07 | 359 | if (open_pack_bitmap_1(bitmap_git, p) == 0) |
fff42755 VM |
360 | ret = 0; |
361 | } | |
362 | ||
363 | return ret; | |
364 | } | |
365 | ||
7c141127 | 366 | struct bitmap_index *prepare_bitmap_git(struct repository *r) |
fff42755 | 367 | { |
3ae5fa07 | 368 | struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git)); |
fff42755 | 369 | |
7c141127 | 370 | if (!open_pack_bitmap(r, bitmap_git) && !load_pack_bitmap(bitmap_git)) |
3ae5fa07 | 371 | return bitmap_git; |
fff42755 | 372 | |
f3c23db2 | 373 | free_bitmap_index(bitmap_git); |
3ae5fa07 | 374 | return NULL; |
fff42755 VM |
375 | } |
376 | ||
377 | struct include_data { | |
3ae5fa07 | 378 | struct bitmap_index *bitmap_git; |
fff42755 VM |
379 | struct bitmap *base; |
380 | struct bitmap *seen; | |
381 | }; | |
382 | ||
3ae5fa07 | 383 | static inline int bitmap_position_extended(struct bitmap_index *bitmap_git, |
3c771448 | 384 | const struct object_id *oid) |
fff42755 | 385 | { |
4ed43d16 | 386 | kh_oid_pos_t *positions = bitmap_git->ext_index.positions; |
3c771448 | 387 | khiter_t pos = kh_get_oid_pos(positions, *oid); |
fff42755 VM |
388 | |
389 | if (pos < kh_end(positions)) { | |
390 | int bitmap_pos = kh_value(positions, pos); | |
3ae5fa07 | 391 | return bitmap_pos + bitmap_git->pack->num_objects; |
fff42755 VM |
392 | } |
393 | ||
394 | return -1; | |
395 | } | |
396 | ||
3ae5fa07 | 397 | static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git, |
3c771448 | 398 | const struct object_id *oid) |
fff42755 | 399 | { |
3c771448 | 400 | off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack); |
fff42755 VM |
401 | if (!offset) |
402 | return -1; | |
403 | ||
3ae5fa07 | 404 | return find_revindex_position(bitmap_git->pack, offset); |
fff42755 VM |
405 | } |
406 | ||
3ae5fa07 | 407 | static int bitmap_position(struct bitmap_index *bitmap_git, |
3c771448 | 408 | const struct object_id *oid) |
fff42755 | 409 | { |
3c771448 | 410 | int pos = bitmap_position_packfile(bitmap_git, oid); |
411 | return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid); | |
fff42755 VM |
412 | } |
413 | ||
3ae5fa07 JT |
414 | static int ext_index_add_object(struct bitmap_index *bitmap_git, |
415 | struct object *object, const char *name) | |
fff42755 | 416 | { |
3ae5fa07 | 417 | struct eindex *eindex = &bitmap_git->ext_index; |
fff42755 VM |
418 | |
419 | khiter_t hash_pos; | |
420 | int hash_ret; | |
421 | int bitmap_pos; | |
422 | ||
3c771448 | 423 | hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret); |
fff42755 VM |
424 | if (hash_ret > 0) { |
425 | if (eindex->count >= eindex->alloc) { | |
426 | eindex->alloc = (eindex->alloc + 16) * 3 / 2; | |
2756ca43 RS |
427 | REALLOC_ARRAY(eindex->objects, eindex->alloc); |
428 | REALLOC_ARRAY(eindex->hashes, eindex->alloc); | |
fff42755 VM |
429 | } |
430 | ||
431 | bitmap_pos = eindex->count; | |
432 | eindex->objects[eindex->count] = object; | |
433 | eindex->hashes[eindex->count] = pack_name_hash(name); | |
434 | kh_value(eindex->positions, hash_pos) = bitmap_pos; | |
435 | eindex->count++; | |
436 | } else { | |
437 | bitmap_pos = kh_value(eindex->positions, hash_pos); | |
438 | } | |
439 | ||
3ae5fa07 | 440 | return bitmap_pos + bitmap_git->pack->num_objects; |
fff42755 VM |
441 | } |
442 | ||
3ae5fa07 JT |
443 | struct bitmap_show_data { |
444 | struct bitmap_index *bitmap_git; | |
445 | struct bitmap *base; | |
446 | }; | |
447 | ||
448 | static void show_object(struct object *object, const char *name, void *data_) | |
fff42755 | 449 | { |
3ae5fa07 | 450 | struct bitmap_show_data *data = data_; |
fff42755 VM |
451 | int bitmap_pos; |
452 | ||
3c771448 | 453 | bitmap_pos = bitmap_position(data->bitmap_git, &object->oid); |
fff42755 | 454 | |
de1e67d0 | 455 | if (bitmap_pos < 0) |
3ae5fa07 JT |
456 | bitmap_pos = ext_index_add_object(data->bitmap_git, object, |
457 | name); | |
fff42755 | 458 | |
3ae5fa07 | 459 | bitmap_set(data->base, bitmap_pos); |
fff42755 VM |
460 | } |
461 | ||
462 | static void show_commit(struct commit *commit, void *data) | |
463 | { | |
464 | } | |
465 | ||
3ae5fa07 JT |
466 | static int add_to_include_set(struct bitmap_index *bitmap_git, |
467 | struct include_data *data, | |
3c771448 | 468 | const struct object_id *oid, |
fff42755 VM |
469 | int bitmap_pos) |
470 | { | |
471 | khiter_t hash_pos; | |
472 | ||
473 | if (data->seen && bitmap_get(data->seen, bitmap_pos)) | |
474 | return 0; | |
475 | ||
476 | if (bitmap_get(data->base, bitmap_pos)) | |
477 | return 0; | |
478 | ||
3c771448 | 479 | hash_pos = kh_get_oid_map(bitmap_git->bitmaps, *oid); |
3ae5fa07 JT |
480 | if (hash_pos < kh_end(bitmap_git->bitmaps)) { |
481 | struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, hash_pos); | |
fff42755 VM |
482 | bitmap_or_ewah(data->base, lookup_stored_bitmap(st)); |
483 | return 0; | |
484 | } | |
485 | ||
486 | bitmap_set(data->base, bitmap_pos); | |
487 | return 1; | |
488 | } | |
489 | ||
490 | static int should_include(struct commit *commit, void *_data) | |
491 | { | |
492 | struct include_data *data = _data; | |
493 | int bitmap_pos; | |
494 | ||
3c771448 | 495 | bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid); |
fff42755 | 496 | if (bitmap_pos < 0) |
3ae5fa07 JT |
497 | bitmap_pos = ext_index_add_object(data->bitmap_git, |
498 | (struct object *)commit, | |
499 | NULL); | |
fff42755 | 500 | |
3c771448 | 501 | if (!add_to_include_set(data->bitmap_git, data, &commit->object.oid, |
3ae5fa07 | 502 | bitmap_pos)) { |
fff42755 VM |
503 | struct commit_list *parent = commit->parents; |
504 | ||
505 | while (parent) { | |
506 | parent->item->object.flags |= SEEN; | |
507 | parent = parent->next; | |
508 | } | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
513 | return 1; | |
514 | } | |
515 | ||
3ae5fa07 JT |
516 | static struct bitmap *find_objects(struct bitmap_index *bitmap_git, |
517 | struct rev_info *revs, | |
fff42755 | 518 | struct object_list *roots, |
9639474b JK |
519 | struct bitmap *seen, |
520 | struct list_objects_filter_options *filter) | |
fff42755 VM |
521 | { |
522 | struct bitmap *base = NULL; | |
523 | int needs_walk = 0; | |
524 | ||
525 | struct object_list *not_mapped = NULL; | |
526 | ||
527 | /* | |
528 | * Go through all the roots for the walk. The ones that have bitmaps | |
529 | * on the bitmap index will be `or`ed together to form an initial | |
530 | * global reachability analysis. | |
531 | * | |
532 | * The ones without bitmaps in the index will be stored in the | |
533 | * `not_mapped_list` for further processing. | |
534 | */ | |
535 | while (roots) { | |
536 | struct object *object = roots->item; | |
537 | roots = roots->next; | |
538 | ||
539 | if (object->type == OBJ_COMMIT) { | |
3c771448 | 540 | khiter_t pos = kh_get_oid_map(bitmap_git->bitmaps, object->oid); |
fff42755 | 541 | |
3ae5fa07 JT |
542 | if (pos < kh_end(bitmap_git->bitmaps)) { |
543 | struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, pos); | |
fff42755 VM |
544 | struct ewah_bitmap *or_with = lookup_stored_bitmap(st); |
545 | ||
546 | if (base == NULL) | |
547 | base = ewah_to_bitmap(or_with); | |
548 | else | |
549 | bitmap_or_ewah(base, or_with); | |
550 | ||
551 | object->flags |= SEEN; | |
552 | continue; | |
553 | } | |
554 | } | |
555 | ||
556 | object_list_insert(object, ¬_mapped); | |
557 | } | |
558 | ||
559 | /* | |
560 | * Best case scenario: We found bitmaps for all the roots, | |
561 | * so the resulting `or` bitmap has the full reachability analysis | |
562 | */ | |
563 | if (not_mapped == NULL) | |
564 | return base; | |
565 | ||
566 | roots = not_mapped; | |
567 | ||
568 | /* | |
569 | * Let's iterate through all the roots that don't have bitmaps to | |
570 | * check if we can determine them to be reachable from the existing | |
571 | * global bitmap. | |
572 | * | |
573 | * If we cannot find them in the existing global bitmap, we'll need | |
574 | * to push them to an actual walk and run it until we can confirm | |
575 | * they are reachable | |
576 | */ | |
577 | while (roots) { | |
578 | struct object *object = roots->item; | |
579 | int pos; | |
580 | ||
581 | roots = roots->next; | |
3c771448 | 582 | pos = bitmap_position(bitmap_git, &object->oid); |
fff42755 VM |
583 | |
584 | if (pos < 0 || base == NULL || !bitmap_get(base, pos)) { | |
585 | object->flags &= ~UNINTERESTING; | |
586 | add_pending_object(revs, object, ""); | |
587 | needs_walk = 1; | |
588 | } else { | |
589 | object->flags |= SEEN; | |
590 | } | |
591 | } | |
592 | ||
593 | if (needs_walk) { | |
594 | struct include_data incdata; | |
3ae5fa07 | 595 | struct bitmap_show_data show_data; |
fff42755 VM |
596 | |
597 | if (base == NULL) | |
598 | base = bitmap_new(); | |
599 | ||
3ae5fa07 | 600 | incdata.bitmap_git = bitmap_git; |
fff42755 VM |
601 | incdata.base = base; |
602 | incdata.seen = seen; | |
603 | ||
604 | revs->include_check = should_include; | |
605 | revs->include_check_data = &incdata; | |
606 | ||
607 | if (prepare_revision_walk(revs)) | |
608 | die("revision walk setup failed"); | |
609 | ||
3ae5fa07 JT |
610 | show_data.bitmap_git = bitmap_git; |
611 | show_data.base = base; | |
612 | ||
9639474b JK |
613 | traverse_commit_list_filtered(filter, revs, |
614 | show_commit, show_object, | |
615 | &show_data, NULL); | |
fff42755 VM |
616 | } |
617 | ||
618 | return base; | |
619 | } | |
620 | ||
3ae5fa07 | 621 | static void show_extended_objects(struct bitmap_index *bitmap_git, |
4eb707eb | 622 | struct rev_info *revs, |
fff42755 VM |
623 | show_reachable_fn show_reach) |
624 | { | |
3ae5fa07 JT |
625 | struct bitmap *objects = bitmap_git->result; |
626 | struct eindex *eindex = &bitmap_git->ext_index; | |
fff42755 VM |
627 | uint32_t i; |
628 | ||
629 | for (i = 0; i < eindex->count; ++i) { | |
630 | struct object *obj; | |
631 | ||
3ae5fa07 | 632 | if (!bitmap_get(objects, bitmap_git->pack->num_objects + i)) |
fff42755 VM |
633 | continue; |
634 | ||
635 | obj = eindex->objects[i]; | |
4eb707eb JK |
636 | if ((obj->type == OBJ_BLOB && !revs->blob_objects) || |
637 | (obj->type == OBJ_TREE && !revs->tree_objects) || | |
638 | (obj->type == OBJ_TAG && !revs->tag_objects)) | |
639 | continue; | |
640 | ||
20664967 | 641 | show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0); |
fff42755 VM |
642 | } |
643 | } | |
644 | ||
551cf8b6 JK |
645 | static void init_type_iterator(struct ewah_iterator *it, |
646 | struct bitmap_index *bitmap_git, | |
647 | enum object_type type) | |
648 | { | |
649 | switch (type) { | |
650 | case OBJ_COMMIT: | |
651 | ewah_iterator_init(it, bitmap_git->commits); | |
652 | break; | |
653 | ||
654 | case OBJ_TREE: | |
655 | ewah_iterator_init(it, bitmap_git->trees); | |
656 | break; | |
657 | ||
658 | case OBJ_BLOB: | |
659 | ewah_iterator_init(it, bitmap_git->blobs); | |
660 | break; | |
661 | ||
662 | case OBJ_TAG: | |
663 | ewah_iterator_init(it, bitmap_git->tags); | |
664 | break; | |
665 | ||
666 | default: | |
667 | BUG("object type %d not stored by bitmap type index", type); | |
668 | break; | |
669 | } | |
670 | } | |
671 | ||
fff42755 | 672 | static void show_objects_for_type( |
3ae5fa07 | 673 | struct bitmap_index *bitmap_git, |
fff42755 VM |
674 | enum object_type object_type, |
675 | show_reachable_fn show_reach) | |
676 | { | |
d2ea0310 | 677 | size_t i = 0; |
fff42755 VM |
678 | uint32_t offset; |
679 | ||
680 | struct ewah_iterator it; | |
681 | eword_t filter; | |
682 | ||
3ae5fa07 JT |
683 | struct bitmap *objects = bitmap_git->result; |
684 | ||
551cf8b6 | 685 | init_type_iterator(&it, bitmap_git, object_type); |
fff42755 | 686 | |
d2ea0310 JK |
687 | for (i = 0; i < objects->word_alloc && |
688 | ewah_iterator_next(&filter, &it); i++) { | |
fff42755 | 689 | eword_t word = objects->words[i] & filter; |
d2ea0310 JK |
690 | size_t pos = (i * BITS_IN_EWORD); |
691 | ||
692 | if (!word) | |
693 | continue; | |
fff42755 | 694 | |
34b935c0 | 695 | for (offset = 0; offset < BITS_IN_EWORD; ++offset) { |
20664967 | 696 | struct object_id oid; |
fff42755 VM |
697 | struct revindex_entry *entry; |
698 | uint32_t hash = 0; | |
699 | ||
700 | if ((word >> offset) == 0) | |
701 | break; | |
702 | ||
703 | offset += ewah_bit_ctz64(word >> offset); | |
704 | ||
3ae5fa07 | 705 | entry = &bitmap_git->pack->revindex[pos + offset]; |
0763671b | 706 | nth_packed_object_id(&oid, bitmap_git->pack, entry->nr); |
fff42755 | 707 | |
3ae5fa07 JT |
708 | if (bitmap_git->hashes) |
709 | hash = get_be32(bitmap_git->hashes + entry->nr); | |
ae4f07fb | 710 | |
3ae5fa07 | 711 | show_reach(&oid, object_type, 0, hash, bitmap_git->pack, entry->offset); |
fff42755 | 712 | } |
fff42755 VM |
713 | } |
714 | } | |
715 | ||
3ae5fa07 JT |
716 | static int in_bitmapped_pack(struct bitmap_index *bitmap_git, |
717 | struct object_list *roots) | |
fff42755 VM |
718 | { |
719 | while (roots) { | |
720 | struct object *object = roots->item; | |
721 | roots = roots->next; | |
722 | ||
3ae5fa07 | 723 | if (find_pack_entry_one(object->oid.hash, bitmap_git->pack) > 0) |
fff42755 VM |
724 | return 1; |
725 | } | |
726 | ||
727 | return 0; | |
728 | } | |
729 | ||
856e12c1 TB |
730 | static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git, |
731 | struct object_list *tip_objects, | |
732 | enum object_type type) | |
4f3bd560 JK |
733 | { |
734 | struct bitmap *result = bitmap_new(); | |
735 | struct object_list *p; | |
736 | ||
737 | for (p = tip_objects; p; p = p->next) { | |
738 | int pos; | |
739 | ||
856e12c1 | 740 | if (p->item->type != type) |
4f3bd560 JK |
741 | continue; |
742 | ||
743 | pos = bitmap_position(bitmap_git, &p->item->oid); | |
744 | if (pos < 0) | |
745 | continue; | |
746 | ||
747 | bitmap_set(result, pos); | |
748 | } | |
749 | ||
750 | return result; | |
751 | } | |
752 | ||
856e12c1 TB |
753 | static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git, |
754 | struct object_list *tip_objects, | |
755 | struct bitmap *to_filter, | |
756 | enum object_type type) | |
4f3bd560 JK |
757 | { |
758 | struct eindex *eindex = &bitmap_git->ext_index; | |
759 | struct bitmap *tips; | |
760 | struct ewah_iterator it; | |
761 | eword_t mask; | |
762 | uint32_t i; | |
763 | ||
b0a8d482 | 764 | if (type != OBJ_BLOB && type != OBJ_TREE) |
856e12c1 TB |
765 | BUG("filter_bitmap_exclude_type: unsupported type '%d'", type); |
766 | ||
4f3bd560 JK |
767 | /* |
768 | * The non-bitmap version of this filter never removes | |
856e12c1 | 769 | * objects which the other side specifically asked for, |
4f3bd560 JK |
770 | * so we must match that behavior. |
771 | */ | |
856e12c1 | 772 | tips = find_tip_objects(bitmap_git, tip_objects, type); |
4f3bd560 JK |
773 | |
774 | /* | |
775 | * We can use the blob type-bitmap to work in whole words | |
776 | * for the objects that are actually in the bitmapped packfile. | |
777 | */ | |
856e12c1 | 778 | for (i = 0, init_type_iterator(&it, bitmap_git, type); |
4f3bd560 JK |
779 | i < to_filter->word_alloc && ewah_iterator_next(&mask, &it); |
780 | i++) { | |
781 | if (i < tips->word_alloc) | |
782 | mask &= ~tips->words[i]; | |
783 | to_filter->words[i] &= ~mask; | |
784 | } | |
785 | ||
786 | /* | |
787 | * Clear any blobs that weren't in the packfile (and so would not have | |
788 | * been caught by the loop above. We'll have to check them | |
789 | * individually. | |
790 | */ | |
791 | for (i = 0; i < eindex->count; i++) { | |
792 | uint32_t pos = i + bitmap_git->pack->num_objects; | |
856e12c1 | 793 | if (eindex->objects[i]->type == type && |
4f3bd560 JK |
794 | bitmap_get(to_filter, pos) && |
795 | !bitmap_get(tips, pos)) | |
796 | bitmap_unset(to_filter, pos); | |
797 | } | |
798 | ||
799 | bitmap_free(tips); | |
800 | } | |
801 | ||
856e12c1 TB |
802 | static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git, |
803 | struct object_list *tip_objects, | |
804 | struct bitmap *to_filter) | |
805 | { | |
806 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, | |
807 | OBJ_BLOB); | |
808 | } | |
809 | ||
84243da1 JK |
810 | static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git, |
811 | uint32_t pos) | |
812 | { | |
813 | struct packed_git *pack = bitmap_git->pack; | |
814 | unsigned long size; | |
815 | struct object_info oi = OBJECT_INFO_INIT; | |
816 | ||
817 | oi.sizep = &size; | |
818 | ||
819 | if (pos < pack->num_objects) { | |
820 | struct revindex_entry *entry = &pack->revindex[pos]; | |
821 | if (packed_object_info(the_repository, pack, | |
822 | entry->offset, &oi) < 0) { | |
823 | struct object_id oid; | |
e8e71848 | 824 | nth_packed_object_id(&oid, pack, entry->nr); |
84243da1 JK |
825 | die(_("unable to get size of %s"), oid_to_hex(&oid)); |
826 | } | |
827 | } else { | |
828 | struct eindex *eindex = &bitmap_git->ext_index; | |
829 | struct object *obj = eindex->objects[pos - pack->num_objects]; | |
830 | if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0) | |
831 | die(_("unable to get size of %s"), oid_to_hex(&obj->oid)); | |
832 | } | |
833 | ||
834 | return size; | |
835 | } | |
836 | ||
837 | static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git, | |
838 | struct object_list *tip_objects, | |
839 | struct bitmap *to_filter, | |
840 | unsigned long limit) | |
841 | { | |
842 | struct eindex *eindex = &bitmap_git->ext_index; | |
843 | struct bitmap *tips; | |
844 | struct ewah_iterator it; | |
845 | eword_t mask; | |
846 | uint32_t i; | |
847 | ||
856e12c1 | 848 | tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB); |
84243da1 JK |
849 | |
850 | for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB); | |
851 | i < to_filter->word_alloc && ewah_iterator_next(&mask, &it); | |
852 | i++) { | |
853 | eword_t word = to_filter->words[i] & mask; | |
854 | unsigned offset; | |
855 | ||
856 | for (offset = 0; offset < BITS_IN_EWORD; offset++) { | |
857 | uint32_t pos; | |
858 | ||
859 | if ((word >> offset) == 0) | |
860 | break; | |
861 | offset += ewah_bit_ctz64(word >> offset); | |
862 | pos = i * BITS_IN_EWORD + offset; | |
863 | ||
864 | if (!bitmap_get(tips, pos) && | |
865 | get_size_by_pos(bitmap_git, pos) >= limit) | |
866 | bitmap_unset(to_filter, pos); | |
867 | } | |
868 | } | |
869 | ||
870 | for (i = 0; i < eindex->count; i++) { | |
871 | uint32_t pos = i + bitmap_git->pack->num_objects; | |
872 | if (eindex->objects[i]->type == OBJ_BLOB && | |
873 | bitmap_get(to_filter, pos) && | |
874 | !bitmap_get(tips, pos) && | |
875 | get_size_by_pos(bitmap_git, pos) >= limit) | |
876 | bitmap_unset(to_filter, pos); | |
877 | } | |
878 | ||
879 | bitmap_free(tips); | |
880 | } | |
881 | ||
b0a8d482 TB |
882 | static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git, |
883 | struct object_list *tip_objects, | |
884 | struct bitmap *to_filter, | |
885 | unsigned long limit) | |
886 | { | |
887 | if (limit) | |
888 | BUG("filter_bitmap_tree_depth given non-zero limit"); | |
889 | ||
890 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, | |
891 | OBJ_TREE); | |
892 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, | |
893 | OBJ_BLOB); | |
894 | } | |
895 | ||
6663ae0a JK |
896 | static int filter_bitmap(struct bitmap_index *bitmap_git, |
897 | struct object_list *tip_objects, | |
898 | struct bitmap *to_filter, | |
899 | struct list_objects_filter_options *filter) | |
900 | { | |
901 | if (!filter || filter->choice == LOFC_DISABLED) | |
902 | return 0; | |
903 | ||
4f3bd560 JK |
904 | if (filter->choice == LOFC_BLOB_NONE) { |
905 | if (bitmap_git) | |
906 | filter_bitmap_blob_none(bitmap_git, tip_objects, | |
907 | to_filter); | |
908 | return 0; | |
909 | } | |
910 | ||
84243da1 JK |
911 | if (filter->choice == LOFC_BLOB_LIMIT) { |
912 | if (bitmap_git) | |
913 | filter_bitmap_blob_limit(bitmap_git, tip_objects, | |
914 | to_filter, | |
915 | filter->blob_limit_value); | |
916 | return 0; | |
917 | } | |
918 | ||
b0a8d482 TB |
919 | if (filter->choice == LOFC_TREE_DEPTH && |
920 | filter->tree_exclude_depth == 0) { | |
921 | if (bitmap_git) | |
922 | filter_bitmap_tree_depth(bitmap_git, tip_objects, | |
923 | to_filter, | |
924 | filter->tree_exclude_depth); | |
925 | return 0; | |
926 | } | |
927 | ||
6663ae0a JK |
928 | /* filter choice not handled */ |
929 | return -1; | |
930 | } | |
931 | ||
932 | static int can_filter_bitmap(struct list_objects_filter_options *filter) | |
933 | { | |
934 | return !filter_bitmap(NULL, NULL, NULL, filter); | |
935 | } | |
936 | ||
937 | struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs, | |
938 | struct list_objects_filter_options *filter) | |
fff42755 VM |
939 | { |
940 | unsigned int i; | |
fff42755 VM |
941 | |
942 | struct object_list *wants = NULL; | |
943 | struct object_list *haves = NULL; | |
944 | ||
945 | struct bitmap *wants_bitmap = NULL; | |
946 | struct bitmap *haves_bitmap = NULL; | |
947 | ||
d90fe06e JK |
948 | struct bitmap_index *bitmap_git; |
949 | ||
950 | /* | |
951 | * We can't do pathspec limiting with bitmaps, because we don't know | |
952 | * which commits are associated with which object changes (let alone | |
953 | * even which objects are associated with which paths). | |
954 | */ | |
955 | if (revs->prune) | |
956 | return NULL; | |
957 | ||
6663ae0a JK |
958 | if (!can_filter_bitmap(filter)) |
959 | return NULL; | |
960 | ||
3ae5fa07 JT |
961 | /* try to open a bitmapped pack, but don't parse it yet |
962 | * because we may not need to use it */ | |
d90fe06e | 963 | bitmap_git = xcalloc(1, sizeof(*bitmap_git)); |
7c141127 | 964 | if (open_pack_bitmap(revs->repo, bitmap_git) < 0) |
f3c23db2 | 965 | goto cleanup; |
fff42755 | 966 | |
4d01a7fa MÅ |
967 | for (i = 0; i < revs->pending.nr; ++i) { |
968 | struct object *object = revs->pending.objects[i].item; | |
fff42755 VM |
969 | |
970 | if (object->type == OBJ_NONE) | |
c251c83d | 971 | parse_object_or_die(&object->oid, NULL); |
fff42755 VM |
972 | |
973 | while (object->type == OBJ_TAG) { | |
974 | struct tag *tag = (struct tag *) object; | |
975 | ||
976 | if (object->flags & UNINTERESTING) | |
977 | object_list_insert(object, &haves); | |
978 | else | |
979 | object_list_insert(object, &wants); | |
980 | ||
dad3f060 | 981 | object = parse_object_or_die(get_tagged_oid(tag), NULL); |
fff42755 VM |
982 | } |
983 | ||
984 | if (object->flags & UNINTERESTING) | |
985 | object_list_insert(object, &haves); | |
986 | else | |
987 | object_list_insert(object, &wants); | |
988 | } | |
989 | ||
990 | /* | |
991 | * if we have a HAVES list, but none of those haves is contained | |
992 | * in the packfile that has a bitmap, we don't have anything to | |
993 | * optimize here | |
994 | */ | |
3ae5fa07 | 995 | if (haves && !in_bitmapped_pack(bitmap_git, haves)) |
f3c23db2 | 996 | goto cleanup; |
fff42755 VM |
997 | |
998 | /* if we don't want anything, we're done here */ | |
999 | if (!wants) | |
f3c23db2 | 1000 | goto cleanup; |
fff42755 VM |
1001 | |
1002 | /* | |
1003 | * now we're going to use bitmaps, so load the actual bitmap entries | |
1004 | * from disk. this is the point of no return; after this the rev_list | |
1005 | * becomes invalidated and we must perform the revwalk through bitmaps | |
1006 | */ | |
199c86be | 1007 | if (load_pack_bitmap(bitmap_git) < 0) |
f3c23db2 | 1008 | goto cleanup; |
fff42755 | 1009 | |
4d01a7fa | 1010 | object_array_clear(&revs->pending); |
fff42755 VM |
1011 | |
1012 | if (haves) { | |
2db1a43f | 1013 | revs->ignore_missing_links = 1; |
9639474b JK |
1014 | haves_bitmap = find_objects(bitmap_git, revs, haves, NULL, |
1015 | filter); | |
fff42755 | 1016 | reset_revision_walk(); |
2db1a43f | 1017 | revs->ignore_missing_links = 0; |
fff42755 VM |
1018 | |
1019 | if (haves_bitmap == NULL) | |
033abf97 | 1020 | BUG("failed to perform bitmap walk"); |
fff42755 VM |
1021 | } |
1022 | ||
9639474b JK |
1023 | wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap, |
1024 | filter); | |
fff42755 VM |
1025 | |
1026 | if (!wants_bitmap) | |
033abf97 | 1027 | BUG("failed to perform bitmap walk"); |
fff42755 VM |
1028 | |
1029 | if (haves_bitmap) | |
1030 | bitmap_and_not(wants_bitmap, haves_bitmap); | |
1031 | ||
6663ae0a JK |
1032 | filter_bitmap(bitmap_git, wants, wants_bitmap, filter); |
1033 | ||
3ae5fa07 | 1034 | bitmap_git->result = wants_bitmap; |
30cdc33f | 1035 | bitmap_git->haves = haves_bitmap; |
fff42755 | 1036 | |
acac50dd JK |
1037 | object_list_free(&wants); |
1038 | object_list_free(&haves); | |
1039 | ||
3ae5fa07 | 1040 | return bitmap_git; |
f3c23db2 JT |
1041 | |
1042 | cleanup: | |
1043 | free_bitmap_index(bitmap_git); | |
acac50dd JK |
1044 | object_list_free(&wants); |
1045 | object_list_free(&haves); | |
f3c23db2 | 1046 | return NULL; |
fff42755 VM |
1047 | } |
1048 | ||
bb514de3 JK |
1049 | static void try_partial_reuse(struct bitmap_index *bitmap_git, |
1050 | size_t pos, | |
1051 | struct bitmap *reuse, | |
1052 | struct pack_window **w_curs) | |
fff42755 | 1053 | { |
bb514de3 JK |
1054 | struct revindex_entry *revidx; |
1055 | off_t offset; | |
1056 | enum object_type type; | |
1057 | unsigned long size; | |
1058 | ||
1059 | if (pos >= bitmap_git->pack->num_objects) | |
1060 | return; /* not actually in the pack */ | |
1061 | ||
1062 | revidx = &bitmap_git->pack->revindex[pos]; | |
1063 | offset = revidx->offset; | |
1064 | type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size); | |
1065 | if (type < 0) | |
1066 | return; /* broken packfile, punt */ | |
1067 | ||
1068 | if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) { | |
1069 | off_t base_offset; | |
1070 | int base_pos; | |
1071 | ||
1072 | /* | |
1073 | * Find the position of the base object so we can look it up | |
1074 | * in our bitmaps. If we can't come up with an offset, or if | |
1075 | * that offset is not in the revidx, the pack is corrupt. | |
1076 | * There's nothing we can do, so just punt on this object, | |
1077 | * and the normal slow path will complain about it in | |
1078 | * more detail. | |
1079 | */ | |
1080 | base_offset = get_delta_base(bitmap_git->pack, w_curs, | |
1081 | &offset, type, revidx->offset); | |
1082 | if (!base_offset) | |
1083 | return; | |
1084 | base_pos = find_revindex_position(bitmap_git->pack, base_offset); | |
1085 | if (base_pos < 0) | |
1086 | return; | |
1087 | ||
1088 | /* | |
1089 | * We assume delta dependencies always point backwards. This | |
1090 | * lets us do a single pass, and is basically always true | |
1091 | * due to the way OFS_DELTAs work. You would not typically | |
1092 | * find REF_DELTA in a bitmapped pack, since we only bitmap | |
1093 | * packs we write fresh, and OFS_DELTA is the default). But | |
1094 | * let's double check to make sure the pack wasn't written with | |
1095 | * odd parameters. | |
1096 | */ | |
1097 | if (base_pos >= pos) | |
1098 | return; | |
1099 | ||
1100 | /* | |
1101 | * And finally, if we're not sending the base as part of our | |
1102 | * reuse chunk, then don't send this object either. The base | |
1103 | * would come after us, along with other objects not | |
1104 | * necessarily in the pack, which means we'd need to convert | |
1105 | * to REF_DELTA on the fly. Better to just let the normal | |
1106 | * object_entry code path handle it. | |
1107 | */ | |
1108 | if (!bitmap_get(reuse, base_pos)) | |
1109 | return; | |
1110 | } | |
1111 | ||
fff42755 | 1112 | /* |
bb514de3 | 1113 | * If we got here, then the object is OK to reuse. Mark it. |
fff42755 | 1114 | */ |
bb514de3 JK |
1115 | bitmap_set(reuse, pos); |
1116 | } | |
fff42755 | 1117 | |
bb514de3 JK |
1118 | int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git, |
1119 | struct packed_git **packfile_out, | |
1120 | uint32_t *entries, | |
1121 | struct bitmap **reuse_out) | |
1122 | { | |
3ae5fa07 | 1123 | struct bitmap *result = bitmap_git->result; |
bb514de3 JK |
1124 | struct bitmap *reuse; |
1125 | struct pack_window *w_curs = NULL; | |
1126 | size_t i = 0; | |
1127 | uint32_t offset; | |
fff42755 VM |
1128 | |
1129 | assert(result); | |
1130 | ||
bb514de3 JK |
1131 | while (i < result->word_alloc && result->words[i] == (eword_t)~0) |
1132 | i++; | |
fff42755 | 1133 | |
bb514de3 JK |
1134 | /* Don't mark objects not in the packfile */ |
1135 | if (i > bitmap_git->pack->num_objects / BITS_IN_EWORD) | |
1136 | i = bitmap_git->pack->num_objects / BITS_IN_EWORD; | |
fff42755 | 1137 | |
bb514de3 JK |
1138 | reuse = bitmap_word_alloc(i); |
1139 | memset(reuse->words, 0xFF, i * sizeof(eword_t)); | |
fff42755 | 1140 | |
bb514de3 JK |
1141 | for (; i < result->word_alloc; ++i) { |
1142 | eword_t word = result->words[i]; | |
1143 | size_t pos = (i * BITS_IN_EWORD); | |
fff42755 | 1144 | |
bb514de3 JK |
1145 | for (offset = 0; offset < BITS_IN_EWORD; ++offset) { |
1146 | if ((word >> offset) == 0) | |
1147 | break; | |
fff42755 | 1148 | |
bb514de3 JK |
1149 | offset += ewah_bit_ctz64(word >> offset); |
1150 | try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs); | |
1151 | } | |
fff42755 | 1152 | } |
fff42755 | 1153 | |
bb514de3 | 1154 | unuse_pack(&w_curs); |
fff42755 | 1155 | |
bb514de3 JK |
1156 | *entries = bitmap_popcount(reuse); |
1157 | if (!*entries) { | |
1158 | bitmap_free(reuse); | |
fff42755 | 1159 | return -1; |
fff42755 VM |
1160 | } |
1161 | ||
bb514de3 JK |
1162 | /* |
1163 | * Drop any reused objects from the result, since they will not | |
1164 | * need to be handled separately. | |
1165 | */ | |
1166 | bitmap_and_not(result, reuse); | |
1167 | *packfile_out = bitmap_git->pack; | |
1168 | *reuse_out = reuse; | |
fff42755 VM |
1169 | return 0; |
1170 | } | |
fff42755 | 1171 | |
40d18ff8 JK |
1172 | int bitmap_walk_contains(struct bitmap_index *bitmap_git, |
1173 | struct bitmap *bitmap, const struct object_id *oid) | |
1174 | { | |
1175 | int idx; | |
fff42755 | 1176 | |
40d18ff8 JK |
1177 | if (!bitmap) |
1178 | return 0; | |
fff42755 | 1179 | |
40d18ff8 JK |
1180 | idx = bitmap_position(bitmap_git, oid); |
1181 | return idx >= 0 && bitmap_get(bitmap, idx); | |
fff42755 VM |
1182 | } |
1183 | ||
3ae5fa07 | 1184 | void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git, |
4eb707eb | 1185 | struct rev_info *revs, |
3ae5fa07 | 1186 | show_reachable_fn show_reachable) |
fff42755 | 1187 | { |
3ae5fa07 | 1188 | assert(bitmap_git->result); |
fff42755 | 1189 | |
551cf8b6 | 1190 | show_objects_for_type(bitmap_git, OBJ_COMMIT, show_reachable); |
4eb707eb JK |
1191 | if (revs->tree_objects) |
1192 | show_objects_for_type(bitmap_git, OBJ_TREE, show_reachable); | |
1193 | if (revs->blob_objects) | |
1194 | show_objects_for_type(bitmap_git, OBJ_BLOB, show_reachable); | |
1195 | if (revs->tag_objects) | |
1196 | show_objects_for_type(bitmap_git, OBJ_TAG, show_reachable); | |
fff42755 | 1197 | |
4eb707eb | 1198 | show_extended_objects(bitmap_git, revs, show_reachable); |
fff42755 VM |
1199 | } |
1200 | ||
3ae5fa07 | 1201 | static uint32_t count_object_type(struct bitmap_index *bitmap_git, |
fff42755 VM |
1202 | enum object_type type) |
1203 | { | |
3ae5fa07 JT |
1204 | struct bitmap *objects = bitmap_git->result; |
1205 | struct eindex *eindex = &bitmap_git->ext_index; | |
fff42755 VM |
1206 | |
1207 | uint32_t i = 0, count = 0; | |
1208 | struct ewah_iterator it; | |
1209 | eword_t filter; | |
1210 | ||
551cf8b6 | 1211 | init_type_iterator(&it, bitmap_git, type); |
fff42755 VM |
1212 | |
1213 | while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) { | |
1214 | eword_t word = objects->words[i++] & filter; | |
1215 | count += ewah_bit_popcount64(word); | |
1216 | } | |
1217 | ||
1218 | for (i = 0; i < eindex->count; ++i) { | |
1219 | if (eindex->objects[i]->type == type && | |
3ae5fa07 | 1220 | bitmap_get(objects, bitmap_git->pack->num_objects + i)) |
fff42755 VM |
1221 | count++; |
1222 | } | |
1223 | ||
1224 | return count; | |
1225 | } | |
1226 | ||
3ae5fa07 JT |
1227 | void count_bitmap_commit_list(struct bitmap_index *bitmap_git, |
1228 | uint32_t *commits, uint32_t *trees, | |
fff42755 VM |
1229 | uint32_t *blobs, uint32_t *tags) |
1230 | { | |
3ae5fa07 | 1231 | assert(bitmap_git->result); |
fff42755 VM |
1232 | |
1233 | if (commits) | |
3ae5fa07 | 1234 | *commits = count_object_type(bitmap_git, OBJ_COMMIT); |
fff42755 VM |
1235 | |
1236 | if (trees) | |
3ae5fa07 | 1237 | *trees = count_object_type(bitmap_git, OBJ_TREE); |
fff42755 VM |
1238 | |
1239 | if (blobs) | |
3ae5fa07 | 1240 | *blobs = count_object_type(bitmap_git, OBJ_BLOB); |
fff42755 VM |
1241 | |
1242 | if (tags) | |
3ae5fa07 | 1243 | *tags = count_object_type(bitmap_git, OBJ_TAG); |
fff42755 VM |
1244 | } |
1245 | ||
1246 | struct bitmap_test_data { | |
3ae5fa07 | 1247 | struct bitmap_index *bitmap_git; |
fff42755 VM |
1248 | struct bitmap *base; |
1249 | struct progress *prg; | |
1250 | size_t seen; | |
1251 | }; | |
1252 | ||
de1e67d0 JK |
1253 | static void test_show_object(struct object *object, const char *name, |
1254 | void *data) | |
fff42755 VM |
1255 | { |
1256 | struct bitmap_test_data *tdata = data; | |
1257 | int bitmap_pos; | |
1258 | ||
3c771448 | 1259 | bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid); |
fff42755 | 1260 | if (bitmap_pos < 0) |
f2fd0760 | 1261 | die("Object not in bitmap: %s\n", oid_to_hex(&object->oid)); |
fff42755 VM |
1262 | |
1263 | bitmap_set(tdata->base, bitmap_pos); | |
1264 | display_progress(tdata->prg, ++tdata->seen); | |
1265 | } | |
1266 | ||
1267 | static void test_show_commit(struct commit *commit, void *data) | |
1268 | { | |
1269 | struct bitmap_test_data *tdata = data; | |
1270 | int bitmap_pos; | |
1271 | ||
3ae5fa07 | 1272 | bitmap_pos = bitmap_position(tdata->bitmap_git, |
3c771448 | 1273 | &commit->object.oid); |
fff42755 | 1274 | if (bitmap_pos < 0) |
f2fd0760 | 1275 | die("Object not in bitmap: %s\n", oid_to_hex(&commit->object.oid)); |
fff42755 VM |
1276 | |
1277 | bitmap_set(tdata->base, bitmap_pos); | |
1278 | display_progress(tdata->prg, ++tdata->seen); | |
1279 | } | |
1280 | ||
1281 | void test_bitmap_walk(struct rev_info *revs) | |
1282 | { | |
1283 | struct object *root; | |
1284 | struct bitmap *result = NULL; | |
1285 | khiter_t pos; | |
1286 | size_t result_popcnt; | |
1287 | struct bitmap_test_data tdata; | |
3ae5fa07 | 1288 | struct bitmap_index *bitmap_git; |
fff42755 | 1289 | |
7c141127 | 1290 | if (!(bitmap_git = prepare_bitmap_git(revs->repo))) |
fff42755 VM |
1291 | die("failed to load bitmap indexes"); |
1292 | ||
1293 | if (revs->pending.nr != 1) | |
1294 | die("you must specify exactly one commit to test"); | |
1295 | ||
1296 | fprintf(stderr, "Bitmap v%d test (%d entries loaded)\n", | |
3ae5fa07 | 1297 | bitmap_git->version, bitmap_git->entry_count); |
fff42755 VM |
1298 | |
1299 | root = revs->pending.objects[0].item; | |
3c771448 | 1300 | pos = kh_get_oid_map(bitmap_git->bitmaps, root->oid); |
fff42755 | 1301 | |
3ae5fa07 JT |
1302 | if (pos < kh_end(bitmap_git->bitmaps)) { |
1303 | struct stored_bitmap *st = kh_value(bitmap_git->bitmaps, pos); | |
fff42755 VM |
1304 | struct ewah_bitmap *bm = lookup_stored_bitmap(st); |
1305 | ||
1306 | fprintf(stderr, "Found bitmap for %s. %d bits / %08x checksum\n", | |
f2fd0760 | 1307 | oid_to_hex(&root->oid), (int)bm->bit_size, ewah_checksum(bm)); |
fff42755 VM |
1308 | |
1309 | result = ewah_to_bitmap(bm); | |
1310 | } | |
1311 | ||
1312 | if (result == NULL) | |
f2fd0760 | 1313 | die("Commit %s doesn't have an indexed bitmap", oid_to_hex(&root->oid)); |
fff42755 VM |
1314 | |
1315 | revs->tag_objects = 1; | |
1316 | revs->tree_objects = 1; | |
1317 | revs->blob_objects = 1; | |
1318 | ||
1319 | result_popcnt = bitmap_popcount(result); | |
1320 | ||
1321 | if (prepare_revision_walk(revs)) | |
1322 | die("revision walk setup failed"); | |
1323 | ||
3ae5fa07 | 1324 | tdata.bitmap_git = bitmap_git; |
fff42755 VM |
1325 | tdata.base = bitmap_new(); |
1326 | tdata.prg = start_progress("Verifying bitmap entries", result_popcnt); | |
1327 | tdata.seen = 0; | |
1328 | ||
1329 | traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata); | |
1330 | ||
1331 | stop_progress(&tdata.prg); | |
1332 | ||
1333 | if (bitmap_equals(result, tdata.base)) | |
1334 | fprintf(stderr, "OK!\n"); | |
1335 | else | |
2978b006 | 1336 | die("mismatch in bitmap results"); |
f86a3747 | 1337 | |
f3c23db2 | 1338 | free_bitmap_index(bitmap_git); |
fff42755 | 1339 | } |
7cc8f971 VM |
1340 | |
1341 | static int rebuild_bitmap(uint32_t *reposition, | |
1342 | struct ewah_bitmap *source, | |
1343 | struct bitmap *dest) | |
1344 | { | |
1345 | uint32_t pos = 0; | |
1346 | struct ewah_iterator it; | |
1347 | eword_t word; | |
1348 | ||
1349 | ewah_iterator_init(&it, source); | |
1350 | ||
1351 | while (ewah_iterator_next(&word, &it)) { | |
1352 | uint32_t offset, bit_pos; | |
1353 | ||
34b935c0 | 1354 | for (offset = 0; offset < BITS_IN_EWORD; ++offset) { |
7cc8f971 VM |
1355 | if ((word >> offset) == 0) |
1356 | break; | |
1357 | ||
1358 | offset += ewah_bit_ctz64(word >> offset); | |
1359 | ||
1360 | bit_pos = reposition[pos + offset]; | |
1361 | if (bit_pos > 0) | |
1362 | bitmap_set(dest, bit_pos - 1); | |
1363 | else /* can't reuse, we don't have the object */ | |
1364 | return -1; | |
1365 | } | |
1366 | ||
34b935c0 | 1367 | pos += BITS_IN_EWORD; |
7cc8f971 VM |
1368 | } |
1369 | return 0; | |
1370 | } | |
1371 | ||
3ae5fa07 JT |
1372 | int rebuild_existing_bitmaps(struct bitmap_index *bitmap_git, |
1373 | struct packing_data *mapping, | |
d2bc62b1 | 1374 | kh_oid_map_t *reused_bitmaps, |
7cc8f971 VM |
1375 | int show_progress) |
1376 | { | |
1377 | uint32_t i, num_objects; | |
1378 | uint32_t *reposition; | |
1379 | struct bitmap *rebuild; | |
1380 | struct stored_bitmap *stored; | |
1381 | struct progress *progress = NULL; | |
1382 | ||
1383 | khiter_t hash_pos; | |
1384 | int hash_ret; | |
1385 | ||
3ae5fa07 | 1386 | num_objects = bitmap_git->pack->num_objects; |
7cc8f971 VM |
1387 | reposition = xcalloc(num_objects, sizeof(uint32_t)); |
1388 | ||
1389 | for (i = 0; i < num_objects; ++i) { | |
3df28cae | 1390 | struct object_id oid; |
7cc8f971 VM |
1391 | struct revindex_entry *entry; |
1392 | struct object_entry *oe; | |
1393 | ||
3ae5fa07 | 1394 | entry = &bitmap_git->pack->revindex[i]; |
0763671b | 1395 | nth_packed_object_id(&oid, bitmap_git->pack, entry->nr); |
3a37876b | 1396 | oe = packlist_find(mapping, &oid); |
7cc8f971 VM |
1397 | |
1398 | if (oe) | |
06af3bba | 1399 | reposition[i] = oe_in_pack_pos(mapping, oe) + 1; |
7cc8f971 VM |
1400 | } |
1401 | ||
1402 | rebuild = bitmap_new(); | |
1403 | i = 0; | |
1404 | ||
1405 | if (show_progress) | |
1406 | progress = start_progress("Reusing bitmaps", 0); | |
1407 | ||
3ae5fa07 | 1408 | kh_foreach_value(bitmap_git->bitmaps, stored, { |
7cc8f971 VM |
1409 | if (stored->flags & BITMAP_FLAG_REUSE) { |
1410 | if (!rebuild_bitmap(reposition, | |
1411 | lookup_stored_bitmap(stored), | |
1412 | rebuild)) { | |
d2bc62b1 JK |
1413 | hash_pos = kh_put_oid_map(reused_bitmaps, |
1414 | stored->oid, | |
1415 | &hash_ret); | |
7cc8f971 VM |
1416 | kh_value(reused_bitmaps, hash_pos) = |
1417 | bitmap_to_ewah(rebuild); | |
1418 | } | |
1419 | bitmap_reset(rebuild); | |
1420 | display_progress(progress, ++i); | |
1421 | } | |
1422 | }); | |
1423 | ||
1424 | stop_progress(&progress); | |
1425 | ||
1426 | free(reposition); | |
1427 | bitmap_free(rebuild); | |
1428 | return 0; | |
1429 | } | |
f3c23db2 JT |
1430 | |
1431 | void free_bitmap_index(struct bitmap_index *b) | |
1432 | { | |
1433 | if (!b) | |
1434 | return; | |
1435 | ||
1436 | if (b->map) | |
1437 | munmap(b->map, b->map_size); | |
1438 | ewah_pool_free(b->commits); | |
1439 | ewah_pool_free(b->trees); | |
1440 | ewah_pool_free(b->blobs); | |
1441 | ewah_pool_free(b->tags); | |
3c771448 | 1442 | kh_destroy_oid_map(b->bitmaps); |
f3c23db2 JT |
1443 | free(b->ext_index.objects); |
1444 | free(b->ext_index.hashes); | |
1445 | bitmap_free(b->result); | |
30cdc33f | 1446 | bitmap_free(b->haves); |
f3c23db2 JT |
1447 | free(b); |
1448 | } | |
30cdc33f | 1449 | |
3c771448 | 1450 | int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git, |
1451 | const struct object_id *oid) | |
30cdc33f | 1452 | { |
8ebf5296 JK |
1453 | return bitmap_git && |
1454 | bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid); | |
30cdc33f | 1455 | } |