]> git.ipfire.org Git - thirdparty/git.git/commitdiff
midx: teach `prepare_midx_pack()` about incremental MIDXs
authorTaylor Blau <me@ttaylorr.com>
Tue, 6 Aug 2024 15:37:21 +0000 (11:37 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 6 Aug 2024 19:01:36 +0000 (12:01 -0700)
The function `prepare_midx_pack()` is part of the midx.h API and
loads the pack identified by the MIDX-local 'pack_int_id'. This patch
prepares that function to be aware of an incremental MIDX world.

To do this, introduce the second of the two general purpose helpers
mentioned in the previous commit. This commit introduces
`midx_for_pack()`, which is the pack-specific analog of
`midx_for_object()`, and works in the same fashion.

Like `midx_for_object()`, this function chases down the '->base_midx'
field until it finds the MIDX layer within the chain that contains the
given pack.

Use this function within `prepare_midx_pack()` so that the `pack_int_id`
it expects is now relative to the entire MIDX chain, and that it
prepares the given pack in the appropriate MIDX.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
midx.c

diff --git a/midx.c b/midx.c
index 39d358da2005aa18505ac1550d36895af97e7f60..07b3981a7a05845f93969af3d0021d80a9f32690 100644 (file)
--- a/midx.c
+++ b/midx.c
@@ -259,14 +259,32 @@ static uint32_t midx_for_object(struct multi_pack_index **_m, uint32_t pos)
        return pos - m->num_objects_in_base;
 }
 
-int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
+static uint32_t midx_for_pack(struct multi_pack_index **_m,
+                             uint32_t pack_int_id)
+{
+       struct multi_pack_index *m = *_m;
+       while (m && pack_int_id < m->num_packs_in_base)
+               m = m->base_midx;
+
+       if (!m)
+               BUG("NULL multi-pack-index for pack ID: %"PRIu32, pack_int_id);
+
+       if (pack_int_id >= m->num_packs + m->num_packs_in_base)
+               die(_("bad pack-int-id: %u (%u total packs)"),
+                   pack_int_id, m->num_packs + m->num_packs_in_base);
+
+       *_m = m;
+
+       return pack_int_id - m->num_packs_in_base;
+}
+
+int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
+                     uint32_t pack_int_id)
 {
        struct strbuf pack_name = STRBUF_INIT;
        struct packed_git *p;
 
-       if (pack_int_id >= m->num_packs)
-               die(_("bad pack-int-id: %u (%u total packs)"),
-                   pack_int_id, m->num_packs);
+       pack_int_id = midx_for_pack(&m, pack_int_id);
 
        if (m->packs[pack_int_id])
                return 0;