diff options
| author | Jesse Ezell <[email protected]> | 2014-03-19 00:19:02 -0700 |
|---|---|---|
| committer | Jesse Ezell <[email protected]> | 2014-03-19 00:19:02 -0700 |
| commit | 006286edb75849a38a33cfbe341d8c92de0e119d (patch) | |
| tree | 46446da8b8b185a435ab286129e51fef145fb28b /branch.go | |
| parent | 5f01bd7abdd39b4d8c701c8a73f3c3e49fcd70b9 (diff) | |
remove Branch struct, unify reference iterators
Diffstat (limited to 'branch.go')
| -rw-r--r-- | branch.go | 68 |
1 files changed, 35 insertions, 33 deletions
@@ -19,26 +19,27 @@ const ( BranchRemote = C.GIT_BRANCH_REMOTE ) -type Branch struct { - Reference -} - -type BranchIterator struct { +type branchIterator struct { ptr *C.git_branch_iterator } -func newBranchIteratorFromC(ptr *C.git_branch_iterator) *BranchIterator { - i := &BranchIterator{ptr: ptr} - runtime.SetFinalizer(i, (*BranchIterator).Free) +func newBranchIteratorFromC(ptr *C.git_branch_iterator) ReferenceIterator { + i := &branchIterator{ptr: ptr} + runtime.SetFinalizer(i, (*branchIterator).Free) return i } -func (i *BranchIterator) Next() (*Reference, error) { - ref, _, err := i.NextWithType() - return ref, err +func (i *branchIterator) NextName() (string, error) { + ref, err := i.Next() + if err != nil { + return "", err + } + defer ref.Free() + + return ref.Name(), err } -func (i *BranchIterator) NextWithType() (*Reference, BranchType, error) { +func (i *branchIterator) Next() (*Reference, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -49,20 +50,20 @@ func (i *BranchIterator) NextWithType() (*Reference, BranchType, error) { ecode := C.git_branch_next(&refPtr, &refType, i.ptr) if ecode == C.GIT_ITEROVER { - return nil, BranchLocal, ErrIterOver + return nil, ErrIterOver } else if ecode < 0 { - return nil, BranchLocal, MakeGitError(ecode) + return nil, MakeGitError(ecode) } - return newReferenceFromC(refPtr), BranchType(refType), nil + return newReferenceFromC(refPtr), nil } -func (i *BranchIterator) Free() { +func (i *branchIterator) Free() { runtime.SetFinalizer(i, nil) C.git_branch_iterator_free(i.ptr) } -func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, error) { +func (repo *Repository) NewBranchIterator(flags BranchType) (ReferenceIterator, error) { refType := C.git_branch_t(flags) var ptr *C.git_branch_iterator @@ -105,7 +106,7 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo return ref, nil } -func (b *Branch) Delete() error { +func (b *Reference) DeleteBranch() error { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -116,8 +117,8 @@ func (b *Branch) Delete() error { return nil } -func (b *Branch) Move(newBranchName string, force bool, signature *Signature, msg string) (*Branch, error) { - newBranch := new(Branch) +func (b *Reference) MoveBranch(newBranchName string, force bool, signature *Signature, msg string) (*Reference, error) { + var ptr *C.git_reference cNewBranchName := C.CString(newBranchName) cForce := cbool(force) @@ -135,14 +136,14 @@ func (b *Branch) Move(newBranchName string, force bool, signature *Signature, ms runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_branch_move(&newBranch.ptr, b.ptr, cNewBranchName, cForce, cSignature, cmsg) + ret := C.git_branch_move(&ptr, b.ptr, cNewBranchName, cForce, cSignature, cmsg) if ret < 0 { return nil, MakeGitError(ret) } - return newBranch, nil + return newReferenceFromC(ptr), nil } -func (b *Branch) IsHead() (bool, error) { +func (b *Reference) IsBranchHead() (bool, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -158,21 +159,22 @@ func (b *Branch) IsHead() (bool, error) { } -func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch, error) { - branch := new(Branch) +func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Reference, error) { + var ptr *C.git_reference + cName := C.CString(branchName) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_branch_lookup(&branch.ptr, repo.ptr, cName, C.git_branch_t(bt)) + ret := C.git_branch_lookup(&ptr, repo.ptr, cName, C.git_branch_t(bt)) if ret < 0 { return nil, MakeGitError(ret) } - return branch, nil + return newReferenceFromC(ptr), nil } -func (b *Branch) Name() (string, error) { +func (b *Reference) BranchName() (string, error) { var cName *C.char defer C.free(unsafe.Pointer(cName)) @@ -204,7 +206,7 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) { return C.GoString(nameBuf.ptr), nil } -func (b *Branch) SetUpstream(upstreamName string) error { +func (b *Reference) SetUpstream(upstreamName string) error { cName := C.CString(upstreamName) runtime.LockOSThread() @@ -217,17 +219,17 @@ func (b *Branch) SetUpstream(upstreamName string) error { return nil } -func (b *Branch) Upstream() (*Branch, error) { - upstream := new(Branch) +func (b *Reference) Upstream() (*Reference, error) { + var ptr *C.git_reference runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_branch_upstream(&upstream.ptr, b.ptr) + ret := C.git_branch_upstream(&ptr, b.ptr) if ret < 0 { return nil, MakeGitError(ret) } - return upstream, nil + return newReferenceFromC(ptr), nil } func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error) { |
