Commit d54aa9aef7e8d4547c128428da87b0920f61bf68

Edward Thomson 2018-06-26T15:25:30

iterator: introduce `git_iterator_foreach` Introduce a `git_iterator_foreach` helper function which invokes a callback on all files for a given iterator.

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);