From 4ce2eb713bda2cb361f6d2b58502bea758ca980e Mon Sep 17 00:00:00 2001 From: Claudiu-Vlad Ursache Date: Sat, 25 Jan 2014 22:18:32 +0100 Subject: [PATCH 1/3] Add Odb hash function. --- odb.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/odb.go b/odb.go index 638ef74..6d18ce9 100644 --- a/odb.go +++ b/odb.go @@ -79,6 +79,22 @@ func (v *Odb) ForEach() chan *Oid { return ch } +// 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 := (*C.char)(unsafe.Pointer(header.Data)) + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(ptr), C.size_t(header.Len), C.git_otype(otype)); + if ret < 0 { + err = LastError() + } + return +} + // NewReadStream opens a read stream from the ODB. Reading from it will give you the // contents of the object. func (v *Odb) NewReadStream(id *Oid) (*OdbReadStream, error) { -- 2.45.2 From 53f2ebce5f5124eff6b8e0ac2c45651d043899fa Mon Sep 17 00:00:00 2001 From: Claudiu-Vlad Ursache Date: Sat, 25 Jan 2014 22:18:43 +0100 Subject: [PATCH 2/3] Test for Odb hash function. --- odb_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/odb_test.go b/odb_test.go index 3c7624c..a4f8943 100644 --- a/odb_test.go +++ b/odb_test.go @@ -32,4 +32,31 @@ func TestOdbStream(t *testing.T) { if stream.Id.Cmp(expectedId) != 0 { t.Fatal("Wrong data written") } +} + +func TestOdbHash(t *testing.T) { + + repo := createTestRepo(t) + defer os.RemoveAll(repo.Workdir()) + _, _ = seedTestRepo(t, repo) + + odb, error := repo.Odb() + checkFatal(t, error) + + str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7 +parent 66e1c476199ebcd3e304659992233132c5a52c6c +author John Doe 1390682018 +0000 +committer John Doe 1390682018 +0000 + +Initial commit.`; + + oid, error := odb.Hash([]byte(str), ObjectCommit) + checkFatal(t, error) + + coid, error := odb.Write([]byte(str), ObjectCommit) + checkFatal(t, error) + + if oid.Cmp(coid) != 0 { + t.Fatal("Hash and write Oids are different") + } } \ No newline at end of file -- 2.45.2 From bf209ca2ba293116bdd3062ba9ff5ac071f9ddd5 Mon Sep 17 00:00:00 2001 From: Claudiu-Vlad Ursache Date: Sun, 26 Jan 2014 12:36:05 +0100 Subject: [PATCH 3/3] Remove unnecessary cast. --- odb.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/odb.go b/odb.go index 6d18ce9..953791c 100644 --- a/odb.go +++ b/odb.go @@ -83,12 +83,12 @@ func (v *Odb) ForEach() chan *Oid { func (v *Odb) Hash(data []byte, otype ObjectType) (oid *Oid, err error) { oid = new(Oid) header := (*reflect.SliceHeader)(unsafe.Pointer(&data)) - ptr := (*C.char)(unsafe.Pointer(header.Data)) + ptr := unsafe.Pointer(header.Data) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_odb_hash(oid.toC(), unsafe.Pointer(ptr), C.size_t(header.Len), C.git_otype(otype)); + ret := C.git_odb_hash(oid.toC(), ptr, C.size_t(header.Len), C.git_otype(otype)); if ret < 0 { err = LastError() } -- 2.45.2