Skip to content

Commit a0241c4

Browse files
authored
feat(plugin-git): improve commits parsing and optimize plugins (#292)
1 parent a7a792a commit a0241c4

File tree

9 files changed

+69
-11
lines changed

9 files changed

+69
-11
lines changed

docs/plugins/development/git.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ This plugin will significantly slow down the speed of data preparation, especial
107107
*/
108108
avatar?: boolean
109109

110+
/**
111+
* Avatar url pattern
112+
* - `:username` - Contributor's username
113+
*
114+
* @example 'https://github.com/:username'
115+
*/
116+
avatarPattern?: string
117+
110118
/**
111119
* Functions to transform contributors, e.g. remove duplicates ones and sort them.
112120
* The input is the contributors collected by this plugin, and the output should be the transformed contributors.
@@ -123,7 +131,7 @@ This plugin will significantly slow down the speed of data preparation, especial
123131

124132
### changelog
125133

126-
- Type: `false | ChangelogOptions`
134+
- Type: `boolean | ChangelogOptions`
127135

128136
```ts
129137
interface ChangelogOptions {
@@ -264,8 +272,11 @@ export default {
264272

265273
```ts
266274
interface GitContributor {
275+
// display name
267276
name: string
268277
email: string
278+
// username on the git hosting service
279+
username: string
269280
commits: number
270281
avatar?: string
271282
url?: string

docs/zh/plugins/development/git.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ export default {
8282
* 如果 git 托管服务为 `github`,则可以忽略不填,由插件自动填充
8383
*/
8484
avatar?: string
85+
/**
86+
* 头像访问地址模式
87+
* - `:username` - 贡献者的用户名
88+
*
89+
* @example 'https://github.com/:username'
90+
*/
91+
avatarPattern?: string
8592
/**
8693
* 贡献者访问地址
8794
* 如果 git 托管服务为 `github`,则可以忽略不填,由插件自动填充
@@ -117,7 +124,7 @@ export default {
117124

118125
### changelog
119126

120-
- 类型: `false | ChangelogOptions`
127+
- 类型: `boolean | ChangelogOptions`
121128

122129
```ts
123130
interface ChangelogOptions {
@@ -258,8 +265,11 @@ export default {
258265

259266
```ts
260267
interface GitContributor {
268+
// 在页面中显示的贡献者名称
261269
name: string
262270
email: string
271+
// 在 git 托管服务中的用户名
272+
username: string
263273
commits: number
264274
avatar?: string
265275
url?: string

plugins/development/plugin-git/src/node/gitPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export const gitPlugin =
5858
),
5959
]
6060

61-
const commits = await getCommits(filePaths, cwd)
61+
const commits = await getCommits(filePaths, cwd, {
62+
contributors: (frontmatter.contributors ?? contributors) !== false,
63+
changelog: frontmatter.changelog ?? changelog,
64+
})
6265

6366
if (commits.length === 0) return
6467

plugins/development/plugin-git/src/node/options.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ export interface ContributorsOptions {
7070
*/
7171
avatar?: boolean
7272

73+
/**
74+
* Avatar url pattern
75+
* - `:username` - Contributor's username
76+
*
77+
* 头像访问地址模式
78+
* - `:username` - 贡献者的用户名
79+
*
80+
* @example 'https://github.com/:username'
81+
*/
82+
avatarPattern?: string
83+
7384
/**
7485
* Functions to transform contributors, e.g. remove duplicates ones and sort them
7586
*
@@ -173,7 +184,7 @@ export interface GitPluginOptions {
173184
*
174185
* 是否收集页面的变更历史记录
175186
*/
176-
changelog?: ChangelogOptions | false
187+
changelog?: ChangelogOptions | boolean
177188

178189
/**
179190
* @deprecated use `contributors.transform` instead

plugins/development/plugin-git/src/node/resolveChangelog.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const parseTagName = (refs: string): string | undefined => {
6363
.split(',')
6464
.map((tag) => tag.trim())
6565

66-
return tags[0]?.replace('tag:', '').trim()
66+
return tags[0]?.includes('tag:') ? tags[0].replace('tag:', '').trim() : ''
6767
}
6868

6969
export const resolveChangelog = (

plugins/development/plugin-git/src/node/resolveContributors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ export const getRawContributors = (
3838
} else {
3939
const item: GitContributor = {
4040
name,
41+
username,
4142
email,
4243
commits: 1,
4344
}
4445

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

98100
const result: GitContributor = {
99101
name: contributorInfo.name ?? extraContributor,
102+
username: contributorInfo.name ?? extraContributor,
100103
email: '',
101104
commits: 0,
102105
}
@@ -110,6 +113,10 @@ export const resolveContributors = (
110113
if (options.avatar)
111114
result.avatar =
112115
contributorInfo.avatar ??
116+
options.avatarPattern?.replace(
117+
':username',
118+
contributorInfo.username,
119+
) ??
113120
(gitProvider === 'github'
114121
? `https://avatars.githubusercontent.com/${contributorInfo.username}?v=4`
115122
: `https://gravatar.com/avatar/${digestSHA256(contributorInfo.username)}?d=retro`)

plugins/development/plugin-git/src/node/typings.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,18 @@ export interface MergedRawCommit extends Omit<RawCommit, 'filepath'> {
5757

5858
export interface GitContributor {
5959
/**
60-
* Contributor name
60+
* Contributor display name
6161
*/
6262
name: string
6363
/**
6464
* Contributor email
6565
*/
6666
email: string
67+
68+
/**
69+
* Contributor username on the git hosting service
70+
*/
71+
username: string
6772
/**
6873
* Number of commits
6974
*/

plugins/development/plugin-git/src/node/utils/digestSHA256.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { createHash } from 'node:crypto'
22

3-
const hash = createHash('sha256')
4-
53
export const digestSHA256 = (message: string): string => {
4+
const hash = createHash('sha256')
65
hash.update(message)
76

87
return hash.digest('hex')

plugins/development/plugin-git/src/node/utils/getCommits.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execa } from 'execa'
2+
import type { GitPluginOptions } from '../options.js'
23
import type { GitContributor, MergedRawCommit, RawCommit } from '../typings.js'
34

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

@@ -20,6 +20,15 @@ const getCoAuthors = (
2020
.filter(Boolean)
2121
}
2222

23+
const getFormat = ({ contributors, changelog }: GitPluginOptions): string => {
24+
// hash | _ | _ | author_date | _ | _ | _
25+
if (!contributors && !changelog) return '%H|||%ad|||'
26+
// hash | author_name | author_email | author_date | _ | _ | body
27+
if (contributors && !changelog) return '%H|%an|%ae|%ad|||%b'
28+
// hash | author_name | author_email | author_date | subject | ref | body
29+
return '%H|%an|%ae|%ad|%s|%d|%b'
30+
}
31+
2332
/**
2433
* Get raw commits
2534
*
@@ -30,14 +39,16 @@ const getCoAuthors = (
3039
export const getRawCommits = async (
3140
filepath: string,
3241
cwd: string,
42+
options: GitPluginOptions,
3343
): Promise<RawCommit[]> => {
44+
const format = getFormat(options)
3445
try {
3546
const { stdout } = await execa(
3647
'git',
3748
[
3849
'log',
3950
'--max-count=-1',
40-
`--format=${FORMAT}${SPLIT_CHAR}`,
51+
`--format=${format}${SPLIT_CHAR}`,
4152
'--date=unix',
4253
'--follow',
4354
'--',
@@ -87,9 +98,10 @@ export const mergeRawCommits = (commits: RawCommit[]): MergedRawCommit[] => {
8798
export const getCommits = async (
8899
filepaths: string[],
89100
cwd: string,
101+
options: GitPluginOptions,
90102
): Promise<MergedRawCommit[]> => {
91103
const rawCommits = await Promise.all(
92-
filepaths.map((filepath) => getRawCommits(filepath, cwd)),
104+
filepaths.map((filepath) => getRawCommits(filepath, cwd, options)),
93105
)
94106

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

0 commit comments

Comments
 (0)