odb/source: make `reprepare()` function pluggable

Introduce a new callback function in `struct odb_source` to make the function pluggable. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 5, 2026 at 15:19 UTC 05151cf3602d5ebaaa4e04c415e8481cb1c7ddf6
3 files changed +27 -5
odb.c
+2 -5
@@ -1119,11 +1119,8 @@ void odb_reprepare(struct object_database *o)
1119 o->loaded_alternates = 0;
1120 odb_prepare_alternates(o);
1121
1122 - for (source = o->sources; source; source = source->next) {
1123 - struct odb_source_files *files = odb_source_files_downcast(source);
1124 - odb_source_loose_reprepare(source);
1125 - packfile_store_reprepare(files->packed);
1126 - }
1122 + for (source = o->sources; source; source = source->next)
1123 + odb_source_reprepare(source);
1124
1125 o->approximate_object_count_valid = 0;
1126
odb/source-files.c
+8
@@ -28,6 +28,13 @@ static void odb_source_files_free(struct odb_source *source)
28 free(files);
29 }
30
31 +static void odb_source_files_reprepare(struct odb_source *source)
32 +{
33 + struct odb_source_files *files = odb_source_files_downcast(source);
34 + odb_source_loose_reprepare(&files->base);
35 + packfile_store_reprepare(files->packed);
36 +}
37 +
38 struct odb_source_files *odb_source_files_new(struct object_database *odb,
39 const char *path,
40 bool local)
@@ -40,6 +47,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
47 files->packed = packfile_store_new(&files->base);
48
49 files->base.free = odb_source_files_free;
50 + files->base.reprepare = odb_source_files_reprepare;
51
52 /*
53 * Ideally, we would only ever store absolute paths in the source. This
odb/source.h
+17
@@ -57,6 +57,13 @@ struct odb_source {
57 * all associated resources. The function will never be called with a NULL pointer.
58 */
59 void (*free)(struct odb_source *source);
60 +
61 + /*
62 + * This callback is expected to clear underlying caches of the object
63 + * database source. The function is called when the repository has for
64 + * example just been repacked so that new objects will become visible.
65 + */
66 + void (*reprepare)(struct odb_source *source);
67 };
68
69 /*
@@ -96,4 +103,14 @@ void odb_source_free(struct odb_source *source);
103 */
104 void odb_source_release(struct odb_source *source);
105
106 +/*
107 + * Reprepare the object database source and clear any caches. Depending on the
108 + * backend used this may have the effect that concurrently-written objects
109 + * become visible.
110 + */
111 +static inline void odb_source_reprepare(struct odb_source *source)
112 +{
113 + source->reprepare(source);
114 +}
115 +
116 #endif