Skip to content

Instantly share code, notes, and snippets.

@cornzz
Last active August 30, 2024 09:37
Show Gist options
  • Save cornzz/6573991e8924986ceb41870aa3ac1649 to your computer and use it in GitHub Desktop.
Save cornzz/6573991e8924986ceb41870aa3ac1649 to your computer and use it in GitHub Desktop.

Generate git commit message using LLMs

Adapted from https://gist.github.com/karpathy/1dd0294ef9567971c1e4348a90d69285

Installation

Prerequisite: python

  1. Install the llm cli util: pip install llm
  2. Set your openai API key: llm keys set openai
  3. Download git_commit_ai.sh and make it executable:
sudo curl -o /usr/local/bin/git_commit_ai.sh https://gist.githubusercontent.com/cornzz/6573991e8924986ceb41870aa3ac1649/raw/70ebedaa7e980239904ce284856a18cca6099a56/git_commit_ai.sh && sudo chmod +x /usr/local/bin/git_commit_ai.sh
  1. Add git ai alias: git config --global alias.ai '!git_commit_ai.sh'

Now you can run git ai and it will generate a commit message for your staged changes.

#!/usr/bin/env zsh
# -----------------------------------------------------------------------------
# AI-powered Git Commit Function
# How it works:
# 1) gets the current staged changed diff
# 2) sends them to an LLM to write the git commit message
# 3) allows you to easily accept, edit, regenerate, cancel
# But - just read and edit the code however you like
# the `llm` CLI util is awesome, can get it here: https://llm.datasette.io/en/stable/
# Function to generate commit message
generate_commit_message() {
# Check if the script was called with the --llama flag
llm --no-stream $@ "
Below is a diff of all staged changes, coming from the command:
\`\`\`
git diff --cached
\`\`\`
Please generate a concise, one-line commit message for these changes without quotes and special characters. Start with a capital letter and avoid mentioning file names.
$(git diff --cached)"
}
# Function to read user input compatibly with both Bash and Zsh
read_input() {
if [ -n "$ZSH_VERSION" ]; then
echo -n "$1"
read -r REPLY
else
read -p "$1" -r REPLY
fi
}
# Check if the output is empty
if [ -z "$(git diff --cached)" ]; then
echo "No changes staged for commit."
exit 1
fi
# Main script
echo "Generating AI-powered commit message..."
commit_message=$(generate_commit_message "$@")
while true; do
echo "\nProposed commit message:"
echo "$commit_message\n"
read_input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? "
choice=$REPLY
case "$choice" in
a|A )
if git commit -m "$commit_message"; then
echo "Changes committed successfully!"
exit 0
else
echo "Commit failed. Please check your changes and try again."
exit 1
fi
;;
e|E )
commit_message="${commit_message//\"/\\\"}" # Escape existing double quotes
BUFFER="$commit_message"
vared BUFFER
commit_message="$BUFFER"
if [ -n "$commit_message" ] && git commit -m "$commit_message"; then
echo "Changes committed successfully with your message!"
exit 0
else
echo "Commit failed. Please check your message and try again."
exit 1
fi
;;
r|R )
echo "Regenerating commit message..."
commit_message=$(generate_commit_message "$@")
;;
c|C )
echo "Commit cancelled."
exit 1
;;
* )
echo "Invalid choice. Please try again."
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment