Skip to content

Conversation

@DFanso
Copy link
Owner

@DFanso DFanso commented Oct 5, 2025

Description

Handle .env load errors gracefully

Type of Change

  • Changes godotenv.Load() to ignore errors, allowing

  • fallback to system environment variables for

  • robustness in various environments.

  • Bug fix (non-breaking change which fixes an issue)

  • New feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

  • Documentation update

  • Code refactoring

  • Performance improvement

  • Other (please describe):

Related Issue

Fixes #(issue number)

Changes Made

  • Changes godotenv.Load() to ignore errors, allowing
  • fallback to system environment variables for
  • robustness in various environments.

Testing

  • Tested with Gemini API
  • Tested with Grok API
  • Tested on Windows
  • Tested on Linux
  • Tested on macOS
  • Added/updated tests (if applicable)

Checklist

  • My code follows the project's code style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have tested this in a real Git repository
  • I have read the CONTRIBUTING.md guidelines

Screenshots (if applicable)

Additional Notes


For Hacktoberfest Participants

  • This PR is submitted as part of Hacktoberfest 2025

Thank you for your contribution! 🎉

Summary by CodeRabbit

  • Bug Fixes

    • Running without a .env file no longer causes errors; environment variables still work when available.
    • Clearer early exit when no changes are detected.
  • Refactor

    • Streamlined startup sequence for more reliable configuration and repository validation.
    • Improved ordering of stats collection, commit message generation, and UI feedback for a smoother experience.
    • More consistent spinner, output, and copy-to-clipboard flow.

- Changes godotenv.Load() to ignore errors, allowing
- fallback to system environment variables for
- robustness in various environments.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 5, 2025

Walkthrough

The main program restructures initialization and control flow in cmd/commit-msg/main.go: .env loading is made non-fatal, COMMIT_LLM validation occurs earlier, repository checks and config initialization are reordered, generator dispatch is moved near spinner start, and UI/clipboard steps are resequenced. No public API changes.

Changes

Cohort / File(s) Summary
Initialization and environment handling
cmd/commit-msg/main.go
Makes godotenv.Load() non-fatal; reorders early setup to validate COMMIT_LLM before repository checks; adjusts creation order of repoConfig and config.
Change detection and flow control
cmd/commit-msg/main.go
Clarifies early returns when no changes; preserves stats retrieval/display with minor sequencing adjustments.
Generator dispatch and spinner
cmd/commit-msg/main.go
Starts spinner before generator selection; moves switch for LLM dispatch earlier; keeps ollama handling and default paths with slight scope changes; consistent spinner error handling.
UI output and clipboard
cmd/commit-msg/main.go
Reorders commit message display and copy-to-clipboard steps; aligns final changes preview with new sequence.
Formatting and structure
cmd/commit-msg/main.go
Minor re-indentation and block reordering without altering external interfaces.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant User
  participant CLI as commit-msg CLI
  participant Env as .env Loader
  participant Repo as Git Repo
  participant Gen as LLM Generator
  participant CB as Clipboard

  User->>CLI: Run commit-msg
  CLI->>Env: Load .env (non-fatal)
  Note right of Env: Ignore failure

  CLI->>CLI: Read COMMIT_LLM / API keys
  CLI->>CLI: Validate COMMIT_LLM and required keys
  CLI->>Repo: Validate repository state
  Repo-->>CLI: OK / No changes

  alt No changes
    CLI-->>User: Exit early (nothing to commit)
  else Changes present
    CLI->>CLI: Collect file stats/changes
    CLI->>CLI: Start spinner
    CLI->>Gen: Dispatch by LLM (ollama/grok/chatgpt/claude/gemini)
    Gen-->>CLI: Generate commit message
    CLI->>CLI: Stop spinner
    CLI-->>User: Display commit message
    CLI->>CB: Copy to clipboard (if enabled)
    CB-->>CLI: Success/Failure
    CLI-->>User: Show changes preview
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

hacktoberfest, go

Poem

I twitched my nose at envs that fail, no fright—
Now flows hop neatly, left to right.
A spinner spins, the LLMs align,
Commit words brewed, concise, divine.
I copy-paste with paws so light—
Carrots for code, and messages tight! 🥕✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly and concisely summarizes the primary change—updating .env loading to ignore errors—without extraneous details, making it immediately obvious to reviewers what the PR accomplishes. It is directly tied to the main code adjustments for godotenv.Load() and reflects the intent to improve robustness across environments. The phrasing is specific enough to convey the core update without listing implementation steps or modules.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch dev

Comment @coderabbitai help to get the list of available commands and usage tips.

@DFanso DFanso self-assigned this Oct 5, 2025
@DFanso DFanso added bug Something isn't working hacktoberfest Eligible for Hacktoberfest hacktoberfest-accepted Approved Hacktoberfest contribution go Pull requests that update go code labels Oct 5, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
cmd/commit-msg/main.go (2)

56-57: Enhance error message with supported values.

The error message for invalid COMMIT_LLM values would be more helpful if it listed the supported options.

Apply this diff to improve the error message:

 	default:
-		log.Fatalf("Invalid COMMIT_LLM value: %s", commitLLM)
+		log.Fatalf("Invalid COMMIT_LLM value: %s. Supported values: gemini, grok, chatgpt, claude, ollama", commitLLM)

124-144: Make grok case explicit in switch statement.

The grok case is currently handled by the default clause. While functionally correct (since invalid values are caught earlier), this pattern is confusing and fragile. An explicit case for grok would improve code clarity and maintainability.

Apply this diff to make the grok case explicit:

 	var commitMsg string
 	switch commitLLM {
 	case "gemini":
 		commitMsg, err = gemini.GenerateCommitMessage(config, changes, apiKey)
 	case "chatgpt":
 		commitMsg, err = chatgpt.GenerateCommitMessage(config, changes, apiKey)
 	case "claude":
 		commitMsg, err = claude.GenerateCommitMessage(config, changes, apiKey)
+	case "grok":
+		commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey)
 	case "ollama":
 		url := os.Getenv("OLLAMA_URL")
 		if url == "" {
 			url = "http://localhost:11434/api/generate"
 		}
 		model := os.Getenv("OLLAMA_MODEL")
 		if model == "" {
 			model = "llama3:latest"
 		}
 		commitMsg, err = ollama.GenerateCommitMessage(config, changes, url, model)
 	default:
-		commitMsg, err = grok.GenerateCommitMessage(config, changes, apiKey)
+		// This should be unreachable since we validate commitLLM earlier
+		log.Fatalf("Unexpected COMMIT_LLM value: %s", commitLLM)
 	}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4082d3a and 9547765.

📒 Files selected for processing (1)
  • cmd/commit-msg/main.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
cmd/commit-msg/main.go (9)
internal/git/operations.go (2)
  • IsRepository (16-23)
  • GetChanges (26-130)
pkg/types/types.go (2)
  • Config (4-7)
  • RepoConfig (10-13)
internal/stats/statistics.go (1)
  • GetFileStatistics (14-74)
internal/display/display.go (3)
  • ShowFileStatistics (20-96)
  • ShowCommitMessage (99-115)
  • ShowChangesPreview (118-133)
internal/chatgpt/chatgpt.go (1)
  • GenerateCommitMessage (13-32)
internal/claude/claude.go (1)
  • GenerateCommitMessage (33-85)
internal/ollama/ollama.go (1)
  • GenerateCommitMessage (24-75)
internal/gemini/gemini.go (1)
  • GenerateCommitMessage (13-44)
internal/grok/grok.go (1)
  • GenerateCommitMessage (15-87)
🔇 Additional comments (3)
cmd/commit-msg/main.go (3)

24-26: LGTM! Non-fatal .env loading improves robustness.

The change to silently ignore .env loading errors allows the application to gracefully fall back to system environment variables, improving portability across different deployment environments.


60-79: LGTM! Logical initialization order.

The reordering of initialization steps makes sense: environment validation occurs first, followed by filesystem and git repository checks. This fail-fast approach improves user experience by catching configuration errors early.


146-169: LGTM! Proper error handling and user feedback.

The error handling, success messaging, and clipboard operations are well-implemented with appropriate user feedback for both success and failure cases.

@DFanso DFanso merged commit 64baf24 into main Oct 5, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code hacktoberfest Eligible for Hacktoberfest hacktoberfest-accepted Approved Hacktoberfest contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants