22import logging
33import sys
44from functools import partial
5+ from typing import List
56
67import argcomplete
78from decli import cli
89
9- from commitizen import commands , config
10- from commitizen .exceptions import CommitizenException , ExpectedExit , NoCommandFoundError
10+ from commitizen import commands , config , out
11+ from commitizen .exceptions import (
12+ CommitizenException ,
13+ ExitCode ,
14+ ExpectedExit ,
15+ NoCommandFoundError ,
16+ )
1117
1218logger = logging .getLogger (__name__ )
1319data = {
2430 "name" : ["-n" , "--name" ],
2531 "help" : "use the given commitizen (default: cz_conventional_commits)" ,
2632 },
33+ {
34+ "name" : ["-nr" , "--no-raise" ],
35+ "type" : str ,
36+ "required" : False ,
37+ "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/" ,
38+ },
2739 ],
2840 "subcommands" : {
2941 "title" : "commands" ,
274286original_excepthook = sys .excepthook
275287
276288
277- def commitizen_excepthook (type , value , tracekback , debug = False ):
289+ def commitizen_excepthook (
290+ type , value , tracekback , debug = False , no_raise : List [int ] = None
291+ ):
292+ if not no_raise :
293+ no_raise = []
278294 if isinstance (value , CommitizenException ):
279295 if value .message :
280296 value .output_method (value .message )
281297 if debug :
282298 original_excepthook (type , value , tracekback )
283- sys .exit (value .exit_code )
299+ exit_code = value .exit_code
300+ if exit_code in no_raise :
301+ exit_code = 0
302+ sys .exit (exit_code )
284303 else :
285304 original_excepthook (type , value , tracekback )
286305
@@ -290,6 +309,27 @@ def commitizen_excepthook(type, value, tracekback, debug=False):
290309sys .excepthook = commitizen_excepthook
291310
292311
312+ def parse_no_raise (comma_separated_no_raise : str ) -> List [int ]:
313+ """
314+ Convert the given string with exit code digits or exit
315+ codes name to its integer representation
316+ """
317+ no_raise_items = comma_separated_no_raise .split ("," )
318+ no_raise_codes = []
319+ for item in no_raise_items :
320+ if item .isdecimal ():
321+ no_raise_codes .append (int (item ))
322+ continue
323+ try :
324+ exit_code = ExitCode [item ]
325+ except KeyError :
326+ out .warn (f"WARN: no_raise key { item } does not exist. Skipping." )
327+ continue
328+ else :
329+ no_raise_codes .append (exit_code .value )
330+ return no_raise_codes
331+
332+
293333def main ():
294334 conf = config .read_cfg ()
295335 parser = cli (data )
@@ -319,6 +359,12 @@ def main():
319359 if args .debug :
320360 logging .getLogger ("commitizen" ).setLevel (logging .DEBUG )
321361 sys .excepthook = commitizen_debug_excepthook
362+ elif args .no_raise :
363+ no_raise_exit_codes = parse_no_raise (args .no_raise )
364+ no_raise_debug_excepthook = partial (
365+ commitizen_excepthook , no_raise = no_raise_exit_codes
366+ )
367+ sys .excepthook = no_raise_debug_excepthook
322368
323369 args .func (conf , vars (args ))()
324370
0 commit comments