forked from craicoverflow/sailr
-
Notifications
You must be signed in to change notification settings - Fork 4
/
commit-msg-hook.sh
executable file
·63 lines (51 loc) · 1.55 KB
/
commit-msg-hook.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
#!/bin/sh
#config_file_name="conventional-commits.json"
# checks that jq is usable
#function check_jq_exists_and_executable {
#if ! [ -x "$(command -v jq)" ]; then
# echo -e "\`commit-msg\` hook failed. Please install jq."
# exit 1
#fi
#}
# set values from config file to variables
function set_config_values() {
# local_config="$PWD/$config_file_name"
#
# if [ -f "$local_config" ]; then
# CONFIG=$local_config
# types=($(jq -r '.types[]' "$CONFIG"))
# else
types=('build' 'docs' 'feat' 'fix' 'perf' 'refactor' 'style' 'test' 'chore')
# fi
}
# build the regex pattern based on the config file
function build_regex() {
set_config_values
regexp="^[.0-9]+$|"
regexp="${regexp}^([Rr]evert|[Mm]erge):? .*$|^("
for type in "${types[@]}"
do
regexp="${regexp}$type|"
done
regexp="${regexp%|})(\(.+\))?: "
}
# get the first line of the commit message
INPUT_FILE=$1
commit_message=`head -n1 $INPUT_FILE`
# Print out a standard error message which explains
# how the commit message should be structured
function print_error() {
regular_expression=$2
echo -e "\n\e[31m[Invalid Commit Message]"
echo -e "------------------------\033[0m\e[0m"
echo -e "Valid types: \e[36m${types[@]}\033[0m"
echo -e "\e[37mActual commit message: \e[33m\"$commit_message\"\033[0m"
echo -e "\e[37mExample valid commit message: \e[36m\"fix(subject): message\"\033[0m"
echo -e "\e[37mRegex: \e[33m\"$regexp\"\033[0m"
}
build_regex
if [[ ! $commit_message =~ $regexp ]]; then
# commit message is invalid according to config - block commit
print_error
exit 1
fi