Skip to content

Instantly share code, notes, and snippets.

@alameenkhader
Created April 18, 2025 00:13
Show Gist options
  • Save alameenkhader/b8917e0146b4f74ed715b504a937281c to your computer and use it in GitHub Desktop.
Save alameenkhader/b8917e0146b4f74ed715b504a937281c to your computer and use it in GitHub Desktop.
Simplifying Commits with AI-Generated Messages

Simplifying Commits with AI-Generated Messages

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.

How It Works:

  1. Add All Changes:
    git add -A
  2. Check for Changes: If no changes are staged, it exits.
  3. 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.
  4. Optional JIRA ID Integration: Prepend the Jira ticket number if found in the branch name.

Implementation:

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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment