]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source: make `freshen_object()` function pluggable
authorPatrick Steinhardt <ps@pks.im>
Thu, 5 Mar 2026 14:19:52 +0000 (15:19 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Mar 2026 19:45:16 +0000 (11:45 -0800)
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>
odb.c
odb/source-files.c
odb/source.h

diff --git a/odb.c b/odb.c
index 494a3273cf77873eebb0348072f398cc3d6105fc..c9f42c5afd0b4f8c03a260a3cf3f7af9ce872dbe 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -959,18 +959,10 @@ int odb_freshen_object(struct object_database *odb,
                       const struct object_id *oid)
 {
        struct odb_source *source;
-
        odb_prepare_alternates(odb);
-       for (source = odb->sources; source; source = source->next) {
-               struct odb_source_files *files = odb_source_files_downcast(source);
-
-               if (packfile_store_freshen_object(files->packed, oid))
+       for (source = odb->sources; source; source = source->next)
+               if (odb_source_freshen_object(source, oid))
                        return 1;
-
-               if (odb_source_loose_freshen_object(source, oid))
-                       return 1;
-       }
-
        return 0;
 }
 
index d8ef1d82378c83e19a991d84d9642792b832dfc9..a6447909e01bbb3baec6218c4c6fb40eb951dd90 100644 (file)
@@ -88,6 +88,16 @@ static int odb_source_files_for_each_object(struct odb_source *source,
        return 0;
 }
 
+static int odb_source_files_freshen_object(struct odb_source *source,
+                                          const struct object_id *oid)
+{
+       struct odb_source_files *files = odb_source_files_downcast(source);
+       if (packfile_store_freshen_object(files->packed, oid) ||
+           odb_source_loose_freshen_object(source, oid))
+               return 1;
+       return 0;
+}
+
 struct odb_source_files *odb_source_files_new(struct object_database *odb,
                                              const char *path,
                                              bool local)
@@ -105,6 +115,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
        files->base.read_object_info = odb_source_files_read_object_info;
        files->base.read_object_stream = odb_source_files_read_object_stream;
        files->base.for_each_object = odb_source_files_for_each_object;
+       files->base.freshen_object = odb_source_files_freshen_object;
 
        /*
         * Ideally, we would only ever store absolute paths in the source. This
index be569953898e87033fb38c5bc913057a76a630a5..7f2ecf420bd44c81313a245dd781337431b787c5 100644 (file)
@@ -186,6 +186,18 @@ struct odb_source {
                               odb_for_each_object_cb cb,
                               void *cb_data,
                               unsigned flags);
+
+       /*
+        * This callback is expected to freshen the given object so that its
+        * last access time is set to the current time. This is used to ensure
+        * that objects that are recent will not get garbage collected even if
+        * they were unreachable.
+        *
+        * Returns 0 in case the object does not exist, 1 in case the object
+        * has been freshened.
+        */
+       int (*freshen_object)(struct odb_source *source,
+                             const struct object_id *oid);
 };
 
 /*
@@ -297,4 +309,15 @@ static inline int odb_source_for_each_object(struct odb_source *source,
        return source->for_each_object(source, request, cb, cb_data, flags);
 }
 
+/*
+ * Freshen an object in the object database by updating its timestamp.
+ * Returns 1 in case the object has been freshened, 0 in case the object does
+ * not exist.
+ */
+static inline int odb_source_freshen_object(struct odb_source *source,
+                                           const struct object_id *oid)
+{
+       return source->freshen_object(source, oid);
+}
+
 #endif