summaryrefslogtreecommitdiff
path: root/git.go
diff options
context:
space:
mode:
Diffstat (limited to 'git.go')
-rw-r--r--git.go50
1 files changed, 29 insertions, 21 deletions
diff --git a/git.go b/git.go
index 07892e4..db9522b 100644
--- a/git.go
+++ b/git.go
@@ -1,7 +1,7 @@
package git
/*
-#cgo pkg-config: --static libgit2
+#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
*/
@@ -10,8 +10,8 @@ import (
"bytes"
"errors"
"runtime"
- "unsafe"
"strings"
+ "unsafe"
)
const (
@@ -61,8 +61,8 @@ func NewOidFromString(s string) (*Oid, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
- if C.git_oid_fromstr(o.toC(), cs) < 0 {
- return nil, LastError()
+ if ret := C.git_oid_fromstr(o.toC(), cs); ret < 0 {
+ return nil, MakeGitError(ret)
}
return o, nil
@@ -93,7 +93,7 @@ func (oid *Oid) Equal(oid2 *Oid) bool {
}
func (oid *Oid) IsZero() bool {
- for _, a := range(oid.bytes) {
+ for _, a := range oid.bytes {
if a != '0' {
return false
}
@@ -123,38 +123,47 @@ 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{
+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 {
- if (b) {
+ if b {
return C.int(1)
}
return C.int(0)
}
func ucbool(b bool) C.uint {
- if (b) {
+ if b {
return C.uint(1)
}
return C.uint(0)
@@ -167,17 +176,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
}