Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --sort-tags #210

Closed
wants to merge 12 commits into from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Options:
--ignore-commit-pattern [regex] # pattern to ignore when parsing commits
--tag-pattern [regex] # override regex pattern for version tags
--tag-prefix [prefix] # prefix used in version tags, default: v
--sort-tags [property] # sort commits by semantic versioning or git fields, default: semver
--starting-version [tag] # specify earliest version to include in changelog
--starting-date [yyyy-mm-dd] # specify earliest date to include in changelog
--sort-commits [property] # sort commits by property [relevance, date, date-desc, subject, subject-desc], default: relevance
Expand Down
2 changes: 2 additions & 0 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const DEFAULT_OPTIONS = {
commitLimit: 3,
backfillLimit: 3,
tagPrefix: '',
sortTags: 'semver',
sortCommits: 'relevance',
appendGitLog: '',
appendGitTag: '',
Expand Down Expand Up @@ -44,6 +45,7 @@ const getOptions = async argv => {
.option('--ignore-commit-pattern <regex>', 'pattern to ignore when parsing commits')
.option('--tag-pattern <regex>', 'override regex pattern for version tags')
.option('--tag-prefix <prefix>', 'prefix used in version tags')
.option('--sort-tags <property>', `sort commits by semantic versioning or git fields, default: ${DEFAULT_OPTIONS.sortTags}`)
.option('--starting-version <tag>', 'specify earliest version to include in changelog')
.option('--starting-date <yyyy-mm-dd>', 'specify earliest date to include in changelog')
.option('--sort-commits <property>', `sort commits by property [relevance, date, date-desc], default: ${DEFAULT_OPTIONS.sortCommits}`)
Expand Down
21 changes: 15 additions & 6 deletions src/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ const MATCH_V = /^v\d/

const fetchTags = async (options, remote) => {
const format = `%(refname:short)${DIVIDER}%(creatordate:short)`
const tags = (await cmd(`git tag -l --format=${format} ${options.appendGitTag}`))
.trim()
.split('\n')
.map(parseTag(options))
.filter(isValidTag(options))
.sort(sortTags)
let tags
if (options.sortTags === 'semver') {
tags = (await cmd(`git tag -l --format=${format} ${options.appendGitTag}`))
.trim()
.split('\n')
.map(parseTag(options))
.filter(isValidTag(options))
.sort(sortTags)
} else {
tags = (await cmd(`git tag -l --sort=${options.sortTags} --format=${format} ${options.appendGitTag}`))
.trim()
.split('\n')
.map(parseTag(options))
.filter(isValidTag(options))
}

const { latestVersion, unreleased, unreleasedOnly, getCompareLink } = options
if (latestVersion || unreleased || unreleasedOnly) {
Expand Down
34 changes: 34 additions & 0 deletions test/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {

const options = {
tagPrefix: '',
sortTags: 'semver',
...remotes.github
}

Expand Down Expand Up @@ -113,6 +114,39 @@ describe('fetchTags', () => {
expect(await fetchTags({ ...options, startingDate: '2000-05-01' })).to.have.lengthOf(1)
})

it('supports --sort-tags=semver', async () => {
const tags = await fetchTags({ ...options, sortTags: 'semver' })
expect(tags.map(t => t.version)).to.deep.equal([
'v1.0.0',
'v0.3.0',
'v0.2.2',
'v0.2.1',
'v0.2.0',
'v0.1.0'
])
})

// with --sort-tags, we shouldn't sort by semver, keep git command's order.
it('supports --sort-tags', async () => {
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved
mock('cmd', () => Promise.resolve([
'v0.1.0---2000-02-01',
'v0.2.0---2000-03-01',
'v0.2.1---2000-03-02',
'v1.0.0---2000-03-03',
'v0.2.2---2000-04-01',
'v0.3.0---2001-01-01'
].join('\n')))
const tags = await fetchTags({ ...options, sortTags: 'creatordate' })
expect(tags.map(t => t.version)).to.deep.equal([
'v0.1.0',
'v0.2.0',
'v0.2.1',
'v1.0.0',
'v0.2.2',
'v0.3.0'
])
})

it('supports partial semver tags', async () => {
mock('cmd', () => Promise.resolve([
'v0.1---2000-02-01',
Expand Down