Skip to content

Commit aacbbb8

Browse files
authored
Add max-length option to conventional-commit hook (#13)
* Redo conventional-commit * Add max-length option to conventional-commit hook
1 parent 46ec087 commit aacbbb8

File tree

1 file changed

+104
-42
lines changed

1 file changed

+104
-42
lines changed

conventional-commit/conventional-commit

+104-42
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ CMD="conventional-commit"
3838
VERSION="1.0.0"
3939
HOME="https://github.com/slavcodev/git-hooks-scripts"
4040
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+
4149
HELP="$ABOUT
4250
4351
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.
5361
${FCY}Example of config:${NC}
5462
${FCS}~~~
5563
[${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}
6069
~~~${NC}
6170
6271
$HOME
@@ -75,73 +84,126 @@ report_done() {
7584
[[ $# > 0 ]] && echo "${FCG}$@${NC}" || echo "${FCG}✓ Done${NC}"
7685
}
7786

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"
8190

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}
8497
fi
98+
}
8599

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
87140

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})"
89144

90-
if [[ -z ${excluded_branches} ]]; then
91-
excluded_branches="master"
145+
if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then
146+
return
92147
fi
93148

149+
excluded_branches="$(read_all_configs ${OPTION_EXCLUDE_BRANCH} ${OPTION_EXCLUDE_BRANCH_DEFAULT})"
150+
151+
branch_name=$(git rev-parse --abbrev-ref HEAD)
152+
94153
for excluded_branch in ${excluded_branches} ; do
95154
if [[ "${branch_name}" =~ "${excluded_branch}" ]]; then
96-
exit
155+
return
97156
fi
98157
done
99158

100-
commit_file=$1
101-
commit_title=$(head -n 1 "${commit_file}")
159+
mutated_commit_title=${commit_title}
102160

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 "]"
106165

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
112172
fi
113173

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 "("
116178

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
119186

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
125190
fi
126191

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})'"
130195
else
131-
echo "Suffix '${suffix}' already exists, skipping..."
196+
echo "No changes required"
132197
fi
133198

134-
echo "Title was changed to '$(head -n 1 "${commit_file}")'"
135-
136199
report_done
137-
138-
exit $?
139200
}
140201

141202
case "$1" in
142203
-h|--help|-? )
143204
echo "${HELP}"
144205
;;
145206
* )
146-
main
207+
main "$@"
208+
exit $?
147209
esac

0 commit comments

Comments
 (0)