Wrap the index and test it a bit #4
|
@ -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)
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
package git
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateRepoAndStage(t *testing.T) {
|
||||||
|
// figure out where we can create the test repo
|
||||||
|
path, err := ioutil.TempDir("", "git2go")
|
||||||
|
checkFatal(t, err)
|
||||||
|
repo, err := Init(path, false)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
tmpfile := "README"
|
||||||
|
err = ioutil.WriteFile(path + "/" + tmpfile, []byte("foo\n"), 0644)
|
||||||
|
checkFatal(t, err)
|
||||||
|
defer os.RemoveAll(path)
|
||||||
|
|
||||||
|
idx, err := repo.Index()
|
||||||
|
checkFatal(t, err)
|
||||||
|
err = idx.AddByPath(tmpfile)
|
||||||
|
checkFatal(t, err)
|
||||||
|
treeId, err := idx.WriteTree()
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
|
||||||
|
t.Fatalf("%v", treeId.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkFatal(t *testing.T, err error) {
|
||||||
|
if err == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The failure happens at wherever we were called, not here
|
||||||
|
_, file, line, ok := runtime.Caller(1)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Fatalf("Fail at %v:%v", file, line)
|
||||||
|
}
|
|
@ -62,6 +62,16 @@ func (v *Repository) Config() (*Config, error) {
|
||||||
return config, nil
|
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) {
|
func (v *Repository) LookupTree(oid *Oid) (*Tree, error) {
|
||||||
tree := new(Tree)
|
tree := new(Tree)
|
||||||
ret := C.git_tree_lookup(&tree.ptr, v.ptr, oid.toC())
|
ret := C.git_tree_lookup(&tree.ptr, v.ptr, oid.toC())
|
||||||
|
|
Loading…
Reference in New Issue