Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 298c98a

Browse files
committed
Add subject length validation
1 parent 65a1a02 commit 298c98a

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

template/hooks/commit-msg

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Confirms that a commit message does not exceed 72 characters for any line and suggests one if it does.
44
#
55

6-
readonly MAX_LINE_LENGTH=72
6+
readonly MAX_SUBJECT_LENGTH=50
7+
readonly MAX_BODY_LENGTH=72
78
readonly VERSION="commit-msg 0.1.0"
89

910
while getopts ":v-:" opt; do
@@ -32,18 +33,24 @@ OPTIND=1
3233

3334
corrected=''
3435
ok=true
36+
line_count=0
3537
while IFS='' read -r line || [[ -n "$line" ]]; do
36-
if (( "${#line}" > "$MAX_LINE_LENGTH" )) && [[ "$line" != \#* ]] ; then
37-
corrected+="$(echo "$line" | fold -w "$MAX_LINE_LENGTH" -s)\n"
38+
if (( "$line_count" == 0 )) && (( "${#line}" > "$MAX_SUBJECT_LENGTH" )) && [[ "$line" != \#* ]]; then
39+
echo "error: the subject must be less than or equal to $MAX_SUBJECT_LENGTH characters" >&2
40+
exit 1
41+
elif (( "${#line}" > "$MAX_BODY_LENGTH" )) && [[ "$line" != \#* ]]; then
42+
corrected+="$(echo "$line" | fold -w "$MAX_BODY_LENGTH" -s)\n"
3843
ok=false
44+
((line_count++))
3945
elif [[ "$line" != \#* ]]; then
4046
corrected+="$line\n"
47+
((line_count++))
4148
fi
4249
done < "$1"
4350

4451
if [[ "$ok" == false ]]; then
4552
echo "error: maximum line length exceeded" >&2
46-
echo "error: all lines must be less than or equal to $MAX_LINE_LENGTH characters" >&2
53+
echo "error: all subject lines must be less than or equal to $MAX_BODY_LENGTH characters" >&2
4754
echo "error: use --no-verify to skip this check next time"
4855
echo "consider:" >&2
4956
echo "$(echo -e "$corrected")" >&2 # the nested echo is a cheap hack to remove multiple trailing new lines

0 commit comments

Comments
 (0)