Created
May 17, 2026 05:59
-
-
Save nqbao/f0a964e1b90d954c1b621cf59fa84b86 to your computer and use it in GitHub Desktop.
ai-commit.sh
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
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Check there's something staged | |
| if git diff --cached --quiet; then | |
| echo "Nothing staged. Run 'git add' first." | |
| exit 1 | |
| fi | |
| DIFF=$(git diff --cached) | |
| LOG=$(git log --oneline -10 2>/dev/null || true) | |
| PROMPT="Here is the recent commit history for style reference: | |
| ${LOG} | |
| Here is the staged diff: | |
| ${DIFF} | |
| Write a single short precise commit message (1 line, no quotes). Match the tone and style of the recent commits. Output only the message, nothing else." | |
| echo "Generating commit message..." | |
| MESSAGE=$(ollama run gemma4:e4b "$PROMPT" 2>/dev/null | head -1 || true) | |
| MESSAGE=$(echo "$MESSAGE" | sed 's/^[[:space:]]*//' | sed 's/[[:space:]]*$//') | |
| if [[ -z "$MESSAGE" ]]; then | |
| echo "Model returned empty output." | |
| exit 1 | |
| fi | |
| echo "" | |
| echo " $MESSAGE" | |
| echo "" | |
| read -rp "Commit with this message? [y/N] " CONFIRM | |
| if [[ "$CONFIRM" == "y" || "$CONFIRM" == "Y" ]]; then | |
| git commit -m "$MESSAGE" | |
| else | |
| echo "Aborted." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment