blob: d7e5483efaced801bdb9852cd83331cd1dee9a1a [file] [log] [blame]
Anas Nashifa35378e2017-04-22 11:59:30 -04001from gitlint.rules import CommitRule, RuleViolation
2from gitlint.options import IntOption
3
4"""
5The classes below are examples of user-defined CommitRules. Commit rules are gitlint rules that
6act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them
7to the entire commit. This happens exactly once per commit.
8
9A CommitRule contrasts with a LineRule (see examples/my_line_rules.py) in that a commit rule is only applied once on
10an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks
11that should only be done once per gitlint run.
12
13While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if
14that fits your needs.
15"""
16
17
18class 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
36class 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)]