From 53ae12359d324890c4d30cc06bd2631ebdec43bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 13 Aug 2012 14:00:53 +0200 Subject: [PATCH 1/2] tree: bring back the documented behaviour for a walk However, there should be a way to cancel the walk and another to skip the entry. --- src/tree.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/tree.c b/src/tree.c index e5858b50e..911cbadcf 100644 --- a/src/tree.c +++ b/src/tree.c @@ -787,10 +787,8 @@ static int tree_walk( for (i = 0; i < tree->entries.length; ++i) { git_tree_entry *entry = tree->entries.contents[i]; - if (preorder && callback(path->ptr, entry, payload)) { - error = GIT_EUSER; - break; - } + if (preorder && callback(path->ptr, entry, payload) < 0) + continue if (git_tree_entry__is_tree(entry)) { git_tree *subtree; From a6bf16878a121152f5bddf4d46f641e8f044d278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 13 Aug 2012 14:07:47 +0200 Subject: [PATCH 2/2] tree: allow the user to skip an entry or cancel the walk Returning a negative cancels the walk, and returning a positive one causes us to skip an entry, which was previously done by a negative value. This allows us to stay consistent with the rest of the functions that take a callback and keeps the skipping functionality. --- include/git2/tree.h | 5 +++-- src/tree.c | 11 ++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/git2/tree.h b/include/git2/tree.h index b91340624..85407d7ac 100644 --- a/include/git2/tree.h +++ b/include/git2/tree.h @@ -351,8 +351,9 @@ enum git_treewalk_mode { * the current (relative) root for the entry and the entry * data itself. * - * If the callback returns a negative value, the passed entry - * will be skipped on the traversal. + * If the callback returns a positive value, the passed entry will be + * skipped on the traversal (in pre mode). A negative value stops the + * walk. * * @param tree The tree to walk * @param callback Function to call on each tree entry diff --git a/src/tree.c b/src/tree.c index 911cbadcf..19250fe5e 100644 --- a/src/tree.c +++ b/src/tree.c @@ -787,8 +787,13 @@ static int tree_walk( for (i = 0; i < tree->entries.length; ++i) { git_tree_entry *entry = tree->entries.contents[i]; - if (preorder && callback(path->ptr, entry, payload) < 0) - continue + if (preorder) { + error = callback(path->ptr, entry, payload); + if (error > 0) + continue; + if (error < 0) + return GIT_EUSER; + } if (git_tree_entry__is_tree(entry)) { git_tree *subtree; @@ -813,7 +818,7 @@ static int tree_walk( git_tree_free(subtree); } - if (!preorder && callback(path->ptr, entry, payload)) { + if (!preorder && callback(path->ptr, entry, payload) < 0) { error = GIT_EUSER; break; }