]> git.ipfire.org Git - thirdparty/git.git/commitdiff
midx-write.c: extract inner loop from fill_packs_from_midx()
authorTaylor Blau <me@ttaylorr.com>
Wed, 28 May 2025 22:59:06 +0000 (18:59 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 29 May 2025 19:53:47 +0000 (12:53 -0700)
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 <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
midx-write.c

index e4a3830d45db29412b7db2a46f4e0afbb6b4cd58..ca2384e291c5c02fc144504a60f217331a75ac64 100644 (file)
@@ -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;
 }