summaryrefslogtreecommitdiff
path: root/remote.go
diff options
context:
space:
mode:
authorCarlos Martín Nieto <[email protected]>2014-10-27 17:42:28 +0100
committerCarlos Martín Nieto <[email protected]>2014-10-27 17:42:28 +0100
commit9c6db70fc2836b6f5eb56c505f9cc82461b999c3 (patch)
treea8e952a5f5fe9eeca236d4290c32da37a9251c03 /remote.go
parent99d10775d66ffceca36c1fb0b947d8209f5153f8 (diff)
parentd722c11f7f6eb85d287da6091cdc84d6f8ebe8fa (diff)
Merge pull request #127 from calavera/ls_remote
Add Remote#Ls.
Diffstat (limited to 'remote.go')
-rw-r--r--remote.go86
1 files changed, 82 insertions, 4 deletions
diff --git a/remote.go b/remote.go
index 2ce3be8..edb2b38 100644
--- a/remote.go
+++ b/remote.go
@@ -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
+}