Created
August 7, 2024 14:07
-
-
Save scottpersinger/386d340b9dfb671da785d0a6f3b2b1cf to your computer and use it in GitHub Desktop.
Bash script to use GPT4o-mini to summary git commits
This file contains hidden or 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 | |
# Check if the OpenAI API key is set | |
if [ -z "$OPENAI_API_KEY" ]; then | |
echo "Error: OPENAI_API_KEY environment variable is not set." | |
exit 1 | |
fi | |
# Create a temporary file | |
TEMP_FILE=$(mktemp) | |
# Function to clean up temporary file | |
cleanup() { | |
rm -f "$TEMP_FILE" | |
} | |
# Set up trap to ensure cleanup on script exit | |
trap cleanup EXIT | |
# Function to escape special characters for JSON string | |
escape_for_json() { | |
sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/$/\\n/' -e 's/\t/\\t/g' | tr -d '\n' | |
} | |
# Function to send data to OpenAI API and get summary | |
get_summary() { | |
local input_file="$1" | |
local escaped_content=$(cat "$input_file" | escape_for_json) | |
local payload=$(cat << EOF | |
{ | |
"model": "gpt-4o-mini", | |
"messages": [ | |
{ | |
"role": "system", | |
"content": "You are a helpful assistant that summarizes git commit information." | |
}, | |
{ | |
"role": "user", | |
"content": "Please provide a detailed summary of the following git commit. Include a brief description of the changes and a list of modified files:\n\n${escaped_content}" | |
} | |
] | |
} | |
EOF | |
) | |
local response=$(curl -s https://api.openai.com/v1/chat/completions \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $OPENAI_API_KEY" \ | |
-d "$payload") | |
# Extract the summary from the API response | |
echo "$response" | jq -r '.choices[0].message.content' | |
} | |
# Function to get git commit by index | |
get_git_commit() { | |
local index=$1 | |
git log -p -n 1 --skip=$((${index#-} - 1)) | |
} | |
# Main script | |
# Check if an argument is provided, otherwise use -1 as default | |
if [ $# -eq 0 ]; then | |
commit_index=-1 | |
else | |
commit_index=$1 | |
# Validate that the input is a negative integer | |
if ! [[ $commit_index =~ ^-[0-9]+$ ]]; then | |
echo "Error: Please provide a negative integer as the commit index." | |
exit 1 | |
fi | |
fi | |
# Get the specified git commit and save to temporary file | |
get_git_commit $commit_index > "$TEMP_FILE" | |
# Check if git log command was successful | |
if [ $? -ne 0 ] || [ ! -s "$TEMP_FILE" ]; then | |
echo "Error: Failed to retrieve git commit. Make sure you're in a git repository with enough commits." | |
exit 1 | |
fi | |
# Get summary from OpenAI API | |
summary=$(get_summary "$TEMP_FILE") | |
# Print the summary | |
echo "Summary of git commit (index $commit_index):" | |
echo "$summary" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example run: