Anas Nashif | b520075 | 2017-06-06 08:50:11 -0400 | [diff] [blame] | 1 | from gitlint.rules import CommitRule, RuleViolation, TitleRegexMatches, CommitMessageTitle, LineRule, CommitMessageBody |
Anas Nashif | 3c27c46 | 2017-05-05 19:37:52 -0400 | [diff] [blame] | 2 | from gitlint.options import IntOption, BoolOption, StrOption, ListOption |
Anas Nashif | 1338f49 | 2017-05-05 16:39:34 -0400 | [diff] [blame] | 3 | import re |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 4 | |
| 5 | """ |
| 6 | The classes below are examples of user-defined CommitRules. Commit rules are gitlint rules that |
| 7 | act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them |
| 8 | to the entire commit. This happens exactly once per commit. |
| 9 | |
| 10 | A CommitRule contrasts with a LineRule (see examples/my_line_rules.py) in that a commit rule is only applied once on |
| 11 | an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks |
| 12 | that should only be done once per gitlint run. |
| 13 | |
| 14 | While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if |
| 15 | that fits your needs. |
| 16 | """ |
| 17 | |
| 18 | |
| 19 | class BodyMaxLineCount(CommitRule): |
| 20 | # A rule MUST have a human friendly name |
| 21 | name = "body-max-line-count" |
| 22 | |
| 23 | # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule). |
| 24 | id = "UC1" |
| 25 | |
| 26 | # A rule MAY have an option_spec if its behavior should be configurable. |
| 27 | options_spec = [IntOption('max-line-count', 3, "Maximum body line count")] |
| 28 | |
| 29 | def validate(self, commit): |
| 30 | line_count = len(commit.message.body) |
| 31 | max_line_count = self.options['max-line-count'].value |
| 32 | if line_count > max_line_count: |
| 33 | message = "Body contains too many lines ({0} > {1})".format(line_count, max_line_count) |
| 34 | return [RuleViolation(self.id, message, line_nr=1)] |
| 35 | |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 36 | class SignedOffBy(CommitRule): |
| 37 | """ This rule will enforce that each commit contains a "Signed-Off-By" line. |
| 38 | We keep things simple here and just check whether the commit body contains a line that starts with "Signed-Off-By". |
| 39 | """ |
| 40 | |
| 41 | # A rule MUST have a human friendly name |
| 42 | name = "body-requires-signed-off-by" |
| 43 | |
| 44 | # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule). |
| 45 | id = "UC2" |
| 46 | |
| 47 | def validate(self, commit): |
Anas Nashif | 1338f49 | 2017-05-05 16:39:34 -0400 | [diff] [blame] | 48 | flags = re.UNICODE |
| 49 | flags |= re.IGNORECASE |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 50 | for line in commit.message.body: |
| 51 | if line.lower().startswith("signed-off-by"): |
Anas Nashif | 1338f49 | 2017-05-05 16:39:34 -0400 | [diff] [blame] | 52 | if not re.search('(^)Signed-off-by: ([-\w.]+) ([-\w.]+) (.*)', line, flags=flags): |
| 53 | return [RuleViolation(self.id, "Signed-off-by: must have a full name", line_nr=1)] |
| 54 | else: |
| 55 | return |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 56 | return [RuleViolation(self.id, "Body does not contain a 'Signed-Off-By' line", line_nr=1)] |
Anas Nashif | 3c27c46 | 2017-05-05 19:37:52 -0400 | [diff] [blame] | 57 | |
Anas Nashif | 87766a2 | 2017-08-08 08:36:01 -0400 | [diff] [blame] | 58 | class TitleMaxLengthRevert(LineRule): |
| 59 | name = "title-max-length-no-revert" |
| 60 | id = "UC5" |
| 61 | target = CommitMessageTitle |
| 62 | options_spec = [IntOption('line-length', 72, "Max line length")] |
| 63 | violation_message = "Title exceeds max length ({0}>{1})" |
| 64 | |
| 65 | def validate(self, line, _commit): |
| 66 | max_length = self.options['line-length'].value |
| 67 | if len(line) > max_length and not line.startswith("Revert"): |
| 68 | return [RuleViolation(self.id, self.violation_message.format(len(line), max_length), line)] |
Anas Nashif | 3c27c46 | 2017-05-05 19:37:52 -0400 | [diff] [blame] | 69 | |
| 70 | class TitleStartsWithSubsystem(LineRule): |
| 71 | name = "title-starts-with-subsystem" |
| 72 | id = "UC3" |
| 73 | target = CommitMessageTitle |
| 74 | options_spec = [StrOption('regex', ".*", "Regex the title should match")] |
| 75 | |
| 76 | def validate(self, title, _commit): |
| 77 | regex = self.options['regex'].value |
| 78 | pattern = re.compile(regex, re.UNICODE) |
Anas Nashif | 789d51c | 2017-10-27 14:29:59 -0400 | [diff] [blame] | 79 | violation_message = "Title does not follow [subsystem]: [subject]" |
Anas Nashif | 3c27c46 | 2017-05-05 19:37:52 -0400 | [diff] [blame] | 80 | if not pattern.search(title): |
| 81 | return [RuleViolation(self.id, violation_message, title)] |
Anas Nashif | b520075 | 2017-06-06 08:50:11 -0400 | [diff] [blame] | 82 | |
| 83 | class MaxLineLengthExceptions(LineRule): |
| 84 | name = "max-line-length-with-exceptions" |
| 85 | id = "UC4" |
| 86 | target = CommitMessageBody |
| 87 | options_spec = [IntOption('line-length', 80, "Max line length")] |
| 88 | violation_message = "Line exceeds max length ({0}>{1})" |
| 89 | |
| 90 | def validate(self, line, _commit): |
| 91 | max_length = self.options['line-length'].value |
Anas Nashif | 408a61d | 2017-08-08 08:07:00 -0400 | [diff] [blame] | 92 | urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line) |
| 93 | if line.startswith('Signed-off-by'): |
| 94 | return |
| 95 | |
| 96 | if len(urls) > 0: |
| 97 | return |
| 98 | |
| 99 | if len(line) > max_length: |
Anas Nashif | b520075 | 2017-06-06 08:50:11 -0400 | [diff] [blame] | 100 | return [RuleViolation(self.id, self.violation_message.format(len(line), max_length), line)] |