cashmere

cashmere

Semantic Commit hook for git

Preface

As I do utilize Semantic Commit Changes a lot, I finally had some time to implement a small commit hook, to make commit messages easier for me.

Add the following snippet into your .git/hooks/commit-msg.

Snippet

#!/usr/bin/env sh
set -e

MSG_FILE="$1"
PATTERN='^(chore|docs|feat|fix|refactor|style|test)(\([^)]+\))?: .+'

FIRST_LINE=$(grep -m1 -v '^#' "$MSG_FILE" || true)

if ! echo "$FIRST_LINE" | grep -Eq "$PATTERN"; then
  echo "Invalid commit message format." >&2
  echo "Expected: <type>(<scope>): <subject>" >&2
  echo "Types: chore, docs, feat, fix, refactor, style, test" >&2
  exit 1
fi