From d54aa9aef7e8d4547c128428da87b0920f61bf68 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 26 Jun 2018 15:25:30 +0100 Subject: iterator: introduce `git_iterator_foreach` Introduce a `git_iterator_foreach` helper function which invokes a callback on all files for a given iterator. --- src/iterator.c | 29 +++++++++++++++++++++++++++++ src/iterator.h | 13 +++++++++++++ 2 files changed, 42 insertions(+) (limited to 'src') diff --git a/src/iterator.c b/src/iterator.c index 0bd67c7d4..40f675957 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 fe358f16c..bbe357fbd 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); -- cgit v1.2.1