6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-04 15:39:46 +00:00

Change repo status to struct instead of map

This commit is contained in:
Grzegorz Dlugoszewski
2020-05-18 23:13:34 +02:00
parent 9757a96d34
commit ad7c3a4afc
2 changed files with 47 additions and 42 deletions

View File

@@ -5,50 +5,35 @@ import (
"github.com/pkg/errors"
)
type RepoStatus int
type RepoStatus struct {
HasUntrackedFiles bool
HasUncommittedChanges bool
BranchStatuses []BranchStatus
}
const (
StatusOk RepoStatus = iota
StatusUntrackedFiles
StatusUncommittedChanges
StatusUnknown
)
func Status(path string) (RepoStatus, error) {
var status RepoStatus
func GetStatus(path string) ([]RepoStatus, error) {
repo, err := git.OpenRepository(path)
if err != nil {
return nil, errors.Wrap(err, "Failed opening repository")
return status, errors.Wrap(err, "Failed opening repository")
}
entries, err := statusEntries(repo)
if err != nil {
return nil, errors.Wrap(err, "Failed opening repository")
return status, errors.Wrap(err, "Failed getting repository status")
}
statusSet := make(map[RepoStatus]bool)
statusSet[StatusOk] = true
for _, entry := range entries {
switch entry.Status {
case git.StatusWtNew:
statusSet[StatusUntrackedFiles] = true
statusSet[StatusOk] = false
status.HasUntrackedFiles = true
case git.StatusIndexNew:
statusSet[StatusUncommittedChanges] = true
statusSet[StatusOk] = false
default:
statusSet[StatusUnknown] = true
statusSet[StatusOk] = false
status.HasUncommittedChanges = true
}
}
var statusSlice []RepoStatus
for k, v := range statusSet {
if v {
statusSlice = append(statusSlice, k)
}
}
return statusSlice, nil
return status, nil
}
func statusEntries(repo *git.Repository) ([]git.StatusEntry, error) {

View File

@@ -16,11 +16,15 @@ func TestStatusWithEmptyRepo(t *testing.T) {
t.Errorf("Empty repo should have no status entries")
}
statuses, err := GetStatus(repo.Workdir())
status, err := Status(repo.Workdir())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk {
t.Errorf("Empty repo should have a single StatusOk status")
if status.HasUntrackedFiles != false {
t.Errorf("Repo should not have untracked files")
}
if status.HasUncommittedChanges != false {
t.Errorf("Repo should not have uncommitted changes")
}
}
@@ -39,11 +43,15 @@ func TestStatusWithUntrackedFile(t *testing.T) {
t.Errorf("Invalid status, got %d; want %d", entries[0].Status, git.StatusWtNew)
}
statuses, err := GetStatus(repo.Workdir())
status, err := Status(repo.Workdir())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusUntrackedFiles {
t.Errorf("Repo with untracked file should have a single StatusUntrackedFiles status")
if status.HasUntrackedFiles != true {
t.Errorf("Repo should have untracked files")
}
if status.HasUncommittedChanges != false {
t.Errorf("Repo should not have uncommitted changes")
}
}
@@ -71,11 +79,15 @@ func TestStatusWithStagedFile(t *testing.T) {
t.Errorf("Invalid status, got %d; want %d", entries[0].Status, git.StatusIndexNew)
}
statuses, err := GetStatus(repo.Workdir())
status, err := Status(repo.Workdir())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusUncommittedChanges {
t.Errorf("Repo with staged file should have a single StatusUncommittedChange status")
if status.HasUntrackedFiles != false {
t.Errorf("Repo should not have untracked files")
}
if status.HasUncommittedChanges != true {
t.Errorf("Repo should have uncommitted changes")
}
}
@@ -92,11 +104,15 @@ func TestStatusWithSingleCommit(t *testing.T) {
t.Errorf("Repo with no uncommitted files should have no status entries")
}
statuses, err := GetStatus(repo.Workdir())
status, err := Status(repo.Workdir())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk {
t.Errorf("Repo with no uncommitted files should have a single StatusOk status")
if status.HasUntrackedFiles != false {
t.Errorf("Repo should not have untracked files")
}
if status.HasUncommittedChanges != false {
t.Errorf("Repo should not have uncommitted changes")
}
}
@@ -115,10 +131,14 @@ func TestStatusWithMultipleCommits(t *testing.T) {
if len(entries) != 0 {
t.Errorf("Repo with no uncommitted files should have no status entries")
}
statuses, err := GetStatus(repo.Workdir())
status, err := Status(repo.Workdir())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk {
t.Errorf("Repo with no uncommitted files should have a single StatusOk status")
if status.HasUntrackedFiles != false {
t.Errorf("Repo should not have untracked files")
}
if status.HasUncommittedChanges != false {
t.Errorf("Repo should not have uncommitted changes")
}
}