git2go/clone.go

133 lines
2.9 KiB
Go
Raw Normal View History

2014-01-03 18:40:21 -06:00
package git
/*
#include <git2.h>
extern void _go_git_populate_clone_callbacks(git_clone_options *opts);
2014-01-03 18:40:21 -06:00
*/
import "C"
import (
"errors"
2014-01-03 18:40:21 -06:00
"runtime"
"unsafe"
)
2015-08-31 06:49:17 -05:00
type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, ErrorCode)
2014-01-03 18:40:21 -06:00
type CloneOptions struct {
*CheckoutOpts
2015-06-07 21:11:21 -05:00
*FetchOptions
Bare bool
CheckoutBranch string
RemoteCreateCallback RemoteCreateCallback
2014-01-03 18:40:21 -06:00
}
func Clone(url string, path string, options *CloneOptions) (*Repository, error) {
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
2014-01-03 18:40:21 -06:00
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
2014-01-03 18:40:21 -06:00
var err error
cOptions := populateCloneOptions(&C.git_clone_options{}, options, &err)
defer freeCloneOptions(cOptions)
2014-01-03 18:40:21 -06:00
2014-01-06 14:05:35 -06:00
if len(options.CheckoutBranch) != 0 {
cOptions.checkout_branch = C.CString(options.CheckoutBranch)
2014-01-06 14:05:35 -06:00
}
2014-01-03 18:40:21 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2015-08-31 06:49:17 -05:00
var ptr *C.git_repository
ret := C.git_clone(&ptr, curl, cpath, cOptions)
if ret == C.int(ErrorCodeUser) && err != nil {
return nil, err
}
2014-01-03 18:40:21 -06:00
if ret < 0 {
return nil, MakeGitError(ret)
2014-01-03 18:40:21 -06:00
}
return newRepositoryFromC(ptr), nil
2014-01-03 18:40:21 -06:00
}
//export remoteCreateCallback
func remoteCreateCallback(
out **C.git_remote,
crepo *C.git_repository,
cname, curl *C.char,
handle unsafe.Pointer,
) C.int {
name := C.GoString(cname)
url := C.GoString(curl)
repo := newRepositoryFromC(crepo)
repo.weak = true
defer repo.Free()
data, ok := pointerHandles.Get(handle).(*cloneCallbackData)
if !ok {
panic("invalid remote create callback")
}
remote, ret := data.options.RemoteCreateCallback(repo, name, url)
if ret < 0 {
*data.errorTarget = errors.New(ErrorCode(ret).String())
return C.int(ErrorCodeUser)
}
if remote == nil {
panic("no remote created by callback")
}
*out = remote.ptr
// clear finalizer as the calling C function will
// free the remote itself
runtime.SetFinalizer(remote, nil)
return C.int(ErrorCodeOK)
}
type cloneCallbackData struct {
options *CloneOptions
errorTarget *error
}
func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions, errorTarget *error) *C.git_clone_options {
C.git_clone_options_init(ptr, C.GIT_CLONE_OPTIONS_VERSION)
2014-03-11 15:19:12 -05:00
2014-01-03 18:40:21 -06:00
if opts == nil {
return nil
2014-01-03 18:40:21 -06:00
}
populateCheckoutOptions(&ptr.checkout_opts, opts.CheckoutOpts, errorTarget)
2015-06-07 21:11:21 -05:00
populateFetchOptions(&ptr.fetch_opts, opts.FetchOptions)
ptr.bare = cbool(opts.Bare)
if opts.RemoteCreateCallback != nil {
data := &cloneCallbackData{
options: opts,
errorTarget: errorTarget,
}
// Go v1.1 does not allow to assign a C function pointer
C._go_git_populate_clone_callbacks(ptr)
ptr.remote_cb_payload = pointerHandles.Track(data)
}
return ptr
}
func freeCloneOptions(ptr *C.git_clone_options) {
if ptr == nil {
return
2014-01-03 18:40:21 -06:00
}
freeCheckoutOptions(&ptr.checkout_opts)
if ptr.remote_cb_payload != nil {
pointerHandles.Untrack(ptr.remote_cb_payload)
}
C.free(unsafe.Pointer(ptr.checkout_branch))
2014-01-03 18:40:21 -06:00
}