some callbacks
This commit is contained in:
parent
ee7e153ff8
commit
516586d2e7
37
remote.go
37
remote.go
|
@ -6,6 +6,7 @@ package git
|
|||
#include <git2/errors.h>
|
||||
|
||||
extern int _go_git_remote_ls(git_remote *remote, void *payload);
|
||||
extern int _go_git_remote_set_callbacks(git_remote *remote, void *payload);
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
|
@ -89,7 +90,32 @@ func (r *Remote) Ls() ([]*RemoteHead, error) {
|
|||
return data.slice, nil
|
||||
}
|
||||
|
||||
func (r *Remote) Download (error) {
|
||||
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))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (r *Remote) Download() (error) {
|
||||
ret := C.git_remote_download(r.ptr)
|
||||
if ret < 0 {
|
||||
LastError()
|
||||
|
@ -98,6 +124,15 @@ 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
|
||||
}
|
||||
|
|
|
@ -35,3 +35,24 @@ func TestRemoteLs(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteProgress(t *testing.T) {
|
||||
repo := createTestRepo(t)
|
||||
remote, err := repo.CreateRemote("origin", "git://github.com/libgit2/TestGitRepository")
|
||||
checkFatal(t, err)
|
||||
|
||||
called := false
|
||||
cbs := RemoteCallbacks{
|
||||
Progress: func(bytes []byte) int {
|
||||
called = true
|
||||
return 1
|
||||
},
|
||||
}
|
||||
|
||||
remote.SetCallbacks(&cbs)
|
||||
remote.Connect(RemoteDirectionFetch)
|
||||
err = remote.Download()
|
||||
if !called {
|
||||
t.Fatal("Callback not called")
|
||||
}
|
||||
}
|
26
wrapper.c
26
wrapper.c
|
@ -2,6 +2,7 @@
|
|||
#include "git2.h"
|
||||
#include "git2/submodule.h"
|
||||
#include "git2/pack.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef int (*gogit_submodule_cbk)(git_submodule *sm, const char *name, void *payload);
|
||||
|
||||
|
@ -30,6 +31,29 @@ 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.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 */
|
||||
|
||||
|
||||
|
@ -41,3 +65,5 @@ int _go_git_remote_ls(git_remote *remote, void *payload)
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue