iterator: introduce `git_iterator_foreach` Introduce a `git_iterator_foreach` helper function which invokes a callback on all files for a given iterator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
diff --git a/src/iterator.c b/src/iterator.c
index 0bd67c7..40f6759 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -2298,6 +2298,35 @@ void git_iterator_free(git_iterator *iter)
git__free(iter);
}
+int git_iterator_foreach(
+ git_iterator *iterator,
+ git_iterator_foreach_cb cb,
+ void *data)
+{
+ const git_index_entry *iterator_item;
+ int error = 0;
+
+ if ((error = git_iterator_current(&iterator_item, iterator)) < 0)
+ goto done;
+
+ if ((error = cb(iterator_item, data)) != 0)
+ goto done;
+
+ while (true) {
+ if ((error = git_iterator_advance(&iterator_item, iterator)) < 0)
+ goto done;
+
+ if ((error = cb(iterator_item, data)) != 0)
+ goto done;
+ }
+
+done:
+ if (error == GIT_ITEROVER)
+ error = 0;
+
+ return error;
+}
+
int git_iterator_walk(
git_iterator **iterators,
size_t cnt,
diff --git a/src/iterator.h b/src/iterator.h
index fe358f1..bbe357f 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -291,6 +291,19 @@ extern int git_iterator_current_workdir_path(
*/
extern git_index *git_iterator_index(git_iterator *iter);
+typedef int (*git_iterator_foreach_cb)(
+ const git_index_entry *entry,
+ void *data);
+
+/**
+ * Walk the given iterator and invoke the callback for each path
+ * contained in the iterator.
+ */
+extern int git_iterator_foreach(
+ git_iterator *iterator,
+ git_iterator_foreach_cb cb,
+ void *data);
+
typedef int (*git_iterator_walk_cb)(
const git_index_entry **entries,
void *data);