-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg
executable file
·132 lines (121 loc) · 4.61 KB
/
commit-msg
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# To use this hook, place it in .git/hooks for your repo
# The file must be named "commit-msg" (no extension)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Color codes
red='\033[0;31m'
yellow='\033[0;33m'
blue='\033[0;34m'
green='\033[0;32m'
NC='\033[0m'
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Utilities
print_newline_if_needed(){
if [ "${errs_found}" -gt 0 ] || [ "${warns_found}" -gt 0 ]; then
echo -e
fi
}
print_length_error(){
# $1 should be the line number in text, e.g., "second"
# $2 should be the requirement in text, e.g., "be blank"
# $3 should be the line passed through
print_newline_if_needed
echo -e "${red}The ${1} line of your commit message must ${2}.${NC}"
echo -e " Your ${1} line has ${yellow}${#3}${NC} characters: ${yellow}${3}${NC}"
}
print_result(){
# $1 should be the error or warning counter, e.g., "${errs_found}"
# $2 should be what to print if the counter is 0
# $3 should be what to print if the counter is 1
# $4 should be what to print for all other counter values
case "${1}" in
0)
print_newline_if_needed
echo -e "${2}"
;;
1)
print_newline_if_needed
echo -e "${3}"
;;
*)
print_newline_if_needed
echo -e "${4}"
;;
esac
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Conventional Commit Checker
line_number=-1
errs_found=0
warns_found=0
if ! [ -f "${1}" ]; then
echo -e "${red}No such file or directory \"${1}\". Try again with a valid file.${NC}"
((errs_found++))
fi
while read -r line; do
# Skip comments
if [ "${line:0:1}" = "#" ]; then
continue
fi
((line_number++))
# Linewise requirements
case ${line_number} in
0)
downcase_line=$(echo "$line" | tr '[:upper:]' '[:lower:]')
if ! [[ ${downcase_line} =~ ^((build|chore|ci|docs|feat|fix|perf|refactor|style|test|wip)(\([a-zA-Z0-9]+\))?(!)?: [^ ]+.*$) ]]; then
if [ "${downcase_line:0:7}" = "fixup! " ]; then
echo -e "${yellow}Conventional commit message check skipped on fixup commits.${NC}"
break;
elif [[ "${downcase_line:0:6}" = "merge " ]]; then
echo -e "${yellow}Conventional commit first-line check skipped on merge commits.${NC}"
continue;
elif [[ "${downcase_line:0:7}" = "revert " ]]; then
echo -e "${yellow}Conventional commit first-line check skipped on revert commits.${NC}"
continue;
fi
print_newline_if_needed
echo -e "${red}Please use conventional commit messages:"
echo -e " ${yellow}fix${green}(parser)${NC}: ${blue}properly handle new lines${NC}"
echo -e " ${yellow}^┬^${green}^──┬───^${NC} ${blue}^───────────────────────^ ─> Summary in present tense.${NC}"
echo -e " ${yellow} │ ${green} └─> [optional]: Scope of the commit.${NC}"
echo -e " ${yellow} └─> Type: build, chore, ci, docs, feat, fix, perf, refactor, style, test, wip.${NC}"
((errs_found++))
fi
if [ ${#line} -gt 50 ]; then
print_length_error "first" "be 50 characters or fewer" "${line}"
((errs_found++))
fi
;;
1)
if [[ ${#line} -gt 0 ]]; then
print_length_error "second" "be blank" "${line}"
((errs_found++))
fi
;;
*)
if [ ${#line} -gt 72 ]; then
if [ ${warns_found} -eq 0 ]; then
print_newline_if_needed
echo -e "${yellow}Human readable lines in commit message body should be 72 characters or shorter.${NC}"
echo -e "${yellow}If lines violate this warning, use git commit --amend to fix them.${NC}"
fi
echo -e " Line #$((line_number+1)) has ${yellow}${#line}${NC} characters: ${yellow}${line}${NC}"
((warns_found++))
fi
;;
esac
done < "${1}"
print_result "${warns_found}" \
"" \
"${yellow}${warns_found} warning detected with your commit message.\n\
Please fix it using git commit --amend.${NC}" \
"${yellow}${warns_found} warnings detected with your commit message.\n\
Please fix them using git commit --amend.${NC}"
print_result "${errs_found}" \
"${green}\xE2\x9C\x94${NC} commit message follows conventional commits syntax." \
"${red}${errs_found} issue detected with your commit message. \
Please fix it and try again.${NC}" \
"${red}${errs_found} issues detected with your commit message. \
Please fix these and try again.${NC}"
exit "${errs_found}"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #