Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: ignore-by-author-name crashes without --staged #445

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gitlint-core/gitlint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setup_logging():
# Root log, mostly used for debug
root_log = logging.getLogger("gitlint")
root_log.propagate = False # Don't propagate to child loggers, the gitlint root logger handles everything
root_log.setLevel(logging.ERROR)
root_log.setLevel(logging.WARN)
handler = logging.StreamHandler()
formatter = logging.Formatter(LOG_FORMAT)
handler.setFormatter(formatter)
Expand Down
11 changes: 11 additions & 0 deletions gitlint-core/gitlint/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,17 @@ def apply(self, config, commit):
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):
Expand Down
15 changes: 14 additions & 1 deletion gitlint-core/gitlint/tests/rules/test_configuration_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,27 @@ def test_ignore_by_author_name(self):
self.assertEqual(config, LintConfig())
self.assert_logged([]) # nothing logged -> nothing ignored

# No author available -> rule is skipped and warning logged
staged_commit = self.gitcommit("Tïtle\n\nThis is\n a relëase body\n line")
rule = rules.IgnoreByAuthorName({"regex": "foo"})
config = LintConfig()
rule.apply(config, staged_commit)
self.assertEqual(config, LintConfig())
expected_log_messages = [
"WARNING: gitlint.rules ignore-by-author-name - I4: 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.assert_logged(expected_log_messages)

# Matching regex -> expect config to ignore all rules
rule = rules.IgnoreByAuthorName({"regex": "(.*)ëst(.*)"})
expected_config = LintConfig()
expected_config.ignore = "all"
rule.apply(config, commit)
self.assertEqual(config, expected_config)

expected_log_messages = [
expected_log_messages += [
EXPECTED_REGEX_STYLE_SEARCH_DEPRECATION_WARNING.format("I4", "ignore-by-author-name"),
"DEBUG: gitlint.rules Ignoring commit because of rule 'I4': "
"Commit Author Name 'Tëst nåme' matches the regex '(.*)ëst(.*)',"
Expand Down