git2go/clone.go

77 lines
1.6 KiB
Go
Raw Normal View History

2014-01-03 18:40:21 -06:00
package git
/*
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type CloneOptions struct {
*CheckoutOpts
*RemoteCallbacks
Bare bool
2014-01-03 18:40:21 -06:00
IgnoreCertErrors bool
RemoteName string
CheckoutBranch string
2014-01-03 18:40:21 -06:00
}
func Clone(url string, path string, options *CloneOptions) (*Repository, error) {
repo := new(Repository)
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 copts C.git_clone_options
populateCloneOptions(&copts, options)
2014-01-06 14:05:35 -06:00
// finish populating clone options here so we can defer CString free
if len(options.RemoteName) != 0 {
copts.remote_name = C.CString(options.RemoteName)
defer C.free(unsafe.Pointer(copts.remote_name))
}
if len(options.CheckoutBranch) != 0 {
copts.checkout_branch = C.CString(options.CheckoutBranch)
defer C.free(unsafe.Pointer(copts.checkout_branch))
}
2014-01-03 18:40:21 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_clone(&repo.ptr, curl, cpath, &copts)
if ret < 0 {
return nil, MakeGitError(ret)
2014-01-03 18:40:21 -06:00
}
runtime.SetFinalizer(repo, (*Repository).Free)
return repo, nil
}
func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) {
2014-03-11 15:19:12 -05:00
ptr = &C.git_clone_options{}
C.git_clone_init_options(ptr, 1)
2014-01-03 18:40:21 -06:00
if opts == nil {
return
}
populateCheckoutOpts(&ptr.checkout_opts, opts.CheckoutOpts)
populateRemoteCallbacks(&ptr.remote_callbacks, opts.RemoteCallbacks)
if opts.Bare {
ptr.bare = 1
} else {
ptr.bare = 0
}
if opts.IgnoreCertErrors {
ptr.ignore_cert_errors = 1
} else {
ptr.ignore_cert_errors = 0
}
}