Add a few basic index operations #202
18
index.go
18
index.go
|
@ -96,6 +96,24 @@ func NewIndex() (*Index, error) {
|
||||||
return &Index{ptr: ptr}, nil
|
return &Index{ptr: ptr}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OpenIndex creates a new index at the given path. If the file does
|
||||||
|
// not exist it will be created when Write() is called.
|
||||||
|
func OpenIndex(path string) (*Index, error) {
|
||||||
|
var ptr *C.git_index
|
||||||
|
|
||||||
|
var cpath = C.CString(path)
|
||||||
|
defer C.free(unsafe.Pointer(cpath))
|
||||||
|
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
if err := C.git_index_open(&ptr, cpath); err < 0 {
|
||||||
|
return nil, MakeGitError(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Index{ptr: ptr}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Add adds or replaces the given entry to the index, making a copy of
|
// Add adds or replaces the given entry to the index, making a copy of
|
||||||
// the data
|
// the data
|
||||||
func (v *Index) Add(entry *IndexEntry) error {
|
func (v *Index) Add(entry *IndexEntry) error {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -148,6 +149,29 @@ func TestIndexAddAllCallback(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIndexOpen(t *testing.T) {
|
||||||
|
repo := createTestRepo(t)
|
||||||
|
defer cleanupTestRepo(t, repo)
|
||||||
|
|
||||||
|
path := repo.Workdir() + "/heyindex"
|
||||||
|
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
t.Fatal("new index file already exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
idx, err := OpenIndex(path)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
err = idx.Write()
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
_, err = os.Stat(path)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
t.Fatal("new index file did not get written")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func checkFatal(t *testing.T, err error) {
|
func checkFatal(t *testing.T, err error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue