This repository contains a prepare-commit-msg Git hook script that
automatically drafts commit messages using an OpenAI-compatible language model
(LLM). The script analyzes staged changes and generates a concise,
well-structured commit message following best practices.
- Generates a commit message with:
- A 1-line summary in the imperative mood (max 50 characters).
- A detailed body explaining the changes, reasons, and context.
- Proper formatting with line wrapping at 75 characters.
- Supports testing mode to generate a draft for an existing commit.
- Includes token usage information from the LLM in the draft message.
-
Copy the
prepare-commit-msgscript to your repository's.git/hooks/directory:cp prepare-commit-msg /path/to/your/repo/.git/hooks/
-
Make the script executable:
chmod +x /path/to/your/repo/.git/hooks/prepare-commit-msg
The script requires the following environment variables to be set:
LLM_API_ENDPOINT: The base URL of the OpenAI-compatible API (e.g.,https://api.model.com/v1).LLM_API_KEY: The API key used as a bearer token for authentication.LLM_MODEL: The name of the model to use (e.g.,gpt-3.5-turbo).
When committing changes, the script will automatically:
- Analyze the staged changes.
- Generate a draft commit message.
- Prepend the draft to the existing commit message file.
To test the script with an existing commit, use the -t option followed by
the commit hash:
prepare-commit-msg -t <commit-hash>This will:
- Retrieve the original commit message and diff for the specified commit.
- Generate a new draft commit message.
- Print both the original and the draft commit messages to the console.
- If the required environment variables are not set, the script will log an error but will not block the commit process.
- The script uses only Python's standard library, ensuring minimal dependencies.
This project is licensed under the MIT License. See the LICENSE file for details.