A Python script that generates commit messages based on git diffs using Azure OpenAI's GPT models, following a customizable commit style guide.
Writing detailed and well-formatted commit messages is crucial for maintaining a clear project history. The OpenAI Commit Message Generator simplifies this process by automatically generating commit messages based on your git diffs. It leverages Azure OpenAI's GPT models to produce concise and structured commit messages that adhere to a specified style guide.
- Automated Commit Message Generation: Generates commit messages by summarizing git diffs.
- Customizable Style Guide: Follows a commit style guide that can be customized to your preferences.
- Azure OpenAI Integration: Utilizes Azure OpenAI services for advanced text generation.
- Handles Large Diffs: Automatically chunks large diffs to comply with model token limits.
- Python: Version 3.6 or higher.
- Azure Subscription: Access to Azure OpenAI services.
- Git: Installed and configured on your system.
-
Clone the Repository
git clone https://github.com/danielwolfman/openai-commit-message-generator.git cd openai-commit-message-generator -
Install Dependencies
Install the required Python packages using pip:
pip install -r requirements.txt
Before using the script, you need to configure it to communicate with Azure OpenAI services.
-
Obtain Azure OpenAI Credentials
- Azure OpenAI Endpoint: This is your Azure OpenAI resource endpoint, typically in the format
https://<your-resource-name>.openai.azure.com/. - API Version: The version of the Azure OpenAI API you are using (e.g.,
2023-06-01-preview). - OpenAI Model Name: The name of the model you wish to use (e.g.,
gpt-4,gpt-3.5-turbo).
- Azure OpenAI Endpoint: This is your Azure OpenAI resource endpoint, typically in the format
-
Set Environment Variables
Create a
.envfile in the root directory of the project or set the following environment variables:AZURE_OPENAI_ENDPOINT=<your-azure-openai-endpoint> AZURE_OPENAI_API_VERSION=<your-api-version> OPENAI_MODEL=<model-name> OPENAI_MODEL_MAX_TOKENS=<max-tokens>
Example
.envFile:AZURE_OPENAI_ENDPOINT=https://your-resource-name.openai.azure.com/ AZURE_OPENAI_API_VERSION=2023-06-01-preview OPENAI_MODEL=gpt-4 OPENAI_MODEL_MAX_TOKENS=8192
The script uses the
python-dotenvpackage to load these variables. -
Azure Authentication
The script uses Azure's
DefaultAzureCredentialfor authentication. Ensure that you are authenticated with Azure CLI or have set up the appropriate environment variables for Azure authentication.- Azure CLI Authentication: Log in using
az login. - Environment Variables: Set
AZURE_CLIENT_ID,AZURE_TENANT_ID, andAZURE_CLIENT_SECRET.
- Azure CLI Authentication: Log in using
You can customize the commit style guide to match your project's conventions.
-
Create a Style Guide File
- Create a file named
COMMIT_STYLE.mdin the root directory of the project. - Alternatively, place a
~/.commit_style_guide.mdfile in your home directory.
- Create a file named
-
Define Your Style Guide
Example
COMMIT_STYLE.md:# Commit Style Guide 1. Start the commit message with a capitalized verb (e.g., "Add", "Fix"). 2. Use the present tense (e.g., "Add feature" not "Added feature"). 3. Keep the subject line under 50 characters. 4. Separate the subject from the body with a blank line. 5. Provide a detailed description in the body. 6. Reference related issues or pull requests at the end.
-
Fallback Style Guide
If no custom style guide is provided, the script uses a default style guide defined within the code.
The script caches Azure OpenAI configuration in a file located at ~/.azure_openai_cache for quicker access in subsequent runs. This cache file stores:
AZURE_OPENAI_ENDPOINTAZURE_OPENAI_API_VERSIONOPENAI_MODELOPENAI_MODEL_MAX_TOKENS
If you update your environment variables, the cache file will be updated automatically.
The script reads git diffs from standard input and outputs a formatted commit message to standard output.
-
Navigate to Your Git Repository
cd /path/to/your/git/repository -
Stage Your Changes
git add . -
Generate and Use the Commit Message
git diff --cached | python openai-commit-message-generator.py > COMMIT_MSG.txt git commit -F COMMIT_MSG.txt
Or, in a single command:
git commit -a -m "$(git diff --cached | python /path/to/openai-commit-message-generator.py)"
The script automatically chunks large diffs to comply with the model's maximum token limit. No additional action is required from the user.
Example 1: Generate a commit message for unstaged changes.
git diff | python openai-commit-message-generator.pyExample 2: Generate a commit message and save it to a file.
git diff --cached | python openai-commit-message-generator.py > COMMIT_MSG.txtExample 3: Commit changes using the generated message.
git commit -a -m "$(git diff | python openai-commit-message-generator.py)"-
Authentication Error: If you encounter an
AuthenticationError, ensure your Azure credentials are correctly set up and you have access to the OpenAI service. -
Azure OpenAI Endpoint Not Found: Verify that
AZURE_OPENAI_ENDPOINTand other required environment variables are set either in your environment or in a.envfile. -
Invalid Git Diff Input: Ensure you are providing a valid git diff. The script expects input starting with
diff --git. -
Missing Dependencies: If you receive import errors, ensure all dependencies are installed via
pip install -r requirements.txt.
Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
-
Fork the Repository
git clone https://github.com/danielwolfman/openai-commit-message-generator.git
-
Create a Feature Branch
git checkout -b feature/YourFeature
-
Commit Your Changes
git commit -am 'Add new feature' -
Push to the Branch
git push origin feature/YourFeature
-
Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenAI: For providing powerful language models.
- Azure: For making AI services accessible.
- Contributors: Thanks to all contributors who have helped improve this project.