diff options
Diffstat (limited to 'git.go')
| -rw-r--r-- | git.go | 60 |
1 files changed, 49 insertions, 11 deletions
@@ -8,6 +8,8 @@ package git import "C" import ( "bytes" + "errors" + "runtime" "unsafe" "strings" ) @@ -18,6 +20,10 @@ const ( ENOTFOUND = C.GIT_ENOTFOUND ) +var ( + ErrIterOver = errors.New("Iteration is over") +) + func init() { C.git_threads_init() } @@ -52,6 +58,9 @@ func NewOidFromString(s string) (*Oid, error) { cs := C.CString(s) defer C.free(unsafe.Pointer(cs)) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + if C.git_oid_fromstr(o.toC(), cs) < 0 { return nil, LastError() } @@ -84,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 } @@ -96,29 +105,56 @@ func (oid *Oid) NCmp(oid2 *Oid, n uint) int { return bytes.Compare(oid.bytes[:n], oid2.bytes[:n]) } +func ShortenOids(ids []*Oid, minlen int) (int, error) { + shorten := C.git_oid_shorten_new(C.size_t(minlen)) + if shorten == nil { + panic("Out of memory") + } + defer C.git_oid_shorten_free(shorten) + + var ret C.int + + runtime.LockOSThread() + defer runtime.UnlockOSThread() + + for _, id := range ids { + buf := make([]byte, 41) + C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), id.toC()) + 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), nil +} + type GitError struct { Message string - Code int + Code int } -func (e GitError) Error() string{ +func (e GitError) Error() string { return e.Message } func LastError() error { err := C.giterr_last() + if err == nil { + return &GitError{"No message", 0} + } return &GitError{C.GoString(err.message), int(err.klass)} } 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) @@ -131,14 +167,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) - r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs) + runtime.LockOSThread() + defer runtime.UnlockOSThread() - if r == 0 { - return C.GoString(retpath), nil + ret := C.git_repository_discover(&buf, cstart, cbool(across_fs), ceildirs) + if ret < 0 { + return "", LastError() } - return "", LastError() + return C.GoString(buf.ptr), nil } |
