Skip to content

Commit

Permalink
Support modifying commit limit with --commit-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
eliperelman committed Nov 8, 2017
1 parent 0c5a093 commit 9dd87be
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 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] # modify the limit of rendered commits 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
4 changes: 2 additions & 2 deletions src/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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.merges) && (!commitLimit || release.commits.length < commitLimit)) {
release.commits.push(commit)
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)

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

0 comments on commit 9dd87be

Please sign in to comment.