-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add git hook to check commit messages (#2223)
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"maxLength": 100, | ||
"types": [ | ||
"build", | ||
"ci", | ||
"docs", | ||
"feat", | ||
"fix", | ||
"perf", | ||
"refactor", | ||
"release", | ||
"style", | ||
"test", | ||
"chore", | ||
"revert" | ||
], | ||
"scopes": [ | ||
"showcase", | ||
"packaging", | ||
"changelog", | ||
"schematics", | ||
"module:*" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env node | ||
|
||
// from https://github.com/angular/angular/blob/master/scripts/git/commit-msg.js | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const checkMsg = require('./validate-commit-message'); | ||
const msgFile = process.env['HUSKY_GIT_PARAMS']; | ||
|
||
let isValid = true; | ||
if (msgFile || true) { | ||
const commitMsg = fs.readFileSync(msgFile, {encoding: 'utf-8'}); | ||
const firstLine = commitMsg.split('\n')[0]; | ||
isValid = checkMsg(firstLine); | ||
if (!isValid) { | ||
console.error('\x1b[36mCheck CONTRIBUTING.md at the root of the repo for more information.(请查看根目录下的 CONTRIBUTING.md 获取更多信息)\x1b[0m\n'); | ||
} | ||
} | ||
|
||
process.exit(isValid ? 0 : 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#!/usr/bin/env node | ||
|
||
// from https://github.com/angular/angular/blob/master/tools/validate-commit-message/validate-commit-message.js | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const configPath = path.resolve(__dirname, './commit-message.json'); | ||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); | ||
const PATTERN = /^(\w+)(?:\(([^)]+)\))?\: (.+)$/; | ||
const FIXUP_SQUASH = /^(fixup|squash)\! /i; | ||
const REVERT = /^revert:? /i; | ||
|
||
module.exports = function (commitSubject) { | ||
|
||
const subject = commitSubject.replace(FIXUP_SQUASH, ''); | ||
|
||
if (subject.match(REVERT)) { | ||
return true; | ||
} | ||
|
||
if (subject.length > config['maxLength']) { | ||
error(`The commit message is longer than ${config['maxLength']} characters`, commitSubject); | ||
error(`commit 信息不能超过 ${config['maxLength']} 字符`, commitSubject, 'zh'); | ||
return false; | ||
} | ||
|
||
const match = PATTERN.exec(subject); | ||
if (!match) { | ||
error(`The commit message does not match the format of '<type>(<scope>): <subject>' OR '<type>: <subject>'`, commitSubject); | ||
error(`这条 commit 信息格式不正确 '<type>(<scope>): <subject>' 或 '<type>: <subject>`, commitSubject, 'zh'); | ||
return false; | ||
} | ||
|
||
const type = match[1]; | ||
|
||
if (type.toLowerCase() === 'wip') { | ||
error(`wip are not allowed in a commit, you can change this PR title`, commitSubject); | ||
error(`wip 不允许出现在 commit 中,你可以在 PR 中修改它的标题`, commitSubject, 'zh'); | ||
return false; | ||
} | ||
|
||
if (config['types'].indexOf(type) === -1) { | ||
error( | ||
`${type} is not an allowed type.\n => TYPES: ${config['types'].join(', ')}`, commitSubject); | ||
error( | ||
`${type} 是不允许的 type.\n => TYPES: ${config['types'].join(', ')}`, commitSubject, 'zh'); | ||
return false; | ||
} | ||
|
||
const scope = match[2]; | ||
|
||
if (scope && !config['scopes'].includes(scope) && type !== 'release' && !/module:.+/.test(scope)) { | ||
error( | ||
`"${scope}" is not an allowed scope.\n => SCOPES: ${config['scopes'].join(', ')}`, commitSubject); | ||
error( | ||
`"${scope}" 是不允许的 scope.\n => SCOPES: ${config['scopes'].join(', ')}`, commitSubject, 'zh'); | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
function error(errorMessage, commitMessage, lang) { | ||
if (lang === 'zh') { | ||
console.error(`\x1b[33m无效的 COMMIT 信息: "${commitMessage}"\x1b[0m\n\x1b[31m => 错误: ${errorMessage}\x1b[0m\n`); | ||
} else { | ||
console.error(`\x1b[33mINVALID COMMIT MSG: "${commitMessage}"\x1b[0m\n\x1b[31m => ERROR: ${errorMessage}\x1b[0m\n`); | ||
} | ||
} | ||
|
||
module.exports.config = config; |