Skip to content

Commit

Permalink
Do not sort tags when sorting via --append-git-tag
Browse files Browse the repository at this point in the history
Closes #197
Closes #210
  • Loading branch information
cookpete committed Feb 3, 2022
1 parent 9d271fc commit 3e25b8c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const fetchTags = async (options, remote) => {
.split('\n')
.map(parseTag(options))
.filter(isValidTag(options))
.sort(sortTags)
.sort(sortTags(options))

const { latestVersion, unreleased, unreleasedOnly, getCompareLink } = options
if (latestVersion || unreleased || unreleasedOnly) {
Expand Down Expand Up @@ -87,7 +87,10 @@ const isValidTag = ({ tagPattern }) => ({ tag, version }) => {
return semver.valid(version)
}

const sortTags = ({ version: a }, { version: b }) => {
const sortTags = ({ appendGitTag }) => ({ version: a }, { version: b }) => {
if (/--sort/.test(appendGitTag)) {
return 0
}
if (semver.valid(a) && semver.valid(b)) {
return semver.rcompare(a, b)
}
Expand Down
40 changes: 40 additions & 0 deletions test/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,46 @@ describe('fetchTags', () => {
expect(await fetchTags({ ...options, startingDate: '2000-05-01' })).to.have.lengthOf(1)
})

it('sorts tags using semver', async () => {
mock('cmd', () => Promise.resolve([
'0.1.0---2000-02-01',
'0.2.0---2000-03-01',
'0.3.0---2000-04-01',
'0.2.1---2000-03-02',
'0.2.2---2000-03-03',
'1.0.0---2001-01-01'
].join('\n')))
const tags = await fetchTags(options)
expect(tags.map(t => t.title)).to.deep.equal([
'1.0.0',
'0.3.0',
'0.2.2',
'0.2.1',
'0.2.0',
'0.1.0'
])
})

it('does not sort when sorting via --append-git-tag', async () => {
mock('cmd', () => Promise.resolve([
'0.1.0---2000-02-01',
'0.2.0---2000-03-01',
'0.3.0---2000-04-01',
'0.2.1---2000-03-02',
'0.2.2---2000-03-03',
'1.0.0---2001-01-01'
].join('\n')))
const tags = await fetchTags({ ...options, appendGitTag: '--sort=v:refname' })
expect(tags.map(t => t.title)).to.deep.equal([
'0.1.0',
'0.2.0',
'0.3.0',
'0.2.1',
'0.2.2',
'1.0.0'
])
})

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

0 comments on commit 3e25b8c

Please sign in to comment.