Skip to content

Commit

Permalink
check for the root of the git project
Browse files Browse the repository at this point in the history
  • Loading branch information
milldr committed Jan 30, 2025
1 parent 14493bb commit f6379fa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
26 changes: 16 additions & 10 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/cloudposse/atmos/pkg/ui/theme"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/cloudposse/atmos/pkg/version"
"github.com/go-git/go-git/v5"
)

// ValidateConfig holds configuration options for Atmos validation.
Expand Down Expand Up @@ -459,6 +458,9 @@ func printMessageForMissingAtmosConfig(atmosConfig schema.AtmosConfiguration) {
u.LogErrorAndExit(atmosConfig, err)
}

// Check if we're in a git repo and report a warning if not
checkGitAndEnvVars()

if atmosConfig.Default {
// If Atmos did not find an `atmos.yaml` config file and is using the default config
u.PrintMessageInColor("atmos.yaml", c1)
Expand Down Expand Up @@ -609,15 +611,19 @@ func getConfigAndStacksInfo(commandName string, cmd *cobra.Command, args []strin
return info
}

// isGitRepository checks if the current directory is within a git repository
// isGitRepository checks if the current directory is the root of a git repository
func isGitRepository() bool {
// Create a new repository instance pointing to the current directory
_, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{
DetectDotGit: true,
})
// Get current working directory
currentDir, err := os.Getwd()
if err != nil {
u.LogTrace(atmosConfig, fmt.Sprintf("failed to get current directory: %v", err))
return false
}

// Check if .git directory exists directly in the current directory
_, err = os.Stat(filepath.Join(currentDir, ".git"))
if err != nil {
if err != git.ErrRepositoryNotExists {
if !os.IsNotExist(err) {
u.LogTrace(atmosConfig, fmt.Sprintf("git check failed: %v", err))
}
return false
Expand All @@ -626,15 +632,15 @@ func isGitRepository() bool {
return true
}

// checkGitAndEnvVars checks if we're in a git repo and if required env vars are set
// checkGitAndEnvVars checks if we're at the root of a git repo and if required env vars are set
func checkGitAndEnvVars() {
// Skip check if either env var is set
if os.Getenv("ATMOS_BASE_PATH") != "" || os.Getenv("ATMOS_CLI_CONFIG_PATH") != "" {
return
}

// Check if we're in a git repo
// Check if we're at the root of a git repo
if !isGitRepository() {
u.LogWarning(atmosConfig, "You're not inside a git repository. Atmos feels lonely outside - bring it home!")
u.LogWarning(atmosConfig, "You're not at the root of a git repository. Atmos feels lonely outside - bring it home!\n")
}
}
3 changes: 0 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ func Execute() error {
}
}

// Check if we're in a git repo and report a warning if not
checkGitAndEnvVars()

var err error
// If CLI configuration was found, process its custom commands and command aliases
if initErr == nil {
Expand Down
18 changes: 18 additions & 0 deletions tests/test-cases/empty-dir.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,21 @@ tests:
stderr:
- "^$"
exit_code: 0

- name: run atmos outside git repo
enabled: true
snapshot: true
description: "Test checkGitAndEnvVars function outside of a git repo with ATMOS_LOGS_LEVEL=Warn"
workdir: "fixtures/scenarios/empty-dir"
command: "atmos"
args:
- version
env:
ATMOS_LOGS_LEVEL: Warning
expect:
diff: []
stdout:
- "You're not at the root of a git repository. Atmos feels lonely outside - bring it home!"
stderr:
- "^$"
exit_code: 0
2 changes: 1 addition & 1 deletion tests/test-cases/log-level-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,4 @@ tests:
- '^\n👽 Atmos (\d+\.\d+\.\d+|test) on [a-z]+/[a-z0-9_]+\n\n'
stderr:
- "^$"
exit_code: 0
exit_code: 0

0 comments on commit f6379fa

Please sign in to comment.