Created
February 8, 2026 04:23
-
-
Save mylxsw/03476fc070990c56202966df120f80a8 to your computer and use it in GitHub Desktop.
A smart shell script that automatically generates conventional commit messages using OpenAI API based on your git changes.
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 | |
| OPENAI_API_BASE="${SQUIRREL_OPENAI_API_BASE:-${OPENAI_API_BASE:-https://api.openai.com/v1}}" | |
| OPENAI_API_KEY="${SQUIRREL_OPENAI_API_KEY:-${OPENAI_API_KEY:?Error: OPENAI_API_KEY is not set}}" | |
| OPENAI_MODEL="${SQUIRREL_OPENAI_MODEL:-${OPENAI_MODEL:-gpt-5-mini}}" | |
| DO_COMMIT=false | |
| DO_PUSH=false | |
| show_help() { | |
| cat <<'HELP' | |
| Usage: git-committer [OPTIONS] | |
| Generate a git commit message from current changes using OpenAI API. | |
| Options: | |
| --commit Stage all changes and commit with the generated message | |
| --push Implies --commit; also push to remote after committing | |
| -h, --help Show this help message and exit | |
| Environment Variables: | |
| OPENAI_API_KEY API key for OpenAI (required) | |
| OPENAI_API_BASE API base URL (default: https://api.openai.com/v1) | |
| OPENAI_MODEL Model to use (default: gpt-5-mini) | |
| SQUIRREL_OPENAI_API_KEY Override for OPENAI_API_KEY | |
| SQUIRREL_OPENAI_API_BASE Override for OPENAI_API_BASE | |
| SQUIRREL_OPENAI_MODEL Override for OPENAI_MODEL | |
| By default, the generated message is printed and copied to clipboard. | |
| HELP | |
| } | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| -h|--help) show_help; exit 0 ;; | |
| --commit) DO_COMMIT=true; shift ;; | |
| --push) DO_COMMIT=true; DO_PUSH=true; shift ;; | |
| *) echo "Unknown option: $1"; echo "Use --help for usage information."; exit 1 ;; | |
| esac | |
| done | |
| if ! git rev-parse --is-inside-work-tree &>/dev/null; then | |
| echo "Error: not a git repository" | |
| exit 1 | |
| fi | |
| DIFF=$(git diff HEAD 2>/dev/null || git diff --cached 2>/dev/null || true) | |
| UNTRACKED=$(git ls-files --others --exclude-standard) | |
| if [[ -z "$DIFF" && -z "$UNTRACKED" ]]; then | |
| echo "No changes detected." | |
| exit 0 | |
| fi | |
| SUMMARY="$DIFF" | |
| if [[ -n "$UNTRACKED" ]]; then | |
| SUMMARY="${SUMMARY}\n\nNew untracked files:\n${UNTRACKED}" | |
| fi | |
| ESCAPED_SUMMARY=$(printf '%s' "$SUMMARY" | python3 -c 'import sys,json; print(json.dumps(sys.stdin.read()))') | |
| PAYLOAD=$(cat <<EOF | |
| { | |
| "model": "${OPENAI_MODEL}", | |
| "messages": [ | |
| {"role": "system", "content": "You are a helpful assistant that generates concise git commit messages in conventional commit format based on code diffs. Output only the commit message, nothing else."}, | |
| {"role": "user", "content": ${ESCAPED_SUMMARY}} | |
| ], | |
| "temperature": 0.3 | |
| } | |
| EOF | |
| ) | |
| echo "Generating commit message..." | |
| RESPONSE=$(curl -s "${OPENAI_API_BASE}/chat/completions" \ | |
| -H "Content-Type: application/json" \ | |
| -H "Authorization: Bearer ${OPENAI_API_KEY}" \ | |
| -d "$PAYLOAD") | |
| COMMIT_MSG=$(echo "$RESPONSE" | python3 -c 'import sys,json; print(json.loads(sys.stdin.read())["choices"][0]["message"]["content"].strip())') | |
| if [[ -z "$COMMIT_MSG" ]]; then | |
| echo "Error: failed to generate commit message" | |
| exit 1 | |
| fi | |
| echo "$COMMIT_MSG" | |
| if command -v pbcopy &>/dev/null; then | |
| echo "$COMMIT_MSG" | pbcopy | |
| echo "(Copied to clipboard)" | |
| elif command -v xclip &>/dev/null; then | |
| echo "$COMMIT_MSG" | xclip -selection clipboard | |
| echo "(Copied to clipboard)" | |
| elif command -v xsel &>/dev/null; then | |
| echo "$COMMIT_MSG" | xsel --clipboard | |
| echo "(Copied to clipboard)" | |
| fi | |
| if [[ "$DO_COMMIT" == true ]]; then | |
| git add -A | |
| git commit -m "$COMMIT_MSG" | |
| if [[ "$DO_PUSH" == true ]]; then | |
| echo "Pushing to remote..." | |
| git push | |
| fi | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π€ AI-Powered Git Commit Message Generator
A smart shell script that automatically generates conventional commit messages using OpenAI API based on your git changes.
Features:
git diffand untracked files--commitand--pushflagsQuick Start:
Requirements:
Say goodbye to commit message writer's block! π