git2go/remote.go

402 lines
9.8 KiB
Go
Raw Normal View History

2014-01-03 18:40:21 -06:00
package git
/*
#include <git2.h>
#include <git2/errors.h>
2014-01-06 10:55:29 -06:00
extern void _go_git_setup_callbacks(git_remote_callbacks *callbacks);
2014-01-06 14:05:35 -06:00
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);
2014-01-03 18:40:21 -06:00
*/
import "C"
import "unsafe"
2014-01-06 14:05:35 -06:00
import "runtime"
type TransferProgress struct {
2014-03-11 15:19:12 -05:00
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)}
}
2014-01-03 18:40:21 -06:00
type RemoteCompletion uint
2014-01-03 18:40:21 -06:00
const (
RemoteCompletionDownload RemoteCompletion = C.GIT_REMOTE_COMPLETION_DOWNLOAD
RemoteCompletionIndexing = C.GIT_REMOTE_COMPLETION_INDEXING
RemoteCompletionError = C.GIT_REMOTE_COMPLETION_ERROR
2014-01-03 18:40:21 -06:00
)
type ProgressCallback func(str string) int
type CompletionCallback func(RemoteCompletion) int
2014-03-11 15:19:12 -05:00
type CredentialsCallback func(url string, username_from_url string, allowed_types CredType) (int, *Cred)
type TransferProgressCallback func(stats TransferProgress) int
2014-01-03 18:40:21 -06:00
type UpdateTipsCallback func(refname string, a *Oid, b *Oid) int
type RemoteCallbacks struct {
ProgressCallback
CompletionCallback
CredentialsCallback
TransferProgressCallback
UpdateTipsCallback
}
2014-02-27 18:36:44 -06:00
type Remote struct {
2014-01-06 14:05:35 -06:00
ptr *C.git_remote
}
func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
2014-03-11 15:19:12 -05:00
C.git_remote_init_callbacks(ptr, C.GIT_REMOTE_CALLBACKS_VERSION)
if callbacks == nil {
return
}
2014-01-06 10:55:29 -06:00
C._go_git_setup_callbacks(ptr)
ptr.payload = unsafe.Pointer(callbacks)
}
2014-01-03 18:40:21 -06:00
//export progressCallback
func progressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data)
if callbacks.ProgressCallback == nil {
return 0
}
str := C.GoStringN(_str, _len)
2014-01-03 18:40:21 -06:00
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
}
2014-01-03 18:40:21 -06:00
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)
if callbacks.CredentialsCallback == nil {
return 0
}
2014-01-03 18:40:21 -06:00
url := C.GoString(_url)
username_from_url := C.GoString(_username_from_url)
ret, cred := callbacks.CredentialsCallback(url, username_from_url, (CredType)(allowed_types))
2014-03-11 15:19:12 -05:00
*_cred = cred.ptr
return ret
2014-01-03 18:40:21 -06:00
}
//export transferProgressCallback
func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int {
2014-01-03 18:40:21 -06:00
callbacks := (*RemoteCallbacks)(data)
if callbacks.TransferProgressCallback == nil {
return 0
}
2014-03-11 15:19:12 -05:00
return callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
2014-01-03 18:40:21 -06:00
}
//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
}
2014-01-03 18:40:21 -06:00
refname := C.GoString(_refname)
a := newOidFromC(_a)
b := newOidFromC(_b)
return callbacks.UpdateTipsCallback(refname, a, b)
}
2014-01-06 14:05:35 -06:00
func RemoteIsValidName(name string) bool {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
if C.git_remote_is_valid_name(cname) == 1 {
return true
}
return false
}
2014-02-27 18:36:44 -06:00
func (r *Remote) Free() {
runtime.SetFinalizer(r, nil)
C.git_remote_free(r.ptr)
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
remote := &Remote{}
2014-01-06 14:05:35 -06:00
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-01-06 14:05:35 -06:00
ret := C.git_remote_create(&remote.ptr, repo.ptr, cname, curl)
if ret < 0 {
return nil, MakeGitError(ret)
}
2014-02-27 18:36:44 -06:00
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch string) (*Remote, error) {
remote := &Remote{}
2014-01-06 14:05:35 -06:00
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
cfetch := C.CString(fetch)
defer C.free(unsafe.Pointer(cfetch))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-01-06 14:05:35 -06:00
ret := C.git_remote_create_with_fetchspec(&remote.ptr, repo.ptr, cname, curl, cfetch)
if ret < 0 {
return nil, MakeGitError(ret)
}
2014-02-27 18:36:44 -06:00
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (repo *Repository) CreateRemoteInMemory(fetch string, url string) (*Remote, error) {
remote := &Remote{}
2014-01-06 14:05:35 -06:00
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
cfetch := C.CString(fetch)
defer C.free(unsafe.Pointer(cfetch))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-01-06 14:05:35 -06:00
ret := C.git_remote_create_inmemory(&remote.ptr, repo.ptr, cfetch, curl)
if ret < 0 {
return nil, MakeGitError(ret)
}
2014-02-27 18:36:44 -06:00
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (repo *Repository) LoadRemote(name string) (*Remote, error) {
remote := &Remote{}
2014-01-06 14:05:35 -06:00
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-01-06 14:05:35 -06:00
ret := C.git_remote_load(&remote.ptr, repo.ptr, cname)
if ret < 0 {
return nil, MakeGitError(ret)
}
2014-02-27 18:36:44 -06:00
runtime.SetFinalizer(remote, (*Remote).Free)
return remote, nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) Save() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_save(o.ptr)
if ret < 0 {
return MakeGitError(ret)
}
return nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) Owner() Repository {
2014-01-06 14:05:35 -06:00
return Repository{C.git_remote_owner(o.ptr)}
}
2014-01-06 14:05:35 -06:00
2014-02-27 18:36:44 -06:00
func (o *Remote) Name() string {
2014-01-06 14:05:35 -06:00
return C.GoString(C.git_remote_name(o.ptr))
}
2014-02-27 18:36:44 -06:00
func (o *Remote) Url() string {
2014-01-06 14:05:35 -06:00
return C.GoString(C.git_remote_url(o.ptr))
}
2014-02-27 18:36:44 -06:00
func (o *Remote) PushUrl() string {
2014-01-06 14:05:35 -06:00
return C.GoString(C.git_remote_pushurl(o.ptr))
}
2014-02-27 18:36:44 -06:00
func (o *Remote) SetUrl(url string) error {
2014-01-06 14:05:35 -06:00
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_set_url(o.ptr, curl)
if ret < 0 {
return MakeGitError(ret)
}
return nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) SetPushUrl(url string) error {
2014-01-06 14:05:35 -06:00
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_set_pushurl(o.ptr, curl)
if ret < 0 {
return MakeGitError(ret)
}
return nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) AddFetch(refspec string) error {
2014-01-06 14:05:35 -06:00
crefspec := C.CString(refspec)
defer C.free(unsafe.Pointer(crefspec))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_add_fetch(o.ptr, crefspec)
if ret < 0 {
return MakeGitError(ret)
}
return nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) GetFetchRefspecs() ([]string, error) {
2014-01-06 14:05:35 -06:00
crefspecs := C.git_strarray{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_get_fetch_refspecs(&crefspecs, o.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
2014-01-06 14:05:35 -06:00
defer C.git_strarray_free(&crefspecs)
refspecs := make([]string, crefspecs.count)
2014-01-06 14:05:35 -06:00
for i := 0; i < int(crefspecs.count); i++ {
refspecs[i] = C.GoString(C._go_git_get_strarray_n(&crefspecs, C.size_t(i)))
}
return refspecs, nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) SetFetchRefspecs(refspecs []string) error {
2014-01-06 14:05:35 -06:00
crefspecs := C.git_strarray{}
crefspecs.count = C.size_t(len(refspecs))
crefspecs.strings = (**C.char)(C.malloc(C.size_t(unsafe.Sizeof(unsafe.Pointer(nil)) * uintptr(crefspecs.count))))
for i, refspec := range refspecs {
C._go_git_set_strarray_n(&crefspecs, C.CString(refspec), C.size_t(i))
}
defer C.git_strarray_free(&crefspecs)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_set_fetch_refspecs(o.ptr, &crefspecs)
if ret < 0 {
return MakeGitError(ret)
}
return nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) AddPush(refspec string) error {
2014-01-06 14:05:35 -06:00
crefspec := C.CString(refspec)
defer C.free(unsafe.Pointer(crefspec))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_add_push(o.ptr, crefspec)
if ret < 0 {
return MakeGitError(ret)
}
return nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) GetPushRefspecs() ([]string, error) {
2014-01-06 14:05:35 -06:00
crefspecs := C.git_strarray{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_get_push_refspecs(&crefspecs, o.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
2014-01-06 14:05:35 -06:00
defer C.git_strarray_free(&crefspecs)
refspecs := make([]string, crefspecs.count)
2014-01-06 14:05:35 -06:00
for i := 0; i < int(crefspecs.count); i++ {
refspecs[i] = C.GoString(C._go_git_get_strarray_n(&crefspecs, C.size_t(i)))
}
return refspecs, nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) SetPushRefspecs(refspecs []string) error {
2014-01-06 14:05:35 -06:00
crefspecs := C.git_strarray{}
crefspecs.count = C.size_t(len(refspecs))
crefspecs.strings = (**C.char)(C.malloc(C.size_t(unsafe.Sizeof(unsafe.Pointer(nil)) * uintptr(crefspecs.count))))
for i, refspec := range refspecs {
C._go_git_set_strarray_n(&crefspecs, C.CString(refspec), C.size_t(i))
}
defer C.git_strarray_free(&crefspecs)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_remote_set_push_refspecs(o.ptr, &crefspecs)
if ret < 0 {
return MakeGitError(ret)
}
return nil
2014-01-06 14:05:35 -06:00
}
2014-02-27 18:36:44 -06:00
func (o *Remote) ClearRefspecs() {
2014-01-06 14:05:35 -06:00
C.git_remote_clear_refspecs(o.ptr)
}
2014-02-27 18:36:44 -06:00
func (o *Remote) RefspecCount() uint {
2014-01-06 14:05:35 -06:00
return uint(C.git_remote_refspec_count(o.ptr))
}
2014-02-27 18:36:44 -06:00
func (o *Remote) Fetch(sig *Signature, msg string) error {
var csig *C.git_signature = nil
if sig != nil {
csig = sig.toC()
defer C.free(unsafe.Pointer(csig))
}
var cmsg *C.char
if msg == "" {
cmsg = nil
} else {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
2014-02-27 18:36:44 -06:00
ret := C.git_remote_fetch(o.ptr, csig, cmsg)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}