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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user