Skip to content

Commit

Permalink
feat: add git root support
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmedeski committed Sep 27, 2024
1 parent 008312f commit df4d46c
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions dir/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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, ""
}
}

0 comments on commit df4d46c

Please sign in to comment.