Skip to content

Commit

Permalink
Add context and error handling to git repository check function
Browse files Browse the repository at this point in the history
  • Loading branch information
milldr committed Jan 30, 2025
1 parent 7247d20 commit 11e0b71
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -611,10 +613,37 @@ func getConfigAndStacksInfo(commandName string, cmd *cobra.Command, args []strin

// isGitRepository checks if the current directory is within a git repository
func isGitRepository() bool {
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
cmd.Stderr = nil
// 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()
return err == nil

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

0 comments on commit 11e0b71

Please sign in to comment.