Skip to content

Commit 193deb7

Browse files
committed
Merge pull request #202 from libgit2/index-basics
Add a few basic index operations
2 parents f7781c0 + 72c19f7 commit 193deb7

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

index.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ func NewIndex() (*Index, error) {
9696
return &Index{ptr: ptr}, nil
9797
}
9898

99+
// OpenIndex creates a new index at the given path. If the file does
100+
// not exist it will be created when Write() is called.
101+
func OpenIndex(path string) (*Index, error) {
102+
var ptr *C.git_index
103+
104+
var cpath = C.CString(path)
105+
defer C.free(unsafe.Pointer(cpath))
106+
107+
runtime.LockOSThread()
108+
defer runtime.UnlockOSThread()
109+
110+
if err := C.git_index_open(&ptr, cpath); err < 0 {
111+
return nil, MakeGitError(err)
112+
}
113+
114+
return &Index{ptr: ptr}, nil
115+
}
116+
117+
// Path returns the index' path on disk or an empty string if it
118+
// exists only in memory.
119+
func (v *Index) Path() string {
120+
return C.GoString(C.git_index_path(v.ptr))
121+
}
122+
99123
// Add adds or replaces the given entry to the index, making a copy of
100124
// the data
101125
func (v *Index) Add(entry *IndexEntry) error {
@@ -240,6 +264,20 @@ func (v *Index) WriteTreeTo(repo *Repository) (*Oid, error) {
240264
return oid, nil
241265
}
242266

267+
// ReadTree replaces the contents of the index with those of the given
268+
// tree
269+
func (v *Index) ReadTree(tree *Tree) error {
270+
runtime.LockOSThread()
271+
defer runtime.UnlockOSThread()
272+
273+
ret := C.git_index_read_tree(v.ptr, tree.cast_ptr);
274+
if ret < 0 {
275+
return MakeGitError(ret)
276+
}
277+
278+
return nil
279+
}
280+
243281
func (v *Index) WriteTree() (*Oid, error) {
244282
oid := new(Oid)
245283

index_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git
22

33
import (
44
"io/ioutil"
5+
"os"
56
"runtime"
67
"testing"
78
)
@@ -22,6 +23,34 @@ func TestCreateRepoAndStage(t *testing.T) {
2223
}
2324
}
2425

26+
func TestIndexReadTree(t *testing.T) {
27+
repo := createTestRepo(t)
28+
defer cleanupTestRepo(t, repo)
29+
30+
_, _ = seedTestRepo(t, repo)
31+
32+
ref, err := repo.Head()
33+
checkFatal(t, err)
34+
35+
obj, err := ref.Peel(ObjectTree);
36+
checkFatal(t, err)
37+
38+
tree := obj.(*Tree)
39+
40+
idx, err := NewIndex()
41+
checkFatal(t, err)
42+
43+
err = idx.ReadTree(tree)
44+
checkFatal(t, err)
45+
46+
id, err := idx.WriteTreeTo(repo)
47+
checkFatal(t, err)
48+
49+
if tree.Id().Cmp(id) != 0 {
50+
t.Fatalf("Read and written trees are not the same")
51+
}
52+
}
53+
2554
func TestIndexWriteTreeTo(t *testing.T) {
2655
repo := createTestRepo(t)
2756
defer cleanupTestRepo(t, repo)
@@ -54,6 +83,10 @@ func TestIndexAddAndWriteTreeTo(t *testing.T) {
5483
idx, err := NewIndex()
5584
checkFatal(t, err)
5685

86+
if idx.Path() != "" {
87+
t.Fatal("in-memory repo has a path")
88+
}
89+
5790
entry := IndexEntry{
5891
Path: "README",
5992
Id: blobID,
@@ -120,6 +153,33 @@ func TestIndexAddAllCallback(t *testing.T) {
120153
}
121154
}
122155

156+
func TestIndexOpen(t *testing.T) {
157+
repo := createTestRepo(t)
158+
defer cleanupTestRepo(t, repo)
159+
160+
path := repo.Workdir() + "/heyindex"
161+
162+
_, err := os.Stat(path)
163+
if !os.IsNotExist(err) {
164+
t.Fatal("new index file already exists")
165+
}
166+
167+
idx, err := OpenIndex(path)
168+
checkFatal(t, err)
169+
170+
if path != idx.Path() {
171+
t.Fatalf("mismatched index paths, expected %v, got %v", path, idx.Path())
172+
}
173+
174+
err = idx.Write()
175+
checkFatal(t, err)
176+
177+
_, err = os.Stat(path)
178+
if os.IsNotExist(err) {
179+
t.Fatal("new index file did not get written")
180+
}
181+
}
182+
123183
func checkFatal(t *testing.T, err error) {
124184
if err == nil {
125185
return

0 commit comments

Comments
 (0)