| 1 | #include "git-compat-util.h" |
| 2 | #include "abspath.h" |
| 3 | #include "config.h" |
| 4 | #include "hex.h" |
| 5 | #include "lockfile.h" |
| 6 | #include "packfile.h" |
| 7 | #include "object-file.h" |
| 8 | #include "hash-lookup.h" |
| 9 | #include "midx.h" |
| 10 | #include "progress.h" |
| 11 | #include "trace2.h" |
| 12 | #include "run-command.h" |
| 13 | #include "chunk-format.h" |
| 14 | #include "pack-bitmap.h" |
| 15 | #include "refs.h" |
| 16 | #include "revision.h" |
| 17 | #include "list-objects.h" |
| 18 | #include "path.h" |
| 19 | #include "pack-revindex.h" |
| 20 | |
| 21 | #define PACK_EXPIRED UINT_MAX |
| 22 | #define BITMAP_POS_UNKNOWN (~((uint32_t)0)) |
| 23 | #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256) |
| 24 | #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t)) |
| 25 | #define NO_PREFERRED_PACK (~((uint32_t)0)) |
| 26 | |
| 27 | extern int midx_checksum_valid(struct multi_pack_index *m); |
| 28 | extern void clear_midx_files_ext(struct odb_source_packed *source, const char *ext, |
| 29 | const char *keep_hash); |
| 30 | extern void clear_incremental_midx_files_ext(struct odb_source_packed *source, |
| 31 | const char *ext, |
| 32 | const struct strvec *keep_hashes); |
| 33 | extern int cmp_idx_or_pack_name(const char *idx_or_pack_name, |
| 34 | const char *idx_name); |
| 35 | |
| 36 | static size_t write_midx_header(const struct git_hash_algo *hash_algo, |
| 37 | struct hashfile *f, unsigned char num_chunks, |
| 38 | uint32_t num_packs, int version) |
| 39 | { |
| 40 | if (version != MIDX_VERSION_V1 && version != MIDX_VERSION_V2) |
| 41 | BUG("unexpected MIDX version: %d", version); |
| 42 | |
| 43 | hashwrite_be32(f, MIDX_SIGNATURE); |
| 44 | hashwrite_u8(f, version); |
| 45 | hashwrite_u8(f, oid_version(hash_algo)); |
| 46 | hashwrite_u8(f, num_chunks); |
| 47 | hashwrite_u8(f, 0); /* unused */ |
| 48 | hashwrite_be32(f, num_packs); |
| 49 | |
| 50 | return MIDX_HEADER_SIZE; |
| 51 | } |
| 52 | |
| 53 | struct pack_info { |
| 54 | uint32_t orig_pack_int_id; |
| 55 | char *pack_name; |
| 56 | struct packed_git *p; |
| 57 | |
| 58 | uint32_t bitmap_pos; |
| 59 | uint32_t bitmap_nr; |
| 60 | |
| 61 | unsigned expired : 1; |
| 62 | }; |
| 63 | |
| 64 | static void fill_pack_info(struct pack_info *info, |
| 65 | struct packed_git *p, const char *pack_name, |
| 66 | uint32_t orig_pack_int_id) |
| 67 | { |
| 68 | memset(info, 0, sizeof(struct pack_info)); |
| 69 | |
| 70 | info->orig_pack_int_id = orig_pack_int_id; |
| 71 | info->pack_name = xstrdup(pack_name); |
| 72 | info->p = p; |
| 73 | info->bitmap_pos = BITMAP_POS_UNKNOWN; |
| 74 | } |
| 75 | |
| 76 | static int pack_info_compare(const void *_a, const void *_b) |
| 77 | { |
| 78 | struct pack_info *a = (struct pack_info *)_a; |
| 79 | struct pack_info *b = (struct pack_info *)_b; |
| 80 | return strcmp(a->pack_name, b->pack_name); |
| 81 | } |
| 82 | |
| 83 | static int idx_or_pack_name_cmp(const void *_va, const void *_vb) |
| 84 | { |
| 85 | const char *pack_name = _va; |
| 86 | const struct pack_info *compar = _vb; |
| 87 | |
| 88 | return cmp_idx_or_pack_name(pack_name, compar->pack_name); |
| 89 | } |
| 90 | |
| 91 | struct write_midx_context { |
| 92 | struct pack_info *info; |
| 93 | size_t nr; |
| 94 | size_t alloc; |
| 95 | struct multi_pack_index *m; |
| 96 | struct multi_pack_index *base_midx; |
| 97 | struct progress *progress; |
| 98 | unsigned pack_paths_checked; |
| 99 | |
| 100 | struct pack_midx_entry *entries; |
| 101 | size_t entries_nr; |
| 102 | |
| 103 | uint32_t *pack_perm; |
| 104 | uint32_t *pack_order; |
| 105 | unsigned large_offsets_needed:1; |
| 106 | uint32_t num_large_offsets; |
| 107 | |
| 108 | uint32_t preferred_pack_idx; |
| 109 | |
| 110 | int version; /* must be MIDX_VERSION_V1 or _V2 */ |
| 111 | |
| 112 | int incremental; |
| 113 | uint32_t num_multi_pack_indexes_before; |
| 114 | |
| 115 | struct multi_pack_index *compact_from; |
| 116 | struct multi_pack_index *compact_to; |
| 117 | int compact; |
| 118 | |
| 119 | struct string_list *to_include; |
| 120 | |
| 121 | struct repository *repo; |
| 122 | struct odb_source_packed *source; |
| 123 | }; |
| 124 | |
| 125 | static uint32_t midx_pack_perm(struct write_midx_context *ctx, |
| 126 | uint32_t orig_pack_int_id) |
| 127 | { |
| 128 | if (ctx->compact) |
| 129 | orig_pack_int_id -= ctx->compact_from->num_packs_in_base; |
| 130 | return ctx->pack_perm[orig_pack_int_id]; |
| 131 | } |
| 132 | |
| 133 | static int should_include_pack(const struct write_midx_context *ctx, |
| 134 | const char *file_name) |
| 135 | { |
| 136 | struct multi_pack_index *m = ctx->m; |
| 137 | /* |
| 138 | * When writing incrementally, ctx->m may contain layers above |
| 139 | * the selected base MIDX, which must be included in the new |
| 140 | * layer. |
| 141 | */ |
| 142 | if (ctx->incremental) |
| 143 | m = ctx->base_midx; |
| 144 | |
| 145 | /* |
| 146 | * Note that at most one of m and ctx->to_include are set, |
| 147 | * so we are testing midx_contains_pack() and |
| 148 | * string_list_has_string() independently (guarded by the |
| 149 | * appropriate NULL checks). |
| 150 | * |
| 151 | * We could support passing to_include while reusing an existing |
| 152 | * MIDX, but don't currently since the reuse process drags |
| 153 | * forward all packs from an existing MIDX (without checking |
| 154 | * whether or not they appear in the to_include list). |
| 155 | * |
| 156 | * If we added support for that, these next two conditional |
| 157 | * should be performed independently (likely checking |
| 158 | * to_include before the existing MIDX). |
| 159 | */ |
| 160 | if (m && midx_contains_pack(m, file_name)) |
| 161 | return 0; |
| 162 | else if (ctx->to_include && |
| 163 | !string_list_has_string(ctx->to_include, file_name)) |
| 164 | return 0; |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | static void add_pack_to_midx(const char *full_path, size_t full_path_len, |
| 169 | const char *file_name, void *data) |
| 170 | { |
| 171 | struct write_midx_context *ctx = data; |
| 172 | struct packed_git *p; |
| 173 | |
| 174 | if (ends_with(file_name, ".idx")) { |
| 175 | display_progress(ctx->progress, ++ctx->pack_paths_checked); |
| 176 | |
| 177 | if (!should_include_pack(ctx, file_name)) |
| 178 | return; |
| 179 | |
| 180 | ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); |
| 181 | p = add_packed_git(ctx->repo, full_path, full_path_len, 0); |
| 182 | if (!p) { |
| 183 | warning(_("failed to add packfile '%s'"), |
| 184 | full_path); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (open_pack_index(p)) { |
| 189 | warning(_("failed to open pack-index '%s'"), |
| 190 | full_path); |
| 191 | close_pack(p); |
| 192 | free(p); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | fill_pack_info(&ctx->info[ctx->nr], p, file_name, ctx->nr); |
| 197 | ctx->nr++; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | struct pack_midx_entry { |
| 202 | struct object_id oid; |
| 203 | uint32_t pack_int_id; |
| 204 | time_t pack_mtime; |
| 205 | uint64_t offset; |
| 206 | unsigned preferred : 1; |
| 207 | }; |
| 208 | |
| 209 | static int midx_oid_compare(const void *_a, const void *_b) |
| 210 | { |
| 211 | const struct pack_midx_entry *a = (const struct pack_midx_entry *)_a; |
| 212 | const struct pack_midx_entry *b = (const struct pack_midx_entry *)_b; |
| 213 | int cmp = oidcmp(&a->oid, &b->oid); |
| 214 | |
| 215 | if (cmp) |
| 216 | return cmp; |
| 217 | |
| 218 | /* Sort objects in a preferred pack first when multiple copies exist. */ |
| 219 | if (a->preferred > b->preferred) |
| 220 | return -1; |
| 221 | if (a->preferred < b->preferred) |
| 222 | return 1; |
| 223 | |
| 224 | if (a->pack_mtime > b->pack_mtime) |
| 225 | return -1; |
| 226 | else if (a->pack_mtime < b->pack_mtime) |
| 227 | return 1; |
| 228 | |
| 229 | return a->pack_int_id - b->pack_int_id; |
| 230 | } |
| 231 | |
| 232 | static int nth_midxed_pack_midx_entry(struct multi_pack_index *m, |
| 233 | struct pack_midx_entry *e, |
| 234 | uint32_t pos) |
| 235 | { |
| 236 | if (pos >= m->num_objects + m->num_objects_in_base) |
| 237 | return 1; |
| 238 | |
| 239 | nth_midxed_object_oid(&e->oid, m, pos); |
| 240 | e->pack_int_id = nth_midxed_pack_int_id(m, pos); |
| 241 | e->offset = nth_midxed_offset(m, pos); |
| 242 | |
| 243 | /* consider objects in midx to be from "old" packs */ |
| 244 | e->pack_mtime = 0; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static void fill_pack_entry(uint32_t pack_int_id, |
| 249 | struct packed_git *p, |
| 250 | uint32_t cur_object, |
| 251 | struct pack_midx_entry *entry, |
| 252 | int preferred) |
| 253 | { |
| 254 | if (nth_packed_object_id(&entry->oid, p, cur_object) < 0) |
| 255 | die(_("failed to locate object %d in packfile"), cur_object); |
| 256 | |
| 257 | entry->pack_int_id = pack_int_id; |
| 258 | entry->pack_mtime = p->mtime; |
| 259 | |
| 260 | entry->offset = nth_packed_object_offset(p, cur_object); |
| 261 | entry->preferred = !!preferred; |
| 262 | } |
| 263 | |
| 264 | struct midx_fanout { |
| 265 | struct pack_midx_entry *entries; |
| 266 | size_t nr, alloc; |
| 267 | }; |
| 268 | |
| 269 | static void midx_fanout_grow(struct midx_fanout *fanout, size_t nr) |
| 270 | { |
| 271 | if (nr < fanout->nr) |
| 272 | BUG("negative growth in midx_fanout_grow() (%"PRIuMAX" < %"PRIuMAX")", |
| 273 | (uintmax_t)nr, (uintmax_t)fanout->nr); |
| 274 | ALLOC_GROW(fanout->entries, nr, fanout->alloc); |
| 275 | } |
| 276 | |
| 277 | static void midx_fanout_sort(struct midx_fanout *fanout) |
| 278 | { |
| 279 | QSORT(fanout->entries, fanout->nr, midx_oid_compare); |
| 280 | } |
| 281 | |
| 282 | static void midx_fanout_add_midx_fanout_1(struct midx_fanout *fanout, |
| 283 | struct multi_pack_index *m, |
| 284 | uint32_t cur_fanout, |
| 285 | uint32_t preferred_pack) |
| 286 | { |
| 287 | uint32_t start = m->num_objects_in_base, end; |
| 288 | uint32_t cur_object; |
| 289 | |
| 290 | if (cur_fanout) |
| 291 | start += ntohl(m->chunk_oid_fanout[cur_fanout - 1]); |
| 292 | end = m->num_objects_in_base + ntohl(m->chunk_oid_fanout[cur_fanout]); |
| 293 | |
| 294 | for (cur_object = start; cur_object < end; cur_object++) { |
| 295 | if ((preferred_pack != NO_PREFERRED_PACK) && |
| 296 | (preferred_pack == nth_midxed_pack_int_id(m, cur_object))) { |
| 297 | /* |
| 298 | * Objects from preferred packs are added |
| 299 | * separately. |
| 300 | */ |
| 301 | continue; |
| 302 | } |
| 303 | |
| 304 | midx_fanout_grow(fanout, fanout->nr + 1); |
| 305 | nth_midxed_pack_midx_entry(m, |
| 306 | &fanout->entries[fanout->nr], |
| 307 | cur_object); |
| 308 | fanout->entries[fanout->nr].preferred = 0; |
| 309 | fanout->nr++; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout, |
| 314 | struct multi_pack_index *m, |
| 315 | uint32_t cur_fanout, |
| 316 | uint32_t preferred_pack) |
| 317 | { |
| 318 | if (m->base_midx) |
| 319 | midx_fanout_add_midx_fanout(fanout, m->base_midx, cur_fanout, |
| 320 | preferred_pack); |
| 321 | midx_fanout_add_midx_fanout_1(fanout, m, cur_fanout, preferred_pack); |
| 322 | } |
| 323 | |
| 324 | static void midx_fanout_add_pack_fanout(struct midx_fanout *fanout, |
| 325 | struct pack_info *info, |
| 326 | uint32_t cur_pack, |
| 327 | int preferred, |
| 328 | uint32_t cur_fanout) |
| 329 | { |
| 330 | struct packed_git *pack = info[cur_pack].p; |
| 331 | uint32_t start = 0, end; |
| 332 | uint32_t cur_object; |
| 333 | |
| 334 | if (cur_fanout) |
| 335 | start = get_pack_fanout(pack, cur_fanout - 1); |
| 336 | end = get_pack_fanout(pack, cur_fanout); |
| 337 | |
| 338 | for (cur_object = start; cur_object < end; cur_object++) { |
| 339 | midx_fanout_grow(fanout, fanout->nr + 1); |
| 340 | fill_pack_entry(cur_pack, |
| 341 | info[cur_pack].p, |
| 342 | cur_object, |
| 343 | &fanout->entries[fanout->nr], |
| 344 | preferred); |
| 345 | fanout->nr++; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static void midx_fanout_add(struct midx_fanout *fanout, |
| 350 | struct write_midx_context *ctx, |
| 351 | uint32_t start_pack, |
| 352 | uint32_t cur_fanout) |
| 353 | { |
| 354 | uint32_t cur_pack; |
| 355 | |
| 356 | if (ctx->m && !ctx->incremental) |
| 357 | midx_fanout_add_midx_fanout(fanout, ctx->m, cur_fanout, |
| 358 | ctx->preferred_pack_idx); |
| 359 | |
| 360 | for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++) { |
| 361 | int preferred = cur_pack == ctx->preferred_pack_idx; |
| 362 | midx_fanout_add_pack_fanout(fanout, ctx->info, cur_pack, |
| 363 | preferred, cur_fanout); |
| 364 | } |
| 365 | |
| 366 | if (ctx->preferred_pack_idx != NO_PREFERRED_PACK && |
| 367 | ctx->preferred_pack_idx < start_pack) |
| 368 | midx_fanout_add_pack_fanout(fanout, ctx->info, |
| 369 | ctx->preferred_pack_idx, 1, |
| 370 | cur_fanout); |
| 371 | } |
| 372 | |
| 373 | static void midx_fanout_add_compact(struct midx_fanout *fanout, |
| 374 | struct write_midx_context *ctx, |
| 375 | uint32_t cur_fanout) |
| 376 | { |
| 377 | struct multi_pack_index *m = ctx->compact_to; |
| 378 | |
| 379 | ASSERT(ctx->compact); |
| 380 | |
| 381 | while (m && m != ctx->compact_from->base_midx) { |
| 382 | midx_fanout_add_midx_fanout_1(fanout, m, cur_fanout, |
| 383 | NO_PREFERRED_PACK); |
| 384 | m = m->base_midx; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * It is possible to artificially get into a state where there are many |
| 390 | * duplicate copies of objects. That can create high memory pressure if |
| 391 | * we are to create a list of all objects before de-duplication. To reduce |
| 392 | * this memory pressure without a significant performance drop, automatically |
| 393 | * group objects by the first byte of their object id. Use the IDX fanout |
| 394 | * tables to group the data, copy to a local array, then sort. |
| 395 | * |
| 396 | * Copy only the de-duplicated entries (selected by most-recent modified time |
| 397 | * of a packfile containing the object). |
| 398 | */ |
| 399 | static void compute_sorted_entries(struct write_midx_context *ctx, |
| 400 | uint32_t start_pack) |
| 401 | { |
| 402 | uint32_t cur_fanout, cur_pack, cur_object; |
| 403 | size_t alloc_objects, total_objects = 0; |
| 404 | struct midx_fanout fanout = { 0 }; |
| 405 | |
| 406 | if (ctx->compact) |
| 407 | ASSERT(!start_pack); |
| 408 | |
| 409 | for (cur_pack = start_pack; cur_pack < ctx->nr; cur_pack++) |
| 410 | total_objects = st_add(total_objects, |
| 411 | ctx->info[cur_pack].p->num_objects); |
| 412 | |
| 413 | /* |
| 414 | * As we de-duplicate by fanout value, we expect the fanout |
| 415 | * slices to be evenly distributed, with some noise. Hence, |
| 416 | * allocate slightly more than one 256th. |
| 417 | */ |
| 418 | alloc_objects = fanout.alloc = total_objects > 3200 ? total_objects / 200 : 16; |
| 419 | |
| 420 | ALLOC_ARRAY(fanout.entries, fanout.alloc); |
| 421 | ALLOC_ARRAY(ctx->entries, alloc_objects); |
| 422 | ctx->entries_nr = 0; |
| 423 | |
| 424 | for (cur_fanout = 0; cur_fanout < 256; cur_fanout++) { |
| 425 | fanout.nr = 0; |
| 426 | |
| 427 | if (ctx->compact) |
| 428 | midx_fanout_add_compact(&fanout, ctx, cur_fanout); |
| 429 | else |
| 430 | midx_fanout_add(&fanout, ctx, start_pack, cur_fanout); |
| 431 | midx_fanout_sort(&fanout); |
| 432 | |
| 433 | /* |
| 434 | * The batch is now sorted by OID and then mtime (descending). |
| 435 | * Take only the first duplicate. |
| 436 | */ |
| 437 | for (cur_object = 0; cur_object < fanout.nr; cur_object++) { |
| 438 | if (cur_object && oideq(&fanout.entries[cur_object - 1].oid, |
| 439 | &fanout.entries[cur_object].oid)) |
| 440 | continue; |
| 441 | if (ctx->incremental && ctx->base_midx && |
| 442 | midx_has_oid(ctx->base_midx, |
| 443 | &fanout.entries[cur_object].oid)) |
| 444 | continue; |
| 445 | |
| 446 | ALLOC_GROW(ctx->entries, st_add(ctx->entries_nr, 1), |
| 447 | alloc_objects); |
| 448 | memcpy(&ctx->entries[ctx->entries_nr], |
| 449 | &fanout.entries[cur_object], |
| 450 | sizeof(struct pack_midx_entry)); |
| 451 | ctx->entries_nr++; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | free(fanout.entries); |
| 456 | } |
| 457 | |
| 458 | static int write_midx_pack_names(struct hashfile *f, void *data) |
| 459 | { |
| 460 | struct write_midx_context *ctx = data; |
| 461 | uint32_t i; |
| 462 | unsigned char padding[MIDX_CHUNK_ALIGNMENT]; |
| 463 | size_t written = 0; |
| 464 | |
| 465 | for (i = 0; i < ctx->nr; i++) { |
| 466 | size_t writelen; |
| 467 | |
| 468 | if (ctx->info[i].expired) |
| 469 | continue; |
| 470 | |
| 471 | if (ctx->version == MIDX_VERSION_V1 && |
| 472 | i && strcmp(ctx->info[i].pack_name, |
| 473 | ctx->info[i - 1].pack_name) <= 0) |
| 474 | BUG("incorrect pack-file order: %s before %s", |
| 475 | ctx->info[i - 1].pack_name, |
| 476 | ctx->info[i].pack_name); |
| 477 | |
| 478 | writelen = strlen(ctx->info[i].pack_name) + 1; |
| 479 | hashwrite(f, ctx->info[i].pack_name, writelen); |
| 480 | written += writelen; |
| 481 | } |
| 482 | |
| 483 | /* add padding to be aligned */ |
| 484 | i = MIDX_CHUNK_ALIGNMENT - (written % MIDX_CHUNK_ALIGNMENT); |
| 485 | if (i < MIDX_CHUNK_ALIGNMENT) { |
| 486 | memset(padding, 0, sizeof(padding)); |
| 487 | hashwrite(f, padding, i); |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static int write_midx_bitmapped_packs(struct hashfile *f, void *data) |
| 494 | { |
| 495 | struct write_midx_context *ctx = data; |
| 496 | size_t i; |
| 497 | |
| 498 | for (i = 0; i < ctx->nr; i++) { |
| 499 | struct pack_info *pack = &ctx->info[i]; |
| 500 | if (pack->expired) |
| 501 | continue; |
| 502 | |
| 503 | if (pack->bitmap_pos == BITMAP_POS_UNKNOWN && pack->bitmap_nr) |
| 504 | BUG("pack '%s' has no bitmap position, but has %d bitmapped object(s)", |
| 505 | pack->pack_name, pack->bitmap_nr); |
| 506 | |
| 507 | hashwrite_be32(f, pack->bitmap_pos); |
| 508 | hashwrite_be32(f, pack->bitmap_nr); |
| 509 | } |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int write_midx_oid_fanout(struct hashfile *f, |
| 514 | void *data) |
| 515 | { |
| 516 | struct write_midx_context *ctx = data; |
| 517 | struct pack_midx_entry *list = ctx->entries; |
| 518 | struct pack_midx_entry *last = ctx->entries + ctx->entries_nr; |
| 519 | uint32_t count = 0; |
| 520 | uint32_t i; |
| 521 | |
| 522 | /* |
| 523 | * Write the first-level table (the list is sorted, |
| 524 | * but we use a 256-entry lookup to be able to avoid |
| 525 | * having to do eight extra binary search iterations). |
| 526 | */ |
| 527 | for (i = 0; i < 256; i++) { |
| 528 | struct pack_midx_entry *next = list; |
| 529 | |
| 530 | while (next < last && next->oid.hash[0] == i) { |
| 531 | count++; |
| 532 | next++; |
| 533 | } |
| 534 | |
| 535 | hashwrite_be32(f, count); |
| 536 | list = next; |
| 537 | } |
| 538 | |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | static int write_midx_oid_lookup(struct hashfile *f, |
| 543 | void *data) |
| 544 | { |
| 545 | struct write_midx_context *ctx = data; |
| 546 | unsigned char hash_len = ctx->repo->hash_algo->rawsz; |
| 547 | struct pack_midx_entry *list = ctx->entries; |
| 548 | uint32_t i; |
| 549 | |
| 550 | for (i = 0; i < ctx->entries_nr; i++) { |
| 551 | struct pack_midx_entry *obj = list++; |
| 552 | |
| 553 | if (i < ctx->entries_nr - 1) { |
| 554 | struct pack_midx_entry *next = list; |
| 555 | if (oidcmp(&obj->oid, &next->oid) >= 0) |
| 556 | BUG("OIDs not in order: %s >= %s", |
| 557 | oid_to_hex(&obj->oid), |
| 558 | oid_to_hex(&next->oid)); |
| 559 | } |
| 560 | |
| 561 | hashwrite(f, obj->oid.hash, (int)hash_len); |
| 562 | } |
| 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static int write_midx_object_offsets(struct hashfile *f, |
| 568 | void *data) |
| 569 | { |
| 570 | struct write_midx_context *ctx = data; |
| 571 | struct pack_midx_entry *list = ctx->entries; |
| 572 | uint32_t i, nr_large_offset = 0; |
| 573 | |
| 574 | for (i = 0; i < ctx->entries_nr; i++) { |
| 575 | struct pack_midx_entry *obj = list++; |
| 576 | |
| 577 | if (midx_pack_perm(ctx, obj->pack_int_id) == PACK_EXPIRED) |
| 578 | BUG("object %s is in an expired pack with int-id %d", |
| 579 | oid_to_hex(&obj->oid), |
| 580 | obj->pack_int_id); |
| 581 | |
| 582 | hashwrite_be32(f, midx_pack_perm(ctx, obj->pack_int_id)); |
| 583 | |
| 584 | if (ctx->large_offsets_needed && obj->offset >> 31) |
| 585 | hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++); |
| 586 | else if (!ctx->large_offsets_needed && obj->offset >> 32) |
| 587 | BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!", |
| 588 | oid_to_hex(&obj->oid), |
| 589 | obj->offset); |
| 590 | else |
| 591 | hashwrite_be32(f, (uint32_t)obj->offset); |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static int write_midx_large_offsets(struct hashfile *f, |
| 598 | void *data) |
| 599 | { |
| 600 | struct write_midx_context *ctx = data; |
| 601 | struct pack_midx_entry *list = ctx->entries; |
| 602 | struct pack_midx_entry *end = ctx->entries + ctx->entries_nr; |
| 603 | uint32_t nr_large_offset = ctx->num_large_offsets; |
| 604 | |
| 605 | while (nr_large_offset) { |
| 606 | struct pack_midx_entry *obj; |
| 607 | uint64_t offset; |
| 608 | |
| 609 | if (list >= end) |
| 610 | BUG("too many large-offset objects"); |
| 611 | |
| 612 | obj = list++; |
| 613 | offset = obj->offset; |
| 614 | |
| 615 | if (!(offset >> 31)) |
| 616 | continue; |
| 617 | |
| 618 | hashwrite_be64(f, offset); |
| 619 | |
| 620 | nr_large_offset--; |
| 621 | } |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static int write_midx_revindex(struct hashfile *f, |
| 627 | void *data) |
| 628 | { |
| 629 | struct write_midx_context *ctx = data; |
| 630 | uint32_t i, nr_base; |
| 631 | |
| 632 | if (ctx->incremental && ctx->base_midx) |
| 633 | nr_base = ctx->base_midx->num_objects + |
| 634 | ctx->base_midx->num_objects_in_base; |
| 635 | else |
| 636 | nr_base = 0; |
| 637 | |
| 638 | for (i = 0; i < ctx->entries_nr; i++) |
| 639 | hashwrite_be32(f, ctx->pack_order[i] + nr_base); |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | struct midx_pack_order_data { |
| 645 | uint32_t nr; |
| 646 | uint32_t pack; |
| 647 | off_t offset; |
| 648 | }; |
| 649 | |
| 650 | static int midx_pack_order_cmp(const void *va, const void *vb) |
| 651 | { |
| 652 | const struct midx_pack_order_data *a = va, *b = vb; |
| 653 | if (a->pack < b->pack) |
| 654 | return -1; |
| 655 | else if (a->pack > b->pack) |
| 656 | return 1; |
| 657 | else if (a->offset < b->offset) |
| 658 | return -1; |
| 659 | else if (a->offset > b->offset) |
| 660 | return 1; |
| 661 | else |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static uint32_t *midx_pack_order(struct write_midx_context *ctx) |
| 666 | { |
| 667 | struct midx_pack_order_data *data; |
| 668 | uint32_t *pack_order, base_objects = 0; |
| 669 | uint32_t i; |
| 670 | |
| 671 | trace2_region_enter("midx", "midx_pack_order", ctx->repo); |
| 672 | |
| 673 | if (ctx->incremental && ctx->base_midx) |
| 674 | base_objects = ctx->base_midx->num_objects + |
| 675 | ctx->base_midx->num_objects_in_base; |
| 676 | |
| 677 | ALLOC_ARRAY(pack_order, ctx->entries_nr); |
| 678 | ALLOC_ARRAY(data, ctx->entries_nr); |
| 679 | |
| 680 | for (i = 0; i < ctx->entries_nr; i++) { |
| 681 | struct pack_midx_entry *e = &ctx->entries[i]; |
| 682 | data[i].nr = i; |
| 683 | data[i].pack = midx_pack_perm(ctx, e->pack_int_id); |
| 684 | if (!e->preferred || ctx->compact) |
| 685 | data[i].pack |= (1U << 31); |
| 686 | data[i].offset = e->offset; |
| 687 | } |
| 688 | |
| 689 | QSORT(data, ctx->entries_nr, midx_pack_order_cmp); |
| 690 | |
| 691 | for (i = 0; i < ctx->entries_nr; i++) { |
| 692 | struct pack_midx_entry *e = &ctx->entries[data[i].nr]; |
| 693 | struct pack_info *pack = &ctx->info[midx_pack_perm(ctx, e->pack_int_id)]; |
| 694 | if (pack->bitmap_pos == BITMAP_POS_UNKNOWN) |
| 695 | pack->bitmap_pos = i + base_objects; |
| 696 | pack->bitmap_nr++; |
| 697 | pack_order[i] = data[i].nr; |
| 698 | } |
| 699 | for (i = 0; i < ctx->nr; i++) { |
| 700 | struct pack_info *pack = &ctx->info[i]; |
| 701 | if (pack->bitmap_pos == BITMAP_POS_UNKNOWN) |
| 702 | pack->bitmap_pos = 0; |
| 703 | } |
| 704 | free(data); |
| 705 | |
| 706 | trace2_region_leave("midx", "midx_pack_order", ctx->repo); |
| 707 | |
| 708 | return pack_order; |
| 709 | } |
| 710 | |
| 711 | static void write_midx_reverse_index(struct write_midx_context *ctx, |
| 712 | unsigned char *midx_hash) |
| 713 | { |
| 714 | struct strbuf buf = STRBUF_INIT; |
| 715 | char *tmp_file; |
| 716 | |
| 717 | trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo); |
| 718 | |
| 719 | if (ctx->incremental) |
| 720 | get_split_midx_filename_ext(ctx->source, &buf, |
| 721 | midx_hash, MIDX_EXT_REV); |
| 722 | else |
| 723 | get_midx_filename_ext(ctx->source, &buf, |
| 724 | midx_hash, MIDX_EXT_REV); |
| 725 | |
| 726 | tmp_file = write_rev_file_order(ctx->repo, NULL, ctx->pack_order, |
| 727 | ctx->entries_nr, midx_hash, WRITE_REV); |
| 728 | |
| 729 | if (finalize_object_file(ctx->repo, tmp_file, buf.buf)) |
| 730 | die(_("cannot store reverse index file")); |
| 731 | |
| 732 | strbuf_release(&buf); |
| 733 | free(tmp_file); |
| 734 | |
| 735 | trace2_region_leave("midx", "write_midx_reverse_index", ctx->repo); |
| 736 | } |
| 737 | |
| 738 | static void prepare_midx_packing_data(struct packing_data *pdata, |
| 739 | struct write_midx_context *ctx) |
| 740 | { |
| 741 | uint32_t i; |
| 742 | |
| 743 | trace2_region_enter("midx", "prepare_midx_packing_data", ctx->repo); |
| 744 | |
| 745 | memset(pdata, 0, sizeof(struct packing_data)); |
| 746 | prepare_packing_data(ctx->repo, pdata); |
| 747 | |
| 748 | for (i = 0; i < ctx->entries_nr; i++) { |
| 749 | uint32_t pos = ctx->pack_order[i]; |
| 750 | struct pack_midx_entry *from = &ctx->entries[pos]; |
| 751 | struct object_entry *to = packlist_alloc(pdata, &from->oid); |
| 752 | |
| 753 | oe_set_in_pack(pdata, to, |
| 754 | ctx->info[midx_pack_perm(ctx, from->pack_int_id)].p); |
| 755 | } |
| 756 | |
| 757 | trace2_region_leave("midx", "prepare_midx_packing_data", ctx->repo); |
| 758 | } |
| 759 | |
| 760 | static int add_ref_to_pending(const struct reference *ref, void *cb_data) |
| 761 | { |
| 762 | struct rev_info *revs = (struct rev_info*)cb_data; |
| 763 | const struct object_id *maybe_peeled = ref->oid; |
| 764 | struct object_id peeled; |
| 765 | struct object *object; |
| 766 | |
| 767 | if ((ref->flags & REF_ISSYMREF) && (ref->flags & REF_ISBROKEN)) { |
| 768 | warning("symbolic ref is dangling: %s", ref->name); |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | if (!reference_get_peeled_oid(revs->repo, ref, &peeled)) |
| 773 | maybe_peeled = &peeled; |
| 774 | |
| 775 | object = parse_object_or_die(revs->repo, maybe_peeled, ref->name); |
| 776 | if (object->type != OBJ_COMMIT) |
| 777 | return 0; |
| 778 | |
| 779 | add_pending_object(revs, object, ""); |
| 780 | if (bitmap_is_preferred_refname(revs->repo, ref->name)) |
| 781 | object->flags |= NEEDS_BITMAP; |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | struct bitmap_commit_cb { |
| 786 | struct commit_stack *commits; |
| 787 | struct write_midx_context *ctx; |
| 788 | }; |
| 789 | |
| 790 | static const struct object_id *bitmap_oid_access(size_t index, |
| 791 | const void *_entries) |
| 792 | { |
| 793 | const struct pack_midx_entry *entries = _entries; |
| 794 | return &entries[index].oid; |
| 795 | } |
| 796 | |
| 797 | static void bitmap_show_commit(struct commit *commit, void *_data) |
| 798 | { |
| 799 | struct bitmap_commit_cb *data = _data; |
| 800 | int pos = oid_pos(&commit->object.oid, data->ctx->entries, |
| 801 | data->ctx->entries_nr, |
| 802 | bitmap_oid_access); |
| 803 | if (pos < 0) |
| 804 | return; |
| 805 | |
| 806 | commit_stack_push(data->commits, commit); |
| 807 | } |
| 808 | |
| 809 | static int read_refs_snapshot(const char *refs_snapshot, |
| 810 | struct rev_info *revs) |
| 811 | { |
| 812 | struct strbuf buf = STRBUF_INIT; |
| 813 | struct object_id oid; |
| 814 | FILE *f = xfopen(refs_snapshot, "r"); |
| 815 | |
| 816 | while (strbuf_getline(&buf, f) != EOF) { |
| 817 | struct object *object; |
| 818 | int preferred = 0; |
| 819 | char *hex = buf.buf; |
| 820 | const char *end = NULL; |
| 821 | |
| 822 | if (buf.len && *buf.buf == '+') { |
| 823 | preferred = 1; |
| 824 | hex = &buf.buf[1]; |
| 825 | } |
| 826 | |
| 827 | if (parse_oid_hex_algop(hex, &oid, &end, revs->repo->hash_algo) < 0) |
| 828 | die(_("could not parse line: %s"), buf.buf); |
| 829 | if (*end) |
| 830 | die(_("malformed line: %s"), buf.buf); |
| 831 | |
| 832 | object = parse_object_or_die(revs->repo, &oid, NULL); |
| 833 | if (preferred) |
| 834 | object->flags |= NEEDS_BITMAP; |
| 835 | |
| 836 | add_pending_object(revs, object, ""); |
| 837 | } |
| 838 | |
| 839 | fclose(f); |
| 840 | strbuf_release(&buf); |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | static void find_commits_for_midx_bitmap(struct commit_stack *commits, |
| 845 | const char *refs_snapshot, |
| 846 | struct write_midx_context *ctx) |
| 847 | { |
| 848 | struct rev_info revs; |
| 849 | struct bitmap_commit_cb cb = { .commits = commits, .ctx = ctx }; |
| 850 | |
| 851 | trace2_region_enter("midx", "find_commits_for_midx_bitmap", ctx->repo); |
| 852 | |
| 853 | repo_init_revisions(ctx->repo, &revs, NULL); |
| 854 | if (refs_snapshot) { |
| 855 | read_refs_snapshot(refs_snapshot, &revs); |
| 856 | } else { |
| 857 | setup_revisions(0, NULL, &revs, NULL); |
| 858 | refs_for_each_ref(get_main_ref_store(ctx->repo), |
| 859 | add_ref_to_pending, &revs); |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * Skipping promisor objects here is intentional, since it only excludes |
| 864 | * them from the list of reachable commits that we want to select from |
| 865 | * when computing the selection of MIDX'd commits to receive bitmaps. |
| 866 | * |
| 867 | * Reachability bitmaps do require that their objects be closed under |
| 868 | * reachability, but fetching any objects missing from promisors at this |
| 869 | * point is too late. But, if one of those objects can be reached from |
| 870 | * an another object that is included in the bitmap, then we will |
| 871 | * complain later that we don't have reachability closure (and fail |
| 872 | * appropriately). |
| 873 | */ |
| 874 | fetch_if_missing = 0; |
| 875 | revs.exclude_promisor_objects = 1; |
| 876 | |
| 877 | if (prepare_revision_walk(&revs)) |
| 878 | die(_("revision walk setup failed")); |
| 879 | |
| 880 | traverse_commit_list(&revs, bitmap_show_commit, NULL, &cb); |
| 881 | |
| 882 | release_revisions(&revs); |
| 883 | |
| 884 | trace2_region_leave("midx", "find_commits_for_midx_bitmap", ctx->repo); |
| 885 | } |
| 886 | |
| 887 | static int write_midx_bitmap(struct write_midx_context *ctx, |
| 888 | const unsigned char *midx_hash, |
| 889 | struct packing_data *pdata, |
| 890 | struct commit **commits, |
| 891 | uint32_t commits_nr, |
| 892 | unsigned flags) |
| 893 | { |
| 894 | int ret; |
| 895 | uint16_t options = 0; |
| 896 | struct bitmap_writer writer; |
| 897 | struct pack_idx_entry **index; |
| 898 | struct strbuf bitmap_name = STRBUF_INIT; |
| 899 | |
| 900 | trace2_region_enter("midx", "write_midx_bitmap", ctx->repo); |
| 901 | |
| 902 | if (ctx->incremental) |
| 903 | get_split_midx_filename_ext(ctx->source, &bitmap_name, |
| 904 | midx_hash, MIDX_EXT_BITMAP); |
| 905 | else |
| 906 | get_midx_filename_ext(ctx->source, &bitmap_name, |
| 907 | midx_hash, MIDX_EXT_BITMAP); |
| 908 | |
| 909 | if (flags & MIDX_WRITE_BITMAP_HASH_CACHE) |
| 910 | options |= BITMAP_OPT_HASH_CACHE; |
| 911 | |
| 912 | if (flags & MIDX_WRITE_BITMAP_LOOKUP_TABLE) |
| 913 | options |= BITMAP_OPT_LOOKUP_TABLE; |
| 914 | |
| 915 | /* |
| 916 | * Build the MIDX-order index based on pdata.objects (which is already |
| 917 | * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of |
| 918 | * this order). |
| 919 | */ |
| 920 | ALLOC_ARRAY(index, pdata->nr_objects); |
| 921 | for (uint32_t i = 0; i < pdata->nr_objects; i++) |
| 922 | index[i] = &pdata->objects[i].idx; |
| 923 | |
| 924 | bitmap_writer_init(&writer, ctx->repo, pdata, |
| 925 | ctx->incremental ? ctx->base_midx : NULL); |
| 926 | bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS); |
| 927 | bitmap_writer_build_type_index(&writer, index); |
| 928 | |
| 929 | /* |
| 930 | * bitmap_writer_finish expects objects in lex order, but pack_order |
| 931 | * gives us exactly that. use it directly instead of re-sorting the |
| 932 | * array. |
| 933 | * |
| 934 | * This changes the order of objects in 'index' between |
| 935 | * bitmap_writer_build_type_index and bitmap_writer_finish. |
| 936 | * |
| 937 | * The same re-ordering takes place in the single-pack bitmap code via |
| 938 | * write_idx_file(), which is called by finish_tmp_packfile(), which |
| 939 | * happens between bitmap_writer_build_type_index() and |
| 940 | * bitmap_writer_finish(). |
| 941 | */ |
| 942 | for (uint32_t i = 0; i < pdata->nr_objects; i++) |
| 943 | index[ctx->pack_order[i]] = &pdata->objects[i].idx; |
| 944 | |
| 945 | bitmap_writer_select_commits(&writer, commits, commits_nr); |
| 946 | ret = bitmap_writer_build(&writer); |
| 947 | if (ret < 0) |
| 948 | goto cleanup; |
| 949 | |
| 950 | bitmap_writer_set_checksum(&writer, midx_hash); |
| 951 | bitmap_writer_finish(&writer, index, bitmap_name.buf, options); |
| 952 | |
| 953 | cleanup: |
| 954 | free(index); |
| 955 | strbuf_release(&bitmap_name); |
| 956 | bitmap_writer_free(&writer); |
| 957 | |
| 958 | trace2_region_leave("midx", "write_midx_bitmap", ctx->repo); |
| 959 | |
| 960 | return ret; |
| 961 | } |
| 962 | |
| 963 | static int fill_pack_from_midx(struct pack_info *info, |
| 964 | struct multi_pack_index *m, |
| 965 | uint32_t pack_int_id) |
| 966 | { |
| 967 | if (prepare_midx_pack(m, pack_int_id)) |
| 968 | return error(_("could not load pack %d"), pack_int_id); |
| 969 | |
| 970 | fill_pack_info(info, |
| 971 | m->packs[pack_int_id - m->num_packs_in_base], |
| 972 | m->pack_names[pack_int_id - m->num_packs_in_base], |
| 973 | pack_int_id); |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static int fill_packs_from_midx(struct write_midx_context *ctx) |
| 979 | { |
| 980 | struct multi_pack_index *m; |
| 981 | |
| 982 | for (m = ctx->m; m; m = m->base_midx) { |
| 983 | uint32_t i; |
| 984 | |
| 985 | for (i = m->num_packs_in_base; |
| 986 | i < m->num_packs_in_base + m->num_packs; i++) { |
| 987 | ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); |
| 988 | |
| 989 | if (fill_pack_from_midx(&ctx->info[ctx->nr], m, i) < 0) |
| 990 | return -1; |
| 991 | |
| 992 | ctx->nr++; |
| 993 | } |
| 994 | } |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | static uint32_t compactible_packs_between(const struct multi_pack_index *from, |
| 999 | const struct multi_pack_index *to) |
| 1000 | { |
| 1001 | uint32_t nr; |
| 1002 | |
| 1003 | ASSERT(from && to); |
| 1004 | |
| 1005 | if (unsigned_add_overflows(to->num_packs, to->num_packs_in_base)) |
| 1006 | die(_("too many packs, unable to compact")); |
| 1007 | |
| 1008 | nr = to->num_packs + to->num_packs_in_base; |
| 1009 | if (nr < from->num_packs_in_base) |
| 1010 | BUG("unexpected number of packs in base during compaction: " |
| 1011 | "%"PRIu32" < %"PRIu32, nr, from->num_packs_in_base); |
| 1012 | |
| 1013 | return nr - from->num_packs_in_base; |
| 1014 | } |
| 1015 | |
| 1016 | static int fill_packs_from_midx_range(struct write_midx_context *ctx, |
| 1017 | int bitmap_order) |
| 1018 | { |
| 1019 | struct multi_pack_index *m = ctx->compact_to; |
| 1020 | uint32_t packs_nr; |
| 1021 | |
| 1022 | ASSERT(ctx->compact && !ctx->nr); |
| 1023 | ASSERT(ctx->compact_from); |
| 1024 | ASSERT(ctx->compact_to); |
| 1025 | |
| 1026 | packs_nr = compactible_packs_between(ctx->compact_from, |
| 1027 | ctx->compact_to); |
| 1028 | |
| 1029 | ALLOC_GROW(ctx->info, packs_nr, ctx->alloc); |
| 1030 | |
| 1031 | while (m != ctx->compact_from->base_midx) { |
| 1032 | uint32_t pack_int_id, preferred_pack_id; |
| 1033 | uint32_t i; |
| 1034 | |
| 1035 | if (bitmap_order) { |
| 1036 | if (midx_preferred_pack(m, &preferred_pack_id) < 0) |
| 1037 | die(_("could not determine preferred pack")); |
| 1038 | } else { |
| 1039 | preferred_pack_id = m->num_packs_in_base; |
| 1040 | } |
| 1041 | |
| 1042 | pack_int_id = m->num_packs_in_base - ctx->compact_from->num_packs_in_base; |
| 1043 | |
| 1044 | if (fill_pack_from_midx(&ctx->info[pack_int_id++], m, |
| 1045 | preferred_pack_id) < 0) |
| 1046 | return -1; |
| 1047 | |
| 1048 | for (i = m->num_packs_in_base; |
| 1049 | i < m->num_packs_in_base + m->num_packs; i++) { |
| 1050 | if (preferred_pack_id == i) |
| 1051 | continue; |
| 1052 | |
| 1053 | if (fill_pack_from_midx(&ctx->info[pack_int_id++], m, |
| 1054 | i) < 0) |
| 1055 | return -1; |
| 1056 | } |
| 1057 | |
| 1058 | ctx->nr += m->num_packs; |
| 1059 | m = m->base_midx; |
| 1060 | } |
| 1061 | |
| 1062 | ASSERT(ctx->nr == packs_nr); |
| 1063 | |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | static struct { |
| 1068 | const char *non_split; |
| 1069 | const char *split; |
| 1070 | } midx_exts[] = { |
| 1071 | {NULL, MIDX_EXT_MIDX}, |
| 1072 | {MIDX_EXT_BITMAP, MIDX_EXT_BITMAP}, |
| 1073 | {MIDX_EXT_REV, MIDX_EXT_REV}, |
| 1074 | }; |
| 1075 | |
| 1076 | static int link_midx_to_chain(struct multi_pack_index *m) |
| 1077 | { |
| 1078 | struct strbuf from = STRBUF_INIT; |
| 1079 | struct strbuf to = STRBUF_INIT; |
| 1080 | int ret = 0; |
| 1081 | size_t i; |
| 1082 | |
| 1083 | if (!m || m->has_chain) { |
| 1084 | /* |
| 1085 | * Either no MIDX previously existed, or it was already |
| 1086 | * part of a MIDX chain. In both cases, we have nothing |
| 1087 | * to link, so return early. |
| 1088 | */ |
| 1089 | goto done; |
| 1090 | } |
| 1091 | |
| 1092 | for (i = 0; i < ARRAY_SIZE(midx_exts); i++) { |
| 1093 | const unsigned char *hash = midx_get_checksum_hash(m); |
| 1094 | |
| 1095 | get_midx_filename_ext(m->source, &from, |
| 1096 | hash, midx_exts[i].non_split); |
| 1097 | get_split_midx_filename_ext(m->source, &to, hash, |
| 1098 | midx_exts[i].split); |
| 1099 | |
| 1100 | if (link(from.buf, to.buf) < 0 && errno != ENOENT) { |
| 1101 | ret = error_errno(_("unable to link '%s' to '%s'"), |
| 1102 | from.buf, to.buf); |
| 1103 | goto done; |
| 1104 | } |
| 1105 | |
| 1106 | strbuf_reset(&from); |
| 1107 | strbuf_reset(&to); |
| 1108 | } |
| 1109 | |
| 1110 | done: |
| 1111 | strbuf_release(&from); |
| 1112 | strbuf_release(&to); |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
| 1116 | static void clear_midx_files(struct odb_source_packed *source, |
| 1117 | const struct strvec *hashes, unsigned incremental) |
| 1118 | { |
| 1119 | /* |
| 1120 | * if incremental: |
| 1121 | * - remove all non-incremental MIDX files |
| 1122 | * - remove any incremental MIDX files not in the current one |
| 1123 | * |
| 1124 | * if non-incremental: |
| 1125 | * - remove all incremental MIDX files |
| 1126 | * - remove any non-incremental MIDX files not matching the current |
| 1127 | * hash |
| 1128 | */ |
| 1129 | struct strbuf buf = STRBUF_INIT; |
| 1130 | const char *exts[] = { MIDX_EXT_BITMAP, MIDX_EXT_REV, MIDX_EXT_MIDX }; |
| 1131 | uint32_t i; |
| 1132 | |
| 1133 | for (i = 0; i < ARRAY_SIZE(exts); i++) { |
| 1134 | clear_incremental_midx_files_ext(source, exts[i], hashes); |
| 1135 | if (hashes) { |
| 1136 | for (size_t j = 0; j < hashes->nr; j++) |
| 1137 | clear_midx_files_ext(source, exts[i], |
| 1138 | hashes->v[j]); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | if (incremental) |
| 1143 | get_midx_filename(source, &buf); |
| 1144 | else |
| 1145 | get_midx_chain_filename(source, &buf); |
| 1146 | |
| 1147 | if (unlink(buf.buf) && errno != ENOENT) |
| 1148 | die_errno(_("failed to clear multi-pack-index at %s"), buf.buf); |
| 1149 | |
| 1150 | strbuf_release(&buf); |
| 1151 | } |
| 1152 | |
| 1153 | static bool midx_needs_update(struct multi_pack_index *midx, struct write_midx_context *ctx) |
| 1154 | { |
| 1155 | struct strset packs = STRSET_INIT; |
| 1156 | struct strbuf buf = STRBUF_INIT; |
| 1157 | bool needed = true; |
| 1158 | |
| 1159 | /* |
| 1160 | * Ensure that we have a valid checksum before consulting the |
| 1161 | * existing MIDX in order to determine if we can avoid an |
| 1162 | * update. |
| 1163 | * |
| 1164 | * This is necessary because the given MIDX is loaded directly |
| 1165 | * from the object store (because we still compare our proposed |
| 1166 | * update to any on-disk MIDX regardless of whether or not we |
| 1167 | * have assigned "ctx.m") and is thus not guaranteed to have a |
| 1168 | * valid checksum. |
| 1169 | */ |
| 1170 | if (!midx_checksum_valid(midx)) |
| 1171 | goto out; |
| 1172 | |
| 1173 | /* |
| 1174 | * If the version differs, we need to update. |
| 1175 | */ |
| 1176 | if (midx->version != ctx->version) |
| 1177 | goto out; |
| 1178 | |
| 1179 | /* |
| 1180 | * Ignore incremental updates for now. The assumption is that any |
| 1181 | * incremental update would be either empty (in which case we will bail |
| 1182 | * out later) or it would actually cover at least one new pack. |
| 1183 | */ |
| 1184 | if (ctx->incremental) |
| 1185 | goto out; |
| 1186 | |
| 1187 | if (ctx->compact) |
| 1188 | goto out; /* Compaction always requires an update. */ |
| 1189 | |
| 1190 | /* |
| 1191 | * Otherwise, we need to verify that the packs covered by the existing |
| 1192 | * MIDX match the packs that we already have. The logic to do so is way |
| 1193 | * more complicated than it has any right to be. This is because: |
| 1194 | * |
| 1195 | * - We cannot assume any ordering. |
| 1196 | * |
| 1197 | * - The MIDX packs may not be loaded at all, and loading them would |
| 1198 | * be wasteful. So we need to use the pack names tracked by the |
| 1199 | * MIDX itself. |
| 1200 | * |
| 1201 | * - The MIDX pack names are tracking the ".idx" files, whereas the |
| 1202 | * packs themselves are tracking the ".pack" files. So we need to |
| 1203 | * strip suffixes. |
| 1204 | */ |
| 1205 | if (ctx->nr != midx->num_packs + midx->num_packs_in_base) |
| 1206 | goto out; |
| 1207 | |
| 1208 | for (uint32_t i = 0; i < ctx->nr; i++) { |
| 1209 | strbuf_reset(&buf); |
| 1210 | strbuf_addstr(&buf, pack_basename(ctx->info[i].p)); |
| 1211 | strbuf_strip_suffix(&buf, ".pack"); |
| 1212 | |
| 1213 | if (!strset_add(&packs, buf.buf)) |
| 1214 | BUG("same pack added twice?"); |
| 1215 | } |
| 1216 | |
| 1217 | for (struct multi_pack_index *m = midx; m; m = m->base_midx) { |
| 1218 | for (uint32_t i = 0; i < m->num_packs; i++) { |
| 1219 | strbuf_reset(&buf); |
| 1220 | strbuf_addstr(&buf, m->pack_names[i]); |
| 1221 | strbuf_strip_suffix(&buf, ".idx"); |
| 1222 | |
| 1223 | if (!strset_contains(&packs, buf.buf)) |
| 1224 | goto out; |
| 1225 | strset_remove(&packs, buf.buf); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | needed = false; |
| 1230 | |
| 1231 | out: |
| 1232 | strbuf_release(&buf); |
| 1233 | strset_clear(&packs); |
| 1234 | return needed; |
| 1235 | } |
| 1236 | |
| 1237 | static int midx_hashcmp(const struct multi_pack_index *a, |
| 1238 | const struct multi_pack_index *b, |
| 1239 | const struct git_hash_algo *algop) |
| 1240 | { |
| 1241 | return hashcmp(midx_get_checksum_hash(a), midx_get_checksum_hash(b), |
| 1242 | algop); |
| 1243 | } |
| 1244 | |
| 1245 | struct write_midx_opts { |
| 1246 | struct odb_source_packed *source; /* non-optional */ |
| 1247 | |
| 1248 | struct string_list *packs_to_include; |
| 1249 | struct string_list *packs_to_drop; |
| 1250 | |
| 1251 | struct multi_pack_index *compact_from; |
| 1252 | struct multi_pack_index *compact_to; |
| 1253 | |
| 1254 | const char *preferred_pack_name; |
| 1255 | const char *refs_snapshot; |
| 1256 | const char *incremental_base; |
| 1257 | unsigned flags; |
| 1258 | }; |
| 1259 | |
| 1260 | static int write_midx_internal(struct write_midx_opts *opts) |
| 1261 | { |
| 1262 | struct repository *r = opts->source->base.odb->repo; |
| 1263 | struct strbuf midx_name = STRBUF_INIT; |
| 1264 | unsigned char midx_hash[GIT_MAX_RAWSZ]; |
| 1265 | uint32_t start_pack; |
| 1266 | struct hashfile *f = NULL; |
| 1267 | struct lock_file lk = LOCK_INIT; |
| 1268 | struct tempfile *incr; |
| 1269 | struct write_midx_context ctx = { |
| 1270 | .preferred_pack_idx = NO_PREFERRED_PACK, |
| 1271 | }; |
| 1272 | struct multi_pack_index *midx_to_free = NULL; |
| 1273 | int bitmapped_packs_concat_len = 0; |
| 1274 | int pack_name_concat_len = 0; |
| 1275 | int dropped_packs = 0; |
| 1276 | int result = -1; |
| 1277 | struct strvec keep_hashes = STRVEC_INIT; |
| 1278 | struct chunkfile *cf; |
| 1279 | |
| 1280 | trace2_region_enter("midx", "write_midx_internal", r); |
| 1281 | |
| 1282 | ctx.repo = r; |
| 1283 | ctx.source = opts->source; |
| 1284 | |
| 1285 | ctx.version = ((opts->flags & MIDX_WRITE_COMPACT) |
| 1286 | ? MIDX_VERSION_V2 |
| 1287 | : MIDX_VERSION_V1); |
| 1288 | repo_config_get_int(ctx.repo, "midx.version", &ctx.version); |
| 1289 | if (ctx.version != MIDX_VERSION_V1 && ctx.version != MIDX_VERSION_V2) |
| 1290 | die(_("unknown MIDX version: %d"), ctx.version); |
| 1291 | |
| 1292 | ctx.incremental = !!(opts->flags & MIDX_WRITE_INCREMENTAL); |
| 1293 | ctx.compact = !!(opts->flags & MIDX_WRITE_COMPACT); |
| 1294 | |
| 1295 | if (ctx.compact) { |
| 1296 | if (ctx.version != MIDX_VERSION_V2) |
| 1297 | die(_("cannot perform MIDX compaction with v1 format")); |
| 1298 | if (!opts->compact_from) |
| 1299 | BUG("expected non-NULL 'from' MIDX during compaction"); |
| 1300 | if (!opts->compact_to) |
| 1301 | BUG("expected non-NULL 'to' MIDX during compaction"); |
| 1302 | |
| 1303 | ctx.compact_from = opts->compact_from; |
| 1304 | ctx.compact_to = opts->compact_to; |
| 1305 | } |
| 1306 | |
| 1307 | if (ctx.incremental) |
| 1308 | strbuf_addf(&midx_name, |
| 1309 | "%s/pack/multi-pack-index.d/tmp_midx_XXXXXX", |
| 1310 | opts->source->base.path); |
| 1311 | else |
| 1312 | get_midx_filename(opts->source, &midx_name); |
| 1313 | if (safe_create_leading_directories(r, midx_name.buf)) |
| 1314 | die_errno(_("unable to create leading directories of %s"), |
| 1315 | midx_name.buf); |
| 1316 | |
| 1317 | if (!opts->packs_to_include || ctx.incremental) { |
| 1318 | struct multi_pack_index *m = get_multi_pack_index(opts->source); |
| 1319 | if (m && !midx_checksum_valid(m)) { |
| 1320 | warning(_("ignoring existing multi-pack-index; checksum mismatch")); |
| 1321 | m = NULL; |
| 1322 | } |
| 1323 | |
| 1324 | if (m) { |
| 1325 | /* |
| 1326 | * Only reference an existing MIDX when not filtering |
| 1327 | * which packs to include, since all packs and objects |
| 1328 | * are copied blindly from an existing MIDX if one is |
| 1329 | * present. |
| 1330 | */ |
| 1331 | if (ctx.incremental) |
| 1332 | ctx.base_midx = m; |
| 1333 | if (!opts->packs_to_include) |
| 1334 | ctx.m = m; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * If compacting MIDX layer(s) in the range [from, to], then the |
| 1340 | * compacted MIDX will share the same base MIDX as 'from', |
| 1341 | * unless a custom --base is specified (see below). |
| 1342 | */ |
| 1343 | if (ctx.compact) |
| 1344 | ctx.base_midx = ctx.compact_from->base_midx; |
| 1345 | |
| 1346 | if (opts->incremental_base) { |
| 1347 | if (!strcmp(opts->incremental_base, "none")) { |
| 1348 | ctx.base_midx = NULL; |
| 1349 | } else { |
| 1350 | while (ctx.base_midx) { |
| 1351 | const char *cmp = midx_get_checksum_hex(ctx.base_midx); |
| 1352 | if (!strcmp(opts->incremental_base, cmp)) |
| 1353 | break; |
| 1354 | |
| 1355 | ctx.base_midx = ctx.base_midx->base_midx; |
| 1356 | } |
| 1357 | |
| 1358 | if (!ctx.base_midx) { |
| 1359 | error(_("could not find base MIDX '%s'"), |
| 1360 | opts->incremental_base); |
| 1361 | goto cleanup; |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | ctx.nr = 0; |
| 1367 | ctx.alloc = ctx.m ? ctx.m->num_packs + ctx.m->num_packs_in_base : 16; |
| 1368 | ctx.info = NULL; |
| 1369 | ALLOC_ARRAY(ctx.info, ctx.alloc); |
| 1370 | |
| 1371 | if (ctx.incremental) { |
| 1372 | struct multi_pack_index *m = ctx.base_midx; |
| 1373 | while (m) { |
| 1374 | if (opts->flags & MIDX_WRITE_BITMAP && load_midx_revindex(m)) { |
| 1375 | error(_("could not load reverse index for MIDX %s"), |
| 1376 | midx_get_checksum_hex(m)); |
| 1377 | goto cleanup; |
| 1378 | } |
| 1379 | ctx.num_multi_pack_indexes_before++; |
| 1380 | m = m->base_midx; |
| 1381 | } |
| 1382 | } else if (ctx.m && !ctx.compact && fill_packs_from_midx(&ctx)) { |
| 1383 | goto cleanup; |
| 1384 | } |
| 1385 | |
| 1386 | start_pack = ctx.nr; |
| 1387 | |
| 1388 | ctx.pack_paths_checked = 0; |
| 1389 | if (opts->flags & MIDX_PROGRESS) |
| 1390 | ctx.progress = start_delayed_progress(r, |
| 1391 | _("Adding packfiles to multi-pack-index"), 0); |
| 1392 | else |
| 1393 | ctx.progress = NULL; |
| 1394 | |
| 1395 | if (ctx.compact) { |
| 1396 | int bitmap_order = 0; |
| 1397 | if (opts->preferred_pack_name) |
| 1398 | bitmap_order |= 1; |
| 1399 | else if (opts->flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) |
| 1400 | bitmap_order |= 1; |
| 1401 | |
| 1402 | fill_packs_from_midx_range(&ctx, bitmap_order); |
| 1403 | } else { |
| 1404 | ctx.to_include = opts->packs_to_include; |
| 1405 | for_each_file_in_pack_dir(opts->source->base.path, add_pack_to_midx, &ctx); |
| 1406 | } |
| 1407 | stop_progress(&ctx.progress); |
| 1408 | |
| 1409 | if (!opts->packs_to_drop) { |
| 1410 | /* |
| 1411 | * If there is no MIDX then either it doesn't exist, or we're |
| 1412 | * doing a geometric repack. Try to load it from the source to |
| 1413 | * tell these two cases apart. |
| 1414 | */ |
| 1415 | struct multi_pack_index *midx = ctx.m; |
| 1416 | if (!midx) |
| 1417 | midx = midx_to_free = load_multi_pack_index(ctx.source); |
| 1418 | |
| 1419 | if (midx && !midx_needs_update(midx, &ctx)) { |
| 1420 | struct bitmap_index *bitmap_git; |
| 1421 | int bitmap_exists; |
| 1422 | int want_bitmap = opts->flags & MIDX_WRITE_BITMAP; |
| 1423 | |
| 1424 | bitmap_git = prepare_midx_bitmap_git(midx); |
| 1425 | bitmap_exists = bitmap_git && bitmap_is_midx(bitmap_git); |
| 1426 | free_bitmap_index(bitmap_git); |
| 1427 | |
| 1428 | if (bitmap_exists || !want_bitmap) { |
| 1429 | /* |
| 1430 | * The correct MIDX already exists, and so does a |
| 1431 | * corresponding bitmap (or one wasn't requested). |
| 1432 | */ |
| 1433 | if (!want_bitmap) |
| 1434 | clear_midx_files_ext(ctx.source, "bitmap", NULL); |
| 1435 | result = 0; |
| 1436 | goto cleanup; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | close_midx(midx_to_free); |
| 1441 | midx_to_free = NULL; |
| 1442 | } |
| 1443 | |
| 1444 | if (ctx.incremental && !ctx.nr) { |
| 1445 | result = 0; |
| 1446 | goto cleanup; /* nothing to do */ |
| 1447 | } |
| 1448 | |
| 1449 | if (opts->preferred_pack_name) { |
| 1450 | ctx.preferred_pack_idx = NO_PREFERRED_PACK; |
| 1451 | |
| 1452 | for (size_t i = 0; i < ctx.nr; i++) { |
| 1453 | if (!cmp_idx_or_pack_name(opts->preferred_pack_name, |
| 1454 | ctx.info[i].pack_name)) { |
| 1455 | ctx.preferred_pack_idx = i; |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | if (ctx.preferred_pack_idx == NO_PREFERRED_PACK) |
| 1461 | warning(_("unknown preferred pack: '%s'"), |
| 1462 | opts->preferred_pack_name); |
| 1463 | } else if (ctx.nr && |
| 1464 | (opts->flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) { |
| 1465 | struct packed_git *oldest = ctx.info[0].p; |
| 1466 | ctx.preferred_pack_idx = 0; |
| 1467 | |
| 1468 | /* |
| 1469 | * Attempt opening the pack index to populate num_objects. |
| 1470 | * Ignore failures as they can be expected and are not |
| 1471 | * fatal during this selection time. |
| 1472 | */ |
| 1473 | open_pack_index(oldest); |
| 1474 | |
| 1475 | if (opts->packs_to_drop && opts->packs_to_drop->nr) |
| 1476 | BUG("cannot write a MIDX bitmap during expiration"); |
| 1477 | |
| 1478 | /* |
| 1479 | * set a preferred pack when writing a bitmap to ensure that |
| 1480 | * the pack from which the first object is selected in pseudo |
| 1481 | * pack-order has all of its objects selected from that pack |
| 1482 | * (and not another pack containing a duplicate) |
| 1483 | */ |
| 1484 | for (size_t i = 1; i < ctx.nr; i++) { |
| 1485 | struct packed_git *p = ctx.info[i].p; |
| 1486 | |
| 1487 | if (!oldest->num_objects || p->mtime < oldest->mtime) { |
| 1488 | oldest = p; |
| 1489 | open_pack_index(oldest); |
| 1490 | ctx.preferred_pack_idx = i; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | if (!oldest->num_objects) { |
| 1495 | /* |
| 1496 | * If all packs are empty; unset the preferred index. |
| 1497 | * This is acceptable since there will be no duplicate |
| 1498 | * objects to resolve, so the preferred value doesn't |
| 1499 | * matter. |
| 1500 | */ |
| 1501 | ctx.preferred_pack_idx = NO_PREFERRED_PACK; |
| 1502 | } |
| 1503 | } else { |
| 1504 | /* |
| 1505 | * otherwise don't mark any pack as preferred to avoid |
| 1506 | * interfering with expiration logic below |
| 1507 | */ |
| 1508 | ctx.preferred_pack_idx = NO_PREFERRED_PACK; |
| 1509 | } |
| 1510 | |
| 1511 | if (ctx.preferred_pack_idx != NO_PREFERRED_PACK) { |
| 1512 | struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p; |
| 1513 | |
| 1514 | if (open_pack_index(preferred)) |
| 1515 | die(_("failed to open preferred pack %s"), |
| 1516 | ctx.info[ctx.preferred_pack_idx].pack_name); |
| 1517 | |
| 1518 | if (!preferred->num_objects) { |
| 1519 | error(_("cannot select preferred pack %s with no objects"), |
| 1520 | preferred->pack_name); |
| 1521 | goto cleanup; |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | compute_sorted_entries(&ctx, start_pack); |
| 1526 | |
| 1527 | ctx.large_offsets_needed = 0; |
| 1528 | for (size_t i = 0; i < ctx.entries_nr; i++) { |
| 1529 | if (ctx.entries[i].offset > 0x7fffffff) |
| 1530 | ctx.num_large_offsets++; |
| 1531 | if (ctx.entries[i].offset > 0xffffffff) |
| 1532 | ctx.large_offsets_needed = 1; |
| 1533 | } |
| 1534 | |
| 1535 | if (ctx.compact) { |
| 1536 | if (ctx.version != MIDX_VERSION_V2) |
| 1537 | BUG("performing MIDX compaction with v1 MIDX"); |
| 1538 | } else { |
| 1539 | QSORT(ctx.info, ctx.nr, pack_info_compare); |
| 1540 | } |
| 1541 | |
| 1542 | if (opts->packs_to_drop && opts->packs_to_drop->nr) { |
| 1543 | size_t drop_index = 0; |
| 1544 | int missing_drops = 0; |
| 1545 | |
| 1546 | ASSERT(!ctx.compact); |
| 1547 | |
| 1548 | for (size_t i = 0; |
| 1549 | i < ctx.nr && drop_index < opts->packs_to_drop->nr; i++) { |
| 1550 | int cmp = strcmp(ctx.info[i].pack_name, |
| 1551 | opts->packs_to_drop->items[drop_index].string); |
| 1552 | |
| 1553 | if (!cmp) { |
| 1554 | drop_index++; |
| 1555 | ctx.info[i].expired = 1; |
| 1556 | } else if (cmp > 0) { |
| 1557 | error(_("did not see pack-file %s to drop"), |
| 1558 | opts->packs_to_drop->items[drop_index].string); |
| 1559 | drop_index++; |
| 1560 | missing_drops++; |
| 1561 | i--; |
| 1562 | } else { |
| 1563 | ctx.info[i].expired = 0; |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | if (missing_drops) |
| 1568 | goto cleanup; |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * pack_perm stores a permutation between pack-int-ids from the |
| 1573 | * previous multi-pack-index to the new one we are writing: |
| 1574 | * |
| 1575 | * pack_perm[old_id] = new_id |
| 1576 | */ |
| 1577 | ALLOC_ARRAY(ctx.pack_perm, ctx.nr); |
| 1578 | for (size_t i = 0; i < ctx.nr; i++) { |
| 1579 | uint32_t from = ctx.info[i].orig_pack_int_id; |
| 1580 | uint32_t to; |
| 1581 | |
| 1582 | if (ctx.info[i].expired) { |
| 1583 | to = PACK_EXPIRED; |
| 1584 | dropped_packs++; |
| 1585 | } else { |
| 1586 | to = i - dropped_packs; |
| 1587 | } |
| 1588 | |
| 1589 | if (ctx.compact) |
| 1590 | from -= ctx.compact_from->num_packs_in_base; |
| 1591 | |
| 1592 | ctx.pack_perm[from] = to; |
| 1593 | } |
| 1594 | |
| 1595 | for (size_t i = 0; i < ctx.nr; i++) { |
| 1596 | if (ctx.info[i].expired) |
| 1597 | continue; |
| 1598 | pack_name_concat_len += strlen(ctx.info[i].pack_name) + 1; |
| 1599 | bitmapped_packs_concat_len += 2 * sizeof(uint32_t); |
| 1600 | } |
| 1601 | |
| 1602 | /* Check that the preferred pack wasn't expired (if given). */ |
| 1603 | if (opts->preferred_pack_name) { |
| 1604 | struct pack_info *preferred = bsearch(opts->preferred_pack_name, |
| 1605 | ctx.info, ctx.nr, |
| 1606 | sizeof(*ctx.info), |
| 1607 | idx_or_pack_name_cmp); |
| 1608 | if (preferred) { |
| 1609 | uint32_t perm = midx_pack_perm(&ctx, preferred->orig_pack_int_id); |
| 1610 | if (perm == PACK_EXPIRED) |
| 1611 | warning(_("preferred pack '%s' is expired"), |
| 1612 | opts->preferred_pack_name); |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT) |
| 1617 | pack_name_concat_len += MIDX_CHUNK_ALIGNMENT - |
| 1618 | (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT); |
| 1619 | |
| 1620 | if (ctx.nr - dropped_packs == 0) { |
| 1621 | error(_("no pack files to index.")); |
| 1622 | goto cleanup; |
| 1623 | } |
| 1624 | |
| 1625 | if (!ctx.entries_nr) { |
| 1626 | if (opts->flags & MIDX_WRITE_BITMAP) |
| 1627 | warning(_("refusing to write multi-pack .bitmap without any objects")); |
| 1628 | opts->flags &= ~(MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP); |
| 1629 | } |
| 1630 | |
| 1631 | if (ctx.incremental) { |
| 1632 | if (!(opts->flags & MIDX_WRITE_NO_CHAIN)) { |
| 1633 | struct strbuf lock_name = STRBUF_INIT; |
| 1634 | |
| 1635 | get_midx_chain_filename(opts->source, &lock_name); |
| 1636 | repo_hold_lock_file_for_update(r, &lk, lock_name.buf, |
| 1637 | LOCK_DIE_ON_ERROR); |
| 1638 | strbuf_release(&lock_name); |
| 1639 | } |
| 1640 | |
| 1641 | incr = mks_tempfile_m(midx_name.buf, 0444); |
| 1642 | if (!incr) { |
| 1643 | error(_("unable to create temporary MIDX layer")); |
| 1644 | goto cleanup; |
| 1645 | } |
| 1646 | |
| 1647 | if (adjust_shared_perm(r, get_tempfile_path(incr))) { |
| 1648 | error(_("unable to adjust shared permissions for '%s'"), |
| 1649 | get_tempfile_path(incr)); |
| 1650 | goto cleanup; |
| 1651 | } |
| 1652 | |
| 1653 | f = hashfd(r->hash_algo, get_tempfile_fd(incr), |
| 1654 | get_tempfile_path(incr)); |
| 1655 | } else { |
| 1656 | repo_hold_lock_file_for_update(r, &lk, midx_name.buf, |
| 1657 | LOCK_DIE_ON_ERROR); |
| 1658 | f = hashfd(r->hash_algo, get_lock_file_fd(&lk), |
| 1659 | get_lock_file_path(&lk)); |
| 1660 | } |
| 1661 | |
| 1662 | cf = init_chunkfile(f); |
| 1663 | |
| 1664 | add_chunk(cf, MIDX_CHUNKID_PACKNAMES, pack_name_concat_len, |
| 1665 | write_midx_pack_names); |
| 1666 | add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE, |
| 1667 | write_midx_oid_fanout); |
| 1668 | add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP, |
| 1669 | st_mult(ctx.entries_nr, r->hash_algo->rawsz), |
| 1670 | write_midx_oid_lookup); |
| 1671 | add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS, |
| 1672 | st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH), |
| 1673 | write_midx_object_offsets); |
| 1674 | |
| 1675 | if (ctx.large_offsets_needed) |
| 1676 | add_chunk(cf, MIDX_CHUNKID_LARGEOFFSETS, |
| 1677 | st_mult(ctx.num_large_offsets, |
| 1678 | MIDX_CHUNK_LARGE_OFFSET_WIDTH), |
| 1679 | write_midx_large_offsets); |
| 1680 | |
| 1681 | if (opts->flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP)) { |
| 1682 | ctx.pack_order = midx_pack_order(&ctx); |
| 1683 | add_chunk(cf, MIDX_CHUNKID_REVINDEX, |
| 1684 | st_mult(ctx.entries_nr, sizeof(uint32_t)), |
| 1685 | write_midx_revindex); |
| 1686 | add_chunk(cf, MIDX_CHUNKID_BITMAPPEDPACKS, |
| 1687 | bitmapped_packs_concat_len, |
| 1688 | write_midx_bitmapped_packs); |
| 1689 | } |
| 1690 | |
| 1691 | write_midx_header(r->hash_algo, f, get_num_chunks(cf), |
| 1692 | ctx.nr - dropped_packs, ctx.version); |
| 1693 | write_chunkfile(cf, &ctx); |
| 1694 | |
| 1695 | finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA, |
| 1696 | CSUM_FSYNC | CSUM_HASH_IN_STREAM); |
| 1697 | free_chunkfile(cf); |
| 1698 | |
| 1699 | if (opts->flags & MIDX_WRITE_REV_INDEX && |
| 1700 | git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0)) |
| 1701 | write_midx_reverse_index(&ctx, midx_hash); |
| 1702 | |
| 1703 | if (opts->flags & MIDX_WRITE_BITMAP) { |
| 1704 | struct packing_data pdata; |
| 1705 | struct commit_stack commits = COMMIT_STACK_INIT; |
| 1706 | |
| 1707 | if (!ctx.entries_nr) |
| 1708 | BUG("cannot write a bitmap without any objects"); |
| 1709 | |
| 1710 | prepare_midx_packing_data(&pdata, &ctx); |
| 1711 | |
| 1712 | find_commits_for_midx_bitmap(&commits, opts->refs_snapshot, &ctx); |
| 1713 | |
| 1714 | /* |
| 1715 | * The previous steps translated the information from |
| 1716 | * 'entries' into information suitable for constructing |
| 1717 | * bitmaps. We no longer need that array, so clear it to |
| 1718 | * reduce memory pressure. |
| 1719 | */ |
| 1720 | FREE_AND_NULL(ctx.entries); |
| 1721 | ctx.entries_nr = 0; |
| 1722 | |
| 1723 | if (write_midx_bitmap(&ctx, midx_hash, &pdata, commits.items, |
| 1724 | commits.nr, opts->flags) < 0) { |
| 1725 | error(_("could not write multi-pack bitmap")); |
| 1726 | clear_packing_data(&pdata); |
| 1727 | commit_stack_clear(&commits); |
| 1728 | goto cleanup; |
| 1729 | } |
| 1730 | |
| 1731 | clear_packing_data(&pdata); |
| 1732 | commit_stack_clear(&commits); |
| 1733 | } |
| 1734 | /* |
| 1735 | * NOTE: Do not use ctx.entries beyond this point, since it might |
| 1736 | * have been freed in the previous if block. |
| 1737 | */ |
| 1738 | |
| 1739 | if (ctx.num_multi_pack_indexes_before == UINT32_MAX) |
| 1740 | die(_("too many multi-pack-indexes")); |
| 1741 | |
| 1742 | if (!is_lock_file_locked(&lk)) |
| 1743 | printf("%s\n", hash_to_hex_algop(midx_hash, r->hash_algo)); |
| 1744 | else if (opts->flags & MIDX_WRITE_NO_CHAIN) |
| 1745 | BUG("lockfile held with MIDX_WRITE_NO_CHAIN set?"); |
| 1746 | |
| 1747 | if (ctx.incremental) { |
| 1748 | struct strbuf final_midx_name = STRBUF_INIT; |
| 1749 | struct multi_pack_index *m = ctx.base_midx; |
| 1750 | struct multi_pack_index **layers = NULL; |
| 1751 | size_t layers_nr = 0, layers_alloc = 0; |
| 1752 | |
| 1753 | if (is_lock_file_locked(&lk)){ |
| 1754 | FILE *chainf = fdopen_lock_file(&lk, "w"); |
| 1755 | if (!chainf) { |
| 1756 | error_errno(_("unable to open multi-pack-index chain file")); |
| 1757 | goto cleanup; |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | if (link_midx_to_chain(ctx.base_midx) < 0) |
| 1762 | goto cleanup; |
| 1763 | |
| 1764 | get_split_midx_filename_ext(opts->source, &final_midx_name, |
| 1765 | midx_hash, MIDX_EXT_MIDX); |
| 1766 | |
| 1767 | if (rename_tempfile(&incr, final_midx_name.buf) < 0) { |
| 1768 | error_errno(_("unable to rename new multi-pack-index layer")); |
| 1769 | goto cleanup; |
| 1770 | } |
| 1771 | |
| 1772 | strbuf_release(&final_midx_name); |
| 1773 | |
| 1774 | if (ctx.compact) { |
| 1775 | struct multi_pack_index *mp; |
| 1776 | |
| 1777 | for (mp = ctx.base_midx; mp; mp = mp->base_midx) { |
| 1778 | ALLOC_GROW(layers, layers_nr + 1, layers_alloc); |
| 1779 | layers[layers_nr++] = mp; |
| 1780 | } |
| 1781 | while (layers_nr) |
| 1782 | strvec_push(&keep_hashes, |
| 1783 | midx_get_checksum_hex(layers[--layers_nr])); |
| 1784 | |
| 1785 | strvec_push(&keep_hashes, |
| 1786 | hash_to_hex_algop(midx_hash, |
| 1787 | r->hash_algo)); |
| 1788 | |
| 1789 | for (mp = ctx.m; |
| 1790 | mp && midx_hashcmp(mp, ctx.compact_to, |
| 1791 | r->hash_algo); |
| 1792 | mp = mp->base_midx) { |
| 1793 | ALLOC_GROW(layers, layers_nr + 1, layers_alloc); |
| 1794 | layers[layers_nr++] = mp; |
| 1795 | } |
| 1796 | while (layers_nr) |
| 1797 | strvec_push(&keep_hashes, |
| 1798 | midx_get_checksum_hex(layers[--layers_nr])); |
| 1799 | } else { |
| 1800 | for (; m; m = m->base_midx) { |
| 1801 | ALLOC_GROW(layers, layers_nr + 1, layers_alloc); |
| 1802 | layers[layers_nr++] = m; |
| 1803 | } |
| 1804 | while (layers_nr) |
| 1805 | strvec_push(&keep_hashes, |
| 1806 | midx_get_checksum_hex(layers[--layers_nr])); |
| 1807 | |
| 1808 | strvec_push(&keep_hashes, |
| 1809 | hash_to_hex_algop(midx_hash, |
| 1810 | r->hash_algo)); |
| 1811 | } |
| 1812 | |
| 1813 | free(layers); |
| 1814 | |
| 1815 | if (is_lock_file_locked(&lk)) |
| 1816 | for (size_t i = 0; i < keep_hashes.nr; i++) |
| 1817 | fprintf(get_lock_file_fp(&lk), "%s\n", |
| 1818 | keep_hashes.v[i]); |
| 1819 | } else { |
| 1820 | strvec_push(&keep_hashes, |
| 1821 | hash_to_hex_algop(midx_hash, r->hash_algo)); |
| 1822 | } |
| 1823 | |
| 1824 | if (ctx.m || ctx.base_midx) |
| 1825 | odb_close(ctx.repo->objects); |
| 1826 | |
| 1827 | if (is_lock_file_locked(&lk)) { |
| 1828 | if (commit_lock_file(&lk) < 0) |
| 1829 | die_errno(_("could not write multi-pack-index")); |
| 1830 | |
| 1831 | clear_midx_files(opts->source, &keep_hashes, ctx.incremental); |
| 1832 | } |
| 1833 | result = 0; |
| 1834 | |
| 1835 | cleanup: |
| 1836 | for (size_t i = 0; i < ctx.nr; i++) { |
| 1837 | if (ctx.info[i].p) { |
| 1838 | close_pack(ctx.info[i].p); |
| 1839 | free(ctx.info[i].p); |
| 1840 | } |
| 1841 | free(ctx.info[i].pack_name); |
| 1842 | } |
| 1843 | |
| 1844 | free(ctx.info); |
| 1845 | free(ctx.entries); |
| 1846 | free(ctx.pack_perm); |
| 1847 | free(ctx.pack_order); |
| 1848 | strvec_clear(&keep_hashes); |
| 1849 | strbuf_release(&midx_name); |
| 1850 | close_midx(midx_to_free); |
| 1851 | |
| 1852 | trace2_region_leave("midx", "write_midx_internal", r); |
| 1853 | |
| 1854 | return result; |
| 1855 | } |
| 1856 | |
| 1857 | int write_midx_file(struct odb_source_packed *source, |
| 1858 | const char *preferred_pack_name, |
| 1859 | const char *refs_snapshot, |
| 1860 | const char *incremental_base, |
| 1861 | unsigned flags) |
| 1862 | { |
| 1863 | struct write_midx_opts opts = { |
| 1864 | .source = source, |
| 1865 | .preferred_pack_name = preferred_pack_name, |
| 1866 | .refs_snapshot = refs_snapshot, |
| 1867 | .incremental_base = incremental_base, |
| 1868 | .flags = flags, |
| 1869 | }; |
| 1870 | |
| 1871 | return write_midx_internal(&opts); |
| 1872 | } |
| 1873 | |
| 1874 | int write_midx_file_only(struct odb_source_packed *source, |
| 1875 | struct string_list *packs_to_include, |
| 1876 | const char *preferred_pack_name, |
| 1877 | const char *refs_snapshot, |
| 1878 | const char *incremental_base, |
| 1879 | unsigned flags) |
| 1880 | { |
| 1881 | struct write_midx_opts opts = { |
| 1882 | .source = source, |
| 1883 | .packs_to_include = packs_to_include, |
| 1884 | .preferred_pack_name = preferred_pack_name, |
| 1885 | .refs_snapshot = refs_snapshot, |
| 1886 | .incremental_base = incremental_base, |
| 1887 | .flags = flags, |
| 1888 | }; |
| 1889 | |
| 1890 | return write_midx_internal(&opts); |
| 1891 | } |
| 1892 | |
| 1893 | int write_midx_file_compact(struct odb_source_packed *source, |
| 1894 | struct multi_pack_index *from, |
| 1895 | struct multi_pack_index *to, |
| 1896 | const char *incremental_base, |
| 1897 | unsigned flags) |
| 1898 | { |
| 1899 | struct write_midx_opts opts = { |
| 1900 | .source = source, |
| 1901 | .compact_from = from, |
| 1902 | .compact_to = to, |
| 1903 | .incremental_base = incremental_base, |
| 1904 | .flags = flags | MIDX_WRITE_COMPACT, |
| 1905 | }; |
| 1906 | |
| 1907 | return write_midx_internal(&opts); |
| 1908 | } |
| 1909 | |
| 1910 | int expire_midx_packs(struct odb_source_packed *source, unsigned flags) |
| 1911 | { |
| 1912 | uint32_t i, *count, result = 0; |
| 1913 | struct string_list packs_to_drop = STRING_LIST_INIT_DUP; |
| 1914 | struct multi_pack_index *m = get_multi_pack_index(source); |
| 1915 | struct progress *progress = NULL; |
| 1916 | |
| 1917 | if (!m) |
| 1918 | return 0; |
| 1919 | |
| 1920 | if (m->base_midx) |
| 1921 | die(_("cannot expire packs from an incremental multi-pack-index")); |
| 1922 | |
| 1923 | CALLOC_ARRAY(count, m->num_packs); |
| 1924 | |
| 1925 | if (flags & MIDX_PROGRESS) |
| 1926 | progress = start_delayed_progress( |
| 1927 | source->base.odb->repo, |
| 1928 | _("Counting referenced objects"), |
| 1929 | m->num_objects); |
| 1930 | for (i = 0; i < m->num_objects; i++) { |
| 1931 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); |
| 1932 | count[pack_int_id]++; |
| 1933 | display_progress(progress, i + 1); |
| 1934 | } |
| 1935 | stop_progress(&progress); |
| 1936 | |
| 1937 | if (flags & MIDX_PROGRESS) |
| 1938 | progress = start_delayed_progress( |
| 1939 | source->base.odb->repo, |
| 1940 | _("Finding and deleting unreferenced packfiles"), |
| 1941 | m->num_packs); |
| 1942 | for (i = 0; i < m->num_packs; i++) { |
| 1943 | char *pack_name; |
| 1944 | display_progress(progress, i + 1); |
| 1945 | |
| 1946 | if (count[i]) |
| 1947 | continue; |
| 1948 | |
| 1949 | if (prepare_midx_pack(m, i)) |
| 1950 | continue; |
| 1951 | |
| 1952 | if (m->packs[i]->pack_keep || m->packs[i]->is_cruft) |
| 1953 | continue; |
| 1954 | |
| 1955 | pack_name = xstrdup(m->packs[i]->pack_name); |
| 1956 | close_pack(m->packs[i]); |
| 1957 | |
| 1958 | string_list_insert(&packs_to_drop, m->pack_names[i]); |
| 1959 | unlink_pack_path(pack_name, 0); |
| 1960 | free(pack_name); |
| 1961 | } |
| 1962 | stop_progress(&progress); |
| 1963 | |
| 1964 | free(count); |
| 1965 | |
| 1966 | if (packs_to_drop.nr) { |
| 1967 | struct write_midx_opts opts = { |
| 1968 | .source = source, |
| 1969 | .packs_to_drop = &packs_to_drop, |
| 1970 | .flags = flags & MIDX_PROGRESS, |
| 1971 | }; |
| 1972 | result = write_midx_internal(&opts); |
| 1973 | } |
| 1974 | |
| 1975 | string_list_clear(&packs_to_drop, 0); |
| 1976 | |
| 1977 | return result; |
| 1978 | } |
| 1979 | |
| 1980 | struct repack_info { |
| 1981 | timestamp_t mtime; |
| 1982 | uint32_t referenced_objects; |
| 1983 | uint32_t pack_int_id; |
| 1984 | }; |
| 1985 | |
| 1986 | static int compare_by_mtime(const void *a_, const void *b_) |
| 1987 | { |
| 1988 | const struct repack_info *a, *b; |
| 1989 | |
| 1990 | a = (const struct repack_info *)a_; |
| 1991 | b = (const struct repack_info *)b_; |
| 1992 | |
| 1993 | if (a->mtime < b->mtime) |
| 1994 | return -1; |
| 1995 | if (a->mtime > b->mtime) |
| 1996 | return 1; |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
| 2000 | static int want_included_pack(struct multi_pack_index *m, |
| 2001 | int pack_kept_objects, |
| 2002 | uint32_t pack_int_id) |
| 2003 | { |
| 2004 | struct packed_git *p; |
| 2005 | if (prepare_midx_pack(m, pack_int_id)) |
| 2006 | return 0; |
| 2007 | p = m->packs[pack_int_id]; |
| 2008 | if (!pack_kept_objects && p->pack_keep) |
| 2009 | return 0; |
| 2010 | if (p->is_cruft) |
| 2011 | return 0; |
| 2012 | if (open_pack_index(p) || !p->num_objects) |
| 2013 | return 0; |
| 2014 | return 1; |
| 2015 | } |
| 2016 | |
| 2017 | static void fill_included_packs_all(struct repository *r, |
| 2018 | struct multi_pack_index *m, |
| 2019 | unsigned char *include_pack) |
| 2020 | { |
| 2021 | uint32_t i; |
| 2022 | int pack_kept_objects = 0; |
| 2023 | |
| 2024 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); |
| 2025 | |
| 2026 | for (i = 0; i < m->num_packs; i++) { |
| 2027 | if (!want_included_pack(m, pack_kept_objects, i)) |
| 2028 | continue; |
| 2029 | |
| 2030 | include_pack[i] = 1; |
| 2031 | } |
| 2032 | } |
| 2033 | |
| 2034 | static void fill_included_packs_batch(struct repository *r, |
| 2035 | struct multi_pack_index *m, |
| 2036 | unsigned char *include_pack, |
| 2037 | size_t batch_size) |
| 2038 | { |
| 2039 | uint32_t i; |
| 2040 | size_t total_size; |
| 2041 | struct repack_info *pack_info; |
| 2042 | int pack_kept_objects = 0; |
| 2043 | |
| 2044 | CALLOC_ARRAY(pack_info, m->num_packs); |
| 2045 | |
| 2046 | repo_config_get_bool(r, "repack.packkeptobjects", &pack_kept_objects); |
| 2047 | |
| 2048 | for (i = 0; i < m->num_packs; i++) { |
| 2049 | pack_info[i].pack_int_id = i; |
| 2050 | |
| 2051 | if (prepare_midx_pack(m, i)) |
| 2052 | continue; |
| 2053 | |
| 2054 | pack_info[i].mtime = m->packs[i]->mtime; |
| 2055 | } |
| 2056 | |
| 2057 | for (i = 0; i < m->num_objects; i++) { |
| 2058 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); |
| 2059 | pack_info[pack_int_id].referenced_objects++; |
| 2060 | } |
| 2061 | |
| 2062 | QSORT(pack_info, m->num_packs, compare_by_mtime); |
| 2063 | |
| 2064 | total_size = 0; |
| 2065 | for (i = 0; total_size < batch_size && i < m->num_packs; i++) { |
| 2066 | uint32_t pack_int_id = pack_info[i].pack_int_id; |
| 2067 | struct packed_git *p = m->packs[pack_int_id]; |
| 2068 | uint64_t expected_size; |
| 2069 | |
| 2070 | if (!want_included_pack(m, pack_kept_objects, pack_int_id)) |
| 2071 | continue; |
| 2072 | |
| 2073 | /* |
| 2074 | * Use shifted integer arithmetic to calculate the |
| 2075 | * expected pack size to ~4 significant digits without |
| 2076 | * overflow for packsizes less that 1PB. |
| 2077 | */ |
| 2078 | expected_size = (uint64_t)pack_info[i].referenced_objects << 14; |
| 2079 | expected_size /= p->num_objects; |
| 2080 | expected_size = u64_mult(expected_size, p->pack_size); |
| 2081 | expected_size = u64_add(expected_size, 1u << 13) >> 14; |
| 2082 | |
| 2083 | if (expected_size >= batch_size) |
| 2084 | continue; |
| 2085 | |
| 2086 | if (unsigned_add_overflows(total_size, (size_t)expected_size)) |
| 2087 | total_size = SIZE_MAX; |
| 2088 | else |
| 2089 | total_size += expected_size; |
| 2090 | |
| 2091 | include_pack[pack_int_id] = 1; |
| 2092 | } |
| 2093 | |
| 2094 | free(pack_info); |
| 2095 | } |
| 2096 | |
| 2097 | int midx_repack(struct odb_source_packed *source, size_t batch_size, unsigned flags) |
| 2098 | { |
| 2099 | struct repository *r = source->base.odb->repo; |
| 2100 | int result = 0; |
| 2101 | uint32_t i, packs_to_repack = 0; |
| 2102 | unsigned char *include_pack; |
| 2103 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 2104 | FILE *cmd_in; |
| 2105 | struct multi_pack_index *m = get_multi_pack_index(source); |
| 2106 | struct write_midx_opts opts = { |
| 2107 | .source = source, |
| 2108 | .flags = flags, |
| 2109 | }; |
| 2110 | |
| 2111 | /* |
| 2112 | * When updating the default for these configuration |
| 2113 | * variables in builtin/repack.c, these must be adjusted |
| 2114 | * to match. |
| 2115 | */ |
| 2116 | int delta_base_offset = 1; |
| 2117 | int use_delta_islands = 0; |
| 2118 | |
| 2119 | if (!m) |
| 2120 | return 0; |
| 2121 | if (m->base_midx) |
| 2122 | die(_("cannot repack an incremental multi-pack-index")); |
| 2123 | |
| 2124 | CALLOC_ARRAY(include_pack, m->num_packs); |
| 2125 | |
| 2126 | if (batch_size) |
| 2127 | fill_included_packs_batch(r, m, include_pack, batch_size); |
| 2128 | else |
| 2129 | fill_included_packs_all(r, m, include_pack); |
| 2130 | |
| 2131 | for (i = 0; i < m->num_packs; i++) { |
| 2132 | if (include_pack[i]) |
| 2133 | packs_to_repack++; |
| 2134 | } |
| 2135 | if (packs_to_repack <= 1) |
| 2136 | goto cleanup; |
| 2137 | |
| 2138 | repo_config_get_bool(r, "repack.usedeltabaseoffset", &delta_base_offset); |
| 2139 | repo_config_get_bool(r, "repack.usedeltaislands", &use_delta_islands); |
| 2140 | |
| 2141 | strvec_push(&cmd.args, "pack-objects"); |
| 2142 | |
| 2143 | strvec_pushf(&cmd.args, "%s/pack/pack", source->base.path); |
| 2144 | |
| 2145 | if (delta_base_offset) |
| 2146 | strvec_push(&cmd.args, "--delta-base-offset"); |
| 2147 | if (use_delta_islands) |
| 2148 | strvec_push(&cmd.args, "--delta-islands"); |
| 2149 | |
| 2150 | if (flags & MIDX_PROGRESS) |
| 2151 | strvec_push(&cmd.args, "--progress"); |
| 2152 | else |
| 2153 | strvec_push(&cmd.args, "-q"); |
| 2154 | |
| 2155 | cmd.git_cmd = 1; |
| 2156 | cmd.in = cmd.out = -1; |
| 2157 | |
| 2158 | if (start_command(&cmd)) { |
| 2159 | error(_("could not start pack-objects")); |
| 2160 | result = 1; |
| 2161 | goto cleanup; |
| 2162 | } |
| 2163 | |
| 2164 | cmd_in = xfdopen(cmd.in, "w"); |
| 2165 | |
| 2166 | for (i = 0; i < m->num_objects; i++) { |
| 2167 | struct object_id oid; |
| 2168 | uint32_t pack_int_id = nth_midxed_pack_int_id(m, i); |
| 2169 | |
| 2170 | if (!include_pack[pack_int_id]) |
| 2171 | continue; |
| 2172 | |
| 2173 | nth_midxed_object_oid(&oid, m, i); |
| 2174 | fprintf(cmd_in, "%s\n", oid_to_hex(&oid)); |
| 2175 | } |
| 2176 | fclose(cmd_in); |
| 2177 | |
| 2178 | if (finish_command(&cmd)) { |
| 2179 | error(_("could not finish pack-objects")); |
| 2180 | result = 1; |
| 2181 | goto cleanup; |
| 2182 | } |
| 2183 | |
| 2184 | result = write_midx_internal(&opts); |
| 2185 | |
| 2186 | cleanup: |
| 2187 | free(include_pack); |
| 2188 | return result; |
| 2189 | } |