| 1 |
from gitlint.rules import CommitRule, RuleViolation |
| 2 |
from gitlint.options import IntOption, ListOption |
| 3 |
|
| 4 |
""" |
| 5 |
Full details on user-defined rules: https://jorisroovers.com/gitlint/user_defined_rules |
| 6 |
|
| 7 |
The classes below are examples of user-defined CommitRules. Commit rules are gitlint rules that |
| 8 |
act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them |
| 9 |
to the entire commit. This happens exactly once per commit. |
| 10 |
|
| 11 |
A CommitRule contrasts with a LineRule (see examples/my_line_rules.py) in that a commit rule is only applied once on |
| 12 |
an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks |
| 13 |
that should only be done once per gitlint run. |
| 14 |
|
| 15 |
While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if |
| 16 |
that fits your needs. |
| 17 |
""" |
| 18 |
|
| 19 |
|
| 20 |
class BodyMaxLineCount(CommitRule): |
| 21 |
|
| 22 |
name = "body-max-line-count" |
| 23 |
|
| 24 |
|
| 25 |
id = "UC1" |
| 26 |
|
| 27 |
|
| 28 |
options_spec = [IntOption("max-line-count", 3, "Maximum body line count")] |
| 29 |
|
| 30 |
def validate(self, commit): |
| 31 |
self.log.debug("BodyMaxLineCount: This will be visible when running `gitlint --debug`") |
| 32 |
|
| 33 |
line_count = len(commit.message.body) |
| 34 |
max_line_count = self.options["max-line-count"].value |
| 35 |
if line_count > max_line_count: |
| 36 |
message = f"Body contains too many lines ({line_count} > {max_line_count})" |
| 37 |
return [RuleViolation(self.id, message, line_nr=1)] |
| 38 |
|
| 39 |
|
| 40 |
class SignedOffBy(CommitRule): |
| 41 |
"""This rule will enforce that each commit contains a "Signed-off-by" line. |
| 42 |
We keep things simple here and just check whether the commit body contains a line that starts with "Signed-off-by". |
| 43 |
""" |
| 44 |
|
| 45 |
|
| 46 |
name = "body-requires-signed-off-by" |
| 47 |
|
| 48 |
|
| 49 |
id = "UC2" |
| 50 |
|
| 51 |
def validate(self, commit): |
| 52 |
self.log.debug("SignedOffBy: This will be visible when running `gitlint --debug`") |
| 53 |
|
| 54 |
for line in commit.message.body: |
| 55 |
if line.startswith("Signed-off-by"): |
| 56 |
return |
| 57 |
|
| 58 |
return [RuleViolation(self.id, "Body does not contain a 'Signed-off-by' line", line_nr=1)] |
| 59 |
|
| 60 |
|
| 61 |
class BranchNamingConventions(CommitRule): |
| 62 |
"""This rule will enforce that a commit is part of a branch that meets certain naming conventions. |
| 63 |
See GitFlow for real-world example of this: https://nvie.com/posts/a-successful-git-branching-model/ |
| 64 |
""" |
| 65 |
|
| 66 |
|
| 67 |
name = "branch-naming-conventions" |
| 68 |
|
| 69 |
|
| 70 |
id = "UC3" |
| 71 |
|
| 72 |
|
| 73 |
options_spec = [ListOption("branch-prefixes", ["feature/", "hotfix/", "release/"], "Allowed branch prefixes")] |
| 74 |
|
| 75 |
def validate(self, commit): |
| 76 |
self.log.debug("BranchNamingConventions: This line will be visible when running `gitlint --debug`") |
| 77 |
|
| 78 |
violations = [] |
| 79 |
allowed_branch_prefixes = self.options["branch-prefixes"].value |
| 80 |
for branch in commit.branches: |
| 81 |
valid_branch_name = False |
| 82 |
|
| 83 |
for allowed_prefix in allowed_branch_prefixes: |
| 84 |
if branch.startswith(allowed_prefix): |
| 85 |
valid_branch_name = True |
| 86 |
break |
| 87 |
|
| 88 |
if not valid_branch_name: |
| 89 |
msg = f"Branch name '{branch}' does not start with one of {allowed_branch_prefixes}" |
| 90 |
violations.append(RuleViolation(self.id, msg, line_nr=1)) |
| 91 |
|
| 92 |
return violations |