Wrap some remote functions

This commit is contained in:
Carlos Martín Nieto 2013-09-14 17:39:38 +02:00
parent d8c3772e35
commit 7b9e3895da
2 changed files with 136 additions and 0 deletions

106
remote.go Normal file
View File

@ -0,0 +1,106 @@
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type RemoteDirection int
const (
RemoteDirectionFetch RemoteDirection = C.GIT_DIRECTION_FETCH
RemoteDirectionPush = C.GIT_DIRECTION_PUSH
)
type AutotagOption int
const (
AutotagAuto AutotagOption = C.GIT_REMOTE_DOWNLOAD_TAGS_AUTO
AutotagNone = C.GIT_REMOTE_DOWNLOAD_TAGS_NONE
AutotagAll = C.GIT_REMOTE_DOWNLOAD_TAGS_ALL
)
type Remote struct {
Name string
Url string
ptr *C.git_remote
}
func (r *Remote) Connect(direction RemoteDirection) error {
ret := C.git_remote_connect(r.ptr, C.git_direction(direction))
if ret < 0 {
return LastError()
}
return nil
}
func (r *Remote) IsConnected() bool {
return C.git_remote_connected(r.ptr) != 0
}
func (r *Remote) Disconnect() {
C.git_remote_disconnect(r.ptr)
}
func (r *Remote) Autotag() AutotagOption {
return AutotagOption(C.git_remote_autotag(r.ptr))
}
func (r *Remote) SetAutotag(opt AutotagOption) {
C.git_remote_set_autotag(r.ptr, C.git_remote_autotag_option_t(opt))
}
func (r *Remote) Stop() {
C.git_remote_stop(r.ptr)
}
func (r *Remote) Save() error {
ret := C.git_remote_save(r.ptr)
if ret < 0 {
return LastError()
}
return nil
}
func (r *Remote) Free() {
runtime.SetFinalizer(r, nil)
C.git_remote_free(r.ptr)
}
func newRemoteFromC(ptr *C.git_remote) *Remote {
remote := &Remote{
ptr: ptr,
Name: C.GoString(C.git_remote_name(ptr)),
Url: C.GoString(C.git_remote_url(ptr)),
}
runtime.SetFinalizer(remote, (*Remote).Free)
return remote
}
// These belong to the git_remote namespace but don't require any remote
func UrlIsValid(url string) bool {
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
return C.git_remote_valid_url(curl) != 0
}
func UrlIsSupported(url string) bool {
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
return C.git_remote_supported_url(curl) != 0
}

View File

@ -125,6 +125,20 @@ func (v *Repository) LookupReference(name string) (*Reference, error) {
return newReferenceFromC(ptr), nil
}
func (v *Repository) LookupRemote(name string) (*Remote, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var remote *C.git_remote
ecode := C.git_remote_load(&remote, v.ptr, cname)
if ecode < 0 {
return nil, LastError()
}
return newRemoteFromC(remote), nil
}
func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
@ -207,6 +221,22 @@ func (v *Repository) CreateCommit(
return oid, nil
}
func (v *Repository) CreateRemote(name, url string) (*Remote, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
curl := C.CString(url)
defer C.free(unsafe.Pointer(curl))
var remote *C.git_remote
ret := C.git_remote_create(&remote, v.ptr, cname, curl)
if ret < 0 {
return nil, LastError()
}
return newRemoteFromC(remote), nil
}
func (v *Odb) Free() {
runtime.SetFinalizer(v, nil)
C.git_odb_free(v.ptr)