work in progress wrapping git_clone

This commit is contained in:
Jason Toffaletti 2014-01-04 00:40:21 +00:00
parent 313e1126dd
commit e825d66fba
4 changed files with 169 additions and 1 deletions

69
clone.go Normal file
View File

@ -0,0 +1,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
}
}

2
git.go
View File

@ -1,7 +1,7 @@
package git package git
/* /*
#cgo pkg-config: libgit2 #cgo pkg-config: --static libgit2
#include <git2.h> #include <git2.h>
#include <git2/errors.h> #include <git2/errors.h>
*/ */

85
remote.go Normal file
View File

@ -0,0 +1,85 @@
package git
/*
#include <git2.h>
#include <git2/errors.h>
static git_remote_callbacks git_remote_callbacks_init() {
git_remote_callbacks ret = GIT_REMOTE_CALLBACKS_INIT;
return ret;
}
extern void _setup_callbacks(git_remote_callbacks *callbacks);
*/
import "C"
import (
"unsafe"
)
type RemoteCompletion uint
const (
RemoteCompletionDownload RemoteCompletion = C.GIT_REMOTE_COMPLETION_DOWNLOAD
RemoteCompletionIndexing = C.GIT_REMOTE_COMPLETION_INDEXING
RemoteCompletionError = C.GIT_REMOTE_COMPLETION_ERROR
)
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 UpdateTipsCallback func(refname string, a *Oid, b *Oid) int
//export progressCallback
func progressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
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)
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)
url := C.GoString(_url)
username_from_url := C.GoString(_username_from_url)
return callbacks.CredentialsCallback(url, username_from_url, allowed_types)
}
//export transferProgressCallback
func transferProgressCallback(stats C.git_transfer_progress, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
return callbacks.TransferProgressCallback()
}
//export updateTipsCallback
func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
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 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)
}

View File

@ -24,4 +24,18 @@ int _go_git_odb_foreach(git_odb *db, void *payload)
{ {
return git_odb_foreach(db, (git_odb_foreach_cb)&odbForEachCb, payload); return git_odb_foreach(db, (git_odb_foreach_cb)&odbForEachCb, payload);
} }
void _setup_callbacks(git_remote_callbacks *callbacks) {
typedef int (*progress_cb)(const char *str, int len, void *data);
typedef int (*completion_cb)(git_remote_completion_type type, void *data);
typedef int (*credentials_cb)(git_cred **cred, const char *url, const char *username_from_url, unsigned int allowed_types, void *data);
typedef int (*transfer_progress_cb)(const git_transfer_progress *stats, void *data);
typedef int (*update_tips_cb)(const char *refname, const git_oid *a, const git_oid *b, void *data);
callbacks->progress = (progress_cb)progressCallback;
callbacks->completion = (completion_cb)completionCallback;
callbacks->credentials = (credentials_cb)credentialsCallback;
callbacks->transfer_progress = (transfer_progress_cb)transferProgressCallback;
callbacks->update_tips = (update_tips_cb)updateTipsCallback;
}
/* EOF */ /* EOF */