diff options
| author | Carlos Martín Nieto <[email protected]> | 2014-03-18 05:21:35 +0100 |
|---|---|---|
| committer | Carlos Martín Nieto <[email protected]> | 2014-03-18 05:21:35 +0100 |
| commit | 5f35f137372917332cd329cf723a9d005e13d582 (patch) | |
| tree | 5fb0ce34a23761b99d4873f6d241545a4b84e32d /clone.go | |
| parent | dbdbb4b0d124ef1f00cb67f84cef2ea35278aeef (diff) | |
| parent | 51aa76d6f7170bba60ab2252b74a3cab0276996f (diff) | |
Merge pull request #61 from jezell/remotes-wip
Cleaned up remotes / clone / add push / fetch
Diffstat (limited to 'clone.go')
| -rw-r--r-- | clone.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/clone.go b/clone.go new file mode 100644 index 0000000..c2abbe5 --- /dev/null +++ b/clone.go @@ -0,0 +1,76 @@ +package git + +/* +#include <git2.h> +#include <git2/errors.h> + +*/ +import "C" +import ( + "runtime" + "unsafe" +) + +type CloneOptions struct { + *CheckoutOpts + *RemoteCallbacks + Bare bool + IgnoreCertErrors bool + RemoteName string + CheckoutBranch string +} + +func Clone(url string, path string, options *CloneOptions) (*Repository, error) { + repo := new(Repository) + + curl := C.CString(url) + defer C.free(unsafe.Pointer(curl)) + + cpath := C.CString(path) + defer C.free(unsafe.Pointer(cpath)) + + var copts C.git_clone_options + populateCloneOptions(&copts, options) + + // 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)) + } + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + ret := C.git_clone(&repo.ptr, curl, cpath, &copts) + if ret < 0 { + return nil, MakeGitError(ret) + } + + runtime.SetFinalizer(repo, (*Repository).Free) + return repo, nil +} + +func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) { + ptr = &C.git_clone_options{} + C.git_clone_init_options(ptr, C.GIT_CLONE_OPTIONS_VERSION) + + 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 + } +} |
