Skip to content

Commit

Permalink
feat(plugin-git): improve commits parsing and optimize plugins (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo authored Nov 27, 2024
1 parent a7a792a commit a0241c4
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 11 deletions.
13 changes: 12 additions & 1 deletion docs/plugins/development/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ This plugin will significantly slow down the speed of data preparation, especial
*/
avatar?: boolean

/**
* Avatar url pattern
* - `:username` - Contributor's username
*
* @example 'https://github.com/:username'
*/
avatarPattern?: string

/**
* Functions to transform contributors, e.g. remove duplicates ones and sort them.
* The input is the contributors collected by this plugin, and the output should be the transformed contributors.
Expand All @@ -123,7 +131,7 @@ This plugin will significantly slow down the speed of data preparation, especial

### changelog

- Type: `false | ChangelogOptions`
- Type: `boolean | ChangelogOptions`

```ts
interface ChangelogOptions {
Expand Down Expand Up @@ -264,8 +272,11 @@ export default {

```ts
interface GitContributor {
// display name
name: string
email: string
// username on the git hosting service
username: string
commits: number
avatar?: string
url?: string
Expand Down
12 changes: 11 additions & 1 deletion docs/zh/plugins/development/git.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export default {
* 如果 git 托管服务为 `github`,则可以忽略不填,由插件自动填充
*/
avatar?: string
/**
* 头像访问地址模式
* - `:username` - 贡献者的用户名
*
* @example 'https://github.com/:username'
*/
avatarPattern?: string
/**
* 贡献者访问地址
* 如果 git 托管服务为 `github`,则可以忽略不填,由插件自动填充
Expand Down Expand Up @@ -117,7 +124,7 @@ export default {

### changelog

- 类型: `false | ChangelogOptions`
- 类型: `boolean | ChangelogOptions`

```ts
interface ChangelogOptions {
Expand Down Expand Up @@ -258,8 +265,11 @@ export default {

```ts
interface GitContributor {
// 在页面中显示的贡献者名称
name: string
email: string
// 在 git 托管服务中的用户名
username: string
commits: number
avatar?: string
url?: string
Expand Down
5 changes: 4 additions & 1 deletion plugins/development/plugin-git/src/node/gitPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export const gitPlugin =
),
]

const commits = await getCommits(filePaths, cwd)
const commits = await getCommits(filePaths, cwd, {
contributors: (frontmatter.contributors ?? contributors) !== false,
changelog: frontmatter.changelog ?? changelog,
})

if (commits.length === 0) return

Expand Down
13 changes: 12 additions & 1 deletion plugins/development/plugin-git/src/node/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ export interface ContributorsOptions {
*/
avatar?: boolean

/**
* Avatar url pattern
* - `:username` - Contributor's username
*
* 头像访问地址模式
* - `:username` - 贡献者的用户名
*
* @example 'https://github.com/:username'
*/
avatarPattern?: string

/**
* Functions to transform contributors, e.g. remove duplicates ones and sort them
*
Expand Down Expand Up @@ -173,7 +184,7 @@ export interface GitPluginOptions {
*
* 是否收集页面的变更历史记录
*/
changelog?: ChangelogOptions | false
changelog?: ChangelogOptions | boolean

/**
* @deprecated use `contributors.transform` instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const parseTagName = (refs: string): string | undefined => {
.split(',')
.map((tag) => tag.trim())

return tags[0]?.replace('tag:', '').trim()
return tags[0]?.includes('tag:') ? tags[0].replace('tag:', '').trim() : ''
}

export const resolveChangelog = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ export const getRawContributors = (
} else {
const item: GitContributor = {
name,
username,
email,
commits: 1,
}

if (options.avatar)
item.avatar =
config?.avatar ??
options.avatarPattern?.replace(':username', username) ??
(gitProvider === 'github'
? `https://avatars.githubusercontent.com/${username}?v=4`
: `https://gravatar.com/avatar/${digestSHA256(email || username)}?d=retro`)
Expand Down Expand Up @@ -97,6 +99,7 @@ export const resolveContributors = (

const result: GitContributor = {
name: contributorInfo.name ?? extraContributor,
username: contributorInfo.name ?? extraContributor,
email: '',
commits: 0,
}
Expand All @@ -110,6 +113,10 @@ export const resolveContributors = (
if (options.avatar)
result.avatar =
contributorInfo.avatar ??
options.avatarPattern?.replace(
':username',
contributorInfo.username,
) ??
(gitProvider === 'github'
? `https://avatars.githubusercontent.com/${contributorInfo.username}?v=4`
: `https://gravatar.com/avatar/${digestSHA256(contributorInfo.username)}?d=retro`)
Expand Down
7 changes: 6 additions & 1 deletion plugins/development/plugin-git/src/node/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ export interface MergedRawCommit extends Omit<RawCommit, 'filepath'> {

export interface GitContributor {
/**
* Contributor name
* Contributor display name
*/
name: string
/**
* Contributor email
*/
email: string

/**
* Contributor username on the git hosting service
*/
username: string
/**
* Number of commits
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createHash } from 'node:crypto'

const hash = createHash('sha256')

export const digestSHA256 = (message: string): string => {
const hash = createHash('sha256')
hash.update(message)

return hash.digest('hex')
Expand Down
18 changes: 15 additions & 3 deletions plugins/development/plugin-git/src/node/utils/getCommits.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execa } from 'execa'
import type { GitPluginOptions } from '../options.js'
import type { GitContributor, MergedRawCommit, RawCommit } from '../typings.js'

const FORMAT = '%H|%an|%ae|%ad|%s|%d|%b'
const SPLIT_CHAR = '[GIT_LOG_COMMIT_END]'
const RE_SPLIT = /\[GIT_LOG_COMMIT_END\]$/

Expand All @@ -20,6 +20,15 @@ const getCoAuthors = (
.filter(Boolean)
}

const getFormat = ({ contributors, changelog }: GitPluginOptions): string => {
// hash | _ | _ | author_date | _ | _ | _
if (!contributors && !changelog) return '%H|||%ad|||'
// hash | author_name | author_email | author_date | _ | _ | body
if (contributors && !changelog) return '%H|%an|%ae|%ad|||%b'
// hash | author_name | author_email | author_date | subject | ref | body
return '%H|%an|%ae|%ad|%s|%d|%b'
}

/**
* Get raw commits
*
Expand All @@ -30,14 +39,16 @@ const getCoAuthors = (
export const getRawCommits = async (
filepath: string,
cwd: string,
options: GitPluginOptions,
): Promise<RawCommit[]> => {
const format = getFormat(options)
try {
const { stdout } = await execa(
'git',
[
'log',
'--max-count=-1',
`--format=${FORMAT}${SPLIT_CHAR}`,
`--format=${format}${SPLIT_CHAR}`,
'--date=unix',
'--follow',
'--',
Expand Down Expand Up @@ -87,9 +98,10 @@ export const mergeRawCommits = (commits: RawCommit[]): MergedRawCommit[] => {
export const getCommits = async (
filepaths: string[],
cwd: string,
options: GitPluginOptions,
): Promise<MergedRawCommit[]> => {
const rawCommits = await Promise.all(
filepaths.map((filepath) => getRawCommits(filepath, cwd)),
filepaths.map((filepath) => getRawCommits(filepath, cwd, options)),
)

return mergeRawCommits(rawCommits.flat()).sort((a, b) =>
Expand Down

0 comments on commit a0241c4

Please sign in to comment.