summaryrefslogtreecommitdiff
path: root/git.go
diff options
context:
space:
mode:
authorJesse Ezell <[email protected]>2014-04-04 00:26:22 -0700
committerJesse Ezell <[email protected]>2014-04-04 00:26:22 -0700
commit8319a792f3184714d8f2bfa562523d1a91a9392c (patch)
treec3914d4a38355f9145884c3a278b81fbb428ce46 /git.go
parent5590078e6ff04be425b4a833adb44a0845c0b52f (diff)
parent9cd1d129bcd567ef65137783a603f8d898d8d933 (diff)
Merge remote-tracking branch 'libgit/master' into branch-iterator
Diffstat (limited to 'git.go')
-rw-r--r--git.go58
1 files changed, 28 insertions, 30 deletions
diff --git a/git.go b/git.go
index 7c76c30..8159244 100644
--- a/git.go
+++ b/git.go
@@ -8,10 +8,11 @@ package git
import "C"
import (
"bytes"
+ "encoding/hex"
"errors"
"runtime"
- "unsafe"
"strings"
+ "unsafe"
)
const (
@@ -28,10 +29,8 @@ func init() {
C.git_threads_init()
}
-// Oid
-type Oid struct {
- bytes [20]byte
-}
+// Oid represents the id for a Git object.
+type Oid [20]byte
func newOidFromC(coid *C.git_oid) *Oid {
if coid == nil {
@@ -39,62 +38,57 @@ func newOidFromC(coid *C.git_oid) *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
}
-func NewOid(b []byte) *Oid {
+func NewOidFromBytes(b []byte) *Oid {
oid := new(Oid)
- copy(oid.bytes[0:20], b[0:20])
+ copy(oid[0:20], b[0:20])
return 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) {
- o := new(Oid)
- cs := C.CString(s)
- defer C.free(unsafe.Pointer(cs))
+func NewOid(s string) (*Oid, error) {
+ 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)
-}
-
-func (oid *Oid) Bytes() []byte {
- return oid.bytes[0:]
+ return hex.EncodeToString(oid[:])
}
func (oid *Oid) Cmp(oid2 *Oid) int {
- return bytes.Compare(oid.bytes[:], oid2.bytes[:])
+ return bytes.Compare(oid[:], oid2[:])
}
func (oid *Oid) Copy() *Oid {
ret := new(Oid)
- copy(ret.bytes[:], oid.bytes[:])
+ copy(ret[:], oid[:])
return ret
}
func (oid *Oid) Equal(oid2 *Oid) bool {
- return bytes.Equal(oid.bytes[:], oid2.bytes[:])
+ return bytes.Equal(oid[:], oid2[:])
}
func (oid *Oid) IsZero() bool {
- for _, a := range oid.bytes {
- if a != '0' {
+ for _, a := range oid {
+ if a != 0 {
return false
}
}
@@ -102,7 +96,7 @@ func (oid *Oid) IsZero() bool {
}
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) {
@@ -155,6 +149,10 @@ func MakeGitError(errorCode C.int) error {
return &GitError{C.GoString(err.message), int(err.klass), int(errorCode)}
}
+func MakeGitError2(err int) error {
+ return MakeGitError(C.int(err))
+}
+
func cbool(b bool) C.int {
if b {
return C.int(1)