Skip to content

Commit e710b0f

Browse files
committed
formal: check for more guideline violations
Check if first word after prefix is capitalized. Check if subject ends with a period. Check subject line and body line lengths and colorize the excess. Signed-off-by: George Sapkin <[email protected]>
1 parent 8924f0b commit e710b0f

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

.github/scripts/check_formalities.sh

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash
22

3+
# Based on https://openwrt.org/submitting-patches#submission_guidelines
4+
MAX_SUBJECT_LEN=50
5+
MAX_BODY_LINE_LEN=75
6+
37
source workflow_context/.github/scripts/ci_helpers.sh
48

59
RET=0
@@ -48,19 +52,63 @@ for commit in $(git rev-list HEAD ^origin/"$BRANCH"); do
4852
RET=1
4953
fi
5054

55+
echo
56+
info "Checking subject:"
5157
subject="$(git show -s --format=%s "$commit")"
52-
if echo "$subject" | grep -q -e '^[0-9A-Za-z,+/_-]\+: ' -e '^Revert '; then
53-
success "Commit subject line seems OK ($subject)"
54-
elif echo "$subject" | grep -iq '^Translated using Weblate.*'; then
55-
warn "Weblate commit subject line exception: $subject"
56-
elif echo "$subject" | grep -iq '^Added translation using Weblate.*'; then
57-
warn "Weblate commit subject line exception: $subject"
58+
echo "$subject"
59+
60+
# Check subject format
61+
if echo "$subject" | grep -iq -e '^Translated using Weblate.*' -e '^Added translation using Weblate.*'; then
62+
warn "Weblate commit subject line exception"
63+
elif echo "$subject" | grep -q -e '^[0-9A-Za-z,+/_-]\+: [a-z]' -e '^Revert '; then
64+
success "Commit subject line format seems OK"
65+
elif echo "$subject" | grep -q -e '^[0-9A-Za-z,+/_-]\+: [A-Z]'; then
66+
err "First word after prefix in subject should not be capitalized"
67+
RET=1
68+
elif echo "$subject" | grep -q -e '^[0-9A-Za-z,+/_-]\+: '; then
69+
# Handles cases when there's a prefix but the check for capitalization fails (e.g. no word
70+
# after prefix)
71+
err "Commit subject line MUST start with '<package name>: ' and be followed by a lower-case word"
72+
RET=1
5873
else
59-
err "Commit subject line MUST start with '<package name>: ' ($subject)"
74+
err "Commit subject line MUST start with '<package name>: '"
75+
RET=1
76+
fi
77+
78+
if echo "$subject" | grep -q '\.$'; then
79+
err "Commit subject line should not end with a period"
6080
RET=1
6181
fi
6282

83+
# Check subject length
84+
if [ ${#subject} -gt "$MAX_SUBJECT_LEN" ]; then
85+
warn "Commit subject line is longer that $MAX_SUBJECT_LEN characters (is ${#subject})"
86+
split_err "$MAX_SUBJECT_LEN" "$subject"
87+
else
88+
success "Commit subject line is $MAX_SUBJECT_LEN characters or less"
89+
fi
90+
91+
echo
92+
info "Checking body:"
6393
body="$(git show -s --format=%b "$commit")"
94+
echo "$body"
95+
echo
96+
97+
# Check body line lengths
98+
body_line_too_long=0
99+
line_num=0
100+
while IFS= read -r line; do
101+
line_num=$((line_num + 1))
102+
if [ ${#line} -gt "$MAX_BODY_LINE_LEN" ]; then
103+
warn "Commit body line $line_num is longer than $MAX_BODY_LINE_LEN characters (is ${#line}):"
104+
split_err "$MAX_BODY_LINE_LEN" "$line"
105+
body_line_too_long=1
106+
fi
107+
done <<< "$body"
108+
if [ "$body_line_too_long" = 0 ]; then
109+
success "Commit body lines are $MAX_BODY_LINE_LEN characters or less"
110+
fi
111+
64112
sob="$(git show -s --format='Signed-off-by: %aN <%aE>' "$commit")"
65113
if echo "$body" | grep -qF "$sob"; then
66114
success "Signed-off-by matches author"
@@ -78,12 +126,15 @@ for commit in $(git rev-list HEAD ^origin/"$BRANCH"); do
78126
success "Signed-off-by email is not a GitHub noreply email"
79127
fi
80128

81-
if echo "$body" | grep -v "Signed-off-by:"; then
129+
if echo "$body" | grep -qv "Signed-off-by:"; then
82130
success "A commit message exists"
83131
else
84132
err "Commit message is missing. Please describe your changes."
85133
RET=1
86134
fi
135+
136+
info "=== Done checking commit '$commit'"
137+
echo
87138
done
88139

89140
exit $RET

.github/scripts/ci_helpers.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ warn() {
2020
color_out 33 "$1"
2121
}
2222

23+
# Prints the string and colors the part after the given length in red
24+
split_err() {
25+
printf "%s\e[0;31m%s\e[0;0m\n" "${2:0:$1}" "${2:$1}"
26+
}
27+
2328
err_die() {
2429
err "$1"
2530
exit 1

0 commit comments

Comments
 (0)