From df4d46c84f6f8b08e35e193dd74877e5798fd446 Mon Sep 17 00:00:00 2001 From: Josh Medeski Date: Thu, 26 Sep 2024 19:50:14 -0500 Subject: [PATCH] feat: add git root support --- dir/dir.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/dir/dir.go b/dir/dir.go index b0e0d0a..0880e5c 100644 --- a/dir/dir.go +++ b/dir/dir.go @@ -41,8 +41,20 @@ func (d *RealDir) Dir(path string) (isDir bool, absPath string) { } func (d *RealDir) RootDir(path string) (hasRootDir bool, absPath string) { - isGit, commonDir, _ := d.git.GitCommonDir(path) - if isGit && strings.HasSuffix(commonDir, "/.bare") { + isGitBare, absPath := gitBareRootDir(d, path) + if isGitBare { + return true, absPath + } + isGit, absPath := gitRootDir(d, path) + if isGit { + return true, absPath + } + return false, "" +} + +func gitBareRootDir(d *RealDir, path string) (hasRootDir bool, absPath string) { + isGitBare, commonDir, _ := d.git.GitCommonDir(path) + if isGitBare && strings.HasSuffix(commonDir, "/.bare") { topLevelDir := strings.TrimSuffix(commonDir, "/.bare") relativePath := strings.TrimPrefix(path, topLevelDir) firstDir := strings.Split(relativePath, string("/"))[1] @@ -55,3 +67,12 @@ func (d *RealDir) RootDir(path string) (hasRootDir bool, absPath string) { return false, "" } } + +func gitRootDir(d *RealDir, path string) (hasDir bool, absPath string) { + isGit, topLevelDir, _ := d.git.ShowTopLevel(path) + if isGit && topLevelDir != "" { + return true, topLevelDir + } else { + return false, "" + } +}