5
0
Fork 0
mirror of https://github.com/wagoid/commitlint-github-action.git synced 2025-11-07 16:06:56 +00:00
commitlint-github-action/src/gitCommits.js

43 lines
977 B
JavaScript

import dargs from 'dargs'
import execa from 'execa'
const commitDelimiter = '--------->commit---------'
const hashDelimiter = '--------->hash---------'
const format = `%H${hashDelimiter}%B%n${commitDelimiter}`
const buildGitArgs = (gitOpts) => {
const { from, to, ...otherOpts } = gitOpts
const formatArg = `--format=${format}`
const fromToArg = [from, to].filter(Boolean).join('..')
const gitArgs = ['log', formatArg, fromToArg]
return gitArgs.concat(
dargs(gitOpts, {
includes: Object.keys(otherOpts),
}),
)
}
const gitCommits = async (gitOpts) => {
const args = buildGitArgs(gitOpts)
const { stdout } = await execa('git', args, {
cwd: process.cwd(),
})
const commits = stdout.split(`${commitDelimiter}\n`).map((messageItem) => {
const [hash, message] = messageItem.split(hashDelimiter)
return {
hash,
message: message.replace(commitDelimiter, ''),
}
})
return commits
}
export default gitCommits