Skip to content

Commit 0c50f3a

Browse files
committed
feat: use hook args for custom types list
1 parent ac4fb54 commit 0c50f3a

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ repos:
2525
hooks:
2626
- id: conventional-pre-commit
2727
stages: [commit-msg]
28+
args: [] # optional: list of Conventional Commits types to allow
2829
```
2930
3031
Install the `pre-commit` script:

conventional-pre-commit.sh

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
11
#!/usr/bin/env bash
22

3-
if ! grep -Pq '^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([\w][\w -]+\))?!?: [\w][\s\S]+$' "$1"; then
4-
echo "[Commit message] $( cat $1 )"
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="(\([\w][\w -]+\))?"
25+
# optional breaking change indicator and colon delimiter
26+
r_delim='!?:'
27+
# subject line, body, footer
28+
r_subject=" [\w][\s\S]+"
29+
# the full regex pattern
30+
pattern="^$r_types$r_scope$r_delim$r_subject$"
31+
32+
# check commit message
33+
if ! grep -Pq "$pattern" "$msg_file"; then
34+
echo "[Commit message] $( cat $msg_file )"
535
echo "
636
Your commit message does not follow Conventional Commits formatting
737
https://www.conventionalcommits.org/
838
939
Conventional Commits start with one of the below types, followed by a colon,
1040
followed by the commit message:
1141
12-
build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test
42+
$(IFS=' '; echo "${types[*]}")
1343
1444
Example commit message adding a feature:
1545

0 commit comments

Comments
 (0)