forked from coveord/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
238 lines (198 loc) · 4.84 KB
/
object.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
package git
/*
#include <git2.h>
*/
import "C"
import (
"errors"
"fmt"
"runtime"
)
type ObjectType int
const (
ObjectAny ObjectType = C.GIT_OBJECT_ANY
ObjectInvalid ObjectType = C.GIT_OBJECT_INVALID
ObjectCommit ObjectType = C.GIT_OBJECT_COMMIT
ObjectTree ObjectType = C.GIT_OBJECT_TREE
ObjectBlob ObjectType = C.GIT_OBJECT_BLOB
ObjectTag ObjectType = C.GIT_OBJECT_TAG
)
type Object struct {
doNotCompare
ptr *C.git_object
repo *Repository
}
// Objecter lets us accept any kind of Git object in functions.
type Objecter interface {
AsObject() *Object
}
func (t ObjectType) String() string {
switch t {
case ObjectAny:
return "Any"
case ObjectInvalid:
return "Invalid"
case ObjectCommit:
return "Commit"
case ObjectTree:
return "Tree"
case ObjectBlob:
return "Blob"
case ObjectTag:
return "Tag"
}
// Never reached
return ""
}
func (o *Object) Id() *Oid {
ret := newOidFromC(C.git_object_id(o.ptr))
runtime.KeepAlive(o)
return ret
}
func (o *Object) ShortId() (string, error) {
resultBuf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_object_short_id(&resultBuf, o.ptr)
runtime.KeepAlive(o)
if ecode < 0 {
return "", MakeGitError(ecode)
}
defer C.git_buf_dispose(&resultBuf)
return C.GoString(resultBuf.ptr), nil
}
func (o *Object) Type() ObjectType {
ret := ObjectType(C.git_object_type(o.ptr))
runtime.KeepAlive(o)
return ret
}
// Owner returns a weak reference to the repository which owns this object.
// This won't keep the underlying repository alive, but it should still be
// Freed.
func (o *Object) Owner() *Repository {
repo := newRepositoryFromC(C.git_object_owner(o.ptr))
runtime.KeepAlive(o)
repo.weak = true
return repo
}
func dupObject(obj *Object, kind ObjectType) (*C.git_object, error) {
if obj.Type() != kind {
return nil, errors.New(fmt.Sprintf("object is not a %v", kind))
}
var cobj *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_object_dup(&cobj, obj.ptr)
runtime.KeepAlive(obj)
if err < 0 {
return nil, MakeGitError(err)
}
return cobj, nil
}
func allocTree(ptr *C.git_tree, repo *Repository) *Tree {
tree := &Tree{
Object: Object{
ptr: (*C.git_object)(ptr),
repo: repo,
},
cast_ptr: ptr,
}
runtime.SetFinalizer(tree, (*Tree).Free)
return tree
}
func (o *Object) AsTree() (*Tree, error) {
cobj, err := dupObject(o, ObjectTree)
if err != nil {
return nil, err
}
return allocTree((*C.git_tree)(cobj), o.repo), nil
}
func allocCommit(ptr *C.git_commit, repo *Repository) *Commit {
commit := &Commit{
Object: Object{
ptr: (*C.git_object)(ptr),
repo: repo,
},
cast_ptr: ptr,
}
runtime.SetFinalizer(commit, (*Commit).Free)
return commit
}
func (o *Object) AsCommit() (*Commit, error) {
cobj, err := dupObject(o, ObjectCommit)
if err != nil {
return nil, err
}
return allocCommit((*C.git_commit)(cobj), o.repo), nil
}
func allocBlob(ptr *C.git_blob, repo *Repository) *Blob {
blob := &Blob{
Object: Object{
ptr: (*C.git_object)(ptr),
repo: repo,
},
cast_ptr: ptr,
}
runtime.SetFinalizer(blob, (*Blob).Free)
return blob
}
func (o *Object) AsBlob() (*Blob, error) {
cobj, err := dupObject(o, ObjectBlob)
if err != nil {
return nil, err
}
return allocBlob((*C.git_blob)(cobj), o.repo), nil
}
func allocTag(ptr *C.git_tag, repo *Repository) *Tag {
tag := &Tag{
Object: Object{
ptr: (*C.git_object)(ptr),
repo: repo,
},
cast_ptr: ptr,
}
runtime.SetFinalizer(tag, (*Tag).Free)
return tag
}
func (o *Object) AsTag() (*Tag, error) {
cobj, err := dupObject(o, ObjectTag)
if err != nil {
return nil, err
}
return allocTag((*C.git_tag)(cobj), o.repo), nil
}
func (o *Object) Free() {
runtime.SetFinalizer(o, nil)
C.git_object_free(o.ptr)
}
// Peel recursively peels an object until an object of the specified type is met.
//
// If the query cannot be satisfied due to the object model, ErrorCodeInvalidSpec
// will be returned (e.g. trying to peel a blob to a tree).
//
// If you pass ObjectAny as the target type, then the object will be peeled
// until the type changes. A tag will be peeled until the referenced object
// is no longer a tag, and a commit will be peeled to a tree. Any other object
// type will return ErrorCodeInvalidSpec.
//
// If peeling a tag we discover an object which cannot be peeled to the target
// type due to the object model, an error will be returned.
func (o *Object) Peel(t ObjectType) (*Object, error) {
var cobj *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := C.git_object_peel(&cobj, o.ptr, C.git_object_t(t))
runtime.KeepAlive(o)
if err < 0 {
return nil, MakeGitError(err)
}
return allocObject(cobj, o.repo), nil
}
func allocObject(cobj *C.git_object, repo *Repository) *Object {
obj := &Object{
ptr: cobj,
repo: repo,
}
runtime.SetFinalizer(obj, (*Object).Free)
return obj
}