Skip to content

Commit f3a8769

Browse files
committed
feat: add --no-raise to avoid raising error codes
Closes #485
1 parent 6efad39 commit f3a8769

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

commitizen/cli.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import sys
44
from functools import partial
5+
from typing import List
56

67
import argcomplete
78
from decli import cli
@@ -24,6 +25,13 @@
2425
"name": ["-n", "--name"],
2526
"help": "use the given commitizen (default: cz_conventional_commits)",
2627
},
28+
{
29+
"name": ["-nr", "--no-raise"],
30+
"action": "append",
31+
"type": int,
32+
"required": False,
33+
"help": "error codes that won't rise error, provide -nr multiple times for many",
34+
},
2735
],
2836
"subcommands": {
2937
"title": "commands",
@@ -268,13 +276,20 @@
268276
original_excepthook = sys.excepthook
269277

270278

271-
def commitizen_excepthook(type, value, tracekback, debug=False):
279+
def commitizen_excepthook(
280+
type, value, tracekback, debug=False, no_raise: List[int] = None
281+
):
282+
if no_raise is None:
283+
no_raise = []
272284
if isinstance(value, CommitizenException):
273285
if value.message:
274286
value.output_method(value.message)
275287
if debug:
276288
original_excepthook(type, value, tracekback)
277-
sys.exit(value.exit_code)
289+
exit_code = value.exit_code
290+
if exit_code in no_raise:
291+
exit_code = 0
292+
sys.exit(exit_code)
278293
else:
279294
original_excepthook(type, value, tracekback)
280295

@@ -313,6 +328,11 @@ def main():
313328
if args.debug:
314329
logging.getLogger("commitizen").setLevel(logging.DEBUG)
315330
sys.excepthook = commitizen_debug_excepthook
331+
elif args.no_raise:
332+
no_raise_debug_excepthook = partial(
333+
commitizen_excepthook, no_raise=args.no_raise
334+
)
335+
sys.excepthook = no_raise_debug_excepthook
316336

317337
args.func(conf, vars(args))()
318338

tests/test_cli.py

+13
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,16 @@ def test_argcomplete_activation():
9595
output = subprocess.run(["register-python-argcomplete", "cz"])
9696

9797
assert output.returncode == 0
98+
99+
100+
def test_commitizen_excepthook_no_raises(capsys):
101+
with pytest.raises(SystemExit) as excinfo:
102+
cli.commitizen_excepthook(
103+
NotAGitProjectError,
104+
NotAGitProjectError(),
105+
"",
106+
no_raise=[NotAGitProjectError.exit_code],
107+
)
108+
109+
assert excinfo.type == SystemExit
110+
assert excinfo.value.code == 0

0 commit comments

Comments
 (0)