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
|
2014-02-26 12:41:20 -06:00
|
|
|
Bare bool
|
2014-01-03 18:40:21 -06:00
|
|
|
IgnoreCertErrors bool
|
2014-02-26 12:41:20 -06:00
|
|
|
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)
|
|
|
|
|
2014-02-26 12:41:20 -06:00
|
|
|
curl := C.CString(url)
|
|
|
|
defer C.free(unsafe.Pointer(curl))
|
2014-01-03 18:40:21 -06:00
|
|
|
|
2014-02-26 12:41:20 -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 {
|
2014-02-26 12:41:20 -06:00
|
|
|
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:22:00 -05:00
|
|
|
C.git_clone_init_options(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
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|