-
Notifications
You must be signed in to change notification settings - Fork 316
/
clone.go
133 lines (106 loc) · 3.01 KB
/
clone.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package git
/*
#include <git2.h>
extern void _go_git_populate_clone_callbacks(git_clone_options *opts);
*/
import "C"
import (
"runtime"
"unsafe"
)
type RemoteCreateCallback func(repo *Repository, name, url string) (*Remote, error)
type CloneOptions struct {
CheckoutOptions CheckoutOptions
FetchOptions FetchOptions
Bare bool
CheckoutBranch string
RemoteCreateCallback RemoteCreateCallback
}
func Clone(url string, path string, options *CloneOptions) (*Repository, error) {
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var err error
cOptions := populateCloneOptions(&C.git_clone_options{}, options, &err)
defer freeCloneOptions(cOptions)
if len(options.CheckoutBranch) != 0 {
cOptions.checkout_branch = C.CString(options.CheckoutBranch)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_repository
ret := C.git_clone(&ptr, curl, cpath, cOptions)
if ret == C.int(ErrorCodeUser) && err != nil {
return nil, err
}
if ret < 0 {
return nil, MakeGitError(ret)
}
return newRepositoryFromC(ptr), nil
}
//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, err := data.options.RemoteCreateCallback(repo, name, url)
if err != nil {
*data.errorTarget = err
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)
remote.repo.Remotes.untrackRemote(remote)
return C.int(ErrorCodeOK)
}
type cloneCallbackData struct {
options *CloneOptions
errorTarget *error
}
func populateCloneOptions(copts *C.git_clone_options, opts *CloneOptions, errorTarget *error) *C.git_clone_options {
C.git_clone_options_init(copts, C.GIT_CLONE_OPTIONS_VERSION)
if opts == nil {
return nil
}
populateCheckoutOptions(&copts.checkout_opts, &opts.CheckoutOptions, errorTarget)
populateFetchOptions(&copts.fetch_opts, &opts.FetchOptions, errorTarget)
copts.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(copts)
copts.remote_cb_payload = pointerHandles.Track(data)
}
return copts
}
func freeCloneOptions(copts *C.git_clone_options) {
if copts == nil {
return
}
freeCheckoutOptions(&copts.checkout_opts)
freeFetchOptions(&copts.fetch_opts)
if copts.remote_cb_payload != nil {
pointerHandles.Untrack(copts.remote_cb_payload)
}
C.free(unsafe.Pointer(copts.checkout_branch))
}