forked from libgit2/git2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
revparse.go
113 lines (85 loc) · 2.04 KB
/
revparse.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
package git
/*
#include <git2.h>
extern void _go_git_revspec_free(git_revspec *revspec);
*/
import "C"
import (
"runtime"
"unsafe"
)
type RevparseFlag int
const (
RevparseSingle RevparseFlag = C.GIT_REVPARSE_SINGLE
RevparseRange RevparseFlag = C.GIT_REVPARSE_RANGE
RevparseMergeBase RevparseFlag = C.GIT_REVPARSE_MERGE_BASE
)
type Revspec struct {
to Object
from Object
flags RevparseFlag
}
func (rs *Revspec) To() Object {
return rs.to
}
func (rs *Revspec) From() Object {
return rs.from
}
func (rs *Revspec) Flags() RevparseFlag {
return rs.flags
}
func newRevspecFromC(ptr *C.git_revspec, repo *Repository) *Revspec {
var to Object
var from Object
if ptr.to != nil {
to = allocObject(ptr.to, repo)
}
if ptr.from != nil {
from = allocObject(ptr.from, repo)
}
return &Revspec{
to: to,
from: from,
flags: RevparseFlag(ptr.flags),
}
}
func (r *Repository) Revparse(spec string) (*Revspec, error) {
cspec := C.CString(spec)
defer C.free(unsafe.Pointer(cspec))
var crevspec C.git_revspec
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revparse(&crevspec, r.ptr, cspec)
if ecode != 0 {
return nil, MakeGitError(ecode)
}
return newRevspecFromC(&crevspec, r), nil
}
func (v *Repository) RevparseSingle(spec string) (Object, error) {
cspec := C.CString(spec)
defer C.free(unsafe.Pointer(cspec))
var ptr *C.git_object
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revparse_single(&ptr, v.ptr, cspec)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
return allocObject(ptr, v), nil
}
func (r *Repository) RevparseExt(spec string) (Object, *Reference, error) {
cspec := C.CString(spec)
defer C.free(unsafe.Pointer(cspec))
var obj *C.git_object
var ref *C.git_reference
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ecode := C.git_revparse_ext(&obj, &ref, r.ptr, cspec)
if ecode != 0 {
return nil, nil, MakeGitError(ecode)
}
if ref == nil {
return allocObject(obj, r), nil, nil
}
return allocObject(obj, r), newReferenceFromC(ref, r), nil
}