From 9dd87bebb63c4b8bc65b4560727dea8f4548a53b Mon Sep 17 00:00:00 2001 From: Eli Perelman Date: Tue, 31 Oct 2017 21:35:50 -0500 Subject: [PATCH 1/2] Support modifying commit limit with --commit-limit --- README.md | 18 +++++++++++++----- src/releases.js | 4 ++-- src/run.js | 5 ++++- src/template.js | 10 ---------- templates/compact.hbs | 4 ++-- templates/keepachangelog.hbs | 4 ++-- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6df7cf38..b63520f4 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ If you are upgrading from `0.x`, the same options are still supported out of the - The default template is now `compact` - If you still want to use the [`keepachangelog`](http://keepachangelog.com) format, use `--template keepachangelog` - Templates now use `-` instead of `*` for lists -- Up to 3 commits are now shown per release +- Up to 3 commits are now shown per release by default - Unreleased changes are no longer listed by default, use `--unreleased` to include them - [GitLab](https://gitlab.com) and [BitBucket](https://bitbucket.org) are now fully supported @@ -43,6 +43,7 @@ Options: -r, --remote [remote] # specify git remote to use for links, default: origin -p, --package # use version from package.json as latest release -u, --unreleased # include section for unreleased changes + -l, --commit-limit [count] # modify the limit of rendered commits per release, default: 3 -V, --version # output the version number -h, --help # output usage information @@ -58,6 +59,12 @@ auto-changelog --template keepachangelog # Write log using custom handlebars template in current directory auto-changelog --template my-custom-template.hbs + +# Change rendered commit limit to 5 +auto-changelog --commit-limit 5 + +# Disable the commit limit, rendering all commits +auto-changelog --commit-limit false ``` You can also set options in `package.json`: @@ -72,7 +79,8 @@ You can also set options in `package.json`: "auto-changelog": { "output": "HISTORY.md", "template": "keepachangelog", - "unreleased": true + "unreleased": true, + "commitLimit": false } } ``` @@ -97,7 +105,7 @@ Add `auto-changelog -p; git add CHANGELOG.md` to the `version` scripts in your ` "auto-changelog": "*" }, "scripts": { - "version": "auto-changelog -p; git add CHANGELOG.md" + "version": "auto-changelog -p && git add CHANGELOG.md" } } ``` @@ -126,9 +134,9 @@ My custom changelog template. Don’t worry about indentation here; it is automa {{#each fixes}} - Each fix has a {{commit}} with a {{commit.subject}}, an {{id}} and a {{href}} to the fixed issue. {{/each}} - {{#limit commits limit="5"}} + {{#each commits}} - Commits have a {{shorthash}}, a {{subject}} and a {{href}}, amongst other things. - {{/limit}} + {{/each}} {{/each}} ``` diff --git a/src/releases.js b/src/releases.js index 4ada4df9..e586e444 100644 --- a/src/releases.js +++ b/src/releases.js @@ -2,7 +2,7 @@ import semver from 'semver' import { niceDate } from './utils' -export function parseReleases (commits, origin, packageVersion, includeUnreleased) { +export function parseReleases (commits, origin, packageVersion, includeUnreleased, commitLimit) { let release = newRelease(packageVersion) const releases = [] for (let commit of commits) { @@ -23,7 +23,7 @@ export function parseReleases (commits, origin, packageVersion, includeUnrelease fixes: commit.fixes, commit }) - } else if (filterCommit(commit, release.merges)) { + } else if (filterCommit(commit, release.merges) && (!commitLimit || release.commits.length < commitLimit)) { release.commits.push(commit) } } diff --git a/src/run.js b/src/run.js index bcd82020..6e7c3064 100644 --- a/src/run.js +++ b/src/run.js @@ -10,6 +10,7 @@ import { compileTemplate } from './template' const DEFAULT_OUTPUT = 'CHANGELOG.md' const DEFAULT_TEMPLATE = 'compact' const DEFAULT_REMOTE = 'origin' +const DEFAULT_COMMIT_LIMIT = 3 const NPM_VERSION_TAG_PREFIX = 'v' const PACKAGE_OPTIONS_KEY = 'auto-changelog' @@ -20,6 +21,7 @@ function getOptions (argv, pkg) { .option('-r, --remote [remote]', `specify git remote to use for links, default: ${DEFAULT_REMOTE}`, DEFAULT_REMOTE) .option('-p, --package', 'use version from package.json as latest release') .option('-u, --unreleased', 'include section for unreleased changes') + .option('-l, --commit-limit [count]', `modify the limit of rendered commits per release, default: ${DEFAULT_COMMIT_LIMIT}`, DEFAULT_COMMIT_LIMIT) .version(version) .parse(argv) @@ -41,7 +43,8 @@ export default async function run (argv) { const origin = await fetchOrigin(options.remote) const commits = await fetchCommits(origin) const packageVersion = options.package ? NPM_VERSION_TAG_PREFIX + pkg.version : null - const releases = parseReleases(commits, origin, packageVersion, options.unreleased) + const commitLimit = options.commitLimit === 'false' ? false : parseInt(options.commitLimit, 10) + const releases = parseReleases(commits, origin, packageVersion, options.unreleased, commitLimit) const log = await compileTemplate(options.template, { releases }) await writeFile(options.output, log) return `${Buffer.byteLength(log, 'utf8')} bytes written to ${options.output}` diff --git a/src/template.js b/src/template.js index 1f778fda..5ddc8932 100644 --- a/src/template.js +++ b/src/template.js @@ -6,16 +6,6 @@ import { removeIndentation } from './utils' const TEMPLATES_DIR = join(__dirname, '..', 'templates') -Handlebars.registerHelper('limit', function (context, block) { - let string = '' - for (let i = 0; i !== parseInt(block.hash.limit); i++) { - if (context[i]) { - string += block.fn(context[i]) - } - } - return string -}) - Handlebars.registerHelper('json', function (object) { return new Handlebars.SafeString(JSON.stringify(object, null, 2)) }) diff --git a/templates/compact.hbs b/templates/compact.hbs index 369b2de5..6dac398f 100644 --- a/templates/compact.hbs +++ b/templates/compact.hbs @@ -16,8 +16,8 @@ Generated by [auto-changelog](https://github.com/CookPete/auto-changelog). {{#each fixes}} - {{commit.subject}}{{#each fixes}} [`#{{id}}`]({{href}}){{/each}} {{/each}} - {{#limit commits limit="3"}} + {{#each commits}} - {{subject}} [`{{shorthash}}`]({{href}}) - {{/limit}} + {{/each}} {{/each}} diff --git a/templates/keepachangelog.hbs b/templates/keepachangelog.hbs index ec9930d4..c86e9650 100644 --- a/templates/keepachangelog.hbs +++ b/templates/keepachangelog.hbs @@ -28,9 +28,9 @@ Generated by [auto-changelog](https://github.com/CookPete/auto-changelog). {{/if}} {{#if commits}} ### Commits - {{#limit commits limit="3"}} + {{#each commits}} - {{subject}} [`{{shorthash}}`]({{href}}) - {{/limit}} + {{/each}} {{/if}} {{/each}} From f864ad2a16f277c3c0c0a9b3d8c6745df5bdf686 Mon Sep 17 00:00:00 2001 From: Pete Cook Date: Tue, 7 Nov 2017 13:11:56 +0000 Subject: [PATCH 2/2] Tidy up commit limit logic --- README.md | 2 +- src/releases.js | 15 +++++++++------ src/run.js | 6 +++--- src/utils.js | 4 ++++ test/releases.js | 9 +++++++-- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b63520f4..501f2646 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Options: -r, --remote [remote] # specify git remote to use for links, default: origin -p, --package # use version from package.json as latest release -u, --unreleased # include section for unreleased changes - -l, --commit-limit [count] # modify the limit of rendered commits per release, default: 3 + -l, --commit-limit [count] # number of commits to display per release, default: 3 -V, --version # output the version number -h, --help # output usage information diff --git a/src/releases.js b/src/releases.js index e586e444..1414ca36 100644 --- a/src/releases.js +++ b/src/releases.js @@ -2,12 +2,12 @@ import semver from 'semver' import { niceDate } from './utils' -export function parseReleases (commits, origin, packageVersion, includeUnreleased, commitLimit) { +export function parseReleases (commits, origin, packageVersion, options) { let release = newRelease(packageVersion) const releases = [] for (let commit of commits) { if (commit.tag && semver.valid(commit.tag)) { - if (release.tag || includeUnreleased) { + if (release.tag || options.unreleased) { releases.push({ ...release, href: getCompareLink(commit.tag, release.tag || 'HEAD', origin), @@ -23,7 +23,7 @@ export function parseReleases (commits, origin, packageVersion, includeUnrelease fixes: commit.fixes, commit }) - } else if (filterCommit(commit, release.merges) && (!commitLimit || release.commits.length < commitLimit)) { + } else if (filterCommit(commit, release, options.commitLimit)) { release.commits.push(commit) } } @@ -45,16 +45,19 @@ function newRelease (tag = null, date = new Date().toISOString()) { return release } -function filterCommit (commit, merges) { +function filterCommit (commit, release, limit) { if (semver.valid(commit.subject)) { // Filter out version commits return false } - if (merges.findIndex(m => m.message === commit.subject) !== -1) { + if (release.merges.findIndex(m => m.message === commit.subject) !== -1) { // Filter out commits with the same message as an existing merge return false } - return true + if (limit === false) { + return true + } + return release.commits.length < limit } function getCompareLink (from, to, origin) { diff --git a/src/run.js b/src/run.js index 6e7c3064..645b9315 100644 --- a/src/run.js +++ b/src/run.js @@ -6,6 +6,7 @@ import { fetchOrigin } from './origin' import { fetchCommits } from './commits' import { parseReleases } from './releases' import { compileTemplate } from './template' +import { parseLimit } from './utils' const DEFAULT_OUTPUT = 'CHANGELOG.md' const DEFAULT_TEMPLATE = 'compact' @@ -21,7 +22,7 @@ function getOptions (argv, pkg) { .option('-r, --remote [remote]', `specify git remote to use for links, default: ${DEFAULT_REMOTE}`, DEFAULT_REMOTE) .option('-p, --package', 'use version from package.json as latest release') .option('-u, --unreleased', 'include section for unreleased changes') - .option('-l, --commit-limit [count]', `modify the limit of rendered commits per release, default: ${DEFAULT_COMMIT_LIMIT}`, DEFAULT_COMMIT_LIMIT) + .option('-l, --commit-limit [count]', `number of commits to display per release, default: ${DEFAULT_COMMIT_LIMIT}`, parseLimit, DEFAULT_COMMIT_LIMIT) .version(version) .parse(argv) @@ -43,8 +44,7 @@ export default async function run (argv) { const origin = await fetchOrigin(options.remote) const commits = await fetchCommits(origin) const packageVersion = options.package ? NPM_VERSION_TAG_PREFIX + pkg.version : null - const commitLimit = options.commitLimit === 'false' ? false : parseInt(options.commitLimit, 10) - const releases = parseReleases(commits, origin, packageVersion, options.unreleased, commitLimit) + const releases = parseReleases(commits, origin, packageVersion, options) const log = await compileTemplate(options.template, { releases }) await writeFile(options.output, log) return `${Buffer.byteLength(log, 'utf8')} bytes written to ${options.output}` diff --git a/src/utils.js b/src/utils.js index 9cf96ee4..4a80ccca 100644 --- a/src/utils.js +++ b/src/utils.js @@ -32,3 +32,7 @@ export function removeIndentation (string) { export function isLink (string) { return /^http/.test(string) } + +export function parseLimit (limit) { + return limit === 'false' ? false : parseInt(limit, 10) +} diff --git a/test/releases.js b/test/releases.js index 1caba53c..986061f3 100644 --- a/test/releases.js +++ b/test/releases.js @@ -6,13 +6,18 @@ import commits from './data/commits' import releases from './data/releases' import { parseReleases } from '../src/releases' +const options = { + unreleased: false, + commitLimit: 3 +} + describe('parseReleases', () => { it('parses releases', () => { - expect(parseReleases(commits, origins.github, null, false)).to.deep.equal(releases) + expect(parseReleases(commits, origins.github, null, options)).to.deep.equal(releases) }) it('supports a package version override', () => { - const result = parseReleases(commits, origins.github, 'v3.0.0', false) + const result = parseReleases(commits, origins.github, 'v3.0.0', options) expect(result).to.be.an('array') expect(result[0]).to.have.property('tag', 'v3.0.0') })