Skip to content

Commit

Permalink
Support inserting a range of (or all) releases
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Jan 27, 2022
1 parent 4f9dd1a commit 99016ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Options:

#### Notes on `add`

If the (resulting) version is greater than the current version then commits will be taken from the semver-latest tag until HEAD. I.e. documenting a new release before it's git-tagged. If the version matches an existing tag then a release will be inserted at the appriopriate place, populated with commits between that version's tag and the one before it. I.e. documenting a past release after it's git-tagged.
If the (resulting) version is greater than the current version then commits will be taken from the semver-latest tag until HEAD. I.e. documenting a new release before it's git-tagged. If the version matches an existing tag then a release will be inserted at the appriopriate place, populated with commits between that version's tag and the one before it. I.e. documenting a past release after it's git-tagged. If the version equals `0.0.1` or `1.0.0` and zero versions exist, then a [notice](https://common-changelog.org/#23notice) will be inserted (rather than commits) containing the text `:seedling: Initial release.`.

Works best on a linear git history. If `remark-common-changelog` encounters other tags in the commit range (which may happen if releases were made in parallel on other branches) it will stop there and not include further (older) commits.

Expand Down
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function attacher (opts) {

if (fix) {
if (add) {
addRelease(add)
addRelease(add, true)
}

changelog.children.sort(cmpRelease)
Expand Down Expand Up @@ -138,9 +138,29 @@ export default function attacher (opts) {
}
}

function addRelease (add) {
function addRelease (add, asReleaseType) {
if (Array.isArray(add)) {
add.forEach(addRelease)
add.forEach(x => addRelease(x, asReleaseType))
return
} else if (typeof add === 'object' && add !== null) {
// NOTE: experimental and undocumented
const range = { gte: null, lte: null }

for (const k of ['gte', 'lte']) {
if (add[k]) {
range[k] = typeof add[k] === 'string' ? semver.parse(add[k]) : null

if (!range[k]) {
warn('The `' + k + '` option must be a semver-valid version', root, 'add-new-release')
return
}
}
}

const versions = tags.map(t => t.version)
const matches = versions.filter(rangeFilter(range))

addRelease(matches, false)
return
} else if (typeof add !== 'string' || add === '') {
warn('Target must be a non-empty string', root, 'add-new-release')
Expand All @@ -150,7 +170,7 @@ export default function attacher (opts) {
let target = semver.valid(add)
const specificVersion = !!target

if (!target) {
if (!target && asReleaseType) {
let from = currentVersion

// Take version of last release if greater than current version
Expand Down Expand Up @@ -336,6 +356,16 @@ function cmpVersion (a, b) {
return av && bv ? semver.compare(bv, av) : av ? -1 : bv ? 1 : a.localeCompare(b)
}

// TODO: use a binary search
function rangeFilter (range) {
return function filter (v) {
return (
(range.gte == null || semver.gte(v, range.gte)) &&
(range.lte == null || semver.lte(v, range.lte))
)
}
}

function defaultReleaseUrl (githubUrl, tags, version) {
return `${githubUrl}/releases/tag/${forgivingTag(`v${version}`, tags)}`
}
Expand Down

0 comments on commit 99016ab

Please sign in to comment.