]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb/source: make `read_alternates()` function pluggable
authorPatrick Steinhardt <ps@pks.im>
Thu, 5 Mar 2026 14:19:55 +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.h
odb/source-files.c
odb/source.h

diff --git a/odb.c b/odb.c
index f439de9db287bee80dbad6893c91f7af7c23bc1c..d9424cdfd0610da401951662338b78270b1db345 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -131,10 +131,10 @@ out:
        return usable;
 }
 
-static void parse_alternates(const char *string,
-                            int sep,
-                            const char *relative_base,
-                            struct strvec *out)
+void parse_alternates(const char *string,
+                     int sep,
+                     const char *relative_base,
+                     struct strvec *out)
 {
        struct strbuf pathbuf = STRBUF_INIT;
        struct strbuf buf = STRBUF_INIT;
@@ -198,24 +198,6 @@ static void parse_alternates(const char *string,
        strbuf_release(&buf);
 }
 
-static void odb_source_read_alternates(struct odb_source *source,
-                                      struct strvec *out)
-{
-       struct strbuf buf = STRBUF_INIT;
-       char *path;
-
-       path = xstrfmt("%s/info/alternates", source->path);
-       if (strbuf_read_file(&buf, path, 1024) < 0) {
-               warn_on_fopen_errors(path);
-               free(path);
-               return;
-       }
-       parse_alternates(buf.buf, '\n', source->path, out);
-
-       strbuf_release(&buf);
-       free(path);
-}
-
 static struct odb_source *odb_add_alternate_recursively(struct object_database *odb,
                                                        const char *source,
                                                        int depth)
diff --git a/odb.h b/odb.h
index 692d9029efd36c2b918540f618ddb966ef110cf4..86e0365c243863b824dcf79df02f83ddff3d7efa 100644 (file)
--- a/odb.h
+++ b/odb.h
@@ -500,4 +500,9 @@ int odb_write_object_stream(struct object_database *odb,
                            struct odb_write_stream *stream, size_t len,
                            struct object_id *oid);
 
+void parse_alternates(const char *string,
+                     int sep,
+                     const char *relative_base,
+                     struct strvec *out);
+
 #endif /* ODB_H */
index b8844f11b7fab2be730fd7d1e7093d3d3fe76f00..199c55cfa4ca1a86bb930d2cf23ee4564205be93 100644 (file)
@@ -2,9 +2,11 @@
 #include "abspath.h"
 #include "chdir-notify.h"
 #include "object-file.h"
+#include "odb.h"
 #include "odb/source.h"
 #include "odb/source-files.h"
 #include "packfile.h"
+#include "strbuf.h"
 
 static void odb_source_files_reparent(const char *name UNUSED,
                                      const char *old_cwd,
@@ -117,6 +119,25 @@ static int odb_source_files_write_object_stream(struct odb_source *source,
        return odb_source_loose_write_stream(source, stream, len, oid);
 }
 
+static int odb_source_files_read_alternates(struct odb_source *source,
+                                           struct strvec *out)
+{
+       struct strbuf buf = STRBUF_INIT;
+       char *path;
+
+       path = xstrfmt("%s/info/alternates", source->path);
+       if (strbuf_read_file(&buf, path, 1024) < 0) {
+               warn_on_fopen_errors(path);
+               free(path);
+               return 0;
+       }
+       parse_alternates(buf.buf, '\n', source->path, out);
+
+       strbuf_release(&buf);
+       free(path);
+       return 0;
+}
+
 struct odb_source_files *odb_source_files_new(struct object_database *odb,
                                              const char *path,
                                              bool local)
@@ -137,6 +158,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
        files->base.freshen_object = odb_source_files_freshen_object;
        files->base.write_object = odb_source_files_write_object;
        files->base.write_object_stream = odb_source_files_write_object_stream;
+       files->base.read_alternates = odb_source_files_read_alternates;
 
        /*
         * Ideally, we would only ever store absolute paths in the source. This
index 6c8bec19126e6a83b95a73ebce21ec2194708c89..fbdddcb2eb4312188c2ea04603e39ec197841362 100644 (file)
@@ -54,6 +54,7 @@ struct object_id;
 struct object_info;
 struct odb_read_stream;
 struct odb_write_stream;
+struct strvec;
 
 /*
  * A callback function that can be used to iterate through objects. If given,
@@ -231,6 +232,19 @@ struct odb_source {
        int (*write_object_stream)(struct odb_source *source,
                                   struct odb_write_stream *stream, size_t len,
                                   struct object_id *oid);
+
+       /*
+        * This callback is expected to read the list of alternate object
+        * database sources connected to it and write them into the `strvec`.
+        *
+        * The result is expected to be paths to the alternates. All paths must
+        * be resolved to absolute paths.
+        *
+        * The callback is expected to return 0 on success, a negative error
+        * code otherwise.
+        */
+       int (*read_alternates)(struct odb_source *source,
+                              struct strvec *out);
 };
 
 /*
@@ -384,4 +398,18 @@ static inline int odb_source_write_object_stream(struct odb_source *source,
        return source->write_object_stream(source, stream, len, oid);
 }
 
+/*
+ * Read the list of alternative object database sources from the given backend
+ * and populate the `strvec` with them. The listing is not recursive -- that
+ * is, if any of the yielded alternate sources has alternates itself, those
+ * will not be yielded as part of this function call.
+ *
+ * Return 0 on success, a negative error code otherwise.
+ */
+static inline int odb_source_read_alternates(struct odb_source *source,
+                                            struct strvec *out)
+{
+       return source->read_alternates(source, out);
+}
+
 #endif