Skip to content

Elevate your Git workflow with intelligent, context-aware commit messages generated by an AI language model.

License

Notifications You must be signed in to change notification settings

danilop/commit-message-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🤖 AI-Powered Git Commit Message Generator

Elevate your Git workflow with intelligent, context-aware commit messages generated by an AI language model.

🌟 Features

  • 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.

🚀 Quick Start

  1. Clone the repository:

    git clone https://github.com/yourusername/commit-message-generator.git
    cd commit-message-generator
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Set up AWS credentials: Ensure your AWS credentials are configured with access to Amazon Bedrock.

  4. Add the script to your PATH:

    export PATH="$PATH:/path/to/commit-message-generator"
    
  5. Run the script from within your repository:

    commit-message-generator.py
    
  6. Review and approve the generated commit message.

🛠️ Usage

After staging your changes in Git, run the script (it should be in your PATH):

commit-message-generator.py [options]

Command Line 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:

  1. Analyze staged changes
  2. Generate a commit message using an AI model
  3. Display the message for your review
  4. Commit the changes if you approve

🧠 How It Works

  1. File Analysis: Gathers information about staged files, including content, history, and diffs.
  2. Prompt Generation: Creates a detailed XML-formatted prompt, including file changes and commit context.
  3. AI Processing: Sends the prompt to Amazon Bedrock for intelligent analysis.
  4. Message Generation: The model generates a contextually relevant and well-formatted commit message.
  5. User Interaction: Presents the generated message for user review and approval.
  6. Git Integration: Commits the changes using the approved message.

📝 Sample Output

% 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!

🔧 Configuration

The script uses the following configuration:

  • AWS Region: Set to us-east-1 by default. Modify the AWS_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 the MODEL_ID to use a different model/version.

🚨 Error Handling

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.

🤝 Contributing

Contributions are welcome! Please ensure your code adheres to the existing style and includes appropriate tests.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


Happy committing! 🎉 Let AI supercharge your Git workflow!

About

Elevate your Git workflow with intelligent, context-aware commit messages generated by an AI language model.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages