dsh-commit-msg-check 0.0.0-stage → 0.1.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 +58 -2
- package/bin/cli.js +31 -0
- package/cordis.patch.yml +4 -0
- package/package.json +45 -4
- package/src/index.js +101 -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,59 @@
|
|
|
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
|
+
修了几个 bug
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
真实运行(逐字输出,由测试与工具保持同步):
|
|
15
|
+
|
|
16
|
+
```text
|
|
17
|
+
$ commit-msg-check commit-sample.txt
|
|
18
|
+
error type-missing 第1行 第 1 行没有类型前缀(认得:feat/fix/docs/style/refactor/perf/test/build/ci/chore/revert)——「修了几个 bug」看不出这次改动的性质
|
|
19
|
+
fail: type=- subject=8ch, 1 errors, 0 warnings
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
出口码:1。
|
|
23
|
+
<!-- example:start -->
|
|
24
|
+
|
|
25
|
+
Listed in [awesome-dsh-plugin](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin).
|
|
26
|
+
|
|
27
|
+
## What it decides
|
|
28
|
+
|
|
29
|
+
| rule | level | meaning |
|
|
30
|
+
| --- | --- | --- |
|
|
31
|
+
| `type-missing` | error | no `type(scope): subject` prefix — a bare sentence does not say what kind of change it is |
|
|
32
|
+
| `type-unknown` | warn | a type outside the known set (custom types are fine, just be deliberate) |
|
|
33
|
+
| `subject-long` | error | subject over the limit (default 72 — where `git log` wraps) |
|
|
34
|
+
| `subject-empty` | error | nothing after the colon |
|
|
35
|
+
| `subject-period` | warn | subject ends with a period — it is a title, not a sentence |
|
|
36
|
+
| `body-separator` | error | no blank line after the header — git would fold the body into the subject |
|
|
37
|
+
| `breaking-untrailed` | warn | `!` in the header but no `BREAKING CHANGE:` trailer in the body — tooling parses the trailer |
|
|
38
|
+
|
|
39
|
+
Known types: `feat fix docs style refactor perf test build ci chore revert`.
|
|
40
|
+
|
|
41
|
+
## Use
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npx commit-msg-check .git/COMMIT_EDITMSG --json
|
|
45
|
+
npx commit-msg-check msg.txt --max-subject 50
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
As a git hook, `.git/hooks/commit-msg`:
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
#!/bin/sh
|
|
52
|
+
npx commit-msg-check "$1" || exit 1
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
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
|
+
|
|
57
|
+
## Honest scope
|
|
58
|
+
|
|
59
|
+
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,31 @@
|
|
|
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] [--warnings-fail]\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
|
+
try {
|
|
20
|
+
const result = await lintCommitMessage(await readFile(target, 'utf8'), rules)
|
|
21
|
+
if (args.includes('--json')) {
|
|
22
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`)
|
|
23
|
+
} else {
|
|
24
|
+
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`)
|
|
26
|
+
}
|
|
27
|
+
process.exit(result.ok ? 0 : 1)
|
|
28
|
+
} catch (error) {
|
|
29
|
+
process.stderr.write(`${error.message}\n`)
|
|
30
|
+
process.exit(2)
|
|
31
|
+
}
|
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.1.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,101 @@
|
|
|
1
|
+
// A commit message is decidable from its text: a type prefix from a known set,
|
|
2
|
+
// a subject that fits in a git log line, no trailing period, a blank line
|
|
3
|
+
// before the body, and breaking changes announced in the body rather than
|
|
4
|
+
// buried in the subject. This verifies exactly those.
|
|
5
|
+
|
|
6
|
+
export const name = 'commit-msg-check'
|
|
7
|
+
export const inject = ['webServer']
|
|
8
|
+
|
|
9
|
+
export const TYPES = ['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'chore', 'revert']
|
|
10
|
+
|
|
11
|
+
// `type(scope): subject` or `type: subject`. The colon is required; a bare
|
|
12
|
+
// sentence with no prefix is the most common violation and the cheapest to fix.
|
|
13
|
+
const HEADER = /^(\w+)(?:\(([^)]+)\))?(!)?:\s*(.+)$/
|
|
14
|
+
|
|
15
|
+
export function parseCommit(text) {
|
|
16
|
+
const lines = String(text).split('\n')
|
|
17
|
+
const header = lines[0] || ''
|
|
18
|
+
const match = header.match(HEADER)
|
|
19
|
+
const body = lines.slice(1).join('\n')
|
|
20
|
+
return {
|
|
21
|
+
header,
|
|
22
|
+
type: match ? match[1] : null,
|
|
23
|
+
scope: match && match[2] ? match[2] : null,
|
|
24
|
+
bang: match ? match[3] === '!' : false,
|
|
25
|
+
subject: match ? match[4] : header,
|
|
26
|
+
body,
|
|
27
|
+
bodyStartLine: lines.length > 1 ? 2 : 0,
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function lintCommitMessage(text, rules = {}) {
|
|
32
|
+
const maxSubject = rules.maxSubject ?? 72
|
|
33
|
+
const requireType = rules.requireType ?? true
|
|
34
|
+
const types = rules.types ?? TYPES
|
|
35
|
+
const findings = []
|
|
36
|
+
const add = (level, rule, line, message) => findings.push({ level, rule, line, message })
|
|
37
|
+
const lines = String(text).split('\n')
|
|
38
|
+
const commit = parseCommit(text)
|
|
39
|
+
|
|
40
|
+
if (!commit.header.trim()) {
|
|
41
|
+
return { ok: false, checked: { lines: lines.length }, errors: 1, warnings: 0, findings: [{ level: 'error', rule: 'empty', line: 0, message: '提交信息为空' }] }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Type prefix.
|
|
45
|
+
if (commit.type === null) {
|
|
46
|
+
if (requireType) add('error', 'type-missing', 1, `第 1 行没有类型前缀(认得:${types.join('/')})——「${commit.header.slice(0, 30)}」看不出这次改动的性质`)
|
|
47
|
+
} else if (!types.includes(commit.type)) {
|
|
48
|
+
add('warn', 'type-unknown', 1, `第 1 行类型「${commit.type}」不在已知集合(${types.join('/')})——自定义类型可以,但要确认是有意的`)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Subject length: 72 is where git log wraps in a standard terminal.
|
|
52
|
+
const length = [...commit.subject].length
|
|
53
|
+
if (length > maxSubject) add('error', 'subject-long', 1, `第 1 行主题 ${length} 字,超过 ${maxSubject}——git log 会折行,细节放正文`)
|
|
54
|
+
if (length === 0) add('error', 'subject-empty', 1, '第 1 行冒号后没有主题')
|
|
55
|
+
|
|
56
|
+
// Trailing period: a commit subject is a title, not a sentence.
|
|
57
|
+
if (/[.。]\s*$/.test(commit.subject)) add('warn', 'subject-period', 1, '主题以句号结尾——提交主题是标题,不是句子')
|
|
58
|
+
|
|
59
|
+
// Blank line between header and body: without it git treats everything as
|
|
60
|
+
// one long subject line.
|
|
61
|
+
if (lines.length > 1 && lines[1].trim() !== '') {
|
|
62
|
+
add('error', 'body-separator', 2, '第 2 行应为空行——没有空行时 git 会把正文并入主题')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Breaking changes: either `!` in the header or a BREAKING CHANGE: trailer.
|
|
66
|
+
const hasBang = commit.bang
|
|
67
|
+
const hasTrailer = /^\s*BREAKING[ _-]?CHANGE:/im.test(commit.body)
|
|
68
|
+
if (hasBang && !hasTrailer) {
|
|
69
|
+
add('warn', 'breaking-untrailed', 1, '第 1 行用了 ! 标记破坏性变更,但正文没有 BREAKING CHANGE: 段——下游工具按 trailer 解析')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A body that is only one line and has no blank line is a long subject in
|
|
73
|
+
// disguise; warn only when the header already fit.
|
|
74
|
+
const order = { error: 0, warn: 1 }
|
|
75
|
+
findings.sort((a, b) => order[a.level] - order[b.level] || a.line - b.line)
|
|
76
|
+
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 }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function apply(ctx, config = {}) {
|
|
81
|
+
const prefix = config.path || '/commit-msg-check/api'
|
|
82
|
+
const handler = async (req, res) => {
|
|
83
|
+
const send = (status, body) => {
|
|
84
|
+
res.statusCode = status
|
|
85
|
+
res.setHeader('content-type', 'application/json; charset=utf-8')
|
|
86
|
+
res.end(JSON.stringify(body))
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
if (req.method !== 'POST') return send(405, { error: 'POST only' })
|
|
90
|
+
const chunks = []
|
|
91
|
+
for await (const chunk of req) chunks.push(chunk)
|
|
92
|
+
const body = JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}')
|
|
93
|
+
const text = typeof body.text === 'string' && body.text ? body.text : body.path ? (await import('node:fs/promises')).readFile(body.path, 'utf8') : ''
|
|
94
|
+
if (!text) return send(400, { error: 'text or path is required' })
|
|
95
|
+
return send(200, lintCommitMessage(text, body.rules))
|
|
96
|
+
} catch (error) {
|
|
97
|
+
return send(400, { error: error.message || 'request failed' })
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
ctx.effect(() => ctx.webServer.register({ kind: 'prefix', path: prefix, handler }))
|
|
101
|
+
}
|