Make the network code use handles

This wasn't ported together with the rest, but it does exhibit the same
issues, so let's port it over now.
This commit is contained in:
Carlos Martín Nieto 2015-06-10 13:03:53 +02:00
parent 53fd8ea011
commit c00a05586b
2 changed files with 23 additions and 15 deletions

View File

@ -28,18 +28,20 @@ func Clone(url string, path string, options *CloneOptions) (*Repository, error)
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var copts C.git_clone_options
populateCloneOptions(&copts, options)
defer freeCheckoutOpts(&copts.checkout_opts)
copts := (*C.git_clone_options)(C.calloc(1, C.size_t(unsafe.Sizeof(C.git_clone_options{}))))
populateCloneOptions(copts, options)
if len(options.CheckoutBranch) != 0 {
copts.checkout_branch = C.CString(options.CheckoutBranch)
defer C.free(unsafe.Pointer(copts.checkout_branch))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_clone(&repo.ptr, curl, cpath, &copts)
ret := C.git_clone(&repo.ptr, curl, cpath, copts)
freeCheckoutOpts(&copts.checkout_opts)
C.free(unsafe.Pointer(copts.checkout_branch))
C.free(unsafe.Pointer(copts))
if ret < 0 {
return nil, MakeGitError(ret)
}

View File

@ -129,12 +129,12 @@ func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallb
return
}
C._go_git_setup_callbacks(ptr)
ptr.payload = unsafe.Pointer(callbacks)
ptr.payload = pointerHandles.Track(callbacks)
}
//export sidebandProgressCallback
func sidebandProgressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks := pointerHandles.Get(data).(*RemoteCallbacks)
if callbacks.SidebandProgressCallback == nil {
return 0
}
@ -144,7 +144,7 @@ func sidebandProgressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int
//export completionCallback
func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks := pointerHandles.Get(data).(*RemoteCallbacks)
if callbacks.CompletionCallback == nil {
return 0
}
@ -153,7 +153,7 @@ func completionCallback(completion_type C.git_remote_completion_type, data unsaf
//export credentialsCallback
func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C.char, allowed_types uint, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
if callbacks.CredentialsCallback == nil {
return 0
}
@ -166,7 +166,7 @@ func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C
//export transferProgressCallback
func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
if callbacks.TransferProgressCallback == nil {
return 0
}
@ -175,7 +175,7 @@ func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointe
//export updateTipsCallback
func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
if callbacks.UpdateTipsCallback == nil {
return 0
}
@ -187,7 +187,7 @@ func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data uns
//export certificateCheckCallback
func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
// if there's no callback set, we need to make sure we fail if the library didn't consider this cert valid
if callbacks.CertificateCheckCallback == nil {
if _valid == 1 {
@ -228,7 +228,7 @@ func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, da
//export packProgressCallback
func packProgressCallback(stage C.int, current, total C.uint, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
if callbacks.PackProgressCallback == nil {
return 0
@ -239,7 +239,7 @@ func packProgressCallback(stage C.int, current, total C.uint, data unsafe.Pointe
//export pushTransferProgressCallback
func pushTransferProgressCallback(current, total C.uint, bytes C.size_t, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
if callbacks.PushTransferProgressCallback == nil {
return 0
}
@ -249,7 +249,7 @@ func pushTransferProgressCallback(current, total C.uint, bytes C.size_t, data un
//export pushUpdateReferenceCallback
func pushUpdateReferenceCallback(refname, status *C.char, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
callbacks, _ := pointerHandles.Get(data).(*RemoteCallbacks)
if callbacks.PushUpdateReferenceCallback == nil {
return 0
@ -286,6 +286,12 @@ func (r *Remote) SetCallbacks(callbacks *RemoteCallbacks) error {
func (r *Remote) Free() {
runtime.SetFinalizer(r, nil)
callbacks := C.git_remote_get_callbacks(r.ptr)
if callbacks != nil && callbacks.payload != nil {
pointerHandles.Untrack(callbacks.payload)
}
C.git_remote_free(r.ptr)
}