forked from commitizen-tools/commitizen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
411 lines (390 loc) · 15.2 KB
/
cli.py
File metadata and controls
411 lines (390 loc) · 15.2 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import argparse
import logging
import sys
from functools import partial
from typing import List
import argcomplete
from decli import cli
from commitizen import commands, config, out
from commitizen.exceptions import (
CommitizenException,
ExitCode,
ExpectedExit,
NoCommandFoundError,
)
logger = logging.getLogger(__name__)
data = {
"prog": "cz",
"description": (
"Commitizen is a cli tool to generate conventional commits.\n"
"For more information about the topic go to "
"https://conventionalcommits.org/"
),
"formatter_class": argparse.RawDescriptionHelpFormatter,
"arguments": [
{"name": "--debug", "action": "store_true", "help": "use debug mode"},
{
"name": ["-n", "--name"],
"help": "use the given commitizen (default: cz_conventional_commits)",
},
{
"name": ["-nr", "--no-raise"],
"type": str,
"required": False,
"help": "comma separated error codes that won't rise error, e.g: cz -nr 1,2,3 bump. See codes at https://commitizen-tools.github.io/commitizen/exit_codes/",
},
],
"subcommands": {
"title": "commands",
"required": True,
"commands": [
{
"name": ["init"],
"help": "init commitizen configuration",
"func": commands.Init,
},
{
"name": ["commit", "c"],
"help": "create new commit",
"func": commands.Commit,
"arguments": [
{
"name": ["--retry"],
"action": "store_true",
"help": "retry last commit",
},
{
"name": "--dry-run",
"action": "store_true",
"help": "show output to stdout, no commit, no modified files",
},
{
"name": ["-s", "--signoff"],
"action": "store_true",
"help": "Sign off the commit",
},
],
},
{
"name": "ls",
"help": "show available commitizens",
"func": commands.ListCz,
},
{
"name": "example",
"help": "show commit example",
"func": commands.Example,
},
{
"name": "info",
"help": "show information about the cz",
"func": commands.Info,
},
{"name": "schema", "help": "show commit schema", "func": commands.Schema},
{
"name": "bump",
"help": "bump semantic version based on the git log",
"func": commands.Bump,
"arguments": [
{
"name": "--dry-run",
"action": "store_true",
"help": "show output to stdout, no commit, no modified files",
},
{
"name": "--files-only",
"action": "store_true",
"help": "bump version in the files from the config",
},
{
"name": "--local-version",
"action": "store_true",
"help": "bump only the local version portion",
},
{
"name": ["--changelog", "-ch"],
"action": "store_true",
"default": False,
"help": "generate the changelog for the newest version",
},
{
"name": ["--no-verify"],
"action": "store_true",
"default": False,
"help": "this option bypasses the pre-commit and commit-msg hooks",
},
{
"name": "--yes",
"action": "store_true",
"help": "accept automatically questions done",
},
{
"name": "--tag-format",
"help": (
"the format used to tag the commit and read it, "
"use it in existing projects, "
"wrap around simple quotes"
),
},
{
"name": "--bump-message",
"help": (
"template used to create the release commit, "
"useful when working with CI"
),
},
{
"name": ["--prerelease", "-pr"],
"help": "choose type of prerelease",
"choices": ["alpha", "beta", "rc"],
},
{
"name": ["--devrelease", "-d"],
"help": "specify non-negative integer for dev. release",
"type": int,
},
{
"name": ["--increment"],
"help": "manually specify the desired increment",
"choices": ["MAJOR", "MINOR", "PATCH"],
"type": str.upper,
},
{
"name": ["--check-consistency", "-cc"],
"help": (
"check consistency among versions defined in "
"commitizen configuration and version_files"
),
"action": "store_true",
},
{
"name": ["--annotated-tag", "-at"],
"help": "create annotated tag instead of lightweight one",
"action": "store_true",
},
{
"name": ["--gpg-sign", "-s"],
"help": "sign tag instead of lightweight one",
"default": False,
"action": "store_true",
},
{
"name": ["--changelog-to-stdout"],
"action": "store_true",
"default": False,
"help": "Output changelog to the stdout",
},
{
"name": ["--retry"],
"action": "store_true",
"default": False,
"help": "retry commit if it fails the 1st time",
},
{
"name": ["--major-version-zero"],
"action": "store_true",
"default": None,
"help": "keep major version at zero, even for breaking changes",
},
{
"name": "manual_version",
"type": str,
"nargs": "?",
"help": "bump to the given version (e.g: 1.5.3)",
"metavar": "MANUAL_VERSION",
},
],
},
{
"name": ["changelog", "ch"],
"help": (
"generate changelog (note that it will overwrite existing file)"
),
"func": commands.Changelog,
"arguments": [
{
"name": "--dry-run",
"action": "store_true",
"default": False,
"help": "show changelog to stdout",
},
{
"name": "--file-name",
"help": "file name of changelog (default: 'CHANGELOG.md')",
},
{
"name": "--unreleased-version",
"help": (
"set the value for the new version (use the tag value), "
"instead of using unreleased"
),
},
{
"name": "--incremental",
"action": "store_true",
"default": False,
"help": (
"generates changelog from last created version, "
"useful if the changelog has been manually modified"
),
},
{
"name": "rev_range",
"type": str,
"nargs": "?",
"help": "generates changelog for the given version (e.g: 1.5.3) or version range (e.g: 1.5.3..1.7.9)",
},
{
"name": "--start-rev",
"default": None,
"help": (
"start rev of the changelog."
"If not set, it will generate changelog from the start"
),
},
],
},
{
"name": ["check"],
"help": "validates that a commit message matches the commitizen schema",
"func": commands.Check,
"arguments": [
{
"name": "--commit-msg-file",
"help": (
"ask for the name of the temporal file that contains "
"the commit message. "
"Using it in a git hook script: MSG_FILE=$1"
),
"exclusive_group": "group1",
},
{
"name": "--rev-range",
"help": "a range of git rev to check. e.g, master..HEAD",
"exclusive_group": "group1",
},
{
"name": ["-m", "--message"],
"help": "commit message that needs to be checked",
"exclusive_group": "group1",
},
{
"name": ["--allow-abort"],
"action": "store_true",
"default": False,
"help": "allow empty commit messages, which typically abort a commit",
},
],
},
{
"name": ["version"],
"help": (
"get the version of the installed commitizen or the current project"
" (default: installed commitizen)"
),
"func": commands.Version,
"arguments": [
{
"name": ["-r", "--report"],
"help": "get system information for reporting bugs",
"action": "store_true",
"exclusive_group": "group1",
},
{
"name": ["-p", "--project"],
"help": "get the version of the current project",
"action": "store_true",
"exclusive_group": "group1",
},
{
"name": ["-c", "--commitizen"],
"help": "get the version of the installed commitizen",
"action": "store_true",
"exclusive_group": "group1",
},
{
"name": ["-v", "--verbose"],
"help": (
"get the version of both the installed commitizen "
"and the current project"
),
"action": "store_true",
"exclusive_group": "group1",
},
],
},
],
},
}
original_excepthook = sys.excepthook
def commitizen_excepthook(
type, value, tracekback, debug=False, no_raise: List[int] = None
):
if not no_raise:
no_raise = []
if isinstance(value, CommitizenException):
if value.message:
value.output_method(value.message)
if debug:
original_excepthook(type, value, tracekback)
exit_code = value.exit_code
if exit_code in no_raise:
exit_code = 0
sys.exit(exit_code)
else:
original_excepthook(type, value, tracekback)
commitizen_debug_excepthook = partial(commitizen_excepthook, debug=True)
sys.excepthook = commitizen_excepthook
def parse_no_raise(comma_separated_no_raise: str) -> List[int]:
"""Convert the given string to exit codes.
Receives digits and strings and outputs the parsed integer which
represents the exit code found in exceptions.
"""
no_raise_items: List[str] = comma_separated_no_raise.split(",")
no_raise_codes = []
for item in no_raise_items:
if item.isdecimal():
no_raise_codes.append(int(item))
continue
try:
exit_code = ExitCode[item.strip()]
except KeyError:
out.warn(f"WARN: no_raise key `{item}` does not exist. Skipping.")
continue
else:
no_raise_codes.append(exit_code.value)
return no_raise_codes
def main():
conf = config.read_cfg()
parser = cli(data)
argcomplete.autocomplete(parser)
# Show help if no arg provided
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
raise ExpectedExit()
# This is for the command required constraint in 2.0
try:
args = parser.parse_args()
except (TypeError, SystemExit) as e:
# https://github.com/commitizen-tools/commitizen/issues/429
# argparse raises TypeError when non exist command is provided on Python < 3.9
# but raise SystemExit with exit code == 2 on Python 3.9
if isinstance(e, TypeError) or (isinstance(e, SystemExit) and e.code == 2):
raise NoCommandFoundError()
raise e
if args.name:
conf.update({"name": args.name})
elif not args.name and not conf.path:
conf.update({"name": "cz_conventional_commits"})
if args.debug:
logging.getLogger("commitizen").setLevel(logging.DEBUG)
sys.excepthook = commitizen_debug_excepthook
elif args.no_raise:
no_raise_exit_codes = parse_no_raise(args.no_raise)
no_raise_debug_excepthook = partial(
commitizen_excepthook, no_raise=no_raise_exit_codes
)
sys.excepthook = no_raise_debug_excepthook
args.func(conf, vars(args))()
if __name__ == "__main__":
main()