Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add packbuilder.SetCallbacks() #947

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions odb.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ func (v *Odb) AddAlternate(backend *OdbBackend, priority int) (err error) {
return nil
}

func (v *Odb) AddDiskAlternate(path string) (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()

cstr := C.CString(path)
defer C.free(unsafe.Pointer(cstr))

ret := C.git_odb_add_disk_alternate(v.ptr, cstr)
runtime.KeepAlive(v)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}

func (v *Odb) AddBackend(backend *OdbBackend, priority int) (err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
Expand Down
60 changes: 57 additions & 3 deletions packbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package git
#include <stdlib.h>

extern int _go_git_packbuilder_foreach(git_packbuilder *pb, void *payload);
extern int _go_git_packbuilder_set_callbacks(git_packbuilder *pb, void *payload);
*/
import "C"
import (
Expand All @@ -15,10 +16,16 @@ import (
"unsafe"
)

const (
PackbuilderAddingObjects int32 = C.GIT_PACKBUILDER_ADDING_OBJECTS
PackbuilderDeltafication int32 = C.GIT_PACKBUILDER_DELTAFICATION
)

type Packbuilder struct {
doNotCompare
ptr *C.git_packbuilder
r *Repository
ptr *C.git_packbuilder
r *Repository
callbackHandle unsafe.Pointer
}

func (repo *Repository) NewPackbuilder() (*Packbuilder, error) {
Expand All @@ -34,13 +41,16 @@ func (repo *Repository) NewPackbuilder() (*Packbuilder, error) {
}

func newPackbuilderFromC(ptr *C.git_packbuilder, r *Repository) *Packbuilder {
pb := &Packbuilder{ptr: ptr, r: r}
pb := &Packbuilder{ptr: ptr, r: r, callbackHandle: nil}
runtime.SetFinalizer(pb, (*Packbuilder).Free)
return pb
}

func (pb *Packbuilder) Free() {
runtime.SetFinalizer(pb, nil)
if pb.callbackHandle != nil {
pointerHandles.Untrack(pb.callbackHandle)
}
C.git_packbuilder_free(pb.ptr)
}

Expand Down Expand Up @@ -183,3 +193,47 @@ func (pb *Packbuilder) ForEach(callback PackbuilderForeachCallback) error {

return nil
}

type packbuilderProgressCallbackData struct {
callback PackbuilderProgressCallback
errorTarget *error
}

//export packbuilderProgressCallback
func packbuilderProgressCallback(errorMessage **C.char, stage C.int, current, total C.uint, handle unsafe.Pointer) C.int {
data := pointerHandles.Get(handle).(*packbuilderProgressCallbackData)
if data.callback == nil {
return C.int(ErrorCodeOK)
}

err := data.callback(int32(stage), uint32(current), uint32(total))
if err != nil {
if data.errorTarget != nil {
*data.errorTarget = err
}
return setCallbackError(errorMessage, err)
}
return C.int(ErrorCodeOK)
}

func (pb *Packbuilder) SetCallbacks(callback PackbuilderProgressCallback) error {
var err error
data := packbuilderProgressCallbackData{
callback: callback,
errorTarget: &err,
}
handle := pointerHandles.Track(&data)
if pb.callbackHandle != nil {
pointerHandles.Untrack(pb.callbackHandle)
}
pb.callbackHandle = handle

runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C._go_git_packbuilder_set_callbacks(pb.ptr, handle)
runtime.KeepAlive(pb)
if ret != 0 {
return MakeGitError(ret)
}
return nil
}
18 changes: 18 additions & 0 deletions wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,24 @@ static int pack_progress_callback(int stage, unsigned int current, unsigned int
return set_callback_error(error_message, ret);
}

static int packbuilder_progress_callback(int stage, unsigned int current, unsigned int total, void *data)
{
char *error_message = NULL;
const int ret = packbuilderProgressCallback(
&error_message,
stage,
current,
total,
data
);
return set_callback_error(error_message, ret);
}

int _go_git_packbuilder_set_callbacks(git_packbuilder *pb, void *payload)
{
return git_packbuilder_set_callbacks(pb, (git_packbuilder_progress)&packbuilder_progress_callback, payload);
}

static int push_transfer_progress_callback(
unsigned int current,
unsigned int total,
Expand Down