6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-11 11:49:17 +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" "github.com/pkg/errors"
) )
type RepoStatus int type RepoStatus struct {
HasUntrackedFiles bool
HasUncommittedChanges bool
BranchStatuses []BranchStatus
}
const ( func Status(path string) (RepoStatus, error) {
StatusOk RepoStatus = iota var status RepoStatus
StatusUntrackedFiles
StatusUncommittedChanges
StatusUnknown
)
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 status, 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 status, errors.Wrap(err, "Failed getting repository status")
} }
statusSet := make(map[RepoStatus]bool)
statusSet[StatusOk] = true
for _, entry := range entries { for _, entry := range entries {
switch entry.Status { switch entry.Status {
case git.StatusWtNew: case git.StatusWtNew:
statusSet[StatusUntrackedFiles] = true status.HasUntrackedFiles = true
statusSet[StatusOk] = false
case git.StatusIndexNew: case git.StatusIndexNew:
statusSet[StatusUncommittedChanges] = true status.HasUncommittedChanges = true
statusSet[StatusOk] = false
default:
statusSet[StatusUnknown] = true
statusSet[StatusOk] = false
} }
} }
var statusSlice []RepoStatus return status, nil
for k, v := range statusSet {
if v {
statusSlice = append(statusSlice, k)
}
}
return statusSlice, nil
} }
func statusEntries(repo *git.Repository) ([]git.StatusEntry, error) { 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") t.Errorf("Empty repo should have no status entries")
} }
statuses, err := GetStatus(repo.Workdir()) status, err := Status(repo.Workdir())
checkFatal(t, err) checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk { if status.HasUntrackedFiles != false {
t.Errorf("Empty repo should have a single StatusOk status") 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) 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) checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusUntrackedFiles { if status.HasUntrackedFiles != true {
t.Errorf("Repo with untracked file should have a single StatusUntrackedFiles status") 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) 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) checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusUncommittedChanges { if status.HasUntrackedFiles != false {
t.Errorf("Repo with staged file should have a single StatusUncommittedChange status") 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") 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) checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk { if status.HasUntrackedFiles != false {
t.Errorf("Repo with no uncommitted files should have a single StatusOk status") 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 { if len(entries) != 0 {
t.Errorf("Repo with no uncommitted files should have no status entries") 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) checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk { if status.HasUntrackedFiles != false {
t.Errorf("Repo with no uncommitted files should have a single StatusOk status") t.Errorf("Repo should not have untracked files")
}
if status.HasUncommittedChanges != false {
t.Errorf("Repo should not have uncommitted changes")
} }
} }