git - 自定义 commitlint header 格式

标签 git githooks husky commitlint

我正在使用 Husky 来设置我的 git 钩子(Hook),并且正在尝试更改为 Commitlint 期望的标题的默认格式:

type(scope?): subject

我特别想尝试这种格式:

:gitmoji:? [scope] subject

使用 :gitmoji: Gitmoji 之一的表情符号并且是可选的,用方括号包围范围(而不是可选的)而不是括号,并且没有 : 将类型 + 范围与主题分开。此外,我希望 scope 具有类似于 TCKT-666 的格式(例如引用 Jira 的票证),

现在,我一直在使用 parserPresetparserOptsheaderPatternheaderCorrespondence 尝试很多事情来自 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/

相关文章:

git 无法在 Visual Studio 代码中工作

javascript - 预提交时是否可以使用 ESlint 或其他任何方式禁止单词列表?

git - 获取不带前缀的git分支名称

eslint - 如何使用 Husky 版本 6 进行 lint-staged 工作

git - 我仍在使用旧电子邮件推送我的代码?

git - 调试 git hook 的执行

linux - 无法在 git hook 预推脚本调用的 shell 脚本中获取用户的输入

githooks - 如何让 git 将分支合并回父分支

git - 间歇性 git 错误 : "Cannot rebase: You have unstaged changes."

git - 如何使分支 B 与分支 A 完全相同?