midx-write: extract function to test whether MIDX needs updating

In `write_midx_internal()` we know to skip writing the new multi-pack index in case it would be the same as the existing one. This logic does not handle the `--stdin-packs` option yet though, so we end up always rewriting the MIDX if that option is passed to us. Extract the logic to decide whether or not to rewrite the MIDX into a separate function. This will allow us to extend that feature in the next commit to address the above issue. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 10, 2025 at 13:52 UTC b3bab9d2729fde1f52c407447711c34a75c5c377
1 file changed +36 -3
midx-write.c
+36 -3
@@ -1015,6 +1015,41 @@ static void clear_midx_files(struct odb_source *source,
1015 strbuf_release(&buf);
1016 }
1017
1018 +static bool midx_needs_update(struct write_midx_context *ctx)
1019 +{
1020 + struct multi_pack_index *midx = ctx->m;
1021 + bool needed = true;
1022 +
1023 + /*
1024 + * Ignore incremental updates for now. The assumption is that any
1025 + * incremental update would be either empty (in which case we will bail
1026 + * out later) or it would actually cover at least one new pack.
1027 + */
1028 + if (ctx->incremental)
1029 + goto out;
1030 +
1031 + /*
1032 + * If there is no MIDX then either it doesn't exist, or we're doing a
1033 + * geometric repack. We cannot (yet) determine whether we need to
1034 + * update the multi-pack index in the second case.
1035 + */
1036 + if (!midx)
1037 + goto out;
1038 +
1039 + /*
1040 + * Otherwise, we need to verify that the packs covered by the existing
1041 + * MIDX match the packs that we already have. This test is somewhat
1042 + * lenient and will be fixed.
1043 + */
1044 + if (ctx->nr != midx->num_packs + midx->num_packs_in_base)
1045 + goto out;
1046 +
1047 + needed = false;
1048 +
1049 +out:
1050 + return needed;
1051 +}
1052 +
1053 static int write_midx_internal(struct odb_source *source,
1054 struct string_list *packs_to_include,
1055 struct string_list *packs_to_drop,
@@ -1112,9 +1147,7 @@ static int write_midx_internal(struct odb_source *source,
1147 for_each_file_in_pack_dir(source->path, add_pack_to_midx, &ctx);
1148 stop_progress(&ctx.progress);
1149
1115 - if ((ctx.m && ctx.nr == ctx.m->num_packs + ctx.m->num_packs_in_base) &&
1116 - !ctx.incremental &&
1117 - !(packs_to_include || packs_to_drop)) {
1150 + if (!packs_to_include && !packs_to_drop && !midx_needs_update(&ctx)) {
1151 struct bitmap_index *bitmap_git;
1152 int bitmap_exists;
1153 int want_bitmap = flags & MIDX_WRITE_BITMAP;