summaryrefslogtreecommitdiff
path: root/odb.go
diff options
context:
space:
mode:
authorMichel Lespinasse <[email protected]>2018-07-03 16:43:07 -0700
committerMichel Lespinasse <[email protected]>2018-07-03 16:43:07 -0700
commitbdca40d27558337f2aa84856b0dd8c6b1b6bb5c8 (patch)
treecdac7016c796c77ca3288290bb7c9570a741ce3d /odb.go
parenta2de5abababeb291f269fe254fc0341e5323af3f (diff)
git2go: small fixes to odb module
- Fix couple cgo issues in odb.Write() and odb.Hash(). This is the same issue - and same solution - as repo.CreateBlobFromBuffer() used to have. - Add test for odb.Read()
Diffstat (limited to 'odb.go')
-rw-r--r--odb.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/odb.go b/odb.go
index f236fc4..fa6779f 100644
--- a/odb.go
+++ b/odb.go
@@ -80,15 +80,19 @@ func (v *Odb) Exists(oid *Oid) bool {
func (v *Odb) Write(data []byte, otype ObjectType) (oid *Oid, err error) {
oid = new(Oid)
- var cptr unsafe.Pointer
- if len(data) > 0 {
- cptr = unsafe.Pointer(&data[0])
- }
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_odb_write(oid.toC(), v.ptr, cptr, C.size_t(len(data)), C.git_otype(otype))
+ var size C.size_t
+ if len(data) > 0 {
+ size = C.size_t(len(data))
+ } else {
+ data = []byte{0}
+ size = C.size_t(0)
+ }
+
+ ret := C.git_odb_write(oid.toC(), v.ptr, unsafe.Pointer(&data[0]), size, C.git_otype(otype))
runtime.KeepAlive(v)
if ret < 0 {
return nil, MakeGitError(ret)
@@ -164,13 +168,19 @@ func (v *Odb) ForEach(callback OdbForEachCallback) error {
// Hash determines the object-ID (sha1) of a data buffer.
func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) {
oid = new(Oid)
- header := (*reflect.SliceHeader)(unsafe.Pointer(&data))
- ptr := unsafe.Pointer(header.Data)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- ret := C.git_odb_hash(oid.toC(), ptr, C.size_t(header.Len), C.git_otype(otype))
+ var size C.size_t
+ if len(data) > 0 {
+ size = C.size_t(len(data))
+ } else {
+ data = []byte{0}
+ size = C.size_t(0)
+ }
+
+ ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(&data[0]), size, C.git_otype(otype))
runtime.KeepAlive(data)
if ret < 0 {
return nil, MakeGitError(ret)