Skip to content

11 read api key from env file - #12

Merged
nazarli-shabnam merged 6 commits into
mainfrom
11-read-api-key-from-env-file
Apr 17, 2026
Merged

11 read api key from env file#12
nazarli-shabnam merged 6 commits into
mainfrom
11-read-api-key-from-env-file

Conversation

@nazarli-shabnam

@nazarli-shabnam nazarli-shabnam commented Apr 13, 2026

Copy link
Copy Markdown
Owner

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:

  • Added _load_ai_env_from_dotenv to load only AI-related environment variables (GEMINI_API_KEY, GOOGLE_API_KEY, GEMINI_MODEL) from a .env file at the git repo root, overriding process values without affecting unrelated variables. This is now called automatically if a .env file is present in the repo. [1] [2] [3] [4] [5]
  • Added comprehensive tests for _load_ai_env_from_dotenv, covering correct variable loading, overriding, and ignoring empty values.

Commit Message Heuristics:

  • Improved suggest_from_changes to 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:

  • Changed the CLI prompt to default to "yes" for applying suggested commands and removed the "auto" option from the prompt, simplifying user interaction.
  • Cleaned up and clarified the README to focus on conventional usage, AI setup, and streamlined install/run instructions.

Testing and Compatibility:

  • Added and improved tests for CLI selection parsing, grouping logic (including Windows-style paths), and edge cases in the CLI utility functions. [1] [2]
  • Added Python 3.13 to the supported versions in pyproject.toml.

Other Notable Changes:

@nazarli-shabnam nazarli-shabnam self-assigned this Apr 13, 2026
@nazarli-shabnam nazarli-shabnam added the enhancement New feature or request label Apr 13, 2026
@nazarli-shabnam nazarli-shabnam linked an issue Apr 13, 2026 that may be closed by this pull request
Copilot AI review requested due to automatic review settings April 13, 2026 07:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 .env from the resolved repo_root in the CLI before AI usage.
  • Refresh README usage/docs (including .env placement guidance) and bump version to 2.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.

Comment thread git_explain/cli.py
Comment on lines +311 to +314
repo_env = repo_root / ".env"
if repo_env.is_file():
load_dotenv(dotenv_path=repo_env, override=False)

Copilot AI Apr 13, 2026

Copy link

Choose a reason for hiding this comment

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

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).

Copilot uses AI. Check for mistakes.
Comment thread git_explain/cli.py
Comment on lines +311 to +314
repo_env = repo_root / ".env"
if repo_env.is_file():
load_dotenv(dotenv_path=repo_env, override=False)

Copilot AI Apr 13, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment thread git_explain/cli.py
Comment on lines +311 to +314
repo_env = repo_root / ".env"
if repo_env.is_file():
load_dotenv(dotenv_path=repo_env, override=False)

Copilot AI Apr 13, 2026

Copy link

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment thread git_explain/cli.py
Comment thread README.md

@nazarli-shabnam nazarli-shabnam left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

- 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)
@nazarli-shabnam
nazarli-shabnam merged commit 971ed7f into main Apr 17, 2026
9 checks passed
nazarli-shabnam added a commit that referenced this pull request Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

read api key from env file

2 participants