I created a method called po
to streamline commits using an AI-driven approach for
generating concise commit messages. This function can be added to your .zshrc
or
.bashrc
.
- Add All Changes:
git add -A
- Check for Changes: If no changes are staged, it exits.
- Generate Commit Message via AI:
Fetches the diff using
git diff --cached
and sends it to an API endpoint (http://localhost:11434/api/generate
) to generate a commit message with Qwen2.5-coder. - Optional JIRA ID Integration: Prepend the Jira ticket number if found in the branch name.
Add this function to your .zshrc
or .bashrc
:
po() {
git add -A
# Check for changes
gitdiff=$(git diff --cached)
[ -z "$gitdiff" ] && echo "No changes to commit" && return 1
# Send request to Ollama API
response=$(curl -s -w "\n%{http_code}" -X POST http://localhost:11434/api/generate -d
"{
\"model\": \"qwen2.5-coder:14b\",
\"prompt\": \"Generate a concise commit message in imperative mood for these
changes:\\n${gitdiff}\"
}")
# Parse response
http_code=$(echo "$response" | tail -n1)
[ "$http_code" -ne 200 ] && echo "AI generation failed." && return 1
message=$(echo "$response" | sed '$d' | grep -o '"response":"[^"]*' | cut -d'"' -f4)
# Prepend JIRA ID if exists
jiraID=$(git rev-parse --abbrev-ref HEAD | grep -o '[A-Z]\+-[0-9]\+')
[ ! -z "$jiraID" ] && message="${jiraID}: ${message}"
git commit -m "$message"
git push origin $(git rev-parse --abbrev-ref HEAD)
}