Skip to content

Commit f1475c3

Browse files
committed
tree::walk: Allow callback to return an int
`git_tree_walk` in libgit2 accepts a callback which can return an int. The int tells the walker whether to recurse on a tree object or not. This is useful behaviour if you don't want to walk an *entire* large tree. To upgrade existing code, just change existing callbacks to return `int` instead of `void`, and add a `return 0;`
1 parent 18d450e commit f1475c3

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# cppgit2: Koordinates Fork
2+
3+
This fork is maintained by Koordinates for the purposes of implementing [Kart](https://kartproject.org).
4+
5+
This fork is likely to be (somewhat) actively maintained, so someone will respond to issues/PRs.
6+
7+
# cppgit2
8+
19
<p align="center">
210
<img height="100" src="img/logo.png"/>
311
</p>
@@ -314,6 +322,7 @@ int main(int argc, char **argv) {
314322
}
315323
std::cout << type_string << " [" << entry.id().to_hex_string(8)
316324
<< "] " << entry.filename() << std::endl;
325+
return 0;
317326
});
318327

319328
} else {

include/cppgit2/tree.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ class tree : public libgit2_api {
140140
class repository owner() const;
141141

142142
// Traverse the entries in a tree and its subtrees in post or pre order.
143+
// If the callback returns a positive value, the passed entry will be skipped
144+
// on the traversal (in pre mode). A negative value stops the walk.
143145
void walk(traversal_mode mode,
144-
std::function<void(const std::string &, const tree::entry &)>
146+
std::function<int(const std::string &, const tree::entry &)>
145147
visitor) const;
146148

147149
enum class update_type {

samples/walk_tree.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ int main(int argc, char **argv) {
3131
}
3232
std::cout << type_string << " [" << entry.id().to_hex_string(8)
3333
<< "] " << entry.filename() << std::endl;
34+
return 0;
3435
});
3536

3637
} else {

src/tree.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ size_t tree::size() const { return git_tree_entrycount(c_ptr_); }
6464
repository tree::owner() const { return repository(git_tree_owner(c_ptr_)); }
6565

6666
void tree::walk(traversal_mode mode,
67-
std::function<void(const std::string &, const tree::entry &)>
67+
std::function<int(const std::string &, const tree::entry &)>
6868
visitor) const {
6969
struct visitor_wrapper {
70-
std::function<void(const std::string &, const tree::entry &)> fn;
70+
std::function<int(const std::string &, const tree::entry &)> fn;
7171
};
7272

7373
visitor_wrapper wrapper;
@@ -76,8 +76,7 @@ void tree::walk(traversal_mode mode,
7676
auto callback_c = [](const char *root, const git_tree_entry *entry,
7777
void *payload) {
7878
auto wrapper = reinterpret_cast<visitor_wrapper *>(payload);
79-
wrapper->fn(root ? std::string(root) : "", tree::entry(entry));
80-
return 0;
79+
return wrapper->fn(root ? std::string(root) : "", tree::entry(entry));
8180
};
8281

8382
if (git_tree_walk(c_ptr_, static_cast<git_treewalk_mode>(mode), callback_c,

0 commit comments

Comments
 (0)