summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--index.go14
-rw-r--r--index_test.go28
2 files changed, 42 insertions, 0 deletions
diff --git a/index.go b/index.go
index 9f37f33..009aeb6 100644
--- a/index.go
+++ b/index.go
@@ -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)
diff --git a/index_test.go b/index_test.go
index 647a0b8..a1f0c9c 100644
--- a/index_test.go
+++ b/index_test.go
@@ -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)