Debian Codegraph @

script/gitlint/custom_gitlint_rules.py

raw
1 # pylint: disable=too-few-public-methods
2 ###############################################################################
3 # Copyright (C) Intel Corporation
4 #
5 # SPDX-License-Identifier: MIT
6 ###############################################################################
7 """
8 Custom rules for gitlint
9 """
10
11 import os
12 from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle
13
14
15 class TitleCapitalized(LineRule):
16 """This rule will enforce that the first word of the commit message title is
17 capitalized"""
18
19 name = "title-is-capitalized"
20 id = "UL3"
21 target = CommitMessageTitle
22
23 def validate(self, line, _commit):
24 """Validate that the title is capitalized"""
25 violations = []
26 if line:
27 if not line[0].isupper():
28 violation = RuleViolation(
29 self.id, "Title does not start with a capital letter",
30 line)
31 violations.append(violation)
32
33 return violations
34
35
36 __location__ = os.path.realpath(
37 os.path.join(os.getcwd(), os.path.dirname(__file__)))
38
39
40 class TitleImperative(LineRule):
41 """This rule will enforce that the commit message title uses the imperative
42 mood.
43
44 For now rather than using an advanced linguistic database, it just rejects
45 common bad patterns, such as words ending in ed or ing."""
46
47 name = "title-is-imperative"
48 id = "UL5"
49 target = CommitMessageTitle
50
51 def validate(self, line, _commit):
52 """Validate that the title is imperative"""
53 violations = []
54 bad_suffixes = ('ed', 'ing')
55 with open(os.path.join(__location__, 'verbs.txt'),
56 encoding="utf-8") as verbfile:
57 verbs_ending_with_s = [line.rstrip() for line in verbfile]
58 for word in line.split():
59 if word.endswith(bad_suffixes) or (word.endswith('s')
60 and word.lower()
61 not in verbs_ending_with_s):
62 violation = RuleViolation(self.id, "Title is not imperative",
63 line)
64 violations.append(violation)
65 break
66
67 return violations