diff --git a/cmd/cloud.go b/cmd/cloud.go index b9c8bf89..29f4c725 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -378,7 +378,7 @@ Examples: IsRepo: true, } } else { - info, err := deployer.GetGitInfo(logger, dir) + info, err := deployer.GetGitInfoRecursive(logger, dir) if err != nil { logger.Debug("Failed to get git info: %v", err) } diff --git a/internal/deployer/metadata.go b/internal/deployer/metadata.go index 8b6b7b39..e82e8dc8 100644 --- a/internal/deployer/metadata.go +++ b/internal/deployer/metadata.go @@ -103,3 +103,43 @@ func GetGitInfo(logger logger.Logger, dir string) (*GitInfo, error) { return info, nil } + +// GetGitInfoRecursive walks up directories until it finds a git repo and returns its info +func GetGitInfoRecursive(logger logger.Logger, startDir string) (*GitInfo, error) { + depth := 0 + dir := startDir + for { + if depth >= 100 { + logger.Warn("Max depth reached while trying to find git dir") + return &GitInfo{}, nil + } + info, err := GetGitInfo(logger, dir) + if err != nil { + return nil, err + } + if info != nil && info.IsRepo { + return info, nil + } + + parent := parentDir(dir) + if parent == dir { + break + } + dir = parent + depth++ + } + return &GitInfo{}, nil +} + +// parentDir returns the parent directory of the given path +func parentDir(path string) string { + if path == "/" { + return path + } + cleaned := strings.TrimRight(path, "/") + idx := strings.LastIndex(cleaned, "/") + if idx <= 0 { + return "/" + } + return cleaned[:idx] +}