Compare commits

...

4 Commits

Author SHA1 Message Date
Carlos Martín Nieto 92bb4425bc Remote: remove redundant Callback suffix
These fields are in a callback struct, we don't need every field to have
the Callback suffix.
2014-03-23 10:36:54 +01:00
Carlos Martín Nieto d805ba7409 Remote: incorporate callbacks into the struct
Let a user with a Remote object use it, same as it's wired up for clone.
2014-03-21 07:20:41 +01:00
Carlos Martín Nieto 805085c1d7 Cred: make returns more consistent
We need to lock the thread as well as return the error struct instead of
simply the error number.
2014-03-21 07:03:57 +01:00
Carlos Martín Nieto 9fc307a58d Remote: keep a pointer to the repo
Make sure the finalizer doesn't get called too early, and unify where
that setup happens.
2014-03-20 03:52:53 +01:00
2 changed files with 96 additions and 46 deletions

View File

@ -5,7 +5,10 @@ package git
#include <git2/errors.h>
*/
import "C"
import "unsafe"
import (
"unsafe"
"runtime"
)
type CredType uint
@ -21,31 +24,37 @@ type Cred struct {
}
func (o *Cred) HasUsername() bool {
if C.git_cred_has_username(o.ptr) == 1 {
return true
}
return false
return C.git_cred_has_username(o.ptr) != 0
}
func (o *Cred) Type() CredType {
return (CredType)(o.ptr.credtype)
return CredType(o.ptr.credtype)
}
func credFromC(ptr *C.git_cred) *Cred {
return &Cred{ptr}
}
func NewCredUserpassPlaintext(username string, password string) (int, Cred) {
func NewCredUserpassPlaintext(username string, password string) (*Cred, error) {
cred := Cred{}
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
cpassword := C.CString(password)
defer C.free(unsafe.Pointer(cpassword))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_cred_userpass_plaintext_new(&cred.ptr, cusername, cpassword)
return int(ret), cred
if ret < 0 {
return nil, MakeGitError(ret)
}
return &cred, nil
}
func NewCredSshKey(username string, publickey string, privatekey string, passphrase string) (int, Cred) {
func NewCredSshKey(username, publickey, privatekey, passphrase string) (*Cred, error) {
cred := Cred{}
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
@ -55,20 +64,44 @@ func NewCredSshKey(username string, publickey string, privatekey string, passphr
defer C.free(unsafe.Pointer(cprivatekey))
cpassphrase := C.CString(passphrase)
defer C.free(unsafe.Pointer(cpassphrase))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_cred_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
return int(ret), cred
if ret < 0 {
return nil, MakeGitError(ret)
}
return &cred, nil
}
func NewCredSshKeyFromAgent(username string) (int, Cred) {
func NewCredSshKeyFromAgent(username string) (*Cred, error) {
cred := Cred{}
cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername)
return int(ret), cred
if ret < 0 {
return nil, MakeGitError(ret)
}
return &cred, nil
}
func NewCredDefault() (int, Cred) {
func NewCredDefault() (*Cred, error) {
cred := Cred{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_cred_default_new(&cred.ptr)
return int(ret), cred
if ret < 0 {
return nil, MakeGitError(ret)
}
return &cred, nil
}

View File

@ -45,15 +45,31 @@ type TransferProgressCallback func(stats TransferProgress) int
type UpdateTipsCallback func(refname string, a *Oid, b *Oid) int
type RemoteCallbacks struct {
ProgressCallback
CompletionCallback
CredentialsCallback
TransferProgressCallback
UpdateTipsCallback
Progress ProgressCallback
Completion CompletionCallback
Credentials CredentialsCallback
TransferProgress TransferProgressCallback
UpdateTips UpdateTipsCallback
}
type Remote struct {
ptr *C.git_remote
ptr *C.git_remote
repo *Repository
Callbacks RemoteCallbacks
}
func newRemote(cremote *C.git_remote, repo *Repository) *Remote {
remote := &Remote{
ptr: cremote,
repo: repo,
}
var callbacks C.git_remote_callbacks
populateRemoteCallbacks(&callbacks, &remote.Callbacks)
runtime.SetFinalizer(remote, (*Remote).Free)
return remote
}
func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
@ -68,31 +84,31 @@ func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallb
//export progressCallback
func progressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
if callbacks.ProgressCallback == nil {
if callbacks.Progress == nil {
return 0
}
str := C.GoStringN(_str, _len)
return callbacks.ProgressCallback(str)
return callbacks.Progress(str)
}
//export completionCallback
func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
if callbacks.CompletionCallback == nil {
if callbacks.Completion == nil {
return 0
}
return callbacks.CompletionCallback((RemoteCompletion)(completion_type))
return callbacks.Completion(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)
if callbacks.CredentialsCallback == nil {
if callbacks.Credentials == nil {
return 0
}
url := C.GoString(_url)
username_from_url := C.GoString(_username_from_url)
ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types))
ret, cred := callbacks.Credentials(url, username_from_url, CredType(allowed_types))
*_cred = cred.ptr
return ret
}
@ -100,22 +116,22 @@ func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C
//export transferProgressCallback
func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
if callbacks.TransferProgressCallback == nil {
if callbacks.TransferProgress == nil {
return 0
}
return callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
return callbacks.TransferProgress(newTransferProgressFromC(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 {
if callbacks.UpdateTips == nil {
return 0
}
refname := C.GoString(_refname)
a := newOidFromC(_a)
b := newOidFromC(_b)
return callbacks.UpdateTipsCallback(refname, a, b)
return callbacks.UpdateTips(refname, a, b)
}
func RemoteIsValidName(name string) bool {
@ -129,11 +145,12 @@ func RemoteIsValidName(name string) bool {
func (r *Remote) Free() {
runtime.SetFinalizer(r, nil)
r.repo = nil
C.git_remote_free(r.ptr)
}
func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
remote := &Remote{}
var ptr *C.git_remote
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@ -143,16 +160,16 @@ func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_create(&remote.ptr, repo.ptr, cname, curl)
ret := C.git_remote_create(&ptr, repo.ptr, cname, curl)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
return newRemote(ptr, repo), nil
}
func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch string) (*Remote, error) {
remote := &Remote{}
var ptr *C.git_remote
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@ -164,16 +181,16 @@ func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_create_with_fetchspec(&remote.ptr, repo.ptr, cname, curl, cfetch)
ret := C.git_remote_create_with_fetchspec(&ptr, repo.ptr, cname, curl, cfetch)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
return newRemote(ptr, repo), nil
}
func (repo *Repository) CreateRemoteInMemory(fetch string, url string) (*Remote, error) {
remote := &Remote{}
var ptr *C.git_remote
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
@ -183,16 +200,16 @@ func (repo *Repository) CreateRemoteInMemory(fetch string, url string) (*Remote,
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_create_inmemory(&remote.ptr, repo.ptr, cfetch, curl)
ret := C.git_remote_create_inmemory(&ptr, repo.ptr, cfetch, curl)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
return newRemote(ptr, repo), nil
}
func (repo *Repository) LoadRemote(name string) (*Remote, error) {
remote := &Remote{}
var ptr *C.git_remote
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@ -200,12 +217,12 @@ func (repo *Repository) LoadRemote(name string) (*Remote, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_load(&remote.ptr, repo.ptr, cname)
ret := C.git_remote_load(&ptr, repo.ptr, cname)
if ret < 0 {
return nil, MakeGitError(ret)
}
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
return newRemote(ptr, repo), nil
}
func (o *Remote) Save() error {