/
note.go
79 lines (70 loc) · 1.84 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
package git2
// #cgo pkg-config: libgit2
// #include <git2.h>
import "C"
import (
"unsafe"
)
type Note struct {
git_note *C.git_note
}
func (note *Note) Free() {
C.git_note_free(note.git_note)
}
func (note *Note) Message() string {
return C.GoString(C.git_note_message(note.git_note))
}
func (note *Note) Oid() *Oid {
oid := new(Oid)
oid.git_oid = C.git_note_oid(note.git_note)
if oid.git_oid == nil {
return nil
}
return oid
}
func (repo *Repository) CreateNote(author, committer *Signature, ref string, oid *Oid, note string) (*Oid, error) {
out := new(Oid)
var cref *C.char
if ref != "" {
cref = C.CString(ref)
defer C.free(unsafe.Pointer(cref))
}
var cnote *C.char
if note != "" {
cnote = C.CString(note)
defer C.free(unsafe.Pointer(cnote))
}
ecode := C.git_note_create(out.git_oid, repo.git_repository, author.git_signature, committer.git_signature, cref, oid.git_oid, cnote)
if ecode != git_SUCCESS {
return nil, gitError()
}
return out, nil
}
func (repo *Repository) DefaultNoteRef() (string, error) {
var ref [1]int8
cref := (*C.char)(&ref[0])
ecode := C.git_note_default_ref(&cref, repo.git_repository)
if ecode != git_SUCCESS {
return "", gitError()
}
return C.GoString(cref), nil
}
func (repo *Repository) ReadNote(ref string, oid *Oid) (*Note, error) {
note := new(Note)
cref := C.CString(ref)
defer C.free(unsafe.Pointer(cref))
ecode := C.git_note_read(¬e.git_note, repo.git_repository, cref, oid.git_oid)
if ecode != git_SUCCESS {
return nil, gitError()
}
return note, nil
}
func (repo *Repository) RemoveNote(ref string, author, committer *Signature, oid *Oid) error {
cref := C.CString(ref)
defer C.free(unsafe.Pointer(cref))
ecode := C.git_note_remove(repo.git_repository, cref, author.git_signature, committer.git_signature, oid.git_oid)
if ecode != git_SUCCESS {
return gitError()
}
return nil
}