/
object.go
65 lines (53 loc) · 1.12 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
package goit
//#include <git2.h>
import "C"
import "unsafe"
const _GIT_OID_HEXSZ = C.GIT_OID_HEXSZ
// Unique git object identifier (sha)
type Oid struct {
oid *C.git_oid
}
func NewOid() *Oid {
return &Oid{new(C.git_oid)}
}
func NewOidFromString(from string) (*Oid, error) {
oid := NewOid()
cfrom := C.CString(from)
defer C.free(unsafe.Pointer(cfrom))
n := len(from)
if n > _GIT_OID_HEXSZ {
n = _GIT_OID_HEXSZ
}
err := gitError(C.git_oid_fromstrn(oid.oid, cfrom, C.size_t(n)))
if err != nil {
return nil, err
}
return oid, nil
}
func (o Oid) String() string {
if o.oid == nil {
return "(invalid)"
}
ptr := C.git_oid_allocfmt(o.oid)
defer C.free(unsafe.Pointer(ptr))
return C.GoString(ptr)
}
type Object struct {
obj *C.struct_git_object
}
func (o Object) Oid() Oid {
oid := Oid{}
oid.oid = C.git_object_id(o.obj)
return oid
}
func (o Object) Type() string {
return C.GoString(C.git_object_type2string(C.git_object_type(o.obj)))
}
func (o Object) String() string {
return o.Oid().String()
}
func (o Object) Repository() *Repository {
r := new(Repository)
r.repo = C.git_object_owner(o.obj)
return r
}