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
|
2014-10-27 09:12:18 -05:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2014-03-20 03:27:03 -05:00
|
|
|
type BranchIterator struct {
|
2014-03-19 22:24:19 -05:00
|
|
|
ptr *C.git_branch_iterator
|
|
|
|
repo *Repository
|
|
|
|
}
|
|
|
|
|
2015-02-18 04:02:50 -06:00
|
|
|
type BranchIteratorFunc func(*Branch, BranchType) error
|
2014-03-12 17:49:11 -05:00
|
|
|
|
2014-03-20 03:27:03 -05:00
|
|
|
func newBranchIteratorFromC(repo *Repository, ptr *C.git_branch_iterator) *BranchIterator {
|
|
|
|
i := &BranchIterator{repo: repo, ptr: ptr}
|
|
|
|
runtime.SetFinalizer(i, (*BranchIterator).Free)
|
2014-03-12 17:49:11 -05:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2014-03-20 03:27:03 -05:00
|
|
|
func (i *BranchIterator) Next() (*Branch, BranchType, error) {
|
2014-03-12 17:49:11 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-04-04 02:56:58 -05:00
|
|
|
if ecode < 0 {
|
2014-03-20 03:27:03 -05:00
|
|
|
return nil, BranchLocal, MakeGitError(ecode)
|
2014-03-12 17:49:11 -05:00
|
|
|
}
|
|
|
|
|
2014-05-26 02:28:07 -05:00
|
|
|
branch := newReferenceFromC(refPtr, i.repo).Branch()
|
2014-03-19 22:24:19 -05:00
|
|
|
|
2014-03-20 03:27:03 -05:00
|
|
|
return branch, BranchType(refType), nil
|
2014-03-19 22:24:19 -05:00
|
|
|
}
|
|
|
|
|
2014-03-20 03:27:03 -05:00
|
|
|
func (i *BranchIterator) Free() {
|
2014-03-12 17:49:11 -05:00
|
|
|
runtime.SetFinalizer(i, nil)
|
|
|
|
C.git_branch_iterator_free(i.ptr)
|
|
|
|
}
|
|
|
|
|
2015-02-18 04:02:50 -06:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2014-03-12 17:49:11 -05:00
|
|
|
|
2015-02-18 04:02:50 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, error) {
|
2014-03-12 17:49:11 -05:00
|
|
|
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-12 17:49:11 -05:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:49:32 -05:00
|
|
|
func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool) (*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
|
|
|
|
2014-02-26 10:45:38 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2015-03-14 19:49:32 -05:00
|
|
|
ret := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.cast_ptr, cForce)
|
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
|
|
|
|
}
|
|
|
|
|
2015-03-14 19:49:32 -05:00
|
|
|
func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) {
|
2014-03-19 02:19:02 -05:00
|
|
|
var ptr *C.git_reference
|
2013-10-07 19:07:06 -05:00
|
|
|
cNewBranchName := C.CString(newBranchName)
|
|
|
|
cForce := cbool(force)
|
|
|
|
|
2014-02-26 10:45:38 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2015-03-14 19:49:32 -05:00
|
|
|
ret := C.git_branch_move(&ptr, b.Reference.ptr, cNewBranchName, cForce)
|
2014-02-26 10:50:47 -06:00
|
|
|
if ret < 0 {
|
|
|
|
return nil, MakeGitError(ret)
|
2013-10-07 19:07:06 -05:00
|
|
|
}
|
2014-05-26 02:28:07 -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
|
|
|
|
}
|
2014-02-28 13:08:15 -06:00
|
|
|
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) {
|
2014-03-19 02:19:02 -05:00
|
|
|
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()
|
|
|
|
|
2014-03-19 02:19:02 -05:00
|
|
|
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
|
|
|
}
|
2014-05-26 02:28:07 -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
|
|
|
|
2014-03-19 02:19:02 -05: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
|
|
|
}
|
2014-05-26 02:28:07 -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
|
|
|
}
|