Git fork

midx: write multi-pack indices via their source

Similar to the preceding commit, refactor the writing side of multi-pack
indices so that we pass in the object database source where the index
should be written to.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Patrick Steinhardt and committed by
Junio C Hamano
c3f5d251 017db7bb

+47 -49
+11 -8
builtin/multi-pack-index.c
··· 147 N_("refs snapshot for selecting bitmap commits")), 148 OPT_END(), 149 }; 150 int ret; 151 152 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE; ··· 165 if (argc) 166 usage_with_options(builtin_multi_pack_index_write_usage, 167 options); 168 - handle_object_dir_option(repo); 169 170 FREE_AND_NULL(options); 171 ··· 174 175 read_packs_from_stdin(&packs); 176 177 - ret = write_midx_file_only(repo, opts.object_dir, &packs, 178 opts.preferred_pack, 179 opts.refs_snapshot, opts.flags); 180 ··· 185 186 } 187 188 - ret = write_midx_file(repo, opts.object_dir, opts.preferred_pack, 189 opts.refs_snapshot, opts.flags); 190 191 free(opts.refs_snapshot); ··· 233 N_("force progress reporting"), MIDX_PROGRESS), 234 OPT_END(), 235 }; 236 options = add_common_options(builtin_multi_pack_index_expire_options); 237 238 trace2_cmd_mode(argv[0]); ··· 245 if (argc) 246 usage_with_options(builtin_multi_pack_index_expire_usage, 247 options); 248 - handle_object_dir_option(the_repository); 249 250 FREE_AND_NULL(options); 251 252 - return expire_midx_packs(the_repository, opts.object_dir, opts.flags); 253 } 254 255 static int cmd_multi_pack_index_repack(int argc, const char **argv, ··· 264 N_("force progress reporting"), MIDX_PROGRESS), 265 OPT_END(), 266 }; 267 268 options = add_common_options(builtin_multi_pack_index_repack_options); 269 ··· 278 if (argc) 279 usage_with_options(builtin_multi_pack_index_repack_usage, 280 options); 281 - handle_object_dir_option(the_repository); 282 283 FREE_AND_NULL(options); 284 285 - return midx_repack(the_repository, opts.object_dir, 286 - (size_t)opts.batch_size, opts.flags); 287 } 288 289 int cmd_multi_pack_index(int argc,
··· 147 N_("refs snapshot for selecting bitmap commits")), 148 OPT_END(), 149 }; 150 + struct odb_source *source; 151 int ret; 152 153 opts.flags |= MIDX_WRITE_BITMAP_HASH_CACHE; ··· 166 if (argc) 167 usage_with_options(builtin_multi_pack_index_write_usage, 168 options); 169 + source = handle_object_dir_option(repo); 170 171 FREE_AND_NULL(options); 172 ··· 175 176 read_packs_from_stdin(&packs); 177 178 + ret = write_midx_file_only(source, &packs, 179 opts.preferred_pack, 180 opts.refs_snapshot, opts.flags); 181 ··· 186 187 } 188 189 + ret = write_midx_file(source, opts.preferred_pack, 190 opts.refs_snapshot, opts.flags); 191 192 free(opts.refs_snapshot); ··· 234 N_("force progress reporting"), MIDX_PROGRESS), 235 OPT_END(), 236 }; 237 + struct odb_source *source; 238 + 239 options = add_common_options(builtin_multi_pack_index_expire_options); 240 241 trace2_cmd_mode(argv[0]); ··· 248 if (argc) 249 usage_with_options(builtin_multi_pack_index_expire_usage, 250 options); 251 + source = handle_object_dir_option(the_repository); 252 253 FREE_AND_NULL(options); 254 255 + return expire_midx_packs(source, opts.flags); 256 } 257 258 static int cmd_multi_pack_index_repack(int argc, const char **argv, ··· 267 N_("force progress reporting"), MIDX_PROGRESS), 268 OPT_END(), 269 }; 270 + struct odb_source *source; 271 272 options = add_common_options(builtin_multi_pack_index_repack_options); 273 ··· 282 if (argc) 283 usage_with_options(builtin_multi_pack_index_repack_usage, 284 options); 285 + source = handle_object_dir_option(the_repository); 286 287 FREE_AND_NULL(options); 288 289 + return midx_repack(source, (size_t)opts.batch_size, opts.flags); 290 } 291 292 int cmd_multi_pack_index(int argc,
+1 -1
builtin/repack.c
··· 1711 unsigned flags = 0; 1712 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0)) 1713 flags |= MIDX_WRITE_INCREMENTAL; 1714 - write_midx_file(the_repository, repo_get_object_directory(the_repository), 1715 NULL, NULL, flags); 1716 } 1717
··· 1711 unsigned flags = 0; 1712 if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0)) 1713 flags |= MIDX_WRITE_INCREMENTAL; 1714 + write_midx_file(the_repository->objects->sources, 1715 NULL, NULL, flags); 1716 } 1717
+31 -36
midx-write.c
··· 913 return ret; 914 } 915 916 - static struct multi_pack_index *lookup_multi_pack_index(struct repository *r, 917 - const char *object_dir) 918 - { 919 - struct odb_source *source = odb_find_source_or_die(r->objects, object_dir); 920 - return get_multi_pack_index(source); 921 - } 922 - 923 static int fill_packs_from_midx(struct write_midx_context *ctx, 924 const char *preferred_pack_name, uint32_t flags) 925 { ··· 1010 return ret; 1011 } 1012 1013 - static void clear_midx_files(struct repository *r, const char *object_dir, 1014 const char **hashes, uint32_t hashes_nr, 1015 unsigned incremental) 1016 { ··· 1029 uint32_t i, j; 1030 1031 for (i = 0; i < ARRAY_SIZE(exts); i++) { 1032 - clear_incremental_midx_files_ext(object_dir, exts[i], 1033 hashes, hashes_nr); 1034 for (j = 0; j < hashes_nr; j++) 1035 - clear_midx_files_ext(object_dir, exts[i], hashes[j]); 1036 } 1037 1038 if (incremental) 1039 - get_midx_filename(r->hash_algo, &buf, object_dir); 1040 else 1041 - get_midx_chain_filename(&buf, object_dir); 1042 1043 if (unlink(buf.buf) && errno != ENOENT) 1044 die_errno(_("failed to clear multi-pack-index at %s"), buf.buf); ··· 1046 strbuf_release(&buf); 1047 } 1048 1049 - static int write_midx_internal(struct repository *r, const char *object_dir, 1050 struct string_list *packs_to_include, 1051 struct string_list *packs_to_drop, 1052 const char *preferred_pack_name, 1053 const char *refs_snapshot, 1054 unsigned flags) 1055 { 1056 struct strbuf midx_name = STRBUF_INIT; 1057 unsigned char midx_hash[GIT_MAX_RAWSZ]; 1058 uint32_t i, start_pack; ··· 1076 if (ctx.incremental) 1077 strbuf_addf(&midx_name, 1078 "%s/pack/multi-pack-index.d/tmp_midx_XXXXXX", 1079 - object_dir); 1080 else 1081 - get_midx_filename(r->hash_algo, &midx_name, object_dir); 1082 if (safe_create_leading_directories(r, midx_name.buf)) 1083 die_errno(_("unable to create leading directories of %s"), 1084 midx_name.buf); 1085 1086 if (!packs_to_include || ctx.incremental) { 1087 - struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir); 1088 if (m && !midx_checksum_valid(m)) { 1089 warning(_("ignoring existing multi-pack-index; checksum mismatch")); 1090 m = NULL; ··· 1138 1139 ctx.to_include = packs_to_include; 1140 1141 - for_each_file_in_pack_dir(object_dir, add_pack_to_midx, &ctx); 1142 stop_progress(&ctx.progress); 1143 1144 if ((ctx.m && ctx.nr == ctx.m->num_packs + ctx.m->num_packs_in_base) && ··· 1158 * corresponding bitmap (or one wasn't requested). 1159 */ 1160 if (!want_bitmap) 1161 - clear_midx_files_ext(object_dir, "bitmap", NULL); 1162 goto cleanup; 1163 } 1164 } ··· 1326 if (ctx.incremental) { 1327 struct strbuf lock_name = STRBUF_INIT; 1328 1329 - get_midx_chain_filename(&lock_name, object_dir); 1330 hold_lock_file_for_update(&lk, lock_name.buf, LOCK_DIE_ON_ERROR); 1331 strbuf_release(&lock_name); 1332 ··· 1389 1390 if (flags & MIDX_WRITE_REV_INDEX && 1391 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0)) 1392 - write_midx_reverse_index(&ctx, object_dir, midx_hash); 1393 1394 if (flags & MIDX_WRITE_BITMAP) { 1395 struct packing_data pdata; ··· 1412 FREE_AND_NULL(ctx.entries); 1413 ctx.entries_nr = 0; 1414 1415 - if (write_midx_bitmap(&ctx, object_dir, 1416 midx_hash, &pdata, commits, commits_nr, 1417 flags) < 0) { 1418 error(_("could not write multi-pack bitmap")); ··· 1446 return -1; 1447 1448 get_split_midx_filename_ext(r->hash_algo, &final_midx_name, 1449 - object_dir, midx_hash, MIDX_EXT_MIDX); 1450 1451 if (rename_tempfile(&incr, final_midx_name.buf) < 0) { 1452 error_errno(_("unable to rename new multi-pack-index layer")); ··· 1479 if (commit_lock_file(&lk) < 0) 1480 die_errno(_("could not write multi-pack-index")); 1481 1482 - clear_midx_files(r, object_dir, keep_hashes, 1483 ctx.num_multi_pack_indexes_before + 1, 1484 ctx.incremental); 1485 ··· 1508 return result; 1509 } 1510 1511 - int write_midx_file(struct repository *r, const char *object_dir, 1512 const char *preferred_pack_name, 1513 const char *refs_snapshot, unsigned flags) 1514 { 1515 - return write_midx_internal(r, object_dir, NULL, NULL, 1516 preferred_pack_name, refs_snapshot, 1517 flags); 1518 } 1519 1520 - int write_midx_file_only(struct repository *r, const char *object_dir, 1521 struct string_list *packs_to_include, 1522 const char *preferred_pack_name, 1523 const char *refs_snapshot, unsigned flags) 1524 { 1525 - return write_midx_internal(r, object_dir, packs_to_include, NULL, 1526 preferred_pack_name, refs_snapshot, flags); 1527 } 1528 1529 - int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags) 1530 { 1531 uint32_t i, *count, result = 0; 1532 struct string_list packs_to_drop = STRING_LIST_INIT_DUP; 1533 - struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir); 1534 struct progress *progress = NULL; 1535 1536 if (!m) ··· 1543 1544 if (flags & MIDX_PROGRESS) 1545 progress = start_delayed_progress( 1546 - r, 1547 _("Counting referenced objects"), 1548 m->num_objects); 1549 for (i = 0; i < m->num_objects; i++) { ··· 1555 1556 if (flags & MIDX_PROGRESS) 1557 progress = start_delayed_progress( 1558 - r, 1559 _("Finding and deleting unreferenced packfiles"), 1560 m->num_packs); 1561 for (i = 0; i < m->num_packs; i++) { ··· 1583 free(count); 1584 1585 if (packs_to_drop.nr) 1586 - result = write_midx_internal(r, object_dir, NULL, 1587 &packs_to_drop, NULL, NULL, flags); 1588 1589 string_list_clear(&packs_to_drop, 0); ··· 1708 free(pack_info); 1709 } 1710 1711 - int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags) 1712 { 1713 int result = 0; 1714 uint32_t i, packs_to_repack = 0; 1715 unsigned char *include_pack; 1716 struct child_process cmd = CHILD_PROCESS_INIT; 1717 FILE *cmd_in; 1718 - struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir); 1719 1720 /* 1721 * When updating the default for these configuration ··· 1749 1750 strvec_push(&cmd.args, "pack-objects"); 1751 1752 - strvec_pushf(&cmd.args, "%s/pack/pack", object_dir); 1753 1754 if (delta_base_offset) 1755 strvec_push(&cmd.args, "--delta-base-offset"); ··· 1790 goto cleanup; 1791 } 1792 1793 - result = write_midx_internal(r, object_dir, NULL, NULL, NULL, NULL, 1794 flags); 1795 1796 cleanup:
··· 913 return ret; 914 } 915 916 static int fill_packs_from_midx(struct write_midx_context *ctx, 917 const char *preferred_pack_name, uint32_t flags) 918 { ··· 1003 return ret; 1004 } 1005 1006 + static void clear_midx_files(struct odb_source *source, 1007 const char **hashes, uint32_t hashes_nr, 1008 unsigned incremental) 1009 { ··· 1022 uint32_t i, j; 1023 1024 for (i = 0; i < ARRAY_SIZE(exts); i++) { 1025 + clear_incremental_midx_files_ext(source->path, exts[i], 1026 hashes, hashes_nr); 1027 for (j = 0; j < hashes_nr; j++) 1028 + clear_midx_files_ext(source->path, exts[i], hashes[j]); 1029 } 1030 1031 if (incremental) 1032 + get_midx_filename(source->odb->repo->hash_algo, &buf, source->path); 1033 else 1034 + get_midx_chain_filename(&buf, source->path); 1035 1036 if (unlink(buf.buf) && errno != ENOENT) 1037 die_errno(_("failed to clear multi-pack-index at %s"), buf.buf); ··· 1039 strbuf_release(&buf); 1040 } 1041 1042 + static int write_midx_internal(struct odb_source *source, 1043 struct string_list *packs_to_include, 1044 struct string_list *packs_to_drop, 1045 const char *preferred_pack_name, 1046 const char *refs_snapshot, 1047 unsigned flags) 1048 { 1049 + struct repository *r = source->odb->repo; 1050 struct strbuf midx_name = STRBUF_INIT; 1051 unsigned char midx_hash[GIT_MAX_RAWSZ]; 1052 uint32_t i, start_pack; ··· 1070 if (ctx.incremental) 1071 strbuf_addf(&midx_name, 1072 "%s/pack/multi-pack-index.d/tmp_midx_XXXXXX", 1073 + source->path); 1074 else 1075 + get_midx_filename(r->hash_algo, &midx_name, source->path); 1076 if (safe_create_leading_directories(r, midx_name.buf)) 1077 die_errno(_("unable to create leading directories of %s"), 1078 midx_name.buf); 1079 1080 if (!packs_to_include || ctx.incremental) { 1081 + struct multi_pack_index *m = get_multi_pack_index(source); 1082 if (m && !midx_checksum_valid(m)) { 1083 warning(_("ignoring existing multi-pack-index; checksum mismatch")); 1084 m = NULL; ··· 1132 1133 ctx.to_include = packs_to_include; 1134 1135 + for_each_file_in_pack_dir(source->path, add_pack_to_midx, &ctx); 1136 stop_progress(&ctx.progress); 1137 1138 if ((ctx.m && ctx.nr == ctx.m->num_packs + ctx.m->num_packs_in_base) && ··· 1152 * corresponding bitmap (or one wasn't requested). 1153 */ 1154 if (!want_bitmap) 1155 + clear_midx_files_ext(source->path, "bitmap", NULL); 1156 goto cleanup; 1157 } 1158 } ··· 1320 if (ctx.incremental) { 1321 struct strbuf lock_name = STRBUF_INIT; 1322 1323 + get_midx_chain_filename(&lock_name, source->path); 1324 hold_lock_file_for_update(&lk, lock_name.buf, LOCK_DIE_ON_ERROR); 1325 strbuf_release(&lock_name); 1326 ··· 1383 1384 if (flags & MIDX_WRITE_REV_INDEX && 1385 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0)) 1386 + write_midx_reverse_index(&ctx, source->path, midx_hash); 1387 1388 if (flags & MIDX_WRITE_BITMAP) { 1389 struct packing_data pdata; ··· 1406 FREE_AND_NULL(ctx.entries); 1407 ctx.entries_nr = 0; 1408 1409 + if (write_midx_bitmap(&ctx, source->path, 1410 midx_hash, &pdata, commits, commits_nr, 1411 flags) < 0) { 1412 error(_("could not write multi-pack bitmap")); ··· 1440 return -1; 1441 1442 get_split_midx_filename_ext(r->hash_algo, &final_midx_name, 1443 + source->path, midx_hash, MIDX_EXT_MIDX); 1444 1445 if (rename_tempfile(&incr, final_midx_name.buf) < 0) { 1446 error_errno(_("unable to rename new multi-pack-index layer")); ··· 1473 if (commit_lock_file(&lk) < 0) 1474 die_errno(_("could not write multi-pack-index")); 1475 1476 + clear_midx_files(source, keep_hashes, 1477 ctx.num_multi_pack_indexes_before + 1, 1478 ctx.incremental); 1479 ··· 1502 return result; 1503 } 1504 1505 + int write_midx_file(struct odb_source *source, 1506 const char *preferred_pack_name, 1507 const char *refs_snapshot, unsigned flags) 1508 { 1509 + return write_midx_internal(source, NULL, NULL, 1510 preferred_pack_name, refs_snapshot, 1511 flags); 1512 } 1513 1514 + int write_midx_file_only(struct odb_source *source, 1515 struct string_list *packs_to_include, 1516 const char *preferred_pack_name, 1517 const char *refs_snapshot, unsigned flags) 1518 { 1519 + return write_midx_internal(source, packs_to_include, NULL, 1520 preferred_pack_name, refs_snapshot, flags); 1521 } 1522 1523 + int expire_midx_packs(struct odb_source *source, unsigned flags) 1524 { 1525 uint32_t i, *count, result = 0; 1526 struct string_list packs_to_drop = STRING_LIST_INIT_DUP; 1527 + struct multi_pack_index *m = get_multi_pack_index(source); 1528 struct progress *progress = NULL; 1529 1530 if (!m) ··· 1537 1538 if (flags & MIDX_PROGRESS) 1539 progress = start_delayed_progress( 1540 + source->odb->repo, 1541 _("Counting referenced objects"), 1542 m->num_objects); 1543 for (i = 0; i < m->num_objects; i++) { ··· 1549 1550 if (flags & MIDX_PROGRESS) 1551 progress = start_delayed_progress( 1552 + source->odb->repo, 1553 _("Finding and deleting unreferenced packfiles"), 1554 m->num_packs); 1555 for (i = 0; i < m->num_packs; i++) { ··· 1577 free(count); 1578 1579 if (packs_to_drop.nr) 1580 + result = write_midx_internal(source, NULL, 1581 &packs_to_drop, NULL, NULL, flags); 1582 1583 string_list_clear(&packs_to_drop, 0); ··· 1702 free(pack_info); 1703 } 1704 1705 + int midx_repack(struct odb_source *source, size_t batch_size, unsigned flags) 1706 { 1707 + struct repository *r = source->odb->repo; 1708 int result = 0; 1709 uint32_t i, packs_to_repack = 0; 1710 unsigned char *include_pack; 1711 struct child_process cmd = CHILD_PROCESS_INIT; 1712 FILE *cmd_in; 1713 + struct multi_pack_index *m = get_multi_pack_index(source); 1714 1715 /* 1716 * When updating the default for these configuration ··· 1744 1745 strvec_push(&cmd.args, "pack-objects"); 1746 1747 + strvec_pushf(&cmd.args, "%s/pack/pack", source->path); 1748 1749 if (delta_base_offset) 1750 strvec_push(&cmd.args, "--delta-base-offset"); ··· 1785 goto cleanup; 1786 } 1787 1788 + result = write_midx_internal(source, NULL, NULL, NULL, NULL, 1789 flags); 1790 1791 cleanup:
+4 -4
midx.h
··· 126 * Variant of write_midx_file which writes a MIDX containing only the packs 127 * specified in packs_to_include. 128 */ 129 - int write_midx_file(struct repository *r, const char *object_dir, 130 const char *preferred_pack_name, const char *refs_snapshot, 131 unsigned flags); 132 - int write_midx_file_only(struct repository *r, const char *object_dir, 133 struct string_list *packs_to_include, 134 const char *preferred_pack_name, 135 const char *refs_snapshot, unsigned flags); 136 void clear_midx_file(struct repository *r); 137 int verify_midx_file(struct odb_source *source, unsigned flags); 138 - int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags); 139 - int midx_repack(struct repository *r, const char *object_dir, size_t batch_size, unsigned flags); 140 141 void close_midx(struct multi_pack_index *m); 142
··· 126 * Variant of write_midx_file which writes a MIDX containing only the packs 127 * specified in packs_to_include. 128 */ 129 + int write_midx_file(struct odb_source *source, 130 const char *preferred_pack_name, const char *refs_snapshot, 131 unsigned flags); 132 + int write_midx_file_only(struct odb_source *source, 133 struct string_list *packs_to_include, 134 const char *preferred_pack_name, 135 const char *refs_snapshot, unsigned flags); 136 void clear_midx_file(struct repository *r); 137 int verify_midx_file(struct odb_source *source, unsigned flags); 138 + int expire_midx_packs(struct odb_source *source, unsigned flags); 139 + int midx_repack(struct odb_source *source, size_t batch_size, unsigned flags); 140 141 void close_midx(struct multi_pack_index *m); 142