diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2018-06-26 15:25:30 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2018-11-04 09:21:48 +0000 |
commit | d54aa9aef7e8d4547c128428da87b0920f61bf68 (patch) | |
tree | 46a4f4082d2b5598607af25e5339bbe52fa8d7ac /src | |
parent | 9c34c9961e3897261c1ceafbdace55fcf56d7abe (diff) | |
download | libgit2-d54aa9aef7e8d4547c128428da87b0920f61bf68.tar.gz |
iterator: introduce `git_iterator_foreach`
Introduce a `git_iterator_foreach` helper function which invokes a
callback on all files for a given iterator.
Diffstat (limited to 'src')
-rw-r--r-- | src/iterator.c | 29 | ||||
-rw-r--r-- | src/iterator.h | 13 |
2 files changed, 42 insertions, 0 deletions
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); |