Anas Nashif | 3ae5262 | 2019-04-06 09:08:09 -0400 | [diff] [blame] | 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 3 | """ |
| 4 | The classes below are examples of user-defined CommitRules. Commit rules are gitlint rules that |
| 5 | act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them |
| 6 | to the entire commit. This happens exactly once per commit. |
| 7 | |
| 8 | A CommitRule contrasts with a LineRule (see examples/my_line_rules.py) in that a commit rule is only applied once on |
| 9 | an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks |
| 10 | that should only be done once per gitlint run. |
| 11 | |
| 12 | While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if |
| 13 | that fits your needs. |
| 14 | """ |
| 15 | |
Ulf Magnusson | d5b0bd1 | 2019-03-19 19:37:07 +0100 | [diff] [blame] | 16 | from gitlint.rules import CommitRule, RuleViolation, CommitMessageTitle, LineRule, CommitMessageBody |
| 17 | from gitlint.options import IntOption, StrOption |
Ulf Magnusson | 7da0053 | 2019-03-25 20:23:42 +0100 | [diff] [blame] | 18 | import re |
| 19 | |
Anas Nashif | 2dd5cef | 2018-01-10 19:12:00 -0500 | [diff] [blame] | 20 | class BodyMinLineCount(CommitRule): |
| 21 | # A rule MUST have a human friendly name |
| 22 | name = "body-min-line-count" |
| 23 | |
| 24 | # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule). |
| 25 | id = "UC6" |
| 26 | |
Marc Herbert | 8e7c7c6 | 2023-09-20 19:45:22 +0000 | [diff] [blame] | 27 | # A rule MAY have an options_spec if its behavior should be configurable. |
| 28 | options_spec = [IntOption('min-line-count', 1, "Minimum body line count excluding Signed-off-by")] |
Anas Nashif | 2dd5cef | 2018-01-10 19:12:00 -0500 | [diff] [blame] | 29 | |
| 30 | def validate(self, commit): |
| 31 | filtered = [x for x in commit.message.body if not x.lower().startswith("signed-off-by") and x != ''] |
| 32 | line_count = len(filtered) |
| 33 | min_line_count = self.options['min-line-count'].value |
Anas Nashif | abfed53 | 2018-01-11 09:57:03 -0500 | [diff] [blame] | 34 | if line_count < min_line_count: |
Marti Bolivar | 2149d21 | 2023-01-27 15:06:48 -0800 | [diff] [blame] | 35 | message = "Commit message body is empty, should at least have {} line(s).".format(min_line_count) |
Anas Nashif | 2dd5cef | 2018-01-10 19:12:00 -0500 | [diff] [blame] | 36 | return [RuleViolation(self.id, message, line_nr=1)] |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 37 | |
| 38 | class BodyMaxLineCount(CommitRule): |
| 39 | # A rule MUST have a human friendly name |
| 40 | name = "body-max-line-count" |
| 41 | |
| 42 | # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule). |
| 43 | id = "UC1" |
| 44 | |
Marc Herbert | 8e7c7c6 | 2023-09-20 19:45:22 +0000 | [diff] [blame] | 45 | # A rule MAY have an options_spec if its behavior should be configurable. |
| 46 | options_spec = [IntOption('max-line-count', 200, "Maximum body line count")] |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 47 | |
| 48 | def validate(self, commit): |
| 49 | line_count = len(commit.message.body) |
| 50 | max_line_count = self.options['max-line-count'].value |
| 51 | if line_count > max_line_count: |
Marti Bolivar | 2149d21 | 2023-01-27 15:06:48 -0800 | [diff] [blame] | 52 | message = "Commit message body contains too many lines ({0} > {1})".format(line_count, max_line_count) |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 53 | return [RuleViolation(self.id, message, line_nr=1)] |
| 54 | |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 55 | class SignedOffBy(CommitRule): |
Paul Sokolovsky | 26e562c | 2019-02-18 19:52:59 +0300 | [diff] [blame] | 56 | """ This rule will enforce that each commit contains a "Signed-off-by" line. |
| 57 | We keep things simple here and just check whether the commit body contains a line that starts with "Signed-off-by". |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 58 | """ |
| 59 | |
| 60 | # A rule MUST have a human friendly name |
| 61 | name = "body-requires-signed-off-by" |
| 62 | |
| 63 | # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule). |
| 64 | id = "UC2" |
| 65 | |
| 66 | def validate(self, commit): |
Anas Nashif | 1338f49 | 2017-05-05 16:39:34 -0400 | [diff] [blame] | 67 | flags = re.UNICODE |
| 68 | flags |= re.IGNORECASE |
Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 69 | for line in commit.message.body: |
| 70 | if line.lower().startswith("signed-off-by"): |
Ulf Magnusson | a449c98 | 2019-03-21 21:38:03 +0100 | [diff] [blame] | 71 | if not re.search(r"(^)Signed-off-by: ([-'\w.]+) ([-'\w.]+) (.*)", line, flags=flags): |
Anas Nashif | 1338f49 | 2017-05-05 16:39:34 -0400 | [diff] [blame] | 72 | return [RuleViolation(self.id, "Signed-off-by: must have a full name", line_nr=1)] |
| 73 | else: |
| 74 | return |
Marti Bolivar | 2149d21 | 2023-01-27 15:06:48 -0800 | [diff] [blame] | 75 | return [RuleViolation(self.id, "Commit message does not contain a 'Signed-off-by:' line", line_nr=1)] |
Anas Nashif | 3c27c46 | 2017-05-05 19:37:52 -0400 | [diff] [blame] | 76 | |
Anas Nashif | 87766a2 | 2017-08-08 08:36:01 -0400 | [diff] [blame] | 77 | class TitleMaxLengthRevert(LineRule): |
| 78 | name = "title-max-length-no-revert" |
| 79 | id = "UC5" |
| 80 | target = CommitMessageTitle |
Marc Herbert | 8e7c7c6 | 2023-09-20 19:45:22 +0000 | [diff] [blame] | 81 | options_spec = [IntOption('line-length', 75, "Max line length")] |
Tristan Honscheid | ae97971 | 2022-12-06 16:43:45 -0700 | [diff] [blame] | 82 | violation_message = "Commit title exceeds max length ({0}>{1})" |
Anas Nashif | 87766a2 | 2017-08-08 08:36:01 -0400 | [diff] [blame] | 83 | |
| 84 | def validate(self, line, _commit): |
| 85 | max_length = self.options['line-length'].value |
| 86 | if len(line) > max_length and not line.startswith("Revert"): |
| 87 | 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] | 88 | |
| 89 | class TitleStartsWithSubsystem(LineRule): |
| 90 | name = "title-starts-with-subsystem" |
| 91 | id = "UC3" |
| 92 | target = CommitMessageTitle |
| 93 | options_spec = [StrOption('regex', ".*", "Regex the title should match")] |
| 94 | |
| 95 | def validate(self, title, _commit): |
| 96 | regex = self.options['regex'].value |
| 97 | pattern = re.compile(regex, re.UNICODE) |
Tristan Honscheid | ae97971 | 2022-12-06 16:43:45 -0700 | [diff] [blame] | 98 | violation_message = "Commit title does not follow [subsystem]: [subject] (and should not start with literal subsys:)" |
Anas Nashif | 3c27c46 | 2017-05-05 19:37:52 -0400 | [diff] [blame] | 99 | if not pattern.search(title): |
| 100 | return [RuleViolation(self.id, violation_message, title)] |
Anas Nashif | b520075 | 2017-06-06 08:50:11 -0400 | [diff] [blame] | 101 | |
| 102 | class MaxLineLengthExceptions(LineRule): |
| 103 | name = "max-line-length-with-exceptions" |
| 104 | id = "UC4" |
| 105 | target = CommitMessageBody |
Marc Herbert | 8e7c7c6 | 2023-09-20 19:45:22 +0000 | [diff] [blame] | 106 | options_spec = [IntOption('line-length', 75, "Max line length")] |
Marti Bolivar | 2149d21 | 2023-01-27 15:06:48 -0800 | [diff] [blame] | 107 | violation_message = "Commit message body line exceeds max length ({0}>{1})" |
Anas Nashif | b520075 | 2017-06-06 08:50:11 -0400 | [diff] [blame] | 108 | |
| 109 | def validate(self, line, _commit): |
| 110 | max_length = self.options['line-length'].value |
Ulf Magnusson | a449c98 | 2019-03-21 21:38:03 +0100 | [diff] [blame] | 111 | urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line) |
Anas Nashif | 9e87bd7 | 2023-09-26 23:04:33 +0000 | [diff] [blame] | 112 | if line.lower().startswith('signed-off-by') or line.lower().startswith('co-authored-by'): |
Anas Nashif | 408a61d | 2017-08-08 08:07:00 -0400 | [diff] [blame] | 113 | return |
| 114 | |
Ulf Magnusson | 1c57b22 | 2019-09-02 14:38:31 +0200 | [diff] [blame] | 115 | if urls: |
Anas Nashif | 408a61d | 2017-08-08 08:07:00 -0400 | [diff] [blame] | 116 | return |
| 117 | |
| 118 | if len(line) > max_length: |
Anas Nashif | b520075 | 2017-06-06 08:50:11 -0400 | [diff] [blame] | 119 | return [RuleViolation(self.id, self.violation_message.format(len(line), max_length), line)] |
Fabio Baltieri | d42b2e6 | 2022-04-20 15:06:11 +0100 | [diff] [blame] | 120 | |
| 121 | class BodyContainsBlockedTags(LineRule): |
| 122 | name = "body-contains-blocked-tags" |
| 123 | id = "UC7" |
| 124 | target = CommitMessageBody |
| 125 | tags = ["Change-Id"] |
| 126 | |
| 127 | def validate(self, line, _commit): |
| 128 | flags = re.IGNORECASE |
| 129 | for tag in self.tags: |
| 130 | if re.search(rf"^\s*{tag}:", line, flags=flags): |
Marti Bolivar | 2149d21 | 2023-01-27 15:06:48 -0800 | [diff] [blame] | 131 | return [RuleViolation(self.id, f"Commit message contains a blocked tag: {tag}")] |
Fabio Baltieri | d42b2e6 | 2022-04-20 15:06:11 +0100 | [diff] [blame] | 132 | return |