Skip to content

Instantly share code, notes, and snippets.

@BrambleXu
Forked from umutdz/commit-msg-checker.sh
Last active February 13, 2023 08:18
Show Gist options
  • Save BrambleXu/4f02fcaaf4039f8a233bb525a2464981 to your computer and use it in GitHub Desktop.
Save BrambleXu/4f02fcaaf4039f8a233bb525a2464981 to your computer and use it in GitHub Desktop.
Add your own custom commit message check into git hooks. The checker control commit messages whether correct according to conventional commit .
#!/usr/bin/env bash
if [ ! -x .git/hooks/commit-msg ] || [ ! -f .git/hooks/commit-msg ] || ! cmp ./hooks/commit-msg.sh .git/hooks/commit-msg
then
echo -e "\033[33m Setting Up Git commit Hook..."
mkdir -p .git/hooks/
cp ./hooks/commit-msg.sh .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
echo -e "\033[32m Done"
echo -e "\033[33m You can make commit now."
exit 1
fi
#!/bin/bash
MESSAGE=$(cat $1)
COMMITFORMAT="^(feat|fix|build|chore|docs|style|refactor|perf|test|merge)?(\(.+\))?!?: (.+[^.\r\n])([\r\n]+(.+[\r\n]+)+)?$"
if ! [[ "$MESSAGE" =~ $COMMITFORMAT ]]; then
echo "Your commit was rejected due to the commit message. Skipping..."
echo ""
echo "Please use the following format:"
echo "feat: feature example comment"
echo "fix(ui): bugfix example comment"
echo ""
echo "More details on https://www.conventionalcommits.org/en/v1.0.0/"
exit 1
fi
- repos:
- repo: local
hooks:
- id: commit-msg-checker
name: commit-msg-checker
entry: ./hooks/commit-msg-checker.sh
language: script
exclude: ^\.pre-commit-config$
pass_filenames: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment