diff options
| author | Jesse Ezell <[email protected]> | 2014-03-12 15:49:11 -0700 |
|---|---|---|
| committer | Jesse Ezell <[email protected]> | 2014-03-12 15:49:11 -0700 |
| commit | 5f01bd7abdd39b4d8c701c8a73f3c3e49fcd70b9 (patch) | |
| tree | 3c30d127effd815d910371eb3a1fbac711d9f4aa /branch.go | |
| parent | 1cf81178141c504c62bb3faaa406db665dc5471a (diff) | |
add branch iterator / remove useless repo from reference iterator
Diffstat (limited to 'branch.go')
| -rw-r--r-- | branch.go | 55 |
1 files changed, 55 insertions, 0 deletions
@@ -23,6 +23,61 @@ type Branch struct { Reference } +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) + return i +} + +func (i *BranchIterator) Next() (*Reference, error) { + ref, _, err := i.NextWithType() + return ref, err +} + +func (i *BranchIterator) NextWithType() (*Reference, 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 == C.GIT_ITEROVER { + return nil, BranchLocal, ErrIterOver + } else if ecode < 0 { + return nil, BranchLocal, MakeGitError(ecode) + } + + return newReferenceFromC(refPtr), BranchType(refType), nil +} + +func (i *BranchIterator) Free() { + runtime.SetFinalizer(i, nil) + C.git_branch_iterator_free(i.ptr) +} + +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) + } + + return newBranchIteratorFromC(ptr), nil +} + func (repo *Repository) CreateBranch(branchName string, target *Commit, force bool, signature *Signature, msg string) (*Reference, error) { ref := new(Reference) |
