强制commit规范常用的有两种,一种是在package项目中的gitHooks选项下的"commit-msg"设置自己的commit规则脚本。比如我要强制commit内容为feat:加空格xxx或者fix:加空格xxx,如果不符合给出相应提示,则我应该这么写:
verify-commit-msg.js中代码:
const chalk = require("chalk");
const msgPath = process.env.GIT_PARAMS;
const msg = require("fs")
.readFileSync(msgPath, "utf-8")
.trim();
const commitRE = /^(revert: )?(feat|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build|Merge)(\(.+\))?: .{1,50}|Merge /;
if (!commitRE.test(msg)) {
console.error(
` ${chalk.bgRed.white(" ERROR ")} ${chalk.red(
`invalid commit message format.`
)}\n\n` +
chalk.red(
` Proper commit message format is required for automated changelog generation. Examples:\n\n`
) +
` ${chalk.green(`feat: add 'comments' option`)}\n` +
` ${chalk.green(`fix: handle events on blur (close #28)`)}\n\n` +
chalk.red(
` You can also use ${chalk.cyan(
`yarn commit`
)} to interactively generate a commit message.\n`
)
);
process.exit(1);
}
另一种是也可以使用git-cz 通过交互的方式生成更专业的commit,效果如下