diff --git a/src/segments/git.go b/src/segments/git.go index 1a2abd20..eaacb684 100644 --- a/src/segments/git.go +++ b/src/segments/git.go @@ -120,6 +120,9 @@ func (g *Git) Template() string { } func (g *Git) Enabled() bool { + g.Working = &GitStatus{} + g.Staging = &GitStatus{} + if !g.shouldDisplay() { return false } @@ -127,12 +130,7 @@ func (g *Git) Enabled() bool { g.RepoName = environment.Base(g.env, g.convertToLinuxPath(g.realDir)) if g.IsBare { - head := g.FileContents(g.workingDir, "HEAD") - branchIcon := g.props.GetString(BranchIcon, "\uE0A0") - g.Ref = strings.Replace(head, "ref: refs/heads/", "", 1) - g.HEAD = fmt.Sprintf("%s%s", branchIcon, g.Ref) - g.Working = &GitStatus{} - g.Staging = &GitStatus{} + g.getBareRepoInfo() return true } @@ -169,7 +167,7 @@ func (g *Git) Kraken() string { if len(g.Upstream) == 0 { g.Upstream = "origin" } - g.RawUpstreamURL = g.getOriginURL() + g.RawUpstreamURL = g.getRemoteURL() } if len(g.Hash) == 0 { g.Hash = g.getGitCommandOutput("rev-parse", "HEAD") @@ -214,6 +212,20 @@ func (g *Git) shouldDisplay() bool { return true } +func (g *Git) getBareRepoInfo() { + head := g.FileContents(g.workingDir, "HEAD") + branchIcon := g.props.GetString(BranchIcon, "\uE0A0") + g.Ref = strings.Replace(head, "ref: refs/heads/", "", 1) + g.HEAD = fmt.Sprintf("%s%s", branchIcon, g.Ref) + if !g.props.GetBool(FetchUpstreamIcon, false) { + return + } + g.Upstream = g.getGitCommandOutput("remote") + if len(g.Upstream) != 0 { + g.UpstreamIcon = g.getUpstreamIcon() + } +} + func (g *Git) setDir(dir string) { dir = environment.ReplaceHomeDirPrefixWithTilde(g.env, dir) // align with template PWD if g.env.GOOS() == environment.WINDOWS { @@ -313,7 +325,7 @@ func (g *Git) getUpstreamIcon() string { url = strings.ReplaceAll(url, ":", "/") return fmt.Sprintf("https://%s", url) } - g.RawUpstreamURL = g.getOriginURL() + g.RawUpstreamURL = g.getRemoteURL() g.UpstreamURL = cleanSSHURL(g.RawUpstreamURL) if strings.Contains(g.UpstreamURL, "github") { return g.props.GetString(GithubIcon, "\uF408 ") @@ -588,7 +600,7 @@ func (g *Git) getWorktreeContext() int { return count } -func (g *Git) getOriginURL() string { +func (g *Git) getRemoteURL() string { upstream := regex.ReplaceAllString("/.*", g.Upstream, "") cfg, err := ini.Load(g.rootDir + "/config") if err != nil { diff --git a/src/segments/git_test.go b/src/segments/git_test.go index ca26fd4d..762cd3bf 100644 --- a/src/segments/git_test.go +++ b/src/segments/git_test.go @@ -150,8 +150,12 @@ func TestEnabledInBareRepo(t *testing.T) { Case string HEAD string IsBare string + FetchRemote bool + Remote string + RemoteURL string ExpectedEnabled bool ExpectedHEAD string + ExpectedRemote string }{ { Case: "Bare repo on main", @@ -164,6 +168,17 @@ func TestEnabledInBareRepo(t *testing.T) { Case: "Not a bare repo", IsBare: "false", }, + { + Case: "Bare repo on main remote enabled", + IsBare: "true", + HEAD: "ref: refs/heads/main", + ExpectedEnabled: true, + ExpectedHEAD: "main", + FetchRemote: true, + Remote: "origin", + RemoteURL: "git@github.com:JanDeDobbeleer/oh-my-posh.git", + ExpectedRemote: "\uf408 ", + }, } for _, tc := range cases { pwd := "/home/user/bare.git" @@ -175,16 +190,20 @@ func TestEnabledInBareRepo(t *testing.T) { env.MockGitCommand(pwd, tc.IsBare, "rev-parse", "--is-bare-repository") env.On("Pwd").Return(pwd) env.On("FileContent", "/home/user/bare.git/HEAD").Return(tc.HEAD) + env.MockGitCommand(pwd, tc.Remote, "remote") + env.MockGitCommand(pwd, tc.RemoteURL, "remote", "get-url", tc.Remote) g := &Git{ scm: scm{ env: env, props: properties.Map{ - FetchBareInfo: true, + FetchBareInfo: true, + FetchUpstreamIcon: tc.FetchRemote, }, }, } assert.Equal(t, g.Enabled(), tc.ExpectedEnabled, tc.Case) assert.Equal(t, g.Ref, tc.ExpectedHEAD, tc.Case) + assert.Equal(t, g.UpstreamIcon, tc.ExpectedRemote, tc.Case) } }