Skip to content

Commit

Permalink
Meta: Ignore title length of revert commits
Browse files Browse the repository at this point in the history
Even if a commit message title previously fit into the 72 character
limit it may go over that with 'Revert ""' added - asking the author to
tweak the commit message is not appropriate in that case.

Since additional context can be added to the commit message body the
usual length restriction applies to the remaining lines.

Also restructure the code to group checks based on the line number we're
currently looking at.
  • Loading branch information
linusg committed Dec 16, 2024
1 parent 7bcf97c commit 8c7c758
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions Meta/lint-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,43 @@ while read -r line; do
((line_number += 1))
line_length=${#line}

if [[ $line_number -eq 2 ]] && [[ $line_length -ne 0 ]]; then
error "Empty line between commit title and body is missing"
fi
if [[ $line_number -eq 1 ]]; then
merge_commit_pattern="^Merge branch"
if (echo "$line" | grep -E -q "$merge_commit_pattern"); then
error "Commit is a git merge commit, use the rebase command instead"
fi

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
category_pattern='^(Revert "|\S+: )'
if (echo "$line" | grep -E -v -q "$category_pattern"); then
error "Missing category in commit title (if this is a fix up of a previous commit, it should be squashed)"
fi

category_pattern='^(Revert "|\S+: )'
if [[ $line_number -eq 1 ]] && (echo "$line" | grep -E -v -q "$category_pattern"); then
error "Missing category in commit title (if this is a fix up of a previous commit, it should be squashed)"
fi
revert_pattern='^Revert "'
if [[ $line_length -gt 72 ]] && (echo "$line" | grep -E -v -q "$revert_pattern"); then
error "Commit title is too long (maximum allowed is 72 characters)"
fi

title_case_pattern="^\S.*?: [A-Z0-9]"
if [[ $line_number -eq 1 ]] && (echo "$line" | grep -E -v -q "$title_case_pattern"); then
error "First word of commit after the subsystem is not capitalized"
fi
title_case_pattern="^\S.*?: [A-Z0-9]"
if (echo "$line" | grep -E -v -q "$title_case_pattern"); then
error "First word of commit after the subsystem is not capitalized"
fi

if [[ $line_number -eq 1 ]] && [[ "$line" =~ \.$ ]]; then
error "Commit title ends in a period"
fi

url_pattern="([a-z]+:\/\/)?(([a-zA-Z0-9_]|-)+\.)+[a-z]{2,}(:\d+)?([a-zA-Z_0-9@:%\+.~\?&\/=]|-)+"
if [[ $line_length -gt 72 ]] && (echo "$line" | grep -E -v -q "$url_pattern"); then
error "Commit message lines are too long (maximum allowed is 72 characters)"
fi
if [[ "$line" =~ \.$ ]]; then
error "Commit title ends in a period"
fi
elif [[ $line_number -eq 2 ]]; then
if [[ $line_length -ne 0 ]]; then
error "Empty line between commit title and body is missing"
fi
else
url_pattern="([a-z]+:\/\/)?(([a-zA-Z0-9_]|-)+\.)+[a-z]{2,}(:\d+)?([a-zA-Z_0-9@:%\+.~\?&\/=]|-)+"
if [[ $line_length -gt 72 ]] && (echo "$line" | grep -E -v -q "$url_pattern"); then
error "Commit message lines are too long (maximum allowed is 72 characters)"
fi

if [[ "$line" == "Signed-off-by: "* ]]; then
error "Commit body contains a Signed-off-by tag"
if [[ "$line" == "Signed-off-by: "* ]]; then
error "Commit body contains a Signed-off-by tag"
fi
fi

done <"$commit_file"
Expand Down

0 comments on commit 8c7c758

Please sign in to comment.