From 09735e7cccb1a139ad0417e18cdf687a6dbc3dc9 Mon Sep 17 00:00:00 2001 From: lnu Date: Mon, 11 Apr 2022 20:54:30 +0200 Subject: [PATCH] feat(git): --separate-git-dir support --- src/segments/git.go | 9 +++++++++ src/segments/git_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/segments/git.go b/src/segments/git.go index e0b71e76..02b328f7 100644 --- a/src/segments/git.go +++ b/src/segments/git.go @@ -190,6 +190,15 @@ func (g *Git) shouldDisplay() bool { return true } + // check for separate git folder(--separate-git-dir) + // check if the folder contains a HEAD file + if g.env.HasFilesInDir(g.gitWorkingFolder, "HEAD") { + gitFolder := strings.TrimSuffix(g.gitRootFolder, ".git") + g.gitRootFolder = g.gitWorkingFolder + g.gitWorkingFolder = gitFolder + g.gitRealFolder = gitFolder + return true + } return false } return false diff --git a/src/segments/git_test.go b/src/segments/git_test.go index ca5cab39..fa384bd4 100644 --- a/src/segments/git_test.go +++ b/src/segments/git_test.go @@ -109,6 +109,33 @@ func TestEnabledInSubmodule(t *testing.T) { assert.Equal(t, "/dev/parent/test-submodule/../.git/modules/test-submodule", g.gitRootFolder) } +func TestEnabledInSeparateGitDir(t *testing.T) { + env := new(mock.MockedEnvironment) + env.On("InWSLSharedDrive").Return(false) + env.On("HasCommand", "git").Return(true) + env.On("GOOS").Return("") + env.On("IsWsl").Return(false) + fileInfo := &environment.FileInfo{ + Path: "/dev/parent/test-separate-git-dir/.git", + ParentFolder: "/dev/parent/test-separate-git-dir", + IsDir: false, + } + env.On("HasFilesInDir", "/dev/separate-git-dir", "HEAD").Return(true) + env.On("FileContent", "/dev/parent/test-separate-git-dir//HEAD").Return("") + env.MockGitCommand("/dev/parent/test-separate-git-dir/", "", "describe", "--tags", "--exact-match") + env.On("HasParentFilePath", ".git").Return(fileInfo, nil) + env.On("FileContent", "/dev/parent/test-separate-git-dir/.git").Return("gitdir: /dev/separate-git-dir") + g := &Git{ + scm: scm{ + env: env, + props: properties.Map{}, + }, + } + assert.True(t, g.Enabled()) + assert.Equal(t, "/dev/parent/test-separate-git-dir/", g.gitWorkingFolder) + assert.Equal(t, "/dev/parent/test-separate-git-dir/", g.gitRealFolder) + assert.Equal(t, "/dev/separate-git-dir", g.gitRootFolder) +} func TestGetGitOutputForCommand(t *testing.T) { args := []string{"-C", "", "--no-optional-locks", "-c", "core.quotepath=false", "-c", "color.status=false"} commandArgs := []string{"symbolic-ref", "--short", "HEAD"}