📝 docs: 规定提交信息只有标题不要正文与脚注

This commit is contained in:
pyh
2026-09-30 16:58:42 +08:00
parent 07f5af6fff
commit 229ff342d9
3 changed files with 47 additions and 48 deletions
+26 -12
View File
@@ -1,6 +1,6 @@
// Checks real commit subjects (newest first) against the CONTRIBUTING.md spec:
// the documented regex must parse them, the leading emoji must be present and
// match the type table exactly, and the subject must stay within 72 width.
// Checks existing commits against the CONTRIBUTING.md spec. The spec allows one
// line only, so a subject must both parse (emoji + type + Chinese description)
// and be the whole message: any body or footer is reported too.
//
// This is a reporting tool, not a hook: it never blocks a commit.
//
@@ -19,7 +19,7 @@ const specPath = process.env.DSD_SPEC ?? resolve(repoRoot, 'CONTRIBUTING.md')
const doc = readFileSync(specPath, 'utf8')
const count = process.argv[2] ?? '10'
/** The regex the spec documents for stripping a leading emoji. */
/** The regex the spec documents for a subject line. */
const SUBJECT = new RegExp(
'^(?:\\p{Extended_Pictographic}\\uFE0F?\\s+)?' +
'(?<type>[a-z]+)' +
@@ -43,17 +43,29 @@ const base = (text) => text.replace(/\uFE0F/g, '')
/** Display width, counting anything above U+2000 as double. */
const widthOf = (text) => [...text].reduce((n, c) => n + (c.codePointAt(0) > 0x2000 ? 2 : 1), 0)
const args = count === 'all' ? ['log', '--format=%h%x09%s'] : ['log', `-${Number(count)}`, '--format=%h%x09%s']
const rows = execFileSync('git', args, { encoding: 'utf8', cwd: repoRoot })
.trim().split('\n').filter((line) => line !== '')
// %B is the raw message body, so a record can hold several lines: a record ends
// at the next hash that starts a line, which is what this splits on.
const range = count === 'all' ? [] : [`-${Number(count)}`]
const raw = execFileSync('git', ['log', ...range, '--format=%h%x09%B%x00'], {
encoding: 'utf8',
cwd: repoRoot,
})
const commits = raw.split('\0').map((chunk) => chunk.replace(/^\n+/, '')).filter((chunk) => chunk.trim() !== '')
let problems = 0
for (const row of rows) {
const [hash, subject] = row.split('\t')
for (const chunk of commits) {
const tab = chunk.indexOf('\t')
const hash = chunk.slice(0, tab)
const message = chunk.slice(tab + 1).replace(/\n+$/, '')
const lines = message.split('\n')
const subject = lines[0]
const extra = lines.slice(1).filter((line) => line.trim() !== '')
const notes = []
const match = SUBJECT.exec(subject)
if (match === null) {
notes.push('缺少类型前缀,或整体格式不是 <emoji> <type>[(scope)][!]: <描述>')
notes.push('首行缺少类型前缀,或整体格式不是 <emoji> <type>[(scope)][!]: <描述>')
} else {
const expected = emojiOfType.get(match.groups.type)
const lead = [...subject][0]
@@ -63,12 +75,14 @@ for (const row of rows) {
if (width > 72) notes.push(`首行宽度 ${width},超过 72`)
if (/[。.]$/.test(subject)) notes.push('首行结尾不应有句号')
}
if (extra.length > 0) notes.push(`带了 ${extra.length} 行正文或脚注,规范要求只有一行`)
if (notes.length > 0) problems += 1
const mark = notes.length === 0 ? 'ok ' : 'FAIL'
console.log(` ${mark} ${hash} ${notes.length === 0 ? '' : notes.join(';') + ' — '}${subject}`)
}
console.log(problems === 0
? `\n检查了 ${rows.length} 条提交,全部符合规范`
: `\n检查了 ${rows.length} 条提交,其中 ${problems} 条不符合规范(规范文件:${specPath})`)
? `\n检查了 ${commits.length} 条提交,全部符合规范`
: `\n检查了 ${commits.length} 条提交,其中 ${problems} 条不符合规范(规范文件:${specPath})`)
process.exit(problems === 0 ? 0 : 1)