# AI-Powered Git Commit Message Generator This tool automatically generates Git commit messages based on your staged changes using the Google Gemini API. It analyzes the diff of your staged files and suggests a commit message following conventional commit standards. ## Features - Analyzes staged Git diffs. - Generates commit messages (subject and body) using the Gemini API. - Follows standard Git commit message conventions (imperative mood, character limits, separation of subject/body). - Runs in a Docker container for a consistent environment. - Prompts for user confirmation before creating the commit. ## Prerequisites - **Git:** Must be installed on your host machine. - **Docker:** Must be installed and running on your host machine. - **Gemini API Key:** You need an API key from Google Cloud. ## Setup 1. **Build the Docker Image:** ```bash docker build -t docker.senomas.com/commit:1.0 . ``` _(Note: The tag `docker.senomas.com/commit:1.0` is used in the `ai-commit.sh` script. You can change it if needed, but make sure to update the script accordingly.)_ 2. **Set Environment Variable:** Export your Gemini API key as an environment variable: ```bash export GEMINI_API_KEY='YOUR_API_KEY' ``` _(Replace `YOUR_API_KEY` with your actual key. You might want to add this line to your shell profile (e.g., `.bashrc`, `.zshrc`, `config.fish`) for persistence.)_ ## Usage 1. **Stage your changes:** Use `git add` to stage the files you want to include in the commit. 2. **Run the script:** * **For a new commit:** Execute the `ai-commit.sh` script from the root of your Git repository: ```bash ./ai-commit.sh ``` * **To amend the previous commit:** Use the `-a` or `--amend` flag: ```bash ./ai-commit.sh -a # or ./ai-commit.sh --amend ``` 3. **Review and Confirm:** The script will: - Fetch the staged diff (for a new commit) or the diff representing the changes to be amended (for an amend). - Generate a commit message using the Gemini API. - Print the generated message. - Ask for your confirmation (`y/n`). 4. **Commit:** If you confirm ('y'), the script will create the commit using the generated message. If you decline ('n'), the commit will be aborted. ## How it Works 1. The `ai-commit.sh` script runs the `git_commit_ai.py` script inside a Docker container (`docker.senomas.com/commit:1.0`). 2. It mounts the current directory (`pwd`) as `/repo` inside the container, allowing the script to access your Git repository. 3. It mounts your `.gitconfig` and `.git-credentials` (read-only) for Git operations within the container. 4. It passes the `GEMINI_API_KEY` environment variable into the container. 5. It forwards any arguments passed to `ai-commit.sh` (like `-a` or `--amend`) to the Python script inside the container. 6. The `git_commit_ai.py` script: - Parses command-line arguments (e.g., `--amend`). - Retrieves the staged diff using `git diff --staged`. - Sends the diff to the Gemini API with a prompt asking for a commit message. - Receives the generated message. - Prompts the user for confirmation (mentioning if it's an amend). - If confirmed, runs `git commit -m "..."` or `git commit --amend -m "..."` with the generated message. ## Files - `git_commit_ai.py`: The main Python script that interacts with Git and the Gemini API. - `Dockerfile`: Defines the Docker image environment. - `ai-commit.sh`: A helper script to easily run the tool in Docker. - `README.md`: This file.