-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg
39 lines (34 loc) · 1.1 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
#!/bin/bash
# Input arguments
MSG=$(cat "$1")
MSG_LINES_N=$(echo "$MSG" | wc -l)
# Helper
echo_error () {
echo -e "$1\nBut was:\n---\n$2\n---" >&2
exit 1
}
# Validate message payload (line 1)
MSG_LINE_1=$(echo "$MSG" | head -n1)
MSG_LINE_1_N=${#MSG_LINE_1}
MSG_LINE_1_MAX=80
REGEX_LINE_1='^(Merge |Revert |PJ-[[:digit:]]+: [[:lower:]].*[\.]$)'
if (( MSG_LINE_1_N > MSG_LINE_1_MAX )); then
echo_error "First line must not have >$MSG_LINE_1_MAX ($MSG_LINE_1_N) characters!" "$MSG_LINE_1"
elif ! [[ $MSG_LINE_1 =~ $REGEX_LINE_1 ]]; then
echo_error "First line must match pattern '$REGEX_LINE_1'!" "$MSG_LINE_1"
fi
# Validate message gap (line 2)
if (( MSG_LINES_N >= 2 )); then
MSG_LINE_2=$(echo "$MSG" | head -n2 | tail -n1)
if [[ $MSG_LINE_2 ]]; then
echo_error 'Second line must be empty!' "$MSG_LINE_2"
fi
fi
# Validate message details (lines 3+)
if (( MSG_LINES_N >= 3 )); then
MSG_LINE_3=$(echo "$MSG" | tail -n+3)
REGEX_LINE_3='^[[:upper:]].*\.$'
if ! [[ $MSG_LINE_3 =~ $REGEX_LINE_3 ]]; then
echo_error "Third line and those after must match pattern '$REGEX_LINE_3'!" "$MSG_LINE_3"
fi
fi