summaryrefslogtreecommitdiff
path: root/tree.go
diff options
context:
space:
mode:
authorlhchavez <[email protected]>2021-09-30 09:24:49 -0700
committerGitHub <[email protected]>2021-09-30 09:24:49 -0700
commit9b155184fe1f0f0258730bccd2759ac7ec39be2a (patch)
treeb750693d723a888f7f283fc42ddb9c17ca41a348 /tree.go
parentc6da3b97a8a2c7bbbed4496f50c990a1aab4f7bf (diff)
Allow skipping an entry expansion in `tree.Walk()` (#838)
It is now possible to skip expanding an entry in `tree.Walk()` by returning `TreeWalkSkip`, in addition to stopping altogether by returning a non-nil error. Fixes: #837
Diffstat (limited to 'tree.go')
-rw-r--r--tree.go18
1 files changed, 17 insertions, 1 deletions
diff --git a/tree.go b/tree.go
index b1aeaa7..1410b96 100644
--- a/tree.go
+++ b/tree.go
@@ -8,6 +8,7 @@ extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr);
import "C"
import (
+ "errors"
"runtime"
"unsafe"
)
@@ -134,7 +135,9 @@ func treeWalkCallback(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer
}
err := data.callback(C.GoString(_root), newTreeEntry(entry))
- if err != nil {
+ if err == TreeWalkSkip {
+ return C.int(1)
+ } else if err != nil {
*data.errorTarget = err
return C.int(ErrorCodeUser)
}
@@ -142,6 +145,19 @@ func treeWalkCallback(_root *C.char, entry *C.git_tree_entry, ptr unsafe.Pointer
return C.int(ErrorCodeOK)
}
+// TreeWalkSkip is an error that can be returned form TreeWalkCallback to skip
+// a subtree from being expanded.
+var TreeWalkSkip = errors.New("skip")
+
+// Walk traverses the entries in a tree and its subtrees in pre order.
+//
+// The entries will be traversed in the pre order, children subtrees will be
+// automatically loaded as required, and the callback will be called once per
+// entry with the current (relative) root for the entry and the entry data
+// itself.
+//
+// If the callback returns TreeWalkSkip, the passed entry will be skipped on
+// the traversal. Any other non-nil error stops the walk.
func (t *Tree) Walk(callback TreeWalkCallback) error {
var err error
data := treeWalkCallbackData{