| 1 |
from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle |
| 2 |
from gitlint.options import ListOption |
| 3 |
|
| 4 |
""" |
| 5 |
Full details on user-defined rules: https://jorisroovers.com/gitlint/user_defined_rules |
| 6 |
|
| 7 |
The SpecialChars class below is an example of a user-defined LineRule. Line rules are gitlint rules that only act on a |
| 8 |
single line at once. Once the rule is discovered, gitlint will automatically take care of applying this rule |
| 9 |
against each line of the commit message title or body (whether it is applied to the title or body is determined by the |
| 10 |
`target` attribute of the class). |
| 11 |
|
| 12 |
A LineRule contrasts with a CommitRule (see examples/my_commit_rules.py) in that a commit rule is only applied once on |
| 13 |
an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks |
| 14 |
that should only be done once per gitlint run. |
| 15 |
|
| 16 |
While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if |
| 17 |
that fits your needs. |
| 18 |
""" |
| 19 |
|
| 20 |
|
| 21 |
class SpecialChars(LineRule): |
| 22 |
"""This rule will enforce that the commit message title does not contain any of the following characters: |
| 23 |
$^%@!*()""" |
| 24 |
|
| 25 |
|
| 26 |
name = "title-no-special-chars" |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
id = "UL1" |
| 31 |
|
| 32 |
|
| 33 |
target = CommitMessageTitle |
| 34 |
|
| 35 |
|
| 36 |
options_spec = [ |
| 37 |
ListOption( |
| 38 |
"special-chars", |
| 39 |
["$", "^", "%", "@", "!", "*", "(", ")"], |
| 40 |
"Comma separated list of characters that should not occur in the title", |
| 41 |
) |
| 42 |
] |
| 43 |
|
| 44 |
def validate(self, line, _commit): |
| 45 |
self.log.debug("SpecialChars: This will be visible when running `gitlint --debug`") |
| 46 |
|
| 47 |
violations = [] |
| 48 |
|
| 49 |
for char in self.options["special-chars"].value: |
| 50 |
if char in line: |
| 51 |
msg = f"Title contains the special character '{char}'" |
| 52 |
violation = RuleViolation(self.id, msg, line) |
| 53 |
violations.append(violation) |
| 54 |
|
| 55 |
return violations |