33import os
44import re
55import sys
6- from typing import Any
6+ from typing import Any , TypedDict
77
88from commitizen import factory , git , out
99from commitizen .config import BaseConfig
1414)
1515
1616
17+ class CheckArgs (TypedDict , total = False ):
18+ commit_msg_file : str
19+ commit_msg : str
20+ rev_range : str
21+ allow_abort : bool
22+ message_length_limit : int
23+ allowed_prefixes : list [str ]
24+ message : str
25+ default_range : bool
26+ verbose : bool
27+
28+
1729class Check :
1830 """Check if the current commit msg matches the commitizen format."""
1931
@@ -31,7 +43,9 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
3143 self .allow_abort : bool = bool (
3244 arguments .get ("allow_abort" , config .settings ["allow_abort" ])
3345 )
34- self .max_msg_length : int = arguments .get ("message_length_limit" , 0 )
46+ self .default_range = bool (arguments .get ("default_range" ))
47+ self .verbose = bool (arguments .get ("verbose" ))
48+ self .max_msg_length = arguments .get ("message_length_limit" , 0 )
3549
3650 # we need to distinguish between None and [], which is a valid value
3751
@@ -42,25 +56,28 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
4256 else config .settings ["allowed_prefixes" ]
4357 )
4458
45- self ._valid_command_argument ()
46-
47- self .config : BaseConfig = config
48- self .encoding = config .settings ["encoding" ]
49- self .cz = factory .committer_factory (self .config )
50-
51- def _valid_command_argument (self ):
52- num_exclusive_args_provided = sum (
59+ num_exclusive_args_provided = self .default_range + sum (
5360 arg is not None
54- for arg in (self .commit_msg_file , self .commit_msg , self .rev_range )
61+ for arg in (
62+ self .commit_msg_file ,
63+ self .commit_msg ,
64+ self .rev_range ,
65+ )
5566 )
56- if num_exclusive_args_provided == 0 and not sys .stdin .isatty ():
57- self .commit_msg = sys .stdin .read ()
58- elif num_exclusive_args_provided != 1 :
67+
68+ if num_exclusive_args_provided > 1 :
5969 raise InvalidCommandArgumentError (
6070 "Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
6171 "See 'cz check -h' for more information"
6272 )
6373
74+ if num_exclusive_args_provided == 0 and not sys .stdin .isatty ():
75+ self .commit_msg = sys .stdin .read ()
76+
77+ self .config : BaseConfig = config
78+ self .encoding = config .settings ["encoding" ]
79+ self .cz = factory .committer_factory (self .config )
80+
6481 def __call__ (self ):
6582 """Validate if commit messages follows the conventional pattern.
6683
@@ -102,7 +119,10 @@ def _get_commits(self):
102119 return [git .GitCommit (rev = "" , title = "" , body = self ._filter_comments (msg ))]
103120
104121 # Get commit messages from git log (--rev-range)
105- return git .get_commits (end = self .rev_range or "HEAD" )
122+ return git .get_commits (
123+ git .get_default_branch () if self .default_range else None ,
124+ self .rev_range or "HEAD" ,
125+ )
106126
107127 @staticmethod
108128 def _filter_comments (msg : str ) -> str :
@@ -136,6 +156,9 @@ def _filter_comments(msg: str) -> str:
136156 return "\n " .join (lines )
137157
138158 def validate_commit_message (self , commit_msg : str , pattern : str ) -> bool :
159+ if self .verbose :
160+ out .info (commit_msg )
161+
139162 if not commit_msg :
140163 return self .allow_abort
141164
0 commit comments