fix(git): show original worktree as repo name

This commit is contained in:
Jan De Dobbeleer 2024-02-06 12:58:19 +01:00 committed by Jan De Dobbeleer
parent b86bd04574
commit bb5e1b1a2e
2 changed files with 64 additions and 3 deletions

View file

@ -167,7 +167,7 @@ func (g *Git) Enabled() bool {
g.setUser()
}
g.RepoName = platform.Base(g.env, g.convertToLinuxPath(g.realDir))
g.RepoName = g.repoName()
g.Working = &GitStatus{}
g.Staging = &GitStatus{}
@ -268,6 +268,10 @@ func (g *Git) Kraken() string {
return fmt.Sprintf("gitkraken://repolink/%s/commit/%s?url=%s", root, g.Hash, url2.QueryEscape(g.RawUpstreamURL))
}
func (g *Git) LatestTag() string {
return g.getGitCommandOutput("describe", "--tags", "--abbrev=0")
}
func (g *Git) shouldDisplay() bool {
if !g.hasCommand(GITCOMMAND) {
return false
@ -850,6 +854,15 @@ func (g *Git) getSwitchMode(property properties.Property, gitSwitch, mode string
return fmt.Sprintf("%s%s", gitSwitch, mode)
}
func (g *Git) LatestTag() string {
return g.getGitCommandOutput("describe", "--tags", "--abbrev=0")
func (g *Git) repoName() string {
if !g.IsWorkTree {
return platform.Base(g.env, g.convertToLinuxPath(g.realDir))
}
ind := strings.LastIndex(g.workingDir, ".git/worktrees")
if ind > -1 {
return platform.Base(g.env, g.workingDir[:ind])
}
return ""
}

View file

@ -1108,3 +1108,51 @@ func TestGitRemotes(t *testing.T) {
assert.Equal(t, tc.Expected, len(got), tc.Case)
}
}
func TestGitRepoName(t *testing.T) {
cases := []struct {
Case string
Expected string
WorkingDir string
RealDir string
IsWorkTree bool
}{
{
Case: "In worktree",
Expected: "oh-my-posh",
IsWorkTree: true,
WorkingDir: "/Users/jan/Code/oh-my-posh/.git/worktrees/oh-my-posh2",
},
{
Case: "Not in worktree",
Expected: "oh-my-posh",
IsWorkTree: false,
RealDir: "/Users/jan/Code/oh-my-posh",
},
{
Case: "In worktree, unexpected dir",
Expected: "",
IsWorkTree: true,
WorkingDir: "/Users/jan/Code/oh-my-posh2",
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("PathSeparator").Return("/")
env.On("GOOS").Return(platform.LINUX)
g := &Git{
scm: scm{
props: properties.Map{},
env: env,
realDir: tc.RealDir,
workingDir: tc.WorkingDir,
},
IsWorkTree: tc.IsWorkTree,
}
got := g.repoName()
assert.Equal(t, tc.Expected, got, tc.Case)
}
}