Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(DEV-2781): Notify Not Running in a Git Repo #990

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"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 @@ -523,6 +524,9 @@
u.LogErrorAndExit(err)
}

// Check if we're in a git repo. Warn if not.
verifyInsideGitRepo()

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 @@ -665,6 +669,36 @@
return info
}

// isGitRepository checks if the current directory is within a git repository

Check failure on line 672 in cmd/cmd_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/cmd_utils.go#L672

Comment should end in a period (godot)
Raw output
cmd/cmd_utils.go:672:1: Comment should end in a period (godot)
// isGitRepository checks if the current directory is within a git repository
^
func isGitRepository() bool {
_, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{

Check failure on line 674 in cmd/cmd_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/cmd_utils.go#L674

add-constant: string literal "." appears, at least, 4 times, create a named constant for it (revive)
Raw output
cmd/cmd_utils.go:674:37: add-constant: string literal "." appears, at least, 4 times, create a named constant for it (revive)
	_, err := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{
	                                   ^
DetectDotGit: true,
})
if err != nil {
if err != git.ErrRepositoryNotExists {

Check failure on line 678 in cmd/cmd_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/cmd_utils.go#L678

do not compare errors directly "err != git.ErrRepositoryNotExists", use "!errors.Is(err, git.ErrRepositoryNotExists)" instead (err113)
Raw output
cmd/cmd_utils.go:678:6: do not compare errors directly "err != git.ErrRepositoryNotExists", use "!errors.Is(err, git.ErrRepositoryNotExists)" instead (err113)
		if err != git.ErrRepositoryNotExists {
		   ^
Copy link
Preview

Copilot AI Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using errors.Is(err, git.ErrRepositoryNotExists) for error checking to correctly handle wrapped errors.

Suggested change
if err != git.ErrRepositoryNotExists {
if !errors.Is(err, git.ErrRepositoryNotExists) {

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
u.LogTrace(fmt.Sprintf("git check failed: %v", err))

Check failure on line 679 in cmd/cmd_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/cmd_utils.go#L679

SA1019: u.LogTrace is deprecated: Use `log.Debug` instead. This function will be removed in a future release. LogTrace logs the provided trace message (staticcheck)
Raw output
cmd/cmd_utils.go:679:4: SA1019: u.LogTrace is deprecated: Use `log.Debug` instead. This function will be removed in a future release. LogTrace logs the provided trace message (staticcheck)
			u.LogTrace(fmt.Sprintf("git check failed: %v", err))
			^
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Semantic logging

}
return false
}

return true
}

// verifyInsideGitRepo checks if we're in a git repo

Check failure on line 687 in cmd/cmd_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/cmd_utils.go#L687

Comment should end in a period (godot)
Raw output
cmd/cmd_utils.go:687:1: Comment should end in a period (godot)
// verifyInsideGitRepo checks if we're in a git repo
^
func verifyInsideGitRepo() bool {
// Skip check if either env var is set
if os.Getenv("ATMOS_BASE_PATH") != "" || os.Getenv("ATMOS_CLI_CONFIG_PATH") != "" {
return true
}

// Check if we're in a git repo
if !isGitRepository() {
u.LogWarning("You're not inside a git repository. Atmos feels lonely outside - bring it home!\n")

Check failure on line 696 in cmd/cmd_utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/cmd_utils.go#L696

SA1019: u.LogWarning is deprecated: Use `log.Warn` instead. This function will be removed in a future release. LogWarning logs the provided warning message (staticcheck)
Raw output
cmd/cmd_utils.go:696:3: SA1019: u.LogWarning is deprecated: Use `log.Warn` instead. This function will be removed in a future release. LogWarning logs the provided warning message (staticcheck)
		u.LogWarning("You're not inside a git repository. Atmos feels lonely outside - bring it home!\n")
		^
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move to the semantic logging.

return false
}
return true
}

func showErrorExampleFromMarkdown(cmd *cobra.Command, arg string) {
commandPath := cmd.CommandPath()
suggestions := []string{}
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 @@ -42,3 +42,21 @@ tests:
expect:
diff: []
exit_code: 0

- name: atmos warns if not in git repo
enabled: true
snapshot: true
description: "Test that atmos warns if not run inside of a git repo"
workdir: "/"
command: "atmos"
args:
- version
env:
ATMOS_LOGS_LEVEL: Warning
expect:
diff: []
stdout:
- "You're not inside a git repository. Atmos feels lonely outside - bring it home!"
stderr:
- "^$"
exit_code: 0
Loading