This repository has been archived by the owner on Mar 14, 2019. It is now read-only.
forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
247 lines (192 loc) · 5.09 KB
/
repository.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import (
"unsafe"
"runtime"
)
// Repository
type Repository struct {
ptr *C.git_repository
}
func OpenRepository(path string) (*Repository, error) {
repo := new(Repository)
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.git_repository_open(&repo.ptr, cpath)
if ret < 0 {
return nil, LastError()
}
runtime.SetFinalizer(repo, (*Repository).Free)
return repo, nil
}
func InitRepository(path string, isbare bool) (*Repository, error) {
repo := new(Repository)
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
ret := C.git_repository_init(&repo.ptr, cpath, ucbool(isbare))
if ret < 0 {
return nil, LastError()
}
runtime.SetFinalizer(repo, (*Repository).Free)
return repo, nil
}
func (v *Repository) Free() {
runtime.SetFinalizer(v, nil)
C.git_repository_free(v.ptr)
}
func (v *Repository) Config() (*Config, error) {
config := new(Config)
ret := C.git_repository_config(&config.ptr, v.ptr)
if ret < 0 {
return nil, LastError()
}
return config, nil
}
func (v *Repository) Index() (*Index, error) {
var ptr *C.git_index
ret := C.git_repository_index(&ptr, v.ptr)
if ret < 0 {
return nil, LastError()
}
return newIndexFromC(ptr), nil
}
func (v *Repository) LookupTree(oid *Oid) (*Tree, error) {
tree := new(Tree)
ret := C.git_tree_lookup(&tree.ptr, v.ptr, oid.toC())
if ret < 0 {
return nil, LastError()
}
return tree, nil
}
func (v *Repository) LookupCommit(o *Oid) (*Commit, error) {
commit := new(Commit)
ecode := C.git_commit_lookup(&commit.ptr, v.ptr, o.toC())
if ecode < 0 {
return nil, LastError()
}
return commit, nil
}
func (v *Repository) LookupBlob(o *Oid) (*Blob, error) {
blob := new(Blob)
ecode := C.git_blob_lookup(&blob.ptr, v.ptr, o.toC())
if ecode < 0 {
return nil, LastError()
}
runtime.SetFinalizer(blob, (*Blob).Free)
return blob, nil
}
func (v *Repository) LookupReference(name string) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var ptr *C.git_reference
ecode := C.git_reference_lookup(&ptr, v.ptr, cname)
if ecode < 0 {
return nil, LastError()
}
return newReferenceFromC(ptr), nil
}
func (v *Repository) CreateReference(name string, oid *Oid, force bool) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var ptr *C.git_reference
ecode := C.git_reference_create(&ptr, v.ptr, cname, oid.toC(), cbool(force))
if ecode < 0 {
return nil, LastError()
}
return newReferenceFromC(ptr), nil
}
func (v *Repository) CreateSymbolicReference(name, target string, force bool) (*Reference, error) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ctarget := C.CString(target)
defer C.free(unsafe.Pointer(ctarget))
var ptr *C.git_reference
ecode := C.git_reference_symbolic_create(&ptr, v.ptr, cname, ctarget, cbool(force))
if ecode < 0 {
return nil, LastError()
}
return newReferenceFromC(ptr), nil
}
func (v *Repository) Walk() (*RevWalk, error) {
walk := new(RevWalk)
ecode := C.git_revwalk_new(&walk.ptr, v.ptr)
if ecode < 0 {
return nil, LastError()
}
walk.repo = v
runtime.SetFinalizer(walk, freeRevWalk)
return walk, nil
}
func (v *Repository) CreateCommit(
refname string, author, committer *Signature,
message string, tree *Tree, parents ...*Commit) (*Oid, error) {
oid := new(Oid)
cref := C.CString(refname)
defer C.free(unsafe.Pointer(cref))
cmsg := C.CString(message)
defer C.free(unsafe.Pointer(cmsg))
var cparents []*C.git_commit = nil
var parentsarg **C.git_commit = nil
nparents:= len(parents)
if nparents > 0 {
cparents = make([]*C.git_commit, nparents)
for i, v := range parents {
cparents[i] = v.ptr
}
parentsarg = &cparents[0]
}
authorSig := author.toC()
defer C.git_signature_free(authorSig)
committerSig := committer.toC()
defer C.git_signature_free(committerSig)
ret := C.git_commit_create(
oid.toC(), v.ptr, cref,
authorSig, committerSig,
nil, cmsg, tree.ptr, C.int(nparents), parentsarg)
if ret < 0 {
return nil, LastError()
}
return oid, nil
}
func (v *Odb) Free() {
runtime.SetFinalizer(v, nil)
C.git_odb_free(v.ptr)
}
func (v *Repository) Odb() (odb *Odb, err error) {
odb = new(Odb)
if ret := C.git_repository_odb(&odb.ptr, v.ptr); ret < 0 {
return nil, LastError()
}
runtime.SetFinalizer(odb, (*Odb).Free)
return
}
func (repo *Repository) Path() string {
return C.GoString(C.git_repository_path(repo.ptr))
}
func (repo *Repository) IsBare() (bool) {
return C.git_repository_is_bare(repo.ptr) != 0
}
func (repo *Repository) Workdir() string {
return C.GoString(C.git_repository_workdir(repo.ptr))
}
func (repo *Repository) SetWorkdir(workdir string, updateGitlink bool) error {
cstr := C.CString(workdir)
defer C.free(unsafe.Pointer(cstr))
if C.git_repository_set_workdir(repo.ptr, cstr, cbool(updateGitlink)) < 0 {
return LastError()
}
return nil
}
func (v *Repository) TreeBuilder() (*TreeBuilder, error) {
bld := new(TreeBuilder)
if ret := C.git_treebuilder_create(&bld.ptr, nil); ret < 0 {
return nil, LastError()
}
bld.repo = v
return bld, nil
}