dsh-commit-msg-check 0.1.0 → 0.3.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 CHANGED
@@ -2,21 +2,28 @@
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
4
 
5
+ ## 前置要求
6
+
7
+ Node.js ≥ 22.19,就是 `package.json` 里 `engines.node` 写的那一条;CLI 只依赖 Node,别的不装也能跑。想在对话里用同一个检查,需要 DSH `>=0.2.2-rc.1 <0.3.0-0`。装之前 `node -v` 看一眼,比装完再猜为什么报错快。
8
+
9
+ 跑通大概 2 分钟。卡住了在仓库开 issue,别自己啃。
10
+
5
11
  <!-- example:start -->
6
12
  ## 真实输入输出
7
13
 
8
14
  输入:
9
15
 
10
16
  ```markdown
11
- 修了几个 bug
17
+ 修复登录页在弱网环境下偶发超时却没有任何反馈的问题,补上错误文案、重试按钮与失败提示,并新增三条回归测试,覆盖该路径、重试次数上限以及超时之后的状态回收逻辑
12
18
  ```
13
19
 
14
20
  真实运行(逐字输出,由测试与工具保持同步):
15
21
 
16
22
  ```text
17
23
  $ 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
24
+ error subject-long 第1行 第 1 行主题 78 字,超过 72——git log 会折行,细节放正文
25
+ warn type-missing 第1行 第 1 行没有类型前缀(认得:feat/fix/docs/style/refactor/perf/test/build/ci/chore/revert)——「修复登录页在弱网环境下偶发超时却没有任何反馈的问题,补上错误」这条是团队约定,不是 git log 的硬要求——想强制就传 rules.requireType: "error"
26
+ fail: type=- subject=78ch, 1 errors, 1 warnings (type policy: warn)
20
27
  ```
21
28
 
22
29
  出口码:1。
@@ -28,7 +35,7 @@ Listed in [awesome-dsh-plugin](https://github.com/awesome-dsh-plugin/awesome-dsh
28
35
 
29
36
  | rule | level | meaning |
30
37
  | --- | --- | --- |
31
- | `type-missing` | error | no `type(scope): subject` prefix — a bare sentence does not say what kind of change it is |
38
+ | `type-missing` | warn | no `type(scope): subject` prefix — a team convention, not a git requirement (see below) |
32
39
  | `type-unknown` | warn | a type outside the known set (custom types are fine, just be deliberate) |
33
40
  | `subject-long` | error | subject over the limit (default 72 — where `git log` wraps) |
34
41
  | `subject-empty` | error | nothing after the colon |
@@ -43,6 +50,8 @@ Known types: `feat fix docs style refactor perf test build ci chore revert`.
43
50
  ```bash
44
51
  npx commit-msg-check .git/COMMIT_EDITMSG --json
45
52
  npx commit-msg-check msg.txt --max-subject 50
53
+ npx commit-msg-check msg.txt --require-type # prefix becomes an error
54
+ npx commit-msg-check msg.txt --no-require-type # drop the finding entirely
46
55
  ```
47
56
 
48
57
  As a git hook, `.git/hooks/commit-msg`:
@@ -54,6 +63,16 @@ npx commit-msg-check "$1" || exit 1
54
63
 
55
64
  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
65
 
66
+ ## Two kinds of rule, two levels
67
+
68
+ Not every rule here is the same kind of claim, and the levels say so.
69
+
70
+ **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.
71
+
72
+ **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'`).
73
+
74
+ 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.
75
+
57
76
  ## Honest scope
58
77
 
59
78
  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] [--warnings-fail]\n')
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.1.0",
3
+ "version": "0.3.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 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.
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']
@@ -10,7 +24,12 @@ export const TYPES = ['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test'
10
24
 
11
25
  // `type(scope): subject` or `type: subject`. The colon is required; a bare
12
26
  // sentence with no prefix is the most common violation and the cheapest to fix.
13
- const HEADER = /^(\w+)(?:\(([^)]+)\))?(!)?:\s*(.+)$/
27
+ //
28
+ // The subject is `.*`, not `.+`, on purpose: with `.+` a header of `feat:` fails
29
+ // to parse, so `type` reads as null and the `subject-empty` rule below can never
30
+ // fire. It was dead code until the rules audit looked for a test that makes each
31
+ // rule fire and found none for this one.
32
+ const HEADER = /^(\w+)(?:\(([^)]+)\))?(!)?:\s*(.*)$/
14
33
 
15
34
  export function parseCommit(text) {
16
35
  const lines = String(text).split('\n')
@@ -28,9 +47,19 @@ export function parseCommit(text) {
28
47
  }
29
48
  }
30
49
 
50
+ // `requireType` is 'warn' (default), 'error', or 'off'. Booleans from 0.1.0
51
+ // still work: true meant error, false meant off. Anything unrecognised falls
52
+ // back to the default rather than silently disabling a check.
53
+ export function typePolicy(rules = {}) {
54
+ const requested = rules.requireType ?? 'warn'
55
+ if (requested === true) return 'error'
56
+ if (requested === false) return 'off'
57
+ return ['warn', 'error', 'off'].includes(requested) ? requested : 'warn'
58
+ }
59
+
31
60
  export function lintCommitMessage(text, rules = {}) {
32
61
  const maxSubject = rules.maxSubject ?? 72
33
- const requireType = rules.requireType ?? true
62
+ const policy = typePolicy(rules)
34
63
  const types = rules.types ?? TYPES
35
64
  const findings = []
36
65
  const add = (level, rule, line, message) => findings.push({ level, rule, line, message })
@@ -41,9 +70,14 @@ export function lintCommitMessage(text, rules = {}) {
41
70
  return { ok: false, checked: { lines: lines.length }, errors: 1, warnings: 0, findings: [{ level: 'error', rule: 'empty', line: 0, message: '提交信息为空' }] }
42
71
  }
43
72
 
44
- // Type prefix.
73
+ // Type prefix. A warning by default, on purpose — see the note at the top.
45
74
  if (commit.type === null) {
46
- if (requireType) add('error', 'type-missing', 1, `第 1 行没有类型前缀(认得:${types.join('/')})——「${commit.header.slice(0, 30)}」看不出这次改动的性质`)
75
+ if (policy !== 'off') {
76
+ const why = policy === 'error'
77
+ ? '看不出这次改动的性质'
78
+ : '这条是团队约定,不是 git log 的硬要求——想强制就传 rules.requireType: "error"'
79
+ add(policy, 'type-missing', 1, `第 1 行没有类型前缀(认得:${types.join('/')})——「${commit.header.slice(0, 30)}」${why}`)
80
+ }
47
81
  } else if (!types.includes(commit.type)) {
48
82
  add('warn', 'type-unknown', 1, `第 1 行类型「${commit.type}」不在已知集合(${types.join('/')})——自定义类型可以,但要确认是有意的`)
49
83
  }
@@ -74,7 +108,7 @@ export function lintCommitMessage(text, rules = {}) {
74
108
  const order = { error: 0, warn: 1 }
75
109
  findings.sort((a, b) => order[a.level] - order[b.level] || a.line - b.line)
76
110
  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 }
111
+ 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
112
  }
79
113
 
80
114
  export function apply(ctx, config = {}) {