Skip to content

Commit

Permalink
Merge pull request #79 from 142vip/feat/verifyCommit
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl authored Aug 29, 2024
2 parents 855dca1 + aa29ab2 commit 2447682
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/fairy-cli/src/shared/git.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { execSync } from 'node:child_process'
import process from 'node:process'
import path from 'node:path'
import { readFileSync } from 'node:fs'

/**
* 获取当前分支名
Expand Down Expand Up @@ -46,3 +48,58 @@ export function getCommitLogs(latestTag: string, branch?: string): string[] {
// 整理出git提交日志
return commitLogs.split('\n')
}

export interface CommitValidate {
/**
* 提交信息是否符合Git规范
*/
isSuccess: boolean

/**
* 提交类型
*/
type?: string

/**
* 提交范围
*/
scope?: string

/**
* 提交信息
*/
message?: string

/**
* 提交内容
*/
commit: string
}

/**
* 校验、解析Git 提交信息
* - 规范:
*/
export function verifyCommit(commitRgx?: RegExp): CommitValidate {
// 正则匹配
const regExp = commitRgx ?? /^(?:revert: )?(?:feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(?:\(.+\))?: .{1,80}/

const msgPath = path.resolve('.git/COMMIT_EDITMSG')
const msg = readFileSync(msgPath, 'utf-8').trim()

const isSuccess = regExp.test(msg)

// 解析出type scope message等信息
// eslint-disable-next-line regexp/no-super-linear-backtracking
const regex = /^(?<type>\w+)(?:\((?<scope>[^()]*)\))?:\s*(?<message>.*)$/

const { groups } = msg.match(regex)!

return {
isSuccess,
type: groups?.type,
scope: groups?.scope,
message: groups?.message,
commit: msg,
}
}

0 comments on commit 2447682

Please sign in to comment.