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
|
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import (
"bytes"
"unsafe"
)
const (
ITEROVER = C.GIT_ITEROVER
EEXISTS = C.GIT_EEXISTS
ENOTFOUND = C.GIT_ENOTFOUND
)
func init() {
C.git_threads_init()
}
// Oid
type Oid struct {
bytes [20]byte
}
func newOidFromC(coid *C.git_oid) *Oid {
if coid == nil {
return nil
}
oid := new(Oid)
copy(oid.bytes[0:20], C.GoBytes(unsafe.Pointer(coid), 20))
return oid
}
func NewOid(b []byte) *Oid {
oid := new(Oid)
copy(oid.bytes[0:20], b[0:20])
return oid
}
func (oid *Oid) toC() *C.git_oid {
return (*C.git_oid)(unsafe.Pointer(&oid.bytes))
}
func NewOidFromString(s string) (*Oid, error) {
o := new(Oid)
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
if C.git_oid_fromstr(o.toC(), cs) < 0 {
return nil, LastError()
}
return o, nil
}
func (oid *Oid) String() string {
buf := make([]byte, 40)
C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), oid.toC())
return string(buf)
}
func (oid *Oid) Bytes() []byte {
return oid.bytes[0:]
}
func (oid *Oid) Cmp(oid2 *Oid) int {
return bytes.Compare(oid.bytes[:], oid2.bytes[:])
}
func (oid *Oid) Copy() *Oid {
ret := new(Oid)
copy(ret.bytes[:], oid.bytes[:])
return ret
}
func (oid *Oid) Equal(oid2 *Oid) bool {
return bytes.Equal(oid.bytes[:], oid2.bytes[:])
}
func (oid *Oid) IsZero() bool {
for _, a := range(oid.bytes) {
if a != '0' {
return false
}
}
return true
}
func (oid *Oid) NCmp(oid2 *Oid, n uint) int {
return bytes.Compare(oid.bytes[:n], oid2.bytes[:n])
}
type GitError struct {
Message string
Code int
}
func (e GitError) Error() string{
return e.Message
}
func LastError() error {
err := C.giterr_last()
return &GitError{C.GoString(err.message), int(err.klass)}
}
func cbool(b bool) C.int {
if (b) {
return C.int(1)
}
return C.int(0)
}
func ucbool(b bool) C.uint {
if (b) {
return C.uint(1)
}
return C.uint(0)
}
|