Skip to content

Instantly share code, notes, and snippets.

@rickdaalhuizen90
Created December 27, 2023 20:04
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 rickdaalhuizen90/76c386574dfd15498280958fb2fcefbb to your computer and use it in GitHub Desktop.
Save rickdaalhuizen90/76c386574dfd15498280958fb2fcefbb to your computer and use it in GitHub Desktop.
Generate a commit message using the OpenAI GPT-3 API
OPENAI_API_KEY="<YOUR_SECRET_KEY>"
OPENAI_ENDPOINT="https://api.openai.com/v1/chat/completions"
#!/bin/bash
if [ -f .env ]; then
source .env
else
echo "Error: .env file not found."
exit 1
fi
# Get the diff using git
DIFF=$(git diff --cached)
# Use OpenAI API to generate a commit message
COMMIT_MESSAGE=$(curl $OPENAI_ENDPOINT -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Create a brief commit message following the conventional pattern /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\\(.+\\))?: .+/. Keep it within 1-3 sentences, summarizing the changes in the git diff."
}
]
}')
# Trim leading and trailing whitespaces
COMMIT_MESSAGE=$(echo "$COMMIT_MESSAGE" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
# Commit with the generated message
git commit -m "$COMMIT_MESSAGE"
# Exit with the original exit code of git commit
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment