refactor: display git stash count

This commit is contained in:
Jan De Dobbeleer 2020-10-10 19:27:04 +02:00 committed by Jan De Dobbeleer
parent 07c985a680
commit 6df97363b8
3 changed files with 16 additions and 38 deletions

View file

@ -50,3 +50,5 @@ Local changes can also shown by default using the following syntax for both the
- cherry_pick_icon: `string` - icon/text to display before the context when doing a cherry-pick
- commit_icon: `string` - icon/text to display before the commit context
- tag_icon: `string` - icon/text to display before the tag context
- display_stash_count: `boolean` show stash count or not
- stash_count_icon: `string` icon/text to display before the stash context

View file

@ -15,7 +15,7 @@ type gitRepo struct {
behind int
HEAD string
upstream string
stashCount int
stashCount string
}
type gitStatus struct {
@ -57,6 +57,10 @@ const (
CommitIcon Property = "commit_icon"
//TagIcon shows before the tag context
TagIcon Property = "tag_icon"
//DisplayStashCount show stash count or not
DisplayStashCount Property = "display_stash_count"
//StashCountIcon shows before the stash context
StashCountIcon Property = "stash_count_icon"
)
func (g *git) enabled() bool {
@ -97,7 +101,9 @@ func (g *git) string() string {
if g.hasWorking() {
fmt.Fprintf(buffer, " %s +%d ~%d -%d", g.props.getString(LocalWorkingIcon, "#"), g.repo.working.added+g.repo.working.untracked, g.repo.working.modified, g.repo.working.deleted)
}
// TODO: Add stash entries
if g.props.getBool(DisplayStashCount, false) && g.repo.stashCount != "" {
fmt.Fprintf(buffer, " %s%s", g.props.getString(StashCountIcon, ""), g.repo.stashCount)
}
return buffer.String()
}
@ -210,9 +216,8 @@ func (g *git) parseGitStats(output []string, working bool) *gitStatus {
return &status
}
func (g *git) getStashContext() int {
stash := g.getGitCommandOutput("stash", "list")
return numberOfLinesInString(stash)
func (g *git) getStashContext() string {
return g.getGitCommandOutput("rev-list", "--walk-reflogs", "--count", "refs/stash")
}
func (g *git) hasStaging() bool {
@ -240,16 +245,3 @@ func groupDict(pattern *regexp.Regexp, haystack string) map[string]string {
}
return result
}
func numberOfLinesInString(s string) int {
n := 0
for _, r := range s {
if r == '\n' {
n++
}
}
if len(s) > 0 && !strings.HasSuffix(s, "\n") {
n++
}
return n
}

View file

@ -1,7 +1,6 @@
package main
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
@ -186,9 +185,9 @@ func TestGetGitHEADContextCherryPickOnTag(t *testing.T) {
}
func TestGetStashContextZeroEntries(t *testing.T) {
want := 0
want := ""
env := new(MockedEnvironment)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "stash", "list"}).Return("")
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-list", "--walk-reflogs", "--count", "refs/stash"}).Return("")
g := &git{
env: env,
}
@ -197,24 +196,9 @@ func TestGetStashContextZeroEntries(t *testing.T) {
}
func TestGetStashContextMultipleEntries(t *testing.T) {
want := rand.Intn(100)
var response string
for i := 0; i < want; i++ {
response += "I'm a stash entry\n"
}
want := "2"
env := new(MockedEnvironment)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "stash", "list"}).Return(response)
g := &git{
env: env,
}
got := g.getStashContext()
assert.Equal(t, want, got)
}
func TestGetStashContextOneEntry(t *testing.T) {
want := 1
env := new(MockedEnvironment)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "stash", "list"}).Return("stash entry")
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-list", "--walk-reflogs", "--count", "refs/stash"}).Return("2")
g := &git{
env: env,
}