summaryrefslogtreecommitdiffstats
path: root/examples/my_line_rules.py
diff options
context:
space:
mode:
authorJoris Roovers <jroovers@cisco.com>2016-08-04 11:48:38 +0200
committerJoris Roovers <jroovers@cisco.com>2016-08-04 11:48:38 +0200
commitc6077c23d6f815a8b96382fd40328458614e1f08 (patch)
tree9ec5ecb813611b1d93a779609360debe7f625bfd /examples/my_line_rules.py
parent9ff92ee6b77b7a4fbd4cc31bed2fcd8e5ff68d1e (diff)
Initial support for user-defined rules
This is the initial commit adding support for user-defined rules. It's a rather large commit, but I decided to keep a few things out to still try to keep the size manageable. In particular, there will be follow-up commits adding more tests, documentation and validation checks as well as general cleanup. What is present in the current commit: - Support for the 'extra-path' parameter that specifies where to search for user-defined rules - A new user_rules.py module that implements the business logic for finding user-defined rule classes - Example user-defined rules in examples/my_commit_rules.py and examples/my_line_rules.py - A new DirectoryOption class for specifying directory paths as rule options - ./run_tests.sh now print py.test warnings
Diffstat (limited to 'examples/my_line_rules.py')
-rw-r--r--examples/my_line_rules.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/examples/my_line_rules.py b/examples/my_line_rules.py
new file mode 100644
index 0000000..e5dda59
--- /dev/null
+++ b/examples/my_line_rules.py
@@ -0,0 +1,45 @@
+from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle
+from gitlint.options import ListOption
+
+"""
+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
+`target` attribute of the class).
+
+A LineRule contrasts with a CommitRule (see examples/my_commit_rules.py) in that a commit rule is only applied once on
+an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks
+that should only be done once per gitlint run.
+
+While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if
+that fits your needs.
+"""
+
+
+class SpecialChars(LineRule):
+ """ 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"
+
+ # A rule MUST have an *unique* id, we recommend starting with UL (for User-defined Line-rule), but this can
+ # really be anything.
+ id = "UL1"
+
+ # A line-rule MUST have a target (not required for CommitRules).
+ 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")]
+
+ def validate(self, line, _commit):
+ violations = []
+ # options can be accessed by looking them up by their name in self.options
+ for char in self.options['special-chars'].value:
+ if char in line:
+ violation = RuleViolation(self.id, "Title contains the special character '{}'".format(char), line)
+ violations.append(violation)
+
+ return violations