- Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathrefspec.go
149 lines (116 loc) · 3.13 KB
/
refspec.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
package git
/*
#include <git2.h>
*/
import"C"
import (
"runtime"
"unsafe"
)
typeRefspecstruct {
doNotCompare
ptr*C.git_refspec
}
// ParseRefspec parses a given refspec string
funcParseRefspec(inputstring, isFetchbool) (*Refspec, error) {
varptr*C.git_refspec
cinput:=C.CString(input)
deferC.free(unsafe.Pointer(cinput))
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_refspec_parse(&ptr, cinput, cbool(isFetch))
ifret<0 {
returnnil, MakeGitError(ret)
}
spec:=&Refspec{ptr: ptr}
runtime.SetFinalizer(spec, (*Refspec).Free)
returnspec, nil
}
// Free releases a refspec object which has been created by ParseRefspec
func (s*Refspec) Free() {
runtime.SetFinalizer(s, nil)
C.git_refspec_free(s.ptr)
}
// Direction returns the refspec's direction
func (s*Refspec) Direction() ConnectDirection {
direction:=C.git_refspec_direction(s.ptr)
returnConnectDirection(direction)
}
// Src returns the refspec's source specifier
func (s*Refspec) Src() string {
varretstring
cstr:=C.git_refspec_src(s.ptr)
ifcstr!=nil {
ret=C.GoString(cstr)
}
runtime.KeepAlive(s)
returnret
}
// Dst returns the refspec's destination specifier
func (s*Refspec) Dst() string {
varretstring
cstr:=C.git_refspec_dst(s.ptr)
ifcstr!=nil {
ret=C.GoString(cstr)
}
runtime.KeepAlive(s)
returnret
}
// Force returns the refspec's force-update setting
func (s*Refspec) Force() bool {
force:=C.git_refspec_force(s.ptr)
returnforce!=0
}
// String returns the refspec's string representation
func (s*Refspec) String() string {
varretstring
cstr:=C.git_refspec_string(s.ptr)
ifcstr!=nil {
ret=C.GoString(cstr)
}
runtime.KeepAlive(s)
returnret
}
// SrcMatches checks if a refspec's source descriptor matches a reference
func (s*Refspec) SrcMatches(refnamestring) bool {
cname:=C.CString(refname)
deferC.free(unsafe.Pointer(cname))
matches:=C.git_refspec_src_matches(s.ptr, cname)
returnmatches!=0
}
// SrcMatches checks if a refspec's destination descriptor matches a reference
func (s*Refspec) DstMatches(refnamestring) bool {
cname:=C.CString(refname)
deferC.free(unsafe.Pointer(cname))
matches:=C.git_refspec_dst_matches(s.ptr, cname)
returnmatches!=0
}
// Transform a reference to its target following the refspec's rules
func (s*Refspec) Transform(refnamestring) (string, error) {
buf:= C.git_buf{}
cname:=C.CString(refname)
deferC.free(unsafe.Pointer(cname))
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_refspec_transform(&buf, s.ptr, cname)
ifret<0 {
return"", MakeGitError(ret)
}
deferC.git_buf_dispose(&buf)
returnC.GoString(buf.ptr), nil
}
// Rtransform converts a target reference to its source reference following the
// refspec's rules
func (s*Refspec) Rtransform(refnamestring) (string, error) {
buf:= C.git_buf{}
cname:=C.CString(refname)
deferC.free(unsafe.Pointer(cname))
runtime.LockOSThread()
deferruntime.UnlockOSThread()
ret:=C.git_refspec_rtransform(&buf, s.ptr, cname)
ifret<0 {
return"", MakeGitError(ret)
}
deferC.git_buf_dispose(&buf)
returnC.GoString(buf.ptr), nil
}