git2go/branch.go

268 lines
5.4 KiB
Go
Raw Normal View History

2013-10-07 19:07:06 -05:00
package git
/*
#include <git2.h>
*/
import "C"
import (
2014-02-26 10:45:38 -06:00
"runtime"
2013-10-07 19:07:06 -05:00
"unsafe"
)
2013-10-08 07:39:05 -05:00
type BranchType uint
2013-10-07 19:07:06 -05:00
const (
2014-02-26 09:33:50 -06:00
BranchLocal BranchType = C.GIT_BRANCH_LOCAL
BranchRemote BranchType = C.GIT_BRANCH_REMOTE
2013-10-07 19:07:06 -05:00
)
2014-03-19 22:24:19 -05:00
type Branch struct {
*Reference
}
func (r *Reference) Branch() *Branch {
return &Branch{Reference: r}
}
type BranchIterator struct {
2014-03-19 22:24:19 -05:00
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()
2014-03-19 22:24:19 -05:00
return branch, BranchType(refType), nil
2014-03-19 22:24:19 -05:00
}
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()
}
}
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)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
2014-03-19 22:24:19 -05:00
return newBranchIteratorFromC(repo, ptr), nil
}
2014-03-19 22:24:19 -05:00
func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, msg string) (*Branch, error) {
2014-02-26 10:45:38 -06:00
2013-10-07 19:07:06 -05:00
ref := new(Reference)
cBranchName := C.CString(branchName)
cForce := cbool(force)
2014-02-26 09:33:50 -06:00
2015-03-04 13:39:35 -06:00
cSignature, err := signature.toC()
if err != nil {
return nil, err
}
2014-02-26 09:33:50 -06:00
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))
}
2014-02-26 09:33:50 -06:00
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.cast_ptr, cForce, cSignature, cmsg)
2014-02-26 10:50:47 -06:00
if ret < 0 {
return nil, MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
2014-03-19 22:24:19 -05:00
return ref.Branch(), nil
2013-10-07 19:07:06 -05:00
}
2014-03-19 22:24:19 -05:00
func (b *Branch) Delete() error {
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-03-19 22:24:19 -05:00
ret := C.git_branch_delete(b.Reference.ptr)
2014-02-26 10:50:47 -06:00
if ret < 0 {
return MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
return nil
}
func (b *Branch) Move(newBranchName string, force bool, signature *Signature, msg string) (*Branch, error) {
var ptr *C.git_reference
2013-10-07 19:07:06 -05:00
cNewBranchName := C.CString(newBranchName)
cForce := cbool(force)
2015-03-04 13:39:35 -06:00
cSignature, err := signature.toC()
if err != nil {
return nil, err
}
2014-02-26 09:33:50 -06:00
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))
}
2014-02-26 09:33:50 -06:00
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-03-19 22:24:19 -05:00
ret := C.git_branch_move(&ptr, b.Reference.ptr, cNewBranchName, cForce, cSignature, cmsg)
2014-02-26 10:50:47 -06:00
if ret < 0 {
return nil, MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
return newReferenceFromC(ptr, b.repo).Branch(), nil
2013-10-07 19:07:06 -05:00
}
2014-03-19 22:24:19 -05:00
func (b *Branch) IsHead() (bool, error) {
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-03-19 22:24:19 -05:00
ret := C.git_branch_is_head(b.Reference.ptr)
2014-02-26 10:50:47 -06:00
switch ret {
2013-10-07 19:07:06 -05:00
case 1:
return true, nil
case 0:
return false, nil
}
return false, MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
2014-03-19 22:24:19 -05:00
func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, error) {
var ptr *C.git_reference
2013-10-07 19:07:06 -05:00
cName := C.CString(branchName)
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_branch_lookup(&ptr, repo.ptr, cName, C.git_branch_t(bt))
2014-02-26 10:50:47 -06:00
if ret < 0 {
return nil, MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
return newReferenceFromC(ptr, repo).Branch(), nil
2013-10-07 19:07:06 -05:00
}
2014-03-19 22:24:19 -05:00
func (b *Branch) Name() (string, error) {
2013-10-07 19:07:06 -05:00
var cName *C.char
defer C.free(unsafe.Pointer(cName))
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-03-19 22:24:19 -05:00
ret := C.git_branch_name(&cName, b.Reference.ptr)
2014-02-26 10:50:47 -06:00
if ret < 0 {
return "", MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
return C.GoString(cName), nil
}
func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
cName := C.CString(canonicalBranchName)
2014-02-26 09:33:50 -06:00
nameBuf := C.git_buf{}
2013-10-07 19:07:06 -05:00
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-02-26 10:50:47 -06:00
ret := C.git_branch_remote_name(&nameBuf, repo.ptr, cName)
if ret < 0 {
return "", MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
2014-02-28 12:46:57 -06:00
defer C.git_buf_free(&nameBuf)
2013-10-07 19:07:06 -05:00
2014-02-28 12:46:57 -06:00
return C.GoString(nameBuf.ptr), nil
2013-10-07 19:07:06 -05:00
}
2014-03-19 22:24:19 -05:00
func (b *Branch) SetUpstream(upstreamName string) error {
2013-10-07 19:07:06 -05:00
cName := C.CString(upstreamName)
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-03-19 22:24:19 -05:00
ret := C.git_branch_set_upstream(b.Reference.ptr, cName)
2014-02-26 10:50:47 -06:00
if ret < 0 {
return MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
return nil
}
2014-03-19 22:24:19 -05:00
func (b *Branch) Upstream() (*Reference, error) {
2014-02-26 10:45:38 -06:00
var ptr *C.git_reference
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-03-19 22:24:19 -05:00
ret := C.git_branch_upstream(&ptr, b.Reference.ptr)
2014-02-26 10:50:47 -06:00
if ret < 0 {
return nil, MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
return newReferenceFromC(ptr, b.repo), nil
2013-10-07 19:07:06 -05:00
}
func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) {
cName := C.CString(canonicalBranchName)
2014-02-26 09:33:50 -06:00
nameBuf := C.git_buf{}
2013-10-07 19:07:06 -05:00
2014-02-26 10:45:38 -06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2014-02-26 10:50:47 -06:00
ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName)
if ret < 0 {
return "", MakeGitError(ret)
2013-10-07 19:07:06 -05:00
}
2014-02-28 12:46:57 -06:00
defer C.git_buf_free(&nameBuf)
2014-02-26 09:33:50 -06:00
2014-02-28 12:46:57 -06:00
return C.GoString(nameBuf.ptr), nil
2013-10-07 19:07:06 -05:00
}