fix(git): submodules support

This commit is contained in:
lnu 2021-12-24 15:19:59 +01:00 committed by Jan De Dobbeleer
parent 541e8e95aa
commit 393f99da93
2 changed files with 43 additions and 4 deletions

View file

@ -134,14 +134,28 @@ func (g *git) enabled() bool {
// if we open a worktree file in a shared wsl2 folder, we have to convert it back
// to the mounted path
g.gitWorkingFolder = g.convertToLinuxPath(matches["dir"])
// in worktrees, the path looks like this: gitdir: path/.git/worktrees/branch
// strips the last .git/worktrees part
// :ind+5 = index + /.git
ind := strings.LastIndex(g.gitWorkingFolder, "/.git/worktrees")
g.gitRootFolder = g.gitWorkingFolder[:ind+5]
g.gitRealFolder = strings.TrimSuffix(g.env.getFileContent(g.gitWorkingFolder+"/gitdir"), ".git\n")
g.IsWorkTree = true
return true
if ind > -1 {
g.gitRootFolder = g.gitWorkingFolder[:ind+5]
g.gitRealFolder = strings.TrimSuffix(g.env.getFileContent(g.gitWorkingFolder+"/gitdir"), ".git\n")
g.IsWorkTree = true
return true
}
// in submodules, the path looks like this: gitdir: ../.git/modules/test-submodule
// we need the parent folder to detect where the real .git folder is
ind = strings.LastIndex(g.gitWorkingFolder, "/.git/modules")
if ind > -1 {
g.gitRootFolder = gitdir.parentFolder + "/" + g.gitWorkingFolder
g.gitRealFolder = g.gitRootFolder
g.gitWorkingFolder = g.gitRootFolder
return true
}
return false
}
return false
}

View file

@ -72,6 +72,31 @@ func TestEnabledInWorkingTree(t *testing.T) {
assert.Equal(t, "/dev/folder_worktree", g.gitRealFolder)
}
func TestEnabledInSubmodule(t *testing.T) {
env := new(MockedEnvironment)
env.On("inWSLSharedDrive", nil).Return(false)
env.On("hasCommand", "git").Return(true)
env.On("getRuntimeGOOS", nil).Return("")
env.On("isWsl", nil).Return(false)
fileInfo := &fileInfo{
path: "/dev/parent/test-submodule/.git",
parentFolder: "/dev/parent/test-submodule",
isDir: false,
}
env.On("hasParentFilePath", ".git").Return(fileInfo, nil)
env.On("getFileContent", "/dev/parent/test-submodule/.git").Return("gitdir: ../.git/modules/test-submodule")
env.On("getFileContent", "/dev/parent/.git/modules/test-submodule").Return("/dev/folder_worktree.git\n")
g := &git{
scm: scm{
env: env,
},
}
assert.True(t, g.enabled())
assert.Equal(t, "/dev/parent/test-submodule/../.git/modules/test-submodule", g.gitWorkingFolder)
assert.Equal(t, "/dev/parent/test-submodule/../.git/modules/test-submodule", g.gitRealFolder)
assert.Equal(t, "/dev/parent/test-submodule/../.git/modules/test-submodule", 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"}