summaryrefslogtreecommitdiff
path: root/blob.go
diff options
context:
space:
mode:
Diffstat (limited to 'blob.go')
-rw-r--r--blob.go65
1 files changed, 63 insertions, 2 deletions
diff --git a/blob.go b/blob.go
index b638c4f..ced2cb1 100644
--- a/blob.go
+++ b/blob.go
@@ -3,9 +3,18 @@ package git
/*
#include <git2.h>
#include <git2/errors.h>
+#include <string.h>
+
+extern int _go_git_blob_create_fromchunks(git_oid *id,
+ git_repository *repo,
+ const char *hintpath,
+ void *payload);
+
*/
import "C"
import (
+ "io"
+ "runtime"
"unsafe"
)
@@ -13,13 +22,65 @@ type Blob struct {
gitObject
}
-func (v Blob) Size() int64 {
+func (v *Blob) Size() int64 {
return int64(C.git_blob_rawsize(v.ptr))
}
-func (v Blob) Contents() []byte {
+func (v *Blob) Contents() []byte {
size := C.int(C.git_blob_rawsize(v.ptr))
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.ptr))
return C.GoBytes(buffer, size)
}
+func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+ oid := C.git_oid{}
+ ecode := C.git_blob_create_frombuffer(&oid, repo.ptr, unsafe.Pointer(&data[0]), C.size_t(len(data)))
+ if ecode < 0 {
+ return nil, MakeGitError(ecode)
+ }
+ return newOidFromC(&oid), nil
+}
+
+type BlobChunkCallback func(maxLen int) ([]byte, error)
+
+type BlobCallbackData struct {
+ Callback BlobChunkCallback
+ Error error
+}
+
+//export blobChunkCb
+func blobChunkCb(buffer *C.char, maxLen C.size_t, payload unsafe.Pointer) int {
+ data := (*BlobCallbackData)(payload)
+ goBuf, err := data.Callback(int(maxLen))
+ if err == io.EOF {
+ return 1
+ } else if err != nil {
+ data.Error = err
+ return -1
+ }
+ C.memcpy(unsafe.Pointer(buffer), unsafe.Pointer(&goBuf), C.size_t(len(goBuf)))
+ return 0
+}
+
+func (repo *Repository) CreateBlobFromChunks(hintPath string, callback BlobChunkCallback) (*Oid, error) {
+ runtime.LockOSThread()
+ defer runtime.UnlockOSThread()
+
+ var chintPath *C.char = nil
+ if len(hintPath) > 0 {
+ C.CString(hintPath)
+ defer C.free(unsafe.Pointer(chintPath))
+ }
+ oid := C.git_oid{}
+ payload := &BlobCallbackData{Callback: callback}
+ ecode := C._go_git_blob_create_fromchunks(&oid, repo.ptr, chintPath, unsafe.Pointer(payload))
+ if payload.Error != nil {
+ return nil, payload.Error
+ }
+ if ecode < 0 {
+ return nil, MakeGitError(ecode)
+ }
+ return newOidFromC(&oid), nil
+}