Index: Add OpenIndex

This lets you persist an index at an arbitrary location.
This commit is contained in:
Carlos Martín Nieto 2015-05-19 14:56:01 +02:00
parent a8ad0d2040
commit d7a0495000
2 changed files with 42 additions and 0 deletions

View File

@ -96,6 +96,24 @@ func NewIndex() (*Index, error) {
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
// the data
func (v *Index) Add(entry *IndexEntry) error {

View File

@ -2,6 +2,7 @@ package git
import (
"io/ioutil"
"os"
"runtime"
"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) {
if err == nil {
return