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 AutotagAll = C.GIT_REMOTE_DOWNLOAD_TAGS_ALL
) )
type ProgressCb func([]byte) int
type UpdateTipsCb func(string, *Oid, *Oid) int
type Remote struct { type Remote struct {
Name string Name string
Url string Url string
// callbacks
Progress ProgressCb
UpdateTips UpdateTipsCb
ptr *C.git_remote ptr *C.git_remote
} }
@ -90,26 +98,22 @@ func (r *Remote) Ls() ([]*RemoteHead, error) {
return data.slice, nil return data.slice, nil
} }
func (r *Remote) SetCallbacks(callbacks *RemoteCallbacks) {
C._go_git_remote_set_callbacks(r.ptr, unsafe.Pointer(callbacks))
}
//export remoteProgress //export remoteProgress
func remoteProgress(str *C.char, length C.int, data unsafe.Pointer) int { func remoteProgress(str *C.char, length C.int, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data) remote := (*Remote)(data)
if callbacks.Progress != nil { if remote.Progress != nil {
return callbacks.Progress(C.GoBytes(unsafe.Pointer(str), length)) return remote.Progress(C.GoBytes(unsafe.Pointer(str), length))
} }
return 0 return 0
} }
//export updateTips //export remoteUpdateTips
func updateTips(str *C.char, a, b *C.git_oid, data unsafe.Pointer) int { func remoteUpdateTips(str *C.char, a, b *C.git_oid, data unsafe.Pointer) int {
callbacks := (*RemoteCallbacks)(data) remote := (*Remote)(data)
if remote.UpdateTips != nil {
goa, gob := newOidFromC(a), newOidFromC(b) goa, gob := newOidFromC(a), newOidFromC(b)
if callbacks.UpdateTips != nil { return remote.UpdateTips(C.GoString(str), goa, gob)
return callbacks.UpdateTips(C.GoString(str), goa, gob)
} }
return 0 return 0
@ -124,15 +128,6 @@ func (r *Remote) Download() (error) {
return nil 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 { type headlistData struct {
slice []*RemoteHead slice []*RemoteHead
} }
@ -159,6 +154,9 @@ func newRemoteFromC(ptr *C.git_remote) *Remote {
Url: C.GoString(C.git_remote_url(ptr)), 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) runtime.SetFinalizer(remote, (*Remote).Free)
return remote return remote

View File

@ -42,14 +42,11 @@ func TestRemoteProgress(t *testing.T) {
checkFatal(t, err) checkFatal(t, err)
called := false called := false
cbs := RemoteCallbacks{ remote.Progress = func(bytes []byte) int {
Progress: func(bytes []byte) int {
called = true called = true
return 1 return 1
},
} }
remote.SetCallbacks(&cbs)
remote.Connect(RemoteDirectionFetch) remote.Connect(RemoteDirectionFetch)
err = remote.Download() err = remote.Download()
if !called { 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); 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) int _go_git_remote_set_callbacks(git_remote *remote, void *payload)
{ {
git_remote_callbacks cbs = GIT_REMOTE_CALLBACKS_INIT; git_remote_callbacks cbs = GIT_REMOTE_CALLBACKS_INIT;
cbs.progress = remoteProgress; cbs.progress = remoteProgress;
cbs.update_tips = updateTips; cbs.update_tips = remoteUpdateTips;
cbs.payload = payload; cbs.payload = payload;
git_remote_set_callbacks(remote, &cbs); 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 */ /* EOF */