odb: introduce `odb_for_each_object()`

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>

Patrick Steinhardt committed Jan 26, 2026 at 10:51 UTC df2fbdfa553526062e5234286f60bd643941298a
3 files changed +53 -3
object-file.h
+4 -3
@@ -139,9 +139,10 @@ int for_each_loose_object(struct object_database *odb,
139
140 /*
141 * Iterate through all loose objects in the given object database source and
142 - * invoke the callback function for each of them. If given, the object info
143 - * will be populated with the object's data as if you had called
144 - * `odb_source_loose_read_object_info()` on the object.
142 + * invoke the callback function for each of them. If an object info request is
143 + * given, then the object info will be read for every individual object and
144 + * passed to the callback as if `odb_source_loose_read_object_info()` was
145 + * called for the object.
146 */
147 int odb_source_loose_for_each_object(struct odb_source *source,
148 const struct object_info *request,
odb.c
+29
@@ -995,6 +995,35 @@ int odb_freshen_object(struct object_database *odb,
995 return 0;
996 }
997
998 +int odb_for_each_object(struct object_database *odb,
999 + const struct object_info *request,
1000 + odb_for_each_object_cb cb,
1001 + void *cb_data,
1002 + unsigned flags)
1003 +{
1004 + int ret;
1005 +
1006 + odb_prepare_alternates(odb);
1007 + for (struct odb_source *source = odb->sources; source; source = source->next) {
1008 + if (flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
1009 + continue;
1010 +
1011 + if (!(flags & ODB_FOR_EACH_OBJECT_PROMISOR_ONLY)) {
1012 + ret = odb_source_loose_for_each_object(source, request,
1013 + cb, cb_data, flags);
1014 + if (ret)
1015 + return ret;
1016 + }
1017 +
1018 + ret = packfile_store_for_each_object(source->packfiles, request,
1019 + cb, cb_data, flags);
1020 + if (ret)
1021 + return ret;
1022 + }
1023 +
1024 + return 0;
1025 +}
1026 +
1027 void odb_assert_oid_type(struct object_database *odb,
1028 const struct object_id *oid, enum object_type expect)
1029 {
odb.h
+20
@@ -475,6 +475,26 @@ typedef int (*odb_for_each_object_cb)(const struct object_id *oid,
475 struct object_info *oi,
476 void *cb_data);
477
478 +/*
479 + * Iterate through all objects contained in the object database. Note that
480 + * objects may be iterated over multiple times in case they are either stored
481 + * in different backends or in case they are stored in multiple sources.
482 + * If an object info request is given, then the object info will be read and
483 + * passed to the callback as if `odb_read_object_info()` was called for the
484 + * object.
485 + *
486 + * Returning a non-zero error code from the callback function will cause
487 + * iteration to abort. The error code will be propagated.
488 + *
489 + * Returns 0 on success, a negative error code in case a failure occurred, or
490 + * an arbitrary non-zero error code returned by the callback itself.
491 + */
492 +int odb_for_each_object(struct object_database *odb,
493 + const struct object_info *request,
494 + odb_for_each_object_cb cb,
495 + void *cb_data,
496 + unsigned flags);
497 +
498 enum {
499 /*
500 * By default, `odb_write_object()` does not actually write anything