midx-write.c: extract `fill_packs_from_midx()`

When write_midx_internal() loads an existing MIDX, all packs are copied forward into the new MIDX. Improve the readability of write_midx_internal() by extracting this functionality out into a separate function. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 29, 2024 at 18:55 UTC c5e204af1f395f38724aff08c3440aa86fdd9657
1 file changed +38 -30
midx-write.c
+38 -30
@@ -882,6 +882,40 @@ cleanup:
882 return result;
883 }
884
885 +static int fill_packs_from_midx(struct write_midx_context *ctx,
886 + const char *preferred_pack_name, uint32_t flags)
887 +{
888 + uint32_t i;
889 +
890 + for (i = 0; i < ctx->m->num_packs; i++) {
891 + ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
892 +
893 + if (flags & MIDX_WRITE_REV_INDEX || preferred_pack_name) {
894 + /*
895 + * If generating a reverse index, need to have
896 + * packed_git's loaded to compare their
897 + * mtimes and object count.
898 + *
899 + *
900 + * If a preferred pack is specified, need to
901 + * have packed_git's loaded to ensure the chosen
902 + * preferred pack has a non-zero object count.
903 + */
904 + if (prepare_midx_pack(the_repository, ctx->m, i))
905 + return error(_("could not load pack"));
906 +
907 + if (open_pack_index(ctx->m->packs[i]))
908 + die(_("could not open index for %s"),
909 + ctx->m->packs[i]->pack_name);
910 + }
911 +
912 + fill_pack_info(&ctx->info[ctx->nr++], ctx->m->packs[i],
913 + ctx->m->pack_names[i], i);
914 + }
915 +
916 + return 0;
917 +}
918 +
919 static int write_midx_internal(const char *object_dir,
920 struct string_list *packs_to_include,
921 struct string_list *packs_to_drop,
@@ -927,36 +961,10 @@ static int write_midx_internal(const char *object_dir,
961 ctx.info = NULL;
962 ALLOC_ARRAY(ctx.info, ctx.alloc);
963
930 - if (ctx.m) {
931 - for (i = 0; i < ctx.m->num_packs; i++) {
932 - ALLOC_GROW(ctx.info, ctx.nr + 1, ctx.alloc);
933 -
934 - if (flags & MIDX_WRITE_REV_INDEX ||
935 - preferred_pack_name) {
936 - /*
937 - * If generating a reverse index, need to have
938 - * packed_git's loaded to compare their
939 - * mtimes and object count.
940 - *
941 - * If a preferred pack is specified,
942 - * need to have packed_git's loaded to
943 - * ensure the chosen preferred pack has
944 - * a non-zero object count.
945 - */
946 - if (prepare_midx_pack(the_repository, ctx.m, i)) {
947 - error(_("could not load pack"));
948 - result = 1;
949 - goto cleanup;
950 - }
951 -
952 - if (open_pack_index(ctx.m->packs[i]))
953 - die(_("could not open index for %s"),
954 - ctx.m->packs[i]->pack_name);
955 - }
956 -
957 - fill_pack_info(&ctx.info[ctx.nr++], ctx.m->packs[i],
958 - ctx.m->pack_names[i], i);
959 - }
964 + if (ctx.m && fill_packs_from_midx(&ctx, preferred_pack_name,
965 + flags) < 0) {
966 + result = 1;
967 + goto cleanup;
968 }
969
970 start_pack = ctx.nr;