summaryrefslogtreecommitdiff
path: root/git.go
diff options
context:
space:
mode:
authorCarlos Martín Nieto <[email protected]>2014-03-19 03:39:34 +0100
committerCarlos Martín Nieto <[email protected]>2014-03-19 03:57:36 +0100
commit0bb73e43a8f26be8608cdd304d73cacb05753417 (patch)
treed8ccca7fe48f7d800bc47f7f4f4d58a8f448274b /git.go
parentc243c31f7d428680579a1dd20273cd3888c730e4 (diff)
Oid: use Go's conversion functions
Go already has all the necessary pieces for encoding and decoding hex strings. Using them let's us avoid going into C land. Benchmarks show this takes about half the time as using libgit2's functions.
Diffstat (limited to 'git.go')
-rw-r--r--git.go20
1 files changed, 10 insertions, 10 deletions
diff --git a/git.go b/git.go
index 9e1d3e7..e5fe812 100644
--- a/git.go
+++ b/git.go
@@ -8,6 +8,7 @@ package git
import "C"
import (
"bytes"
+ "encoding/hex"
"errors"
"runtime"
"strings"
@@ -52,24 +53,23 @@ func (oid *Oid) toC() *C.git_oid {
}
func NewOid(s string) (*Oid, error) {
- o := new(Oid)
- cs := C.CString(s)
- defer C.free(unsafe.Pointer(cs))
+ if len(s) > C.GIT_OID_HEXSZ {
+ return nil, errors.New("string is too long for oid")
+ }
- runtime.LockOSThread()
- defer runtime.UnlockOSThread()
+ o := new(Oid)
- if ret := C.git_oid_fromstr(o.toC(), cs); ret < 0 {
- return nil, MakeGitError(ret)
+ slice, error := hex.DecodeString(s)
+ if error != nil {
+ return nil, error
}
+ copy(o[:], slice[:20])
return o, nil
}
func (oid *Oid) String() string {
- buf := make([]byte, 40)
- C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), oid.toC())
- return string(buf)
+ return hex.EncodeToString(oid[:])
}
func (oid *Oid) Cmp(oid2 *Oid) int {