dsh-commit-msg-check 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -4
- package/bin/cli.js +6 -2
- package/package.json +1 -1
- package/src/index.js +37 -8
package/README.md
CHANGED
|
@@ -8,15 +8,16 @@ Commit-message hygiene from a single message. A commit is decidable: it has a **
|
|
|
8
8
|
输入:
|
|
9
9
|
|
|
10
10
|
```markdown
|
|
11
|
-
|
|
11
|
+
修复登录页在弱网环境下偶发超时却没有任何反馈的问题,补上错误文案、重试按钮与失败提示,并新增三条回归测试,覆盖该路径、重试次数上限以及超时之后的状态回收逻辑
|
|
12
12
|
```
|
|
13
13
|
|
|
14
14
|
真实运行(逐字输出,由测试与工具保持同步):
|
|
15
15
|
|
|
16
16
|
```text
|
|
17
17
|
$ commit-msg-check commit-sample.txt
|
|
18
|
-
error
|
|
19
|
-
|
|
18
|
+
error subject-long 第1行 第 1 行主题 78 字,超过 72——git log 会折行,细节放正文
|
|
19
|
+
warn type-missing 第1行 第 1 行没有类型前缀(认得:feat/fix/docs/style/refactor/perf/test/build/ci/chore/revert)——「修复登录页在弱网环境下偶发超时却没有任何反馈的问题,补上错误」这条是团队约定,不是 git log 的硬要求——想强制就传 rules.requireType: "error"
|
|
20
|
+
fail: type=- subject=78ch, 1 errors, 1 warnings (type policy: warn)
|
|
20
21
|
```
|
|
21
22
|
|
|
22
23
|
出口码:1。
|
|
@@ -28,7 +29,7 @@ Listed in [awesome-dsh-plugin](https://github.com/awesome-dsh-plugin/awesome-dsh
|
|
|
28
29
|
|
|
29
30
|
| rule | level | meaning |
|
|
30
31
|
| --- | --- | --- |
|
|
31
|
-
| `type-missing` |
|
|
32
|
+
| `type-missing` | warn | no `type(scope): subject` prefix — a team convention, not a git requirement (see below) |
|
|
32
33
|
| `type-unknown` | warn | a type outside the known set (custom types are fine, just be deliberate) |
|
|
33
34
|
| `subject-long` | error | subject over the limit (default 72 — where `git log` wraps) |
|
|
34
35
|
| `subject-empty` | error | nothing after the colon |
|
|
@@ -43,6 +44,8 @@ Known types: `feat fix docs style refactor perf test build ci chore revert`.
|
|
|
43
44
|
```bash
|
|
44
45
|
npx commit-msg-check .git/COMMIT_EDITMSG --json
|
|
45
46
|
npx commit-msg-check msg.txt --max-subject 50
|
|
47
|
+
npx commit-msg-check msg.txt --require-type # prefix becomes an error
|
|
48
|
+
npx commit-msg-check msg.txt --no-require-type # drop the finding entirely
|
|
46
49
|
```
|
|
47
50
|
|
|
48
51
|
As a git hook, `.git/hooks/commit-msg`:
|
|
@@ -54,6 +57,16 @@ npx commit-msg-check "$1" || exit 1
|
|
|
54
57
|
|
|
55
58
|
Same function over HTTP: `POST /commit-msg-check/api` with `{"text": "…"}` — the CLI and the host route share one implementation, so they cannot disagree.
|
|
56
59
|
|
|
60
|
+
## Two kinds of rule, two levels
|
|
61
|
+
|
|
62
|
+
Not every rule here is the same kind of claim, and the levels say so.
|
|
63
|
+
|
|
64
|
+
**Convention-neutral — errors.** A subject past 72 characters wraps in `git log`. A trailing period reads as a truncated sentence. A missing blank line folds the body into the subject. A `!` without a `BREAKING CHANGE:` trailer loses what the release tooling parses. Every repository, in every language, is wrong the same way.
|
|
65
|
+
|
|
66
|
+
**Convention-carrying — a warning.** `feat:`/`fix:` is the Conventional Commits spec, not a git requirement. Plenty of good repos write `Verb noun`, and a Chinese subject has no English type word to offer. Flagging those as errors is not catching a mistake, it is imposing a house style on a stranger — and a check that blocks every commit of a repo that never adopted that style gets uninstalled on the first commit. So `type-missing` is a `warn`, and teams that do use Conventional Commits turn it into an error with `--require-type` (or `rules.requireType: 'error'`).
|
|
67
|
+
|
|
68
|
+
Measured on this project's own three repositories, 102 real commits: 85 (83%) had no type prefix, and 58 (56%) had a subject over 72 characters. Under a default error the first number made the tool unusable on a codebase that is perfectly fine; the second number was a real defect in those commits and stayed an error.
|
|
69
|
+
|
|
57
70
|
## Honest scope
|
|
58
71
|
|
|
59
72
|
This checks the message's **shape**, not the commit's content: it verifies the message is parseable by tooling and readable in a log. Whether the diff does what the subject claims is what review is for. A message can pass every rule here and still describe the wrong change — this tool has nothing to say about that, and does not pretend otherwise.
|
package/bin/cli.js
CHANGED
|
@@ -6,7 +6,7 @@ import { lintCommitMessage } from '../src/index.js'
|
|
|
6
6
|
const args = process.argv.slice(2)
|
|
7
7
|
const target = args.find(argument => !argument.startsWith('--'))
|
|
8
8
|
if (!target) {
|
|
9
|
-
process.stderr.write('usage: commit-msg-check <COMMIT_EDITMSG|msg.txt> [--json] [--max-subject 72] [--
|
|
9
|
+
process.stderr.write('usage: commit-msg-check <COMMIT_EDITMSG|msg.txt> [--json] [--max-subject 72] [--require-type] [--no-require-type]\n')
|
|
10
10
|
process.exit(2)
|
|
11
11
|
}
|
|
12
12
|
const option = name => {
|
|
@@ -16,13 +16,17 @@ const option = name => {
|
|
|
16
16
|
const rules = {}
|
|
17
17
|
const maxSubject = option('--max-subject')
|
|
18
18
|
if (maxSubject !== undefined) rules.maxSubject = maxSubject
|
|
19
|
+
// Type prefix is a team convention, not a git requirement: warn by default.
|
|
20
|
+
// --require-type makes it an error; --no-require-type drops the finding.
|
|
21
|
+
if (args.includes('--require-type')) rules.requireType = 'error'
|
|
22
|
+
if (args.includes('--no-require-type')) rules.requireType = 'off'
|
|
19
23
|
try {
|
|
20
24
|
const result = await lintCommitMessage(await readFile(target, 'utf8'), rules)
|
|
21
25
|
if (args.includes('--json')) {
|
|
22
26
|
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`)
|
|
23
27
|
} else {
|
|
24
28
|
for (const finding of result.findings) process.stdout.write(`${finding.level} ${finding.rule} 第${finding.line}行 ${finding.message}\n`)
|
|
25
|
-
process.stdout.write(`${result.ok ? 'ok' : 'fail'}: type=${result.checked.type ?? '-'} subject=${result.checked.subject ?? '-'}ch, ${result.errors} errors, ${result.warnings} warnings\n`)
|
|
29
|
+
process.stdout.write(`${result.ok ? 'ok' : 'fail'}: type=${result.checked.type ?? '-'} subject=${result.checked.subject ?? '-'}ch, ${result.errors} errors, ${result.warnings} warnings (type policy: ${result.checked.typePolicy})\n`)
|
|
26
30
|
}
|
|
27
31
|
process.exit(result.ok ? 0 : 1)
|
|
28
32
|
} catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-commit-msg-check",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Commit-message hygiene from a single message: a type prefix from a known set, a subject that fits a git log line, no trailing period, a blank line before the body, and breaking changes announced in a trailer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
package/src/index.js
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
-
// A commit message is decidable from its text: a
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// A commit message is decidable from its text: a subject that fits in a git log
|
|
2
|
+
// line, no trailing period, a blank line before the body, breaking changes
|
|
3
|
+
// announced in the body rather than buried in the subject, and — optionally —
|
|
4
|
+
// a type prefix from a known set. This verifies exactly those.
|
|
5
|
+
//
|
|
6
|
+
// Two kinds of rule live here, and they are not the same kind of thing:
|
|
7
|
+
//
|
|
8
|
+
// convention-neutral — the subject wraps in git log, the period reads as a
|
|
9
|
+
// truncated sentence, a missing blank line folds the body into the subject,
|
|
10
|
+
// a `!` alone loses the trailer downstream tools parse. Every repo, in every
|
|
11
|
+
// language, is wrong in the same way.
|
|
12
|
+
//
|
|
13
|
+
// convention-carrying — `feat:`/`fix:` is the Conventional Commits spec.
|
|
14
|
+
// Plenty of good repos use `Verb noun`, and a Chinese subject has no English
|
|
15
|
+
// type word to offer. Erroring on those is not catching a mistake; it is
|
|
16
|
+
// imposing a house style on a stranger, and the check gets uninstalled on
|
|
17
|
+
// the first commit. So this one warns by default and errors only when the
|
|
18
|
+
// repo asks for it via `requireType: 'error'`.
|
|
5
19
|
|
|
6
20
|
export const name = 'commit-msg-check'
|
|
7
21
|
export const inject = ['webServer']
|
|
@@ -28,9 +42,19 @@ export function parseCommit(text) {
|
|
|
28
42
|
}
|
|
29
43
|
}
|
|
30
44
|
|
|
45
|
+
// `requireType` is 'warn' (default), 'error', or 'off'. Booleans from 0.1.0
|
|
46
|
+
// still work: true meant error, false meant off. Anything unrecognised falls
|
|
47
|
+
// back to the default rather than silently disabling a check.
|
|
48
|
+
export function typePolicy(rules = {}) {
|
|
49
|
+
const requested = rules.requireType ?? 'warn'
|
|
50
|
+
if (requested === true) return 'error'
|
|
51
|
+
if (requested === false) return 'off'
|
|
52
|
+
return ['warn', 'error', 'off'].includes(requested) ? requested : 'warn'
|
|
53
|
+
}
|
|
54
|
+
|
|
31
55
|
export function lintCommitMessage(text, rules = {}) {
|
|
32
56
|
const maxSubject = rules.maxSubject ?? 72
|
|
33
|
-
const
|
|
57
|
+
const policy = typePolicy(rules)
|
|
34
58
|
const types = rules.types ?? TYPES
|
|
35
59
|
const findings = []
|
|
36
60
|
const add = (level, rule, line, message) => findings.push({ level, rule, line, message })
|
|
@@ -41,9 +65,14 @@ export function lintCommitMessage(text, rules = {}) {
|
|
|
41
65
|
return { ok: false, checked: { lines: lines.length }, errors: 1, warnings: 0, findings: [{ level: 'error', rule: 'empty', line: 0, message: '提交信息为空' }] }
|
|
42
66
|
}
|
|
43
67
|
|
|
44
|
-
// Type prefix.
|
|
68
|
+
// Type prefix. A warning by default, on purpose — see the note at the top.
|
|
45
69
|
if (commit.type === null) {
|
|
46
|
-
if (
|
|
70
|
+
if (policy !== 'off') {
|
|
71
|
+
const why = policy === 'error'
|
|
72
|
+
? '看不出这次改动的性质'
|
|
73
|
+
: '这条是团队约定,不是 git log 的硬要求——想强制就传 rules.requireType: "error"'
|
|
74
|
+
add(policy, 'type-missing', 1, `第 1 行没有类型前缀(认得:${types.join('/')})——「${commit.header.slice(0, 30)}」${why}`)
|
|
75
|
+
}
|
|
47
76
|
} else if (!types.includes(commit.type)) {
|
|
48
77
|
add('warn', 'type-unknown', 1, `第 1 行类型「${commit.type}」不在已知集合(${types.join('/')})——自定义类型可以,但要确认是有意的`)
|
|
49
78
|
}
|
|
@@ -74,7 +103,7 @@ export function lintCommitMessage(text, rules = {}) {
|
|
|
74
103
|
const order = { error: 0, warn: 1 }
|
|
75
104
|
findings.sort((a, b) => order[a.level] - order[b.level] || a.line - b.line)
|
|
76
105
|
const errors = findings.filter(finding => finding.level === 'error').length
|
|
77
|
-
return { ok: errors === 0, checked: { type: commit.type, scope: commit.scope, subject: length, lines: lines.length }, errors, warnings: findings.length - errors, findings }
|
|
106
|
+
return { ok: errors === 0, checked: { type: commit.type, scope: commit.scope, subject: length, lines: lines.length, typePolicy: policy }, errors, warnings: findings.length - errors, findings }
|
|
78
107
|
}
|
|
79
108
|
|
|
80
109
|
export function apply(ctx, config = {}) {
|