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" ,
268
280
original_excepthook = sys .excepthook
269
281
270
282
271
- def commitizen_excepthook (type , value , tracekback , debug = False ):
283
+ def commitizen_excepthook (
284
+ type , value , tracekback , debug = False , no_raise : List [int ] = None
285
+ ):
286
+ if not no_raise :
287
+ no_raise = []
272
288
if isinstance (value , CommitizenException ):
273
289
if value .message :
274
290
value .output_method (value .message )
275
291
if debug :
276
292
original_excepthook (type , value , tracekback )
277
- sys .exit (value .exit_code )
293
+ exit_code = value .exit_code
294
+ if exit_code in no_raise :
295
+ exit_code = 0
296
+ sys .exit (exit_code )
278
297
else :
279
298
original_excepthook (type , value , tracekback )
280
299
@@ -284,6 +303,27 @@ def commitizen_excepthook(type, value, tracekback, debug=False):
284
303
sys .excepthook = commitizen_excepthook
285
304
286
305
306
+ def parse_no_raise (comma_separated_no_raise : str ) -> List [int ]:
307
+ """
308
+ Convert the given string with exit code digits or exit
309
+ codes name to its integer representation
310
+ """
311
+ no_raise_items = comma_separated_no_raise .split ("," )
312
+ no_raise_codes = []
313
+ for item in no_raise_items :
314
+ if item .isdecimal ():
315
+ no_raise_codes .append (int (item ))
316
+ continue
317
+ try :
318
+ exit_code = ExitCode [item ]
319
+ except KeyError :
320
+ out .warn (f"WARN: no_raise key { item } does not exist. Skipping." )
321
+ continue
322
+ else :
323
+ no_raise_codes .append (exit_code .value )
324
+ return no_raise_codes
325
+
326
+
287
327
def main ():
288
328
conf = config .read_cfg ()
289
329
parser = cli (data )
@@ -313,6 +353,12 @@ def main():
313
353
if args .debug :
314
354
logging .getLogger ("commitizen" ).setLevel (logging .DEBUG )
315
355
sys .excepthook = commitizen_debug_excepthook
356
+ elif args .no_raise :
357
+ no_raise_exit_codes = parse_no_raise (args .no_raise )
358
+ no_raise_debug_excepthook = partial (
359
+ commitizen_excepthook , no_raise = no_raise_exit_codes
360
+ )
361
+ sys .excepthook = no_raise_debug_excepthook
316
362
317
363
args .func (conf , vars (args ))()
318
364
0 commit comments