6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 20:54:41 +00:00

Add checking what is the current branch and if HEAD is detached

This commit is contained in:
Grzegorz Dlugoszewski
2020-06-02 16:46:10 +02:00
parent 1406aa78c6
commit 8a79dcfd9f
4 changed files with 88 additions and 11 deletions

View File

@@ -13,9 +13,18 @@ import (
"github.com/pkg/errors"
)
const (
StatusUnknown = "unknown"
StatusDetached = "detached HEAD"
StatusOk = "ok"
StatusUncommitted = "uncommitted"
StatusUntracked = "untracked"
)
type RepoStatus struct {
HasUntrackedFiles bool
HasUncommittedChanges bool
Current string
Branches []*BranchStatus
}
@@ -54,6 +63,7 @@ func (r *Repo) LoadStatus() error {
r.Status.HasUncommittedChanges = hasUncommitted(status)
r.Status.HasUntrackedFiles = hasUntracked(status)
r.Status.Current = currentBranch(r)
err = r.loadBranchesStatus()
if err != nil {
@@ -91,6 +101,19 @@ func hasUncommitted(status git.Status) bool {
return false
}
func currentBranch(r *Repo) string {
head, err := r.repo.Head()
if err != nil {
return StatusUnknown
}
if head.Name().Short() == plumbing.HEAD.String() {
return StatusDetached
}
return head.Name().Short()
}
func (r *Repo) loadBranchesStatus() error {
iter, err := r.repo.Branches()
if err != nil {