Last active
June 20, 2025 22:33
-
-
Save DavidWells/dbf1cd375d838177c1146f7cec8508cb to your computer and use it in GitHub Desktop.
Alias this to something like `aic` "AI commit". Takes additional optional arg for commit message to have a body
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 | |
claude_commit_message() { | |
local debug=false | |
local should_push=false | |
local additional_info="" | |
# Handle arguments | |
for arg in "$@"; do | |
if [[ "$arg" == "--debug" ]]; then | |
debug=true | |
elif [[ "$arg" == "--push" ]]; then | |
should_push=true | |
else | |
additional_info="$arg" | |
fi | |
done | |
echo -e "\nRunning claude-commit-messages.sh\n" | |
# Check if there are any staged changes | |
if git diff --cached --quiet; then | |
echo "No staged changes to commit" | |
# If push flag is set, try to push anyway | |
if [ "$should_push" = true ]; then | |
echo "" | |
echo "Attempting to push any unpushed commits..." | |
current_branch=$(git branch --show-current) | |
echo -e "\nPushing changes to ${current_branch} branch..." | |
git push --quiet | |
# Get the repository URL and commit hash | |
repo_url=$(git config --get remote.origin.url | sed 's/\.git$//' | sed 's/[email protected]:/https:\/\/github.com\//') | |
commit_hash=$(git rev-parse HEAD) | |
# Display the commit URL | |
echo -e "\n[Details]" | |
echo "Commit URL: ${repo_url}/commit/${commit_hash}" | |
echo "Branch URL: ${repo_url}/tree/${current_branch}" | |
# Check for open PR using GitHub CLI | |
if command -v gh &> /dev/null; then | |
pr_url=$(gh pr view --json url --jq .url 2>/dev/null) | |
if [ -n "$pr_url" ]; then | |
echo "PR URL: ${pr_url}" | |
echo "" | |
echo "To merge this PR, run:" | |
echo "gh pr merge --auto --delete-branch" | |
fi | |
fi | |
echo "" | |
fi | |
return 0 | |
fi | |
local prompt="Look at the staged git changes and create a summarizing git commit title. Only respond with the title and no affirmation." | |
# If additional info is provided, modify prompt to request both title and body | |
if [ -n "$additional_info" ]; then | |
prompt="Look at the staged git changes and create a git commit message with a title and body. The body should be based on this context: ${additional_info}. Format your response as 'title\n\nbody'" | |
fi | |
# If push flag is set, push and show URL | |
if [ "$should_push" = true ]; then | |
echo "[PUSH MODE ON]" | |
echo "This will push the changes to the remote repository." | |
echo "Cancel with CTRL+C if you don't want to push." | |
echo "" | |
fi | |
echo "[Prompt]" | |
echo "$prompt" | fmt -w 80 | |
echo "" | |
echo 'Generating commit message 🤖...' | |
# Get the commit message from Claude | |
commit_message=$(claude -p "$prompt") | |
# Check if the response contains the OAuth token revoked message | |
if [[ "$commit_message" == *"OAuth token revoked"* ]]; then | |
echo "Error: OAuth token has been revoked. Please run /login to authenticate." | |
return 1 | |
fi | |
echo "" | |
echo "[Commit message]" | |
echo "$commit_message" | fmt -w 80 | |
# If debug flag is set, exit early | |
if [ "$debug" = true ]; then | |
echo "" | |
echo "--debug flag set: Not committing changes" | |
return 0 | |
fi | |
echo "" | |
echo "[Committing changes]" | |
# If no error, proceed with the commit | |
git commit -m "$commit_message" | |
# If push flag is set, push and show URL | |
if [ "$should_push" = true ]; then | |
echo "" | |
current_branch=$(git branch --show-current) | |
echo "Pushing changes to ${current_branch} branch..." | |
git push | |
# Get the repository URL and commit hash | |
repo_url=$(git config --get remote.origin.url | sed 's/\.git$//' | sed 's/[email protected]:/https:\/\/github.com\//') | |
commit_hash=$(git rev-parse HEAD) | |
# Display the commit URL | |
echo -e "\n[Details]" | |
echo "Commit URL: ${repo_url}/commit/${commit_hash}" | |
echo "Branch URL: ${repo_url}/tree/${current_branch}" | |
# Check for open PR using GitHub CLI | |
if command -v gh &> /dev/null; then | |
pr_url=$(gh pr view --json url --jq .url 2>/dev/null) | |
if [ -n "$pr_url" ]; then | |
echo "PR URL: ${pr_url}" | |
echo "" | |
echo "To merge this PR, run:" | |
echo "gh pr merge --auto --delete-branch" | |
fi | |
fi | |
echo "" | |
fi | |
} | |
# If script is run directly (not sourced), execute the function | |
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | |
claude_commit_message "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment