Elevate your Git workflow with intelligent, context-aware commit messages generated by an AI language model.
- AI-Powered: Leverages AI models via Amazon Bedrock for high-quality commit message generation.
- Context-Aware: Analyzes file changes, diffs, and commit history for comprehensive understanding.
- Smart Formatting: Generates commit messages following best practices, including summary lines and detailed explanations.
- Interactive: Allows user review and approval of generated commit messages.
- Git-Integrated: Seamlessly works with your existing Git workflow.
- Customizable: Supports debug mode for detailed logging and troubleshooting.
-
Clone the repository:
git clone https://github.com/yourusername/commit-message-generator.git cd commit-message-generator
-
Install dependencies:
pip install -r requirements.txt
-
Set up AWS credentials: Ensure your AWS credentials are configured with access to Amazon Bedrock.
-
Add the script to your PATH:
export PATH="$PATH:/path/to/commit-message-generator"
-
Run the script from within your repository:
commit-message-generator.py
-
Review and approve the generated commit message.
After staging your changes in Git, run the script (it should be in your PATH
):
commit-message-generator.py [options]
--debug
: Enable detailed debug logging--include-content
: Include file content in the analysis (disabled by default)--include-history
: Include file commit history in the analysis (disabled by default)--context-lines N
: Number of context lines to show in git diff (default: 3)
Example:
# Run with debug logging and include file history
commit-message-generator.py --debug --include-history
# Include both content and history, with 5 lines of context
commit-message-generator.py --include-content --include-history --context-lines 5
The script will:
- Analyze staged changes
- Generate a commit message using an AI model
- Display the message for your review
- Commit the changes if you approve
- File Analysis: Gathers information about staged files, including content, history, and diffs.
- Prompt Generation: Creates a detailed XML-formatted prompt, including file changes and commit context.
- AI Processing: Sends the prompt to Amazon Bedrock for intelligent analysis.
- Message Generation: The model generates a contextually relevant and well-formatted commit message.
- User Interaction: Presents the generated message for user review and approval.
- Git Integration: Commits the changes using the approved message.
% git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: commit-message-generator.py
no changes added to commit (use "git add" and/or "git commit -a")
% git add .
% commit-message-generator.py
2024-10-23 16:50:40,625 - INFO - Found 1 staged files
2024-10-23 16:50:40,648 - INFO - Found 0 non-staged changes
2024-10-23 16:50:48,496 - INFO -
Suggested commit message:
--------------------------------------------------
Add prompt truncation and move XML order for better performance
- Introduce MAX_PROMPT_SIZE constant to limit prompt size
- Truncate prompt if it exceeds maximum size and log a warning
- Reorder XML tags to send diffs before content for efficiency
This commit optimizes the commit message generator by:
- Adding a maximum prompt size limit to avoid sending excessively large prompts to the model
- Truncating prompts that exceed the size limit and logging a warning
- Reordering the XML tags to send file diffs before content, allowing the model to process diffs first for better performance
These changes help improve the efficiency and reliability of the commit message generation process, especially for repositories with large files or many changes.
--------------------------------------------------
Would you like to use this commit message? (y/n): y
2024-10-23 16:50:56,662 - INFO - Successfully committed changes: [main 98971fb] Add prompt truncation and move XML order for better performance
1 file changed, 10 insertions(+), 2 deletions(-)
Changes committed successfully!
The script uses the following configuration:
- AWS Region: Set to
us-east-1
by default. Modify theAWS_REGION
variable in the script if needed. - Anthropic's Claude Model: Uses Anthropic Claude 3.5 Sonnet (
anthropic.claude-3-sonnet-20240229-v1:0
). Update theMODEL_ID
to use a different model/version.
The script includes robust error handling:
- Checks if the current directory is a Git repository
- Validates the presence of staged changes
- Handles file reading errors gracefully
- Manages API call failures
If you encounter issues, run the script with the --debug
flag for more detailed logging.
Contributions are welcome! Please ensure your code adheres to the existing style and includes appropriate tests.
This project is licensed under the MIT License - see the LICENSE file for details.
Happy committing! 🎉 Let AI supercharge your Git workflow!