2021-05-02 11:16:21 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# the file containing the commit message is passed as the first argument
|
|
|
|
commit_file="$1"
|
|
|
|
commit_message=$(cat "$commit_file")
|
|
|
|
|
|
|
|
error() {
|
|
|
|
echo -e "\033[0;31m$1:\033[0m"
|
|
|
|
echo "$commit_message"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# fail if the commit message contains windows style line breaks (carriage returns)
|
|
|
|
if grep -q -U $'\x0D' "$commit_file"; then
|
|
|
|
error "Commit message contains CRLF line breaks (only unix-style LF linebreaks are allowed)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
line_number=0
|
|
|
|
while read -r line; do
|
2021-07-27 20:12:33 +00:00
|
|
|
# break on git cut line, used by git commit --verbose
|
|
|
|
if [[ "$line" == "# ------------------------ >8 ------------------------" ]]; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
2021-05-02 11:16:21 +00:00
|
|
|
# ignore comment lines
|
|
|
|
[[ "$line" =~ ^#.* ]] && continue
|
2021-11-28 18:59:23 +00:00
|
|
|
# ignore overlong 'fixup!' commit descriptions
|
|
|
|
[[ "$line" =~ ^fixup!\ .* ]] && continue
|
2021-05-02 11:16:21 +00:00
|
|
|
|
|
|
|
((line_number += 1))
|
|
|
|
line_length=${#line}
|
|
|
|
|
2022-01-11 21:31:08 +00:00
|
|
|
if [[ $line_number -eq 2 ]] && [[ $line_length -ne 0 ]]; then
|
|
|
|
error "Empty line between commit title and body is missing"
|
|
|
|
fi
|
|
|
|
|
2022-11-19 15:53:54 +00:00
|
|
|
merge_commit_pattern="^Merge branch"
|
|
|
|
if [[ $line_number -eq 1 ]] && (echo "$line" | grep -E -q "$merge_commit_pattern"); then
|
|
|
|
error "Commit is a git merge commit, use the rebase command instead"
|
|
|
|
fi
|
|
|
|
|
2022-09-14 20:48:54 +00:00
|
|
|
category_pattern='^(Revert "|\S+: )'
|
2021-10-26 18:08:39 +00:00
|
|
|
if [[ $line_number -eq 1 ]] && (echo "$line" | grep -E -v -q "$category_pattern"); then
|
2021-05-02 11:16:21 +00:00
|
|
|
error "Missing category in commit title (if this is a fix up of a previous commit, it should be squashed)"
|
|
|
|
fi
|
|
|
|
|
2021-07-27 19:37:17 +00:00
|
|
|
title_case_pattern="^\S.*?: [A-Z0-9]"
|
2021-10-26 18:08:39 +00:00
|
|
|
if [[ $line_number -eq 1 ]] && (echo "$line" | grep -E -v -q "$title_case_pattern"); then
|
2021-07-27 19:37:17 +00:00
|
|
|
error "First word of commit after the subsystem is not capitalized"
|
|
|
|
fi
|
|
|
|
|
2021-05-15 23:59:04 +00:00
|
|
|
if [[ $line_number -eq 1 ]] && [[ "$line" =~ \.$ ]]; then
|
|
|
|
error "Commit title ends in a period"
|
|
|
|
fi
|
|
|
|
|
2021-07-27 19:37:17 +00:00
|
|
|
url_pattern="([a-z]+:\/\/)?(([a-zA-Z0-9_]|-)+\.)+[a-z]{2,}(:\d+)?([a-zA-Z_0-9@:%\+.~\?&\/=]|-)+"
|
2021-10-26 18:08:39 +00:00
|
|
|
if [[ $line_length -gt 72 ]] && (echo "$line" | grep -E -v -q "$url_pattern"); then
|
2021-05-02 11:16:21 +00:00
|
|
|
error "Commit message lines are too long (maximum allowed is 72 characters)"
|
|
|
|
fi
|
2021-09-07 19:36:17 +00:00
|
|
|
|
|
|
|
if [[ "$line" == "Signed-off-by: "* ]]; then
|
|
|
|
error "Commit body contains a Signed-off-by tag"
|
|
|
|
fi
|
|
|
|
|
2021-05-02 11:16:21 +00:00
|
|
|
done <"$commit_file"
|
|
|
|
exit 0
|