Compare commits
4 Commits
main
...
cmn/remote
Author | SHA1 | Date |
---|---|---|
|
92bb4425bc | |
|
d805ba7409 | |
|
805085c1d7 | |
|
9fc307a58d |
|
@ -5,7 +5,10 @@ package git
|
||||||
#include <git2/errors.h>
|
#include <git2/errors.h>
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import "unsafe"
|
import (
|
||||||
|
"unsafe"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
type CredType uint
|
type CredType uint
|
||||||
|
|
||||||
|
@ -21,31 +24,37 @@ type Cred struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Cred) HasUsername() bool {
|
func (o *Cred) HasUsername() bool {
|
||||||
if C.git_cred_has_username(o.ptr) == 1 {
|
return C.git_cred_has_username(o.ptr) != 0
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Cred) 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 &Cred{ptr}
|
return &Cred{ptr}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCredUserpassPlaintext(username string, password string) (int, Cred) {
|
func NewCredUserpassPlaintext(username string, password string) (*Cred, error) {
|
||||||
cred := Cred{}
|
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)
|
||||||
defer C.free(unsafe.Pointer(cpassword))
|
defer C.free(unsafe.Pointer(cpassword))
|
||||||
|
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
ret := C.git_cred_userpass_plaintext_new(&cred.ptr, cusername, cpassword)
|
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{}
|
cred := Cred{}
|
||||||
cusername := C.CString(username)
|
cusername := C.CString(username)
|
||||||
defer C.free(unsafe.Pointer(cusername))
|
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))
|
defer C.free(unsafe.Pointer(cprivatekey))
|
||||||
cpassphrase := C.CString(passphrase)
|
cpassphrase := C.CString(passphrase)
|
||||||
defer C.free(unsafe.Pointer(cpassphrase))
|
defer C.free(unsafe.Pointer(cpassphrase))
|
||||||
|
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
ret := C.git_cred_ssh_key_new(&cred.ptr, cusername, cpublickey, cprivatekey, cpassphrase)
|
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{}
|
cred := Cred{}
|
||||||
cusername := C.CString(username)
|
cusername := C.CString(username)
|
||||||
defer C.free(unsafe.Pointer(cusername))
|
defer C.free(unsafe.Pointer(cusername))
|
||||||
|
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
ret := C.git_cred_ssh_key_from_agent(&cred.ptr, cusername)
|
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{}
|
cred := Cred{}
|
||||||
|
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
ret := C.git_cred_default_new(&cred.ptr)
|
ret := C.git_cred_default_new(&cred.ptr)
|
||||||
return int(ret), cred
|
if ret < 0 {
|
||||||
|
return nil, MakeGitError(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &cred, nil
|
||||||
}
|
}
|
||||||
|
|
81
remote.go
81
remote.go
|
@ -45,15 +45,31 @@ 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
|
||||||
|
|
||||||
type RemoteCallbacks struct {
|
type RemoteCallbacks struct {
|
||||||
ProgressCallback
|
Progress ProgressCallback
|
||||||
CompletionCallback
|
Completion CompletionCallback
|
||||||
CredentialsCallback
|
Credentials CredentialsCallback
|
||||||
TransferProgressCallback
|
TransferProgress TransferProgressCallback
|
||||||
UpdateTipsCallback
|
UpdateTips UpdateTipsCallback
|
||||||
}
|
}
|
||||||
|
|
||||||
type Remote struct {
|
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) {
|
func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
|
||||||
|
@ -68,31 +84,31 @@ func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallb
|
||||||
//export progressCallback
|
//export progressCallback
|
||||||
func progressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
|
func progressCallback(_str *C.char, _len C.int, data unsafe.Pointer) int {
|
||||||
callbacks := (*RemoteCallbacks)(data)
|
callbacks := (*RemoteCallbacks)(data)
|
||||||
if callbacks.ProgressCallback == nil {
|
if callbacks.Progress == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
str := C.GoStringN(_str, _len)
|
str := C.GoStringN(_str, _len)
|
||||||
return callbacks.ProgressCallback(str)
|
return callbacks.Progress(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
//export completionCallback
|
//export completionCallback
|
||||||
func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int {
|
func completionCallback(completion_type C.git_remote_completion_type, data unsafe.Pointer) int {
|
||||||
callbacks := (*RemoteCallbacks)(data)
|
callbacks := (*RemoteCallbacks)(data)
|
||||||
if callbacks.CompletionCallback == nil {
|
if callbacks.Completion == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return callbacks.CompletionCallback((RemoteCompletion)(completion_type))
|
return callbacks.Completion(RemoteCompletion(completion_type))
|
||||||
}
|
}
|
||||||
|
|
||||||
//export credentialsCallback
|
//export credentialsCallback
|
||||||
func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C.char, allowed_types uint, data unsafe.Pointer) int {
|
func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C.char, allowed_types uint, data unsafe.Pointer) int {
|
||||||
callbacks := (*RemoteCallbacks)(data)
|
callbacks := (*RemoteCallbacks)(data)
|
||||||
if callbacks.CredentialsCallback == nil {
|
if callbacks.Credentials == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
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.Credentials(url, username_from_url, CredType(allowed_types))
|
||||||
*_cred = cred.ptr
|
*_cred = cred.ptr
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
@ -100,22 +116,22 @@ func credentialsCallback(_cred **C.git_cred, _url *C.char, _username_from_url *C
|
||||||
//export transferProgressCallback
|
//export transferProgressCallback
|
||||||
func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int {
|
func transferProgressCallback(stats *C.git_transfer_progress, data unsafe.Pointer) int {
|
||||||
callbacks := (*RemoteCallbacks)(data)
|
callbacks := (*RemoteCallbacks)(data)
|
||||||
if callbacks.TransferProgressCallback == nil {
|
if callbacks.TransferProgress == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return callbacks.TransferProgressCallback(newTransferProgressFromC(stats))
|
return callbacks.TransferProgress(newTransferProgressFromC(stats))
|
||||||
}
|
}
|
||||||
|
|
||||||
//export updateTipsCallback
|
//export updateTipsCallback
|
||||||
func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int {
|
func updateTipsCallback(_refname *C.char, _a *C.git_oid, _b *C.git_oid, data unsafe.Pointer) int {
|
||||||
callbacks := (*RemoteCallbacks)(data)
|
callbacks := (*RemoteCallbacks)(data)
|
||||||
if callbacks.UpdateTipsCallback == nil {
|
if callbacks.UpdateTips == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
refname := C.GoString(_refname)
|
refname := C.GoString(_refname)
|
||||||
a := newOidFromC(_a)
|
a := newOidFromC(_a)
|
||||||
b := newOidFromC(_b)
|
b := newOidFromC(_b)
|
||||||
return callbacks.UpdateTipsCallback(refname, a, b)
|
return callbacks.UpdateTips(refname, a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoteIsValidName(name string) bool {
|
func RemoteIsValidName(name string) bool {
|
||||||
|
@ -129,11 +145,12 @@ func RemoteIsValidName(name string) bool {
|
||||||
|
|
||||||
func (r *Remote) Free() {
|
func (r *Remote) Free() {
|
||||||
runtime.SetFinalizer(r, nil)
|
runtime.SetFinalizer(r, nil)
|
||||||
|
r.repo = nil
|
||||||
C.git_remote_free(r.ptr)
|
C.git_remote_free(r.ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
|
func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
|
||||||
remote := &Remote{}
|
var ptr *C.git_remote
|
||||||
|
|
||||||
cname := C.CString(name)
|
cname := C.CString(name)
|
||||||
defer C.free(unsafe.Pointer(cname))
|
defer C.free(unsafe.Pointer(cname))
|
||||||
|
@ -143,16 +160,16 @@ func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
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 {
|
if ret < 0 {
|
||||||
return nil, MakeGitError(ret)
|
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) {
|
func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch string) (*Remote, error) {
|
||||||
remote := &Remote{}
|
var ptr *C.git_remote
|
||||||
|
|
||||||
cname := C.CString(name)
|
cname := C.CString(name)
|
||||||
defer C.free(unsafe.Pointer(cname))
|
defer C.free(unsafe.Pointer(cname))
|
||||||
|
@ -164,16 +181,16 @@ func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
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 {
|
if ret < 0 {
|
||||||
return nil, MakeGitError(ret)
|
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) {
|
func (repo *Repository) CreateRemoteInMemory(fetch string, url string) (*Remote, error) {
|
||||||
remote := &Remote{}
|
var ptr *C.git_remote
|
||||||
|
|
||||||
curl := C.CString(url)
|
curl := C.CString(url)
|
||||||
defer C.free(unsafe.Pointer(curl))
|
defer C.free(unsafe.Pointer(curl))
|
||||||
|
@ -183,16 +200,16 @@ func (repo *Repository) CreateRemoteInMemory(fetch string, url string) (*Remote,
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
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 {
|
if ret < 0 {
|
||||||
return nil, MakeGitError(ret)
|
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) {
|
func (repo *Repository) LoadRemote(name string) (*Remote, error) {
|
||||||
remote := &Remote{}
|
var ptr *C.git_remote
|
||||||
|
|
||||||
cname := C.CString(name)
|
cname := C.CString(name)
|
||||||
defer C.free(unsafe.Pointer(cname))
|
defer C.free(unsafe.Pointer(cname))
|
||||||
|
@ -200,12 +217,12 @@ func (repo *Repository) LoadRemote(name string) (*Remote, error) {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
defer runtime.UnlockOSThread()
|
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 {
|
if ret < 0 {
|
||||||
return nil, MakeGitError(ret)
|
return nil, MakeGitError(ret)
|
||||||
}
|
}
|
||||||
runtime.SetFinalizer(remote, (*Remote).Free)
|
|
||||||
return remote, nil
|
return newRemote(ptr, repo), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *Remote) Save() error {
|
func (o *Remote) Save() error {
|
||||||
|
|
Loading…
Reference in New Issue