Created
August 9, 2025 12:37
-
-
Save marcinbunsch/4f2046859aabe20f09ece143ac50ec5d to your computer and use it in GitHub Desktop.
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
# AI commit message generator | |
# | |
# Assumes LM Studio is running locally and jq is installed | |
# | |
# Put this in .zprofile and you can run `aicommit` from any Git repository | |
aicommit() { | |
local diff | |
diff=$(git diff --cached) | |
if [ -z "$diff" ]; then | |
echo "No staged changes to commit." | |
return 1 | |
fi | |
echo "Generating commit message from diff..." | |
# Construct JSON payload safely with jq | |
local payload | |
payload=$(jq -n --arg diff "$diff" --arg model "openai/gpt-oss-20b" ' | |
{ | |
model: $model, | |
messages: [ | |
{ | |
role: "system", | |
content: "You are a helpful assistant that writes concise and descriptive Git commit messages." | |
}, | |
{ | |
role: "user", | |
content: "Generate a Git commit message for the following diff. Do not use markdown for the first line. The first line should be max 80 characters.\n\n\($diff)" | |
} | |
] | |
} | |
') | |
# Call LM Studio API - store to a file to ease jq parsing | |
local response | |
response=$(curl -s http://localhost:1234/v1/chat/completions \ | |
-H "Content-Type: application/json" \ | |
-d "$payload" > /tmp/lm_commit_response.json) | |
# Try to extract commit message | |
message=$(jq -r '.choices[0].message.content' /tmp/lm_commit_response.json) | |
if [ -z "$message" ]; then | |
echo "❌ Failed to get commit message from LM Studio." | |
return 1 | |
fi | |
echo -e "\n💡 Suggested commit message:\n\n$message\n" | |
read "confirm?Use this message? [Y/n]" | |
case "$confirm" in | |
[nN]*) echo "Aborted."; return 1 ;; | |
[yY]*) git commit -m "$message" ;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment