Move gitDirOfRepo to git_commands

The file loader is going to need it too.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-10-15 09:13:12 +02:00
co-authored by Claude Opus 5.5
parent 8b79e1dc1f
commit 8a2d9758e0
2 changed files with 32 additions and 32 deletions
+31
View File
@@ -313,3 +313,34 @@ func runGitRevParse(gitCmd *oscommands.CmdObj) (string, error) {
}
return strings.TrimSpace(res), nil
}
// GitDirOfRepo returns the directory that holds the git data of the repo at
// repoPath, and whether it could be found. An ordinary repo keeps that data in
// a .git directory; a worktree and a submodule have a .git file naming the
// directory instead.
func GitDirOfRepo(repoPath string) (string, bool) {
gitDirPath := filepath.Join(repoPath, ".git")
stat, err := os.Stat(gitDirPath)
if err != nil {
return "", false
}
if stat.IsDir() {
return gitDirPath, true
}
content, err := os.ReadFile(gitDirPath)
if err != nil {
return "", false
}
gitDir, ok := strings.CutPrefix(strings.TrimSpace(string(content)), "gitdir: ")
if !ok {
return "", false
}
// A relative name is relative to the repo. Git writes one for a submodule,
// and for a worktree created with --relative-paths.
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(repoPath, gitDir)
}
return gitDir, true
}
+1 -32
View File
@@ -70,37 +70,6 @@ type headInfo struct {
hash string
}
// gitDirOfRepo returns the directory that holds the git data of the repo at
// repoPath, and whether it could be found. An ordinary repo keeps that data in
// a .git directory; a worktree and a submodule have a .git file naming the
// directory instead.
func gitDirOfRepo(repoPath string) (string, bool) {
gitDirPath := filepath.Join(repoPath, ".git")
stat, err := os.Stat(gitDirPath)
if err != nil {
return "", false
}
if stat.IsDir() {
return gitDirPath, true
}
content, err := os.ReadFile(gitDirPath)
if err != nil {
return "", false
}
gitDir, ok := strings.CutPrefix(strings.TrimSpace(string(content)), "gitdir: ")
if !ok {
return "", false
}
// A relative name is relative to the repo. Git writes one for a submodule,
// and for a worktree created with --relative-paths.
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(repoPath, gitDir)
}
return gitDir, true
}
// The branch git names in the HEAD file of a repo that keeps its refs in a
// reftable. The refs live in a binary table there, and the name in HEAD
// resolves nowhere, so that a reader of the file gets an error instead of a
@@ -110,7 +79,7 @@ const reftablePlaceholderBranch = ".invalid"
// readHeadInfo reads the HEAD file of the repo at repoPath to find out what it
// has checked out, and reports whether that worked.
func readHeadInfo(repoPath string) (headInfo, bool) {
gitDir, ok := gitDirOfRepo(repoPath)
gitDir, ok := git_commands.GitDirOfRepo(repoPath)
if !ok {
return headInfo{}, false
}