summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <[email protected]>2013-03-06 13:10:53 +0100
committerCarlos Martín Nieto <[email protected]>2013-03-06 13:10:53 +0100
commit33f4594e9c0cc7854582ac42ff86c30f83533635 (patch)
tree4d55f7f5d6d8204c1f187ea51b54bac7ea68d942
parent9b6d570748ae51bf38e3192f9a5de45bc68b23c6 (diff)
Wrap git_index
-rw-r--r--index.go49
-rw-r--r--repository.go10
2 files changed, 59 insertions, 0 deletions
diff --git a/index.go b/index.go
new file mode 100644
index 0000000..72b1d5b
--- /dev/null
+++ b/index.go
@@ -0,0 +1,49 @@
+package git
+
+/*
+#cgo pkg-config: libgit2
+#include <git2.h>
+#include <git2/errors.h>
+*/
+import "C"
+import (
+ "runtime"
+ "unsafe"
+)
+
+type Index struct {
+ ptr *C.git_index
+}
+
+func newIndexFromC(ptr *C.git_index) *Index {
+ idx := &Index{ptr}
+ runtime.SetFinalizer(idx, (*Index).Free)
+ return idx
+}
+
+func (v *Index) AddByPath(path string) error {
+ cstr := C.CString(path)
+ defer C.free(unsafe.Pointer(cstr))
+
+ ret := C.git_index_add_bypath(v.ptr, cstr)
+ if ret < 0 {
+ return LastError()
+ }
+
+ return nil
+}
+
+func (v *Index) WriteTree() (*Oid, error) {
+ oid := new(Oid)
+ ret := C.git_index_write_tree(oid.toC(), v.ptr)
+ if ret < 0 {
+ return nil, LastError()
+ }
+
+ return oid, nil
+}
+
+func (v *Index) Free() {
+ runtime.SetFinalizer(v, nil)
+ C.git_index_free(v.ptr)
+}
diff --git a/repository.go b/repository.go
index bfbfebd..5313af3 100644
--- a/repository.go
+++ b/repository.go
@@ -62,6 +62,16 @@ func (v *Repository) Config() (*Config, error) {
return config, nil
}
+func (v *Repository) Index() (*Index, error) {
+ var ptr *C.git_index
+ ret := C.git_repository_index(&ptr, v.ptr)
+ if ret < 0 {
+ return nil, LastError()
+ }
+
+ return newIndexFromC(ptr), nil
+}
+
func (v *Repository) LookupTree(oid *Oid) (*Tree, error) {
tree := new(Tree)
ret := C.git_tree_lookup(&tree.ptr, v.ptr, oid.toC())