@@ -52,6 +52,18 @@ struct object_id;
5252struct object_info ;
5353struct odb_read_stream ;
5454
55+ /*
56+ * A callback function that can be used to iterate through objects. If given,
57+ * the optional `oi` parameter will be populated the same as if you would call
58+ * `odb_read_object_info()`.
59+ *
60+ * Returning a non-zero error code will cause iteration to abort. The error
61+ * code will be propagated.
62+ */
63+ typedef int (* odb_for_each_object_cb )(const struct object_id * oid ,
64+ struct object_info * oi ,
65+ void * cb_data );
66+
5567/*
5668 * The source is the part of the object database that stores the actual
5769 * objects. It thus encapsulates the logic to read and write the specific
@@ -150,6 +162,30 @@ struct odb_source {
150162 int (* read_object_stream )(struct odb_read_stream * * out ,
151163 struct odb_source * source ,
152164 const struct object_id * oid );
165+
166+ /*
167+ * This callback is expected to iterate over all objects stored in this
168+ * source and invoke the callback function for each of them. It is
169+ * valid to yield the same object multiple time. A non-zero exit code
170+ * from the object callback shall abort iteration.
171+ *
172+ * The optional `request` structure should serve as a template for
173+ * looking up object info for every individual iterated object. It
174+ * should not be modified directly and should instead be copied into a
175+ * separate `struct object_info` that gets passed to the callback. If
176+ * the caller passes a `NULL` pointer then the object itself shall not
177+ * be read.
178+ *
179+ * The callback is expected to return a negative error code in case the
180+ * iteration has failed to read all objects, 0 otherwise. When the
181+ * callback function returns a non-zero error code then that error code
182+ * should be returned.
183+ */
184+ int (* for_each_object )(struct odb_source * source ,
185+ const struct object_info * request ,
186+ odb_for_each_object_cb cb ,
187+ void * cb_data ,
188+ unsigned flags );
153189};
154190
155191/*
@@ -232,4 +268,33 @@ static inline int odb_source_read_object_stream(struct odb_read_stream **out,
232268 return source -> read_object_stream (out , source , oid );
233269}
234270
271+ /*
272+ * Iterate through all objects contained in the given source and invoke the
273+ * callback function for each of them. Returning a non-zero code from the
274+ * callback function aborts iteration. There is no guarantee that objects
275+ * are only iterated over once.
276+ *
277+ * The optional `request` structure serves as a template for retrieving the
278+ * object info for each indvidual iterated object and will be populated as if
279+ * `odb_source_read_object_info()` was called on the object. It will not be
280+ * modified, the callback will instead be invoked with a separate `struct
281+ * object_info` for every object. Object info will not be read when passing a
282+ * `NULL` pointer.
283+ *
284+ * The flags is a bitfield of `ODB_FOR_EACH_OBJECT_*` flags. Not all flags may
285+ * apply to a specific backend, so whether or not they are honored is defined
286+ * by the implementation.
287+ *
288+ * Returns 0 when all objects have been iterated over, a negative error code in
289+ * case iteration has failed, or a non-zero value returned from the callback.
290+ */
291+ static inline int odb_source_for_each_object (struct odb_source * source ,
292+ const struct object_info * request ,
293+ odb_for_each_object_cb cb ,
294+ void * cb_data ,
295+ unsigned flags )
296+ {
297+ return source -> for_each_object (source , request , cb , cb_data , flags );
298+ }
299+
235300#endif
0 commit comments