6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-11 16:58:59 +00:00

Add function mapping libgit's statusList entries to RepoStatus slice

This commit is contained in:
Grzegorz Dlugoszewski
2020-05-18 14:54:21 +02:00
parent acdb877527
commit 0ed1c2147d
2 changed files with 70 additions and 9 deletions

View File

@@ -14,14 +14,41 @@ const (
StatusUnknown StatusUnknown
) )
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 StatusUnknown, errors.Wrap(err, "failed opening repository") return nil, errors.Wrap(err, "failed opening repository")
} }
_, err = statusEntries(repo) entries, err := statusEntries(repo)
return StatusOk, nil if err != nil {
return nil, errors.Wrap(err, "failed opening repository")
}
statusSet := make(map[RepoStatus]bool)
statusSet[StatusOk] = true
for _, entry := range entries {
switch entry.Status {
case git.StatusWtNew:
statusSet[StatusUntrackedFiles] = true
statusSet[StatusOk] = false
case git.StatusIndexNew:
statusSet[StatusUncommittedChanges] = true
statusSet[StatusOk] = false
default:
statusSet[StatusUnknown] = true
statusSet[StatusOk] = false
}
}
var statusSlice []RepoStatus
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

@@ -6,7 +6,7 @@ import (
git "github.com/libgit2/git2go/v30" git "github.com/libgit2/git2go/v30"
) )
func TestStatusEntriesWithEmptyRepo(t *testing.T) { func TestStatusWithEmptyRepo(t *testing.T) {
repo, err := newTestRepo() repo, err := newTestRepo()
checkFatal(t, err) checkFatal(t, err)
defer cleanupRepo(t, repo) defer cleanupRepo(t, repo)
@@ -17,9 +17,16 @@ func TestStatusEntriesWithEmptyRepo(t *testing.T) {
if len(entries) != 0 { if len(entries) != 0 {
t.Errorf("Empty repo should have no status entries") t.Errorf("Empty repo should have no status entries")
} }
statuses, err := GetStatus(repo.Workdir())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk {
t.Errorf("Empty repo should have a single StatusOk status")
}
} }
func TestStatusEntriesWithSingleUnstagedFile(t *testing.T) { func TestStatusWithSingleUnstagedFile(t *testing.T) {
repo, err := newTestRepo() repo, err := newTestRepo()
checkFatal(t, err) checkFatal(t, err)
defer cleanupRepo(t, repo) defer cleanupRepo(t, repo)
@@ -37,9 +44,16 @@ func TestStatusEntriesWithSingleUnstagedFile(t *testing.T) {
if entries[0].Status != git.StatusWtNew { if entries[0].Status != git.StatusWtNew {
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())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusUntrackedFiles {
t.Errorf("Empty repo should have a single StatusUntrackedFiles status")
}
} }
func TestStatusEntriesWithSingleStagedFile(t *testing.T) { func TestStatusWithSingleStagedFile(t *testing.T) {
repo, err := newTestRepo() repo, err := newTestRepo()
checkFatal(t, err) checkFatal(t, err)
defer cleanupRepo(t, repo) defer cleanupRepo(t, repo)
@@ -59,9 +73,16 @@ func TestStatusEntriesWithSingleStagedFile(t *testing.T) {
if entries[0].Status != git.StatusIndexNew { if entries[0].Status != git.StatusIndexNew {
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())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusUncommittedChanges {
t.Errorf("Empty repo should have a single SStatusUncommittedChange status")
}
} }
func TestStatusEntriesWithSingleCommit(t *testing.T) { func TestStatusWithSingleCommit(t *testing.T) {
repo, err := newTestRepo() repo, err := newTestRepo()
checkFatal(t, err) checkFatal(t, err)
defer cleanupRepo(t, repo) defer cleanupRepo(t, repo)
@@ -79,9 +100,16 @@ func TestStatusEntriesWithSingleCommit(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())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk {
t.Errorf("Repo with no uncommitted files should have a single StatusOk status")
}
} }
func TestStatusEntriesWithMultipleCommits(t *testing.T) { func TestStatusWithMultipleCommits(t *testing.T) {
repo, err := newTestRepo() repo, err := newTestRepo()
checkFatal(t, err) checkFatal(t, err)
defer cleanupRepo(t, repo) defer cleanupRepo(t, repo)
@@ -106,4 +134,10 @@ func TestStatusEntriesWithMultipleCommits(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())
checkFatal(t, err)
if len(statuses) != 1 && statuses[0] != StatusOk {
t.Errorf("Repo with no uncommitted files should have a single StatusOk status")
}
} }