3
3
import os
4
4
import re
5
5
import sys
6
- from typing import Any
6
+ from typing import Any , TypedDict
7
7
8
8
from commitizen import factory , git , out
9
9
from commitizen .config import BaseConfig
14
14
)
15
15
16
16
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
+
17
29
class Check :
18
30
"""Check if the current commit msg matches the commitizen format."""
19
31
@@ -31,7 +43,9 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
31
43
self .allow_abort : bool = bool (
32
44
arguments .get ("allow_abort" , config .settings ["allow_abort" ])
33
45
)
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 )
35
49
36
50
# we need to distinguish between None and [], which is a valid value
37
51
@@ -42,25 +56,28 @@ def __init__(self, config: BaseConfig, arguments: dict[str, Any], cwd=os.getcwd(
42
56
else config .settings ["allowed_prefixes" ]
43
57
)
44
58
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 (
53
60
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
+ )
55
66
)
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 :
59
69
raise InvalidCommandArgumentError (
60
70
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
61
71
"See 'cz check -h' for more information"
62
72
)
63
73
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
+
64
81
def __call__ (self ):
65
82
"""Validate if commit messages follows the conventional pattern.
66
83
@@ -102,7 +119,10 @@ def _get_commits(self):
102
119
return [git .GitCommit (rev = "" , title = "" , body = self ._filter_comments (msg ))]
103
120
104
121
# 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
+ )
106
126
107
127
@staticmethod
108
128
def _filter_comments (msg : str ) -> str :
@@ -136,6 +156,9 @@ def _filter_comments(msg: str) -> str:
136
156
return "\n " .join (lines )
137
157
138
158
def validate_commit_message (self , commit_msg : str , pattern : str ) -> bool :
159
+ if self .verbose :
160
+ out .info (commit_msg )
161
+
139
162
if not commit_msg :
140
163
return self .allow_abort
141
164
0 commit comments