This file contains essential information for agents working on this codebase.
auto-commit is a Python utility that detects changed files in a git repository, collects their diffs, and automatically generates commit messages using AI. Users can choose between Google Gemini (via google-genai), Z.AI GLM Coding Plan (OpenAI-compatible), or OpenAI.
# Install dependencies
pip install -r requirements.txt
# Activate virtual environment (if present)
source .venv/bin/activate# Direct execution
python3 auto-commit.py
# Or via installed executable (when set up)
autocommit
# With options
autocommit --provider gemini --lang Deutsch --model gemini-2.5-flash
autocommit --provider zai --model GLM-4.6 --zai-base-url https://api.z.ai/api/coding/paas/v4
autocommit --provider openai --model gpt-5.2--lang: Commit message language--provider: AI provider (gemini,zai, oropenai)--model: Model name for the selected provider--zai-base-url: Custom base URL for Z.AI Coding API--openai-base-url: Custom base URL for OpenAI--style: Commit style (sarcastic,humorous, orstandard- default)
auto-commit.py- Main script (single-file utility)requirements.txt- Python dependenciesenv.example- Environment configuration template.env- Local environment configuration (not tracked in git).venv/- Virtual environment (not tracked in git)
find_git_root()- Locates git repository rootget_staged_and_deleted_files()- Separates staged files from deleted filesget_diff_for_file()- Retrieves staged changes for a specific filecreate_ai_clients()- Initializes AI provider clientsgenerate_commit_message()- Generates commit message using AIprompt_to_stage()- Interactive prompt for staging fileswrite_commit_template()- Writes commit message + context to temp filemain()- Main execution flow
- Uses
python-dotenvto load.envfile - Configuration defaults defined at top of script (lines 16-29)
- CLI arguments override
.envvalues - Provider selection via
AI_PROVIDERenvironment variable or--providerflag
gitpython- Git repository operationsgoogle-genai- Google Gemini API clientopenai- OpenAI API client (also used for Z.AI compatibility)python-dotenv- Environment variable management
- Uses
google-genailibrary - Default model:
gemini-2.5-flash - Configuration:
GEMINI_API_KEY,GEMINI_MODEL
- Uses OpenAI-compatible API
- Endpoint:
https://api.z.ai/api/coding/paas/v4(Coding API, not General API) - Recommended models:
GLM-4.7,GLM-4.6,GLM-4.5,GLM-4.5-air - Configuration:
ZAI_API_KEY,ZAI_MODEL,ZAI_BASE_URL
- Uses OpenAI Python SDK (
chat.completions) - Default model:
gpt-5.2 - Configuration:
OPENAI_API_KEY,OPENAI_MODEL, optionalOPENAI_BASE_URL
- Python 3 with type hints throughout
- German language in user-facing strings, comments, and documentation
- Function docstrings in German
- Use of descriptive function names (e.g.,
get_staged_and_deleted_files)
- Constants:
UPPER_SNAKE_CASE(e.g.,DEFAULT_LANGUAGE,AI_PROVIDER) - Functions:
snake_case(e.g.,generate_commit_message) - Variables:
snake_case(e.g.,commit_message,file_diffs)
- Custom exception:
CommitGenerationErrorfor AI generation failures - Catches 429/RESOURCE_EXHAUSTED errors and raises custom error
- Falls back to
"chore: update changes"on non-critical errors - Explicit
ValueErrorfor missing configuration
- Uses both
GitPythonlibrary andsubprocessfor git operations get_staged_and_deleted_files()separates deleted files to handle them correctly- Untracked files added with
--allflag:repo.git.add(all=True) - Modified files added explicitly:
repo.git.add(files) - Always checks for
originremote before attempting push
- Commit message prompt includes file paths and diffs
- Enforces short summary line (max 72 characters)
- Instructs against markdown formatting (no ``` or headers)
- Reduces multiple newlines to double newlines (regex:
r"\n{3,}")
- Uses
$EDITORenvironment variable, defaults tovim - Creates temp file with commit template containing:
- Generated commit message
- Comments listing modified and deleted files
- Full diffs for context
- Removes comment lines when reading final message
- Deletes temp file in
finallyblock
- Interactive prompts for staging files (y/n)
- Confirmation prompt before committing
- Checks if final commit message is empty before proceeding
- Controlled by
--styleCLI option (default:standard) - Supported styles:
standard,sarcastic,humorous - When
sarcasticorhumorousis selected:- Enhanced prompt with style-specific instructions
- Requests dry, subtle sarcasm or humor
- Requires staying technically accurate, understandable, and git-conform
- Prohibits silliness and memes
- Emojis are allowed in all commit styles
- Default mode prompt remains unchanged
- Style is passed to
generate_commit_message()ascommit_styleparameter
No tests currently present in this codebase.
If adding tests, consider:
- Mocking Git operations and AI clients
- Testing file staging logic
- Testing prompt generation and message formatting
- Testing error handling paths
Copy env.example to .env and configure:
cp env.example .envRequired variables (depending on provider):
AI_PROVIDER- Choosegemini,zai, oropenai- Provider-specific API key
COMMIT_LANGUAGE- Default commit message language
- Detect untracked and unstaged modified files
- Prompt user to stage files (interactive y/n)
- Collect diffs from staged files
- Generate commit message using selected AI provider
- Open editor with commit message template
- User edits and confirms (y/n)
- Execute git commit
- Push to origin if remote exists
- Uses
cast(str, item.a_path)when processing unstaged files to satisfy type checker
- Z.AI and OpenAI both use OpenAI-compatible client
- Provider selection happens at runtime based on configuration
- Default to Gemini if invalid provider specified
- All user-facing text is in German
- Error messages in German
- Comments and docstrings in German
- Only commit message language is configurable
- Collects diffs for both modified and deleted files
- Deleted files included in
file_diffsdict for AI context - Uses
--cachedflag for staged changes only