Skip to content

Instantly share code, notes, and snippets.

@scottpersinger
Created August 7, 2024 14:07
Show Gist options
  • Save scottpersinger/386d340b9dfb671da785d0a6f3b2b1cf to your computer and use it in GitHub Desktop.
Save scottpersinger/386d340b9dfb671da785d0a6f3b2b1cf to your computer and use it in GitHub Desktop.
Bash script to use GPT4o-mini to summary git commits
#!/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"
@scottpersinger
Copy link
Author

Uses GPT4o-mini to summarize the changes from a git commit. Pass a negative integer to move back in commit history (-1 is the most recent commit, -2 is the previous, and so on).

@scottpersinger
Copy link
Author

Example run:

scottp@Daniels-MacBook-Air dashboard % bash ~/Downloads/git-log-summary-script.sh -9
Summary of git commit (index -9):
### Commit Summary

**Commit ID:** 4591a9a5d31533b8ce93d5661392bd10e3d38237  
**Author:** ALEXANDER Rufino OSBORNE <[email protected]>  
**Date:** Fri Aug 2 22:23:19 2024 -0700  

**Description:**  
This commit focuses on enhancing the functionality for detecting Markdown tables in the codebase related to the `EditorState` class. The table detection logic has been made more robust and includes new functionality to identify the start position of a Markdown table. Additionally, references to OpenAI usage statistics and authentication configurations have been removed from the AdminTool but are still included in the list of available tools.

### Key Changes:
1. **Markdown Table Detection:**
   - Enhanced the `detect_markdown_table` static method to not only detect the presence of tables but also to track their starting position in the `Answer` object.
   - Updated the method to handle line-by-line checking for Markdown table headers and delimiters.
  
2. **Code Cleanup:**
   - Removed configurations related to OpenAI API keys from the AdminTool class, improving security and simplifying the handling of sensitive information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment