summaryrefslogtreecommitdiff
path: root/git.go
diff options
context:
space:
mode:
Diffstat (limited to 'git.go')
-rw-r--r--git.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/git.go b/git.go
index 68712d4..d8cacc4 100644
--- a/git.go
+++ b/git.go
@@ -7,7 +7,11 @@ package git
*/
import "C"
import (
+ "bytes"
+ "errors"
"unsafe"
+ "strings"
+ "fmt"
)
const (
@@ -16,6 +20,10 @@ const (
ENOTFOUND = C.GIT_ENOTFOUND
)
+var (
+ ErrIterOver = errors.New("Iteration is over")
+)
+
func init() {
C.git_threads_init()
}
@@ -67,6 +75,53 @@ func (oid *Oid) Bytes() []byte {
return oid.bytes[0:]
}
+func (oid *Oid) Cmp(oid2 *Oid) int {
+ return bytes.Compare(oid.bytes[:], oid2.bytes[:])
+}
+
+func (oid *Oid) Copy() *Oid {
+ ret := new(Oid)
+ copy(ret.bytes[:], oid.bytes[:])
+ return ret
+}
+
+func (oid *Oid) Equal(oid2 *Oid) bool {
+ return bytes.Equal(oid.bytes[:], oid2.bytes[:])
+}
+
+func (oid *Oid) IsZero() bool {
+ for _, a := range(oid.bytes) {
+ if a != '0' {
+ return false
+ }
+ }
+ return true
+}
+
+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
+ 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
@@ -78,6 +133,9 @@ func (e GitError) Error() string{
func LastError() error {
err := C.giterr_last()
+ if err == nil {
+ return &GitError{"No message", 0}
+ }
return &GitError{C.GoString(err.message), int(err.klass)}
}
@@ -94,3 +152,22 @@ func ucbool(b bool) C.uint {
}
return C.uint(0)
}
+
+func Discover(start string, across_fs bool, ceiling_dirs []string) (string, error) {
+ ceildirs := C.CString(strings.Join(ceiling_dirs, string(C.GIT_PATH_LIST_SEPARATOR)))
+ defer C.free(unsafe.Pointer(ceildirs))
+
+ 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))
+
+ r := C.git_repository_discover(retpath, C.GIT_PATH_MAX, cstart, cbool(across_fs), ceildirs)
+
+ if r == 0 {
+ return C.GoString(retpath), nil
+ }
+
+ return "", LastError()
+}