feat(git): fetch remote on bare repo

This commit is contained in:
Jan De Dobbeleer 2022-10-13 08:35:11 +02:00 committed by Jan De Dobbeleer
parent e0b28bd2c4
commit 040a73e0f0
2 changed files with 41 additions and 10 deletions

View file

@ -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 {

View file

@ -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)
}
}