]> git.ipfire.org Git - thirdparty/git.git/commit
builtin/pack-objects: simplify logic to find kept or nonlocal objects
authorPatrick Steinhardt <ps@pks.im>
Thu, 30 Oct 2025 10:38:42 +0000 (11:38 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Oct 2025 14:09:53 +0000 (07:09 -0700)
commit0d0e4b5954e97fcdfd0a4120e17e2c570497981a
tree387d04f43ff2522d03248f58e20c63c864a7fff4
parent02a7f6ffab9ec7641f88032f30998976bca07820
builtin/pack-objects: simplify logic to find kept or nonlocal objects

The function `has_sha1_pack_kept_or_nonlocal()` takes an object ID and
then searches through packed objects to figure out whether the object
exists in a kept or non-local pack. As a performance optimization we
remember the packfile that contains a given object ID so that the next
call to the function first checks that same packfile again.

The way this is written is rather hard to follow though, as the caching
mechanism is intertwined with the loop that iterates through the packs.
Consequently, we need to do some gymnastics to re-start the iteration if
the cached pack does not contain the objects.

Refactor this so that we check the cached packfile at the beginning. We
don't have to re-verify whether the packfile meets the properties as we
have already verified those when storing the pack in `last_found` in the
first place. So all we need to do is to use `find_pack_entry_one()` to
check whether the pack contains the object ID, and to skip the cached
pack in the loop so that we don't search it twice.

Furthermore, stop using the `(void *)1` sentinel value and instead use a
simple `NULL` pointer to indicate that we don't have a last-found pack
yet.

This refactoring significantly simplifies the logic and makes it much
easier to follow.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/pack-objects.c