summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <[email protected]>2015-05-19 14:56:01 +0200
committerCarlos Martín Nieto <[email protected]>2015-05-19 14:56:01 +0200
commitd7a0495000e35d06993605a9a31a5f9823292f8a (patch)
tree3c0098d3ede28bb1488b786482b41c26d736e54e
parenta8ad0d204052d8bd9c4d0093cce62c54afa67188 (diff)
Index: Add OpenIndex
This lets you persist an index at an arbitrary location.
-rw-r--r--index.go18
-rw-r--r--index_test.go24
2 files changed, 42 insertions, 0 deletions
diff --git a/index.go b/index.go
index 009aeb6..2082ebd 100644
--- a/index.go
+++ b/index.go
@@ -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 {
diff --git a/index_test.go b/index_test.go
index a1f0c9c..9b54945 100644
--- a/index_test.go
+++ b/index_test.go
@@ -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