refactor(git): count stash using file content

relates to #305
This commit is contained in:
Jan De Dobbeleer 2021-01-09 21:06:59 +01:00 committed by Jan De Dobbeleer
parent 780722a371
commit 82b97bc3b9
2 changed files with 28 additions and 22 deletions

View file

@ -14,7 +14,7 @@ type gitRepo struct {
behind int behind int
HEAD string HEAD string
upstream string upstream string
stashCount string stashCount int
gitFolder string gitFolder string
} }
@ -178,8 +178,8 @@ func (g *git) string() string {
if g.repo.working.changed { if g.repo.working.changed {
fmt.Fprint(buffer, g.getStatusDetailString(g.repo.working, WorkingColor, LocalWorkingIcon, " \uF044")) fmt.Fprint(buffer, g.getStatusDetailString(g.repo.working, WorkingColor, LocalWorkingIcon, " \uF044"))
} }
if g.repo.stashCount != "" { if g.repo.stashCount != 0 {
fmt.Fprintf(buffer, " %s%s", g.props.getString(StashCountIcon, "\uF692 "), g.repo.stashCount) fmt.Fprintf(buffer, " %s%d", g.props.getString(StashCountIcon, "\uF692 "), g.repo.stashCount)
} }
return buffer.String() return buffer.String()
} }
@ -368,8 +368,13 @@ func (g *git) parseGitStats(output []string, working bool) *gitStatus {
return &status return &status
} }
func (g *git) getStashContext() string { func (g *git) getStashContext() int {
return g.getGitCommandOutput("rev-list", "--walk-reflogs", "--count", "refs/stash") stashContent := g.getGitFileContents("logs/refs/stash")
if stashContent == "" {
return 0
}
lines := strings.Split(stashContent, "\n")
return len(lines)
} }
func (g *git) parseGitStatusInfo(branchInfo string) map[string]string { func (g *git) parseGitStatusInfo(branchInfo string) map[string]string {

View file

@ -230,25 +230,26 @@ func TestGetGitHEADContextMergeTag(t *testing.T) {
} }
func TestGetStashContextZeroEntries(t *testing.T) { func TestGetStashContextZeroEntries(t *testing.T) {
want := "" cases := []struct {
Expected int
StashContent string
}{
{Expected: 0, StashContent: ""},
{Expected: 2, StashContent: "1\n2\n"},
{Expected: 4, StashContent: "1\n2\n3\n4\n\n"},
}
for _, tc := range cases {
env := new(MockedEnvironment) env := new(MockedEnvironment)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-list", "--walk-reflogs", "--count", "refs/stash"}).Return("", nil) env.On("getFileContent", "/logs/refs/stash").Return(tc.StashContent)
g := &git{ g := &git{
repo: &gitRepo{
gitFolder: "",
},
env: env, env: env,
} }
got := g.getStashContext() got := g.getStashContext()
assert.Equal(t, want, got) assert.Equal(t, tc.Expected, got)
}
func TestGetStashContextMultipleEntries(t *testing.T) {
want := "2"
env := new(MockedEnvironment)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-list", "--walk-reflogs", "--count", "refs/stash"}).Return("2", nil)
g := &git{
env: env,
} }
got := g.getStashContext()
assert.Equal(t, want, got)
} }
func TestParseGitBranchInfoEqual(t *testing.T) { func TestParseGitBranchInfoEqual(t *testing.T) {