Welcome to mirror list, hosted at ThFree Co, Russian Federation.

branch.go « git2go « libgit2 « github.com « vendor - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d6e7a5323c38c604b7cf25d79789e94ee8040e70 (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package git

/*
#include <git2.h>
*/
import "C"

import (
	"runtime"
	"unsafe"
)

type BranchType uint

const (
	BranchAll    BranchType = C.GIT_BRANCH_ALL
	BranchLocal  BranchType = C.GIT_BRANCH_LOCAL
	BranchRemote BranchType = C.GIT_BRANCH_REMOTE
)

type Branch struct {
	*Reference
}

func (r *Reference) Branch() *Branch {
	return &Branch{Reference: r}
}

type BranchIterator struct {
	ptr  *C.git_branch_iterator
	repo *Repository
}

type BranchIteratorFunc func(*Branch, BranchType) error

func newBranchIteratorFromC(repo *Repository, ptr *C.git_branch_iterator) *BranchIterator {
	i := &BranchIterator{repo: repo, ptr: ptr}
	runtime.SetFinalizer(i, (*BranchIterator).Free)
	return i
}

func (i *BranchIterator) Next() (*Branch, BranchType, error) {

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	var refPtr *C.git_reference
	var refType C.git_branch_t

	ecode := C.git_branch_next(&refPtr, &refType, i.ptr)

	if ecode < 0 {
		return nil, BranchLocal, MakeGitError(ecode)
	}

	branch := newReferenceFromC(refPtr, i.repo).Branch()

	return branch, BranchType(refType), nil
}

func (i *BranchIterator) Free() {
	runtime.SetFinalizer(i, nil)
	C.git_branch_iterator_free(i.ptr)
}

func (i *BranchIterator) ForEach(f BranchIteratorFunc) error {
	b, t, err := i.Next()

	for err == nil {
		err = f(b, t)
		if err == nil {
			b, t, err = i.Next()
		}
	}

	if err != nil && IsErrorCode(err, ErrIterOver) {
		return nil
	}

	return err
}

func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, error) {
	refType := C.git_branch_t(flags)
	var ptr *C.git_branch_iterator

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ecode := C.git_branch_iterator_new(&ptr, repo.ptr, refType)
	runtime.KeepAlive(repo)
	if ecode < 0 {
		return nil, MakeGitError(ecode)
	}

	return newBranchIteratorFromC(repo, ptr), nil
}

func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool) (*Branch, error) {

	var ptr *C.git_reference
	cBranchName := C.CString(branchName)
	defer C.free(unsafe.Pointer(cBranchName))
	cForce := cbool(force)

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_branch_create(&ptr, repo.ptr, cBranchName, target.cast_ptr, cForce)
	runtime.KeepAlive(repo)
	runtime.KeepAlive(target)
	if ret < 0 {
		return nil, MakeGitError(ret)
	}
	return newReferenceFromC(ptr, repo).Branch(), nil
}

func (b *Branch) Delete() error {

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	ret := C.git_branch_delete(b.Reference.ptr)
	runtime.KeepAlive(b.Reference)
	if ret < 0 {
		return MakeGitError(ret)
	}
	return nil
}

func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) {
	var ptr *C.git_reference
	cNewBranchName := C.CString(newBranchName)
	defer C.free(unsafe.Pointer(cNewBranchName))
	cForce := cbool(force)

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_branch_move(&ptr, b.Reference.ptr, cNewBranchName, cForce)
	runtime.KeepAlive(b.Reference)
	if ret < 0 {
		return nil, MakeGitError(ret)
	}
	return newReferenceFromC(ptr, b.repo).Branch(), nil
}

func (b *Branch) IsHead() (bool, error) {

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_branch_is_head(b.Reference.ptr)
	runtime.KeepAlive(b.Reference)
	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) {
	var ptr *C.git_reference

	cName := C.CString(branchName)
	defer C.free(unsafe.Pointer(cName))

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_branch_lookup(&ptr, repo.ptr, cName, C.git_branch_t(bt))
	runtime.KeepAlive(repo)
	if ret < 0 {
		return nil, MakeGitError(ret)
	}
	return newReferenceFromC(ptr, repo).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.Reference.ptr)
	runtime.KeepAlive(b.Reference)
	if ret < 0 {
		return "", MakeGitError(ret)
	}

	return C.GoString(cName), nil
}

func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
	cName := C.CString(canonicalBranchName)
	defer C.free(unsafe.Pointer(cName))

	nameBuf := C.git_buf{}

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_branch_remote_name(&nameBuf, repo.ptr, cName)
	runtime.KeepAlive(repo)
	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)
	defer C.free(unsafe.Pointer(cName))

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_branch_set_upstream(b.Reference.ptr, cName)
	runtime.KeepAlive(b.Reference)
	if ret < 0 {
		return MakeGitError(ret)
	}
	return nil
}

func (b *Branch) Upstream() (*Reference, error) {

	var ptr *C.git_reference
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_branch_upstream(&ptr, b.Reference.ptr)
	runtime.KeepAlive(b.Reference)
	if ret < 0 {
		return nil, MakeGitError(ret)
	}
	return newReferenceFromC(ptr, b.repo), nil
}

func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) {
	cName := C.CString(canonicalBranchName)
	defer C.free(unsafe.Pointer(cName))

	nameBuf := C.git_buf{}

	runtime.LockOSThread()
	defer runtime.UnlockOSThread()

	ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName)
	runtime.KeepAlive(repo)
	if ret < 0 {
		return "", MakeGitError(ret)
	}
	defer C.git_buf_free(&nameBuf)

	return C.GoString(nameBuf.ptr), nil
}