move the callbacks into the struct

This commit is contained in:
Carlos Martín Nieto 2013-09-21 07:02:03 +02:00
parent 516586d2e7
commit f33c856845
3 changed files with 24 additions and 41 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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 */