forked from libgit2/git2go
/
branch.go
193 lines (148 loc) · 3.83 KB
/
branch.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
package git
/*
#cgo pkg-config: libgit2
#include <git2.h>
#include <git2/errors.h>
*/
import "C"
import (
"runtime"
"unsafe"
)
type BranchType uint
const (
BranchLocal BranchType = C.GIT_BRANCH_LOCAL
BranchRemote = C.GIT_BRANCH_REMOTE
)
type Branch struct {
Reference
}
func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, msg string) (*Reference, error) {
ref := new(Reference)
cBranchName := C.CString(branchName)
cForce := cbool(force)
cSignature := signature.toC()
defer C.git_signature_free(cSignature)
var cmsg *C.char
if msg == "" {
cmsg = nil
} else {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.cast_ptr, cForce, cSignature, cmsg)
if ret < 0 {
return nil, MakeGitError(ret)
}
return ref, nil
}
func (b *Branch) Delete() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_delete(b.ptr)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (b *Branch) Move(newBranchName string, force bool, signature *Signature, msg string) (*Branch, error) {
newBranch := new(Branch)
cNewBranchName := C.CString(newBranchName)
cForce := cbool(force)
cSignature := signature.toC()
defer C.git_signature_free(cSignature)
var cmsg *C.char
if msg == "" {
cmsg = nil
} else {
cmsg = C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cmsg)
if ret < 0 {
return nil, MakeGitError(ret)
}
return newBranch, nil
}
func (b *Branch) IsHead() (bool, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_is_head(b.ptr)
switch ret {
case 1:
return true, nil
case 0:
return false, nil
}
return false, MakeGitError(ret)
}
func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, error) {
branch := new(Branch)
cName := C.CString(branchName)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(bt))
if ret < 0 {
return nil, MakeGitError(ret)
}
return branch, nil
}
func (b *Branch) Name() (string, error) {
var cName *C.char
defer C.free(unsafe.Pointer(cName))
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_name(&cName, b.ptr)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(cName), nil
}
func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
cName := C.CString(canonicalBranchName)
nameBuf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_remote_name(&nameBuf, repo.ptr, cName)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_free(&nameBuf)
return C.GoString(nameBuf.ptr), nil
}
func (b *Branch) SetUpstream(upstreamName string) error {
cName := C.CString(upstreamName)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_set_upstream(b.ptr, cName)
if ret < 0 {
return MakeGitError(ret)
}
return nil
}
func (b *Branch) Upstream() (*Branch, error) {
upstream := new(Branch)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_upstream(&upstream.ptr, b.ptr)
if ret < 0 {
return nil, MakeGitError(ret)
}
return upstream, nil
}
func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) {
cName := C.CString(canonicalBranchName)
nameBuf := C.git_buf{}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName)
if ret < 0 {
return "", MakeGitError(ret)
}
defer C.git_buf_free(&nameBuf)
return C.GoString(nameBuf.ptr), nil
}