🌐 AI搜索 & 代理 主页
Skip to content

Commit 2570497

Browse files
committed
chore: filter out commits before last tag when constructing changelog
1 parent b6530b5 commit 2570497

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed

scripts/_utils.ts

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { execSync } from 'node:child_process'
21
import { promises as fsp } from 'node:fs'
32
import { $fetch } from 'ofetch'
43
import { resolve } from 'pathe'
4+
import { compare } from 'semver'
55
import { glob } from 'tinyglobby'
66
import { exec } from 'tinyexec'
77
import { determineSemverChange, getGitDiff, loadChangelogConfig, parseCommits } from 'changelogen'
@@ -99,26 +99,70 @@ export async function loadWorkspace (dir: string) {
9999
}
100100
}
101101

102-
export async function determineBumpType () {
102+
export async function determineBumpType (since?: string) {
103103
const config = await loadChangelogConfig(process.cwd())
104-
const commits = await getLatestCommits()
104+
const commits = await getLatestCommits(since)
105105

106106
return determineSemverChange(commits, config)
107107
}
108108

109-
export async function getLatestCommits () {
110-
const config = await loadChangelogConfig(process.cwd())
109+
export async function getLatestTag () {
111110
const { stdout: latestTag } = await exec('git', ['describe', '--tags', '--abbrev=0'])
111+
return latestTag.trim()
112+
}
113+
114+
export async function getLatestReleasedTag () {
115+
const latestReleasedTag = await exec('git', ['tag', '-l']).then(r => r.stdout.trim().split('\n').filter(t => /v3\.\d+\.\d+/.test(t)).sort(compare)).then(r => r.pop()!.trim())
116+
return latestReleasedTag
117+
}
118+
119+
export async function getPreviousReleasedCommits () {
120+
const config = await loadChangelogConfig(process.cwd())
121+
const latestTag = await getLatestTag()
122+
const latestReleasedTag = await getLatestReleasedTag()
123+
const commits = parseCommits(await getGitDiff(latestTag, latestReleasedTag), config)
124+
return commits
125+
}
126+
127+
export async function getLatestCommits (since?: string) {
128+
const config = await loadChangelogConfig(process.cwd())
129+
const latestTag = await getLatestTag()
130+
131+
// If filtering by date, get commits with git log --since
132+
if (since) {
133+
const { stdout } = await exec('git', ['log', `${latestTag}..HEAD`, '--since', since, '--pretty=format:%H'])
134+
const commitHashes = new Set(stdout.trim().split('\n').filter(Boolean))
135+
136+
const allCommits = parseCommits(await getGitDiff(latestTag), config)
137+
return allCommits.filter((commit) => {
138+
// Match against full hash (shortHash is abbreviated)
139+
return Array.from(commitHashes).some(hash => hash.startsWith(commit.shortHash))
140+
})
141+
}
112142

113-
return parseCommits(await getGitDiff(latestTag.trim()), config)
143+
return parseCommits(await getGitDiff(latestTag), config)
114144
}
115145

116-
export async function getContributors () {
146+
export async function getContributors (since?: string) {
117147
const contributors = [] as Array<{ name: string, username: string }>
118148
const emails = new Set<string>()
119-
const latestTag = execSync('git describe --tags --abbrev=0').toString().trim()
149+
const latestTag = await getLatestTag()
150+
120151
const rawCommits = await getGitDiff(latestTag)
152+
153+
// Get commit hashes filtered by date if specified
154+
let allowedHashes: Set<string> | null = null
155+
if (since) {
156+
const { stdout } = await exec('git', ['log', `${latestTag}..HEAD`, '--since', since, '--pretty=format:%H'])
157+
allowedHashes = new Set(stdout.trim().split('\n').filter(Boolean))
158+
}
159+
121160
for (const commit of rawCommits) {
161+
// Filter by date if specified
162+
if (allowedHashes && !Array.from(allowedHashes).some(hash => hash.startsWith(commit.shortHash))) {
163+
continue
164+
}
165+
122166
if (emails.has(commit.author.email) || commit.author.name === 'renovate[bot]') { continue }
123167
const { author } = await $fetch<{ author: { login: string, email: string } }>(`https://api.github.com/repos/nuxt/nuxt/commits/${commit.shortHash}`, {
124168
headers: {

scripts/bump-nightly.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { execSync } from 'node:child_process'
22
import { inc } from 'semver'
3-
import { determineBumpType, loadWorkspace } from './_utils.ts'
3+
import { determineBumpType, getLatestTag, loadWorkspace } from './_utils.ts'
44

55
const nightlyPackages = {
66
// nitro: 'nitro-nightly',
@@ -15,7 +15,13 @@ export async function bumpNightly () {
1515
const commit = execSync('git rev-parse --short HEAD').toString('utf-8').trim().slice(0, 8)
1616
const date = Math.round(Date.now() / (1000 * 60))
1717

18-
const bumpType = await determineBumpType()
18+
// TODO: revert after release of v4.2.0
19+
// Get the date of the latest tag to filter out merged history commits
20+
const latestTagName = await getLatestTag()
21+
const tagDate = execSync(`git log -1 --format=%ai ${latestTagName}`, { encoding: 'utf-8' })
22+
const sinceDate = tagDate.trim()
23+
24+
const bumpType = await determineBumpType(sinceDate)
1925

2026
for (const pkg of workspace.packages.filter(p => !p.data.private)) {
2127
const newVersion = inc(pkg.data.version, bumpType || 'patch')

scripts/update-changelog.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@ import { $fetch } from 'ofetch'
33
import { inc } from 'semver'
44
import { generateMarkDown, getCurrentGitBranch, loadChangelogConfig } from 'changelogen'
55
import { consola } from 'consola'
6-
import { determineBumpType, getContributors, getLatestCommits, loadWorkspace } from './_utils.ts'
6+
import { determineBumpType, getContributors, getLatestCommits, getLatestReleasedTag, getLatestTag, getPreviousReleasedCommits, loadWorkspace } from './_utils.ts'
7+
8+
const handleSeparateBranch = false
79

810
async function main () {
911
const releaseBranch = getCurrentGitBranch()
1012
const workspace = await loadWorkspace(process.cwd())
1113
const config = await loadChangelogConfig(process.cwd(), {})
1214

13-
const commits = await getLatestCommits().then(commits => commits.filter(
14-
c => config.types[c.type] && !(c.type === 'chore' && c.scope === 'deps'),
15+
const prevMessages = new Set(handleSeparateBranch ? await getPreviousReleasedCommits().then(r => r.map(c => c.message)) : [])
16+
17+
// TODO: revert after release of v4.2.0
18+
// Get the date of the latest tag to filter out merged history commits
19+
const latestTagName = await getLatestTag()
20+
const tagDate = execSync(`git log -1 --format=%ai ${latestTagName}`, { encoding: 'utf-8' })
21+
const sinceDate = tagDate.trim()
22+
23+
const commits = await getLatestCommits(sinceDate).then(commits => commits.filter(
24+
c => config.types[c.type] && !(c.type === 'chore' && c.scope === 'deps') && !prevMessages.has(c.message),
1525
))
16-
const bumpType = await determineBumpType() || 'patch'
26+
const bumpType = await determineBumpType(sinceDate) || 'patch'
1727

1828
const newVersion = inc(workspace.find('nuxt').data.version, bumpType)
1929
const changelog = await generateMarkDown(commits, config)
@@ -36,7 +46,10 @@ async function main () {
3646

3747
// Get the current PR for this release, if it exists
3848
const [currentPR] = await $fetch(`https://api.github.com/repos/nuxt/nuxt/pulls?head=nuxt:v${newVersion}`)
39-
const contributors = await getContributors()
49+
const contributors = await getContributors(sinceDate)
50+
51+
const latestTag = latestTagName
52+
const previousReleasedTag = handleSeparateBranch ? await getLatestReleasedTag() : latestTag
4053

4154
const releaseNotes = [
4255
currentPR?.body.replace(/## 👉 Changelog[\s\S]*$/, '') || `> ${newVersion} is the next ${bumpType} release.\n>\n> **Timetable**: to be announced.`,
@@ -45,7 +58,8 @@ async function main () {
4558
.replace(/^## v.*\n/, '')
4659
.replace(`...${releaseBranch}`, `...v${newVersion}`)
4760
.replace(/### Contributors[\s\S]*$/, '')
48-
.replace(/[\n\r]+/g, '\n'),
61+
.replace(/[\n\r]+/g, '\n')
62+
.replace(latestTag, previousReleasedTag),
4963
'### ❤️ Contributors',
5064
contributors.map(c => `- ${c.name} (@${c.username})`).join('\n'),
5165
].join('\n')

0 commit comments

Comments
 (0)