summaryrefslogtreecommitdiff
path: root/clone.go
blob: 672c32506408938834acd61a99a72c4665f5e1ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package git

/*
#include <git2.h>
#include <git2/errors.h>

static git_clone_options git_clone_options_init() {
	git_clone_options ret = GIT_CLONE_OPTIONS_INIT;
	return ret;
}

*/
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)

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	ret := C.git_clone(&repo.ptr, curl, cpath, &copts)
	if ret < 0 {
		return nil, LastError()
	}

	runtime.SetFinalizer(repo, (*Repository).Free)
	return repo, nil
}

func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) {
	*ptr = C.git_clone_options_init()
	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
	}
}