diff --git a/.gitlint b/.gitlint index a30bdb6472..0afb421327 100644 --- a/.gitlint +++ b/.gitlint @@ -1,9 +1,9 @@ [general] -ignore=title-trailing-punctuation, body-min-length, body-is-missing +ignore=title-trailing-punctuation, body-min-length, body-is-missing, title-imperative-mood -# extra-path=tools/lib/gitlint-rules.py +extra-path=tools/lib/gitlint-rules.py -[title-match-regex] +[title-match-regex-allow-exception] regex=^(.+:\ )?[A-Z].+\.$ [title-max-length] diff --git a/tools/lib/gitlint-rules.py b/tools/lib/gitlint-rules.py index 58eba8fbda..8045ad575a 100644 --- a/tools/lib/gitlint-rules.py +++ b/tools/lib/gitlint-rules.py @@ -2,6 +2,8 @@ from typing import Text, List import gitlint from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle +from gitlint.options import StrOption +import re # Word list from https://github.com/m1foley/fit-commit # Copyright (c) 2015 Mike Foley @@ -141,3 +143,23 @@ class ImperativeMood(LineRule): violations.append(violation) return violations + + +class TitleMatchRegexAllowException(LineRule): + """Allows revert commits contrary to the built-in title-match-regex rule""" + + name = 'title-match-regex-allow-exception' + id = 'Z2' + target = CommitMessageTitle + options_spec = [StrOption('regex', ".*", "Regex the title should match")] + + def validate(self, title, commit): + # type: (Text, gitlint.commit) -> List[RuleViolation] + + regex = self.options['regex'].value + pattern = re.compile(regex, re.UNICODE) + if not pattern.search(title) and not title.startswith("Revert \""): + violation_msg = u"Title does not match regex ({0})".format(regex) + return [RuleViolation(self.id, violation_msg, title)] + + return []