我正在使用 Husky 来设置我的 git 钩子(Hook),并且正在尝试更改为 Commitlint 期望的标题的默认格式:
type(scope?): subject
我特别想尝试这种格式:
:gitmoji:? [scope] subject
使用 :gitmoji:
Gitmoji 之一的表情符号并且是可选的,用方括号包围范围(而不是可选的)而不是括号,并且没有 :
将类型 + 范围与主题分开。此外,我希望 scope
具有类似于 TCKT-666
的格式(例如引用 Jira 的票证),
现在,我一直在使用 parserPreset
、parserOpts
、headerPattern
和 headerCorrespondence
尝试很多事情来自 commitlint.config.js
的 code> 属性,但我遇到了几个问题:
headerPattern
正则表达式似乎被完全忽略了,我得到的所有错误都来 self 在commitlint.config.js
中设置的规则 - 所以我无法设置我的scope
的特定格式(尽管 commitlint-plugin-function-rules 可能对此有帮助)- 我完全不知道如何在类型之后删除对
:
的需要,或者如何用范围内的方括号替换圆括号
最佳答案
这应该适用于 :gitmoji:? [范围] 主题
module.exports = {
parserPreset: {
parserOpts: {
headerPattern: /^(?:(:\w+:)\s)?\[(\w+)\] (.+)/,
headerCorrespondence: ["type", "scope", "subject"],
},
},
plugins: [
{
rules: {
"header-match-team-pattern": (parsed) => {
const { type, scope, subject } = parsed;
if (type === null && scope === null && subject === null) {
return [
false,
"header must be in format ':gitmoji:? [scope] subject'",
];
}
return [true, ""];
},
"gitmoji-type-enum": (parsed, _when, expectedValue) => {
const { type } = parsed;
if (type && !expectedValue.includes(type)) {
return [
false,
`type must be one of ${expectedValue}
see https://gitmoji.dev`,
];
}
return [true, ""];
},
},
},
],
rules: {
// "type-empty": [2, "never"],
"header-match-team-pattern": [2, "always"],
"gitmoji-type-enum": [2, "always", [":bug:", ":sparkle:"]], // custom rule defined in plugins
// "subject-case": [2, "always", "sentence-case"],
},
};
看起来需要有一个自定义规则,例如 header-match-team-pattern
以确保 RegExp 匹配。
关于git - 自定义 commitlint header 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70521236/