| 1 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 2 | |
| 3 | #include "git-compat-util.h" |
| 4 | #include "commit.h" |
| 5 | #include "gettext.h" |
| 6 | #include "hex.h" |
| 7 | #include "strbuf.h" |
| 8 | #include "tag.h" |
| 9 | #include "diff.h" |
| 10 | #include "revision.h" |
| 11 | #include "progress.h" |
| 12 | #include "list-objects.h" |
| 13 | #include "pack.h" |
| 14 | #include "pack-bitmap.h" |
| 15 | #include "pack-revindex.h" |
| 16 | #include "pack-objects.h" |
| 17 | #include "packfile.h" |
| 18 | #include "repository.h" |
| 19 | #include "trace2.h" |
| 20 | #include "odb.h" |
| 21 | #include "list-objects-filter-options.h" |
| 22 | #include "midx.h" |
| 23 | #include "config.h" |
| 24 | #include "pseudo-merge.h" |
| 25 | |
| 26 | /* |
| 27 | * An entry on the bitmap index, representing the bitmap for a given |
| 28 | * commit. |
| 29 | */ |
| 30 | struct stored_bitmap { |
| 31 | struct object_id oid; |
| 32 | struct ewah_bitmap *root; |
| 33 | struct stored_bitmap *xor; |
| 34 | size_t map_pos; |
| 35 | int flags; |
| 36 | }; |
| 37 | |
| 38 | /* |
| 39 | * The active bitmap index for a repository. By design, repositories only have |
| 40 | * a single bitmap index available (the index for the biggest packfile in |
| 41 | * the repository), since bitmap indexes need full closure. |
| 42 | * |
| 43 | * If there is more than one bitmap index available (e.g. because of alternates), |
| 44 | * the active bitmap index is the largest one. |
| 45 | */ |
| 46 | struct bitmap_index { |
| 47 | /* |
| 48 | * The pack or multi-pack index (MIDX) that this bitmap index belongs |
| 49 | * to. |
| 50 | * |
| 51 | * Exactly one of these must be non-NULL; this specifies the object |
| 52 | * order used to interpret this bitmap. |
| 53 | */ |
| 54 | struct packed_git *pack; |
| 55 | struct multi_pack_index *midx; |
| 56 | |
| 57 | /* |
| 58 | * If using a multi-pack index chain, 'base' points to the |
| 59 | * bitmap index corresponding to this bitmap's midx->base_midx. |
| 60 | * |
| 61 | * base_nr indicates how many layers precede this one, and is |
| 62 | * zero when base is NULL. |
| 63 | */ |
| 64 | struct bitmap_index *base; |
| 65 | uint32_t base_nr; |
| 66 | |
| 67 | /* mmapped buffer of the whole bitmap index */ |
| 68 | unsigned char *map; |
| 69 | size_t map_size; /* size of the mmaped buffer */ |
| 70 | size_t map_pos; /* current position when loading the index */ |
| 71 | |
| 72 | /* |
| 73 | * Type indexes. |
| 74 | * |
| 75 | * Each bitmap marks which objects in the packfile are of the given |
| 76 | * type. This provides type information when yielding the objects from |
| 77 | * the packfile during a walk, which allows for better delta bases. |
| 78 | */ |
| 79 | struct ewah_bitmap *commits; |
| 80 | struct ewah_bitmap *trees; |
| 81 | struct ewah_bitmap *blobs; |
| 82 | struct ewah_bitmap *tags; |
| 83 | |
| 84 | /* |
| 85 | * Type index arrays when this bitmap is associated with an |
| 86 | * incremental multi-pack index chain. |
| 87 | * |
| 88 | * If n is the number of unique layers in the MIDX chain, then |
| 89 | * commits_all[n-1] is this structs 'commits' field, |
| 90 | * commits_all[n-2] is the commits field of this bitmap's |
| 91 | * 'base', and so on. |
| 92 | * |
| 93 | * When associated either with a non-incremental MIDX or a |
| 94 | * single packfile, these arrays each contain a single element. |
| 95 | */ |
| 96 | struct ewah_bitmap **commits_all; |
| 97 | struct ewah_bitmap **trees_all; |
| 98 | struct ewah_bitmap **blobs_all; |
| 99 | struct ewah_bitmap **tags_all; |
| 100 | |
| 101 | /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */ |
| 102 | kh_oid_map_t *bitmaps; |
| 103 | |
| 104 | /* Number of bitmapped commits */ |
| 105 | uint32_t entry_count; |
| 106 | |
| 107 | /* If not NULL, this is a name-hash cache pointing into map. */ |
| 108 | uint32_t *hashes; |
| 109 | |
| 110 | /* The checksum of the packfile or MIDX; points into map. */ |
| 111 | const unsigned char *checksum; |
| 112 | |
| 113 | /* |
| 114 | * If not NULL, this point into the commit table extension |
| 115 | * (within the memory mapped region `map`). |
| 116 | */ |
| 117 | unsigned char *table_lookup; |
| 118 | |
| 119 | /* This contains the pseudo-merge cache within 'map' (if found). */ |
| 120 | struct pseudo_merge_map pseudo_merges; |
| 121 | |
| 122 | /* |
| 123 | * Extended index. |
| 124 | * |
| 125 | * When trying to perform bitmap operations with objects that are not |
| 126 | * packed in `pack`, these objects are added to this "fake index" and |
| 127 | * are assumed to appear at the end of the packfile for all operations |
| 128 | */ |
| 129 | struct eindex { |
| 130 | struct object **objects; |
| 131 | uint32_t *hashes; |
| 132 | uint32_t count, alloc; |
| 133 | kh_oid_pos_t *positions; |
| 134 | } ext_index; |
| 135 | |
| 136 | /* Bitmap result of the last performed walk */ |
| 137 | struct bitmap *result; |
| 138 | |
| 139 | /* "have" bitmap from the last performed walk */ |
| 140 | struct bitmap *haves; |
| 141 | |
| 142 | /* Version of the bitmap index */ |
| 143 | unsigned int version; |
| 144 | }; |
| 145 | |
| 146 | static int pseudo_merges_satisfied_nr; |
| 147 | static int pseudo_merges_cascades_nr; |
| 148 | static int existing_bitmaps_hits_nr; |
| 149 | static int existing_bitmaps_misses_nr; |
| 150 | static int roots_with_bitmaps_nr; |
| 151 | static int roots_without_bitmaps_nr; |
| 152 | |
| 153 | static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st) |
| 154 | { |
| 155 | struct ewah_bitmap *parent; |
| 156 | struct ewah_bitmap *composed; |
| 157 | |
| 158 | if (!st->xor) |
| 159 | return st->root; |
| 160 | |
| 161 | composed = ewah_pool_new(); |
| 162 | parent = lookup_stored_bitmap(st->xor); |
| 163 | ewah_xor(st->root, parent, composed); |
| 164 | |
| 165 | ewah_pool_free(st->root); |
| 166 | st->root = composed; |
| 167 | st->xor = NULL; |
| 168 | |
| 169 | return composed; |
| 170 | } |
| 171 | |
| 172 | struct ewah_bitmap *read_bitmap(const unsigned char *map, |
| 173 | size_t map_size, size_t *map_pos) |
| 174 | { |
| 175 | struct ewah_bitmap *b = ewah_pool_new(); |
| 176 | |
| 177 | ssize_t bitmap_size = ewah_read_mmap(b, map + *map_pos, |
| 178 | map_size - *map_pos); |
| 179 | |
| 180 | if (bitmap_size < 0) { |
| 181 | error(_("failed to load bitmap index (corrupted?)")); |
| 182 | ewah_pool_free(b); |
| 183 | return NULL; |
| 184 | } |
| 185 | |
| 186 | *map_pos += bitmap_size; |
| 187 | |
| 188 | return b; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Read a bitmap from the current read position on the mmaped |
| 193 | * index, and increase the read position accordingly |
| 194 | */ |
| 195 | static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index) |
| 196 | { |
| 197 | return read_bitmap(index->map, index->map_size, &index->map_pos); |
| 198 | } |
| 199 | |
| 200 | static uint32_t bitmap_num_objects_total(struct bitmap_index *index) |
| 201 | { |
| 202 | if (index->midx) { |
| 203 | struct multi_pack_index *m = index->midx; |
| 204 | return m->num_objects + m->num_objects_in_base; |
| 205 | } |
| 206 | return index->pack->num_objects; |
| 207 | } |
| 208 | |
| 209 | static uint32_t bitmap_num_objects(struct bitmap_index *index) |
| 210 | { |
| 211 | if (index->midx) |
| 212 | return index->midx->num_objects; |
| 213 | return index->pack->num_objects; |
| 214 | } |
| 215 | |
| 216 | static uint32_t bitmap_name_hash(struct bitmap_index *index, uint32_t pos) |
| 217 | { |
| 218 | if (bitmap_is_midx(index)) { |
| 219 | while (index && pos < index->midx->num_objects_in_base) { |
| 220 | ASSERT(bitmap_is_midx(index)); |
| 221 | index = index->base; |
| 222 | } |
| 223 | |
| 224 | if (!index) |
| 225 | BUG("NULL base bitmap for object position: %"PRIu32, pos); |
| 226 | |
| 227 | pos -= index->midx->num_objects_in_base; |
| 228 | if (pos >= index->midx->num_objects) |
| 229 | BUG("out-of-bounds midx bitmap object at %"PRIu32, pos); |
| 230 | } |
| 231 | |
| 232 | if (!index->hashes) |
| 233 | return 0; |
| 234 | |
| 235 | return get_be32(index->hashes + pos); |
| 236 | } |
| 237 | |
| 238 | static struct repository *bitmap_repo(struct bitmap_index *bitmap_git) |
| 239 | { |
| 240 | if (bitmap_is_midx(bitmap_git)) |
| 241 | return bitmap_git->midx->source->base.odb->repo; |
| 242 | return bitmap_git->pack->repo; |
| 243 | } |
| 244 | |
| 245 | static int load_bitmap_header(struct bitmap_index *index) |
| 246 | { |
| 247 | struct bitmap_disk_header *header = (void *)index->map; |
| 248 | const struct git_hash_algo *hash_algo = bitmap_repo(index)->hash_algo; |
| 249 | |
| 250 | size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + hash_algo->rawsz; |
| 251 | |
| 252 | if (index->map_size < header_size + hash_algo->rawsz) |
| 253 | return error(_("corrupted bitmap index (too small)")); |
| 254 | |
| 255 | if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0) |
| 256 | return error(_("corrupted bitmap index file (wrong header)")); |
| 257 | |
| 258 | index->version = ntohs(header->version); |
| 259 | if (index->version != 1) |
| 260 | return error(_("unsupported version '%d' for bitmap index file"), index->version); |
| 261 | |
| 262 | /* Parse known bitmap format options */ |
| 263 | { |
| 264 | uint32_t flags = ntohs(header->options); |
| 265 | size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t)); |
| 266 | unsigned char *index_end = index->map + index->map_size - hash_algo->rawsz; |
| 267 | |
| 268 | if ((flags & BITMAP_OPT_FULL_DAG) == 0) |
| 269 | BUG("unsupported options for bitmap index file " |
| 270 | "(Git requires BITMAP_OPT_FULL_DAG)"); |
| 271 | |
| 272 | if (flags & BITMAP_OPT_HASH_CACHE) { |
| 273 | if (cache_size > index_end - index->map - header_size) |
| 274 | return error(_("corrupted bitmap index file (too short to fit hash cache)")); |
| 275 | index->hashes = (void *)(index_end - cache_size); |
| 276 | index_end -= cache_size; |
| 277 | } |
| 278 | |
| 279 | if (flags & BITMAP_OPT_LOOKUP_TABLE) { |
| 280 | size_t table_size = st_mult(ntohl(header->entry_count), |
| 281 | BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH); |
| 282 | if (table_size > index_end - index->map - header_size) |
| 283 | return error(_("corrupted bitmap index file (too short to fit lookup table)")); |
| 284 | if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1)) |
| 285 | index->table_lookup = (void *)(index_end - table_size); |
| 286 | index_end -= table_size; |
| 287 | } |
| 288 | |
| 289 | if (flags & BITMAP_OPT_PSEUDO_MERGES) { |
| 290 | unsigned char *pseudo_merge_ofs; |
| 291 | size_t table_size; |
| 292 | uint32_t i; |
| 293 | |
| 294 | if (sizeof(table_size) > index_end - index->map - header_size) |
| 295 | return error(_("corrupted bitmap index file (too short to fit pseudo-merge table header)")); |
| 296 | |
| 297 | table_size = get_be64(index_end - 8); |
| 298 | if (table_size > index_end - index->map - header_size) |
| 299 | return error(_("corrupted bitmap index file (too short to fit pseudo-merge table)")); |
| 300 | |
| 301 | if (git_env_bool("GIT_TEST_USE_PSEUDO_MERGES", 1)) { |
| 302 | const unsigned char *ext = (index_end - table_size); |
| 303 | |
| 304 | index->pseudo_merges.map = index->map; |
| 305 | index->pseudo_merges.map_size = index->map_size; |
| 306 | index->pseudo_merges.commits = ext + get_be64(index_end - 16); |
| 307 | index->pseudo_merges.commits_nr = get_be32(index_end - 20); |
| 308 | index->pseudo_merges.nr = get_be32(index_end - 24); |
| 309 | |
| 310 | if (st_add(st_mult(index->pseudo_merges.nr, |
| 311 | sizeof(uint64_t)), |
| 312 | 24) > table_size) |
| 313 | return error(_("corrupted bitmap index file, pseudo-merge table too short")); |
| 314 | |
| 315 | CALLOC_ARRAY(index->pseudo_merges.v, |
| 316 | index->pseudo_merges.nr); |
| 317 | |
| 318 | pseudo_merge_ofs = index_end - 24 - |
| 319 | (index->pseudo_merges.nr * sizeof(uint64_t)); |
| 320 | for (i = 0; i < index->pseudo_merges.nr; i++) { |
| 321 | index->pseudo_merges.v[i].at = get_be64(pseudo_merge_ofs); |
| 322 | pseudo_merge_ofs += sizeof(uint64_t); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | index_end -= table_size; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | index->entry_count = ntohl(header->entry_count); |
| 331 | index->checksum = header->checksum; |
| 332 | index->map_pos += header_size; |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static struct stored_bitmap *store_bitmap(struct bitmap_index *index, |
| 337 | struct ewah_bitmap *root, |
| 338 | const struct object_id *oid, |
| 339 | struct stored_bitmap *xor_with, |
| 340 | int flags, size_t map_pos) |
| 341 | { |
| 342 | struct stored_bitmap *stored; |
| 343 | khiter_t hash_pos; |
| 344 | int ret; |
| 345 | |
| 346 | stored = xmalloc(sizeof(struct stored_bitmap)); |
| 347 | stored->map_pos = map_pos; |
| 348 | stored->root = root; |
| 349 | stored->xor = xor_with; |
| 350 | stored->flags = flags; |
| 351 | oidcpy(&stored->oid, oid); |
| 352 | |
| 353 | hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret); |
| 354 | |
| 355 | /* |
| 356 | * A 0 return code means the insertion succeeded with no changes, |
| 357 | * because the SHA1 already existed on the map. This is bad, there |
| 358 | * shouldn't be duplicated commits in the index. |
| 359 | */ |
| 360 | if (ret == 0) { |
| 361 | error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid)); |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | kh_value(index->bitmaps, hash_pos) = stored; |
| 366 | return stored; |
| 367 | } |
| 368 | |
| 369 | static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos) |
| 370 | { |
| 371 | uint32_t result = get_be32(buffer + *pos); |
| 372 | (*pos) += sizeof(result); |
| 373 | return result; |
| 374 | } |
| 375 | |
| 376 | static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos) |
| 377 | { |
| 378 | return buffer[(*pos)++]; |
| 379 | } |
| 380 | |
| 381 | #define MAX_XOR_OFFSET 160 |
| 382 | |
| 383 | static int nth_bitmap_object_oid(struct bitmap_index *index, |
| 384 | struct object_id *oid, |
| 385 | uint32_t n) |
| 386 | { |
| 387 | if (index->midx) |
| 388 | return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1; |
| 389 | return nth_packed_object_id(oid, index->pack, n); |
| 390 | } |
| 391 | |
| 392 | static int load_bitmap_entries_v1(struct bitmap_index *index) |
| 393 | { |
| 394 | uint32_t i; |
| 395 | struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL }; |
| 396 | |
| 397 | for (i = 0; i < index->entry_count; ++i) { |
| 398 | int xor_offset, flags; |
| 399 | struct ewah_bitmap *bitmap = NULL; |
| 400 | struct stored_bitmap *xor_bitmap = NULL; |
| 401 | uint32_t commit_idx_pos; |
| 402 | struct object_id oid; |
| 403 | size_t entry_map_pos; |
| 404 | |
| 405 | if (index->map_size - index->map_pos < 6) |
| 406 | return error(_("corrupt ewah bitmap: truncated header for entry %d"), i); |
| 407 | |
| 408 | entry_map_pos = index->map_pos; |
| 409 | commit_idx_pos = read_be32(index->map, &index->map_pos); |
| 410 | xor_offset = read_u8(index->map, &index->map_pos); |
| 411 | flags = read_u8(index->map, &index->map_pos); |
| 412 | |
| 413 | if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0) |
| 414 | return error(_("corrupt ewah bitmap: commit index %u out of range"), |
| 415 | (unsigned)commit_idx_pos); |
| 416 | |
| 417 | if (xor_offset > MAX_XOR_OFFSET || xor_offset > i) |
| 418 | return error(_("corrupted bitmap pack index")); |
| 419 | |
| 420 | if (xor_offset > 0) { |
| 421 | xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET]; |
| 422 | |
| 423 | if (!xor_bitmap) |
| 424 | return error(_("invalid XOR offset in bitmap pack index")); |
| 425 | } |
| 426 | |
| 427 | bitmap = read_bitmap_1(index); |
| 428 | if (!bitmap) |
| 429 | return -1; |
| 430 | |
| 431 | recent_bitmaps[i % MAX_XOR_OFFSET] = |
| 432 | store_bitmap(index, bitmap, &oid, xor_bitmap, flags, |
| 433 | entry_map_pos); |
| 434 | } |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | char *midx_bitmap_filename(struct multi_pack_index *midx) |
| 440 | { |
| 441 | struct strbuf buf = STRBUF_INIT; |
| 442 | if (midx->has_chain) |
| 443 | get_split_midx_filename_ext(midx->source, &buf, |
| 444 | midx_get_checksum_hash(midx), |
| 445 | MIDX_EXT_BITMAP); |
| 446 | else |
| 447 | get_midx_filename_ext(midx->source, &buf, |
| 448 | midx_get_checksum_hash(midx), |
| 449 | MIDX_EXT_BITMAP); |
| 450 | |
| 451 | return strbuf_detach(&buf, NULL); |
| 452 | } |
| 453 | |
| 454 | char *pack_bitmap_filename(struct packed_git *p) |
| 455 | { |
| 456 | size_t len; |
| 457 | |
| 458 | if (!strip_suffix(p->pack_name, ".pack", &len)) |
| 459 | BUG("pack_name does not end in .pack"); |
| 460 | return xstrfmt("%.*s.bitmap", (int)len, p->pack_name); |
| 461 | } |
| 462 | |
| 463 | static int open_midx_bitmap(struct bitmap_index *bitmap_git, |
| 464 | struct multi_pack_index *midx) |
| 465 | { |
| 466 | struct stat st; |
| 467 | char *bitmap_name = midx_bitmap_filename(midx); |
| 468 | int fd = git_open(bitmap_name); |
| 469 | uint32_t i; |
| 470 | |
| 471 | if (fd < 0) { |
| 472 | if (errno != ENOENT) |
| 473 | warning_errno("cannot open '%s'", bitmap_name); |
| 474 | free(bitmap_name); |
| 475 | return -1; |
| 476 | } |
| 477 | free(bitmap_name); |
| 478 | |
| 479 | if (fstat(fd, &st)) { |
| 480 | error_errno(_("cannot fstat bitmap file")); |
| 481 | close(fd); |
| 482 | return -1; |
| 483 | } |
| 484 | |
| 485 | if (bitmap_git->pack || bitmap_git->midx) { |
| 486 | struct strbuf buf = STRBUF_INIT; |
| 487 | get_midx_filename(midx->source, &buf); |
| 488 | trace2_data_string("bitmap", bitmap_repo(bitmap_git), |
| 489 | "ignoring extra midx bitmap file", buf.buf); |
| 490 | close(fd); |
| 491 | strbuf_release(&buf); |
| 492 | return -1; |
| 493 | } |
| 494 | |
| 495 | bitmap_git->midx = midx; |
| 496 | bitmap_git->map_size = xsize_t(st.st_size); |
| 497 | bitmap_git->map_pos = 0; |
| 498 | bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, |
| 499 | MAP_PRIVATE, fd, 0); |
| 500 | close(fd); |
| 501 | |
| 502 | if (load_bitmap_header(bitmap_git) < 0) |
| 503 | goto cleanup; |
| 504 | |
| 505 | if (!hasheq(midx_get_checksum_hash(bitmap_git->midx), bitmap_git->checksum, |
| 506 | bitmap_repo(bitmap_git)->hash_algo)) { |
| 507 | error(_("checksum doesn't match in MIDX and bitmap")); |
| 508 | goto cleanup; |
| 509 | } |
| 510 | |
| 511 | if (load_midx_revindex(bitmap_git->midx)) { |
| 512 | warning(_("multi-pack bitmap is missing required reverse index")); |
| 513 | goto cleanup; |
| 514 | } |
| 515 | |
| 516 | for (i = 0; i < bitmap_git->midx->num_packs + bitmap_git->midx->num_packs_in_base; i++) { |
| 517 | if (prepare_midx_pack(bitmap_git->midx, i)) { |
| 518 | warning(_("could not open pack %s"), |
| 519 | bitmap_git->midx->pack_names[i]); |
| 520 | goto cleanup; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (midx->base_midx) { |
| 525 | bitmap_git->base = prepare_midx_bitmap_git(midx->base_midx); |
| 526 | if (!bitmap_git->base) { |
| 527 | warning(_("could not open bitmap for base MIDX")); |
| 528 | goto cleanup; |
| 529 | } |
| 530 | bitmap_git->base_nr = bitmap_git->base->base_nr + 1; |
| 531 | } else { |
| 532 | bitmap_git->base_nr = 0; |
| 533 | } |
| 534 | |
| 535 | return 0; |
| 536 | |
| 537 | cleanup: |
| 538 | munmap(bitmap_git->map, bitmap_git->map_size); |
| 539 | bitmap_git->map_size = 0; |
| 540 | bitmap_git->map_pos = 0; |
| 541 | bitmap_git->map = NULL; |
| 542 | bitmap_git->midx = NULL; |
| 543 | return -1; |
| 544 | } |
| 545 | |
| 546 | static int open_pack_bitmap(struct bitmap_index *bitmap_git, struct packed_git *packfile) |
| 547 | { |
| 548 | int fd; |
| 549 | struct stat st; |
| 550 | char *bitmap_name; |
| 551 | |
| 552 | bitmap_name = pack_bitmap_filename(packfile); |
| 553 | fd = git_open(bitmap_name); |
| 554 | |
| 555 | if (fd < 0) { |
| 556 | if (errno != ENOENT) |
| 557 | warning_errno("cannot open '%s'", bitmap_name); |
| 558 | free(bitmap_name); |
| 559 | return -1; |
| 560 | } |
| 561 | free(bitmap_name); |
| 562 | |
| 563 | if (fstat(fd, &st)) { |
| 564 | error_errno(_("cannot fstat bitmap file")); |
| 565 | close(fd); |
| 566 | return -1; |
| 567 | } |
| 568 | |
| 569 | if (bitmap_git->pack || bitmap_git->midx) { |
| 570 | trace2_data_string("bitmap", bitmap_repo(bitmap_git), |
| 571 | "ignoring extra bitmap file", |
| 572 | packfile->pack_name); |
| 573 | close(fd); |
| 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | if (!is_pack_valid(packfile)) { |
| 578 | close(fd); |
| 579 | return -1; |
| 580 | } |
| 581 | |
| 582 | bitmap_git->pack = packfile; |
| 583 | bitmap_git->map_size = xsize_t(st.st_size); |
| 584 | bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 585 | bitmap_git->map_pos = 0; |
| 586 | bitmap_git->base_nr = 0; |
| 587 | close(fd); |
| 588 | |
| 589 | if (load_bitmap_header(bitmap_git) < 0) { |
| 590 | munmap(bitmap_git->map, bitmap_git->map_size); |
| 591 | bitmap_git->map = NULL; |
| 592 | bitmap_git->map_size = 0; |
| 593 | bitmap_git->map_pos = 0; |
| 594 | bitmap_git->pack = NULL; |
| 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | trace2_data_string("bitmap", bitmap_repo(bitmap_git), |
| 599 | "opened bitmap file", packfile->pack_name); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git) |
| 604 | { |
| 605 | if (bitmap_is_midx(bitmap_git)) { |
| 606 | struct multi_pack_index *m; |
| 607 | |
| 608 | /* |
| 609 | * The multi-pack-index's .rev file is already loaded via |
| 610 | * open_pack_bitmap(). |
| 611 | * |
| 612 | * But we still need to open the individual pack .rev files, |
| 613 | * since we will need to make use of them in pack-objects. |
| 614 | */ |
| 615 | for (m = bitmap_git->midx; m; m = m->base_midx) { |
| 616 | uint32_t i; |
| 617 | int ret; |
| 618 | |
| 619 | for (i = 0; i < m->num_packs; i++) { |
| 620 | ret = load_pack_revindex(r, m->packs[i]); |
| 621 | if (ret) |
| 622 | return ret; |
| 623 | } |
| 624 | } |
| 625 | return 0; |
| 626 | } |
| 627 | return load_pack_revindex(r, bitmap_git->pack); |
| 628 | } |
| 629 | |
| 630 | static void load_all_type_bitmaps(struct bitmap_index *bitmap_git) |
| 631 | { |
| 632 | struct bitmap_index *curr = bitmap_git; |
| 633 | size_t i = bitmap_git->base_nr; |
| 634 | |
| 635 | ALLOC_ARRAY(bitmap_git->commits_all, bitmap_git->base_nr + 1); |
| 636 | ALLOC_ARRAY(bitmap_git->trees_all, bitmap_git->base_nr + 1); |
| 637 | ALLOC_ARRAY(bitmap_git->blobs_all, bitmap_git->base_nr + 1); |
| 638 | ALLOC_ARRAY(bitmap_git->tags_all, bitmap_git->base_nr + 1); |
| 639 | |
| 640 | while (curr) { |
| 641 | bitmap_git->commits_all[i] = curr->commits; |
| 642 | bitmap_git->trees_all[i] = curr->trees; |
| 643 | bitmap_git->blobs_all[i] = curr->blobs; |
| 644 | bitmap_git->tags_all[i] = curr->tags; |
| 645 | |
| 646 | curr = curr->base; |
| 647 | if (curr && !i) |
| 648 | BUG("unexpected number of bitmap layers, expected %"PRIu32, |
| 649 | bitmap_git->base_nr + 1); |
| 650 | i -= 1; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git, |
| 655 | int recursing) |
| 656 | { |
| 657 | assert(bitmap_git->map); |
| 658 | |
| 659 | bitmap_git->bitmaps = kh_init_oid_map(); |
| 660 | bitmap_git->ext_index.positions = kh_init_oid_pos(); |
| 661 | |
| 662 | if (load_reverse_index(r, bitmap_git)) |
| 663 | return -1; |
| 664 | |
| 665 | if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) || |
| 666 | !(bitmap_git->trees = read_bitmap_1(bitmap_git)) || |
| 667 | !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) || |
| 668 | !(bitmap_git->tags = read_bitmap_1(bitmap_git))) |
| 669 | return -1; |
| 670 | |
| 671 | if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0) |
| 672 | return -1; |
| 673 | |
| 674 | if (bitmap_git->base) { |
| 675 | if (!bitmap_is_midx(bitmap_git)) |
| 676 | BUG("non-MIDX bitmap has non-NULL base bitmap index"); |
| 677 | if (load_bitmap(r, bitmap_git->base, 1) < 0) |
| 678 | return -1; |
| 679 | } |
| 680 | |
| 681 | if (!recursing) |
| 682 | load_all_type_bitmaps(bitmap_git); |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | static int open_bitmap_for_source(struct odb_source_packed *source, |
| 688 | struct bitmap_index *bitmap_git) |
| 689 | { |
| 690 | struct multi_pack_index *midx = get_multi_pack_index(source); |
| 691 | struct packfile_list_entry *e; |
| 692 | bool found = false; |
| 693 | |
| 694 | if (midx && !open_midx_bitmap(bitmap_git, midx)) |
| 695 | found = true; |
| 696 | |
| 697 | for (e = packfile_store_get_packs(source); e; e = e->next) { |
| 698 | /* |
| 699 | * When tracing is enabled we want to keep looking to report |
| 700 | * duplicates even if we have already found a bitmap. |
| 701 | */ |
| 702 | if (found && !trace2_is_enabled()) |
| 703 | break; |
| 704 | |
| 705 | if (!open_pack_bitmap(bitmap_git, e->pack)) |
| 706 | found = true; |
| 707 | } |
| 708 | |
| 709 | return found ? 0 : -1; |
| 710 | } |
| 711 | |
| 712 | static int open_bitmap(struct repository *r, |
| 713 | struct bitmap_index *bitmap_git) |
| 714 | { |
| 715 | struct odb_source *source; |
| 716 | bool found = false; |
| 717 | |
| 718 | assert(!bitmap_git->map); |
| 719 | |
| 720 | odb_prepare_alternates(r->objects); |
| 721 | for (source = r->objects->sources; source; source = source->next) { |
| 722 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 723 | |
| 724 | if (!open_bitmap_for_source(files->packed, bitmap_git)) |
| 725 | found = true; |
| 726 | |
| 727 | /* |
| 728 | * The only reason to keep looking after having found a bitmap |
| 729 | * is to report duplicates. |
| 730 | */ |
| 731 | if (found && !trace2_is_enabled()) |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | return found ? 0 : -1; |
| 736 | } |
| 737 | |
| 738 | struct bitmap_index *prepare_bitmap_git(struct repository *r) |
| 739 | { |
| 740 | struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git)); |
| 741 | |
| 742 | if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git, 0)) |
| 743 | return bitmap_git; |
| 744 | |
| 745 | free_bitmap_index(bitmap_git); |
| 746 | return NULL; |
| 747 | } |
| 748 | |
| 749 | struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx) |
| 750 | { |
| 751 | struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git)); |
| 752 | |
| 753 | if (!open_midx_bitmap(bitmap_git, midx)) |
| 754 | return bitmap_git; |
| 755 | |
| 756 | free_bitmap_index(bitmap_git); |
| 757 | return NULL; |
| 758 | } |
| 759 | |
| 760 | struct bitmap_index *prepare_bitmap_git_for_source(struct odb_source_packed *source) |
| 761 | { |
| 762 | struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git)); |
| 763 | |
| 764 | if (!open_bitmap_for_source(source, bitmap_git) && |
| 765 | !load_bitmap(source->base.odb->repo, bitmap_git, 0)) |
| 766 | return bitmap_git; |
| 767 | |
| 768 | free_bitmap_index(bitmap_git); |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | int bitmap_index_contains_pack(struct bitmap_index *bitmap, struct packed_git *pack) |
| 773 | { |
| 774 | for (; bitmap; bitmap = bitmap->base) { |
| 775 | if (bitmap_is_midx(bitmap)) { |
| 776 | for (size_t i = 0; i < bitmap->midx->num_packs; i++) |
| 777 | if (bitmap->midx->packs[i] == pack) |
| 778 | return 1; |
| 779 | } else if (bitmap->pack == pack) { |
| 780 | return 1; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | struct include_data { |
| 788 | struct bitmap_index *bitmap_git; |
| 789 | struct bitmap *base; |
| 790 | struct bitmap *seen; |
| 791 | }; |
| 792 | |
| 793 | struct bitmap_lookup_table_triplet { |
| 794 | uint32_t commit_pos; |
| 795 | uint64_t offset; |
| 796 | uint32_t xor_row; |
| 797 | }; |
| 798 | |
| 799 | struct bitmap_lookup_table_xor_item { |
| 800 | struct object_id oid; |
| 801 | uint64_t offset; |
| 802 | }; |
| 803 | |
| 804 | /* |
| 805 | * Given a `triplet` struct pointer and pointer `p`, this |
| 806 | * function reads the triplet beginning at `p` into the struct. |
| 807 | * Note that this function assumes that there is enough memory |
| 808 | * left for filling the `triplet` struct from `p`. |
| 809 | */ |
| 810 | static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet, |
| 811 | const unsigned char *p) |
| 812 | { |
| 813 | if (!triplet) |
| 814 | return -1; |
| 815 | |
| 816 | triplet->commit_pos = get_be32(p); |
| 817 | p += sizeof(uint32_t); |
| 818 | triplet->offset = get_be64(p); |
| 819 | p += sizeof(uint64_t); |
| 820 | triplet->xor_row = get_be32(p); |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | /* |
| 825 | * This function gets the raw triplet from `row`'th row in the |
| 826 | * lookup table and fills that data to the `triplet`. |
| 827 | */ |
| 828 | static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git, |
| 829 | uint32_t pos, |
| 830 | struct bitmap_lookup_table_triplet *triplet) |
| 831 | { |
| 832 | unsigned char *p = NULL; |
| 833 | if (pos >= bitmap_git->entry_count) |
| 834 | return error(_("corrupt bitmap lookup table: triplet position out of index")); |
| 835 | |
| 836 | p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH); |
| 837 | |
| 838 | return bitmap_lookup_table_get_triplet_by_pointer(triplet, p); |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * Searches for a matching triplet. `commit_pos` is a pointer |
| 843 | * to the wanted commit position value. `table_entry` points to |
| 844 | * a triplet in lookup table. The first 4 bytes of each |
| 845 | * triplet (pointed by `table_entry`) are compared with `*commit_pos`. |
| 846 | */ |
| 847 | static int triplet_cmp(const void *commit_pos, const void *table_entry) |
| 848 | { |
| 849 | |
| 850 | uint32_t a = *(uint32_t *)commit_pos; |
| 851 | uint32_t b = get_be32(table_entry); |
| 852 | if (a > b) |
| 853 | return 1; |
| 854 | else if (a < b) |
| 855 | return -1; |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git, |
| 861 | struct object_id *oid, |
| 862 | uint32_t *result) |
| 863 | { |
| 864 | int found; |
| 865 | |
| 866 | if (bitmap_is_midx(bitmap_git)) |
| 867 | found = bsearch_midx(oid, bitmap_git->midx, result); |
| 868 | else |
| 869 | found = bsearch_pack(oid, bitmap_git->pack, result); |
| 870 | |
| 871 | return found; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * `bsearch_triplet_by_pos` function searches for the raw triplet |
| 876 | * having commit position same as `commit_pos` and fills `triplet` |
| 877 | * object from the raw triplet. Returns 1 on success and 0 on |
| 878 | * failure. |
| 879 | */ |
| 880 | static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos, |
| 881 | struct bitmap_index *bitmap_git, |
| 882 | struct bitmap_lookup_table_triplet *triplet) |
| 883 | { |
| 884 | unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count, |
| 885 | BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp); |
| 886 | |
| 887 | if (!p) |
| 888 | return -1; |
| 889 | |
| 890 | return bitmap_lookup_table_get_triplet_by_pointer(triplet, p); |
| 891 | } |
| 892 | |
| 893 | static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git, |
| 894 | struct commit *commit) |
| 895 | { |
| 896 | uint32_t commit_pos, xor_row; |
| 897 | uint64_t offset; |
| 898 | int flags; |
| 899 | struct bitmap_lookup_table_triplet triplet; |
| 900 | struct object_id *oid = &commit->object.oid; |
| 901 | struct ewah_bitmap *bitmap; |
| 902 | struct stored_bitmap *xor_bitmap = NULL; |
| 903 | const int bitmap_header_size = 6; |
| 904 | static struct bitmap_lookup_table_xor_item *xor_items = NULL; |
| 905 | static size_t xor_items_nr = 0, xor_items_alloc = 0; |
| 906 | static int is_corrupt = 0; |
| 907 | int xor_flags; |
| 908 | khiter_t hash_pos; |
| 909 | struct bitmap_lookup_table_xor_item *xor_item; |
| 910 | size_t entry_map_pos; |
| 911 | |
| 912 | if (is_corrupt) |
| 913 | return NULL; |
| 914 | |
| 915 | if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos)) |
| 916 | return NULL; |
| 917 | |
| 918 | if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0) |
| 919 | return NULL; |
| 920 | |
| 921 | xor_items_nr = 0; |
| 922 | offset = triplet.offset; |
| 923 | xor_row = triplet.xor_row; |
| 924 | |
| 925 | while (xor_row != 0xffffffff) { |
| 926 | ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc); |
| 927 | |
| 928 | if (xor_items_nr + 1 >= bitmap_git->entry_count) { |
| 929 | error(_("corrupt bitmap lookup table: xor chain exceeds entry count")); |
| 930 | goto corrupt; |
| 931 | } |
| 932 | |
| 933 | if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0) |
| 934 | goto corrupt; |
| 935 | |
| 936 | xor_item = &xor_items[xor_items_nr]; |
| 937 | xor_item->offset = triplet.offset; |
| 938 | |
| 939 | if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) { |
| 940 | error(_("corrupt bitmap lookup table: commit index %u out of range"), |
| 941 | triplet.commit_pos); |
| 942 | goto corrupt; |
| 943 | } |
| 944 | |
| 945 | hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid); |
| 946 | |
| 947 | /* |
| 948 | * If desired bitmap is already stored, we don't need |
| 949 | * to iterate further. Because we know that bitmaps |
| 950 | * that are needed to be parsed to parse this bitmap |
| 951 | * has already been stored. So, assign this stored bitmap |
| 952 | * to the xor_bitmap. |
| 953 | */ |
| 954 | if (hash_pos < kh_end(bitmap_git->bitmaps) && |
| 955 | (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos))) |
| 956 | break; |
| 957 | xor_items_nr++; |
| 958 | xor_row = triplet.xor_row; |
| 959 | } |
| 960 | |
| 961 | while (xor_items_nr) { |
| 962 | xor_item = &xor_items[xor_items_nr - 1]; |
| 963 | bitmap_git->map_pos = xor_item->offset; |
| 964 | if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) { |
| 965 | error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""), |
| 966 | oid_to_hex(&xor_item->oid)); |
| 967 | goto corrupt; |
| 968 | } |
| 969 | |
| 970 | entry_map_pos = bitmap_git->map_pos; |
| 971 | bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t); |
| 972 | xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos); |
| 973 | bitmap = read_bitmap_1(bitmap_git); |
| 974 | |
| 975 | if (!bitmap) |
| 976 | goto corrupt; |
| 977 | |
| 978 | xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, |
| 979 | xor_bitmap, xor_flags, entry_map_pos); |
| 980 | xor_items_nr--; |
| 981 | } |
| 982 | |
| 983 | bitmap_git->map_pos = offset; |
| 984 | if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) { |
| 985 | error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""), |
| 986 | oid_to_hex(oid)); |
| 987 | goto corrupt; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * Don't bother reading the commit's index position or its xor |
| 992 | * offset: |
| 993 | * |
| 994 | * - The commit's index position is irrelevant to us, since |
| 995 | * load_bitmap_entries_v1 only uses it to learn the object |
| 996 | * id which is used to compute the hashmap's key. We already |
| 997 | * have an object id, so no need to look it up again. |
| 998 | * |
| 999 | * - The xor_offset is unusable for us, since it specifies how |
| 1000 | * many entries previous to ours we should look at. This |
| 1001 | * makes sense when reading the bitmaps sequentially (as in |
| 1002 | * load_bitmap_entries_v1()), since we can keep track of |
| 1003 | * each bitmap as we read them. |
| 1004 | * |
| 1005 | * But it can't work for us, since the bitmap's don't have a |
| 1006 | * fixed size. So we learn the position of the xor'd bitmap |
| 1007 | * from the commit table (and resolve it to a bitmap in the |
| 1008 | * above if-statement). |
| 1009 | * |
| 1010 | * Instead, we can skip ahead and immediately read the flags and |
| 1011 | * ewah bitmap. |
| 1012 | */ |
| 1013 | entry_map_pos = bitmap_git->map_pos; |
| 1014 | bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t); |
| 1015 | flags = read_u8(bitmap_git->map, &bitmap_git->map_pos); |
| 1016 | bitmap = read_bitmap_1(bitmap_git); |
| 1017 | |
| 1018 | if (!bitmap) |
| 1019 | goto corrupt; |
| 1020 | |
| 1021 | return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags, |
| 1022 | entry_map_pos); |
| 1023 | |
| 1024 | corrupt: |
| 1025 | free(xor_items); |
| 1026 | is_corrupt = 1; |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | |
| 1030 | static struct ewah_bitmap *find_bitmap_for_commit(struct bitmap_index *bitmap_git, |
| 1031 | struct commit *commit, |
| 1032 | struct bitmap_index **found) |
| 1033 | { |
| 1034 | khiter_t hash_pos; |
| 1035 | if (!bitmap_git) |
| 1036 | return NULL; |
| 1037 | |
| 1038 | hash_pos = kh_get_oid_map(bitmap_git->bitmaps, commit->object.oid); |
| 1039 | if (hash_pos >= kh_end(bitmap_git->bitmaps)) { |
| 1040 | struct stored_bitmap *bitmap = NULL; |
| 1041 | if (!bitmap_git->table_lookup) |
| 1042 | return find_bitmap_for_commit(bitmap_git->base, commit, |
| 1043 | found); |
| 1044 | |
| 1045 | /* this is a fairly hot codepath - no trace2_region please */ |
| 1046 | /* NEEDSWORK: cache misses aren't recorded */ |
| 1047 | bitmap = lazy_bitmap_for_commit(bitmap_git, commit); |
| 1048 | if (!bitmap) |
| 1049 | return find_bitmap_for_commit(bitmap_git->base, commit, |
| 1050 | found); |
| 1051 | if (found) |
| 1052 | *found = bitmap_git; |
| 1053 | return lookup_stored_bitmap(bitmap); |
| 1054 | } |
| 1055 | if (found) |
| 1056 | *found = bitmap_git; |
| 1057 | return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos)); |
| 1058 | } |
| 1059 | |
| 1060 | struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git, |
| 1061 | struct commit *commit) |
| 1062 | { |
| 1063 | return find_bitmap_for_commit(bitmap_git, commit, NULL); |
| 1064 | } |
| 1065 | |
| 1066 | static inline int bitmap_position_extended(struct bitmap_index *bitmap_git, |
| 1067 | const struct object_id *oid) |
| 1068 | { |
| 1069 | kh_oid_pos_t *positions = bitmap_git->ext_index.positions; |
| 1070 | khiter_t pos = kh_get_oid_pos(positions, *oid); |
| 1071 | |
| 1072 | if (pos < kh_end(positions)) { |
| 1073 | int bitmap_pos = kh_value(positions, pos); |
| 1074 | return bitmap_pos + bitmap_num_objects_total(bitmap_git); |
| 1075 | } |
| 1076 | |
| 1077 | return -1; |
| 1078 | } |
| 1079 | |
| 1080 | static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git, |
| 1081 | const struct object_id *oid) |
| 1082 | { |
| 1083 | uint32_t pos; |
| 1084 | off_t offset = find_pack_entry_one(oid, bitmap_git->pack); |
| 1085 | if (!offset) |
| 1086 | return -1; |
| 1087 | |
| 1088 | if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0) |
| 1089 | return -1; |
| 1090 | return pos; |
| 1091 | } |
| 1092 | |
| 1093 | static int bitmap_position_midx(struct bitmap_index *bitmap_git, |
| 1094 | const struct object_id *oid) |
| 1095 | { |
| 1096 | uint32_t want, got; |
| 1097 | if (!bsearch_midx(oid, bitmap_git->midx, &want)) |
| 1098 | return -1; |
| 1099 | |
| 1100 | if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0) |
| 1101 | return -1; |
| 1102 | return got; |
| 1103 | } |
| 1104 | |
| 1105 | static int bitmap_position(struct bitmap_index *bitmap_git, |
| 1106 | const struct object_id *oid) |
| 1107 | { |
| 1108 | int pos; |
| 1109 | if (bitmap_is_midx(bitmap_git)) |
| 1110 | pos = bitmap_position_midx(bitmap_git, oid); |
| 1111 | else |
| 1112 | pos = bitmap_position_packfile(bitmap_git, oid); |
| 1113 | return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid); |
| 1114 | } |
| 1115 | |
| 1116 | static int ext_index_add_object(struct bitmap_index *bitmap_git, |
| 1117 | struct object *object, const char *name) |
| 1118 | { |
| 1119 | struct eindex *eindex = &bitmap_git->ext_index; |
| 1120 | |
| 1121 | khiter_t hash_pos; |
| 1122 | int hash_ret; |
| 1123 | int bitmap_pos; |
| 1124 | |
| 1125 | hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret); |
| 1126 | if (hash_ret > 0) { |
| 1127 | if (eindex->count >= eindex->alloc) { |
| 1128 | eindex->alloc = (eindex->alloc + 16) * 3 / 2; |
| 1129 | REALLOC_ARRAY(eindex->objects, eindex->alloc); |
| 1130 | REALLOC_ARRAY(eindex->hashes, eindex->alloc); |
| 1131 | } |
| 1132 | |
| 1133 | bitmap_pos = eindex->count; |
| 1134 | eindex->objects[eindex->count] = object; |
| 1135 | eindex->hashes[eindex->count] = pack_name_hash(name); |
| 1136 | kh_value(eindex->positions, hash_pos) = bitmap_pos; |
| 1137 | eindex->count++; |
| 1138 | } else { |
| 1139 | bitmap_pos = kh_value(eindex->positions, hash_pos); |
| 1140 | } |
| 1141 | |
| 1142 | return bitmap_pos + bitmap_num_objects_total(bitmap_git); |
| 1143 | } |
| 1144 | |
| 1145 | struct bitmap_show_data { |
| 1146 | struct bitmap_index *bitmap_git; |
| 1147 | struct bitmap *base; |
| 1148 | }; |
| 1149 | |
| 1150 | static void show_object(struct object *object, const char *name, void *data_) |
| 1151 | { |
| 1152 | struct bitmap_show_data *data = data_; |
| 1153 | int bitmap_pos; |
| 1154 | |
| 1155 | bitmap_pos = bitmap_position(data->bitmap_git, &object->oid); |
| 1156 | |
| 1157 | if (bitmap_pos < 0) |
| 1158 | bitmap_pos = ext_index_add_object(data->bitmap_git, object, |
| 1159 | name); |
| 1160 | |
| 1161 | bitmap_set(data->base, bitmap_pos); |
| 1162 | } |
| 1163 | |
| 1164 | static void show_commit(struct commit *commit UNUSED, |
| 1165 | void *data UNUSED) |
| 1166 | { |
| 1167 | } |
| 1168 | |
| 1169 | static unsigned apply_pseudo_merges_for_commit_1(struct bitmap_index *bitmap_git, |
| 1170 | struct bitmap *result, |
| 1171 | struct commit *commit, |
| 1172 | uint32_t commit_pos) |
| 1173 | { |
| 1174 | struct bitmap_index *curr = bitmap_git; |
| 1175 | int ret = 0; |
| 1176 | |
| 1177 | while (curr) { |
| 1178 | ret += apply_pseudo_merges_for_commit(&curr->pseudo_merges, |
| 1179 | result, commit, |
| 1180 | commit_pos); |
| 1181 | curr = curr->base; |
| 1182 | } |
| 1183 | |
| 1184 | if (ret) |
| 1185 | pseudo_merges_satisfied_nr += ret; |
| 1186 | |
| 1187 | return ret; |
| 1188 | } |
| 1189 | |
| 1190 | static int add_to_include_set(struct bitmap_index *bitmap_git, |
| 1191 | struct include_data *data, |
| 1192 | struct commit *commit, |
| 1193 | int bitmap_pos) |
| 1194 | { |
| 1195 | struct ewah_bitmap *partial; |
| 1196 | |
| 1197 | if (data->seen && bitmap_get(data->seen, bitmap_pos)) |
| 1198 | return 0; |
| 1199 | |
| 1200 | if (bitmap_get(data->base, bitmap_pos)) |
| 1201 | return 0; |
| 1202 | |
| 1203 | partial = bitmap_for_commit(bitmap_git, commit); |
| 1204 | if (partial) { |
| 1205 | existing_bitmaps_hits_nr++; |
| 1206 | |
| 1207 | bitmap_or_ewah(data->base, partial); |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | existing_bitmaps_misses_nr++; |
| 1212 | |
| 1213 | bitmap_set(data->base, bitmap_pos); |
| 1214 | if (apply_pseudo_merges_for_commit_1(bitmap_git, data->base, commit, |
| 1215 | bitmap_pos)) |
| 1216 | return 0; |
| 1217 | |
| 1218 | return 1; |
| 1219 | } |
| 1220 | |
| 1221 | static int should_include(struct commit *commit, void *_data) |
| 1222 | { |
| 1223 | struct include_data *data = _data; |
| 1224 | int bitmap_pos; |
| 1225 | |
| 1226 | bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid); |
| 1227 | if (bitmap_pos < 0) |
| 1228 | bitmap_pos = ext_index_add_object(data->bitmap_git, |
| 1229 | (struct object *)commit, |
| 1230 | NULL); |
| 1231 | |
| 1232 | if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) { |
| 1233 | struct commit_list *parent = commit->parents; |
| 1234 | |
| 1235 | while (parent) { |
| 1236 | parent->item->object.flags |= SEEN; |
| 1237 | parent = parent->next; |
| 1238 | } |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | return 1; |
| 1244 | } |
| 1245 | |
| 1246 | static int should_include_obj(struct object *obj, void *_data) |
| 1247 | { |
| 1248 | struct include_data *data = _data; |
| 1249 | int bitmap_pos; |
| 1250 | |
| 1251 | bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid); |
| 1252 | if (bitmap_pos < 0) |
| 1253 | return 1; |
| 1254 | if ((data->seen && bitmap_get(data->seen, bitmap_pos)) || |
| 1255 | bitmap_get(data->base, bitmap_pos)) { |
| 1256 | obj->flags |= SEEN; |
| 1257 | return 0; |
| 1258 | } |
| 1259 | return 1; |
| 1260 | } |
| 1261 | |
| 1262 | static int add_commit_to_bitmap(struct bitmap_index *bitmap_git, |
| 1263 | struct bitmap **base, |
| 1264 | struct commit *commit) |
| 1265 | { |
| 1266 | struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit); |
| 1267 | |
| 1268 | if (!or_with) { |
| 1269 | existing_bitmaps_misses_nr++; |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | existing_bitmaps_hits_nr++; |
| 1274 | |
| 1275 | if (!*base) |
| 1276 | *base = ewah_to_bitmap(or_with); |
| 1277 | else |
| 1278 | bitmap_or_ewah(*base, or_with); |
| 1279 | |
| 1280 | return 1; |
| 1281 | } |
| 1282 | |
| 1283 | static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git, |
| 1284 | struct rev_info *revs, |
| 1285 | struct bitmap *base, |
| 1286 | struct bitmap *seen) |
| 1287 | { |
| 1288 | struct include_data incdata; |
| 1289 | struct bitmap_show_data show_data; |
| 1290 | |
| 1291 | if (!base) |
| 1292 | base = bitmap_new(); |
| 1293 | |
| 1294 | incdata.bitmap_git = bitmap_git; |
| 1295 | incdata.base = base; |
| 1296 | incdata.seen = seen; |
| 1297 | |
| 1298 | revs->include_check = should_include; |
| 1299 | revs->include_check_obj = should_include_obj; |
| 1300 | revs->include_check_data = &incdata; |
| 1301 | |
| 1302 | if (prepare_revision_walk(revs)) |
| 1303 | die(_("revision walk setup failed")); |
| 1304 | |
| 1305 | show_data.bitmap_git = bitmap_git; |
| 1306 | show_data.base = base; |
| 1307 | |
| 1308 | traverse_commit_list(revs, show_commit, show_object, &show_data); |
| 1309 | |
| 1310 | revs->include_check = NULL; |
| 1311 | revs->include_check_obj = NULL; |
| 1312 | revs->include_check_data = NULL; |
| 1313 | |
| 1314 | return base; |
| 1315 | } |
| 1316 | |
| 1317 | struct bitmap_boundary_cb { |
| 1318 | struct bitmap_index *bitmap_git; |
| 1319 | struct bitmap *base; |
| 1320 | |
| 1321 | struct object_array boundary; |
| 1322 | }; |
| 1323 | |
| 1324 | static void show_boundary_commit(struct commit *commit, void *_data) |
| 1325 | { |
| 1326 | struct bitmap_boundary_cb *data = _data; |
| 1327 | |
| 1328 | if (commit->object.flags & BOUNDARY) |
| 1329 | add_object_array(&commit->object, "", &data->boundary); |
| 1330 | |
| 1331 | if (commit->object.flags & UNINTERESTING) { |
| 1332 | if (bitmap_walk_contains(data->bitmap_git, data->base, |
| 1333 | &commit->object.oid)) |
| 1334 | return; |
| 1335 | |
| 1336 | add_commit_to_bitmap(data->bitmap_git, &data->base, commit); |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | static void show_boundary_object(struct object *object UNUSED, |
| 1341 | const char *name UNUSED, |
| 1342 | void *data UNUSED) |
| 1343 | { |
| 1344 | BUG("should not be called"); |
| 1345 | } |
| 1346 | |
| 1347 | static unsigned cascade_pseudo_merges_1(struct bitmap_index *bitmap_git, |
| 1348 | struct bitmap *result, |
| 1349 | struct bitmap *roots) |
| 1350 | { |
| 1351 | int ret = cascade_pseudo_merges(&bitmap_git->pseudo_merges, |
| 1352 | result, roots); |
| 1353 | if (ret) { |
| 1354 | pseudo_merges_cascades_nr++; |
| 1355 | pseudo_merges_satisfied_nr += ret; |
| 1356 | } |
| 1357 | |
| 1358 | return ret; |
| 1359 | } |
| 1360 | |
| 1361 | static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git, |
| 1362 | struct rev_info *revs, |
| 1363 | struct object_list *roots) |
| 1364 | { |
| 1365 | struct bitmap_boundary_cb cb; |
| 1366 | struct object_list *root; |
| 1367 | struct repository *repo; |
| 1368 | unsigned int i; |
| 1369 | unsigned int tmp_blobs, tmp_trees, tmp_tags; |
| 1370 | int any_missing = 0; |
| 1371 | int existing_bitmaps = 0; |
| 1372 | |
| 1373 | cb.bitmap_git = bitmap_git; |
| 1374 | cb.base = bitmap_new(); |
| 1375 | object_array_init(&cb.boundary); |
| 1376 | |
| 1377 | repo = bitmap_repo(bitmap_git); |
| 1378 | |
| 1379 | revs->ignore_missing_links = 1; |
| 1380 | |
| 1381 | if (bitmap_git->pseudo_merges.nr) { |
| 1382 | struct bitmap *roots_bitmap = bitmap_new(); |
| 1383 | struct object_list *objects = NULL; |
| 1384 | |
| 1385 | for (objects = roots; objects; objects = objects->next) { |
| 1386 | struct object *object = objects->item; |
| 1387 | int pos; |
| 1388 | |
| 1389 | pos = bitmap_position(bitmap_git, &object->oid); |
| 1390 | if (pos < 0) |
| 1391 | continue; |
| 1392 | |
| 1393 | bitmap_set(roots_bitmap, pos); |
| 1394 | } |
| 1395 | |
| 1396 | cascade_pseudo_merges_1(bitmap_git, cb.base, roots_bitmap); |
| 1397 | bitmap_free(roots_bitmap); |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * OR in any existing reachability bitmaps among `roots` into |
| 1402 | * `cb.base`. |
| 1403 | */ |
| 1404 | for (root = roots; root; root = root->next) { |
| 1405 | struct object *object = root->item; |
| 1406 | if (object->type != OBJ_COMMIT || |
| 1407 | bitmap_walk_contains(bitmap_git, cb.base, &object->oid)) |
| 1408 | continue; |
| 1409 | |
| 1410 | if (add_commit_to_bitmap(bitmap_git, &cb.base, |
| 1411 | (struct commit *)object)) { |
| 1412 | existing_bitmaps = 1; |
| 1413 | continue; |
| 1414 | } |
| 1415 | |
| 1416 | any_missing = 1; |
| 1417 | } |
| 1418 | |
| 1419 | if (!any_missing) |
| 1420 | goto cleanup; |
| 1421 | |
| 1422 | if (existing_bitmaps) |
| 1423 | cascade_pseudo_merges_1(bitmap_git, cb.base, NULL); |
| 1424 | |
| 1425 | tmp_blobs = revs->blob_objects; |
| 1426 | tmp_trees = revs->tree_objects; |
| 1427 | tmp_tags = revs->tag_objects; |
| 1428 | revs->blob_objects = 0; |
| 1429 | revs->tree_objects = 0; |
| 1430 | revs->tag_objects = 0; |
| 1431 | |
| 1432 | /* |
| 1433 | * We didn't have complete coverage of the roots. First setup a |
| 1434 | * revision walk to (a) OR in any bitmaps that are UNINTERESTING |
| 1435 | * between the tips and boundary, and (b) record the boundary. |
| 1436 | */ |
| 1437 | trace2_region_enter("pack-bitmap", "boundary-prepare", repo); |
| 1438 | if (prepare_revision_walk(revs)) |
| 1439 | die("revision walk setup failed"); |
| 1440 | trace2_region_leave("pack-bitmap", "boundary-prepare", repo); |
| 1441 | |
| 1442 | trace2_region_enter("pack-bitmap", "boundary-traverse", repo); |
| 1443 | revs->boundary = 1; |
| 1444 | traverse_commit_list_filtered(revs, |
| 1445 | show_boundary_commit, |
| 1446 | show_boundary_object, |
| 1447 | &cb, NULL); |
| 1448 | revs->boundary = 0; |
| 1449 | trace2_region_leave("pack-bitmap", "boundary-traverse", repo); |
| 1450 | |
| 1451 | revs->blob_objects = tmp_blobs; |
| 1452 | revs->tree_objects = tmp_trees; |
| 1453 | revs->tag_objects = tmp_tags; |
| 1454 | |
| 1455 | reset_revision_walk(); |
| 1456 | clear_object_flags(repo, UNINTERESTING); |
| 1457 | |
| 1458 | /* |
| 1459 | * Then add the boundary commit(s) as fill-in traversal tips. |
| 1460 | */ |
| 1461 | trace2_region_enter("pack-bitmap", "boundary-fill-in", repo); |
| 1462 | for (i = 0; i < cb.boundary.nr; i++) { |
| 1463 | struct object *obj = cb.boundary.objects[i].item; |
| 1464 | if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid)) |
| 1465 | obj->flags |= SEEN; |
| 1466 | else |
| 1467 | add_pending_object(revs, obj, ""); |
| 1468 | } |
| 1469 | if (revs->pending.nr) |
| 1470 | cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL); |
| 1471 | trace2_region_leave("pack-bitmap", "boundary-fill-in", repo); |
| 1472 | |
| 1473 | cleanup: |
| 1474 | object_array_clear(&cb.boundary); |
| 1475 | revs->ignore_missing_links = 0; |
| 1476 | |
| 1477 | return cb.base; |
| 1478 | } |
| 1479 | |
| 1480 | struct ewah_bitmap *pseudo_merge_bitmap_for_commit(struct bitmap_index *bitmap_git, |
| 1481 | struct commit *commit) |
| 1482 | { |
| 1483 | struct commit_list *p; |
| 1484 | struct bitmap *parents; |
| 1485 | struct pseudo_merge *match = NULL; |
| 1486 | |
| 1487 | if (!bitmap_git->pseudo_merges.nr) |
| 1488 | return NULL; |
| 1489 | |
| 1490 | parents = bitmap_new(); |
| 1491 | |
| 1492 | for (p = commit->parents; p; p = p->next) { |
| 1493 | int pos = bitmap_position(bitmap_git, &p->item->object.oid); |
| 1494 | if (pos < 0 || pos >= bitmap_num_objects(bitmap_git)) |
| 1495 | goto done; |
| 1496 | |
| 1497 | /* |
| 1498 | * Use bitmap-relative positions instead of offsetting |
| 1499 | * by bitmap_git->num_objects_in_base because we use |
| 1500 | * this to find a match in pseudo_merge_for_parents(), |
| 1501 | * and pseudo-merge groups cannot span multiple bitmap |
| 1502 | * layers. |
| 1503 | */ |
| 1504 | bitmap_set(parents, pos); |
| 1505 | } |
| 1506 | |
| 1507 | match = pseudo_merge_for_parents(&bitmap_git->pseudo_merges, parents); |
| 1508 | |
| 1509 | done: |
| 1510 | bitmap_free(parents); |
| 1511 | if (match) |
| 1512 | return pseudo_merge_bitmap(&bitmap_git->pseudo_merges, match); |
| 1513 | |
| 1514 | return NULL; |
| 1515 | } |
| 1516 | |
| 1517 | static void unsatisfy_all_pseudo_merges(struct bitmap_index *bitmap_git) |
| 1518 | { |
| 1519 | uint32_t i; |
| 1520 | for (i = 0; i < bitmap_git->pseudo_merges.nr; i++) |
| 1521 | bitmap_git->pseudo_merges.v[i].satisfied = 0; |
| 1522 | } |
| 1523 | |
| 1524 | static struct bitmap *find_objects(struct bitmap_index *bitmap_git, |
| 1525 | struct rev_info *revs, |
| 1526 | struct object_list *roots, |
| 1527 | struct bitmap *seen) |
| 1528 | { |
| 1529 | struct bitmap *base = NULL; |
| 1530 | int needs_walk = 0; |
| 1531 | unsigned existing_bitmaps = 0; |
| 1532 | |
| 1533 | struct object_list *not_mapped = NULL; |
| 1534 | |
| 1535 | unsatisfy_all_pseudo_merges(bitmap_git); |
| 1536 | |
| 1537 | if (bitmap_git->pseudo_merges.nr) { |
| 1538 | struct bitmap *roots_bitmap = bitmap_new(); |
| 1539 | struct object_list *objects = NULL; |
| 1540 | |
| 1541 | for (objects = roots; objects; objects = objects->next) { |
| 1542 | struct object *object = objects->item; |
| 1543 | int pos; |
| 1544 | |
| 1545 | pos = bitmap_position(bitmap_git, &object->oid); |
| 1546 | if (pos < 0) |
| 1547 | continue; |
| 1548 | |
| 1549 | bitmap_set(roots_bitmap, pos); |
| 1550 | } |
| 1551 | |
| 1552 | base = bitmap_new(); |
| 1553 | cascade_pseudo_merges_1(bitmap_git, base, roots_bitmap); |
| 1554 | bitmap_free(roots_bitmap); |
| 1555 | } |
| 1556 | |
| 1557 | /* |
| 1558 | * Go through all the roots for the walk. The ones that have bitmaps |
| 1559 | * on the bitmap index will be `or`ed together to form an initial |
| 1560 | * global reachability analysis. |
| 1561 | * |
| 1562 | * The ones without bitmaps in the index will be stored in the |
| 1563 | * `not_mapped_list` for further processing. |
| 1564 | */ |
| 1565 | while (roots) { |
| 1566 | struct object *object = roots->item; |
| 1567 | |
| 1568 | roots = roots->next; |
| 1569 | |
| 1570 | if (base) { |
| 1571 | int pos = bitmap_position(bitmap_git, &object->oid); |
| 1572 | if (pos >= 0 && bitmap_get(base, pos)) { |
| 1573 | object->flags |= SEEN; |
| 1574 | continue; |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | if (object->type == OBJ_COMMIT && |
| 1579 | add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) { |
| 1580 | object->flags |= SEEN; |
| 1581 | existing_bitmaps = 1; |
| 1582 | continue; |
| 1583 | } |
| 1584 | |
| 1585 | object_list_insert(object, ¬_mapped); |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * Best case scenario: We found bitmaps for all the roots, |
| 1590 | * so the resulting `or` bitmap has the full reachability analysis |
| 1591 | */ |
| 1592 | if (!not_mapped) |
| 1593 | return base; |
| 1594 | |
| 1595 | roots = not_mapped; |
| 1596 | |
| 1597 | if (existing_bitmaps) |
| 1598 | cascade_pseudo_merges_1(bitmap_git, base, NULL); |
| 1599 | |
| 1600 | /* |
| 1601 | * Let's iterate through all the roots that don't have bitmaps to |
| 1602 | * check if we can determine them to be reachable from the existing |
| 1603 | * global bitmap. |
| 1604 | * |
| 1605 | * If we cannot find them in the existing global bitmap, we'll need |
| 1606 | * to push them to an actual walk and run it until we can confirm |
| 1607 | * they are reachable |
| 1608 | */ |
| 1609 | while (roots) { |
| 1610 | struct object *object = roots->item; |
| 1611 | int pos; |
| 1612 | |
| 1613 | roots = roots->next; |
| 1614 | pos = bitmap_position(bitmap_git, &object->oid); |
| 1615 | |
| 1616 | if (pos < 0 || base == NULL || !bitmap_get(base, pos)) { |
| 1617 | object->flags &= ~UNINTERESTING; |
| 1618 | add_pending_object(revs, object, ""); |
| 1619 | needs_walk = 1; |
| 1620 | |
| 1621 | roots_without_bitmaps_nr++; |
| 1622 | } else { |
| 1623 | object->flags |= SEEN; |
| 1624 | |
| 1625 | roots_with_bitmaps_nr++; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | if (needs_walk) { |
| 1630 | /* |
| 1631 | * This fill-in traversal may walk over some objects |
| 1632 | * again, since we have already traversed in order to |
| 1633 | * find the boundary. |
| 1634 | * |
| 1635 | * But this extra walk should be extremely cheap, since |
| 1636 | * all commit objects are loaded into memory, and |
| 1637 | * because we skip walking to parents that are |
| 1638 | * UNINTERESTING, since it will be marked in the haves |
| 1639 | * bitmap already (or it has an on-disk bitmap, since |
| 1640 | * OR-ing it in covers all of its ancestors). |
| 1641 | */ |
| 1642 | base = fill_in_bitmap(bitmap_git, revs, base, seen); |
| 1643 | } |
| 1644 | |
| 1645 | object_list_free(¬_mapped); |
| 1646 | |
| 1647 | return base; |
| 1648 | } |
| 1649 | |
| 1650 | static void show_extended_objects(struct bitmap_index *bitmap_git, |
| 1651 | struct rev_info *revs, |
| 1652 | show_reachable_fn show_reach) |
| 1653 | { |
| 1654 | struct bitmap *objects = bitmap_git->result; |
| 1655 | struct eindex *eindex = &bitmap_git->ext_index; |
| 1656 | uint32_t i; |
| 1657 | |
| 1658 | for (i = 0; i < eindex->count; ++i) { |
| 1659 | struct object *obj; |
| 1660 | |
| 1661 | if (!bitmap_get(objects, |
| 1662 | st_add(bitmap_num_objects_total(bitmap_git), |
| 1663 | i))) |
| 1664 | continue; |
| 1665 | |
| 1666 | obj = eindex->objects[i]; |
| 1667 | if ((obj->type == OBJ_BLOB && !revs->blob_objects) || |
| 1668 | (obj->type == OBJ_TREE && !revs->tree_objects) || |
| 1669 | (obj->type == OBJ_TAG && !revs->tag_objects)) |
| 1670 | continue; |
| 1671 | |
| 1672 | show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0, NULL); |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | static void init_type_iterator(struct ewah_or_iterator *it, |
| 1677 | struct bitmap_index *bitmap_git, |
| 1678 | enum object_type type) |
| 1679 | { |
| 1680 | switch (type) { |
| 1681 | case OBJ_COMMIT: |
| 1682 | ewah_or_iterator_init(it, bitmap_git->commits_all, |
| 1683 | bitmap_git->base_nr + 1); |
| 1684 | break; |
| 1685 | |
| 1686 | case OBJ_TREE: |
| 1687 | ewah_or_iterator_init(it, bitmap_git->trees_all, |
| 1688 | bitmap_git->base_nr + 1); |
| 1689 | break; |
| 1690 | |
| 1691 | case OBJ_BLOB: |
| 1692 | ewah_or_iterator_init(it, bitmap_git->blobs_all, |
| 1693 | bitmap_git->base_nr + 1); |
| 1694 | break; |
| 1695 | |
| 1696 | case OBJ_TAG: |
| 1697 | ewah_or_iterator_init(it, bitmap_git->tags_all, |
| 1698 | bitmap_git->base_nr + 1); |
| 1699 | break; |
| 1700 | |
| 1701 | default: |
| 1702 | BUG("object type %d not stored by bitmap type index", type); |
| 1703 | break; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | static int show_objects_for_type( |
| 1708 | struct bitmap_index *bitmap_git, |
| 1709 | struct bitmap *objects, |
| 1710 | enum object_type object_type, |
| 1711 | show_reachable_fn show_reach, |
| 1712 | void *payload) |
| 1713 | { |
| 1714 | size_t i = 0; |
| 1715 | uint32_t offset; |
| 1716 | int ret; |
| 1717 | |
| 1718 | struct ewah_or_iterator it; |
| 1719 | eword_t filter; |
| 1720 | |
| 1721 | init_type_iterator(&it, bitmap_git, object_type); |
| 1722 | |
| 1723 | for (i = 0; i < objects->word_alloc && |
| 1724 | ewah_or_iterator_next(&filter, &it); i++) { |
| 1725 | eword_t word = objects->words[i] & filter; |
| 1726 | size_t pos = (i * BITS_IN_EWORD); |
| 1727 | |
| 1728 | if (!word) |
| 1729 | continue; |
| 1730 | |
| 1731 | for (offset = 0; offset < BITS_IN_EWORD; ++offset) { |
| 1732 | struct packed_git *pack; |
| 1733 | struct object_id oid; |
| 1734 | uint32_t hash = 0, index_pos; |
| 1735 | off_t ofs; |
| 1736 | |
| 1737 | if ((word >> offset) == 0) |
| 1738 | break; |
| 1739 | |
| 1740 | offset += ewah_bit_ctz64(word >> offset); |
| 1741 | |
| 1742 | if (bitmap_is_midx(bitmap_git)) { |
| 1743 | struct multi_pack_index *m = bitmap_git->midx; |
| 1744 | uint32_t pack_id; |
| 1745 | |
| 1746 | index_pos = pack_pos_to_midx(m, pos + offset); |
| 1747 | ofs = nth_midxed_offset(m, index_pos); |
| 1748 | nth_midxed_object_oid(&oid, m, index_pos); |
| 1749 | |
| 1750 | pack_id = nth_midxed_pack_int_id(m, index_pos); |
| 1751 | pack = nth_midxed_pack(bitmap_git->midx, pack_id); |
| 1752 | } else { |
| 1753 | index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset); |
| 1754 | ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset); |
| 1755 | nth_bitmap_object_oid(bitmap_git, &oid, index_pos); |
| 1756 | |
| 1757 | pack = bitmap_git->pack; |
| 1758 | } |
| 1759 | |
| 1760 | hash = bitmap_name_hash(bitmap_git, index_pos); |
| 1761 | |
| 1762 | ret = show_reach(&oid, object_type, 0, hash, pack, ofs, payload); |
| 1763 | if (ret) |
| 1764 | goto out; |
| 1765 | } |
| 1766 | } |
| 1767 | |
| 1768 | ret = 0; |
| 1769 | |
| 1770 | out: |
| 1771 | ewah_or_iterator_release(&it); |
| 1772 | return ret; |
| 1773 | } |
| 1774 | |
| 1775 | static int in_bitmapped_pack(struct bitmap_index *bitmap_git, |
| 1776 | struct object_list *roots) |
| 1777 | { |
| 1778 | while (roots) { |
| 1779 | struct object *object = roots->item; |
| 1780 | roots = roots->next; |
| 1781 | |
| 1782 | if (bitmap_is_midx(bitmap_git)) { |
| 1783 | if (bsearch_midx(&object->oid, bitmap_git->midx, NULL)) |
| 1784 | return 1; |
| 1785 | } else { |
| 1786 | if (find_pack_entry_one(&object->oid, bitmap_git->pack) > 0) |
| 1787 | return 1; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git, |
| 1795 | struct object_list *tip_objects, |
| 1796 | enum object_type type) |
| 1797 | { |
| 1798 | struct bitmap *result = bitmap_new(); |
| 1799 | struct object_list *p; |
| 1800 | |
| 1801 | for (p = tip_objects; p; p = p->next) { |
| 1802 | int pos; |
| 1803 | |
| 1804 | if (p->item->type != type) |
| 1805 | continue; |
| 1806 | |
| 1807 | pos = bitmap_position(bitmap_git, &p->item->oid); |
| 1808 | if (pos < 0) |
| 1809 | continue; |
| 1810 | |
| 1811 | bitmap_set(result, pos); |
| 1812 | } |
| 1813 | |
| 1814 | return result; |
| 1815 | } |
| 1816 | |
| 1817 | static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git, |
| 1818 | struct object_list *tip_objects, |
| 1819 | struct bitmap *to_filter, |
| 1820 | enum object_type type) |
| 1821 | { |
| 1822 | struct eindex *eindex = &bitmap_git->ext_index; |
| 1823 | struct bitmap *tips; |
| 1824 | struct ewah_or_iterator it; |
| 1825 | eword_t mask; |
| 1826 | uint32_t i; |
| 1827 | |
| 1828 | /* |
| 1829 | * The non-bitmap version of this filter never removes |
| 1830 | * objects which the other side specifically asked for, |
| 1831 | * so we must match that behavior. |
| 1832 | */ |
| 1833 | tips = find_tip_objects(bitmap_git, tip_objects, type); |
| 1834 | |
| 1835 | /* |
| 1836 | * We can use the type-level bitmap for 'type' to work in whole |
| 1837 | * words for the objects that are actually in the bitmapped |
| 1838 | * packfile. |
| 1839 | */ |
| 1840 | for (i = 0, init_type_iterator(&it, bitmap_git, type); |
| 1841 | i < to_filter->word_alloc && ewah_or_iterator_next(&mask, &it); |
| 1842 | i++) { |
| 1843 | if (i < tips->word_alloc) |
| 1844 | mask &= ~tips->words[i]; |
| 1845 | to_filter->words[i] &= ~mask; |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * Clear any objects that weren't in the packfile (and so would |
| 1850 | * not have been caught by the loop above. We'll have to check |
| 1851 | * them individually. |
| 1852 | */ |
| 1853 | for (i = 0; i < eindex->count; i++) { |
| 1854 | size_t pos = st_add(i, bitmap_num_objects_total(bitmap_git)); |
| 1855 | if (eindex->objects[i]->type == type && |
| 1856 | bitmap_get(to_filter, pos) && |
| 1857 | !bitmap_get(tips, pos)) |
| 1858 | bitmap_unset(to_filter, pos); |
| 1859 | } |
| 1860 | |
| 1861 | ewah_or_iterator_release(&it); |
| 1862 | bitmap_free(tips); |
| 1863 | } |
| 1864 | |
| 1865 | static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git, |
| 1866 | struct object_list *tip_objects, |
| 1867 | struct bitmap *to_filter) |
| 1868 | { |
| 1869 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, |
| 1870 | OBJ_BLOB); |
| 1871 | } |
| 1872 | |
| 1873 | static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git, |
| 1874 | uint32_t pos) |
| 1875 | { |
| 1876 | size_t size; |
| 1877 | struct object_info oi = OBJECT_INFO_INIT; |
| 1878 | |
| 1879 | oi.sizep = &size; |
| 1880 | |
| 1881 | if (pos < bitmap_num_objects_total(bitmap_git)) { |
| 1882 | struct packed_git *pack; |
| 1883 | off_t ofs; |
| 1884 | |
| 1885 | if (bitmap_is_midx(bitmap_git)) { |
| 1886 | uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos); |
| 1887 | uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos); |
| 1888 | |
| 1889 | pack = nth_midxed_pack(bitmap_git->midx, pack_id); |
| 1890 | ofs = nth_midxed_offset(bitmap_git->midx, midx_pos); |
| 1891 | } else { |
| 1892 | pack = bitmap_git->pack; |
| 1893 | ofs = pack_pos_to_offset(pack, pos); |
| 1894 | } |
| 1895 | |
| 1896 | if (packed_object_info(NULL, pack, ofs, &oi) < 0) { |
| 1897 | struct object_id oid; |
| 1898 | nth_bitmap_object_oid(bitmap_git, &oid, |
| 1899 | pack_pos_to_index(pack, pos)); |
| 1900 | die(_("unable to get size of %s"), oid_to_hex(&oid)); |
| 1901 | } |
| 1902 | } else { |
| 1903 | size_t eindex_pos = pos - bitmap_num_objects_total(bitmap_git); |
| 1904 | struct eindex *eindex = &bitmap_git->ext_index; |
| 1905 | struct object *obj = eindex->objects[eindex_pos]; |
| 1906 | if (odb_read_object_info_extended(bitmap_repo(bitmap_git)->objects, &obj->oid, |
| 1907 | &oi, 0) < 0) |
| 1908 | die(_("unable to get size of %s"), oid_to_hex(&obj->oid)); |
| 1909 | } |
| 1910 | |
| 1911 | return cast_size_t_to_ulong(size); |
| 1912 | } |
| 1913 | |
| 1914 | static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git, |
| 1915 | struct object_list *tip_objects, |
| 1916 | struct bitmap *to_filter, |
| 1917 | unsigned long limit) |
| 1918 | { |
| 1919 | struct eindex *eindex = &bitmap_git->ext_index; |
| 1920 | struct bitmap *tips; |
| 1921 | struct ewah_or_iterator it; |
| 1922 | eword_t mask; |
| 1923 | uint32_t i; |
| 1924 | |
| 1925 | tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB); |
| 1926 | |
| 1927 | for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB); |
| 1928 | i < to_filter->word_alloc && ewah_or_iterator_next(&mask, &it); |
| 1929 | i++) { |
| 1930 | eword_t word = to_filter->words[i] & mask; |
| 1931 | unsigned offset; |
| 1932 | |
| 1933 | for (offset = 0; offset < BITS_IN_EWORD; offset++) { |
| 1934 | uint32_t pos; |
| 1935 | |
| 1936 | if ((word >> offset) == 0) |
| 1937 | break; |
| 1938 | offset += ewah_bit_ctz64(word >> offset); |
| 1939 | pos = i * BITS_IN_EWORD + offset; |
| 1940 | |
| 1941 | if (!bitmap_get(tips, pos) && |
| 1942 | get_size_by_pos(bitmap_git, pos) >= limit) |
| 1943 | bitmap_unset(to_filter, pos); |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | for (i = 0; i < eindex->count; i++) { |
| 1948 | size_t pos = st_add(i, bitmap_num_objects(bitmap_git)); |
| 1949 | if (eindex->objects[i]->type == OBJ_BLOB && |
| 1950 | bitmap_get(to_filter, pos) && |
| 1951 | !bitmap_get(tips, pos) && |
| 1952 | get_size_by_pos(bitmap_git, pos) >= limit) |
| 1953 | bitmap_unset(to_filter, pos); |
| 1954 | } |
| 1955 | |
| 1956 | ewah_or_iterator_release(&it); |
| 1957 | bitmap_free(tips); |
| 1958 | } |
| 1959 | |
| 1960 | static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git, |
| 1961 | struct object_list *tip_objects, |
| 1962 | struct bitmap *to_filter, |
| 1963 | unsigned long limit) |
| 1964 | { |
| 1965 | if (limit) |
| 1966 | BUG("filter_bitmap_tree_depth given non-zero limit"); |
| 1967 | |
| 1968 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, |
| 1969 | OBJ_TREE); |
| 1970 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, |
| 1971 | OBJ_BLOB); |
| 1972 | } |
| 1973 | |
| 1974 | static void filter_bitmap_object_type(struct bitmap_index *bitmap_git, |
| 1975 | struct object_list *tip_objects, |
| 1976 | struct bitmap *to_filter, |
| 1977 | enum object_type object_type) |
| 1978 | { |
| 1979 | if (object_type < OBJ_COMMIT || object_type > OBJ_TAG) |
| 1980 | BUG("filter_bitmap_object_type given invalid object"); |
| 1981 | |
| 1982 | if (object_type != OBJ_TAG) |
| 1983 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG); |
| 1984 | if (object_type != OBJ_COMMIT) |
| 1985 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT); |
| 1986 | if (object_type != OBJ_TREE) |
| 1987 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE); |
| 1988 | if (object_type != OBJ_BLOB) |
| 1989 | filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB); |
| 1990 | } |
| 1991 | |
| 1992 | static int filter_bitmap(struct bitmap_index *bitmap_git, |
| 1993 | struct object_list *tip_objects, |
| 1994 | struct bitmap *to_filter, |
| 1995 | const struct list_objects_filter_options *filter) |
| 1996 | { |
| 1997 | if (!filter || filter->choice == LOFC_DISABLED) |
| 1998 | return 0; |
| 1999 | |
| 2000 | if (filter->choice == LOFC_BLOB_NONE) { |
| 2001 | if (bitmap_git) |
| 2002 | filter_bitmap_blob_none(bitmap_git, tip_objects, |
| 2003 | to_filter); |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
| 2007 | if (filter->choice == LOFC_BLOB_LIMIT) { |
| 2008 | if (bitmap_git) |
| 2009 | filter_bitmap_blob_limit(bitmap_git, tip_objects, |
| 2010 | to_filter, |
| 2011 | filter->blob_limit_value); |
| 2012 | return 0; |
| 2013 | } |
| 2014 | |
| 2015 | if (filter->choice == LOFC_TREE_DEPTH && |
| 2016 | filter->tree_exclude_depth == 0) { |
| 2017 | if (bitmap_git) |
| 2018 | filter_bitmap_tree_depth(bitmap_git, tip_objects, |
| 2019 | to_filter, |
| 2020 | filter->tree_exclude_depth); |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
| 2024 | if (filter->choice == LOFC_OBJECT_TYPE) { |
| 2025 | if (bitmap_git) |
| 2026 | filter_bitmap_object_type(bitmap_git, tip_objects, |
| 2027 | to_filter, |
| 2028 | filter->object_type); |
| 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | if (filter->choice == LOFC_COMBINE) { |
| 2033 | int i; |
| 2034 | for (i = 0; i < filter->sub_nr; i++) { |
| 2035 | if (filter_bitmap(bitmap_git, tip_objects, to_filter, |
| 2036 | &filter->sub[i]) < 0) |
| 2037 | return -1; |
| 2038 | } |
| 2039 | return 0; |
| 2040 | } |
| 2041 | |
| 2042 | /* filter choice not handled */ |
| 2043 | return -1; |
| 2044 | } |
| 2045 | |
| 2046 | bool can_filter_bitmap(const struct list_objects_filter_options *filter) |
| 2047 | { |
| 2048 | return !filter_bitmap(NULL, NULL, NULL, filter); |
| 2049 | } |
| 2050 | |
| 2051 | static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git, |
| 2052 | struct bitmap *result) |
| 2053 | { |
| 2054 | struct eindex *eindex = &bitmap_git->ext_index; |
| 2055 | uint32_t objects_nr; |
| 2056 | size_t i, pos; |
| 2057 | |
| 2058 | objects_nr = bitmap_num_objects_total(bitmap_git); |
| 2059 | pos = objects_nr / BITS_IN_EWORD; |
| 2060 | |
| 2061 | if (pos > result->word_alloc) |
| 2062 | pos = result->word_alloc; |
| 2063 | |
| 2064 | memset(result->words, 0x00, sizeof(eword_t) * pos); |
| 2065 | for (i = pos * BITS_IN_EWORD; i < objects_nr; i++) |
| 2066 | bitmap_unset(result, i); |
| 2067 | |
| 2068 | for (i = 0; i < eindex->count; ++i) { |
| 2069 | if (has_object_pack(bitmap_repo(bitmap_git), |
| 2070 | &eindex->objects[i]->oid)) |
| 2071 | bitmap_unset(result, objects_nr + i); |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | int for_each_bitmapped_object(struct bitmap_index *bitmap_git, |
| 2076 | const struct list_objects_filter_options *filter, |
| 2077 | show_reachable_fn show_reach, |
| 2078 | void *payload) |
| 2079 | { |
| 2080 | const enum object_type types[] = { |
| 2081 | OBJ_COMMIT, |
| 2082 | OBJ_TREE, |
| 2083 | OBJ_BLOB, |
| 2084 | OBJ_TAG, |
| 2085 | }; |
| 2086 | struct bitmap *filtered_bitmap = NULL; |
| 2087 | uint32_t objects_nr; |
| 2088 | size_t full_word_count; |
| 2089 | int ret; |
| 2090 | |
| 2091 | if (!can_filter_bitmap(filter)) { |
| 2092 | ret = -1; |
| 2093 | goto out; |
| 2094 | } |
| 2095 | |
| 2096 | objects_nr = bitmap_num_objects(bitmap_git); |
| 2097 | full_word_count = objects_nr / BITS_IN_EWORD; |
| 2098 | |
| 2099 | /* We start from the all-1 bitmap and then filter down from there. */ |
| 2100 | filtered_bitmap = bitmap_word_alloc(full_word_count + !!(objects_nr % BITS_IN_EWORD)); |
| 2101 | memset(filtered_bitmap->words, 0xff, full_word_count * sizeof(*filtered_bitmap->words)); |
| 2102 | for (size_t i = full_word_count * BITS_IN_EWORD; i < objects_nr; i++) |
| 2103 | bitmap_set(filtered_bitmap, i); |
| 2104 | |
| 2105 | if (filter_bitmap(bitmap_git, NULL, filtered_bitmap, filter) < 0) { |
| 2106 | ret = -1; |
| 2107 | goto out; |
| 2108 | } |
| 2109 | |
| 2110 | for (size_t i = 0; i < ARRAY_SIZE(types); i++) { |
| 2111 | ret = show_objects_for_type(bitmap_git, filtered_bitmap, |
| 2112 | types[i], show_reach, payload); |
| 2113 | if (ret) |
| 2114 | goto out; |
| 2115 | } |
| 2116 | |
| 2117 | ret = 0; |
| 2118 | out: |
| 2119 | bitmap_free(filtered_bitmap); |
| 2120 | return ret; |
| 2121 | } |
| 2122 | |
| 2123 | struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs, |
| 2124 | int filter_provided_objects) |
| 2125 | { |
| 2126 | unsigned int i; |
| 2127 | int use_boundary_traversal; |
| 2128 | |
| 2129 | struct object_list *wants = NULL; |
| 2130 | struct object_list *haves = NULL; |
| 2131 | |
| 2132 | struct bitmap *wants_bitmap = NULL; |
| 2133 | struct bitmap *haves_bitmap = NULL; |
| 2134 | |
| 2135 | struct bitmap_index *bitmap_git; |
| 2136 | struct repository *repo; |
| 2137 | |
| 2138 | /* |
| 2139 | * We can't do pathspec limiting with bitmaps, because we don't know |
| 2140 | * which commits are associated with which object changes (let alone |
| 2141 | * even which objects are associated with which paths). |
| 2142 | */ |
| 2143 | if (revs->prune) |
| 2144 | return NULL; |
| 2145 | |
| 2146 | if (!can_filter_bitmap(&revs->filter)) |
| 2147 | return NULL; |
| 2148 | |
| 2149 | /* try to open a bitmapped pack, but don't parse it yet |
| 2150 | * because we may not need to use it */ |
| 2151 | CALLOC_ARRAY(bitmap_git, 1); |
| 2152 | if (open_bitmap(revs->repo, bitmap_git) < 0) |
| 2153 | goto cleanup; |
| 2154 | |
| 2155 | for (i = 0; i < revs->pending.nr; ++i) { |
| 2156 | struct object *object = revs->pending.objects[i].item; |
| 2157 | |
| 2158 | if (object->type == OBJ_NONE) |
| 2159 | parse_object_or_die(revs->repo, &object->oid, NULL); |
| 2160 | |
| 2161 | while (object->type == OBJ_TAG) { |
| 2162 | struct tag *tag = (struct tag *) object; |
| 2163 | |
| 2164 | if (object->flags & UNINTERESTING) |
| 2165 | object_list_insert(object, &haves); |
| 2166 | else |
| 2167 | object_list_insert(object, &wants); |
| 2168 | |
| 2169 | object = parse_object_or_die(revs->repo, get_tagged_oid(tag), NULL); |
| 2170 | object->flags |= (tag->object.flags & UNINTERESTING); |
| 2171 | } |
| 2172 | |
| 2173 | if (object->flags & UNINTERESTING) |
| 2174 | object_list_insert(object, &haves); |
| 2175 | else |
| 2176 | object_list_insert(object, &wants); |
| 2177 | } |
| 2178 | |
| 2179 | use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1); |
| 2180 | if (use_boundary_traversal < 0) { |
| 2181 | prepare_repo_settings(revs->repo); |
| 2182 | use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal; |
| 2183 | } |
| 2184 | |
| 2185 | if (!use_boundary_traversal) { |
| 2186 | /* |
| 2187 | * if we have a HAVES list, but none of those haves is contained |
| 2188 | * in the packfile that has a bitmap, we don't have anything to |
| 2189 | * optimize here |
| 2190 | */ |
| 2191 | if (haves && !in_bitmapped_pack(bitmap_git, haves)) |
| 2192 | goto cleanup; |
| 2193 | } |
| 2194 | |
| 2195 | /* if we don't want anything, we're done here */ |
| 2196 | if (!wants) |
| 2197 | goto cleanup; |
| 2198 | |
| 2199 | /* |
| 2200 | * now we're going to use bitmaps, so load the actual bitmap entries |
| 2201 | * from disk. this is the point of no return; after this the rev_list |
| 2202 | * becomes invalidated and we must perform the revwalk through bitmaps |
| 2203 | */ |
| 2204 | if (load_bitmap(revs->repo, bitmap_git, 0) < 0) |
| 2205 | goto cleanup; |
| 2206 | |
| 2207 | if (!use_boundary_traversal) |
| 2208 | object_array_clear(&revs->pending); |
| 2209 | |
| 2210 | repo = bitmap_repo(bitmap_git); |
| 2211 | |
| 2212 | if (haves) { |
| 2213 | if (use_boundary_traversal) { |
| 2214 | trace2_region_enter("pack-bitmap", "haves/boundary", repo); |
| 2215 | haves_bitmap = find_boundary_objects(bitmap_git, revs, haves); |
| 2216 | trace2_region_leave("pack-bitmap", "haves/boundary", repo); |
| 2217 | } else { |
| 2218 | trace2_region_enter("pack-bitmap", "haves/classic", repo); |
| 2219 | revs->ignore_missing_links = 1; |
| 2220 | haves_bitmap = find_objects(bitmap_git, revs, haves, NULL); |
| 2221 | reset_revision_walk(); |
| 2222 | revs->ignore_missing_links = 0; |
| 2223 | trace2_region_leave("pack-bitmap", "haves/classic", repo); |
| 2224 | } |
| 2225 | |
| 2226 | if (!haves_bitmap) |
| 2227 | BUG("failed to perform bitmap walk"); |
| 2228 | } |
| 2229 | |
| 2230 | if (use_boundary_traversal) { |
| 2231 | object_array_clear(&revs->pending); |
| 2232 | reset_revision_walk(); |
| 2233 | } |
| 2234 | |
| 2235 | wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap); |
| 2236 | |
| 2237 | if (!wants_bitmap) |
| 2238 | BUG("failed to perform bitmap walk"); |
| 2239 | |
| 2240 | if (haves_bitmap) |
| 2241 | bitmap_and_not(wants_bitmap, haves_bitmap); |
| 2242 | |
| 2243 | filter_bitmap(bitmap_git, |
| 2244 | (revs->filter.choice && filter_provided_objects) ? NULL : wants, |
| 2245 | wants_bitmap, |
| 2246 | &revs->filter); |
| 2247 | |
| 2248 | if (revs->unpacked) |
| 2249 | filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap); |
| 2250 | |
| 2251 | bitmap_git->result = wants_bitmap; |
| 2252 | bitmap_git->haves = haves_bitmap; |
| 2253 | |
| 2254 | object_list_free(&wants); |
| 2255 | object_list_free(&haves); |
| 2256 | |
| 2257 | trace2_data_intmax("bitmap", repo, "pseudo_merges_satisfied", |
| 2258 | pseudo_merges_satisfied_nr); |
| 2259 | trace2_data_intmax("bitmap", repo, "pseudo_merges_cascades", |
| 2260 | pseudo_merges_cascades_nr); |
| 2261 | trace2_data_intmax("bitmap", repo, "bitmap/hits", |
| 2262 | existing_bitmaps_hits_nr); |
| 2263 | trace2_data_intmax("bitmap", repo, "bitmap/misses", |
| 2264 | existing_bitmaps_misses_nr); |
| 2265 | trace2_data_intmax("bitmap", repo, "bitmap/roots_with_bitmap", |
| 2266 | roots_with_bitmaps_nr); |
| 2267 | trace2_data_intmax("bitmap", repo, "bitmap/roots_without_bitmap", |
| 2268 | roots_without_bitmaps_nr); |
| 2269 | |
| 2270 | return bitmap_git; |
| 2271 | |
| 2272 | cleanup: |
| 2273 | free_bitmap_index(bitmap_git); |
| 2274 | object_list_free(&wants); |
| 2275 | object_list_free(&haves); |
| 2276 | return NULL; |
| 2277 | } |
| 2278 | |
| 2279 | /* |
| 2280 | * -1 means "stop trying further objects"; 0 means we may or may not have |
| 2281 | * reused, but you can keep feeding bits. |
| 2282 | */ |
| 2283 | static int try_partial_reuse(struct bitmap_index *bitmap_git, |
| 2284 | struct bitmapped_pack *pack, |
| 2285 | size_t bitmap_pos, |
| 2286 | uint32_t pack_pos, |
| 2287 | off_t offset, |
| 2288 | struct bitmap *reuse, |
| 2289 | struct pack_window **w_curs) |
| 2290 | { |
| 2291 | off_t delta_obj_offset; |
| 2292 | enum object_type type; |
| 2293 | size_t size; |
| 2294 | |
| 2295 | if (pack_pos >= pack->p->num_objects) |
| 2296 | return -1; /* not actually in the pack */ |
| 2297 | |
| 2298 | delta_obj_offset = offset; |
| 2299 | type = unpack_object_header(pack->p, w_curs, &offset, &size); |
| 2300 | if (type < 0) |
| 2301 | return -1; /* broken packfile, punt */ |
| 2302 | |
| 2303 | if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) { |
| 2304 | off_t base_offset; |
| 2305 | uint32_t base_pos; |
| 2306 | uint32_t base_bitmap_pos; |
| 2307 | |
| 2308 | /* |
| 2309 | * Find the position of the base object so we can look it up |
| 2310 | * in our bitmaps. If we can't come up with an offset, or if |
| 2311 | * that offset is not in the revidx, the pack is corrupt. |
| 2312 | * There's nothing we can do, so just punt on this object, |
| 2313 | * and the normal slow path will complain about it in |
| 2314 | * more detail. |
| 2315 | */ |
| 2316 | base_offset = get_delta_base(pack->p, w_curs, &offset, type, |
| 2317 | delta_obj_offset); |
| 2318 | if (!base_offset) |
| 2319 | return 0; |
| 2320 | |
| 2321 | offset_to_pack_pos(pack->p, base_offset, &base_pos); |
| 2322 | |
| 2323 | if (bitmap_is_midx(bitmap_git)) { |
| 2324 | /* |
| 2325 | * Cross-pack deltas are rejected for now, but could |
| 2326 | * theoretically be supported in the future. |
| 2327 | * |
| 2328 | * We would need to ensure that we're sending both |
| 2329 | * halves of the delta/base pair, regardless of whether |
| 2330 | * or not the two cross a pack boundary. If they do, |
| 2331 | * then we must convert the delta to an REF_DELTA to |
| 2332 | * refer back to the base in the other pack. |
| 2333 | * */ |
| 2334 | if (midx_pair_to_pack_pos(bitmap_git->midx, |
| 2335 | pack->pack_int_id, |
| 2336 | base_offset, |
| 2337 | &base_bitmap_pos) < 0) { |
| 2338 | return 0; |
| 2339 | } |
| 2340 | } else { |
| 2341 | if (offset_to_pack_pos(pack->p, base_offset, |
| 2342 | &base_pos) < 0) |
| 2343 | return 0; |
| 2344 | /* |
| 2345 | * We assume delta dependencies always point backwards. |
| 2346 | * This lets us do a single pass, and is basically |
| 2347 | * always true due to the way OFS_DELTAs work. You would |
| 2348 | * not typically find REF_DELTA in a bitmapped pack, |
| 2349 | * since we only bitmap packs we write fresh, and |
| 2350 | * OFS_DELTA is the default). But let's double check to |
| 2351 | * make sure the pack wasn't written with odd |
| 2352 | * parameters. |
| 2353 | */ |
| 2354 | if (base_pos >= pack_pos) |
| 2355 | return 0; |
| 2356 | base_bitmap_pos = pack->bitmap_pos + base_pos; |
| 2357 | } |
| 2358 | |
| 2359 | /* |
| 2360 | * And finally, if we're not sending the base as part of our |
| 2361 | * reuse chunk, then don't send this object either. The base |
| 2362 | * would come after us, along with other objects not |
| 2363 | * necessarily in the pack, which means we'd need to convert |
| 2364 | * to REF_DELTA on the fly. Better to just let the normal |
| 2365 | * object_entry code path handle it. |
| 2366 | */ |
| 2367 | if (!bitmap_get(reuse, base_bitmap_pos)) |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
| 2371 | /* |
| 2372 | * If we got here, then the object is OK to reuse. Mark it. |
| 2373 | */ |
| 2374 | bitmap_set(reuse, bitmap_pos); |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
| 2378 | static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git, |
| 2379 | struct bitmapped_pack *pack, |
| 2380 | struct bitmap *reuse) |
| 2381 | { |
| 2382 | struct bitmap *result = bitmap_git->result; |
| 2383 | struct pack_window *w_curs = NULL; |
| 2384 | size_t pos = pack->bitmap_pos / BITS_IN_EWORD; |
| 2385 | |
| 2386 | if (!pack->bitmap_pos) { |
| 2387 | /* |
| 2388 | * If we're processing the first (in the case of a MIDX, the |
| 2389 | * preferred pack) or the only (in the case of single-pack |
| 2390 | * bitmaps) pack, then we can reuse whole words at a time. |
| 2391 | * |
| 2392 | * This is because we know that any deltas in this range *must* |
| 2393 | * have their bases chosen from the same pack, since: |
| 2394 | * |
| 2395 | * - In the single pack case, there is no other pack to choose |
| 2396 | * them from. |
| 2397 | * |
| 2398 | * - In the MIDX case, the first pack is the preferred pack, so |
| 2399 | * all ties are broken in favor of that pack (i.e. the one |
| 2400 | * we're currently processing). So any duplicate bases will be |
| 2401 | * resolved in favor of the pack we're processing. |
| 2402 | */ |
| 2403 | while (pos < result->word_alloc && |
| 2404 | pos < pack->bitmap_nr / BITS_IN_EWORD && |
| 2405 | result->words[pos] == (eword_t)~0) |
| 2406 | pos++; |
| 2407 | memset(reuse->words, 0xFF, pos * sizeof(eword_t)); |
| 2408 | } |
| 2409 | |
| 2410 | for (; pos < result->word_alloc; pos++) { |
| 2411 | eword_t word = result->words[pos]; |
| 2412 | size_t offset; |
| 2413 | |
| 2414 | for (offset = 0; offset < BITS_IN_EWORD; offset++) { |
| 2415 | size_t bit_pos; |
| 2416 | uint32_t pack_pos; |
| 2417 | off_t ofs; |
| 2418 | |
| 2419 | if (word >> offset == 0) |
| 2420 | break; |
| 2421 | |
| 2422 | offset += ewah_bit_ctz64(word >> offset); |
| 2423 | |
| 2424 | bit_pos = pos * BITS_IN_EWORD + offset; |
| 2425 | if (bit_pos < pack->bitmap_pos) |
| 2426 | continue; |
| 2427 | if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr) |
| 2428 | goto done; |
| 2429 | |
| 2430 | if (bitmap_is_midx(bitmap_git)) { |
| 2431 | uint32_t midx_pos; |
| 2432 | |
| 2433 | midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos); |
| 2434 | ofs = nth_midxed_offset(bitmap_git->midx, midx_pos); |
| 2435 | |
| 2436 | if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0) |
| 2437 | BUG("could not find object in pack %s " |
| 2438 | "at offset %"PRIuMAX" in MIDX", |
| 2439 | pack_basename(pack->p), (uintmax_t)ofs); |
| 2440 | } else { |
| 2441 | pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos)); |
| 2442 | if (pack_pos >= pack->p->num_objects) |
| 2443 | BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")", |
| 2444 | pack_basename(pack->p), (uintmax_t)pack_pos, |
| 2445 | pack->p->num_objects); |
| 2446 | |
| 2447 | ofs = pack_pos_to_offset(pack->p, pack_pos); |
| 2448 | } |
| 2449 | |
| 2450 | if (try_partial_reuse(bitmap_git, pack, bit_pos, |
| 2451 | pack_pos, ofs, reuse, &w_curs) < 0) { |
| 2452 | /* |
| 2453 | * try_partial_reuse indicated we couldn't reuse |
| 2454 | * any bits, so there is no point in trying more |
| 2455 | * bits in the current word, or any other words |
| 2456 | * in result. |
| 2457 | * |
| 2458 | * Jump out of both loops to avoid future |
| 2459 | * unnecessary calls to try_partial_reuse. |
| 2460 | */ |
| 2461 | goto done; |
| 2462 | } |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | done: |
| 2467 | unuse_pack(&w_curs); |
| 2468 | } |
| 2469 | |
| 2470 | static int bitmapped_pack_cmp(const void *va, const void *vb) |
| 2471 | { |
| 2472 | const struct bitmapped_pack *a = va; |
| 2473 | const struct bitmapped_pack *b = vb; |
| 2474 | |
| 2475 | if (a->bitmap_pos < b->bitmap_pos) |
| 2476 | return -1; |
| 2477 | if (a->bitmap_pos > b->bitmap_pos) |
| 2478 | return 1; |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
| 2482 | void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git, |
| 2483 | struct bitmapped_pack **packs_out, |
| 2484 | size_t *packs_nr_out, |
| 2485 | struct bitmap **reuse_out, |
| 2486 | int multi_pack_reuse) |
| 2487 | { |
| 2488 | struct repository *r = bitmap_repo(bitmap_git); |
| 2489 | struct bitmapped_pack *packs = NULL; |
| 2490 | struct bitmap *result = bitmap_git->result; |
| 2491 | struct bitmap *reuse; |
| 2492 | size_t i; |
| 2493 | size_t packs_nr = 0, packs_alloc = 0; |
| 2494 | size_t word_alloc; |
| 2495 | uint32_t objects_nr = 0; |
| 2496 | |
| 2497 | assert(result); |
| 2498 | |
| 2499 | load_reverse_index(r, bitmap_git); |
| 2500 | |
| 2501 | if (!bitmap_is_midx(bitmap_git) || !bitmap_git->midx->chunk_bitmapped_packs) |
| 2502 | multi_pack_reuse = 0; |
| 2503 | |
| 2504 | if (multi_pack_reuse) { |
| 2505 | struct multi_pack_index *m = bitmap_git->midx; |
| 2506 | for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) { |
| 2507 | struct bitmapped_pack pack; |
| 2508 | if (nth_bitmapped_pack(bitmap_git->midx, &pack, i) < 0) { |
| 2509 | warning(_("unable to load pack: '%s', disabling pack-reuse"), |
| 2510 | bitmap_git->midx->pack_names[i]); |
| 2511 | free(packs); |
| 2512 | return; |
| 2513 | } |
| 2514 | |
| 2515 | if (!pack.bitmap_nr) |
| 2516 | continue; |
| 2517 | |
| 2518 | if (is_pack_valid(pack.p)) { |
| 2519 | ALLOC_GROW(packs, packs_nr + 1, packs_alloc); |
| 2520 | memcpy(&packs[packs_nr++], &pack, sizeof(pack)); |
| 2521 | } |
| 2522 | |
| 2523 | objects_nr += pack.p->num_objects; |
| 2524 | } |
| 2525 | |
| 2526 | QSORT(packs, packs_nr, bitmapped_pack_cmp); |
| 2527 | } else { |
| 2528 | struct packed_git *pack; |
| 2529 | uint32_t pack_int_id; |
| 2530 | |
| 2531 | if (bitmap_is_midx(bitmap_git)) { |
| 2532 | struct multi_pack_index *m = bitmap_git->midx; |
| 2533 | uint32_t preferred_pack_pos; |
| 2534 | |
| 2535 | while (m->base_midx) |
| 2536 | m = m->base_midx; |
| 2537 | |
| 2538 | if (midx_preferred_pack(m, &preferred_pack_pos) < 0) { |
| 2539 | warning(_("unable to compute preferred pack, disabling pack-reuse")); |
| 2540 | return; |
| 2541 | } |
| 2542 | |
| 2543 | pack = nth_midxed_pack(m, preferred_pack_pos); |
| 2544 | pack_int_id = preferred_pack_pos; |
| 2545 | } else { |
| 2546 | pack = bitmap_git->pack; |
| 2547 | /* |
| 2548 | * Any value for 'pack_int_id' will do here. When we |
| 2549 | * process the pack via try_partial_reuse(), we won't |
| 2550 | * use the `pack_int_id` field since we have a non-MIDX |
| 2551 | * bitmap. |
| 2552 | * |
| 2553 | * Use '-1' as a sentinel value to make it clear |
| 2554 | * that we do not expect to read this field. |
| 2555 | */ |
| 2556 | pack_int_id = -1; |
| 2557 | } |
| 2558 | |
| 2559 | if (is_pack_valid(pack)) { |
| 2560 | ALLOC_GROW(packs, packs_nr + 1, packs_alloc); |
| 2561 | packs[packs_nr].p = pack; |
| 2562 | packs[packs_nr].pack_int_id = pack_int_id; |
| 2563 | packs[packs_nr].bitmap_nr = pack->num_objects; |
| 2564 | packs[packs_nr].bitmap_pos = 0; |
| 2565 | packs[packs_nr].from_midx = bitmap_git->midx; |
| 2566 | packs_nr++; |
| 2567 | } |
| 2568 | |
| 2569 | objects_nr = pack->num_objects; |
| 2570 | } |
| 2571 | |
| 2572 | if (!packs_nr) |
| 2573 | return; |
| 2574 | |
| 2575 | word_alloc = objects_nr / BITS_IN_EWORD; |
| 2576 | if (objects_nr % BITS_IN_EWORD) |
| 2577 | word_alloc++; |
| 2578 | reuse = bitmap_word_alloc(word_alloc); |
| 2579 | |
| 2580 | for (i = 0; i < packs_nr; i++) |
| 2581 | reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse); |
| 2582 | |
| 2583 | if (bitmap_is_empty(reuse)) { |
| 2584 | free(packs); |
| 2585 | bitmap_free(reuse); |
| 2586 | return; |
| 2587 | } |
| 2588 | |
| 2589 | /* |
| 2590 | * Drop any reused objects from the result, since they will not |
| 2591 | * need to be handled separately. |
| 2592 | */ |
| 2593 | bitmap_and_not(result, reuse); |
| 2594 | *packs_out = packs; |
| 2595 | *packs_nr_out = packs_nr; |
| 2596 | *reuse_out = reuse; |
| 2597 | } |
| 2598 | |
| 2599 | int bitmap_walk_contains(struct bitmap_index *bitmap_git, |
| 2600 | struct bitmap *bitmap, const struct object_id *oid) |
| 2601 | { |
| 2602 | int idx; |
| 2603 | |
| 2604 | if (!bitmap) |
| 2605 | return 0; |
| 2606 | |
| 2607 | idx = bitmap_position(bitmap_git, oid); |
| 2608 | return idx >= 0 && bitmap_get(bitmap, idx); |
| 2609 | } |
| 2610 | |
| 2611 | void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git, |
| 2612 | struct rev_info *revs, |
| 2613 | show_reachable_fn show_reachable) |
| 2614 | { |
| 2615 | assert(bitmap_git->result); |
| 2616 | |
| 2617 | show_objects_for_type(bitmap_git, bitmap_git->result, |
| 2618 | OBJ_COMMIT, show_reachable, NULL); |
| 2619 | if (revs->tree_objects) |
| 2620 | show_objects_for_type(bitmap_git, bitmap_git->result, |
| 2621 | OBJ_TREE, show_reachable, NULL); |
| 2622 | if (revs->blob_objects) |
| 2623 | show_objects_for_type(bitmap_git, bitmap_git->result, |
| 2624 | OBJ_BLOB, show_reachable, NULL); |
| 2625 | if (revs->tag_objects) |
| 2626 | show_objects_for_type(bitmap_git, bitmap_git->result, |
| 2627 | OBJ_TAG, show_reachable, NULL); |
| 2628 | |
| 2629 | show_extended_objects(bitmap_git, revs, show_reachable); |
| 2630 | } |
| 2631 | |
| 2632 | static uint32_t count_object_type(struct bitmap_index *bitmap_git, |
| 2633 | enum object_type type) |
| 2634 | { |
| 2635 | struct bitmap *objects = bitmap_git->result; |
| 2636 | struct eindex *eindex = &bitmap_git->ext_index; |
| 2637 | |
| 2638 | uint32_t i = 0, count = 0; |
| 2639 | struct ewah_or_iterator it; |
| 2640 | eword_t filter; |
| 2641 | |
| 2642 | init_type_iterator(&it, bitmap_git, type); |
| 2643 | |
| 2644 | while (i < objects->word_alloc && ewah_or_iterator_next(&filter, &it)) { |
| 2645 | eword_t word = objects->words[i++] & filter; |
| 2646 | count += ewah_bit_popcount64(word); |
| 2647 | } |
| 2648 | |
| 2649 | for (i = 0; i < eindex->count; ++i) { |
| 2650 | if (eindex->objects[i]->type == type && |
| 2651 | bitmap_get(objects, |
| 2652 | st_add(bitmap_num_objects_total(bitmap_git), i))) |
| 2653 | count++; |
| 2654 | } |
| 2655 | |
| 2656 | ewah_or_iterator_release(&it); |
| 2657 | |
| 2658 | return count; |
| 2659 | } |
| 2660 | |
| 2661 | void count_bitmap_commit_list(struct bitmap_index *bitmap_git, |
| 2662 | uint32_t *commits, uint32_t *trees, |
| 2663 | uint32_t *blobs, uint32_t *tags) |
| 2664 | { |
| 2665 | assert(bitmap_git->result); |
| 2666 | |
| 2667 | if (commits) |
| 2668 | *commits = count_object_type(bitmap_git, OBJ_COMMIT); |
| 2669 | |
| 2670 | if (trees) |
| 2671 | *trees = count_object_type(bitmap_git, OBJ_TREE); |
| 2672 | |
| 2673 | if (blobs) |
| 2674 | *blobs = count_object_type(bitmap_git, OBJ_BLOB); |
| 2675 | |
| 2676 | if (tags) |
| 2677 | *tags = count_object_type(bitmap_git, OBJ_TAG); |
| 2678 | } |
| 2679 | |
| 2680 | struct bitmap_test_data { |
| 2681 | struct bitmap_index *bitmap_git; |
| 2682 | struct bitmap *base; |
| 2683 | struct bitmap *commits; |
| 2684 | struct bitmap *trees; |
| 2685 | struct bitmap *blobs; |
| 2686 | struct bitmap *tags; |
| 2687 | struct progress *prg; |
| 2688 | size_t seen; |
| 2689 | |
| 2690 | struct bitmap_test_data *base_tdata; |
| 2691 | }; |
| 2692 | |
| 2693 | static void test_bitmap_type(struct bitmap_test_data *tdata, |
| 2694 | struct object *obj, int pos) |
| 2695 | { |
| 2696 | enum object_type bitmap_type = OBJ_NONE; |
| 2697 | int bitmaps_nr = 0; |
| 2698 | |
| 2699 | if (bitmap_is_midx(tdata->bitmap_git)) { |
| 2700 | while (pos < tdata->bitmap_git->midx->num_objects_in_base) |
| 2701 | tdata = tdata->base_tdata; |
| 2702 | } |
| 2703 | |
| 2704 | if (bitmap_get(tdata->commits, pos)) { |
| 2705 | bitmap_type = OBJ_COMMIT; |
| 2706 | bitmaps_nr++; |
| 2707 | } |
| 2708 | if (bitmap_get(tdata->trees, pos)) { |
| 2709 | bitmap_type = OBJ_TREE; |
| 2710 | bitmaps_nr++; |
| 2711 | } |
| 2712 | if (bitmap_get(tdata->blobs, pos)) { |
| 2713 | bitmap_type = OBJ_BLOB; |
| 2714 | bitmaps_nr++; |
| 2715 | } |
| 2716 | if (bitmap_get(tdata->tags, pos)) { |
| 2717 | bitmap_type = OBJ_TAG; |
| 2718 | bitmaps_nr++; |
| 2719 | } |
| 2720 | |
| 2721 | if (bitmap_type == OBJ_NONE) |
| 2722 | die(_("object '%s' not found in type bitmaps"), |
| 2723 | oid_to_hex(&obj->oid)); |
| 2724 | |
| 2725 | if (bitmaps_nr > 1) |
| 2726 | die(_("object '%s' does not have a unique type"), |
| 2727 | oid_to_hex(&obj->oid)); |
| 2728 | |
| 2729 | if (bitmap_type != obj->type) |
| 2730 | die(_("object '%s': real type '%s', expected: '%s'"), |
| 2731 | oid_to_hex(&obj->oid), |
| 2732 | type_name(obj->type), |
| 2733 | type_name(bitmap_type)); |
| 2734 | } |
| 2735 | |
| 2736 | static void test_show_object(struct object *object, |
| 2737 | const char *name UNUSED, |
| 2738 | void *data) |
| 2739 | { |
| 2740 | struct bitmap_test_data *tdata = data; |
| 2741 | int bitmap_pos; |
| 2742 | |
| 2743 | bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid); |
| 2744 | if (bitmap_pos < 0) |
| 2745 | die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid)); |
| 2746 | test_bitmap_type(tdata, object, bitmap_pos); |
| 2747 | |
| 2748 | bitmap_set(tdata->base, bitmap_pos); |
| 2749 | display_progress(tdata->prg, ++tdata->seen); |
| 2750 | } |
| 2751 | |
| 2752 | static void test_show_commit(struct commit *commit, void *data) |
| 2753 | { |
| 2754 | struct bitmap_test_data *tdata = data; |
| 2755 | int bitmap_pos; |
| 2756 | |
| 2757 | bitmap_pos = bitmap_position(tdata->bitmap_git, |
| 2758 | &commit->object.oid); |
| 2759 | if (bitmap_pos < 0) |
| 2760 | die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid)); |
| 2761 | test_bitmap_type(tdata, &commit->object, bitmap_pos); |
| 2762 | |
| 2763 | bitmap_set(tdata->base, bitmap_pos); |
| 2764 | display_progress(tdata->prg, ++tdata->seen); |
| 2765 | } |
| 2766 | |
| 2767 | static uint32_t bitmap_total_entry_count(struct bitmap_index *bitmap_git) |
| 2768 | { |
| 2769 | uint32_t total = 0; |
| 2770 | do { |
| 2771 | total = st_add(total, bitmap_git->entry_count); |
| 2772 | bitmap_git = bitmap_git->base; |
| 2773 | } while (bitmap_git); |
| 2774 | |
| 2775 | return total; |
| 2776 | } |
| 2777 | |
| 2778 | static void bitmap_test_data_prepare(struct bitmap_test_data *tdata, |
| 2779 | struct bitmap_index *bitmap_git) |
| 2780 | { |
| 2781 | memset(tdata, 0, sizeof(struct bitmap_test_data)); |
| 2782 | |
| 2783 | tdata->bitmap_git = bitmap_git; |
| 2784 | tdata->base = bitmap_new(); |
| 2785 | tdata->commits = ewah_to_bitmap(bitmap_git->commits); |
| 2786 | tdata->trees = ewah_to_bitmap(bitmap_git->trees); |
| 2787 | tdata->blobs = ewah_to_bitmap(bitmap_git->blobs); |
| 2788 | tdata->tags = ewah_to_bitmap(bitmap_git->tags); |
| 2789 | |
| 2790 | if (bitmap_git->base) { |
| 2791 | tdata->base_tdata = xmalloc(sizeof(struct bitmap_test_data)); |
| 2792 | bitmap_test_data_prepare(tdata->base_tdata, bitmap_git->base); |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | static void bitmap_test_data_release(struct bitmap_test_data *tdata) |
| 2797 | { |
| 2798 | if (!tdata) |
| 2799 | return; |
| 2800 | |
| 2801 | bitmap_test_data_release(tdata->base_tdata); |
| 2802 | free(tdata->base_tdata); |
| 2803 | |
| 2804 | bitmap_free(tdata->base); |
| 2805 | bitmap_free(tdata->commits); |
| 2806 | bitmap_free(tdata->trees); |
| 2807 | bitmap_free(tdata->blobs); |
| 2808 | bitmap_free(tdata->tags); |
| 2809 | } |
| 2810 | |
| 2811 | void test_bitmap_walk(struct rev_info *revs) |
| 2812 | { |
| 2813 | struct object *root; |
| 2814 | struct bitmap *result = NULL; |
| 2815 | size_t result_popcnt; |
| 2816 | struct bitmap_test_data tdata; |
| 2817 | struct bitmap_index *bitmap_git, *found; |
| 2818 | struct ewah_bitmap *bm; |
| 2819 | |
| 2820 | if (!(bitmap_git = prepare_bitmap_git(revs->repo))) |
| 2821 | die(_("failed to load bitmap indexes")); |
| 2822 | |
| 2823 | if (revs->pending.nr != 1) |
| 2824 | die(_("you must specify exactly one commit to test")); |
| 2825 | |
| 2826 | fprintf_ln(stderr, "Bitmap v%d test (%d entries%s, %d total)", |
| 2827 | bitmap_git->version, |
| 2828 | bitmap_git->entry_count, |
| 2829 | bitmap_git->table_lookup ? "" : " loaded", |
| 2830 | bitmap_total_entry_count(bitmap_git)); |
| 2831 | |
| 2832 | root = revs->pending.objects[0].item; |
| 2833 | bm = find_bitmap_for_commit(bitmap_git, (struct commit *)root, &found); |
| 2834 | |
| 2835 | if (bm) { |
| 2836 | fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum", |
| 2837 | oid_to_hex(&root->oid), |
| 2838 | (int)bm->bit_size, ewah_checksum(bm)); |
| 2839 | |
| 2840 | if (bitmap_is_midx(found)) |
| 2841 | fprintf_ln(stderr, "Located via MIDX '%s'.", |
| 2842 | midx_get_checksum_hex(found->midx)); |
| 2843 | else |
| 2844 | fprintf_ln(stderr, "Located via pack '%s'.", |
| 2845 | hash_to_hex_algop(found->pack->hash, |
| 2846 | revs->repo->hash_algo)); |
| 2847 | |
| 2848 | result = ewah_to_bitmap(bm); |
| 2849 | } |
| 2850 | |
| 2851 | if (!result) |
| 2852 | die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid)); |
| 2853 | |
| 2854 | revs->tag_objects = 1; |
| 2855 | revs->tree_objects = 1; |
| 2856 | revs->blob_objects = 1; |
| 2857 | |
| 2858 | result_popcnt = bitmap_popcount(result); |
| 2859 | |
| 2860 | if (prepare_revision_walk(revs)) |
| 2861 | die(_("revision walk setup failed")); |
| 2862 | |
| 2863 | bitmap_test_data_prepare(&tdata, bitmap_git); |
| 2864 | tdata.prg = start_progress(revs->repo, |
| 2865 | "Verifying bitmap entries", |
| 2866 | result_popcnt); |
| 2867 | |
| 2868 | traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata); |
| 2869 | |
| 2870 | stop_progress(&tdata.prg); |
| 2871 | |
| 2872 | if (bitmap_equals(result, tdata.base)) |
| 2873 | fprintf_ln(stderr, "OK!"); |
| 2874 | else |
| 2875 | die(_("mismatch in bitmap results")); |
| 2876 | |
| 2877 | bitmap_free(result); |
| 2878 | bitmap_test_data_release(&tdata); |
| 2879 | free_bitmap_index(bitmap_git); |
| 2880 | } |
| 2881 | |
| 2882 | int test_bitmap_commits(struct repository *r) |
| 2883 | { |
| 2884 | struct object_id oid; |
| 2885 | MAYBE_UNUSED void *value; |
| 2886 | struct bitmap_index *bitmap_git = prepare_bitmap_git(r); |
| 2887 | |
| 2888 | if (!bitmap_git) |
| 2889 | die(_("failed to load bitmap indexes")); |
| 2890 | |
| 2891 | /* |
| 2892 | * Since this function needs to print the bitmapped |
| 2893 | * commits, bypass the commit lookup table (if one exists) |
| 2894 | * by forcing the bitmap to eagerly load its entries. |
| 2895 | */ |
| 2896 | if (bitmap_git->table_lookup) { |
| 2897 | if (load_bitmap_entries_v1(bitmap_git) < 0) |
| 2898 | die(_("failed to load bitmap indexes")); |
| 2899 | } |
| 2900 | |
| 2901 | kh_foreach(bitmap_git->bitmaps, oid, value, { |
| 2902 | printf_ln("%s", oid_to_hex(&oid)); |
| 2903 | }); |
| 2904 | |
| 2905 | free_bitmap_index(bitmap_git); |
| 2906 | |
| 2907 | return 0; |
| 2908 | } |
| 2909 | |
| 2910 | int test_bitmap_commits_with_offset(struct repository *r) |
| 2911 | { |
| 2912 | struct object_id oid; |
| 2913 | struct stored_bitmap *stored; |
| 2914 | struct bitmap_index *bitmap_git; |
| 2915 | size_t commit_idx_pos_map_pos, xor_offset_map_pos, flag_map_pos, |
| 2916 | ewah_bitmap_map_pos; |
| 2917 | |
| 2918 | bitmap_git = prepare_bitmap_git(r); |
| 2919 | if (!bitmap_git) |
| 2920 | die(_("failed to load bitmap indexes")); |
| 2921 | |
| 2922 | /* |
| 2923 | * Since this function needs to know the position of each individual |
| 2924 | * bitmap, bypass the commit lookup table (if one exists) by forcing |
| 2925 | * the bitmap to eagerly load its entries. |
| 2926 | */ |
| 2927 | if (bitmap_git->table_lookup) { |
| 2928 | if (load_bitmap_entries_v1(bitmap_git) < 0) |
| 2929 | die(_("failed to load bitmap indexes")); |
| 2930 | } |
| 2931 | |
| 2932 | kh_foreach (bitmap_git->bitmaps, oid, stored, { |
| 2933 | commit_idx_pos_map_pos = stored->map_pos; |
| 2934 | xor_offset_map_pos = stored->map_pos + sizeof(uint32_t); |
| 2935 | flag_map_pos = xor_offset_map_pos + sizeof(uint8_t); |
| 2936 | ewah_bitmap_map_pos = flag_map_pos + sizeof(uint8_t); |
| 2937 | |
| 2938 | printf_ln("%s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX" %"PRIuMAX, |
| 2939 | oid_to_hex(&oid), |
| 2940 | (uintmax_t)commit_idx_pos_map_pos, |
| 2941 | (uintmax_t)xor_offset_map_pos, |
| 2942 | (uintmax_t)flag_map_pos, |
| 2943 | (uintmax_t)ewah_bitmap_map_pos); |
| 2944 | }) |
| 2945 | ; |
| 2946 | |
| 2947 | free_bitmap_index(bitmap_git); |
| 2948 | |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
| 2952 | int test_bitmap_hashes(struct repository *r) |
| 2953 | { |
| 2954 | struct bitmap_index *bitmap_git = prepare_bitmap_git(r); |
| 2955 | struct object_id oid; |
| 2956 | uint32_t i, index_pos; |
| 2957 | |
| 2958 | if (!bitmap_git || !bitmap_git->hashes) |
| 2959 | goto cleanup; |
| 2960 | |
| 2961 | for (i = 0; i < bitmap_num_objects(bitmap_git); i++) { |
| 2962 | if (bitmap_is_midx(bitmap_git)) |
| 2963 | index_pos = pack_pos_to_midx(bitmap_git->midx, i); |
| 2964 | else |
| 2965 | index_pos = pack_pos_to_index(bitmap_git->pack, i); |
| 2966 | |
| 2967 | nth_bitmap_object_oid(bitmap_git, &oid, index_pos); |
| 2968 | |
| 2969 | printf_ln("%s %"PRIu32"", |
| 2970 | oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos)); |
| 2971 | } |
| 2972 | |
| 2973 | cleanup: |
| 2974 | free_bitmap_index(bitmap_git); |
| 2975 | |
| 2976 | return 0; |
| 2977 | } |
| 2978 | |
| 2979 | static void bit_pos_to_object_id(struct bitmap_index *bitmap_git, |
| 2980 | uint32_t bit_pos, |
| 2981 | struct object_id *oid) |
| 2982 | { |
| 2983 | uint32_t index_pos; |
| 2984 | |
| 2985 | if (bitmap_is_midx(bitmap_git)) |
| 2986 | index_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos); |
| 2987 | else |
| 2988 | index_pos = pack_pos_to_index(bitmap_git->pack, bit_pos); |
| 2989 | |
| 2990 | nth_bitmap_object_oid(bitmap_git, oid, index_pos); |
| 2991 | } |
| 2992 | |
| 2993 | int test_bitmap_pseudo_merges(struct repository *r) |
| 2994 | { |
| 2995 | struct bitmap_index *bitmap_git; |
| 2996 | uint32_t i; |
| 2997 | |
| 2998 | bitmap_git = prepare_bitmap_git(r); |
| 2999 | if (!bitmap_git || !bitmap_git->pseudo_merges.nr) |
| 3000 | goto cleanup; |
| 3001 | |
| 3002 | for (i = 0; i < bitmap_git->pseudo_merges.nr; i++) { |
| 3003 | struct pseudo_merge *merge; |
| 3004 | struct ewah_bitmap *commits_bitmap, *merge_bitmap; |
| 3005 | |
| 3006 | merge = use_pseudo_merge(&bitmap_git->pseudo_merges, |
| 3007 | &bitmap_git->pseudo_merges.v[i]); |
| 3008 | commits_bitmap = merge->commits; |
| 3009 | merge_bitmap = pseudo_merge_bitmap(&bitmap_git->pseudo_merges, |
| 3010 | merge); |
| 3011 | |
| 3012 | printf("at=%"PRIuMAX", commits=%"PRIuMAX", objects=%"PRIuMAX"\n", |
| 3013 | (uintmax_t)merge->at, |
| 3014 | (uintmax_t)ewah_bitmap_popcount(commits_bitmap), |
| 3015 | (uintmax_t)ewah_bitmap_popcount(merge_bitmap)); |
| 3016 | } |
| 3017 | |
| 3018 | cleanup: |
| 3019 | free_bitmap_index(bitmap_git); |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | static void dump_ewah_object_ids(struct bitmap_index *bitmap_git, |
| 3024 | struct ewah_bitmap *bitmap) |
| 3025 | |
| 3026 | { |
| 3027 | struct ewah_iterator it; |
| 3028 | eword_t word; |
| 3029 | uint32_t pos = 0; |
| 3030 | |
| 3031 | ewah_iterator_init(&it, bitmap); |
| 3032 | |
| 3033 | while (ewah_iterator_next(&word, &it)) { |
| 3034 | struct object_id oid; |
| 3035 | uint32_t offset; |
| 3036 | |
| 3037 | for (offset = 0; offset < BITS_IN_EWORD; offset++) { |
| 3038 | if (!(word >> offset)) |
| 3039 | break; |
| 3040 | |
| 3041 | offset += ewah_bit_ctz64(word >> offset); |
| 3042 | |
| 3043 | bit_pos_to_object_id(bitmap_git, pos + offset, &oid); |
| 3044 | printf("%s\n", oid_to_hex(&oid)); |
| 3045 | } |
| 3046 | pos += BITS_IN_EWORD; |
| 3047 | } |
| 3048 | } |
| 3049 | |
| 3050 | int test_bitmap_pseudo_merge_commits(struct repository *r, uint32_t n) |
| 3051 | { |
| 3052 | struct bitmap_index *bitmap_git; |
| 3053 | struct pseudo_merge *merge; |
| 3054 | int ret = 0; |
| 3055 | |
| 3056 | bitmap_git = prepare_bitmap_git(r); |
| 3057 | if (!bitmap_git || !bitmap_git->pseudo_merges.nr) |
| 3058 | goto cleanup; |
| 3059 | |
| 3060 | if (n >= bitmap_git->pseudo_merges.nr) { |
| 3061 | ret = error(_("pseudo-merge index out of range " |
| 3062 | "(%"PRIu32" >= %"PRIuMAX")"), |
| 3063 | n, (uintmax_t)bitmap_git->pseudo_merges.nr); |
| 3064 | goto cleanup; |
| 3065 | } |
| 3066 | |
| 3067 | merge = use_pseudo_merge(&bitmap_git->pseudo_merges, |
| 3068 | &bitmap_git->pseudo_merges.v[n]); |
| 3069 | dump_ewah_object_ids(bitmap_git, merge->commits); |
| 3070 | |
| 3071 | cleanup: |
| 3072 | free_bitmap_index(bitmap_git); |
| 3073 | return ret; |
| 3074 | } |
| 3075 | |
| 3076 | int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n) |
| 3077 | { |
| 3078 | struct bitmap_index *bitmap_git; |
| 3079 | struct pseudo_merge *merge; |
| 3080 | int ret = 0; |
| 3081 | |
| 3082 | bitmap_git = prepare_bitmap_git(r); |
| 3083 | if (!bitmap_git || !bitmap_git->pseudo_merges.nr) |
| 3084 | goto cleanup; |
| 3085 | |
| 3086 | if (n >= bitmap_git->pseudo_merges.nr) { |
| 3087 | ret = error(_("pseudo-merge index out of range " |
| 3088 | "(%"PRIu32" >= %"PRIuMAX")"), |
| 3089 | n, (uintmax_t)bitmap_git->pseudo_merges.nr); |
| 3090 | goto cleanup; |
| 3091 | } |
| 3092 | |
| 3093 | merge = use_pseudo_merge(&bitmap_git->pseudo_merges, |
| 3094 | &bitmap_git->pseudo_merges.v[n]); |
| 3095 | |
| 3096 | dump_ewah_object_ids(bitmap_git, |
| 3097 | pseudo_merge_bitmap(&bitmap_git->pseudo_merges, |
| 3098 | merge)); |
| 3099 | |
| 3100 | cleanup: |
| 3101 | free_bitmap_index(bitmap_git); |
| 3102 | return ret; |
| 3103 | } |
| 3104 | |
| 3105 | int rebuild_bitmap(const uint32_t *reposition, |
| 3106 | struct ewah_bitmap *source, |
| 3107 | struct bitmap *dest) |
| 3108 | { |
| 3109 | uint32_t pos = 0; |
| 3110 | struct ewah_iterator it; |
| 3111 | eword_t word; |
| 3112 | |
| 3113 | ewah_iterator_init(&it, source); |
| 3114 | |
| 3115 | while (ewah_iterator_next(&word, &it)) { |
| 3116 | uint32_t offset, bit_pos; |
| 3117 | |
| 3118 | for (offset = 0; offset < BITS_IN_EWORD; ++offset) { |
| 3119 | if ((word >> offset) == 0) |
| 3120 | break; |
| 3121 | |
| 3122 | offset += ewah_bit_ctz64(word >> offset); |
| 3123 | |
| 3124 | bit_pos = reposition[pos + offset]; |
| 3125 | if (bit_pos > 0) |
| 3126 | bitmap_set(dest, bit_pos - 1); |
| 3127 | else /* can't reuse, we don't have the object */ |
| 3128 | return -1; |
| 3129 | } |
| 3130 | |
| 3131 | pos += BITS_IN_EWORD; |
| 3132 | } |
| 3133 | return 0; |
| 3134 | } |
| 3135 | |
| 3136 | uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git, |
| 3137 | struct packing_data *mapping) |
| 3138 | { |
| 3139 | struct repository *r = bitmap_repo(bitmap_git); |
| 3140 | uint32_t i, num_objects; |
| 3141 | uint32_t *reposition; |
| 3142 | |
| 3143 | if (!bitmap_is_midx(bitmap_git)) |
| 3144 | load_reverse_index(r, bitmap_git); |
| 3145 | else if (load_midx_revindex(bitmap_git->midx)) |
| 3146 | BUG("rebuild_existing_bitmaps: missing required rev-cache " |
| 3147 | "extension"); |
| 3148 | |
| 3149 | num_objects = bitmap_num_objects_total(bitmap_git); |
| 3150 | CALLOC_ARRAY(reposition, num_objects); |
| 3151 | |
| 3152 | for (i = 0; i < num_objects; ++i) { |
| 3153 | struct object_id oid; |
| 3154 | struct object_entry *oe; |
| 3155 | uint32_t index_pos; |
| 3156 | |
| 3157 | if (bitmap_is_midx(bitmap_git)) |
| 3158 | index_pos = pack_pos_to_midx(bitmap_git->midx, i); |
| 3159 | else |
| 3160 | index_pos = pack_pos_to_index(bitmap_git->pack, i); |
| 3161 | nth_bitmap_object_oid(bitmap_git, &oid, index_pos); |
| 3162 | oe = packlist_find(mapping, &oid); |
| 3163 | |
| 3164 | if (oe) { |
| 3165 | reposition[i] = oe_in_pack_pos(mapping, oe) + 1; |
| 3166 | if (!oe->hash) |
| 3167 | oe->hash = bitmap_name_hash(bitmap_git, index_pos); |
| 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | return reposition; |
| 3172 | } |
| 3173 | |
| 3174 | void free_bitmap_index(struct bitmap_index *b) |
| 3175 | { |
| 3176 | if (!b) |
| 3177 | return; |
| 3178 | |
| 3179 | if (b->map) |
| 3180 | munmap(b->map, b->map_size); |
| 3181 | ewah_pool_free(b->commits); |
| 3182 | ewah_pool_free(b->trees); |
| 3183 | ewah_pool_free(b->blobs); |
| 3184 | ewah_pool_free(b->tags); |
| 3185 | free(b->commits_all); |
| 3186 | free(b->trees_all); |
| 3187 | free(b->blobs_all); |
| 3188 | free(b->tags_all); |
| 3189 | if (b->bitmaps) { |
| 3190 | struct stored_bitmap *sb; |
| 3191 | kh_foreach_value(b->bitmaps, sb, { |
| 3192 | ewah_pool_free(sb->root); |
| 3193 | free(sb); |
| 3194 | }); |
| 3195 | } |
| 3196 | kh_destroy_oid_map(b->bitmaps); |
| 3197 | free(b->ext_index.objects); |
| 3198 | free(b->ext_index.hashes); |
| 3199 | kh_destroy_oid_pos(b->ext_index.positions); |
| 3200 | bitmap_free(b->result); |
| 3201 | bitmap_free(b->haves); |
| 3202 | if (bitmap_is_midx(b)) { |
| 3203 | /* |
| 3204 | * Multi-pack bitmaps need to have resources associated with |
| 3205 | * their on-disk reverse indexes unmapped so that stale .rev and |
| 3206 | * .bitmap files can be removed. |
| 3207 | * |
| 3208 | * Unlike pack-based bitmaps, multi-pack bitmaps can be read and |
| 3209 | * written in the same 'git multi-pack-index write --bitmap' |
| 3210 | * process. Close resources so they can be removed safely on |
| 3211 | * platforms like Windows. |
| 3212 | */ |
| 3213 | close_midx_revindex(b->midx); |
| 3214 | } |
| 3215 | free_pseudo_merge_map(&b->pseudo_merges); |
| 3216 | free_bitmap_index(b->base); |
| 3217 | free(b); |
| 3218 | } |
| 3219 | |
| 3220 | int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git, |
| 3221 | const struct object_id *oid) |
| 3222 | { |
| 3223 | return bitmap_git && |
| 3224 | bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid); |
| 3225 | } |
| 3226 | |
| 3227 | static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git, |
| 3228 | enum object_type object_type) |
| 3229 | { |
| 3230 | struct bitmap *result = bitmap_git->result; |
| 3231 | off_t total = 0; |
| 3232 | struct ewah_or_iterator it; |
| 3233 | eword_t filter; |
| 3234 | size_t i; |
| 3235 | |
| 3236 | init_type_iterator(&it, bitmap_git, object_type); |
| 3237 | for (i = 0; i < result->word_alloc && |
| 3238 | ewah_or_iterator_next(&filter, &it); i++) { |
| 3239 | eword_t word = result->words[i] & filter; |
| 3240 | size_t base = (i * BITS_IN_EWORD); |
| 3241 | unsigned offset; |
| 3242 | |
| 3243 | if (!word) |
| 3244 | continue; |
| 3245 | |
| 3246 | for (offset = 0; offset < BITS_IN_EWORD; offset++) { |
| 3247 | if ((word >> offset) == 0) |
| 3248 | break; |
| 3249 | |
| 3250 | offset += ewah_bit_ctz64(word >> offset); |
| 3251 | |
| 3252 | if (bitmap_is_midx(bitmap_git)) { |
| 3253 | uint32_t pack_pos; |
| 3254 | uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset); |
| 3255 | off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos); |
| 3256 | |
| 3257 | uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos); |
| 3258 | struct packed_git *pack = nth_midxed_pack(bitmap_git->midx, pack_id); |
| 3259 | |
| 3260 | if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) { |
| 3261 | struct object_id oid; |
| 3262 | nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos); |
| 3263 | |
| 3264 | die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX), |
| 3265 | oid_to_hex(&oid), |
| 3266 | pack->pack_name, |
| 3267 | (uintmax_t)offset); |
| 3268 | } |
| 3269 | |
| 3270 | total += pack_pos_to_offset(pack, pack_pos + 1) - offset; |
| 3271 | } else { |
| 3272 | size_t pos = base + offset; |
| 3273 | total += pack_pos_to_offset(bitmap_git->pack, pos + 1) - |
| 3274 | pack_pos_to_offset(bitmap_git->pack, pos); |
| 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | |
| 3279 | ewah_or_iterator_release(&it); |
| 3280 | |
| 3281 | return total; |
| 3282 | } |
| 3283 | |
| 3284 | static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git) |
| 3285 | { |
| 3286 | struct bitmap *result = bitmap_git->result; |
| 3287 | struct eindex *eindex = &bitmap_git->ext_index; |
| 3288 | off_t total = 0; |
| 3289 | struct object_info oi = OBJECT_INFO_INIT; |
| 3290 | off_t object_size; |
| 3291 | size_t i; |
| 3292 | |
| 3293 | oi.disk_sizep = &object_size; |
| 3294 | |
| 3295 | for (i = 0; i < eindex->count; i++) { |
| 3296 | struct object *obj = eindex->objects[i]; |
| 3297 | |
| 3298 | if (!bitmap_get(result, |
| 3299 | st_add(bitmap_num_objects_total(bitmap_git), |
| 3300 | i))) |
| 3301 | continue; |
| 3302 | |
| 3303 | if (odb_read_object_info_extended(bitmap_repo(bitmap_git)->objects, |
| 3304 | &obj->oid, &oi, 0) < 0) |
| 3305 | die(_("unable to get disk usage of '%s'"), |
| 3306 | oid_to_hex(&obj->oid)); |
| 3307 | |
| 3308 | total += object_size; |
| 3309 | } |
| 3310 | return total; |
| 3311 | } |
| 3312 | |
| 3313 | off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git, |
| 3314 | struct rev_info *revs) |
| 3315 | { |
| 3316 | off_t total = 0; |
| 3317 | |
| 3318 | total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT); |
| 3319 | if (revs->tree_objects) |
| 3320 | total += get_disk_usage_for_type(bitmap_git, OBJ_TREE); |
| 3321 | if (revs->blob_objects) |
| 3322 | total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB); |
| 3323 | if (revs->tag_objects) |
| 3324 | total += get_disk_usage_for_type(bitmap_git, OBJ_TAG); |
| 3325 | |
| 3326 | total += get_disk_usage_for_extended(bitmap_git); |
| 3327 | |
| 3328 | return total; |
| 3329 | } |
| 3330 | |
| 3331 | int bitmap_is_midx(struct bitmap_index *bitmap_git) |
| 3332 | { |
| 3333 | return !!bitmap_git->midx; |
| 3334 | } |
| 3335 | |
| 3336 | static const struct string_list *bitmap_preferred_tips(struct repository *r) |
| 3337 | { |
| 3338 | const struct string_list *dest; |
| 3339 | |
| 3340 | if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest)) |
| 3341 | return dest; |
| 3342 | return NULL; |
| 3343 | } |
| 3344 | |
| 3345 | void for_each_preferred_bitmap_tip(struct repository *repo, |
| 3346 | refs_for_each_cb cb, void *cb_data) |
| 3347 | { |
| 3348 | struct refs_for_each_ref_options opts = { 0 }; |
| 3349 | struct string_list_item *item; |
| 3350 | const struct string_list *preferred_tips; |
| 3351 | struct strbuf buf = STRBUF_INIT; |
| 3352 | |
| 3353 | preferred_tips = bitmap_preferred_tips(repo); |
| 3354 | if (!preferred_tips) |
| 3355 | return; |
| 3356 | |
| 3357 | for_each_string_list_item(item, preferred_tips) { |
| 3358 | opts.prefix = item->string; |
| 3359 | |
| 3360 | if (!ends_with(opts.prefix, "/")) { |
| 3361 | strbuf_reset(&buf); |
| 3362 | strbuf_addf(&buf, "%s/", opts.prefix); |
| 3363 | opts.prefix = buf.buf; |
| 3364 | } |
| 3365 | |
| 3366 | refs_for_each_ref_ext(get_main_ref_store(repo), |
| 3367 | cb, cb_data, &opts); |
| 3368 | } |
| 3369 | |
| 3370 | strbuf_release(&buf); |
| 3371 | } |
| 3372 | |
| 3373 | int bitmap_is_preferred_refname(struct repository *r, const char *refname) |
| 3374 | { |
| 3375 | const struct string_list *preferred_tips = bitmap_preferred_tips(r); |
| 3376 | struct string_list_item *item; |
| 3377 | |
| 3378 | if (!preferred_tips) |
| 3379 | return 0; |
| 3380 | |
| 3381 | for_each_string_list_item(item, preferred_tips) { |
| 3382 | if (starts_with(refname, item->string)) |
| 3383 | return 1; |
| 3384 | } |
| 3385 | |
| 3386 | return 0; |
| 3387 | } |
| 3388 | |
| 3389 | static int verify_bitmap_file(const struct git_hash_algo *algop, |
| 3390 | const char *name) |
| 3391 | { |
| 3392 | struct stat st; |
| 3393 | unsigned char *data; |
| 3394 | int fd = git_open(name); |
| 3395 | int res = 0; |
| 3396 | |
| 3397 | /* It is OK to not have the file. */ |
| 3398 | if (fd < 0 || fstat(fd, &st)) { |
| 3399 | if (fd >= 0) |
| 3400 | close(fd); |
| 3401 | return 0; |
| 3402 | } |
| 3403 | |
| 3404 | data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 3405 | close(fd); |
| 3406 | if (!hashfile_checksum_valid(algop, data, st.st_size)) |
| 3407 | res = error(_("bitmap file '%s' has invalid checksum"), |
| 3408 | name); |
| 3409 | |
| 3410 | munmap(data, st.st_size); |
| 3411 | return res; |
| 3412 | } |
| 3413 | |
| 3414 | int verify_bitmap_files(struct repository *r) |
| 3415 | { |
| 3416 | struct odb_source *source; |
| 3417 | struct packed_git *p; |
| 3418 | int res = 0; |
| 3419 | |
| 3420 | odb_prepare_alternates(r->objects); |
| 3421 | for (source = r->objects->sources; source; source = source->next) { |
| 3422 | struct odb_source_files *files = odb_source_files_downcast(source); |
| 3423 | struct multi_pack_index *m = get_multi_pack_index(files->packed); |
| 3424 | char *midx_bitmap_name; |
| 3425 | |
| 3426 | if (!m) |
| 3427 | continue; |
| 3428 | |
| 3429 | midx_bitmap_name = midx_bitmap_filename(m); |
| 3430 | res |= verify_bitmap_file(r->hash_algo, midx_bitmap_name); |
| 3431 | free(midx_bitmap_name); |
| 3432 | } |
| 3433 | |
| 3434 | repo_for_each_pack(r, p) { |
| 3435 | char *pack_bitmap_name = pack_bitmap_filename(p); |
| 3436 | res |= verify_bitmap_file(r->hash_algo, pack_bitmap_name); |
| 3437 | free(pack_bitmap_name); |
| 3438 | } |
| 3439 | |
| 3440 | return res; |
| 3441 | } |