From: Taylor Blau Date: Wed, 28 May 2025 22:59:06 +0000 (-0400) Subject: midx-write.c: extract inner loop from fill_packs_from_midx() X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5dff093ec0763cc3bcc083c9ebd04e4e119f086c;p=thirdparty%2Fgit.git midx-write.c: extract inner loop from fill_packs_from_midx() The function fill_packs_from_midx() does relatively little, but ends up in a doubly-nested loop because we're enumerating each pack within each layer of the incremental MIDX chain. Let's de-dent the inner loop of fill_packs_from_midx() by extracting its contents into a separate function, and calling that. Signed-off-by: Taylor Blau Signed-off-by: Junio C Hamano --- diff --git a/midx-write.c b/midx-write.c index e4a3830d45..ca2384e291 100644 --- a/midx-write.c +++ b/midx-write.c @@ -938,44 +938,54 @@ cleanup: return result; } +static int fill_packs_from_midx_1(struct write_midx_context *ctx, + struct multi_pack_index *m, + int prepare_packs) +{ + for (uint32_t i = 0; i < m->num_packs; i++) { + /* + * If generating a reverse index, need to have + * packed_git's loaded to compare their + * mtimes and object count. + */ + if (prepare_packs) { + if (prepare_midx_pack(ctx->repo, m, + m->num_packs_in_base + i)) { + error(_("could not load pack")); + return 1; + } + + if (open_pack_index(m->packs[i])) + die(_("could not open index for %s"), + m->packs[i]->pack_name); + } + + fill_pack_info(&ctx->info[ctx->nr++], m->packs[i], + m->pack_names[i], + m->num_packs_in_base + i); + } + + return 0; +} + static int fill_packs_from_midx(struct write_midx_context *ctx, const char *preferred_pack_name, uint32_t flags) { struct multi_pack_index *m; + int prepare_packs; - for (m = ctx->m; m; m = m->base_midx) { - uint32_t i; - - for (i = 0; i < m->num_packs; i++) { - ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc); - - /* - * If generating a reverse index, need to have - * packed_git's loaded to compare their - * mtimes and object count. - * - * If a preferred pack is specified, need to - * have packed_git's loaded to ensure the chosen - * preferred pack has a non-zero object count. - */ - if (flags & MIDX_WRITE_REV_INDEX || - preferred_pack_name) { - if (prepare_midx_pack(ctx->repo, m, - m->num_packs_in_base + i)) { - error(_("could not load pack")); - return 1; - } - - if (open_pack_index(m->packs[i])) - die(_("could not open index for %s"), - m->packs[i]->pack_name); - } + /* + * If generating a reverse index, need to have packed_git's + * loaded to compare their mtimes and object count. + */ + prepare_packs = !!(flags & MIDX_WRITE_REV_INDEX || preferred_pack_name); - fill_pack_info(&ctx->info[ctx->nr++], m->packs[i], - m->pack_names[i], - m->num_packs_in_base + i); - } + for (m = ctx->m; m; m = m->base_midx) { + int ret = fill_packs_from_midx_1(ctx, m, prepare_packs); + if (ret) + return ret; } + return 0; }