|
9 | 9 | # constant
|
10 | 10 | LINE_LIMIT = 100
|
11 | 11 | BODY_REQUIRED = False
|
| 12 | +# TODO: moved to config later, changed to zh-CN after fix encoding issue |
| 13 | +LANG = 'en' |
12 | 14 |
|
13 | 15 | TYPE_LIST = [
|
14 | 16 | 'feat', # 新功能(feature)
|
|
38 | 40 | module=__name__)
|
39 | 41 |
|
40 | 42 | # error message
|
| 43 | +# TODO: moved to config or lang file later |
41 | 44 | ERROR_MESSAGES = {
|
42 | | - # Normal case |
43 | | - ErrorEnum.VALIDATED: '{errorname}:commit message 符合规范。', |
44 | | - ErrorEnum.MERGE: '{errorname}:检测到 merge commit,跳过规范检查。', |
45 | | - # File error |
46 | | - ErrorEnum.ARG_MISSING: '错误 {errorname}:缺少 commit message 文件参数。', |
47 | | - ErrorEnum.FILE_MISSING: '错误 {errorname}:文件 {filepath} 不存在。', |
48 | | - # Empty content |
49 | | - ErrorEnum.EMPTY_MESSAGE: '错误 {errorname}:commit message 没有内容或只有空白字符。', |
50 | | - ErrorEnum.EMPTY_HEADER: '错误 {errorname}:header (首行) 没有内容或只有空白字符。', |
51 | | - # Header error |
52 | | - ErrorEnum.BAD_HEADER_FORMAT: '错误 {errorname}:header (首行) 不符合规范:\n{header}\n如果检查没有发现错误,请确认是否使用了中文冒号,以及冒号后面漏了空格。', |
53 | | - ErrorEnum.WRONG_TYPE: '错误 {errorname}:{type} 不是以下关键字之一:\n%s' % (', '.join(TYPE_LIST)), |
54 | | - # Body error |
55 | | - ErrorEnum.BODY_MISSING: '错误 {errorname}:body 没有内容或只有空白字符。', # 仅 BODY_REQUIRED 为 True时生效 |
56 | | - ErrorEnum.NO_BLANK_LINE_BEFORE_BODY: '错误 {errorname}:header 和 body 之间没有空一行。', |
57 | | - # Common error |
58 | | - ErrorEnum.LINE_OVERLONG: '错误 {errorname}:单行内容长度为{length},超过了%d个字符:\n{line}' % (LINE_LIMIT) |
| 45 | + 'en': { |
| 46 | + # Normal case |
| 47 | + ErrorEnum.VALIDATED: '{errorname}: commit message meet the rule.', |
| 48 | + ErrorEnum.MERGE: '{errorname}: merge commit detected,skip check.', |
| 49 | + # File error |
| 50 | + ErrorEnum.ARG_MISSING: 'Error {errorname}: commit message file argument missing.', |
| 51 | + ErrorEnum.FILE_MISSING: 'Error {errorname}: file {filepath} not exists.', |
| 52 | + # Empty content |
| 53 | + ErrorEnum.EMPTY_MESSAGE: 'Error {errorname}: commit message has no content except whitespaces.', |
| 54 | + ErrorEnum.EMPTY_HEADER: 'Error {errorname}: header (first line) has no content except whitespaces.', |
| 55 | + # Header error |
| 56 | + ErrorEnum.BAD_HEADER_FORMAT: 'Error {errorname}: header (first line) not following the rule:\n{header}\nif you can not find any error after check, maybe you use Chinese colon, or lack of whitespace after the colon.', |
| 57 | + ErrorEnum.WRONG_TYPE: 'Error {errorname}: {type} one of the keywords:\n%s' % (', '.join(TYPE_LIST)), |
| 58 | + # Body error |
| 59 | + ErrorEnum.BODY_MISSING: 'Error {errorname}: body has no content except whitespaces.', # 仅 BODY_REQUIRED 为 True时生效 |
| 60 | + ErrorEnum.NO_BLANK_LINE_BEFORE_BODY: 'Error {errorname}: no empty line between header and body.', |
| 61 | + # Common error |
| 62 | + ErrorEnum.LINE_OVERLONG: 'Error {errorname}: the length of line is {length}, exceed %d:\n{line}' % (LINE_LIMIT) |
| 63 | + }, |
| 64 | + 'zh-CN': { |
| 65 | + # Normal case |
| 66 | + ErrorEnum.VALIDATED: '{errorname}:commit message 符合规范。', |
| 67 | + ErrorEnum.MERGE: '{errorname}:检测到 merge commit,跳过规范检查。', |
| 68 | + # File error |
| 69 | + ErrorEnum.ARG_MISSING: '错误 {errorname}:缺少 commit message 文件参数。', |
| 70 | + ErrorEnum.FILE_MISSING: '错误 {errorname}:文件 {filepath} 不存在。', |
| 71 | + # Empty content |
| 72 | + ErrorEnum.EMPTY_MESSAGE: '错误 {errorname}:commit message 没有内容或只有空白字符。', |
| 73 | + ErrorEnum.EMPTY_HEADER: '错误 {errorname}:header (首行) 没有内容或只有空白字符。', |
| 74 | + # Header error |
| 75 | + ErrorEnum.BAD_HEADER_FORMAT: '错误 {errorname}:header (首行) 不符合规范:\n{header}\n如果检查没有发现错误,请确认是否使用了中文冒号,以及冒号后面漏了空格。', |
| 76 | + ErrorEnum.WRONG_TYPE: '错误 {errorname}:{type} 不是以下关键字之一:\n%s' % (', '.join(TYPE_LIST)), |
| 77 | + # Body error |
| 78 | + ErrorEnum.BODY_MISSING: '错误 {errorname}:body 没有内容或只有空白字符。', # 仅 BODY_REQUIRED 为 True时生效 |
| 79 | + ErrorEnum.NO_BLANK_LINE_BEFORE_BODY: '错误 {errorname}:header 和 body 之间没有空一行。', |
| 80 | + # Common error |
| 81 | + ErrorEnum.LINE_OVERLONG: '错误 {errorname}:单行内容长度为{length},超过了%d个字符:\n{line}' % (LINE_LIMIT) |
| 82 | + } |
59 | 83 | }
|
60 | 84 |
|
61 | 85 | NON_FORMAT_ERROR = (
|
|
65 | 89 | ErrorEnum.FILE_MISSING
|
66 | 90 | )
|
67 | 91 |
|
68 | | -RULE_MESSAGE = ''' |
| 92 | +# TODO: moved to config or lang file later |
| 93 | +RULE_MESSAGE = { |
| 94 | + 'en': ''' |
| 95 | +Commit message rule as follow: |
| 96 | +<type>(<scope>): <subject> |
| 97 | +// empty line |
| 98 | +<body> |
| 99 | +// empty line |
| 100 | +<footer> |
| 101 | + |
| 102 | +(<scope>), <body> and <footer> are optional |
| 103 | +<type> must be one of %s |
| 104 | +more specific instructions, please refer to http://192.168.19.127:3000/jayce/git-hook-commit-msg''' % (', '.join(TYPE_LIST)), |
| 105 | + |
| 106 | + 'zh-CN': ''' |
69 | 107 | Commit message 的格式要求如下:
|
70 | 108 | <type>(<scope>): <subject>
|
71 | 109 | // 空一行
|
|
76 | 114 | 其中 (<scope>) <body> 和 <footer> 可选
|
77 | 115 | <type> 必须是 %s 中的一个
|
78 | 116 | 更详细的格式要求说明,请参考 http://192.168.19.127:3000/jayce/git-hook-commit-msg''' % (', '.join(TYPE_LIST))
|
| 117 | +} |
79 | 118 |
|
80 | 119 | MERGE_PATTEN = r'^Merge '
|
81 | 120 | # 弱匹配,只检查基本的格式,各个部分允许为空,留到match.group(x)部分检查,以提供更详细的报错信息
|
|
88 | 127 |
|
89 | 128 | def print_error_msg(state, **kwargs):
|
90 | 129 | kwargs['errorname'] = state.name
|
91 | | - print(ERROR_MESSAGES[state].format(**kwargs)) |
| 130 | + print(ERROR_MESSAGES[LANG][state].format(**kwargs)) |
92 | 131 | if state not in NON_FORMAT_ERROR:
|
93 | | - print(RULE_MESSAGE) |
| 132 | + print(RULE_MESSAGE[LANG]) |
94 | 133 |
|
95 | 134 |
|
96 | 135 | def check_header(header):
|
|
0 commit comments