Skip to content

Commit

Permalink
Support modifying commit limit with --commit-limit (#12)
Browse files Browse the repository at this point in the history
* Support modifying commit limit with --commit-limit

* Tidy up commit limit logic
  • Loading branch information
eliperelman authored and cookpete committed Nov 9, 2017
1 parent ddcffe0 commit f970076
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 28 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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] # number of commits to display per release, default: 3
-V, --version # output the version number
-h, --help # output usage information

Expand All @@ -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`:
Expand All @@ -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
}
}
```
Expand All @@ -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"
}
}
```
Expand Down Expand Up @@ -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}}
```

Expand Down
15 changes: 9 additions & 6 deletions src/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import semver from 'semver'

import { niceDate } from './utils'

export function parseReleases (commits, origin, packageVersion, includeUnreleased) {
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),
Expand All @@ -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, options.commitLimit)) {
release.commits.push(commit)
}
}
Expand All @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ 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'
const DEFAULT_REMOTE = 'origin'
const DEFAULT_COMMIT_LIMIT = 3
const NPM_VERSION_TAG_PREFIX = 'v'
const PACKAGE_OPTIONS_KEY = 'auto-changelog'

Expand All @@ -20,6 +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]', `number of commits to display per release, default: ${DEFAULT_COMMIT_LIMIT}`, parseLimit, DEFAULT_COMMIT_LIMIT)
.version(version)
.parse(argv)

Expand All @@ -41,7 +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 releases = parseReleases(commits, origin, packageVersion, options.unreleased)
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}`
Expand Down
10 changes: 0 additions & 10 deletions src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
4 changes: 4 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions templates/compact.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
4 changes: 2 additions & 2 deletions templates/keepachangelog.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
9 changes: 7 additions & 2 deletions test/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down

0 comments on commit f970076

Please sign in to comment.