odb/source: make `freshen_object()` 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
6a38b13fbac73d1a1982f9211c5c3e64e1191696
3 files changed
+36
-10
odb.c
+2
-10
@@ -959,18 +959,10 @@ int odb_freshen_object(struct object_database *odb,
959
const struct object_id *oid)
960
{
961
struct odb_source *source;
962
-
962
odb_prepare_alternates(odb);
964
- for (source = odb->sources; source; source = source->next) {
965
- struct odb_source_files *files = odb_source_files_downcast(source);
966
-
967
- if (packfile_store_freshen_object(files->packed, oid))
963
+ for (source = odb->sources; source; source = source->next)
964
+ if (odb_source_freshen_object(source, oid))
965
return 1;
969
-
970
- if (odb_source_loose_freshen_object(source, oid))
971
- return 1;
972
- }
973
-
966
return 0;
967
}
968
odb/source-files.c
+11
@@ -88,6 +88,16 @@ static int odb_source_files_for_each_object(struct odb_source *source,
88
return 0;
89
}
90
91
+static int odb_source_files_freshen_object(struct odb_source *source,
92
+ const struct object_id *oid)
93
+{
94
+ struct odb_source_files *files = odb_source_files_downcast(source);
95
+ if (packfile_store_freshen_object(files->packed, oid) ||
96
+ odb_source_loose_freshen_object(source, oid))
97
+ return 1;
98
+ return 0;
99
+}
100
+
101
struct odb_source_files *odb_source_files_new(struct object_database *odb,
102
const char *path,
103
bool local)
@@ -105,6 +115,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
115
files->base.read_object_info = odb_source_files_read_object_info;
116
files->base.read_object_stream = odb_source_files_read_object_stream;
117
files->base.for_each_object = odb_source_files_for_each_object;
118
+ files->base.freshen_object = odb_source_files_freshen_object;
119
120
/*
121
* Ideally, we would only ever store absolute paths in the source. This
odb/source.h
+23
@@ -186,6 +186,18 @@ struct odb_source {
186
odb_for_each_object_cb cb,
187
void *cb_data,
188
unsigned flags);
189
+
190
+ /*
191
+ * This callback is expected to freshen the given object so that its
192
+ * last access time is set to the current time. This is used to ensure
193
+ * that objects that are recent will not get garbage collected even if
194
+ * they were unreachable.
195
+ *
196
+ * Returns 0 in case the object does not exist, 1 in case the object
197
+ * has been freshened.
198
+ */
199
+ int (*freshen_object)(struct odb_source *source,
200
+ const struct object_id *oid);
201
};
202
203
/*
@@ -297,4 +309,15 @@ static inline int odb_source_for_each_object(struct odb_source *source,
309
return source->for_each_object(source, request, cb, cb_data, flags);
310
}
311
312
+/*
313
+ * Freshen an object in the object database by updating its timestamp.
314
+ * Returns 1 in case the object has been freshened, 0 in case the object does
315
+ * not exist.
316
+ */
317
+static inline int odb_source_freshen_object(struct odb_source *source,
318
+ const struct object_id *oid)
319
+{
320
+ return source->freshen_object(source, oid);
321
+}
322
+
323
#endif