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
HEAD string
upstream string
stashCount string
stashCount int
gitFolder string
}
@ -178,8 +178,8 @@ func (g *git) string() string {
if g.repo.working.changed {
fmt.Fprint(buffer, g.getStatusDetailString(g.repo.working, WorkingColor, LocalWorkingIcon, " \uF044"))
}
if g.repo.stashCount != "" {
fmt.Fprintf(buffer, " %s%s", g.props.getString(StashCountIcon, "\uF692 "), g.repo.stashCount)
if g.repo.stashCount != 0 {
fmt.Fprintf(buffer, " %s%d", g.props.getString(StashCountIcon, "\uF692 "), g.repo.stashCount)
}
return buffer.String()
}
@ -368,8 +368,13 @@ func (g *git) parseGitStats(output []string, working bool) *gitStatus {
return &status
}
func (g *git) getStashContext() string {
return g.getGitCommandOutput("rev-list", "--walk-reflogs", "--count", "refs/stash")
func (g *git) getStashContext() int {
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 {

View file

@ -230,25 +230,26 @@ func TestGetGitHEADContextMergeTag(t *testing.T) {
}
func TestGetStashContextZeroEntries(t *testing.T) {
want := ""
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)
g := &git{
env: env,
cases := []struct {
Expected int
StashContent string
}{
{Expected: 0, StashContent: ""},
{Expected: 2, StashContent: "1\n2\n"},
{Expected: 4, StashContent: "1\n2\n3\n4\n\n"},
}
got := g.getStashContext()
assert.Equal(t, want, 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,
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("getFileContent", "/logs/refs/stash").Return(tc.StashContent)
g := &git{
repo: &gitRepo{
gitFolder: "",
},
env: env,
}
got := g.getStashContext()
assert.Equal(t, tc.Expected, got)
}
got := g.getStashContext()
assert.Equal(t, want, got)
}
func TestParseGitBranchInfoEqual(t *testing.T) {