forked from coveord/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
note.go
247 lines (205 loc) · 5 KB
/
note.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
247
package git
/*
#include <git2.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
// This object represents the possible operations which can be
// performed on the collection of notes for a repository.
type NoteCollection struct {
doNotCompare
repo *Repository
}
// Create adds a note for an object
func (c *NoteCollection) Create(
ref string, author, committer *Signature, id *Oid,
note string, force bool) (*Oid, error) {
oid := new(Oid)
var cref *C.char
if ref == "" {
cref = nil
} else {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
authorSig, err := author.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return nil, err
}
defer C.git_signature_free(committerSig)
cnote := C.CString(note)
defer C.free(unsafe.Pointer(cnote))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_note_create(
oid.toC(), c.repo.ptr, cref, authorSig,
committerSig, id.toC(), cnote, cbool(force))
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
}
return oid, nil
}
// Read reads the note for an object
func (c *NoteCollection) Read(ref string, id *Oid) (*Note, error) {
var cref *C.char
if ref == "" {
cref = nil
} else {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var ptr *C.git_note
ret := C.git_note_read(&ptr, c.repo.ptr, cref, id.toC())
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newNoteFromC(ptr, c.repo), nil
}
// Remove removes the note for an object
func (c *NoteCollection) Remove(ref string, author, committer *Signature, id *Oid) error {
var cref *C.char
if ref == "" {
cref = nil
} else {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
authorSig, err := author.toC()
if err != nil {
return err
}
defer C.git_signature_free(authorSig)
committerSig, err := committer.toC()
if err != nil {
return err
}
defer C.git_signature_free(committerSig)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_note_remove(c.repo.ptr, cref, authorSig, committerSig, id.toC())
runtime.KeepAlive(c)
runtime.KeepAlive(id)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
// DefaultRef returns the default notes reference for a repository
func (c *NoteCollection) DefaultRef() (string, error) {
buf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_note_default_ref(&buf, c.repo.ptr)
runtime.KeepAlive(c)
if ecode < 0 {
return "", MakeGitError(ecode)
}
ret := C.GoString(buf.ptr)
C.git_buf_dispose(&buf)
return ret, nil
}
// Note
type Note struct {
doNotCompare
ptr *C.git_note
r *Repository
}
func newNoteFromC(ptr *C.git_note, r *Repository) *Note {
note := &Note{ptr: ptr, r: r}
runtime.SetFinalizer(note, (*Note).Free)
return note
}
// Free frees a git_note object
func (n *Note) Free() error {
if n.ptr == nil {
return ErrInvalid
}
runtime.SetFinalizer(n, nil)
C.git_note_free(n.ptr)
n.ptr = nil
return nil
}
// Author returns the signature of the note author
func (n *Note) Author() *Signature {
ptr := C.git_note_author(n.ptr)
return newSignatureFromC(ptr)
}
// Id returns the note object's id
func (n *Note) Id() *Oid {
ptr := C.git_note_id(n.ptr)
runtime.KeepAlive(n)
return newOidFromC(ptr)
}
// Committer returns the signature of the note committer
func (n *Note) Committer() *Signature {
ptr := C.git_note_committer(n.ptr)
runtime.KeepAlive(n)
return newSignatureFromC(ptr)
}
// Message returns the note message
func (n *Note) Message() string {
ret := C.GoString(C.git_note_message(n.ptr))
runtime.KeepAlive(n)
return ret
}
// NoteIterator
type NoteIterator struct {
doNotCompare
ptr *C.git_note_iterator
r *Repository
}
// NewNoteIterator creates a new iterator for notes
func (repo *Repository) NewNoteIterator(ref string) (*NoteIterator, error) {
var cref *C.char
if ref == "" {
cref = nil
} else {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
var ptr *C.git_note_iterator
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_note_iterator_new(&ptr, repo.ptr, cref)
runtime.KeepAlive(repo)
if ret < 0 {
return nil, MakeGitError(ret)
}
iter := &NoteIterator{ptr: ptr, r: repo}
runtime.SetFinalizer(iter, (*NoteIterator).Free)
return iter, nil
}
// Free frees the note interator
func (v *NoteIterator) Free() {
runtime.SetFinalizer(v, nil)
C.git_note_iterator_free(v.ptr)
}
// Next returns the current item (note id & annotated id) and advances the
// iterator internally to the next item
func (it *NoteIterator) Next() (noteId, annotatedId *Oid, err error) {
noteId, annotatedId = new(Oid), new(Oid)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_note_next(noteId.toC(), annotatedId.toC(), it.ptr)
runtime.KeepAlive(noteId)
runtime.KeepAlive(annotatedId)
runtime.KeepAlive(it)
if ret < 0 {
err = MakeGitError(ret)
}
return
}