summaryrefslogtreecommitdiffstats
path: root/gitlint/rules.py
diff options
context:
space:
mode:
Diffstat (limited to 'gitlint/rules.py')
-rw-r--r--gitlint/rules.py62
1 files changed, 21 insertions, 41 deletions
diff --git a/gitlint/rules.py b/gitlint/rules.py
index 1cb50da..3dc85b7 100644
--- a/gitlint/rules.py
+++ b/gitlint/rules.py
@@ -4,10 +4,10 @@ import logging
import re
from gitlint.options import IntOption, BoolOption, StrOption, ListOption, RegexOption
-from gitlint.utils import sstr
+from gitlint.exception import GitlintError
-class Rule(object):
+class Rule:
""" Class representing gitlint rules. """
options_spec = []
id = None
@@ -36,17 +36,8 @@ class Rule(object):
return self.id == other.id and self.name == other.name and \
self.options == other.options and self.target == other.target # noqa
- def __ne__(self, other):
- return not self.__eq__(other) # required for py2
-
def __str__(self):
- return sstr(self) # pragma: no cover
-
- def __unicode__(self):
- return u"{0} {1}".format(self.id, self.name) # pragma: no cover
-
- def __repr__(self):
- return self.__str__() # pragma: no cover
+ return f"{self.id} {self.name}" # pragma: no cover
class ConfigurationRule(Rule):
@@ -64,7 +55,7 @@ class LineRule(Rule):
pass
-class LineRuleTarget(object):
+class LineRuleTarget:
""" Base class for LineRule targets. A LineRuleTarget specifies where a given rule will be applied
(e.g. commit message title, commit message body).
Each LineRule MUST have a target specified. """
@@ -81,7 +72,7 @@ class CommitMessageBody(LineRuleTarget):
pass
-class RuleViolation(object):
+class RuleViolation:
""" Class representing a violation of a rule. I.e.: When a rule is broken, the rule will instantiate this class
to indicate how and where the rule was broken. """
@@ -96,21 +87,11 @@ class RuleViolation(object):
equal = equal and self.content == other.content and self.line_nr == other.line_nr
return equal
- def __ne__(self, other):
- return not self.__eq__(other) # required for py2
-
def __str__(self):
- return sstr(self) # pragma: no cover
-
- def __unicode__(self):
- return u"{0}: {1} {2}: \"{3}\"".format(self.line_nr, self.rule_id, self.message,
- self.content) # pragma: no cover
-
- def __repr__(self):
- return self.__unicode__() # pragma: no cover
+ return f"{self.line_nr}: {self.rule_id} {self.message}: \"{self.content}\""
-class UserRuleError(Exception):
+class UserRuleError(GitlintError):
""" Error used to indicate that an error occurred while trying to load a user rule """
pass
@@ -154,7 +135,7 @@ class LineMustNotContainWord(LineRule):
name = "line-must-not-contain"
id = "R5"
options_spec = [ListOption('words', [], "Comma separated list of words that should not be found")]
- violation_message = u"Line contains {0}"
+ violation_message = "Line contains {0}"
def validate(self, line, _commit):
strings = self.options['words'].value
@@ -202,7 +183,7 @@ class TitleTrailingPunctuation(LineRule):
punctuation_marks = '?:!.,;'
for punctuation_mark in punctuation_marks:
if title.endswith(punctuation_mark):
- return [RuleViolation(self.id, u"Title has trailing punctuation ({0})".format(punctuation_mark), title)]
+ return [RuleViolation(self.id, f"Title has trailing punctuation ({punctuation_mark})", title)]
class TitleHardTab(HardTab):
@@ -217,7 +198,7 @@ class TitleMustNotContainWord(LineMustNotContainWord):
id = "T5"
target = CommitMessageTitle
options_spec = [ListOption('words', ["WIP"], "Must not contain word")]
- violation_message = u"Title contains the word '{0}' (case-insensitive)"
+ violation_message = "Title contains the word '{0}' (case-insensitive)"
class TitleLeadingWhitespace(LeadingWhiteSpace):
@@ -239,7 +220,7 @@ class TitleRegexMatches(LineRule):
return
if not self.options['regex'].value.search(title):
- violation_msg = u"Title does not match regex ({0})".format(self.options['regex'].value.pattern)
+ violation_msg = f"Title does not match regex ({self.options['regex'].value.pattern})"
return [RuleViolation(self.id, violation_msg, title)]
@@ -253,7 +234,7 @@ class TitleMinLength(LineRule):
min_length = self.options['min-length'].value
actual_length = len(title)
if actual_length < min_length:
- violation_message = "Title is too short ({0}<{1})".format(actual_length, min_length)
+ violation_message = f"Title is too short ({actual_length}<{min_length})"
return [RuleViolation(self.id, violation_message, title, 1)]
@@ -296,7 +277,7 @@ class BodyMinLength(CommitRule):
body_message_no_newline = "".join([line for line in commit.message.body if line is not None])
actual_length = len(body_message_no_newline)
if 0 < actual_length < min_length:
- violation_message = "Body message is too short ({0}<{1})".format(actual_length, min_length)
+ violation_message = f"Body message is too short ({actual_length}<{min_length})"
return [RuleViolation(self.id, violation_message, body_message_no_newline, 3)]
@@ -325,7 +306,7 @@ class BodyChangedFileMention(CommitRule):
# in the commit msg body
if needs_mentioned_file in commit.changed_files:
if needs_mentioned_file not in " ".join(commit.message.body):
- violation_message = u"Body does not mention changed file '{0}'".format(needs_mentioned_file)
+ violation_message = f"Body does not mention changed file '{needs_mentioned_file}'"
violations.append(RuleViolation(self.id, violation_message, None, len(commit.message.body) + 1))
return violations if violations else None
@@ -354,7 +335,7 @@ class BodyRegexMatches(CommitRule):
full_body = "\n".join(body_lines)
if not self.options['regex'].value.search(full_body):
- violation_msg = u"Body does not match regex ({0})".format(self.options['regex'].value.pattern)
+ violation_msg = f"Body does not match regex ({self.options['regex'].value.pattern})"
return [RuleViolation(self.id, violation_msg, None, len(commit.message.body) + 1)]
@@ -386,9 +367,8 @@ class IgnoreByTitle(ConfigurationRule):
if self.options['regex'].value.match(commit.message.title):
config.ignore = self.options['ignore'].value
- message = u"Commit title '{0}' matches the regex '{1}', ignoring rules: {2}"
- message = message.format(commit.message.title, self.options['regex'].value.pattern,
- self.options['ignore'].value)
+ message = f"Commit title '{commit.message.title}' matches the regex " + \
+ f"'{self.options['regex'].value.pattern}', ignoring rules: {self.options['ignore'].value}"
self.log.debug("Ignoring commit because of rule '%s': %s", self.id, message)
@@ -408,8 +388,8 @@ class IgnoreByBody(ConfigurationRule):
if self.options['regex'].value.match(line):
config.ignore = self.options['ignore'].value
- message = u"Commit message line '{0}' matches the regex '{1}', ignoring rules: {2}"
- message = message.format(line, self.options['regex'].value.pattern, self.options['ignore'].value)
+ message = f"Commit message line '{line}' matches the regex '{self.options['regex'].value.pattern}'," + \
+ f" ignoring rules: {self.options['ignore'].value}"
self.log.debug("Ignoring commit because of rule '%s': %s", self.id, message)
# No need to check other lines if we found a match
@@ -429,10 +409,10 @@ class IgnoreBodyLines(ConfigurationRule):
new_body = []
for line in commit.message.body:
if self.options['regex'].value.match(line):
- debug_msg = u"Ignoring line '%s' because it matches '%s'"
+ debug_msg = "Ignoring line '%s' because it matches '%s'"
self.log.debug(debug_msg, line, self.options['regex'].value.pattern)
else:
new_body.append(line)
commit.message.body = new_body
- commit.message.full = u"\n".join([commit.message.title] + new_body)
+ commit.message.full = "\n".join([commit.message.title] + new_body)