move the callbacks into the struct
This commit is contained in:
parent
516586d2e7
commit
f33c856845
42
remote.go
42
remote.go
|
@ -29,9 +29,17 @@ const (
|
|||
AutotagAll = C.GIT_REMOTE_DOWNLOAD_TAGS_ALL
|
||||
)
|
||||
|
||||
type ProgressCb func([]byte) int
|
||||
type UpdateTipsCb func(string, *Oid, *Oid) int
|
||||
|
||||
type Remote struct {
|
||||
Name string
|
||||
Url string
|
||||
|
||||
// callbacks
|
||||
Progress ProgressCb
|
||||
UpdateTips UpdateTipsCb
|
||||
|
||||
ptr *C.git_remote
|
||||
}
|
||||
|
||||
|
@ -90,26 +98,22 @@ func (r *Remote) Ls() ([]*RemoteHead, error) {
|
|||
return data.slice, nil
|
||||
}
|
||||
|
||||
func (r *Remote) SetCallbacks(callbacks *RemoteCallbacks) {
|
||||
C._go_git_remote_set_callbacks(r.ptr, unsafe.Pointer(callbacks))
|
||||
}
|
||||
|
||||
//export remoteProgress
|
||||
func remoteProgress(str *C.char, length C.int, data unsafe.Pointer) int {
|
||||
callbacks := (*RemoteCallbacks)(data)
|
||||
if callbacks.Progress != nil {
|
||||
return callbacks.Progress(C.GoBytes(unsafe.Pointer(str), length))
|
||||
remote := (*Remote)(data)
|
||||
if remote.Progress != nil {
|
||||
return remote.Progress(C.GoBytes(unsafe.Pointer(str), length))
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
//export updateTips
|
||||
func updateTips(str *C.char, a, b *C.git_oid, data unsafe.Pointer) int {
|
||||
callbacks := (*RemoteCallbacks)(data)
|
||||
goa, gob := newOidFromC(a), newOidFromC(b)
|
||||
if callbacks.UpdateTips != nil {
|
||||
return callbacks.UpdateTips(C.GoString(str), goa, gob)
|
||||
//export remoteUpdateTips
|
||||
func remoteUpdateTips(str *C.char, a, b *C.git_oid, data unsafe.Pointer) int {
|
||||
remote := (*Remote)(data)
|
||||
if remote.UpdateTips != nil {
|
||||
goa, gob := newOidFromC(a), newOidFromC(b)
|
||||
return remote.UpdateTips(C.GoString(str), goa, gob)
|
||||
}
|
||||
|
||||
return 0
|
||||
|
@ -124,15 +128,6 @@ func (r *Remote) Download() (error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
type ProgressCb func([]byte) int
|
||||
type UpdateTipsCb func(string, *Oid, *Oid) int
|
||||
|
||||
//export RemoteCallbacks
|
||||
type RemoteCallbacks struct {
|
||||
Progress ProgressCb
|
||||
UpdateTips UpdateTipsCb
|
||||
}
|
||||
|
||||
type headlistData struct {
|
||||
slice []*RemoteHead
|
||||
}
|
||||
|
@ -159,6 +154,9 @@ func newRemoteFromC(ptr *C.git_remote) *Remote {
|
|||
Url: C.GoString(C.git_remote_url(ptr)),
|
||||
}
|
||||
|
||||
// allways set the callbacks, we'll decide whether to call
|
||||
// them once we're back in go-land
|
||||
C._go_git_remote_set_callbacks(remote.ptr, unsafe.Pointer(remote))
|
||||
runtime.SetFinalizer(remote, (*Remote).Free)
|
||||
|
||||
return remote
|
||||
|
|
|
@ -42,14 +42,11 @@ func TestRemoteProgress(t *testing.T) {
|
|||
checkFatal(t, err)
|
||||
|
||||
called := false
|
||||
cbs := RemoteCallbacks{
|
||||
Progress: func(bytes []byte) int {
|
||||
called = true
|
||||
return 1
|
||||
},
|
||||
remote.Progress = func(bytes []byte) int {
|
||||
called = true
|
||||
return 1
|
||||
}
|
||||
|
||||
remote.SetCallbacks(&cbs)
|
||||
remote.Connect(RemoteDirectionFetch)
|
||||
err = remote.Download()
|
||||
if !called {
|
||||
|
|
14
wrapper.c
14
wrapper.c
|
@ -31,29 +31,17 @@ int _go_git_remote_ls(git_remote *remote, void *payload)
|
|||
return git_remote_ls(remote, (git_headlist_cb) remoteHeadlistCb, payload);
|
||||
}
|
||||
|
||||
int _go_git_remote_progress(const char *str, int len, void *data)
|
||||
{
|
||||
printf("calling remoteProgress\n");
|
||||
return remoteProgress((char *) str, len, data);
|
||||
}
|
||||
|
||||
int _go_git_remote_set_callbacks(git_remote *remote, void *payload)
|
||||
{
|
||||
git_remote_callbacks cbs = GIT_REMOTE_CALLBACKS_INIT;
|
||||
|
||||
cbs.progress = remoteProgress;
|
||||
cbs.update_tips = updateTips;
|
||||
cbs.update_tips = remoteUpdateTips;
|
||||
cbs.payload = payload;
|
||||
|
||||
git_remote_set_callbacks(remote, &cbs);
|
||||
}
|
||||
|
||||
int _go_git_remote_update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data)
|
||||
{
|
||||
printf("calling updateTips\n");
|
||||
return updateTips((char *) refname, (git_oid *) a, (git_oid *) b, data);
|
||||
}
|
||||
|
||||
/* EOF */
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue