mirror of
https://github.com/grdl/git-get.git
synced 2026-02-10 11:59:18 +00:00
Add a basic branch status parser
This commit is contained in:
58
branch.go
58
branch.go
@@ -1,8 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
git "github.com/libgit2/git2go/v30"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -13,24 +11,58 @@ type BranchStatus struct {
|
||||
HasUpstream bool
|
||||
NeedsPull bool
|
||||
NeedsPush bool
|
||||
Ahead int
|
||||
Behind int
|
||||
}
|
||||
|
||||
func Branches(repo *git.Repository) ([]*git.Branch, error) {
|
||||
it, err := repo.NewBranchIterator(git.BranchAll)
|
||||
func Branches(repo *git.Repository) ([]BranchStatus, error) {
|
||||
iter, err := repo.NewBranchIterator(git.BranchAll)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Failed creating branch iterator")
|
||||
}
|
||||
|
||||
it.ForEach(func(branch *git.Branch, branchType git.BranchType) error {
|
||||
fmt.Print(branch.IsRemote())
|
||||
upstream, err := branch.Upstream()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
} else {
|
||||
fmt.Println(upstream.Name())
|
||||
}
|
||||
var branches []*git.Branch
|
||||
err = iter.ForEach(func(branch *git.Branch, branchType git.BranchType) error {
|
||||
branches = append(branches, branch)
|
||||
return nil
|
||||
})
|
||||
|
||||
return nil, nil
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Failed iterating over branches")
|
||||
}
|
||||
|
||||
var statuses []BranchStatus
|
||||
for _, branch := range branches {
|
||||
status, err := NewBranchStatus(branch)
|
||||
if err != nil {
|
||||
// TODO: handle error
|
||||
continue
|
||||
}
|
||||
statuses = append(statuses, status)
|
||||
}
|
||||
|
||||
return statuses, nil
|
||||
}
|
||||
|
||||
func NewBranchStatus(branch *git.Branch) (BranchStatus, error) {
|
||||
var status BranchStatus
|
||||
|
||||
name, err := branch.Name()
|
||||
if err != nil {
|
||||
return status, errors.Wrap(err, "Failed getting branch name")
|
||||
}
|
||||
status.Name = name
|
||||
|
||||
status.IsRemote = branch.IsRemote()
|
||||
|
||||
_, err = branch.Upstream()
|
||||
if err != nil {
|
||||
if git.IsErrorCode(err, git.ErrNotFound) {
|
||||
status.HasUpstream = false
|
||||
} else {
|
||||
return status, errors.Wrap(err, "Failed getting branch upstream")
|
||||
}
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user