-
-
Save juanchaves/3171ed9a5f93ce168617562f7acff2c2 to your computer and use it in GitHub Desktop.
🤖 Generate git commit messages automatically using Amazon Q Cli (`q`). Simple bash script that analyzes your git diff and creates meaningful commit messages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Based on https://gist.github.com/fritzprix/5d2aa93a1d1e117ed374bb18dbd3bb27 | |
| # Default mode is normal commit | |
| AMEND_MODE=false | |
| USE_TEMPLATE=false | |
| TEMPLATE_PATH="$HOME/.gitcommit.template" | |
| # Show help message | |
| show_help() { | |
| echo "Usage: $(basename "$0") [OPTIONS]" | |
| echo "Generate a commit message using Amazon Q CLI based on staged changes." | |
| echo "" | |
| echo "Options:" | |
| echo " --amend Amend the previous commit instead of creating a new one" | |
| echo " --template Use a git commit template" | |
| echo " --create-template Create a default commit template at $TEMPLATE_PATH" | |
| echo " --help Show this help message" | |
| } | |
| # Function to create a default commit template | |
| create_template() { | |
| cat > "$TEMPLATE_PATH" << 'EOF' | |
| <type>(<scope>): <subject> | |
| <body> | |
| <footer> | |
| # Commit Message Format Guidelines: | |
| # <type>: feat, fix, docs, style, refactor, test, chore | |
| # <scope>: component affected (optional) | |
| # <subject>: short description in present tense, imperative mood | |
| # <body>: detailed explanation of changes (why and what) | |
| # <footer>: reference issues, PRs, breaking changes | |
| # | |
| # Example: | |
| # feat(auth): add user registration feature | |
| # | |
| # This commit introduces a new user registration feature that | |
| # allows users to create accounts with email verification. | |
| # | |
| # References: #102, #105 | |
| EOF | |
| echo "Created commit template at $TEMPLATE_PATH" | |
| echo "To use it globally, run: git config --global commit.template $TEMPLATE_PATH" | |
| # Only exit if called directly with --create-template | |
| if [ "$1" = "exit" ]; then | |
| exit 0 | |
| fi | |
| } | |
| # Parse command line arguments | |
| while [[ $# -gt 0 ]]; do | |
| case $1 in | |
| --amend) | |
| AMEND_MODE=true | |
| shift | |
| ;; | |
| --template) | |
| USE_TEMPLATE=true | |
| shift | |
| ;; | |
| --create-template) | |
| create_template "exit" | |
| ;; | |
| --help) | |
| show_help | |
| exit 0 | |
| ;; | |
| *) | |
| shift | |
| ;; | |
| esac | |
| done | |
| # Get the git diff and save it to a temporary file | |
| git diff --cached > /tmp/git_diff.txt | |
| # If there's no diff and we're in amend mode, use the last commit diff | |
| if [ ! -s /tmp/git_diff.txt ]; then | |
| if [ "$AMEND_MODE" = true ]; then | |
| # Get the diff of the last commit | |
| git show -p HEAD > /tmp/git_diff.txt | |
| if [ ! -s /tmp/git_diff.txt ]; then | |
| echo "No changes in the last commit to amend" | |
| exit 1 | |
| fi | |
| echo "Using changes from the last commit for amend" | |
| else | |
| echo "No staged changes to commit" | |
| exit 1 | |
| fi | |
| fi | |
| # Check if template exists when requested, create it if it doesn't | |
| if [ "$USE_TEMPLATE" = true ] && [ ! -f "$TEMPLATE_PATH" ]; then | |
| echo "Template file not found at $TEMPLATE_PATH, creating it automatically..." | |
| create_template | |
| echo "Template created and will be used for this commit." | |
| fi | |
| # Create the prompt with the diff content | |
| if [ "$USE_TEMPLATE" = true ]; then | |
| # Get template content | |
| TEMPLATE_CONTENT=$(cat "$TEMPLATE_PATH" | grep -v "^#" | sed '/^$/N;/^\n$/D') | |
| prompt="Given the following git diff and commit template, write ONLY a git commit message that follows the template structure. The message should explain the changes focusing on WHAT changed and WHY. Use present tense, imperative mood. Keep the first line under 72 characters. DO NOT include any introductory text, explanations, or conclusions - ONLY the commit message itself that follows the template:\n\nTemplate:\n$TEMPLATE_CONTENT\n\nGit diff:\n$(cat /tmp/git_diff.txt)" | |
| else | |
| prompt="Given the following git diff, write ONLY a git commit message with no additional text, explanations, or formatting. The message should explain the changes focusing on WHAT changed and WHY. Use present tense, imperative mood. Keep the first line under 72 characters, then add more details after a blank line if needed. DO NOT include any introductory text, explanations, or conclusions - ONLY the commit message itself:\n\n$(cat /tmp/git_diff.txt)" | |
| fi | |
| # Run the prompt through Amazon Q CLI and save the response | |
| if ! q chat --non-interactive "$prompt" > /tmp/commit_msg_raw.txt; then | |
| echo "Error: Failed to generate commit message using Amazon Q CLI" | |
| exit 1 | |
| fi | |
| # Clean up any ANSI color codes or formatting and remove leading > symbol | |
| cat /tmp/commit_msg_raw.txt | sed 's/\x1B\[[0-9;]*[mK]//g' | sed 's/^[[:space:]]*>[[:space:]]*//' > /tmp/commit_msg.txt | |
| rm /tmp/commit_msg_raw.txt | |
| # Check if the commit message is empty | |
| if [ ! -s /tmp/commit_msg.txt ]; then | |
| echo "Error: Amazon Q CLI returned an empty commit message" | |
| exit 1 | |
| fi | |
| # Show the proposed commit message and ask for confirmation | |
| echo -e "\nProposed commit message:" | |
| if [ "$AMEND_MODE" = true ]; then | |
| echo -e "(AMEND MODE)" | |
| fi | |
| if [ "$USE_TEMPLATE" = true ]; then | |
| echo -e "(USING TEMPLATE)" | |
| fi | |
| echo "------------------------" | |
| cat /tmp/commit_msg.txt | |
| echo "------------------------" | |
| echo -e "\nDo you want to proceed with this commit message? (y/n)" | |
| read answer | |
| if [ "$answer" = "y" ]; then | |
| # Perform the commit using the generated message | |
| if [ "$AMEND_MODE" = true ]; then | |
| # For amend mode, we need to handle the case where there are no staged changes | |
| if [ -z "$(git diff --cached)" ]; then | |
| # No staged changes, just amend the commit message | |
| git commit --amend -F /tmp/commit_msg.txt --no-edit | |
| else | |
| # Normal amend with staged changes | |
| git commit --amend -F /tmp/commit_msg.txt | |
| fi | |
| echo "Commit amended successfully!" | |
| else | |
| git commit -F /tmp/commit_msg.txt | |
| echo "Changes committed successfully!" | |
| fi | |
| else | |
| echo "Commit canceled" | |
| fi | |
| # Clean up temporary files | |
| rm -f /tmp/git_diff.txt /tmp/commit_msg.txt /tmp/commit_msg_raw.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment