cleanup clone code

This commit is contained in:
Jesse Ezell 2014-03-11 13:19:12 -07:00
parent 634acbe498
commit d560b9e9bd
5 changed files with 37 additions and 65 deletions

View File

@ -4,11 +4,6 @@ package git
#include <git2.h> #include <git2.h>
#include <git2/errors.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 "C"
import ( import (
@ -60,7 +55,9 @@ func Clone(url string, path string, options *CloneOptions) (*Repository, error)
} }
func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) { func populateCloneOptions(ptr *C.git_clone_options, opts *CloneOptions) {
*ptr = C.git_clone_options_init() ptr = &C.git_clone_options{}
C.git_clone_init_options(ptr, 1)
if opts == nil { if opts == nil {
return return
} }

View File

@ -8,39 +8,35 @@ import "C"
import "unsafe" import "unsafe"
type CredType uint type CredType uint
const ( const (
CredTypeUserpassPlaintext CredType = C.GIT_CREDTYPE_USERPASS_PLAINTEXT CredTypeUserpassPlaintext CredType = C.GIT_CREDTYPE_USERPASS_PLAINTEXT
CredTypeSshKey = C.GIT_CREDTYPE_SSH_KEY CredTypeSshKey = C.GIT_CREDTYPE_SSH_KEY
CredTypeSshCustom = C.GIT_CREDTYPE_SSH_CUSTOM CredTypeSshCustom = C.GIT_CREDTYPE_SSH_CUSTOM
CredTypeDefault = C.GIT_CREDTYPE_DEFAULT CredTypeDefault = C.GIT_CREDTYPE_DEFAULT
) )
type Cred interface { type Cred struct {
HasUsername() bool
Type() CredType
}
type gitCred struct {
ptr *C.git_cred ptr *C.git_cred
} }
func (o gitCred) HasUsername() bool { func (o *Cred) HasUsername() bool {
if C.git_cred_has_username(o.ptr) == 1 { if C.git_cred_has_username(o.ptr) == 1 {
return true return true
} }
return false return false
} }
func (o gitCred) Type() CredType { func (o *Cred) Type() CredType {
return (CredType)(o.ptr.credtype); return (CredType)(o.ptr.credtype)
} }
func credFromC(ptr *C.git_cred) Cred { func credFromC(ptr *C.git_cred) *Cred {
return gitCred{ptr} return &Cred{ptr}
} }
func NewCredUserpassPlaintext(username string, password string) (int, Cred) { func NewCredUserpassPlaintext(username string, password string) (int, Cred) {
cred := gitCred{} cred := Cred{}
cusername := C.CString(username) cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername)) defer C.free(unsafe.Pointer(cusername))
cpassword := C.CString(password) cpassword := C.CString(password)
@ -50,7 +46,7 @@ func NewCredUserpassPlaintext(username string, password string) (int, Cred) {
} }
func NewCredSshKey(username string, publickey string, privatekey string, passphrase string) (int, Cred) { func NewCredSshKey(username string, publickey string, privatekey string, passphrase string) (int, Cred) {
cred := gitCred{} cred := Cred{}
cusername := C.CString(username) cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername)) defer C.free(unsafe.Pointer(cusername))
cpublickey := C.CString(publickey) cpublickey := C.CString(publickey)
@ -64,7 +60,7 @@ func NewCredSshKey(username string, publickey string, privatekey string, passphr
} }
func NewCredSshKeyFromAgent(username string) (int, Cred) { func NewCredSshKeyFromAgent(username string) (int, Cred) {
cred := gitCred{} cred := Cred{}
cusername := C.CString(username) cusername := C.CString(username)
defer C.free(unsafe.Pointer(cusername)) defer C.free(unsafe.Pointer(cusername))
ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername) ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername)
@ -72,8 +68,7 @@ func NewCredSshKeyFromAgent(username string) (int, Cred) {
} }
func NewCredDefault() (int, Cred) { func NewCredDefault() (int, Cred) {
cred := gitCred{} cred := Cred{}
ret := C.git_cred_default_new(&cred.ptr) ret := C.git_cred_default_new(&cred.ptr)
return int(ret), cred return int(ret), cred
} }

View File

@ -59,9 +59,6 @@ func (p *Push) Finish() error {
func (p *Push) UnpackOk() bool { func (p *Push) UnpackOk() bool {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_push_unpack_ok(p.ptr) ret := C.git_push_unpack_ok(p.ptr)
if ret == 0 { if ret == 0 {
return false return false

View File

@ -5,7 +5,6 @@ package git
#include <git2/errors.h> #include <git2/errors.h>
extern void _go_git_setup_callbacks(git_remote_callbacks *callbacks); extern void _go_git_setup_callbacks(git_remote_callbacks *callbacks);
extern git_remote_callbacks _go_git_remote_callbacks_init();
extern void _go_git_set_strarray_n(git_strarray *array, char *str, size_t n); extern void _go_git_set_strarray_n(git_strarray *array, char *str, size_t n);
extern char *_go_git_get_strarray_n(git_strarray *array, size_t n); extern char *_go_git_get_strarray_n(git_strarray *array, size_t n);
@ -15,7 +14,22 @@ import "unsafe"
import "runtime" import "runtime"
type TransferProgress struct { type TransferProgress struct {
ptr *C.git_transfer_progress TotalObjects uint
IndexedObjects uint
ReceivedObjects uint
LocalObjects uint
TotalDeltas uint
ReceivedBytes uint
}
func newTransferProgressFromC(c *C.git_transfer_progress) TransferProgress {
return TransferProgress{
TotalObjects: uint(c.total_objects),
IndexedObjects: uint(c.indexed_objects),
ReceivedObjects: uint(c.received_objects),
LocalObjects: uint(c.local_objects),
TotalDeltas: uint(c.total_deltas),
ReceivedBytes: uint(c.received_bytes)}
} }
type RemoteCompletion uint type RemoteCompletion uint
@ -28,7 +42,7 @@ const (
type ProgressCallback func(str string) int type ProgressCallback func(str string) int
type CompletionCallback func(RemoteCompletion) int type CompletionCallback func(RemoteCompletion) int
type CredentialsCallback func(url string, username_from_url string, allowed_types CredType) (int, Cred) type CredentialsCallback func(url string, username_from_url string, allowed_types CredType) (int, *Cred)
type TransferProgressCallback func(stats TransferProgress) int type TransferProgressCallback func(stats TransferProgress) int
type UpdateTipsCallback func(refname string, a *Oid, b *Oid) int type UpdateTipsCallback func(refname string, a *Oid, b *Oid) int
@ -45,7 +59,7 @@ type Remote struct {
} }
func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) { func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
*ptr = C._go_git_remote_callbacks_init() C.git_remote_init_callbacks(ptr, C.GIT_REMOTE_CALLBACKS_VERSION)
if callbacks == nil { if callbacks == nil {
return return
} }
@ -81,9 +95,7 @@ func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C
url := C.GoString(_url) url := C.GoString(_url)
username_from_url := C.GoString(_username_from_url) username_from_url := C.GoString(_username_from_url)
ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types)) ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types))
if gcred, ok := cred.(gitCred); ok { *_cred = cred.ptr
*_cred = gcred.ptr
}
return ret return ret
} }
@ -93,7 +105,7 @@ func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointe
if callbacks.TransferProgressCallback == nil { if callbacks.TransferProgressCallback == nil {
return 0 return 0
} }
return callbacks.TransferProgressCallback(TransferProgress{stats}) return callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
} }
//export updateTipsCallback //export updateTipsCallback
@ -108,30 +120,6 @@ func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data uns
return callbacks.UpdateTipsCallback(refname, a, b) return callbacks.UpdateTipsCallback(refname, a, b)
} }
func (o TransferProgress) TotalObjects() uint {
return uint(o.ptr.total_objects)
}
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)
}
func RemoteIsValidName(name string) bool { func RemoteIsValidName(name string) bool {
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))

View File

@ -38,11 +38,6 @@ void _go_git_setup_callbacks(git_remote_callbacks *callbacks) {
callbacks->update_tips = (update_tips_cb)updateTipsCallback; callbacks->update_tips = (update_tips_cb)updateTipsCallback;
} }
git_remote_callbacks _go_git_remote_callbacks_init() {
git_remote_callbacks ret = GIT_REMOTE_CALLBACKS_INIT;
return ret;
}
void _go_git_set_strarray_n(git_strarray *array, char *str, size_t n) { void _go_git_set_strarray_n(git_strarray *array, char *str, size_t n) {
array->strings[n] = str; array->strings[n] = str;
} }