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 3 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
51 changes: 51 additions & 0 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package cmd

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -607,3 +610,51 @@ func getConfigAndStacksInfo(commandName string, cmd *cobra.Command, args []strin
}
return info
}

// isGitRepository checks if the current directory is within a git repository
func isGitRepository() bool {
// Create command with timeout context
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

cmd := exec.CommandContext(ctx, "git", "rev-parse", "--is-inside-work-tree")

// Capture stderr for debugging
var stderr bytes.Buffer
cmd.Stderr = &stderr

// Run the command
err := cmd.Run()

// Check for timeout
if ctx.Err() == context.DeadlineExceeded {
u.LogTrace(atmosConfig, "git check timed out after 3 seconds")
return false
}

// Check if git is not installed
if errors.Is(err, exec.ErrNotFound) {
u.LogTrace(atmosConfig, "git is not installed")
return false
}

if err != nil {
u.LogTrace(atmosConfig, fmt.Sprintf("git check failed: %v (stderr: %s)", err, stderr.String()))
return false
}

return true
}

// checkGitAndEnvVars checks if we're in 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
if !isGitRepository() {
u.LogWarning(atmosConfig, "You're not inside a git repository. Atmos feels lonely outside - bring it home!")
}
}
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func Execute() error {
u.LogErrorAndExit(schema.AtmosConfiguration{}, initErr)
}
}

// 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