Codebase list golang-gopkg-libgit2-git2go.v30 / 193deb7
Merge pull request #202 from libgit2/index-basics Add a few basic index operations Carlos Martín Nieto 9 years ago
2 changed file(s) with 98 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
9595 return &Index{ptr: ptr}, nil
9696 }
9797
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
98122 // Add adds or replaces the given entry to the index, making a copy of
99123 // the data
100124 func (v *Index) Add(entry *IndexEntry) error {
239263 return oid, nil
240264 }
241265
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
242280 func (v *Index) WriteTree() (*Oid, error) {
243281 oid := new(Oid)
244282
11
22 import (
33 "io/ioutil"
4 "os"
45 "runtime"
56 "testing"
67 )
1819
1920 if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
2021 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")
2150 }
2251 }
2352
5281
5382 idx, err := NewIndex()
5483 checkFatal(t, err)
84
85 if idx.Path() != "" {
86 t.Fatal("in-memory repo has a path")
87 }
5588
5689 entry := IndexEntry{
5790 Path: "README",
119152 }
120153 }
121154
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
122182 func checkFatal(t *testing.T, err error) {
123183 if err == nil {
124184 return