From 2c03a94ea4fde0934cd2ce34c621c98edfb9c814 Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Sun, 21 Apr 2024 14:32:15 +0800 Subject: [PATCH 1/6] feat: add an argument to limit the length of commit message --- commitizen/cli.py | 6 ++++++ commitizen/commands/commit.py | 4 +++- commitizen/cz/base.py | 12 ++++++++++- .../conventional_commits.py | 8 ++++---- commitizen/cz/customize/customize.py | 13 +++++++----- commitizen/cz/jira/jira.py | 6 ++++-- commitizen/defaults.py | 2 ++ commitizen/exceptions.py | 5 +++++ docs/commit.md | 10 ++++++++++ docs/customization.md | 6 ++++-- tests/conftest.py | 8 +++++--- tests/test_cz_base.py | 20 +++++++++++++++++-- 12 files changed, 80 insertions(+), 20 deletions(-) diff --git a/commitizen/cli.py b/commitizen/cli.py index cf3d6c5eef..8b2d008e0e 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -154,6 +154,12 @@ def __call__( "action": "store_true", "help": "Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.", }, + { + "name": ["-l", "--message-length-limit"], + "type": int, + "default": 0, + "help": "length limit of the commit message; 0 for no limit", + }, ], }, { diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 7591a28658..1f21b45571 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -61,7 +61,9 @@ def prompt_commit_questions(self) -> str: if not answers: raise NoAnswersError() - return cz.message(answers) + + message_length_limit: int = self.arguments.get("message_length_limit", 0) + return cz.message(answers, message_length_limit=message_length_limit) def __call__(self): dry_run: bool = self.arguments.get("dry_run") diff --git a/commitizen/cz/base.py b/commitizen/cz/base.py index bd116ceb02..9cbfe795db 100644 --- a/commitizen/cz/base.py +++ b/commitizen/cz/base.py @@ -9,6 +9,7 @@ from commitizen import git from commitizen.config.base_config import BaseConfig from commitizen.defaults import Questions +from commitizen.exceptions import CommitMessageLengthExceededError class MessageBuilderHook(Protocol): @@ -71,7 +72,7 @@ def questions(self) -> Questions: """Questions regarding the commit message.""" @abstractmethod - def message(self, answers: dict) -> str: + def message(self, answers: dict, message_length_limit: int) -> str: """Format your git message.""" @property @@ -105,3 +106,12 @@ def process_commit(self, commit: str) -> str: If not overwritten, it returns the first line of commit. """ return commit.split("\n")[0] + + def _check_message_length_limit( + self, message: str, message_length_limit: int + ) -> None: + message_len = len(message) + if message_length_limit > 0 and message_len > message_length_limit: + raise CommitMessageLengthExceededError( + f"Length of commit message exceeds limit ({message_len}/{message_length_limit})" + ) diff --git a/commitizen/cz/conventional_commits/conventional_commits.py b/commitizen/cz/conventional_commits/conventional_commits.py index 5f693963e7..50d6e94133 100644 --- a/commitizen/cz/conventional_commits/conventional_commits.py +++ b/commitizen/cz/conventional_commits/conventional_commits.py @@ -150,7 +150,7 @@ def questions(self) -> Questions: ] return questions - def message(self, answers: dict) -> str: + def message(self, answers: dict, message_length_limit: int = 0) -> str: prefix = answers["prefix"] scope = answers["scope"] subject = answers["subject"] @@ -167,9 +167,9 @@ def message(self, answers: dict) -> str: if footer: footer = f"\n\n{footer}" - message = f"{prefix}{scope}: {subject}{body}{footer}" - - return message + message = f"{prefix}{scope}: {subject}" + self._check_message_length_limit(message, message_length_limit) + return f"{message}{body}{footer}" def example(self) -> str: return ( diff --git a/commitizen/cz/customize/customize.py b/commitizen/cz/customize/customize.py index 5c3b4e76b4..0dc5b26794 100644 --- a/commitizen/cz/customize/customize.py +++ b/commitizen/cz/customize/customize.py @@ -61,12 +61,15 @@ def __init__(self, config: BaseConfig): def questions(self) -> Questions: return self.custom_settings.get("questions", [{}]) - def message(self, answers: dict) -> str: + def message(self, answers: dict, message_length_limit: int = 0) -> str: message_template = Template(self.custom_settings.get("message_template", "")) - if getattr(Template, "substitute", None): - return message_template.substitute(**answers) # type: ignore - else: - return message_template.render(**answers) + message: str = ( + message_template.substitute(**answers) # type: ignore + if getattr(Template, "substitute", None) + else message_template.render(**answers) + ) + self._check_message_length_limit(message, message_length_limit) + return message def example(self) -> str | None: return self.custom_settings.get("example") diff --git a/commitizen/cz/jira/jira.py b/commitizen/cz/jira/jira.py index a4fdcfa09e..6a727f78ed 100644 --- a/commitizen/cz/jira/jira.py +++ b/commitizen/cz/jira/jira.py @@ -44,8 +44,8 @@ def questions(self) -> Questions: ] return questions - def message(self, answers) -> str: - return " ".join( + def message(self, answers: dict, message_length_limit: int = 0) -> str: + message = " ".join( filter( bool, [ @@ -57,6 +57,8 @@ def message(self, answers) -> str: ], ) ) + self._check_message_length_limit(message, message_length_limit) + return message def example(self) -> str: return ( diff --git a/commitizen/defaults.py b/commitizen/defaults.py index a1651ebe88..cb35319168 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -57,6 +57,7 @@ class Settings(TypedDict, total=False): always_signoff: bool template: str | None extras: dict[str, Any] + message_length_limit: int name: str = "cz_conventional_commits" @@ -102,6 +103,7 @@ class Settings(TypedDict, total=False): "always_signoff": False, "template": None, # default provided by plugin "extras": {}, + "message_length_limit": 0, } MAJOR = "MAJOR" diff --git a/commitizen/exceptions.py b/commitizen/exceptions.py index 9cb1517680..bcdd0d1e3c 100644 --- a/commitizen/exceptions.py +++ b/commitizen/exceptions.py @@ -36,6 +36,7 @@ class ExitCode(enum.IntEnum): CHANGELOG_FORMAT_UNKNOWN = 29 CONFIG_FILE_NOT_FOUND = 30 CONFIG_FILE_IS_EMPTY = 31 + COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED = 32 class CommitizenException(Exception): @@ -201,3 +202,7 @@ class ConfigFileNotFound(CommitizenException): class ConfigFileIsEmpty(CommitizenException): exit_code = ExitCode.CONFIG_FILE_IS_EMPTY message = "Config file is empty, please check your file path again." + + +class CommitMessageLengthExceededError(CommitizenException): + exit_code = ExitCode.COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED diff --git a/docs/commit.md b/docs/commit.md index 54c792f743..fe6fb415c4 100644 --- a/docs/commit.md +++ b/docs/commit.md @@ -36,3 +36,13 @@ You can use `cz commit --retry` to reuse the last commit message when the previo To automatically retry when running `cz commit`, you can set the `retry_after_failure` configuration option to `true`. Running `cz commit --no-retry` makes commitizen ignore `retry_after_failure`, forcing a new commit message to be prompted. + +### Commit message length limit + +The argument `-l` (or `--message-length-limit`) followed by a positive number can limit the length of commit messages. +An exception would be raised when the message length exceeds the limit. +For example, `cz commit -l 72` will limit the length of commit messages to 72 characters. +By default the limit is set to 0, which means no limit on the length. + +Note that for `ConventionalCommitsCz`, the limit applies only from the prefix to the subject. +In other words, everything after the first line (the body and the footer) are not counted in the length. diff --git a/docs/customization.md b/docs/customization.md index 1fd1826e03..e7832e319f 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -227,9 +227,11 @@ class JiraCz(BaseCommitizen): ] return questions - def message(self, answers: dict) -> str: + def message(self, answers: dict, message_length_limit: int = 0) -> str: """Generate the message with the given answers.""" - return "{0} (#{1})".format(answers["title"], answers["issue"]) + message = "{0} (#{1})".format(answers["title"], answers["issue"]) + self._check_message_length_limit(message, message_length_limit) + return message def example(self) -> str: """Provide an example to help understand the style (OPTIONAL) diff --git a/tests/conftest.py b/tests/conftest.py index 76d2e53fb7..0431a39556 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -204,10 +204,12 @@ def questions(self) -> list: }, ] - def message(self, answers: dict) -> str: + def message(self, answers: dict, message_length_limit: int = 0) -> str: prefix = answers["prefix"] subject = answers.get("subject", "default message").trim() - return f"{prefix}: {subject}" + message = f"{prefix}: {subject}" + self._check_message_length_limit(message, message_length_limit) + return message @pytest.fixture() @@ -220,7 +222,7 @@ class MockPlugin(BaseCommitizen): def questions(self) -> defaults.Questions: return [] - def message(self, answers: dict) -> str: + def message(self, answers: dict, message_length_limit: int = 0) -> str: return "" diff --git a/tests/test_cz_base.py b/tests/test_cz_base.py index 891ee01167..3b903d80b8 100644 --- a/tests/test_cz_base.py +++ b/tests/test_cz_base.py @@ -1,14 +1,17 @@ import pytest from commitizen.cz.base import BaseCommitizen +from commitizen.exceptions import CommitMessageLengthExceededError class DummyCz(BaseCommitizen): def questions(self): return [{"type": "input", "name": "commit", "message": "Initial commit:\n"}] - def message(self, answers): - return answers["commit"] + def message(self, answers: dict, message_length_limit: int = 0): + message = answers["commit"] + self._check_message_length_limit(message, message_length_limit) + return message def test_base_raises_error(config): @@ -48,3 +51,16 @@ def test_process_commit(config): cz = DummyCz(config) message = cz.process_commit("test(test_scope): this is test msg") assert message == "test(test_scope): this is test msg" + + +def test_message_length_limit(config): + cz = DummyCz(config) + commit_message = "123456789" + message_length = len(commit_message) + assert cz.message({"commit": commit_message}) == commit_message + assert ( + cz.message({"commit": commit_message}, message_length_limit=message_length) + == commit_message + ) + with pytest.raises(CommitMessageLengthExceededError): + cz.message({"commit": commit_message}, message_length_limit=message_length - 1) From 1d2172e9ee6d7670deffdc2943e02a8dac4f4909 Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Sun, 21 Apr 2024 16:58:26 +0800 Subject: [PATCH 2/6] fix: resolve test error by removing defaults --- commitizen/defaults.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/commitizen/defaults.py b/commitizen/defaults.py index cb35319168..a1651ebe88 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -57,7 +57,6 @@ class Settings(TypedDict, total=False): always_signoff: bool template: str | None extras: dict[str, Any] - message_length_limit: int name: str = "cz_conventional_commits" @@ -103,7 +102,6 @@ class Settings(TypedDict, total=False): "always_signoff": False, "template": None, # default provided by plugin "extras": {}, - "message_length_limit": 0, } MAJOR = "MAJOR" From ab75e348ee96ba324b3320e022ca7e4b974b07a1 Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Sun, 21 Apr 2024 22:18:57 +0800 Subject: [PATCH 3/6] refactor: check the length in Commit instead of Commitizen --- commitizen/commands/commit.py | 10 ++++++- commitizen/cz/base.py | 12 +-------- .../conventional_commits.py | 8 +++--- commitizen/cz/customize/customize.py | 13 ++++----- commitizen/cz/jira/jira.py | 6 ++--- commitizen/exceptions.py | 1 + docs/commit.md | 5 ++-- docs/customization.md | 6 ++--- tests/commands/test_commit_command.py | 27 +++++++++++++++++++ tests/conftest.py | 8 +++--- tests/test_cz_base.py | 20 ++------------ 11 files changed, 59 insertions(+), 57 deletions(-) diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 1f21b45571..15f2cc903c 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -12,6 +12,7 @@ from commitizen.cz.utils import get_backup_file_path from commitizen.exceptions import ( CommitError, + CommitMessageLengthExceededError, CustomError, DryRunExit, NoAnswersError, @@ -62,8 +63,15 @@ def prompt_commit_questions(self) -> str: if not answers: raise NoAnswersError() + message = cz.message(answers) + message_len = len(message.partition("\n")[0]) message_length_limit: int = self.arguments.get("message_length_limit", 0) - return cz.message(answers, message_length_limit=message_length_limit) + if message_length_limit > 0 and message_len > message_length_limit: + raise CommitMessageLengthExceededError( + f"Length of commit message exceeds limit ({message_len}/{message_length_limit})" + ) + + return message def __call__(self): dry_run: bool = self.arguments.get("dry_run") diff --git a/commitizen/cz/base.py b/commitizen/cz/base.py index 9cbfe795db..bd116ceb02 100644 --- a/commitizen/cz/base.py +++ b/commitizen/cz/base.py @@ -9,7 +9,6 @@ from commitizen import git from commitizen.config.base_config import BaseConfig from commitizen.defaults import Questions -from commitizen.exceptions import CommitMessageLengthExceededError class MessageBuilderHook(Protocol): @@ -72,7 +71,7 @@ def questions(self) -> Questions: """Questions regarding the commit message.""" @abstractmethod - def message(self, answers: dict, message_length_limit: int) -> str: + def message(self, answers: dict) -> str: """Format your git message.""" @property @@ -106,12 +105,3 @@ def process_commit(self, commit: str) -> str: If not overwritten, it returns the first line of commit. """ return commit.split("\n")[0] - - def _check_message_length_limit( - self, message: str, message_length_limit: int - ) -> None: - message_len = len(message) - if message_length_limit > 0 and message_len > message_length_limit: - raise CommitMessageLengthExceededError( - f"Length of commit message exceeds limit ({message_len}/{message_length_limit})" - ) diff --git a/commitizen/cz/conventional_commits/conventional_commits.py b/commitizen/cz/conventional_commits/conventional_commits.py index 50d6e94133..5f693963e7 100644 --- a/commitizen/cz/conventional_commits/conventional_commits.py +++ b/commitizen/cz/conventional_commits/conventional_commits.py @@ -150,7 +150,7 @@ def questions(self) -> Questions: ] return questions - def message(self, answers: dict, message_length_limit: int = 0) -> str: + def message(self, answers: dict) -> str: prefix = answers["prefix"] scope = answers["scope"] subject = answers["subject"] @@ -167,9 +167,9 @@ def message(self, answers: dict, message_length_limit: int = 0) -> str: if footer: footer = f"\n\n{footer}" - message = f"{prefix}{scope}: {subject}" - self._check_message_length_limit(message, message_length_limit) - return f"{message}{body}{footer}" + message = f"{prefix}{scope}: {subject}{body}{footer}" + + return message def example(self) -> str: return ( diff --git a/commitizen/cz/customize/customize.py b/commitizen/cz/customize/customize.py index 0dc5b26794..5c3b4e76b4 100644 --- a/commitizen/cz/customize/customize.py +++ b/commitizen/cz/customize/customize.py @@ -61,15 +61,12 @@ def __init__(self, config: BaseConfig): def questions(self) -> Questions: return self.custom_settings.get("questions", [{}]) - def message(self, answers: dict, message_length_limit: int = 0) -> str: + def message(self, answers: dict) -> str: message_template = Template(self.custom_settings.get("message_template", "")) - message: str = ( - message_template.substitute(**answers) # type: ignore - if getattr(Template, "substitute", None) - else message_template.render(**answers) - ) - self._check_message_length_limit(message, message_length_limit) - return message + if getattr(Template, "substitute", None): + return message_template.substitute(**answers) # type: ignore + else: + return message_template.render(**answers) def example(self) -> str | None: return self.custom_settings.get("example") diff --git a/commitizen/cz/jira/jira.py b/commitizen/cz/jira/jira.py index 6a727f78ed..b8fd056a71 100644 --- a/commitizen/cz/jira/jira.py +++ b/commitizen/cz/jira/jira.py @@ -44,8 +44,8 @@ def questions(self) -> Questions: ] return questions - def message(self, answers: dict, message_length_limit: int = 0) -> str: - message = " ".join( + def message(self, answers: dict) -> str: + return " ".join( filter( bool, [ @@ -57,8 +57,6 @@ def message(self, answers: dict, message_length_limit: int = 0) -> str: ], ) ) - self._check_message_length_limit(message, message_length_limit) - return message def example(self) -> str: return ( diff --git a/commitizen/exceptions.py b/commitizen/exceptions.py index bcdd0d1e3c..b0fc4e382d 100644 --- a/commitizen/exceptions.py +++ b/commitizen/exceptions.py @@ -206,3 +206,4 @@ class ConfigFileIsEmpty(CommitizenException): class CommitMessageLengthExceededError(CommitizenException): exit_code = ExitCode.COMMIT_MESSAGE_LENGTH_LIMIT_EXCEEDED + message = "Length of commit message exceeds the given limit." diff --git a/docs/commit.md b/docs/commit.md index fe6fb415c4..7e3c6268ca 100644 --- a/docs/commit.md +++ b/docs/commit.md @@ -44,5 +44,6 @@ An exception would be raised when the message length exceeds the limit. For example, `cz commit -l 72` will limit the length of commit messages to 72 characters. By default the limit is set to 0, which means no limit on the length. -Note that for `ConventionalCommitsCz`, the limit applies only from the prefix to the subject. -In other words, everything after the first line (the body and the footer) are not counted in the length. +Note that the limit applies only to the first line of message. +Specifically, for `ConventionalCommitsCz` the length only counts from the type of change to the subject, +while the body and the footer are not counted. diff --git a/docs/customization.md b/docs/customization.md index e7832e319f..1fd1826e03 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -227,11 +227,9 @@ class JiraCz(BaseCommitizen): ] return questions - def message(self, answers: dict, message_length_limit: int = 0) -> str: + def message(self, answers: dict) -> str: """Generate the message with the given answers.""" - message = "{0} (#{1})".format(answers["title"], answers["issue"]) - self._check_message_length_limit(message, message_length_limit) - return message + return "{0} (#{1})".format(answers["title"], answers["issue"]) def example(self) -> str: """Provide an example to help understand the style (OPTIONAL) diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 1930b2eaee..0e0a12751a 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -9,6 +9,7 @@ from commitizen.cz.utils import get_backup_file_path from commitizen.exceptions import ( CommitError, + CommitMessageLengthExceededError, CustomError, DryRunExit, NoAnswersError, @@ -379,3 +380,29 @@ def test_commit_command_with_extra_args(config, mocker: MockFixture): commands.Commit(config, {"extra_cli_args": "-- -extra-args1 -extra-arg2"})() commit_mock.assert_called_once_with(ANY, args="-- -extra-args1 -extra-arg2") success_mock.assert_called_once() + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_command_with_message_length_limit(config, mocker: MockFixture): + prompt_mock = mocker.patch("questionary.prompt") + prefix = "feat" + subject = "random subject" + message_length = len(prefix) + len(": ") + len(subject) + prompt_mock.return_value = { + "prefix": prefix, + "subject": subject, + "scope": "", + "is_breaking_change": False, + "body": "random body", + "footer": "random footer", + } + + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) + success_mock = mocker.patch("commitizen.out.success") + + commands.Commit(config, {"message_length_limit": message_length})() + success_mock.assert_called_once() + + with pytest.raises(CommitMessageLengthExceededError): + commands.Commit(config, {"message_length_limit": message_length - 1})() diff --git a/tests/conftest.py b/tests/conftest.py index 0431a39556..76d2e53fb7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -204,12 +204,10 @@ def questions(self) -> list: }, ] - def message(self, answers: dict, message_length_limit: int = 0) -> str: + def message(self, answers: dict) -> str: prefix = answers["prefix"] subject = answers.get("subject", "default message").trim() - message = f"{prefix}: {subject}" - self._check_message_length_limit(message, message_length_limit) - return message + return f"{prefix}: {subject}" @pytest.fixture() @@ -222,7 +220,7 @@ class MockPlugin(BaseCommitizen): def questions(self) -> defaults.Questions: return [] - def message(self, answers: dict, message_length_limit: int = 0) -> str: + def message(self, answers: dict) -> str: return "" diff --git a/tests/test_cz_base.py b/tests/test_cz_base.py index 3b903d80b8..4ee1cc6eda 100644 --- a/tests/test_cz_base.py +++ b/tests/test_cz_base.py @@ -1,17 +1,14 @@ import pytest from commitizen.cz.base import BaseCommitizen -from commitizen.exceptions import CommitMessageLengthExceededError class DummyCz(BaseCommitizen): def questions(self): return [{"type": "input", "name": "commit", "message": "Initial commit:\n"}] - def message(self, answers: dict, message_length_limit: int = 0): - message = answers["commit"] - self._check_message_length_limit(message, message_length_limit) - return message + def message(self, answers: dict): + return answers["commit"] def test_base_raises_error(config): @@ -51,16 +48,3 @@ def test_process_commit(config): cz = DummyCz(config) message = cz.process_commit("test(test_scope): this is test msg") assert message == "test(test_scope): this is test msg" - - -def test_message_length_limit(config): - cz = DummyCz(config) - commit_message = "123456789" - message_length = len(commit_message) - assert cz.message({"commit": commit_message}) == commit_message - assert ( - cz.message({"commit": commit_message}, message_length_limit=message_length) - == commit_message - ) - with pytest.raises(CommitMessageLengthExceededError): - cz.message({"commit": commit_message}, message_length_limit=message_length - 1) From d0f3a4a9de562b8a9af58bf43e00d271f2903c8a Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Fri, 26 Apr 2024 16:45:19 +0800 Subject: [PATCH 4/6] fix: strip the commit message for calculating length --- commitizen/commands/commit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 15f2cc903c..7a2a01c9cb 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -64,7 +64,7 @@ def prompt_commit_questions(self) -> str: raise NoAnswersError() message = cz.message(answers) - message_len = len(message.partition("\n")[0]) + message_len = len(message.partition("\n")[0].strip()) message_length_limit: int = self.arguments.get("message_length_limit", 0) if message_length_limit > 0 and message_len > message_length_limit: raise CommitMessageLengthExceededError( From 0d7eaee7630f1443ef328d5241537ef5aad4edcc Mon Sep 17 00:00:00 2001 From: Yu-Sheng Li Date: Fri, 26 Apr 2024 16:46:41 +0800 Subject: [PATCH 5/6] docs: emphasize the note on commit message length calculation --- docs/commit.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/commit.md b/docs/commit.md index 7e3c6268ca..2c1b94f63e 100644 --- a/docs/commit.md +++ b/docs/commit.md @@ -44,6 +44,6 @@ An exception would be raised when the message length exceeds the limit. For example, `cz commit -l 72` will limit the length of commit messages to 72 characters. By default the limit is set to 0, which means no limit on the length. -Note that the limit applies only to the first line of message. +**Note that the limit applies only to the first line of the message.** Specifically, for `ConventionalCommitsCz` the length only counts from the type of change to the subject, while the body and the footer are not counted. From 39792f24be19a6366569ae7065dbf3d73166916b Mon Sep 17 00:00:00 2001 From: Wei Lee Date: Mon, 29 Apr 2024 10:05:20 +0800 Subject: [PATCH 6/6] refactor(commands/commit): replace comparison with chained comparison --- commitizen/commands/commit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 7a2a01c9cb..df28b2a5dd 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -66,7 +66,7 @@ def prompt_commit_questions(self) -> str: message = cz.message(answers) message_len = len(message.partition("\n")[0].strip()) message_length_limit: int = self.arguments.get("message_length_limit", 0) - if message_length_limit > 0 and message_len > message_length_limit: + if 0 < message_length_limit < message_len: raise CommitMessageLengthExceededError( f"Length of commit message exceeds limit ({message_len}/{message_length_limit})" )