mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
fix(git): read real worktree folder from gitdir
This commit is contained in:
parent
18a3b82dd1
commit
2733865edf
|
@ -78,8 +78,9 @@ type git struct {
|
||||||
WorktreeCount int
|
WorktreeCount int
|
||||||
IsWorkTree bool
|
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
|
||||||
|
gitWorktreeFolder string // .git real worktree path
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -152,8 +153,7 @@ func (g *git) enabled() bool {
|
||||||
}
|
}
|
||||||
// handle worktree
|
// handle worktree
|
||||||
g.gitRootFolder = gitdir.path
|
g.gitRootFolder = gitdir.path
|
||||||
dirPointer := g.env.getFileContent(gitdir.path)
|
dirPointer := strings.Trim(g.env.getFileContent(gitdir.path), " \r\n")
|
||||||
dirPointer = strings.Trim(dirPointer, " \r\n")
|
|
||||||
matches := findNamedRegexMatch(`^gitdir: (?P<dir>.*)$`, dirPointer)
|
matches := findNamedRegexMatch(`^gitdir: (?P<dir>.*)$`, dirPointer)
|
||||||
if matches != nil && matches["dir"] != "" {
|
if matches != nil && matches["dir"] != "" {
|
||||||
g.gitWorkingFolder = matches["dir"]
|
g.gitWorkingFolder = matches["dir"]
|
||||||
|
@ -162,6 +162,7 @@ func (g *git) enabled() bool {
|
||||||
// :ind+5 = index + /.git
|
// :ind+5 = index + /.git
|
||||||
ind := strings.LastIndex(g.gitWorkingFolder, "/.git/worktrees")
|
ind := strings.LastIndex(g.gitWorkingFolder, "/.git/worktrees")
|
||||||
g.gitRootFolder = g.gitWorkingFolder[:ind+5]
|
g.gitRootFolder = g.gitWorkingFolder[:ind+5]
|
||||||
|
g.gitWorktreeFolder = strings.TrimSuffix(g.env.getFileContent(g.gitWorkingFolder+"/gitdir"), ".git\n")
|
||||||
g.IsWorkTree = true
|
g.IsWorkTree = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -292,7 +293,7 @@ func (g *git) getGitCommand() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *git) getGitCommandOutput(args ...string) string {
|
func (g *git) getGitCommandOutput(args ...string) string {
|
||||||
args = append([]string{"--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
|
args = append([]string{"-C", g.gitWorktreeFolder, "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
|
||||||
val, _ := g.env.runCommand(g.getGitCommand(), args...)
|
val, _ := g.env.runCommand(g.getGitCommand(), args...)
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,21 +46,23 @@ func TestEnabledInWorkingTree(t *testing.T) {
|
||||||
env.On("getRuntimeGOOS", nil).Return("")
|
env.On("getRuntimeGOOS", nil).Return("")
|
||||||
env.On("isWsl", nil).Return(false)
|
env.On("isWsl", nil).Return(false)
|
||||||
fileInfo := &fileInfo{
|
fileInfo := &fileInfo{
|
||||||
path: "/dir/hello",
|
path: "/dev/folder_worktree/.git",
|
||||||
parentFolder: "/dir",
|
parentFolder: "/dev/folder_worktree",
|
||||||
isDir: false,
|
isDir: false,
|
||||||
}
|
}
|
||||||
env.On("hasParentFilePath", ".git").Return(fileInfo, nil)
|
env.On("hasParentFilePath", ".git").Return(fileInfo, nil)
|
||||||
env.On("getFileContent", "/dir/hello").Return("gitdir: /dir/hello/burp/burp")
|
env.On("getFileContent", "/dev/folder_worktree/.git").Return("gitdir: /dev/real_folder/.git/worktrees/folder_worktree")
|
||||||
|
env.On("getFileContent", "/dev/real_folder/.git/worktrees/folder_worktree/gitdir").Return("/dev/folder_worktree.git\n")
|
||||||
g := &git{
|
g := &git{
|
||||||
env: env,
|
env: env,
|
||||||
}
|
}
|
||||||
assert.True(t, g.enabled())
|
assert.True(t, g.enabled())
|
||||||
assert.Equal(t, "/dir/hello/burp/burp", g.gitWorkingFolder)
|
assert.Equal(t, "/dev/real_folder/.git/worktrees/folder_worktree", g.gitWorkingFolder)
|
||||||
|
assert.Equal(t, "/dev/folder_worktree", g.gitWorktreeFolder)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetGitOutputForCommand(t *testing.T) {
|
func TestGetGitOutputForCommand(t *testing.T) {
|
||||||
args := []string{"--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}
|
args := []string{"-C", "", "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}
|
||||||
commandArgs := []string{"symbolic-ref", "--short", "HEAD"}
|
commandArgs := []string{"symbolic-ref", "--short", "HEAD"}
|
||||||
want := "je suis le output"
|
want := "je suis le output"
|
||||||
env := new(MockedEnvironment)
|
env := new(MockedEnvironment)
|
||||||
|
@ -137,7 +139,7 @@ func setupHEADContextEnv(context *detachedContext) *git {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockedEnvironment) mockGitCommand(returnValue string, args ...string) {
|
func (m *MockedEnvironment) mockGitCommand(returnValue string, args ...string) {
|
||||||
args = append([]string{"--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
|
args = append([]string{"-C", "", "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...)
|
||||||
m.On("runCommand", "git", args).Return(returnValue, nil)
|
m.On("runCommand", "git", args).Return(returnValue, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -550,7 +552,8 @@ func TestGitUpstream(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
env := &MockedEnvironment{}
|
env := &MockedEnvironment{}
|
||||||
env.On("isWsl", nil).Return(false)
|
env.On("isWsl", nil).Return(false)
|
||||||
env.On("runCommand", "git", []string{"--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false", "remote", "get-url", "origin"}).Return(tc.Upstream, nil)
|
env.On("runCommand", "git", []string{"-C", "", "--no-optional-locks", "-c", "core.quotepath=false",
|
||||||
|
"-c", "color.status=false", "remote", "get-url", "origin"}).Return(tc.Upstream, nil)
|
||||||
env.On("getRuntimeGOOS", nil).Return("unix")
|
env.On("getRuntimeGOOS", nil).Return("unix")
|
||||||
props := &properties{
|
props := &properties{
|
||||||
values: map[Property]interface{}{
|
values: map[Property]interface{}{
|
||||||
|
|
Loading…
Reference in a new issue