From 8a2d9758e0ca075d3b626672f9f5fc56a7b19476 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 12 Oct 2026 18:21:54 +0200 Subject: [PATCH] Move gitDirOfRepo to git_commands The file loader is going to need it too. Co-Authored-By: Claude Opus 5.5 (1M context) --- pkg/commands/git_commands/repo_paths.go | 31 +++++++++++++++++++ pkg/gui/controllers/helpers/repos_helper.go | 33 +-------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/pkg/commands/git_commands/repo_paths.go b/pkg/commands/git_commands/repo_paths.go index 9850b5a4f..967268f88 100644 --- a/pkg/commands/git_commands/repo_paths.go +++ b/pkg/commands/git_commands/repo_paths.go @@ -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 +} diff --git a/pkg/gui/controllers/helpers/repos_helper.go b/pkg/gui/controllers/helpers/repos_helper.go index 00cfbbd0a..b31185659 100644 --- a/pkg/gui/controllers/helpers/repos_helper.go +++ b/pkg/gui/controllers/helpers/repos_helper.go @@ -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 }