2013-10-07 19:07:06 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo pkg-config: libgit2
|
|
|
|
#include <git2.h>
|
|
|
|
#include <git2/errors.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrEUser = errors.New("Error in user callback function")
|
|
|
|
|
|
|
|
type ListFlags uint
|
|
|
|
|
2013-10-08 07:39:05 -05:00
|
|
|
type BranchType uint
|
2013-10-07 19:07:06 -05:00
|
|
|
|
|
|
|
const (
|
2013-10-08 07:39:05 -05:00
|
|
|
BRANCH_LOCAL BranchType = C.GIT_BRANCH_LOCAL
|
|
|
|
BRANCH_REMOTE = C.GIT_BRANCH_REMOTE
|
2013-10-07 19:07:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
REFS_DIR = "refs/"
|
|
|
|
REFS_HEADS_DIR = REFS_DIR + "heads/"
|
|
|
|
REFS_TAGS_DIR = REFS_DIR + "tags/"
|
|
|
|
REFS_REMOTES_DIR = REFS_DIR + "remotes/"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Branch struct {
|
|
|
|
Reference
|
|
|
|
}
|
|
|
|
|
2013-10-08 07:49:03 -05:00
|
|
|
func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool) (*Reference, error) {
|
2013-10-07 19:07:06 -05:00
|
|
|
ref := new(Reference)
|
|
|
|
cBranchName := C.CString(branchName)
|
|
|
|
cForce := cbool(force)
|
|
|
|
err := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce)
|
|
|
|
if err < 0 {
|
|
|
|
return nil, LastError()
|
|
|
|
}
|
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
func (b *Branch) BranchDelete() error {
|
|
|
|
if err := C.git_branch_delete(b.ptr); err < 0 {
|
2013-10-07 19:07:06 -05:00
|
|
|
return LastError()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type BranchForeachCB func(name string, flags ListFlags, payload interface{}) error
|
|
|
|
|
|
|
|
func (repo *Repository) BranchForeach(flags ListFlags, callback BranchForeachCB, payload interface{}) error {
|
|
|
|
iter, err := repo.NewReferenceIterator()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
ref, err := iter.Next()
|
|
|
|
if err == ErrIterOver {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags == ListFlags(BRANCH_LOCAL)) && strings.HasPrefix(ref.Name(), REFS_HEADS_DIR) {
|
|
|
|
name := strings.TrimPrefix(ref.Name(), REFS_HEADS_DIR)
|
|
|
|
err = callback(name, ListFlags(BRANCH_LOCAL), payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags == ListFlags(BRANCH_REMOTE)) && strings.HasPrefix(ref.Name(), REFS_REMOTES_DIR) {
|
|
|
|
name := strings.TrimPrefix(ref.Name(), REFS_REMOTES_DIR)
|
|
|
|
err = callback(name, ListFlags(BRANCH_REMOTE), payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == ErrIterOver {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) {
|
2013-10-07 19:07:06 -05:00
|
|
|
newBranch := new(Branch)
|
|
|
|
cNewBranchName := C.CString(newBranchName)
|
|
|
|
cForce := cbool(force)
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
err := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce)
|
2013-10-07 19:07:06 -05:00
|
|
|
if err < 0 {
|
|
|
|
return nil, LastError()
|
|
|
|
}
|
|
|
|
return newBranch, nil
|
|
|
|
}
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
func (b *Branch) IsHead() (bool, error) {
|
|
|
|
isHead := C.git_branch_is_head(b.ptr)
|
2013-10-07 19:07:06 -05:00
|
|
|
switch isHead {
|
|
|
|
case 1:
|
|
|
|
return true, nil
|
|
|
|
case 0:
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return false, LastError()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
func (repo *Repository) BranchLookup(branchName string, bt BranchType) (*Branch, error) {
|
2013-10-07 19:07:06 -05:00
|
|
|
branch := new(Branch)
|
|
|
|
cName := C.CString(branchName)
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
err := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(bt))
|
2013-10-07 19:07:06 -05:00
|
|
|
if err < 0 {
|
|
|
|
return nil, LastError()
|
|
|
|
}
|
|
|
|
return branch, nil
|
|
|
|
}
|
|
|
|
|
2013-10-08 07:44:11 -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))
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
err := C.git_branch_name(&cName, b.ptr)
|
2013-10-07 19:07:06 -05:00
|
|
|
if err < 0 {
|
|
|
|
return "", LastError()
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.GoString(cName), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
|
|
|
|
cName := C.CString(canonicalBranchName)
|
|
|
|
|
|
|
|
// Obtain the length of the name
|
|
|
|
ret := C.git_branch_remote_name(nil, 0, repo.ptr, cName)
|
|
|
|
if ret < 0 {
|
|
|
|
return "", LastError()
|
|
|
|
}
|
|
|
|
|
|
|
|
cBuf := (*C.char)(C.malloc(C.size_t(ret)))
|
|
|
|
defer C.free(unsafe.Pointer(cBuf))
|
|
|
|
|
|
|
|
// Actually obtain the name
|
|
|
|
ret = C.git_branch_remote_name(cBuf, C.size_t(ret), repo.ptr, cName)
|
|
|
|
if ret < 0 {
|
|
|
|
return "", LastError()
|
|
|
|
}
|
|
|
|
|
|
|
|
return C.GoString(cBuf), nil
|
|
|
|
}
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
func (b *Branch) SetUpstream(upstreamName string) error {
|
2013-10-07 19:07:06 -05:00
|
|
|
cName := C.CString(upstreamName)
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
err := C.git_branch_set_upstream(b.ptr, cName)
|
2013-10-07 19:07:06 -05:00
|
|
|
if err < 0 {
|
|
|
|
return LastError()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
func (b *Branch) Upstream() (*Branch, error) {
|
2013-10-07 19:07:06 -05:00
|
|
|
upstream := new(Branch)
|
2013-10-08 07:44:11 -05:00
|
|
|
err := C.git_branch_upstream(&upstream.ptr, b.ptr)
|
2013-10-07 19:07:06 -05:00
|
|
|
if err < 0 {
|
|
|
|
return nil, LastError()
|
|
|
|
}
|
|
|
|
return upstream, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) {
|
|
|
|
cName := C.CString(canonicalBranchName)
|
|
|
|
|
|
|
|
// Obtain the length of the name
|
|
|
|
ret := C.git_branch_upstream_name(nil, 0, repo.ptr, cName)
|
|
|
|
if ret < 0 {
|
|
|
|
return "", LastError()
|
|
|
|
}
|
|
|
|
|
|
|
|
cBuf := (*C.char)(C.malloc(C.size_t(ret)))
|
|
|
|
defer C.free(unsafe.Pointer(cBuf))
|
|
|
|
|
|
|
|
// Actually obtain the name
|
|
|
|
ret = C.git_branch_upstream_name(cBuf, C.size_t(ret), repo.ptr, cName)
|
|
|
|
if ret < 0 {
|
|
|
|
return "", LastError()
|
|
|
|
}
|
|
|
|
return C.GoString(cBuf), nil
|
|
|
|
}
|