From 946d11f677198d89de379ef8570cf68eac9a3321 Mon Sep 17 00:00:00 2001 From: Serge Date: Sun, 26 Feb 2023 14:42:20 -0500 Subject: [PATCH 1/2] Add packbuilder.SetCallbacks() Add `SetCallbacks` function to packbuilder which invokes `git_packbuilder_set_callbacks` and allows packbuilder progress information to be reported back to the caller. --- packbuilder.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++--- wrapper.c | 18 +++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/packbuilder.go b/packbuilder.go index 0ec0fb1e..e271ed5a 100644 --- a/packbuilder.go +++ b/packbuilder.go @@ -6,6 +6,7 @@ package git #include 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 ( @@ -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) { @@ -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) } @@ -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 +} diff --git a/wrapper.c b/wrapper.c index e999136d..9672b853 100644 --- a/wrapper.c +++ b/wrapper.c @@ -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, From 48a2e9f682c99f984263b13e014698625d58fe2f Mon Sep 17 00:00:00 2001 From: Serge Date: Sun, 12 Mar 2023 18:14:00 -0400 Subject: [PATCH 2/2] Add AddDiskAlternate() Support libgit2 `git_odb_add_disk_alternate`. --- odb.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/odb.go b/odb.go index cbb1b870..9df2245b 100644 --- a/odb.go +++ b/odb.go @@ -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()