11#! /usr/bin/env bash
22
3- if ! grep -qE " ^\bbuild\b|\bchore\b|\bci\b|\bdocs\b|\bfeat\b|\bfix\b|\bperf\b|\brefactor\b|\brevert\b|\bstyle\b|\btest\b(\([a-z ]+\)!)?: [\w ]+$" " $1 " ; then
4- cat $1
5- echo "
6- Your commit message doesn't conform to Convential Commit Rules (https://www.conventionalcommits.org/).
7- At a minimum, your commit should start with one of the below keywords:
8- build: chore: ci: docs: feat: fix: perf: refactor: revert: style: test:
9- Example commit message to add a feature: 'feat: Changed something to add a lovely new feature'
10- Example commit message to fix an issue: 'fix: Removed infinite loop causing that annoying bug'
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+ # join types with | to form regex ORs
22+ r_types=" ($( IFS=' |' ; echo " ${types[*]} " ) )"
23+ # optional (scope)
24+ r_scope=" (\([[:alnum:] \/-]+\))?"
25+ # optional breaking change indicator and colon delimiter
26+ r_delim=' !?:'
27+ # subject line, body, footer
28+ r_subject=" [[:alnum:]].+"
29+ # the full regex pattern
30+ pattern=" ^$r_types$r_scope$r_delim$r_subject $"
31+ merge_msg_regex=" ^Merge branch '.+'\$ "
32+
33+ # Check if commit is conventional commit
34+ if grep -Eq " $pattern |$merge_msg_regex " " $msg_file " ; then
35+ exit 0
36+ fi
37+
38+ echo " [Commit message] $( cat " $msg_file " ) "
39+ echo "
40+ Your commit message does not follow Conventional Commits formatting
41+ https://www.conventionalcommits.org/
42+ Conventional Commits start with one of the below types, followed by a colon,
43+ followed by the commit message:
44+ $( IFS=' ' ; echo " ${types[*]} " )
45+ Example commit message adding a feature:
46+ feat: implement new API
47+ Example commit message fixing an issue:
48+ fix: remove infinite loop
49+ Optionally, include a scope in parentheses after the type for more context:
50+ fix(account): remove infinite loop
1151"
12- exit 1
13- fi
52+ exit 1
0 commit comments