Skip to content

Instantly share code, notes, and snippets.

@whoabuddy
Last active July 19, 2023 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whoabuddy/708d6e109900d7f5acd374cddf73f5a0 to your computer and use it in GitHub Desktop.
Save whoabuddy/708d6e109900d7f5acd374cddf73f5a0 to your computer and use it in GitHub Desktop.
ai-commit-messages
# OpenAI API key
export OPENAI_API_KEY="your-api-key"
# AI commit messages!
gitai() {
# Check if the current directory is a git repository
if ! git status > /dev/null 2>&1; then
echo "Not a git repository"
return
fi
# Check for changes
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit"
return
fi
# Stage all changes
git add -A
# Gather the status, diff, and contents of new files
git_status=$(git status --porcelain)
git_diff=$(git diff --cached)
new_files_contents=$(git diff --cached --diff-filter=A | grep '^\+' | cut -c2-)
# Combine these into a single message
message="I need a git commit message that adheres strictly to the structure described below in a very simple and direct tone.\n\n1. The commit will follow the Conventional Commits standard and include the scope.\n2. The first message will be a brief summary of all changes, no more than 80 characters.\n3. The second message will be a section titled \"Changes Made\" that includes a bulleted list of changes.\n4. The third message will be a section titled \"New Additions\" that includes a bulleted list of new files and their functions.\n5. There will be no additional messages, notes, or output so the commit message can be passed directly to git.\n\nAdditional criteria:\n- do not reference components as file names\n- do not include changes where the original code remains but was moved to a new line\n\nHere's an example:\n\n\\\`\\\`\\\`\nfeat(scope): summary of changes\n\nChanges Made:\n- change 1 in component a\n- change 2 in component b\n\nNew Additions:\n- new file x that does y\n- new file z that does w\n\\\`\\\`\\\`\n\nNow, please create a structured commit message using the following information and do not include the git status, git diff, or new files:\n\nGit Status:\n${git_status}\n\nGit Diff:\n${git_diff}\n\nContents of New Files:\n${new_files_contents}"
# JSON-encode the message
json_message=$(jq -n --arg msg "$message" '{"role": "user", "content": $msg}')
# Create the JSON data for the API request
json_data=$(jq -n --argjson msg "$json_message" '{"model": "gpt-3.5-turbo-16k", "messages": [{"role": "system", "content": "You are a laser-focused, detail-oriented developer with a deep understanding of best practices in software development, including writing clear and meaningful git commit messages."}, $msg], "temperature": 0}')
# Make the API request and get the commit message
commit_message=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$json_data" | jq -r '.choices[0].message.content')
# Print the commit message and ask for confirmation
echo "Commit message:"
echo "---"
echo "$commit_message"
echo "---"
read -p "Do you want to commit with this message? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
git commit -m "$commit_message"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment