Skip to content

Commit

Permalink
refactor(git): count stash using file content
Browse files Browse the repository at this point in the history
relates to #305
  • Loading branch information
JanDeDobbeleer committed Jan 9, 2021
1 parent 780722a commit 82b97bc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
15 changes: 10 additions & 5 deletions src/segment_git.go
Expand Up @@ -14,7 +14,7 @@ type gitRepo struct {
behind int
HEAD string
upstream string
stashCount string
stashCount int
gitFolder string
}

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand Down
37 changes: 19 additions & 18 deletions src/segment_git_test.go
Expand Up @@ -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,
}
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,
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.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) {
Expand Down

0 comments on commit 82b97bc

Please sign in to comment.