Skip to content

feat: add config option for line length warning #1574

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

Open
wants to merge 6 commits into
base: v4-9-0-test
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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: 0 additions & 2 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ def __call__(
{
"name": ["-l", "--message-length-limit"],
"type": int,
"default": 0,
"help": "length limit of the commit message; 0 for no limit",
},
{
Expand Down Expand Up @@ -492,7 +491,6 @@ def __call__(
{
"name": ["-l", "--message-length-limit"],
"type": int,
"default": 0,
"help": "length limit of the commit message; 0 for no limit",
},
],
Expand Down
16 changes: 14 additions & 2 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from commitizen import factory, git, out
from commitizen.config import BaseConfig
from commitizen.exceptions import (
CommitMessageLengthExceededError,
InvalidCommandArgumentError,
InvalidCommitMessageError,
NoCommitsFoundError,
Expand Down Expand Up @@ -40,7 +41,13 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
self.allow_abort = bool(
arguments.get("allow_abort", config.settings["allow_abort"])
)
self.max_msg_length = arguments.get("message_length_limit", 0)

# Use command line argument if provided, otherwise use config setting
cmd_length_limit = arguments.get("message_length_limit")
if cmd_length_limit is None:
self.max_msg_length = config.settings.get("message_length_limit", 0)
else:
self.max_msg_length = cmd_length_limit

# we need to distinguish between None and [], which is a valid value
allowed_prefixes = arguments.get("allowed_prefixes")
Expand Down Expand Up @@ -154,6 +161,11 @@ def _validate_commit_message(
if self.max_msg_length:
msg_len = len(commit_msg.partition("\n")[0].strip())
if msg_len > self.max_msg_length:
return False
raise CommitMessageLengthExceededError(
f"commit validation: failed!\n"
f"commit message length exceeds the limit.\n"
f'commit "": "{commit_msg}"\n'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does "" mean here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you forgot to put something like commit hash here in the error message

Copy link
Author

@Narwhal-fish Narwhal-fish Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right. I referenced the error handling for invalid messages and forgot to add the commit hash. I’ll make the correction.

f"message length limit: {self.max_msg_length} (actual: {msg_len})"
)

return bool(pattern.match(commit_msg))
6 changes: 5 additions & 1 deletion commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ def _prompt_commit_questions(self) -> str:

message = cz.message(answers)
message_len = len(message.partition("\n")[0].strip())
message_length_limit = self.arguments.get("message_length_limit", 0)

message_length_limit = self.arguments.get("message_length_limit")
if message_length_limit is None:
message_length_limit = self.config.settings.get("message_length_limit", 0)

if 0 < message_length_limit < message_len:
raise CommitMessageLengthExceededError(
f"Length of commit message exceeds limit ({message_len}/{message_length_limit})"
Expand Down
2 changes: 2 additions & 0 deletions commitizen/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Settings(TypedDict, total=False):
ignored_tag_formats: Sequence[str]
legacy_tag_formats: Sequence[str]
major_version_zero: bool
message_length_limit: int
name: str
post_bump_hooks: list[str] | None
pre_bump_hooks: list[str] | None
Expand Down Expand Up @@ -108,6 +109,7 @@ class Settings(TypedDict, total=False):
"always_signoff": False,
"template": None, # default provided by plugin
"extras": {},
"message_length_limit": 0, # 0 for no limit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just use None for no limit?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I set it to 0 simply because I thought commit messages wouldn't have a 0-character scenario, but your suggestion of using None is more clear. I've made this change accordingly.

}

MAJOR = "MAJOR"
Expand Down
8 changes: 8 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ Default: `false`

Disallow empty commit messages, useful in CI. [Read more][allow_abort]

### `message_length_limit`

Type: `int`

Default: `0`

Maximum length of the commit message. Setting it to `0` disables the length limit. It can be overridden by the `-l/--message-length-limit` command line argument.

### `allowed_prefixes`

Type: `list`
Expand Down
62 changes: 61 additions & 1 deletion tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from commitizen import cli, commands, git
from commitizen.exceptions import (
CommitMessageLengthExceededError,
InvalidCommandArgumentError,
InvalidCommitMessageError,
NoCommitsFoundError,
Expand Down Expand Up @@ -449,6 +450,65 @@ def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFi
arguments={"message": message, "message_length_limit": len(message) - 1},
)

with pytest.raises(InvalidCommitMessageError):
with pytest.raises(CommitMessageLengthExceededError):
check_cmd()
error_mock.assert_called_once()


def test_check_command_with_config_message_length_limit(config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
message = "fix(scope): some commit message"

config.settings["message_length_limit"] = len(message) + 1

check_cmd = commands.Check(
config=config,
arguments={"message": message},
)

check_cmd()
success_mock.assert_called_once()


def test_check_command_with_config_message_length_limit_exceeded(
config, mocker: MockFixture
):
error_mock = mocker.patch("commitizen.out.error")
message = "fix(scope): some commit message"

config.settings["message_length_limit"] = len(message) - 1

check_cmd = commands.Check(
config=config,
arguments={"message": message},
)

with pytest.raises(CommitMessageLengthExceededError):
check_cmd()
error_mock.assert_called_once()


def test_check_command_cli_overrides_config_message_length_limit(
config, mocker: MockFixture
):
success_mock = mocker.patch("commitizen.out.success")
message = "fix(scope): some commit message"

config.settings["message_length_limit"] = len(message) - 1

check_cmd = commands.Check(
config=config,
arguments={"message": message, "message_length_limit": len(message) + 1},
)

check_cmd()
success_mock.assert_called_once()

success_mock.reset_mock()
check_cmd = commands.Check(
config=config,
arguments={"message": message, "message_length_limit": 0},
)

check_cmd()
success_mock.assert_called_once()
59 changes: 59 additions & 0 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,62 @@ def test_commit_when_nothing_added_to_commit(config, mocker: MockFixture, out):

commit_mock.assert_called_once()
error_mock.assert_called_once_with(out)


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_command_with_config_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")

config.settings["message_length_limit"] = message_length
commands.Commit(config, {})()
success_mock.assert_called_once()

config.settings["message_length_limit"] = message_length - 1
with pytest.raises(CommitMessageLengthExceededError):
commands.Commit(config, {})()


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_command_cli_overrides_config_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")

config.settings["message_length_limit"] = message_length - 1

commands.Commit(config, {"message_length_limit": message_length})()
success_mock.assert_called_once()

success_mock.reset_mock()
commands.Commit(config, {"message_length_limit": 0})()
success_mock.assert_called_once()
2 changes: 2 additions & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"always_signoff": False,
"template": None,
"extras": {},
"message_length_limit": 0,
}

_new_settings: dict[str, Any] = {
Expand Down Expand Up @@ -126,6 +127,7 @@
"always_signoff": False,
"template": None,
"extras": {},
"message_length_limit": 0,
}


Expand Down
Loading