object-file: hide internals when we need to reprepare loose sources

There are two different situations where we have to clear the cache of loose objects: - When freeing the loose object source itself to avoid memory leaks. - When repreparing the loose object source so that any potentially- stale data is getting evicted from the cache. The former is already handled by `odb_source_loose_free()`. But the latter case is still done manually by in `odb_reprepare()`, so we are leaking internals into that code. Introduce a new `odb_source_loose_reprepare()` function as an equivalent to `packfile_store_prepare()` to hide these implementation details. Furthermore, while at it, rename the function `odb_clear_loose_cache()` to `odb_source_loose_clear()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Nov 3, 2025 at 08:42 UTC be659c97eae3b68e38b71f0a67067dede23903b5
3 files changed +15 -10
object-file.c
+11 -6
@@ -1834,12 +1834,17 @@ struct oidtree *odb_source_loose_cache(struct odb_source *source,
1834 return source->loose->cache;
1835 }
1836
1837 -void odb_clear_loose_cache(struct odb_source *source)
1837 +static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
1838 {
1839 - oidtree_clear(source->loose->cache);
1840 - FREE_AND_NULL(source->loose->cache);
1841 - memset(&source->loose->subdir_seen, 0,
1842 - sizeof(source->loose->subdir_seen));
1839 + oidtree_clear(loose->cache);
1840 + FREE_AND_NULL(loose->cache);
1841 + memset(&loose->subdir_seen, 0,
1842 + sizeof(loose->subdir_seen));
1843 +}
1844 +
1845 +void odb_source_loose_reprepare(struct odb_source *source)
1846 +{
1847 + odb_source_loose_clear_cache(source->loose);
1848 }
1849
1850 static int check_stream_oid(git_zstream *stream,
@@ -2008,6 +2013,6 @@ void odb_source_loose_free(struct odb_source_loose *loose)
2013 {
2014 if (!loose)
2015 return;
2011 - odb_clear_loose_cache(loose->source);
2016 + odb_source_loose_clear_cache(loose);
2017 free(loose);
2018 }
object-file.h
+3 -3
@@ -37,6 +37,9 @@ struct odb_source_loose {
37 struct odb_source_loose *odb_source_loose_new(struct odb_source *source);
38 void odb_source_loose_free(struct odb_source_loose *loose);
39
40 +/* Reprepare the loose source by emptying the loose object cache. */
41 +void odb_source_loose_reprepare(struct odb_source *source);
42 +
43 /*
44 * Populate and return the loose object cache array corresponding to the
45 * given object ID.
@@ -44,9 +47,6 @@ void odb_source_loose_free(struct odb_source_loose *loose);
47 struct oidtree *odb_source_loose_cache(struct odb_source *source,
48 const struct object_id *oid);
49
47 -/* Empty the loose object cache for the specified object directory. */
48 -void odb_clear_loose_cache(struct odb_source *source);
49 -
50 /*
51 * Put in `buf` the name of the file in the local object database that
52 * would be used to store a loose object with the specified oid.
odb.c
+1 -1
@@ -1071,7 +1071,7 @@ void odb_reprepare(struct object_database *o)
1071 odb_prepare_alternates(o);
1072
1073 for (source = o->sources; source; source = source->next)
1074 - odb_clear_loose_cache(source);
1074 + odb_source_loose_reprepare(source);
1075
1076 o->approximate_object_count_valid = 0;
1077