AI-powered Git commit helper using Google Gemini
Project description
Git Auto: AI-Powered Commit Assistant
git-auto is a command-line tool designed to streamline your Git workflow. It leverages AI (Google Gemini) to analyze your staged code changes, perform local validation checks, generate informative Conventional Commit messages, and provide an interactive review process before committing.
✨ Features
- AI-Powered Commit Messages: Generates commit messages following the Conventional Commits specification based on your staged changes.
- AI Code Validation: Analyzes your diff using AI to identify potential issues, security concerns, and deviations from best practices within the changed lines.
- Local Validation: Performs pre-commit checks for common file types:
- Python (
py_compile) - JavaScript/TypeScript (
node --check, if Node.js is available) - JSON (Syntax validation, basic
package.jsonchecks) - YAML (Syntax validation via PyYAML, multi-document support, optional
yamllintintegration) - Dockerfile (Basic best practice checks)
- Shell Scripts (
shellcheckintegration if available, basic fallback checks) - TOML (Syntax validation via
toml, basicpyproject.tomlchecks) requirements.txt(Checks for unpinned dependencies)- Character Checks (Detects control characters, BOMs, and potentially problematic non-ASCII characters)
- Python (
- Data Infuscation (Sanitization): Protects sensitive data by replacing it with placeholders before sending code diffs to the AI. Supports:
- Built-in rules for common patterns (IPs, emails, keys, etc.).
- Custom regex patterns defined in an external file.
- Custom literal strings defined in an external file.
- Interactive Workflow: Provides a prompt to review AI suggestions, edit messages, view diffs, regenerate messages, or even revert the last commit before finalizing.
- Configurable: Customize behavior via a
.git_auto.yamlfile (project-specific or user-global) and environment variables. - Caching: Caches AI responses to speed up analysis of unchanged diffs and reduce API usage.
- Dynamic Context: Provides the AI with an overview of your repository structure for better context.
- Enhanced Output: Uses
richfor clear, formatted terminal output and Jinja2 for customizable output templates. - Git Hook Integration: Includes a command to easily install a pre-commit hook.
🚀 Installation
-
Prerequisites:
- Python 3.8+
- Git
- (Optional but Recommended)
pipxfor isolated CLI tool installation, or a Python virtual environment (venv). - (Optional)
yamllintandshellcheckinstalled and in your PATH for enhanced local validation.
-
Install via
pipx(Recommended for CLI tools):# Replace with the actual Git repository URL or PyPI name after publishing pipx install git+https://github.com/oxie/git-auto.git # Or after publishing: pipx install git-auto # To include optional yamllint dependency (if you have it installed): # pipx inject git-auto yamllint
-
Install via
pip(in a Virtual Environment):- Clone the repository:
# Replace with your actual repository URL git clone https://github.com/oxie/git-auto.git cd git-auto
- Create and activate a virtual environment:
python -m venv .venv source .venv/bin/activate # Linux/macOS # .\.venv\Scripts\activate # Windows
- Install the package:
- Basic:
pip install git-auto
- With optional YAML linter:
pip install "git-auto[linter]"
- For development:
pip install -e ".[dev,linter]"
- Basic:
- Clone the repository:
⚙️ Configuration
Configuration is loaded from multiple sources with the following precedence (highest first):
- Environment Variables
- Configuration File (
.git_auto.yaml) in the current Git repository root. - Configuration File (
.git_auto.yaml) in the user's home directory (~). - Configuration File (
config.yaml) in~/.config/git-auto/(XDG standard). - Default values defined in the tool.
1. API Key (Required)
You need a Google Gemini API key.
- Get one from Google AI Studio.
- Set via Environment Variable (Recommended):
export GEMINI_API_KEY='your-api-key'
(Add this to your.bashrc,.zshrc,.profile, or equivalent) - Set via Configuration File:
Add
api_key: your-api-keyto your.git_auto.yamlfile.
2. Configuration File (.git_auto.yaml)
Create this YAML file in your project root or home directory to customize settings.
Example .git_auto.yaml:
# AI Model Configuration
model: gemini-1.5-flash # Or gemini-1.5-pro, etc.
# API Key (Alternative to environment variable)
# api_key: your-api-key
# Diff Processing
max_diff_size: 4000 # Max characters of diff sent to AI (default: 4000)
# Custom Prompt Paths (Optional)
# validation_prompt_path: /path/to/my/validation_prompt.txt
# commit_prompt_path: /path/to/my/commit_prompt.txt
# Infuscation / Sanitization (Optional)
infuscation_patterns_file: .git_auto_secrets.txt # File with custom regex patterns
infuscation_literals_file: .git_auto_literals.txt # File with custom literal strings
# Output Templating (Optional)
# standard_output_template_path: /path/to/standard_output.j2
# minimal_output_template_path: /path/to/minimal_output.j2
# Caching Configuration
cache_enabled: true # Default: true
cache_dir: ~/.cache/git-auto # Default: User cache directory
cache_ttl_seconds: 604800 # Default: 604800 (1 week)
# AI Generation Parameters (Optional Fine-tuning)
generation_config_validation:
temperature: 0.2 # Default: 0.2 (Lower for more deterministic validation)
top_p: 0.95 # Default: 0.95
top_k: 40 # Default: 40
max_output_tokens: 1024 # Default: 1024
generation_config_commit:
temperature: 0.4 # Default: 0.4 (Slightly higher for commit message creativity)
top_p: 0.95 # Default: 0.95
top_k: 40 # Default: 40
max_output_tokens: 256 # Default: 256 (Shorter for commit messages)
# Repository Context (Optional Override - Usually generated automatically)
# repo_context: "Custom description of this project..."
3. Environment Variables
You can override any configuration file setting using environment variables:
GEMINI_API_KEY: Your API key.MODEL: AI model name (e.g.,gemini-1.5-pro).MAX_DIFF_SIZE: Max diff size in characters.VALIDATION_PROMPT_PATH: Path to custom validation prompt file.COMMIT_PROMPT_PATH: Path to custom commit prompt file.INFUSCATION_PATTERNS_FILE: Path to custom infuscation regex patterns file.INFUSCATION_LITERALS_FILE: Path to custom infuscation literal strings file.STANDARD_OUTPUT_TEMPLATE_PATH: Path to custom standard output Jinja2 template.MINIMAL_OUTPUT_TEMPLATE_PATH: Path to custom minimal output Jinja2 template.CACHE_ENABLED: Set tofalseor0to disable caching.CACHE_DIR: Path to the cache directory.CACHE_TTL_SECONDS: Cache time-to-live in seconds.VALIDATION_TEMP,VALIDATION_TOP_P,VALIDATION_TOP_K,VALIDATION_MAX_TOKENS: Validation generation parameters.COMMIT_TEMP,COMMIT_TOP_P,COMMIT_TOP_K,COMMIT_MAX_TOKENS: Commit generation parameters.REPO_CONTEXT: Override the automatically generated repository context string.
🚀 Usage
Core Workflow (commit command):
- Make changes to your code.
- Stage the changes you want to include in the commit:
git add <file1> <file2>... # or git add -A
- Run
git-autoinstead ofgit commit:git-auto commit
(You can add options like--infuscate,--minimal, etc.) - Review Output: The tool will display:
- Local validation results (if any issues are found).
- AI Validation Status (
PASSED,PASSED WITH WARNINGS,NEEDS REVISION). - AI Proposed Commit Message.
- Interactive Prompt: Choose your next action:
[Y]es: Accepts the proposed (or edited) commit message and performsgit commit.[N]o: Cancels the operation without committing.[E]dit: Opens your default Git editor ($EDITORorvim/nano) to modify the proposed commit message. After saving and closing, you'll be asked to confirm the edited message.[V]iew diff: Displays the full staged diff using your system's pager.[R]egenerate: Asks the AI to generate a new commit message based on the same diff (bypasses cache).[D]etails: (Only shown if AI validation found issues and--minimalis not used) Displays a detailed table of the issues identified by the AI.[Z]Revert last & retry: (Only shown if a previous commit exists) Performsgit reset --soft HEAD~1to undo the last commit (keeping changes staged) and restarts thegit-autoanalysis process. Use this if you accidentally committed too early or want to refine the previous commit with the current changes.
Command-Line Options (commit command):
--infuscate,-i: Enables data sanitization (built-in rules + custom patterns/literals) before sending the diff to the AI.--minimal,-m: Reduces output during the approval step, showing only the validation status line and the proposed commit message. Also suppressesINFOlevel log messages.--verbose,-v: Enables detailedDEBUGlevel logging, showing internal steps, full prompts sent to AI, raw AI responses, etc.--no-cache: Forces new calls to the AI, ignoring any previously cached responses for the current diff.--dry-run: Performs all analysis and generation steps but skips the finalgit commitexecution. Shows the commit message that would have been used.
Other Commands:
git-auto clear-cache: Finds the configured cache directory and interactively prompts to remove all cached AI responses.git-auto install-hook: Attempts to install the Git pre-commit hook script into the current repository's.git/hooks/pre-commitfile. Will prompt before overwriting an existing hook.git-auto --version: Displays the installed version of the tool.
✨ Features Deep Dive
AI Validation: The AI analyzes only the changed lines in your diff against general programming best practices, potential security concerns, and common configuration pitfalls. It assigns severity levels (CRITICAL, HIGH, MEDIUM, LOW) based on the potential impact of the changes. The goal is to catch significant issues before committing.
Commit Message Generation:
The AI generates messages following the Conventional Commits standard. It attempts to infer the appropriate type (feat, fix, chore, etc.) and scope based on the diff content and file paths. The subject line summarizes the primary action, and the body provides context based only on the diff.
Infuscation/Sanitization (--infuscate):
When enabled, this feature protects potentially sensitive information before it leaves your machine.
- Built-in Rules: Automatically detect and replace common patterns like IP addresses (excluding private ranges), email addresses, UUIDs, API keys, secrets/passwords/tokens found in common config formats, and hostnames.
- Custom Regex Patterns: Define your own regular expressions (one per line) in the file specified by
infuscation_patterns_file(e.g.,.git_auto_secrets.txt) to catch project-specific sensitive data formats. - Custom Literal Strings: Define exact strings (one per line) in the file specified by
infuscation_literals_file(e.g.,.git_auto_literals.txt) that should always be replaced. Longer literals are replaced before shorter ones to handle overlaps correctly. - Placeholders: Replaced data uses placeholders like
IP-1,API-KEY-abcdef12,LITERAL-1-1234abcd, etc. The original values are restored in the AI's response before it's displayed to you.
Local Validation: Before calling the AI, the tool runs quick local checks on modified files:
- Python: Checks syntax using
py_compile. - JS/TS: Checks syntax using
node --check(requires Node.js). - JSON: Validates JSON syntax. Checks for required
nameandversioninpackage.json. - YAML: Validates YAML syntax using PyYAML (handles multi-document files). Optionally uses
yamllintfor stricter checks if installed. - Dockerfile: Basic checks (e.g.,
apt-get updatewithoutrm, usinglatesttag,ADDwith URL). - Shell: Uses
shellcheckfor detailed linting if installed. Falls back to basic checks (missingset -euo pipefail, deprecated backticks ``). - TOML: Validates TOML syntax using
toml. Checks for required sections/keys inpyproject.toml. - requirements.txt: Warns about potentially unpinned dependencies (lines without version specifiers like
==,>=, etc.). - Characters: Detects non-ASCII printable characters (potential typos/homographs), control characters, and UTF-8 BOMs.
Caching:
AI responses are cached locally based on the AI model and the exact prompt sent (including the diff, context, etc.). This avoids repeated API calls for the same analysis. Configure via cache_enabled, cache_dir, cache_ttl_seconds, or bypass with --no-cache. Use git-auto clear-cache to empty the cache.
Dynamic Repo Context: The tool automatically scans your repository's directory structure (up to a certain depth, ignoring common temporary/build directories) and includes this overview in the prompt sent to the AI. This helps the AI understand the context of the changes better.
Output Formatting (Rich & Jinja2):
Terminal output uses the rich library for better readability (panels, tables, colors). The final approval screen is rendered using Jinja2 templates. You can customize the standard and minimal output formats by providing your own Jinja2 template files via the standard_output_template_path and minimal_output_template_path configuration options. The following variables are available in the template context:
validation_status: Raw status line (e.g., "VALIDATION: PASSED").validation_status_colored: Status line with ANSI colors.issues: Dictionary of issues keyed by severity ({'CRITICAL': [...], 'HIGH': [...]}).commit_message: Proposed commit message string.has_issues: Boolean indicating if validation found issues.minimal_mode: Boolean indicating if--minimalwas used.severity_order: List of severities for ordered iteration:["CRITICAL", "HIGH", "MEDIUM", "LOW", "PRACTICAL"].
↩️ Integrating with Git Hooks
Use git-auto install-hook to automatically add a pre-commit script to your current repository's .git/hooks/ directory. This script will run git-auto commit every time you execute git commit. If the tool cancels the commit (e.g., you select 'n' or an error occurs), the git commit process will be aborted.
Alternatively, configure it manually or using a framework like pre-commit.
🛠️ Development
- Install:
pip install -e ".[dev,linter]" - Run Tests:
pytest - Format & Lint:
ruff format . && ruff check --fix . - Type Check:
mypy git_auto/src
⚠️ Troubleshooting
ModuleNotFoundError: No module named 'git_auto': This usually means the package wasn't installed correctly after cloning or renaming. Ensure you have renamed the source directory togit_auto(with an underscore) and runpip uninstall git-auto git-auto-commit mycli -yfollowed bypip install .(orpip install -e .) from the project root.- API Key Errors: Ensure your
GEMINI_API_KEYis correctly set either as an environment variable or in your.git_auto.yamlfile and that the key is valid. - Configuration Not Loading: Check the search paths mentioned in the Configuration section. Ensure your file is named correctly (
.git_auto.yamlorconfig.yamlin the XDG path) and has valid YAML syntax. Environment variables always override file settings.
🤝 Contributing
(Placeholder - Add contribution guidelines if desired)
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file git_auto-0.1.3.tar.gz.
File metadata
- Download URL: git_auto-0.1.3.tar.gz
- Upload date:
- Size: 69.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f89eae62cd06e7c7372c0a1e2c81db7abe110e989e26ec036756950d76510825
|
|
| MD5 |
d011ab807da70843d73833e06a3c66d4
|
|
| BLAKE2b-256 |
a64f892652f6640b82783e55199b0467e9f27376c228fa78f63f340392a0f027
|
File details
Details for the file git_auto-0.1.3-py3-none-any.whl.
File metadata
- Download URL: git_auto-0.1.3-py3-none-any.whl
- Upload date:
- Size: 43.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f6936f443e37b972e8ed9c65d1d15067b3c263b4a1cd586e89af3050a276779
|
|
| MD5 |
32d7f73bb39738147f79a8a12fc3df42
|
|
| BLAKE2b-256 |
5fea569171aa8dc98bf50139348e9f465a12287e25fa4a7e82f92c38baaf5642
|