Merge pull request #68 from libgit2/cmn/oid-revamp

Oid revamp
This commit is contained in:
Carlos Martín Nieto 2014-03-19 07:20:45 +01:00
commit 3274d477c9
3 changed files with 35 additions and 33 deletions

56
git.go
View File

@ -8,6 +8,7 @@ package git
import "C" import "C"
import ( import (
"bytes" "bytes"
"encoding/hex"
"errors" "errors"
"runtime" "runtime"
"strings" "strings"
@ -28,10 +29,8 @@ func init() {
C.git_threads_init() C.git_threads_init()
} }
// Oid // Oid represents the id for a Git object.
type Oid struct { type Oid [20]byte
bytes [20]byte
}
func newOidFromC(coid *C.git_oid) *Oid { func newOidFromC(coid *C.git_oid) *Oid {
if coid == nil { if coid == nil {
@ -39,62 +38,57 @@ func newOidFromC(coid *C.git_oid) *Oid {
} }
oid := new(Oid) oid := new(Oid)
copy(oid.bytes[0:20], C.GoBytes(unsafe.Pointer(coid), 20)) copy(oid[0:20], C.GoBytes(unsafe.Pointer(coid), 20))
return oid return oid
} }
func NewOid(b []byte) *Oid { func NewOidFromBytes(b []byte) *Oid {
oid := new(Oid) oid := new(Oid)
copy(oid.bytes[0:20], b[0:20]) copy(oid[0:20], b[0:20])
return oid return oid
} }
func (oid *Oid) toC() *C.git_oid { func (oid *Oid) toC() *C.git_oid {
return (*C.git_oid)(unsafe.Pointer(&oid.bytes)) return (*C.git_oid)(unsafe.Pointer(oid))
} }
func NewOidFromString(s string) (*Oid, error) { func NewOid(s string) (*Oid, error) {
o := new(Oid) if len(s) > C.GIT_OID_HEXSZ {
cs := C.CString(s) return nil, errors.New("string is too long for oid")
defer C.free(unsafe.Pointer(cs))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if ret := C.git_oid_fromstr(o.toC(), cs); ret < 0 {
return nil, MakeGitError(ret)
} }
o := new(Oid)
slice, error := hex.DecodeString(s)
if error != nil {
return nil, error
}
copy(o[:], slice[:20])
return o, nil return o, nil
} }
func (oid *Oid) String() string { func (oid *Oid) String() string {
buf := make([]byte, 40) return hex.EncodeToString(oid[:])
C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), oid.toC())
return string(buf)
}
func (oid *Oid) Bytes() []byte {
return oid.bytes[0:]
} }
func (oid *Oid) Cmp(oid2 *Oid) int { func (oid *Oid) Cmp(oid2 *Oid) int {
return bytes.Compare(oid.bytes[:], oid2.bytes[:]) return bytes.Compare(oid[:], oid2[:])
} }
func (oid *Oid) Copy() *Oid { func (oid *Oid) Copy() *Oid {
ret := new(Oid) ret := new(Oid)
copy(ret.bytes[:], oid.bytes[:]) copy(ret[:], oid[:])
return ret return ret
} }
func (oid *Oid) Equal(oid2 *Oid) bool { func (oid *Oid) Equal(oid2 *Oid) bool {
return bytes.Equal(oid.bytes[:], oid2.bytes[:]) return bytes.Equal(oid[:], oid2[:])
} }
func (oid *Oid) IsZero() bool { func (oid *Oid) IsZero() bool {
for _, a := range oid.bytes { for _, a := range oid {
if a != '0' { if a != 0 {
return false return false
} }
} }
@ -102,7 +96,7 @@ func (oid *Oid) IsZero() bool {
} }
func (oid *Oid) NCmp(oid2 *Oid, n uint) int { func (oid *Oid) NCmp(oid2 *Oid, n uint) int {
return bytes.Compare(oid.bytes[:n], oid2.bytes[:n]) return bytes.Compare(oid[:n], oid2[:n])
} }
func ShortenOids(ids []*Oid, minlen int) (int, error) { func ShortenOids(ids []*Oid, minlen int) (int, error) {

View File

@ -54,3 +54,11 @@ func seedTestRepo(t *testing.T, repo *Repository) (*Oid, *Oid) {
return commitId, treeId return commitId, treeId
} }
func TestOidZero(t *testing.T) {
var zeroId Oid
if !zeroId.IsZero() {
t.Error("Zero Oid is not zero")
}
}

View File

@ -27,7 +27,7 @@ func TestOdbStream(t *testing.T) {
error = stream.Close() error = stream.Close()
checkFatal(t, error) checkFatal(t, error)
expectedId, error := NewOidFromString("30f51a3fba5274d53522d0f19748456974647b4f") expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
checkFatal(t, error) checkFatal(t, error)
if stream.Id.Cmp(expectedId) != 0 { if stream.Id.Cmp(expectedId) != 0 {
t.Fatal("Wrong data written") t.Fatal("Wrong data written")