summaryrefslogtreecommitdiffstats
path: root/examples/my_line_rules.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/my_line_rules.py')
-rw-r--r--examples/my_line_rules.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/examples/my_line_rules.py b/examples/my_line_rules.py
index cc69fb9..58b0108 100644
--- a/examples/my_line_rules.py
+++ b/examples/my_line_rules.py
@@ -2,6 +2,8 @@ from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle
from gitlint.options import ListOption
"""
+Full details on user-defined rules: https://jorisroovers.com/gitlint/user_defined_rules
+
The SpecialChars class below is an example of a user-defined LineRule. Line rules are gitlint rules that only act on a
single line at once. Once the rule is discovered, gitlint will automatically take care of applying this rule
against each line of the commit message title or body (whether it is applied to the title or body is determined by the
@@ -17,8 +19,8 @@ that fits your needs.
class SpecialChars(LineRule):
- """ This rule will enforce that the commit message title does not contain any of the following characters:
- $^%@!*() """
+ """This rule will enforce that the commit message title does not contain any of the following characters:
+ $^%@!*()"""
# A rule MUST have a human friendly name
name = "title-no-special-chars"
@@ -31,15 +33,23 @@ class SpecialChars(LineRule):
target = CommitMessageTitle
# A rule MAY have an option_spec if its behavior should be configurable.
- options_spec = [ListOption('special-chars', ['$', '^', '%', '@', '!', '*', '(', ')'],
- "Comma separated list of characters that should not occur in the title")]
+ options_spec = [
+ ListOption(
+ "special-chars",
+ ["$", "^", "%", "@", "!", "*", "(", ")"],
+ "Comma separated list of characters that should not occur in the title",
+ )
+ ]
def validate(self, line, _commit):
+ self.log.debug("SpecialChars: This will be visible when running `gitlint --debug`")
+
violations = []
# options can be accessed by looking them up by their name in self.options
- for char in self.options['special-chars'].value:
+ for char in self.options["special-chars"].value:
if char in line:
- violation = RuleViolation(self.id, "Title contains the special character '{0}'".format(char), line)
+ msg = f"Title contains the special character '{char}'"
+ violation = RuleViolation(self.id, msg, line)
violations.append(violation)
return violations