Skip to content

Commit

Permalink
chore: add git hook to check commit messages (#2223)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz authored and vthinkxie committed Oct 7, 2018
1 parent fe4fc52 commit e11247c
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ng-zorro-antd-workspace",
"name": "ng-zorro-antd",
"version": "0.0.0-NOT-USED",
"license": "MIT",
"description": "An enterprise-class UI components based on Ant Design and Angular",
Expand Down Expand Up @@ -67,6 +67,7 @@
"core-js": "^2.5.4",
"fs-extra": "^6.0.1",
"hammerjs": "^2.0.8",
"husky": "^1.0.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~1.7.1",
Expand Down Expand Up @@ -100,5 +101,10 @@
"typescript": "~2.9.2",
"yaml-front-matter": "^3.4.0",
"zone.js": "^0.8.26"
},
"husky": {
"hooks": {
"commit-msg": "node ./scripts/git/commit-msg.js -E HUSKY_GIT_PARAMS"
}
}
}
24 changes: 24 additions & 0 deletions scripts/git/commit-message.json
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:*"
]
}
21 changes: 21 additions & 0 deletions scripts/git/commit-msg.js
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);
73 changes: 73 additions & 0 deletions scripts/git/validate-commit-message.js
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;

0 comments on commit e11247c

Please sign in to comment.