Merge pull request #202 from libgit2/index-basics
Add a few basic index operations
This commit is contained in:
commit
193deb7ae3
38
index.go
38
index.go
|
@ -96,6 +96,30 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path returns the index' path on disk or an empty string if it
|
||||||
|
// exists only in memory.
|
||||||
|
func (v *Index) Path() string {
|
||||||
|
return C.GoString(C.git_index_path(v.ptr))
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
|
@ -240,6 +264,20 @@ func (v *Index) WriteTreeTo(repo *Repository) (*Oid, error) {
|
||||||
return oid, nil
|
return oid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadTree replaces the contents of the index with those of the given
|
||||||
|
// tree
|
||||||
|
func (v *Index) ReadTree(tree *Tree) error {
|
||||||
|
runtime.LockOSThread()
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
ret := C.git_index_read_tree(v.ptr, tree.cast_ptr);
|
||||||
|
if ret < 0 {
|
||||||
|
return MakeGitError(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (v *Index) WriteTree() (*Oid, error) {
|
func (v *Index) WriteTree() (*Oid, error) {
|
||||||
oid := new(Oid)
|
oid := new(Oid)
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -22,6 +23,34 @@ func TestCreateRepoAndStage(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIndexReadTree(t *testing.T) {
|
||||||
|
repo := createTestRepo(t)
|
||||||
|
defer cleanupTestRepo(t, repo)
|
||||||
|
|
||||||
|
_, _ = seedTestRepo(t, repo)
|
||||||
|
|
||||||
|
ref, err := repo.Head()
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
obj, err := ref.Peel(ObjectTree);
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
tree := obj.(*Tree)
|
||||||
|
|
||||||
|
idx, err := NewIndex()
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
err = idx.ReadTree(tree)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
id, err := idx.WriteTreeTo(repo)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
if tree.Id().Cmp(id) != 0 {
|
||||||
|
t.Fatalf("Read and written trees are not the same")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIndexWriteTreeTo(t *testing.T) {
|
func TestIndexWriteTreeTo(t *testing.T) {
|
||||||
repo := createTestRepo(t)
|
repo := createTestRepo(t)
|
||||||
defer cleanupTestRepo(t, repo)
|
defer cleanupTestRepo(t, repo)
|
||||||
|
@ -54,6 +83,10 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) {
|
||||||
idx, err := NewIndex()
|
idx, err := NewIndex()
|
||||||
checkFatal(t, err)
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
if idx.Path() != "" {
|
||||||
|
t.Fatal("in-memory repo has a path")
|
||||||
|
}
|
||||||
|
|
||||||
entry := IndexEntry{
|
entry := IndexEntry{
|
||||||
Path: "README",
|
Path: "README",
|
||||||
Id: blobID,
|
Id: blobID,
|
||||||
|
@ -120,6 +153,33 @@ 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)
|
||||||
|
|
||||||
|
if path != idx.Path() {
|
||||||
|
t.Fatalf("mismatched index paths, expected %v, got %v", path, idx.Path())
|
||||||
|
}
|
||||||
|
|
||||||
|
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