-
Notifications
You must be signed in to change notification settings - Fork 316
/
git.go
323 lines (271 loc) · 7.86 KB
/
git.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package git
/*
#include <git2.h>
#include <git2/sys/openssl.h>
*/
import "C"
import (
"bytes"
"encoding/hex"
"errors"
"runtime"
"strings"
"unsafe"
)
type ErrorClass int
const (
ErrClassNone ErrorClass = C.GITERR_NONE
ErrClassNoMemory ErrorClass = C.GITERR_NOMEMORY
ErrClassOs ErrorClass = C.GITERR_OS
ErrClassInvalid ErrorClass = C.GITERR_INVALID
ErrClassReference ErrorClass = C.GITERR_REFERENCE
ErrClassZlib ErrorClass = C.GITERR_ZLIB
ErrClassRepository ErrorClass = C.GITERR_REPOSITORY
ErrClassConfig ErrorClass = C.GITERR_CONFIG
ErrClassRegex ErrorClass = C.GITERR_REGEX
ErrClassOdb ErrorClass = C.GITERR_ODB
ErrClassIndex ErrorClass = C.GITERR_INDEX
ErrClassObject ErrorClass = C.GITERR_OBJECT
ErrClassNet ErrorClass = C.GITERR_NET
ErrClassTag ErrorClass = C.GITERR_TAG
ErrClassTree ErrorClass = C.GITERR_TREE
ErrClassIndexer ErrorClass = C.GITERR_INDEXER
ErrClassSSL ErrorClass = C.GITERR_SSL
ErrClassSubmodule ErrorClass = C.GITERR_SUBMODULE
ErrClassThread ErrorClass = C.GITERR_THREAD
ErrClassStash ErrorClass = C.GITERR_STASH
ErrClassCheckout ErrorClass = C.GITERR_CHECKOUT
ErrClassFetchHead ErrorClass = C.GITERR_FETCHHEAD
ErrClassMerge ErrorClass = C.GITERR_MERGE
ErrClassSsh ErrorClass = C.GITERR_SSH
ErrClassFilter ErrorClass = C.GITERR_FILTER
ErrClassRevert ErrorClass = C.GITERR_REVERT
ErrClassCallback ErrorClass = C.GITERR_CALLBACK
ErrClassRebase ErrorClass = C.GITERR_REBASE
)
type ErrorCode int
const (
// No error
ErrOk ErrorCode = C.GIT_OK
// Generic error
ErrGeneric ErrorCode = C.GIT_ERROR
// Requested object could not be found
ErrNotFound ErrorCode = C.GIT_ENOTFOUND
// Object exists preventing operation
ErrExists ErrorCode = C.GIT_EEXISTS
// More than one object matches
ErrAmbigious ErrorCode = C.GIT_EAMBIGUOUS
// Output buffer too short to hold data
ErrBuffs ErrorCode = C.GIT_EBUFS
// GIT_EUSER is a special error that is never generated by libgit2
// code. You can return it from a callback (e.g to stop an iteration)
// to know that it was generated by the callback and not by libgit2.
ErrUser ErrorCode = C.GIT_EUSER
// Operation not allowed on bare repository
ErrBareRepo ErrorCode = C.GIT_EBAREREPO
// HEAD refers to branch with no commits
ErrUnbornBranch ErrorCode = C.GIT_EUNBORNBRANCH
// Merge in progress prevented operation
ErrUnmerged ErrorCode = C.GIT_EUNMERGED
// Reference was not fast-forwardable
ErrNonFastForward ErrorCode = C.GIT_ENONFASTFORWARD
// Name/ref spec was not in a valid format
ErrInvalidSpec ErrorCode = C.GIT_EINVALIDSPEC
// Checkout conflicts prevented operation
ErrConflict ErrorCode = C.GIT_ECONFLICT
// Lock file prevented operation
ErrLocked ErrorCode = C.GIT_ELOCKED
// Reference value does not match expected
ErrModified ErrorCode = C.GIT_EMODIFIED
// Authentication failed
ErrAuth ErrorCode = C.GIT_EAUTH
// Server certificate is invalid
ErrCertificate ErrorCode = C.GIT_ECERTIFICATE
// Patch/merge has already been applied
ErrApplied ErrorCode = C.GIT_EAPPLIED
// The requested peel operation is not possible
ErrPeel ErrorCode = C.GIT_EPEEL
// Unexpected EOF
ErrEOF ErrorCode = C.GIT_EEOF
// Uncommitted changes in index prevented operation
ErrUncommitted ErrorCode = C.GIT_EUNCOMMITTED
// The operation is not valid for a directory
ErrDirectory ErrorCode = C.GIT_EDIRECTORY
// A merge conflict exists and cannot continue
ErrMergeConflict ErrorCode = C.GIT_EMERGECONFLICT
// Internal only
ErrPassthrough ErrorCode = C.GIT_PASSTHROUGH
// Signals end of iteration with iterator
ErrIterOver ErrorCode = C.GIT_ITEROVER
)
var (
ErrInvalid = errors.New("Invalid state for operation")
)
var pointerHandles *HandleList
func init() {
pointerHandles = NewHandleList()
C.git_libgit2_init()
// Due to the multithreaded nature of Go and its interaction with
// calling C functions, we cannot work with a library that was not built
// with multi-threading support. The most likely outcome is a segfault
// or panic at an incomprehensible time, so let's make it easy by
// panicking right here.
if Features()&FeatureThreads == 0 {
panic("libgit2 was not built with threading support")
}
// This is not something we should be doing, as we may be
// stomping all over someone else's setup. The user should do
// this themselves or use some binding/wrapper which does it
// in such a way that they can be sure they're the only ones
// setting it up.
C.git_openssl_set_locking()
}
// Oid represents the id for a Git object.
type Oid [20]byte
func newOidFromC(coid *C.git_oid) *Oid {
if coid == nil {
return nil
}
oid := new(Oid)
copy(oid[0:20], C.GoBytes(unsafe.Pointer(coid), 20))
return oid
}
func NewOidFromBytes(b []byte) *Oid {
oid := new(Oid)
copy(oid[0:20], b[0:20])
return oid
}
func (oid *Oid) toC() *C.git_oid {
return (*C.git_oid)(unsafe.Pointer(oid))
}
func NewOid(s string) (*Oid, error) {
if len(s) > C.GIT_OID_HEXSZ {
return nil, errors.New("string is too long for oid")
}
o := new(Oid)
slice, error := hex.DecodeString(s)
if error != nil {
return nil, error
}
if len(slice) != 20 {
return nil, &GitError{"Invalid Oid", ErrClassNone, ErrGeneric}
}
copy(o[:], slice[:20])
return o, nil
}
func (oid *Oid) String() string {
return hex.EncodeToString(oid[:])
}
func (oid *Oid) Cmp(oid2 *Oid) int {
return bytes.Compare(oid[:], oid2[:])
}
func (oid *Oid) Copy() *Oid {
ret := new(Oid)
copy(ret[:], oid[:])
return ret
}
func (oid *Oid) Equal(oid2 *Oid) bool {
return bytes.Equal(oid[:], oid2[:])
}
func (oid *Oid) IsZero() bool {
for _, a := range oid {
if a != 0 {
return false
}
}
return true
}
func (oid *Oid) NCmp(oid2 *Oid, n uint) int {
return bytes.Compare(oid[:n], oid2[:n])
}
func ShortenOids(ids []*Oid, minlen int) (int, error) {
shorten := C.git_oid_shorten_new(C.size_t(minlen))
if shorten == nil {
panic("Out of memory")
}
defer C.git_oid_shorten_free(shorten)
var ret C.int
runtime.LockOSThread()
defer runtime.UnlockOSThread()
for _, id := range ids {
buf := make([]byte, 41)
C.git_oid_fmt((*C.char)(unsafe.Pointer(&buf[0])), id.toC())
buf[40] = 0
ret = C.git_oid_shorten_add(shorten, (*C.char)(unsafe.Pointer(&buf[0])))
if ret < 0 {
return int(ret), MakeGitError(ret)
}
}
runtime.KeepAlive(ids)
return int(ret), nil
}
type GitError struct {
Message string
Class ErrorClass
Code ErrorCode
}
func (e GitError) Error() string {
return e.Message
}
func IsErrorClass(err error, c ErrorClass) bool {
if err == nil {
return false
}
if gitError, ok := err.(*GitError); ok {
return gitError.Class == c
}
return false
}
func IsErrorCode(err error, c ErrorCode) bool {
if err == nil {
return false
}
if gitError, ok := err.(*GitError); ok {
return gitError.Code == c
}
return false
}
func MakeGitError(errorCode C.int) error {
var errMessage string
var errClass ErrorClass
if errorCode != C.GIT_ITEROVER {
err := C.giterr_last()
if err != nil {
errMessage = C.GoString(err.message)
errClass = ErrorClass(err.klass)
} else {
errClass = ErrClassInvalid
}
}
return &GitError{errMessage, errClass, ErrorCode(errorCode)}
}
func MakeGitError2(err int) error {
return MakeGitError(C.int(err))
}
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)
}
func Discover(start string, across_fs bool, ceiling_dirs []string) (string, error) {
ceildirs := C.CString(strings.Join(ceiling_dirs, string(C.GIT_PATH_LIST_SEPARATOR)))
defer C.free(unsafe.Pointer(ceildirs))
cstart := C.CString(start)
defer C.free(unsafe.Pointer(cstart))
var buf C.git_buf
defer C.git_buf_free(&buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_discover(&buf, cstart, cbool(across_fs), ceildirs)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(buf.ptr), nil
}