]> git.ipfire.org Git - thirdparty/git.git/commitdiff
odb: introduce `odb_for_each_object()`
authorPatrick Steinhardt <ps@pks.im>
Mon, 26 Jan 2026 09:51:23 +0000 (10:51 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Jan 2026 16:26:07 +0000 (08:26 -0800)
Introduce a new function `odb_for_each_object()` that knows to iterate
through all objects part of a given object database. This function is
essentially a simple wrapper around the object database sources.

Subsequent commits will adapt callers to use this new function.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
object-file.h
odb.c
odb.h

index 5b9641cd890175a429b4d6e015850af5a7d3981b..b5eac0349ebc9828abd7418349794c82b1fb5ed2 100644 (file)
@@ -139,9 +139,10 @@ int for_each_loose_object(struct object_database *odb,
 
 /*
  * Iterate through all loose objects in the given object database source and
- * invoke the callback function for each of them. If given, the object info
- * will be populated with the object's data as if you had called
- * `odb_source_loose_read_object_info()` on the object.
+ * invoke the callback function for each of them. If an object info request is
+ * given, then the object info will be read for every individual object and
+ * passed to the callback as if `odb_source_loose_read_object_info()` was
+ * called for the object.
  */
 int odb_source_loose_for_each_object(struct odb_source *source,
                                     const struct object_info *request,
diff --git a/odb.c b/odb.c
index ac70b6a099f5882862f55e0650f7f502a65cc1aa..13a415c2c3e415139a8801697f2ee7b40df05e38 100644 (file)
--- a/odb.c
+++ b/odb.c
@@ -995,6 +995,35 @@ int odb_freshen_object(struct object_database *odb,
        return 0;
 }
 
+int odb_for_each_object(struct object_database *odb,
+                       const struct object_info *request,
+                       odb_for_each_object_cb cb,
+                       void *cb_data,
+                       unsigned flags)
+{
+       int ret;
+
+       odb_prepare_alternates(odb);
+       for (struct odb_source *source = odb->sources; source; source = source->next) {
+               if (flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
+                       continue;
+
+               if (!(flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) {
+                       ret = odb_source_loose_for_each_object(source, request,
+                                                              cb, cb_data, flags);
+                       if (ret)
+                               return ret;
+               }
+
+               ret = packfile_store_for_each_object(source->packfiles, request,
+                                                    cb, cb_data, flags);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
 void odb_assert_oid_type(struct object_database *odb,
                         const struct object_id *oid, enum object_type expect)
 {
diff --git a/odb.h b/odb.h
index f97f249580e08ac67f64f2a1efb6bc768fe3dab6..b5d28bc188f95704eaa8c13deb66e7a68f22d1f9 100644 (file)
--- a/odb.h
+++ b/odb.h
@@ -475,6 +475,26 @@ typedef int (*odb_for_each_object_cb)(const struct object_id *oid,
                                      struct object_info *oi,
                                      void *cb_data);
 
+/*
+ * Iterate through all objects contained in the object database. Note that
+ * objects may be iterated over multiple times in case they are either stored
+ * in different backends or in case they are stored in multiple sources.
+ * If an object info request is given, then the object info will be read and
+ * passed to the callback as if `odb_read_object_info()` was called for the
+ * object.
+ *
+ * Returning a non-zero error code from the callback function will cause
+ * iteration to abort. The error code will be propagated.
+ *
+ * Returns 0 on success, a negative error code in case a failure occurred, or
+ * an arbitrary non-zero error code returned by the callback itself.
+ */
+int odb_for_each_object(struct object_database *odb,
+                       const struct object_info *request,
+                       odb_for_each_object_cb cb,
+                       void *cb_data,
+                       unsigned flags);
+
 enum {
        /*
         * By default, `odb_write_object()` does not actually write anything