Created
December 27, 2023 20:04
-
-
Save rickdaalhuizen90/76c386574dfd15498280958fb2fcefbb to your computer and use it in GitHub Desktop.
Generate a commit message using the OpenAI GPT-3 API
This file contains 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
OPENAI_API_KEY="<YOUR_SECRET_KEY>" | |
OPENAI_ENDPOINT="https://api.openai.com/v1/chat/completions" |
This file contains 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 | |
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