summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Toffaletti <[email protected]>2014-01-05 20:55:32 +0000
committerJason Toffaletti <[email protected]>2014-01-05 20:55:32 +0000
commit5d8a14d108ac963a4865cd03e20e56b28ee9626e (patch)
tree5badc289d5021ada588cdffe3b5c27c6ff9defae
parente825d66fba2cb4169c7f3b0a43c491cf9e8c0738 (diff)
wrappers for git_cred, git_transfer_progress. don't call nil callbacks.
-rw-r--r--remote.go90
-rw-r--r--transport.go79
2 files changed, 146 insertions, 23 deletions
diff --git a/remote.go b/remote.go
index 47866ed..ebf1fd4 100644
--- a/remote.go
+++ b/remote.go
@@ -13,9 +13,11 @@ extern void _setup_callbacks(git_remote_callbacks *callbacks);
*/
import "C"
-import (
- "unsafe"
-)
+import "unsafe"
+
+type TransferProgress struct {
+ ptr *C.git_transfer_progress
+}
type RemoteCompletion uint
const (
@@ -26,60 +28,102 @@ const (
type ProgressCallback func(str string) int
type CompletionCallback func(RemoteCompletion) int
-type CredentialsCallback func(url string, username_from_url string, allowed_types uint) int // FIXME
-type TransferProgressCallback func() int // FIXME
+type CredentialsCallback func(url string, username_from_url string, allowed_types CredType) (int, Cred)
+type TransferProgressCallback func(stats TransferProgress) int
type UpdateTipsCallback func(refname string, a *Oid, b *Oid) int
+type RemoteCallbacks struct {
+ ProgressCallback
+ CompletionCallback
+ CredentialsCallback
+ TransferProgressCallback
+ UpdateTipsCallback
+}
+
+func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
+ *ptr = C.git_remote_callbacks_init()
+ if callbacks == nil {
+ return
+ }
+ C._setup_callbacks(ptr)
+ ptr.payload = unsafe.Pointer(callbacks)
+}
+
//export progressCallback
func progressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
- str := C.GoStringN(_str, _len)
+ if callbacks.ProgressCallback == nil {
+ return 0
+ }
+ str := C.GoStringN(_str, _len)
return callbacks.ProgressCallback(str)
}
//export completionCallback
func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
+ if callbacks.CompletionCallback == nil {
+ return 0
+ }
return callbacks.CompletionCallback((RemoteCompletion)(completion_type))
}
//export credentialsCallback
func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C.char, allowed_types uint, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
- //cred := C.GoString(_cred)
+ if callbacks.CredentialsCallback == nil {
+ return 0
+ }
url := C.GoString(_url)
username_from_url := C.GoString(_username_from_url)
- return callbacks.CredentialsCallback(url, username_from_url, allowed_types)
+ ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types))
+ if gcred, ok := cred.(gitCred); ok {
+ *_cred = gcred.ptr
+ }
+ return ret
}
//export transferProgressCallback
-func transferProgressCallback(stats C.git_transfer_progress, data unsafe.Pointer) int {
+func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
- return callbacks.TransferProgressCallback()
+ if callbacks.TransferProgressCallback == nil {
+ return 0
+ }
+ return callbacks.TransferProgressCallback(TransferProgress{stats})
}
//export updateTipsCallback
func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
+ if callbacks.UpdateTipsCallback == nil {
+ return 0
+ }
refname := C.GoString(_refname)
a := newOidFromC(_a)
b := newOidFromC(_b)
return callbacks.UpdateTipsCallback(refname, a, b)
}
-type RemoteCallbacks struct {
- ProgressCallback
- CompletionCallback
- CredentialsCallback
- TransferProgressCallback
- UpdateTipsCallback
+func (o TransferProgress) TotalObjects() uint {
+ return uint(o.ptr.total_objects)
}
-func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
- *ptr = C.git_remote_callbacks_init()
- if callbacks == nil {
- return
- }
- C._setup_callbacks(ptr)
- ptr.payload = unsafe.Pointer(callbacks)
+func (o TransferProgress) IndexedObjects() uint {
+ return uint(o.ptr.indexed_objects)
+}
+
+func (o TransferProgress) ReceivedObjects() uint {
+ return uint(o.ptr.received_objects)
+}
+
+func (o TransferProgress) LocalObjects() uint {
+ return uint(o.ptr.local_objects)
+}
+
+func (o TransferProgress) TotalDeltas() uint {
+ return uint(o.ptr.total_deltas)
+}
+
+func (o TransferProgress) ReceivedBytes() uint {
+ return uint(o.ptr.received_bytes)
}
diff --git a/transport.go b/transport.go
new file mode 100644
index 0000000..e97a70c
--- /dev/null
+++ b/transport.go
@@ -0,0 +1,79 @@
+package git
+
+/*
+#include <git2.h>
+#include <git2/errors.h>
+*/
+import "C"
+import "unsafe"
+
+type CredType uint
+const (
+ CredTypeUserpassPlaintext CredType = C.GIT_CREDTYPE_USERPASS_PLAINTEXT
+ CredTypeSshKey = C.GIT_CREDTYPE_SSH_KEY
+ CredTypeSshCustom = C.GIT_CREDTYPE_SSH_CUSTOM
+ CredTypeDefault = C.GIT_CREDTYPE_DEFAULT
+)
+
+type Cred interface {
+ HasUsername() bool
+ Type() CredType
+}
+
+type gitCred struct {
+ ptr *C.git_cred
+}
+
+func (o gitCred) HasUsername() bool {
+ if C.git_cred_has_username(o.ptr) == 1 {
+ return true
+ }
+ return false
+}
+
+func (o gitCred) Type() CredType {
+ return (CredType)(o.ptr.credtype);
+}
+
+func credFromC(ptr *C.git_cred) Cred {
+ return gitCred{ptr}
+}
+
+func NewCredUserpassPlaintext(username string, password string) (int, Cred) {
+ cred := gitCred{}
+ cusername := C.CString(username)
+ defer C.free(unsafe.Pointer(cusername))
+ cpassword := C.CString(password)
+ defer C.free(unsafe.Pointer(cpassword))
+ ret := C.git_cred_userpass_plaintext_new(&cred.ptr, cusername, cpassword)
+ return int(ret), cred
+}
+
+func NewCredSshKey(username string, publickey string, privatekey string, passphrase string) (int, Cred) {
+ cred := gitCred{}
+ cusername := C.CString(username)
+ defer C.free(unsafe.Pointer(cusername))
+ cpublickey := C.CString(publickey)
+ defer C.free(unsafe.Pointer(cpublickey))
+ cprivatekey := C.CString(privatekey)
+ defer C.free(unsafe.Pointer(cprivatekey))
+ cpassphrase := C.CString(passphrase)
+ defer C.free(unsafe.Pointer(cpassphrase))
+ ret := C.git_cred_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
+ return int(ret), cred
+}
+
+func NewCredSshKeyFromAgent(username string) (int, Cred) {
+ cred := gitCred{}
+ cusername := C.CString(username)
+ defer C.free(unsafe.Pointer(cusername))
+ ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername)
+ return int(ret), cred
+}
+
+func NewCredDefault() (int, Cred) {
+ cred := gitCred{}
+ ret := C.git_cred_default_new(&cred.ptr)
+ return int(ret), cred
+}
+