forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
195 lines (151 loc) · 4.13 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package git
/*
#include <git2.h>
extern int _go_git_treewalk(git_tree *tree, git_treewalk_mode mode, void *ptr);
*/
import "C"
import (
"runtime"
"unsafe"
)
type Filemode int
const (
FilemodeTree Filemode = C.GIT_FILEMODE_TREE
FilemodeBlob Filemode = C.GIT_FILEMODE_BLOB
FilemodeBlobExecutable Filemode = C.GIT_FILEMODE_BLOB_EXECUTABLE
FilemodeLink Filemode = C.GIT_FILEMODE_LINK
FilemodeCommit Filemode = C.GIT_FILEMODE_COMMIT
)
type Tree struct {
Object
cast_ptr *C.git_tree
}
type TreeEntry struct {
Name string
Id *Oid
Type ObjectType
Filemode Filemode
}
func newTreeEntry(entry *C.git_tree_entry) *TreeEntry {
return &TreeEntry{
C.GoString(C.git_tree_entry_name(entry)),
newOidFromC(C.git_tree_entry_id(entry)),
ObjectType(C.git_tree_entry_type(entry)),
Filemode(C.git_tree_entry_filemode(entry)),
}
}
func (t Tree) EntryByName(filename string) *TreeEntry {
cname := C.CString(filename)
defer C.free(unsafe.Pointer(cname))
entry := C.git_tree_entry_byname(t.cast_ptr, cname)
if entry == nil {
return nil
}
return newTreeEntry(entry)
}
// EntryById performs a lookup for a tree entry with the given SHA value.
//
// It returns a *TreeEntry that is owned by the Tree. You don't have to
// free it, but you must not use it after the Tree is freed.
//
// Warning: this must examine every entry in the tree, so it is not fast.
func (t Tree) EntryById(id *Oid) *TreeEntry {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
entry := C.git_tree_entry_byid(t.cast_ptr, id.toC())
if entry == nil {
return nil
}
return newTreeEntry(entry)
}
// EntryByPath looks up an entry by its full path, recursing into
// deeper trees if necessary (i.e. if there are slashes in the path)
func (t Tree) EntryByPath(path string) (*TreeEntry, error) {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
var entry *C.git_tree_entry
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_tree_entry_bypath(&entry, t.cast_ptr, cpath)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newTreeEntry(entry), nil
}
func (t Tree) EntryByIndex(index uint64) *TreeEntry {
entry := C.git_tree_entry_byindex(t.cast_ptr, C.size_t(index))
if entry == nil {
return nil
}
return newTreeEntry(entry)
}
func (t Tree) EntryCount() uint64 {
num := C.git_tree_entrycount(t.cast_ptr)
return uint64(num)
}
type TreeWalkCallback func(string, *TreeEntry) int
//export CallbackGitTreeWalk
func CallbackGitTreeWalk(_root *C.char, _entry unsafe.Pointer, ptr unsafe.Pointer) C.int {
root := C.GoString(_root)
entry := (*C.git_tree_entry)(_entry)
if callback, ok := pointerHandles.Get(ptr).(TreeWalkCallback); ok {
return C.int(callback(root, newTreeEntry(entry)))
} else {
panic("invalid treewalk callback")
}
}
func (t Tree) Walk(callback TreeWalkCallback) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ptr := pointerHandles.Track(callback)
defer pointerHandles.Untrack(ptr)
err := C._go_git_treewalk(
t.cast_ptr,
C.GIT_TREEWALK_PRE,
ptr,
)
if err < 0 {
return MakeGitError(err)
}
return nil
}
type TreeBuilder struct {
ptr *C.git_treebuilder
repo *Repository
}
func (v *TreeBuilder) Free() {
runtime.SetFinalizer(v, nil)
C.git_treebuilder_free(v.ptr)
}
func (v *TreeBuilder) Insert(filename string, id *Oid, filemode Filemode) error {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_treebuilder_insert(nil, v.ptr, cfilename, id.toC(), C.git_filemode_t(filemode))
if err < 0 {
return MakeGitError(err)
}
return nil
}
func (v *TreeBuilder) Remove(filename string) error {
cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(cfilename))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_treebuilder_remove(v.ptr, cfilename)
if err < 0 {
return MakeGitError(err)
}
return nil
}
func (v *TreeBuilder) Write() (*Oid, error) {
oid := new(Oid)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_treebuilder_write(oid.toC(), v.ptr)
if err < 0 {
return nil, MakeGitError(err)
}
return oid, nil
}