2
2
import logging
3
3
import sys
4
4
from functools import partial
5
+ from typing import List
5
6
6
7
import argcomplete
7
8
from decli import cli
8
9
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
+ )
11
17
12
18
logger = logging .getLogger (__name__ )
13
19
data = {
24
30
"name" : ["-n" , "--name" ],
25
31
"help" : "use the given commitizen (default: cz_conventional_commits)" ,
26
32
},
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
+ },
27
39
],
28
40
"subcommands" : {
29
41
"title" : "commands" ,
274
286
original_excepthook = sys .excepthook
275
287
276
288
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 = []
278
294
if isinstance (value , CommitizenException ):
279
295
if value .message :
280
296
value .output_method (value .message )
281
297
if debug :
282
298
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 )
284
303
else :
285
304
original_excepthook (type , value , tracekback )
286
305
@@ -290,6 +309,27 @@ def commitizen_excepthook(type, value, tracekback, debug=False):
290
309
sys .excepthook = commitizen_excepthook
291
310
292
311
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
+
293
333
def main ():
294
334
conf = config .read_cfg ()
295
335
parser = cli (data )
@@ -319,6 +359,12 @@ def main():
319
359
if args .debug :
320
360
logging .getLogger ("commitizen" ).setLevel (logging .DEBUG )
321
361
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
322
368
323
369
args .func (conf , vars (args ))()
324
370
0 commit comments