]> git.ipfire.org Git - thirdparty/git.git/commitdiff
packfile: always add packfiles to MRU when adding a pack
authorPatrick Steinhardt <ps@pks.im>
Thu, 30 Oct 2025 10:38:44 +0000 (11:38 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Oct 2025 14:09:53 +0000 (07:09 -0700)
When preparing the packfile store we know to also prepare the MRU list
of packfiles with all packs that are currently loaded in the store via
`packfile_store_prepare_mru()`. So we know that the list of packs in the
MRU list should match the list of packs in the non-MRU list.

But there are some direct or indirect callsites that add a packfile to
the store via `packfile_store_add_pack()` without adding the pack to the
MRU. And while functions that access the MRU (e.g. `find_pack_entry()`)
know to call `packfile_store_prepare()`, which knows to prepare the MRU
via `packfile_store_prepare_mru()`, that operation will be turned into a
no-op because the packfile store is already prepared. So this will not
cause us to add the packfile to the MRU, and consequently we won't be
able to find the packfile in our MRU list.

There are only a handful of callers outside of "packfile.c" that add a
packfile to the store:

  - "builtin/fast-import.c" adds multiple packs of imported objects, but
    it knows to look up objects via `packfile_store_get_packs()`. This
    function does not use the MRU, so we're good.

  - "builtin/index-pack.c" adds the indexed pack to the store in case it
    needs to perform consistency checks on its objects.

  - "http.c" adds the fetched pack to the store so that we can access
    its objects.

In all of these cases we actually want to access the contained objects.
And luckily, reading these objects works as expected:

  1. We eventually end up in `do_oid_object_info_extended()`.

  2. Calling `find_pack_entry()` fails because the MRU list doesn't
     contain the newly added packfile.

  3. The callers don't pass `OBJECT_INFO_QUICK`, so we end up
     repreparing the object database. This will also cause us to
     reprepare the MRU list.

  4. We now retry reading the object via `find_pack_entry()`, and now we
     succeed because the MRU list got populated.

This logic feels quite fragile: we intentionally add the packfile to the
store, but we then ultimately rely on repreparing the entire store only
to make the packfile accessible. While we do the correct thing in
`do_oid_object_info_extended()`, other sites that access the MRU may not
know to reprepare.

But besides being fragile it's also a waste of resources: repreparing
the object database requires us to re-read the alternates file and
discard any caches.

Refactor the code so that we unconditionally add packfiles to the MRU
when adding them to a packfile store. This makes the logic less fragile
and ensures that we don't have to reprepare the store to make the pack
accessible.

Note that this does not allow us to drop `packfile_store_prepare_mru()`
just yet: while the MRU list is already populated with all packs now,
the order in which we add these packs is indeterministic for most of the
part. So by first calling `sort_pack()` on the other packfile list and
then re-preparing the MRU list we inherit its sorting.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
midx.c
packfile.c

diff --git a/midx.c b/midx.c
index 8022be9a45ecb981a54d9e0ed573d0b3d020d4a8..24e1e721754d0cbffed53d7545f12bca77d157bd 100644 (file)
--- a/midx.c
+++ b/midx.c
@@ -462,8 +462,6 @@ int prepare_midx_pack(struct multi_pack_index *m,
                    m->pack_names[pack_int_id]);
        p = packfile_store_load_pack(r->objects->packfiles,
                                     pack_name.buf, m->source->local);
-       if (p)
-               packfile_list_append(&m->source->odb->packfiles->mru, p);
        strbuf_release(&pack_name);
 
        if (!p) {
index 71e95ae11c56d225b3ccb1763d9c3cc3b1ba5a3a..60f2e42876a82372ff2f17afd5f1214c04018d80 100644 (file)
@@ -871,6 +871,7 @@ void packfile_store_add_pack(struct packfile_store *store,
                pack_open_fds++;
 
        packfile_list_prepend(&store->packs, pack);
+       packfile_list_append(&store->mru, pack);
 
        strmap_put(&store->packs_by_path, pack->pack_name, pack);
 }