From 82b97bc3b9eed4a65324075c45f45a3e3aa4958f Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 9 Jan 2021 21:06:59 +0100 Subject: [PATCH] refactor(git): count stash using file content relates to #305 --- src/segment_git.go | 15 ++++++++++----- src/segment_git_test.go | 37 +++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/segment_git.go b/src/segment_git.go index 917733905f96..d2663d3491d4 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -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 { diff --git a/src/segment_git_test.go b/src/segment_git_test.go index d26dcf6f8881..2bc6adffa58a 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -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) {