midx: teach `prepare_midx_pack()` about incremental MIDXs

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>

Taylor Blau committed Aug 6, 2024 at 11:37 UTC 1820bd878c623a71ffc719ff09071fc6bbc6e34e
1 file changed +22 -4
midx.c
+22 -4
@@ -259,14 +259,32 @@ static uint32_t midx_for_object(struct multi_pack_index **_m, uint32_t pos)
259 return pos - m->num_objects_in_base;
260 }
261
262 -int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id)
262 +static uint32_t midx_for_pack(struct multi_pack_index **_m,
263 + uint32_t pack_int_id)
264 +{
265 + struct multi_pack_index *m = *_m;
266 + while (m && pack_int_id < m->num_packs_in_base)
267 + m = m->base_midx;
268 +
269 + if (!m)
270 + BUG("NULL multi-pack-index for pack ID: %"PRIu32, pack_int_id);
271 +
272 + if (pack_int_id >= m->num_packs + m->num_packs_in_base)
273 + die(_("bad pack-int-id: %u (%u total packs)"),
274 + pack_int_id, m->num_packs + m->num_packs_in_base);
275 +
276 + *_m = m;
277 +
278 + return pack_int_id - m->num_packs_in_base;
279 +}
280 +
281 +int prepare_midx_pack(struct repository *r, struct multi_pack_index *m,
282 + uint32_t pack_int_id)
283 {
284 struct strbuf pack_name = STRBUF_INIT;
285 struct packed_git *p;
286
267 - if (pack_int_id >= m->num_packs)
268 - die(_("bad pack-int-id: %u (%u total packs)"),
269 - pack_int_id, m->num_packs);
287 + pack_int_id = midx_for_pack(&m, pack_int_id);
288
289 if (m->packs[pack_int_id])
290 return 0;