Index: Add ReadTree()

This commit is contained in:
Carlos Martín Nieto 2015-05-19 14:33:30 +02:00
parent f7781c0e00
commit a8ad0d2040
2 changed files with 42 additions and 0 deletions

View File

@ -240,6 +240,20 @@ func (v *Index) WriteTreeTo(repo *Repository) (*Oid, error) {
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) {
oid := new(Oid)

View File

@ -22,6 +22,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) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)