Merge pull request #202 from libgit2/index-basics
Add a few basic index operations
Carlos MartÃn Nieto
9 years ago
95 | 95 |
return &Index{ptr: ptr}, nil
|
96 | 96 |
}
|
97 | 97 |
|
|
98 |
// OpenIndex creates a new index at the given path. If the file does
|
|
99 |
// not exist it will be created when Write() is called.
|
|
100 |
func OpenIndex(path string) (*Index, error) {
|
|
101 |
var ptr *C.git_index
|
|
102 |
|
|
103 |
var cpath = C.CString(path)
|
|
104 |
defer C.free(unsafe.Pointer(cpath))
|
|
105 |
|
|
106 |
runtime.LockOSThread()
|
|
107 |
defer runtime.UnlockOSThread()
|
|
108 |
|
|
109 |
if err := C.git_index_open(&ptr, cpath); err < 0 {
|
|
110 |
return nil, MakeGitError(err)
|
|
111 |
}
|
|
112 |
|
|
113 |
return &Index{ptr: ptr}, nil
|
|
114 |
}
|
|
115 |
|
|
116 |
// Path returns the index' path on disk or an empty string if it
|
|
117 |
// exists only in memory.
|
|
118 |
func (v *Index) Path() string {
|
|
119 |
return C.GoString(C.git_index_path(v.ptr))
|
|
120 |
}
|
|
121 |
|
98 | 122 |
// Add adds or replaces the given entry to the index, making a copy of
|
99 | 123 |
// the data
|
100 | 124 |
func (v *Index) Add(entry *IndexEntry) error {
|
|
239 | 263 |
return oid, nil
|
240 | 264 |
}
|
241 | 265 |
|
|
266 |
// ReadTree replaces the contents of the index with those of the given
|
|
267 |
// tree
|
|
268 |
func (v *Index) ReadTree(tree *Tree) error {
|
|
269 |
runtime.LockOSThread()
|
|
270 |
defer runtime.UnlockOSThread()
|
|
271 |
|
|
272 |
ret := C.git_index_read_tree(v.ptr, tree.cast_ptr);
|
|
273 |
if ret < 0 {
|
|
274 |
return MakeGitError(ret)
|
|
275 |
}
|
|
276 |
|
|
277 |
return nil
|
|
278 |
}
|
|
279 |
|
242 | 280 |
func (v *Index) WriteTree() (*Oid, error) {
|
243 | 281 |
oid := new(Oid)
|
244 | 282 |
|
1 | 1 |
|
2 | 2 |
import (
|
3 | 3 |
"io/ioutil"
|
|
4 |
"os"
|
4 | 5 |
"runtime"
|
5 | 6 |
"testing"
|
6 | 7 |
)
|
|
18 | 19 |
|
19 | 20 |
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
|
20 | 21 |
t.Fatalf("%v", treeId.String())
|
|
22 |
}
|
|
23 |
}
|
|
24 |
|
|
25 |
func TestIndexReadTree(t *testing.T) {
|
|
26 |
repo := createTestRepo(t)
|
|
27 |
defer cleanupTestRepo(t, repo)
|
|
28 |
|
|
29 |
_, _ = seedTestRepo(t, repo)
|
|
30 |
|
|
31 |
ref, err := repo.Head()
|
|
32 |
checkFatal(t, err)
|
|
33 |
|
|
34 |
obj, err := ref.Peel(ObjectTree);
|
|
35 |
checkFatal(t, err)
|
|
36 |
|
|
37 |
tree := obj.(*Tree)
|
|
38 |
|
|
39 |
idx, err := NewIndex()
|
|
40 |
checkFatal(t, err)
|
|
41 |
|
|
42 |
err = idx.ReadTree(tree)
|
|
43 |
checkFatal(t, err)
|
|
44 |
|
|
45 |
id, err := idx.WriteTreeTo(repo)
|
|
46 |
checkFatal(t, err)
|
|
47 |
|
|
48 |
if tree.Id().Cmp(id) != 0 {
|
|
49 |
t.Fatalf("Read and written trees are not the same")
|
21 | 50 |
}
|
22 | 51 |
}
|
23 | 52 |
|
|
52 | 81 |
|
53 | 82 |
idx, err := NewIndex()
|
54 | 83 |
checkFatal(t, err)
|
|
84 |
|
|
85 |
if idx.Path() != "" {
|
|
86 |
t.Fatal("in-memory repo has a path")
|
|
87 |
}
|
55 | 88 |
|
56 | 89 |
entry := IndexEntry{
|
57 | 90 |
Path: "README",
|
|
119 | 152 |
}
|
120 | 153 |
}
|
121 | 154 |
|
|
155 |
func TestIndexOpen(t *testing.T) {
|
|
156 |
repo := createTestRepo(t)
|
|
157 |
defer cleanupTestRepo(t, repo)
|
|
158 |
|
|
159 |
path := repo.Workdir() + "/heyindex"
|
|
160 |
|
|
161 |
_, err := os.Stat(path)
|
|
162 |
if !os.IsNotExist(err) {
|
|
163 |
t.Fatal("new index file already exists")
|
|
164 |
}
|
|
165 |
|
|
166 |
idx, err := OpenIndex(path)
|
|
167 |
checkFatal(t, err)
|
|
168 |
|
|
169 |
if path != idx.Path() {
|
|
170 |
t.Fatalf("mismatched index paths, expected %v, got %v", path, idx.Path())
|
|
171 |
}
|
|
172 |
|
|
173 |
err = idx.Write()
|
|
174 |
checkFatal(t, err)
|
|
175 |
|
|
176 |
_, err = os.Stat(path)
|
|
177 |
if os.IsNotExist(err) {
|
|
178 |
t.Fatal("new index file did not get written")
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
122 | 182 |
func checkFatal(t *testing.T, err error) {
|
123 | 183 |
if err == nil {
|
124 | 184 |
return
|