|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# list of Conventional Commits types |
| 4 | +cc_types=("feat" "fix") |
| 5 | +default_types=("build" "chore" "ci" "docs" "${cc_types[@]}" "perf" "refactor" "revert" "style" "test") |
| 6 | +types=( "${cc_types[@]}" ) |
| 7 | + |
| 8 | +if [ $# -eq 1 ]; then |
| 9 | + types=( "${default_types[@]}" ) |
| 10 | +else |
| 11 | + # assume all args but the last are types |
| 12 | + while [ $# -gt 1 ]; do |
| 13 | + types+=( "$1" ) |
| 14 | + shift |
| 15 | + done |
| 16 | +fi |
| 17 | + |
| 18 | +# the commit message file is the last remaining arg |
| 19 | +msg_file="$1" |
| 20 | + |
| 21 | +commit_msg_type_regex='feat|fix|refactor|style|test|docs|build' |
| 22 | +commit_msg_scope_regex='.{1,20}' |
| 23 | +commit_msg_subject_regex='.{1,100}' |
| 24 | +commit_msg_regex="^(${commit_msg_type_regex})(\(${commit_msg_scope_regex}\))?: (${commit_msg_subject_regex})\$" |
| 25 | +merge_msg_regex="^Merge branch '.+'\$" |
| 26 | + |
| 27 | +commit_msg_header=$(git show -s --format=%s $msg_file) |
| 28 | + |
| 29 | +# Check if commit is conventional commit |
| 30 | +if ! [[ "$commit_msg_header" =~ (${commit_msg_regex})|(${merge_msg_regex}) ]]; then |
| 31 | + exit 0 |
| 32 | +fi |
| 33 | + |
| 34 | +echo "[Commit message] $( cat "$msg_file" )" |
| 35 | +echo " |
| 36 | +Your commit message does not follow Conventional Commits formatting |
| 37 | +https://www.conventionalcommits.org/ |
| 38 | +Conventional Commits start with one of the below types, followed by a colon, |
| 39 | +followed by the commit message: |
| 40 | + $(IFS=' '; echo "${types[*]}") |
| 41 | +Example commit message adding a feature: |
| 42 | + feat: implement new API |
| 43 | +Example commit message fixing an issue: |
| 44 | + fix: remove infinite loop |
| 45 | +Optionally, include a scope in parentheses after the type for more context: |
| 46 | + fix(account): remove infinite loop |
| 47 | +" |
| 48 | +exit 1 |
0 commit comments