diff options
Diffstat (limited to 'remote.go')
| -rw-r--r-- | remote.go | 86 |
1 files changed, 82 insertions, 4 deletions
@@ -9,9 +9,11 @@ extern void _go_git_setup_callbacks(git_remote_callbacks *callbacks); */ import "C" import ( - "unsafe" - "runtime" "crypto/x509" + "reflect" + "runtime" + "strings" + "unsafe" ) type TransferProgress struct { @@ -34,11 +36,15 @@ func newTransferProgressFromC(c *C.git_transfer_progress) TransferProgress { } type RemoteCompletion uint +type ConnectDirection uint const ( RemoteCompletionDownload RemoteCompletion = C.GIT_REMOTE_COMPLETION_DOWNLOAD RemoteCompletionIndexing = C.GIT_REMOTE_COMPLETION_INDEXING RemoteCompletionError = C.GIT_REMOTE_COMPLETION_ERROR + + ConnectDirectionFetch ConnectDirection = C.GIT_DIRECTION_FETCH + ConnectDirectionPush = C.GIT_DIRECTION_PUSH ) type TransportMessageCallback func(str string) int @@ -58,7 +64,7 @@ type RemoteCallbacks struct { } type Remote struct { - ptr *C.git_remote + ptr *C.git_remote callbacks RemoteCallbacks } @@ -95,6 +101,18 @@ type HostkeyCertificate struct { HashSHA1 [20]byte } +type RemoteHead struct { + Id *Oid + Name string +} + +func newRemoteHeadFromC(ptr *C.git_remote_head) RemoteHead { + return RemoteHead{ + Id: newOidFromC(&ptr.oid), + Name: C.GoString(ptr.name), + } +} + func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) { C.git_remote_init_callbacks(ptr, C.GIT_REMOTE_CALLBACKS_VERSION) if callbacks == nil { @@ -195,7 +213,6 @@ func certificateCheckCallback(_cert *C.git_cert, _valid C.int, _host *C.char, da return -1 // we don't support anything else atm } - return callbacks.CertificateCheckCallback(&cert, valid, host) } @@ -548,3 +565,64 @@ func (o *Remote) Fetch(refspecs []string, sig *Signature, msg string) error { } return nil } + +func (o *Remote) ConnectFetch() error { + return o.Connect(ConnectDirectionFetch) +} + +func (o *Remote) ConnectPush() error { + return o.Connect(ConnectDirectionPush) +} + +func (o *Remote) Connect(direction ConnectDirection) error { + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + if ret := C.git_remote_connect(o.ptr, C.git_direction(direction)); ret != 0 { + return MakeGitError(ret) + } + return nil +} + +func (o *Remote) Ls(filterRefs ...string) ([]RemoteHead, error) { + + var refs **C.git_remote_head + var length C.size_t + + if ret := C.git_remote_ls(&refs, &length, o.ptr); ret != 0 { + return nil, MakeGitError(ret) + } + + size := int(length) + + if size == 0 { + return make([]RemoteHead, 0), nil + } + + hdr := reflect.SliceHeader{ + Data: uintptr(unsafe.Pointer(refs)), + Len: size, + Cap: size, + } + + goSlice := *(*[]*C.git_remote_head)(unsafe.Pointer(&hdr)) + + var heads []RemoteHead + + for _, s := range goSlice { + head := newRemoteHeadFromC(s) + + if len(filterRefs) > 0 { + for _, r := range filterRefs { + if strings.Contains(head.Name, r) { + heads = append(heads, head) + break + } + } + } else { + heads = append(heads, head) + } + } + + return heads, nil +} |
