feat(git): remove stash and worktree count from template

they are now individual functions so they no longer need a switch
This commit is contained in:
Jan De Dobbeleer 2022-12-28 10:13:25 +01:00 committed by Jan De Dobbeleer
parent dee040c719
commit 60664e245a
6 changed files with 42 additions and 39 deletions

View file

@ -195,7 +195,7 @@ func TestSegmentTemplateMigration(t *testing.T) {
}{
{
Case: "GIT",
Expected: " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} working {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} and{{ end }}{{ if .Staging.Changed }} staged {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0}} stash {{ .StashCount }}{{ end }}{{ if gt .WorktreeCount 0}} worktree {{ .WorktreeCount }}{{ end }} ", //nolint: lll
Expected: " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} working {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} and{{ end }}{{ if .Staging.Changed }} staged {{ .Staging.String }}{{ end }} ", //nolint: lll
Type: GIT,
Props: properties.Map{
"local_working_icon": " working ",
@ -207,7 +207,7 @@ func TestSegmentTemplateMigration(t *testing.T) {
},
{
Case: "GIT - Staging and Working Color",
Expected: " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} working <#123456>{{ .Working.String }}</>{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} and{{ end }}{{ if .Staging.Changed }} staged <#123456>{{ .Staging.String }}</>{{ end }}{{ if gt .StashCount 0}} stash {{ .StashCount }}{{ end }}{{ if gt .WorktreeCount 0}} worktree {{ .WorktreeCount }}{{ end }} ", //nolint: lll
Expected: " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} working <#123456>{{ .Working.String }}</>{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} and{{ end }}{{ if .Staging.Changed }} staged <#123456>{{ .Staging.String }}</>{{ end }} ", //nolint: lll
Type: GIT,
Props: properties.Map{
"local_working_icon": " working ",

View file

@ -108,15 +108,18 @@ type Git struct {
UpstreamURL string
RawUpstreamURL string
UpstreamGone bool
StashCount int
WorktreeCount int
IsWorkTree bool
RepoName string
IsBare bool
// needed for posh-git support
poshgit bool
stashCount int
worktreeCount int
}
func (g *Git) Template() string {
return " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0}} \uF692 {{ .StashCount }}{{ end }}{{ if gt .WorktreeCount 0}} \uf1bb {{ .WorktreeCount }}{{ end }} " //nolint: lll
return " {{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }} " //nolint: lll
}
func (g *Git) Enabled() bool {
@ -134,10 +137,6 @@ func (g *Git) Enabled() bool {
return true
}
if g.props.GetBool(FetchWorktreeCount, false) {
g.WorktreeCount = g.getWorktreeContext()
}
if g.hasPoshGitStatus() {
return true
}
@ -155,12 +154,22 @@ func (g *Git) Enabled() bool {
if len(g.Upstream) != 0 && g.props.GetBool(FetchUpstreamIcon, false) {
g.UpstreamIcon = g.getUpstreamIcon()
}
if g.props.GetBool(FetchStashCount, false) {
g.StashCount = g.getStashContext()
}
return true
}
func (g *Git) StashCount() int {
if g.poshgit || g.stashCount != 0 {
return g.stashCount
}
stashContent := g.FileContents(g.rootDir, "logs/refs/stash")
if stashContent == "" {
return 0
}
lines := strings.Split(stashContent, "\n")
g.stashCount = len(lines)
return g.stashCount
}
func (g *Git) Kraken() string {
root := g.getGitCommandOutput("rev-list", "--max-parents=0", "HEAD")
if len(g.RawUpstreamURL) == 0 {
@ -577,16 +586,10 @@ func (g *Git) setPrettyHEADName() {
g.HEAD = fmt.Sprintf("%s%s", g.props.GetString(CommitIcon, "\uF417"), g.ShortHash)
}
func (g *Git) getStashContext() int {
stashContent := g.FileContents(g.rootDir, "logs/refs/stash")
if stashContent == "" {
return 0
func (g *Git) WorktreeCount() int {
if g.worktreeCount > 0 {
return g.worktreeCount
}
lines := strings.Split(stashContent, "\n")
return len(lines)
}
func (g *Git) getWorktreeContext() int {
if !g.env.HasFolder(g.rootDir + "/worktrees") {
return 0
}

View file

@ -580,7 +580,7 @@ func TestGetStashContextZeroEntries(t *testing.T) {
workingDir: "",
},
}
got := g.getStashContext()
got := g.StashCount()
assert.Equal(t, tc.Expected, got)
}
}
@ -760,7 +760,8 @@ func TestGitTemplateString(t *testing.T) {
Modified: 1,
},
},
StashCount: 3,
stashCount: 3,
poshgit: true,
},
},
{

View file

@ -58,7 +58,7 @@ func (g *Git) hasPoshGitStatus() bool {
g.Staging = &GitStatus{}
g.Staging.parsePoshGitStatus(posh.Index)
g.HEAD = g.parsePoshGitHEAD(posh.Branch)
g.StashCount = posh.StashCount
g.stashCount = posh.StashCount
g.Ahead = posh.AheadBy
g.Behind = posh.BehindBy
g.UpstreamGone = len(posh.Upstream) == 0
@ -67,6 +67,7 @@ func (g *Git) hasPoshGitStatus() bool {
if len(g.Upstream) != 0 && g.props.GetBool(FetchUpstreamIcon, false) {
g.UpstreamIcon = g.getUpstreamIcon()
}
g.poshgit = true
return true
}

View file

@ -113,7 +113,7 @@ func TestPoshGitSegment(t *testing.T) {
"Branch": "posh-git-json"
}
`,
ExpectedString: "\ue0a0posh-git-json ↑1 ↓1 \uf044 ~2 | \uf046 -2 \uf692 2",
ExpectedString: "\ue0a0posh-git-json ↑1 ↓1 \uf044 ~2 | \uf046 -2",
ExpectedEnabled: true,
},
{
@ -140,7 +140,7 @@ func TestPoshGitSegment(t *testing.T) {
"Branch": "posh-git-json"
}
`,
ExpectedString: "\ue0a0posh-git-json ≢ \uf692 2",
ExpectedString: "\ue0a0posh-git-json ≢",
ExpectedEnabled: true,
},
{
@ -160,7 +160,7 @@ func TestPoshGitSegment(t *testing.T) {
"Branch": "posh-git-json"
}
`,
ExpectedString: "\ue0a0posh-git-json ≢ \uf692 2",
ExpectedString: "\ue0a0posh-git-json ≢",
ExpectedEnabled: true,
},
{

View file

@ -70,8 +70,6 @@ You can set the following properties to `true` to enable fetching additional inf
| Name | Type | Description |
| ---------------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `fetch_status` | `boolean` | fetch the local changes - defaults to `false` |
| `fetch_stash_count` | `boolean` | fetch stash count - defaults to `false` |
| `fetch_worktree_count` | `boolean` | fetch worktree count - defaults to `false` |
| `fetch_upstream_icon` | `boolean` | fetch upstream icon - defaults to `false` |
| `fetch_bare_info` | `boolean` | fetch bare repo info - defaults to `false` |
| `untracked_modes` | `map[string]string` | map of repo's where to override the default [untracked files mode][untracked]:<ul><li>`no`</li><li>`normal`</li><li>`all`</li></ul>For example `"untracked_modes": { "/Users/me/repos/repo1": "no" }` - defaults to `normal` for all repo's. If you want to override for all repo's, use `*` to set the mode instead of the repo path |
@ -119,7 +117,7 @@ You can set the following properties to `true` to enable fetching additional inf
:::note default template
```template
{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if gt .StashCount 0}} \uF692 {{ .StashCount }}{{ end }}{{ if gt .WorktreeCount 0}} \uf1bb {{ .WorktreeCount }}{{ end }}
{{ .HEAD }}{{if .BranchStatus }} {{ .BranchStatus }}{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if and (.Staging.Changed) (.Working.Changed) }} |{{ end }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}
```
:::
@ -150,12 +148,12 @@ You can set the following properties to `true` to enable fetching additional inf
### GitStatus
| Name | Type | Description |
| ----------- | --------- | -------------------------------------------- |
| ------------ | --------- | -------------------------------------------- |
| `.Unmerged` | `int` | number of unmerged changes |
| `.Deleted` | `int` | number of deleted changes |
| `.Added` | `int` | number of added changes |
| `.Modified` | `int` | number of modified changes |
| `.Untracked`| `int` | number of untracked changes |
| `.Untracked` | `int` | number of untracked changes |
| `.Changed` | `boolean` | if the status contains changes or not |
| `.String` | `string` | a string representation of the changes above |