2013-10-07 19:07:06 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo pkg-config: libgit2
|
|
|
|
#include <git2.h>
|
|
|
|
#include <git2/errors.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 = C.GIT_BRANCH_REMOTE
|
2013-10-07 19:07:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-02-26 09:33:50 -06:00
|
|
|
RefsDir = "refs/"
|
|
|
|
RefsHeadsDir = RefsDir + "heads/"
|
|
|
|
RefsTagsDir = RefsDir + "tags/"
|
|
|
|
RefsRemotesDir = RefsDir + "remotes/"
|
2013-10-07 19:07:06 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Branch struct {
|
|
|
|
Reference
|
|
|
|
}
|
|
|
|
|
2014-02-26 09:33:50 -06:00
|
|
|
func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, message string) (*Reference, 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
|
|
|
|
|
|
|
cSignature := signature.toC()
|
|
|
|
defer C.git_signature_free(cSignature)
|
|
|
|
|
|
|
|
cMessage := C.CString(message)
|
|
|
|
defer C.free(unsafe.Pointer(cMessage))
|
|
|
|
|
2014-02-26 10:45:38 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2014-02-26 09:33:50 -06:00
|
|
|
err := C.git_branch_create(&ref.ptr, repo.ptr, cBranchName, target.ptr, cForce, cSignature, cMessage)
|
2013-10-07 19:07:06 -05:00
|
|
|
if err < 0 {
|
|
|
|
return nil, LastError()
|
|
|
|
}
|
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
|
2014-02-26 10:45:38 -06:00
|
|
|
func (b *Branch) Delete() error {
|
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
if err := C.git_branch_delete(b.ptr); err < 0 {
|
2013-10-07 19:07:06 -05:00
|
|
|
return LastError()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-26 09:33:50 -06:00
|
|
|
func (b *Branch) Move(newBranchName string, force bool, signature *Signature, message string) (*Branch, error) {
|
2013-10-07 19:07:06 -05:00
|
|
|
newBranch := new(Branch)
|
|
|
|
cNewBranchName := C.CString(newBranchName)
|
|
|
|
cForce := cbool(force)
|
|
|
|
|
2014-02-26 09:33:50 -06:00
|
|
|
cSignature := signature.toC()
|
|
|
|
defer C.git_signature_free(cSignature)
|
|
|
|
|
|
|
|
cMessage := C.CString(message)
|
|
|
|
defer C.free(unsafe.Pointer(cMessage))
|
|
|
|
|
2014-02-26 10:45:38 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2014-02-26 09:33:50 -06:00
|
|
|
err := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cMessage)
|
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) {
|
2014-02-26 10:45:38 -06:00
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
2013-10-08 07:44:11 -05:00
|
|
|
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:52:22 -05:00
|
|
|
func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, error) {
|
2013-10-07 19:07:06 -05:00
|
|
|
branch := new(Branch)
|
|
|
|
cName := C.CString(branchName)
|
|
|
|
|
2014-02-26 10:45:38 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
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))
|
|
|
|
|
2014-02-26 10:45:38 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
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)
|
|
|
|
|
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 09:33:50 -06:00
|
|
|
if C.git_branch_remote_name(&nameBuf, repo.ptr, cName) < 0 {
|
2013-10-07 19:07:06 -05:00
|
|
|
return "", LastError()
|
|
|
|
}
|
2014-02-26 09:33:50 -06:00
|
|
|
C.git_buf_free(&nameBuf)
|
2013-10-07 19:07:06 -05:00
|
|
|
|
2014-02-26 09:33:50 -06:00
|
|
|
return C.GoStringN(nameBuf.ptr, C.int(nameBuf.size)), nil
|
2013-10-07 19:07:06 -05:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2014-02-26 10:45:38 -06:00
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
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)
|
2014-02-26 10:45:38 -06:00
|
|
|
|
|
|
|
runtime.LockOSThread()
|
|
|
|
defer runtime.UnlockOSThread()
|
|
|
|
|
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)
|
|
|
|
|
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 09:33:50 -06:00
|
|
|
if C.git_branch_upstream_name(&nameBuf, repo.ptr, cName) < 0 {
|
2013-10-07 19:07:06 -05:00
|
|
|
return "", LastError()
|
|
|
|
}
|
2014-02-26 09:33:50 -06:00
|
|
|
C.git_buf_free(&nameBuf)
|
|
|
|
|
|
|
|
return C.GoStringN(nameBuf.ptr, C.int(nameBuf.size)), nil
|
2013-10-07 19:07:06 -05:00
|
|
|
}
|