Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customise commitlint header format

I am using Husky to set my git hooks, and am trying to change to default format of the header expected by Commitlint:

type(scope?): subject

I am specifically trying to have this formatting:

:gitmoji:? [scope] subject

With :gitmoji: one of Gitmoji's emoji and being optional, with square brackets around the scope (and not optional) instead of the parentheses, and without the : to separate the type + the scope from the subject. Also I'd like the scope to have a formatting kind of like TCKT-666 (to refer a Jira's ticket, for example),

Right now, I've been trying a lot of things using the parserPreset, parserOpts, headerPattern and headerCorrespondence properties from commitlint.config.js, but I encountered several issues:

  • the headerPattern regex seems to be totally ignored, and all the errors I get only come from the rules I set within commitlint.config.js - so I cannot set a specific formatting for my scope (although commitlint-plugin-function-rules might help with that)
  • I have absolutely no idea how to remove the need for the : after the type, or how to replace parentheses by square brackets around the scope
like image 782
Thanh-Quy Nguyen Avatar asked Dec 16 '25 21:12

Thanh-Quy Nguyen


1 Answers

This should work for :gitmoji:? [scope] subject

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"],
  },
};

Looks like it's required to have a custom rule like header-match-team-pattern that makes sure that RegExp matched.

like image 129
strdr4605 Avatar answered Dec 19 '25 12:12

strdr4605



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!