A simple Python tool that transforms raw commit messages into Conventional (Semantic) Commit messages using the new OpenAI Python API (v1) interface.
- Overview
- Features
- Requirements
- Installation
- Setup
- Usage
- Example
- Git Integration (Optional)
- Troubleshooting
This tool leverages the OpenAI Chat Completion API to parse your plain-text commit messages and convert them into the Conventional Commits format. It helps maintain a clean, consistent, and parseable commit history in your repository.
- Automatic Classification into types like
feat,fix,docs, etc. - Short, Imperative Subject Lines following the
<type>(<scope>): <subject>format. - Optional body or
BREAKING CHANGEfooter for major updates. - Lightweight single-file Python script.
- Uses
python-dotenvto keep your OpenAI API key out of source control.
- Python 3.8+
- Pip for installing dependencies
- An OpenAI API Key (sign up here if you don’t have one)
- Clone this repository or place the
rewrite_commit_message.pyscript in your project. - Install the required dependencies:
pip install --upgrade openai pip install python-dotenv
- Create a
.envfile (in the same folder asrewrite_commit_message.py) containing your OpenAI API key:OPENAI_API_KEY=sk-1234_your_secret_key
- Ignore
.envin your.gitignoreto avoid committing it:echo ".env" >> .gitignore
-
Run the script with a raw commit message as an argument. The script will output a Conventional Commit-formatted message:
python rewrite_commit_message.py "my raw commit message here" -
Use the output in an actual
git commitcommand:git commit -m "$(python rewrite_commit_message.py 'my raw commit message here')"
$ python rewrite_commit_message.py "added a new pipeline validator and simulator"
feat(pipeline): add validator and simulatorExample raw input:
"fixing the user login bug that caused random timeouts"
Example output:
fix(auth): resolve user login timeout issue
Want to fully automate this process? You can integrate the script into a Git hook so every commit message is auto-converted. For example, a simple commit-msg hook:
- Copy the
commit-msgfile into your.git/hooks/(must be executable). - Replace the
PATH/TO/YOUR/rewrite_commit_message.pywith the actual path torewrite_commit_message.py - Now, whenever you run
git commit -m "some random message", the hook calls OpenAI to rewrite it automatically.
Note: This approach will invoke the OpenAI API on every commit, which could incur usage costs. Consider setting usage limits in your OpenAI dashboard.
OPENAI_API_KEY not set- Ensure
.envis in the same directory and contains a validOPENAI_API_KEY. - Make sure you’re calling
load_dotenv()before accessingos.getenv(...).
- Ensure
- Version Mismatch
- Double-check you installed
openai>=1.0.0. - Run
pip show openaito confirm the version is correct.
- Double-check you installed
- Multiple Python Environments
- Ensure you’re installing dependencies in the same environment you’re running the script from.
- Billing / Usage
- The script uses the OpenAI API on each invocation, so check your usage dashboard regularly.