77from commitizen import factory , git , out
88from commitizen .config import BaseConfig
99from commitizen .exceptions import (
10+ CommitMessageLengthExceededError ,
1011 InvalidCommandArgumentError ,
1112 InvalidCommitMessageError ,
1213 NoCommitsFoundError ,
@@ -18,7 +19,7 @@ class CheckArgs(TypedDict, total=False):
1819 commit_msg : str
1920 rev_range : str
2021 allow_abort : bool
21- message_length_limit : int
22+ message_length_limit : int | None
2223 allowed_prefixes : list [str ]
2324 message : str
2425 use_default_range : bool
@@ -41,8 +42,11 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
4142 self .allow_abort = bool (
4243 arguments .get ("allow_abort" , config .settings ["allow_abort" ])
4344 )
45+
4446 self .use_default_range = bool (arguments .get ("use_default_range" ))
45- self .max_msg_length = arguments .get ("message_length_limit" , 0 )
47+ self .max_msg_length = arguments .get (
48+ "message_length_limit" , config .settings .get ("message_length_limit" , None )
49+ )
4650
4751 # we need to distinguish between None and [], which is a valid value
4852 allowed_prefixes = arguments .get ("allowed_prefixes" )
@@ -88,7 +92,7 @@ def __call__(self) -> None:
8892 invalid_msgs_content = "\n " .join (
8993 f'commit "{ commit .rev } ": "{ commit .message } "'
9094 for commit in commits
91- if not self ._validate_commit_message (commit .message , pattern )
95+ if not self ._validate_commit_message (commit .message , pattern , commit . rev )
9296 )
9397 if invalid_msgs_content :
9498 # TODO: capitalize the first letter of the error message for consistency in v5
@@ -153,17 +157,22 @@ def _filter_comments(msg: str) -> str:
153157 return "\n " .join (lines )
154158
155159 def _validate_commit_message (
156- self , commit_msg : str , pattern : re .Pattern [str ]
160+ self , commit_msg : str , pattern : re .Pattern [str ], commit_hash : str
157161 ) -> bool :
158162 if not commit_msg :
159163 return self .allow_abort
160164
161165 if any (map (commit_msg .startswith , self .allowed_prefixes )):
162166 return True
163167
164- if self .max_msg_length :
168+ if self .max_msg_length is not None :
165169 msg_len = len (commit_msg .partition ("\n " )[0 ].strip ())
166170 if msg_len > self .max_msg_length :
167- return False
171+ raise CommitMessageLengthExceededError (
172+ f"commit validation: failed!\n "
173+ f"commit message length exceeds the limit.\n "
174+ f'commit "{ commit_hash } ": "{ commit_msg } "\n '
175+ f"message length limit: { self .max_msg_length } (actual: { msg_len } )"
176+ )
168177
169178 return bool (pattern .match (commit_msg ))
0 commit comments