mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-30 12:32:22 -08:00
refactor(git): add branch status to template
This commit is contained in:
parent
4dbed1176c
commit
3f8400e8f1
|
@ -10,16 +10,17 @@ import (
|
||||||
|
|
||||||
// Repo represents a git repository
|
// Repo represents a git repository
|
||||||
type Repo struct {
|
type Repo struct {
|
||||||
Working *GitStatus
|
Working *GitStatus
|
||||||
Staging *GitStatus
|
Staging *GitStatus
|
||||||
Ahead int
|
Ahead int
|
||||||
Behind int
|
Behind int
|
||||||
HEAD string
|
HEAD string
|
||||||
Upstream string
|
BranchStatus string
|
||||||
UpstreamIcon string
|
Upstream string
|
||||||
StashCount int
|
UpstreamIcon string
|
||||||
WorktreeCount int
|
StashCount int
|
||||||
IsWorkTree bool
|
WorktreeCount int
|
||||||
|
IsWorkTree bool
|
||||||
|
|
||||||
gitWorkingFolder string // .git working folder, can be different of root if using worktree
|
gitWorkingFolder string // .git working folder, can be different of root if using worktree
|
||||||
gitRootFolder string // .git root folder
|
gitRootFolder string // .git root folder
|
||||||
|
@ -191,7 +192,9 @@ func (g *git) shouldIgnoreRootRepository(rootDir string) bool {
|
||||||
func (g *git) string() string {
|
func (g *git) string() string {
|
||||||
statusColorsEnabled := g.props.getBool(StatusColorsEnabled, false)
|
statusColorsEnabled := g.props.getBool(StatusColorsEnabled, false)
|
||||||
displayStatus := g.props.getBool(DisplayStatus, false)
|
displayStatus := g.props.getBool(DisplayStatus, false)
|
||||||
|
if !displayStatus {
|
||||||
|
g.repo.HEAD = g.getPrettyHEADName()
|
||||||
|
}
|
||||||
if displayStatus || statusColorsEnabled {
|
if displayStatus || statusColorsEnabled {
|
||||||
g.setGitStatus()
|
g.setGitStatus()
|
||||||
}
|
}
|
||||||
|
@ -201,23 +204,30 @@ func (g *git) string() string {
|
||||||
if g.repo.Upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) {
|
if g.repo.Upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) {
|
||||||
g.repo.UpstreamIcon = g.getUpstreamIcon()
|
g.repo.UpstreamIcon = g.getUpstreamIcon()
|
||||||
}
|
}
|
||||||
|
if g.props.getBool(DisplayBranchStatus, true) {
|
||||||
|
g.repo.BranchStatus = g.getBranchStatus()
|
||||||
|
}
|
||||||
// use template if available
|
// use template if available
|
||||||
segmentTemplate := g.props.getString(SegmentTemplate, "")
|
segmentTemplate := g.props.getString(SegmentTemplate, "")
|
||||||
if len(segmentTemplate) > 0 {
|
if len(segmentTemplate) > 0 {
|
||||||
template := &textTemplate{
|
return g.templateString(segmentTemplate)
|
||||||
Template: segmentTemplate,
|
|
||||||
Context: g.repo,
|
|
||||||
Env: g.env,
|
|
||||||
}
|
|
||||||
text, err := template.render()
|
|
||||||
if err != nil {
|
|
||||||
return err.Error()
|
|
||||||
}
|
|
||||||
return text
|
|
||||||
}
|
}
|
||||||
// legacy render string if no template
|
// legacy render string if no template
|
||||||
// remove this for 6.0
|
// remove this for 6.0
|
||||||
return g.renderDeprecatedString(displayStatus)
|
return g.renderDeprecatedString()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *git) templateString(segmentTemplate string) string {
|
||||||
|
template := &textTemplate{
|
||||||
|
Template: segmentTemplate,
|
||||||
|
Context: g.repo,
|
||||||
|
Env: g.env,
|
||||||
|
}
|
||||||
|
text, err := template.render()
|
||||||
|
if err != nil {
|
||||||
|
return err.Error()
|
||||||
|
}
|
||||||
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *git) init(props *properties, env environmentInfo) {
|
func (g *git) init(props *properties, env environmentInfo) {
|
||||||
|
|
|
@ -32,10 +32,7 @@ const (
|
||||||
WorktreeCountIcon Property = "worktree_count_icon"
|
WorktreeCountIcon Property = "worktree_count_icon"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (g *git) renderDeprecatedString(displayStatus bool) string {
|
func (g *git) renderDeprecatedString() string {
|
||||||
if !displayStatus {
|
|
||||||
return g.getPrettyHEADName()
|
|
||||||
}
|
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
// remote (if available)
|
// remote (if available)
|
||||||
if len(g.repo.UpstreamIcon) != 0 {
|
if len(g.repo.UpstreamIcon) != 0 {
|
||||||
|
@ -43,8 +40,8 @@ func (g *git) renderDeprecatedString(displayStatus bool) string {
|
||||||
}
|
}
|
||||||
// branchName
|
// branchName
|
||||||
fmt.Fprintf(buffer, "%s", g.repo.HEAD)
|
fmt.Fprintf(buffer, "%s", g.repo.HEAD)
|
||||||
if g.props.getBool(DisplayBranchStatus, true) {
|
if len(g.repo.BranchStatus) != 0 {
|
||||||
buffer.WriteString(g.getBranchStatus())
|
buffer.WriteString(g.repo.BranchStatus)
|
||||||
}
|
}
|
||||||
if g.repo.Staging.Changed {
|
if g.repo.Staging.Changed {
|
||||||
fmt.Fprint(buffer, g.getStatusDetailString(g.repo.Staging, StagingColor, LocalStagingIcon, " \uF046"))
|
fmt.Fprint(buffer, g.getStatusDetailString(g.repo.Staging, StagingColor, LocalStagingIcon, " \uF046"))
|
||||||
|
|
|
@ -131,8 +131,8 @@ func setupHEADContextEnv(context *detachedContext) *git {
|
||||||
env: env,
|
env: env,
|
||||||
repo: &Repo{
|
repo: &Repo{
|
||||||
gitWorkingFolder: "",
|
gitWorkingFolder: "",
|
||||||
Working: &GitStatus{},
|
Working: &GitStatus{},
|
||||||
Staging: &GitStatus{},
|
Staging: &GitStatus{},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return g
|
return g
|
||||||
|
@ -993,7 +993,7 @@ func TestGetGitCommand(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGitTemplate(t *testing.T) {
|
func TestGitTemplateString(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Case string
|
Case string
|
||||||
Expected string
|
Expected string
|
||||||
|
@ -1001,29 +1001,29 @@ func TestGitTemplate(t *testing.T) {
|
||||||
Repo *Repo
|
Repo *Repo
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Case: "Only HEAD name",
|
Case: "Only HEAD name",
|
||||||
Expected: "main",
|
Expected: "main",
|
||||||
Template: "{{ .HEAD }}",
|
Template: "{{ .HEAD }}",
|
||||||
Repo: &Repo{
|
Repo: &Repo{
|
||||||
HEAD: "main",
|
HEAD: "main",
|
||||||
Behind: 2,
|
Behind: 2,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Case: "Working area changes",
|
Case: "Working area changes",
|
||||||
Expected: "main \uF044 +2 ~3",
|
Expected: "main \uF044 +2 ~3",
|
||||||
Template: "{{ .HEAD }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
|
Template: "{{ .HEAD }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
|
||||||
Repo: &Repo{
|
Repo: &Repo{
|
||||||
HEAD: "main",
|
HEAD: "main",
|
||||||
Working: &GitStatus{
|
Working: &GitStatus{
|
||||||
Added: 2,
|
Added: 2,
|
||||||
Modified: 3,
|
Modified: 3,
|
||||||
Changed: true,
|
Changed: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Case: "No working area changes",
|
Case: "No working area changes",
|
||||||
Expected: "main",
|
Expected: "main",
|
||||||
Template: "{{ .HEAD }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
|
Template: "{{ .HEAD }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
|
||||||
Repo: &Repo{
|
Repo: &Repo{
|
||||||
|
@ -1034,44 +1034,67 @@ func TestGitTemplate(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Case: "Working and staging area changes",
|
Case: "Working and staging area changes",
|
||||||
Expected: "main \uF046 +5 ~1 \uF044 +2 ~3",
|
Expected: "main \uF046 +5 ~1 \uF044 +2 ~3",
|
||||||
Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
|
Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
|
||||||
Repo: &Repo{
|
Repo: &Repo{
|
||||||
HEAD: "main",
|
HEAD: "main",
|
||||||
Working: &GitStatus{
|
Working: &GitStatus{
|
||||||
Added: 2,
|
Added: 2,
|
||||||
Modified: 3,
|
Modified: 3,
|
||||||
Changed: true,
|
Changed: true,
|
||||||
},
|
},
|
||||||
Staging: &GitStatus{
|
Staging: &GitStatus{
|
||||||
Added: 5,
|
Added: 5,
|
||||||
Modified: 1,
|
Modified: 1,
|
||||||
Changed: true,
|
Changed: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Case: "No local changes",
|
Case: "No local changes",
|
||||||
Expected: "main",
|
Expected: "main",
|
||||||
Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
|
Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}",
|
||||||
Repo: &Repo{
|
Repo: &Repo{
|
||||||
HEAD: "main",
|
HEAD: "main",
|
||||||
Staging: &GitStatus{},
|
Staging: &GitStatus{},
|
||||||
Working: &GitStatus{},
|
Working: &GitStatus{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Case: "Upstream Icon",
|
||||||
|
Expected: "from GitHub on main",
|
||||||
|
Template: "from {{ .UpstreamIcon }} on {{ .HEAD }}",
|
||||||
|
Repo: &Repo{
|
||||||
|
HEAD: "main",
|
||||||
|
Staging: &GitStatus{},
|
||||||
|
Working: &GitStatus{},
|
||||||
|
UpstreamIcon: "GitHub",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "Branch status",
|
||||||
|
Expected: "from GitHub on main \u21912",
|
||||||
|
Template: "from {{ .UpstreamIcon }} on {{ .HEAD }} {{ .BranchStatus }}",
|
||||||
|
Repo: &Repo{
|
||||||
|
HEAD: "main",
|
||||||
|
Staging: &GitStatus{},
|
||||||
|
Working: &GitStatus{},
|
||||||
|
UpstreamIcon: "GitHub",
|
||||||
|
BranchStatus: "\u21912",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
g := &git{
|
g := &git{
|
||||||
props: &properties{
|
props: &properties{
|
||||||
values: map[Property]interface{}{
|
values: map[Property]interface{}{
|
||||||
SegmentTemplate: tc.Template,
|
DisplayStatus: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
repo: tc.Repo,
|
repo: tc.Repo,
|
||||||
}
|
}
|
||||||
assert.Equal(t, tc.Expected, g.string(), tc.Case)
|
assert.Equal(t, tc.Expected, g.templateString(tc.Template), tc.Case)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue