mirror of
https://github.com/grdl/git-get.git
synced 2026-02-11 08:19:16 +00:00
Add a basic branch iterator for getting branches state
This commit is contained in:
36
branch.go
Normal file
36
branch.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
git "github.com/libgit2/git2go/v30"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BranchStatus struct {
|
||||||
|
Name string
|
||||||
|
IsRemote bool
|
||||||
|
HasUpstream bool
|
||||||
|
NeedsPull bool
|
||||||
|
NeedsPush bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func Branches(repo *git.Repository) ([]*git.Branch, error) {
|
||||||
|
it, 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())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
18
branch_test.go
Normal file
18
branch_test.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
git "github.com/libgit2/git2go/v30"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBranches(t *testing.T) {
|
||||||
|
repo, err := git.OpenRepository("/home/grdl/workspace/gitlab.com/grdl/git-get")
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
branches, err := Branches(repo)
|
||||||
|
checkFatal(t, err)
|
||||||
|
|
||||||
|
fmt.Println(len(branches))
|
||||||
|
}
|
||||||
10
status.go
10
status.go
@@ -17,12 +17,12 @@ const (
|
|||||||
func GetStatus(path string) ([]RepoStatus, error) {
|
func GetStatus(path string) ([]RepoStatus, error) {
|
||||||
repo, err := git.OpenRepository(path)
|
repo, err := git.OpenRepository(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed opening repository")
|
return nil, errors.Wrap(err, "Failed opening repository")
|
||||||
}
|
}
|
||||||
|
|
||||||
entries, err := statusEntries(repo)
|
entries, err := statusEntries(repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed opening repository")
|
return nil, errors.Wrap(err, "Failed opening repository")
|
||||||
}
|
}
|
||||||
|
|
||||||
statusSet := make(map[RepoStatus]bool)
|
statusSet := make(map[RepoStatus]bool)
|
||||||
@@ -59,19 +59,19 @@ func statusEntries(repo *git.Repository) ([]git.StatusEntry, error) {
|
|||||||
|
|
||||||
status, err := repo.StatusList(opts)
|
status, err := repo.StatusList(opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed getting repository status")
|
return nil, errors.Wrap(err, "Failed getting repository status")
|
||||||
}
|
}
|
||||||
|
|
||||||
entryCount, err := status.EntryCount()
|
entryCount, err := status.EntryCount()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed getting repository status count")
|
return nil, errors.Wrap(err, "Failed getting repository status count")
|
||||||
}
|
}
|
||||||
|
|
||||||
var entries []git.StatusEntry
|
var entries []git.StatusEntry
|
||||||
for i := 0; i < entryCount; i++ {
|
for i := 0; i < entryCount; i++ {
|
||||||
entry, err := status.ByIndex(i)
|
entry, err := status.ByIndex(i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed getting repository status entry")
|
return nil, errors.Wrap(err, "Failed getting repository status entry")
|
||||||
}
|
}
|
||||||
|
|
||||||
entries = append(entries, entry)
|
entries = append(entries, entry)
|
||||||
|
|||||||
Reference in New Issue
Block a user