diff --git a/src/segment_git.go b/src/segment_git.go index 404e1fa4..0eda3ee7 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -297,7 +297,7 @@ func (g *git) getGitCommand() 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", strings.TrimSuffix(g.Repo.gitRootFolder, ".git"), "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"}, args...) val, _ := g.env.runCommand(g.getGitCommand(), args...) return val } diff --git a/src/segment_git_test.go b/src/segment_git_test.go index 87ccb402..d1fadaf3 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -60,7 +60,7 @@ func TestEnabledInWorkingTree(t *testing.T) { } func TestGetGitOutputForCommand(t *testing.T) { - args := []string{"--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"} + args := []string{"-C", "test", "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"} commandArgs := []string{"symbolic-ref", "--short", "HEAD"} want := "je suis le output" env := new(MockedEnvironment) @@ -69,6 +69,9 @@ func TestGetGitOutputForCommand(t *testing.T) { env.On("getRuntimeGOOS", nil).Return("unix") g := &git{ env: env, + Repo: &Repo{ + gitRootFolder: "test", + }, } got := g.getGitCommandOutput(commandArgs...) assert.Equal(t, want, got) @@ -139,7 +142,7 @@ func setupHEADContextEnv(context *detachedContext) *git { } 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) } @@ -554,7 +557,7 @@ func TestGitUpstream(t *testing.T) { for _, tc := range cases { env := &MockedEnvironment{} 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) //nolint:lll env.On("getRuntimeGOOS", nil).Return("unix") props := &properties{ values: map[Property]interface{}{