Skip to content

Commit

Permalink
feat: max branch length
Browse files Browse the repository at this point in the history
resolves #755
  • Loading branch information
JanDeDobbeleer committed May 29, 2021
1 parent 266bce3 commit bc50377
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
5 changes: 3 additions & 2 deletions docs/docs/segment-git.mdx
Expand Up @@ -20,8 +20,8 @@ To enable this, set `$env:POSH_GIT_ENABLED = $true` in your `$PROFILE`.
:::

:::warning
Starting from version 3.152.0, `display_status` is disabled by default.
It improves performance but reduces the quantity of information. Don't forget to enable it in your theme if needed.
Starting from version 3.152.0, `display_status` is disabled by default.
It improves performance but reduces the quantity of information. Don't forget to enable it in your theme if needed.
An alternative is to use the [Posh-Git segment][poshgit]
:::

Expand Down Expand Up @@ -52,6 +52,7 @@ An alternative is to use the [Posh-Git segment][poshgit]
- branch_ahead_icon: `string` - the icon to display when the local branch is ahead of its remote - defaults to `\u2191`
- branch_behind_icon: `string` - the icon to display when the local branch is behind its remote - defaults to `\u2193`
- branch_gone_icon: `string` - the icon to display when there's no remote branch - defaults to `\u2262`
- branch_max_length: `int` - the max length for the displayed branch name where `0` implies full length - defaults to `0`

### Status

Expand Down
17 changes: 17 additions & 0 deletions src/properties.go
Expand Up @@ -112,6 +112,23 @@ func (p *properties) getFloat64(property Property, defaultValue float64) float64
return floatValue
}

func (p *properties) getInt(property Property, defaultValue int) int {
if p == nil || p.values == nil {
return defaultValue
}
val, found := p.values[property]
if !found {
return defaultValue
}

intValue, ok := val.(int)
if !ok {
return defaultValue
}

return intValue
}

func (p *properties) getKeyValueMap(property Property, defaultValue map[string]string) map[string]string {
if p == nil || p.values == nil {
return defaultValue
Expand Down
18 changes: 17 additions & 1 deletion src/segment_git.go
Expand Up @@ -112,6 +112,8 @@ const (
BehindColor Property = "behind_color"
// AheadColor if set, the color to use when the branch is ahead and behind the remote
AheadColor Property = "ahead_color"
// BranchMaxLength truncates the length of the branch name
BranchMaxLength Property = "branch_max_length"
)

func (g *git) enabled() bool {
Expand Down Expand Up @@ -295,13 +297,16 @@ func (g *git) getGitHEADContext(ref string) string {
if ref == "" {
ref = g.getPrettyHEADName()
} else {
ref = g.truncateBranch(ref)
ref = fmt.Sprintf("%s%s", branchIcon, ref)
}
// rebase
if g.hasGitFolder("rebase-merge") {
head := g.getGitFileContents("rebase-merge/head-name")
origin := strings.Replace(head, "refs/heads/", "", 1)
origin = g.truncateBranch(origin)
onto := g.getGitRefFileSymbolicName("rebase-merge/onto")
onto = g.truncateBranch(onto)
step := g.getGitFileContents("rebase-merge/msgnum")
total := g.getGitFileContents("rebase-merge/end")
icon := g.props.getString(RebaseIcon, "\uE728 ")
Expand All @@ -310,6 +315,7 @@ func (g *git) getGitHEADContext(ref string) string {
if g.hasGitFolder("rebase-apply") {
head := g.getGitFileContents("rebase-apply/head-name")
origin := strings.Replace(head, "refs/heads/", "", 1)
origin = g.truncateBranch(origin)
step := g.getGitFileContents("rebase-apply/next")
total := g.getGitFileContents("rebase-apply/last")
icon := g.props.getString(RebaseIcon, "\uE728 ")
Expand All @@ -321,7 +327,8 @@ func (g *git) getGitHEADContext(ref string) string {
mergeContext := g.getGitFileContents("MERGE_MSG")
matches := findNamedRegexMatch(`Merge branch '(?P<head>.*)' into`, mergeContext)
if matches != nil && matches["head"] != "" {
return fmt.Sprintf("%s%s%s into %s", icon, branchIcon, matches["head"], ref)
branch := g.truncateBranch(matches["head"])
return fmt.Sprintf("%s%s%s into %s", icon, branchIcon, branch, ref)
}
}
// cherry-pick
Expand All @@ -333,6 +340,14 @@ func (g *git) getGitHEADContext(ref string) string {
return ref
}

func (g *git) truncateBranch(branch string) string {
maxLength := g.props.getInt(BranchMaxLength, 0)
if maxLength == 0 {
return branch
}
return branch[0:maxLength]
}

func (g *git) hasGitFile(file string) bool {
return g.env.hasFilesInDir(g.repo.gitFolder, file)
}
Expand All @@ -356,6 +371,7 @@ func (g *git) getGitRefFileSymbolicName(refFile string) string {
func (g *git) getPrettyHEADName() string {
ref := g.getGitCommandOutput("branch", "--show-current")
if ref != "" {
ref = g.truncateBranch(ref)
return fmt.Sprintf("%s%s", g.props.getString(BranchIcon, "\uE0A0"), ref)
}
// check for tag
Expand Down
25 changes: 25 additions & 0 deletions src/segment_git_test.go
Expand Up @@ -769,3 +769,28 @@ func TestGetBranchStatus(t *testing.T) {
assert.Equal(t, tc.Expected, g.getBranchStatus(), tc.Case)
}
}

func TestTruncateBranch(t *testing.T) {
cases := []struct {
Case string
Expected string
Branch string
MaxLength interface{}
}{
{Case: "No limit", Expected: "all-your-base-are-belong-to-us", Branch: "all-your-base-are-belong-to-us"},
{Case: "No limit - larger", Expected: "all-your-base", Branch: "all-your-base-are-belong-to-us", MaxLength: 13},
{Case: "No limit - smaller", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 13},
{Case: "Invalid setting", Expected: "all-your-base", Branch: "all-your-base", MaxLength: "burp"},
}

for _, tc := range cases {
g := &git{
props: &properties{
values: map[Property]interface{}{
BranchMaxLength: tc.MaxLength,
},
},
}
assert.Equal(t, tc.Expected, g.truncateBranch(tc.Branch), tc.Case)
}
}

0 comments on commit bc50377

Please sign in to comment.