diff options
author | Joris Roovers <joris.roovers@gmail.com> | 2023-02-14 10:37:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-14 10:37:24 +0000 |
commit | 2c9af7877db095321c5a2af17b2bb65ecbbbdc9f (patch) | |
tree | a4c158150c1e1e4837e711c995b19751803ad9f4 | |
parent | 97d6e46b36121117f85f592dff867673a7c8e7f9 (diff) |
Bugfix: ignore-by-author-name crashes without --staged (#445)
If the user has an ignore-by-author-name rule configured, and when manually
passing a git commit message to gitlint using --msg-filename or via STDIN
without also using the --staged flag, gitlint crashes because
commit.author_name is unknown.
This fix will print a warning instead and skip the rule.
Fixes #403.
-rw-r--r-- | gitlint-core/gitlint/cli.py | 2 | ||||
-rw-r--r-- | gitlint-core/gitlint/rules.py | 11 | ||||
-rw-r--r-- | gitlint-core/gitlint/tests/rules/test_configuration_rules.py | 15 |
3 files changed, 26 insertions, 2 deletions
diff --git a/gitlint-core/gitlint/cli.py b/gitlint-core/gitlint/cli.py index 32d9648..619f006 100644 --- a/gitlint-core/gitlint/cli.py +++ b/gitlint-core/gitlint/cli.py @@ -47,7 +47,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) diff --git a/gitlint-core/gitlint/rules.py b/gitlint-core/gitlint/rules.py index 492de6d..ca4a05b 100644 --- a/gitlint-core/gitlint/rules.py +++ b/gitlint-core/gitlint/rules.py @@ -459,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): diff --git a/gitlint-core/gitlint/tests/rules/test_configuration_rules.py b/gitlint-core/gitlint/tests/rules/test_configuration_rules.py index eeb74c7..d5b70a9 100644 --- a/gitlint-core/gitlint/tests/rules/test_configuration_rules.py +++ b/gitlint-core/gitlint/tests/rules/test_configuration_rules.py @@ -92,6 +92,19 @@ class ConfigurationRuleTests(BaseTestCase): 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() @@ -99,7 +112,7 @@ class ConfigurationRuleTests(BaseTestCase): 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(.*)'," |