summaryrefslogtreecommitdiff
path: root/scripts/commit.sh
blob: 6c7597c9642175670146f4c6b5396350280d5205 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env bash

set -euo pipefail

if git diff --cached --quiet; then
    echo "No staged changes."
    exit 1
fi

MSG=$(
git diff --cached \
| ollama run git-commit \
| sed '/^$/d' \
| tail -n 1
)

COMMIT_RE='^(feat|fix|docs|refactor|build|chore|test|perf|ci|revert)(\([[:alnum:]_-]+\))?: .+$'

if ! [[ $MSG =~ $COMMIT_RE ]]; then
    echo "Invalid commit message:"
    echo "$MSG"
    exit 1
fi

echo
echo "Generated commit message:"
echo "  $MSG"
echo

read -rp "Commit? [Y/n] " CONFIRM

[[ "$CONFIRM" =~ ^[Nn]$ ]] && exit 0

git commit -m "$MSG"