Skip to content

Instantly share code, notes, and snippets.

@sdwvit
Last active May 29, 2025 10:00
Show Gist options
  • Save sdwvit/44594c727ac10f53c2e9fc87f1843f5c to your computer and use it in GitHub Desktop.
Save sdwvit/44594c727ac10f53c2e9fc87f1843f5c to your computer and use it in GitHub Desktop.
Generate git commit message using bash and openai api
#!/bin/bash
# Generate patch for uncommitted changes
patch=$(git diff)
if [ -z "$patch" ]; then
echo "No uncommitted changes found."
exit 0
fi
# Check for the OpenAI API key
if [ -z "$OPENAI_API_KEY" ]; then
echo "Error: Please set the OPENAI_API_KEY environment variable."
exit 1
fi
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. Please install jq to parse JSON responses."
exit 1
fi
# Prepare the prompt with your instructions and the git diff patch
read -r -d '' prompt <<'EOF'
You are my assistant for writing commit messages and merge request (MR) descriptions. I will provide you with a git diff patch each time, and you need to do the following:
Write a concise, professional commit message. First line should be a precise and short summary, preferably under 80 characters. A bullet list of specific changes or technical considerations from the diff should follow, each line should be prefixed with ' - ' and be less than 100 chars. Follow best commit message practices.
Instructions:
Always derive the commit message directly from the git diff patch (the actual code changes).
Consider only latest commit from the git diff patch.
Maintain a professional tone and avoid generic statements.
Be concise but provide enough detail to clarify the context of the changes.
Diff:
EOF
# Append the actual diff patch to the prompt
prompt="$prompt"$'\n'"$patch"
# Use jq to construct the JSON payload. This ensures proper escaping.
payload=$(jq -n --arg prompt "$prompt" '{
model: "gpt-3.5-turbo",
messages: [
{ role: "user", content: $prompt }
]
}')
# Send the prompt to ChatGPT using the OpenAI API and capture the response
response=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "$payload")
# For debugging, uncomment the following line to see the full API response
# echo "$response" | jq .
# Extract and print only the content from the response
content=$(echo "$response" | jq -r '.choices[0].message.content')
if [ "$content" = "null" ] || [ -z "$content" ]; then
echo "Error: Received an empty response. Full response:"
echo "$response" | jq .
exit 1
fi
echo "Generated Commit Message:"
echo "$content"
# Check if the current directory is part of a git repository
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
# Determine the correct .git directory path
git_dir=$(git rev-parse --git-dir)
commit_msg_path="$git_dir/COMMIT_EDITMSG"
# Write the commit message to the COMMIT_EDITMSG file
echo "$content" > "$commit_msg_path"
echo "Commit message written to $commit_msg_path"
else
echo "Not a git repository. Commit message not saved."
fi%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment