Skip to content

Commit

Permalink
feat(packages): @sa/scripts: add ignore pattern list for command gitC…
Browse files Browse the repository at this point in the history
…ommitVerify
  • Loading branch information
honghuangdc committed Sep 8, 2024
1 parent 1e345aa commit 5a95026
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/scripts/src/commands/git-commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ export async function gitCommit(lang: Lang = 'en-us') {
}

/** Git commit message verify */
export async function gitCommitVerify(lang: Lang = 'en-us') {
export async function gitCommitVerify(lang: Lang = 'en-us', ignores: RegExp[] = []) {
const gitPath = await execCommand('git', ['rev-parse', '--show-toplevel']);

const gitMsgPath = path.join(gitPath, '.git', 'COMMIT_EDITMSG');

const commitMsg = readFileSync(gitMsgPath, 'utf8').trim();

if (ignores.some(regExp => regExp.test(commitMsg))) return;

const REG_EXP = /(?<type>[a-z]+)(?:\((?<scope>.+)\))?(?<breaking>!)?: (?<description>.+)/i;

if (!REG_EXP.test(commitMsg)) {
Expand Down
12 changes: 11 additions & 1 deletion packages/scripts/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ const defaultOptions: CliOption = {
'!node_modules/**'
],
ncuCommandArgs: ['--deep', '-u'],
changelogOptions: {}
changelogOptions: {},
gitCommitVerifyIgnores: [
/^((Merge pull request)|(Merge (.*?) into (.*?)|(Merge branch (.*?)))(?:\r?\n)*$)/m,
/^(Merge tag (.*?))(?:\r?\n)*$/m,
/^(R|r)evert (.*)/,
/^(amend|fixup|squash)!/,
/^(Merged (.*?)(in|into) (.*)|Merged PR (.*): (.*))/,
/^Merge remote-tracking branch(\s*)(.*)/,
/^Automatic merge(.*)/,
/^Auto-merged (.*?) into (.*)/
]
};

export async function loadCliOptions(overrides?: Partial<CliOption>, cwd = process.cwd()) {
Expand Down
4 changes: 2 additions & 2 deletions packages/scripts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export async function setupCli() {
},
'git-commit-verify': {
desc: 'verify git commit message, make sure it match Conventional Commits standard',
action: async () => {
await gitCommitVerify();
action: async args => {
await gitCommitVerify(args?.lang, cliOptions.gitCommitVerifyIgnores);
}
},
changelog: {
Expand Down
2 changes: 2 additions & 0 deletions packages/scripts/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ export interface CliOption {
* @link https://github.com/soybeanjs/changelog
*/
changelogOptions: Partial<ChangelogOption>;
/** The ignore pattern list of git commit verify */
gitCommitVerifyIgnores: RegExp[];
}

0 comments on commit 5a95026

Please sign in to comment.