@@ -38,6 +38,14 @@ CMD="conventional-commit"
38
38
VERSION=" 1.0.0"
39
39
HOME=" https://github.com/slavcodev/git-hooks-scripts"
40
40
ABOUT=" ${FCG} Conventional commit title${NC} version ${FCY} $VERSION ${NC} "
41
+
42
+ OPTION_EXCLUDE_BRANCH=" excluded-branch"
43
+ OPTION_PREFIX_PATTERN=" prefix-pattern"
44
+ OPTION_SUFFIX_PATTERN=" suffix-pattern"
45
+ OPTION_MAX_LENGTH=" max-length"
46
+ OPTION_EXCLUDE_BRANCH_DEFAULT=" master"
47
+ OPTION_MAX_LENGTH_DEFAULT=80
48
+
41
49
HELP=" $ABOUT
42
50
43
51
Git 'commit-msg' hook that looks for patterns the branch name
@@ -53,10 +61,11 @@ Note: you can use 'commit --no-verify' to skip 'commit-msg' hooks.
53
61
${FCY} Example of config:${NC}
54
62
${FCS} ~~~
55
63
[${CMD} ]
56
- excluded-branch = master
57
- excluded-branch = development
58
- prefix-pattern = " FOO-[0-9]+"
59
- suffix-pattern = " FOO-[0-9]+"
64
+ ${OPTION_EXCLUDE_BRANCH} = master
65
+ ${OPTION_EXCLUDE_BRANCH} = development
66
+ ${OPTION_PREFIX_PATTERN} = " FOO-[0-9]+"
67
+ ${OPTION_SUFFIX_PATTERN} = " FOO-[0-9]+"
68
+ ${OPTION_MAX_LENGTH} = ${OPTION_MAX_LENGTH_DEFAULT}
60
69
~~~${NC}
61
70
62
71
$HOME
@@ -75,73 +84,126 @@ report_done() {
75
84
[[ $# > 0 ]] && echo " ${FCG} ✓ $@ ${NC} " || echo " ${FCG} ✓ Done${NC} "
76
85
}
77
86
78
- main () {
79
- prefix_pattern= $( git config --get conventional-commit.prefix-pattern )
80
- suffix_pattern= $( git config --get conventional-commit.suffix-pattern )
87
+ read_config () {
88
+ option= " $1 "
89
+ default= " $2 "
81
90
82
- if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then
83
- exit
91
+ value=" $( git config --get ${CMD} .${option} ) "
92
+
93
+ if [[ -z ${value} ]] ; then
94
+ echo ${default}
95
+ else
96
+ echo ${value}
84
97
fi
98
+ }
85
99
86
- branch_name=$( git rev-parse --abbrev-ref HEAD)
100
+ read_all_configs () {
101
+ option=" $1 "
102
+ default=" $2 "
103
+
104
+ value=" $( git config --get-all ${CMD} .${option} ) "
105
+
106
+ if [[ -z ${value} ]] ; then
107
+ echo ${default}
108
+ else
109
+ echo ${value}
110
+ fi
111
+ }
112
+
113
+ write_commit_title () {
114
+ commit_file=" ${1} "
115
+ commit_title=" ${2} "
116
+ mutated_commit_title=" ${3} "
117
+
118
+ if [[ -f " ${commit_file} " ]] ; then
119
+ sed -i -e ' s/' " ${commit_title} " ' /' " ${mutated_commit_title} " ' /g' " ${commit_file} "
120
+ fi
121
+ }
122
+
123
+ read_commit_title () {
124
+ if [[ -f " ${1} " ]] ; then
125
+ echo " $( head -n 1 " ${1} " ) "
126
+ else
127
+ echo " ${1} "
128
+ fi
129
+ }
130
+
131
+ main () {
132
+ report_start " Preparing conventional prefix or suffix for the commit..."
133
+
134
+ commit_file=" ${1} "
135
+ commit_title=" $( read_commit_title ${commit_file} ) "
136
+
137
+ if [[ -z " ${commit_title} " ]]; then
138
+ return
139
+ fi
87
140
88
- excluded_branches=$( git config --get-all conventional-commit.excluded-branch)
141
+ prefix_pattern=" $( read_config ${OPTION_PREFIX_PATTERN} ) "
142
+ suffix_pattern=" $( read_config ${OPTION_SUFFIX_PATTERN} ) "
143
+ max_length=" $( read_config ${OPTION_MAX_LENGTH} ${OPTION_MAX_LENGTH_DEFAULT} ) "
89
144
90
- if [[ -z ${excluded_branches } ]]; then
91
- excluded_branches= " master "
145
+ if [[ -z ${prefix_pattern} && -z ${suffix_pattern } ]]; then
146
+ return
92
147
fi
93
148
149
+ excluded_branches=" $( read_all_configs ${OPTION_EXCLUDE_BRANCH} ${OPTION_EXCLUDE_BRANCH_DEFAULT} ) "
150
+
151
+ branch_name=$( git rev-parse --abbrev-ref HEAD)
152
+
94
153
for excluded_branch in ${excluded_branches} ; do
95
154
if [[ " ${branch_name} " =~ " ${excluded_branch} " ]]; then
96
- exit
155
+ return
97
156
fi
98
157
done
99
158
100
- commit_file=$1
101
- commit_title=$( head -n 1 " ${commit_file} " )
159
+ mutated_commit_title=${commit_title}
102
160
103
- if [[ -z " ${commit_title} " ]]; then
104
- exit
105
- fi
161
+ if [[ -n " ${prefix_pattern} " ]]; then
162
+ prefix=$( echo " ${branch_name} " | grep -Eo " ${prefix_pattern} " )
163
+ prefix_exists=$( echo " ${commit_title} " | grep -o " ^\[${prefix} \] " )
164
+ # commit_title=${commit_title#*]} # remove prefix ending in "]"
106
165
107
- prefix=$( echo " ${branch_name} " | grep -Eo " ${prefix_pattern} " )
108
- suffix=$( echo " ${branch_name} " | grep -Eo " ${suffix_pattern} " )
109
-
110
- if [[ -z " ${prefix} " && -z " ${suffix} " ]]; then
111
- exit
166
+ if [[ -z " ${prefix_exists} " ]]; then
167
+ echo " Adding prefix '${prefix} ' to the commit title..."
168
+ mutated_commit_title=" [${prefix} ] ${mutated_commit_title} "
169
+ else
170
+ echo " Prefix '${prefix} ' already exists, skipping..."
171
+ fi
112
172
fi
113
173
114
- prefix_exists=$( echo " ${commit_title} " | grep -o " ^\[${prefix} \] " )
115
- suffix_exists=$( echo " ${commit_title} " | grep -o " (${suffix} )$" )
174
+ if [[ -n " ${suffix_pattern} " ]]; then
175
+ suffix=$( echo " ${branch_name} " | grep -Eo " ${suffix_pattern} " )
176
+ suffix_exists=$( echo " ${commit_title} " | grep -o " (${suffix} )$" )
177
+ # commit_title=${commit_title%(*} # remove suffix starting with "("
116
178
117
- commit_title=${commit_title#* ]} # remove prefix ending in "."
118
- commit_title=${commit_title% (* } # remove suffix starting with "."
179
+ if [[ -z " ${suffix_exists} " ]]; then
180
+ echo " Adding suffix '${suffix} ' to the commit title..."
181
+ mutated_commit_title=" ${mutated_commit_title} (${suffix} )"
182
+ else
183
+ echo " Suffix '${suffix} ' already exists, skipping..."
184
+ fi
185
+ fi
119
186
120
- if [[ -z " ${prefix_exists} " ]]; then
121
- echo " Adding prefix '${prefix} ' to the commit title..."
122
- sed -i -e ' s/' " ${commit_title} " ' /' " [${prefix} ] ${commit_title} " ' /g' " ${commit_file} "
123
- else
124
- echo " Prefix '${prefix} ' already exists, skipping..."
187
+ if [[ ${# mutated_commit_title} -gt ${max_length} ]]; then
188
+ report_error " Too long commit message, must be maximum ${max_length} characters"
189
+ return 1
125
190
fi
126
191
127
- if [[ -z " ${suffix_exists } " ]]; then
128
- echo " Adding suffix ' ${suffix} ' to the commit title... "
129
- sed -i -e ' s/ ' " ${commit_title} " ' / ' " ${commit_title} ( ${suffix} ) " ' /g ' " ${commit_file} "
192
+ if [[ " ${mutated_commit_title} " != " ${commit_title } " ]] ; then
193
+ $( write_commit_title " ${commit_file} " " ${commit_title} " " ${mutated_commit_title} " )
194
+ echo " Commit message was changed to ' $( read_commit_title ${commit_file} ) ' "
130
195
else
131
- echo " Suffix ' ${suffix} ' already exists, skipping... "
196
+ echo " No changes required "
132
197
fi
133
198
134
- echo " Title was changed to '$( head -n 1 " ${commit_file} " ) '"
135
-
136
199
report_done
137
-
138
- exit $?
139
200
}
140
201
141
202
case " $1 " in
142
203
-h|--help|-? )
143
204
echo " ${HELP} "
144
205
;;
145
206
* )
146
- main
207
+ main " $@ "
208
+ exit $?
147
209
esac
0 commit comments