dsh-commit-msg-check 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +71 -2
- package/bin/cli.js +35 -0
- package/cordis.patch.yml +4 -0
- package/package.json +45 -4
- package/src/index.js +130 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zjukop3
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-commit-msg-check
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Commit-message hygiene from a single message. A commit is decidable: it has a **type prefix** from a known set, a **subject that fits a git log line**, a **blank line before the body**, and **breaking changes announced in a trailer** rather than buried in the subject. This verifies exactly those — one file, no git history needed.
|
|
4
|
+
|
|
5
|
+
<!-- example:start -->
|
|
6
|
+
## 真实输入输出
|
|
7
|
+
|
|
8
|
+
输入:
|
|
9
|
+
|
|
10
|
+
```markdown
|
|
11
|
+
修复登录页在弱网环境下偶发超时却没有任何反馈的问题,补上错误文案、重试按钮与失败提示,并新增三条回归测试,覆盖该路径、重试次数上限以及超时之后的状态回收逻辑
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
真实运行(逐字输出,由测试与工具保持同步):
|
|
15
|
+
|
|
16
|
+
```text
|
|
17
|
+
$ commit-msg-check commit-sample.txt
|
|
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)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
出口码:1。
|
|
24
|
+
<!-- example:start -->
|
|
25
|
+
|
|
26
|
+
Listed in [awesome-dsh-plugin](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin).
|
|
27
|
+
|
|
28
|
+
## What it decides
|
|
29
|
+
|
|
30
|
+
| rule | level | meaning |
|
|
31
|
+
| --- | --- | --- |
|
|
32
|
+
| `type-missing` | warn | no `type(scope): subject` prefix — a team convention, not a git requirement (see below) |
|
|
33
|
+
| `type-unknown` | warn | a type outside the known set (custom types are fine, just be deliberate) |
|
|
34
|
+
| `subject-long` | error | subject over the limit (default 72 — where `git log` wraps) |
|
|
35
|
+
| `subject-empty` | error | nothing after the colon |
|
|
36
|
+
| `subject-period` | warn | subject ends with a period — it is a title, not a sentence |
|
|
37
|
+
| `body-separator` | error | no blank line after the header — git would fold the body into the subject |
|
|
38
|
+
| `breaking-untrailed` | warn | `!` in the header but no `BREAKING CHANGE:` trailer in the body — tooling parses the trailer |
|
|
39
|
+
|
|
40
|
+
Known types: `feat fix docs style refactor perf test build ci chore revert`.
|
|
41
|
+
|
|
42
|
+
## Use
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npx commit-msg-check .git/COMMIT_EDITMSG --json
|
|
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
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
As a git hook, `.git/hooks/commit-msg`:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
#!/bin/sh
|
|
55
|
+
npx commit-msg-check "$1" || exit 1
|
|
56
|
+
```
|
|
57
|
+
|
|
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.
|
|
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
|
+
|
|
70
|
+
## Honest scope
|
|
71
|
+
|
|
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
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Exit codes: 0 clean, 1 findings, 2 usage or read error.
|
|
3
|
+
import { readFile } from 'node:fs/promises'
|
|
4
|
+
import { lintCommitMessage } from '../src/index.js'
|
|
5
|
+
|
|
6
|
+
const args = process.argv.slice(2)
|
|
7
|
+
const target = args.find(argument => !argument.startsWith('--'))
|
|
8
|
+
if (!target) {
|
|
9
|
+
process.stderr.write('usage: commit-msg-check <COMMIT_EDITMSG|msg.txt> [--json] [--max-subject 72] [--require-type] [--no-require-type]\n')
|
|
10
|
+
process.exit(2)
|
|
11
|
+
}
|
|
12
|
+
const option = name => {
|
|
13
|
+
const at = args.indexOf(name)
|
|
14
|
+
return at === -1 ? undefined : Number(args[at + 1])
|
|
15
|
+
}
|
|
16
|
+
const rules = {}
|
|
17
|
+
const maxSubject = option('--max-subject')
|
|
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'
|
|
23
|
+
try {
|
|
24
|
+
const result = await lintCommitMessage(await readFile(target, 'utf8'), rules)
|
|
25
|
+
if (args.includes('--json')) {
|
|
26
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`)
|
|
27
|
+
} else {
|
|
28
|
+
for (const finding of result.findings) process.stdout.write(`${finding.level} ${finding.rule} 第${finding.line}行 ${finding.message}\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`)
|
|
30
|
+
}
|
|
31
|
+
process.exit(result.ok ? 0 : 1)
|
|
32
|
+
} catch (error) {
|
|
33
|
+
process.stderr.write(`${error.message}\n`)
|
|
34
|
+
process.exit(2)
|
|
35
|
+
}
|
package/cordis.patch.yml
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-commit-msg-check",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
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
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js",
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
11
|
+
"bin": {
|
|
12
|
+
"commit-msg-check": "./bin/cli.js"
|
|
13
|
+
},
|
|
14
|
+
"dsh": {
|
|
15
|
+
"bundle": {
|
|
16
|
+
"patch": "./cordis.patch.yml"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22.19.0",
|
|
21
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0-0"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"bin",
|
|
26
|
+
"cordis.patch.yml",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"keywords": [
|
|
31
|
+
"dsh",
|
|
32
|
+
"dsh-plugin",
|
|
33
|
+
"deepseek-harness",
|
|
34
|
+
"commit-message",
|
|
35
|
+
"conventional-commits",
|
|
36
|
+
"git",
|
|
37
|
+
"提交信息",
|
|
38
|
+
"约定式提交"
|
|
39
|
+
],
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"author": "zjukop3",
|
|
42
|
+
"repository": {
|
|
43
|
+
"type": "git",
|
|
44
|
+
"url": "git+https://github.com/zjukop3/dsh-vertical-kit.git"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/zjukop3/dsh-vertical-kit/tree/main/packages/dsh-commit-msg-check"
|
|
47
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
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'`.
|
|
19
|
+
|
|
20
|
+
export const name = 'commit-msg-check'
|
|
21
|
+
export const inject = ['webServer']
|
|
22
|
+
|
|
23
|
+
export const TYPES = ['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'chore', 'revert']
|
|
24
|
+
|
|
25
|
+
// `type(scope): subject` or `type: subject`. The colon is required; a bare
|
|
26
|
+
// sentence with no prefix is the most common violation and the cheapest to fix.
|
|
27
|
+
const HEADER = /^(\w+)(?:\(([^)]+)\))?(!)?:\s*(.+)$/
|
|
28
|
+
|
|
29
|
+
export function parseCommit(text) {
|
|
30
|
+
const lines = String(text).split('\n')
|
|
31
|
+
const header = lines[0] || ''
|
|
32
|
+
const match = header.match(HEADER)
|
|
33
|
+
const body = lines.slice(1).join('\n')
|
|
34
|
+
return {
|
|
35
|
+
header,
|
|
36
|
+
type: match ? match[1] : null,
|
|
37
|
+
scope: match && match[2] ? match[2] : null,
|
|
38
|
+
bang: match ? match[3] === '!' : false,
|
|
39
|
+
subject: match ? match[4] : header,
|
|
40
|
+
body,
|
|
41
|
+
bodyStartLine: lines.length > 1 ? 2 : 0,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
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
|
+
|
|
55
|
+
export function lintCommitMessage(text, rules = {}) {
|
|
56
|
+
const maxSubject = rules.maxSubject ?? 72
|
|
57
|
+
const policy = typePolicy(rules)
|
|
58
|
+
const types = rules.types ?? TYPES
|
|
59
|
+
const findings = []
|
|
60
|
+
const add = (level, rule, line, message) => findings.push({ level, rule, line, message })
|
|
61
|
+
const lines = String(text).split('\n')
|
|
62
|
+
const commit = parseCommit(text)
|
|
63
|
+
|
|
64
|
+
if (!commit.header.trim()) {
|
|
65
|
+
return { ok: false, checked: { lines: lines.length }, errors: 1, warnings: 0, findings: [{ level: 'error', rule: 'empty', line: 0, message: '提交信息为空' }] }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Type prefix. A warning by default, on purpose — see the note at the top.
|
|
69
|
+
if (commit.type === null) {
|
|
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
|
+
}
|
|
76
|
+
} else if (!types.includes(commit.type)) {
|
|
77
|
+
add('warn', 'type-unknown', 1, `第 1 行类型「${commit.type}」不在已知集合(${types.join('/')})——自定义类型可以,但要确认是有意的`)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Subject length: 72 is where git log wraps in a standard terminal.
|
|
81
|
+
const length = [...commit.subject].length
|
|
82
|
+
if (length > maxSubject) add('error', 'subject-long', 1, `第 1 行主题 ${length} 字,超过 ${maxSubject}——git log 会折行,细节放正文`)
|
|
83
|
+
if (length === 0) add('error', 'subject-empty', 1, '第 1 行冒号后没有主题')
|
|
84
|
+
|
|
85
|
+
// Trailing period: a commit subject is a title, not a sentence.
|
|
86
|
+
if (/[.。]\s*$/.test(commit.subject)) add('warn', 'subject-period', 1, '主题以句号结尾——提交主题是标题,不是句子')
|
|
87
|
+
|
|
88
|
+
// Blank line between header and body: without it git treats everything as
|
|
89
|
+
// one long subject line.
|
|
90
|
+
if (lines.length > 1 && lines[1].trim() !== '') {
|
|
91
|
+
add('error', 'body-separator', 2, '第 2 行应为空行——没有空行时 git 会把正文并入主题')
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Breaking changes: either `!` in the header or a BREAKING CHANGE: trailer.
|
|
95
|
+
const hasBang = commit.bang
|
|
96
|
+
const hasTrailer = /^\s*BREAKING[ _-]?CHANGE:/im.test(commit.body)
|
|
97
|
+
if (hasBang && !hasTrailer) {
|
|
98
|
+
add('warn', 'breaking-untrailed', 1, '第 1 行用了 ! 标记破坏性变更,但正文没有 BREAKING CHANGE: 段——下游工具按 trailer 解析')
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// A body that is only one line and has no blank line is a long subject in
|
|
102
|
+
// disguise; warn only when the header already fit.
|
|
103
|
+
const order = { error: 0, warn: 1 }
|
|
104
|
+
findings.sort((a, b) => order[a.level] - order[b.level] || a.line - b.line)
|
|
105
|
+
const errors = findings.filter(finding => finding.level === 'error').length
|
|
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 }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function apply(ctx, config = {}) {
|
|
110
|
+
const prefix = config.path || '/commit-msg-check/api'
|
|
111
|
+
const handler = async (req, res) => {
|
|
112
|
+
const send = (status, body) => {
|
|
113
|
+
res.statusCode = status
|
|
114
|
+
res.setHeader('content-type', 'application/json; charset=utf-8')
|
|
115
|
+
res.end(JSON.stringify(body))
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
if (req.method !== 'POST') return send(405, { error: 'POST only' })
|
|
119
|
+
const chunks = []
|
|
120
|
+
for await (const chunk of req) chunks.push(chunk)
|
|
121
|
+
const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}')
|
|
122
|
+
const text = typeof body.text === 'string' && body.text ? body.text : body.path ? (await import('node:fs/promises')).readFile(body.path, 'utf8') : ''
|
|
123
|
+
if (!text) return send(400, { error: 'text or path is required' })
|
|
124
|
+
return send(200, lintCommitMessage(text, body.rules))
|
|
125
|
+
} catch (error) {
|
|
126
|
+
return send(400, { error: error.message || 'request failed' })
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
ctx.effect(() => ctx.webServer.register({ kind: 'prefix', path: prefix, handler }))
|
|
130
|
+
}
|