forked from commitizen-tools/commitizen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconventional_commits.py
More file actions
219 lines (198 loc) · 7.41 KB
/
conventional_commits.py
File metadata and controls
219 lines (198 loc) · 7.41 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, TypedDict
from commitizen import defaults
from commitizen.cz.base import BaseCommitizen
from commitizen.cz.utils import multiple_line_breaker, required_validator
if TYPE_CHECKING:
from commitizen.question import CzQuestion
__all__ = ["ConventionalCommitsCz"]
def _parse_scope(text: str) -> str:
return "-".join(text.strip().split())
def _parse_subject(text: str) -> str:
return required_validator(text.strip(".").strip(), msg="Subject is required.")
class ConventionalCommitsAnswers(TypedDict):
prefix: str
scope: str
subject: str
body: str
footer: str
is_breaking_change: bool
class ConventionalCommitsCz(BaseCommitizen):
bump_pattern = defaults.BUMP_PATTERN
bump_map = defaults.BUMP_MAP
bump_map_major_version_zero = defaults.BUMP_MAP_MAJOR_VERSION_ZERO
commit_parser = r"^((?P<change_type>feat|fix|refactor|perf|BREAKING CHANGE)(?:\((?P<scope>[^()\r\n]*)\)|\()?(?P<breaking>!)?|\w+!):\s(?P<message>.*)?"
change_type_map = {
"feat": "Feat",
"fix": "Fix",
"refactor": "Refactor",
"perf": "Perf",
}
changelog_pattern = defaults.BUMP_PATTERN
def questions(self) -> list[CzQuestion]:
return [
{
"type": "list",
"name": "prefix",
"message": "Select the type of change you are committing",
"choices": [
{
"value": "fix",
"name": "fix: A bug fix. Correlates with PATCH in SemVer",
"key": "x",
},
{
"value": "feat",
"name": "feat: A new feature. Correlates with MINOR in SemVer",
"key": "f",
},
{
"value": "docs",
"name": "docs: Documentation only changes. No version bump",
"key": "d",
},
{
"value": "style",
"name": (
"style: Changes that do not affect the "
"meaning of the code (white-space, formatting,"
" missing semi-colons, etc). No version bump"
),
"key": "s",
},
{
"value": "refactor",
"name": (
"refactor: A code change that neither fixes "
"a bug nor adds a feature. Correlates with PATCH in SemVer"
),
"key": "r",
},
{
"value": "perf",
"name": "perf: A code change that improves performance. Correlates with PATCH in SemVer",
"key": "p",
},
{
"value": "test",
"name": ("test: Adding missing or correcting existing tests. No version bump"),
"key": "t",
},
{
"value": "build",
"name": (
"build: Changes that affect the build system or "
"external dependencies (example scopes: pip, docker, npm). "
"No version bump"
),
"key": "b",
},
{
"value": "ci",
"name": (
"ci: Changes to CI configuration files and "
"scripts (example scopes: GitLabCI). No version bump"
),
"key": "c",
},
],
},
{
"type": "input",
"name": "scope",
"message": (
"What is the scope of this change? (class or file name): (press [enter] to skip)\n"
),
"filter": _parse_scope,
},
{
"type": "input",
"name": "subject",
"filter": _parse_subject,
"message": (
"Write a short and imperative summary of the code changes: (lower case and no period)\n"
),
},
{
"type": "input",
"name": "body",
"message": (
"Provide additional contextual information about the code changes: (press [enter] to skip)\n"
),
"filter": multiple_line_breaker,
},
{
"type": "confirm",
"name": "is_breaking_change",
"message": "Is this a BREAKING CHANGE? Correlates with MAJOR in SemVer",
"default": False,
},
{
"type": "input",
"name": "footer",
"message": (
"Footer. Information about Breaking Changes and "
"reference issues that this commit closes: (press [enter] to skip)\n"
),
},
]
def message(self, answers: ConventionalCommitsAnswers) -> str: # type: ignore[override]
prefix = answers["prefix"]
scope = answers["scope"]
subject = answers["subject"]
body = answers["body"]
footer = answers["footer"]
is_breaking_change = answers["is_breaking_change"]
formatted_scope = f"({scope})" if scope else ""
title = f"{prefix}{formatted_scope}"
if is_breaking_change:
if self.config.settings.get("breaking_change_exclamation_in_title", False):
title = f"{title}!"
footer = f"BREAKING CHANGE: {footer}"
formatted_body = f"\n\n{body}" if body else ""
formatted_footter = f"\n\n{footer}" if footer else ""
return f"{title}: {subject}{formatted_body}{formatted_footter}"
def example(self) -> str:
return (
"fix: correct minor typos in code\n"
"\n"
"see the issue for details on the typos fixed\n"
"\n"
"closes issue #12"
)
def schema(self) -> str:
return (
"<type>(<scope>): <subject>\n"
"<BLANK LINE>\n"
"<body>\n"
"<BLANK LINE>\n"
"(BREAKING CHANGE: )<footer>"
)
def schema_pattern(self) -> str:
change_types = (
"build",
"bump",
"chore",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test",
)
return (
r"(?s)" # To explicitly make . match new line
r"(" + "|".join(change_types) + r")" # type
r"(\(\S+\))?" # scope
r"!?"
r": "
r"([^\n\r]+)" # subject
r"((\n\n.*)|(\s*))?$"
)
def info(self) -> str:
filepath = Path(__file__).parent / "conventional_commits_info.txt"
return filepath.read_text(encoding=self.config.settings["encoding"])