Skip to content

Instantly share code, notes, and snippets.

@keller
Created April 12, 2023 18:42
Show Gist options
  • Save keller/620e39c0fa273411013c7d0733b37de6 to your computer and use it in GitHub Desktop.
Save keller/620e39c0fa273411013c7d0733b37de6 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Script written by GPT-4
#
# Description:
# This script generates commit message suggestions based on the staged git diff.
# It uses the OpenAI Chat API with the specified model to generate the suggestions,
# and outputs a list of recommended commit messages. The user can customize the
# model, maximum number of lines from the git diff, and whether to follow
# conventional commit message format.
#
# Requirements:
# - jq (https://stedolan.github.io/jq/): A lightweight and flexible command-line JSON processor
# - An OpenAI API key (https://beta.openai.com/signup/)
#
# Setting the OPENAI_API_KEY variable:
# 1. Replace 'your_api_key' with your actual API key in the line below:
# OPENAI_API_KEY="your_api_key"
# 2. Uncomment the line by removing the '#' at the beginning of the line.
# 3. Alternatively, you can set an environment variable in your .bashrc or .zshrc file.
#
# Usage:
# ./commit_message_generator.sh [options]
#
# Options:
# -m, --model Specify the model to use (default: gpt-3.5-turbo)
# -l, --lines Set the maximum number of lines from git diff output (default: 200, use -1 for no limit)
# -c, --conventional-commit Toggle conventional commit message (default: false)
# -h, --help Display this help message and exit
#
# How to run the script:
# 1. Make sure the script is executable by running the following command:
# chmod +x commit_message_generator.sh
# 2. Optionally, move the script to a directory in your PATH for easier access, e.g.:
# mv commit_message_generator.sh /usr/local/bin/commit-message-generator
# Note: You might need to use 'sudo' for the above command if you don't have write permissions.
# 3. Stage your changes in git using 'git add' command.
# 4. Run the script with the desired options, for example:
# commit-message-generator -m gpt-3.5-turbo -l 200 -c
# (If you didn't move the script to a directory in your PATH, use the original command: ./commit_message_generator.sh -m gpt-3.5-turbo -l 200 -c)
# 5. Based on the generated commit message suggestions, run the 'git commit' command with your chosen message.
#OPENAI_API_KEY="your_api_key"
# Default values for model and max_lines
MODEL="gpt-3.5-turbo"
MAX_LINES=200
CONVENTION=""
# Help message
display_help() {
echo "Usage: $(basename $0) [options]" >&2
echo
echo " -m, --model Specify the model to use (default: gpt-3.5-turbo)"
echo " -l, --lines Set the maximum number of lines from git diff output (default: 200, use -1 for no limit)"
echo " -c, --conventional-commit Toggle conventional commit message (default: false)"
echo " -h, --help Display this help message and exit"
echo
exit 1
}
# Parse command-line arguments
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-m|--model)
MODEL="$2"
shift 2
;;
-l|--lines)
MAX_LINES="$2"
shift 2
;;
-c|--conventional-commit)
CONVENTION="following conventional commit (<type>: <subject>)"
shift
;;
-h|--help)
display_help
;;
*)
shift
;;
esac
done
# Define the Chat API endpoint URL
API_ENDPOINT="https://api.openai.com/v1/chat/completions"
# Set up the header with the API key for authentication
HEADER="Content-Type: application/json"
AUTH_HEADER="Authorization: Bearer $OPENAI_API_KEY"
# Get the git diff output and store it in a variable
if [ $MAX_LINES -eq -1 ]; then
GIT_DIFF=$(git diff --cached)
else
GIT_DIFF=$(git diff --cached | head -n $MAX_LINES)
fi
# Escape the git diff for JSON
GIT_DIFF_ESCAPED=$(echo -n "$GIT_DIFF" | jq -sRr '@json[1:-1]')
MESSAGES="[{\"role\": \"user\", \"content\": \"Based on the following git diff (escaped as a JSON string), suggest a few good commit messages for my commit${CONVENTION}: ${GIT_DIFF_ESCAPED} --- Output results as a numbered list, not more than 6 items.\"}]"
# Set other parameters for the API request
TEMPERATURE=0.5
MAX_TOKENS=100
# Perform the request to the OpenAI Chat API using the git diff as a message
RESPONSE=$(curl -s -X POST "${API_ENDPOINT}" \
-H "${HEADER}" \
-H "${AUTH_HEADER}" \
--data-binary "{\"model\": \"${MODEL}\", \"messages\": ${MESSAGES}, \"temperature\": ${TEMPERATURE}, \"max_tokens\": ${MAX_TOKENS}}")
# Check for errors in the API response
API_ERROR=$(echo "${RESPONSE}" | jq -r '.error')
if [ "$API_ERROR" != "null" ]; then
echo "Error: $(echo "${API_ERROR}" | jq -r '.message')"
exit 1
fi
# Extract and display the recommended commit message
COMMIT_MESSAGE=$(echo "${RESPONSE}" | jq -r '.choices[0].message.content')
echo -e "Recommended commit messages:\n${COMMIT_MESSAGE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment