Skip to content

Commit

Permalink
refactor: display git stash count
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 10, 2020
1 parent 07c985a commit 6df9736
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
2 changes: 2 additions & 0 deletions docs/docs/segment-git.md
Expand Up @@ -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
28 changes: 10 additions & 18 deletions segment_git.go
Expand Up @@ -15,7 +15,7 @@ type gitRepo struct {
behind int
HEAD string
upstream string
stashCount int
stashCount string
}

type gitStatus struct {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
24 changes: 4 additions & 20 deletions segment_git_test.go
@@ -1,7 +1,6 @@
package main

import (
"math/rand"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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,
}
Expand All @@ -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"
}
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
want := "2"
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,
}
Expand Down

0 comments on commit 6df9736

Please sign in to comment.