forked from compilerla/conventional-pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conventional-pre-commit.sh
executable file
·86 lines (67 loc) · 2.42 KB
/
conventional-pre-commit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env bash
# list of Conventional Commits types
cc_types=("feat" "fix")
default_types=("build" "chore" "ci" "docs" "${cc_types[@]}" "perf" "refactor" "revert" "style" "test")
types=( "${cc_types[@]}" )
if [ $# -eq 1 ]; then
types=( "${default_types[@]}" )
else
# assume all args but the last are types
while [ $# -gt 1 ]; do
types+=( "$1" )
shift
done
fi
# the commit message file is the last remaining arg
msg_file="$1"
# join types with | to form regex ORs
r_types="($(IFS='|'; echo "${types[*]}"))"
# optional (scope)
r_scope="(\([[:alnum:] \.\/-]+\))?"
# optional breaking change indicator and colon delimiter
r_delim='!?:'
# subject line, body, footer
r_subject=" [[:alnum:]].+"
# the full regex pattern
pattern="^$r_types$r_scope$r_delim$r_subject$"
# Check if commit is conventional commit
if grep -Eq "$pattern" "$msg_file"; then
exit 0
fi
# Check if commit is a merge commit and ignore it from conventional commits
merge_start_str="Merged in "
if grep -Eq "$merge_start_str" "$msg_file"; then
exit 0
fi
merge_start_str="Merge branch "
if grep -Eq "$merge_start_str" "$msg_file"; then
exit 0
fi
# Check if commit is a revert commit and ignore it from conventional commits
revert_start_str="This reverts commit "
if grep -Eq "$revert_start_str" "$msg_file"; then
exit 0
fi
# Check if commit is a revert commit and ignore it from conventional commits
bump_v_start_str="Bump version to "
if grep -Eq "$bump_v_start_str" "$msg_file"; then
exit 0
fi
printf "\e[48;5;226m\033[1;30m---[WARNING] This is not a Conventional Commit---\e[0m\n"
printf "\e[48;5;226m\033[1;30m[Commit message] $( cat "$msg_file" )\e[0m\n"
printf "
\e[48;5;226m\033[1;30mYour commit message does not follow Conventional Commits formatting\e[0m
\e[48;5;226m\033[1;30mhttps://www.conventionalcommits.org/\e[0m
Conventional Commits start with one of the below types, followed by a colon,
followed by the commit message:
$(IFS=' '; echo "${types[*]}")
Example commit message adding a feature:
feat: implement new API
Example commit message fixing an issue:
fix: remove infinite loop
Optionally, include a scope in parentheses after the type for more context:
fix(account): remove infinite loop
\e[48;5;226m\033[1;30mThis WARNING will be soon an\e[0m\e[48;5;196m ERROR \e[0m
\e[48;5;226m\033[1;30mFind out why in: https://qustodio.atlassian.net/l/cp/Vd1ZT2pA\e[0m
"
exit 0 # 0 as this is only a warning