From 5d8eaf7e65c404a0d10d3705697dd99369630dda Mon Sep 17 00:00:00 2001 From: lhchavez Date: Sat, 5 Dec 2020 13:13:59 -0800 Subject: Refactor all callbacks (#700) This change is a preparation for another change that makes all callback types return a Go error instead of an error code / an integer. That is going to make make things a lot more idiomatic. The reason this change is split is threefold: a) This change is mostly mechanical and should contain no semantic changes. b) This change is backwards-compatible (in the Go API compatibility sense of the word), and thus can be backported to all other releases. c) It makes the other change a bit smaller and more focused on just one thing. Concretely, this change makes all callbacks populate a Go error when they fail. If the callback is invoked from the same stack as the function to which it was passed (e.g. for `Tree.Walk`), it will preserve the error object directly into a struct that also holds the callback function. Otherwise if the callback is pased to one func and will be invoked when run from another one (e.g. for `Repository.InitRebase`), the error string is saved into the libgit2 thread-local storage and then re-created as a `GitError`. --- tag.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'tag.go') diff --git a/tag.go b/tag.go index 20b9ba2..dd8477a 100644 --- a/tag.go +++ b/tag.go @@ -197,48 +197,48 @@ func (c *TagsCollection) ListWithMatch(pattern string) ([]string, error) { // so repo.LookupTag() will return an error for these tags. Use // repo.References.Lookup() instead. type TagForeachCallback func(name string, id *Oid) error -type tagForeachData struct { - callback TagForeachCallback - err error +type tagForeachCallbackData struct { + callback TagForeachCallback + errorTarget *error } -//export gitTagForeachCb -func gitTagForeachCb(name *C.char, id *C.git_oid, handle unsafe.Pointer) int { +//export tagForeachCallback +func tagForeachCallback(name *C.char, id *C.git_oid, handle unsafe.Pointer) C.int { payload := pointerHandles.Get(handle) - data, ok := payload.(*tagForeachData) + data, ok := payload.(*tagForeachCallbackData) if !ok { panic("could not retrieve tag foreach CB handle") } err := data.callback(C.GoString(name), newOidFromC(id)) if err != nil { - data.err = err - return C.GIT_EUSER + *data.errorTarget = err + return C.int(ErrorCodeUser) } - return 0 + return C.int(ErrorCodeOK) } // Foreach calls the callback for each tag in the repository. func (c *TagsCollection) Foreach(callback TagForeachCallback) error { - data := tagForeachData{ - callback: callback, - err: nil, + var err error + data := tagForeachCallbackData{ + callback: callback, + errorTarget: &err, } - handle := pointerHandles.Track(&data) defer pointerHandles.Untrack(handle) runtime.LockOSThread() defer runtime.UnlockOSThread() - err := C._go_git_tag_foreach(c.repo.ptr, handle) + ret := C._go_git_tag_foreach(c.repo.ptr, handle) runtime.KeepAlive(c) - if err == C.GIT_EUSER { - return data.err + if ret == C.int(ErrorCodeUser) && err != nil { + return err } - if err < 0 { - return MakeGitError(err) + if ret < 0 { + return MakeGitError(ret) } return nil -- cgit v1.2.3