Skip to content

Commit d147583

Browse files
committed
Fix seg-fault when opening submodule in nested folder
1 parent 579791e commit d147583

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

pkg/commands/git_commands/repo_paths.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,28 @@ func getCurrentRepoGitDirPath(
194194
return "", "", errors.Errorf("could not find git dir for %s: %v", currentPath, err)
195195
}
196196

197-
// confirm whether the next directory up is the worktrees/submodules directory
197+
_, err = fs.Stat(worktreeGitPath)
198+
if err != nil {
199+
return "", "", errors.Errorf("could not find git dir for %s: %v", currentPath, err)
200+
}
201+
202+
// confirm whether the next directory up is the worktrees directory
198203
parent := path.Dir(worktreeGitPath)
199-
if path.Base(parent) != "worktrees" && path.Base(parent) != "modules" {
200-
return "", "", errors.Errorf("could not find git dir for %s", currentPath)
204+
if path.Base(parent) == "worktrees" {
205+
gitDirPath := path.Dir(parent)
206+
return gitDirPath, path.Dir(gitDirPath), nil
201207
}
202208

203-
// if it's a submodule, we treat it as its own repo
204-
if path.Base(parent) == "modules" {
209+
// Unlike worktrees, submodules can be nested arbitrarily deep, so we check
210+
// if the `modules` directory is anywhere up the chain.
211+
if strings.Contains(worktreeGitPath, "/modules/") {
212+
// For submodules, we just return the path directly
205213
return worktreeGitPath, currentPath, nil
206214
}
207215

208-
gitDirPath := path.Dir(parent)
209-
return gitDirPath, path.Dir(gitDirPath), nil
216+
// If this error causes issues, we could relax the constraint and just always
217+
// return the path
218+
return "", "", errors.Errorf("could not find git dir for %s: path is not under `worktrees` or `modules` directories", currentPath)
210219
}
211220

212221
// takes a path containing a symlink and returns the true path

pkg/commands/git_commands/repo_paths_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestGetRepoPathsAux(t *testing.T) {
7373
},
7474
Path: "/path/to/repo/worktree2",
7575
Expected: nil,
76-
Err: errors.New("failed to get repo git dir path: could not find git dir for /path/to/repo/worktree2"),
76+
Err: errors.New("failed to get repo git dir path: could not find git dir for /path/to/repo/worktree2: open /nonexistant: file does not exist"),
7777
},
7878
{
7979
Name: "submodule",
@@ -92,6 +92,23 @@ func TestGetRepoPathsAux(t *testing.T) {
9292
},
9393
Err: nil,
9494
},
95+
{
96+
Name: "submodule in nested directory",
97+
BeforeFunc: func(fs afero.Fs) {
98+
_ = fs.MkdirAll("/path/to/repo/.git/modules/my/submodule1", 0o755)
99+
_ = afero.WriteFile(fs, "/path/to/repo/my/submodule1/.git", []byte("gitdir: /path/to/repo/.git/modules/my/submodule1"), 0o644)
100+
},
101+
Path: "/path/to/repo/my/submodule1",
102+
Expected: &RepoPaths{
103+
currentPath: "/path/to/repo/my/submodule1",
104+
worktreePath: "/path/to/repo/my/submodule1",
105+
worktreeGitDirPath: "/path/to/repo/.git/modules/my/submodule1",
106+
repoPath: "/path/to/repo/my/submodule1",
107+
repoGitDirPath: "/path/to/repo/.git/modules/my/submodule1",
108+
repoName: "submodule1",
109+
},
110+
Err: nil,
111+
},
95112
}
96113

97114
for _, s := range scenarios {

0 commit comments

Comments
 (0)