commit
0a1052b0e0
16
odb.go
16
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 := 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));
|
||||
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) {
|
||||
|
|
27
odb_test.go
27
odb_test.go
|
@ -33,3 +33,30 @@ func TestOdbStream(t *testing.T) {
|
|||
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 <john@doe.com> 1390682018 +0000
|
||||
committer John Doe <john@doe.com> 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")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue