FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
commitizen/commitizen/commands/check.py at master · commitizen-tools/commitizen · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
commitizen-tools
/
commitizen
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
349
Star
3.5k
Code
Issues
115
Pull requests
47
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
commitizen
/
commitizen
/
commands
/
check.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
169 lines (140 loc) · 5.67 KB
Breadcrumbs
commitizen
/
commitizen
/
commands
/
check.py
Copy path
File metadata and controls
169 lines (140 loc) · 5.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from
__future__
import
annotations
import
os
import
re
import
sys
from
pathlib
import
Path
from
typing
import
TYPE_CHECKING
,
TypedDict
from
commitizen
import
factory
,
git
,
out
from
commitizen
.
exceptions
import
(
InvalidCommandArgumentError
,
InvalidCommitMessageError
,
NoCommitsFoundError
,
)
if
TYPE_CHECKING
:
from
commitizen
.
config
import
BaseConfig
class
CheckArgs
(
TypedDict
,
total
=
False
):
commit_msg_file
:
str
commit_msg
:
str
rev_range
:
str
allow_abort
:
bool
message_length_limit
:
int
allowed_prefixes
:
list
[
str
]
message
:
str
use_default_range
:
bool
class
Check
:
"""Check if the current commit msg matches the commitizen format."""
def
__init__
(
self
,
config
:
BaseConfig
,
arguments
:
CheckArgs
,
*
args
:
object
)
->
None
:
"""Initial check command.
Args:
config: The config object required for the command to perform its action
arguments: All the flags provided by the user
cwd: Current work directory
"""
self
.
commit_msg_file
=
arguments
.
get
(
"commit_msg_file"
)
self
.
commit_msg
=
arguments
.
get
(
"message"
)
rev_range
=
arguments
.
get
(
"rev_range"
)
# Expand env vars so the packaged ``commitizen-branch`` pre-push hook
# (which passes the literal ``$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF``)
# keeps working after the ``shell=False`` switch in #1941. See #2003.
self
.
rev_range
=
os
.
path
.
expandvars
(
rev_range
)
if
rev_range
else
rev_range
self
.
allow_abort
=
bool
(
arguments
.
get
(
"allow_abort"
,
config
.
settings
[
"allow_abort"
])
)
self
.
use_default_range
=
bool
(
arguments
.
get
(
"use_default_range"
))
self
.
max_msg_length
=
arguments
.
get
(
"message_length_limit"
,
config
.
settings
.
get
(
"message_length_limit"
,
0
)
)
# we need to distinguish between None and [], which is a valid value
allowed_prefixes
=
arguments
.
get
(
"allowed_prefixes"
)
self
.
allowed_prefixes
:
list
[
str
]
=
(
allowed_prefixes
if
allowed_prefixes
is
not
None
else
config
.
settings
[
"allowed_prefixes"
]
)
num_exclusive_args_provided
=
sum
(
arg
is
not
None
for
arg
in
(
self
.
commit_msg_file
,
self
.
commit_msg
,
self
.
rev_range
,
)
)
if
num_exclusive_args_provided
>
1
:
raise
InvalidCommandArgumentError
(
"Only one of --rev-range, --message, and --commit-msg-file is permitted by check command! "
"See 'cz check -h' for more information"
)
if
num_exclusive_args_provided
==
0
and
not
sys
.
stdin
.
isatty
():
self
.
commit_msg
=
sys
.
stdin
.
read
()
self
.
config
:
BaseConfig
=
config
self
.
cz
=
factory
.
committer_factory
(
self
.
config
)
def
__call__
(
self
)
->
None
:
"""Validate if commit messages follows the conventional pattern.
Raises:
InvalidCommitMessageError: if the commit provided does not follow the conventional pattern
NoCommitsFoundError: if no commit is found with the given range
"""
commits
=
self
.
_get_commits
()
if
not
commits
:
raise
NoCommitsFoundError
(
f"No commit found with range: '
{
self
.
rev_range
}
'"
)
pattern
=
re
.
compile
(
self
.
cz
.
schema_pattern
())
invalid_commits
=
[
(
commit
,
check
.
errors
)
for
commit
in
commits
if
not
(
check
:=
self
.
cz
.
validate_commit_message
(
commit_msg
=
commit
.
message
,
pattern
=
pattern
,
allow_abort
=
self
.
allow_abort
,
allowed_prefixes
=
self
.
allowed_prefixes
,
max_msg_length
=
self
.
max_msg_length
,
commit_hash
=
commit
.
rev
,
)
).
is_valid
]
if
invalid_commits
:
raise
InvalidCommitMessageError
(
self
.
cz
.
format_exception_message
(
invalid_commits
)
)
out
.
success
(
"Commit validation: successful!"
)
def
_get_commit_message
(
self
)
->
str
|
None
:
if
self
.
commit_msg_file
is
None
:
# Get commit message from command line (--message)
return
self
.
commit_msg
# Get commit message from file (--commit-msg-file)
return
Path
(
self
.
commit_msg_file
).
read_text
(
encoding
=
self
.
config
.
settings
[
"encoding"
]
)
def
_get_commits
(
self
)
->
list
[
git
.
GitCommit
]:
if
(
msg
:=
self
.
_get_commit_message
())
is
not
None
:
return
[
git
.
GitCommit
(
rev
=
""
,
title
=
""
,
body
=
self
.
_filter_comments
(
msg
))]
# Get commit messages from git log (--rev-range)
return
git
.
get_commits
(
git
.
get_default_branch
()
if
self
.
use_default_range
else
None
,
self
.
rev_range
,
)
@
staticmethod
def
_filter_comments
(
msg
:
str
)
->
str
:
"""Filter the commit message by removing comments.
When using `git commit --verbose`, we exclude the diff that is going to
generated, like the following example:
```bash
...
# ------------------------ >8 ------------------------
# Do not modify or remove the line above.
# Everything below it will be ignored.
diff --git a/... b/...
...
```
Args:
msg: The commit message to filter.
Returns:
The filtered commit message without comments.
"""
lines
:
list
[
str
]
=
[]
for
line
in
msg
.
split
(
"
\n
"
):
if
"# ------------------------ >8 ------------------------"
in
line
:
break
if
not
line
.
startswith
(
"#"
):
lines
.
append
(
line
)
return
"
\n
"
.
join
(
lines
)
Back
|
FazBrowse Home
|
New Git URL