summaryrefslogtreecommitdiffstats
path: root/gitlint-core/gitlint/rules.py
diff options
context:
space:
mode:
Diffstat (limited to 'gitlint-core/gitlint/rules.py')
-rw-r--r--gitlint-core/gitlint/rules.py38
1 files changed, 17 insertions, 21 deletions
diff --git a/gitlint-core/gitlint/rules.py b/gitlint-core/gitlint/rules.py
index 6d486a5..ca4a05b 100644
--- a/gitlint-core/gitlint/rules.py
+++ b/gitlint-core/gitlint/rules.py
@@ -1,11 +1,10 @@
-# pylint: disable=inconsistent-return-statements
import copy
import logging
import re
-from gitlint.options import IntOption, BoolOption, StrOption, ListOption, RegexOption
-from gitlint.exception import GitlintError
from gitlint.deprecation import Deprecation
+from gitlint.exception import GitlintError
+from gitlint.options import BoolOption, IntOption, ListOption, RegexOption, StrOption
class Rule:
@@ -50,40 +49,28 @@ class Rule:
class ConfigurationRule(Rule):
"""Class representing rules that can dynamically change the configuration of gitlint during runtime."""
- pass
-
class CommitRule(Rule):
"""Class representing rules that act on an entire commit at once"""
- pass
-
class LineRule(Rule):
"""Class representing rules that act on a line by line basis"""
- pass
-
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."""
- pass
-
class CommitMessageTitle(LineRuleTarget):
"""Target class used for rules that apply to a commit message title"""
- pass
-
class CommitMessageBody(LineRuleTarget):
"""Target class used for rules that apply to a commit message body"""
- pass
-
class RuleViolation:
"""Class representing a violation of a rule. I.e.: When a rule is broken, the rule will instantiate this class
@@ -107,8 +94,6 @@ class RuleViolation:
class UserRuleError(GitlintError):
"""Error used to indicate that an error occurred while trying to load a user rule"""
- pass
-
class MaxLineLength(LineRule):
name = "max-line-length"
@@ -305,7 +290,7 @@ class BodyMissing(CommitRule):
# ignore merges when option tells us to, which may have no body
if self.options["ignore-merge-commits"].value and commit.is_merge_commit:
return
- if len(commit.message.body) < 2 or not "".join(commit.message.body).strip():
+ if len(commit.message.body) < 2 or not "".join(commit.message.body).strip(): # noqa: PLR2004 (Magic value)
return [RuleViolation(self.id, "Body message is missing", None, 3)]
@@ -319,7 +304,7 @@ class BodyChangedFileMention(CommitRule):
for needs_mentioned_file in self.options["files"].value:
# if a file that we need to look out for is actually changed, then check whether it occurs
# in the commit msg body
- if needs_mentioned_file in commit.changed_files:
+ if needs_mentioned_file in commit.changed_files: # noqa: SIM102
if needs_mentioned_file not in " ".join(commit.message.body):
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))
@@ -370,7 +355,7 @@ class AuthorValidEmail(CommitRule):
# We're replacing regex match with search semantics, see https://github.com/jorisroovers/gitlint/issues/254
# In case the user is using the default regex, we can silently change to using search
# If not, it depends on config (handled by Deprecation class)
- if self.DEFAULT_AUTHOR_VALID_EMAIL_REGEX == self.options["regex"].value.pattern:
+ if self.options["regex"].value.pattern == self.DEFAULT_AUTHOR_VALID_EMAIL_REGEX:
regex_method = self.options["regex"].value.search
else:
regex_method = Deprecation.get_regex_method(self, self.options["regex"])
@@ -458,7 +443,7 @@ class IgnoreBodyLines(ConfigurationRule):
new_body.append(line)
commit.message.body = new_body
- commit.message.full = "\n".join([commit.message.title] + new_body)
+ commit.message.full = "\n".join([commit.message.title, *new_body])
class IgnoreByAuthorName(ConfigurationRule):
@@ -474,6 +459,17 @@ class IgnoreByAuthorName(ConfigurationRule):
if not self.options["regex"].value:
return
+ # If commit.author_name is not available, log warning and return
+ if commit.author_name is None:
+ warning_msg = (
+ "%s - %s: skipping - commit.author_name unknown. "
+ "Suggested fix: Use the --staged flag (or set general.staged=True in .gitlint). "
+ "More details: https://jorisroovers.com/gitlint/configuration/#staged"
+ )
+
+ self.log.warning(warning_msg, self.name, self.id)
+ return
+
regex_method = Deprecation.get_regex_method(self, self.options["regex"])
if regex_method(commit.author_name):