-
-
Save appleseed-iii/f21624b8bd9103eab3aae5f267326960 to your computer and use it in GitHub Desktop.
Git Commit Message AI
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/zsh | |
# ----------------------------------------------------------------------------- | |
# AI-powered (llama) Git Commit Function | |
# Copy paste this gist into your ~/.bashrc or ~/.zshrc to gain the `gcm` command, or simply source this file in your .zshrc. | |
# It: | |
# 1) gets the current staged changed diff | |
# 2) sends them to an LLM to write the git commit message | |
# 3) allows you to easily accept, edit, regenerate, cancel | |
# But - just read and edit the code however you like | |
# the `llm` CLI util is awesome, can get it here: https://llm.datasette.io/en/stable/ | |
# or you can you ollama: https://github.com/ollama/ollama/blob/main/README.md#quickstart | |
function gcm() { | |
# Check if a merge is in progress using Git's built-in method | |
if git rev-parse --verify MERGE_HEAD > /dev/null 2>&1; then | |
echo "Merge in progress. Running git commit directly." | |
git commit | |
return $? | |
fi | |
# Check if -m flag is provideda | |
if [ "$1" = "-m" ] && [ -n "$2" ]; then | |
# Use the provided message directly | |
if git commit -m "$2"; then | |
echo "Changes committed successfully with your message!" | |
return 0 | |
else | |
echo "Commit failed. Please check your message and try again." | |
return 1 | |
fi | |
fi | |
# Function to generate commit message | |
generate_commit_message() { | |
git diff --cached | ollama run llama3.2 " | |
Below is a diff of all staged changes, coming from the command: | |
\`\`\` | |
git diff --cached | |
\`\`\` | |
Please generate a concise, one-line commit message for these changes. | |
DO NOT PROVIDE ANY OTHER COMMENTARY. | |
Just provide the commit message. No quotation marks." | |
} | |
# Function to read user input compatibly with both Bash and Zsh | |
read_input() { | |
# usage: `read_input prompt default_value` | |
# prompt: The message to display before reading input | |
# default_value: The default value to use if no input is provided | |
local prompt="$1" | |
local default_value="$2" | |
if [ -n "$default_value" ]; then | |
# Only add brackets if default_value is non-empty | |
if [ -n "$ZSH_VERSION" ]; then | |
# For Zsh, use vared to pre-populate the input | |
REPLY="$default_value" | |
echo -n "$prompt: " | |
vared REPLY | |
else | |
read -p "$prompt $default_value" -e -i "$default_value" -r REPLY | |
fi | |
else | |
# Original behavior when no default value | |
if [ -n "$ZSH_VERSION" ]; then | |
echo -n "$prompt" | |
read -r REPLY | |
else | |
read -p "$prompt" -r REPLY | |
fi | |
fi | |
# Use default value if input is empty and default exists | |
if [ -z "$REPLY" ] && [ -n "$default_value" ]; then | |
REPLY="$default_value" | |
fi | |
} | |
# Main script | |
echo "Generating AI-powered commit message..." | |
commit_message=$(generate_commit_message) | |
# Strip surrounding quotes if present | |
commit_message=$(echo "$commit_message" | sed 's/^"//;s/"$//') | |
while true; do | |
echo -e "\nProposed commit message:" | |
echo "$commit_message" | |
read_input "Do you want to (a)ccept, (e)dit, (r)egenerate, or (c)ancel? " | |
choice=$REPLY | |
case "$choice" in | |
a|A ) | |
if git commit -m "$commit_message"; then | |
echo "Changes committed successfully!" | |
return 0 | |
else | |
echo "Commit failed. Please check your changes and try again." | |
return 1 | |
fi | |
;; | |
e|E ) | |
read_input "Enter your commit message: " "$commit_message" | |
commit_message=$REPLY | |
if [ -n "$commit_message" ] && git commit -m "$commit_message"; then | |
echo "Changes committed successfully with your message!" | |
return 0 | |
else | |
echo "Commit failed. Please check your message and try again." | |
return 1 | |
fi | |
;; | |
r|R ) | |
echo "Regenerating commit message..." | |
commit_message=$(generate_commit_message) | |
;; | |
c|C ) | |
echo "Commit cancelled." | |
return 1 | |
;; | |
* ) | |
echo "Invalid choice. Please try again." | |
;; | |
esac | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment