blob: cded9e613adade8d5444881ff7415c2db86b579e [file] [log] [blame]
Anas Nashifb5200752017-06-06 08:50:11 -04001from gitlint.rules import CommitRule, RuleViolation, TitleRegexMatches, CommitMessageTitle, LineRule, CommitMessageBody
Anas Nashif3c27c462017-05-05 19:37:52 -04002from gitlint.options import IntOption, BoolOption, StrOption, ListOption
Anas Nashif1338f492017-05-05 16:39:34 -04003import re
Anas Nashifa35378e2017-04-22 11:59:30 -04004
5"""
6The classes below are examples of user-defined CommitRules. Commit rules are gitlint rules that
7act on the entire commit at once. Once the rules are discovered, gitlint will automatically take care of applying them
8to the entire commit. This happens exactly once per commit.
9
10A CommitRule contrasts with a LineRule (see examples/my_line_rules.py) in that a commit rule is only applied once on
11an entire commit. This allows commit rules to implement more complex checks that span multiple lines and/or checks
12that should only be done once per gitlint run.
13
14While every LineRule can be implemented as a CommitRule, it's usually easier and more concise to go with a LineRule if
15that fits your needs.
16"""
17
18
19class BodyMaxLineCount(CommitRule):
20 # A rule MUST have a human friendly name
21 name = "body-max-line-count"
22
23 # A rule MUST have an *unique* id, we recommend starting with UC (for User-defined Commit-rule).
24 id = "UC1"
25
26 # A rule MAY have an option_spec if its behavior should be configurable.
27 options_spec = [IntOption('max-line-count', 3, "Maximum body line count")]
28
29 def validate(self, commit):
30 line_count = len(commit.message.body)
31 max_line_count = self.options['max-line-count'].value
32 if line_count > max_line_count:
33 message = "Body contains too many lines ({0} > {1})".format(line_count, max_line_count)
34 return [RuleViolation(self.id, message, line_nr=1)]
35
Anas Nashifa35378e2017-04-22 11:59:30 -040036class 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):
Anas Nashif1338f492017-05-05 16:39:34 -040048 flags = re.UNICODE
49 flags |= re.IGNORECASE
Anas Nashifa35378e2017-04-22 11:59:30 -040050 for line in commit.message.body:
51 if line.lower().startswith("signed-off-by"):
Anas Nashif1338f492017-05-05 16:39:34 -040052 if not re.search('(^)Signed-off-by: ([-\w.]+) ([-\w.]+) (.*)', line, flags=flags):
53 return [RuleViolation(self.id, "Signed-off-by: must have a full name", line_nr=1)]
54 else:
55 return
Anas Nashifa35378e2017-04-22 11:59:30 -040056 return [RuleViolation(self.id, "Body does not contain a 'Signed-Off-By' line", line_nr=1)]
Anas Nashif3c27c462017-05-05 19:37:52 -040057
Anas Nashif87766a22017-08-08 08:36:01 -040058class TitleMaxLengthRevert(LineRule):
59 name = "title-max-length-no-revert"
60 id = "UC5"
61 target = CommitMessageTitle
62 options_spec = [IntOption('line-length', 72, "Max line length")]
63 violation_message = "Title exceeds max length ({0}>{1})"
64
65 def validate(self, line, _commit):
66 max_length = self.options['line-length'].value
67 if len(line) > max_length and not line.startswith("Revert"):
68 return [RuleViolation(self.id, self.violation_message.format(len(line), max_length), line)]
Anas Nashif3c27c462017-05-05 19:37:52 -040069
70class TitleStartsWithSubsystem(LineRule):
71 name = "title-starts-with-subsystem"
72 id = "UC3"
73 target = CommitMessageTitle
74 options_spec = [StrOption('regex', ".*", "Regex the title should match")]
75
76 def validate(self, title, _commit):
77 regex = self.options['regex'].value
78 pattern = re.compile(regex, re.UNICODE)
Anas Nashif789d51c2017-10-27 14:29:59 -040079 violation_message = "Title does not follow [subsystem]: [subject]"
Anas Nashif3c27c462017-05-05 19:37:52 -040080 if not pattern.search(title):
81 return [RuleViolation(self.id, violation_message, title)]
Anas Nashifb5200752017-06-06 08:50:11 -040082
83class MaxLineLengthExceptions(LineRule):
84 name = "max-line-length-with-exceptions"
85 id = "UC4"
86 target = CommitMessageBody
87 options_spec = [IntOption('line-length', 80, "Max line length")]
88 violation_message = "Line exceeds max length ({0}>{1})"
89
90 def validate(self, line, _commit):
91 max_length = self.options['line-length'].value
Anas Nashif408a61d2017-08-08 08:07:00 -040092 urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line)
93 if line.startswith('Signed-off-by'):
94 return
95
96 if len(urls) > 0:
97 return
98
99 if len(line) > max_length:
Anas Nashifb5200752017-06-06 08:50:11 -0400100 return [RuleViolation(self.id, self.violation_message.format(len(line), max_length), line)]