Anas Nashif | a35378e | 2017-04-22 11:59:30 -0400 | [diff] [blame] | 1 | from gitlint.rules import CommitRule, RuleViolation |
| 2 | from gitlint.options import IntOption |
| 3 | |
| 4 | """ |
| 5 | The classes below are examples of user-defined CommitRules. Commit rules are gitlint rules that |
| 6 | act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them |
| 7 | to the entire commit. This happens exactly once per commit. |
| 8 | |
| 9 | A CommitRule contrasts with a LineRule (see examples/my_line_rules.py) in that a commit rule is only applied once on |
| 10 | an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks |
| 11 | that should only be done once per gitlint run. |
| 12 | |
| 13 | While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if |
| 14 | that fits your needs. |
| 15 | """ |
| 16 | |
| 17 | |
| 18 | class BodyMaxLineCount(CommitRule): |
| 19 | # A rule MUST have a human friendly name |
| 20 | name = "body-max-line-count" |
| 21 | |
| 22 | # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule). |
| 23 | id = "UC1" |
| 24 | |
| 25 | # A rule MAY have an option_spec if its behavior should be configurable. |
| 26 | options_spec = [IntOption('max-line-count', 3, "Maximum body line count")] |
| 27 | |
| 28 | def validate(self, commit): |
| 29 | line_count = len(commit.message.body) |
| 30 | max_line_count = self.options['max-line-count'].value |
| 31 | if line_count > max_line_count: |
| 32 | message = "Body contains too many lines ({0} > {1})".format(line_count, max_line_count) |
| 33 | return [RuleViolation(self.id, message, line_nr=1)] |
| 34 | |
| 35 | |
| 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): |
| 48 | for line in commit.message.body: |
| 49 | if line.lower().startswith("signed-off-by"): |
| 50 | return |
| 51 | |
| 52 | return [RuleViolation(self.id, "Body does not contain a 'Signed-Off-By' line", line_nr=1)] |