forked from toolshed/abra
35
pkg/git/dir.go
Normal file
35
pkg/git/dir.go
Normal file
@ -0,0 +1,35 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"coopcloud.tech/abra/pkg/log"
|
||||
)
|
||||
|
||||
func FindDir(dir string) string {
|
||||
dir, err := filepath.Abs(dir)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
realPath, err := filepath.EvalSymlinks(dir)
|
||||
if err != nil {
|
||||
log.Warn("unable to find git repo: broken symlink: %s", dir)
|
||||
return ""
|
||||
}
|
||||
|
||||
dir = realPath
|
||||
|
||||
if dir == os.ExpandEnv("$HOME/.abra") || dir == os.ExpandEnv("$ABRA_DIR") || dir == "/" {
|
||||
return ""
|
||||
}
|
||||
|
||||
p := path.Join(dir, ".git")
|
||||
if _, err := os.Stat(p); err == nil {
|
||||
return path.Dir(p)
|
||||
}
|
||||
|
||||
return FindDir(filepath.Dir(dir))
|
||||
}
|
Reference in New Issue
Block a user