11 read api key from env file - #12
Conversation
There was a problem hiding this comment.
Pull request overview
Adds support for loading Gemini API credentials from a .env file located at the target repository’s git root, and updates packaging/docs to reflect the new behavior and release.
Changes:
- Load
.envfrom the resolvedrepo_rootin the CLI before AI usage. - Refresh README usage/docs (including
.envplacement guidance) and bump version to2.3.0. - Advertise Python 3.13 support via PyPI classifier.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| README.md | Updates usage instructions and documents .env placement + interaction changes. |
| pyproject.toml | Adds Python 3.13 classifier. |
| git_explain/cli.py | Loads .env from repo root; changes apply prompt behavior. |
| git_explain/init.py | Version bump to 2.3.0. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| repo_env = repo_root / ".env" | ||
| if repo_env.is_file(): | ||
| load_dotenv(dotenv_path=repo_env, override=False) | ||
|
|
There was a problem hiding this comment.
The new repo_root/.env load is likely ineffective because this module already calls load_dotenv() at import time (which can load a different .env based on the current working directory). Since override=False here, any variables loaded earlier (e.g. from a subdir .env) will prevent the repo-root key from being applied, contradicting the README’s “git root” behavior. Consider removing the import-time load_dotenv() and only loading from repo_root (keeping override=False so shell env still wins).
| repo_env = repo_root / ".env" | ||
| if repo_env.is_file(): | ||
| load_dotenv(dotenv_path=repo_env, override=False) | ||
|
|
There was a problem hiding this comment.
Loading the repo’s .env with load_dotenv() will import all variables from that file into the process environment, which can unexpectedly affect subsequent subprocess calls (e.g., git) beyond just GEMINI_* settings. Since the feature goal is “read API key from .env”, consider reading the file (e.g., via dotenv_values) and only populating GEMINI_API_KEY/GOOGLE_API_KEY (and possibly GEMINI_MODEL) when they aren’t already set.
| repo_env = repo_root / ".env" | ||
| if repo_env.is_file(): | ||
| load_dotenv(dotenv_path=repo_env, override=False) | ||
|
|
There was a problem hiding this comment.
This new .env resolution behavior is subtle (repo_root vs CWD precedence, and “shell overrides .env”). There’s existing pytest coverage for cli helpers, but no test validating that GEMINI_API_KEY is picked up from repo_root/.env and that an already-set environment variable is not overridden. Adding a focused unit/integration test would help prevent regressions.
- Selection parser robustness for out-of-range/range input - Grouping precedence (tests bucket wins over generic code for test-like filenames) - Windows-style path handling in grouping logic - .env loader behavior with blank values (does not clobber existing env values)
…file 11 read api key from env file

This pull request introduces several improvements and refactorings to
git-explain, focusing on AI environment variable handling, improved commit message heuristics, CLI usability, and test coverage. The most important changes are summarized below:AI Environment Variable Handling:
_load_ai_env_from_dotenvto load only AI-related environment variables (GEMINI_API_KEY,GOOGLE_API_KEY,GEMINI_MODEL) from a.envfile at the git repo root, overriding process values without affecting unrelated variables. This is now called automatically if a.envfile is present in the repo. [1] [2] [3] [4] [5]_load_ai_env_from_dotenv, covering correct variable loading, overriding, and ignoring empty values.Commit Message Heuristics:
suggest_from_changesto generate more compact and relevant commit messages when many code files are changed under a single root directory (e.g., "6 modules under api" instead of listing all folders), and to handle multiple roots more gracefully. [1] [2] [3]CLI Usability and Behavior:
Testing and Compatibility:
pyproject.toml.Other Notable Changes:
closes read api key from env file #11