diff options
| author | Jesse Ezell <[email protected]> | 2014-03-20 22:02:19 -0700 |
|---|---|---|
| committer | Jesse Ezell <[email protected]> | 2014-03-20 22:02:19 -0700 |
| commit | 37964e878f70229171f94668cb96f347b798e2b2 (patch) | |
| tree | 5781761e13e57edb3589844b6faa799c1158b93a /git.go | |
| parent | d0b334b24409ddc190a7010be0072d87df6b6bfe (diff) | |
| parent | 2811845a1287d949a74b8ed80a5791fd8875002a (diff) | |
merge with latest
Diffstat (limited to 'git.go')
| -rw-r--r-- | git.go | 86 |
1 files changed, 44 insertions, 42 deletions
@@ -8,6 +8,7 @@ package git import "C" import ( "bytes" + "encoding/hex" "errors" "runtime" "strings" @@ -29,10 +30,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 { @@ -40,62 +39,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 C.git_oid_fromstr(o.toC(), cs) < 0 { - return nil, LastError() + 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 } } @@ -103,7 +97,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) { @@ -124,27 +118,36 @@ func ShortenOids(ids []*Oid, minlen int) (int, error) { buf[40] = 0 ret = C.git_oid_shorten_add(shorten, (*C.char)(unsafe.Pointer(&buf[0]))) if ret < 0 { - return int(ret), LastError() + return int(ret), MakeGitError(ret) } } return int(ret), nil } type GitError struct { - Message string - Code int + Message string + Class int + ErrorCode int } func (e GitError) Error() string { return e.Message } -func LastError() error { +func IsNotExist(err error) bool { + return err.(*GitError).ErrorCode == C.GIT_ENOTFOUND +} + +func IsExist(err error) bool { + return err.(*GitError).ErrorCode == C.GIT_EEXISTS +} + +func MakeGitError(errorCode C.int) error { err := C.giterr_last() if err == nil { - return &GitError{"No message", 0} + return &GitError{"No message", C.GITERR_INVALID, C.GIT_ERROR} } - return &GitError{C.GoString(err.message), int(err.klass)} + return &GitError{C.GoString(err.message), int(err.klass), int(errorCode)} } func cbool(b bool) C.int { @@ -168,17 +171,16 @@ func Discover(start string, across_fs bool, ceiling_dirs []string) (string, erro cstart := C.CString(start) defer C.free(unsafe.Pointer(cstart)) - retpath := (*C.char)(C.malloc(C.GIT_PATH_MAX)) - defer C.free(unsafe.Pointer(retpath)) + var buf C.git_buf + defer C.git_buf_free(&buf) runtime.LockOSThread() defer runtime.UnlockOSThread() - r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs) - - if r == 0 { - return C.GoString(retpath), nil + ret := C.git_repository_discover(&buf, cstart, cbool(across_fs), ceildirs) + if ret < 0 { + return "", MakeGitError(ret) } - return "", LastError() + return C.GoString(buf.ptr), nil } |
