File: custom_gitlint_rules.py

package info (click to toggle)
libvpl-tools 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,652 kB
  • sloc: cpp: 107,469; python: 4,303; ansic: 3,202; sh: 159; lisp: 52; makefile: 13
file content (67 lines) | stat: -rw-r--r-- 2,201 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# pylint: disable=too-few-public-methods
###############################################################################
# Copyright (C) Intel Corporation
#
# SPDX-License-Identifier: MIT
###############################################################################
"""
Custom rules for gitlint
"""

import os
from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle


class TitleCapitalized(LineRule):
    """This rule will enforce that the first word of the commit message title is
capitalized"""

    name = "title-is-capitalized"
    id = "UL3"
    target = CommitMessageTitle

    def validate(self, line, _commit):
        """Validate that the title is capitalized"""
        violations = []
        if line:
            if not line[0].isupper():
                violation = RuleViolation(
                    self.id, "Title does not start with a capital letter",
                    line)
                violations.append(violation)

        return violations


__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))


class TitleImperative(LineRule):
    """This rule will enforce that the commit message title uses the imperative
mood.

For now rather than using an advanced linguistic database, it just rejects
common bad patterns, such as words ending in ed or ing."""

    name = "title-is-imperative"
    id = "UL5"
    target = CommitMessageTitle

    def validate(self, line, _commit):
        """Validate that the title is imperative"""
        violations = []
        bad_suffixes = ('ed', 'ing')
        with open(os.path.join(__location__, 'verbs.txt'),
                  encoding="utf8") as verbfile:
            verbs_ending_with_s = [line.rstrip() for line in verbfile]
        for word in line.split():
            if word.endswith(bad_suffixes) or (word.endswith('s')
                                               and word.lower()
                                               not in verbs_ending_with_s):
                violation = RuleViolation(self.id, "Title is not imperative",
                                          line)
                violations.append(violation)
            break

        return violations